This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
/
class-wp-customize-posts-preview.php
1581 lines (1430 loc) · 52.4 KB
/
class-wp-customize-posts-preview.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Customize Posts Preview Class
*
* Implements post management in the Customizer.
*
* @package WordPress
* @subpackage Customize
*/
/**
* Class WP_Customize_Posts_Preview
*/
final class WP_Customize_Posts_Preview {
/**
* WP_Customize_Posts instance.
*
* @access public
* @var WP_Customize_Posts
*/
public $component;
/**
* Post IDs for all posts that were seen by the_posts filters.
*
* @var int[]
*/
public $queried_post_ids = array();
/**
* Previewed post settings by post ID.
*
* @var WP_Customize_Post_Setting[]
*/
public $previewed_post_settings = array();
/**
* Previewed postmeta settings by post ID and meta key.
*
* @var WP_Customize_Postmeta_Setting[]
*/
public $previewed_postmeta_settings = array();
/**
* List of the orderby keys used in queries in the response.
*
* @var array
*/
public $queried_orderby_keys = array();
/**
* Whether the preview filters have been added.
*
* @see WP_Customize_Posts_Preview::add_preview_filters()
* @var bool
*/
protected $has_preview_filters = false;
/**
* Collection of sanitized post setting values used for syncing post setting changes into nav menu items (particularly original_title).
*
* Mapping of post ID to sanitized post data value.
*
* @var array
*/
protected $sanitized_dirty_post_setting_values = array();
/**
* Initial loader.
*
* @access public
*
* @param WP_Customize_Posts $component Component.
*/
public function __construct( WP_Customize_Posts $component ) {
$this->component = $component;
$priority = 10; // Must be before 11 at which WP_Customize_Nav_Menus::customize_register() runs and the nav_menu_items are constructed.
add_action( 'customize_register', array( $this, 'capture_sanitized_post_setting_values_for_nav_menu_items' ), $priority );
add_filter( 'wp_setup_nav_menu_item', array( $this, 'filter_pristine_early_nav_menu_item' ), 5 );
add_action( 'customize_register', array( $this, 'remove_filter_pristine_early_nav_menu_item' ), 12 );
add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) );
}
/**
* Setup the customizer preview.
*/
public function customize_preview_init() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'parse_query', array( $this, 'ensure_page_for_posts_preview' ), 5 );
add_filter( 'customize_dynamic_partial_args', array( $this, 'filter_customize_dynamic_partial_args' ), 10, 2 );
add_filter( 'customize_dynamic_partial_class', array( $this, 'filter_customize_dynamic_partial_class' ), 10, 3 );
add_filter( 'the_posts', array( $this, 'filter_the_posts_to_tally_previewed_posts' ), 1000 );
add_action( 'the_post', array( $this, 'handle_the_post_to_tally_previewed_post' ) );
add_filter( 'the_posts', array( $this, 'filter_the_posts_to_tally_orderby_keys' ), 10, 2 );
add_action( 'wp_footer', array( $this, 'export_preview_data' ), 10 );
add_filter( 'get_edit_post_link', array( $this, 'filter_get_edit_post_link' ), 10, 2 );
add_filter( 'get_avatar', array( $this, 'filter_get_avatar' ), 10, 6 );
add_filter( 'infinite_scroll_results', array( $this, 'amend_with_queried_post_ids' ) );
add_filter( 'customize_render_partials_response', array( $this, 'amend_with_queried_post_ids' ) );
add_filter( 'customize_render_partials_response', array( $this, 'amend_partials_response_with_rest_resources' ), 10, 3 );
remove_filter( 'get_edit_post_link', '__return_empty_string' ); // See <https://core.trac.wordpress.org/ticket/38648>.
}
/**
* Add preview filters for post and postmeta settings.
*/
public function add_preview_filters() {
if ( $this->has_preview_filters ) {
return false;
}
add_action( 'pre_get_posts', array( $this, 'prepare_query_preview' ) );
add_filter( 'get_meta_sql', array( $this, 'filter_get_meta_sql_to_inject_customized_state' ), 10, 6 );
add_filter( 'posts_request', array( $this, 'filter_posts_request_to_inject_customized_state' ), 10, 2 );
add_filter( 'the_posts', array( $this, 'filter_the_posts_to_preview_settings' ), 1, 2 );
add_filter( 'get_pages', array( $this, 'filter_get_pages_to_preview_settings' ), 1, 2 );
add_action( 'the_post', array( $this, 'preview_setup_postdata' ) );
add_filter( 'the_title', array( $this, 'filter_the_title' ), 1, 2 );
add_filter( 'get_post_metadata', array( $this, 'filter_get_post_meta_to_preview' ), 1000, 4 );
add_filter( 'wp_setup_nav_menu_item', array( $this, 'filter_nav_menu_item_to_set_post_dependent_props' ), 100 );
add_filter( 'comments_open', array( $this, 'filter_preview_comments_open' ), 10, 2 );
add_filter( 'pings_open', array( $this, 'filter_preview_pings_open' ), 10, 2 );
add_filter( 'get_post_status', array( $this, 'filter_get_post_status' ), 10, 2 );
$this->has_preview_filters = true;
return true;
}
/**
* Enqueue scripts for the customizer preview.
*/
public function enqueue_scripts() {
wp_enqueue_script( 'customize-post-field-partial' );
wp_enqueue_script( 'customize-preview-posts' );
}
/**
* Ensure the page_for_posts can be previewed as the page for posts.
*
* Prevents the page for posts from being previewed as a standard page.
*
* @param WP_Query $query Query.
*/
public function ensure_page_for_posts_preview( WP_Query $query ) {
if ( ! empty( $query->query_vars['page_id'] ) && 'page' === get_option( 'show_on_front' ) && intval( $query->query_vars['page_id'] ) === intval( get_option( 'page_for_posts' ) ) ) {
$query->is_preview = false;
unset( $query->query_vars['preview'] );
}
}
/**
* Override calls to setup_postdata with the previewed post_data. In most
* cases, the get_posts filter above should already set this up as expected
* but if a post os fetched via get_post() or by some other means, then
* this will ensure that it gets supplied with the previewed data when
* the post data is setup.
*
* @todo The WP_Post class does not provide any facility to filter post fields.
*
* @param WP_Post $post Post.
*/
public function preview_setup_postdata( WP_Post $post ) {
static $prevent_setup_postdata_recursion = false;
if ( $prevent_setup_postdata_recursion ) {
return;
}
$setting_id = WP_Customize_Post_Setting::get_post_setting_id( $post );
$setting = $this->component->manager->get_setting( $setting_id );
if ( $setting instanceof WP_Customize_Post_Setting ) {
$prevent_setup_postdata_recursion = true;
$setting->override_post_data( $post );
setup_postdata( $post );
$prevent_setup_postdata_recursion = false;
}
}
/**
* Retrieve post title and filter according to the current Customizer state.
*
* This is necessary because the is currently no filter yet in WP to mutate
* the underling post object. This specifically was noticed in the `get_the_title()`
* call in `WP_REST_Posts_Controller::prepare_item_for_response()`.
*
* @link https://github.com/xwp/wp-customize-posts/issues/96
* @link https://core.trac.wordpress.org/ticket/12955
*
* @param string $title Filtered title.
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @return string Title.
*/
public function filter_the_title( $title, $post ) {
if ( empty( $post ) ) {
return $title;
}
$post = get_post( $post );
if ( empty( $post ) ) {
return $title;
}
$setting_id = WP_Customize_Post_Setting::get_post_setting_id( $post );
$setting = $this->component->manager->get_setting( $setting_id );
if ( ! ( $setting instanceof WP_Customize_Post_Setting ) ) {
return $title;
}
$post_data = $setting->post_value();
if ( ! is_array( $post_data ) || ! isset( $post_data['post_title'] ) ) {
return $title;
}
$title = $post_data['post_title'];
/*
* Begin code modified from get_the_title():
* https://github.com/xwp/wordpress-develop/blob/6792df6fab87063e0564148c6634aaa0ed3156b4/src/wp-includes/post-template.php#L113-L148
*/
if ( ! is_admin() ) {
$mock_post = new WP_Post( (object) array_merge(
$post->to_array(),
$post_data
) );
if ( ! empty( $post_data['post_password'] ) ) {
/** This filter is documented in wp-includes/post-template.php */
$protected_title_format = apply_filters(
'protected_title_format',
/* translators: %s is post title */
__( 'Protected: %s', 'customize-posts' ),
$mock_post
);
$title = sprintf( $protected_title_format, $title );
} elseif ( isset( $post_data['post_status'] ) && 'private' === $post_data['post_status'] ) {
/** This filter is documented in wp-includes/post-template.php */
$private_title_format = apply_filters(
'private_title_format',
/* translators: %s is post title */
__( 'Private: %s', 'customize-posts' ),
$mock_post
);
$title = sprintf( $private_title_format, $title );
}
}
return $title;
}
/**
* Tally the posts that are previewed in the page.
*
* @param array $posts Posts.
* @return array
*/
public function filter_the_posts_to_tally_previewed_posts( array $posts ) {
foreach ( $posts as $post ) {
$this->queried_post_ids[] = $post->ID;
}
return $posts;
}
/**
* Tally the set-up post as being previewed in the page.
*
* @param WP_Post $post Post.
*/
public function handle_the_post_to_tally_previewed_post( $post ) {
$this->queried_post_ids[] = $post->ID;
}
/**
* Override post data for previewed settings.
*
* @param array $posts Posts.
* @return array Previewed posts.
*/
public function filter_the_posts_to_preview_settings( array $posts ) {
foreach ( $posts as &$post ) {
$post_setting_id = WP_Customize_Post_Setting::get_post_setting_id( $post );
$setting = $this->component->manager->get_setting( $post_setting_id );
if ( $setting instanceof WP_Customize_Post_Setting && isset( $this->previewed_post_settings[ $post->ID ] ) ) {
$setting->override_post_data( $post );
}
}
return $posts;
}
/**
* Prevent recursion in filter_get_pages_to_preview_settings().
*
* @var bool
*/
protected $disable_filter_get_pages_to_preview_settings = false;
/**
* Filter get_pages() to preview settings.
*
* Eventually this should become irrelevant once `get_pages()` uses `WP_Query`. See {@link https://core.trac.wordpress.org/ticket/12821}.
*
* @see get_pages()
*
* @param array $initial_posts List of pages to retrieve.
* @param array $args {
* Array of get_pages() arguments.
*
* @type array $exclude_tree Supported. This must be supported because it is used by wp_dropdown_pages().
* @type int $child_of Supported. This needs to be supported as it can be used by wp_list_pages().
* @type int $parent Supported.
* @type string $sort_order Supported.
* @type string $sort_column Supported.
* @type string $authors Supported.
* @type string $post_status Supported.
* @type int $number Supported, but there won't be 100% fidelity due to customized posts being amended to the subset results without being aware of underlying placement in full results.
* @type array $exclude No special support needed.
* @type array $include No special support needed.
* @type string $meta_key Not supported.
* @type string $meta_value Not supported.
* @type int $offset Not supported.
* @type bool $hierarchical Not needing to be examined since this is a property of the registered post type itself.
* @type string $post_type Not needing to be examined since post_type is immutable.
* }
* @return array|false Pages or false on error.
*/
public function filter_get_pages_to_preview_settings( $initial_posts, $args ) {
// Abort if we're making a recursive call due to exclude_tree.
if ( $this->disable_filter_get_pages_to_preview_settings ) {
return $initial_posts;
}
$unsupported_args = array( 'offset', 'meta_key', 'meta_value' );
foreach ( $unsupported_args as $unsupported_arg ) {
if ( ! empty( $args[ $unsupported_arg ] ) ) {
_doing_it_wrong(
'get_pages',
sprintf(
/* translators: %s is unsupported arg name */
esc_html__( 'The %s argument for get_pages() is not supported by Customize Posts.', 'customize-posts' ), esc_html( $unsupported_arg )
),
'0.8.0'
);
return false;
}
}
if ( ! is_array( $args['post_status'] ) ) {
$args['post_status'] = array_filter( explode( ',', $args['post_status'] ) );
}
$author_ids = array();
$authors = $args['authors'];
if ( ! is_array( $authors ) ) {
$authors = array_filter( explode( ',', $authors ) );
}
foreach ( $authors as $author ) {
if ( 0 === intval( $author ) ) {
$post_author = get_user_by( 'login', $author );
if ( empty( $post_author ) ) {
continue;
}
if ( empty( $post_author->ID ) ) {
continue;
}
$author_ids[] = $post_author->ID;
} else {
$author_ids[] = intval( $author );
}
}
$args['exclude_tree'] = array_filter( wp_parse_id_list( $args['exclude_tree'] ) );
$args['exclude'] = array_filter( wp_parse_id_list( $args['exclude'] ) );
$args['include'] = array_filter( wp_parse_id_list( $args['include'] ) );
$args['parent'] = intval( $args['parent'] );
$args['child_of'] = intval( $args['child_of'] );
$args['number'] = intval( $args['number'] );
if ( ! empty( $args['include'] ) ) {
$args['child_of'] = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include.
$args['parent'] = -1;
$args['exclude'] = '';
$args['meta_key'] = '';
$args['meta_value'] = '';
$args['hierarchical'] = false;
}
$customized_posts = array();
$filtered_posts = array();
foreach ( $initial_posts as $post ) {
$filtered_posts[ $post->ID ] = $post;
}
$post_values = $this->component->manager->unsanitized_post_values();
foreach ( $this->component->manager->settings() as $setting ) {
// Skip any settings that aren't customized.
if ( ! isset( $post_values[ $setting->id ] ) ) {
continue;
}
// Gather up post settings that have customizations to amend/augment the initial posts.
if ( ! ( $setting instanceof WP_Customize_Post_Setting ) ) {
continue;
}
if ( $args['post_type'] !== $setting->post_type ) {
continue;
}
if ( in_array( $setting->post_id, $args['exclude'], true ) ) {
continue;
}
if ( ! empty( $args['include'] ) && ! in_array( $setting->post_id, $args['include'], true ) ) {
continue;
}
if ( isset( $filtered_posts[ $setting->post_id ] ) ) {
$post = $filtered_posts[ $setting->post_id ];
} else {
$post = get_post( $setting->post_id );
$filtered_posts[ $setting->post_id ] = $post;
}
$setting->override_post_data( $post );
$customized_posts[ $setting->post_id ] = $post;
}
if ( ! empty( $args['exclude_tree'] ) || ! empty( $args['child_of'] ) ) {
// Include posts that are no longer in the exclude_tree.
$excluded_posts_to_remove = array();
foreach ( $args['exclude_tree'] as $exclude_tree ) {
// Re-add the all posts that were excluded but may no longer should be.
$this->disable_filter_get_pages_to_preview_settings = true;
$excluded_tree_posts = get_pages( array_merge(
$args,
array(
'child_of' => $exclude_tree,
'exclude_tree' => '',
)
) );
$this->disable_filter_get_pages_to_preview_settings = false;
foreach ( $excluded_tree_posts as $excluded_tree_post ) {
if ( ! isset( $filtered_posts[ $excluded_tree_post->ID ] ) ) {
$filtered_posts[ $excluded_tree_post->ID ] = $excluded_tree_post;
}
}
// Re-remove all excluded posts.
$excluded_posts_to_remove = array_merge(
$excluded_posts_to_remove,
array( $exclude_tree ),
wp_list_pluck( get_page_children( $exclude_tree, $filtered_posts ), 'ID' )
);
}
foreach ( $excluded_posts_to_remove as $post_id ) {
unset( $filtered_posts[ $post_id ] );
}
// Remove any posts that are no longer descendants of child_of.
if ( ! empty( $args['child_of'] ) ) {
// Re-add the all posts that were excluded but may no longer should be.
$this->disable_filter_get_pages_to_preview_settings = true;
$child_of_posts = get_pages( array_merge(
$args,
array(
'child_of' => '',
'exclude_tree' => $args['child_of'],
)
) );
$this->disable_filter_get_pages_to_preview_settings = false;
foreach ( $child_of_posts as $child_of_post ) {
if ( ! isset( $filtered_posts[ $child_of_post->ID ] ) ) {
$filtered_posts[ $child_of_post->ID ] = $child_of_post;
}
}
// Re-remove posts that are not child_of.
$child_of_post_ids = array();
foreach ( get_page_children( $args['child_of'], $filtered_posts ) as $child_of_post ) {
$child_of_post_ids[] = $child_of_post->ID;
}
foreach ( array_keys( $filtered_posts ) as $post_id ) {
if ( ! in_array( $post_id, $child_of_post_ids, true ) ) {
unset( $filtered_posts[ $post_id ] );
}
}
}
}
// Remove filtered posts that no longer match.
foreach ( array_keys( $filtered_posts ) as $post_id ) {
$post = $filtered_posts[ $post_id ];
$should_remove = (
! empty( $args['post_status'] ) && ! in_array( $post->post_status, $args['post_status'], true )
||
! empty( $author_ids ) && ! in_array( (int) $post->post_author, $author_ids, true )
||
$args['parent'] > 0 && $args['parent'] !== $post->post_parent
);
if ( $should_remove ) {
unset( $filtered_posts[ $post->ID ], $customized_posts[ $post->ID ] );
}
}
// Normalize sort_column and sort_order according to logic in get_pages().
$sort_columns = array_filter( explode( ',', $args['sort_column'] ) );
$allowed_keys = array( 'author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified', 'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent', 'ID', 'comment_count' );
foreach ( $sort_columns as $sort_column ) {
$sort_column = trim( $sort_column );
if ( ! in_array( $sort_column, $allowed_keys, true ) ) {
continue;
}
switch ( $sort_column ) {
case 'menu_order':
case 'ID':
case 'comment_count':
break;
default:
if ( 0 !== strpos( $sort_column, 'post_' ) ) {
$sort_column = "post_{$sort_column}";
}
}
$sort_columns[] = $sort_column;
}
$args['sort_column'] = $sort_columns;
if ( empty( $args['sort_column'] ) ) {
$args['sort_column'] = array( 'post_title' );
}
$args['sort_order'] = strtoupper( $args['sort_order'] );
if ( ! in_array( $args['sort_order'], array( 'ASC', 'DESC' ), true ) ) {
$args['sort_order'] = 'ASC';
}
// Re-sort posts according to args.
$this->current_get_pages_args = $args;
usort( $filtered_posts, array( $this, 'compare_posts_for_get_pages' ) );
$this->current_get_pages_args = array();
if ( ! empty( $args['number'] ) ) {
$filtered_posts = array_slice( $filtered_posts, 0, $args['number'] );
}
return array_values( $filtered_posts );
}
/**
* Current $args passed to get_pages().
*
* @var array
*/
protected $current_get_pages_args;
/**
* Sort two customized posts in get_pages().
*
* @access private
*
* @param WP_Post $post1 Post.
* @param WP_Post $post2 Post.
* @return int Comparison.
*/
protected function compare_posts_for_get_pages( $post1, $post2 ) {
foreach ( $this->current_get_pages_args['sort_column'] as $sort_column ) {
if ( is_string( $post1->$sort_column ) ) {
$comparison = strcmp( $post1->$sort_column, $post2->$sort_column );
} else {
$comparison = $post1->$sort_column - $post2->$sort_column;
}
if ( 'DESC' === $this->current_get_pages_args['sort_order'] ) {
$comparison = -$comparison;
}
if ( 0 !== $comparison ) {
return $comparison;
}
}
return 0;
}
/**
* Keep track of the orderby keys used in queries on the page.
*
* @param array $posts Posts.
* @param WP_Query $query Query.
* @return array Previewed posts.
*/
public function filter_the_posts_to_tally_orderby_keys( array $posts, WP_Query $query ) {
$orderby = $query->get( 'orderby' );
if ( empty( $orderby ) ) {
$orderby = 'date';
}
$this->queried_orderby_keys[] = $orderby;
return $posts;
}
/**
* Prepare for previewing a query to ensure that filters get applied and that customized query results don't get cached.
*
* @param WP_Query $query The WP_Query instance.
*/
public function prepare_query_preview( WP_Query $query ) {
if ( ! $query->is_singular() ) {
$query->set( 'cache_results', false );
$query->set( 'suppress_filters', false );
$query->set( 'es_integrate', false ); // Disable offloading to ElasticSearch for <https://github.com/10up/ElasticPress>.
$query->set( 'es', false ); // Disable offloading to ElasticSearch for <https://github.com/alleyinteractive/es-wp-query>.
}
}
/**
* Filter posts_request to inject subselect UNIONs to include posts with the customized state.
*
* @param string $sql_select A SQL SELECT query for posts.
* @param WP_Query $query The WP_Query instance (passed by reference).
* @return string SQL SELECT query.
*/
public function filter_posts_request_to_inject_customized_state( $sql_select, $query ) {
global $wpdb;
if ( $query->is_singular() ) {
return $sql_select;
}
/*
* Strip out SQL_CALC_FOUND_ROWS, ORDER BY, and LIMIT from subselect query since only relevant to outer query.
* The original SQL is constructed in WP_Query::get_posts() via:
* SELECT $found_rows $distinct $fields FROM {$this->db->posts} $join WHERE 1=1 $where $groupby $orderby $limits
*/
$sql_subselect = preg_replace( '#^SELECT\s+SQL_CALC_FOUND_ROWS\s+#i', 'SELECT ', $sql_select );
$sql_subselect = preg_replace( '#\s+LIMIT\s\d+(,\s*\d+)$#i', '', $sql_subselect );
$sql_subselect = preg_replace( '#\s+ORDER\s+BY\s+(\w+\.\w+(\s+(ASC|DESC))?)(\s*,\s*\w+\.\w+(\s+(ASC|DESC))?)*$#i', '', $sql_subselect );
// Notice: The list of fields must match the list of fields in the CREATE TABLE statement or else a MySQL error will occur.
$table_fields = array(
'ID' => 'UNSIGNED',
'post_author' => 'UNSIGNED',
'post_date' => 'DATETIME',
'post_date_gmt' => 'DATETIME',
'post_content' => 'TEXT',
'post_title' => 'TEXT',
'post_excerpt' => 'TEXT',
'post_status' => 'CHAR',
'comment_status' => 'CHAR',
'ping_status' => 'CHAR',
'post_password' => 'CHAR',
'post_name' => 'CHAR',
'to_ping' => 'NULL',
'pinged' => 'NULL',
'post_modified' => 'DATETIME',
'post_modified_gmt' => 'DATETIME',
'post_content_filtered' => 'TEXT',
'post_parent' => 'UNSIGNED',
'guid' => 'CHAR',
'menu_order' => 'UNSIGNED',
'post_type' => 'CHAR',
'post_mime_type' => 'CHAR',
'comment_count' => 'UNSIGNED',
);
/*
* Make sure subselect fields are the same as the unioned literal field selects.
* This is is important when requesting fields 'ids' or 'id=>parent' to prevent a MySQL error:
* > The used SELECT statements have a different number of columns for query
*/
$subselect_fields = array();
foreach ( array_keys( $table_fields ) as $field_name ) {
$subselect_fields[] = "$wpdb->posts.$field_name";
}
$sql_subselect = preg_replace(
'#^(SELECT\s+(DISTINCT\s+)?).+?(?=\s+FROM\s+)#i',
sprintf( '$1 %s', join( ',', $subselect_fields ) ),
$sql_subselect
);
$mentioned_fields = array();
foreach ( array_keys( $table_fields ) as $field_name ) {
$mentioned_fields[ $field_name ] = (bool) preg_match(
'/\b' . preg_quote( "$wpdb->posts.$field_name" ) . '\b/',
$sql_select
);
}
$sql_literal_selects = array();
$customized_post_ids = array();
$empty_date = '0000-00-00 00:00:00';
$post_values = $this->component->manager->unsanitized_post_values();
foreach ( $this->component->manager->settings() as $setting ) {
if ( ! ( $setting instanceof WP_Customize_Post_Setting ) ) {
continue;
}
if ( ! array_key_exists( $setting->id, $post_values ) ) {
continue;
}
$post = get_post( $setting->post_id );
if ( empty( $post ) ) {
continue;
}
$customized_post_ids[] = $setting->post_id;
$post_data = array_merge(
$post->to_array(),
$setting->augment_gmt_dates( $setting->value() )
);
foreach ( array( 'post_date', 'post_modified' ) as $date_field ) {
if ( $empty_date === $post_data[ $date_field ] ) {
$post_data[ $date_field ] = current_time( 'mysql', false );
}
}
$post_data = $setting->augment_gmt_dates( $post_data );
$select_fields = array();
foreach ( $table_fields as $field_name => $type ) {
if ( 'NULL' === $type || ( 'TEXT' === $type && ! $mentioned_fields[ $field_name ] ) ) {
$select_field = sprintf(
'NULL AS %s',
$field_name
);
} elseif ( 'CHAR' === $type || 'TEXT' === $type ) {
$select_field = sprintf(
'CAST( %s AS CHAR CHARACTER SET %s ) %s AS %s',
$wpdb->prepare( '%s', $post_data[ $field_name ] ), // Note: Not doing maybe_serialize() since not expected.
$wpdb->charset,
! empty( $wpdb->collate ) ? " COLLATE $wpdb->collate " : '',
$field_name
);
} else {
$select_field = sprintf(
'CAST( %s AS %s ) AS %s',
$wpdb->prepare( '%s', $post_data[ $field_name ] ),
$type,
$field_name
);
}
$select_fields[] = $select_field;
}
$sql_literal_select = sprintf( 'SELECT %s', join( ', ', $select_fields ) );
$sql_literal_selects[] = sprintf( '( %s )', $sql_literal_select );
}
if ( empty( $customized_post_ids ) ) {
return $sql_select;
}
// Exclude the customized posts from the subselect since they will be unioned with select literals.
$sql_subselect = preg_replace(
'#(?<=WHERE 1=1)#',
sprintf( " AND ( $wpdb->posts.ID NOT IN ( %s ) )", join( ',', array_map( 'absint', $customized_post_ids ) ) ),
$sql_subselect,
1 // Limit.
);
$sql_select = preg_replace(
sprintf( '#\sFROM %s\s#', preg_quote( $wpdb->posts, '#' ) ),
sprintf(
' FROM ( ( %1$s ) UNION ALL %2$s ) AS %3$s ',
$sql_subselect,
join( ' UNION ALL ', $sql_literal_selects ),
$wpdb->posts
),
$sql_select,
1 // Limit.
);
return $sql_select;
}
/**
* Queried meta keys.
*
* Used by `WP_Customize_Posts_Preview::_inject_meta_sql_customized_derived_tables()` due to the lack of closures.
*
* @var array
*/
protected $current_queried_meta_keys = array();
/**
* Current meta clauses.
*
* Used by `WP_Customize_Posts_Preview::_inject_meta_sql_customized_derived_tables()` due to the lack of closures.
*
* @var array
*/
protected $current_meta_clauses;
/**
* Filters the meta query's generated SQL to inject the customized data into a unioned derived table.
*
* @param array $clauses Array containing the query's JOIN and WHERE clauses.
* @param array $queries Array of meta queries.
* @param string $type Type of meta.
* @param string $primary_table Primary table.
* @param string $primary_id_column Primary column ID.
* @param WP_Query $main_query The main query object.
* @return array Clauses.
*/
public function filter_get_meta_sql_to_inject_customized_state( $clauses, $queries, $type, $primary_table, $primary_id_column, $main_query ) {
global $wpdb;
unset( $primary_table, $primary_id_column );
if ( 'post' !== $type ) {
return $clauses;
}
if ( $main_query && $main_query->is_singular() ) {
return $clauses;
}
$this->current_queried_meta_keys = array();
foreach ( $queries as $query ) {
if ( isset( $query['key'] ) ) {
$this->current_queried_meta_keys[] = $query['key'];
} elseif ( isset( $query['relation'] ) && isset( $query[0] ) ) {
foreach ( $query as $subquery ) {
if ( isset( $subquery['key'] ) ) {
$this->current_queried_meta_keys[] = $subquery['key'];
}
}
}
}
$this->current_queried_meta_keys = array_unique( $this->current_queried_meta_keys );
if ( empty( $this->current_queried_meta_keys ) ) {
return $clauses;
}
$this->current_meta_clauses = $clauses;
$clauses['join'] = preg_replace_callback(
'#(?P<join>(INNER|LEFT)\s+JOIN)\s+' . $wpdb->postmeta . '(?:\s+AS\s+(?P<table_alias>\w+))?(?=\s+ON)#',
array( $this, '_inject_meta_sql_customized_derived_tables' ),
$clauses['join']
);
$this->current_meta_clauses = null;
$this->current_queried_meta_keys = array();
return $clauses;
}
/**
* Inject customized derived tables into meta SQL.
*
* @access private
*
* @param array $matches Matches.
* @return string SQL JOIN.
*/
public function _inject_meta_sql_customized_derived_tables( $matches ) {
global $wpdb;
$table_alias = isset( $matches['table_alias'] ) ? $matches['table_alias'] : $wpdb->postmeta;
// Warning: The list of fields must match the list of fields in the CREATE TABLE statement or else a MySQL error will occur.
$table_fields = array(
'meta_id' => 'NULL',
'post_id' => 'UNSIGNED',
'meta_key' => 'CHAR',
'meta_value' => 'TEXT',
);
$mentioned_fields = array();
foreach ( array_keys( $table_fields ) as $field_name ) {
$mentioned_fields[ $field_name ] = (bool) preg_match(
'/\b' . preg_quote( "$table_alias.$field_name" ) . '\b/',
$this->current_meta_clauses['where']
);
}
$sql_literal_selects = array();
$sql_meta_exclusion_where_clauses = array();
$post_values = $this->component->manager->unsanitized_post_values();
foreach ( $this->component->manager->settings() as $setting ) {
if ( ! ( $setting instanceof WP_Customize_Postmeta_Setting ) ) {
continue;
}
if ( ! array_key_exists( $setting->id, $post_values ) ) {
continue;
}
$postmeta_rows = array();
if ( $setting->single ) {
$postmeta_rows[] = array(
'post_id' => $setting->post_id,
'meta_key' => $setting->meta_key,
'meta_value' => $setting->value(),
);
} else {
foreach ( $setting->value() as $meta_value ) {
$postmeta_rows[] = array(
'post_id' => $setting->post_id,
'meta_key' => $setting->meta_key,
'meta_value' => $meta_value,
);
}
}
/**
* Filter the postmeta rows that are being previewed.
*
* @param array $postmeta_rows Postmeta rows, associative arrays with keys for post_id, meta_key, and meta_value.
* @param WP_Customize_Postmeta_Setting $setting Post meta setting.
*/
$postmeta_rows = apply_filters( 'customize_previewed_postmeta_rows', $postmeta_rows, $setting );
/**
* Filter the postmeta rows that are being previewed for a specific key.
*
* @param array $postmeta_rows Postmeta rows, associative arrays with keys for post_id, meta_key, and meta_value.
* @param WP_Customize_Postmeta_Setting $setting Post meta setting.
*/
$postmeta_rows = apply_filters( "customize_previewed_postmeta_rows_{$setting->meta_key}", $postmeta_rows, $setting );
$previewed_meta_keys = wp_list_pluck( $postmeta_rows, 'meta_key' );
// Skip joining customized meta that isn't being queried.
if ( 0 === count( array_intersect( $previewed_meta_keys, $this->current_queried_meta_keys ) ) ) {
continue;
}
$sql_meta_exclusion_where_clauses[] = $wpdb->prepare( '( ! ( post_id = %d AND meta_key = %s ) )', $setting->post_id, $setting->meta_key );
foreach ( $postmeta_rows as $postmeta_row ) {
$postmeta_row['meta_id'] = null;
$select_fields = array();
foreach ( $table_fields as $field_name => $type ) {
if ( 'NULL' === $type || ( 'TEXT' === $type && ! $mentioned_fields[ $field_name ] ) ) {
$select_field = sprintf(
'NULL AS %s',
$field_name
);
} elseif ( 'CHAR' === $type || 'TEXT' === $type ) {
$select_field = sprintf(
'CAST( %s AS CHAR CHARACTER SET %s ) %s AS %s',
$wpdb->prepare( '%s', maybe_serialize( $postmeta_row[ $field_name ] ) ),
$wpdb->charset,
! empty( $wpdb->collate ) ? " COLLATE $wpdb->collate " : '',
$field_name
);
} else {
$select_field = sprintf(
'CAST( %s AS %s) AS %s',
$wpdb->prepare( '%s', $postmeta_row[ $field_name ] ),
$type,
$field_name
);
}
$select_fields[] = $select_field;
}
$sql_literal_selects[] = sprintf( '( SELECT %s )', join( ', ', $select_fields ) );
}
}
if ( empty( $sql_literal_selects ) ) {
return $matches[0];
}
$sql = ' ' . $matches['join'] . ' ';
$sql .= sprintf(
' ( ( %1$s ) UNION ALL %2$s ) AS %3$s ',
"SELECT * FROM $wpdb->postmeta WHERE " . join( ' AND ', $sql_meta_exclusion_where_clauses ),
join( ' UNION ALL ', $sql_literal_selects ),
$table_alias
);
return $sql;
}
/**
* Capture the post setting values before the nav menu item settings are created.
*
* For non-dirty nav menu item settings that reference a dirty post setting,
* the dirty post setting's url and title need to be filtered into the
* nav_menu_item value that is passed into the constructor for
* WP_Customize_Nav_Menu_Item_Setting because if the nav menu item setting
* is not itself dirty, then there is no subsequent filter to inject the
* dirty post setting's value.
*
* Additionally, for any nav_menu_item settings that are dirty, this method
* will ensure that the original_title for their values is set to be whatever
* the corresponding post title is in the incoming post setting value.
*
* @todo Core will need to re-work how it handles the original_title.
*/
public function capture_sanitized_post_setting_values_for_nav_menu_items() {
// Gather all of the post setting values.
foreach ( $this->component->manager->unsanitized_post_values() as $setting_id => $unsanitized_value ) {
if ( 'post[' !== substr( $setting_id, 0, 5 ) ) {
continue;
}
$this->component->manager->add_dynamic_settings( array( $setting_id ) );
$setting = $this->component->manager->get_setting( $setting_id );
if ( ! ( $setting instanceof WP_Customize_Post_Setting ) ) {
continue;
}
$sanitized_value = $setting->post_value();