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

Commit 35d5bca

Browse files
author
Ryan Kienstra
committed
Issue #17 : Change logic for toggling link text.
Using Weston's snippet, output link title in PHP, with localization. And attach localized text 'Restore settting' to the link's data-text-restore attribute. On clicking the link, call method changeLinkText. This toggles the title to the other value stored in the data attribute. And stores the old title in the data attribute, for the next time it's clicked.
1 parent 83ad0e9 commit 35d5bca

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

js/customize-snapshots-admin.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
'use strict';
33

44
$( function() {
5-
var component, linkSelector, linkText, linkActions, dataSlug, inputName;
5+
var component, $linkToRemoveOrRestore, linkActions, dataSlug, inputName;
66

77
component = {};
8-
linkSelector = '.snapshot-toggle-setting-removal';
9-
linkText = [ 'Remove setting', 'Restore setting' ];
8+
$linkToRemoveOrRestore = $( '.snapshot-toggle-setting-removal' );
109
linkActions = [ 'remove', 'restore' ];
1110
dataSlug = 'cs-action';
1211
inputName = 'customize_snapshot_remove_settings[]';
1312

1413
component.initializeLink = function() {
15-
$( linkSelector ).text( linkText[ 0 ] )
16-
.data( dataSlug, linkActions[ 0 ] );
14+
$linkToRemoveOrRestore.data( dataSlug, linkActions[ 0 ] );
1715
};
1816

1917
component.initializeLink();
@@ -35,9 +33,9 @@
3533
$settingDisplay = component.getSettingDisplay( $link );
3634
settingId = component.getSettingId( $link );
3735

38-
$link.text( linkText[ 1 ] )
39-
.data( dataSlug, linkActions[ 1 ] )
36+
$link.data( dataSlug, linkActions[ 1 ] )
4037
.after( component.constructHiddenInputWithValue( settingId ) );
38+
component.changeLinkText( $link );
4139
$settingDisplay.removeAttr( 'open' )
4240
.addClass( 'snapshot-setting-removed' );
4341
};
@@ -58,13 +56,22 @@
5856
.val( settingId );
5957
};
6058

59+
component.changeLinkText = function( $link ) {
60+
var oldLinkText, newLinkText;
61+
oldLinkText = $link.text();
62+
newLinkText = $link.data( 'text-restore' );
63+
64+
$link.data( 'text-restore', oldLinkText )
65+
.text( newLinkText );
66+
};
67+
6168
component.showSettingAndChangeLinkText = function( $link ) {
6269
var $settingDisplay, settingId;
6370
$settingDisplay = component.getSettingDisplay( $link );
6471
settingId = component.getSettingId( $link );
6572

66-
$link.text( linkText[ 0 ] )
67-
.data( dataSlug, linkActions[ 0 ] );
73+
$link.data( dataSlug, linkActions[ 0 ] );
74+
component.changeLinkText( $link );
6875
component.removeHiddenInputWithValue( settingId );
6976
$settingDisplay.removeClass( 'snapshot-setting-removed' );
7077
};
@@ -73,7 +80,7 @@
7380
$( 'input[name="' + inputName + '"][value="' + settingId + '"]' ).remove();
7481
};
7582

76-
$( linkSelector ).on( 'click', function( event ) {
83+
$linkToRemoveOrRestore.on( 'click', function( event ) {
7784
var $clickedLink = $( this );
7885

7986
event.preventDefault();

php/class-post-type.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public function render_data_metabox( $post ) {
362362
echo '<li>';
363363
echo '<details open>';
364364
echo '<summary><code>' . esc_html( $setting_id ) . '</code> ';
365-
echo '<a href="#" id="' . esc_attr( $setting_id ) . '" class="snapshot-toggle-setting-removal remove"></a>';
365+
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>';
366366

367367
// Show error message when there was a publishing error.
368368
if ( isset( $setting_params['publish_error'] ) ) {

0 commit comments

Comments
 (0)