@@ -81,7 +81,7 @@ |
||
| 81 | 81 | */ |
| 82 | 82 | public function namespacedBlockType() |
| 83 | 83 | { |
| 84 | - return self::NAME_SPACE . '/' . $this->block_type; |
|
| 84 | + return self::NAME_SPACE.'/'.$this->block_type; |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | |
@@ -20,216 +20,216 @@ |
||
| 20 | 20 | abstract class Block implements BlockInterface |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * BlockAssetManager that this editor block uses for asset registration |
|
| 25 | - * |
|
| 26 | - * @var BlockAssetManagerInterface $block_asset_manager |
|
| 27 | - */ |
|
| 28 | - protected $block_asset_manager; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var array $attributes |
|
| 32 | - */ |
|
| 33 | - private $attributes; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * If set to true, then the block will render its content client side |
|
| 37 | - * If false, then the block will render its content server side using the renderBlock() method |
|
| 38 | - * |
|
| 39 | - * @var bool $dynamic |
|
| 40 | - */ |
|
| 41 | - private $dynamic = false; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var string $block_type |
|
| 45 | - */ |
|
| 46 | - private $block_type; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @var array $supported_post_types |
|
| 50 | - */ |
|
| 51 | - private $supported_post_types; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var WP_Block_Type $wp_block_type |
|
| 55 | - */ |
|
| 56 | - private $wp_block_type; |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * BlockLoader constructor. |
|
| 61 | - * |
|
| 62 | - * @param BlockAssetManagerInterface $block_asset_manager |
|
| 63 | - */ |
|
| 64 | - public function __construct(BlockAssetManagerInterface $block_asset_manager) |
|
| 65 | - { |
|
| 66 | - $this->block_asset_manager = $block_asset_manager; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @return string |
|
| 72 | - */ |
|
| 73 | - public function blockType() |
|
| 74 | - { |
|
| 75 | - return $this->block_type; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @return string |
|
| 81 | - */ |
|
| 82 | - public function namespacedBlockType() |
|
| 83 | - { |
|
| 84 | - return self::NAME_SPACE . '/' . $this->block_type; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param string $block_type |
|
| 90 | - */ |
|
| 91 | - protected function setBlockType($block_type) |
|
| 92 | - { |
|
| 93 | - $this->block_type = $block_type; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * BlockAssetManager that this editor block uses for asset registration |
|
| 99 | - * |
|
| 100 | - * @return BlockAssetManagerInterface |
|
| 101 | - */ |
|
| 102 | - public function assetManager() |
|
| 103 | - { |
|
| 104 | - return $this->block_asset_manager; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @param WP_Block_Type $wp_block_type |
|
| 110 | - */ |
|
| 111 | - protected function setWpBlockType($wp_block_type) |
|
| 112 | - { |
|
| 113 | - $this->wp_block_type = $wp_block_type; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @param array $supported_post_types |
|
| 119 | - */ |
|
| 120 | - protected function setSupportedPostTypes(array $supported_post_types) |
|
| 121 | - { |
|
| 122 | - $this->supported_post_types = $supported_post_types; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @return array |
|
| 128 | - */ |
|
| 129 | - public function attributes() |
|
| 130 | - { |
|
| 131 | - return $this->attributes; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * @param array $attributes |
|
| 137 | - */ |
|
| 138 | - public function setAttributes(array $attributes) |
|
| 139 | - { |
|
| 140 | - $this->attributes = $attributes; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @return bool |
|
| 146 | - */ |
|
| 147 | - public function isDynamic() |
|
| 148 | - { |
|
| 149 | - return $this->dynamic; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @param bool $dynamic |
|
| 155 | - */ |
|
| 156 | - public function setDynamic($dynamic = true) |
|
| 157 | - { |
|
| 158 | - $this->dynamic = filter_var($dynamic, FILTER_VALIDATE_BOOLEAN); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Registers the Editor Block with WP core; |
|
| 164 | - * Returns the registered block type on success, or false on failure. |
|
| 165 | - * |
|
| 166 | - * @return WP_Block_Type|false |
|
| 167 | - */ |
|
| 168 | - public function registerBlock() |
|
| 169 | - { |
|
| 170 | - $args = array( |
|
| 171 | - 'attributes' => $this->attributes(), |
|
| 172 | - 'editor_script' => $this->block_asset_manager->getEditorScriptHandle(), |
|
| 173 | - 'editor_style' => $this->block_asset_manager->getEditorStyleHandle(), |
|
| 174 | - 'script' => $this->block_asset_manager->getScriptHandle(), |
|
| 175 | - 'style' => $this->block_asset_manager->getStyleHandle(), |
|
| 176 | - ); |
|
| 177 | - if ($this->isDynamic()) { |
|
| 178 | - $args['render_callback'] = array($this, 'renderBlock'); |
|
| 179 | - } |
|
| 180 | - $wp_block_type = register_block_type( |
|
| 181 | - new WP_Block_Type( |
|
| 182 | - $this->namespacedBlockType(), |
|
| 183 | - $args |
|
| 184 | - ) |
|
| 185 | - ); |
|
| 186 | - $this->setWpBlockType($wp_block_type); |
|
| 187 | - return $wp_block_type; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @return WP_Block_Type|false The registered block type on success, or false on failure. |
|
| 193 | - */ |
|
| 194 | - public function unRegisterBlock() |
|
| 195 | - { |
|
| 196 | - return unregister_block_type($this->namespacedBlockType()); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * returns true if the block type applies for the supplied post type |
|
| 202 | - * and should be added to that post type's editor |
|
| 203 | - * |
|
| 204 | - * @param string $post_type |
|
| 205 | - * @return boolean |
|
| 206 | - */ |
|
| 207 | - public function appliesToPostType($post_type) |
|
| 208 | - { |
|
| 209 | - return in_array($post_type, $this->supported_post_types, true); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * @return array |
|
| 215 | - */ |
|
| 216 | - public function getEditorContainer() |
|
| 217 | - { |
|
| 218 | - return array( |
|
| 219 | - $this->namespacedBlockType(), |
|
| 220 | - array(), |
|
| 221 | - ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * returns the rendered HTML for the block |
|
| 227 | - * |
|
| 228 | - * @param array $attributes |
|
| 229 | - * @return string |
|
| 230 | - */ |
|
| 231 | - public function renderBlock(array $attributes = array()) |
|
| 232 | - { |
|
| 233 | - return ''; |
|
| 234 | - } |
|
| 23 | + /** |
|
| 24 | + * BlockAssetManager that this editor block uses for asset registration |
|
| 25 | + * |
|
| 26 | + * @var BlockAssetManagerInterface $block_asset_manager |
|
| 27 | + */ |
|
| 28 | + protected $block_asset_manager; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var array $attributes |
|
| 32 | + */ |
|
| 33 | + private $attributes; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * If set to true, then the block will render its content client side |
|
| 37 | + * If false, then the block will render its content server side using the renderBlock() method |
|
| 38 | + * |
|
| 39 | + * @var bool $dynamic |
|
| 40 | + */ |
|
| 41 | + private $dynamic = false; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var string $block_type |
|
| 45 | + */ |
|
| 46 | + private $block_type; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @var array $supported_post_types |
|
| 50 | + */ |
|
| 51 | + private $supported_post_types; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var WP_Block_Type $wp_block_type |
|
| 55 | + */ |
|
| 56 | + private $wp_block_type; |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * BlockLoader constructor. |
|
| 61 | + * |
|
| 62 | + * @param BlockAssetManagerInterface $block_asset_manager |
|
| 63 | + */ |
|
| 64 | + public function __construct(BlockAssetManagerInterface $block_asset_manager) |
|
| 65 | + { |
|
| 66 | + $this->block_asset_manager = $block_asset_manager; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @return string |
|
| 72 | + */ |
|
| 73 | + public function blockType() |
|
| 74 | + { |
|
| 75 | + return $this->block_type; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @return string |
|
| 81 | + */ |
|
| 82 | + public function namespacedBlockType() |
|
| 83 | + { |
|
| 84 | + return self::NAME_SPACE . '/' . $this->block_type; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param string $block_type |
|
| 90 | + */ |
|
| 91 | + protected function setBlockType($block_type) |
|
| 92 | + { |
|
| 93 | + $this->block_type = $block_type; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * BlockAssetManager that this editor block uses for asset registration |
|
| 99 | + * |
|
| 100 | + * @return BlockAssetManagerInterface |
|
| 101 | + */ |
|
| 102 | + public function assetManager() |
|
| 103 | + { |
|
| 104 | + return $this->block_asset_manager; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param WP_Block_Type $wp_block_type |
|
| 110 | + */ |
|
| 111 | + protected function setWpBlockType($wp_block_type) |
|
| 112 | + { |
|
| 113 | + $this->wp_block_type = $wp_block_type; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @param array $supported_post_types |
|
| 119 | + */ |
|
| 120 | + protected function setSupportedPostTypes(array $supported_post_types) |
|
| 121 | + { |
|
| 122 | + $this->supported_post_types = $supported_post_types; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @return array |
|
| 128 | + */ |
|
| 129 | + public function attributes() |
|
| 130 | + { |
|
| 131 | + return $this->attributes; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * @param array $attributes |
|
| 137 | + */ |
|
| 138 | + public function setAttributes(array $attributes) |
|
| 139 | + { |
|
| 140 | + $this->attributes = $attributes; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @return bool |
|
| 146 | + */ |
|
| 147 | + public function isDynamic() |
|
| 148 | + { |
|
| 149 | + return $this->dynamic; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @param bool $dynamic |
|
| 155 | + */ |
|
| 156 | + public function setDynamic($dynamic = true) |
|
| 157 | + { |
|
| 158 | + $this->dynamic = filter_var($dynamic, FILTER_VALIDATE_BOOLEAN); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Registers the Editor Block with WP core; |
|
| 164 | + * Returns the registered block type on success, or false on failure. |
|
| 165 | + * |
|
| 166 | + * @return WP_Block_Type|false |
|
| 167 | + */ |
|
| 168 | + public function registerBlock() |
|
| 169 | + { |
|
| 170 | + $args = array( |
|
| 171 | + 'attributes' => $this->attributes(), |
|
| 172 | + 'editor_script' => $this->block_asset_manager->getEditorScriptHandle(), |
|
| 173 | + 'editor_style' => $this->block_asset_manager->getEditorStyleHandle(), |
|
| 174 | + 'script' => $this->block_asset_manager->getScriptHandle(), |
|
| 175 | + 'style' => $this->block_asset_manager->getStyleHandle(), |
|
| 176 | + ); |
|
| 177 | + if ($this->isDynamic()) { |
|
| 178 | + $args['render_callback'] = array($this, 'renderBlock'); |
|
| 179 | + } |
|
| 180 | + $wp_block_type = register_block_type( |
|
| 181 | + new WP_Block_Type( |
|
| 182 | + $this->namespacedBlockType(), |
|
| 183 | + $args |
|
| 184 | + ) |
|
| 185 | + ); |
|
| 186 | + $this->setWpBlockType($wp_block_type); |
|
| 187 | + return $wp_block_type; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @return WP_Block_Type|false The registered block type on success, or false on failure. |
|
| 193 | + */ |
|
| 194 | + public function unRegisterBlock() |
|
| 195 | + { |
|
| 196 | + return unregister_block_type($this->namespacedBlockType()); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * returns true if the block type applies for the supplied post type |
|
| 202 | + * and should be added to that post type's editor |
|
| 203 | + * |
|
| 204 | + * @param string $post_type |
|
| 205 | + * @return boolean |
|
| 206 | + */ |
|
| 207 | + public function appliesToPostType($post_type) |
|
| 208 | + { |
|
| 209 | + return in_array($post_type, $this->supported_post_types, true); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * @return array |
|
| 215 | + */ |
|
| 216 | + public function getEditorContainer() |
|
| 217 | + { |
|
| 218 | + return array( |
|
| 219 | + $this->namespacedBlockType(), |
|
| 220 | + array(), |
|
| 221 | + ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * returns the rendered HTML for the block |
|
| 227 | + * |
|
| 228 | + * @param array $attributes |
|
| 229 | + * @return string |
|
| 230 | + */ |
|
| 231 | + public function renderBlock(array $attributes = array()) |
|
| 232 | + { |
|
| 233 | + return ''; |
|
| 234 | + } |
|
| 235 | 235 | } |