* @copyright Copyright (C) 2019 wpopal.com. All Rights Reserved. * @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html * * @website http://www.wpopal.com * @support http://www.wpopal.com/support/forum.html */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } class Opalestate_Plugin_Settings { /** * Option key, and option page slug * * @var string */ private $key = 'opalestate_settings'; /** * Array of metaboxes/fields * * @var array */ protected $option_metabox = []; /** * Options Page title * * @var string */ protected $title = ''; /** * Options Page hook * * @var string */ protected $options_page = ''; protected $subtabs = []; protected $setting_object = []; /** * Constructor * * @since 1.0 */ public function __construct() { add_action( 'admin_menu', [ $this, 'admin_menu' ], 10 ); add_action( 'admin_init', [ $this, 'init' ] ); //Custom CMB2 Settings Fields add_action( 'cmb2_render_opalestate_title', 'opalestate_title_callback', 10, 5 ); // add_action( 'cmb2_render_api_keys', 'opalestate_api_keys_callback', 10, 5 ); // add_action( 'cmb2_render_license_key', 'opalestate_license_key_callback', 10, 5 ); add_action( "cmb2_save_options-page_fields", [ $this, 'settings_notices' ], 10, 3 ); // Include CMB CSS in the head to avoid FOUC add_action( "admin_print_styles-opalestate_properties_page_opalestate-settings", [ 'CMB2_hookup', 'enqueue_cmb_css' ] ); } public function admin_menu() { // Settings $opalestate_settings_page = add_submenu_page( 'edit.php?post_type=opalestate_property', esc_html__( 'Opalestate Settings', 'opalestate-pro' ), esc_html__( 'Settings', 'opalestate-pro' ), 'manage_opalestate_settings', 'opalestate-settings', [ $this, 'admin_page_display' ] ); do_action( 'load-' . $opalestate_settings_page, $this ); // Addons $opalestate_addons_page = add_submenu_page( 'edit.php?post_type=opalestate_property', esc_html__( 'Opalestate Addons', 'opalestate-pro' ), esc_html__( 'Addons', 'opalestate-pro' ), 'manage_options', 'opalestate-addons', [ $this, 'admin_addons_page_display' ] ); do_action( 'load-' . $opalestate_addons_page, $this ); } /** * Register our setting to WP * * @since 1.0 */ public function init() { register_setting( $this->key, $this->key ); } /** * Retrieve settings tabs * * @return array $tabs * @since 1.0 */ public function opalestate_get_settings_tabs() { $settings = $this->opalestate_settings( null ); $tabs = []; $tabs['general'] = esc_html__( 'General', 'opalestate-pro' ); $tabs['property'] = esc_html__( 'Property', 'opalestate-pro' ); $tabs['pages'] = esc_html__( 'Pages', 'opalestate-pro' ); $tabs['3rd_party'] = esc_html__( '3rd Party', 'opalestate-pro' ); $tabs['api_keys'] = esc_html__( 'API', 'opalestate-pro' ); if ( ! empty( $settings['addons']['fields'] ) ) { $tabs['addons'] = esc_html__( 'Add-ons', 'opalestate-pro' ); } if ( ! empty( $settings['licenses']['fields'] ) ) { $tabs['licenses'] = esc_html__( 'Licenses', 'opalestate-pro' ); } return apply_filters( 'opalestate_settings_tabs', $tabs ); } public function admin_addons_page_display() { require_once opalestate_get_admin_view( 'addons/list.php' ); } public function get_subtabs_link( $tab_id, $stab_id ) { $tab_url = esc_url( add_query_arg( [ 'settings-updated' => false, 'tab' => $tab_id, 'subtab' => $stab_id, ] ) ); return $tab_url; } /** * Admin page markup. Mostly handled by CMB2 * * @since 1.0 */ public function admin_page_display() { $active_tab = isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $this->opalestate_get_settings_tabs() ) ? $_GET['tab'] : 'general'; $sub_active_tab = isset( $_GET['subtab'] ) ? sanitize_text_field( $_GET['subtab'] ) : ''; $tabs_fields = $this->opalestate_settings( $active_tab ); $sub_tabs_fields = []; if ( empty( $sub_active_tab ) && $this->subtabs ) { $first = array_flip( $this->subtabs ); $sub_active_tab = reset( $first ); } if ( $this->subtabs ) { $sub_tabs_fields = $this->setting_object->get_subtabs_content( $sub_active_tab ); } ?>
subtabs ): ?>
key ); ?> key ); ?>
'page', 'numberposts' => -1, ] ); $general = []; $opalestate_settings = []; //Return all settings array if necessary if ( $active_tab === null ) { return apply_filters( 'opalestate_registered_settings', $opalestate_settings ); } $output = apply_filters( 'opalestate_registered_' . $active_tab . '_settings', isset( $opalestate_settings[ $active_tab ] ) ? $opalestate_settings[ $active_tab ] : [] ); if ( empty( $output ) ) { $class = "Opalestate_Settings_" . ucfirst( $active_tab ) . "_Tab"; if ( class_exists( $class ) ) { $tab = new $class( $this->key ); $this->setting_object = $tab; $this->subtabs = $tab->get_subtabs(); return $tab->get_tab_content( $this->key ); } return [ $active_tab => [] ]; } // Add other tabs and settings fields as needed return $output; } /** * Show Settings Notices * * @param $object_id * @param $updated * @param $cmb */ public function settings_notices( $object_id, $updated, $cmb ) { //Sanity check if ( $object_id !== $this->key ) { return; } if ( did_action( 'cmb2_save_options-page_fields' ) === 1 ) { settings_errors( 'opalestate-notices' ); } add_settings_error( 'opalestate-notices', 'global-settings-updated', esc_html__( 'Settings updated.', 'opalestate-pro' ), 'updated' ); } /** * Public getter method for retrieving protected/private variables * * @param string $field Field to retrieve * * @return mixed Field value or exception is thrown * @since 1.0 * */ public function __get( $field ) { // Allowed fields to retrieve if ( in_array( $field, [ 'key', 'fields', 'opalestate_title', 'options_page' ], true ) ) { return $this->{$field}; } if ( 'option_metabox' === $field ) { return $this->option_metabox(); } throw new Exception( 'Invalid property: ' . $field ); } }