AdvancedCustomFields.php
7.59 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* SearchWP AdvancedCustomFields.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP\Integrations;
use SearchWP\Option;
use SearchWP\Source;
/**
* Class AdvancedCustomFields is responsible for integrating ACF with SearchWP where applicable.
*
* @since 4.0
*/
class AdvancedCustomFields {
/**
* All registered ACF fields.
*
* @since 4.0
* @var array
*/
public $fields = [];
/**
* Stores field name for 'repeatables' e.g. Field Group, Repeater, Flexible.
*
* @since 4.0
* @var array
*/
public $repeatables = [];
/**
* The Source for this integration.
*
* @since 4.0
* @var string
*/
private $source;
/**
* The post type for this integration.
*
* @since 4.0
* @var string
*/
private $post_type;
/**
* Constructor.
*
* @return void
*/
public function __construct( Source $source ) {
if (
! method_exists( $source, 'get_post_type' ) // Only applies to WP_Post sources.
|| ! function_exists( 'acf' )
|| ! function_exists( 'acf_get_field_groups' )
|| ! function_exists( 'acf_get_fields' )
|| ! version_compare( acf()->settings['version'], '5.0', '>=' )
) {
return;
}
$this->post_type = $source->get_post_type();
$this->source = $source->get_name();
add_action( 'searchwp\settings\page', [ $this, 'init' ] );
}
/**
* Initializer.
*
* @since 4.0
* @return void
*/
public function init() {
$this->get_fields();
// Repeatables
if ( ! has_filter( 'searchwp\source\attribute\options', [ $this, 'repeatable_meta_keys' ] ) ) {
add_filter( 'searchwp\source\attribute\options', [ $this, 'repeatable_meta_keys' ], 5, 2 );
}
if ( ! has_filter( 'searchwp\source\attribute\options\special', [ $this, 'repeatable_meta_keys' ] ) ) {
add_filter( 'searchwp\source\attribute\options\special', [ $this, 'repeatable_meta_keys' ], 5, 2 );
}
// Non-Repeatables
if ( ! has_filter( 'searchwp\source\attribute\options', [ $this, 'individual_meta_keys' ] ) ) {
add_filter( 'searchwp\source\attribute\options', [ $this, 'individual_meta_keys' ], 5, 2 );
}
if ( ! has_filter( 'searchwp\source\attribute\options\special', [ $this, 'individual_meta_keys' ] ) ) {
add_filter( 'searchwp\source\attribute\options\special', [ $this, 'individual_meta_keys' ], 5, 2 );
}
// Prevent SearchWP from interfering with ACF's front end searching of its own fields.
add_filter( 'searchwp\native\short_circuit', function( $short_circuit ) {
$acf_actions = apply_filters( 'searchwp\acf\short_circuit_actions', array(
'acf/fields/oembed/search',
'acf/fields/post_object/query',
'acf/fields/relationship/query',
) );
return $short_circuit
? $short_circuit
: isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], $acf_actions );
}, 5 );
}
/**
* Retrieve all registered ACF fields.
*
* @since 4.0
*/
public function get_fields() {
add_action( 'pre_get_posts', [ $this, 'suppress_filters' ] );
$field_groups = acf_get_field_groups();
remove_action( 'pre_get_posts', [ $this, 'suppress_filters' ] );
$fields = [];
foreach ( $field_groups as $field_group ) {
$fields = acf_get_fields( $field_group );
if ( ! empty( $fields ) ) {
$this->categorize_fields( $fields, $field_group );
}
}
return $this->repeatables;
}
/**
* Recursive function to find all repeatable ACF fields keys.
*
* @since 4.0
* @param array $fields The fields for this field group.
* @param array $field_group The field group itself.
*/
public function categorize_fields( $fields, $field_group, $prefix = '' ) {
foreach ( $fields as $field ) {
if ( 'repeater' == $field['type'] || 'group' == $field['type'] ) {
if ( empty( $field['sub_fields'] ) ) {
continue;
}
$this->repeatables[] = [
'field_group' => $field_group,
'field' => $this->reduce_repeatable( $field, $prefix ),
];
$this_prefix = ! empty( $prefix ) ? $prefix . '*' . $field['name'] . '*' : $field['name'] . '*';
$this->categorize_fields( $field['sub_fields'], $field_group, $this_prefix );
} elseif ( 'flexible_content' == $field['type'] ) {
$this->repeatables[] = [
'field_group' => $field_group,
'field' => $this->reduce_repeatable( $field, $prefix ),
];
foreach ( (array) $field['layouts'] as $layout ) {
if ( empty( $field['sub_fields'] ) ) {
continue;
}
$this_prefix = ! empty( $prefix ) ? $prefix . '*' . $field['name'] . '*' : $field['name'] . '*';
$this->categorize_fields( $layout['sub_fields'], $field_group, $this_prefix );
}
} else {
// This is an individual field.
$this->fields[] = [
'field_group' => $field_group,
'field' => $field,
];
}
}
}
/**
* Reduce an ACF Field to only what we need.
*
* @since 4.0
* @param array $field
* @return array
*/
private function reduce_repeatable( array $field, string $prefix ) {
return [
'id' => isset( $field['ID'] ) ? $field['ID'] : null,
'key' => isset( $field['key'] ) ? $field['key'] : null,
'label' => isset( $field['label'] ) ? $field['label'] : null,
'name' => isset( $field['name'] ) ? $prefix . $field['name'] . '*' : null,
];
}
/**
* Callback to suppress filters when retrieving ACF Field Groups.
*
* @since 4.0
*/
public function suppress_filters( $query ) {
$query->set( 'suppress_filters', true );
}
/**
* Callback to return 'repeatable' ACF Fields e.g. Repeater, Flexible Content
*
* @since 4.0
* @param mixed $keys
* @param mixed $args
* @return mixed|array
*/
public function repeatable_meta_keys( $keys, $args ) {
if (
! apply_filters( 'searchwp\acf\show_repeatable_fields', true, $args )
|| $args['source'] !== $this->source
|| $args['attribute'] !== 'meta'
) {
return $keys;
}
// Add Repeatables.
foreach ( $this->repeatables as $pair ) {
$field = $pair['field'];
$group = $pair['field_group'];
$key = $field['name'];
$icon = 'dashicons dashicons-welcome-widgets-menus'; // This is what ACF uses.
$label = 'ACF: ' . $field['label'];
if ( apply_filters( 'searchwp\acf\include_field_group_in_label', false, $pair ) ) {
$label .= ' (' . $group['title'] . ')';
}
$option = new Option( $key, $label, $icon );
// If there's already a match, remove it because we want ours there.
$keys = array_filter( $keys, function( $option ) use ( $key ) {
return $key !== $option->get_value();
});
// Add custom Option for Repeatable.
$keys[] = $option;
}
return $keys;
}
/**
* Callback to include individual ACF fields as Custom Field optoins.
*
* @since 4.1
* @param mixed $keys
* @param mixed $args
* @return mixed|array
*/
public function individual_meta_keys( $keys, $args ) {
if (
! apply_filters( 'searchwp\acf\show_individual_fields', true, $args )
|| $args['source'] !== $this->source
|| $args['attribute'] !== 'meta'
) {
return $keys;
}
// Add individual fields.
foreach ( $this->fields as $pair ) {
$field = $pair['field'];
$group = $pair['field_group'];
$key = $field['name'];
$icon = 'dashicons dashicons-welcome-widgets-menus'; // This is what ACF uses.
$label = 'ACF: ' . $field['label'];
if ( apply_filters( 'searchwp\acf\include_field_group_in_label', false, $pair ) ) {
$label .= ' (' . $group['title'] . ')';
}
$option = new Option( $key, $label, $icon );
// If there's already a match, remove it because we want ours there.
$keys = array_filter( $keys, function( $option ) use ( $key ) {
return $key !== $option->get_value();
});
// Add custom Option for ACF field.
$keys[] = $option;
}
return $keys;
}
}