30, 'aieo_dmm_search_brand_terms' => 30, 'aieo_dmm_search_categories' => 30, 'aieo_dmm_search_need_terms' => 50, ); private static $dialogs = array(); public static function register_ajax() { add_action( 'wp_ajax_' . self::ACTION_SEARCH, array( __CLASS__, 'ajax_search' ) ); add_action( 'wp_ajax_' . self::ACTION_VARIATIONS, array( __CLASS__, 'ajax_variations' ) ); } public static function render_field( array $args ) { $name = isset( $args['name'] ) ? (string) $args['name'] : ''; if ( $name === '' ) { return ''; } $mode = ( isset( $args['mode'] ) && $args['mode'] === 'exclude' ) ? 'exclude' : 'include'; $field = isset( $args['field'] ) && $args['field'] !== '' ? (string) $args['field'] : $name; $label = isset( $args['label'] ) ? (string) $args['label'] : ''; $help = isset( $args['help'] ) ? (string) $args['help'] : ''; $btn = isset( $args['button'] ) && $args['button'] !== '' ? (string) $args['button'] : __( 'Choose products', 'ai-eshop-optimizer' ); $empty = isset( $args['empty_text'] ) && $args['empty_text'] !== '' ? (string) $args['empty_text'] : ( $mode === 'exclude' ? __( 'None excluded', 'ai-eshop-optimizer' ) : __( 'All products', 'ai-eshop-optimizer' ) ); $key = sanitize_html_class( str_replace( array( '[', ']' ), '', $field ) ); $id = 'aieo-pick-' . $key; $picker = 'aieo-picker-' . $key; $form = isset( $args['form'] ) ? (string) $args['form'] : ''; $action = ! empty( $args['search_action'] ) ? (string) $args['search_action'] : self::ACTION_SEARCH; $title = ! empty( $args['modal_title'] ) ? (string) $args['modal_title'] : ( ( $mode === 'exclude' || $field === 'exclude_ids' ) ? __( 'Choose products to EXCLUDE', 'ai-eshop-optimizer' ) : __( 'Choose products to INCLUDE', 'ai-eshop-optimizer' ) ); $selected = array(); if ( isset( $args['selected'] ) && is_array( $args['selected'] ) ) { $selected = array_values( array_filter( array_map( 'absint', $args['selected'] ) ) ); } if ( isset( $args['items'] ) && is_array( $args['items'] ) ) { $pairs = $args['items']; } else { $pairs = $selected ? self::hydrate_ids( $selected ) : array(); } $single = isset( $args['multiple'] ) && ! $args['multiple']; $island = ! empty( $args['island'] ); $chosen = self::chosen_items( $pairs ); if ( $single ) { $chosen = array_slice( $chosen, 0, 1 ); } $opts = array( 'name' => $name, 'form' => $form, 'search' => ! empty( $args['placeholder'] ) ? (string) $args['placeholder'] : __( 'Search products by name or SKU…', 'ai-eshop-optimizer' ), 'none' => ! empty( $args['none_text'] ) ? (string) $args['none_text'] : __( 'No products found.', 'ai-eshop-optimizer' ), 'empty' => $empty, 'wide' => true, 'source' => self::source( $action, $args ), ); if ( $single ) { $opts['multiple'] = false; } if ( $island ) { $opts['island'] = true; } $dialog = \Rooster\Admin\Ui::picker( $picker, $title, $chosen, $opts ); $chips = ''; foreach ( $chosen as $item ) { $chips .= '' . esc_html( $item['label'] ) . ''; } $control = '
' . '' . ( '' !== $chips ? $chips : esc_html( $empty ) ) . '' . '' . '
'; $after = ''; if ( '' !== $form ) { self::queue_dialog( $dialog ); } elseif ( $island ) { $after = $dialog; } else { $control .= $dialog; } $html = ! empty( $args['bare'] ) ? $control : AIEO_Kit::row( $label, $control, $help, array( 'id' => $id ) ); if ( $island ) { $html = '
' . $html . '
' . $after; } return $html; } public static function dialogs() { $html = implode( '', self::$dialogs ); self::$dialogs = array(); return $html; } public static function print_dialogs() { echo self::dialogs(); } private static function queue_dialog( $dialog ) { self::$dialogs[] = (string) $dialog; if ( function_exists( 'has_action' ) && false === has_action( 'admin_footer', array( __CLASS__, 'print_dialogs' ) ) ) { add_action( 'admin_footer', array( __CLASS__, 'print_dialogs' ) ); } } private static function source( $action, array $args ) { $params = array( 'nonce' => wp_create_nonce( class_exists( 'AIEO_DMM' ) ? AIEO_DMM::NONCE_ACTION : 'aieo_dmm_nonce' ) ); if ( ! empty( $args['extra_params'] ) ) { $extra = array(); parse_str( ltrim( (string) $args['extra_params'], '&' ), $extra ); foreach ( $extra as $key => $value ) { if ( is_scalar( $value ) && 'nonce' !== $key ) { $params[ $key ] = (string) $value; } } } $source = array( 'url' => admin_url( 'admin-ajax.php' ), 'action' => $action, 'params' => $params, 'items' => 'results', 'keys' => array( 'value' => 'id', 'label' => 'text', 'children' => '' ), ); if ( isset( self::SEARCH_LIMITS[ $action ] ) ) { $source['limit'] = self::SEARCH_LIMITS[ $action ]; } if ( self::ACTION_SEARCH === $action ) { $source['keys']['children'] = 'n_variations'; $source['children'] = array( 'action' => ! empty( $args['variations_action'] ) ? (string) $args['variations_action'] : self::ACTION_VARIATIONS, 'param' => 'parent_id', ); } return $source; } public static function chosen_items( array $pairs ) { $out = array(); foreach ( $pairs as $pair ) { $pid = is_array( $pair ) && isset( $pair['id'] ) ? (int) $pair['id'] : 0; if ( $pid <= 0 ) { continue; } $text = isset( $pair['text'] ) && '' !== (string) $pair['text'] ? (string) $pair['text'] : '#' . $pid; $status = isset( $pair['status'] ) ? (string) $pair['status'] : ''; if ( 'missing' === $status ) { $text .= ' (' . __( 'deleted', 'ai-eshop-optimizer' ) . ')'; } elseif ( 'draft' === $status ) { $text .= ' · ' . __( 'draft', 'ai-eshop-optimizer' ); } $out[] = array( 'value' => (string) $pid, 'label' => $text, 'parent' => ! empty( $pair['parent_id'] ) ? (string) (int) $pair['parent_id'] : '', ); } return $out; } public static function entities() { return array( 'product' => array( 'action' => self::ACTION_SEARCH, 'button' => __( 'Choose products', 'ai-eshop-optimizer' ), 'title' => __( 'Choose products', 'ai-eshop-optimizer' ), 'search' => __( 'Search by name or SKU…', 'ai-eshop-optimizer' ), 'none' => __( 'No products found.', 'ai-eshop-optimizer' ), 'empty' => __( 'All products', 'ai-eshop-optimizer' ), 'emptyx' => __( 'None excluded', 'ai-eshop-optimizer' ), ), 'brand' => array( 'action' => 'aieo_dmm_search_brand_terms', 'button' => __( 'Choose brands', 'ai-eshop-optimizer' ), 'title' => __( 'Choose brands', 'ai-eshop-optimizer' ), 'search' => __( 'Search brands…', 'ai-eshop-optimizer' ), 'none' => __( 'No brands found.', 'ai-eshop-optimizer' ), 'empty' => __( 'Any brand', 'ai-eshop-optimizer' ), 'emptyx' => __( 'None excluded', 'ai-eshop-optimizer' ), ), 'category' => array( 'action' => 'aieo_dmm_search_categories', 'button' => __( 'Choose categories', 'ai-eshop-optimizer' ), 'title' => __( 'Choose categories', 'ai-eshop-optimizer' ), 'search' => __( 'Search categories…', 'ai-eshop-optimizer' ), 'none' => __( 'No categories found.', 'ai-eshop-optimizer' ), 'empty' => __( 'Any category', 'ai-eshop-optimizer' ), 'emptyx' => __( 'None excluded', 'ai-eshop-optimizer' ), ), 'need' => array( 'action' => 'aieo_dmm_search_need_terms', 'button' => __( 'Choose needs', 'ai-eshop-optimizer' ), 'title' => __( 'Choose needs', 'ai-eshop-optimizer' ), 'search' => __( 'Search needs…', 'ai-eshop-optimizer' ), 'none' => __( 'No needs found.', 'ai-eshop-optimizer' ), 'empty' => __( 'Any need', 'ai-eshop-optimizer' ), 'emptyx' => __( 'None excluded', 'ai-eshop-optimizer' ), ), ); } public static function entity_field( $entity, array $args = array() ) { $all = self::entities(); $e = isset( $all[ $entity ] ) ? $all[ $entity ] : $all['product']; $exc = ( isset( $args['mode'] ) && $args['mode'] === 'exclude' ); $defaults = array( 'search_action' => $e['action'], 'button' => $e['button'], 'modal_title' => $e['title'], 'placeholder' => $e['search'], 'none_text' => $e['none'], 'empty_text' => $exc ? $e['emptyx'] : $e['empty'], ); if ( $entity !== 'product' && ! isset( $args['items'] ) && empty( $args['selected'] ) ) { $defaults['items'] = array(); } return self::render_field( array_merge( $defaults, $args ) ); } public static function collect_ids( $field ) { $raw = isset( $_POST[ $field ] ) ? wp_unslash( $_POST[ $field ] ) : array(); if ( ! is_array( $raw ) ) { $raw = array( $raw ); } $ids = array_map( 'absint', $raw ); $ids = array_filter( $ids ); return array_values( array_unique( $ids ) ); } public static function roll_up_to_parents( array $ids ) { $out = array(); foreach ( $ids as $id ) { $id = (int) $id; if ( $id <= 0 ) { continue; } if ( get_post_type( $id ) === 'product_variation' ) { $parent = (int) wp_get_post_parent_id( $id ); if ( $parent > 0 ) { $id = $parent; } } $out[ $id ] = $id; } return array_values( $out ); } public static function ajax_search() { AIEO_DMM::verify_ajax_request(); $q = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : ''; wp_send_json( array( 'results' => self::search_products_raw( $q, 30 ) ) ); } public static function ajax_variations() { AIEO_DMM::verify_ajax_request(); $parent_id = isset( $_GET['parent_id'] ) ? absint( wp_unslash( $_GET['parent_id'] ) ) : 0; wp_send_json( array( 'results' => self::get_variations( $parent_id ) ) ); } public static function get_variations( $parent_id ) { $parent_id = (int) $parent_id; $out = array(); if ( $parent_id && function_exists( 'wc_get_product' ) ) { $parent = wc_get_product( $parent_id ); if ( $parent && $parent->is_type( 'variable' ) ) { foreach ( $parent->get_children() as $vid ) { $v = wc_get_product( $vid ); if ( ! $v ) { continue; } $out[] = array( 'id' => (int) $vid, 'text' => self::variation_label( $v ), 'status' => $v->get_status(), ); } } } return $out; } public static function variation_label( $v ) { $attrs = function_exists( 'wc_get_formatted_variation' ) ? (string) wc_get_formatted_variation( $v, true ) : ''; $label = $attrs !== '' ? $attrs : ( '#' . (int) $v->get_id() ); if ( $v->get_sku() ) { $label .= ' [' . $v->get_sku() . ']'; } return $label; } public static function hydrate_ids( array $list ) { $out = array(); foreach ( $list as $entry ) { $pid = (int) $entry; if ( $pid <= 0 ) { continue; } $post = get_post( $pid ); if ( ! $post || ( $post->post_type !== 'product' && $post->post_type !== 'product_variation' ) ) { $out[] = array( 'id' => $pid, 'text' => '#' . $pid, 'type' => 'missing', 'status' => 'missing' ); continue; } if ( $post->post_type === 'product_variation' ) { $parent_id = (int) $post->post_parent; $v = function_exists( 'wc_get_product' ) ? wc_get_product( $pid ) : null; $label = $v ? self::variation_label( $v ) : ( '#' . $pid ); $pname = $parent_id ? (string) get_the_title( $parent_id ) : ''; $text = ( $pname !== '' ? $pname : ( '#' . $parent_id ) ) . ' — ' . $label; $status = $parent_id ? (string) get_post_status( $parent_id ) : $post->post_status; $out[] = array( 'id' => $pid, 'text' => $text, 'type' => 'variation', 'status' => $status, 'parent_id' => $parent_id ); continue; } $p = function_exists( 'wc_get_product' ) ? wc_get_product( $pid ) : null; $is_var = $p && $p->is_type( 'variable' ); $title = (string) get_the_title( $pid ); $sku = $p ? (string) $p->get_sku() : ''; $text = ( $title !== '' ? $title : ( '#' . $pid ) ) . ( $sku !== '' ? ' [' . $sku . ']' : '' ); $out[] = array( 'id' => $pid, 'text' => $text, 'type' => $is_var ? 'variable' : 'simple', 'status' => $post->post_status, ); } return $out; } public static function search_products_raw( $q, $limit = 30 ) { if ( ! function_exists( 'wc_get_product' ) ) { return array(); } global $wpdb; $limit = max( 1, (int) $limit ); $q = trim( (string) $q ); if ( $q === '' ) { $ids = $wpdb->get_col( $wpdb->prepare( "SELECT p.ID FROM {$wpdb->posts} p WHERE p.post_type = 'product' AND p.post_status IN ('publish','draft','private') GROUP BY p.ID ORDER BY (p.post_status='publish') DESC, p.post_title ASC LIMIT %d", $limit ) ); } else { $like = '%' . $wpdb->esc_like( $q ) . '%'; $brand_tt = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM {$wpdb->term_taxonomy} tt INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id WHERE tt.taxonomy = 'product_brand' AND t.name LIKE %s LIMIT 20", $like ) ); $brand_sql = ''; $params = array( $like, $like ); if ( ! empty( $brand_tt ) ) { $ph = implode( ',', array_fill( 0, count( $brand_tt ), '%d' ) ); $brand_sql = " OR p.ID IN (SELECT tr.object_id FROM {$wpdb->term_relationships} tr WHERE tr.term_taxonomy_id IN ($ph)) "; $params = array_merge( $params, array_map( 'intval', $brand_tt ) ); } $params[] = $limit; $ids = $wpdb->get_col( $wpdb->prepare( "SELECT p.ID FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} sku ON sku.post_id = p.ID AND sku.meta_key = '_sku' WHERE p.post_type = 'product' AND p.post_status IN ('publish','draft','private') AND ( p.post_title LIKE %s OR sku.meta_value LIKE %s {$brand_sql} ) GROUP BY p.ID ORDER BY (p.post_status='publish') DESC, p.post_title ASC LIMIT %d", $params ) ); } $results = array(); foreach ( (array) $ids as $pid ) { $p = wc_get_product( (int) $pid ); if ( ! $p ) { continue; } $is_variable = $p->is_type( 'variable' ); $results[] = array( 'id' => (int) $pid, 'text' => $p->get_name() . ( $p->get_sku() ? ' [' . $p->get_sku() . ']' : '' ), 'name' => $p->get_name(), 'type' => $is_variable ? 'variable' : 'simple', 'status' => $p->get_status(), 'n_variations' => $is_variable ? count( $p->get_children() ) : 0, ); } return $results; } }