-
Notifications
You must be signed in to change notification settings - Fork 16
Issue #17 : Enable deleting settings in snapshot wp-admin/post page. #84
Changes from 12 commits
73c5b28
3a60aaa
6d4b74b
48a70cc
e925589
625ff99
83ad0e9
35d5bca
92f14c0
f76b4a6
d1487c8
68fa5ae
e5accee
8fc727c
8f29076
88a83de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
details.snapshot-setting-removed summary { | ||
text-decoration: line-through; | ||
} | ||
details:not(.snapshot-setting-removed) .snapshot-toggle-setting-removal { | ||
color: #a00 | ||
} | ||
details:not(.snapshot-setting-removed) .snapshot-toggle-setting-removal:hover { | ||
color: #f00 | ||
} | ||
details .snapshot-toggle-setting-removal { | ||
float: right; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
( function( $ ) { | ||
'use strict'; | ||
|
||
$( function() { | ||
var component, $linkToRemoveOrRestore, linkActions, dataSlug, inputName; | ||
|
||
component = {}; | ||
$linkToRemoveOrRestore = $( '.snapshot-toggle-setting-removal' ); | ||
linkActions = [ 'remove', 'restore' ]; | ||
dataSlug = 'cs-action'; | ||
inputName = 'customize_snapshot_remove_settings[]'; | ||
|
||
component.initializeLink = function() { | ||
$linkToRemoveOrRestore.data( dataSlug, linkActions[ 0 ] ); | ||
}; | ||
|
||
component.initializeLink(); | ||
|
||
component.isLinkSetToRemoveSetting = function( $link ) { | ||
return linkActions[ 0 ] === component.getClickedLinkAction( $link ); | ||
}; | ||
|
||
component.isLinkSetToRestoreSetting = function( $link ) { | ||
return linkActions[ 1 ] === component.getClickedLinkAction( $link ); | ||
}; | ||
|
||
component.getClickedLinkAction = function( $link ) { | ||
return $link.data( dataSlug ); | ||
}; | ||
|
||
component.hideSettingAndChangeLinkText = function( $link ) { | ||
var $settingDisplay, settingId; | ||
$settingDisplay = component.getSettingDisplay( $link ); | ||
settingId = component.getSettingId( $link ); | ||
|
||
$link.data( dataSlug, linkActions[ 1 ] ) | ||
.after( component.constructHiddenInputWithValue( settingId ) ); | ||
component.changeLinkText( $link ); | ||
$settingDisplay.removeAttr( 'open' ) | ||
.addClass( 'snapshot-setting-removed' ); | ||
}; | ||
|
||
component.getSettingDisplay = function( $link ) { | ||
return $link.parents( 'details' ); | ||
}; | ||
|
||
component.getSettingId = function( $link ) { | ||
return $link.attr( 'id' ); | ||
}; | ||
|
||
component.constructHiddenInputWithValue = function( settingId ) { | ||
return $( '<input>' ).attr( { | ||
'name': inputName, | ||
'type': 'hidden' | ||
} ) | ||
.val( settingId ); | ||
}; | ||
|
||
component.changeLinkText = function( $link ) { | ||
var oldLinkText, newLinkText; | ||
oldLinkText = $link.text(); | ||
newLinkText = $link.data( 'text-restore' ); | ||
|
||
$link.data( 'text-restore', oldLinkText ) | ||
.text( newLinkText ); | ||
}; | ||
|
||
component.showSettingAndChangeLinkText = function( $link ) { | ||
var $settingDisplay, settingId; | ||
$settingDisplay = component.getSettingDisplay( $link ); | ||
settingId = component.getSettingId( $link ); | ||
|
||
$link.data( dataSlug, linkActions[ 0 ] ); | ||
component.changeLinkText( $link ); | ||
component.removeHiddenInputWithValue( settingId ); | ||
$settingDisplay.removeClass( 'snapshot-setting-removed' ); | ||
}; | ||
|
||
component.removeHiddenInputWithValue = function( settingId ) { | ||
$( 'input[name="' + inputName + '"][value="' + settingId + '"]' ).remove(); | ||
}; | ||
|
||
$linkToRemoveOrRestore.on( 'click', function( event ) { | ||
var $clickedLink = $( this ); | ||
|
||
event.preventDefault(); | ||
|
||
if ( component.isLinkSetToRemoveSetting( $clickedLink ) ) { | ||
component.hideSettingAndChangeLinkText( $clickedLink ); | ||
} else if ( component.isLinkSetToRestoreSetting( $clickedLink ) ) { | ||
component.showSettingAndChangeLinkText( $clickedLink ); | ||
} | ||
} ); | ||
|
||
} ); | ||
} )( jQuery ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,6 +114,7 @@ public function register() { | |
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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea to use |
||
add_action( 'admin_print_scripts-revision.php', array( $this, 'disable_revision_ui_for_published_posts' ) ); | ||
} | ||
|
||
|
@@ -351,6 +352,7 @@ public function render_data_metabox( $post ) { | |
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'] ) ) { | ||
|
@@ -360,6 +362,7 @@ public function render_data_metabox( $post ) { | |
echo '<li>'; | ||
echo '<details open>'; | ||
echo '<summary><code>' . esc_html( $setting_id ) . '</code> '; | ||
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>'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use translation function |
||
|
||
// Show error message when there was a publishing error. | ||
if ( isset( $setting_params['publish_error'] ) ) { | ||
|
@@ -675,4 +678,55 @@ public function hide_disabled_publishing_actions( $post ) { | |
</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 = ( | ||
isset( $post->post_status ) | ||
&& | ||
( '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 ] ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be checked to see if it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good idea. This commit adds an |
||
&& | ||
is_array( $_REQUEST[ $key_for_settings ] ) | ||
&& | ||
isset( $_REQUEST[ static::SLUG ] ) | ||
&& | ||
wp_verify_nonce( $_REQUEST[ static::SLUG ], static::SLUG . '_settings' ) | ||
&& | ||
! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be using the new ! ( function_exists( 'wp_doing_ajax' ) ? wp_doing_ajax() : defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) This will likely yield benefits for doing unit testing without having to be in a separate Ajax unit test, since the return value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. 😊 I misread the constant here. Nevermind 😄 |
||
); | ||
|
||
if ( ! $should_filter_content ) { | ||
return $content; | ||
} | ||
|
||
$setting_ids_to_unset = $_REQUEST[ $key_for_settings ]; | ||
$data = json_decode( wp_unslash( $content ), true ); | ||
foreach ( $setting_ids_to_unset as $setting_id ) { | ||
unset( $data[ $setting_id ] ); | ||
} | ||
$content = Customize_Snapshot_Manager::encode_json( $data ); | ||
|
||
return $content; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be eliminated in favor of putting the translated text strings inline in the rendered PHP.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this commit outputs the translated strings with PHP.