| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Admin\Pages; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Admin\Pages\interfaces\Page_Script_Declaration_Interface; |
| 7 |
use Jet_Form_Builder\Exceptions\Handler_Exception; |
| 8 |
use JFB_Components\Admin\Page\Interfaces\Action_Page_It; |
| 9 |
use JFB_Components\Admin\Page\Interfaces\Admin_Page_It; |
| 10 |
use Jet_Form_Builder\Admin\Exceptions\Not_Found_Page_Exception; |
| 11 |
use Jet_Form_Builder\Admin\Single_Pages\Base_Single_Page; |
| 12 |
use Jet_Form_Builder\Classes\Instance_Trait; |
| 13 |
use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 14 |
use Jet_Form_Builder\Plugin; |
| 15 |
use JFB_Modules\Framework\Module; |
| 16 |
|
| 17 |
// If this file is called directly, abort. |
| 18 |
if ( ! defined( 'WPINC' ) ) { |
| 19 |
die; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @method static Pages_Manager instance() |
| 24 |
* |
| 25 |
* Class Pages_Manager |
| 26 |
* @package Jet_Form_Builder\Admin\Pages |
| 27 |
*/ |
| 28 |
class Pages_Manager { |
| 29 |
|
| 30 |
use Instance_Trait; |
| 31 |
|
| 32 |
/** @var Base_Page|Base_Single_Page|Action_Page_It */ |
| 33 |
private $current_page; |
| 34 |
|
| 35 |
private $stable_manager; |
| 36 |
private $single_manager; |
| 37 |
private $action_manager; |
| 38 |
|
| 39 |
const STYLE_ADMIN = 'jet-form-builder-admin-style'; |
| 40 |
const STYLE_DASHICONS = 'dashicons'; |
| 41 |
const SCRIPT_VUEX = 'jet-form-builder-admin-vuex'; |
| 42 |
const SCRIPT_PACKAGE = 'jet-form-builder-admin-package'; |
| 43 |
const SCRIPT_VUEX_PACKAGE = 'jet-form-builder-admin-vuex-package'; |
| 44 |
|
| 45 |
protected function __construct() { |
| 46 |
// backward compatibility |
| 47 |
require_once jet_form_builder()->plugin_dir( 'components/admin/buttons/legacy/base-vui-button.php' ); |
| 48 |
|
| 49 |
/** Register pages */ |
| 50 |
$this->stable()->rep_install(); |
| 51 |
$this->single()->rep_install(); |
| 52 |
$this->actions()->rep_install(); |
| 53 |
} |
| 54 |
|
| 55 |
public function set_up() { |
| 56 |
add_action( 'init', array( $this, 'set_current_page' ), 100 ); |
| 57 |
} |
| 58 |
|
| 59 |
public function get_stable_url( string $slug, array $query_args = array() ): string { |
| 60 |
try { |
| 61 |
$page = $this->get_stable( $slug ); |
| 62 |
} catch ( Repository_Exception $exception ) { |
| 63 |
return ''; |
| 64 |
} |
| 65 |
|
| 66 |
return $page->get_url( $query_args ); |
| 67 |
} |
| 68 |
|
| 69 |
public function get_action_url( string $slug, array $query_args = array() ): string { |
| 70 |
try { |
| 71 |
$page = $this->get_action( $slug ); |
| 72 |
} catch ( Repository_Exception $exception ) { |
| 73 |
return ''; |
| 74 |
} |
| 75 |
|
| 76 |
return $page->get_url( $query_args ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param string $slug |
| 81 |
* |
| 82 |
* @return Base_Page |
| 83 |
* @throws Repository_Exception |
| 84 |
*/ |
| 85 |
public function get_stable( string $slug ): Base_Page { |
| 86 |
return $this->stable()->rep_get_item( $slug ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @param string $slug |
| 91 |
* |
| 92 |
* @return Base_Single_Page |
| 93 |
* @throws Repository_Exception |
| 94 |
*/ |
| 95 |
public function get_single( string $slug ): Base_Single_Page { |
| 96 |
return $this->single()->rep_get_item( $slug ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @param string $slug |
| 101 |
* |
| 102 |
* @return Action_Page_It |
| 103 |
* @throws Repository_Exception |
| 104 |
*/ |
| 105 |
public function get_action( string $slug ): Action_Page_It { |
| 106 |
return $this->actions()->rep_get_item( $slug ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @throws Not_Found_Page_Exception |
| 111 |
* @since 3.1.0 |
| 112 |
*/ |
| 113 |
public function is_current_single() { |
| 114 |
if ( is_a( $this->get_current(), Base_Single_Page::class ) ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
throw new Not_Found_Page_Exception( 'not_single' ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @throws Not_Found_Page_Exception |
| 122 |
* @since 3.1.0 |
| 123 |
*/ |
| 124 |
public function is_current_stable() { |
| 125 |
if ( is_a( $this->get_current(), Base_Page::class ) ) { |
| 126 |
return; |
| 127 |
} |
| 128 |
throw new Not_Found_Page_Exception( 'not_stable' ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @throws Not_Found_Page_Exception |
| 133 |
* @since 3.1.0 |
| 134 |
*/ |
| 135 |
public function is_current_action() { |
| 136 |
if ( is_a( $this->get_current(), Action_Page_It::class ) ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
throw new Not_Found_Page_Exception( 'not_action' ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @return Base_Page|Base_Single_Page |
| 144 |
* @throws Not_Found_Page_Exception |
| 145 |
*/ |
| 146 |
public function get_current(): Admin_Page_It { |
| 147 |
if ( is_a( $this->current_page, Admin_Page_It::class ) ) { |
| 148 |
return $this->current_page; |
| 149 |
} |
| 150 |
|
| 151 |
throw new Not_Found_Page_Exception( 'Current page is not defined' ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Set current admin page |
| 156 |
*/ |
| 157 |
public function set_current_page() { |
| 158 |
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 159 |
$post_type = sanitize_key( $_GET['post_type'] ?? '' ); |
| 160 |
|
| 161 |
if ( jet_form_builder()->post_type->slug() !== $post_type ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
try { |
| 166 |
$slug = sanitize_key( $_GET['page'] ?? '' ); |
| 167 |
$page = $this->get_stable( $slug ); |
| 168 |
} catch ( Repository_Exception $exception ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
try { |
| 173 |
$this->set_current_page_raw( $this->get_single( $slug ) ); |
| 174 |
$this->current_page->make(); |
| 175 |
|
| 176 |
} catch ( Not_Found_Page_Exception $exception ) { |
| 177 |
$this->set_current_page_raw( $page ); |
| 178 |
} catch ( Repository_Exception $exception ) { |
| 179 |
$this->set_current_page_raw( $page ); |
| 180 |
} |
| 181 |
|
| 182 |
add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); |
| 183 |
// phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Dashboard assets |
| 188 |
* |
| 189 |
* @throws Repository_Exception |
| 190 |
*/ |
| 191 |
public function assets() { |
| 192 |
/** @var Module $module */ |
| 193 |
$module = jet_form_builder()->module( 'framework' ); |
| 194 |
$module->get_cx_vue_ui()->enqueue_assets(); |
| 195 |
|
| 196 |
$this->current_page->render_config(); |
| 197 |
$this->register_scripts(); |
| 198 |
|
| 199 |
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 200 |
do_action( "jet-fb/admin-pages/before-assets/{$this->current_page->slug()}", $this ); |
| 201 |
|
| 202 |
$this->current_page->assets(); |
| 203 |
} |
| 204 |
|
| 205 |
public function register_scripts() { |
| 206 |
wp_register_script( |
| 207 |
self::SCRIPT_VUEX, |
| 208 |
Plugin::instance()->plugin_url( 'assets/lib/vuex.js' ), |
| 209 |
array(), |
| 210 |
'3.6.2', |
| 211 |
true |
| 212 |
); |
| 213 |
|
| 214 |
$name = ( $this->current_page instanceof Base_Single_Page ) ? 'single' : 'static'; |
| 215 |
|
| 216 |
wp_register_style( |
| 217 |
self::STYLE_ADMIN, |
| 218 |
Plugin::instance()->plugin_url( "assets/css/admin/$name.css" ), |
| 219 |
array( |
| 220 |
self::STYLE_DASHICONS, |
| 221 |
), |
| 222 |
Plugin::instance()->get_version() |
| 223 |
); |
| 224 |
|
| 225 |
// todo: refactor with new way of assets loading |
| 226 |
wp_register_script( |
| 227 |
self::SCRIPT_PACKAGE, |
| 228 |
Plugin::instance()->plugin_url( 'assets/build/admin/package.js' ), |
| 229 |
array( |
| 230 |
'wp-api', |
| 231 |
'wp-api-fetch', |
| 232 |
), |
| 233 |
Plugin::instance()->get_version(), |
| 234 |
true |
| 235 |
); |
| 236 |
|
| 237 |
wp_localize_script( |
| 238 |
self::SCRIPT_PACKAGE, |
| 239 |
'JetFBPageConfigPackage', |
| 240 |
array( |
| 241 |
'nonce' => wp_create_nonce( $this->current_page->slug() ), |
| 242 |
) |
| 243 |
); |
| 244 |
|
| 245 |
// todo: refactor with new way of assets loading |
| 246 |
wp_register_script( |
| 247 |
self::SCRIPT_VUEX_PACKAGE, |
| 248 |
Plugin::instance()->plugin_url( 'assets/build/admin/vuex.package.js' ), |
| 249 |
array( |
| 250 |
self::SCRIPT_VUEX, |
| 251 |
self::SCRIPT_PACKAGE, |
| 252 |
), |
| 253 |
Plugin::instance()->get_version(), |
| 254 |
true |
| 255 |
); |
| 256 |
|
| 257 |
if ( $this->current_page instanceof Page_Script_Declaration_Interface ) { |
| 258 |
$this->current_page->register_scripts(); |
| 259 |
} else { |
| 260 |
wp_register_script( |
| 261 |
$this->current_page->slug(), |
| 262 |
$this->current_page->base_script_url(), |
| 263 |
array(), |
| 264 |
Plugin::instance()->get_version(), |
| 265 |
true |
| 266 |
); |
| 267 |
} |
| 268 |
|
| 269 |
wp_set_script_translations( |
| 270 |
$this->current_page->slug(), |
| 271 |
'jet-form-builder', |
| 272 |
Plugin::instance()->plugin_dir( 'languages' ) |
| 273 |
); |
| 274 |
} |
| 275 |
|
| 276 |
public function set_current_page_raw( Admin_Page_It $page ) { |
| 277 |
$this->current_page = $page; |
| 278 |
} |
| 279 |
|
| 280 |
public function stable(): Stable_Pages_Manager { |
| 281 |
if ( ! $this->stable_manager ) { |
| 282 |
$this->stable_manager = new Stable_Pages_Manager(); |
| 283 |
} |
| 284 |
|
| 285 |
return $this->stable_manager; |
| 286 |
} |
| 287 |
|
| 288 |
public function single(): Single_Pages_Manager { |
| 289 |
if ( ! $this->single_manager ) { |
| 290 |
$this->single_manager = new Single_Pages_Manager(); |
| 291 |
} |
| 292 |
|
| 293 |
return $this->single_manager; |
| 294 |
} |
| 295 |
|
| 296 |
public function actions(): Action_Pages_Manager { |
| 297 |
if ( ! $this->action_manager ) { |
| 298 |
$this->action_manager = new Action_Pages_Manager(); |
| 299 |
} |
| 300 |
|
| 301 |
return $this->action_manager; |
| 302 |
} |
| 303 |
|
| 304 |
} |
| 305 |
|