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