WP_Customize_Widgets::enqueue_scripts()

In this article

Enqueues scripts and styles for Customizer panel and export data to JavaScript.

Source

public function enqueue_scripts() {
	global $wp_scripts, $wp_registered_sidebars, $wp_registered_widgets;

	wp_enqueue_style( 'customize-widgets' );
	wp_enqueue_script( 'customize-widgets' );

	/** This action is documented in wp-admin/admin-header.php */
	do_action( 'admin_enqueue_scripts', 'widgets.php' );

	/*
	 * Export available widgets with control_tpl removed from model
	 * since plugins need templates to be in the DOM.
	 */
	$available_widgets = array();

	foreach ( $this->get_available_widgets() as $available_widget ) {
		unset( $available_widget['control_tpl'] );
		$available_widgets[] = $available_widget;
	}

	$widget_reorder_nav_tpl = sprintf(
		'<div class="widget-reorder-nav"><span class="move-widget" tabindex="0">%1$s</span><span class="move-widget-down" tabindex="0">%2$s</span><span class="move-widget-up" tabindex="0">%3$s</span></div>',
		__( 'Move to another area&hellip;' ),
		__( 'Move down' ),
		__( 'Move up' )
	);

	$move_widget_area_tpl = str_replace(
		array( '{description}', '{btn}' ),
		array(
			__( 'Select an area to move this widget into:' ),
			_x( 'Move', 'Move widget' ),
		),
		'<div class="move-widget-area">
			<p class="description">{description}</p>
			<ul class="widget-area-select">
				<% _.each( sidebars, function ( sidebar ){ %>
					<li class="" data-id="<%- sidebar.id %>" title="<%- sidebar.description %>" tabindex="0"><%- sidebar.name %></li>
				<% }); %>
			</ul>
			<div class="move-widget-actions">
				<button class="move-widget-btn button" type="button">{btn}</button>
			</div>
		</div>'
	);

	/*
	 * Gather all strings in PHP that may be needed by JS on the client.
	 * Once JS i18n is implemented (in #20491), this can be removed.
	 */
	$some_non_rendered_areas_messages    = array();
	$some_non_rendered_areas_messages[1] = html_entity_decode(
		__( 'Your theme has 1 other widget area, but this particular page does not display it.' ),
		ENT_QUOTES,
		get_bloginfo( 'charset' )
	);
	$registered_sidebar_count            = count( $wp_registered_sidebars );
	for ( $non_rendered_count = 2; $non_rendered_count < $registered_sidebar_count; $non_rendered_count++ ) {
		$some_non_rendered_areas_messages[ $non_rendered_count ] = html_entity_decode(
			sprintf(
				/* translators: %s: The number of other widget areas registered but not rendered. */
				_n(
					'Your theme has %s other widget area, but this particular page does not display it.',
					'Your theme has %s other widget areas, but this particular page does not display them.',
					$non_rendered_count
				),
				number_format_i18n( $non_rendered_count )
			),
			ENT_QUOTES,
			get_bloginfo( 'charset' )
		);
	}

	if ( 1 === $registered_sidebar_count ) {
		$no_areas_shown_message = html_entity_decode(
			sprintf(
				__( 'Your theme has 1 widget area, but this particular page does not display it.' )
			),
			ENT_QUOTES,
			get_bloginfo( 'charset' )
		);
	} else {
		$no_areas_shown_message = html_entity_decode(
			sprintf(
				/* translators: %s: The total number of widget areas registered. */
				_n(
					'Your theme has %s widget area, but this particular page does not display it.',
					'Your theme has %s widget areas, but this particular page does not display them.',
					$registered_sidebar_count
				),
				number_format_i18n( $registered_sidebar_count )
			),
			ENT_QUOTES,
			get_bloginfo( 'charset' )
		);
	}

	$settings = array(
		'registeredSidebars'          => array_values( $wp_registered_sidebars ),
		'registeredWidgets'           => $wp_registered_widgets,
		'availableWidgets'            => $available_widgets, // @todo Merge this with registered_widgets.
		'l10n'                        => array(
			'saveBtnLabel'     => __( 'Apply' ),
			'saveBtnTooltip'   => __( 'Save and preview changes before publishing them.' ),
			'removeBtnLabel'   => __( 'Remove' ),
			'removeBtnTooltip' => __( 'Keep widget settings and move it to the inactive widgets' ),
			'error'            => __( 'An error has occurred. Please reload the page and try again.' ),
			'widgetMovedUp'    => __( 'Widget moved up' ),
			'widgetMovedDown'  => __( 'Widget moved down' ),
			'navigatePreview'  => __( 'You can navigate to other pages on your site while using the Customizer to view and edit the widgets displayed on those pages.' ),
			'someAreasShown'   => $some_non_rendered_areas_messages,
			'noAreasShown'     => $no_areas_shown_message,
			'reorderModeOn'    => __( 'Reorder mode enabled' ),
			'reorderModeOff'   => __( 'Reorder mode closed' ),
			'reorderLabelOn'   => esc_attr__( 'Reorder widgets' ),
			/* translators: %d: The number of widgets found. */
			'widgetsFound'     => __( 'Number of widgets found: %d' ),
			'noWidgetsFound'   => __( 'No widgets found.' ),
		),
		'tpl'                         => array(
			'widgetReorderNav' => $widget_reorder_nav_tpl,
			'moveWidgetArea'   => $move_widget_area_tpl,
		),
		'selectiveRefreshableWidgets' => $this->get_selective_refreshable_widgets(),
	);

	foreach ( $settings['registeredWidgets'] as &$registered_widget ) {
		unset( $registered_widget['callback'] ); // May not be JSON-serializable.
	}

	$wp_scripts->add_data(
		'customize-widgets',
		'data',
		sprintf( 'var _wpCustomizeWidgetsSettings = %s;', wp_json_encode( $settings ) )
	);

	/*
	 * TODO: Update 'wp-customize-widgets' to not rely so much on things in
	 * 'customize-widgets'. This will let us skip most of the above and not
	 * enqueue 'customize-widgets' which saves bytes.
	 */

	if ( wp_use_widgets_block_editor() ) {
		$block_editor_context = new WP_Block_Editor_Context(
			array(
				'name' => 'core/customize-widgets',
			)
		);

		$editor_settings = get_block_editor_settings(
			get_legacy_widget_block_editor_settings(),
			$block_editor_context
		);

		wp_add_inline_script(
			'wp-customize-widgets',
			sprintf(
				'wp.domReady( function() {
				   wp.customizeWidgets.initialize( "widgets-customizer", %s );
				} );',
				wp_json_encode( $editor_settings )
			)
		);

		// Preload server-registered block schemas.
		wp_add_inline_script(
			'wp-blocks',
			'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
		);

		// Preload server-registered block bindings sources.
		$registered_sources = get_all_registered_block_bindings_sources();
		if ( ! empty( $registered_sources ) ) {
			$filtered_sources = array();
			foreach ( $registered_sources as $source ) {
				$filtered_sources[] = array(
					'name'        => $source->name,
					'label'       => $source->label,
					'usesContext' => $source->uses_context,
				);
			}
			$script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) );
			wp_add_inline_script(
				'wp-blocks',
				$script
			);
		}

		wp_add_inline_script(
			'wp-blocks',
			sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $block_editor_context ) ) ),
			'after'
		);

		wp_enqueue_script( 'wp-customize-widgets' );
		wp_enqueue_style( 'wp-customize-widgets' );

		/** This action is documented in edit-form-blocks.php */
		do_action( 'enqueue_block_editor_assets' );
	}
}

Hooks

do_action( ‘admin_enqueue_scripts’, string $hook_suffix )

Fires when enqueuing scripts for all admin pages.

do_action( ‘enqueue_block_editor_assets’ )

Fires after block assets have been enqueued for the editing interface.

Changelog

VersionDescription
3.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.