| 1 |
<?php |
| 2 |
/** |
| 3 |
* Blocks Style Manager |
| 4 |
* |
| 5 |
* Version: 2.1.22 |
| 6 |
*/ |
| 7 |
namespace Crocoblock\Blocks_Style; |
| 8 |
|
| 9 |
class Manager { |
| 10 |
|
| 11 |
/** |
| 12 |
* Module directory path. |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
* @access protected |
| 16 |
* @var srting. |
| 17 |
*/ |
| 18 |
protected $path; |
| 19 |
|
| 20 |
/** |
| 21 |
* Module directory URL. |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
* @access protected |
| 25 |
* @var srting. |
| 26 |
*/ |
| 27 |
protected $url; |
| 28 |
|
| 29 |
/** |
| 30 |
* Module version |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $version = '2.1.22'; |
| 35 |
|
| 36 |
public static $migrator_registered = false; |
| 37 |
|
| 38 |
public static $requires_migration = false; |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor |
| 42 |
* |
| 43 |
* @param array $args Arguments. |
| 44 |
*/ |
| 45 |
public function __construct( $args = array() ) { |
| 46 |
|
| 47 |
$this->path = $args['path']; |
| 48 |
$this->url = $args['url']; |
| 49 |
require_once $this->path . 'path-url-trait.php'; |
| 50 |
|
| 51 |
if ( $this->is_migrator_enabled() ) { |
| 52 |
|
| 53 |
if ( ! class_exists( '\Crocoblock\Blocks_Style\Migrator\UI' ) ) { |
| 54 |
require_once $this->path . 'migrator/ui.php'; |
| 55 |
} |
| 56 |
|
| 57 |
new Migrator\UI(); |
| 58 |
self::$migrator_registered = true; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Check if migrator is enabled. |
| 64 |
* |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
public function is_migrator_enabled() { |
| 68 |
return ! self::$migrator_registered && self::$requires_migration; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Register new block to supported blocks registry. |
| 73 |
* |
| 74 |
* Should be called before after init hook but wp_loaded hook with 999 priority. |
| 75 |
* |
| 76 |
* @since 1.0.0 |
| 77 |
* @access public |
| 78 |
* @param string $block_name Block name. |
| 79 |
* @param array $args Style-manager related arguments. |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function register_block_support( $block_name = '', $args = array() ) { |
| 83 |
|
| 84 |
if ( ! $block_name ) { |
| 85 |
_doing_it_wrong( |
| 86 |
'Crocoblock\Blocks_Style\Manager::register_block_support', |
| 87 |
'Block name is required.', |
| 88 |
$this->version |
| 89 |
); |
| 90 |
|
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$this->ensure_block_registry(); |
| 95 |
Registry::instance()->register_block( $block_name, $args ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Start collecting styles for the given blocks set. |
| 100 |
* Collected styles could be get/printed at once. |
| 101 |
* |
| 102 |
* @param string $name |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function start_collection( $name ) { |
| 106 |
$this->ensure_block_registry(); |
| 107 |
Style_Inserter::set_collection( $name ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Start collecting styles for the given blocks set. |
| 112 |
* Collected styles could be get/printed at once. |
| 113 |
* |
| 114 |
* @param string $name |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public function stop_current_collection() { |
| 118 |
$this->ensure_block_registry(); |
| 119 |
Style_Inserter::set_collection( null ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Get styles collection by name. |
| 124 |
* |
| 125 |
* @param string|null $name |
| 126 |
* @return string |
| 127 |
*/ |
| 128 |
public function get_styles_collection( $name = null ) { |
| 129 |
$this->ensure_block_registry(); |
| 130 |
return Style_Inserter::get_styles_collection( $name ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get proxy class instance. |
| 135 |
* |
| 136 |
* @param string $block_name |
| 137 |
* @return Proxy|false |
| 138 |
*/ |
| 139 |
public function get_proxy( $block_name = '' ) { |
| 140 |
|
| 141 |
$this->ensure_block_registry(); |
| 142 |
$block = Registry::instance()->get_block( $block_name ); |
| 143 |
|
| 144 |
if ( ! $block ) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
if ( ! class_exists( '\Crocoblock\Blocks_Style\Proxy' ) ) { |
| 149 |
require_once $this->path . 'proxy.php'; |
| 150 |
} |
| 151 |
|
| 152 |
return new Proxy( $block ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get block instance by name. |
| 157 |
* |
| 158 |
* @param string $block_name |
| 159 |
* @return Block|false |
| 160 |
*/ |
| 161 |
public function get_block( $block_name = '' ) { |
| 162 |
$this->ensure_block_registry(); |
| 163 |
return Registry::instance()->get_block( $block_name ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Allow to manually enqueue block editor assets |
| 168 |
* |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public function enqueue_editor_assets() { |
| 172 |
|
| 173 |
if ( ! class_exists( '\Crocoblock\Blocks_Style\Editor' ) ) { |
| 174 |
require_once $this->path . 'editor.php'; |
| 175 |
} |
| 176 |
|
| 177 |
Editor::instance() |
| 178 |
->set_url( $this->url ) |
| 179 |
->set_path( $this->path ) |
| 180 |
->editor_assets(); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Ensure block registry is initialized. |
| 185 |
* |
| 186 |
* @since 1.0.0 |
| 187 |
* @access protected |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
protected function ensure_block_registry() { |
| 191 |
if ( ! class_exists( '\Crocoblock\Blocks_Style\Registry' ) ) { |
| 192 |
require_once $this->path . 'registry.php'; |
| 193 |
require_once $this->path . 'style-inserter.php'; |
| 194 |
|
| 195 |
// Must be always called before registering the 1st block. |
| 196 |
Registry::instance()->set_path( $this->path )->set_url( $this->url ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
} |
| 201 |
|