-
Notifications
You must be signed in to change notification settings - Fork 2
/
jmh_pew.php
259 lines (229 loc) · 6.25 KB
/
jmh_pew.php
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/*
Plugin Name: Page Excerpt Widget
Plugin URI: http://jonathanmh.com/wordpress-page-excerpt-widget/
Description: Plugin for displaying a page excerpt in a widget area
Author: Jonathan M. Hethey
Version: 0.3
Author URI: http://jonathanmh.com
*/
global $wp_version;
if((float)$wp_version >= 2.8){
class PageExcerptWidget extends WP_Widget {
/*
* construct
*/
function PageExcerptWidget() {
parent::WP_Widget(
'PageExcerptWidget'
, 'Page Excerpt Widget'
, array(
'description' => 'Display Excerpt of Page in any Widget Area'
)
);
}
function pew_trim($text, $length) {
// if the text is longer than the length it is supposed to be
if (strlen($text) > $length){
// trim to length
$text = substr($text, 0, $length);
// find last whitespace in string
$last_whitespace = strrpos($text, ' ');
// trim to last whitespace in string
$text = substr($text, 0, $last_whitespace);
// append dots
return $text;
}
// if the text is shorter than the trim limit, pass it on
else {
return $text;
}
}
function widget($args, $instance) {
extract($args, EXTR_SKIP);
echo $before_widget;
$page_data = get_page($instance['page_id']);
$title = $page_data->post_title;
$permalink = get_permalink($instance['page_id']);
if (!empty($title) && $instance['display_title'] == 'on') {
echo $before_title;
if ($instance['link_title']){
echo '<a class="jmh_pew_title" href="'. $permalink .'">'. $title . '</a>';
}
else {
echo $title;
}
echo $after_title;
};
echo '<p>';
echo $this->pew_trim($page_data->post_content, $instance['excerpt_length']);
if ($instance['dot_excerpt'] == 'on'){
echo ' [...]';
}
echo '</p>';
if ($instance['display_read_more'] == 'on'){
echo ' <a class="jmh_pew_readmore" href="'. $permalink .'">'. $instance['read_more_label'] .'</a>';
}
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['page_id'] = strip_tags($new_instance['page_id']);
$instance['excerpt_length'] = strip_tags($new_instance['excerpt_length']);
$instance['dot_excerpt'] = strip_tags($new_instance['dot_excerpt']);
$instance['display_title'] = strip_tags($new_instance['display_title']);
$instance['link_title'] = strip_tags($new_instance['link_title']);
$instance['display_read_more'] = strip_tags($new_instance['display_read_more']);
$instance['read_more_label'] = strip_tags($new_instance['read_more_label']);
return $instance;
}
function form($instance) {
$default = array(
'title' => 'Page Excerpt Widget'
, 'excerpt_length' => 500
, 'dot_excerpt' => 'on'
, 'display_title' => 'on'
, 'display_read_more' => 'on'
, 'read_more_label' => 'read full page'
);
$instance = wp_parse_args( (array) $instance, $default );
$page_id = $this->get_field_name('page_id');
_e("Page to display: " );
?>
<select name="<?php echo $page_id; ?>">
<?php
$pages = get_pages();
foreach ($pages as $page){
if ($page->ID == $instance['page_id']){
$selected = 'selected="selected"';
}
else {
$selected='';
}
echo '<option value="'
.$page->ID.'"'
.$selected.'>'
.$page->post_title
.'</option>';
};
?>
</select>
<?php
$field_excerpt_length_id = $this->get_field_id('excerpt_length');
$field_excerpt_length = $this->get_field_name('excerpt_length');
echo "\r\n"
.'<p><label for="'
.$field_id
.'">'
.__('Excerpt Length')
.': </label><input type="text" id="'
.$field_excerpt_length_id
.'" name="'
.$field_excerpt_length
.'" value="'
.esc_attr( $instance['excerpt_length'] )
.'" /></p>';
$field_dot_excerpt_id = $this->get_field_id('dot_excerpt');
$field_dot_excerpt = $this->get_field_name('dot_excerpt');
if ($instance['dot_excerpt'] == 'on'){
$checked = 'checked="checked"';
}
else {
$checked = '';
}
echo "\r\n"
.'<p><input type="checkbox" id="'
.$field_dot_excerpt_id
.'" name="'
.$field_dot_excerpt
.'" value="on"'
.$checked
.'/> <label for="'
.$field_dot_excerpt_id
.'">'
.__('Show dots after excerpt `[...]`')
.' </label></p>';
$field_display_title_id = $this->get_field_id('display_title');
$field_display_title = $this->get_field_name('display_title');
if ($instance['display_title'] == 'on'){
$checked = 'checked="checked"';
}
else {
$checked = '';
}
echo "\r\n"
.'<p><input type="checkbox" id="'
.$field_display_title_id
.'" name="'
.$field_display_title
.'" value="on"'
.$checked
.'/> <label for="'
.$field_display_title_id
.'">'
.__('Display Page Title')
.' </label></p>';
$field_link_title_id = $this->get_field_id('link_title');
$field_link_title = $this->get_field_name('link_title');
if ($instance['link_title'] == 'on'){
$checked = 'checked="checked"';
}
else {
$checked = '';
}
echo "\r\n"
.'<p><input type="checkbox" id="'
.$field_link_title_id
.'" name="'
.$field_link_title
.'" value="on"'
.$checked
.'/> <label for="'
.$field_link_title_id
.'">'
.__('Link Page Title')
.' </label></p>';
$field_display_read_more_id = $this->get_field_id('display_read_more');
$field_display_read_more = $this->get_field_name('display_read_more');
if ($instance['display_read_more'] == 'on'){
$checked = 'checked="checked"';
}
else {
$checked = '';
}
echo "\r\n"
.'<p><input type="checkbox" id="'
.$field_display_read_more_id
.'" name="'
.$field_display_read_more
.'" value="on"'
.$checked
.'/> <label for="'
.$field_display_read_more_id
.'">'
.__('Display Read More Link')
.' </label></p>';
$field_read_more_label_id = $this->get_field_id('read_more_label');
$field_read_more_label = $this->get_field_name('read_more_label');
echo "\r\n"
.'<p><label for="'
.$field_read_more_label_id
.'">'
.__('Read More Label')
.': </label><input type="text" id="'
.$field_read_more_label_id
.'" name="'
.$field_read_more_label
.'" value="'
.esc_attr( $instance['read_more_label'] ).'"'
.'placeholder="read full page"'
.'/></p>';
}
/* class end */
}
}
add_action('widgets_init', 'page_excerpt_widgets');
function page_excerpt_widgets(){
register_widget('PageExcerptWidget');
}
?>