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