Plugin Directory

Changeset 374609


Ignore:
Timestamp:
04/19/2011 03:41:08 AM (15 years ago)
Author:
forgueam
Message:

Added Vbpress_Settings class, created settings page with two basic options for testing.

Location:
vbpress/trunk
Files:
3 added
1 edited

Legend:

Unmodified
Added
Removed
  • vbpress/trunk/vbpress.php

    r374590 r374609  
    1313
    1414class Vbpress {
    15 
     15   
    1616    /**
    1717     * Singleton
    18      * @static
    1918     */
    2019    function &init() {
     
    3029
    3130    /**
    32      * Constructor.  Initializes WordPress hooks
     31     * Constructor. Initializes WordPress hooks
    3332     */
    3433    function Vbpress() {
     
    4039    /**
    4140     * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
    42      * @static
    4341     */
    4442    function plugin_activation() {
     
    4846    /**
    4947     * Removes all options.
    50      * @static
    5148     */
    5249    function plugin_deactivation() {
     
    5855     */
    5956    function admin_menu() {
    60         $hook = add_menu_page( 'vBPress', 'vBPress', 'manage_options', 'vbpress', array( $this, 'admin_page' ), '' );
     57        $hook = add_menu_page( 'vBPress', 'vBPress', 'manage_options', 'vbpress_settings', array( $this, 'settings' ), '' );
    6158    }
    62    
    6359
    6460    /**
    6561     * vBPress settings page
    6662     */
    67     function admin_page() {
     63    function settings() {
    6864   
     65        $options = new Vbpress_Options();
     66   
     67        if ( !empty($_POST) && !wp_verify_nonce($_POST['vbpress_update_settings'], 'vbpress_update_settings') ) {
     68            echo 'Sorry, your nonce did not verify.';
     69            exit;
     70        } else if ( !empty($_POST) ) {
     71       
     72            foreach ( $_POST as $name => $value ) {
     73                $options->set($name, $value);
     74            }
     75           
     76            $options->save();
     77        }
     78       
     79        echo '<pre>'.print_r($options->get(), 1).'</pre>';
     80       
     81        // TODO: Could we create 'View' class that is used for loading action views?
     82        require('core/views/settings.php');
     83    }
     84}
     85
     86class Vbpress_Options {
     87
     88    var $optionName = 'vbpress';
     89
     90    var $validOptionNames = array(
     91        'enabled',
     92        'vbulletin_path'
     93    );
     94
     95    var $options = array();
     96   
     97    /**
     98     * Constructor
     99     */
     100    function Vbpress_Options() {
     101        $this->load();
     102    }
     103   
     104    /**
     105     * Populates $options from WordPress database
     106     */
     107    function load() {
     108        $options = get_option($this->optionName, array());
     109        if (!empty($options)) {
     110            $options = $this->decode($options);
     111        }
     112    }
     113   
     114    /**
     115     * Get a specified vBPress option
     116     */
     117    function get($name = '') {
     118        if ($name == '') {
     119            return $this->options;
     120        } if (isset($this->options[$name])) {
     121            return $this->options[$name];
     122        }
     123       
     124        return null;
     125    }
     126   
     127    /**
     128     * Set a specified vBPress option
     129     */
     130    function set($name, $value, $unset = false) {
     131   
     132        // Strip out the prefix that we use in the forms
     133        $name = str_replace('vbpress_', '', $name);
     134       
     135        if ($unset && isset($this->options[$name])) {
     136            unset($this->options[$name]);
     137        } else if ( in_array($name, $this->validOptionNames) ) {
     138            $this->options[$name] = $value;
     139        }
     140    }
     141   
     142    /**
     143     * Add or update the options in the WordPress database
     144     */
     145    function save() {
     146        if (!get_option($this->optionName)) {
     147            add_option($this->optionName, $this->encode($this->options));
     148        } else {
     149            update_option($this->optionName, $this->encode($this->options));
     150        }
     151    }
     152   
     153    /*
     154     * Removes all vBPress option data from the WordPress database
     155     */
     156    function purge() {
     157        return (delete_option($this->optionName) ? true : false);
     158    }
     159   
     160    /**
     161     * Encode vBPress option data
     162     */
     163    function encode($data) {
     164        // TODO: Can we make the assumption that json_encode is an available function?
     165        return json_encode($data);
     166    }
     167   
     168    /**
     169     * Decode vBPress option data
     170     */
     171    function decode($data) {
     172        return !empty($data) ? json_decode($data) : null;
    69173    }
    70174}
Note: See TracChangeset for help on using the changeset viewer.