actions
4 years ago
meta-boxes
4 years ago
meta-containers
4 years ago
base-page-updater.php
4 years ago
base-single-page.php
4 years ago
base-single-page.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Admin\Single_Pages; |
| 5 | |
| 6 | use Jet_Form_Builder\Admin\Admin_Page_Interface; |
| 7 | use Jet_Form_Builder\Admin\Admin_Page_Trait; |
| 8 | use Jet_Form_Builder\Admin\Exceptions\Not_Found_Page_Exception; |
| 9 | use Jet_Form_Builder\Admin\Pages\Base_Page; |
| 10 | use Jet_Form_Builder\Admin\Pages\Pages_Manager; |
| 11 | use Jet_Form_Builder\Admin\Single_Pages\Meta_Containers\Base_Meta_Container; |
| 12 | use Jet_Form_Builder\Classes\Arrayable\Array_Convert_Once; |
| 13 | use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 14 | use Jet_Form_Builder\Classes\Repository\Repository_Item_Instance_Trait; |
| 15 | use Jet_Form_Builder\Classes\Theme\With_Theme_Info; |
| 16 | use Jet_Form_Builder\Db_Queries\Execution_Builder; |
| 17 | |
| 18 | abstract class Base_Single_Page implements Admin_Page_Interface, Repository_Item_Instance_Trait { |
| 19 | |
| 20 | use Admin_Page_Trait; |
| 21 | use With_Theme_Info; |
| 22 | |
| 23 | protected $id; |
| 24 | protected $storage; |
| 25 | protected $containers = array(); |
| 26 | |
| 27 | abstract public function parent_slug(): string; |
| 28 | |
| 29 | public function __construct() { |
| 30 | $this->storage = new Array_Convert_Once(); |
| 31 | } |
| 32 | |
| 33 | public function rep_item_id() { |
| 34 | return $this->parent_slug(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return Base_Meta_Container[] |
| 39 | */ |
| 40 | public function meta_containers(): array { |
| 41 | return array(); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * @return Base_Single_Page |
| 47 | * @throws Not_Found_Page_Exception |
| 48 | */ |
| 49 | public function make(): Base_Single_Page { |
| 50 | $id = $this->query_id(); |
| 51 | |
| 52 | $this->set_id( $id )->query_config(); |
| 53 | |
| 54 | return $this; |
| 55 | } |
| 56 | |
| 57 | public function query_id(): int { |
| 58 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 59 | return absint( $_GET['item_id'] ?? 0 ); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * @param int $id |
| 65 | * |
| 66 | * @return Base_Single_Page |
| 67 | * @throws Not_Found_Page_Exception |
| 68 | */ |
| 69 | public function set_id( int $id ): Base_Single_Page { |
| 70 | if ( ! $id ) { |
| 71 | throw new Not_Found_Page_Exception( 'Item id is empty: ' . $id . ' in ' . static::class ); |
| 72 | } |
| 73 | $this->id = $id; |
| 74 | |
| 75 | return $this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return array |
| 80 | */ |
| 81 | public function page_config(): array { |
| 82 | return array( |
| 83 | 'title' => $this->title(), |
| 84 | 'containers' => Array_Tools::to_array( $this->get_prepared_containers() ), |
| 85 | 'actions' => Array_Tools::to_array( $this->get_actions() ), |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | public function get_prepared_containers(): array { |
| 90 | if ( count( $this->containers ) ) { |
| 91 | return $this->containers; |
| 92 | } |
| 93 | |
| 94 | /** @var Base_Meta_Container[] $containers */ |
| 95 | $containers = apply_filters( "jet-form-builder/page-containers/{$this->slug()}", $this->meta_containers() ); |
| 96 | |
| 97 | foreach ( $containers as $index => $container ) { |
| 98 | $this->containers[] = $container->set_index( $index ); |
| 99 | } |
| 100 | |
| 101 | return $this->containers; |
| 102 | } |
| 103 | |
| 104 | public function slug(): string { |
| 105 | return $this->parent_slug() . '-single'; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns current page url |
| 110 | * |
| 111 | * @param array $query_args |
| 112 | * |
| 113 | * @return string |
| 114 | */ |
| 115 | public function get_url( $query_args = array() ): string { |
| 116 | return $this->get_parent()->get_url( |
| 117 | array_merge( |
| 118 | array( 'item_id' => $this->id ), |
| 119 | $query_args |
| 120 | ) |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | public function get_parent(): Base_Page { |
| 125 | return Pages_Manager::instance()->stable()->rep_get_item_or_die( $this->parent_slug() ); |
| 126 | } |
| 127 | |
| 128 | public function get_id(): int { |
| 129 | return $this->id; |
| 130 | } |
| 131 | |
| 132 | public function get_storage(): Array_Convert_Once { |
| 133 | return $this->storage; |
| 134 | } |
| 135 | |
| 136 | } |
| 137 |