'', 'menu_title' => '', 'menu_slug' => '', 'position' => 10, 'icon_url' => null, 'cap' => 'manage_options', 'template_name' => '', 'is_woocommerce' => false, 'tab_key' => 'tab', 'parent_slug' => null, ); /** * Page Tabs. * * @var array */ protected $tabs = array(); /** * Admin Menu Pages Slugs. * * @var array */ protected $parent_pages_slugs = array( 'index.php', // Dashboard. 'edit.php', // Posts. 'upload.php', // Media. 'edit.php?post_type=page', // Pages. 'edit-comments.php', // Comments. 'edit.php?post_type=custom_post_type_name', // Custom Post Type. 'admin.php?page=wc-admin', // WooCommerce. 'edit.php?post_type=product', // Products. 'themes.php', // Appearance. 'plugins.php', // Plugins. 'users.php', // Users. 'tools.php', // Tools. 'options-general.php', // Settings. 'settings.php', // Network Settings. ); /** * Assets Files to include. * * @return void */ protected $assets = array(); /** * Core Assets Files to include. * * @return void */ protected $core_assets = array(); /** * Settings * * @var Settings */ public $settings = null; /** * Page PATH. * * @var string */ protected $page_path; /** * Templates Folder name * * @var string */ protected $templates_folder; /** * Default Page Properties. * * @var array */ private $default_page_props = array( 'is_woocommerce' => false, 'cap' => 'manage_options', 'position' => 10, ); /** * Prepare Page. * * @return void */ abstract protected function prepare(); /** * Initialize Page. * * @param object $core * @param array $plugin_info * @param AdminPage $parent_page */ public static function init() { if ( is_null( static::$instance ) ) { static::$instance = new static(); } return static::$instance; } /** * Admin Page Constructor. */ protected function __construct() { if ( is_admin() ) { $this->main_setup(); $this->main_hooks(); } } /** * Setup Parent. * * @return void */ public function main_setup() { $this->prepare(); $this->page_props = array_merge( $this->default_page_props, $this->page_props ); // Page PATH. if ( $this->page_props['is_woocommerce'] ) { $this->page_path = admin_url( 'admin.php?page=wc-settings&tab=' . $this->page_props['menu_slug'] ); $this->page_props['parent_slug'] = 'admin.php'; } if ( ! $this->page_path ) { $this->page_path = admin_url( ( $this->page_props['parent_slug'] ? $this->page_props['parent_slug'] : 'admin.php' ) . '?page=' . $this->page_props['menu_slug'] ); } // Main Assets. $this->core_assets = array( array( 'type' => 'js', 'handle' => 'jquery', ), array( 'type' => 'js', 'handle' => 'wp-hook', ), array( 'type' => 'css', 'handle' => self::$plugin_info['name'] . '-bootstrap-css', 'url' => self::$core->core_assets_lib( 'bootstrap', 'css' ), ), array( 'type' => 'js', 'handle' => self::$plugin_info['name'] . '-bootstrap-js', 'url' => self::$core->core_assets_lib( 'bootstrap.bundle', 'js' ), ), ); if ( empty( $this->tabs ) ) { $this->tabs['general'] = array( 'default' => true, 'template' => ! empty( $this->page_props['template_name'] ) ? $this->page_props['template_name'] : '', ); } } /** * Add Page. * * @return void */ public function add_page() { if ( $this->page_props['is_woocommerce'] ) { add_filter( 'woocommerce_settings_tabs_array', array( $this, 'woo_register_settings_tab' ), 100, 1 ); add_action( 'woocommerce_settings_' . $this->page_props['menu_slug'], array( $this, 'woo_settings_tab_action' ), 10 ); return; } if ( ! is_null( $this->page_props['parent_slug'] ) ) { add_submenu_page( $this->page_props['parent_slug'], $this->page_props['page_title'], $this->page_props['menu_title'], $this->page_props['cap'], $this->page_props['menu_slug'], array( $this, 'page_output_function' ), $this->page_props['position'] ); } else { add_menu_page( $this->page_props['page_title'], $this->page_props['menu_title'], $this->page_props['cap'], $this->page_props['menu_slug'], array( $this, 'page_output_function' ), $this->page_props['icon_url'], $this->page_props['position'] ); } } /** * Add the Page to WooCommerce Settings Page. * * @return void */ public function woo_register_settings_tab( $settings_tabs ) { $settings_tabs[ $this->page_props['menu_slug'] ] = $this->page_props['menu_title']; return $settings_tabs; } /** * SHow the Settings Tab Fields. * * @return void */ public function woo_settings_tab_action() { $tab_name = $this->get_current_tab(); $tab = $this->tabs[ $tab_name ]; if ( ! empty( $this->page_props['tab_key'] ) && ! empty( $_GET[ $this->page_props['tab_key'] ] ) ) { $action = sanitize_text_field( wp_unslash( $_GET[ $this->page_props['tab_key'] ] ) ); $method_name = $action . '_tab'; if ( method_exists( $this, $method_name ) ) { if ( empty( $tab['hide_tabs'] ) ) { $this->output_page_tabs_nav( true ); } $this->{$method_name}(); } else { $this->page_output_function(); } } else { $this->page_output_function(); } do_action( self::$plugin_info['name'] . '-' . $this->page_props['menu_slug'] . '-main-settings-tabs-action', self::$plugin_info ); if ( ! empty( $tab['woo_hide_save_btn'] ) ) { $GLOBALS['hide_save_button'] = true; } } /** * Register the page. * * @return void */ protected function main_hooks() { add_action( 'admin_menu', array( $this, 'add_page' ), 100 ); add_action( 'admin_enqueue_scripts', array( $this, 'assets' ), PHP_INT_MAX, 1 ); if ( method_exists( $this, 'hooks' ) ) { $this->hooks(); } } /** * Get Default Tab. * * @return string|false */ private function get_default_tab() { foreach ( $this->tabs as $tab_name => $tab_arr ) { if ( ! empty( $tab_arr['default'] ) ) { return $tab_name; } } return false; } /** * Page Output HTML function. * * @return void */ public function page_output_function( $reserved = '', $display_tabs = true, $show_notices = true ) { $tab_name = $this->get_current_tab(); if ( empty( $tab_name ) || empty( $this->tabs[ $tab_name ] ) ) { return; } $tab = $this->tabs[ $tab_name ]; ?>