block-patterns.php 11 KB

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