wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



_wp_add_entity_field_modules_to_editor_script › WordPress Function

Seit7.2.0
Veraltetn/v
› _wp_add_entity_field_modules_to_editor_script ( $scripts = null )
Zugriff:
  • private
Parameter:
  • (WP_Scripts|null) $scripts Optional. The scripts registry. Default null, the global one.
    Erforderlich: Nein
    Standard: null
Definiert in:
Codex:

Declares the script modules of the entity fields as dependencies of the editor script.

The editor imports the script modules of an entity on demand, with a dynamic import() the browser resolves through the import map of the page. Listing the modules as dynamic module_dependencies of the wp-editor script adds them to the import map of every page that loads the editor script: the post editor, the site editor, and the pages booted by @wordpress/boot, whose prerequisites depend on it. A dynamic dependency is only listed in the import map; the module is fetched when it is imported. Reading the registry fires wp_entity_fields_api_init, so the fields and their script modules are registered on demand. It only reads the registry on the pages that load the editor script, not on every admin request (heartbeat, Ajax, other screens). It runs on admin_footer, the last action before the import map is printed that fires both in the admin template and in the pages rendered outside it, which print the import map themselves. By then init has completed, so the supports the default fields derive from are final. The registry only accepts registrations while the action fires, so the modules declared here are the modules of every field the page can show. It is hooked to admin_footer without arguments, so the global scripts registry is used. In other scenarios (tests), the scripts are given directly.


Quellcode

function _wp_add_entity_field_modules_to_editor_script( $scripts = null ) {
	if ( ! $scripts instanceof WP_Scripts ) {
		$scripts = wp_scripts();
	}

	/*
	 * The fields are loaded by the editor package, see
	 * packages/editor/src/dataviews/store/private-resolvers.ts in Gutenberg,
	 * which means any screen that wants to use this mechanism needs to load
	 * the editor script.
	 */
	if ( ! $scripts->query( 'wp-editor', 'enqueued' ) && ! $scripts->query( 'wp-editor', 'done' ) ) {
		return;
	}

	$kinds = wp_get_all_registered_entity_field_modules();
	if ( empty( $kinds ) ) {
		return;
	}

	$dependencies = $scripts->get_data( 'wp-editor', 'module_dependencies' );
	$dependencies = is_array( $dependencies ) ? $dependencies : array();

	$declared = array();
	foreach ( $dependencies as $dependency ) {
		$declared[] = is_array( $dependency ) ? $dependency['id'] : $dependency;
	}

	$added = false;
	foreach ( $kinds as $entities ) {
		foreach ( $entities as $modules ) {
			foreach ( $modules as $module ) {
				if ( ! in_array( $module, $declared, true ) ) {
					$dependencies[] = array(
						'id'     => $module,
						'import' => 'dynamic',
					);
					$declared[]     = $module;
					$added          = true;
				}
			}
		}
	}

	if ( $added ) {
		// `add_data()` replaces the value, hence the merge above.
		$scripts->add_data( 'wp-editor', 'module_dependencies', $dependencies );
	}
}