Plugin Directory

Changeset 636359


Ignore:
Timestamp:
12/10/2012 01:42:34 AM (13 years ago)
Author:
markparolisi
Message:

better documenatation and stability

Location:
dashboard-widget-api/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • dashboard-widget-api/trunk

    • Property svn:ignore
      •  

        old new  
        1 deploy.sh
         1wpsvn-deploy.sh
        22README.md
         3test
         4tests
        35.git
        46.gitignore
  • dashboard-widget-api/trunk/dashboard-widget-api.php

    r485241 r636359  
    44  Plugin Name: Dashboard Widgets API
    55  Plugin URI: https://github.com/markparolisi/wp-dashboard-widget-api
    6   Description:
    7   Author: markparolisi
    8   Version: 0.3
     6  Description: Add configurable widgets to the WP dashboard using an interface similar to sidebar widgets.
     7  Author: markparolisi, voceplatforms
     8  Version: 1.0
    99  Author URI: http://markparolisi.com/
    1010 */
    11 if (!class_exists('Dashboard_Widgets')) {
     11if ( !class_exists( 'Dashboard_Widgets' ) ) {
    1212
    13     function register_dashboard_widget($classname) {
    14         Dashboard_Widgets::GetInstance()->add_dashboard_widget($classname);
    15     }
     13    function register_dashboard_widget( $classname ) {
     14        Dashboard_Widgets::GetInstance()->add_dashboard_widget( $classname );
     15    }
    1616
    17     class Dashboard_Widgets {
     17    /*   * *
     18     *
     19     */
    1820
    19         private static $instance;
    20         private $widgets = array();
     21    class Dashboard_Widgets {
    2122
    22         public static function GetInstance() {
    23             if (!isset(self::$instance)) {
    24                 self::$instance = new Dashboard_Widgets();
    25             }
    26             return self::$instance;
    27         }
     23        private static $instance;
     24        private $widgets = array( );
    2825
    29         public function __construct() {
    30             $this->setup();
    31         }
     26        /**
     27         * Singleton call
     28         * @return Dashboard_Widgets object
     29         */
     30        public static function GetInstance() {
     31            if ( !isset( self::$instance ) ) {
     32                self::$instance = new Dashboard_Widgets();
     33            }
     34            return self::$instance;
     35        }
    3236
    33         public function setup() {
    34             add_action('wp_dashboard_setup', array($this, 'action_add_dashboard_widgets'));
    35         }
     37        /**
     38         * @constructor
     39         */
     40        public function __construct() {
     41            $this->_setup();
     42        }
    3643
    37         public function add_dashboard_widget($classname) {
    38             $this->widgets[] = new Dashboard_Widget($classname);
    39         }
     44        /**
     45         * Make WP call to add dashboard widget
     46         * @return void
     47         */
     48        private function _setup() {
     49            add_action( 'wp_dashboard_setup', array( $this, 'action_add_dashboard_widgets' ) );
     50        }
    4051
    41         public function action_add_dashboard_widgets() {
    42             foreach ($this->widgets as $w) {
    43                 wp_add_dashboard_widget($w->id, $w->name, array($w, 'widget'), array($w, 'control_callback'));
    44             }
    45         }
     52        /**
     53         * Add Dasboard_Widget object to list of internal widgets
     54         * @param type $classname
     55         * @return boolean|Dashboard_Widget object
     56         */
     57        public function add_dashboard_widget( $classname ) {
     58            if ( !class_exists( $classname ) ) {
     59                return false;
     60            }
     61            $this->widgets[] = new Dashboard_Widget( $classname );
     62            return end( $this->widgets );
     63        }
    4664
    47     }
     65        /**
     66         * Called by WP action. Iterates through widgets and adds them to dashboard.
     67         * @return void
     68         */
     69        public function action_add_dashboard_widgets() {
     70            foreach ( $this->widgets as $w ) {
     71                wp_add_dashboard_widget( $w->id, $w->name, array( $w, 'widget' ), array( $w, 'control_callback' ) );
     72            }
     73        }
     74
     75    }
    4876
    4977}
     
    5179// end Dashboard_Widgets class
    5280
     81/**
     82 *
     83 */
    5384class Dashboard_Widget {
    5485
    55     public $widget;
    56     public $id;
    57     public $name;
    58     public $options = array();
     86    public $widget;
     87    public $id;
     88    public $name;
     89    public $options = array( );
    5990
    60     public function __construct($classname) {
    61         $w = new $classname;
    62         $this->widget = $w;
    63         $this->id = get_class($w);
    64         if ($this->widget->name)
    65             $this->name = $this->widget->name;
    66         else
    67             $this->name = ucwords(str_replace('_', ' ', $this->id));
    68     }
     91    /**
     92     * @constructor
     93     * @param string $classname
     94     */
     95    public function __construct( $classname ) {
     96        if ( class_exists( $classname ) ) {
     97            $w = new $classname;
     98            $this->widget = $w;
     99            $this->id = get_class( $w );
     100            if ( $this->widget->name ) {
     101                $this->name = $this->widget->name;
     102            } else {
     103                $this->name = ucwords( str_replace( '_', ' ', $this->id ) );
     104            }
     105        }
     106    }
    69107
    70     private function get_widget_options() {
    71         if (!$widget_options = get_option('dashboard_widget_options'))
    72             $widget_options = array();
    73         if (!isset($widget_options[$this->id]))
    74             $widget_options[$this->id] = array();
    75         return $widget_options;
    76     }
     108    /**
     109     * Return the array of widget data
     110     * @return array
     111     */
     112    private function _get_widget_options() {
     113        if ( !$widget_options = get_option( 'dashboard_widget_options' ) ) {
     114            $widget_options = array( );
     115        }
     116        if ( !isset( $widget_options[$this->id] ) ) {
     117            $widget_options[$this->id] = array( );
     118        }
     119        return $widget_options;
     120    }
    77121
    78     public function widget() {
    79         if (method_exists($this->widget, 'widget'))
    80             echo $this->widget->widget($this->get_widget_options());
    81         else
    82             echo "No widget display function defined";
    83     }
     122    /**
     123     * Execute the callback method for the widget
     124     * @return void
     125     */
     126    public function widget() {
     127        if ( method_exists( $this->widget, 'widget' ) ) {
     128            echo $this->widget->widget( $this->_get_widget_options() );
     129        } else {
     130            echo "No widget display function defined";
     131        }
     132    }
    84133
    85     public function control_callback() {
    86         $widget_options = $this->get_widget_options();
    87         if ('POST' == $_SERVER['REQUEST_METHOD'] && $_POST['widget_id'] == $this->id) {
    88             foreach ($_POST as $key => $val) {
    89                 if ($key == $this->id && is_array($val)) {
    90                     foreach ($val as $k => $v) {
    91                         $widget_options[$this->id][$k] = esc_attr($v);
    92                     }
    93                 }
    94             }
    95             update_option('dashboard_widget_options', $widget_options);
    96             delete_transient('dash_' . md5($this->id));
    97         }
    98         if (method_exists($this->widget, 'form'))
    99             $this->widget->form($widget_options);
    100         else
    101             echo "This Widget has no options";
    102     }
     134    /**
     135     * Create options for the dashboard widget
     136     * @return void
     137     */
     138    public function control_callback() {
     139        $widget_options = $this->_get_widget_options();
     140        if ( 'POST' == $_SERVER['REQUEST_METHOD'] && $_POST['widget_id'] == $this->id ) {
     141            foreach ( $_POST as $key => $val ) {
     142                var_dump( $key, $val );
     143                if ( $key == $this->id && is_array( $val ) ) {
     144                    foreach ( $val as $k => $v ) {
     145                        $widget_options[$this->id][$k] = esc_attr( $v );
     146                    }
     147                }
     148            }
     149            #die( var_dump( $widget_options ) );
     150            update_option( 'dashboard_widget_options', $widget_options );
     151            delete_transient( 'dash_' . md5( $this->id ) );
     152        }
     153        if ( method_exists( $this->widget, 'form' ) ) {
     154            $this->widget->form( $widget_options );
     155        } else {
     156            echo "This Widget has no options";
     157        }
     158    }
    103159
    104160}
    105 
    106 // end Dashboard_Widget class
  • dashboard-widget-api/trunk/readme.txt

    r485241 r636359  
    11=== Plugin Name ===
    2 Contributors: markparolisi
     2Contributors: markparolisi, voceplatforms
    33Tags: widget
     4Version: 1.0
    45Requires at least: 3.0
    5 Tested up to: 3.3.1
    6 Stable tag: 0.3
     6Tested up to: 3.4
     7Stable tag: 1.0
    78
    89Easily add configurable widgets to your dashboard using a simple class.
     
    2223== Changelog ==
    2324
     25= 1.0 =
     26* Documenation
     27* Better type checking and returns
     28* Style formatting
     29
    2430= 0.3 =
    25 
    2631Updating documentation
    2732
  • dashboard-widget-api/trunk/test-widget.php

    r484495 r636359  
    11<?php
    2 
     2/**
     3 * Demo Dashboard widget
     4 */
    35class Test_Widget {
    46
    5     public $name = 'Test Widget';
     7    public $name = 'Test Widget';
    68
    7     public function form($options) {
    8         $class = __CLASS__;
    9         $options = $options[$class];
    10         echo "<label>MyOption :<input type='text' name='{$class}[MyOption]' value='$options[MyOption]' /></label>";
    11     }
     9    public function form( $options ) {
     10        $class = __CLASS__;
     11        $options = $options[$class];
     12        echo '<label>First Name :<input type="text" name="'.$class.'[first_name]" value="' . $options['first_name'] . '" /></label>';
     13    }
    1214
    13     public function widget($options) {
    14         $options = $options[__CLASS__];
    15         return "this is $options[MyOption]'s Plugin";
    16     }
     15    public function widget( $options ) {
     16        $options = $options[__CLASS__];
     17        $first_name = (isset($options['first_name'])) ? $options['first_name'] : "No One";
     18        return "this is $first_name's Plugin";
     19    }
    1720
    1821}
    1922
    20 if (function_exists('register_dashboard_widget'))
    21     register_dashboard_widget('Test_Widget');
    22 
    23 
     23if ( function_exists( 'register_dashboard_widget' ) ) {
     24    register_dashboard_widget( 'Test_Widget' );
     25}
Note: See TracChangeset for help on using the changeset viewer.