| 1 |
<?php |
| 2 |
/** |
| 3 |
* Proxy class to unify the access to the blocks style manager |
| 4 |
* and the legacy style manager plugin. |
| 5 |
*/ |
| 6 |
namespace Crocoblock\Blocks_Style; |
| 7 |
|
| 8 |
class Proxy { |
| 9 |
|
| 10 |
protected $block; |
| 11 |
protected $legacy_block_manager; |
| 12 |
protected $legacy_controls_manager; |
| 13 |
|
| 14 |
public static $is_migrated = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* Set the Block instance |
| 18 |
* |
| 19 |
* @param Block $block Block instance. |
| 20 |
*/ |
| 21 |
public function __construct( $block ) { |
| 22 |
|
| 23 |
$this->block = $block; |
| 24 |
|
| 25 |
if ( $this->has_legacy_style_manager() ) { |
| 26 |
$this->legacy_block_manager = \JET_SM\Gutenberg\Block_Manager::get_instance(); |
| 27 |
$this->legacy_controls_manager = new \JET_SM\Gutenberg\Controls_Manager( |
| 28 |
$this->block->get_block_name() |
| 29 |
); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get the Block instance |
| 35 |
* |
| 36 |
* @return Block |
| 37 |
*/ |
| 38 |
public function get_block() { |
| 39 |
return $this->block; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Check we has a legacy style manage plugin. |
| 44 |
* |
| 45 |
* @return boolean |
| 46 |
*/ |
| 47 |
public function has_legacy_style_manager() { |
| 48 |
|
| 49 |
if ( self::$is_migrated ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
self::$is_migrated = get_option( 'jet_sm_migration_completed', false ); |
| 54 |
|
| 55 |
return class_exists( '\JET_SM\Gutenberg\Block_Manager' ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Start a new section for block styles |
| 60 |
* |
| 61 |
* @param string $section_id |
| 62 |
* @param array $args |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function start_section( $section_id, $args = array() ) { |
| 66 |
|
| 67 |
if ( $this->has_legacy_style_manager() ) { |
| 68 |
$this->legacy_controls_manager->start_section( $section_id, $args ); |
| 69 |
} |
| 70 |
|
| 71 |
$this->block->start_section( $args ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* End the current section |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function end_section() { |
| 80 |
|
| 81 |
if ( $this->has_legacy_style_manager() ) { |
| 82 |
$this->legacy_controls_manager->end_section(); |
| 83 |
} |
| 84 |
|
| 85 |
$this->block->end_section(); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Add a new control to the current section |
| 90 |
* |
| 91 |
* @param array $args |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function add_control( $args = array() ) { |
| 95 |
|
| 96 |
if ( $this->has_legacy_style_manager() ) { |
| 97 |
|
| 98 |
$is_responsive = ! empty( $args['is_responsive'] ) && $args['is_responsive']; |
| 99 |
|
| 100 |
if ( ! empty( $args['legacy_css_selector'] ) ) { |
| 101 |
$args['css_selector'] = $args['legacy_css_selector']; |
| 102 |
unset( $args['legacy_css_selector'] ); |
| 103 |
} |
| 104 |
|
| 105 |
if ( $is_responsive ) { |
| 106 |
$this->legacy_controls_manager->add_responsive_control( $args ); |
| 107 |
} else { |
| 108 |
$this->legacy_controls_manager->add_control( $args ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if ( ! empty( $args['is_legacy'] ) ) { |
| 113 |
// If the control is marked as legacy, so do not add it |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
// rewrite default attribute |
| 118 |
if ( |
| 119 |
! empty( $args['attributes'] ) |
| 120 |
&& ! empty( $args['attributes']['default'] ) |
| 121 |
) { |
| 122 |
$raw_default = $args['attributes']['default']; |
| 123 |
$default = null; |
| 124 |
|
| 125 |
if ( is_array( $raw_default ) ) { |
| 126 |
if ( ! empty( $raw_default['value'] ) ) { |
| 127 |
if ( is_array( $raw_default['value'] ) ) { |
| 128 |
|
| 129 |
if ( isset( $raw_default['value']['value'] ) ) { |
| 130 |
$default = $raw_default['value']['value']; |
| 131 |
} |
| 132 |
|
| 133 |
if ( ! empty( $raw_default['value']['unit'] ) && null !== $default ) { |
| 134 |
$default .= $raw_default['value']['unit']; |
| 135 |
} |
| 136 |
} else { |
| 137 |
$default = (string) $raw_default['value']; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if ( ! empty( $raw_default['unit'] ) && null !== $default ) { |
| 142 |
$default .= $raw_default['unit']; |
| 143 |
} |
| 144 |
} else { |
| 145 |
$default = $raw_default; |
| 146 |
} |
| 147 |
|
| 148 |
if ( null !== $default ) { |
| 149 |
|
| 150 |
if ( ! empty( $args['return_value'] ) ) { |
| 151 |
$default = $this->find_value_by_return_value( $args['return_value'], $default ); |
| 152 |
} |
| 153 |
|
| 154 |
$args['default'] = $default; |
| 155 |
} |
| 156 |
|
| 157 |
unset( $args['attributes']['default'] ); |
| 158 |
} |
| 159 |
|
| 160 |
$this->block->add_control( $args ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Find the value by return value array |
| 165 |
* |
| 166 |
* @param array $return_value |
| 167 |
* @param mixed $default |
| 168 |
* @return mixed |
| 169 |
*/ |
| 170 |
protected function find_value_by_return_value( $return_value, $default ) { |
| 171 |
if ( is_array( $return_value ) ) { |
| 172 |
|
| 173 |
$key = $default; |
| 174 |
|
| 175 |
if ( is_bool( $key ) ) { |
| 176 |
$key = ( $key ) ? 'true' : 'false'; |
| 177 |
} elseif ( is_null( $key ) ) { |
| 178 |
$key = 'null'; |
| 179 |
} elseif ( is_array( $key ) ) { |
| 180 |
$key = json_encode( $key ); |
| 181 |
} else { |
| 182 |
$key = (string) $key; |
| 183 |
} |
| 184 |
|
| 185 |
return ( isset( $return_value[ $key ] ) ) ? $return_value[ $key ] : $default; |
| 186 |
} |
| 187 |
|
| 188 |
return $default; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Add a new control to the current section. |
| 193 |
* Fallback for legacy style manager. |
| 194 |
* |
| 195 |
* @param array $args |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public function add_responsive_control( $args = array() ) { |
| 199 |
$args['is_responsive'] = true; |
| 200 |
$this->add_control( $args ); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Start tabs group |
| 205 |
* |
| 206 |
* @param string $id |
| 207 |
* @param array $args |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
public function start_tabs( $id, $args = array() ) { |
| 211 |
$this->block->start_tabs( $args ); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Start a new tab |
| 216 |
* |
| 217 |
* @param string $id |
| 218 |
* @param array $args |
| 219 |
* @return void |
| 220 |
*/ |
| 221 |
public function start_tab( $id, $args = array() ) { |
| 222 |
$this->block->start_tab( $args ); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* End tabs group |
| 227 |
* |
| 228 |
* @return void |
| 229 |
*/ |
| 230 |
public function end_tabs() { |
| 231 |
$this->block->end_tabs(); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* End the current tab |
| 236 |
* |
| 237 |
* @return void |
| 238 |
*/ |
| 239 |
public function end_tab() { |
| 240 |
$this->block->end_tab(); |
| 241 |
} |
| 242 |
} |