' . esc_html__( 'Settings', 'affilora-importer-for-woocommerce' ) . '' ); return $links; } public static function menu() { add_menu_page( __( 'Affiliate Importer', 'affilora-importer-for-woocommerce' ), __( 'Affiliate Importer', 'affilora-importer-for-woocommerce' ), self::CAP, 'afli-import', array( __CLASS__, 'render_import' ), 'dashicons-amazon', 56 ); add_submenu_page( 'afli-import', __( 'Search & Import', 'affilora-importer-for-woocommerce' ), __( 'Search & Import', 'affilora-importer-for-woocommerce' ), self::CAP, 'afli-import', array( __CLASS__, 'render_import' ) ); add_submenu_page( 'afli-import', __( 'Settings', 'affilora-importer-for-woocommerce' ), __( 'Settings', 'affilora-importer-for-woocommerce' ), self::CAP, 'afli-settings', array( __CLASS__, 'render_settings' ) ); } public static function register_settings() { register_setting( 'afli_settings_group', AFLI_Settings::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( 'AFLI_Settings', 'sanitize' ), 'default' => AFLI_Settings::defaults(), ) ); } /** * When affiliate IDs change, re-tag every imported product so commissions follow the new ID. */ public static function on_settings_saved( $old, $new ) { $old_tags = ( is_array( $old ) && isset( $old['partner_tags'] ) && is_array( $old['partner_tags'] ) ) ? $old['partner_tags'] : array(); $new_tags = ( is_array( $new ) && isset( $new['partner_tags'] ) && is_array( $new['partner_tags'] ) ) ? $new['partner_tags'] : array(); if ( $old_tags !== $new_tags ) { set_transient( 'afli_retag_' . get_current_user_id(), AFLI_Importer::retag_all(), 300 ); } } public static function assets( $hook ) { if ( false === strpos( $hook, 'afli-' ) ) { return; } wp_enqueue_style( 'afli-admin', AFLI_URL . 'assets/admin.css', array(), AFLI_VERSION ); wp_enqueue_script( 'afli-admin', AFLI_URL . 'assets/admin.js', array(), AFLI_VERSION, true ); wp_localize_script( 'afli-admin', 'AFLI', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'afli_nonce' ), 'store_currency' => get_woocommerce_currency(), 'import_prices' => (bool) AFLI_Settings::get( 'import_prices' ), 'currencies' => wp_list_pluck( AFLI_Countries::all(), 'currency' ), 'i18n' => array( 'error' => __( 'Something went wrong. Please try again.', 'affilora-importer-for-woocommerce' ), 'searching' => __( 'Searching Amazon...', 'affilora-importer-for-woocommerce' ), 'loading' => __( 'Getting products...', 'affilora-importer-for-woocommerce' ), 'enter_keywords' => __( 'Please type what you want to search for.', 'affilora-importer-for-woocommerce' ), 'enter_asins' => __( 'No valid ASINs found. Paste ASINs (like B0XXXXXXXX) or Amazon product links.', 'affilora-importer-for-woocommerce' ), 'no_results' => __( 'No products found. Try different words.', 'affilora-importer-for-woocommerce' ), 'showing' => __( 'Showing %1$s products (about %2$s results on Amazon).', 'affilora-importer-for-woocommerce' ), 'showing_simple' => __( 'Showing %s products.', 'affilora-importer-for-woocommerce' ), 'not_found' => __( '%s product(s) were not found on Amazon.', 'affilora-importer-for-woocommerce' ), 'selected' => __( '%s selected', 'affilora-importer-for-woocommerce' ), 'import_n' => __( 'Import %s products', 'affilora-importer-for-woocommerce' ), 'importing' => __( 'Importing %1$s of %2$s...', 'affilora-importer-for-woocommerce' ), 'done' => __( 'Finished: %1$s created, %2$s updated, %3$s skipped, %4$s failed.', 'affilora-importer-for-woocommerce' ), 'imported' => __( 'Imported', 'affilora-importer-for-woocommerce' ), 'ad' => __( 'Ad', 'affilora-importer-for-woocommerce' ), 'edit' => __( 'Edit product', 'affilora-importer-for-woocommerce' ), 'currency_mismatch' => __( 'Currency differs from your store - price will not be imported.', 'affilora-importer-for-woocommerce' ), 'testing' => __( 'Checking your key...', 'affilora-importer-for-woocommerce' ), 'show' => __( 'Show', 'affilora-importer-for-woocommerce' ), 'hide' => __( 'Hide', 'affilora-importer-for-woocommerce' ), ), ) ); } /* ------------------------------------------------------------------ * Settings page * ---------------------------------------------------------------- */ private static function switch_row( $name, $checked, $title, $desc, $credit = '' ) { ?>

0 ) : ?>

Create a SerpApi account, then copy your key from the dashboard. Testing the key is free.', 'affilora-importer-for-woocommerce' ) ), esc_url( 'https://serpapi.com/users/sign_up' ), esc_url( 'https://serpapi.com/dashboard' ) ); ?>

$tag ) { self::affiliate_row( $i++, $mp, $tag ); } } ?>

$tag ) { $available[ $mp ] = $countries[ $mp ]['name']; } ?>

__( 'You do not have permission to do this.', 'affilora-importer-for-woocommerce' ) ), 403 ); } } /** * The requested country, falling back to the default. Must have an affiliate ID. */ private static function request_marketplace() { $mp = isset( $_POST['marketplace'] ) ? AFLI_Countries::normalize( sanitize_text_field( wp_unslash( $_POST['marketplace'] ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification if ( ! AFLI_Countries::is_valid( $mp ) ) { $mp = AFLI_Settings::get( 'marketplace' ); } if ( '' === AFLI_Settings::partner_tag( $mp ) ) { wp_send_json_error( array( 'message' => sprintf( /* translators: %s: country name */ __( 'No affiliate ID saved for %s. Add it on the Settings page.', 'affilora-importer-for-woocommerce' ), AFLI_Countries::name( $mp ) ), ) ); } return $mp; } private static function request_asins( $max = 5 ) { $raw = isset( $_POST['asins'] ) ? sanitize_text_field( wp_unslash( $_POST['asins'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification $asins = array(); foreach ( explode( ',', $raw ) as $asin ) { $asin = strtoupper( trim( $asin ) ); if ( preg_match( '/^[A-Z0-9]{10}$/', $asin ) ) { $asins[] = $asin; } } return array_slice( array_values( array_unique( $asins ) ), 0, $max ); } private static function mark_existing( array $items ) { foreach ( $items as &$item ) { $item['existing_id'] = AFLI_Importer::find_existing( $item['asin'], $item['marketplace'] ); } return $items; } public static function ajax_search() { self::guard(); $keywords = isset( $_POST['keywords'] ) ? sanitize_text_field( wp_unslash( $_POST['keywords'] ) ) : ''; if ( '' === $keywords ) { wp_send_json_error( array( 'message' => __( 'Please type what you want to search for.', 'affilora-importer-for-woocommerce' ) ) ); } $sort = isset( $_POST['sort_by'] ) ? sanitize_text_field( wp_unslash( $_POST['sort_by'] ) ) : ''; if ( ! isset( AFLI_Countries::sort_options()[ $sort ] ) ) { $sort = ''; } $result = AFLI_Client::search( array( 'marketplace' => self::request_marketplace(), 'keywords' => $keywords, 'sort_by' => $sort, 'page' => isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 1, ) ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } $result['items'] = self::mark_existing( $result['items'] ); wp_send_json_success( $result ); } public static function ajax_asins() { self::guard(); if ( function_exists( 'set_time_limit' ) ) { @set_time_limit( 180 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors } $asins = self::request_asins( 5 ); if ( empty( $asins ) ) { wp_send_json_error( array( 'message' => __( 'No valid ASINs found.', 'affilora-importer-for-woocommerce' ) ) ); } $marketplace = self::request_marketplace(); $items = array(); $missing = 0; foreach ( $asins as $asin ) { $item = AFLI_Client::get_product( $asin, $marketplace ); if ( is_wp_error( $item ) ) { if ( 'afli_not_found' === $item->get_error_code() ) { $missing++; continue; } wp_send_json_error( array( 'message' => $item->get_error_message() ) ); } AFLI_Client::cache_item( $item ); $items[] = $item; } wp_send_json_success( array( 'items' => self::mark_existing( $items ), 'missing' => $missing, ) ); } public static function ajax_import() { self::guard(); if ( function_exists( 'set_time_limit' ) ) { @set_time_limit( 180 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors } $asins = self::request_asins( 5 ); if ( empty( $asins ) ) { wp_send_json_error( array( 'message' => __( 'Nothing selected.', 'affilora-importer-for-woocommerce' ) ) ); } $marketplace = self::request_marketplace(); $status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : 'publish'; // phpcs:ignore WordPress.Security.NonceVerification if ( ! in_array( $status, array( 'publish', 'draft', 'pending' ), true ) ) { $status = 'publish'; } $opts = array( 'category' => isset( $_POST['category'] ) ? absint( $_POST['category'] ) : 0, // phpcs:ignore WordPress.Security.NonceVerification 'new_category' => isset( $_POST['new_category'] ) ? sanitize_text_field( wp_unslash( $_POST['new_category'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification 'status' => $status, ); $results = array(); foreach ( $asins as $asin ) { $item = AFLI_Importer::prepare( $asin, $marketplace ); if ( is_wp_error( $item ) ) { $results[] = array( 'asin' => $asin, 'title' => $asin, 'status' => 'error', 'id' => 0, 'note' => $item->get_error_message(), ); continue; } $row = AFLI_Importer::import_item( $item, $opts ); if ( ! empty( $row['id'] ) ) { $row['edit_url'] = get_edit_post_link( $row['id'], 'raw' ); } $results[] = $row; } wp_send_json_success( array( 'results' => $results ) ); } public static function ajax_test() { self::guard(); // Test the key typed in the box; if it is empty, test the saved one. $key = isset( $_POST['key'] ) ? preg_replace( '/\s+/', '', sanitize_text_field( wp_unslash( $_POST['key'] ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification if ( '' === $key ) { $key = (string) AFLI_Settings::get( 'serpapi_key' ); } if ( '' === $key ) { wp_send_json_error( array( 'message' => __( 'Paste your SerpApi API key first.', 'affilora-importer-for-woocommerce' ) ) ); } $info = AFLI_Client::account( $key ); if ( is_wp_error( $info ) ) { wp_send_json_error( array( 'message' => $info->get_error_message() ) ); } $message = __( 'Connected! Your key works.', 'affilora-importer-for-woocommerce' ); if ( null !== $info['left'] ) { $message = sprintf( /* translators: 1: plan name, 2: searches left */ __( 'Connected! %1$s %2$s searches left this month.', 'affilora-importer-for-woocommerce' ), '' !== $info['plan'] ? $info['plan'] . ' plan -' : '', number_format_i18n( $info['left'] ) ); } wp_send_json_success( array( 'message' => trim( $message ) ) ); } }