| 1 |
<?php |
| 2 |
namespace YayMail\Abstracts; |
| 3 |
|
| 4 |
/** |
| 5 |
* BasePattern Class |
| 6 |
*/ |
| 7 |
abstract class BasePattern { |
| 8 |
protected $id = null; |
| 9 |
public const TYPE = ''; |
| 10 |
|
| 11 |
protected $section = null; |
| 12 |
|
| 13 |
protected $available = true; |
| 14 |
protected $position = 10; |
| 15 |
protected $name = ''; |
| 16 |
protected $elements = []; |
| 17 |
|
| 18 |
public function get_data() { |
| 19 |
return [ |
| 20 |
'id' => $this->id, |
| 21 |
'type' => static::TYPE, |
| 22 |
'section' => $this->section, |
| 23 |
'available' => $this->available, |
| 24 |
'position' => $this->position, |
| 25 |
'elements' => $this->elements, |
| 26 |
'name' => $this->name, |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_type() { |
| 31 |
return static::TYPE; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
public function get_section() { |
| 38 |
return $this->section; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Element keys only the sidebar element registry reads (builder elements list). |
| 43 |
* Elements placed from a pattern never use them, yet the same "Third party |
| 44 |
* elements" SVG icon was repeated on every nested element, ~150KB of the inline |
| 45 |
* yaymailData script. `name` is kept: the element editor and history labels use it. |
| 46 |
*/ |
| 47 |
private const SIDEBAR_ONLY_ELEMENT_KEYS = [ 'icon', 'group' ]; |
| 48 |
|
| 49 |
public function get_raw_data() { |
| 50 |
$data = $this->get_data(); |
| 51 |
$data['elements'] = self::strip_sidebar_only_keys( $data['elements'] ); |
| 52 |
return $data; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Recursively drop SIDEBAR_ONLY_ELEMENT_KEYS from elements and their children. |
| 57 |
* |
| 58 |
* @param array $elements Pattern elements. |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
private static function strip_sidebar_only_keys( $elements ) { |
| 62 |
if ( ! is_array( $elements ) ) { |
| 63 |
return $elements; |
| 64 |
} |
| 65 |
|
| 66 |
return array_map( |
| 67 |
function( $element ) { |
| 68 |
if ( ! is_array( $element ) ) { |
| 69 |
return $element; |
| 70 |
} |
| 71 |
$element = array_diff_key( $element, array_flip( self::SIDEBAR_ONLY_ELEMENT_KEYS ) ); |
| 72 |
if ( ! empty( $element['children'] ) ) { |
| 73 |
$element['children'] = self::strip_sidebar_only_keys( $element['children'] ); |
| 74 |
} |
| 75 |
return $element; |
| 76 |
}, |
| 77 |
$elements |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
} |
| 82 |
|