Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit 73c5b28

Browse files
author
Ryan Kienstra
committed
Issue #17 : Enable deleting settings in snapshot wp-admin/post page.
Create link 'Remove setting,' which toggles to 'Restore setting' on click. It also sets a hidden text field for each setting to be removed. On saving, these appear in [ 'customize_snapshot_remove_settings' ]. Filter post content on 'content_save_pre,' when this is saved. If has settings to be removed, filter them out. Also, add simple styling of this 'Remove setting' link. It appears red when setto 'remove,' and blue when set to 'restore.'
1 parent 856d135 commit 73c5b28

5 files changed

+159
-0
lines changed

css/customize-snapshots-admin.css

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
details.cs-removed summary::-webkit-details-marker {
2+
display:none;
3+
}
4+
details:not(.cs-removed) .cs-toggle-action {
5+
color: #a00
6+
}
7+
details:not(.cs-removed) .cs-toggle-action:hover {
8+
color: #f00
9+
}
10+
details .cs-toggle-action {
11+
float: right;
12+
}

js/customize-snapshots-admin.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
( function( $ ) {
2+
3+
$( function() {
4+
var $link, linkText, linkActions, dataSlug, initializeLink;
5+
6+
$link = $( '.cs-toggle-action' );
7+
linkText = [ 'Remove setting', 'Restore setting' ];
8+
linkActions = [ 'remove', 'restore' ];
9+
dataSlug = 'cs-action';
10+
11+
initializeLink = function() {
12+
$link.text( linkText[ 0 ] )
13+
.data( dataSlug, linkActions[ 0 ] );
14+
};
15+
16+
initializeLink();
17+
18+
$link.on( 'click', function( event ) {
19+
var $clickedLink, $settingDisplay, clickedLinkAction, settingId;
20+
21+
event.preventDefault();
22+
23+
$clickedLink = $( this );
24+
$settingDisplay = $( this ).parents( 'details' );
25+
clickedLinkAction = $( this ).data( dataSlug );
26+
settingId = $( this ).attr( 'id' );
27+
28+
this.isLinkSetToRemoveSetting = function() {
29+
return ( linkActions[ 0 ] === clickedLinkAction );
30+
};
31+
32+
this.hideSettingAndChangeLinkText = function() {
33+
$clickedLink.text( linkText[ 1 ] )
34+
.data( dataSlug, linkActions[ 1 ] )
35+
.after( this.constructHiddenInputWithValue( settingId ) );
36+
$settingDisplay.removeAttr( 'open' )
37+
.addClass( 'cs-removed' );
38+
};
39+
40+
this.constructHiddenInputWithValue = function( settingId ) {
41+
return $( '<input>' ).attr( {
42+
'name': 'customize_snapshot_remove_settings[]',
43+
'type': 'hidden'
44+
})
45+
.val( settingId );
46+
};
47+
48+
this.isLinkSetToRestoreSetting = function() {
49+
return ( linkActions[ 1 ] === clickedLinkAction );
50+
};
51+
52+
this.showSettingAndChangeLinkText = function() {
53+
$clickedLink.text( linkText[ 0 ] )
54+
.data( dataSlug, linkActions[ 0 ] );
55+
this.removeHiddenInputWithValue( settingId );
56+
$settingDisplay.removeClass( 'cs-removed' );
57+
};
58+
59+
this.removeHiddenInputWithValue = function( settingId ) {
60+
$( 'input[value="' + settingId + '"]' ).remove();
61+
};
62+
63+
if ( this.isLinkSetToRemoveSetting() ) {
64+
this.hideSettingAndChangeLinkText();
65+
} else if ( this.isLinkSetToRestoreSetting() ) {
66+
this.showSettingAndChangeLinkText();
67+
}
68+
69+
} );
70+
71+
} );
72+
}( jQuery ) );

php/class-customize-snapshot-manager.php

+16
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ function init() {
102102
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_controls_scripts' ) );
103103
add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) );
104104
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
105+
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
105106

106107
add_action( 'customize_controls_init', array( $this, 'add_snapshot_uuid_to_return_url' ) );
107108
add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_templates' ) );
@@ -744,6 +745,21 @@ public function enqueue_frontend_scripts() {
744745
);
745746
}
746747

748+
/**
749+
* Enqueue admin scripts.
750+
*
751+
* These files control the behavior and styling of links to remove settings.
752+
* Published snapshots can't be edited, so these files are not needed on those pages.
753+
*/
754+
public function enqueue_admin_scripts() {
755+
global $post;
756+
$handle = 'customize-snapshots-admin';
757+
if ( isset( $post->post_type ) && ( Post_Type::SLUG === $post->post_type ) && ( 'publish' !== $post->post_status ) ) {
758+
wp_enqueue_script( $handle );
759+
wp_enqueue_style( $handle );
760+
}
761+
}
762+
747763
/**
748764
* Include the snapshot nonce in the Customizer nonces.
749765
*

php/class-plugin.php

+9
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public function register_scripts( \WP_Scripts $wp_scripts ) {
8282
$src = $this->dir_url . 'js/customize-snapshots-frontend' . $min . '.js';
8383
$deps = array( 'jquery', 'underscore' );
8484
$wp_scripts->add( $handle, $src, $deps );
85+
86+
$handle = 'customize-snapshots-admin';
87+
$src = $this->dir_url . 'js/customize-snapshots-admin' . $min . '.js';
88+
$deps = array( 'jquery' );
89+
$wp_scripts->add( $handle, $src, $deps );
8590
}
8691

8792
/**
@@ -103,5 +108,9 @@ public function register_styles( \WP_Styles $wp_styles ) {
103108
$src = $this->dir_url . 'css/customize-snapshots-preview' . $min . '.css';
104109
$deps = array( 'customize-preview' );
105110
$wp_styles->add( $handle, $src, $deps );
111+
112+
$handle = 'customize-snapshots-admin';
113+
$src = $this->dir_url . 'css/customize-snapshots-admin' . $min . '.css';
114+
$wp_styles->add( $handle, $src );
106115
}
107116
}

php/class-post-type.php

+50
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public function register() {
114114
add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 );
115115
add_action( 'admin_notices', array( $this, 'show_publish_error_admin_notice' ) );
116116
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 );
117118
add_action( 'admin_print_scripts-revision.php', array( $this, 'disable_revision_ui_for_published_posts' ) );
118119
}
119120

@@ -351,6 +352,7 @@ public function render_data_metabox( $post ) {
351352
echo '<hr>';
352353

353354
ksort( $snapshot_content );
355+
wp_nonce_field( static::SLUG . '_settings', static::SLUG );
354356
echo '<ul id="snapshot-settings">';
355357
foreach ( $snapshot_content as $setting_id => $setting_params ) {
356358
if ( ! isset( $setting_params['value'] ) && ! isset( $setting_params['publish_error'] ) ) {
@@ -360,6 +362,7 @@ public function render_data_metabox( $post ) {
360362
echo '<li>';
361363
echo '<details open>';
362364
echo '<summary><code>' . esc_html( $setting_id ) . '</code> ';
365+
echo '<a href="#" id="' . esc_attr( $setting_id ) . '" class="cs-toggle-action remove"></a>';
363366

364367
// Show error message when there was a publishing error.
365368
if ( isset( $setting_params['publish_error'] ) ) {
@@ -675,4 +678,51 @@ public function hide_disabled_publishing_actions( $post ) {
675678
</style>
676679
<?php
677680
}
681+
682+
/**
683+
* Filter settings out of post content, if they were removed in the meta box.
684+
*
685+
* In each snapshot's edit page, there are JavaScript-controlled links to remove each setting.
686+
* On clicking a setting, the JS sets a hidden input field with that setting's ID.
687+
* And these settings appear in $_REQUEST as the array 'customize_snapshot_remove_settings.'
688+
* So look for these removed settings in that array, on saving.
689+
* And possibly filter out those settings from the post content.
690+
*
691+
* @param String $content Post content to filter.
692+
* @return String $content Post content, possibly filtered.
693+
*/
694+
public function filter_out_settings_if_removed_in_metabox( $content ) {
695+
global $post;
696+
$key_for_settings = static::SLUG . '_remove_settings';
697+
$post_type_object = get_post_type_object( static::SLUG );
698+
699+
$should_filter_content = (
700+
( 'publish' !== $post->post_status )
701+
&&
702+
current_user_can( $post_type_object->cap->edit_post, $post->ID )
703+
&&
704+
( static::SLUG === $post->post_type )
705+
&&
706+
! empty( $_REQUEST[ $key_for_settings ] )
707+
&&
708+
isset( $_REQUEST[ static::SLUG ] )
709+
&&
710+
wp_verify_nonce( $_REQUEST[ static::SLUG ], static::SLUG . '_settings' )
711+
&&
712+
! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
713+
);
714+
715+
if ( ! $should_filter_content ) {
716+
return $content;
717+
}
718+
719+
$setting_ids_to_unset = $_REQUEST[ $key_for_settings ];
720+
$data = json_decode( $post->post_content );
721+
foreach ( $setting_ids_to_unset as $setting_id ) {
722+
unset( $data->{ $setting_id } );
723+
}
724+
$content = Customize_Snapshot_Manager::encode_json( $data );
725+
726+
return $content;
727+
}
678728
}

0 commit comments

Comments
 (0)