[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/build/pages/experiments/ -> page.php (source)

   1  <?php
   2  /**
   3   * Page: experiments
   4   * Auto-generated by build process.
   5   * Do not edit this file manually.
   6   *
   7   * @package wp
   8   */
   9  
  10  // Global storage for experiments routes and menu items
  11  global $wp_experiments_routes, $wp_experiments_menu_items;
  12  $wp_experiments_routes     = array();
  13  $wp_experiments_menu_items = array();
  14  
  15  /**
  16   * Register a route for the experiments page.
  17   *
  18   * @param string      $path           Route path (e.g., '/types/$type/edit/$id').
  19   * @param string|null $content_module Script module ID for content (stage/inspector).
  20   * @param string|null $route_module   Script module ID for route lifecycle hooks.
  21   */
  22  function wp_register_experiments_route( $path, $content_module = null, $route_module = null ) {
  23      global $wp_experiments_routes;
  24  
  25      $route = array( 'path' => $path );
  26      if ( ! empty( $content_module ) ) {
  27          $route['content_module'] = $content_module;
  28      }
  29      if ( ! empty( $route_module ) ) {
  30          $route['route_module'] = $route_module;
  31      }
  32  
  33      $wp_experiments_routes[] = $route;
  34  }
  35  
  36  /**
  37   * Register a menu item for the experiments page.
  38   *
  39   * @param string $id          Menu item ID.
  40   * @param string $label       Display label.
  41   * @param string $to          Route path to navigate to.
  42   * @param string $parent_id   Optional. Parent menu item ID.
  43   * @param string $parent_type Optional. Parent type: 'drilldown' or 'dropdown'.
  44   */
  45  function wp_register_experiments_menu_item( $id, $label, $to, $parent_id = '', $parent_type = '' ) {
  46      global $wp_experiments_menu_items;
  47  
  48      $menu_item = array(
  49          'id'    => $id,
  50          'label' => $label,
  51          'to'    => $to,
  52      );
  53  
  54      if ( ! empty( $parent_id ) ) {
  55          $menu_item['parent'] = $parent_id;
  56      }
  57  
  58      if ( ! empty( $parent_type ) && in_array( $parent_type, array( 'drilldown', 'dropdown' ), true ) ) {
  59          $menu_item['parent_type'] = $parent_type;
  60      }
  61  
  62      $wp_experiments_menu_items[] = $menu_item;
  63  }
  64  
  65  /**
  66   * Get all registered routes for the experiments page.
  67   *
  68   * @return array Array of route objects.
  69   */
  70  function wp_get_experiments_routes() {
  71      global $wp_experiments_routes;
  72      return $wp_experiments_routes ?? array();
  73  }
  74  
  75  /**
  76   * Get all registered menu items for the experiments page.
  77   *
  78   * @return array Array of menu item objects.
  79   */
  80  function wp_get_experiments_menu_items() {
  81      global $wp_experiments_menu_items;
  82      return $wp_experiments_menu_items ?? array();
  83  }
  84  
  85  /**
  86   * Preload REST API data for the experiments page.
  87   * Automatically called during page rendering.
  88   */
  89  function wp_experiments_preload_data() {
  90      // Define paths to preload - same for all pages
  91      // Please also change packages/core-data/src/entities.js when changing this.
  92      $preload_paths = array(
  93          '/?_fields=description,gmt_offset,home,image_sizes,image_size_threshold,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front',
  94          array( '/wp/v2/settings', 'OPTIONS' ),
  95      );
  96  
  97      // Use rest_preload_api_request to gather the preloaded data
  98      $preload_data = array_reduce(
  99          $preload_paths,
 100          'rest_preload_api_request',
 101          array()
 102      );
 103  
 104      // Register the preloading middleware with wp-api-fetch
 105      wp_add_inline_script(
 106          'wp-api-fetch',
 107          sprintf(
 108              'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
 109              wp_json_encode( $preload_data )
 110          ),
 111          'after'
 112      );
 113  }
 114  
 115  /**
 116   * Render the experiments page.
 117   * Call this function from add_menu_page or add_submenu_page.
 118   */
 119  function wp_experiments_render_page() {
 120      // Load build constants
 121      $build_constants = require  __DIR__ . '/../../constants.php';
 122  
 123      // Set current screen
 124      set_current_screen();
 125  
 126      // Remove unwanted deprecated handler
 127      remove_action( 'admin_head', 'wp_admin_bar_header' );
 128  
 129      // Remove unwanted scripts and styles that were enqueued during `admin_init`
 130      foreach ( wp_scripts()->queue as $script ) {
 131          wp_dequeue_script( $script );
 132      }
 133      foreach ( wp_styles()->queue as $style ) {
 134          wp_dequeue_style( $style );
 135      }
 136  
 137      // Fire init action for extensions to register routes and menu items
 138      do_action( 'experiments_init' );
 139  
 140      // Enqueue command palette assets for boot-based pages
 141      if ( function_exists( 'wp_enqueue_command_palette_assets' ) ) {
 142          wp_enqueue_command_palette_assets();
 143      }
 144  
 145      // Preload REST API data
 146      wp_experiments_preload_data();
 147  
 148      // Get all registered routes and menu items
 149      $menu_items = wp_get_experiments_menu_items();
 150      $routes = wp_get_experiments_routes();
 151  
 152      // Get boot module asset file for dependencies
 153      $asset_file = ABSPATH . WPINC . '/js/dist/script-modules/boot/index.min.asset.php';
 154      if ( file_exists( $asset_file ) ) {
 155          $asset = require $asset_file;
 156  
 157          // This script serves two purposes:
 158          // 1. It ensures all the globals that are made available to the modules are loaded.
 159          // 2. It initializes the boot module as an inline script.
 160          wp_register_script( 'experiments-prerequisites', '', $asset['dependencies'], $asset['version'], true );
 161  
 162          // Add inline script to initialize the app
 163          $init_modules = [];
 164          wp_add_inline_script(
 165              'experiments-prerequisites',
 166              sprintf(
 167                  'import("@wordpress/boot").then(mod => mod.init({mountId: "%s", menuItems: %s, routes: %s, initModules: %s, dashboardLink: "%s"}));',
 168                  'experiments-app',
 169                  wp_json_encode( $menu_items, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
 170                  wp_json_encode( $routes, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
 171                  wp_json_encode( $init_modules, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
 172                  esc_url( admin_url( '/' ) )
 173              )
 174          );
 175  
 176          // Register prerequisites style by filtering script dependencies to find registered styles
 177          $style_dependencies = array_filter(
 178              $asset['dependencies'],
 179              function ( $handle ) {
 180                  return wp_style_is( $handle, 'registered' );
 181              }
 182          );
 183          wp_register_style( 'experiments-prerequisites', false, $style_dependencies, $asset['version'] );
 184  
 185          // Build dependencies for experiments module
 186          $boot_dependencies = array(
 187              array(
 188                  'import' => 'static',
 189                  'id'     => '@wordpress/boot',
 190              ),
 191          );
 192  
 193          // Add init modules as static dependencies
 194              // No init modules configured
 195  
 196          // Add all registered routes as dependencies
 197          foreach ( $routes as $route ) {
 198              if ( isset( $route['route_module'] ) ) {
 199                  $boot_dependencies[] = array(
 200                      'import' => 'static',
 201                      'id'     => $route['route_module'],
 202                  );
 203              }
 204              if ( isset( $route['content_module'] ) ) {
 205                  $boot_dependencies[] = array(
 206                      'import' => 'dynamic',
 207                      'id'     => $route['content_module'],
 208                  );
 209              }
 210          }
 211  
 212          /**
 213           * Filters the boot script-module dependencies for the
 214           * experiments page.
 215           *
 216           * Surfaces extending this page can append entries to the boot
 217           * dependency list. Each entry is an array with 'import' (string
 218           * 'static' or 'dynamic') and 'id' (script-module handle) keys.
 219           *
 220           * @param array $boot_dependencies Boot dependencies for the page.
 221           */
 222          $boot_dependencies = apply_filters(
 223              'experiments_boot_dependencies',
 224              $boot_dependencies
 225          );
 226  
 227          // Dummy script module to ensure dependencies are loaded
 228          wp_register_script_module(
 229              'experiments',
 230              $build_constants['build_url'] . 'pages/experiments/loader.js',
 231              $boot_dependencies
 232          );
 233  
 234          // Enqueue the boot scripts and styles
 235          wp_enqueue_script( 'experiments-prerequisites' );
 236          wp_enqueue_script_module( 'experiments' );
 237          wp_enqueue_style( 'experiments-prerequisites' );
 238      }
 239  
 240      // Output the HTML
 241      ?>
 242      <!DOCTYPE html>
 243      <html <?php language_attributes(); ?>>
 244      <head>
 245          <meta charset="<?php bloginfo( 'charset' ); ?>">
 246          <meta name="viewport" content="width=device-width, initial-scale=1">
 247          <title><?php echo esc_html( get_admin_page_title() ); ?></title>
 248          <style>
 249              html {
 250                  background: #f1f1f1;
 251                  color: #444;
 252                  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
 253                  font-size: 13px;
 254                  line-height: 1.4em;
 255              }
 256              body {
 257                  margin: 0;
 258              }
 259              #wpadminbar { display: none; }
 260          </style>
 261      <?php
 262      global $hook_suffix;
 263      // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
 264      $hook_suffix = 'experiments';
 265  
 266      // BEGIN see wp-admin/admin-header.php
 267      print_admin_styles();
 268      print_head_scripts();
 269  
 270      /**
 271       * Fires in head section for a specific admin page.
 272       *
 273       * @since 2.1.0
 274       */
 275      do_action( "admin_head-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 276  
 277      /**
 278       * Fires in head section for all admin pages.
 279       *
 280       * @since 2.1.0
 281       */
 282      do_action( 'admin_head' );
 283      // END see wp-admin/admin-header.php
 284      ?>
 285      </head>
 286      <body class="experiments">
 287          <div id="experiments-app" style="height: 100vh; box-sizing: border-box;"></div>
 288      <?php
 289      // BEGIN see wp-admin/admin-footer.php
 290  
 291      /**
 292       * Prints scripts or data before the default footer scripts.
 293       *
 294       * @since 1.2.0
 295       */
 296      do_action( 'admin_footer', '' );
 297  
 298      // Print import map first so it's available for inline scripts
 299      wp_script_modules()->print_import_map();
 300      print_footer_scripts();
 301      wp_script_modules()->print_enqueued_script_modules();
 302      wp_script_modules()->print_script_module_preloads();
 303      wp_script_modules()->print_script_module_data();
 304  
 305      /**
 306       * Prints scripts or data after the default footer scripts.
 307       *
 308       * @since 2.8.0
 309       */
 310      do_action( "admin_footer-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 311      // END see wp-admin/admin-footer.php
 312      ?>
 313      </body>
 314      </html>
 315      <?php
 316      exit;
 317  }
 318  
 319  /**
 320   * Intercept admin_init to render the page early.
 321   * This bypasses the default WordPress admin template.
 322   */
 323  function wp_experiments_intercept_render() {
 324      // phpcs:ignore WordPress.Security.NonceVerification.Recommended
 325      if ( isset( $_GET['page'] ) && 'experiments' === $_GET['page'] ) {
 326          wp_experiments_render_page();
 327          exit;
 328      }
 329  }
 330  
 331  // Hook the interceptor to admin_init
 332  add_action( 'admin_init', 'wp_experiments_intercept_render' );


Generated : Tue Jun 30 08:20:12 2026 Cross-referenced by PHPXref