values) for the default settings on this admin page. public $menu_ops; # associative array of configuration options for the admin menu(s). public $page_ops; # associative array of configuration options for the settings page. /** * Call this method in a subclass constructor to create an admin menu and settings page. * * @since 1.0.0 */ public function create( $page_id = '', $menu_ops = array(), $page_ops = array(), $settings_field = '', $default_settings = array() ) { # Set the properties $this->page_id = $this->page_id ? $this->page_id : (string) $page_id; $this->menu_ops = $this->menu_ops ? $this->menu_ops : (array) $menu_ops; $this->page_ops = $this->page_ops ? $this->page_ops : (array) $page_ops; $this->settings_field = $this->settings_field ? $this->settings_field : (string) $settings_field; $this->default_settings = $this->default_settings ? $this->default_settings : (array) $default_settings; # Default page ops $this->page_ops = wp_parse_args( $this->page_ops, array( 'screen_icon' => 'options-general', 'save_button_text' => __( 'Save Settings', 'bizznis' ), 'reset_button_text' => __( 'Reset Settings', 'bizznis' ), 'saved_notice_text' => __( 'Settings saved.', 'bizznis' ), 'reset_notice_text' => __( 'Settings reset.', 'bizznis' ), 'error_notice_text' => __( 'Error saving settings.', 'bizznis' ), ) ); # Stop here if page_id not set if ( ! $this->page_id ) { return; } # Check to make sure there we are only creating one menu per subclass if ( isset( $this->menu_ops['theme_menu'] ) && ( isset( $this->menu_ops['theme_submenu'] ) ) || isset( $this->menu_ops['submenu'] ) && ( isset( $this->menu_ops['main_menu'] ) || isset( $this->menu_ops['first_submenu'] ) ) ) { wp_die( sprintf( __( 'You cannot use %s to create two menus in the same subclass. Please use separate subclasses for each menu.', 'bizznis' ), 'Bizznis_Admin' ) ); } # Theme options actions add_action( 'admin_menu', array( $this, 'maybe_add_theme_menu' ), 5 ); # create the theme options menu add_action( 'admin_init', array( $this, 'register_settings' ) ); # set up settings add_action( 'admin_notices', array( $this, 'notices' ) ); # set up notices add_action( 'admin_init', array( $this, 'settings_init' ) ); # load the page content add_filter( 'pre_update_option_' . $this->settings_field, array( $this, 'save' ), 10, 2 ); # add a sanitizer/validator } /** * Possibly create a new theme menu. * * @since 1.0.0 */ public function maybe_add_theme_menu() { # Add theme menu if ( isset( $this->menu_ops['theme_menu'] ) && is_array( $this->menu_ops['theme_menu'] ) ) { $menu = wp_parse_args( $this->menu_ops['theme_menu'], array( 'page_title' => '', 'menu_title' => '', 'capability' => 'edit_theme_options' ) ); $this->pagehook = add_theme_page( $menu['page_title'], $menu['menu_title'], $menu['capability'], $this->page_id, array( $this, 'admin' ) ); } # Hide theme menu if ( isset( $this->menu_ops['theme_menu']['menu_hide'] ) && ! bizznis_is_menu_page( $this->page_id ) ) { remove_submenu_page( 'themes.php', $this->page_id ); } } /** * Register the database settings for storage. * * @since 1.0.0 */ public function register_settings() { # If this page doesn't store settings, no need to register them if ( ! $this->settings_field ) { return; } register_setting( $this->settings_field, $this->settings_field ); add_option( $this->settings_field, $this->default_settings ); if ( ! bizznis_is_menu_page( $this->page_id ) ) { return; } if ( bizznis_get_option( 'reset', $this->settings_field ) ) { if ( update_option( $this->settings_field, $this->default_settings ) ) { bizznis_admin_redirect( $this->page_id, array( 'reset' => 'true' ) ); } else { bizznis_admin_redirect( $this->page_id, array( 'error' => 'true' ) ); } exit; } } /** * Display notices on the save or reset of settings. * * @since 1.0.0 */ public function notices() { if ( ! bizznis_is_menu_page( $this->page_id ) ) { return; } if ( isset( $_REQUEST['settings-updated'] ) && $_REQUEST['settings-updated'] == 'true' ) { echo '
' . $this->page_ops['saved_notice_text'] . '
' . $this->page_ops['reset_notice_text'] . '
' . $this->page_ops['error_notice_text'] . '