| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * #9701 Test Plugin |
|---|
| 4 | * |
|---|
| 5 | * Plugin Name: #9701 Test Plugin |
|---|
| 6 | * Plugin URI: http://core.trac.wordpress.org/ticket/9701 |
|---|
| 7 | * Description: Test Plugin to provide a Widget and a Callback to output that Widget with a new "the_widget" template tag. It's function is demonstrated within the footer. |
|---|
| 8 | * Author: hakre |
|---|
| 9 | * Version: 0.2 |
|---|
| 10 | * Author URI: http://codex.wordpress.org/User:Hakre |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * Test Plugin Class |
|---|
| 15 | */ |
|---|
| 16 | class plugin9701test { |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * constructor |
|---|
| 20 | * |
|---|
| 21 | * @return |
|---|
| 22 | */ |
|---|
| 23 | function plugin9701test() { |
|---|
| 24 | |
|---|
| 25 | // register widget |
|---|
| 26 | add_action('widgets_init', create_function('', 'return register_widget(\'widget9701\');')); |
|---|
| 27 | |
|---|
| 28 | // footer output |
|---|
| 29 | add_action('wp_footer', array($this, 'wp_footer')); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | function wp_footer() { |
|---|
| 33 | ?> |
|---|
| 34 | <ul> |
|---|
| 35 | <?php the_widget('widget9701', array('title' => 'Demo Widget w/o Sidebar')); // Self-Demo?> |
|---|
| 36 | <?php the_widget('WP_Widget_Recent_Posts'); // Widget Classname Example ?> |
|---|
| 37 | <?php the_widget('Recent Comments'); // Widget Name Example ?> |
|---|
| 38 | </ul> |
|---|
| 39 | <?php |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | $plugin9701test = new plugin9701test(); |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Test Widget Class |
|---|
| 47 | * |
|---|
| 48 | */ |
|---|
| 49 | class widget9701 Extends WP_Widget { |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * constructor |
|---|
| 53 | * |
|---|
| 54 | * @return instance of self |
|---|
| 55 | */ |
|---|
| 56 | function widget9701() { |
|---|
| 57 | parent::WP_Widget(false, '#9701 Widget'); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /** Echo the widget content. |
|---|
| 61 | * |
|---|
| 62 | * Subclasses should over-ride this function to generate their widget code. |
|---|
| 63 | * |
|---|
| 64 | * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
|---|
| 65 | * @param array $instance The settings for the particular instance of the widget |
|---|
| 66 | */ |
|---|
| 67 | function widget($args, $instance) { |
|---|
| 68 | |
|---|
| 69 | extract( $args ); |
|---|
| 70 | extract( $instance ); |
|---|
| 71 | |
|---|
| 72 | if (!strlen($title)) $title = '#9701 Test (Default Title)'; |
|---|
| 73 | |
|---|
| 74 | echo $before_widget; |
|---|
| 75 | echo $before_title . $title . $after_title; |
|---|
| 76 | ?> |
|---|
| 77 | |
|---|
| 78 | <div style="font-size:1.5em; overflow:scroll;"> |
|---|
| 79 | This widget displays it's innerst values:<br /> |
|---|
| 80 | <?php var_dump($args, $instance); ?> |
|---|
| 81 | </div> |
|---|
| 82 | |
|---|
| 83 | <?php |
|---|
| 84 | echo $after_widget; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** Update a particular instance. |
|---|
| 88 | * |
|---|
| 89 | * This function should check that $new_instance is set correctly. |
|---|
| 90 | * The newly calculated value of $instance should be returned. |
|---|
| 91 | * If "false" is returned, the instance won't be saved/updated. |
|---|
| 92 | * |
|---|
| 93 | * @param array $new_instance New settings for this instance as input by the user via form() |
|---|
| 94 | * @param array $old_instance Old settings for this instance |
|---|
| 95 | * @return array Settings to save or bool false to cancel saving |
|---|
| 96 | */ |
|---|
| 97 | function update($new_instance, $old_instance) { |
|---|
| 98 | return $new_instance; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /** Echo the settings update form |
|---|
| 102 | * |
|---|
| 103 | * @param array $instance Current settings |
|---|
| 104 | */ |
|---|
| 105 | function form($instance) { |
|---|
| 106 | $title = attr($instance['title']); |
|---|
| 107 | ?> |
|---|
| 108 | <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p> |
|---|
| 109 | <?php |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** widget template tag |
|---|
| 115 | * |
|---|
| 116 | * get a configured widgets output without the need of a sidebar and backend administration. |
|---|
| 117 | * |
|---|
| 118 | * NOTE: this is development code |
|---|
| 119 | * |
|---|
| 120 | * @global WP_Widget_Factory $wp_widget_factory |
|---|
| 121 | * @global array $wp_registered_sidebars |
|---|
| 122 | * |
|---|
| 123 | * @param string $widget widget identifier |
|---|
| 124 | * @param array $settings optional widget settings |
|---|
| 125 | * @param array $args optional sidebar options |
|---|
| 126 | * @return void |
|---|
| 127 | */ |
|---|
| 128 | function the_widget($widget, $settings = array(), $args = array()) { |
|---|
| 129 | |
|---|
| 130 | /* sanitize input */ |
|---|
| 131 | $widget = (string) $widget; |
|---|
| 132 | if (!strlen($widget)) |
|---|
| 133 | return; |
|---|
| 134 | if (!is_array($settings)) |
|---|
| 135 | return; |
|---|
| 136 | if (!is_array($args)) |
|---|
| 137 | return; |
|---|
| 138 | |
|---|
| 139 | /* globals */ |
|---|
| 140 | global $wp_widget_factory, $wp_registered_sidebars; |
|---|
| 141 | |
|---|
| 142 | /* pull default (def) sidebar options (args) */ |
|---|
| 143 | $sidebar_id = register_sidebar( array('name' => uniqid('temp-')) ); |
|---|
| 144 | $def = $wp_registered_sidebars[$sidebar_id]; |
|---|
| 145 | $args = array_merge($def, $args); |
|---|
| 146 | |
|---|
| 147 | /* get widget ($instance) */ |
|---|
| 148 | $instance = null; |
|---|
| 149 | if (isset($wp_widget_factory->widgets[$widget])) { |
|---|
| 150 | $instance = $wp_widget_factory->widgets[$widget]; |
|---|
| 151 | } else { |
|---|
| 152 | foreach($wp_widget_factory->widgets as $value) |
|---|
| 153 | if (isset($value->name)) |
|---|
| 154 | if ($value->name == $widget) { |
|---|
| 155 | $instance = $value; |
|---|
| 156 | break; |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | /* display widget */ |
|---|
| 161 | if (is_a($instance, 'WP_Widget')) { |
|---|
| 162 | $settings = apply_filters('widget_display_callback', $settings, $instance); |
|---|
| 163 | if ( false !== $settings ) |
|---|
| 164 | $instance->widget($args, $settings); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | unregister_sidebar($sidebar_id); |
|---|
| 168 | } |
|---|