admin_url( 'admin-ajax.php' ) ) ); } endif; add_action( 'admin_enqueue_scripts', "firsttool_enqueue_scripts" ); if ( ! function_exists( 'firsttool_product_sync' ) ): /** * Add Admin Menu Page for 1Tool API Configuration * * This page is used to configure API credentials that are being used for * connecting to 1Tool APIs. It also has feature to sync products manually * from 1Tool account. * * @since 1.0.0 * */ function firsttool_product_sync() { add_menu_page( __( '1Tool API Settings', '1tool-to-wc-integration' ), __( '1Tool API Settings', '1tool-to-wc-integration' ), 'manage_options', 'firsttool_settings', 'firsttool_api_setting', 'dashicons-admin-generic', ); } endif; add_action( 'admin_menu', 'firsttool_product_sync' ); if ( ! function_exists( 'firsttool_api_setting' ) ): /** * Manages settings form and process it's value to save them in db * * Prepare UI for setting page and save the submitted value in WP Options table * to use them later for API call. * * @since 1.0.0 * */ function firsttool_api_setting() { if ( isset($_POST['km_submit_btn']) ) { $km_applicationSecret = sanitize_text_field( $_POST['km_applicationSecret'] ); update_option( 'km_applicationSecret', $km_applicationSecret ); $km_applicationId = sanitize_text_field( $_POST['km_applicationId'] ); update_option( 'km_applicationId', $km_applicationId ); $km_is_create_invoice = isset( $_POST['km_is_create_invoice'] ) ? sanitize_text_field( $_POST['km_is_create_invoice'] ) : ""; update_option( 'km_is_create_invoice', $km_is_create_invoice ); $km_api = new FIRSTTOOL_API\FirstTool_API; /* get accessToken */ $km_res = $km_api->km_post_request( 'auth/application', array( 'applicationId' => $km_applicationId, 'applicationSecret' => $km_applicationSecret ) ); if ( isset( $km_res["accessToken"] ) ) { update_option( 'km_accessToken', $km_res["accessToken"] ); } } $km_applicationId = get_option( 'km_applicationId' ); $km_applicationSecret = get_option( 'km_applicationSecret' ); $checked_cat_list = get_option( 'km_checked_category' ); $is_sync_value = get_option( 'km_is_sync' ); $km_is_create_invoice = get_option( 'km_is_create_invoice' ); $syn_frequency_choice = get_option( 'km_sync_frequency' ); ?>

' . $km_res["message"] . '

' ); } elseif ( isset( $km_res["accessToken"] ) ) { echo wp_kses_post( '

Saved successfully

' ); } ?>
>


km_get_request( 'model/Product?' . $select . 'limit=10&skip=' . $skip ); $total_synced = 0; if ( $km_api->response_code == 200 ) { $total_product = count( $km_products ); if( ! empty( $km_products ) ) { foreach( $km_products as $i => $km_produc ) { $wc_product_id = $km_pro_sync->km_update_product( $km_produc ); if ( $wc_product_id ) { $total_synced++; } } } $skip = $skip + $total_synced; return array( "status" => 1, "message" => "", "total_product" => $total_product, "total_synced" => $total_synced, "skip" => $skip, ); } else { return array( "status" => 0, "message" => $km_api->response_message, "response_code" => $km_api->response_code, ); } } endif; if ( ! function_exists( 'firsttool_sync_products_ajax' ) ): /** * Callback of AJAX call triggerred from 1Tool settings page on admin side * * Respond the AJAX call triggerred from 1Tool API settings page on admin panel * and starts the product sync process by calling firsttool_sync_products() function. * * @since 1.0.0 * * @see firsttool_sync_products() * */ function firsttool_sync_products_ajax() { $skip = isset( $_POST["skip"] ) ? sanitize_text_field($_POST["skip"]) : 0; $res = firsttool_sync_products( $skip ); echo json_encode( $res ); die(); } endif; add_action( 'wp_ajax_km_sync_products', 'firsttool_sync_products_ajax' ); if ( ! function_exists( 'firsttool_custom_cron_schedule' ) ): /** * Sets cron schedules of every 6 and 12 hours * * Using cron_schedules filter, add schedule of every 6 and 12 hours to have * cron job of sync to run in that specified intervals. * * @since 1.0.0 * */ function firsttool_custom_cron_schedule( $schedules ) { $schedules['every_six_hours'] = array( 'interval' => 21600, // Every 6 hours 'display' =>'Every 6 hours', ); $schedules['every_twelve_hours'] = array( 'interval' => 43200, // Every 12 hours 'display' => 'Every 12 hours', ); return $schedules; } endif; add_filter( 'cron_schedules', 'firsttool_custom_cron_schedule' ); // Schedule the event if it is not scheduled. if ( ! wp_next_scheduled( 'firsttool_sync_products_cron', array(0) ) ) { wp_schedule_event( strtotime('01:00:00'), 'daily', 'firsttool_sync_products_cron', array(0) ); wp_schedule_event( strtotime('13:00:00'), 'daily', 'firsttool_sync_products_cron', array(0) ); } if ( ! function_exists( 'firsttool_sync_products_cron' ) ): /** * Handles schedule cron and set individual events to sync products from API * * Triggered when scheduled event occure and starts sync process of products * from API by scheduling single run event for each products. * * @since 1.0.0 * * @see firsttool_sync_products_cron() * */ function firsttool_sync_products_cron( $skip ) { $km_accessToken = get_option( 'km_accessToken' ); if ( ! empty( $km_accessToken ) ) { $timeIn_new = time(); $res = km_sync_products( $skip ); $log = print_r( $res, true ); if ( $res["status"] == 0 && ! empty( $res["response_code"] ) && $res["response_code"] !== 200 ) { wp_schedule_single_event( $timeIn_new, "firsttool_sync_products_cron", array( $skip ) ); } if ( $res["status"] == 1 && $res["total_product"] == 10 ) { $skip = $res["skip"]; wp_schedule_single_event( $timeIn_new, "firsttool_sync_products_cron", array( $skip ) ); } } } endif; add_action( 'firsttool_sync_products_cron', 'firsttool_sync_products_cron', 10, 1 ); ?>