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', '1tool_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

' ); } ?>
>



Country Codes are Synced.


km_get_request( 'model/Product?' . $select . 'limit=10&skip=' . $skip .'&status = 1' ); $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 ) { if ( 1 === $km_produc['Product.status'] && 1 === $km_produc['Product.inShop'] ) { get_error_log("syncing product " . $km_produc['Product.id'] . " as those are active and inshop" ); $wc_product_id = $km_pro_sync->km_update_product( $km_produc ); firsttool_add_sync_log( $wc_product_id ); if ( $wc_product_id ) { $total_synced++; } } else { get_error_log("not syncing product " . $km_produc['Product.id'] . " as those either not active or not inshop" ); get_error_log( print_r( $km_produc, true ) ); $total_synced++; } } } else { firsttool_sync_log_process_function(); } $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_sync_countries_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_countries_ajax() { $km_api = new FIRSTTOOL_API\FirstTool_API; $api_countries = $km_api->km_get_request( 'model/CountryCode?limit=500' ); if ( ! empty ( $api_countries ) ) { $countries = array(); foreach( $api_countries as $country ) { $countries[ $country['CountryCode.code'] ] = $country['CountryCode.id']; } update_option( 'firsttool_country_ids', $countries ); $res['status'] = 1; } else { $res['status'] = 0; } echo json_encode( $res ); die(); } endif; add_action( 'wp_ajax_km_sync_countries', 'firsttool_sync_countries_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 = firsttool_sync_products( $skip ); $log = print_r( $res, true ); if ( $res["status"] == 1 && $res["total_product"] == 0 ){ if ( ! wp_next_scheduled( 'firsttool_sync_log_process_cron' ) ) { wp_schedule_single_event( $timeIn_new, "firsttool_sync_log_process_function" ); } } 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 ); add_action( 'firsttool_sync_log_process_cron', 'firsttool_sync_log_process_function' ); function firsttool_sync_log_process_function() { // Clear not synced products here $synced_products = firsttool_get_sync_log(); if ( !empty( $synced_products ) ){ $products = wc_get_products( array( 'post__not_in' => $synced_products, ) ); get_error_log("synced log deleting for products " . print_r( $products, true ) ); } foreach( $products as $each_product ){ $each_product->delete( true ); // Force Delete the product } firsttool_clear_sync_log(); } function firsttool_add_sync_log( $product_id ) { $current_list = get_option( 'firsttool_synced_products', array() ); $current_list[] = $product_id; get_error_log("synced product added to log " . $product_id ); update_option( 'firsttool_synced_products', $current_list ); } function firsttool_get_sync_log() { return get_option( 'firsttool_synced_products', array() ); } function firsttool_clear_sync_log() { get_error_log("synced log deleted" ); delete_option( 'firsttool_synced_products' ); } function firsttool_get_country_id_from_code( $country_code ) { $countries_data = get_option( 'firsttool_country_ids', array() ); if ( isset( $countries_data[ $country_code ] ) ) { return $countries_data[ $country_code ]; } return false; } ?>