[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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( __METHOD__, __( 'Pattern name must be a string.' ), '5.5.0' );
  45              return false;
  46          }
  47  
  48          if ( ! isset( $pattern_properties['title'] ) || ! is_string( $pattern_properties['title'] ) ) {
  49              _doing_it_wrong( __METHOD__, __( 'Pattern title must be a string.' ), '5.5.0' );
  50              return false;
  51          }
  52  
  53          if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) {
  54              _doing_it_wrong( __METHOD__, __( 'Pattern content must be a string.' ), '5.5.0' );
  55              return false;
  56          }
  57  
  58          $this->registered_patterns[ $pattern_name ] = array_merge(
  59              $pattern_properties,
  60              array( 'name' => $pattern_name )
  61          );
  62  
  63          return true;
  64      }
  65  
  66      /**
  67       * Unregisters a pattern.
  68       *
  69       * @since 5.5.0
  70       *
  71       * @param string $pattern_name Pattern name including namespace.
  72       * @return bool True if the pattern was unregistered with success and false otherwise.
  73       */
  74  	public function unregister( $pattern_name ) {
  75          if ( ! $this->is_registered( $pattern_name ) ) {
  76              /* translators: 1: Pattern name. */
  77              $message = sprintf( __( 'Pattern "%1$s" not found.' ), $pattern_name );
  78              _doing_it_wrong( __METHOD__, $message, '5.5.0' );
  79              return false;
  80          }
  81  
  82          unset( $this->registered_patterns[ $pattern_name ] );
  83  
  84          return true;
  85      }
  86  
  87      /**
  88       * Retrieves an array containing the properties of a registered pattern.
  89       *
  90       * @since 5.5.0
  91       *
  92       * @param string $pattern_name Pattern name including namespace.
  93       * @return array Registered pattern properties.
  94       */
  95  	public function get_registered( $pattern_name ) {
  96          if ( ! $this->is_registered( $pattern_name ) ) {
  97              return null;
  98          }
  99  
 100          return $this->registered_patterns[ $pattern_name ];
 101      }
 102  
 103      /**
 104       * Retrieves all registered patterns.
 105       *
 106       * @since 5.5.0
 107       *
 108       * @return array Array of arrays containing the registered patterns properties,
 109       *               and per style.
 110       */
 111  	public function get_all_registered() {
 112          return array_values( $this->registered_patterns );
 113      }
 114  
 115      /**
 116       * Checks if a pattern is registered.
 117       *
 118       * @since 5.5.0
 119       *
 120       * @param string $pattern_name Pattern name including namespace.
 121       * @return bool True if the pattern is registered, false otherwise.
 122       */
 123  	public function is_registered( $pattern_name ) {
 124          return isset( $this->registered_patterns[ $pattern_name ] );
 125      }
 126  
 127      /**
 128       * Utility method to retrieve the main instance of the class.
 129       *
 130       * The instance will be created if it does not exist yet.
 131       *
 132       * @since 5.5.0
 133       *
 134       * @return WP_Block_Patterns_Registry The main instance.
 135       */
 136  	public static function get_instance() {
 137          if ( null === self::$instance ) {
 138              self::$instance = new self();
 139          }
 140  
 141          return self::$instance;
 142      }
 143  }
 144  
 145  /**
 146   * Registers a new pattern.
 147   *
 148   * @since 5.5.0
 149   *
 150   * @param string $pattern_name       Pattern name including namespace.
 151   * @param array  $pattern_properties Array containing the properties of the pattern.
 152   * @return bool True if the pattern was registered with success and false otherwise.
 153   */
 154  function register_block_pattern( $pattern_name, $pattern_properties ) {
 155      return WP_Block_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties );
 156  }
 157  
 158  /**
 159   * Unregisters a pattern.
 160   *
 161   * @since 5.5.0
 162   *
 163   * @param string $pattern_name       Pattern name including namespace.
 164   * @return bool True if the pattern was unregistered with success and false otherwise.
 165   */
 166  function unregister_block_pattern( $pattern_name ) {
 167      return WP_Block_Patterns_Registry::get_instance()->unregister( $pattern_name );
 168  }


Generated : Sat Oct 31 08:20:01 2020 Cross-referenced by PHPXref