Skip to content

Commit 83f93d2

Browse files
authored
Merge pull request #1902 from Automattic/change/datepicker-enqueue-strategy
Standardize datepicker enqueueing logic
2 parents 36363a6 + c4396a5 commit 83f93d2

3 files changed

Lines changed: 23 additions & 31 deletions

File tree

includes/admin/class-wp-job-manager-admin.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public function admin_enqueue_scripts() {
9393
wp_enqueue_style( 'jquery-ui' );
9494
wp_enqueue_style( 'select2' );
9595
wp_enqueue_style( 'job_manager_admin_css', JOB_MANAGER_PLUGIN_URL . '/assets/css/admin.css', [], JOB_MANAGER_VERSION );
96+
wp_enqueue_script( 'wp-job-manager-datepicker' );
9697
wp_register_script( 'jquery-tiptip', JOB_MANAGER_PLUGIN_URL . '/assets/js/jquery-tiptip/jquery.tipTip.min.js', [ 'jquery' ], JOB_MANAGER_VERSION, true );
97-
wp_enqueue_script( 'job_manager_datepicker_js', JOB_MANAGER_PLUGIN_URL . '/assets/js/datepicker.min.js', [ 'jquery', 'jquery-ui-datepicker' ], JOB_MANAGER_VERSION, true );
9898
wp_enqueue_script( 'job_manager_admin_js', JOB_MANAGER_PLUGIN_URL . '/assets/js/admin.min.js', [ 'jquery', 'jquery-tiptip', 'select2' ], JOB_MANAGER_VERSION, true );
9999

100100
wp_localize_script(
@@ -113,17 +113,6 @@ public function admin_enqueue_scripts() {
113113
'search_users_nonce' => wp_create_nonce( 'search-users' ),
114114
]
115115
);
116-
117-
if ( ! function_exists( 'wp_localize_jquery_ui_datepicker' ) || ! has_action( 'admin_enqueue_scripts', 'wp_localize_jquery_ui_datepicker' ) ) {
118-
wp_localize_script(
119-
'job_manager_datepicker_js',
120-
'job_manager_datepicker',
121-
[
122-
/* translators: jQuery date format, see http://api.jqueryui.com/datepicker/#utility-formatDate */
123-
'date_format' => _x( 'yy-mm-dd', 'Date format for jQuery datepicker.', 'wp-job-manager' ),
124-
]
125-
);
126-
}
127116
}
128117

129118
wp_enqueue_style( 'job_manager_admin_menu_css', JOB_MANAGER_PLUGIN_URL . '/assets/css/menu.css', [], JOB_MANAGER_VERSION );

includes/class-wp-job-manager.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function __construct() {
8686
add_action( 'widgets_init', [ $this, 'widgets_init' ] );
8787
add_action( 'wp_loaded', [ $this, 'register_shared_assets' ] );
8888
add_action( 'wp_enqueue_scripts', [ $this, 'frontend_scripts' ] );
89+
add_action( 'wp_footer', [ $this, 'maybe_localize_jquery_ui_datepicker' ], 1 );
8990
add_action( 'admin_init', [ $this, 'updater' ] );
9091
add_action( 'admin_init', [ $this, 'add_privacy_policy_content' ] );
9192
add_action( 'wp_logout', [ $this, 'cleanup_job_posting_cookies' ] );
@@ -251,6 +252,9 @@ public function register_shared_assets() {
251252

252253
$jquery_version = isset( $wp_scripts->registered['jquery-ui-core']->ver ) ? $wp_scripts->registered['jquery-ui-core']->ver : '1.9.2';
253254
wp_register_style( 'jquery-ui', '//code.jquery.com/ui/' . $jquery_version . '/themes/smoothness/jquery-ui.min.css', [], $jquery_version );
255+
256+
// Register datepicker JS. It will be enqueued if needed when a date field is used.
257+
wp_register_script( 'wp-job-manager-datepicker', JOB_MANAGER_PLUGIN_URL . '/assets/js/datepicker.min.js', [ 'jquery', 'jquery-ui-datepicker' ], JOB_MANAGER_VERSION, true );
254258
}
255259

256260
/**
@@ -261,6 +265,21 @@ public static function register_select2_assets() {
261265
wp_register_style( 'select2', JOB_MANAGER_PLUGIN_URL . '/assets/js/select2/select2.min.css', [], '4.0.10' );
262266
}
263267

268+
/**
269+
* WordPress localizes this script late in `wp_head`. We sometimes enqueue the datepicker later on.
270+
*
271+
* @access private
272+
* @since 1.34.1
273+
*/
274+
public function maybe_localize_jquery_ui_datepicker() {
275+
// Check if this data has already been added. Prevents outputting localization data multiple times.
276+
if ( wp_scripts()->get_data( 'jquery-ui-datepicker', 'after' ) ) {
277+
return;
278+
}
279+
280+
wp_localize_jquery_ui_datepicker();
281+
}
282+
264283
/**
265284
* Registers and enqueues scripts and CSS.
266285
*

includes/forms/class-wp-job-manager-form-submit-job.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -465,32 +465,16 @@ protected function validate_fields( $values ) {
465465
protected function enqueue_job_form_assets() {
466466
wp_enqueue_script( 'wp-job-manager-job-submission' );
467467
wp_enqueue_style( 'wp-job-manager-job-submission', JOB_MANAGER_PLUGIN_URL . '/assets/css/job-submission.css', [], JOB_MANAGER_VERSION );
468-
469-
// Register datepicker JS. It will be enqueued if needed when a date.
470-
// field is rendered.
471-
wp_register_script( 'wp-job-manager-datepicker', JOB_MANAGER_PLUGIN_URL . '/assets/js/datepicker.min.js', [ 'jquery', 'jquery-ui-datepicker' ], JOB_MANAGER_VERSION, true );
472-
473-
// Localize scripts after the fields are rendered.
474-
add_action( 'submit_job_form_end', [ $this, 'localize_job_form_scripts' ] );
475468
}
476469

477470
/**
478471
* Localize frontend scripts that have been enqueued. This should be called
479472
* after the fields are rendered, in case some of them enqueue new scripts.
473+
*
474+
* @deprecated 1.34.1 No longer needed.
480475
*/
481476
public function localize_job_form_scripts() {
482-
if ( function_exists( 'wp_localize_jquery_ui_datepicker' ) ) {
483-
wp_localize_jquery_ui_datepicker();
484-
} else {
485-
wp_localize_script(
486-
'wp-job-manager-datepicker',
487-
'job_manager_datepicker',
488-
[
489-
/* translators: jQuery date format, see http://api.jqueryui.com/datepicker/#utility-formatDate */
490-
'date_format' => _x( 'yy-mm-dd', 'Date format for jQuery datepicker.', 'wp-job-manager' ),
491-
]
492-
);
493-
}
477+
_deprecated_function( __METHOD__, '1.34.1' );
494478
}
495479

496480
/**

0 commit comments

Comments
 (0)