addons-page.php
4 years ago
base-page.php
4 years ago
page-config.php
4 years ago
pages-manager.php
4 years ago
settings-page.php
4 years ago
pages-manager.php
134 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Admin\Pages; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Plugin; |
| 8 | |
| 9 | class Pages_Manager { |
| 10 | |
| 11 | /** |
| 12 | * @var Base_Page[] |
| 13 | */ |
| 14 | private $pages = array(); |
| 15 | |
| 16 | /** |
| 17 | * @var Base_Page |
| 18 | */ |
| 19 | private $current_page; |
| 20 | |
| 21 | /** |
| 22 | * [__construct description] |
| 23 | * |
| 24 | * @param array $pages [description] |
| 25 | */ |
| 26 | public function __construct( $pages = array() ) { |
| 27 | $this->register_pages( $pages ); |
| 28 | add_action( 'admin_menu', array( $this, 'add_pages' ) ); |
| 29 | |
| 30 | if ( $this->is_dashboard_page() ) { |
| 31 | $this->set_current_page(); |
| 32 | add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Register admin pages |
| 38 | * |
| 39 | * @param $pages |
| 40 | */ |
| 41 | public function register_pages( $pages ) { |
| 42 | foreach ( $pages as $page ) { |
| 43 | $this->pages[ $page->slug() ] = $page; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Check if is dashboard page |
| 49 | * |
| 50 | * @return boolean [description] |
| 51 | */ |
| 52 | public function is_dashboard_page() { |
| 53 | |
| 54 | $page = ! empty( $_GET['page'] ) ? esc_attr( $_GET['page'] ) : false; |
| 55 | |
| 56 | if ( ! $page ) { |
| 57 | return false; |
| 58 | } else { |
| 59 | return isset( $this->pages[ $page ] ); |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set current admin page |
| 66 | */ |
| 67 | public function set_current_page() { |
| 68 | $this->current_page = $this->pages[ esc_attr( $_GET['page'] ) ]; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Dashboard assets |
| 73 | */ |
| 74 | public function assets() { |
| 75 | $ui_data = Plugin::instance()->framework->get_included_module_data( 'cherry-x-vue-ui.php' ); |
| 76 | ( new \CX_Vue_UI( $ui_data ) )->enqueue_assets(); |
| 77 | |
| 78 | ( new Page_Config( $this->current_page ) )->render_config(); |
| 79 | |
| 80 | do_action( "jet-fb/admin-pages/before-assets/{$this->current_page->slug()}", $this ); |
| 81 | |
| 82 | $this->current_page->assets(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param $page_slug |
| 87 | * |
| 88 | * @return string |
| 89 | */ |
| 90 | public function get_url_of( $page_slug ): string { |
| 91 | |
| 92 | if ( ! isset( $this->pages[ $page_slug ] ) ) { |
| 93 | return ''; |
| 94 | } |
| 95 | |
| 96 | return $this->pages[ $page_slug ]->get_url(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Check if passed page is currently displayed |
| 101 | * |
| 102 | * @param Base_Page $page |
| 103 | * |
| 104 | * @return bool |
| 105 | */ |
| 106 | public function is_page_now( Base_Page $page ): bool { |
| 107 | |
| 108 | if ( ! $this->is_dashboard_page() ) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | return ( $page->slug() === $this->current_page->slug() ); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Register appointments |
| 118 | */ |
| 119 | public function add_pages() { |
| 120 | $parent = 'edit.php?post_type=' . jet_form_builder()->post_type->slug(); |
| 121 | |
| 122 | foreach ( $this->pages as $page ) { |
| 123 | add_submenu_page( |
| 124 | $parent, |
| 125 | $page->title(), |
| 126 | $page->title(), |
| 127 | 'manage_options', |
| 128 | $page->slug(), |
| 129 | array( $page, 'render' ) |
| 130 | ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |