-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathenhanced-text-widget.php
More file actions
187 lines (156 loc) · 8.35 KB
/
enhanced-text-widget.php
File metadata and controls
187 lines (156 loc) · 8.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/*
Plugin Name: Enhanced Text Widget
Plugin URI: http://wordpress.org/plugins/enhanced-text-widget/
Description: An enhanced version of the default text widget where you may have Text, HTML, CSS, JavaScript, Flash, Shortcodes, and/or PHP as content with linkable widget title.
Version: 1.4.6
Author: Boston Dell-Vandenberg
Author URI: http://bostondv.com/
Text Domain: enhancedtext
Domain Path: /languages/
License: MIT
*/
class EnhancedTextWidget extends WP_Widget {
/**
* Widget construction
*/
function __construct() {
$widget_ops = array('classname' => 'widget_text enhanced-text-widget', 'description' => __('Text, HTML, CSS, PHP, Flash, JavaScript, Shortcodes', 'enhancedtext'));
$control_ops = array('width' => 450);
parent::__construct('EnhancedTextWidget', __('Enhanced Text', 'enhancedtext'), $widget_ops, $control_ops);
load_plugin_textdomain('enhancedtext', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Setup the widget output
*/
function widget( $args, $instance ) {
if (!isset($args['widget_id'])) {
$args['widget_id'] = null;
}
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance);
$titleUrl = empty($instance['titleUrl']) ? '' : $instance['titleUrl'];
$cssClass = empty($instance['cssClass']) ? '' : $instance['cssClass'];
$text = apply_filters('widget_enhanced_text', $instance['text'], $instance);
$hideTitle = !empty($instance['hideTitle']) ? true : false;
$hideEmpty = !empty($instance['hideEmpty']) ? true : false;
$newWindow = !empty($instance['newWindow']) ? true : false;
$filterText = !empty($instance['filter']) ? true : false;
$bare = !empty($instance['bare']) ? true : false;
if ( $cssClass ) {
if( strpos($before_widget, 'class') === false ) {
$before_widget = str_replace('>', 'class="'. $cssClass . '"', $before_widget);
} else {
$before_widget = str_replace('class="', 'class="'. $cssClass . ' ', $before_widget);
}
}
// Parse the text through PHP
ob_start();
eval('?>' . $text);
$text = ob_get_contents();
ob_end_clean();
// Run text through do_shortcode
$text = do_shortcode($text);
if (!empty($text) || !$hideEmpty) {
echo $bare ? '' : $before_widget;
if ($newWindow) $newWindow = "target='_blank'";
if(!$hideTitle && $title) {
if($titleUrl) $title = "<a href='$titleUrl' $newWindow>$title</a>";
echo $bare ? $title : $before_title . $title . $after_title;
}
echo $bare ? '' : '<div class="textwidget widget-text">';
// Echo the content
echo $filterText ? wpautop($text) : $text;
echo $bare ? '' : '</div>' . $after_widget;
}
}
/**
* Run on widget update
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
if ( current_user_can('unfiltered_html') )
$instance['text'] = $new_instance['text'];
else
$instance['text'] = wp_filter_post_kses($new_instance['text']);
$instance['titleUrl'] = strip_tags($new_instance['titleUrl']);
$instance['cssClass'] = strip_tags($new_instance['cssClass']);
$instance['hideTitle'] = isset($new_instance['hideTitle']);
$instance['hideEmpty'] = isset($new_instance['hideEmpty']);
$instance['newWindow'] = isset($new_instance['newWindow']);
$instance['filter'] = isset($new_instance['filter']);
$instance['bare'] = isset($new_instance['bare']);
return $instance;
}
/**
* Setup the widget admin form
*/
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array(
'title' => '',
'titleUrl' => '',
'cssClass' => '',
'text' => ''
));
$title = $instance['title'];
$titleUrl = $instance['titleUrl'];
$cssClass = $instance['cssClass'];
$text = format_to_edit($instance['text']);
?>
<style>
.monospace {
font-family: Consolas, Lucida Console, monospace;
}
.etw-credits {
font-size: 0.9em;
background: #F7F7F7;
border: 1px solid #EBEBEB;
padding: 4px 6px;
}
</style>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'enhancedtext'); ?>:</label>
<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; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('titleUrl'); ?>"><?php _e('URL', 'enhancedtext'); ?>:</label>
<input class="widefat" id="<?php echo $this->get_field_id('titleUrl'); ?>" name="<?php echo $this->get_field_name('titleUrl'); ?>" type="text" value="<?php echo $titleUrl; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('cssClass'); ?>"><?php _e('CSS Classes', 'enhancedtext'); ?>:</label>
<input class="widefat" id="<?php echo $this->get_field_id('cssClass'); ?>" name="<?php echo $this->get_field_name('cssClass'); ?>" type="text" value="<?php echo $cssClass; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('text'); ?>"><?php _e('Content', 'enhancedtext'); ?>:</label>
<textarea class="widefat monospace" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
</p>
<p>
<input id="<?php echo $this->get_field_id('hideTitle'); ?>" name="<?php echo $this->get_field_name('hideTitle'); ?>" type="checkbox" <?php checked(isset($instance['hideTitle']) ? $instance['hideTitle'] : 0); ?> /> <label for="<?php echo $this->get_field_id('hideTitle'); ?>"><?php _e('Do not display the title', 'enhancedtext'); ?></label>
</p>
<p>
<input id="<?php echo $this->get_field_id('hideEmpty'); ?>" name="<?php echo $this->get_field_name('hideEmpty'); ?>" type="checkbox" <?php checked(isset($instance['hideEmpty']) ? $instance['hideEmpty'] : 0); ?> /> <label for="<?php echo $this->get_field_id('hideEmpty'); ?>"><?php _e('Do not display empty widgets', 'enhancedtext'); ?></label>
</p>
<p>
<input type="checkbox" id="<?php echo $this->get_field_id('newWindow'); ?>" name="<?php echo $this->get_field_name('newWindow'); ?>" <?php checked(isset($instance['newWindow']) ? $instance['newWindow'] : 0); ?> />
<label for="<?php echo $this->get_field_id('newWindow'); ?>"><?php _e('Open the URL in a new window', 'enhancedtext'); ?></label>
</p>
<p>
<input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs to the content', 'enhancedtext'); ?></label>
</p>
<p>
<input id="<?php echo $this->get_field_id('bare'); ?>" name="<?php echo $this->get_field_name('bare'); ?>" type="checkbox" <?php checked(isset($instance['bare']) ? $instance['bare'] : 0); ?> /> <label for="<?php echo $this->get_field_id('bare'); ?>"><?php _e('Do not output before/after_widget/title', 'enhancedtext'); ?></label>
</p>
<p class="etw-credits">
<?php _e('Enjoy this plugin? <a href="http://bostondv.com/tips/" target="_blank">Send a tip to support development</a>.', 'enhancedtext'); ?>
</p>
<?php
}
}
/**
* Register the widget
*/
function enhanced_text_widget_init() {
register_widget('EnhancedTextWidget');
}
add_action('widgets_init', 'enhanced_text_widget_init');