Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mehrshaddarzi/89b6b5e55920f61fb587349c8679c988 to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/89b6b5e55920f61fb587349c8679c988 to your computer and use it in GitHub Desktop.
Settings Class with Metabox
<?php
/* Prevent loading this file directly and/or if the class is already defined */
if ( ! defined( 'ABSPATH' ) || class_exists( 'MYFX_Settings_Class' ) )
return;
/**
* Settings with Meta Boxes
* Helper class to easily create settings page for plugin or theme with Meta Box API.
*
* This settings page helper class is inspired by Hybrid Core Settings page by Justin Tadlock
* and settings page with meta box example by Frank Bültge.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @version 0.1.0
* @author David Chandra Purnama <david.warna@gmail.com>
* @link http://shellcreeper.com
* @link https://gist.github.com/turtlepod/5203512
* @link https://github.com/justintadlock/hybrid-core
* @link https://gist.github.com/bueltge/757903
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* @copyright Copyright (c) 2013, David Chandra Purnama
*/
class MYFX_Settings_Class{
/**
* @var $config the config for the settings
* @access public
*/
var $config;
/**
* Class Constructor
* @param array $config the configuration required for the updater to work
* @return void
*/
public function __construct( $config = array() ) {
/* default config */
$defaults = array(
/* register settings */
'option_group' => '',
'option_name' => '',
/* add menu */
'page_title' => 'My Settings',
'menu_title' => 'My Settings Menu',
'capability' => 'manage_options',
'slug' => 'mysettings',
/* add sub menu */
'menu' => 'Settings',
'parent_slug' => 'themes.php',
/* add top level menu */
'menu_icon' => '',
'position' => 100,
/* submit button */
'submit' => 'Update Settings',
/* callback */
'sanitize_callback' => '',
'defaults_callback' => '',
);
/* merge configs and defaults */
$this->config = wp_parse_args( $config, $defaults );
$config = $this->config;
/* load only when minimum req pass */
if ( !empty( $config['option_group'] ) && !empty( $config['option_name'] ) ){
add_action( 'admin_menu', array( &$this, 'setup' ) );
if ( !empty( $config['sanitize_callback'] ) )
add_filter( 'sanitize_option_' . $config['option_name'], $config['sanitize_callback'] );
if ( !empty( $config['defaults_callback'] ) )
add_filter( $config['slug'] . '_default_settings', $config['defaults_callback'] );
}
}
/**
* Format settings data tor easy usage and sanitize
* @return array
*/
public function settings(){
/* get config */
$config = $this->config;
/* settings array */
$settings = array();
/* id */
$settings['id'] = esc_attr( $config['slug'] );
/* register_settings_args */
$settings['register'] = array(
'option_group' => esc_attr( $config['option_group'] ),
'option_name' => esc_attr( $config['option_name'] ),
);
/* page context, submenu/parent menu */
if ( $config['menu'] == 'toplevel' )
$settings['context'] = esc_attr( 'menu' );
else
$settings['context'] = esc_attr( 'submenu' );
/* add_page_args */
$settings['page'] = array(
'parent_slug' => esc_attr( $config['parent_slug'] ),
'page_title' => esc_attr( $config['page_title'] ),
'menu_title' => esc_attr( $config['menu_title'] ),
'capability' => esc_attr( $config['capability'] ),
'slug' => sanitize_title( $config['slug'] ),
'menu_icon' => ( $config['menu_icon'] == 'div' ? 'div' : esc_url( $config['menu_icon'] ) );,
'position' => absint( $config['position'] ),
);
/* submit text */
$settings['submit'] = esc_attr( $config['submit'] );
return $settings;
}
/**
* Settings Page Hook
* @return string
*/
public function page_hook() {
$config = $this->config;
$page_name = sanitize_title( $config['menu'] ) . '_page_' . $config['slug'];
return $page_name;
}
/**
* Capability
* @return string
*/
public function capability() {
$config = $this->config;
return $config['capability'];
}
/**
* Settings Setup
* @return void
*/
public function setup() {
/* get settings data */
$settings = $this->settings();
$add_page = $settings['page'];
$page_hook = $this->page_hook();
/* Register theme settings. */
register_setting(
$settings['register']['option_group'],
$settings['register']['option_name']
);
/* Create the settings page. */
if ( $settings['context'] == 'menu' ){
$settings_page = add_menu_page(
$add_page['page_title'],
$add_page['menu_title'],
$add_page['capability'],
$add_page['slug'],
array( &$this, 'settings_page' ),
$add_page['menu_icon'],
$add_page['position']
);
}
else{
$settings_page = add_submenu_page(
$add_page['parent_slug'],
$add_page['page_title'],
$add_page['menu_title'],
$add_page['capability'],
$add_page['slug'],
array( &$this, 'settings_page' )
);
}
/* Check if the settings page is being shown before running any functions for it. */
if ( !empty( $settings_page ) ) {
/* Filter the settings page capability so that it recognizes the cap. */
add_filter( "option_page_capability_{$page_hook}", array( &$this, 'capability' ) );
/* Screen layout column */
add_filter( 'screen_layout_columns', array( &$this, 'column' ), 10, 2 );
/* Create a hook for adding meta boxes. */
add_action( "load-{$page_hook}", array( &$this, 'add_meta_boxes' ) );
/* Load the JavaScript needed for the theme settings screen. */
add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_scripts' ) );
add_action( "admin_footer-{$page_hook}", array( &$this, 'load_scripts' ) );
}
}
/**
* Screen Column
*/
public function column( $columns, $screen ) {
$page_hook = $this->page_hook();
if ( $screen == $page_hook )
$columns[$page_hook] = 2;
return $columns;
}
/**
* Enqueue script
*/
public function enqueue_scripts( $hook_suffix ) {
$page_hook = $this->page_hook();
if ( $hook_suffix == $page_hook ){
wp_enqueue_script( 'common' );
wp_enqueue_script( 'wp-lists' );
wp_enqueue_script( 'postbox' );
}
}
/**
* Print script
*/
public function load_scripts() {
$page_hook = $this->page_hook();
?><script type="text/javascript">
//<![CDATA[
jQuery(document).ready( function($) {
$('.if-js-closed').removeClass('if-js-closed').addClass('closed');
postboxes.add_postbox_toggles( '<?php echo $page_hook; ?>' );
});
//]]>
</script><?php
}
/**
* Add meta boxes
*/
public function add_meta_boxes() {
do_action( 'add_meta_boxes', array( &$this, 'page_hook' ) );
}
/**
* Settings page header
*/
public function header() {
$settings = $this->settings();
$header = get_screen_icon('options-general');
$header .= '<h2>' . $settings['page']['page_title'] . '</h2>';
return apply_filters( $settings['id'] . '_settings_page_header', $header );
}
/**
* Settings page html
*/
public function settings_page() {
global $screen_layout_columns;
$settings = $this->settings();
$page_hook = $this->page_hook();
do_action( "{$settings['id']}_before_settings_page" ); ?>
<div class="wrap">
<?php echo $this->header(); ?>
<?php settings_errors(); ?>
<?php do_action( "{$settings['id']}_open_settings_page" ); ?>
<div class="<?php echo $settings['id']; ?>-settings-wrap">
<form method="post" action="options.php">
<?php settings_fields( $settings['register']['option_group'] ); ?>
<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div id="postbox-container-1" class="postbox-container">
<?php do_meta_boxes( $page_hook, 'side', null ); ?>
<!-- #side-sortables -->
</div><!-- #postbox-container-1 -->
<div id="postbox-container-2" class="postbox-container">
<?php do_meta_boxes( $page_hook, 'normal', null ); ?>
<!-- #normal-sortables -->
<?php do_meta_boxes( $page_hook, 'advanced', null ); ?>
<!-- #advanced-sortables -->
</div><!-- #postbox-container-2 -->
</div><!-- #post-body -->
<br class="clear">
</div><!-- #poststuff -->
<?php submit_button( esc_attr( $settings['submit'] ) ); ?>
</form>
</div><!-- .{$settings['id']}-settings-wrap -->
<?php do_action( "{$settings['id']}_close_settings_page" ); ?>
</div><!-- .wrap --><?php
do_action( "{$settings['id']}_after_settings_page" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment