class-vi-w2s-admin.php
1104 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The admin-specific functionality of the plugin. |
| 5 | * |
| 6 | * @link https://villatheme.com |
| 7 | * @since 1.0.0 |
| 8 | * |
| 9 | * @package w2s-migrate-woo-to-shopify |
| 10 | * @subpackage w2s-migrate-woo-to-shopify/admin |
| 11 | * @author Villatheme <support@villatheme.com> |
| 12 | */ |
| 13 | class Vi_W2s_Admin { |
| 14 | |
| 15 | /** |
| 16 | * The ID of this plugin. |
| 17 | * |
| 18 | * @since 1.0.0 |
| 19 | * @access private |
| 20 | * @var string $vi_w2s The ID of this plugin. |
| 21 | */ |
| 22 | private $vi_w2s; |
| 23 | |
| 24 | /** |
| 25 | * The version of this plugin. |
| 26 | * |
| 27 | * @since 1.0.0 |
| 28 | * @access private |
| 29 | * @var string $version The current version of this plugin. |
| 30 | */ |
| 31 | private $version; |
| 32 | |
| 33 | /** |
| 34 | * Initialize the class and set its properties. |
| 35 | * |
| 36 | * @param string $vi_w2s The name of this plugin. |
| 37 | * @param string $version The version of this plugin. |
| 38 | * |
| 39 | * @since 1.0.0 |
| 40 | */ |
| 41 | |
| 42 | private $setting; |
| 43 | private $default_data; |
| 44 | |
| 45 | public function __construct( $vi_w2s, $version ) { |
| 46 | |
| 47 | $this->vi_w2s = $vi_w2s; |
| 48 | $this->version = $version; |
| 49 | $this->setting = new VI_W2S_IMPORT_WOOCOMMERCE_TO_SHOPIFY_DATA(); |
| 50 | $this->default_data = $this->setting->get_default(); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Register the stylesheets for the admin area. |
| 56 | * |
| 57 | * @since 1.0.0 |
| 58 | */ |
| 59 | public function enqueue_styles() { |
| 60 | |
| 61 | /** |
| 62 | * This function is provided for demonstration purposes only. |
| 63 | * |
| 64 | * An instance of this class should be passed to the run() function |
| 65 | * defined in Vi_W2s_Loader as all of the hooks are defined |
| 66 | * in that particular class. |
| 67 | * |
| 68 | * The Vi_W2s_Loader will then create the relationship |
| 69 | * between the defined hooks and the functions defined in this |
| 70 | * class. |
| 71 | */ |
| 72 | $current_screen = get_current_screen()->id; |
| 73 | |
| 74 | if ( |
| 75 | $current_screen == 'toplevel_page_vi-w2s-woo-to-shopify' || |
| 76 | $current_screen === 'woo-to-shopify_page_w2s-import-woocommerce-to-shopify-clear-data' |
| 77 | ) { |
| 78 | wp_enqueue_style( $this->vi_w2s . '-accordion', VIW2S_DIR_URL . 'assets/css/accordion.min.css', array(), $this->version ); |
| 79 | wp_enqueue_style( $this->vi_w2s . '-button', VIW2S_DIR_URL . 'assets/css/button.min.css', array(), $this->version ); |
| 80 | wp_enqueue_style( $this->vi_w2s . '-checkbox', VIW2S_DIR_URL . 'assets/css/checkbox.min.css', array(), $this->version ); |
| 81 | wp_enqueue_style( $this->vi_w2s . '-dropdown', VIW2S_DIR_URL . 'assets/css/dropdown.min.css', array(), $this->version ); |
| 82 | wp_enqueue_style( $this->vi_w2s . '-form', VIW2S_DIR_URL . 'assets/css/form.min.css', array(), $this->version ); |
| 83 | wp_enqueue_style( $this->vi_w2s . '-icon', VIW2S_DIR_URL . 'assets/css/icon.min.css', array(), $this->version ); |
| 84 | wp_enqueue_style( $this->vi_w2s . '-input', VIW2S_DIR_URL . 'assets/css/input.min.css', array(), $this->version ); |
| 85 | wp_enqueue_style( $this->vi_w2s . '-label', VIW2S_DIR_URL . 'assets/css/label.min.css', array(), $this->version ); |
| 86 | wp_enqueue_style( $this->vi_w2s . '-message', VIW2S_DIR_URL . 'assets/css/message.min.css', array(), $this->version ); |
| 87 | wp_enqueue_style( $this->vi_w2s . '-progress', VIW2S_DIR_URL . 'assets/css/progress.min.css', array(), $this->version ); |
| 88 | wp_enqueue_style( $this->vi_w2s . '-segment', VIW2S_DIR_URL . 'assets/css/segment.min.css', array(), $this->version ); |
| 89 | wp_enqueue_style( $this->vi_w2s . '-select2', VIW2S_DIR_URL . 'assets/css/select2.min.css', array(), $this->version ); |
| 90 | wp_enqueue_style( $this->vi_w2s . '-step', VIW2S_DIR_URL . 'assets/css/step.min.css', array(), $this->version ); |
| 91 | wp_enqueue_style( $this->vi_w2s . '-table', VIW2S_DIR_URL . 'assets/css/table.min.css', array(), $this->version ); |
| 92 | wp_enqueue_style( $this->vi_w2s . '-transition', VIW2S_DIR_URL . 'assets/css/transition.min.css', array(), $this->version ); |
| 93 | |
| 94 | if ( WP_DEBUG ) { |
| 95 | wp_enqueue_style( $this->vi_w2s . '-style', VIW2S_DIR_URL . 'admin/css/vi-w2s-admin.css', array(), $this->version ); |
| 96 | } else { |
| 97 | wp_enqueue_style( $this->vi_w2s . '-style', VIW2S_DIR_URL . 'admin/css/vi-w2s-admin.min.css', array(), $this->version ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Register the JavaScript for the admin area. |
| 105 | * |
| 106 | * @since 1.0.0 |
| 107 | */ |
| 108 | public function enqueue_scripts() { |
| 109 | |
| 110 | /** |
| 111 | * This function is provided for demonstration purposes only. |
| 112 | * |
| 113 | * An instance of this class should be passed to the run() function |
| 114 | * defined in Vi_W2s_Loader as all of the hooks are defined |
| 115 | * in that particular class. |
| 116 | * |
| 117 | * The Vi_W2s_Loader will then create the relationship |
| 118 | * between the defined hooks and the functions defined in this |
| 119 | * class. |
| 120 | */ |
| 121 | $current_screen = get_current_screen()->id; |
| 122 | |
| 123 | if ( |
| 124 | $current_screen === 'toplevel_page_vi-w2s-woo-to-shopify' || |
| 125 | $current_screen === 'woo-to-shopify_page_w2s-import-woocommerce-to-shopify-clear-data' |
| 126 | ) { |
| 127 | |
| 128 | wp_enqueue_script( $this->vi_w2s . '-js-accordion', VIW2S_DIR_URL . 'assets/js/accordion.min.js', array( 'jquery' ), $this->version, true ); |
| 129 | wp_enqueue_script( $this->vi_w2s . '-js-checkbox', VIW2S_DIR_URL . 'assets/js/checkbox.min.js', array( 'jquery' ), $this->version, true ); |
| 130 | wp_enqueue_script( $this->vi_w2s . '-js-dropdown', VIW2S_DIR_URL . 'assets/js/dropdown.min.js', array( 'jquery' ), $this->version, true ); |
| 131 | wp_enqueue_script( $this->vi_w2s . '-js-progress', VIW2S_DIR_URL . 'assets/js/progress.min.js', array( 'jquery' ), $this->version, true ); |
| 132 | wp_enqueue_script( $this->vi_w2s . '-js-select2', VIW2S_DIR_URL . 'assets/js/select2.js', array( 'jquery' ), $this->version, true ); |
| 133 | wp_enqueue_script( $this->vi_w2s . '-js-transition', VIW2S_DIR_URL . 'assets/js/transition.min.js', array( 'jquery' ), $this->version, true ); |
| 134 | |
| 135 | wp_enqueue_script( $this->vi_w2s . '-js', VIW2S_DIR_URL . 'admin/js/vi-w2s-admin.js', array( 'jquery', 'jquery-tiptip' ), $this->version, true ); |
| 136 | $viw2s_i18n_params = array( |
| 137 | 'ajaxurl' => admin_url( "admin-ajax.php" ), |
| 138 | 'i18n_empty_store_address_error' => esc_html__( 'Store address can not be empty! ', 'w2s-migrate-woo-to-shopify' ), |
| 139 | 'i18n_empty_store_api_key_error' => esc_html__( 'API key can not be empty! ', 'w2s-migrate-woo-to-shopify' ), |
| 140 | 'i18n_empty_store_api_secret_error' => esc_html__( 'API secret can not be empty! ', 'w2s-migrate-woo-to-shopify' ), |
| 141 | 'i18n_empty_choose_store_import_error' => esc_html__( 'Need to select at least one store to import! ', 'w2s-migrate-woo-to-shopify' ), |
| 142 | 'i18n_empty_choose_data_import_error' => esc_html__( 'Need to select at least one data to import! ', 'w2s-migrate-woo-to-shopify' ), |
| 143 | 'i18n_search_product_placeholder' => esc_html__( 'Select products', 'w2s-migrate-woo-to-shopify' ), |
| 144 | ); |
| 145 | |
| 146 | wp_localize_script( $this->vi_w2s . '-js', 'viw2s_i18n_params', $viw2s_i18n_params ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Register Menu Setting link |
| 152 | * |
| 153 | * @return array |
| 154 | * @since 1.0.0 |
| 155 | */ |
| 156 | function viw2s_add_action_links( $links ) { |
| 157 | $settings_link = array( |
| 158 | '<a href="' . admin_url( 'admin.php?page=vi-w2s-woo-to-shopify' ) . '">' . esc_html__( 'Settings', 'w2s-migrate-woo-to-shopify' ) . '</a>', |
| 159 | ); |
| 160 | |
| 161 | return array_merge( $links, $settings_link ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Register Menu Setting for the admin area. |
| 166 | * @return void |
| 167 | * @since 1.0.0 |
| 168 | */ |
| 169 | public function viw2s_admin_menu() { |
| 170 | add_menu_page( |
| 171 | esc_html__( 'Woo to Shopify', 'w2s-migrate-woo-to-shopify' ), |
| 172 | esc_html__( 'Woo to Shopify', 'w2s-migrate-woo-to-shopify' ), |
| 173 | 'manage_woocommerce', |
| 174 | 'vi-w2s-woo-to-shopify', |
| 175 | array( $this, 'viw2s_page_setting_function' ), |
| 176 | 'dashicons-image-rotate-right', |
| 177 | 3 |
| 178 | ); |
| 179 | do_action( 'viw2s_submenu_clear_data' ); |
| 180 | $menu_slug = 'w2s-import-woocommerce-to-shopify-status'; |
| 181 | add_submenu_page( |
| 182 | 'vi-w2s-woo-to-shopify', |
| 183 | esc_html__( 'System status', 'w2s-migrate-woo-to-shopify' ), |
| 184 | esc_html__( 'System Status', 'w2s-migrate-woo-to-shopify' ), |
| 185 | 'manage_woocommerce', |
| 186 | $menu_slug, |
| 187 | array( $this, 'page_callback_system_status' ) |
| 188 | ); |
| 189 | |
| 190 | $menu_slug = 'w2s-import-woocommerce-to-shopify-logs'; |
| 191 | add_submenu_page( |
| 192 | 'vi-w2s-woo-to-shopify', |
| 193 | esc_html__( 'Logs', 'w2s-migrate-woo-to-shopify' ), |
| 194 | esc_html__( 'Logs', 'w2s-migrate-woo-to-shopify' ), |
| 195 | apply_filters( 'vi_w2s_admin_sub_menu_capability', 'manage_options', $menu_slug ), |
| 196 | $menu_slug, |
| 197 | array( $this, 'page_callback_logs' ) |
| 198 | ); |
| 199 | |
| 200 | } |
| 201 | |
| 202 | public static function security_recommendation_html() { |
| 203 | ?> |
| 204 | <div class="w2s-security-warning"> |
| 205 | <div class="vi-ui warning message"> |
| 206 | <div class="header"> |
| 207 | <?php esc_html_e( 'Shopify Admin API security recommendation', 'w2s-migrate-woo-to-shopify' ); ?> |
| 208 | </div> |
| 209 | <ul class="list"> |
| 210 | <li><?php esc_html_e( 'You should enable only what is necessary for your app to work.', 'w2s-migrate-woo-to-shopify' ); ?></li> |
| 211 | <li><?php esc_html_e( 'Treat the API key and password like you would any other password, since whoever has access to these credentials has API access to the store.', 'w2s-migrate-woo-to-shopify' ); ?></li> |
| 212 | <li><?php esc_html_e( 'Change your API at least once a month', 'w2s-migrate-woo-to-shopify' ); ?></li> |
| 213 | <li><?php esc_html_e( 'If you only use API to import data, remove API permissions or delete the API after import completed', 'w2s-migrate-woo-to-shopify' ); ?></li> |
| 214 | </ul> |
| 215 | </div> |
| 216 | </div> |
| 217 | <?php |
| 218 | } |
| 219 | |
| 220 | public function page_callback_system_status() { |
| 221 | ?> |
| 222 | <h2><?php esc_html_e( 'System Status', 'w2s-migrate-woo-to-shopify' ) ?></h2> |
| 223 | <table id="status" class="widefat"> |
| 224 | <thead> |
| 225 | <tr> |
| 226 | <th><?php esc_html_e( 'Option name', 'w2s-migrate-woo-to-shopify' ) ?></th> |
| 227 | <th><?php esc_html_e( 'Your option value', 'w2s-migrate-woo-to-shopify' ) ?></th> |
| 228 | <th><?php esc_html_e( 'Minimum recommended value', 'w2s-migrate-woo-to-shopify' ) ?></th> |
| 229 | </tr> |
| 230 | </thead> |
| 231 | <tbody> |
| 232 | <tr> |
| 233 | <td data-export-label="file_get_contents">file_get_contents</td> |
| 234 | <td> |
| 235 | <?php |
| 236 | if ( function_exists( 'file_get_contents' ) ) { |
| 237 | ?> |
| 238 | <mark class="yes">✔ <code class="private"></code></mark> |
| 239 | <?php |
| 240 | } else { |
| 241 | ?> |
| 242 | <mark class="error">✕</mark>' |
| 243 | <?php |
| 244 | } |
| 245 | ?> |
| 246 | </td> |
| 247 | <td><?php esc_html_e( 'Required', 'w2s-migrate-woo-to-shopify' ) ?></td> |
| 248 | </tr> |
| 249 | <tr> |
| 250 | <td data-export-label="file_put_contents">file_put_contents</td> |
| 251 | <td> |
| 252 | <?php |
| 253 | if ( function_exists( 'file_put_contents' ) ) { |
| 254 | ?> |
| 255 | <mark class="yes">✔ <code class="private"></code></mark> |
| 256 | <?php |
| 257 | } else { |
| 258 | ?> |
| 259 | <mark class="error">✕</mark> |
| 260 | <?php |
| 261 | } |
| 262 | ?> |
| 263 | |
| 264 | </td> |
| 265 | <td><?php esc_html_e( 'Required', 'w2s-migrate-woo-to-shopify' ) ?></td> |
| 266 | </tr> |
| 267 | <tr> |
| 268 | <td data-export-label="mkdir">mkdir</td> |
| 269 | <td> |
| 270 | <?php |
| 271 | if ( function_exists( 'mkdir' ) ) { |
| 272 | ?> |
| 273 | <mark class="yes">✔ <code class="private"></code></mark> |
| 274 | <?php |
| 275 | } else { |
| 276 | ?> |
| 277 | <mark class="error">✕</mark> |
| 278 | <?php |
| 279 | } |
| 280 | ?> |
| 281 | |
| 282 | </td> |
| 283 | <td><?php esc_html_e( 'Required', 'w2s-migrate-woo-to-shopify' ) ?></td> |
| 284 | </tr> |
| 285 | <?php |
| 286 | $max_execution_time = ini_get( 'max_execution_time' ); |
| 287 | $max_input_vars = ini_get( 'max_input_vars' ); |
| 288 | $memory_limit = ini_get( 'memory_limit' ); |
| 289 | ?> |
| 290 | <tr> |
| 291 | <td data-export-label="<?php esc_attr_e( 'PHP Time Limit', 'w2s-migrate-woo-to-shopify' ) ?>"><?php esc_html_e( 'PHP Time Limit', 'w2s-migrate-woo-to-shopify' ) ?></td> |
| 292 | <td style="<?php if ( $max_execution_time > 0 && $max_execution_time < 300 ) { |
| 293 | echo esc_attr( 'color:red' ); |
| 294 | } ?>"><?php echo esc_html( $max_execution_time ); ?></td> |
| 295 | <td><?php esc_html_e( '3000', 'w2s-migrate-woo-to-shopify' ) ?></td> |
| 296 | </tr> |
| 297 | <tr> |
| 298 | <td data-export-label="<?php esc_attr_e( 'PHP Max Input Vars', 'w2s-migrate-woo-to-shopify' ) ?>"><?php esc_html_e( 'PHP Max Input Vars', 'w2s-migrate-woo-to-shopify' ) ?></td> |
| 299 | <td style="<?php if ( $max_input_vars < 1000 ) { |
| 300 | echo esc_attr( 'color:red' ); |
| 301 | } ?>"><?php echo esc_html( $max_input_vars ); ?></td> |
| 302 | <td><?php esc_html_e( '10000', 'w2s-migrate-woo-to-shopify' ) ?></td> |
| 303 | </tr> |
| 304 | <tr> |
| 305 | <td data-export-label="<?php esc_attr_e( 'Memory Limit', 'w2s-migrate-woo-to-shopify' ) ?>"><?php esc_html_e( 'Memory Limit', 'w2s-migrate-woo-to-shopify' ) ?></td> |
| 306 | <td style="<?php if ( intval( $memory_limit ) < 64 ) { |
| 307 | echo esc_attr( 'color:red' ); |
| 308 | } ?>"><?php echo esc_html( $memory_limit ); ?></td> |
| 309 | <td><?php esc_html_e( '512M', 'w2s-migrate-woo-to-shopify' ) ?></td> |
| 310 | </tr> |
| 311 | </tbody> |
| 312 | </table> |
| 313 | <?php |
| 314 | } |
| 315 | |
| 316 | public function page_callback_logs() { |
| 317 | require plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/vi-w2s-admin-logs.php'; |
| 318 | } |
| 319 | |
| 320 | public function viw2s_page_setting_function() { |
| 321 | require plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/vi-w2s-admin-display.php'; |
| 322 | } |
| 323 | |
| 324 | public static function upgrade_button() { |
| 325 | ?> |
| 326 | <a href="https://1.envato.market/vnr5Nj" |
| 327 | target="_blank" |
| 328 | class="vi-ui button yellow"><?php esc_html_e( 'Upgrade this feature', 'w2s-migrate-woo-to-shopify' ) ?></a> |
| 329 | <?php |
| 330 | } |
| 331 | |
| 332 | public function viw2s_ajax_check_store( $viw2s_store_domain, $viw2s_store_api_key, $viw2s_store_api_secret ) { |
| 333 | |
| 334 | $status = ''; |
| 335 | $api_error = ''; |
| 336 | |
| 337 | if ( ! current_user_can( 'administrator' ) ) { |
| 338 | return array( |
| 339 | 'status' => 'not_admin', |
| 340 | 'api_error' => '', |
| 341 | ); |
| 342 | } |
| 343 | if ( empty( $viw2s_store_domain ) || empty( $viw2s_store_api_key ) || empty( $viw2s_store_api_secret ) ) { |
| 344 | return array( |
| 345 | 'status' => 'field_empty', |
| 346 | 'api_error' => '', |
| 347 | ); |
| 348 | } |
| 349 | $checkShopifyConnect = $this->setting->get_access_scopes( $viw2s_store_domain, $viw2s_store_api_key, $viw2s_store_api_secret ); |
| 350 | // try { |
| 351 | // |
| 352 | // $checkShopifyConnect->Shop->get(); |
| 353 | // $status = 'success'; |
| 354 | // } catch ( Exception $exception ) { |
| 355 | // $api_error = $exception->getMessage(); |
| 356 | // } |
| 357 | |
| 358 | return array( |
| 359 | 'status' => $checkShopifyConnect['status'], |
| 360 | 'api_error' => $checkShopifyConnect['data'], |
| 361 | ); |
| 362 | |
| 363 | |
| 364 | } |
| 365 | |
| 366 | public function viw2s_ajax_search_product() { |
| 367 | $keysearch = isset( $_POST['keysearch'] ) ? wc_clean( wp_unslash( $_POST['keysearch'] ) ) : '';// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 368 | $exclude_product_ids = isset( $_POST['exclude_product_ids'] ) ? wc_clean( wp_unslash( $_POST['exclude_product_ids'] ) ) : array();// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 369 | $product_args = array( |
| 370 | 'status' => 'publish', |
| 371 | 'post_type' => 'product', |
| 372 | 'posts_per_page' => - 1, |
| 373 | 'post__not_in' => $exclude_product_ids,//phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in |
| 374 | 's' => $keysearch, |
| 375 | 'tax_query' => array(//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 376 | array( |
| 377 | 'taxonomy' => 'product_type', |
| 378 | 'field' => 'slug', |
| 379 | 'terms' => array( 'simple', 'variable' ), |
| 380 | ), |
| 381 | ), |
| 382 | |
| 383 | ); |
| 384 | $products_query = new WP_Query( $product_args ); |
| 385 | $arr_data_product = array(); |
| 386 | if ( $products_query->have_posts() ) { |
| 387 | while ( $products_query->have_posts() ) : $products_query->the_post(); |
| 388 | array_push( $arr_data_product, array( |
| 389 | "id" => get_the_ID(), |
| 390 | "text" => get_the_title(), |
| 391 | ) ); |
| 392 | endwhile; |
| 393 | } |
| 394 | $response = $arr_data_product; |
| 395 | wp_send_json( $response ); |
| 396 | die(); |
| 397 | } |
| 398 | |
| 399 | public function viw2s_ajax_search_product_cat() { |
| 400 | $keysearch = isset( $_POST['keysearch'] ) ? wc_clean( wp_unslash( $_POST['keysearch'] ) ) : '';// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 401 | |
| 402 | $arr_tax = get_terms( array( |
| 403 | 'taxonomy' => 'product_cat', |
| 404 | 'orderby' => 'name', |
| 405 | 'order' => 'ASC', |
| 406 | 'search' => $keysearch, |
| 407 | 'hide_empty' => true, |
| 408 | 'fields' => 'all', |
| 409 | ) ); |
| 410 | |
| 411 | $items = array(); |
| 412 | if ( is_array( $arr_tax ) && ! empty( $arr_tax ) ) { |
| 413 | foreach ( $arr_tax as $tax_item ) { |
| 414 | $items[] = array( |
| 415 | 'id' => $tax_item->slug, |
| 416 | 'text' => $tax_item->name |
| 417 | ); |
| 418 | |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | wp_send_json( $items ); |
| 423 | die(); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | public function viw2s_save_option_setting() { |
| 428 | global $viw2s_settings; |
| 429 | $page = isset( $_REQUEST['page'] ) ? sanitize_text_field( $_REQUEST['page'] ) : ''; |
| 430 | |
| 431 | if ( $page !== 'vi-w2s-woo-to-shopify' ) { |
| 432 | return; |
| 433 | } |
| 434 | if ( ! current_user_can( 'manage_options' ) ) { |
| 435 | return; |
| 436 | } |
| 437 | if ( |
| 438 | ! isset( $_POST['_viw2s_save_setting_nonce'] ) || |
| 439 | ! wp_verify_nonce( wc_clean( wp_unslash( $_POST['_viw2s_save_setting_nonce'] ) ), 'viw2s_action_save_setting_nonce' ) |
| 440 | ) { |
| 441 | return; |
| 442 | } |
| 443 | if ( isset( $_POST['viw2s-save-setting'] ) ) { |
| 444 | $viw2s_params = get_option( 'viw2s_params ', false ) ? get_option( 'viw2s_params ' ) : $this->default_data; |
| 445 | |
| 446 | $viw2s_store_setting = isset( $_POST['viw2s_store_setting'] ) ? wc_clean( wp_unslash( $_POST['viw2s_store_setting'] ) ) : array(); |
| 447 | $arr_import_products_option = isset( $_POST['viw2s_import_products_option'] ) ? wc_clean( wp_unslash( $_POST['viw2s_import_products_option'] ) ) : array(); |
| 448 | |
| 449 | $arr_store_setting = array(); |
| 450 | |
| 451 | if ( sizeof( $viw2s_store_setting ) > 0 ) { |
| 452 | foreach ( $viw2s_store_setting as $store_item ) { |
| 453 | $new_store_item = $store_item; |
| 454 | $parse_domain = isset( $store_item['domain'] ) ? wc_clean( wp_parse_url( $store_item['domain'] ) ) : array(); |
| 455 | $domain = isset( $parse_domain['host'] ) ? $parse_domain['host'] : $parse_domain['path']; |
| 456 | $new_store_item['domain'] = $domain; |
| 457 | $api_key = isset( $store_item['api_key'] ) ? wc_clean( $store_item['api_key'] ) : ''; |
| 458 | $api_secret = isset( $store_item['api_secret'] ) ? wc_clean( $store_item['api_secret'] ) : ''; |
| 459 | |
| 460 | if ( $domain && $api_key && $api_secret ) { |
| 461 | |
| 462 | $request = $this->setting->get_access_scopes( $domain, $api_key, $api_secret ); |
| 463 | if ( $request['status'] === 'success' ) { |
| 464 | |
| 465 | $new_store_item['validate'] = 1; |
| 466 | |
| 467 | } else { |
| 468 | $new_store_item['validate'] = ''; |
| 469 | } |
| 470 | |
| 471 | } else { |
| 472 | $new_store_item['validate'] = ''; |
| 473 | } |
| 474 | array_push( $arr_store_setting, $new_store_item ); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | $new_viw2s_params = array( |
| 479 | 'viw2s_store_setting' => $arr_store_setting, |
| 480 | 'viw2s_import_products_option' => $arr_import_products_option |
| 481 | ); |
| 482 | |
| 483 | $new_viw2s_params = wp_parse_args( $new_viw2s_params, $viw2s_params ); |
| 484 | $this->setting->update_option( 'viw2s_params', $new_viw2s_params ); |
| 485 | $viw2s_settings = $new_viw2s_params; |
| 486 | |
| 487 | } else { |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | |
| 492 | } |
| 493 | |
| 494 | public function viw2s_ajax_active_import() { |
| 495 | $status = ''; |
| 496 | |
| 497 | if ( defined( "DOING_AUTOSAVE" ) && DOING_AUTOSAVE ) { |
| 498 | $status = 'error'; |
| 499 | } |
| 500 | if ( wp_verify_nonce( $_POST['_viw2s_action_import_nonce'], 'viw2s_action_import_nonce' ) ) { |
| 501 | $viw2s_setting_store = $this->setting->get_params( 'viw2s_store_setting' ); |
| 502 | $type_import = isset( $_POST['type_import'] ) ? wc_clean( wp_unslash( $_POST['type_import'] ) ) : ''; |
| 503 | if ( is_array( $viw2s_setting_store ) && ! empty( $viw2s_setting_store ) && isset( $viw2s_setting_store[0]['validate'] ) && $viw2s_setting_store[0]['validate'] ) { |
| 504 | $viw2s_get_api_scope = $this->setting->get_access_scopes( $viw2s_setting_store[0]['domain'], $viw2s_setting_store[0]['api_key'], $viw2s_setting_store[0]['api_secret'] ); |
| 505 | if ( $viw2s_get_api_scope['status'] == 'success' ) { |
| 506 | $viw2s_get_api_access_scope_handle = $this->setting->get_access_scopes_handle( $viw2s_setting_store[0]['domain'], $viw2s_setting_store[0]['api_key'], $viw2s_setting_store[0]['api_secret'] ); |
| 507 | $missing_scopes = array_diff( [ |
| 508 | 'read_products', |
| 509 | 'write_products', |
| 510 | 'read_locations', |
| 511 | 'write_inventory' |
| 512 | ], $viw2s_get_api_access_scope_handle ); |
| 513 | if ( empty( $missing_scopes ) ) { |
| 514 | $status = 'success'; |
| 515 | if ( $type_import === 'products' ) { |
| 516 | $viw2s_get_all_product_data = $this->setting->viw2s_get_all_product_data(); |
| 517 | if ( ! empty( $viw2s_get_all_product_data ) ) { |
| 518 | update_option( 'viw2s_importing_arr_product', $viw2s_get_all_product_data ); |
| 519 | $viw2s_item_product = $viw2s_get_all_product_data[0]; |
| 520 | $response = array( |
| 521 | 'status' => $status, |
| 522 | 'total_products' => count( $viw2s_get_all_product_data ), |
| 523 | 'current_import_product_type' => $viw2s_item_product["type"] ?? '', |
| 524 | 'current_import_product_id' => $viw2s_item_product["id"] ?? '', |
| 525 | 'product_index' => 0, |
| 526 | 'all_product_data' => $viw2s_get_all_product_data, |
| 527 | ); |
| 528 | |
| 529 | } else { |
| 530 | $response = array( |
| 531 | 'status' => $status, |
| 532 | 'logs' => '<div><strong class="important_alert">' . esc_html__( 'Not find products to import', 'w2s-migrate-woo-to-shopify' ) . '</strong></div>', |
| 533 | 'total_products' => 0, |
| 534 | ); |
| 535 | } |
| 536 | } elseif ( $type_import === 'product_categories' ) { |
| 537 | $viw2s_get_all_product_cats_data = $this->setting->viw2s_get_all_product_cats(); |
| 538 | if ( ! empty( $viw2s_get_all_product_cats_data ) ) { |
| 539 | update_option( 'viw2s_importing_arr_product_categories', $viw2s_get_all_product_cats_data ); |
| 540 | $response = array( |
| 541 | 'status' => $status, |
| 542 | 'total_categories' => count( $viw2s_get_all_product_cats_data ), |
| 543 | 'categories_index' => 0, |
| 544 | 'all_product_cats_data' => $viw2s_get_all_product_cats_data |
| 545 | ); |
| 546 | } else { |
| 547 | $response = array( |
| 548 | 'status' => $status, |
| 549 | 'logs' => '<div><strong class="important_alert">' . esc_html__( 'Not find categories to import', 'w2s-migrate-woo-to-shopify' ) . '</strong></div>', |
| 550 | 'total_products' => 0, |
| 551 | ); |
| 552 | } |
| 553 | } |
| 554 | } else { |
| 555 | $status = 'permission_not_correct'; |
| 556 | $response = array( |
| 557 | 'status' => $status, |
| 558 | 'logs' => '<div><strong class="important_alert">' . sprintf( esc_html__( 'API permission not correct, missing %s scopes !!!', 'w2s-migrate-woo-to-shopify' ), implode( ', ', $missing_scopes ) ) . '</strong></div>' |
| 559 | ); |
| 560 | } |
| 561 | |
| 562 | } else { |
| 563 | $status = 'error_access_scope'; |
| 564 | $response = array( |
| 565 | 'status' => $status, |
| 566 | ); |
| 567 | } |
| 568 | |
| 569 | } else { |
| 570 | $response = array( |
| 571 | 'status' => 'store_setting_error', |
| 572 | |
| 573 | ); |
| 574 | } |
| 575 | |
| 576 | } else { |
| 577 | $status = 'error_nonce'; |
| 578 | $response = array( |
| 579 | 'status' => $status, |
| 580 | |
| 581 | ); |
| 582 | } |
| 583 | |
| 584 | wp_send_json( $response ); |
| 585 | die(); |
| 586 | } |
| 587 | |
| 588 | public function viw2s_ajax_import_action() { |
| 589 | $response = array(); |
| 590 | if ( wp_verify_nonce( wc_clean( wp_unslash( $_POST['_viw2s_action_import_nonce'] ) ), 'viw2s_action_import_nonce' ) ) { |
| 591 | $viw2s_setting_store = $this->setting->get_params( 'viw2s_store_setting' ); |
| 592 | $domain = $viw2s_setting_store[0]['domain'] ?? ''; |
| 593 | $api_key = $viw2s_setting_store[0]['api_key'] ?? ''; |
| 594 | $api_secret = $viw2s_setting_store[0]['api_secret'] ?? ''; |
| 595 | $viw2s_get_api_scope = $this->setting->get_access_scopes( $domain, $api_key, $api_secret ); |
| 596 | if ( |
| 597 | is_array( $viw2s_get_api_scope ) && |
| 598 | isset( $viw2s_get_api_scope['status'] ) && |
| 599 | $viw2s_get_api_scope['status'] === 'success' |
| 600 | ) { |
| 601 | $viw2s_get_api_access_scope_handle = $this->setting->get_access_scopes_handle( $domain, $api_key, $api_secret ); |
| 602 | $missing_scopes = array_diff( [ |
| 603 | 'read_products', |
| 604 | 'write_products', |
| 605 | 'read_locations', |
| 606 | 'write_inventory' |
| 607 | ], $viw2s_get_api_access_scope_handle ); |
| 608 | if ( empty( $missing_scopes ) ) { |
| 609 | |
| 610 | $bulk_import = Viw2s_Bulk_Import::instance( $domain ); |
| 611 | |
| 612 | $path = VI_W2S_IMPORT_WOOCOMMERCE_TO_SHOPIFY_DATA::get_cache_path( $domain, $api_key, $api_secret ) . '/'; |
| 613 | VI_W2S_IMPORT_WOOCOMMERCE_TO_SHOPIFY_DATA::create_cache_folder( $path ); |
| 614 | $log_file = $path . 'logs.txt'; |
| 615 | $step = isset( $_POST['step'] ) ? wc_clean( wp_unslash( $_POST['step'] ) ) : ''; |
| 616 | $logs = ''; |
| 617 | switch ( $step ) { |
| 618 | case 'products': |
| 619 | $get_all_importing_arr_product = get_option( 'viw2s_importing_arr_product', array() ); |
| 620 | $product_index = isset( $_POST['product_index'] ) ? wc_clean( wp_unslash( $_POST['product_index'] ) ) : ''; |
| 621 | $current_import_product_id = isset( $_POST['current_import_product_id'] ) ? wc_clean( wp_unslash( $_POST['current_import_product_id'] ) ) : ''; |
| 622 | $current_import_product_type = isset( $_POST['current_import_product_type'] ) ? wc_clean( wp_unslash( $_POST['current_import_product_type'] ) ) : ''; |
| 623 | $product_log = $path . 'products.txt'; |
| 624 | $list_products_imported_path = $path . '/list_products_imported.txt'; |
| 625 | $ids_products_imported_path = $path . '/ids_product_imported.txt'; |
| 626 | $list_products_imported = []; |
| 627 | $ids_products_imported = []; |
| 628 | if ( is_file( $list_products_imported_path ) ) { |
| 629 | $list_products_imported = json_decode( file_get_contents( $list_products_imported_path ), true );// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 630 | } |
| 631 | if ( is_file( $ids_products_imported_path ) ) { |
| 632 | $ids_products_imported = json_decode( file_get_contents( $ids_products_imported_path ), true );// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 633 | } |
| 634 | if ( ! empty( $get_all_importing_arr_product ) && ( $product_index !== '' ) ) { |
| 635 | $viw2s_total_product = count( $get_all_importing_arr_product ); |
| 636 | $arr_item_product_data = array(); |
| 637 | $arr_item_product_result = array(); |
| 638 | /* Bulk import product depends on rate*/ |
| 639 | // $bulk_import->set_time_limit(); |
| 640 | for ( $i = 0; $i < $bulk_import->product_rate(); $i ++ ) { |
| 641 | |
| 642 | $product_index ++; |
| 643 | if ( $product_index <= $viw2s_total_product ) { |
| 644 | $viw2s_item_product = $get_all_importing_arr_product[ $product_index - 1 ]; |
| 645 | $viw2s_item_product_data = $bulk_import->get_product_data_format( $viw2s_item_product["id"] ); |
| 646 | $arr_item_product_data[] = $viw2s_item_product_data['data']; |
| 647 | $viw2s_new_import_product_images = array(); |
| 648 | $viw2s_import_product_progress_label = 'Importing...'; |
| 649 | |
| 650 | $error_product_code = ''; |
| 651 | $error_image_code = ''; |
| 652 | $viw2s_import_status = ''; |
| 653 | $title = esc_html( get_the_title( $viw2s_item_product["id"] ) ); |
| 654 | $status = 'successful'; |
| 655 | $log = array( |
| 656 | 'shopify_id' => '', |
| 657 | 'woo_id' => $viw2s_item_product["id"], |
| 658 | 'title' => esc_html( get_the_title( $viw2s_item_product["id"] ) ), |
| 659 | 'message' => '', |
| 660 | ); |
| 661 | try { |
| 662 | switch ( $viw2s_item_product_data['status'] ) { |
| 663 | case 'success': |
| 664 | $result = $bulk_import->import_product( $viw2s_item_product_data['data'] ); |
| 665 | $arr_item_product_result[] = $result; |
| 666 | if ( ! empty( $result['data']['productSet']['userErrors'] ) ) { |
| 667 | $log['title'] = esc_html__( 'Import failed', 'w2s-migrate-woo-to-shopify' ); |
| 668 | $log['product_url'] = get_edit_post_link( $viw2s_item_product["id"] ); |
| 669 | $message = $result['data']['productSet']['userErrors'][0]['message']; |
| 670 | $field_error = $result['data']['productSet']['userErrors'][0]['field']; |
| 671 | $code = 422; |
| 672 | |
| 673 | if ( $message === 'Too many attempts. Please try again later.' ) { |
| 674 | $code = 429; |
| 675 | } elseif ( $message === 'Product options input is required when updating variants' ) { |
| 676 | $message = esc_html__( 'Product don\'t have any variation', 'w2s-migrate-woo-to-shopify' ); |
| 677 | } elseif ( str_contains( $message, 'Please provide a new handle' ) ) { |
| 678 | $message = esc_html__( 'Product\'s slug has been used', 'w2s-migrate-woo-to-shopify' ); |
| 679 | } |
| 680 | throw new Exception( "{$title} - {$message}", $code ); |
| 681 | } |
| 682 | if ( ! empty( $result['errors'][0]['message'] ) ) { |
| 683 | $message = $result['errors'][0]['message']; |
| 684 | $code = 422; |
| 685 | if ( strpos( $message, 'This mutation is unavailable at the moment. Try again' ) !== false || |
| 686 | strpos( $message, 'Throttled' ) !== false ) { |
| 687 | $code = 429; |
| 688 | } |
| 689 | throw new Exception( "{$title} - {$message}", $code ); |
| 690 | } |
| 691 | $viw2s_import_status = $result['data']['productSet']['product']; |
| 692 | $shopify_variants = $viw2s_import_status['variants']['nodes']; |
| 693 | $inventory_item_update = $viw2s_item_product_data['inventory_item_update']; |
| 694 | $shopify_product_id = basename( $viw2s_import_status['id'] ); |
| 695 | $viw2s_import_product_progress_label = sprintf( esc_html( 'Importing... %s /%s completed' ), $product_index, $viw2s_total_product ); |
| 696 | $log['shopify_id'] = $shopify_product_id; |
| 697 | $log['message'] = esc_html__( 'Import successfully', 'w2s-migrate-woo-to-shopify' ); |
| 698 | |
| 699 | /* Because productSet doesn't support inventory, need to do it separately */ |
| 700 | foreach ( $inventory_item_update as $j => $inventory_item ) { |
| 701 | $bulk_import->update_inventory_item( array( |
| 702 | 'id' => $shopify_variants[ $j ]['inventoryItem']['id'], |
| 703 | 'input' => $inventory_item |
| 704 | ) ); |
| 705 | } |
| 706 | |
| 707 | /*Update data to product metadata*/ |
| 708 | $_w2s_shopify_data = get_post_meta( $viw2s_item_product["id"], '_w2s_shopify_data', true ); |
| 709 | if ( ! empty( $_w2s_shopify_data ) && is_array( $_w2s_shopify_data ) ) { |
| 710 | $_w2s_shopify_data[ $domain ] = array( |
| 711 | '_w2s_shopify_product_id' => $shopify_product_id, |
| 712 | '_w2s_shopify_variant_id' => $viw2s_import_status['variants'][0]['id'], |
| 713 | 'status' => 'imported' |
| 714 | ); |
| 715 | update_post_meta( $viw2s_item_product["id"], '_w2s_shopify_data', $_w2s_shopify_data ); |
| 716 | } else { |
| 717 | update_post_meta( $viw2s_item_product["id"], '_w2s_shopify_data', array( |
| 718 | $domain => array( |
| 719 | '_w2s_shopify_product_id' => $shopify_product_id, |
| 720 | '_w2s_shopify_variant_id' => $viw2s_import_status['variants'][0]['id'], |
| 721 | 'status' => 'imported' |
| 722 | ) |
| 723 | ) ); |
| 724 | } |
| 725 | /*Update data to product metadata for children product*/ |
| 726 | if ( $viw2s_item_product['type'] == 'variable' && ! empty( $viw2s_item_product["children_ids"] ) ) { |
| 727 | |
| 728 | foreach ( $viw2s_item_product["children_ids"] as $children_key => $children_id ) { |
| 729 | $_w2s_shopify_data_children = get_post_meta( $children_id, '_w2s_shopify_data', true ); |
| 730 | |
| 731 | if ( ! empty( $_w2s_shopify_data_children ) && is_array( $_w2s_shopify_data_children ) ) { |
| 732 | $_w2s_shopify_data_children[ $domain ] = array( |
| 733 | '_w2s_shopify_product_id' => $shopify_product_id, |
| 734 | '_w2s_shopify_variant_id' => basename( $shopify_variants[ $children_key ]['id'] ), |
| 735 | 'status' => 'imported' |
| 736 | ); |
| 737 | update_post_meta( $children_id, '_w2s_shopify_data', $_w2s_shopify_data_children ); |
| 738 | } else { |
| 739 | update_post_meta( $children_id, '_w2s_shopify_data', array( |
| 740 | $domain => array( |
| 741 | '_w2s_shopify_product_id' => $shopify_product_id, |
| 742 | '_w2s_shopify_variant_id' => basename( $shopify_variants[ $children_key ]['id'] ), |
| 743 | 'status' => 'imported' |
| 744 | ) |
| 745 | ) ); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | } |
| 750 | /*Clean $list_products_imported */ |
| 751 | if ( ! empty( $list_products_imported ) && is_array( $list_products_imported ) ) { |
| 752 | |
| 753 | $list_products_imported = array_unique( $list_products_imported, SORT_NUMERIC ); |
| 754 | $key_old_not_exist = array_search( $viw2s_item_product["id"], $list_products_imported ); |
| 755 | if ( $key_old_not_exist ) { |
| 756 | unset( $list_products_imported[ $key_old_not_exist ] ); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | if ( is_array( $viw2s_import_status ) && key_exists( 'id', $viw2s_import_status ) ) { |
| 761 | $list_products_imported[ $shopify_product_id ] = $viw2s_item_product["id"]; //Set to file array('shopify_id'=>'woo_id') |
| 762 | } |
| 763 | |
| 764 | /*log to file list_product_imported*/ |
| 765 | file_put_contents( $list_products_imported_path, wp_json_encode( $list_products_imported ) );// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 766 | |
| 767 | /*log to file ids_product_imported*/ |
| 768 | $ids_products_imported[] = $viw2s_item_product['id']; |
| 769 | $ids_products_imported = array_unique( $ids_products_imported ); |
| 770 | file_put_contents( $ids_products_imported_path, wp_json_encode( $ids_products_imported ) );// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 771 | break; |
| 772 | case 'attribute_has_any_product': |
| 773 | case 'dont_has_any_product': |
| 774 | $viw2s_import_status = 'error'; |
| 775 | $log['product_url'] = get_edit_post_link( $viw2s_item_product["id"] ); |
| 776 | |
| 777 | $log['message'] = esc_html__( 'Products with variants have not been entered correctly', 'w2s-migrate-woo-to-shopify' ); |
| 778 | |
| 779 | $viw2s_import_product_progress_label = sprintf( esc_html( 'Importing... %1$s /%2$s was skipped' ), $product_index, $viw2s_total_product ); |
| 780 | break; |
| 781 | case 'too_much_attributes': |
| 782 | $viw2s_import_status = 'error'; |
| 783 | $log['product_url'] = get_edit_post_link( $viw2s_item_product["id"] ); |
| 784 | |
| 785 | $log['message'] = esc_html__( 'products with attribute count more than 3', 'w2s-migrate-woo-to-shopify' ); |
| 786 | |
| 787 | |
| 788 | $viw2s_import_product_progress_label = sprintf( esc_html( 'Importing... %1$s /%2$s was skipped' ), $product_index, $viw2s_total_product ); |
| 789 | break; |
| 790 | case 'exist': |
| 791 | $_w2s_shopify_data = get_post_meta( $viw2s_item_product["id"], '_w2s_shopify_data', true ); |
| 792 | if ( |
| 793 | ! empty( $_w2s_shopify_data ) && |
| 794 | is_array( $_w2s_shopify_data ) && |
| 795 | isset( $_w2s_shopify_data[ $domain ] ) && |
| 796 | isset( $_w2s_shopify_data[ $domain ]['_w2s_shopify_product_id'] ) |
| 797 | ) { |
| 798 | $log['product_url'] = esc_url( $domain . '/admin/products/' . $_w2s_shopify_data[ $domain ]['_w2s_shopify_product_id'] ); |
| 799 | } |
| 800 | $viw2s_import_status = 'exist'; |
| 801 | |
| 802 | $log['message'] = esc_html__( 'The product has been imported to shopify', 'w2s-migrate-woo-to-shopify' ); |
| 803 | $viw2s_import_product_progress_label = sprintf( esc_html( 'Importing... %1$s /%2$s was skipped' ), $product_index, $viw2s_total_product ); |
| 804 | break; |
| 805 | } |
| 806 | |
| 807 | } catch ( Exception $exception ) { |
| 808 | $viw2s_import_status = $exception->getMessage(); |
| 809 | $error_product_code = $exception->getCode(); |
| 810 | $viw2s_import_product_progress_label = sprintf( esc_html( 'Importing... %1$s /%2$s error' ), $product_index, $viw2s_total_product ); |
| 811 | $status = 'error_access_scope'; |
| 812 | $log['message'] = $viw2s_import_status; |
| 813 | if ( $error_product_code === 422 ) { |
| 814 | $status = 'skipped'; |
| 815 | } else if ( $error_product_code === 429 ) { |
| 816 | $status = 'retry'; |
| 817 | $product_index = $product_index - 1; |
| 818 | break; |
| 819 | } |
| 820 | } |
| 821 | $logs_content = $log['title'] . ": " . $log['message'] . ", Shopify product ID: " . $log['shopify_id'] . ", WC product ID: " . $log['woo_id']; |
| 822 | VI_W2S_IMPORT_WOOCOMMERCE_TO_SHOPIFY_DATA::viw2s_log( $log_file, $logs_content ); |
| 823 | |
| 824 | if ( $status === 'error_access_scope' ) { |
| 825 | $logs = '<strong class="important_alert"> ' . $viw2s_import_status . '</strong>'; |
| 826 | } else { |
| 827 | $logs .= ' <div>' . $log['title'] . ': <strong > ' . $log['message'] . ' .</strong > ' . ( isset( $log['product_url'] ) ? ' <a href = "' . esc_url( $log['product_url'] ) . '" target = "_blank" rel = "nofollow" > View & edit </a> ' : '' ) . ' </div> '; |
| 828 | } |
| 829 | } else { |
| 830 | $logs = '/*============================================================================*/'; |
| 831 | $response = array( |
| 832 | 'status' => 'finish', |
| 833 | 'logs' => $logs, |
| 834 | 'imported_products' => $product_index, |
| 835 | 'code' => 'no_data', |
| 836 | 'message' => sprintf( esc_html( 'Completed %1$s /%2$s ' ), $viw2s_total_product, $viw2s_total_product ), |
| 837 | ); |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | } |
| 842 | |
| 843 | if ( $status !== 'successful' ) { |
| 844 | $viw2s_import_product_progress_label = sprintf( esc_html( 'Importing... %1$s / %2$s error' ), $product_index, $viw2s_total_product ); |
| 845 | } else if ( $status === 'successful' ) { |
| 846 | $viw2s_import_product_progress_label = sprintf( esc_html( 'Importing... %1$s /%2$s completed' ), $product_index, $viw2s_total_product ); |
| 847 | } |
| 848 | |
| 849 | if ( empty( $response ) ) { |
| 850 | $response = array( |
| 851 | 'logs' => $logs, |
| 852 | 'status' => $status, |
| 853 | 'imported_products' => $product_index, |
| 854 | 'total_products' => $viw2s_total_product, |
| 855 | // 'current_import_product_id' => $viw2s_item_product["id"], |
| 856 | 'viw2s_get_all_product_data' => $arr_item_product_data, |
| 857 | 'viw2s_import_product_result' => $arr_item_product_result, |
| 858 | 'message' => $viw2s_import_product_progress_label, |
| 859 | ); |
| 860 | } |
| 861 | |
| 862 | } else { |
| 863 | $response = array( |
| 864 | 'status' => 'error', |
| 865 | 'code' => 'no_data', |
| 866 | 'message' => esc_html__( 'Import error', 'w2s-migrate-woo-to-shopify' ), |
| 867 | |
| 868 | ); |
| 869 | } |
| 870 | |
| 871 | break; |
| 872 | case 'product_categories': |
| 873 | $get_all_importing_arr_product_categories = get_option( 'viw2s_importing_arr_product_categories', array() ); |
| 874 | $categories_index = isset( $_POST['categories_index'] ) ? wc_clean( wp_unslash( $_POST['categories_index'] ) ) : ''; |
| 875 | $categories_import_status = isset( $_POST['status'] ) ? wc_clean( wp_unslash( $_POST['status'] ) ) : ''; |
| 876 | $viw2s_import_product_cat_progress_label = 'Importing...'; |
| 877 | $logs = ''; |
| 878 | $log = array( |
| 879 | 'shopify_id' => '', |
| 880 | 'woo_product_cat_id' => '', |
| 881 | 'title' => '', |
| 882 | 'message' => '', |
| 883 | ); |
| 884 | $product_category_log = $path . 'product_categories.txt'; |
| 885 | $list_product_categories_imported_path = $path . '/list_product_categories_imported.txt'; |
| 886 | $list_product_categories_imported = []; |
| 887 | if ( is_file( $list_product_categories_imported_path ) ) { |
| 888 | $list_product_categories_imported = json_decode( file_get_contents( $list_product_categories_imported_path ), true );// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 889 | } |
| 890 | if ( |
| 891 | ! empty( $get_all_importing_arr_product_categories ) && |
| 892 | ( $categories_index !== '' ) |
| 893 | ) { |
| 894 | $viw2s_total_product_categories = count( $get_all_importing_arr_product_categories ); |
| 895 | $categories_index ++; |
| 896 | |
| 897 | if ( $categories_index <= $viw2s_total_product_categories ) { |
| 898 | $viw2s_item_product_cat = $get_all_importing_arr_product_categories[ $categories_index - 1 ]; |
| 899 | $viw2s_item_product_cat_data = $this->setting->viw2s_format_product_cats( $viw2s_item_product_cat ); |
| 900 | $title = esc_html( $viw2s_item_product_cat['title'] ); |
| 901 | $viw2s_import_product_cat_status = ''; |
| 902 | $code = ''; |
| 903 | $status = ''; |
| 904 | $log['woo_product_cat_id'] = $viw2s_item_product_cat['term_id']; |
| 905 | $log['title'] = $title; |
| 906 | if ( $viw2s_item_product_cat['status'] === 'success' ) { |
| 907 | try { |
| 908 | $graphQL = " |
| 909 | mutation collectionUpdate(\$input: CollectionInput!) { |
| 910 | collectionCreate(input: \$input) { |
| 911 | collection { |
| 912 | id |
| 913 | products(first: 100 ) { |
| 914 | nodes { |
| 915 | id |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | userErrors { |
| 920 | field |
| 921 | message |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | "; |
| 926 | /* Only exist products on shopify can add collections*/ |
| 927 | if ( ! empty( $viw2s_item_product_cat_data['products'] ) ) { |
| 928 | foreach ( $viw2s_item_product_cat_data['products'] as $product_id ) { |
| 929 | $viw2s_item_product_cat_data['products'] = [ $product_id ]; |
| 930 | $bulk_import->query( $graphQL, array( |
| 931 | 'input' => array( |
| 932 | 'handle' => $viw2s_item_product_cat_data['handle'], |
| 933 | 'title' => $viw2s_item_product_cat_data['title'], |
| 934 | 'products' => [ $product_id ] |
| 935 | ) |
| 936 | ) ); |
| 937 | } |
| 938 | } else { |
| 939 | $viw2s_import_product_cat_status = $bulk_import->query( $graphQL, array( 'input' => $viw2s_item_product_cat_data ) ); |
| 940 | } |
| 941 | |
| 942 | if ( ! empty( $viw2s_import_product_cat_status['data']['collectionCreate']['userErrors'] ) ) { |
| 943 | $message = $viw2s_import_product_cat_status['data']['collectionCreate']['userErrors'][0]['message']; |
| 944 | $field_error = $viw2s_import_product_cat_status['data']['collectionCreate']['userErrors'][0]['field']; |
| 945 | throw new Exception( "{$title} - {$message}", 422 ); |
| 946 | } |
| 947 | $status = 'success'; |
| 948 | $log['message'] = esc_html__( 'Import successfully', 'w2s-migrate-woo-to-shopify' ); |
| 949 | $shopify_collection_id = esc_html( $viw2s_import_product_cat_status['data']['collectionCreate']['collection']['id'] ?? '' ); |
| 950 | $log['shopify_id'] = basename( $shopify_collection_id ); |
| 951 | $viw2s_import_product_cat_progress_label = sprintf( esc_html( 'Importing... %1$s /%2$s completed' ), $categories_index, $viw2s_total_product_categories ); |
| 952 | $_w2s_shopify_cats_data = get_term_meta( $viw2s_item_product_cat['term_id'], '_w2s_shopify_data', true ); |
| 953 | if ( ! empty( $_w2s_shopify_cats_data ) && is_array( $_w2s_shopify_cats_data ) ) { |
| 954 | $_w2s_shopify_cats_data[ $domain ] = array( |
| 955 | '_w2s_shopify_product_cat_id' => $shopify_collection_id, |
| 956 | 'status' => 'imported' |
| 957 | ); |
| 958 | update_term_meta( $viw2s_item_product_cat['term_id'], '_w2s_shopify_data', $_w2s_shopify_cats_data ); |
| 959 | } else { |
| 960 | update_term_meta( $viw2s_item_product_cat['term_id'], '_w2s_shopify_data', array( |
| 961 | $domain => array( |
| 962 | '_w2s_shopify_product_cat_id' => $shopify_collection_id, |
| 963 | 'status' => 'imported' |
| 964 | ) |
| 965 | ) ); |
| 966 | } |
| 967 | /*Clean $list_product_categories_imported */ |
| 968 | if ( ! empty( $list_product_categories_imported ) && is_array( $list_product_categories_imported ) ) { |
| 969 | $list_product_categories_imported = array_unique( $list_product_categories_imported ); |
| 970 | $key_old_not_exist = array_search( $viw2s_item_product_cat["term_id"], $list_product_categories_imported ); |
| 971 | if ( $key_old_not_exist ) { |
| 972 | unset( $list_product_categories_imported[ $key_old_not_exist ] ); |
| 973 | } |
| 974 | } |
| 975 | $list_product_categories_imported[ $shopify_collection_id ] = $viw2s_item_product_cat['term_id']; |
| 976 | /*log to file list_product_categories_imported*/ |
| 977 | file_put_contents( $list_product_categories_imported_path, wp_json_encode( $list_product_categories_imported ) );// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 978 | } catch ( Exception $exception ) { |
| 979 | $code = $exception->getCode(); |
| 980 | $viw2s_import_product_cat_status = $exception->getMessage(); |
| 981 | $status = 'error'; |
| 982 | $log['message'] = esc_html( $exception->getMessage() ); |
| 983 | $viw2s_import_product_cat_progress_label = sprintf( esc_html( 'Importing... %1$s /%2$s error' ), $categories_index, $viw2s_total_product_categories ); |
| 984 | |
| 985 | } |
| 986 | } else if ( $viw2s_item_product_cat['status'] == 'exist' ) { |
| 987 | $viw2s_import_product_cat_progress_label = sprintf( esc_html( 'Importing... %1$s /%2$s skipped' ), $categories_index, $viw2s_total_product_categories ); |
| 988 | $log['message'] = esc_html__( 'This product category has been imported to shopify', 'w2s-migrate-woo-to-shopify' ); |
| 989 | $viw2s_import_product_cat_status = get_term_meta( $viw2s_item_product_cat['term_id'], '_w2s_shopify_data', true ); |
| 990 | $status = 'exist'; |
| 991 | } else { |
| 992 | $viw2s_import_product_cat_progress_label = sprintf( esc_html( 'Importing... %1$s /%2$s skipped' ), $categories_index, $viw2s_total_product_categories ); |
| 993 | $log['message'] = esc_html__( 'This product category has been import error ', 'w2s-migrate-woo-to-shopify' ); |
| 994 | $viw2s_import_product_cat_status = get_term_meta( $viw2s_item_product_cat['term_id'], '_w2s_shopify_data', true ); |
| 995 | $status = 'error'; |
| 996 | } |
| 997 | $logs .= ' <div>' . $log['title'] . ': <strong > ' . $log['message'] . ' .</strong ></div> '; |
| 998 | $response = array( |
| 999 | 'status' => $status, |
| 1000 | 'logs' => $logs, |
| 1001 | 'categories_index' => $categories_index, |
| 1002 | 'total_categories' => $viw2s_total_product_categories, |
| 1003 | 'viw2s_import_product_cat_status' => $viw2s_import_product_cat_status, |
| 1004 | 'viw2s_item_product_cat_data' => $viw2s_item_product_cat_data, |
| 1005 | 'code' => $code, |
| 1006 | 'message' => $viw2s_import_product_cat_progress_label, |
| 1007 | ); |
| 1008 | } else { |
| 1009 | $logs = '/*============================================================================*/'; |
| 1010 | $response = array( |
| 1011 | 'status' => 'finish', |
| 1012 | 'logs' => $logs, |
| 1013 | 'categories_index' => $categories_index - 1, |
| 1014 | 'total_categories' => $viw2s_total_product_categories, |
| 1015 | 'code' => 'no_data', |
| 1016 | 'message' => sprintf( esc_html( 'Completed %1$s /%2$s ' ), $categories_index - 1, $viw2s_total_product_categories ), |
| 1017 | ); |
| 1018 | } |
| 1019 | } else { |
| 1020 | $response = array( |
| 1021 | 'status' => 'error', |
| 1022 | 'code' => 'no_data', |
| 1023 | 'message' => esc_html__( 'Import error, please try again! ', 'w2s-migrate-woo-to-shopify' ), |
| 1024 | |
| 1025 | ); |
| 1026 | } |
| 1027 | break; |
| 1028 | default: |
| 1029 | throw new \Exception( 'Unexpected value' ); |
| 1030 | } |
| 1031 | } else { |
| 1032 | $logs = '<strong class="important_alert"> ' . sprintf( esc_html__( 'API permission must have %s permission', 'w2s-migrate-woo-to-shopify' ), implode( ', ', $missing_scopes ) ) . '</strong>'; |
| 1033 | $response = array( |
| 1034 | 'status' => 'error_access_scope', |
| 1035 | 'logs' => $logs, |
| 1036 | 'code' => 'no_data', |
| 1037 | 'message' => sprintf( esc_html__( 'API permission must have %s permission', 'w2s-migrate-woo-to-shopify' ), implode( ', ', $missing_scopes ) ), |
| 1038 | ); |
| 1039 | } |
| 1040 | |
| 1041 | } else { |
| 1042 | $logs = '<strong class="important_alert"> ' . esc_html( $viw2s_get_api_scope['data'] ?? 'Store setting error' ) . '</strong>'; |
| 1043 | $response = array( |
| 1044 | 'status' => 'error_store_setting', |
| 1045 | 'logs' => $logs, |
| 1046 | 'code' => 'no_data', |
| 1047 | 'message' => esc_html__( 'Store setting error', 'w2s-migrate-woo-to-shopify' ), |
| 1048 | ); |
| 1049 | } |
| 1050 | |
| 1051 | } else { |
| 1052 | $logs = '<strong class="important_alert"> ' . esc_html__( 'Import nonce error', 'w2s-migrate-woo-to-shopify' ) . '</strong>'; |
| 1053 | $response = array( |
| 1054 | 'status' => 'error_none', |
| 1055 | 'logs' => $logs, |
| 1056 | 'code' => 'no_data', |
| 1057 | 'message' => esc_html__( 'Import nonce error', 'w2s-migrate-woo-to-shopify' ), |
| 1058 | ); |
| 1059 | } |
| 1060 | wp_send_json( $response ); |
| 1061 | die(); |
| 1062 | } |
| 1063 | |
| 1064 | public function print_log_html( $logs ) { |
| 1065 | if ( is_array( $logs ) && count( $logs ) ) { |
| 1066 | foreach ( $logs as $log ) { |
| 1067 | ?> |
| 1068 | <p><?php echo esc_html( $log ) ?> |
| 1069 | <a target="_blank" rel="nofollow" |
| 1070 | href="<?php echo esc_url( add_query_arg( array( |
| 1071 | 'action' => 'viw2s_view_log', |
| 1072 | 'viw2s_file' => urlencode( $log ), |
| 1073 | '_wpnonce' => wp_create_nonce( 'viw2s_view_log' ), |
| 1074 | ), admin_url( 'admin-ajax.php' ) ) ) ?>"><?php esc_html_e( 'View', 'w2s-migrate-woo-to-shopify' ) ?> |
| 1075 | </a> |
| 1076 | </p> |
| 1077 | <?php |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | public function generate_log_ajax() { |
| 1083 | /*Check the nonce*/ |
| 1084 | if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'viw2s_view_log' ) || ! current_user_can( 'manage_woocommerce' ) ) { |
| 1085 | wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'w2s-migrate-woo-to-shopify' ) ); |
| 1086 | } |
| 1087 | |
| 1088 | if ( empty( $_GET['viw2s_file'] ) ) { |
| 1089 | wp_die( esc_html__( 'No log file selected.', 'w2s-migrate-woo-to-shopify' ) ); |
| 1090 | } |
| 1091 | |
| 1092 | $allow_file_names = [ 'logs', 'products', 'product_categories', 'ids_product_imported', 'list_products_imported', 'list_product_categories_imported' ]; |
| 1093 | $file = urldecode( wc_clean( $_GET['viw2s_file'] ) ); |
| 1094 | $path_info = pathinfo( $file ); |
| 1095 | |
| 1096 | if ( ! is_file( $file ) || ! in_array( $path_info['filename'], $allow_file_names ) ) { |
| 1097 | wp_die( esc_html__( 'Log file not found.', 'w2s-migrate-woo-to-shopify' ) ); |
| 1098 | } |
| 1099 | echo( wp_kses_post( nl2br( file_get_contents( $file ) ) ) );// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 1100 | |
| 1101 | exit(); |
| 1102 | } |
| 1103 | } |
| 1104 |