Skip to content

Commit b5780fa

Browse files
committed
Widget Customizer: Improve plugin compatibility.
Some plugins are using custom scripts and styles for there widgets. These are available on the Widgets screens, but not in the Customizer yet. Scripts and styles can be enqueued via: * `admin_enqueue_scripts` * `admin_print_scripts` and `admin_print_scripts-widgets.php` * `admin_print_styles` and `admin_print_styles-widgets.php` * `admin_print_footer_scripts` and `admin_footer-widgets.php` All this hooks are now called in the Customizer too. Previously we have add the `#widgets-right` ID to a container div via jQuery. Remember: `#widgets-right` exists on the Widgets screen and is used by many plugins to do event delegation from that element. But since our script files are loaded in the footer, the JavaScript way is a bit late for some plugins. We have decided to add a `div#widgets-right` container element to customizer. "Less hacky hack." props westonruter, ocean90. Thanks dpe415 for testing. fixes #27619. git-svn-id: https://develop.svn.wordpress.org/trunk@27907 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c86d902 commit b5780fa

3 files changed

Lines changed: 55 additions & 6 deletions

File tree

src/wp-admin/customize.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
$cannot_expand = ! ( $screenshot || $wp_customize->theme()->get('Description') );
119119
?>
120120

121+
<div id="widgets-right"><!-- For Widget Customizer, many widgets try to look for instances under div#widgets-right, so we have to add that ID to a container div in the customizer for compat -->
121122
<div class="wp-full-overlay-sidebar-content accordion-container" tabindex="-1">
122123
<div id="customize-info" class="accordion-section <?php if ( $cannot_expand ) echo ' cannot-expand'; ?>">
123124
<div class="accordion-section-title" aria-label="<?php esc_attr_e( 'Theme Customizer Options' ); ?>" tabindex="0">
@@ -146,6 +147,7 @@
146147
?>
147148
</ul></div>
148149
</div>
150+
</div>
149151

150152
<div id="customize-footer-actions" class="wp-full-overlay-footer">
151153
<a href="#" class="collapse-sidebar button-secondary" title="<?php esc_attr_e('Collapse Sidebar'); ?>">

src/wp-admin/js/customize-widgets.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ var WidgetCustomizer = ( function ($) {
3535
window.ajaxurl = wp.ajax.settings.url;
3636
}
3737

38-
// Unfortunately many widgets try to look for instances under div#widgets-right,
39-
// so we have to add that ID to a container div in the customizer for compat
40-
$( '#customize-theme-controls' ).closest( 'div:not([id])' ).attr( 'id', 'widgets-right' );
41-
4238
/**
4339
* Set up model
4440
*/

src/wp-includes/class-wp-customize-widgets.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ public function __construct( WP_Customize_Manager $manager ) {
7474
add_action( 'after_setup_theme', array( $this, 'setup_widget_addition_previews' ) );
7575
add_action( 'customize_controls_init', array( $this, 'customize_controls_init' ) );
7676
add_action( 'customize_register', array( $this, 'schedule_customize_register' ), 1 );
77-
add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_deps' ) );
77+
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
78+
add_action( 'customize_controls_print_styles', array( $this, 'print_styles' ) );
79+
add_action( 'customize_controls_print_scripts', array( $this, 'print_scripts' ) );
80+
add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_footer_scripts' ) );
7881
add_action( 'customize_controls_print_footer_scripts', array( $this, 'output_widget_control_templates' ) );
7982
add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) );
8083

@@ -532,16 +535,49 @@ public function parse_widget_setting_id( $setting_id ) {
532535
return compact( 'id_base', 'number' );
533536
}
534537

538+
/**
539+
* Call admin_print_styles-widgets.php and admin_print_styles hooks to
540+
* allow custom styles from plugins.
541+
*
542+
* @since 3.9.0
543+
* @access public
544+
*/
545+
public function print_styles() {
546+
/** This action is documented in wp-admin/admin-header.php */
547+
do_action( 'admin_print_styles-widgets.php' );
548+
549+
/** This action is documented in wp-admin/admin-header.php */
550+
do_action( 'admin_print_styles' );
551+
}
552+
553+
/**
554+
* Call admin_print_scripts-widgets.php and admin_print_scripts hooks to
555+
* allow custom scripts from plugins.
556+
*
557+
* @since 3.9.0
558+
* @access public
559+
*/
560+
public function print_scripts() {
561+
/** This action is documented in wp-admin/admin-header.php */
562+
do_action( 'admin_print_scripts-widgets.php' );
563+
564+
/** This action is documented in wp-admin/admin-header.php */
565+
do_action( 'admin_print_scripts' );
566+
}
567+
535568
/**
536569
* Enqueue scripts and styles for customizer panel and export data to JavaScript.
537570
*
538571
* @since 3.9.0
539572
* @access public
540573
*/
541-
public function customize_controls_enqueue_deps() {
574+
public function enqueue_scripts() {
542575
wp_enqueue_style( 'customize-widgets' );
543576
wp_enqueue_script( 'customize-widgets' );
544577

578+
/** This action is documented in wp-admin/admin-header.php */
579+
do_action( 'admin_enqueue_scripts', 'widgets.php' );
580+
545581
/*
546582
* Export available widgets with control_tpl removed from model
547583
* since plugins need templates to be in the DOM.
@@ -641,6 +677,21 @@ public function output_widget_control_templates() {
641677
<?php
642678
}
643679

680+
/**
681+
* Call admin_print_footer_scripts and admin_print_scripts hooks to
682+
* allow custom scripts from plugins.
683+
*
684+
* @since 3.9.0
685+
* @access public
686+
*/
687+
public function print_footer_scripts() {
688+
/** This action is documented in wp-admin/admin-footer.php */
689+
do_action( 'admin_print_footer_scripts' );
690+
691+
/** This action is documented in wp-admin/admin-footer.php */
692+
do_action( 'admin_footer-widgets.php' );
693+
}
694+
644695
/**
645696
* Get common arguments to supply when constructing a Customizer setting.
646697
*

0 commit comments

Comments
 (0)