@@ -114,6 +114,7 @@ public function register() {
114
114
add_filter ( 'display_post_states ' , array ( $ this , 'display_post_states ' ), 10 , 2 );
115
115
add_action ( 'admin_notices ' , array ( $ this , 'show_publish_error_admin_notice ' ) );
116
116
add_action ( 'post_submitbox_minor_actions ' , array ( $ this , 'hide_disabled_publishing_actions ' ) );
117
+ add_filter ( 'content_save_pre ' , array ( $ this , 'filter_out_settings_if_removed_in_metabox ' ), 10 );
117
118
add_action ( 'admin_print_scripts-revision.php ' , array ( $ this , 'disable_revision_ui_for_published_posts ' ) );
118
119
119
120
// Version check for bulk action.
@@ -361,6 +362,7 @@ public function render_data_metabox( $post ) {
361
362
echo '<hr> ' ;
362
363
363
364
ksort ( $ snapshot_content );
365
+ wp_nonce_field ( static ::SLUG . '_settings ' , static ::SLUG );
364
366
echo '<ul id="snapshot-settings"> ' ;
365
367
foreach ( $ snapshot_content as $ setting_id => $ setting_params ) {
366
368
if ( ! isset ( $ setting_params ['value ' ] ) && ! isset ( $ setting_params ['publish_error ' ] ) ) {
@@ -370,6 +372,7 @@ public function render_data_metabox( $post ) {
370
372
echo '<li> ' ;
371
373
echo '<details open> ' ;
372
374
echo '<summary><code> ' . esc_html ( $ setting_id ) . '</code> ' ;
375
+ echo '<a href="#" id=" ' . esc_attr ( $ setting_id ) . '" data-text-restore=" ' . esc_attr__ ( 'Restore setting ' , 'customize-snapshots ' ) . '" class="snapshot-toggle-setting-removal remove"> ' . esc_html__ ( 'Remove setting ' , 'customize-snapshots ' ) . '</a> ' ;
373
376
374
377
// Show error message when there was a publishing error.
375
378
if ( isset ( $ setting_params ['publish_error ' ] ) ) {
@@ -822,4 +825,55 @@ public function admin_show_merge_error() {
822
825
}
823
826
printf ( '<div class="notice notice-error is-dismissible"><p>%s</p></div> ' , esc_html ( $ error [ $ error_code ] ) );
824
827
}
828
+
829
+ /**
830
+ * Filter settings out of post content, if they were removed in the meta box.
831
+ *
832
+ * In each snapshot's edit page, there are JavaScript-controlled links to remove each setting.
833
+ * On clicking a setting, the JS sets a hidden input field with that setting's ID.
834
+ * And these settings appear in $_REQUEST as the array 'customize_snapshot_remove_settings.'
835
+ * So look for these removed settings in that array, on saving.
836
+ * And possibly filter out those settings from the post content.
837
+ *
838
+ * @param String $content Post content to filter.
839
+ * @return String $content Post content, possibly filtered.
840
+ */
841
+ public function filter_out_settings_if_removed_in_metabox ( $ content ) {
842
+ global $ post ;
843
+ $ key_for_settings = static ::SLUG . '_remove_settings ' ;
844
+ $ post_type_object = get_post_type_object ( static ::SLUG );
845
+
846
+ $ should_filter_content = (
847
+ isset ( $ post ->post_status )
848
+ &&
849
+ ( 'publish ' !== $ post ->post_status )
850
+ &&
851
+ current_user_can ( $ post_type_object ->cap ->edit_post , $ post ->ID )
852
+ &&
853
+ ( static ::SLUG === $ post ->post_type )
854
+ &&
855
+ ! empty ( $ _REQUEST [ $ key_for_settings ] )
856
+ &&
857
+ is_array ( $ _REQUEST [ $ key_for_settings ] )
858
+ &&
859
+ isset ( $ _REQUEST [ static ::SLUG ] )
860
+ &&
861
+ wp_verify_nonce ( $ _REQUEST [ static ::SLUG ], static ::SLUG . '_settings ' )
862
+ &&
863
+ ! ( defined ( 'DOING_AUTOSAVE ' ) && DOING_AUTOSAVE )
864
+ );
865
+
866
+ if ( ! $ should_filter_content ) {
867
+ return $ content ;
868
+ }
869
+
870
+ $ setting_ids_to_unset = $ _REQUEST [ $ key_for_settings ];
871
+ $ data = json_decode ( wp_unslash ( $ content ), true );
872
+ foreach ( $ setting_ids_to_unset as $ setting_id ) {
873
+ unset( $ data [ $ setting_id ] );
874
+ }
875
+ $ content = Customize_Snapshot_Manager::encode_json ( $ data );
876
+
877
+ return $ content ;
878
+ }
825
879
}
0 commit comments