WooCommerceAdminSearch.php
2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* SearchWP WooCommerceAdminSearch.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP\Integrations;
/**
* Class WooCommerceAdminSearch is responsible for supporting Admin WooCommerce searches.
*
* @since 4.1.4
*/
class WooCommerceAdminSearch {
/**
* Constructor.
*
* @return void
*/
function __construct() {
add_filter( 'searchwp\native\force', [ $this, 'maybe_force_admin_search' ] );
add_filter( 'searchwp\native\short_circuit', function( $short_circuit, $query ) {
if ( ! self::environment_pre_check() ) {
return $short_circuit;
}
return ! self::is_woocommerce_admin_search( $short_circuit, $query );
}, 8, 2 );
}
/**
* Return whether we are in the admin and WooCommerce is active.
*
* @since 4.1.16
* @return bool
*/
public static function environment_pre_check() {
return is_admin()
&& (
function_exists( 'is_plugin_active' )
&& is_plugin_active( 'woocommerce/woocommerce.php' )
);
}
/**
* Whether the current request is a WooCommerce Admin search.
*
* @since 4.1.4
* @return boolean
*/
public static function is_woocommerce_admin_search( $short_circuit, $query ) {
if ( ! self::environment_pre_check() ) {
return $short_circuit;
}
if ( ! is_archive() || ! isset( $_GET['s'] ) || empty( stripslashes( $_GET['s'] ) ) ) {
return $short_circuit;
}
if ( ! isset( $_GET['post_type'] ) || 'product' !== $_GET['post_type'] ) {
return $short_circuit;
}
return apply_filters( 'searchwp\integration\woocommerce_admin_search\force', true );
}
/**
* Force an admin search when applicable.
*
* @since 4.1.4
* @param mixed $args
* @return bool
*/
public function maybe_force_admin_search( $args ) {
if ( ! self::is_woocommerce_admin_search( false, null ) ) {
return false;
}
// If this is an admin search and there is an admin engine with Products, force it to happen.
// We have to do this because $query->is_search() is false at runtime.
$admin_engine = \SearchWP\Settings::get_admin_engine();
if ( empty( $admin_engine ) ) {
return $args;
}
$engine_model = new \SearchWP\Engine( $admin_engine );
if ( ! array_key_exists( 'post' . SEARCHWP_SEPARATOR . 'product', $engine_model->get_sources() ) ) {
return $args;
}
add_filter( 'searchwp\native\args', array( $this, 'set_admin_search_args' ) );
return true;
}
/**
* Sets the arguments for the search.
*
* @since 4.1.4
* @param array $args The query arguments.
* @return array The query arguments.
*/
public function set_admin_search_args( $args ) {
remove_filter( 'searchwp\native\args', array( $this, 'set_admin_search_args' ) );
if ( array_key_exists( 'product_search', $args ) && $args['product_search'] ) {
$args['post__in'] = [];
}
return $args;
}
}