This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
class-wp-customize-posts.php
1730 lines (1571 loc) · 58.4 KB
/
class-wp-customize-posts.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 Component Class
*
* Implements post management in the Customizer.
*
* @package WordPress
* @subpackage Customize
*/
/**
* Class WP_Customize_Posts
*/
final class WP_Customize_Posts {
/**
* WP_Customize_Manager instance.
*
* @access public
* @var WP_Customize_Manager
*/
public $manager;
/**
* Previewing posts.
*
* @var WP_Customize_Posts_Preview
*/
public $preview;
/**
* List of settings that have update conflicts in the current request.
*
* @var WP_Customize_Setting[]
*/
public $update_conflicted_settings = array();
/**
* Registered post meta.
*
* @var array
*/
public $registered_post_meta = array();
/**
* Registered support classes.
*
* @var array
*/
public $supports = array();
/**
* Whether the post link filters are being suppressed.
*
* @var bool
*/
public $suppress_post_link_filters = false;
/**
* Customize draft post IDs.
*
* @var array
*/
public $customize_draft_post_ids = array();
/**
* Initial loader.
*
* @access public
*
* @param WP_Customize_Manager $manager Customize manager bootstrap instance.
*/
public function __construct( WP_Customize_Manager $manager ) {
$this->manager = $manager;
require_once dirname( __FILE__ ) . '/class-wp-customize-posts-preview.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-posts-panel.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-section.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-dynamic-control.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-discussion-fields-control.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-setting.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-postmeta-setting.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-date-control.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-status-control.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-editor-control.php';
require_once ABSPATH . WPINC . '/customize/class-wp-customize-partial.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-field-partial.php';
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'customize_controls_init', array( $this, 'enqueue_editor' ) );
add_filter( 'customize_refresh_nonces', array( $this, 'add_customize_nonce' ) );
add_action( 'customize_register', array( $this, 'ensure_static_front_page_constructs_registered' ), 11 );
add_action( 'customize_register', array( $this, 'register_constructs' ), 20 );
add_filter( 'map_meta_cap', array( $this, 'filter_map_meta_cap' ), 10, 4 );
add_action( 'init', array( $this, 'register_meta' ), 100 );
add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args' ), 10, 2 );
add_filter( 'customize_dynamic_setting_class', array( $this, 'filter_customize_dynamic_setting_class' ), 5, 3 );
add_filter( 'customize_sanitize_nav_menus_created_posts', array( $this, 'filter_out_nav_menus_created_posts_for_customized_posts' ), 20 );
add_filter( 'customize_save_response', array( $this, 'filter_customize_save_response_for_conflicts' ), 10, 2 );
add_filter( 'customize_save_response', array( $this, 'filter_customize_save_response_to_export_saved_values' ), 10, 2 );
add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_templates' ) );
add_action( 'transition_post_status', array( $this, 'transition_customize_draft' ), 20, 3 );
add_action( 'after_setup_theme', array( $this, 'preview_customize_draft_post_ids' ) );
add_action( 'pre_get_posts', array( $this, 'preview_customize_draft' ) );
add_filter( 'post_link', array( $this, 'post_link_draft' ), 10, 2 );
add_filter( 'post_type_link', array( $this, 'post_link_draft' ), 10, 2 );
add_filter( 'page_link', array( $this, 'post_link_draft' ), 10, 2 );
add_action( 'wp_ajax_customize-posts-insert-auto-draft', array( $this, 'ajax_insert_auto_draft_post' ) );
add_action( 'wp_ajax_customize-posts-fetch-settings', array( $this, 'ajax_fetch_settings' ) );
add_action( 'wp_ajax_customize-posts-select2-query', array( $this, 'ajax_posts_select2_query' ) );
add_action( 'customize_register', array( $this, 'replace_nav_menus_hooks' ), 12 ); // Note that WP_Customize_Nav_Menus::customize_register() happens at 11.
$this->preview = new WP_Customize_Posts_Preview( $this );
}
/**
* Replace core's hook handlers with forked versions.
*
* @see WP_Customize_Nav_Menus::ajax_load_available_items()
* @see WP_Customize_Nav_Menus::ajax_search_available_items()
* @param WP_Customize_Manager $wp_customize Manager.
*/
public function replace_nav_menus_hooks( $wp_customize ) {
if ( ! isset( $wp_customize->nav_menus ) ) {
return;
}
if ( version_compare( strtok( get_bloginfo( 'version' ), '-' ), '4.7', '<' ) ) {
$handlers = array(
'wp_ajax_load-available-menu-items-customizer' => 'ajax_load_available_items',
'wp_ajax_search-available-menu-items-customizer' => 'ajax_search_available_items',
);
foreach ( $handlers as $action => $method_name ) {
$priority = has_action( $action, array( $wp_customize->nav_menus, $method_name ) );
if ( false !== $priority ) {
remove_action( $action, array( $wp_customize->nav_menus, $method_name ), $priority );
add_action( $action, array( $this, $method_name ), $priority );
}
}
}
// Make sure that customize-draft posts get published just as auto-draft posts do in core.
$priority = has_filter( 'customize_sanitize_nav_menus_created_posts', array( $wp_customize->nav_menus, 'sanitize_nav_menus_created_posts' ) );
if ( false !== $priority ) {
remove_filter( 'customize_sanitize_nav_menus_created_posts', array( $wp_customize->nav_menus, 'sanitize_nav_menus_created_posts' ), $priority );
add_filter( 'customize_sanitize_nav_menus_created_posts', array( $this, 'sanitize_nav_menus_created_posts' ), $priority, 3 );
}
}
/**
* Add nonce for customize posts.
*
* @param array $nonces Nonces.
* @return array Amended nonces.
*/
public function add_customize_nonce( $nonces ) {
$nonces['customize-posts'] = wp_create_nonce( 'customize-posts' );
return $nonces;
}
/**
* Instantiate a Customize Posts support class.
*
* The support class must extend `Customize_Posts_Support` or one of it's subclasses.
*
* @param string|Customize_Posts_Support $support The support class name or object.
*/
function add_support( $support ) {
if ( is_string( $support ) && class_exists( $support, false ) ) {
$support = new $support( $this );
}
if ( $support instanceof Customize_Posts_Support ) {
$class_name = get_class( $support );
if ( ! isset( $this->supports[ $class_name ] ) ) {
$this->supports[ $class_name ] = $support;
$support->init();
}
}
}
/**
* Get post type objects that can be managed in Customizer.
*
* By default only post types which are public will be included. This can be
* overridden if an explicit show_in_customizer arg is provided when
* registering the post type.
*
* @return array
*/
public function get_post_types() {
$post_types = array();
$post_type_objects = get_post_types( array(), 'objects' );
foreach ( $post_type_objects as $post_type_object ) {
$post_type_object = clone $post_type_object;
if ( ! isset( $post_type_object->show_in_customizer ) ) {
$post_type_object->show_in_customizer = $post_type_object->public;
}
$post_type_object->supports = get_all_post_type_supports( $post_type_object->name );
// Remove unnecessary properties.
unset( $post_type_object->register_meta_box_cb );
$post_types[ $post_type_object->name ] = $post_type_object;
}
return $post_types;
}
/**
* Set missing post type descriptions for built-in post types and explicitly disallow attachments in customizer UI.
*/
public function configure_builtin_post_types() {
global $wp_post_types;
if ( post_type_exists( 'post' ) && empty( $wp_post_types['post']->description ) ) {
$wp_post_types['post']->description = __( 'Posts are entries listed in reverse chronological order, usually on the site homepage or on a dedicated posts page. Posts can be organized by tags or categories.', 'customize-posts' );
}
if ( post_type_exists( 'page' ) && empty( $wp_post_types['page']->description ) ) {
$wp_post_types['page']->description = __( 'Pages are ordered and organized hierarchically instead of being listed by date. The organization of pages generally corresponds to the primary nav menu.', 'customize-posts' );
}
if ( post_type_exists( 'attachment' ) && ! isset( $wp_post_types['attachment']->show_in_customizer ) ) {
$wp_post_types['attachment']->show_in_customizer = false;
}
}
/**
* Register post meta for a given post type.
*
* Please note that a sanitize_callback is intentionally excluded because the
* meta sanitization logic should be re-used with the global register_meta()
* function, which includes a `$sanitize_callback` param.
*
* @see register_meta()
*
* @param string $post_type Post type.
* @param string $meta_key Meta key.
* @param array $setting_args Args.
*/
public function register_post_type_meta( $post_type, $meta_key, $setting_args = array() ) {
$setting_args = array_merge(
array(
'single' => true,
'capability' => null,
'theme_supports' => null,
'default' => null,
'transport' => null,
'sanitize_callback' => null,
'sanitize_js_callback' => null,
'validate_callback' => null,
'setting_class' => 'WP_Customize_Postmeta_Setting',
),
$setting_args
);
if ( ! has_filter( "auth_post_meta_{$meta_key}", array( $this, 'auth_post_meta_callback' ) ) ) {
add_filter( "auth_post_meta_{$meta_key}", array( $this, 'auth_post_meta_callback' ), 10, 4 );
}
// Filter out null values, aka array_filter with ! is_null.
foreach ( array_keys( $setting_args ) as $key => $value ) {
if ( is_null( $value ) ) {
unset( $setting_args[ $key ] );
}
}
if ( ! isset( $this->registered_post_meta[ $post_type ] ) ) {
$this->registered_post_meta[ $post_type ] = array();
}
$this->registered_post_meta[ $post_type ][ $meta_key ] = $setting_args;
}
/**
* Allow editing post meta in Customizer if user can edit_post for registered post meta.
*
* @param bool $allowed Whether the user can add the post meta. Default false.
* @param string $meta_key The meta key.
* @param int $post_id Post ID.
* @param int $user_id User ID.
* @return bool Allowed.
*/
public function auth_post_meta_callback( $allowed, $meta_key, $post_id, $user_id ) {
global $wp_customize;
if ( $allowed || empty( $wp_customize ) ) {
return $allowed;
}
$post = get_post( $post_id );
if ( ! $post ) {
return $allowed;
}
$post_type_object = get_post_type_object( $post->post_type );
if ( ! $post_type_object ) {
return $allowed;
}
if ( ! isset( $this->registered_post_meta[ $post->post_type ][ $meta_key ] ) ) {
return $allowed;
}
$registered_post_meta = $this->registered_post_meta[ $post->post_type ][ $meta_key ];
$allowed = (
( empty( $registered_post_meta['capability'] ) || user_can( $user_id, $registered_post_meta['capability'] ) )
&&
user_can( $user_id, $post_type_object->cap->edit_post, $post_id )
);
return $allowed;
}
/**
* Register post meta for the post types.
*
* Note that this has to be after all post types are registered.
*/
public function register_meta() {
/**
* Allow plugins to register meta.
*
* @param WP_Customize_Posts $this
*/
do_action( 'customize_posts_register_meta', $this );
}
/**
* Ensure that the static front page section and controls are registered even when there are no pages.
*
* @link https://core.trac.wordpress.org/ticket/38013
*
* @param WP_Customize_Manager $wp_customize Manager.
*/
public function ensure_static_front_page_constructs_registered( WP_Customize_Manager $wp_customize ) {
// Section.
$section = $wp_customize->get_section( 'static_front_page' );
if ( ! $section ) {
$section = $wp_customize->add_section( 'static_front_page', array(
'title' => __( 'Static Front Page', 'default' ),
'priority' => 120,
'description' => __( 'Your theme supports a static front page.', 'default' ),
) );
}
if ( array( $section, 'active_callback' ) === $section->active_callback ) {
$section->active_callback = array( $this, 'has_published_pages' );
}
// Show on Front.
if ( ! $wp_customize->get_setting( 'show_on_front' ) ) {
$wp_customize->add_setting( 'show_on_front', array(
'default' => get_option( 'show_on_front' ),
'capability' => 'manage_options',
'type' => 'option',
) );
}
if ( ! $wp_customize->get_control( 'show_on_front' ) ) {
$wp_customize->add_control( 'show_on_front', array(
'label' => __( 'Front page displays', 'default' ),
'section' => 'static_front_page',
'type' => 'radio',
'choices' => array(
'posts' => __( 'Your latest posts', 'default' ),
'page' => __( 'A static page', 'default' ),
),
) );
}
// Page on Front.
if ( ! $wp_customize->get_setting( 'page_on_front' ) ) {
$wp_customize->add_setting( 'page_on_front', array(
'type' => 'option',
'capability' => 'manage_options',
) );
}
$page_on_front_control = $wp_customize->get_control( 'page_on_front' );
if ( ! $page_on_front_control ) {
$page_on_front_control = $wp_customize->add_control( 'page_on_front', array(
'label' => __( 'Front page', 'default' ),
'section' => 'static_front_page',
'type' => 'dropdown-pages',
) );
}
// Disable WP 4.7 UI for page addition in favor of ours. See <https://core.trac.wordpress.org/ticket/38164>.
if ( property_exists( $page_on_front_control, 'allow_addition' ) ) {
$page_on_front_control->allow_addition = false;
}
// Page for Posts.
if ( ! $wp_customize->get_setting( 'page_for_posts' ) ) {
$wp_customize->add_setting( 'page_for_posts', array(
'type' => 'option',
'capability' => 'manage_options',
) );
}
$page_for_posts_control = $wp_customize->get_control( 'page_for_posts' );
if ( ! $page_for_posts_control ) {
$page_for_posts_control = $wp_customize->add_control( 'page_for_posts', array(
'label' => __( 'Posts page', 'default' ),
'section' => 'static_front_page',
'type' => 'dropdown-pages',
) );
}
// Disable WP 4.7 UI for page addition in favor of ours. See <https://core.trac.wordpress.org/ticket/38164>.
if ( property_exists( $page_for_posts_control, 'allow_addition' ) ) {
$page_for_posts_control->allow_addition = false;
}
}
/**
* Return whether there are published pages.
*
* Used as active callback for static front page section and controls.
*
* @returns bool Whether there are published (or to be published) pages.
*/
public function has_published_pages() {
// @todo Also look to see if there are any pages among in $this->get_setting( 'nav_menus_created_posts' )->value().
// Note we cannot use number=>1 since the first-returned page may be previewed to not be published.
return 0 !== count( get_pages( array(
'post_type' => 'page',
'post_status' => 'publish',
) ) );
}
/**
* Register panels for post types, sections for any pre-registered settings, and any control types needed by JS.
*/
public function register_constructs() {
$this->manager->register_section_type( 'WP_Customize_Post_Section' );
$this->manager->register_control_type( 'WP_Customize_Dynamic_Control' );
$this->manager->register_control_type( 'WP_Customize_Post_Discussion_Fields_Control' );
$this->manager->register_control_type( 'WP_Customize_Post_Date_Control' );
$this->manager->register_control_type( 'WP_Customize_Post_Editor_Control' );
$this->manager->register_control_type( 'WP_Customize_Post_Status_Control' );
$panel_priority = 900; // Before widgets.
// Note that this does not include nav_menu_item.
$this->configure_builtin_post_types();
foreach ( $this->get_post_types() as $post_type_object ) {
if ( empty( $post_type_object->show_in_customizer ) ) {
continue;
}
$panel_id = sprintf( 'posts[%s]', $post_type_object->name );
// @todo Should this panel be filterable so that other post types can customize which subclass is used?
$panel = new WP_Customize_Posts_Panel( $this->manager, $panel_id, array(
'title' => $post_type_object->labels->name,
'description' => $post_type_object->description,
'priority' => $panel_priority + $post_type_object->menu_position,
'capability' => $post_type_object->cap->edit_posts,
'post_type' => $post_type_object->name,
) );
$this->manager->add_panel( $panel );
// Note the following is an alternative to doing WP_Customize_Manager::register_panel_type().
add_action( 'customize_controls_print_footer_scripts', array( $panel, 'print_template' ) );
}
}
/**
* Map dynamic post/postmeta capabilities to static capabilities.
*
* @param array $caps Returns the user's actual capabilities.
* @param string $cap Capability name.
* @param int $user_id The user ID.
* @return array Caps.
*/
public function filter_map_meta_cap( $caps, $cap, $user_id ) {
if ( preg_match( '/^(?:edit_post|edit_post_meta)\[\d+/', $cap ) ) {
$keys = explode( '[', str_replace( ']', '', $cap ) );
$map_meta_cap_args = array(
array_shift( $keys ),
$user_id,
intval( array_shift( $keys ) ),
array_shift( $keys ),
);
$caps = call_user_func_array( 'map_meta_cap', $map_meta_cap_args );
}
return $caps;
}
/**
* Determine the arguments for a dynamically-created setting.
*
* @access public
*
* @param false|array $args The arguments to the WP_Customize_Setting constructor.
* @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`.
* @return false|array Setting arguments, false otherwise.
*/
public function filter_customize_dynamic_setting_args( $args, $setting_id ) {
if ( preg_match( WP_Customize_Post_Setting::SETTING_ID_PATTERN, $setting_id, $matches ) ) {
$post_type = get_post_type_object( $matches['post_type'] );
if ( ! $post_type ) {
return $args;
}
if ( false === $args ) {
$args = array();
}
$args['type'] = 'post';
$args['transport'] = 'postMessage';
} elseif ( preg_match( WP_Customize_Postmeta_Setting::SETTING_ID_PATTERN, $setting_id, $matches ) ) {
if ( ! post_type_exists( $matches['post_type'] ) ) {
return $args;
}
if ( ! isset( $this->registered_post_meta[ $matches['post_type'] ][ $matches['meta_key'] ] ) ) {
return $args;
}
$registered = $this->registered_post_meta[ $matches['post_type'] ][ $matches['meta_key'] ];
if ( isset( $registered['theme_supports'] ) && ! current_theme_supports( $registered['theme_supports'] ) ) {
// We don't really need this because theme_supports will already filter it out of being exported.
return $args;
}
if ( false === $args ) {
$args = array();
}
$args = array_merge(
$args,
$registered
);
$args['type'] = 'postmeta';
}
return $args;
}
/**
* Filters customize_dynamic_setting_class.
*
* @param string $class Setting class.
* @param string $setting_id Setting ID.
* @param array $args Setting args.
*
* @return string
*/
public function filter_customize_dynamic_setting_class( $class, $setting_id, $args ) {
unset( $setting_id );
if ( isset( $args['type'] ) ) {
if ( 'post' === $args['type'] ) {
$class = 'WP_Customize_Post_Setting';
} elseif ( 'postmeta' === $args['type'] ) {
if ( isset( $args['setting_class'] ) ) {
$class = $args['setting_class'];
} else {
$class = 'WP_Customize_Postmeta_Setting';
}
}
}
return $class;
}
/**
* Filter the value for `nav_menus_created_posts` to remove post IDs for posts which being fully customized.
*
* If an ID is present among `nav_menus_created_posts` while also among the customized posts,
* then a conflict will arise. For example, if a post stub gets edited with its status changed
* to 'private' then when `WP_Customize_Nav_Menus::save_nav_menus_created_posts()` runs
* it can override it to be 'publish` if the setting gets updated after the post setting
* is saved. If, on the other hand, the `nav_menus_created_posts` setting is processed
* first then the subsequent save for the `post` setting can fail due to post conflict locking.
*
* @param array $post_ids IDs for post/page stubs.
* @return array IDs for posts that do not have post settings.
*/
public function filter_out_nav_menus_created_posts_for_customized_posts( $post_ids ) {
$non_customized_post_ids = array();
foreach ( $post_ids as $post_id ) {
$post = get_post( $post_id );
if ( ! $post ) {
continue;
}
$setting_id = WP_Customize_Post_Setting::get_post_setting_id( $post );
if ( $this->manager->get_setting( $setting_id ) ) {
continue;
}
$non_customized_post_ids[] = $post_id;
}
return $non_customized_post_ids;
}
/**
* Add all postmeta settings for all registered postmeta for a given post type instance.
*
* @param WP_Post $post Post.
* @return array
*/
public function register_post_type_meta_settings( $post ) {
$setting_ids = array();
if ( isset( $this->registered_post_meta[ $post->post_type ] ) ) {
foreach ( array_keys( $this->registered_post_meta[ $post->post_type ] ) as $key ) {
$setting_ids[] = WP_Customize_Postmeta_Setting::get_post_meta_setting_id( $post, $key );
}
$this->manager->add_dynamic_settings( $setting_ids );
}
return $setting_ids;
}
/**
* Get the post status choices array.
*
* @return array
*/
public function get_post_status_choices() {
$choices = array(
array(
'value' => 'draft',
'text' => __( 'Draft', 'customize-posts' ),
),
array(
'value' => 'pending',
'text' => __( 'Pending Review', 'customize-posts' ),
),
array(
'value' => 'private',
'text' => __( 'Private', 'customize-posts' ),
),
array(
'value' => 'publish',
'text' => __( 'Published', 'customize-posts' ),
),
array(
'value' => 'future',
'text' => __( 'Scheduled', 'customize-posts' ),
),
array(
'value' => 'trash',
'text' => __( 'Trash', 'customize-posts' ),
),
);
return $choices;
}
/**
* Get the author choices array.
*
* @return array
*/
public function get_author_choices() {
$choices = array();
$query_args = array(
'orderby' => 'display_name',
'who' => 'authors',
'fields' => array( 'ID', 'user_login', 'display_name' ),
);
$users = get_users( $query_args );
if ( ! empty( $users ) ) {
foreach ( (array) $users as $user ) {
$choices[] = array(
'value' => (int) $user->ID,
'text' => sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'customize-posts' ), $user->display_name, $user->user_login ),
);
}
}
return $choices;
}
/**
* Generate options for the month Select.
*
* Based on touch_time().
*
* @see touch_time()
*
* @return array
*/
public function get_date_month_choices() {
global $wp_locale;
$months = array();
for ( $i = 1; $i < 13; $i = $i + 1 ) {
$month_number = zeroise( $i, 2 );
$month_text = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );
/* translators: 1: month number, 2: month abbreviation */
$months[ $i ]['text'] = sprintf( __( '%1$s-%2$s', 'customize-posts' ), $month_number, $month_text );
$months[ $i ]['value'] = $month_number;
}
return $months;
}
/**
* Return whether current user can edit supplied post.
*
* @param WP_Post|int $post Post.
* @return boolean
*/
public function current_user_can_edit_post( $post ) {
if ( is_int( $post ) ) {
$post = get_post( $post );
}
if ( ! $post ) {
return false;
}
$post_type_obj = get_post_type_object( $post->post_type );
if ( ! $post_type_obj ) {
return false;
}
$can_edit = current_user_can( $post_type_obj->cap->edit_post, $post->ID );
return $can_edit;
}
/**
* Return the latest setting data for conflicted posts.
*
* Note that this uses `WP_Customize_Setting::value()` in a way that assumes
* that the `WP_Customize_Setting::preview()` has not been called, as it not
* called when `WP_Customize_Manager::save()` happens.
*
* @param array $response Response.
* @return array
*/
public function filter_customize_save_response_for_conflicts( $response ) {
if ( ! empty( $this->update_conflicted_settings ) ) {
$response['update_conflicted_setting_values'] = array();
foreach ( $this->update_conflicted_settings as $setting_id => $setting ) {
$response['update_conflicted_setting_values'][ $setting_id ] = $setting->js_value();
}
}
return $response;
}
/**
* Return the saved sanitized values for posts and postmeta to update in the client.
*
* This was originally in the Customize Setting Validation plugin.
*
* @link https://github.com/xwp/wp-customize-setting-validation/blob/2e5ddc66a870ad7b1aee5f8e414bad4b78e120d2/php/class-plugin.php#L283-L317
*
* @param array $response Response.
* @return array
*/
public function filter_customize_save_response_to_export_saved_values( $response ) {
$has_invalidities = (
isset( $response['setting_validities'] )
&&
count( array_filter( $response['setting_validities'], 'is_array' ) ) > 0
);
$changeset_status_publish = (
empty( $response['changeset_status'] ) // Prior to 4.7, this filter only would run on actual saves.
||
'publish' === $response['changeset_status']
);
// Short circuit if there there were invalidities or the changeset status was not publish.
if ( $has_invalidities || ! $changeset_status_publish ) {
return $response;
}
$response['saved_post_setting_values'] = array();
foreach ( array_keys( $this->manager->unsanitized_post_values() ) as $setting_id ) {
$setting = $this->manager->get_setting( $setting_id );
if ( ( $setting instanceof WP_Customize_Post_Setting || $setting instanceof WP_Customize_Postmeta_Setting ) && get_post( $setting->post_id ) ) {
$response['saved_post_setting_values'][ $setting->id ] = $setting->js_value();
}
}
return $response;
}
/**
* Enqueue scripts and styles for Customize Posts.
*/
public function enqueue_scripts() {
wp_enqueue_script( 'customize-posts' );
wp_enqueue_style( 'customize-posts' );
if ( isset( $this->manager->nav_menus ) ) {
wp_enqueue_script( 'customize-nav-menus-posts-extensions' );
}
$this->enqueue_select2_locale_script();
$post_types = array();
foreach ( $this->get_post_types() as $post_type => $post_type_obj ) {
if ( ! current_user_can( $post_type_obj->cap->edit_posts ) ) {
continue;
}
$post_types[ $post_type ] = array_merge(
wp_array_slice_assoc( (array) $post_type_obj, array(
'name',
'supports',
'labels',
'has_archive',
'menu_icon',
'description',
'hierarchical',
'show_in_customizer',
'publicly_queryable',
'public',
) ),
array(
'current_user_can' => array(
'create_posts' => isset( $post_type_obj->cap->create_posts ) && current_user_can( $post_type_obj->cap->create_posts ),
'edit_published_posts' => isset( $post_type_obj->cap->edit_published_posts ) && current_user_can( $post_type_obj->cap->edit_published_posts ),
'delete_posts' => isset( $post_type_obj->cap->delete_posts ) && current_user_can( $post_type_obj->cap->delete_posts ),
),
)
);
}
$exports = array(
'postTypes' => $post_types,
'postStatusChoices' => $this->get_post_status_choices(),
'authorChoices' => $this->get_author_choices(), // @todo Use Ajax to fetch this data or Customize Object Selector (once it supports users).
'dateMonthChoices' => $this->get_date_month_choices(),
'initialServerDate' => current_time( 'mysql', false ),
'initialServerTimestamp' => floor( microtime( true ) * 1000 ),
'l10n' => array(
/* translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */
'sectionCustomizeActionTpl' => __( 'Customizing ▸ %s', 'customize-posts' ),
'fieldTitleLabel' => __( 'Title', 'customize-posts' ),
'fieldSlugLabel' => __( 'Slug', 'customize-posts' ),
'fieldStatusLabel' => __( 'Status', 'customize-posts' ),
'fieldDateLabel' => __( 'Date', 'customize-posts' ),
'fieldContentLabel' => __( 'Content', 'customize-posts' ),
'fieldExcerptLabel' => __( 'Excerpt', 'customize-posts' ),
'fieldDiscussionLabel' => __( 'Discussion', 'customize-posts' ),
'fieldAuthorLabel' => __( 'Author', 'customize-posts' ),
'fieldParentLabel' => __( 'Parent', 'customize-posts' ),
'fieldOrderLabel' => __( 'Order', 'customize-posts' ),
'noTitle' => __( '(no title)', 'customize-posts' ),
'theirChange' => __( 'Their change: %s', 'customize-posts' ),
'openEditor' => __( 'Open Editor', 'customize-posts' ), // @todo Move this into editor control?
'closeEditor' => __( 'Close Editor', 'customize-posts' ),
'invalidDateError' => __( 'Whoops, the provided date is invalid.', 'customize-posts' ),
/* translators: %s is the trashed page name */
'dropdownPagesOptionTrashed' => __( '%s (Trashed)', 'customize-posts' ),
/* translators: %s is the trashed page name */
'dropdownPagesOptionUnpublished' => __( '%s (Unpublished)', 'customize-posts' ),
'editingPageForPostsNotice' => __( 'You are currently editing the page that shows your latest posts.', 'default' ),
'editPostFailure' => __( 'Failed to open for editing.', 'customize-posts' ),
'createPostFailure' => __( 'Failed to create for editing.', 'customize-posts' ),
'installCustomizeObjectSelector' => sprintf(
__( 'This control depends on having the %s plugin installed and activated.', 'customize-posts' ),
sprintf(
'<a href="%s" target="_blank">%s</a>',
'https://github.com/xwp/wp-customize-object-selector',
__( 'Customize Object Selector', 'customize-posts' )
)
),
'trashPostNotification' => __( 'This has been untrashed. If you publish changes now, its status will change to the selected status. Move back to trash to avoid this.' , 'customize-posts' ),
/* translators: %s post type */
'jumpToPostPlaceholder' => __( 'Jump to %s', 'customize-posts' ),
),
);
wp_scripts()->add_data( 'customize-posts', 'data', sprintf( 'var _wpCustomizePostsExports = %s;', wp_json_encode( $exports ) ) );
}
/**
* Enqueue select2 locale script.
*/
public function enqueue_select2_locale_script() {
$plugin_dir = dirname( dirname( __FILE__ ) );
$locale = str_replace( '_', '-', get_locale() );
$locale_files = array();
$locale_files[ $locale ] = 'bower_components/select2/dist/js/i18n/' . $locale . '.js';
if ( false !== strpos( $locale, '-' ) ) {
$language = strtok( $locale, '-' );
$locale_files[ $language ] = 'bower_components/select2/dist/js/i18n/' . $language . '.js';
}
foreach ( $locale_files as $locale => $locale_file ) {
if ( file_exists( $plugin_dir . '/' . $locale_file ) ) {
$handle = 'select2-locale-' . strtolower( $locale );
$src = plugins_url( $locale_file, dirname( __FILE__ ) );
wp_enqueue_script(
$handle,
$src,
array( 'select2' ),
wp_scripts()->query( 'select2' )->ver
);
$data = sprintf( 'jQuery.fn.select2.defaults.set( "language", %s );', wp_json_encode( $locale ) );
wp_add_inline_script( $handle, $data, 'after' );
break;
}
}
}
/**
* Format GMT Offset.
*
* @see wp_timezone_choice()
* @param float $offset Offset in hours.
* @return string Formatted offset.
*/
public function format_gmt_offset( $offset ) {
if ( 0 <= $offset ) {
$formatted_offset = '+' . (string) $offset;
} else {
$formatted_offset = (string) $offset;
}
$formatted_offset = str_replace(
array( '.25', '.5', '.75' ),
array( ':15', ':30', ':45' ),
$formatted_offset
);
return $formatted_offset;
}
/**
* Enqueue a WP Editor instance we can use for rich text editing.
*
* @todo Consider moving this to WP_Customize_Post_Editor_Control::enqueue_scripts().
* @todo This can be added at the customize_controls_enqueue_scripts action.
*/
public function enqueue_editor() {
add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_editor' ), 0 );
// Note that WP_Customize_Widgets::print_footer_scripts() happens at priority 10.
add_action( 'customize_controls_print_footer_scripts', array( $this, 'maybe_do_admin_print_footer_scripts' ), 20 );
// @todo These should be included in _WP_Editors::editor_settings()
if ( false === has_action( 'customize_controls_print_footer_scripts', array( '_WP_Editors', 'enqueue_scripts' ) ) ) {
add_action( 'customize_controls_print_footer_scripts', array( '_WP_Editors', 'enqueue_scripts' ) );
}
}
/**
* Render rich text editor.
*
* @todo Consider moving this to WP_Customize_Post_Editor_Control::enqueue_scripts().
*/
public function render_editor() {
?>
<div id="customize-posts-content-editor-pane">
<div id="customize-posts-content-editor-dragbar">
<span class="screen-reader-text"><?php esc_html_e( 'Resize Editor', 'customize-posts' ); ?></span>
</div>
<h2 id="customize-posts-content-editor-title"></h2>
<?php
// The settings passed in here are derived from those used in edit-form-advanced.php.
wp_editor( '', 'customize-posts-content', array(
'_content_editor_dfw' => false,
'drag_drop_upload' => true,
'tabfocus_elements' => 'content-html,save-post',
'editor_height' => 200,
'default_editor' => 'tinymce',
'tinymce' => array(
'resize' => false,
'wp_autoresize_on' => false,
'add_unload_trigger' => false,
),
) );
?>
</div>
<?php
}
/**
* Do the admin_print_footer_scripts actions if not done already.
*
* Another possibility here is to opt-in selectively to the desired widgets
* via:
* Shortcode_UI::get_instance()->action_admin_enqueue_scripts();
* Shortcake_Bakery::get_instance()->action_admin_enqueue_scripts();
*
* Note that this action is also done in WP_Customize_Widgets::print_footer_scripts()
* at priority 10, so this method runs at a later priority to ensure the action is
* not done twice.
*
* @todo Consider moving this to WP_Customize_Post_Editor_Control::enqueue_scripts().
*
* @codeCoverageIgnore
*/
public function maybe_do_admin_print_footer_scripts() {
if ( ! did_action( 'admin_print_footer_scripts' ) ) {
/** This action is documented in wp-admin/admin-footer.php */
do_action( 'admin_print_footer_scripts' );
}
if ( ! did_action( 'admin_footer-post.php' ) ) {
/** This action is documented in wp-admin/admin-footer.php */
do_action( 'admin_footer-post.php' );
}
}
/**
* Sanitize a value as a post ID.
*
* @param mixed $value Value.
* @return int Sanitized post ID.
*/
public function sanitize_post_id( $value ) {
$value = intval( $value );
return $value;
}
/**
* Underscore (JS) templates.