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_menu', array( $this, 'maybe_hide_theme_menu' ), 5 ); # hide 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 (metaboxes or custom form) 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() { # Maybe 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' ) ); } } /** * Possibly hide a theme menu. * * @since 1.0.0 */ public function maybe_hide_theme_menu() { # Hide theme menu if not viewing/browsing it 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'] . '

'; } elseif ( isset( $_REQUEST['reset'] ) && 'true' == $_REQUEST['reset'] ) { echo '

' . $this->page_ops['reset_notice_text'] . '

'; } elseif ( isset( $_REQUEST['error'] ) && $_REQUEST['error'] == 'true' ) { echo '

' . $this->page_ops['error_notice_text'] . '

'; } } /** * Save method. Override this method to modify form data (for validation, sanitization, etc.) * before it gets saved. * * @since 1.0.0 */ public function save( $newvalue, $oldvalue ) { return $newvalue; } /** * Initialize the settings page. This method must be re-defined in the extended classes, * to hook in the required components for the page. * * @since 1.0.0 */ abstract public function settings_init(); /** * Output the main admin page. This method must be re-defined in the extended class, * to output the main admin page content. * * @since 1.0.0 */ abstract public function admin(); /** * Helper function that constructs name attributes for use in form fields. * * @since 1.0.0 */ protected function get_field_name( $name ) { return sprintf( '%s[%s]', $this->settings_field, $name ); } /** * Helper function that constructs id attributes for use in form fields. * * @since 1.0.0 */ protected function get_field_id( $id ) { return sprintf( '%s[%s]', $this->settings_field, $id ); } /** * Helper function that returns a setting value from this form's settings field for * use in form fields. * * @since 1.0.0 */ protected function get_field_value( $key ) { return bizznis_get_option( $key, $this->settings_field ); } } /** * Abstract subclass of Bizznis_Admin which adds support for displaying a form. * * @since 1.0.0 */ abstract class Bizznis_Admin_Form extends Bizznis_Admin { /** * Output settings page form elements. * * @since 1.0.0 */ abstract public function form(); /** * Normal settings page admin. * * @since 1.0.0 */ public function admin() { ?>
settings_field ); ?> page_ops['screen_icon'] ); ?>

pagehook ); ?> pagehook ); ?>

pagehook . '_settings_page_form', $this->pagehook ); ?> pagehook ); ?> page_id, 'default' ); ?> page_id );?>

page_ops['save_button_text'], 'primary', 'submit', false ); submit_button( $this->page_ops['reset_button_text'], 'secondary', $this->get_field_name( 'reset' ), false, array( 'onclick' => 'return bizznis_confirm(\'' . esc_js( __( 'Are you sure you want to reset?', 'bizznis' ) ) . '\');' ) ); ?>

pagehook . '_settings_page_form', array( $this, 'form' ) ); if ( method_exists( $this, 'help' ) ) { add_action( 'load-' . $this->pagehook, array( $this, 'help' ) ); } } } /** * Abstract subclass of Bizznis_Admin which adds support for registering and displaying metaboxes. * * @since 1.0.0 */ abstract class Bizznis_Admin_Boxes extends Bizznis_Admin { /** * Register the metaboxes. * * @since 1.0.0 */ abstract public function metaboxes(); /** * Include the necessary sortable metabox scripts. * * @since 1.0.0 */ public function scripts() { wp_enqueue_script( 'common' ); wp_enqueue_script( 'wp-lists' ); wp_enqueue_script( 'postbox' ); } /** * Use this as the settings admin callback to create an admin page with sortable metaboxes. * * @since 1.0.0 */ public function admin() { global $wp_meta_boxes; $screen = get_current_screen(); ?>
settings_field ); ?> page_ops['screen_icon'] ); ?>

pagehook ); ?> pagehook ); ?>

pagehook ); ?>
pagehook, 'main', null ); ?>
pagehook, 'column2', null ); ?>
pagehook, 'column3', null ); ?>
pagehook, 'column4', null ); ?>
pagehook ); ?>

page_ops['save_button_text'], 'primary', 'submit', false ); submit_button( $this->page_ops['reset_button_text'], 'secondary', $this->get_field_name( 'reset' ), false, array( 'onclick' => 'return bizznis_confirm(\'' . esc_js( __( 'Are you sure you want to reset?', 'bizznis' ) ) . '\');' ) ); ?>

pagehook ); do_meta_boxes( $this->pagehook, 'main', null ); if ( isset( $wp_meta_boxes[$this->pagehook]['column2'] ) ) { do_meta_boxes( $this->pagehook, 'column2', null ); } do_action( 'bizznis_admin_after_metaboxes', $this->pagehook ); ?>
pagehook, array( $this, 'scripts' ) ); add_action( 'load-' . $this->pagehook, array( $this, 'metaboxes' ) ); add_action( $this->pagehook . '_settings_page_boxes', array( $this, 'do_metaboxes' ) ); if ( method_exists( $this, 'help' ) ) { add_action( 'load-' . $this->pagehook, array( $this, 'help' ) ); } } } /** * Abstract subclass of Bizznis_Admin which adds support for creating a basic admin page * that doesn't make use of a Settings API form or metaboxes. * * @since 1.0.0 */ abstract class Bizznis_Admin_Basic extends Bizznis_Admin { /** * Satisfies the abstract requirements of Bizznis_Admin. * * @since 1.0.0 */ public function settings_init() { if ( method_exists( $this, 'help' ) ) { add_action( 'load-' . $this->pagehook, array( $this, 'help' ) ); } } }