source: trunk/www.guidonia.net/wp/wp-content/plugins/parteibuch-aggregator/pba-widgets.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 6.2 KB
Line 
1<?php
2
3 //let's make some widgets
4 //Thanks to brainfart for the lesson ;-)
5 //http://brainfart.com.ua/post/lesson-wordpress-multi-widgets/
6
7 add_action('init', 'pba_register_widgets');
8
9function pba_register_widgets() {
10 if(!function_exists('wp_register_sidebar_widget')) return false;
11
12 $prefix = 'pba_itemtitles'; // $id prefix
13 $name = __('RSS Aggregator');
14 $widget_ops = array('classname' => 'widget_name_multi', 'description' => __('Add item titles of your aggregated feeds to your blog&#39;s sidebar'));
15 $control_ops = array('width' => 200, 'height' => 200, 'id_base' => $prefix);
16
17 $options = get_option('widget_name_multi');
18 if(isset($options[0])) unset($options[0]);
19
20 if(!empty($options)){
21 foreach(array_keys($options) as $widget_number){
22 wp_register_sidebar_widget($prefix.'-'.$widget_number, $name, 'widget_name_multi', $widget_ops, array( 'number' => $widget_number ));
23 wp_register_widget_control($prefix.'-'.$widget_number, $name, 'widget_name_multi_control', $control_ops, array( 'number' => $widget_number ));
24 }
25 } else{
26 $options = array();
27 $widget_number = 1;
28 wp_register_sidebar_widget($prefix.'-'.$widget_number, $name, 'widget_name_multi', $widget_ops, array( 'number' => $widget_number ));
29 wp_register_widget_control($prefix.'-'.$widget_number, $name, 'widget_name_multi_control', $control_ops, array( 'number' => $widget_number ));
30 }
31}
32
33function widget_name_multi($args) {
34 if(!function_exists('wp_register_sidebar_widget')) return false;
35 $prefix = 'pba_itemtitles'; // $id prefix
36 extract($args);
37 $options = get_option('widget_name_multi');
38
39// echo "<br>See the calling arguments for the widget with id " . $widget_id . ":<br>";
40// print_r($args);
41// echo "<br>See all the saved options:";
42// print_r($options);
43
44 if(preg_match('/'.$prefix.'-([0-9]+)/i', $widget_id, $match)){
45 $widget_number = $match[1];
46 //echo "<br>Fine, this is our widget number " . $widget_number;
47 if(isset($options[$widget_number])){
48 $pba_option_set=$options[$widget_number];
49// echo "<br>Great, we have an option set, which we can use as input parameter to our stuff: ";
50 //print_r($pba_option_set);
51 echo $before_widget; echo $before_title;
52 if(isset($pba_option_set['title'])) echo $pba_option_set['title'];
53 echo $after_title;
54 if(isset($pba_option_set['pba_outputid'])) {
55 //echo "Output ID: " . $pba_option_set['pba_outputid'];
56 $pba_config['outputid']=$pba_option_set['pba_outputid'];
57 $pba_config['show_sidebarwidget'] = 'Y';
58 $pba_return=@PBA::outputwrapper($pba_config);
59 echo $pba_return["result"];
60 }
61 echo $after_widget;
62 }
63 }
64 }
65
66function widget_name_multi_control($args) {
67 if(!function_exists('wp_register_sidebar_widget')) return false;
68
69 $prefix = 'pba_itemtitles'; // $id prefix
70
71 $options = get_option('widget_name_multi');
72 if(empty($options)) $options = array();
73 if(isset($options[0])) unset($options[0]);
74
75 // update options array
76 if(!empty($_POST[$prefix]) && is_array($_POST)){
77 foreach($_POST[$prefix] as $widget_number => $values){
78 if(empty($values) && isset($options[$widget_number])) // user clicked cancel
79 continue;
80
81 if(!isset($options[$widget_number]) && $args['number'] == -1){
82 $args['number'] = $widget_number;
83 $options['last_number'] = $widget_number;
84 }
85 $options[$widget_number] = $values;
86 }
87
88 // update number
89 if($args['number'] == -1 && !empty($options['last_number'])){
90 $args['number'] = $options['last_number'];
91 }
92
93 // clear unused options and update options in DB. return actual options array
94 $options = bf_smart_multiwidget_update($prefix, $options, $_POST[$prefix], $_POST['sidebar'], 'widget_name_multi');
95 }
96
97 // $number - is dynamic number for multi widget, gived by WP
98 // by default $number = -1 (if no widgets activated). In this case we should use %i% for inputs
99 // to allow WP generate number automatically
100 $number = ($args['number'] == -1)? '%i%' : $args['number'];
101
102 // now we can output control
103 $opts = @$options[$number];
104
105 $title = @$opts['title'];
106
107 echo 'Please enter a name for your widget:<br />';
108 echo '<input type="text" ' .
109 'name="' . $prefix . '[' . $number . '][title]" ' .
110 'value="' . $title . '" /><br /><br />';
111
112 global $bdprss_db;
113 $pba_output_streams=$bdprss_db->get_all_pbaoutputs();
114 if($pba_output_streams) {
115 //print_r($pba_output_streams);
116 echo 'Please select an output stream:<br />';
117 echo '<select name="' . $prefix . '[' . $number . '][pba_outputid]">';
118 $didselect=false;
119 foreach($pba_output_streams as $pbaosnr => $pba_os_object){
120 $selected="";
121 if($pba_os_object->identifier == @$opts['pba_outputid']) {
122 $selected=" selected ";
123 $didselect=true;
124 }
125 if(($pbaosnr + 1) == count($pba_output_streams) && !$didselect) $selected=" selected ";
126 echo '<option value="'.$pba_os_object->identifier.'"'.$selected.' >ID: '.$pba_os_object->identifier.'&nbsp;-&nbsp;'.$pba_os_object->name.'&nbsp;</option>' . "\n";
127 }
128 echo '</select>';
129 }else{
130 echo 'Parteibuch Aggregator could not detect any output format to show in this widget.
131 You need to <a href="edit.php?page=parteibuch-aggregator/bdp-rssadmin.php&action=createpbaoutput">create an output format</a> first.';
132 }
133}
134
135// helper function can be defined in another plugin
136if(!function_exists('bf_smart_multiwidget_update')){
137 function bf_smart_multiwidget_update($id_prefix, $options, $post, $sidebar, $option_name = ''){
138 global $wp_registered_widgets;
139 static $updated = false;
140
141 // get active sidebar
142 $sidebars_widgets = wp_get_sidebars_widgets();
143 if ( isset($sidebars_widgets[$sidebar]) )
144 $this_sidebar =& $sidebars_widgets[$sidebar];
145 else
146 $this_sidebar = array();
147
148 // search unused options
149 foreach ( $this_sidebar as $_widget_id ) {
150 if(preg_match('/'.$id_prefix.'-([0-9]+)/i', $_widget_id, $match)){
151 $widget_number = $match[1];
152
153 // $_POST['widget-id'] contain current widgets set for current sidebar
154 // $this_sidebar is not updated yet, so we can determine which was deleted
155 if(!in_array($match[0], $_POST['widget-id'])){
156 unset($options[$widget_number]);
157 }
158 }
159 }
160
161 // update database
162 if(!empty($option_name)){
163 update_option($option_name, $options);
164 $updated = true;
165 }
166
167 // return updated array
168 return $options;
169 }
170}
171
172?>
Note: See TracBrowser for help on using the repository browser.