-
Notifications
You must be signed in to change notification settings - Fork 4
/
class-sippis-acf-field-network-post-select.php
executable file
·441 lines (360 loc) · 13 KB
/
class-sippis-acf-field-network-post-select.php
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class sippis_acf_field_network_post_select extends acf_field {
function __construct( $settings ) {
$this->settings = $settings;
$this->name = 'network_post_select';
$this->label = __('Network posts select', 'sippis-acf-field-network-post-select');
$this->category = 'relational';
$this->defaults = [
'post_type' => [],
'taxonomy' => [],
'allow_null' => false,
'multiple' => false,
'ui' => true,
];
add_action( 'wp_ajax_acf/fields/network_post_select/query', [ $this, 'ajax_query' ] );
add_action( 'wp_ajax_nopriv_acf/fields/network_post_select/query', [ $this, 'ajax_query' ] );
parent::__construct();
} // end __construct
function ajax_query() {
if ( ! acf_verify_ajax() ) {
die();
}
$response = $this->get_ajax_query( $_POST );
acf_send_ajax_results( $response );
} // end ajax_query
/**
* Get posts to return for AJAX query.
*
* @param array $options options from AJAX
* @return array array of posts
*/
function get_ajax_query( $options = [] ) {
$results = [];
$args = [];
$s = false;
$is_search = false;
// default options
$options = acf_parse_args( $options, [
'post_id' => false,
's' => '',
'field_key' => '',
'paged' => true,
] );
// var_dump( $options );
// load field
$field = acf_get_field( $options['field_key'] );
if ( ! $field ) {
return false;
}
// allowed sites
$get_sites_args = [];
if ( ! empty( $field['site_id'] ) ) {
$get_sites_args['site__in'] = acf_get_array( $field['site_id'] );
}
// filters for get_sites query
$get_sites_args = apply_filters( 'acf/fields/network_post_select/get_sites', $get_sites_args, $field, $options['post_id'] );
$get_sites_args = apply_filters( 'acf/fields/network_post_select/get_sites/name=' . $field['name'], $get_sites_args, $field, $options['post_id'] );
$get_sites_args = apply_filters( 'acf/fields/network_post_select/get_sites/key=' . $field['key'], $get_sites_args, $field, $options['post_id'] );
// get sites in network
$sites = get_sites( $get_sites_args );
// store current site id
$current_site_id = get_current_blog_id();
// is the query paged
$args['posts_per_page'] = 20;
$args['paged'] = $options['paged'];
// is the query search
if ( $options['s'] !== '' ) {
$s = wp_unslash( strval( $options['s'] ) ); // strip slashes (search may be integer)
$args['s'] = $s;
$is_search = true;
}
// loop sites
foreach ( $sites as $site ) {
switch_to_blog( $site->blog_id );
// post types for query
if ( ! empty( $field['post_type'] ) ) {
$args['post_type'] = acf_get_array( $field['post_type'] );
} else {
$args['post_type'] = acf_get_post_types();
}
// taxonomies for query
if ( ! empty( $field['taxonomy'] ) ) {
$terms = acf_decode_taxonomy_terms( $field['taxonomy'] );
$args['tax_query'] = [];
foreach( $terms as $k => $v ) {
$args['tax_query'][] = [
'taxonomy' => $k,
'field' => 'slug',
'terms' => $v,
];
}
}
// filters
$args = apply_filters( 'acf/fields/network_post_select/query', $args, $field, $options['post_id'] );
$args = apply_filters( 'acf/fields/network_post_select/query/name=' . $field['name'], $args, $field, $options['post_id'] );
$args = apply_filters( 'acf/fields/network_post_select/query/key=' . $field['key'], $args, $field, $options['post_id'] );
if ( ! array_key_exists( 's', $args ) ) {
$is_search = false;
}
// get posts grouped by post type
$groups = acf_get_grouped_posts( $args );
// loop post groups
foreach ( array_keys( $groups ) as $group_title ) {
$posts = acf_extract_var( $groups, $group_title );
$data = [
'text' => get_bloginfo( 'name' ) . ' - ' . strtolower( $group_title ),
'children' => []
];
// convert post objects to post titles
foreach ( array_keys( $posts ) as $post_id ) {
$posts[ $post_id ] = $this->get_post_title( $posts[ $post_id ], $field, $options['post_id'], $is_search );
}
// order posts by search
if ( $is_search && empty( $args['orderby'] ) ) {
$posts = acf_order_by_search( $posts, $args['s'] );
}
// append to $data
foreach ( array_keys( $posts ) as $post_id ) {
$data['children'][] = $this->get_post_result( $post_id, $posts[ $post_id ], $site->blog_id );
}
$results[] = $data;
}
}
// switch back to current site
switch_to_blog( $current_site_id );
// optgroup or single
$post_type = acf_get_array( $args['post_type'] );
if ( count( $post_type ) === 1 ) {
if ( isset( $results[0] ) ) {
$results = $results[0]['children'];
}
}
$response = [
'results' => $results,
'limit' => $args['posts_per_page']
];
return $response;
} // end get_ajax_query
/**
* Shape single post to AJAX query result suitable form.
*
* @param integer $id post ID
* @param string $text text of option
* @param integer $site_id network site ID
* @return array
*/
function get_post_result( $id, $text, $site_id ) {
$result = [
'id' => $site_id . '|' . $id,
'text' => $text
];
$search = '| ' . __( 'Parent', 'acf' ) . ':';
$pos = strpos( $text, $search );
if ( $pos !== false ) {
$result['description'] = substr( $text, $pos+2 );
$result['text'] = substr( $text, 0, $pos );
}
return $result;
} // end get_post_result
/**
* Get title for the post.
*
* @param object $post post object
* @param array $field field options
* @param integer $post_id post ID
* @param integer $is_search if request comes from search
* @return string
*/
function get_post_title( $post, $field, $post_id = 0, $is_search = 0 ) {
$current_site_id = get_current_blog_id();
// switch to correct site for getting the title from right post
switch_to_blog( $post->blog_id );
// get post_id
if ( ! $post_id ) {
$post_id = acf_get_form_data( 'post_id' );
}
$title = acf_get_post_title( $post, $is_search );
// add site name to title
$site = get_blog_details( $post->blog_id );
$title = $title . ' (' . $site->blogname . ')';
// filters
$title = apply_filters('acf/fields/network_post_select/result', $title, $post, $field, $post_id);
$title = apply_filters('acf/fields/network_post_select/result/name=' . $field['_name'], $title, $post, $field, $post_id);
$title = apply_filters('acf/fields/network_post_select/result/key=' . $field['key'], $title, $post, $field, $post_id);
// switch back to current site
switch_to_blog( $current_site_id );
return $title;
} // end get_post_title
/**
* Change the format in which field value is saved on databse.
*
* @param mixed $value value which will be saved
* @param integer $post_id post ID of which the value will be saved
* @param array $field field options
* @return array
*/
function update_value( $value, $post_id, $field ) {
if ( empty( $value ) ) {
return $value;
}
// store site_id and post_id separately
$exploded = explode( '|', $value );
$value = [
'site_id' => acf_get_numeric( $exploded[0] ),
'post_id' => acf_get_numeric( $exploded[1] ),
];
return $value;
} // end update_value
/*
* get_posts
*
* This function will return an array of posts for a given field value
*
* @type function
* @date 13/06/2014
* @since 5.0.0
*
* @param $value (array)
* @return $value
*/
/**
* Get array of posts with given ID's.
*
* @param mixed $value post ID's
* @param array $field field options
* @return array
*/
function get_posts( $value, $field ) {
$posts = [];
// allowed sites
$get_sites_args = [];
if ( ! empty( $field['site_id'] ) ) {
$get_sites_args['site__in'] = acf_get_array( $field['site_id'] );
}
// filters for get_sites query
$get_sites_args = apply_filters( 'acf/fields/network_post_select/get_sites', $get_sites_args, $field, $options['post_id'] );
$get_sites_args = apply_filters( 'acf/fields/network_post_select/get_sites/name=' . $field['name'], $get_sites_args, $field, $options['post_id'] );
$get_sites_args = apply_filters( 'acf/fields/network_post_select/get_sites/key=' . $field['key'], $get_sites_args, $field, $options['post_id'] );
// get sites in network
$sites = get_sites( $get_sites_args );
// store current site id
$current_site_id = get_current_blog_id();
$args = [
'post__in' => $value,
'post_type' => $field['post_type'],
];
if ( empty( $value ) ) {
$args['posts_per_page'] = $this->query_defaults['posts_per_page'];
}
// loop sites
foreach ( $sites as $site ) {
switch_to_blog( $site->blog_id );
// get posts
$new_posts = acf_get_posts( $args );
foreach ( $new_posts as $key => $new_post ) {
$new_posts[ $key ]->blog_id = $site->blog_id;
}
$posts = array_merge( $posts, $new_posts );
}
// switch back to current site
switch_to_blog( $current_site_id );
return $posts;
} // end get_posts
/**
* Render the field in admin.
*
* @param array $field field options
*/
function render_field( $field ) {
$field['type'] = 'select';
// $field['multiple'] = false;
$field['ui'] = true;
$field['ajax'] = true;
$field['choices'] = [];
// try to get posts based on field value
$posts = $this->get_posts( $field['value'], $field );
if ( $posts ) {
foreach ( array_keys( $posts ) as $i ) {
$post = acf_extract_var( $posts, $i );
// add posts found to choices available without select2
$field['choices'][ $post->blog_id . '|' . $post->ID ] = $this->get_post_title( $post, $field );
}
}
// change field value format so it's in same format with AJAX query return
$field['value'] = $field['value']['site_id'] . '|' . $field['value']['post_id'];
acf_render_field( $field );
} // end render_field
function input_admin_enqueue_scripts() {
$url = $this->settings['url'];
$version = $this->settings['version'];
wp_register_script( 'acf-field-network-post-select', "{$url}assets/js/input.js", [ 'acf-input' ], $version );
wp_enqueue_script( 'acf-field-network-post-select' );
} // end input_admin_enqueue_scripts
/**
* Render field settings when adding the field.
*
* @param array $field field options
*/
function render_field_settings( $field ) {
$sites = [];
$sites_raw = get_sites( [
'number' => apply_filters( 'acf/fields/network_post_select/settings/max_sites', 100, $field ),
] );
foreach ( $sites_raw as $site ) {
$site = get_blog_details( $site->blog_id );
$sites[ $site->blog_id ] = $site->blogname;
}
acf_render_field_setting( $field, [
'label' => __( 'Filter by Post Type', 'acf' ),
'instructions' => '',
'type' => 'select',
'name' => 'post_type',
'choices' => acf_get_pretty_post_types(),
'multiple' => true,
'ui' => true,
'allow_null' => true,
'placeholder' => __( 'All post types', 'acf' ),
] );
acf_render_field_setting( $field, [
'label' => __( 'Filter by Taxonomy', 'acf' ),
'instructions' => '',
'type' => 'select',
'name' => 'taxonomy',
'choices' => acf_get_taxonomy_terms(),
'multiple' => true,
'ui' => true,
'allow_null' => true,
'placeholder' => __( 'All taxonomies', 'acf' ),
] );
acf_render_field_setting( $field, [
'label' => __( 'Filter by Site', 'sippis-acf-field-network-post-select' ),
'instructions' => '',
'type' => 'select',
'name' => 'site_id',
'choices' => $sites,
'multiple' => true,
'ui' => true,
'allow_null' => true,
'placeholder' => __( 'All sites', 'sippis-acf-field-network-post-select' ),
] );
acf_render_field_setting( $field, [
'label' => __( 'Allow Null?', 'acf' ),
'instructions' => '',
'name' => 'allow_null',
'type' => 'true_false',
'ui' => true,
] );
acf_render_field_setting( $field, [
'label' => __( 'Select multiple values?', 'acf' ),
'instructions' => '',
'name' => 'multiple',
'type' => 'true_false',
'ui' => true,
] );
} // end render_field_settings
} // end class
// initialize
new sippis_acf_field_network_post_select( $this->settings );