| 1 |
<?php |
| 2 |
/** |
| 3 |
* Blocks Styles registry |
| 4 |
*/ |
| 5 |
namespace Crocoblock\Blocks_Style; |
| 6 |
|
| 7 |
class Registry { |
| 8 |
|
| 9 |
use Path_Url_Trait; |
| 10 |
|
| 11 |
/** |
| 12 |
* Registry instance |
| 13 |
* |
| 14 |
* @var object |
| 15 |
*/ |
| 16 |
protected static $instance = null; |
| 17 |
|
| 18 |
/** |
| 19 |
* Block styles registry |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
protected $registry = array(); |
| 24 |
|
| 25 |
/** |
| 26 |
* Handlers initialized state |
| 27 |
* |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
protected $handles_inited = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor |
| 34 |
*/ |
| 35 |
public function __construct() {} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get instance of the class |
| 39 |
* |
| 40 |
* @return object |
| 41 |
*/ |
| 42 |
public static function instance() { |
| 43 |
|
| 44 |
if ( null === self::$instance ) { |
| 45 |
self::$instance = new self(); |
| 46 |
} |
| 47 |
|
| 48 |
return self::$instance; |
| 49 |
} |
| 50 |
/** |
| 51 |
* Register new block to supported blocks registry |
| 52 |
* |
| 53 |
* @param string $block_name Block name. |
| 54 |
* @param array $args Style-manager related arguments. |
| 55 |
*/ |
| 56 |
public function register_block( $block_name, $args = array() ) { |
| 57 |
|
| 58 |
add_filter( |
| 59 |
'register_block_type_args', |
| 60 |
function( $block_args, $block_type ) use ( $block_name, $args ) { |
| 61 |
|
| 62 |
if ( $block_type !== $block_name ) { |
| 63 |
return $block_args; |
| 64 |
} |
| 65 |
|
| 66 |
if ( empty( $block_args['supports'] ) ) { |
| 67 |
$block_args['supports'] = array(); |
| 68 |
} |
| 69 |
|
| 70 |
$block_args['supports'][ $this->get_support_name() ] = true; |
| 71 |
|
| 72 |
// Ensure attributes array exists. |
| 73 |
if ( empty( $block_args['attributes'] ) ) { |
| 74 |
$block_args['attributes'] = array(); |
| 75 |
} |
| 76 |
|
| 77 |
if ( empty( $block_args['attributes'][ $this->get_support_name() ] ) ) { |
| 78 |
$defaults = array( |
| 79 |
'_uniqueClassName' => '', |
| 80 |
); |
| 81 |
|
| 82 |
$block_args['attributes'][ $this->get_support_name() ] = array( |
| 83 |
'type' => 'object', |
| 84 |
'default' => $defaults, |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
return $block_args; |
| 89 |
}, |
| 90 |
10, 2 |
| 91 |
); |
| 92 |
|
| 93 |
if ( ! class_exists( 'Crocoblock\Blocks_Style\Block' ) ) { |
| 94 |
require_once $this->get_path( 'style-cache.php' ); |
| 95 |
require_once $this->get_path( 'style-engine.php' ); |
| 96 |
require_once $this->get_path( 'block.php' ); |
| 97 |
} |
| 98 |
|
| 99 |
// Also, when first block is registered we need to load editor ans style render engine |
| 100 |
if ( ! $this->handles_inited ) { |
| 101 |
|
| 102 |
$this->handles_inited = true; |
| 103 |
|
| 104 |
if ( ! class_exists( '\Crocoblock\Blocks_Style\Editor' ) ) { |
| 105 |
require_once $this->get_path( 'editor.php' ); |
| 106 |
} |
| 107 |
|
| 108 |
Editor::instance()->set_url( $this->url )->init(); |
| 109 |
} |
| 110 |
|
| 111 |
if ( ! isset( $this->registry[ $block_name ] ) ) { |
| 112 |
$this->registry[ $block_name ] = new Block( $block_name, $args ); |
| 113 |
} else { |
| 114 |
$this->registry[ $block_name ]->set_attributes( $args ); |
| 115 |
} |
| 116 |
|
| 117 |
return $this->registry[ $block_name ]; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get blocks support name |
| 122 |
* |
| 123 |
* @return string |
| 124 |
*/ |
| 125 |
public function get_support_name() { |
| 126 |
return 'crocoblock_styles'; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get block instance by name |
| 131 |
* |
| 132 |
* @param string $block_name |
| 133 |
* @return Block|false |
| 134 |
*/ |
| 135 |
public function get_block( $block_name = '' ) { |
| 136 |
|
| 137 |
if ( empty( $block_name ) ) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
if ( ! isset( $this->registry[ $block_name ] ) ) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
return $this->registry[ $block_name ]; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Apply block support |
| 150 |
* |
| 151 |
* @param \WP_Block_Type $block_type |
| 152 |
* @param array $block_attributes |
| 153 |
* @return array |
| 154 |
*/ |
| 155 |
public function apply_support( $block_type, array $block_attributes ): array { |
| 156 |
return array(); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Get all registered blocks |
| 161 |
* |
| 162 |
* @return array |
| 163 |
*/ |
| 164 |
public function get_blocks() { |
| 165 |
return $this->registry; |
| 166 |
} |
| 167 |
} |
| 168 |
|