[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Blocks API: WP_Block_Patterns_Registry class
   4   *
   5   * @package WordPress
   6   * @subpackage Blocks
   7   * @since 5.5.0
   8   */
   9  
  10  /**
  11   * Class used for interacting with patterns.
  12   *
  13   * @since 5.5.0
  14   */
  15  final class WP_Block_Patterns_Registry {
  16      /**
  17       * Registered patterns array.
  18       *
  19       * @since 5.5.0
  20       * @var array
  21       */
  22      private $registered_patterns = array();
  23  
  24      /**
  25       * Container for the main instance of the class.
  26       *
  27       * @since 5.5.0
  28       * @var WP_Block_Patterns_Registry|null
  29       */
  30      private static $instance = null;
  31  
  32      /**
  33       * Registers a pattern.
  34       *
  35       * @since 5.5.0
  36       *
  37       * @param string $pattern_name       Pattern name including namespace.
  38       * @param array  $pattern_properties Array containing the properties of the pattern: title,
  39       *                                   content, description, viewportWidth, categories, keywords.
  40       * @return bool True if the pattern was registered with success and false otherwise.
  41       */
  42  	public function register( $pattern_name, $pattern_properties ) {
  43          if ( ! isset( $pattern_name ) || ! is_string( $pattern_name ) ) {
  44              _doing_it_wrong(
  45                  __METHOD__,
  46                  __( 'Pattern name must be a string.' ),
  47                  '5.5.0'
  48              );
  49              return false;
  50          }
  51  
  52          if ( ! isset( $pattern_properties['title'] ) || ! is_string( $pattern_properties['title'] ) ) {
  53              _doing_it_wrong(
  54                  __METHOD__,
  55                  __( 'Pattern title must be a string.' ),
  56                  '5.5.0'
  57              );
  58              return false;
  59          }
  60  
  61          if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) {
  62              _doing_it_wrong(
  63                  __METHOD__,
  64                  __( 'Pattern content must be a string.' ),
  65                  '5.5.0'
  66              );
  67              return false;
  68          }
  69  
  70          $this->registered_patterns[ $pattern_name ] = array_merge(
  71              $pattern_properties,
  72              array( 'name' => $pattern_name )
  73          );
  74  
  75          return true;
  76      }
  77  
  78      /**
  79       * Unregisters a pattern.
  80       *
  81       * @since 5.5.0
  82       *
  83       * @param string $pattern_name Pattern name including namespace.
  84       * @return bool True if the pattern was unregistered with success and false otherwise.
  85       */
  86  	public function unregister( $pattern_name ) {
  87          if ( ! $this->is_registered( $pattern_name ) ) {
  88              _doing_it_wrong(
  89                  __METHOD__,
  90                  /* translators: %s: Pattern name. */
  91                  sprintf( __( 'Pattern "%s" not found.' ), $pattern_name ),
  92                  '5.5.0'
  93              );
  94              return false;
  95          }
  96  
  97          unset( $this->registered_patterns[ $pattern_name ] );
  98  
  99          return true;
 100      }
 101  
 102      /**
 103       * Retrieves an array containing the properties of a registered pattern.
 104       *
 105       * @since 5.5.0
 106       *
 107       * @param string $pattern_name Pattern name including namespace.
 108       * @return array Registered pattern properties.
 109       */
 110  	public function get_registered( $pattern_name ) {
 111          if ( ! $this->is_registered( $pattern_name ) ) {
 112              return null;
 113          }
 114  
 115          return $this->registered_patterns[ $pattern_name ];
 116      }
 117  
 118      /**
 119       * Retrieves all registered patterns.
 120       *
 121       * @since 5.5.0
 122       *
 123       * @return array Array of arrays containing the registered patterns properties,
 124       *               and per style.
 125       */
 126  	public function get_all_registered() {
 127          return array_values( $this->registered_patterns );
 128      }
 129  
 130      /**
 131       * Checks if a pattern is registered.
 132       *
 133       * @since 5.5.0
 134       *
 135       * @param string $pattern_name Pattern name including namespace.
 136       * @return bool True if the pattern is registered, false otherwise.
 137       */
 138  	public function is_registered( $pattern_name ) {
 139          return isset( $this->registered_patterns[ $pattern_name ] );
 140      }
 141  
 142      /**
 143       * Utility method to retrieve the main instance of the class.
 144       *
 145       * The instance will be created if it does not exist yet.
 146       *
 147       * @since 5.5.0
 148       *
 149       * @return WP_Block_Patterns_Registry The main instance.
 150       */
 151  	public static function get_instance() {
 152          if ( null === self::$instance ) {
 153              self::$instance = new self();
 154          }
 155  
 156          return self::$instance;
 157      }
 158  }
 159  
 160  /**
 161   * Registers a new pattern.
 162   *
 163   * @since 5.5.0
 164   *
 165   * @param string $pattern_name       Pattern name including namespace.
 166   * @param array  $pattern_properties Array containing the properties of the pattern.
 167   * @return bool True if the pattern was registered with success and false otherwise.
 168   */
 169  function register_block_pattern( $pattern_name, $pattern_properties ) {
 170      return WP_Block_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties );
 171  }
 172  
 173  /**
 174   * Unregisters a pattern.
 175   *
 176   * @since 5.5.0
 177   *
 178   * @param string $pattern_name Pattern name including namespace.
 179   * @return bool True if the pattern was unregistered with success and false otherwise.
 180   */
 181  function unregister_block_pattern( $pattern_name ) {
 182      return WP_Block_Patterns_Registry::get_instance()->unregister( $pattern_name );
 183  }


Generated: Tue Sep 28 01:00:02 2021 Cross-referenced by PHPXref 0.7.1