This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
/
class-post-type.php
728 lines (665 loc) · 22.7 KB
/
class-post-type.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
<?php
/**
* Customize Snapshot.
*
* @package CustomizeSnapshots
*/
namespace CustomizeSnapshots;
/**
* Customize Snapshot Class
*
* Implements snapshots for Customizer settings
*
* @package CustomizeSnapshots
*/
class Post_Type {
/**
* Post type.
*
* @type string
*/
const SLUG = 'customize_snapshot';
/**
* Customize_Snapshot_Manager instance.
*
* @access protected
* @var Customize_Snapshot_Manager
*/
public $snapshot_manager;
/**
* Whether kses filters on content_save_pre are added.
*
* @var bool
*/
protected $kses_suspended = false;
/**
* Constructor.
*
* @access public
*
* @throws Exception If the UUID is invalid.
*
* @param Customize_Snapshot_Manager $snapshot_manager Customize snapshot bootstrap instance.
*/
public function __construct( Customize_Snapshot_Manager $snapshot_manager ) {
$this->snapshot_manager = $snapshot_manager;
}
/**
* Register post type.
*/
public function register() {
$labels = array(
'name' => _x( 'Snapshots', 'post type general name', 'customize-snapshots' ),
'singular_name' => _x( 'Snapshot', 'post type singular name', 'customize-snapshots' ),
'menu_name' => _x( 'Snapshots', 'admin menu', 'customize-snapshots' ),
'name_admin_bar' => _x( 'Snapshot', 'add new on admin bar', 'customize-snapshots' ),
'add_new' => _x( 'Add New', 'Customize Snapshot', 'customize-snapshots' ),
'add_new_item' => __( 'Add New Snapshot', 'customize-snapshots' ),
'new_item' => __( 'New Snapshot', 'customize-snapshots' ),
'edit_item' => __( 'Inspect Snapshot', 'customize-snapshots' ),
'view_item' => __( 'View Snapshot', 'customize-snapshots' ),
'all_items' => __( 'All Snapshots', 'customize-snapshots' ),
'search_items' => __( 'Search Snapshots', 'customize-snapshots' ),
'not_found' => __( 'No snapshots found.', 'customize-snapshots' ),
'not_found_in_trash' => __( 'No snapshots found in Trash.', 'customize-snapshots' ),
);
$args = array(
'labels' => $labels,
'description' => __( 'Customize Snapshots.', 'customize-snapshots' ),
'public' => true,
'publicly_queryable' => false,
'query_var' => false,
'exclude_from_search' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'show_in_menu' => true,
'show_in_admin_bar' => false,
'map_meta_cap' => true,
'hierarchical' => false,
'delete_with_user' => false,
'menu_position' => null,
'supports' => array( 'author', 'revisions' ),
'capability_type' => static::SLUG,
'capabilities' => array(
'create_posts' => 'do_not_allow',
),
'rewrite' => false,
'show_in_customizer' => false, // Prevent inception.
'show_in_rest' => true,
'rest_base' => 'customize_snapshots',
'rest_controller_class' => __NAMESPACE__ . '\\Snapshot_REST_API_Controller',
'customize_snapshot_post_type_obj' => $this,
'menu_icon' => 'dashicons-camera',
'register_meta_box_cb' => array( $this, 'setup_metaboxes' ),
);
register_post_type( static::SLUG, $args );
add_filter( 'post_type_link', array( $this, 'filter_post_type_link' ), 10, 2 );
add_action( 'add_meta_boxes_' . static::SLUG, array( $this, 'remove_slug_metabox' ), 100 );
add_action( 'load-revision.php', array( $this, 'suspend_kses_for_snapshot_revision_restore' ) );
add_filter( 'get_the_excerpt', array( $this, 'filter_snapshot_excerpt' ), 10, 2 );
add_filter( 'post_row_actions', array( $this, 'filter_post_row_actions' ), 10, 2 );
add_filter( 'wp_insert_post_data', array( $this, 'preserve_post_name_in_insert_data' ), 10, 2 );
add_filter( 'user_has_cap', array( $this, 'filter_user_has_cap' ), 10, 2 );
add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 );
add_action( 'admin_notices', array( $this, 'show_publish_error_admin_notice' ) );
add_action( 'post_submitbox_minor_actions', array( $this, 'hide_disabled_publishing_actions' ) );
add_filter( 'content_save_pre', array( $this, 'filter_out_settings_if_removed_in_metabox' ), 10 );
add_action( 'admin_print_scripts-revision.php', array( $this, 'disable_revision_ui_for_published_posts' ) );
}
/**
* Filter post link.
*
* @param string $url URL.
* @param \WP_Post $post Post.
* @return string URL.
*/
public function filter_post_type_link( $url, $post ) {
if ( self::SLUG === $post->post_type ) {
$url = add_query_arg(
array( 'customize_snapshot_uuid' => $post->post_name ),
home_url( '/' )
);
}
return $url;
}
/**
* Suspend kses which runs on content_save_pre and can corrupt JSON in post_content.
*
* @see \sanitize_post()
*/
function suspend_kses() {
if ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) ) {
$this->kses_suspended = true;
kses_remove_filters();
}
}
/**
* Restore kses which runs on content_save_pre and can corrupt JSON in post_content.
*
* @see \sanitize_post()
*/
function restore_kses() {
if ( $this->kses_suspended ) {
kses_init_filters();
$this->kses_suspended = false;
}
}
/**
* Add the metabox.
*/
public function setup_metaboxes() {
$id = static::SLUG;
$title = __( 'Data', 'customize-snapshots' );
$callback = array( $this, 'render_data_metabox' );
$screen = static::SLUG;
$context = 'normal';
$priority = 'high';
add_meta_box( $id, $title, $callback, $screen, $context, $priority );
}
/**
* Remove publish metabox for published posts, since they should be immutable once published.
*
* @codeCoverageIgnore
*/
public function remove_slug_metabox() {
remove_meta_box( 'slugdiv', static::SLUG, 'normal' );
}
/**
* Make sure that restoring snapshot revisions doesn't involve kses corrupting the post_content.
*
* Ideally there would be an action like pre_wp_restore_post_revision instead
* of having to hack into the load-revision.php action. But even more ideally
* we should be able to disable such content_save_pre filters from even applying
* for certain post types, such as those which store JSON in post_content.
*
* @codeCoverageIgnore
*/
function suspend_kses_for_snapshot_revision_restore() {
if ( ! isset( $_GET['revision'] ) ) { // WPCS: input var ok.
return;
}
if ( ! isset( $_GET['action'] ) || 'restore' !== $_GET['action'] ) { // WPCS: input var ok, sanitization ok.
return;
}
$revision_post_id = intval( $_GET['revision'] ); // WPCS: input var ok.
if ( $revision_post_id <= 0 ) {
return;
}
$revision_post = wp_get_post_revision( $revision_post_id );
if ( empty( $revision_post ) ) {
return;
}
$post = get_post( $revision_post->post_parent );
if ( empty( $post ) || static::SLUG !== $post->post_type ) {
return;
}
$this->suspend_kses();
$that = $this;
add_action( 'wp_restore_post_revision', function() use ( $that ) {
$that->restore_kses();
} );
}
/**
* Include the setting IDs in the excerpt.
*
* @param string $excerpt The post excerpt.
* @param \WP_Post $post Post object.
* @return string Excerpt.
*/
public function filter_snapshot_excerpt( $excerpt, $post = null ) {
$post = get_post( $post );
if ( static::SLUG === $post->post_type ) {
$excerpt = '<ol>';
foreach ( $this->get_post_content( $post ) as $setting_id => $setting_params ) {
if ( ! isset( $setting_params['dirty'] ) || true === $setting_params['dirty'] ) {
$excerpt .= sprintf( '<li><code>%s</code></li>', esc_attr( $setting_id ) );
}
}
$excerpt .= '</ol>';
}
return $excerpt;
}
/**
* Add Customize link to quick edit links.
*
* @param array $actions Actions.
* @param \WP_Post $post Post.
* @return array Actions.
*/
public function filter_post_row_actions( $actions, $post ) {
if ( static::SLUG !== $post->post_type ) {
return $actions;
}
$post_type_obj = get_post_type_object( static::SLUG );
if ( 'publish' !== $post->post_status && current_user_can( $post_type_obj->cap->edit_post, $post->ID ) ) {
$args = array(
'customize_snapshot_uuid' => $post->post_name,
);
$customize_url = add_query_arg( array_map( 'rawurlencode', $args ), wp_customize_url() );
$actions = array_merge(
array(
'customize' => sprintf( '<a href="%s">%s</a>', esc_url( $customize_url ), esc_html__( 'Customize', 'customize-snapshots' ) ),
),
$actions
);
$actions = array_merge(
array(
'front-view' => sprintf(
'<a href="%s">%s</a>',
esc_url( get_permalink( $post->ID ) ),
esc_html__( 'Preview Snapshot', 'customize-snapshots' )
),
),
$actions
);
} else {
unset( $actions['inline hide-if-no-js'] );
if ( isset( $actions['edit'] ) ) {
$actions['edit'] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
get_edit_post_link( $post->ID, 'display' ),
/* translators: %s: post title */
esc_attr( sprintf( __( 'View “%s”', 'customize-snapshots' ), get_the_title( $post->ID ) ) ),
__( 'View', 'customize-snapshots' )
);
}
}
return $actions;
}
/**
* Preserve the post_name when submitting a snapshot for review.
*
* @see wp_insert_post()
* @link https://github.com/xwp/wordpress-develop/blob/831a186108983ade4d647124d4e56e09aa254704/src/wp-includes/post.php#L3134-L3137
*
* @param array $post_data Post data.
* @param array $original_post_data Original post data.
* @return array Post data.
*/
public function preserve_post_name_in_insert_data( $post_data, $original_post_data ) {
if ( empty( $post_data['post_type'] ) || static::SLUG !== $post_data['post_type'] ) {
return $post_data;
}
if ( empty( $post_data['post_name'] ) && 'pending' === $post_data['post_status'] ) {
$post_data['post_name'] = $original_post_data['post_name'];
}
return $post_data;
}
/**
* Render the metabox.
*
* @param \WP_Post $post Post object.
*/
public function render_data_metabox( $post ) {
$snapshot_content = $this->get_post_content( $post );
echo '<p>';
echo esc_html__( 'UUID:', 'customize-snapshots' ) . ' <code>' . esc_html( $post->post_name ) . '</code><br>';
echo sprintf( '%1$s %2$s %3$s', esc_html__( 'Modified:', 'customize-snapshots' ), esc_html( get_the_modified_date( '' ) ), esc_html( get_the_modified_time( '' ) ) ) . '<br>';
echo '</p>';
$snapshot_theme = get_post_meta( $post->ID, '_snapshot_theme', true );
if ( ! empty( $snapshot_theme ) && get_stylesheet() !== $snapshot_theme ) {
echo '<p>';
echo sprintf( esc_html__( 'This snapshot was made when a different theme was active (%1$s), so currently it cannot be edited.', 'customize-snapshots' ), esc_html( $snapshot_theme ) );
echo '</p>';
} elseif ( 'publish' !== $post->post_status ) {
echo '<p>';
$args = array(
'customize_snapshot_uuid' => $post->post_name,
);
$customize_url = add_query_arg( array_map( 'rawurlencode', $args ), wp_customize_url() );
echo sprintf(
'<a href="%s" class="button button-secondary">%s</a> ',
esc_url( $customize_url ),
esc_html__( 'Edit in Customizer', 'customize-snapshots' )
);
$frontend_view_url = get_permalink( $post->ID );
echo sprintf(
'<a href="%s" class="button button-secondary">%s</a>',
esc_url( $frontend_view_url ),
esc_html__( 'Preview Snapshot', 'customize-snapshots' )
);
echo '</p>';
}
echo '<hr>';
ksort( $snapshot_content );
wp_nonce_field( static::SLUG . '_settings', static::SLUG );
echo '<ul id="snapshot-settings">';
foreach ( $snapshot_content as $setting_id => $setting_params ) {
if ( ! isset( $setting_params['value'] ) && ! isset( $setting_params['publish_error'] ) ) {
continue;
}
$value = isset( $setting_params['value'] ) ? $setting_params['value'] : '';
echo '<li>';
echo '<details open>';
echo '<summary><code>' . esc_html( $setting_id ) . '</code> ';
echo '<a href="#" id="' . esc_attr( $setting_id ) . '" class="cs-toggle-action remove"></a>';
// Show error message when there was a publishing error.
if ( isset( $setting_params['publish_error'] ) ) {
echo '<span class="error-message">';
echo '<b>' . esc_html__( 'Publish error:', 'customize-snapshots' ) . '</b> ';
switch ( $setting_params['publish_error'] ) {
case 'null_value':
esc_html_e( 'Missing value.', 'customize-snapshots' );
break;
case 'unrecognized_setting':
esc_html_e( 'Unrecognized setting.', 'customize-snapshots' );
break;
case 'invalid_value':
esc_html_e( 'Invalid value.', 'customize-snapshots' );
break;
default:
echo '<code>' . esc_html( $setting_params['publish_error'] ) . '</code>';
}
echo '</span>';
}
echo '</summary>';
if ( '' === $value ) {
$preview = '<p><em>' . esc_html__( '(Empty string)', 'customize-snapshots' ) . '</em></p>';
} elseif ( is_string( $value ) || is_numeric( $value ) ) {
$preview = '<p>' . esc_html( $value ) . '</p>';
} elseif ( is_bool( $value ) ) {
$preview = '<p>' . wp_json_encode( $value ) . '</p>';
} else {
$preview = sprintf( '<pre class="pre">%s</pre>', esc_html( Customize_Snapshot_Manager::encode_json( $value ) ) );
}
/**
* Filters the previewed value for a snapshot.
*
* @param string $preview HTML markup.
* @param array $context {
* Context.
*
* @type mixed $value Value being previewed.
* @type string $setting_id Setting args, including value.
* @type array $setting_params Setting args, including value.
* @type \WP_Post $post Snapshot post.
* }
*/
$preview = apply_filters( 'customize_snapshot_value_preview', $preview, compact( 'value', 'setting_id', 'setting_params', 'post' ) );
echo $preview; // WPCS: xss ok.
echo '</details>';
echo '</li>';
}
echo '</ul>';
}
/**
* Find a snapshot post by UUID.
*
* @param string $uuid UUID.
* @return int|null Post ID or null if not found.
*/
public function find_post( $uuid ) {
add_action( 'pre_get_posts', array( $this, '_override_wp_query_is_single' ) );
$query = new \WP_Query( array(
'name' => $uuid,
'posts_per_page' => 1,
'post_type' => static::SLUG,
'post_status' => get_post_stati(),
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'cache_results' => false,
) );
$posts = $query->posts;
remove_action( 'pre_get_posts', array( $this, '_override_wp_query_is_single' ) );
$post = array_shift( $posts );
if ( $post ) {
return $post->ID;
} else {
return null;
}
}
/**
* This is needed to ensure that draft posts can be queried by name.
*
* @todo This can probably be removed, since we're explicitly requesting all statuses.
* @codeCoverageIgnore
*
* @param \WP_Query $query WP Query.
*/
public function _override_wp_query_is_single( $query ) {
$query->is_single = false;
}
/**
* Get the snapshot array out of the post_content.
*
* A post revision for a customize_snapshot may also be supplied.
*
* @param \WP_Post $post A customize_snapshot post or a revision post.
* @return array|null Array of data or null if bad post supplied.
*/
public function get_post_content( \WP_Post $post ) {
if ( static::SLUG !== $post->post_type ) {
$parent_post = null;
if ( 'revision' === $post->post_type ) {
$parent_post = get_post( $post->post_parent );
}
if ( ! $parent_post || static::SLUG !== $parent_post->post_type ) {
return null;
}
}
// Snapshot is stored as JSON in post_content.
$data = json_decode( $post->post_content, true );
if ( json_last_error() || ! is_array( $data ) ) {
$this->snapshot_manager->plugin->trigger_warning( 'JSON parse error, expected array: ' . ( function_exists( 'json_last_error_msg' ) ? json_last_error_msg() : json_last_error() ) );
}
if ( ! is_array( $data ) ) {
return array();
}
return $data;
}
/**
* Persist the data in the snapshot post content.
*
* @param array $args Args.
* @return int|\WP_Error Post ID for snapshot or WP_Error instance.
*/
public function save( array $args ) {
// @todo Add support for $args['post_id'].
if ( empty( $args['uuid'] ) || ! Customize_Snapshot_Manager::is_valid_uuid( $args['uuid'] ) ) {
return new \WP_Error( 'missing_valid_uuid' );
}
$post_arr = array(
'post_name' => $args['uuid'],
'post_title' => $args['uuid'],
'post_type' => static::SLUG,
'meta_input' => array(
'_snapshot_version' => $this->snapshot_manager->plugin->version,
),
);
if ( ! empty( $args['status'] ) ) {
if ( isset( $args['post_date'], $args['edit_date'], $args['post_date_gmt'] ) ) {
$post_arr['post_date'] = $args['post_date'];
$post_arr['edit_date'] = $args['edit_date'];
$post_arr['post_date_gmt'] = $args['post_date_gmt'];
}
if ( ! get_post_status_object( $args['status'] ) ) {
return new \WP_Error( 'bad_status' );
}
$post_arr['post_status'] = $args['status'];
}
$post_id = $this->find_post( $args['uuid'] );
$is_update = ! empty( $post_id );
if ( $post_id ) {
$post_arr['ID'] = $post_id;
}
if ( isset( $args['data'] ) ) {
if ( ! is_array( $args['data'] ) ) {
return new \WP_Error( 'missing_data' );
}
foreach ( $args['data'] as $setting_id => $setting_params ) {
if ( ! is_array( $setting_params ) ) {
return new \WP_Error( 'bad_setting_params' );
}
if ( ! array_key_exists( 'value', $setting_params ) ) {
return new \WP_Error( 'missing_value_param' );
}
}
$post_arr['post_content'] = Customize_Snapshot_Manager::encode_json( $args['data'] );
} elseif ( ! $is_update ) {
$post_arr['post_content'] = Customize_Snapshot_Manager::encode_json( array() );
}
if ( ! empty( $args['theme'] ) ) {
$post_arr['meta_input']['_snapshot_theme'] = $args['theme'];
}
if ( ! empty( $args['author'] ) ) {
$post_arr['post_author'] = $args['author'];
}
if ( ! empty( $args['date_gmt'] ) ) {
$post_arr['post_date_gmt'] = $args['date_gmt'];
}
$this->suspend_kses();
if ( $is_update ) {
$r = wp_update_post( wp_slash( $post_arr ), true );
} else {
$r = wp_insert_post( wp_slash( $post_arr ), true );
}
$this->restore_kses();
return $r;
}
/**
* Add the customize_publish capability to users who can edit_theme_options by default.
*
* @param array $allcaps An array of all the user's capabilities.
* @param array $caps Actual capabilities for meta capability.
* @return array All caps.
*/
public function filter_user_has_cap( $allcaps, $caps ) {
if ( ! empty( $allcaps['edit_theme_options'] ) ) {
$allcaps['customize_publish'] = true;
}
// Grant all customize snapshot caps which weren't explicitly disallowed to users who can customize.
if ( isset( $caps[0] ) && false !== strpos( $caps[0], static::SLUG ) ) {
$post_type_obj = get_post_type_object( static::SLUG );
$primitive_caps = array_flip( (array) $post_type_obj->cap );
unset( $primitive_caps['do_not_allow'] );
foreach ( array_keys( $primitive_caps ) as $granted_cap ) {
$allcaps[ $granted_cap ] = current_user_can( 'customize' );
}
if ( ! current_user_can( 'edit_others_posts' ) ) {
$allcaps[ $post_type_obj->cap->edit_others_posts ] = false;
}
if ( ! current_user_can( 'delete_others_posts' ) ) {
$allcaps[ $post_type_obj->cap->delete_others_posts ] = false;
}
}
return $allcaps;
}
/**
* Display snapshot save error on post list table.
*
* @param array $states Display states.
* @param \WP_Post $post Post object.
*
* @return mixed
*/
public function display_post_states( $states, $post ) {
if ( static::SLUG !== $post->post_type ) {
return $states;
}
$maybe_error = get_post_meta( $post->ID, 'snapshot_error_on_publish', true );
if ( $maybe_error ) {
$states['snapshot_error'] = __( 'Error on publish', 'customize-snapshots' );
}
return $states;
}
/**
* Show an admin notice when publishing fails and the post gets kicked back to pending.
*/
public function show_publish_error_admin_notice() {
if ( ! function_exists( 'get_current_screen' ) ) {
return;
}
$current_screen = get_current_screen();
if ( ! $current_screen || 'customize_snapshot' !== $current_screen->id || 'post' !== $current_screen->base ) {
return;
}
if ( ! isset( $_REQUEST['message'] ) || 8 !== intval( $_REQUEST['message'] ) ) {
return;
}
if ( 'pending' !== get_post_status() ) {
return;
}
?>
<div class="notice notice-error is-dismissible">
<p><?php esc_html_e( 'Failed to publish snapshot due to an error with saving one of its settings. This may be due to a theme or plugin having been changed since the snapshot was created. See below.', 'customize-snapshots' ) ?></p>
</div>
<?php
}
/**
* Disable the revision revert UI for published posts.
*/
public function disable_revision_ui_for_published_posts() {
if ( 'publish' !== get_post_status() || self::SLUG !== get_post_type() ) {
return;
}
?>
<style>
.restore-revision.button {
display: none;
}
</style>
<?php
}
/**
* Hide publishing actions that are no longer relevant when a snapshot is published.
*
* @param \WP_Post $post Current post.
*/
public function hide_disabled_publishing_actions( $post ) {
if ( 'publish' !== $post->post_status || self::SLUG !== $post->post_type ) {
return;
}
?>
<style>
#misc-publishing-actions .misc-pub-post-status,
#misc-publishing-actions .misc-pub-visibility,
#misc-publishing-actions .misc-pub-curtime,
.submitbox #publish {
display: none;
}
</style>
<?php
}
/**
* Filter settings out of post content, if they were removed in the meta box.
*
* In each snapshot's edit page, there are JavaScript-controlled links to remove each setting.
* On clicking a setting, the JS sets a hidden input field with that setting's ID.
* And these settings appear in $_REQUEST as the array 'customize_snapshot_remove_settings.'
* So look for these removed settings in that array, on saving.
* And possibly filter out those settings from the post content.
*
* @param String $content Post content to filter.
* @return String $content Post content, possibly filtered.
*/
public function filter_out_settings_if_removed_in_metabox( $content ) {
global $post;
$key_for_settings = static::SLUG . '_remove_settings';
$post_type_object = get_post_type_object( static::SLUG );
$should_filter_content = (
( 'publish' !== $post->post_status )
&&
current_user_can( $post_type_object->cap->edit_post, $post->ID )
&&
( static::SLUG === $post->post_type )
&&
! empty( $_REQUEST[ $key_for_settings ] )
&&
isset( $_REQUEST[ static::SLUG ] )
&&
wp_verify_nonce( $_REQUEST[ static::SLUG ], static::SLUG . '_settings' )
&&
! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
);
if ( ! $should_filter_content ) {
return $content;
}
$setting_ids_to_unset = $_REQUEST[ $key_for_settings ];
$data = json_decode( $post->post_content );
foreach ( $setting_ids_to_unset as $setting_id ) {
unset( $data->{ $setting_id } );
}
$content = Customize_Snapshot_Manager::encode_json( $data );
return $content;
}
}