*/ namespace RankMath_Monitor\Admin; use WP_Http; use CMB2_hookup; use RankMath_Monitor\CMB2; use RankMath_Monitor\Replace_Vars; use RankMath_Monitor\Traits\Hooker; use MyThemeShop\Helpers\Str; use RankMath_Monitor\Helper as GlobalHelper; defined( 'ABSPATH' ) || exit; /** * Options class. */ class Options { use Hooker; /** * Page title. * * @var string */ public $title = 'Settings'; /** * Menu title. * * @var string */ public $menu_title = 'Settings'; /** * Hold tabs for page. * * @var array */ public $tabs = array(); /** * Hold folder name for tab files. * * @var string */ public $folder = ''; /** * Menu Position. * * @var int */ public $position = 10; /** * The capability required for this menu to be displayed to the user. * * @var string */ public $capability = 'manage_options'; /** * CMB2 option page id. * * @var string */ private $cmb_id = null; /** * The Constructor * * @param array $config Array of configuration. */ public function __construct( $config ) { $this->config( $config ); $this->cmb_id = $this->key . '_options'; $this->action( 'cmb2_admin_init', 'register_option_page', $this->position ); $this->action( 'admin_post_' . $this->key, 'reset_options', 2 ); if ( true === empty( get_option( $this->key ) ) ) { $this->action( 'cmb2_init_hookup_' . $this->cmb_id, 'set_defaults', 11 ); } if ( ! $this->is_current_page() ) { return; } $this->action( 'admin_enqueue_scripts', 'enqueue' ); $this->action( 'admin_body_class', 'body_class' ); add_action( 'admin_enqueue_scripts', array( 'CMB2_hookup', 'enqueue_cmb_css' ), 25 ); } /** * Create option object and add settings */ function register_option_page() { $cmb = new_cmb2_box( array( 'id' => $this->cmb_id, 'title' => $this->title, 'menu_title' => $this->menu_title, 'capability' => $this->capability, 'object_types' => array( 'options-page' ), 'option_key' => $this->key, 'parent_slug' => 'rank-math-monitor', 'cmb_styles' => false, 'display_cb' => array( $this, 'display' ), ) ); $tabs = $this->get_tabs(); $cmb->add_field( array( 'id' => 'setting-panel-container-' . $this->cmb_id, 'type' => 'tab_container_open', 'tabs' => $tabs, ) ); foreach ( $tabs as $id => $tab ) { $located = $this->locate_file( $id, $tab ); if ( false === $located ) { continue; } $cmb->add_field( array( 'name' => esc_html__( 'Panel', '404-monitor' ), 'id' => 'setting-panel-' . $id, 'type' => 'tab_open', ) ); $cmb->add_field( array( 'id' => $id . '_section_title', 'type' => 'title', 'name' => isset( $tab['page_title'] ) ? $tab['page_title'] : ( isset( $tab['title'] ) ? $tab['title'] : '' ), 'desc' => isset( $tab['desc'] ) ? $tab['desc'] : '', 'after' => isset( $tab['after'] ) ? $tab['after'] : '', 'classes' => 'main', ) ); include $located; $cmb->add_field( array( 'id' => 'setting-panel-' . $id . '-close', 'type' => 'tab_close', ) ); } $cmb->add_field( array( 'id' => 'setting-panel-container-close-' . $this->cmb_id, 'type' => 'tab_container_close', ) ); CMB2::pre_init( $cmb ); } /** * Set the default values if not set * * @param CMB2 $cmb The CMB2 object to hookup. */ public function set_defaults( $cmb ) { foreach ( $cmb->prop( 'fields' ) as $id => $field_args ) { $field = $cmb->get_field( $id ); if ( isset( $field_args['default'] ) || isset( $field_args['default_cb'] ) ) { $defaults[ $id ] = $field->get_default(); } } // Save Defaults if any. if ( ! empty( $defaults ) ) { add_option( $this->key, $defaults ); } } /** * Reset options */ public function reset_options() { $url = wp_get_referer(); if ( ! $url ) { $url = admin_url(); } if ( isset( $_POST['reset-cmb'], $_POST['action'] ) && $this->key === $_POST['action'] ) { delete_option( $this->key ); wp_safe_redirect( esc_url_raw( $url ), WP_Http::SEE_OTHER ); exit; } } /** * Enqueue styles and scripts */ public function enqueue() { $screen = get_current_screen(); if ( ! Str::contains( $this->key, $screen->id ) ) { return; } CMB2_hookup::enqueue_cmb_css(); wp_enqueue_style( 'font-awesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css', null, rank_math_monitor()->version ); wp_enqueue_style( 'rank-math-options', rank_math_monitor()->plugin_url() . 'assets/admin/css/option-panel.css', array( 'rank-math-common', 'rank-math-cmb2' ), rank_math_monitor()->version ); wp_enqueue_script( 'rank-math-options', rank_math_monitor()->plugin_url() . 'assets/admin/js/option-panel.js', array( 'underscore', 'rank-math-common' ), rank_math_monitor()->version, true ); // Add thank you. GlobalHelper::add_json( 'indexUrl', rank_math_monitor()->plugin_url() . 'assets/admin/js/search-index/' ); GlobalHelper::add_json( 'optionPage', str_replace( 'rank-math-options-', '', $this->key ) ); } /** * Add classes to
of WordPress admin * * @param string $classes Space-separated list of CSS classes. * @return string */ public function body_class( $classes = '' ) { return $classes . ' rank-math-page'; } /** * Display Setting on a page * * @param CMB2_Options $machine CUrrent CMB2 box object. */ public function display( $machine ) { $cmb = $machine->cmb; ?>