source: trunk/www.guidonia.net/wp/wp-content/themes/atahualpa/functions/bfa_text_widget.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 4.6 KB
Line 
1<?php // create new text widgets
2function widget_text($args, $widget_args = 1) {
3 extract( $args, EXTR_SKIP );
4 if ( is_numeric($widget_args) )
5 $widget_args = array( 'number' => $widget_args );
6 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
7 extract( $widget_args, EXTR_SKIP );
8
9 $options = get_option('widget_text');
10 if ( !isset($options[$number]) )
11 return;
12
13 $title = apply_filters('widget_title', $options[$number]['title']);
14 $text = apply_filters( 'widget_text', $options[$number]['text'] );
15?>
16 <?php echo $before_widget; ?>
17 <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
18 <div class="textwidget"><?php echo $text; ?></div>
19 <?php if ( !empty( $title ) ) { echo $after_widget; } else { echo "</div>"; } ?>
20<?php
21}
22// create new text widget controls
23function widget_text_control($widget_args = 1) {
24 global $wp_registered_widgets;
25 static $updated = false;
26
27 if ( is_numeric($widget_args) )
28 $widget_args = array( 'number' => $widget_args );
29 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
30 extract( $widget_args, EXTR_SKIP );
31
32 $options = get_option('widget_text');
33 if ( !is_array($options) )
34 $options = array();
35
36 if ( !$updated && !empty($_POST['sidebar']) ) {
37 $sidebar = (string) $_POST['sidebar'];
38
39 $sidebars_widgets = wp_get_sidebars_widgets();
40 if ( isset($sidebars_widgets[$sidebar]) )
41 $this_sidebar =& $sidebars_widgets[$sidebar];
42 else
43 $this_sidebar = array();
44
45 foreach ( $this_sidebar as $_widget_id ) {
46 if ( 'widget_text' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
47 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
48 if ( !in_array( "text-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
49 unset($options[$widget_number]);
50 }
51 }
52
53 foreach ( (array) $_POST['widget-text'] as $widget_number => $widget_text ) {
54 if ( !isset($widget_text['text']) && isset($options[$widget_number]) ) // user clicked cancel
55 continue;
56 $title = strip_tags(stripslashes($widget_text['title']));
57 if ( current_user_can('unfiltered_html') )
58 $text = stripslashes( $widget_text['text'] );
59 else
60 $text = stripslashes(wp_filter_post_kses( $widget_text['text'] ));
61 $options[$widget_number] = compact( 'title', 'text' );
62 }
63
64 update_option('widget_text', $options);
65 $updated = true;
66 }
67
68 if ( -1 == $number ) {
69 $title = '';
70 $text = '';
71 $number = '%i%';
72 } else {
73 $title = attribute_escape($options[$number]['title']);
74 $text = format_to_edit($options[$number]['text']);
75 }
76?>
77 <p>
78 <input class="widefat" id="text-title-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
79 <textarea class="widefat" rows="16" cols="20" id="text-text-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][text]"><?php echo $text; ?></textarea>
80 <input type="hidden" name="widget-text[<?php echo $number; ?>][submit]" value="1" />
81 </p>
82<?php
83}
84
85// unregister old / register new text widgets
86
87function widget_text_un_re_register() {
88 if ( !$options = get_option('widget_text') )
89 $options = array();
90 $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML','atahualpa'));
91 $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'text');
92 $name = __('Text','atahualpa');
93
94 $id = false;
95 foreach ( array_keys($options) as $o ) {
96 // Old widgets can have null values for some reason
97 if ( !isset($options[$o]['title']) || !isset($options[$o]['text']) )
98 continue;
99 $id = "text-$o"; // Never never never translate an id
100 unregister_sidebar_widget($id, $name, 'wp_widget_text', $widget_ops, array( 'number' => $o ));
101 unregister_widget_control($id, $name, 'wp_widget_text_control', $control_ops, array( 'number' => $o ));
102 wp_register_sidebar_widget($id, $name, 'widget_text', $widget_ops, array( 'number' => $o ));
103 wp_register_widget_control($id, $name, 'widget_text_control', $control_ops, array( 'number' => $o ));
104 }
105
106 // If there are none, we register the widget's existance with a generic template
107 if ( !$id ) {
108 unregister_sidebar_widget( 'text-1', $name, 'wp_widget_text', $widget_ops, array( 'number' => -1 ) );
109 unregister_widget_control( 'text-1', $name, 'wp_widget_text_control', $control_ops, array( 'number' => -1 ) );
110 wp_register_sidebar_widget( 'text-1', $name, 'widget_text', $widget_ops, array( 'number' => -1 ) );
111 wp_register_widget_control( 'text-1', $name, 'widget_text_control', $control_ops, array( 'number' => -1 ) );
112 }
113}
114add_action( 'widgets_init', 'widget_text_un_re_register' )
115?>
Note: See TracBrowser for help on using the repository browser.