PageBuilder.php
3.95 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* SearchWP PageBuilder.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP\Integrations;
/**
* Class PageBuilder is responsible for customizing SearchWP's Native
* implementation to work with page builder queries by allowing Native
* (and forcing Native) to run outside the main query.
*
* @since 4.1.5
*/
abstract class PageBuilder {
/**
* Name used for canonical reference to Integration.
*
* @since 4.1.5
* @var string
*/
protected $name = 'pagebuilder';
/**
* Prevent unwanted overruns.
*
* @since 4.1.8
* @var false
*/
public $run_once = true;
/**
* The Engine to use.
*
* @since 4.1.8
* @var string
*/
public $engine = 'default';
/**
* Modify Native behavior by forcing execution outside the main query.
*
* @since 4.1.5
* @return void
*/
public function modify_native_behavior() {
$applicable = ! ( is_admin() || wp_doing_ajax() );
if ( ! apply_filters( 'searchwp\integration\pagebuilder\enabled', $applicable, [
'context' => $this->name
] ) ) {
return;
}
add_filter( 'searchwp\native\short_circuit', [ $this, 'short_circuit'], 5, 2 );
add_filter( 'searchwp\native\force', [ $this, 'force' ], 990, 2 );
add_filter( 'searchwp\native\strict', [ $this, 'strict' ], 990, 2 );
add_action( 'pre_get_posts', [ $this, 'pre_get_posts' ], -1 );
}
/**
* Hook in before \SearchWP\Native so as to trigger it for the page builder module query.
*
* @since 4.1.8.
* @param mixed $query
* @return void
*/
public function pre_get_posts( $query ) {
if ( ! $query->is_main_query() || ! isset( $_GET['s'] ) ) {
return;
}
$engine = apply_filters( 'searchwp\integration\pagebuilder\engine', 'default', [
'context' => $this->name,
'query' => $query,
] );
// Allow short circuit by emptying engine.
if ( empty( $engine ) ) {
return;
}
$this->engine = (string) $engine;
$query->set( 'searchwp', $this->engine );
add_filter( 'searchwp\native\args', [ $this, 'native_args' ], 990, 2 );
// Prevent this from happening again.
if ( $this->run_once ) {
remove_action( 'pre_get_posts', [ $this, 'pre_get_posts' ], -1 );
}
}
/**
* Updates the \SearchWP\Native arguments.
*
* @since 4.1.8
* @param array $args Incoming arguments.
* @param \WP_Query $query The \WP_Query.
* @return array
*/
public function native_args( $args, $query ) {
$args['s'] = stripslashes( $_GET['s'] );
$args['engine'] = $this->engine;
// Prevent this from happening again.
if ( $this->run_once ) {
remove_filter( 'searchwp\native\args', [ $this, 'native_args' ], 990, 2 );
}
return $args;
}
/**
* Prevent \SearchWP\Native from short circuiting.
*
* @since 4.1.8
* @param boolean $short_circuit
* @param \WP_Query $query
* @return boolean
*/
public function short_circuit( $short_circuit, $query ) {
if ( $query->is_main_query() && isset( $_GET['s'] ) ) {
$short_circuit = false;
}
// Prevent this from happening again.
if ( $this->run_once ) {
remove_filter( 'searchwp\native\short_circuit', [ $this, 'short_circuit '], 5 );
}
return $short_circuit;
}
/**
* Force \SearchWP\Native to run.
*
* @since 4.1.8
* @param boolean $force
* @param array $args
* @return boolean
*/
public function force( $force, $args ) {
if ( $args['query']->is_search() ) {
$force = true;
}
// Prevent this from happening again.
if ( $this->run_once ) {
remove_filter( 'searchwp\native\force', [ $this, 'force' ], 990 );
}
return $force;
}
/**
* Prevent \SearchWP\Native from being strict to is_main_query()
*
* @since 4.1.8
* @param boolean $strict
* @param \WP_Query $query
* @return boolean
*/
public function strict( $strict, $query ) {
if ( ! $query->is_search() ) {
$strict = true;
}
// Prevent this from happening again.
if ( $this->run_once ) {
remove_filter( 'searchwp\native\strict', [ $this, 'strict' ], 990 );
}
return $strict;
}
}