[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> entity-utils.php (source)

   1  <?php
   2  /**
   3   * Utilities shared by the Entity Actions and Entity Fields APIs.
   4   *
   5   * @package WordPress
   6   * @since 7.2.0
   7   */
   8  
   9  /**
  10   * Adds the paths of an entity route for the post types the editors list.
  11   *
  12   * The post editor lists the post type of the edited post. The site editor
  13   * lists that of the edited post, if any, and those of the screen it opens on.
  14   * Post types that are not registered are skipped, and each post type is added
  15   * once.
  16   *
  17   * @since 7.2.0
  18   * @access private
  19   *
  20   * @param mixed  $paths   REST API paths to preload.
  21   * @param mixed  $context Block editor context.
  22   * @param string $route   The entity route, such as `/wp/v2/entity-fields`.
  23   * @return mixed The paths with one path per post type added. An empty value
  24   *               that is not an array becomes an array. Any other value that
  25   *               is not an array, or a context that is not a block editor
  26   *               context, returns `$paths` unchanged.
  27   */
  28  function _wp_add_entity_preload_paths( $paths, $context, $route ) {
  29      if ( empty( $paths ) && ! is_array( $paths ) ) {
  30          $paths = array();
  31      }
  32  
  33      if ( ! is_array( $paths ) || ! $context instanceof WP_Block_Editor_Context ) {
  34          return $paths;
  35      }
  36  
  37      $post_types = array();
  38      if ( 'core/edit-post' === $context->name && isset( $context->post ) ) {
  39          $post_types[] = $context->post->post_type;
  40      } elseif ( 'core/edit-site' === $context->name ) {
  41          if ( isset( $context->post ) ) {
  42              $post_types[] = $context->post->post_type;
  43          }
  44  
  45          /*
  46           * Adds the post types the screen lists or edits, from the first
  47           * segment of its `p` query arg. The screens are the routes in
  48           * packages/edit-site/src/components/site-editor-routes in Gutenberg.
  49           * The patterns screen lists both patterns and template parts.
  50           */
  51          $screen_post_types = array(
  52              'page'             => array( 'page' ),
  53              'template'         => array( 'wp_template' ),
  54              'wp_template'      => array( 'wp_template' ),
  55              'pattern'          => array( 'wp_block', 'wp_template_part' ),
  56              'wp_block'         => array( 'wp_block' ),
  57              'wp_template_part' => array( 'wp_template_part' ),
  58          );
  59  
  60          // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks what to preload.
  61          $screen   = isset( $_GET['p'] ) && is_string( $_GET['p'] ) ? sanitize_text_field( wp_unslash( $_GET['p'] ) ) : '';
  62          $segments = explode( '/', trim( $screen, '/' ) );
  63          if ( isset( $screen_post_types[ $segments[0] ] ) ) {
  64              $post_types = array_merge( $post_types, $screen_post_types[ $segments[0] ] );
  65          }
  66      }
  67  
  68      foreach ( array_unique( $post_types ) as $post_type ) {
  69          if ( ! post_type_exists( $post_type ) ) {
  70              continue;
  71          }
  72          $paths[] = add_query_arg(
  73              array(
  74                  'kind' => 'postType',
  75                  'name' => $post_type,
  76              ),
  77              $route
  78          );
  79      }
  80  
  81      return $paths;
  82  }
  83  
  84  /**
  85   * Declares the script modules of an entity API as dependencies of the editor
  86   * script.
  87   *
  88   * The editor imports the script modules of an entity on demand, with a dynamic
  89   * `import()` the browser resolves through the import map of the page. Listing
  90   * the modules as dynamic `module_dependencies` of the `wp-editor` script adds
  91   * them to the import map of every page that loads the editor script: the post
  92   * editor, the site editor, and the pages booted by `@wordpress/boot`, whose
  93   * prerequisites depend on it. A dynamic dependency is only listed in the
  94   * import map; the module is fetched when it is imported.
  95   *
  96   * The modules are read with `$get_modules`, which reads the registry of the
  97   * API and so fires its init action, registering the entities and their script
  98   * modules on demand. It is only called on the pages that load the editor
  99   * script, not on every admin request (heartbeat, Ajax, other screens). The
 100   * callers run on `admin_footer`, the last action before the import map is
 101   * printed that fires both in the admin template and in the pages rendered
 102   * outside it, which print the import map themselves. By then `init` has
 103   * completed, so the post types and supports the registrations derive from are
 104   * final. The registries only accept registrations while their action fires,
 105   * so the modules declared here are those of everything the page can show.
 106   *
 107   * @since 7.2.0
 108   * @access private
 109   *
 110   * @param WP_Scripts|null $scripts     The scripts registry. Null for the global one.
 111   * @param callable        $get_modules Returns the module ids of the API, keyed
 112   *                                     by kind, then by name.
 113   */
 114  function _wp_add_entity_modules_to_editor_script( $scripts, $get_modules ) {
 115      if ( ! $scripts instanceof WP_Scripts ) {
 116          $scripts = wp_scripts();
 117      }
 118  
 119      if ( ! $scripts->query( 'wp-editor', 'enqueued' ) && ! $scripts->query( 'wp-editor', 'done' ) ) {
 120          return;
 121      }
 122  
 123      $kinds = call_user_func( $get_modules );
 124      if ( empty( $kinds ) ) {
 125          return;
 126      }
 127  
 128      $dependencies = $scripts->get_data( 'wp-editor', 'module_dependencies' );
 129      $dependencies = is_array( $dependencies ) ? $dependencies : array();
 130  
 131      $declared = array();
 132      foreach ( $dependencies as $dependency ) {
 133          $declared[] = is_array( $dependency ) ? $dependency['id'] : $dependency;
 134      }
 135  
 136      $added = false;
 137      foreach ( $kinds as $entities ) {
 138          foreach ( $entities as $modules ) {
 139              foreach ( $modules as $module ) {
 140                  if ( ! in_array( $module, $declared, true ) ) {
 141                      $dependencies[] = array(
 142                          'id'     => $module,
 143                          'import' => 'dynamic',
 144                      );
 145                      $declared[]     = $module;
 146                      $added          = true;
 147                  }
 148              }
 149          }
 150      }
 151  
 152      if ( $added ) {
 153          // `add_data()` replaces the value, hence the merge above.
 154          $scripts->add_data( 'wp-editor', 'module_dependencies', $dependencies );
 155      }
 156  }


Generated : Sat Oct 24 08:20:34 2026 Cross-referenced by PHPXref