base-meta-container.php
3 years ago
normal-meta-container.php
3 years ago
side-meta-container.php
3 years ago
base-meta-container.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Admin\Single_Pages\Meta_Containers; |
| 5 | |
| 6 | use Jet_Form_Builder\Admin\Exceptions\Empty_Box_Exception; |
| 7 | use Jet_Form_Builder\Admin\Single_Pages\Meta_Boxes\Base_Meta_Box; |
| 8 | use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 9 | use Jet_Form_Builder\Classes\Repository\Repository_Pattern_Trait; |
| 10 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 11 | |
| 12 | abstract class Base_Meta_Container implements Arrayable { |
| 13 | |
| 14 | use Repository_Pattern_Trait; |
| 15 | |
| 16 | const TYPE_NORMAL = 'normal-sortables'; |
| 17 | const TYPE_SIDE = 'side-sortables'; |
| 18 | |
| 19 | protected $index; |
| 20 | |
| 21 | public function get_type(): string { |
| 22 | return 1 !== $this->index ? self::TYPE_NORMAL : self::TYPE_SIDE; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @return string[] |
| 27 | */ |
| 28 | public function get_classes(): array { |
| 29 | return array( |
| 30 | 'meta-box-sortables', |
| 31 | 'ui-sortable', |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Base_Meta_Container constructor. |
| 37 | * |
| 38 | * @param Base_Meta_Box[] $meta_boxes |
| 39 | */ |
| 40 | public function __construct( ...$meta_boxes ) { |
| 41 | $this->rep_install( $meta_boxes ); |
| 42 | } |
| 43 | |
| 44 | public function rep_instances(): array { |
| 45 | return array(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @return Base_Meta_Box[] |
| 50 | */ |
| 51 | public function get_boxes(): array { |
| 52 | return $this->rep_get_values(); |
| 53 | } |
| 54 | |
| 55 | public function get_box( string $slug ): Base_Meta_Box { |
| 56 | return $this->rep_get_item_or_die( $slug ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @param string $scoped_slug |
| 61 | * |
| 62 | * @return Base_Meta_Box |
| 63 | * @throws Repository_Exception |
| 64 | */ |
| 65 | public function get_box_by_scope( string $scoped_slug ): Base_Meta_Box { |
| 66 | // cut the `scope-` |
| 67 | $slug = substr( $scoped_slug, 6, 0 ); |
| 68 | |
| 69 | foreach ( $this->get_boxes() as $box ) { |
| 70 | if ( $box->get_slug() === $slug ) { |
| 71 | return $box; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | throw new Repository_Exception( 'Undefined ' . $scoped_slug ); |
| 76 | } |
| 77 | |
| 78 | public function get_migrations(): \Generator { |
| 79 | foreach ( $this->get_boxes() as $box ) { |
| 80 | yield from $box->get_migrations(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param Base_Meta_Box $box |
| 86 | */ |
| 87 | public function add_meta_box( Base_Meta_Box $box ) { |
| 88 | $this->rep_install_item_soft( $box ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param Base_Meta_Box $item |
| 93 | * |
| 94 | * @throws Repository_Exception |
| 95 | */ |
| 96 | public function rep_before_install_item( $item ) { |
| 97 | if ( ! $item->is_active() ) { |
| 98 | $this->_rep_abort_this(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public function set_index( int $index ): Base_Meta_Container { |
| 103 | $this->index = $index; |
| 104 | |
| 105 | return $this; |
| 106 | } |
| 107 | |
| 108 | public function to_array(): array { |
| 109 | $boxes = array(); |
| 110 | $storage = jet_fb_current_page()->get_storage(); |
| 111 | |
| 112 | foreach ( $this->get_boxes() as $box ) { |
| 113 | try { |
| 114 | $box->set_single_id(); |
| 115 | |
| 116 | $boxes[] = $storage->to_array( $box ); |
| 117 | } catch ( Empty_Box_Exception $exception ) { |
| 118 | continue; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return array( |
| 123 | 'wrap_id' => "postbox-container-{$this->index}", |
| 124 | 'id' => $this->get_type(), |
| 125 | 'classes' => implode( ' ', $this->get_classes() ), |
| 126 | 'boxes' => $boxes, |
| 127 | ); |
| 128 | } |
| 129 | } |
| 130 |