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