Changeset 636359
- Timestamp:
- 12/10/2012 01:42:34 AM (13 years ago)
- Location:
- dashboard-widget-api/trunk
- Files:
-
- 4 edited
-
. (modified) (1 prop)
-
dashboard-widget-api.php (modified) (2 diffs)
-
readme.txt (modified) (2 diffs)
-
test-widget.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
dashboard-widget-api/trunk
- Property svn:ignore
-
old new 1 deploy.sh1 wpsvn-deploy.sh 2 2 README.md 3 test 4 tests 3 5 .git 4 6 .gitignore
-
- Property svn:ignore
-
dashboard-widget-api/trunk/dashboard-widget-api.php
r485241 r636359 4 4 Plugin Name: Dashboard Widgets API 5 5 Plugin URI: https://github.com/markparolisi/wp-dashboard-widget-api 6 Description: 7 Author: markparolisi 8 Version: 0.36 Description: Add configurable widgets to the WP dashboard using an interface similar to sidebar widgets. 7 Author: markparolisi, voceplatforms 8 Version: 1.0 9 9 Author URI: http://markparolisi.com/ 10 10 */ 11 if ( !class_exists('Dashboard_Widgets')) {11 if ( !class_exists( 'Dashboard_Widgets' ) ) { 12 12 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 } 16 16 17 class Dashboard_Widgets { 17 /* * * 18 * 19 */ 18 20 19 private static $instance; 20 private $widgets = array(); 21 class Dashboard_Widgets { 21 22 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( ); 28 25 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 } 32 36 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 } 36 43 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 } 40 51 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 } 46 64 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 } 48 76 49 77 } … … 51 79 // end Dashboard_Widgets class 52 80 81 /** 82 * 83 */ 53 84 class Dashboard_Widget { 54 85 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( ); 59 90 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 } 69 107 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 } 77 121 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 } 84 133 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 } 103 159 104 160 } 105 106 // end Dashboard_Widget class -
dashboard-widget-api/trunk/readme.txt
r485241 r636359 1 1 === Plugin Name === 2 Contributors: markparolisi 2 Contributors: markparolisi, voceplatforms 3 3 Tags: widget 4 Version: 1.0 4 5 Requires at least: 3.0 5 Tested up to: 3. 3.16 Stable tag: 0.36 Tested up to: 3.4 7 Stable tag: 1.0 7 8 8 9 Easily add configurable widgets to your dashboard using a simple class. … … 22 23 == Changelog == 23 24 25 = 1.0 = 26 * Documenation 27 * Better type checking and returns 28 * Style formatting 29 24 30 = 0.3 = 25 26 31 Updating documentation 27 32 -
dashboard-widget-api/trunk/test-widget.php
r484495 r636359 1 1 <?php 2 2 /** 3 * Demo Dashboard widget 4 */ 3 5 class Test_Widget { 4 6 5 public $name = 'Test Widget';7 public $name = 'Test Widget'; 6 8 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 } 12 14 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 } 17 20 18 21 } 19 22 20 if (function_exists('register_dashboard_widget')) 21 register_dashboard_widget('Test_Widget'); 22 23 23 if ( function_exists( 'register_dashboard_widget' ) ) { 24 register_dashboard_widget( 'Test_Widget' ); 25 }
Note: See TracChangeset
for help on using the changeset viewer.