| 1 |
<?php |
| 2 |
namespace Crocoblock\Blocks_Style; |
| 3 |
|
| 4 |
class Editor { |
| 5 |
|
| 6 |
use Path_Url_Trait; |
| 7 |
|
| 8 |
/** |
| 9 |
* Is editor initialized |
| 10 |
* |
| 11 |
* @var boolean |
| 12 |
*/ |
| 13 |
protected $inited = false; |
| 14 |
|
| 15 |
/** |
| 16 |
* Editor instance |
| 17 |
* |
| 18 |
* @var object |
| 19 |
*/ |
| 20 |
protected static $instance = null; |
| 21 |
|
| 22 |
protected $defaults = array(); |
| 23 |
|
| 24 |
/** |
| 25 |
* Get instance of the class |
| 26 |
* |
| 27 |
* @return Editor |
| 28 |
*/ |
| 29 |
public static function instance() { |
| 30 |
if ( null === self::$instance ) { |
| 31 |
self::$instance = new self(); |
| 32 |
} |
| 33 |
|
| 34 |
return self::$instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Initialize editor |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function init() { |
| 43 |
|
| 44 |
if ( $this->inited ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
$this->inited = true; |
| 49 |
add_action( 'enqueue_block_editor_assets', array( $this, 'editor_assets' ), -10 ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Enqueue editor assets |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function editor_assets() { |
| 58 |
|
| 59 |
$asset_file = $this->get_path( 'assets/build/editor.asset.php' ); |
| 60 |
$js_file = $this->get_path( 'assets/build/editor.js' ); |
| 61 |
$js_url = $this->get_url( 'assets/build/editor.js' ); |
| 62 |
|
| 63 |
if ( ! file_exists( $js_file ) || ! file_exists( $asset_file ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$asset = include $asset_file; |
| 68 |
|
| 69 |
wp_enqueue_script( |
| 70 |
'crocoblock-blocks-style-editor', |
| 71 |
$js_url, |
| 72 |
$asset['dependencies'], |
| 73 |
$asset['version'], |
| 74 |
true |
| 75 |
); |
| 76 |
|
| 77 |
$colors_manager_url = esc_url( admin_url( 'site-editor.php?p=/styles§ion=/colors/palette' ) ); |
| 78 |
|
| 79 |
$fonts_manager_url = esc_url( admin_url( 'site-editor.php?p=/styles§ion=/typography' ) ); |
| 80 |
$settings = wp_get_global_settings(); |
| 81 |
$fonts = []; |
| 82 |
|
| 83 |
if ( ! empty( $settings['typography']['fontFamilies'] ) ) { |
| 84 |
foreach ( $settings['typography']['fontFamilies'] as $fonts_set ) { |
| 85 |
if ( is_array( $fonts_set ) ) { |
| 86 |
foreach ( $fonts_set as $font ) { |
| 87 |
if ( ! empty( $font['name'] ) && ! empty( $font['fontFamily'] ) ) { |
| 88 |
$fonts[] = [ |
| 89 |
'value' => $font['fontFamily'], |
| 90 |
'label' => $font['name'], |
| 91 |
]; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Note! |
| 100 |
* |
| 101 |
* get_blocks_supports() always must be called before get_block_defaults() |
| 102 |
* because it internally calls extract_children(), |
| 103 |
* which extract also defaults of these children. |
| 104 |
*/ |
| 105 |
$supports = $this->get_blocks_supports(); |
| 106 |
$defaults = $this->get_block_defaults(); |
| 107 |
|
| 108 |
wp_localize_script( |
| 109 |
'crocoblock-blocks-style-editor', |
| 110 |
'crocoStyleEditorData', |
| 111 |
array( |
| 112 |
'blocks_supports' => $supports, |
| 113 |
'support_name' => Registry::instance()->get_support_name(), |
| 114 |
'defaults' => $defaults, |
| 115 |
'class_prefix' => 'cb-', |
| 116 |
'fonts_manager_url' => $fonts_manager_url, |
| 117 |
'colors_manager_url' => $colors_manager_url, |
| 118 |
'global_settings' => $settings, |
| 119 |
'fonts' => $fonts, |
| 120 |
) |
| 121 |
); |
| 122 |
|
| 123 |
$rtl_suffix = is_rtl() ? '-rtl' : ''; |
| 124 |
|
| 125 |
wp_enqueue_style( |
| 126 |
'crocoblock-blocks-style-editor', |
| 127 |
$this->get_url( 'assets/build/editor' . $rtl_suffix . '.css' ), |
| 128 |
array(), |
| 129 |
$asset['version'] |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get blocks supports |
| 135 |
* |
| 136 |
* @return array |
| 137 |
*/ |
| 138 |
public function get_blocks_supports() { |
| 139 |
|
| 140 |
$result = array(); |
| 141 |
$blocks = Registry::instance()->get_blocks(); |
| 142 |
|
| 143 |
foreach ( $blocks as $block ) { |
| 144 |
|
| 145 |
$this->defaults[ $block->get_block_name() ] = $block->get_defaults(); |
| 146 |
|
| 147 |
$result[ $block->get_block_name() ] = $this->extract_children( |
| 148 |
$block->get_controls_stack() |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
return $result; |
| 153 |
} |
| 154 |
|
| 155 |
public function get_block_defaults() { |
| 156 |
|
| 157 |
$result = array(); |
| 158 |
$blocks = Registry::instance()->get_blocks(); |
| 159 |
|
| 160 |
foreach ( $blocks as $block ) { |
| 161 |
$result[ $block->get_block_name() ] = $block->get_defaults(); |
| 162 |
} |
| 163 |
|
| 164 |
return $result; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Recursively convert children prop of ech control (if exists) |
| 169 |
* to indexed array instead of associative array to process in JS as array |
| 170 |
* and not as object, because we can't keep order of object properties in JS |
| 171 |
* (they always be sorted alphabetically). |
| 172 |
* |
| 173 |
* @param array $controls Controls stack. |
| 174 |
* @return array |
| 175 |
*/ |
| 176 |
public function extract_children( array $controls = array() ) { |
| 177 |
|
| 178 |
$result = array(); |
| 179 |
|
| 180 |
foreach ( $controls as $control ) { |
| 181 |
|
| 182 |
if ( ! isset( $control['children'] ) ) { |
| 183 |
$result[] = $control; |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
$children = $this->extract_children( $control['children'] ); |
| 188 |
|
| 189 |
if ( ! empty( $children ) ) { |
| 190 |
$control['children'] = array_values( $children ); |
| 191 |
} |
| 192 |
|
| 193 |
$result[] = $control; |
| 194 |
} |
| 195 |
|
| 196 |
return $result; |
| 197 |
} |
| 198 |
} |
| 199 |
|