Skip to content

Commit 28ffeea

Browse files
Administration: Move inline JS for options screens into options.js
1 parent d7a80fc commit 28ffeea

8 files changed

Lines changed: 139 additions & 123 deletions

File tree

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ module.exports = function(grunt) {
449449
[ WORKING_DIR + 'wp-admin/js/tags.js' ]: [ './src/js/_enqueues/admin/tags.js' ],
450450
[ WORKING_DIR + 'wp-admin/js/site-health.js' ]: [ './src/js/_enqueues/admin/site-health.js' ],
451451
[ WORKING_DIR + 'wp-admin/js/site-icon.js' ]: [ './src/js/_enqueues/admin/site-icon.js' ],
452+
[ WORKING_DIR + 'wp-admin/js/options.js' ]: [ './src/js/_enqueues/admin/options.js' ],
452453
[ WORKING_DIR + 'wp-admin/js/privacy-tools.js' ]: [ './src/js/_enqueues/admin/privacy-tools.js' ],
453454
[ WORKING_DIR + 'wp-admin/js/theme-plugin-editor.js' ]: [ './src/js/_enqueues/wp/theme-plugin-editor.js' ],
454455
[ WORKING_DIR + 'wp-admin/js/theme.js' ]: [ './src/js/_enqueues/wp/theme.js' ],

src/js/_enqueues/admin/options.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Scripts for the options/settings administration screens.
3+
*
4+
* @output wp-admin/js/options.js
5+
*/
6+
7+
/* global ajaxurl */
8+
9+
jQuery( function( $ ) {
10+
var $showAvatars = $( '#show_avatars' ),
11+
$avatarSettings = $( '.avatar-settings' ),
12+
$siteName = $( '#wp-admin-bar-site-name' ).children( 'a' ).first(),
13+
$siteIconPreview = $( '#site-icon-preview-site-title' ),
14+
$languageSelect = $( '#WPLANG' ),
15+
$frontStaticPages = $( '#front-static-pages' ),
16+
homeURL = ( ( window.optionsL10n && window.optionsL10n.homeURL ) || '' ).replace( /^(https?:\/\/)?(www\.)?/, '' );
17+
18+
// options-discussion.php: Toggle avatar settings when 'Show Avatars' is changed.
19+
$showAvatars.on( 'change', function() {
20+
$avatarSettings.toggleClass( 'hide-if-js', ! this.checked );
21+
} );
22+
23+
// options-general.php: Update admin bar site name and site icon preview on title input.
24+
$( '#blogname' ).on( 'input', function() {
25+
var title = $.trim( $( this ).val() ) || homeURL;
26+
27+
// Truncate to 40 characters.
28+
if ( 40 < title.length ) {
29+
title = title.substring( 0, 40 ) + '\u2026';
30+
}
31+
32+
$siteName.text( title );
33+
$siteIconPreview.text( title );
34+
} );
35+
36+
// options-general.php: Date and time format pickers.
37+
$( 'input[name="date_format"]' ).on( 'click', function() {
38+
if ( 'date_format_custom_radio' !== $( this ).attr( 'id' ) ) {
39+
$( 'input[name="date_format_custom"]' ).val( $( this ).val() ).closest( 'fieldset' ).find( '.example' ).text( $( this ).parent( 'label' ).children( '.format-i18n' ).text() );
40+
}
41+
} );
42+
43+
$( 'input[name="date_format_custom"]' ).on( 'click input', function() {
44+
$( '#date_format_custom_radio' ).prop( 'checked', true );
45+
} );
46+
47+
$( 'input[name="time_format"]' ).on( 'click', function() {
48+
if ( 'time_format_custom_radio' !== $( this ).attr( 'id' ) ) {
49+
$( 'input[name="time_format_custom"]' ).val( $( this ).val() ).closest( 'fieldset' ).find( '.example' ).text( $( this ).parent( 'label' ).children( '.format-i18n' ).text() );
50+
}
51+
} );
52+
53+
$( 'input[name="time_format_custom"]' ).on( 'click input', function() {
54+
$( '#time_format_custom_radio' ).prop( 'checked', true );
55+
} );
56+
57+
$( 'input[name="date_format_custom"], input[name="time_format_custom"]' ).on( 'input', function() {
58+
var format = $( this ),
59+
fieldset = format.closest( 'fieldset' ),
60+
example = fieldset.find( '.example' ),
61+
spinner = fieldset.find( '.spinner' );
62+
63+
// Debounce the event callback while users are typing.
64+
clearTimeout( $.data( this, 'timer' ) );
65+
$( this ).data( 'timer', setTimeout( function() {
66+
// If custom date is not empty.
67+
if ( format.val() ) {
68+
spinner.addClass( 'is-active' );
69+
70+
$.post( ajaxurl, {
71+
action: 'date_format_custom' === format.attr( 'name' ) ? 'date_format' : 'time_format',
72+
date: format.val()
73+
}, function( d ) { spinner.removeClass( 'is-active' ); example.text( d ); } );
74+
}
75+
}, 500 ) );
76+
} );
77+
78+
// options-general.php: Language install spinner.
79+
$( 'form' ).on( 'submit', function() {
80+
/*
81+
* Don't show a spinner for English and installed languages,
82+
* as there is nothing to download.
83+
*/
84+
if ( $languageSelect.length && ! $languageSelect.find( 'option:selected' ).data( 'installed' ) ) {
85+
$( '#submit', this ).after( '<span class="spinner language-install-spinner is-active" />' );
86+
}
87+
} );
88+
89+
// options-reading.php: Enable/disable page selects based on 'Your homepage displays' radio.
90+
if ( $frontStaticPages.length ) {
91+
var $staticPage = $frontStaticPages.find( 'input:radio[value="page"]' ),
92+
$selects = $frontStaticPages.find( 'select' ),
93+
checkDisabled = function() {
94+
$selects.prop( 'disabled', ! $staticPage.prop( 'checked' ) );
95+
};
96+
checkDisabled();
97+
$frontStaticPages.find( 'input:radio' ).on( 'change', checkDisabled );
98+
}
99+
} );

src/wp-admin/includes/deprecated.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,3 +1589,36 @@ function image_attachment_fields_to_save( $post, $attachment ) {
15891589

15901590
return $post;
15911591
}
1592+
1593+
/**
1594+
* Output JavaScript to toggle display of additional settings if avatars are disabled.
1595+
*
1596+
* @since 4.2.0
1597+
* @deprecated 7.1.0 Inline JavaScript has been moved to wp-admin/js/options.js,
1598+
* enqueued via {@see wp_enqueue_script()}.
1599+
*/
1600+
function options_discussion_add_js() {
1601+
_deprecated_function( __FUNCTION__, '7.1.0' );
1602+
}
1603+
1604+
/**
1605+
* Display JavaScript on the General Settings screen.
1606+
*
1607+
* @since 3.5.0
1608+
* @deprecated 7.1.0 Inline JavaScript has been moved to wp-admin/js/options.js,
1609+
* enqueued via {@see wp_enqueue_script()}.
1610+
*/
1611+
function options_general_add_js() {
1612+
_deprecated_function( __FUNCTION__, '7.1.0' );
1613+
}
1614+
1615+
/**
1616+
* Display JavaScript on the Reading Settings screen.
1617+
*
1618+
* @since 3.5.0
1619+
* @deprecated 7.1.0 Inline JavaScript has been moved to wp-admin/js/options.js,
1620+
* enqueued via {@see wp_enqueue_script()}.
1621+
*/
1622+
function options_reading_add_js() {
1623+
_deprecated_function( __FUNCTION__, '7.1.0' );
1624+
}

src/wp-admin/includes/options.php

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -7,126 +7,6 @@
77
* @since 4.4.0
88
*/
99

10-
/**
11-
* Output JavaScript to toggle display of additional settings if avatars are disabled.
12-
*
13-
* @since 4.2.0
14-
*/
15-
function options_discussion_add_js() {
16-
?>
17-
<script>
18-
(function($){
19-
var parent = $( '#show_avatars' ),
20-
children = $( '.avatar-settings' );
21-
parent.on( 'change', function(){
22-
children.toggleClass( 'hide-if-js', ! this.checked );
23-
});
24-
})(jQuery);
25-
</script>
26-
<?php
27-
}
28-
29-
/**
30-
* Display JavaScript on the page.
31-
*
32-
* @since 3.5.0
33-
*/
34-
function options_general_add_js() {
35-
?>
36-
<script>
37-
jQuery( function($) {
38-
var $siteName = $( '#wp-admin-bar-site-name' ).children( 'a' ).first(),
39-
$siteIconPreview = $('#site-icon-preview-site-title'),
40-
homeURL = ( <?php echo wp_json_encode( get_home_url(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> || '' ).replace( /^(https?:\/\/)?(www\.)?/, '' );
41-
42-
$( '#blogname' ).on( 'input', function() {
43-
var title = $.trim( $( this ).val() ) || homeURL;
44-
45-
// Truncate to 40 characters.
46-
if ( 40 < title.length ) {
47-
title = title.substring( 0, 40 ) + '\u2026';
48-
}
49-
50-
$siteName.text( title );
51-
$siteIconPreview.text( title );
52-
});
53-
54-
$( 'input[name="date_format"]' ).on( 'click', function() {
55-
if ( 'date_format_custom_radio' !== $(this).attr( 'id' ) )
56-
$( 'input[name="date_format_custom"]' ).val( $( this ).val() ).closest( 'fieldset' ).find( '.example' ).text( $( this ).parent( 'label' ).children( '.format-i18n' ).text() );
57-
});
58-
59-
$( 'input[name="date_format_custom"]' ).on( 'click input', function() {
60-
$( '#date_format_custom_radio' ).prop( 'checked', true );
61-
});
62-
63-
$( 'input[name="time_format"]' ).on( 'click', function() {
64-
if ( 'time_format_custom_radio' !== $(this).attr( 'id' ) )
65-
$( 'input[name="time_format_custom"]' ).val( $( this ).val() ).closest( 'fieldset' ).find( '.example' ).text( $( this ).parent( 'label' ).children( '.format-i18n' ).text() );
66-
});
67-
68-
$( 'input[name="time_format_custom"]' ).on( 'click input', function() {
69-
$( '#time_format_custom_radio' ).prop( 'checked', true );
70-
});
71-
72-
$( 'input[name="date_format_custom"], input[name="time_format_custom"]' ).on( 'input', function() {
73-
var format = $( this ),
74-
fieldset = format.closest( 'fieldset' ),
75-
example = fieldset.find( '.example' ),
76-
spinner = fieldset.find( '.spinner' );
77-
78-
// Debounce the event callback while users are typing.
79-
clearTimeout( $.data( this, 'timer' ) );
80-
$( this ).data( 'timer', setTimeout( function() {
81-
// If custom date is not empty.
82-
if ( format.val() ) {
83-
spinner.addClass( 'is-active' );
84-
85-
$.post( ajaxurl, {
86-
action: 'date_format_custom' === format.attr( 'name' ) ? 'date_format' : 'time_format',
87-
date : format.val()
88-
}, function( d ) { spinner.removeClass( 'is-active' ); example.text( d ); } );
89-
}
90-
}, 500 ) );
91-
} );
92-
93-
var languageSelect = $( '#WPLANG' );
94-
$( 'form' ).on( 'submit', function() {
95-
/*
96-
* Don't show a spinner for English and installed languages,
97-
* as there is nothing to download.
98-
*/
99-
if ( ! languageSelect.find( 'option:selected' ).data( 'installed' ) ) {
100-
$( '#submit', this ).after( '<span class="spinner language-install-spinner is-active" />' );
101-
}
102-
});
103-
} );
104-
</script>
105-
<?php
106-
}
107-
108-
/**
109-
* Display JavaScript on the page.
110-
*
111-
* @since 3.5.0
112-
*/
113-
function options_reading_add_js() {
114-
?>
115-
<script>
116-
jQuery( function($) {
117-
var section = $('#front-static-pages'),
118-
staticPage = section.find('input:radio[value="page"]'),
119-
selects = section.find('select'),
120-
check_disabled = function(){
121-
selects.prop( 'disabled', ! staticPage.prop('checked') );
122-
};
123-
check_disabled();
124-
section.find( 'input:radio' ).on( 'change', check_disabled );
125-
} );
126-
</script>
127-
<?php
128-
}
129-
13010
/**
13111
* Render the site charset setting.
13212
*

src/wp-admin/options-discussion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
$title = __( 'Discussion Settings' );
1717
$parent_file = 'options-general.php';
1818

19-
add_action( 'admin_print_footer_scripts', 'options_discussion_add_js' );
19+
wp_enqueue_script( 'options' );
2020

2121
get_current_screen()->add_help_tab(
2222
array(

src/wp-admin/options-general.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
/* translators: Date and time format for exact current time, mainly about timezones, see https://www.php.net/manual/datetime.format.php */
2323
$timezone_format = _x( 'Y-m-d H:i:s', 'timezone date format' );
2424

25-
add_action( 'admin_head', 'options_general_add_js' );
25+
wp_enqueue_script( 'options' );
26+
wp_localize_script( 'options', 'optionsL10n', array( 'homeURL' => get_home_url() ) );
2627

2728
$options_help = '<p>' . __( 'The fields on this screen determine some of the basics of your site setup.' ) . '</p>' .
2829
'<p>' . __( 'Most themes show the site title at the top of every page, in the title bar of the browser, and as the identifying name for syndicated feeds. Many themes also show the tagline.' ) . '</p>';

src/wp-admin/options-reading.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
$title = __( 'Reading Settings' );
1818
$parent_file = 'options-general.php';
1919

20-
add_action( 'admin_head', 'options_reading_add_js' );
20+
wp_enqueue_script( 'options' );
2121

2222
get_current_screen()->add_help_tab(
2323
array(

src/wp-includes/script-loader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,8 @@ function wp_default_scripts( $scripts ) {
14851485
$scripts->add( 'site-health', "/wp-admin/js/site-health$suffix.js", array( 'clipboard', 'jquery', 'wp-util', 'wp-a11y', 'wp-api-request', 'wp-url', 'wp-i18n', 'wp-hooks' ), false, 1 );
14861486
$scripts->set_translations( 'site-health' );
14871487

1488+
$scripts->add( 'options', "/wp-admin/js/options$suffix.js", array( 'jquery' ), false, 1 );
1489+
14881490
$scripts->add( 'privacy-tools', "/wp-admin/js/privacy-tools$suffix.js", array( 'jquery', 'wp-a11y' ), false, 1 );
14891491
$scripts->set_translations( 'privacy-tools' );
14901492

0 commit comments

Comments
 (0)