[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> block-patterns.php (source)

   1  <?php
   2  /**
   3   * Register the block patterns and block patterns categories
   4   *
   5   * @package WordPress
   6   * @since 5.5.0
   7   */
   8  
   9  add_theme_support( 'core-block-patterns' );
  10  
  11  /**
  12   * Registers the core block patterns and categories.
  13   *
  14   * @since 5.5.0
  15   * @access private
  16   */
  17  function _register_core_block_patterns_and_categories() {
  18      $should_register_core_patterns = get_theme_support( 'core-block-patterns' );
  19  
  20      if ( $should_register_core_patterns ) {
  21          $core_block_patterns = array(
  22              'query-standard-posts',
  23              'query-medium-posts',
  24              'query-small-posts',
  25              'query-grid-posts',
  26              'query-large-title-posts',
  27              'query-offset-posts',
  28              'social-links-shared-background-color',
  29          );
  30  
  31          foreach ( $core_block_patterns as $core_block_pattern ) {
  32              register_block_pattern(
  33                  'core/' . $core_block_pattern,
  34                  require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php'
  35              );
  36          }
  37      }
  38  
  39      register_block_pattern_category( 'buttons', array( 'label' => _x( 'Buttons', 'Block pattern category' ) ) );
  40      register_block_pattern_category( 'columns', array( 'label' => _x( 'Columns', 'Block pattern category' ) ) );
  41      register_block_pattern_category( 'featured', array( 'label' => _x( 'Featured', 'Block pattern category' ) ) );
  42      register_block_pattern_category( 'gallery', array( 'label' => _x( 'Gallery', 'Block pattern category' ) ) );
  43      register_block_pattern_category( 'header', array( 'label' => _x( 'Headers', 'Block pattern category' ) ) );
  44      register_block_pattern_category( 'text', array( 'label' => _x( 'Text', 'Block pattern category' ) ) );
  45      register_block_pattern_category( 'query', array( 'label' => _x( 'Query', 'Block pattern category' ) ) );
  46  }
  47  
  48  /**
  49   * Register Core's official patterns from wordpress.org/patterns.
  50   *
  51   * @since 5.8.0
  52   * @since 5.9.0 The $current_screen argument was removed.
  53   *
  54   * @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from.
  55   */
  56  function _load_remote_block_patterns( $deprecated = null ) {
  57      if ( ! empty( $deprecated ) ) {
  58          _deprecated_argument( __FUNCTION__, '5.9.0' );
  59          $current_screen = $deprecated;
  60          if ( ! $current_screen->is_block_editor ) {
  61              return;
  62          }
  63      }
  64  
  65      $supports_core_patterns = get_theme_support( 'core-block-patterns' );
  66  
  67      /**
  68       * Filter to disable remote block patterns.
  69       *
  70       * @since 5.8.0
  71       *
  72       * @param bool $should_load_remote
  73       */
  74      $should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
  75  
  76      if ( $supports_core_patterns && $should_load_remote ) {
  77          $request         = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
  78          $core_keyword_id = 11; // 11 is the ID for "core".
  79          $request->set_param( 'keyword', $core_keyword_id );
  80          $response = rest_do_request( $request );
  81          if ( $response->is_error() ) {
  82              return;
  83          }
  84          $patterns = $response->get_data();
  85  
  86          foreach ( $patterns as $settings ) {
  87              $pattern_name = 'core/' . sanitize_title( $settings['title'] );
  88              register_block_pattern( $pattern_name, (array) $settings );
  89          }
  90      }
  91  }
  92  
  93  /**
  94   * Register `Featured` (category) patterns from wordpress.org/patterns.
  95   *
  96   * @since 5.9.0
  97   */
  98  function _load_remote_featured_patterns() {
  99      $supports_core_patterns = get_theme_support( 'core-block-patterns' );
 100  
 101      /** This filter is documented in wp-includes/block-patterns.php */
 102      $should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
 103  
 104      if ( ! $should_load_remote || ! $supports_core_patterns ) {
 105          return;
 106      }
 107  
 108      $request         = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
 109      $featured_cat_id = 26; // This is the `Featured` category id from pattern directory.
 110      $request->set_param( 'category', $featured_cat_id );
 111      $response = rest_do_request( $request );
 112      if ( $response->is_error() ) {
 113          return;
 114      }
 115      $patterns = $response->get_data();
 116  
 117      foreach ( $patterns as $pattern ) {
 118          $pattern_name = sanitize_title( $pattern['title'] );
 119          $registry     = WP_Block_Patterns_Registry::get_instance();
 120          // Some patterns might be already registered as core patterns with the `core` prefix.
 121          $is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" );
 122          if ( ! $is_registered ) {
 123              register_block_pattern( $pattern_name, (array) $pattern );
 124          }
 125      }
 126  }
 127  
 128  /**
 129   * Registers patterns from Pattern Directory provided by a theme's
 130   * `theme.json` file.
 131   *
 132   * @since 6.0.0
 133   * @access private
 134   */
 135  function _register_remote_theme_patterns() {
 136      if ( ! get_theme_support( 'core-block-patterns' ) ) {
 137          return;
 138      }
 139  
 140      /** This filter is documented in wp-includes/block-patterns.php */
 141      if ( ! apply_filters( 'should_load_remote_block_patterns', true ) ) {
 142          return;
 143      }
 144  
 145      if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
 146          return;
 147      }
 148  
 149      $pattern_settings = WP_Theme_JSON_Resolver::get_theme_data()->get_patterns();
 150      if ( empty( $pattern_settings ) ) {
 151          return;
 152      }
 153  
 154      $request         = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
 155      $request['slug'] = $pattern_settings;
 156      $response        = rest_do_request( $request );
 157      if ( $response->is_error() ) {
 158          return;
 159      }
 160      $patterns          = $response->get_data();
 161      $patterns_registry = WP_Block_Patterns_Registry::get_instance();
 162      foreach ( $patterns as $pattern ) {
 163          $pattern_name = sanitize_title( $pattern['title'] );
 164          // Some patterns might be already registered as core patterns with the `core` prefix.
 165          $is_registered = $patterns_registry->is_registered( $pattern_name ) || $patterns_registry->is_registered( "core/$pattern_name" );
 166          if ( ! $is_registered ) {
 167              register_block_pattern( $pattern_name, (array) $pattern );
 168          }
 169      }
 170  }
 171  
 172  /**
 173   * Register any patterns that the active theme may provide under its
 174   * `./patterns/` directory. Each pattern is defined as a PHP file and defines
 175   * its metadata using plugin-style headers. The minimum required definition is:
 176   *
 177   *     /**
 178   *      * Title: My Pattern
 179   *      * Slug: my-theme/my-pattern
 180   *      *
 181   *
 182   * The output of the PHP source corresponds to the content of the pattern, e.g.:
 183   *
 184   *     <main><p><?php echo "Hello"; ?></p></main>
 185   *
 186   * If applicable, this will collect from both parent and child theme.
 187   *
 188   * Other settable fields include:
 189   *
 190   *   - Description
 191   *   - Viewport Width
 192   *   - Categories       (comma-separated values)
 193   *   - Keywords         (comma-separated values)
 194   *   - Block Types      (comma-separated values)
 195   *   - Inserter         (yes/no)
 196   *
 197   * @since 6.0.0
 198   * @access private
 199   */
 200  function _register_theme_block_patterns() {
 201      $default_headers = array(
 202          'title'         => 'Title',
 203          'slug'          => 'Slug',
 204          'description'   => 'Description',
 205          'viewportWidth' => 'Viewport Width',
 206          'categories'    => 'Categories',
 207          'keywords'      => 'Keywords',
 208          'blockTypes'    => 'Block Types',
 209          'inserter'      => 'Inserter',
 210      );
 211  
 212      /*
 213       * Register patterns for the active theme. If the theme is a child theme,
 214       * let it override any patterns from the parent theme that shares the same slug.
 215       */
 216      $themes     = array();
 217      $stylesheet = get_stylesheet();
 218      $template   = get_template();
 219      if ( $stylesheet !== $template ) {
 220          $themes[] = wp_get_theme( $stylesheet );
 221      }
 222      $themes[] = wp_get_theme( $template );
 223  
 224      foreach ( $themes as $theme ) {
 225          $dirpath = $theme->get_stylesheet_directory() . '/patterns/';
 226          if ( ! is_dir( $dirpath ) || ! is_readable( $dirpath ) ) {
 227              continue;
 228          }
 229          if ( file_exists( $dirpath ) ) {
 230              $files = glob( $dirpath . '*.php' );
 231              if ( $files ) {
 232                  foreach ( $files as $file ) {
 233                      $pattern_data = get_file_data( $file, $default_headers );
 234  
 235                      if ( empty( $pattern_data['slug'] ) ) {
 236                          _doing_it_wrong(
 237                              '_register_theme_block_patterns',
 238                              sprintf(
 239                                  /* translators: %s: file name. */
 240                                  __( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ),
 241                                  $file
 242                              ),
 243                              '6.0.0'
 244                          );
 245                          continue;
 246                      }
 247  
 248                      if ( ! preg_match( '/^[A-z0-9\/_-]+$/', $pattern_data['slug'] ) ) {
 249                          _doing_it_wrong(
 250                              '_register_theme_block_patterns',
 251                              sprintf(
 252                                  /* translators: %1s: file name; %2s: slug value found. */
 253                                  __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")' ),
 254                                  $file,
 255                                  $pattern_data['slug']
 256                              ),
 257                              '6.0.0'
 258                          );
 259                      }
 260  
 261                      if ( WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_data['slug'] ) ) {
 262                          continue;
 263                      }
 264  
 265                      // Title is a required property.
 266                      if ( ! $pattern_data['title'] ) {
 267                          _doing_it_wrong(
 268                              '_register_theme_block_patterns',
 269                              sprintf(
 270                                  /* translators: %1s: file name; %2s: slug value found. */
 271                                  __( 'Could not register file "%s" as a block pattern ("Title" field missing)' ),
 272                                  $file
 273                              ),
 274                              '6.0.0'
 275                          );
 276                          continue;
 277                      }
 278  
 279                      // For properties of type array, parse data as comma-separated.
 280                      foreach ( array( 'categories', 'keywords', 'blockTypes' ) as $property ) {
 281                          if ( ! empty( $pattern_data[ $property ] ) ) {
 282                              $pattern_data[ $property ] = array_filter(
 283                                  preg_split(
 284                                      '/[\s,]+/',
 285                                      (string) $pattern_data[ $property ]
 286                                  )
 287                              );
 288                          } else {
 289                              unset( $pattern_data[ $property ] );
 290                          }
 291                      }
 292  
 293                      // Parse properties of type int.
 294                      foreach ( array( 'viewportWidth' ) as $property ) {
 295                          if ( ! empty( $pattern_data[ $property ] ) ) {
 296                              $pattern_data[ $property ] = (int) $pattern_data[ $property ];
 297                          } else {
 298                              unset( $pattern_data[ $property ] );
 299                          }
 300                      }
 301  
 302                      // Parse properties of type bool.
 303                      foreach ( array( 'inserter' ) as $property ) {
 304                          if ( ! empty( $pattern_data[ $property ] ) ) {
 305                              $pattern_data[ $property ] = in_array(
 306                                  strtolower( $pattern_data[ $property ] ),
 307                                  array( 'yes', 'true' ),
 308                                  true
 309                              );
 310                          } else {
 311                              unset( $pattern_data[ $property ] );
 312                          }
 313                      }
 314  
 315                      // Translate the pattern metadata.
 316                      $text_domain = $theme->get( 'TextDomain' );
 317                      //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.NonSingularStringLiteralContext, WordPress.WP.I18n.NonSingularStringLiteralDomain, WordPress.WP.I18n.LowLevelTranslationFunction
 318                      $pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', $text_domain );
 319                      if ( ! empty( $pattern_data['description'] ) ) {
 320                          //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.NonSingularStringLiteralContext, WordPress.WP.I18n.NonSingularStringLiteralDomain, WordPress.WP.I18n.LowLevelTranslationFunction
 321                          $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', $text_domain );
 322                      }
 323  
 324                      // The actual pattern content is the output of the file.
 325                      ob_start();
 326                      include $file;
 327                      $pattern_data['content'] = ob_get_clean();
 328                      if ( ! $pattern_data['content'] ) {
 329                          continue;
 330                      }
 331  
 332                      register_block_pattern( $pattern_data['slug'], $pattern_data );
 333                  }
 334              }
 335          }
 336      }
 337  }
 338  add_action( 'init', '_register_theme_block_patterns' );


Generated: Tue Apr 16 01:00:02 2024 Cross-referenced by PHPXref 0.7.1