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