'', // Name of the menu item 'title' => '', // Title displayed on the top of the admin panel 'parent' => null, // id of parent, if blank, then this is a top level menu 'id' => '', // Unique ID of the menu item 'capability' => 'manage_options', // User role 'icon' => 'dashicons-admin-generic', // Menu icon for top level menus only http://melchoyce.github.io/dashicons/ 'position' => null, // Menu position. Can be used for both top and sub level menus 'use_form' => true, // If false, options will not be wrapped in a form 'desc' => '', // Description displayed below the title ); public $settings; public $options = array(); public $tabs = array(); public $owner; public $panelID; private $activeTab = null; private static $idsUsed = array(); function __construct( $settings, $owner ) { $this->owner = $owner; if ( ! is_admin() ) { return; } // If we are just initializing, do not create our admin panels yet. During this phase all we want is to get all the options to create if ( TitanFramework::$initializing ) { return; } $this->settings = array_merge( $this->defaultSettings, $settings ); // $this->options = $options; if ( empty( $this->settings['name'] ) ) { return; } if ( empty( $this->settings['title'] ) ) { $this->settings['title'] = $this->settings['name']; } if ( empty( $this->settings['id'] ) ) { $prefix = ''; if ( ! empty( $this->settings['parent'] ) ) { $prefix = str_replace( ' ', '-', trim( strtolower( $this->settings['parent'] ) ) ) . '-'; } $this->settings['id'] = $prefix . str_replace( ' ', '-', trim( strtolower( $this->settings['name'] ) ) ); $this->settings['id'] = str_replace( '&', '-', $this->settings['id'] ); } // make sure all our IDs are unique $suffix = ''; while ( in_array( $this->settings['id'] . $suffix, self::$idsUsed ) ) { if ( $suffix === '' ) { $suffix = 2; } else { $suffix++; } } $this->settings['id'] .= $suffix; // keep track of all IDs used self::$idsUsed[] = $this->settings['id']; $priority = -1; if ( $this->settings['parent'] ) { $priority = intval( $this->settings['position'] ); } add_action( 'admin_menu', array( $this, 'register' ), $priority ); } public function createAdminPanel( $settings ) { $settings['parent'] = $this->settings['id']; return $this->owner->createAdminPanel( $settings ); } public function register() { // Parent menu if ( empty( $this->settings['parent'] ) ) { // Sub menu } else { $this->panelID = add_theme_page( $this->settings['name'], $this->settings['name'], $this->settings['capability'], $this->settings['id'], array( $this, 'createAdminPage' ) ); } add_action( 'load-' . $this->panelID, array( $this, 'saveOptions' ) ); add_action( 'load-' . $this->panelID, array( $this, 'addTitanCredit' ) ); } public function addTitanCredit() { add_filter( 'admin_footer_text', array( $this, 'addTitanCreditText' ) ); } public function addTitanCreditText() { echo __( "Options Page Created with Titan Framework", 'titan-framework' ); } public function getOptionNamespace() { return $this->owner->optionNamespace; } public function saveOptions() { if ( ! $this->verifySecurity() ) { return; } $message = ''; /* * Save */ if ( $_POST['action'] == 'save' ) { // we are in a tab $activeTab = $this->getActiveTab(); if ( ! empty( $activeTab ) ) { foreach ( $activeTab->options as $option ) { if ( empty( $option->settings['id'] ) ) { continue; } if ( ! empty( $_POST[$this->getOptionNamespace() . '_' . $option->settings['id']] ) ) { $value = $_POST[$this->getOptionNamespace() . '_' . $option->settings['id']]; } else { $value = ''; } // $value = $option->cleanValueForSaving( $value ); $this->owner->setOption( $option->settings['id'], $value ); } } foreach ( $this->options as $option ) { if ( empty( $option->settings['id'] ) ) { continue; } if ( ! empty( $_POST[$this->getOptionNamespace() . '_' . $option->settings['id']] ) ) { $value = $_POST[$this->getOptionNamespace() . '_' . $option->settings['id']]; } else { $value = ''; } $this->owner->setOption( $option->settings['id'], $value ); } $this->owner->saveOptions(); $message = 'saved'; /* * Reset */ } else if ( $_POST['action'] == 'reset' ) { // we are in a tab $activeTab = $this->getActiveTab(); if ( ! empty( $activeTab ) ) { foreach ( $activeTab->options as $option ) { if ( empty( $option->settings['id'] ) ) { continue; } $this->owner->setOption( $option->settings['id'], $option->settings['default'] ); } } foreach ( $this->options as $option ) { if ( empty( $option->settings['id'] ) ) { continue; } $this->owner->setOption( $option->settings['id'], $option->settings['default'] ); } $this->owner->saveOptions(); $message = 'reset'; } /* * Redirect to prevent refresh saving */ // urlencode to allow special characters in the url $url = wp_get_referer(); $activeTab = $this->getActiveTab(); $url = add_query_arg( 'page', urlencode( $this->settings['id'] ), $url ); if ( ! empty( $activeTab ) ) { $url = add_query_arg( 'tab', urlencode( $activeTab->settings['id'] ), $url ); } if ( ! empty( $message ) ) { $url = add_query_arg( 'message', $message, $url ); } do_action( 'tf_admin_options_saved_' . $this->getOptionNamespace() ); wp_redirect( $url ); } private function verifySecurity() { if ( empty( $_POST ) || empty( $_POST['action'] ) ) { return false; } $screen = get_current_screen(); if ( $screen->id != $this->panelID ) { return false; } if ( ! current_user_can( $this->settings['capability'] ) ) { return false; } if ( ! check_admin_referer( $this->settings['id'], TF . '_nonce' ) ) { return false; } return true; } public function getActiveTab() { if ( ! count( $this->tabs ) ) { return ''; } if ( ! empty( $this->activeTab ) ) { return $this->activeTab; } if ( empty( $_GET['tab'] ) ) { $this->activeTab = $this->tabs[0]; return $this->activeTab; } foreach ( $this->tabs as $tab ) { if ( $tab->settings['id'] == $_GET['tab'] ) { $this->activeTab = $tab; return $this->activeTab; } } $this->activeTab = $this->tabs[0]; return $this->activeTab; } public function createAdminPage() { do_action( 'tf_admin_page_before' ); do_action( 'tf_admin_page_before_' . $this->getOptionNamespace() ); ?>
settings['desc'] ?>