Keine Beschreibung

block-patterns.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * @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( 'gallery', array( 'label' => _x( 'Gallery', 'Block pattern category' ) ) );
  37. register_block_pattern_category( 'header', array( 'label' => _x( 'Headers', 'Block pattern category' ) ) );
  38. register_block_pattern_category( 'text', array( 'label' => _x( 'Text', 'Block pattern category' ) ) );
  39. register_block_pattern_category( 'query', array( 'label' => _x( 'Query', 'Block pattern category' ) ) );
  40. }
  41. /**
  42. * Register Core's official patterns from wordpress.org/patterns.
  43. *
  44. * @since 5.8.0
  45. *
  46. * @param WP_Screen $current_screen The screen that the current request was triggered from.
  47. */
  48. function _load_remote_block_patterns( $current_screen ) {
  49. if ( ! $current_screen->is_block_editor ) {
  50. return;
  51. }
  52. $supports_core_patterns = get_theme_support( 'core-block-patterns' );
  53. /**
  54. * Filter to disable remote block patterns.
  55. *
  56. * @since 5.8.0
  57. *
  58. * @param bool $should_load_remote
  59. */
  60. $should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
  61. if ( $supports_core_patterns && $should_load_remote ) {
  62. $request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
  63. $core_keyword_id = 11; // 11 is the ID for "core".
  64. $request->set_param( 'keyword', $core_keyword_id );
  65. $response = rest_do_request( $request );
  66. if ( $response->is_error() ) {
  67. return;
  68. }
  69. $patterns = $response->get_data();
  70. foreach ( $patterns as $settings ) {
  71. $pattern_name = 'core/' . sanitize_title( $settings['title'] );
  72. register_block_pattern( $pattern_name, (array) $settings );
  73. }
  74. }
  75. }