PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.7
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.7
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / includes / API / Product_Search.php

Product_Search.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.7, at includes/API/Product_Search.php

90 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shared product search SQL filters.
4 *
5 * @package WCPOS\WooCommercePOS\API
6 */
7
8 namespace WCPOS\WooCommercePOS\API;
9
10 use WCPOS\WooCommercePOS\Services\Barcode_Field;
11 use WP_Query;
12
13 /**
14 * Keeps v1 and v2 product search fields identical.
15 */
16 final class Product_Search {
17 /**
18 * Search product titles, SKUs, and the configured barcode field.
19 *
20 * @param string $search Search SQL.
21 * @param WP_Query $wp_query Query instance.
22 * @return string
23 */
24 public static function posts_search( string $search, WP_Query $wp_query ): string {
25 global $wpdb;
26 if ( empty( $search ) ) {
27 return $search;
28 }
29 $q = $wp_query->query_vars;
30 $n = ! empty( $q['exact'] ) ? '' : '%';
31 $meta_fields = Barcode_Field::search_keys();
32 $search_conditions = array();
33 foreach ( (array) $q['search_terms'] as $term ) {
34 $term = $n . $wpdb->esc_like( $term ) . $n;
35 $search_conditions[] = $wpdb->prepare( "({$wpdb->posts}.post_title LIKE %s)", $term ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is safe.
36 foreach ( $meta_fields as $field ) {
37 $search_conditions[] = $wpdb->prepare( '(pm1.meta_value LIKE %s AND pm1.meta_key = %s)', $term, $field );
38 }
39 }
40
41 if ( ! empty( $search_conditions ) ) {
42 $search = ' AND (' . implode( ' OR ', $search_conditions ) . ') ';
43 if ( ! is_user_logged_in() ) {
44 $search .= " AND ($wpdb->posts.post_password = '') ";
45 }
46 }
47 return $search;
48 }
49
50 /**
51 * Join product meta while searching.
52 *
53 * @param string $join JOIN SQL.
54 * @param WP_Query $query Query instance.
55 * @return string
56 */
57 public static function posts_join( string $join, WP_Query $query ): string {
58 global $wpdb;
59 if ( self::is_searching( $query ) && false === strpos( $join, 'pm1' ) ) {
60 $join .= " LEFT JOIN {$wpdb->postmeta} pm1 ON {$wpdb->posts}.ID = pm1.post_id ";
61 }
62 return $join;
63 }
64
65 /**
66 * Group product search results after joining meta.
67 *
68 * @param string $groupby GROUP BY SQL.
69 * @param WP_Query $query Query instance.
70 * @return string
71 */
72 public static function posts_groupby( string $groupby, WP_Query $query ): string {
73 global $wpdb;
74 if ( self::is_searching( $query ) ) {
75 $groupby = "{$wpdb->posts}.ID";
76 }
77 return $groupby;
78 }
79
80 /**
81 * Whether the query carries a search term. Not empty(): the literal term "0" is a search.
82 *
83 * @param WP_Query $query Query instance.
84 * @return bool
85 */
86 private static function is_searching( WP_Query $query ): bool {
87 return isset( $query->query_vars['s'] ) && '' !== (string) $query->query_vars['s'];
88 }
89 }
90