base-list-box.php
2 years ago
base-meta-box.php
2 years ago
base-table-box.php
2 years ago
meta-box-options-converter.php
2 years ago
meta-box-options.php
2 years ago
meta-box-update-callback.php
2 years ago
meta-table-options-converter.php
2 years ago
meta-table-options.php
2 years ago
base-meta-box.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Admin\Single_Pages\Meta_Boxes; |
| 5 | |
| 6 | use Jet_Form_Builder\Admin\Exceptions\Empty_Box_Exception; |
| 7 | use Jet_Form_Builder\Admin\Exceptions\Not_Found_Page_Exception; |
| 8 | use Jet_Form_Builder\Admin\Pages\Pages_Manager; |
| 9 | use Jet_Form_Builder\Admin\Single_Pages\Base_Single_Page; |
| 10 | use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 11 | use JFB_Components\Repository\Repository_Item_With_Class; |
| 12 | use JFB_Components\Repository\Repository_Static_Item_It; |
| 13 | use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 14 | use Jet_Form_Builder\Db_Queries\Traits\Model_Dependencies; |
| 15 | use Jet_Form_Builder\Db_Queries\Traits\Model_Dependencies_Interface; |
| 16 | |
| 17 | // If this file is called directly, abort. |
| 18 | if ( ! defined( 'WPINC' ) ) { |
| 19 | die; |
| 20 | } |
| 21 | |
| 22 | abstract class Base_Meta_Box implements |
| 23 | Model_Dependencies_Interface, |
| 24 | Arrayable, |
| 25 | Repository_Static_Item_It { |
| 26 | |
| 27 | const TYPE_LIST = 'list'; |
| 28 | const TYPE_TABLE = 'table'; |
| 29 | |
| 30 | use Repository_Item_With_Class; |
| 31 | use Model_Dependencies; |
| 32 | |
| 33 | private $id = 0; |
| 34 | |
| 35 | abstract public function get_title(): string; |
| 36 | |
| 37 | abstract public function get_values(): array; |
| 38 | |
| 39 | public function get_slug(): string { |
| 40 | return str_replace( ' ', '-', strtolower( $this->get_title() ) ); |
| 41 | } |
| 42 | |
| 43 | public function is_active(): bool { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return int |
| 49 | */ |
| 50 | public function get_id(): int { |
| 51 | return $this->id; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param int $id |
| 56 | * |
| 57 | * @return Base_Meta_Box |
| 58 | */ |
| 59 | public function set_id( int $id ): Base_Meta_Box { |
| 60 | $this->id = $id; |
| 61 | |
| 62 | return $this; |
| 63 | } |
| 64 | |
| 65 | public function set_single_id(): Base_Meta_Box { |
| 66 | /** @var Base_Single_Page $single */ |
| 67 | try { |
| 68 | $single = Pages_Manager::instance()->get_current(); |
| 69 | } catch ( Not_Found_Page_Exception $e ) { |
| 70 | return $this; |
| 71 | } |
| 72 | |
| 73 | return $this->set_id( $single->get_id() ); |
| 74 | } |
| 75 | |
| 76 | public function get_migrations(): \Generator { |
| 77 | foreach ( $this->get_dependencies() as $model ) { |
| 78 | yield from $model->get_migrations(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return int[] |
| 84 | * @throws Empty_Box_Exception |
| 85 | */ |
| 86 | public function to_array(): array { |
| 87 | try { |
| 88 | $this->prepare_dependencies(); |
| 89 | } catch ( Sql_Exception $exception ) { |
| 90 | throw new Empty_Box_Exception( |
| 91 | esc_html( $exception->getMessage() ), |
| 92 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 93 | ...$exception->get_additional() |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | return array( |
| 98 | 'slug' => $this->get_slug(), |
| 99 | 'title' => $this->get_title(), |
| 100 | 'list' => $this->get_values(), |
| 101 | ); |
| 102 | } |
| 103 | } |
| 104 |