[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> script-modules.php (source)

   1  <?php
   2  /**
   3   * Script Modules API: Script Module functions
   4   *
   5   * @since 6.5.0
   6   *
   7   * @package WordPress
   8   * @subpackage Script Modules
   9   */
  10  
  11  /**
  12   * Retrieves the main WP_Script_Modules instance.
  13   *
  14   * This function provides access to the WP_Script_Modules instance, creating one
  15   * if it doesn't exist yet.
  16   *
  17   * @since 6.5.0
  18   *
  19   * @global WP_Script_Modules $wp_script_modules
  20   *
  21   * @return WP_Script_Modules The main WP_Script_Modules instance.
  22   */
  23  function wp_script_modules(): WP_Script_Modules {
  24      global $wp_script_modules;
  25  
  26      if ( ! ( $wp_script_modules instanceof WP_Script_Modules ) ) {
  27          $wp_script_modules = new WP_Script_Modules();
  28      }
  29  
  30      return $wp_script_modules;
  31  }
  32  
  33  /**
  34   * Registers the script module if no script module with that script module
  35   * identifier has already been registered.
  36   *
  37   * @since 6.5.0
  38   *
  39   * @param string            $id      The identifier of the script module. Should be unique. It will be used in the
  40   *                                   final import map.
  41   * @param string            $src     Optional. Full URL of the script module, or path of the script module relative
  42   *                                   to the WordPress root directory. If it is provided and the script module has
  43   *                                   not been registered yet, it will be registered.
  44   * @param array             $deps    {
  45   *                                       Optional. List of dependencies.
  46   *
  47   *                                       @type string|array ...$0 {
  48   *                                           An array of script module identifiers of the dependencies of this script
  49   *                                           module. The dependencies can be strings or arrays. If they are arrays,
  50   *                                           they need an `id` key with the script module identifier, and can contain
  51   *                                           an `import` key with either `static` or `dynamic`. By default,
  52   *                                           dependencies that don't contain an `import` key are considered static.
  53   *
  54   *                                           @type string $id     The script module identifier.
  55   *                                           @type string $import Optional. Import type. May be either `static` or
  56   *                                                                `dynamic`. Defaults to `static`.
  57   *                                       }
  58   *                                   }
  59   * @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
  60   *                                   It is added to the URL as a query string for cache busting purposes. If $version
  61   *                                   is set to false, the version number is the currently installed WordPress version.
  62   *                                   If $version is set to null, no version is added.
  63   */
  64  function wp_register_script_module( string $id, string $src, array $deps = array(), $version = false ) {
  65      wp_script_modules()->register( $id, $src, $deps, $version );
  66  }
  67  
  68  /**
  69   * Marks the script module to be enqueued in the page.
  70   *
  71   * If a src is provided and the script module has not been registered yet, it
  72   * will be registered.
  73   *
  74   * @since 6.5.0
  75   *
  76   * @param string            $id      The identifier of the script module. Should be unique. It will be used in the
  77   *                                   final import map.
  78   * @param string            $src     Optional. Full URL of the script module, or path of the script module relative
  79   *                                   to the WordPress root directory. If it is provided and the script module has
  80   *                                   not been registered yet, it will be registered.
  81   * @param array             $deps    {
  82   *                                       Optional. List of dependencies.
  83   *
  84   *                                       @type string|array ...$0 {
  85   *                                           An array of script module identifiers of the dependencies of this script
  86   *                                           module. The dependencies can be strings or arrays. If they are arrays,
  87   *                                           they need an `id` key with the script module identifier, and can contain
  88   *                                           an `import` key with either `static` or `dynamic`. By default,
  89   *                                           dependencies that don't contain an `import` key are considered static.
  90   *
  91   *                                           @type string $id     The script module identifier.
  92   *                                           @type string $import Optional. Import type. May be either `static` or
  93   *                                                                `dynamic`. Defaults to `static`.
  94   *                                       }
  95   *                                   }
  96   * @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
  97   *                                   It is added to the URL as a query string for cache busting purposes. If $version
  98   *                                   is set to false, the version number is the currently installed WordPress version.
  99   *                                   If $version is set to null, no version is added.
 100   */
 101  function wp_enqueue_script_module( string $id, string $src = '', array $deps = array(), $version = false ) {
 102      wp_script_modules()->enqueue( $id, $src, $deps, $version );
 103  }
 104  
 105  /**
 106   * Unmarks the script module so it is no longer enqueued in the page.
 107   *
 108   * @since 6.5.0
 109   *
 110   * @param string $id The identifier of the script module.
 111   */
 112  function wp_dequeue_script_module( string $id ) {
 113      wp_script_modules()->dequeue( $id );
 114  }
 115  
 116  /**
 117   * Deregisters the script module.
 118   *
 119   * @since 6.5.0
 120   *
 121   * @param string $id The identifier of the script module.
 122   */
 123  function wp_deregister_script_module( string $id ) {
 124      wp_script_modules()->deregister( $id );
 125  }
 126  
 127  /**
 128   * Registers all the default WordPress Script Modules.
 129   *
 130   * @since 6.7.0
 131   */
 132  function wp_default_script_modules() {
 133      $suffix = defined( 'WP_RUN_CORE_TESTS' ) ? '.min' : wp_scripts_get_suffix();
 134  
 135      /*
 136       * Expects multidimensional array like:
 137       *
 138       *     'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'),
 139       *     'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'),
 140       *     'interactivity-router/index.min.js' => …
 141       */
 142      $assets = include ABSPATH . WPINC . "/assets/script-modules-packages{$suffix}.php";
 143  
 144      foreach ( $assets as $file_name => $script_module_data ) {
 145          /*
 146           * Build the WordPress Script Module ID from the file name.
 147           * Prepend `@wordpress/` and remove extensions and `/index` if present:
 148           *   - interactivity/index.min.js  => @wordpress/interactivity
 149           *   - interactivity/debug.min.js  => @wordpress/interactivity/debug
 150           *   - block-library/query/view.js => @wordpress/block-library/query/view
 151           */
 152          $script_module_id = '@wordpress/' . preg_replace( '~(?:/index)?(?:\.min)?\.js$~D', '', $file_name, 1 );
 153  
 154          switch ( $script_module_id ) {
 155              /*
 156               * Interactivity exposes two entrypoints, "/index" and "/debug".
 157               * "/debug" should replace "/index" in development.
 158               */
 159              case '@wordpress/interactivity/debug':
 160                  if ( ! SCRIPT_DEBUG ) {
 161                      continue 2;
 162                  }
 163                  $script_module_id = '@wordpress/interactivity';
 164                  break;
 165              case '@wordpress/interactivity':
 166                  if ( SCRIPT_DEBUG ) {
 167                      continue 2;
 168                  }
 169                  break;
 170          }
 171  
 172          $path = includes_url( "js/dist/script-modules/{$file_name}" );
 173          wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] );
 174      }
 175  }


Generated : Sat Nov 23 08:20:01 2024 Cross-referenced by PHPXref