| @@ -69,8 +69,9 @@ | ||
| 69 | 69 | */ |
| 70 | 70 | private const WCPOS_SORT_PARAM_MAP = array( |
| 71 | 71 | 'orderby' => 'orderby', |
| 72 | 72 | 'order' => 'order', |
| 73 | + 'search' => 'search', | |
| 73 | 74 | ); |
| 74 | 75 | |
| 75 | 76 | /** |
| 76 | 77 | * Memoized parent collection params. |
| @@ -94,10 +95,8 @@ | ||
| 94 | 95 | |
| 95 | 96 | add_filter( 'woocommerce_rest_prepare_product_object', array( $this, 'wcpos_product_response' ), 10, 3 ); |
| 96 | 97 | add_action( 'woocommerce_rest_insert_product_object', array( $this, 'wcpos_insert_product_object' ), 10, 3 ); |
| 97 | 98 | 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 ); | |
| 100 | 99 | |
| 101 | 100 | /* |
| 102 | 101 | * Check if the request is for all products and if the 'posts_per_page' is set to -1. |
| 103 | 102 | * Optimised query for getting all product IDs. |
| @@ -109,8 +108,23 @@ | ||
| 109 | 108 | return $dispatch_result; |
| 110 | 109 | } |
| 111 | 110 | |
| 112 | 111 | /** |
| 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 | + /** | |
| 113 | 127 | * Create a single product. |
| 114 | 128 | * |
| 115 | 129 | * @param WP_REST_Request $request Full details about the request. |
| 116 | 130 | * |
| @@ -191,8 +205,14 @@ | ||
| 191 | 205 | if ( isset( $params['per_page'] ) && \is_array( $params['per_page'] ) ) { |
| 192 | 206 | $params['per_page']['minimum'] = -1; |
| 193 | 207 | } |
| 194 | 208 | |
| 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 | + | |
| 195 | 215 | if ( ! $this->wcpos_parent_collection_supports_param( 'brand' ) ) { |
| 196 | 216 | $params['brand'] = array( |
| 197 | 217 | 'description' => /* translators: REST API collection parameter description. */ __( 'Limit result set to products assigned to brand IDs or slugs, separated by commas.', 'woocommerce-pos' ), |
| 198 | 218 | 'type' => 'string', |
| @@ -367,8 +387,10 @@ | ||
| 367 | 387 | * @param string $search The search SQL query. |
| 368 | 388 | * @param WP_Query $wp_query The WP_Query instance (passed by reference). |
| 369 | 389 | * |
| 370 | 390 | * @return string |
| 391 | + * | |
| 392 | + * @deprecated Collection Rules now installs this behavior. | |
| 371 | 393 | */ |
| 372 | 394 | public function wcpos_posts_search( string $search, WP_Query $wp_query ) { |
| 373 | 395 | return Product_Search::posts_search( $search, $wp_query ); |
| 374 | 396 | } |
| @@ -405,19 +427,8 @@ | ||
| 405 | 427 | * |
| 406 | 428 | * @return array $args Key value array of query var to query value. |
| 407 | 429 | */ |
| 408 | 430 | 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 | - | |
| 420 | 431 | // Check for wcpos_include/wcpos_exclude parameter. |
| 421 | 432 | if ( isset( $request['wcpos_include'] ) || isset( $request['wcpos_exclude'] ) ) { |
| 422 | 433 | add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_include_exclude' ), 20, 2 ); |
| 423 | 434 | } |
| @@ -582,8 +593,10 @@ | ||
| 582 | 593 | * @param string $join The JOIN clause of the query. |
| 583 | 594 | * @param WP_Query $query The WP_Query instance (passed by reference). |
| 584 | 595 | * |
| 585 | 596 | * @return string |
| 597 | + * | |
| 598 | + * @deprecated Collection Rules now installs this behavior. | |
| 586 | 599 | */ |
| 587 | 600 | public function wcpos_posts_join_to_products_search( string $join, WP_Query $query ) { |
| 588 | 601 | return Product_Search::posts_join( $join, $query ); |
| 589 | 602 | } |
| @@ -594,8 +607,10 @@ | ||
| 594 | 607 | * @param string $groupby The GROUP BY clause of the query. |
| 595 | 608 | * @param WP_Query $query The WP_Query instance (passed by reference). |
| 596 | 609 | * |
| 597 | 610 | * @return string |
| 611 | + * | |
| 612 | + * @deprecated Collection Rules now installs this behavior. | |
| 598 | 613 | */ |
| 599 | 614 | public function wcpos_posts_groupby_product_search( string $groupby, WP_Query $query ) { |
| 600 | 615 | return Product_Search::posts_groupby( $groupby, $query ); |
| 601 | 616 | } |
| @@ -609,8 +624,10 @@ | ||
| 609 | 624 | * @param string $where The WHERE clause of the query. |
| 610 | 625 | * @param WP_Query $query The WP_Query instance (passed by reference). |
| 611 | 626 | * |
| 612 | 627 | * @return string |
| 628 | + * | |
| 629 | + * @deprecated Collection Rules now installs this behavior. | |
| 613 | 630 | */ |
| 614 | 631 | public function wcpos_posts_where_product_exclude_online_only( string $where, WP_Query $query ) { |
| 615 | 632 | global $wpdb; |
| 616 | 633 | |