Advertisement
sgaffney

tumbleboard.php

Nov 6th, 2011
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.80 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Plugin Name: Tumbleboard
  4.  * Plugin URI: http://theme.fm/?p=
  5.  * Description: Tumbleboard extends bbPress with a custom twentyten theme
  6.  * Version: 1.0
  7.  * Author: kovshenin
  8.  * Author URI: http://theme.fm
  9.  * License: GPL2
  10.  */
  11.  
  12. /*
  13.  * The main plugin class, holds everything our plugin does,
  14.  * initialized right after declaration
  15.  */
  16.  
  17.  
  18.  
  19. class Tumbleboard_Plugin {
  20.  
  21.     /*
  22.      * For easier overriding we declared the keys
  23.      * here as well as our tabs array which is populated
  24.      * when registering settings
  25.      */
  26.     private $general_settings_key = 'my_general_settings';
  27.     private $advanced_settings_key = 'my_advanced_settings';
  28.     private $plugin_options_key = 'my_plugin_options';
  29.     private $plugin_settings_tabs = array();
  30.  
  31.     /*
  32.      * Fired during plugins_loaded (very very early),
  33.      * so don't miss-use this, only actions and filters,
  34.      * current ones speak for themselves.
  35.      */
  36.     function __construct() {
  37.         add_action( 'init', array( &$this, 'load_settings' ) );
  38.         add_action( 'admin_init', array( &$this, 'register_general_settings' ) );
  39.         add_action( 'admin_init', array( &$this, 'register_advanced_settings' ) );
  40.         add_action( 'admin_menu', array( &$this, 'add_admin_menus' ) );
  41.     }
  42.  
  43.     /*
  44.      * Loads both the general and advanced settings from
  45.      * the database into their respective arrays. Uses
  46.      * array_merge to merge with default values if they're
  47.      * missing.
  48.      */
  49.     function load_settings() {
  50.         $this->general_settings = (array) get_option( $this->general_settings_key );
  51.         $this->advanced_settings = (array) get_option( $this->advanced_settings_key );
  52.  
  53.         // Merge with defaults
  54.         $this->general_settings = array_merge( array(
  55.             'general_option' => 'General value'
  56.         ), $this->general_settings );
  57.  
  58.         $this->advanced_settings = array_merge( array(
  59.             'advanced_option' => 'Advanced value'
  60.         ), $this->advanced_settings );
  61.     }
  62.  
  63.     /*
  64.      * Registers the general settings via the Settings API,
  65.      * appends the setting to the tabs array of the object.
  66.      */
  67.     function register_general_settings() {
  68.         $this->plugin_settings_tabs[$this->general_settings_key] = 'General';
  69.  
  70.         register_setting( $this->general_settings_key, $this->general_settings_key );
  71.         add_settings_section( 'section_general', 'General Plugin Settings', array( &$this, 'section_general_desc' ), $this->general_settings_key );
  72.         add_settings_field( 'general_option', 'A General Option', array( &$this, 'field_general_option' ), $this->general_settings_key, 'section_general' );
  73.     }
  74.  
  75.     /*
  76.      * Registers the advanced settings and appends the
  77.      * key to the plugin settings tabs array.
  78.      */
  79.     function register_advanced_settings() {
  80.         $this->plugin_settings_tabs[$this->advanced_settings_key] = 'Advanced';
  81.  
  82.         register_setting( $this->advanced_settings_key, $this->advanced_settings_key );
  83.         add_settings_section( 'section_advanced', 'Advanced Plugin Settings', array( &$this, 'section_advanced_desc' ), $this->advanced_settings_key );
  84.         add_settings_field( 'advanced_option', 'An Advanced Option', array( &$this, 'field_advanced_option' ), $this->advanced_settings_key, 'section_advanced' );
  85.     }
  86.  
  87.     /*
  88.      * The following methods provide descriptions
  89.      * for their respective sections, used as callbacks
  90.      * with add_settings_section
  91.      */
  92.     function section_general_desc() { echo 'General section description goes here.'; }
  93.     function section_advanced_desc() { echo 'Advanced section description goes here.'; }
  94.  
  95.     /*
  96.      * General Option field callback, renders a
  97.      * text input, note the name and value.
  98.      */
  99.     function field_general_option() {
  100.         ?>
  101.         <input type="text" name="<?php echo $this->general_settings_key; ?>[general_option]" value="<?php echo esc_attr( $this->general_settings['general_option'] ); ?>" />
  102.         <?php
  103.     }
  104.  
  105.     /*
  106.      * Advanced Option field callback, same as above.
  107.      */
  108.     function field_advanced_option() {
  109.         ?>
  110.         <input type="text" name="<?php echo $this->advanced_settings_key; ?>[advanced_option]" value="<?php echo esc_attr( $this->advanced_settings['advanced_option'] ); ?>" />
  111.         <?php
  112.     }
  113.  
  114.     /*
  115.      * Called during admin_menu, adds an options
  116.      * page under Settings called My Settings, rendered
  117.      * using the plugin_options_page method.
  118.      */
  119.     function add_admin_menus() {
  120.         add_options_page( 'Tumbleboard Settings', 'Tumbleboard', 'manage_options', $this->plugin_options_key, array( &$this, 'plugin_options_page' ) );
  121.     }
  122.  
  123.     /*
  124.      * Plugin Options page rendering goes here, checks
  125.      * for active tab and replaces key with the related
  126.      * settings key. Uses the plugin_options_tabs method
  127.      * to render the tabs.
  128.      */
  129.     function plugin_options_page() {
  130.         $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->general_settings_key;
  131.         ?>
  132.         <div class="wrap">
  133.             <?php $this->plugin_options_tabs(); ?>
  134.             <form method="post" action="options.php">
  135.                 <?php wp_nonce_field( 'update-options' ); ?>
  136.                 <?php settings_fields( $tab ); ?>
  137.                 <?php do_settings_sections( $tab ); ?>
  138.                 <?php submit_button(); ?>
  139.             </form>
  140.         </div>
  141.         <?php
  142.     }
  143.  
  144.     /*
  145.      * Renders our tabs in the plugin options page,
  146.      * walks through the object's tabs array and prints
  147.      * them one by one. Provides the heading for the
  148.      * plugin_options_page method.
  149.      */
  150.     function plugin_options_tabs() {
  151.         $current_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->general_settings_key;
  152.  
  153.         screen_icon();
  154.         echo '<h2 class="nav-tab-wrapper">';
  155.         foreach ( $this->plugin_settings_tabs as $tab_key => $tab_caption ) {
  156.             $active = $current_tab == $tab_key ? 'nav-tab-active' : '';
  157.             echo '<a class="nav-tab ' . $active . '" href="?page=' . $this->plugin_options_key . '&tab=' . $tab_key . '">' . $tab_caption . '</a>';
  158.         }
  159.         echo '</h2>';
  160.     }
  161. };
  162.  
  163. // Initialize the plugin
  164. add_action( 'plugins_loaded', create_function( '', '$Tumbleboard_Plugin = new Tumbleboard_Plugin;' ) );
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.     register_theme_directory( trailingslashit( dirname(__FILE__) ) );
  172.  
  173. ?>
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement