-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwppp.php
More file actions
736 lines (636 loc) · 28.7 KB
/
wppp.php
File metadata and controls
736 lines (636 loc) · 28.7 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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
<?php
/*
Plugin Name: WordPress.com Popular Posts
Plugin URI: http://polpoinodroidi.com/wordpress-plugins/wordpresscom-popular-posts/
Description: Shows the most popular posts, using data collected by <a href='http://wordpress.org/extend/plugins/stats/'>WordPress.com stats</a> plugin.
Version: 2.4.2
Text Domain: wordpresscom-popular-posts
Author: Frasten
Author URI: http://polpoinodroidi.com
License: GPL3
*/
/* Created by Frasten (email : frasten@gmail.com) */
if ( ! class_exists( 'WPPP' ) && class_exists( 'WP_Widget' ) ) :
class WPPP extends WP_Widget {
var $defaults;
var $cache_expire = 600;
function WPPP() {
$this->defaults = array('title' => __( 'Popular Posts', 'wordpresscom-popular-posts' )
,'number' => '5'
,'days' => '0'
,'show' => 'both'
,'format' => "<a href='%post_permalink%' title='%post_title_attribute%'>%post_title%</a>"
,'excerpt_length' => '100'
,'title_length' => '0'
,'cutoff' => '0'
,'list_tag' => 'ul'
,'category' => '0'
,'enable_cache' => '1'
,'cache_only_when_visitor' => '0'
,'time_format' => ''
,'magic_number' => '2'
);
$widget_ops = array( 'classname' => 'widget_wppp',
'description' => __( "A list of your most popular posts", 'wordpresscom-popular-posts' )
);
$control_ops = array( 'width' => 350, 'height' => 300 );
$this->WP_Widget( 'wppp', __( 'Popular Posts', 'wordpresscom-popular-posts' ), $widget_ops, $control_ops );
}
function widget( $args, $instance = null ) {
global $wpdb, $allowedposttags;
if ( ! function_exists( 'stats_get_options' ) || ! function_exists( 'stats_get_csv' ) )
return;
extract( $args );
if ( ! $instance ) {
// Called from static non-widget function. (Or maybe some error? :-P)
$instance = $args;
/* cache */
if ( ! empty( $instance['cachename'] ) ) {
$this->id = $instance['cachename'];
}
}
$from_shortcode = false;
if ( ! empty( $instance['from_shortcode'] ) )
$from_shortcode = true;
/* Before the widget (as defined by the theme) */
if ( ! $from_shortcode )
echo $before_widget;
/* CACHE SYSTEM */
if ( $this->id && ( ! isset( $instance['enable_cache'] ) || $instance['enable_cache'] ) ) {
$cache = get_option( 'wppp_cache' );
if ( $cache && is_array( $cache ) ) {
$widget_cache = $cache[$this->id];
/* Don't serve a cached version (forcing refresh) when the user
* is logged in (if set in the preferences) */
if ( ! $instance['cache_only_when_visitor'] || ! is_user_logged_in() ) {
/* Check if it is valid or not */
if ( isset( $widget_cache['time'] ) &&
$widget_cache['time'] > ( time() - $this->cache_expire ) ) {
/* If it's called from the function, let's make some check to
* see if the options have changed. */
$valid = true;
if ( ! empty( $instance['cachename'] ) ) {
$settings_string = implode( '|', $instance );
$md5 = md5( $settings_string );
if ( $md5 != $widget_cache['settings_checksum'] )
$valid = false;
}
if ( $valid ) {
/* Print out the data from the cache. */
if ( ! $from_shortcode ) {
echo $widget_cache['value'];
echo $after_widget;
return;
}
else
return $widget_cache['value'];
}
unset( $valid );
} /* end cache's validity check */
} /* end check if cache_only_when_visitor */
unset( $widget_cache );
}
unset( $cache );
}
/* END CACHE SYSTEM */
// Check against malformed values
$instance['days'] = intval( $instance['days'] );
$instance['number'] = intval( $instance['number'] );
if ( $instance['days'] <= 0 )
$instance['days'] = '-1';
// A little hackish, but "could" work!
$howmany = $instance['number'];
if ( $instance['show'] == 'posts' )
$howmany *= 2;
else if ( $instance['show'] == 'pages' )
$howmany *= 4; // pages are usually less, let's try more!
// If I want to show links by category, I need more data.
if ( $instance['category'] ) {
$howmany *= 3;
}
/* The last workaround: if the automatic guesses are not enough,
* users can raise the "magic number" to increase the size of the
* data asked to the server.
* Note: if this number is set too high, it can slow the requests.
* So it's a good practice to enable the cache for this plugin. */
$instance['magic_number'] = floatval( $instance['magic_number'] );
if ( $instance['magic_number'] > 1 ) {
$howmany *= $instance['magic_number'];
$howmany = intval( $howmany );
}
// If I set some posts to be excluded, I must ask for more data
$excluded_ids = explode( ',', $instance['exclude'] );
if ( is_array( $excluded_ids ) && sizeof( $excluded_ids ) && $excluded_ids[0] !== '' ) {
$howmany += sizeof( $excluded_ids );
}
/* TEMPORARY FIX FOR WP_STATS PLUGIN */
$reset_cache = false;
$stats_cache = get_option( 'stats_cache' );
if ( ! $stats_cache || ! is_array( $stats_cache ) ) {
$reset_cache = true;
}
else {
foreach ( $stats_cache as $key => $val ) {
if ( ! is_array( $val ) || ! sizeof( $val ) ) {
$reset_cache = true;
break;
}
foreach ( $val as $key => $val2 ) {
if ( ! is_array( $val2 ) || ! sizeof( $val2 ) ) {
$reset_cache = true;
break;
}
break;
}
break;
}
}
if ( $reset_cache ) {
update_option( 'stats_cache', "" );
}
/* END FIX */
$top_posts = stats_get_csv( 'postviews', "days={$instance['days']}&limit=$howmany" );
$output = '';
/*********************
* TITLE *
********************/
if ( ! empty( $instance['title'] ) ) {
/* I'm disabling this because it escapes html code and users want to
* use it. If anybody will need this filter, I'll try to find a solution.
* Instead, I'm using wp_kses() for securing it.
$instance['title'] = apply_filters( 'widget_title', $instance['title'] );
*/
$instance['title'] = wp_kses( $instance['title'], $allowedposttags );
// Tags before and after the title (as called by WordPress)
if ( $before_title || $after_title ) {
$instance['title'] = $before_title . $instance['title'] . $after_title;
}
$output .= $instance['title'] . "\n";
}
// Check against malicious data
if ( ! in_array( $instance['list_tag'], array( 'ul', 'ol', 'none' ) ) )
$instance['list_tag'] = $this->defaults['list_tag'];
if ( $instance['list_tag'] != 'none' )
$output .= "<{$instance['list_tag']} class='wppp_list'>\n";
// Cleaning and filtering
if ( is_array( $top_posts ) && sizeof( $top_posts ) ) {
$temp_list = array();
foreach ( $top_posts as $p ) {
// If I set some posts to be excluded:
if ( sizeof( $excluded_ids ) && in_array( $p['post_id'], $excluded_ids ) ) continue;
/* I don't know why, but on some blogs there are "fake" entries,
without data. */
if ( ! $p['post_id'] ) continue;
// Posts with views <= 0 must be excluded
if ( $p['views'] <= 0 ) continue;
// If I have set to have a cutoff, exclude the posts with views below that threshold
if ( $instance['cutoff'] > 0 && $p['views'] < $instance['cutoff'] ) continue;
$temp_list[] = $p;
}
$top_posts = $temp_list;
}
else {
/* No popular posts. Maybe something went wrong while fetching data.
* Write a hidden debug message. */
echo "<!-- WPPP error: no top-posts fetched. -->\n";
return;
}
/*************************************************************
* Removing non-existing posts and updating data from the DB *
************************************************************/
$id_list = array();
foreach ( $top_posts as $p ) {
$id_list[] = $p['post_id'];
}
// If no top-posts, just do nothing gracefully
if ( sizeof( $id_list ) ) {
// Must unescape the CSV data, to avoid issues with truncate functions
for ( $i = 0; $i < sizeof( $top_posts ); $i++ ) {
$top_posts[$i]['post_title'] = stripslashes( htmlspecialchars_decode( $top_posts[$i]['post_title'] ) );
}
/* The data from WP-Stats aren't updated, so we must fetch them
* from the DB, overwriting the old values.
* 1) check if that id is still valid (deleted post?)
* 2) exclude private posts and drafts
* 3) If I chose to show only posts or pages, only show them
* 4) If I chose to show only a category, only show those posts.
* Thanks to Rob Malon for this.
*/
/* Let's save CPU time: execute this long query with joins only if necessary */
$instance['category'] = (int) $instance['category'];
$filter_category = $instance['category'] > 0;
if ( $filter_category ) {
$query = "SELECT p.id, p.post_title FROM $wpdb->posts AS p INNER JOIN $wpdb->term_relationships AS r ON r.object_id = p.id";
}
else {
$query = "SELECT p.id, p.post_title FROM $wpdb->posts AS p";
}
$query .= " WHERE p.id IN (" . implode( ',', $id_list ) . ")";
$query .= " AND p.post_status != 'draft' AND p.post_status != 'private' AND p.post_status != 'trash'";
$query .= " AND p.post_date >= DATE_SUB(NOW(), INTERVAL 30 DAY)";
// If I want to show only posts or only pages:
if ( $instance['show'] != 'both' ) {
$query .= " AND p.post_type = '" . ( $instance['show'] == 'pages' ? 'page' : 'post' ) . "'";
}
if ( $filter_category ) {
/* NOTE: if a category is set, it won't show pages (you can set
* categories only with posts. */
$query .= " AND (r.term_taxonomy_id = '{$instance['category']}'";
/* If I chose a parent category and the post is in a child category,
* I must also check if the post is in any of the child categories. */
/* I need the term_id of the category, because get_categories()
* wants it. */
$query_cat = "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = '{$instance['category']}'";
$cat_id = $wpdb->get_var( $query_cat );
$children = get_categories("child_of=$cat_id&hide_empty=0");
foreach ($children as $child) {
$query .= " OR r.term_taxonomy_id = '$child->term_taxonomy_id'";
}
$query .= ')';
}
$results = $wpdb->get_results( $query );
$valid_list = array();
foreach ( $results as $valid ) {
$valid_list[$valid->id] = $valid;
}
$temp_list = array();
foreach ( $top_posts as $p ) {
if ( in_array( $p['post_id'], array_keys( $valid_list ) ) ) {
// Updating the title from the DB
$p['post_title'] = __( $valid_list[$p['post_id']]->post_title );
$temp_list[] = $p;
}
// Limit the number of posts shown following user settings.
if ( sizeof( $temp_list ) >= $instance['number'] )
break;
}
$top_posts = $temp_list;
unset( $temp_list );
} // end if (I have posts)
foreach ( $top_posts as $post ) {
if ( $instance['list_tag'] != 'none' )
$output .= "\t<li>";
// Replace format with data
$replace = array(
'%post_permalink%' => get_permalink( $post['post_id'] ),
'%post_title%' => esc_html( $this->truncateText( $post['post_title'], $instance['title_length'] ) ),
'%post_title_attribute%' => esc_attr( $post['post_title'] ),
'%post_views%' => number_format_i18n( $post['views'] )
);
// %post_category% stuff
if ( FALSE !== strpos( $instance['format'], '%post_category%' ) ) {
// RS account for multiple categories
$cat = get_the_category( $post['post_id'] );
$replace['%post_category%'] = $cat[0]->cat_name;
}
// %post_comments% stuff
if ( FALSE !== strpos( $instance['format'], '%post_comments%' ) ) {
$replace['%post_comments%'] = get_comments_number( $post['post_id'] );
}
// %post_excerpt% stuff
if ( FALSE !== strpos( $instance['format'], '%post_excerpt%' ) ) {
// I get the excerpt for the post only if necessary, to save CPU time.
$temppost = &get_post( $post['post_id'] );
if ( ! empty( $temppost->post_excerpt ) ) {
/* Excerpt already saved by the user */
$replace['%post_excerpt%'] = $this->truncateText( $temppost->post_excerpt, $instance['excerpt_length'] );
}
else {
// let's calculate the excerpt:
$excerpt = strip_tags( $temppost->post_content );
$excerpt = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $excerpt );
$excerpt = $this->truncateText( $excerpt, $instance['excerpt_length'] );
$replace['%post_excerpt%'] = $excerpt;
}
unset( $temppost );
}
// %post_thumbnail% stuff (Thesis ONLY!)
if ( FALSE !== strpos( $instance['format'], '%post_thumbnail%' ) ) {
// I get the excerpt for the post only if necessary, to save CPU time.
//$temppost = &get_post( $post['post_id'] );
if (get_the_post_thumbnail($post['post_id'])) {
$replace['%post_thumbnail%'] = '<div class="post_image">' . get_the_post_thumbnail($post['post_id']) . '</div>';
} else {
$replace['%post_thumbnail%'] = '<div class="post_image no_thumb"></div>';
}
/*
$image_url = get_post_meta($post['post_id'], 'thesis_thumb', true);
if ($image_url == '') {
$image_url = get_post_meta($post['post_id'], 'thesis_post_image', true);
}
if ($image_url != '') {
global $blog_id;
$plugurl = WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__)). 'thumb.php?site='.$blog_id.'&src=' . urlencode($image_url) . '&w=160&h=80&zc=1&q=100';
$replace['%post_thumbnail%'] = '<div class="post_image"><img src="'.$plugurl.'"></div>';
} else {
$replace['%post_thumbnail%'] = '<div class="post_image no_thumb"></div>';
}
*/
//unset( $temppost );
}
// %post_time% stuff
if ( FALSE !== strpos( $instance['format'], '%post_time%' ) ) {
/* If the first argument of get_the_time() is not set, it will use
* the default DATE format. */
$replace['%post_time%'] = get_the_time( $instance['time_format'], $post['post_id'] );
}
$output .= wp_kses( strtr( $instance['format'], $replace ), $allowedposttags );
if ( $instance['list_tag'] != 'none' )
$output .= "</li>\n";
}
if ( $instance['list_tag'] != 'none' )
$output .= "</{$instance['list_tag']}>\n";
/* Cache data */
$cache = get_option( 'wppp_cache' );
if ( ! is_array($cache) ) $cache = array();
$cache[$this->id] = array( 'value' => $output, 'time' => time() );
if ( $md5 ) {
/* If I'm calling this from the function, I must save the checksum
* for the settings, to reset the cache everytime I change the settings. */
$cache[$this->id]['settings_checksum'] = $md5;
}
update_option( 'wppp_cache', $cache );
if ( ! $from_shortcode )
echo $output;
else
return $output;
/* After the widget (as defined by the theme) */
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['number'] = intval( $new_instance['number'] );
$instance['days'] = intval( $new_instance['days'] );
$instance['format'] = $new_instance['format'];
$instance['show'] = in_array( $new_instance['show'], array( 'both', 'posts', 'pages' ) ) ?
$new_instance['show'] :
$this->defaults['show'];
$instance['excerpt_length'] = intval( $new_instance['excerpt_length'] );
$instance['title_length'] = intval( $new_instance['title_length'] );
// I want only digits or commas for this:
$instance['exclude'] = preg_replace( '/[^0-9,]/', '', $new_instance['exclude'] );
$instance['cutoff'] = max( intval( $new_instance['cutoff'] ), 0 );
$instance['list_tag'] = in_array( $new_instance['list_tag'], array( 'ul', 'ol', 'none') ) ?
$new_instance['list_tag'] :
$this->defaults['list_tag'];
$instance['category'] = intval( $new_instance['category'] );
$instance['enable_cache'] = ( $new_instance['enable_cache'] ? 1 : 0 );
$instance['cache_only_when_visitor'] = ( $new_instance['cache_only_when_visitor'] ? 1 : 0 );
$instance['magic_number'] = floatval( $new_instance['magic_number'] );
$instance['time_format'] = $new_instance['time_format'];
/* Reset cache */
$cache = get_option( 'wppp_cache' );
unset( $cache[$this->id] );
update_option( 'wppp_cache', $cache );
return $instance;
}
function form( $instance ) {
// Set the settings that are still undefined
$instance = wp_parse_args( (array) $instance, $this->defaults );
$field_id = $this->get_field_id( 'title' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Title', 'wordpresscom-popular-posts' );
echo ": <input style='width: 180px;' id='$field_id' name='" .
$this->get_field_name( 'title' ) . "' type='text' value='" .
esc_attr( $instance['title'] ) . "' /></label></p>";
$field_id = $this->get_field_id( 'number' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Number of links shown', 'wordpresscom-popular-posts' );
echo ": <input style='width: 30px;' id='$field_id' name='" .
$this->get_field_name( 'number' ) . "' type='text' value='" .
intval( $instance['number'] ) . "' /></label></p>";
$field_id = $this->get_field_id( 'days' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'The length (in days) of the desired time frame.<br />(0 means unlimited)', 'wordpresscom-popular-posts' );
echo ": <input style='width: 40px;' id='$field_id' name='" .
$this->get_field_name( 'days' ) . "' type='text' value='" .
intval( $instance['days'] ) . "' /></label></p>";
$field_id = $this->get_field_id( 'show' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Show: ', 'wordpresscom-popular-posts' );
$opt = array(
'both' => __( 'posts and pages', 'wordpresscom-popular-posts' ),
'posts' => __( 'only posts', 'wordpresscom-popular-posts' ),
'pages' => __( 'only pages', 'wordpresscom-popular-posts' )
);
if ( ! $instance['show'] )
$instance['show'] = $this->defaults['show'];
echo "<select name='" . $this->get_field_name( 'show' ) . "' id='$field_id'>\n";
foreach ( $opt as $key => $value ) {
echo "<option value='$key'" . selected( $instance['show'], $key ) . ">$value</option>\n";
}
echo '</select></label></p>';
$field_id = $this->get_field_id( 'format' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Format of the links. See <a href="http://wordpress.org/extend/plugins/wordpresscom-popular-posts/faq/">docs</a> for help', 'wordpresscom-popular-posts' );
echo ": <input style='width: 300px;' id='$field_id' name='" .
$this->get_field_name( 'format' ) . "' type='text' value='" .
esc_attr( $instance['format'] ) . "' /></label></p>";
$field_id = $this->get_field_id( 'excerpt_length' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Length of the excerpt (if %post_excerpt% is used in the format above)', 'wordpresscom-popular-posts' );
echo ": <input style='width: 40px;' id='$field_id' name='" .
$this->get_field_name( 'excerpt_length' ) . "' type='text' value='" .
intval( $instance['excerpt_length'] ) . "' />" . __(' characters', 'wordpresscom-popular-posts' ) . "</label></p>";
$field_id = $this->get_field_id( 'time_format' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Date/time format (if %post_time% is used in the format above). See <a href="http://wordpress.org/extend/plugins/wordpresscom-popular-posts/faq/">docs</a> for help', 'wordpresscom-popular-posts' );
echo ": <input style='width: 200px;' id='$field_id' name='" .
$this->get_field_name( 'time_format' ) . "' type='text' value='" .
esc_attr( $instance['time_format'] ) . "' /></label></p>";
$field_id = $this->get_field_id( 'title_length' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Max length of the title links.<br />(0 means unlimited)', 'wordpresscom-popular-posts' );
echo ": <input style='width: 30px;' id='$field_id' name='" .
$this->get_field_name( 'title_length' ) . "' type='text' value='" .
intval( $instance['title_length'] ) . "' />" . __(' characters', 'wordpresscom-popular-posts' ) . "</label></p>";
$field_id = $this->get_field_id( 'exclude' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Exclude these posts: (separate the IDs by commas. e.g. 1,42,52)', 'wordpresscom-popular-posts' );
echo ": <input style='width: 180px;' id='$field_id' name='" .
$this->get_field_name( 'exclude' ) . "' type='text' value='" .
esc_attr( $instance['exclude'] ) . "' /></label></p>";
$field_id = $this->get_field_id( 'cutoff' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Don\'t show posts/pages with a view count under', 'wordpresscom-popular-posts' );
echo ": <input style='width: 50px;' id='$field_id' name='" .
$this->get_field_name( 'cutoff' ) . "' type='text' value='" .
intval( $instance['cutoff'] ) . "' /></label> " . __('(0 means unlimited)', 'wordpresscom-popular-posts' ) . '</p>';
$field_id = $this->get_field_id( 'list_tag' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Kind of list', 'wordpresscom-popular-posts' );
$opt = array(
'ul' => __( 'Unordered list (<ul>)', 'wordpresscom-popular-posts' ),
'ol' => __( 'Ordered list (<ol>)', 'wordpresscom-popular-posts' ),
'none' => __( 'None (use custom format)', 'wordpresscom-popular-posts' )
);
if ( ! $instance['show'] )
$instance['show'] = $this->defaults['list_tag'];
echo ": <select name='" . $this->get_field_name( 'list_tag' ) . "' id='$field_id'>\n";
foreach ( $opt as $key => $value ) {
echo "<option value='$key'" . selected( $key, $instance['list_tag'] ) . ">$value</option>\n";
}
echo '</select></label></p>';
// Category stuff
$field_id = $this->get_field_id( 'category' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Only show posts/pages in this category', 'wordpresscom-popular-posts' );
$cat_list = array(0 => __('<All categories>', 'wordpresscom-popular-posts' ) );
$categories = get_categories();
foreach ( $categories as $c ) {
$cat_list[$c->term_taxonomy_id] = $c->cat_name;
}
echo ": <select name='" . $this->get_field_name( 'category' ) . "' id='$field_id'>\n";
foreach ( $cat_list as $key => $value ) {
echo "<option value='$key'" . selected( $key, $instance['category'] ) . ">$value</option>\n";
}
echo '</select></label></p>';
$field_cache = $field_id = $this->get_field_id( 'enable_cache' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Enable cache (improves speed)', 'wordpresscom-popular-posts' );
echo ": <input type='checkbox' id='$field_id' name='" .
$this->get_field_name( 'enable_cache' ) . "'" .
( $instance['enable_cache'] ? " checked='checked'" : '' ) . ' /></label></p>';
/* This option is enabled only when enable_cache is on. */
$field_id = $this->get_field_id( 'cache_only_when_visitor' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Only show a cached version to the not logged in users', 'wordpresscom-popular-posts' );
echo ": <input type='checkbox' id='$field_id' name='" .
$this->get_field_name( 'cache_only_when_visitor' ) . "'" .
( $instance['cache_only_when_visitor'] ? " checked='checked'" : '' ) .
( $instance['enable_cache'] ? '' : " disabled='disabled'" ) . ' /></label></p>';
/* JavaScript for cache_only_when_visitor */
echo <<<EOF
<script type='text/javascript'>
//<![CDATA[
jQuery(document).ready( function($) {
$('#$field_cache').click(function() {
var checkbox = $('#$field_id');
if (checkbox.attr("disabled"))
checkbox.removeAttr("disabled");
else
checkbox.attr("disabled", "disabled");
});
});
//]]>
</script>
EOF;
$field_id = $this->get_field_id( 'magic_number' );
echo "<p style='text-align:right;'><label for='$field_id'>";
_e( 'Magic number (raise it if you see less links than expected)', 'wordpresscom-popular-posts' );
echo ": <input style='width: 50px;' id='$field_id' name='" .
$this->get_field_name( 'magic_number' ) . "' type='text' value='" .
intval( $instance['magic_number'] ) . "' /></label></p>";
}
function truncateText( $text, $chars = 50 ) {
if ( strlen( $text ) <= $chars || $chars <= 0 )
return $text;
$new = wordwrap( $text, $chars, "|" );
$newtext = explode( "|", $new );
return $newtext[0] . "...";
}
}
endif;
/* You can call this function if you want to integrate the plugin in a theme
* that doesn't support widgets.
*
* Just insert this code:
* <?php if ( function_exists( 'WPPP_show_popular_posts' ) ) WPPP_show_popular_posts();?>
*
* Optionally you can add some parameters to the function, in this format:
* name=value&name=value etc.
*
* Possible names are:
* - title (title of the widget, you can add tags (e.g. <h3>Popular Posts</h3>) default: Popular Posts)
* - number (number of links shown, default: 5)
* - days (length of the time frame of the stats, default 0, i.e. infinite)
* - show (can be: both, posts, pages, default both)
* - format (the format of the links shown, default: <a href='%post_permalink%' title='%post_title%'>%post_title%</a>)
* - time_format (the format used with %post_time%, see http://codex.wordpress.org/Formatting_Date_and_Time)
* - excerpt_length (the length of the excerpt, if %post_excerpt% is used in the format)
* - title_length (the length of the title links, default 0, i.e. unlimited)
* - exclude (the list of post/page IDs to exclude, separated by commas)
* - cutoff (don't show posts/pages with a view count under this number, default 0, i.e. unlimited)
* - list_tag (can be: ul, ol, none, default ul)
* - category (the ID of the category, see FAQ for info. Default 0, i.e. all categories)
* - cachename (it is used to enable the cache. Please see the FAQ)
* - cache_only_when_visitor (if enabled, it doesn't serve a cached version of the popular posts to the users logged in, default 0)
* - magic_number (set it to a number greater than 1 if you see less links than expected)
*
* Example: if you want to show the widget without any title, the 3 most viewed
* articles, in the last week, and in this format: My Article (123 views)
* you will use this:
* WPPP_show_popular_posts( "title=&number=3&days=7&format=<a href='%post_permalink%' title='%post_title_attribute%'>%post_title% (%post_views% views)</a>" );
*
* You don't have to fill every field, you can insert only the values you
* want to change from default values.
*
* You can use these special markers in the `format` value:
* %post_permalink% the link to the post
* %post_title% the title the post
* %post_title_attribute% the title of the post; use this in attributes, e.g. <a title='%post_title_attribute%'
* %post_views% number of views
* %post_excerpt% the first n characters of the content. Set n with excerpt_length.
* %post_category% the category of the post
* %post_comments% the number of comments a post has
* %post_time% the date/time of the post. You can set the format with time_format.
*
* */
function WPPP_show_popular_posts( $user_args = '' ) {
$wppp = new WPPP();
$args = wp_parse_args( $user_args, $wppp->defaults );
$wppp->widget( $args );
}
/**
* This function allows using [wp_popular_posts] shortcodes in posts/pages.
*/
function WPPP_shortcode_popular_posts( $user_args = '' ) {
$wppp = new WPPP();
$default_values = $wppp->defaults;
$default_values['from_shortcode'] = true;
$args = shortcode_atts( $default_values, $user_args );
return $wppp->widget( $args );
}
add_shortcode('wp_popular_posts', 'WPPP_shortcode_popular_posts');
function wppp_notice_incompatible() {
echo "<div class='error'><p>";
printf( __( "Wordpress.com Popular Post >= 2.0.0 is compatible with WordPress >= 2.8 only.<br />
Please either <a href='%s'>update</a> your WordPress installation, <a href='%s'>downgrade this plugin</a> to v1.3.6
or <a href='%s'>uninstall it</a>.", 'wordpresscom-popular-posts' ),
'http://wordpress.org/download/',
'http://downloads.wordpress.org/plugin/wordpresscom-popular-posts.1.3.6.zip',
'plugins.php' );
echo "</p></div>";
}
function wppp_check_upgrade() {
if ( ! class_exists( 'WP_Widget' ) ) return;
// Import eventual old settings (from WPPP < 2.0.0)
$wppp_options = get_option( 'widget_wppp' );
if ( ! $wppp_options ) return;
if ( array_key_exists( '_multiwidget', $wppp_options ) ) return;
$new_options = array( 2 => $wppp_options, '_multiwidget' => 1 );
update_option( 'widget_wppp', $new_options );
$sb_option = get_option( 'sidebars_widgets' );
foreach ( $sb_option as $key => $value ) {
if ( 'wp_inactive_widgets' == $key || 'array_version' == $key ) continue;
foreach ( $value as $i => $widgetname ) {
if ( 'popular-posts' == $widgetname || 'articoli-piu-letti' == $widgetname ) {
$sb_option[$key][$i] = 'wppp-2';
break;
}
}
}
update_option( 'sidebars_widgets', $sb_option );
}
add_action('plugins_loaded', 'wppp_check_upgrade');
// Language loading
load_textdomain( 'wordpresscom-popular-posts', dirname(__FILE__) . "/language/wordpresscom-popular-posts-" . get_locale() . ".mo" );
// This version is incompatible with WP < 2.8
if ( ! class_exists( 'WP_Widget' ) ) {
add_action( 'admin_notices', 'wppp_notice_incompatible' );
}
else {
add_action( 'widgets_init', create_function( '', 'return register_widget( "WPPP" );' ) );
}
?>