-
Notifications
You must be signed in to change notification settings - Fork 0
/
davido-post-category.php
456 lines (337 loc) · 17.7 KB
/
davido-post-category.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<?php
/*
Plugin Name: City Mag Posts By Category Widget
Description: Show recent posts at the widget which you can filter by Category Widget
Plugin Uri: http://www.cityflavourmagazine.com/contact-us
Author: CityMag
Author Uri: http://www.cityflavourmagazine.com
License: GPL
Version: 1.0.2
*/
// Registering the Widget
function register_DAVOO_Widget(){
register_widget('DAVOO_Widget');
}
add_action('widgets_init','register_DAVOO_Widget');
// Registering the Style & Enqueue it
function davoo_style(){
wp_register_style('davooStyle', plugins_url('davoo-list-recent-posts-by-category-style.css', __FILE__));
wp_enqueue_style('davooStyle');
}
add_action('wp_enqueue_scripts','davoo_style');
// Davoo Widget Class
class DAVOO_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
public function __construct() {
parent::__construct(
'davoo', // Base ID
'CityMag - List Recent Posts By Category', // Name
array(
'classname' => 'widget_davoo',
'description' => __( 'List Recent Posts By Category', 'city mag' )
) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
extract( $args );
$allwExcerpts = $instance['allow_excerpts'];
$allwImages = $instance['allow_images'];
$addIcn = $instance['add_icon'];
$iconURI = $instance['icon_uri'];
$iconWidth = $instance['icon_width'];
$iconHeight = $instance['icon_height'];
$iconImage = "<img class='davoo_widget_icon' src='" . $iconURI ."' width='" . $iconWidth ."' height='" . $iconHeight . "' alt='" . $title . "'/>";
$imageWidth = $instance['image_width'];
$imageHeight = $instance['image_height'];
$imageFloat = $instance['image_float'];
$widgtBGColor = $instance['widget_background_color'];
$widgtTextColor = $instance['widget_text_color'];
$widgtLinkColor = $instance['widget_link_color'];
$widgtTitleColor = $instance['widget_title_text_color'];
$allwComments= $instance['allow_comments'];
$shwCredits = $instance['show_credits'];
$commentsText= $instance['no_comments_text'];
$title = apply_filters( 'widget_title', $instance['title'] );
echo $before_widget;
echo "<div class='davoo_widget_inner' style='background:" . $widgtBGColor . ";'>";
if ( ! empty( $title ) )
echo $before_title . '<span style="color:' . $widgtTitleColor . ';">' . ( $addIcn == '1' ? '<p class=fixit>' . $iconImage . $title . '</p>' : $title ) . '</span>' . $after_title;
$totalposts = $instance['noofposts'];
$catName = $instance['category_name'];
//
if($catName == 'ALL'){
$latestposts = query_posts('posts_per_page=' . $totalposts );
}else{
$latestposts = query_posts('posts_per_page=' . $totalposts . '&category_name=' . $catName);
}
echo '<ul class="davoo">';
while( have_posts($latestposts)) : the_post($latestposts); ?>
<li class="fixit" style="color: <?php echo $widgtTextColor; ?>;">
<?php
// Thumbnails
if($allwImages == '1'){
if ( has_post_thumbnail() ) {
$thumbnail = get_the_post_thumbnail($latestpost->ID ,array($imageWidth, $imageHeight));
echo '<div class="davoo_thumbnail_' . ($imageFloat == "left" ? "left" : "right") . '">' . $thumbnail . '</div>';
}
else {
$defaultthumburl = plugins_url('images/thumbnail_default.png', __FILE__);
echo '<div class="davoo_thumbnail_' . ($imageFloat == 'left' ? 'left' : 'right') . '"><img src="' . $defaultthumburl . '" width="' . $imageWidth . '" height="' . $imageHeight . '" /></div>';
}
}
?>
<div class="davoo_content" style="background:<?php ;?>;">
<a style='color: <?php echo $widgtLinkColor; ?>;' href='<?php the_permalink(); ?>'><?php the_title(); ?></a>
<?php
//Excerpts
if($allwExcerpts == '1'){
$rpbc_excerpt_length = create_function('$length', "return " . $instance["excerpts_length"] . ";");
if ( $instance["excerpts_length"] > 0 )
add_filter('excerpt_length', $rpbc_excerpt_length);
$custom_excerpt_more = create_function('$more',"return ' ..';");
add_filter( 'excerpt_more', $custom_excerpt_more);
?>
<p> <?php the_excerpt(); ?></p>
</div>
<?php
}
// Comments
if($allwComments == '1'){
global $wpdb;
$pid = get_the_ID();
$TotalComments = $wpdb->get_var(
$wpdb->prepare("SELECT count(*) as 'totalcomments' FROM wp_comments WHERE comment_post_ID =%d", $pid )
);
if($TotalComments == '0'){
?>
<div class="davoo_comments_count">
<a href="<?php the_permalink(); ?>#comments"><?php echo $commentsText; ?></a>
</div>
<?php
}else{
?>
<div class="davoo_comments_count">
<a href="<?php the_permalink(); ?>#comments"><?php echo $TotalComments . ( $TotalComments > 1 && $TotalComments != 0 ? ' Comments' : ' Comment' ); ?></a>
</div>
<?php
}
}
?>
</li>
<?php
endwhile;
echo '</ul>';
wp_reset_query();
echo "</div>";
echo $after_widget;
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['noofposts'] = strip_tags( $new_instance['noofposts'] );
$instance['category_name'] = strip_tags( $new_instance['category_name'] );
$instance['allow_excerpts'] = (bool)$new_instance['allow_excerpts'];
$instance['excerpts_length'] = strip_tags( $new_instance['excerpts_length'] );
$instance['allow_images'] = (bool)$new_instance['allow_images'];
$instance['image_width'] = strip_tags($new_instance['image_width']);
$instance['image_height'] = strip_tags($new_instance['image_height']);
$instance['image_float'] = strip_tags($new_instance['image_float']);
$instance['add_icon'] = (bool)$new_instance['add_icon'];
$instance['icon_uri'] = strip_tags($new_instance['icon_uri']);
$instance['icon_width'] = strip_tags($new_instance['icon_width']);
$instance['icon_height'] = strip_tags($new_instance['icon_height']);
$instance['widget_background_color'] = $new_instance['widget_background_color'];
$instance['widget_text_color'] = $new_instance['widget_text_color'];
$instance['widget_link_color'] = $new_instance['widget_link_color'];
$instance['widget_title_text_color'] = $new_instance['widget_title_text_color'];
$instance['allow_comments'] = (bool)$new_instance['allow_comments'];
$instance['no_comments_text'] = strip_tags($new_instance['no_comments_text']);
return $instance;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
// Default Values Of Options
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
if ( isset( $instance[ 'noofposts' ] ) ) {
$noofposts = $instance[ 'noofposts' ];
} else{
$noofposts = "5";
}
if ( isset( $instance[ 'excerpts_length' ] ) ) {
$excerptsLength = $instance[ 'excerpts_length' ];
} else{
$excerptsLength = "20";
}
if ( isset( $instance[ 'image_width' ] ) ) {
$imageWidth = $instance[ 'image_width' ];
}else{
$imageWidth = "50";
}
if ( isset( $instance[ 'image_height' ] ) ) {
$imageHeight = $instance[ 'image_height' ];
}else{
$imageHeight = "50";
}
if ( isset( $instance[ 'icon_uri' ] ) ) {
$iconURI = $instance[ 'icon_uri' ];
}
if ( isset( $instance[ 'icon_width' ] ) ) {
$iconWidth = $instance[ 'icon_width' ];
}else{
$iconWidth = "32";
}
if ( isset( $instance[ 'icon_height' ] ) ) {
$iconHeight = $instance[ 'icon_height' ];
}else{
$iconHeight = "32";
}
$widgetBGColor = $instance['widget_background_color'];
$widgetTextColor = $instance['widget_text_color'];
$widgetLinkColor = $instance['widget_link_color'];
$widgetTitleColor = $instance['widget_title_text_color'];
if(isset($instance['no_comments_text'])) {
$commentsText = $instance['no_comments_text'];
}else{
$commentsText = "Leave a Comment";
}
// Form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></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 esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'noofposts' ); ?>"><?php _e( 'No Of Posts:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'noofposts' ); ?>" name="<?php echo $this->get_field_name( 'noofposts' ); ?>" type="text" value="<?php echo esc_attr( $noofposts ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('category_name'); ?>"><?php _e('Select a Category:'); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('category_name'); ?>" name="<?php echo $this->get_field_name('category_name'); ?>">
<option class="widefat" value="ALL" <?php ($instance['category_name'] == "ALL" ? "selected='selected'" : " ") ?>>ALL</option>
<?php
$categories = get_categories();
foreach($categories as $category){
?>
<option class="widefat" value="<?php echo $category->name; ?>" <?php if( $instance['category_name'] == $category->name ) { echo "selected='selected'
"; }else{ echo " "; }?>>
<?php echo $category->name; ?>
</option>
<?php
}
?>
</select></p>
<h4 style="margin-bottom:3px">Excerpts</h4>
<p style="font-size:8px">Enable / Disable Excerpts</p>
<p>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'allow_excerpts' ); ?>" name="<?php echo $this->get_field_name( 'allow_excerpts' ); ?>" value="1" <?php echo ($instance['allow_excerpts'] == "true" ? "checked='checked'" : ""); ?> />
<label for="<?php echo $this->get_field_id( 'allow_excerpts' ); ?>"><?php _e( 'Include Excerpts' ); ?></label>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'excerpts_length' ); ?>"><?php _e( 'Excerpts Length:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'excerpts_length' ); ?>" name="<?php echo $this->get_field_name( 'excerpts_length' ); ?>" type="text" value="<?php echo esc_attr( $excerptsLength ); ?>" />
</p>
<h4 style="margin-bottom:3px">Thumbnails</h4>
<p style="font-size:8px">Enable / Disable Thumbnails, Set Size , Position</p>
<p>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'allow_images' ); ?>" name="<?php echo $this->get_field_name( 'allow_images' ); ?>" value="1" <?php echo ($instance['allow_images'] == "true" ? "checked='checked'" : ""); ?> />
<label for="<?php echo $this->get_field_id( 'allow_images' ); ?>"><?php _e( 'Include Thumbnails' ); ?></label>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'image_width' ); ?>"><?php _e( 'Thumbnail Width:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'image_width' ); ?>" name="<?php echo $this->get_field_name( 'image_width' ); ?>" type="text" value="<?php echo esc_attr( $imageWidth ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'image_height' ); ?>"><?php _e( 'Thumbnail Height:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'image_height' ); ?>" name="<?php echo $this->get_field_name( 'image_height' ); ?>" type="text" value="<?php echo esc_attr( $imageHeight ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('image_float'); ?>"><?php _e('Thumbnail Float:'); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('image_float'); ?>" name="<?php echo $this->get_field_name('image_float'); ?>">
<option class="widefat" value="<?php echo "left"; ?>" <?php if( $instance['image_float'] == "left" ) { echo "selected='selected'
"; }else{ echo " "; }?>>
Left
</option>
<option class="widefat" value="<?php echo "right"; ?>" <?php if( $instance['image_float'] == "right" ) { echo "selected='selected'
"; }else{ echo " "; }?>>
Right
</option>
</select>
</p>
<h4 style="margin-bottom:3px">Icon</h4>
<p style="font-size:10px">Enable / Disable Icon for Widget Title </p>
<p>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'add_icon' ); ?>" name="<?php echo $this->get_field_name( 'add_icon' ); ?>" value="1" <?php echo ($instance['add_icon'] == "true" ? "checked='checked'" : ""); ?> />
<label for="<?php echo $this->get_field_id( 'add_icon' ); ?>"><?php _e( 'Add Icon' ); ?></label>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'icon_uri' ); ?>"><?php _e( 'Icon URI:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'icon_uri' ); ?>" name="<?php echo $this->get_field_name( 'icon_uri' ); ?>" type="text" value="<?php echo esc_attr( $iconURI ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'icon_width' ); ?>"><?php _e( 'Icon Width:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'icon_width' ); ?>" name="<?php echo $this->get_field_name( 'icon_width' ); ?>" type="text" value="<?php echo esc_attr( $iconWidth ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'icon_height' ); ?>"><?php _e( 'Icon Height:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'icon_height' ); ?>" name="<?php echo $this->get_field_name( 'icon_height' ); ?>" type="text" value="<?php echo esc_attr( $iconHeight ); ?>" />
</p>
<h4 style="margin-bottom:3px">Style</h4>
<p style="font-size:10px">Style your widget</p>
<p>
<label for="<?php echo $this->get_field_id( 'widget_background_color' ); ?>"><?php _e( 'Widget Background Color:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'widget_background_color' ); ?>" name="<?php echo $this->get_field_name( 'widget_background_color' ); ?>" type="text" value="<?php echo esc_attr( $widgetBGColor ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'widget_title_text_color' ); ?>"><?php _e( 'Widget Title Text Color:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'widget_title_text_color' ); ?>" name="<?php echo $this->get_field_name( 'widget_title_text_color' ); ?>" type="text" value="<?php echo esc_attr( $widgetTitleColor ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'widget_text_color' ); ?>"><?php _e( 'Widget Text Color:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'widget_text_color' ); ?>" name="<?php echo $this->get_field_name( 'widget_text_color' ); ?>" type="text" value="<?php echo esc_attr( $widgetTextColor ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'widget_link_color' ); ?>"><?php _e( 'Widget Link Color:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'widget_link_color' ); ?>" name="<?php echo $this->get_field_name( 'widget_link_color' ); ?>" type="text" value="<?php echo esc_attr( $widgetLinkColor ); ?>" />
</p>
<h4 style="margin-bottom:3px">Comments</h4>
<p style="font-size:10px">Enable / Disable Comments</p>
<p>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'allow_comments' ); ?>" name="<?php echo $this->get_field_name( 'allow_comments' ); ?>" value="1" <?php echo ($instance['allow_comments'] == "true" ? "checked='checked'" : ""); ?> />
<label for="<?php echo $this->get_field_id( 'allow_comments' ); ?>"><?php _e( 'Include Comments' ); ?></label>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'no_comments_text' ); ?>"><?php _e( 'Comments Text:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'no_comments_text' ); ?>" name="<?php echo $this->get_field_name( 'no_comments_text' ); ?>" type="text" value="<?php echo esc_attr( $commentsText ); ?>" />
</p>
<?php
}
}
?>