| @@ -69,9 +69,8 @@ | ||
| 69 | 69 | */ |
| 70 | 70 | private const WCPOS_SORT_PARAM_MAP = array( |
| 71 | 71 | 'orderby' => 'orderby', |
| 72 | 72 | 'order' => 'order', |
| 73 | - 'search' => 'search', | |
| 74 | 73 | ); |
| 75 | 74 | |
| 76 | 75 | /** |
| 77 | 76 | * Memoized parent collection params. |
| @@ -95,8 +94,10 @@ | ||
| 95 | 94 | |
| 96 | 95 | add_filter( 'woocommerce_rest_prepare_product_object', array( $this, 'wcpos_product_response' ), 10, 3 ); |
| 97 | 96 | add_action( 'woocommerce_rest_insert_product_object', array( $this, 'wcpos_insert_product_object' ), 10, 3 ); |
| 98 | 97 | add_filter( 'woocommerce_rest_product_object_query', array( $this, 'wcpos_product_query' ), 10, 2 ); |
| 98 | + add_filter( 'posts_search', array( $this, 'wcpos_posts_search' ), 10, 2 ); | |
| 99 | + add_filter( 'posts_clauses', array( $this, 'wcpos_posts_clauses' ), 10, 2 ); | |
| 99 | 100 | |
| 100 | 101 | /* |
| 101 | 102 | * Check if the request is for all products and if the 'posts_per_page' is set to -1. |
| 102 | 103 | * Optimised query for getting all product IDs. |
| @@ -108,23 +109,8 @@ | ||
| 108 | 109 | return $dispatch_result; |
| 109 | 110 | } |
| 110 | 111 | |
| 111 | 112 | /** |
| 112 | - * Apply the collection's declared rules for the duration of a direct read. | |
| 113 | - * | |
| 114 | - * @param WP_REST_Request $request Full details about the request. | |
| 115 | - * @return \WP_Error|\WP_REST_Response | |
| 116 | - */ | |
| 117 | - public function get_items( $request ) { | |
| 118 | - $plan = Collection_Rules::for_request( 'products', $request, self::WCPOS_SORT_PARAM_MAP ); | |
| 119 | - return $plan->around( | |
| 120 | - function () use ( $request ) { | |
| 121 | - return parent::get_items( $request ); | |
| 122 | - } | |
| 123 | - ); | |
| 124 | - } | |
| 125 | - | |
| 126 | - /** | |
| 127 | 113 | * Create a single product. |
| 128 | 114 | * |
| 129 | 115 | * @param WP_REST_Request $request Full details about the request. |
| 130 | 116 | * |
| @@ -205,14 +191,8 @@ | ||
| 205 | 191 | if ( isset( $params['per_page'] ) && \is_array( $params['per_page'] ) ) { |
| 206 | 192 | $params['per_page']['minimum'] = -1; |
| 207 | 193 | } |
| 208 | 194 | |
| 209 | - // Search text is literal on every lane: `sanitize_text_field` would strip `%30` | |
| 210 | - // and blank malformed UTF-8 before the declared search rule ever saw them. | |
| 211 | - if ( isset( $params['search'] ) && \is_array( $params['search'] ) ) { | |
| 212 | - $params['search']['sanitize_callback'] = 'rest_sanitize_request_arg'; | |
| 213 | - } | |
| 214 | - | |
| 215 | 195 | if ( ! $this->wcpos_parent_collection_supports_param( 'brand' ) ) { |
| 216 | 196 | $params['brand'] = array( |
| 217 | 197 | 'description' => /* translators: REST API collection parameter description. */ __( 'Limit result set to products assigned to brand IDs or slugs, separated by commas.', 'woocommerce-pos' ), |
| 218 | 198 | 'type' => 'string', |
| @@ -387,10 +367,8 @@ | ||
| 387 | 367 | * @param string $search The search SQL query. |
| 388 | 368 | * @param WP_Query $wp_query The WP_Query instance (passed by reference). |
| 389 | 369 | * |
| 390 | 370 | * @return string |
| 391 | - * | |
| 392 | - * @deprecated Collection Rules now installs this behavior. | |
| 393 | 371 | */ |
| 394 | 372 | public function wcpos_posts_search( string $search, WP_Query $wp_query ) { |
| 395 | 373 | return Product_Search::posts_search( $search, $wp_query ); |
| 396 | 374 | } |
| @@ -427,8 +405,19 @@ | ||
| 427 | 405 | * |
| 428 | 406 | * @return array $args Key value array of query var to query value. |
| 429 | 407 | */ |
| 430 | 408 | public function wcpos_product_query( array $args, WP_REST_Request $request ) { |
| 409 | + if ( ! empty( $request['search'] ) ) { | |
| 410 | + // We need to set the query up for a postmeta join. | |
| 411 | + add_filter( 'posts_join', array( $this, 'wcpos_posts_join_to_products_search' ), 10, 2 ); | |
| 412 | + add_filter( 'posts_groupby', array( $this, 'wcpos_posts_groupby_product_search' ), 10, 2 ); | |
| 413 | + } | |
| 414 | + | |
| 415 | + // if POS only products are enabled, exclude online-only products. | |
| 416 | + if ( $this->wcpos_pos_only_products_enabled() ) { | |
| 417 | + add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_exclude_online_only' ), 10, 2 ); | |
| 418 | + } | |
| 419 | + | |
| 431 | 420 | // Check for wcpos_include/wcpos_exclude parameter. |
| 432 | 421 | if ( isset( $request['wcpos_include'] ) || isset( $request['wcpos_exclude'] ) ) { |
| 433 | 422 | add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_include_exclude' ), 20, 2 ); |
| 434 | 423 | } |
| @@ -593,10 +582,8 @@ | ||
| 593 | 582 | * @param string $join The JOIN clause of the query. |
| 594 | 583 | * @param WP_Query $query The WP_Query instance (passed by reference). |
| 595 | 584 | * |
| 596 | 585 | * @return string |
| 597 | - * | |
| 598 | - * @deprecated Collection Rules now installs this behavior. | |
| 599 | 586 | */ |
| 600 | 587 | public function wcpos_posts_join_to_products_search( string $join, WP_Query $query ) { |
| 601 | 588 | return Product_Search::posts_join( $join, $query ); |
| 602 | 589 | } |
| @@ -607,10 +594,8 @@ | ||
| 607 | 594 | * @param string $groupby The GROUP BY clause of the query. |
| 608 | 595 | * @param WP_Query $query The WP_Query instance (passed by reference). |
| 609 | 596 | * |
| 610 | 597 | * @return string |
| 611 | - * | |
| 612 | - * @deprecated Collection Rules now installs this behavior. | |
| 613 | 598 | */ |
| 614 | 599 | public function wcpos_posts_groupby_product_search( string $groupby, WP_Query $query ) { |
| 615 | 600 | return Product_Search::posts_groupby( $groupby, $query ); |
| 616 | 601 | } |
| @@ -624,10 +609,8 @@ | ||
| 624 | 609 | * @param string $where The WHERE clause of the query. |
| 625 | 610 | * @param WP_Query $query The WP_Query instance (passed by reference). |
| 626 | 611 | * |
| 627 | 612 | * @return string |
| 628 | - * | |
| 629 | - * @deprecated Collection Rules now installs this behavior. | |
| 630 | 613 | */ |
| 631 | 614 | public function wcpos_posts_where_product_exclude_online_only( string $where, WP_Query $query ) { |
| 632 | 615 | global $wpdb; |
| 633 | 616 | |