forked from cabrerahector/wordpress-popular-posts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress-popular-posts.php
3326 lines (2629 loc) · 96.9 KB
/
wordpress-popular-posts.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
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: WordPress Popular Posts
Plugin URI: http://wordpress.org/extend/plugins/wordpress-popular-posts
Description: WordPress Popular Posts is a highly customizable widget that displays the most popular posts on your blog
Version: 3.3.1
Author: Hector Cabrera
Author URI: http://cabrerahector.com
Author Email: [email protected]
Text Domain: wordpress-popular-posts
Domain Path: /lang/
Network: false
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Copyright 2008-2015 Hector Cabrera ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( !defined('ABSPATH') )
exit('Please do not load this file directly.');
/**
* WordPress Popular Posts class.
*/
if ( !class_exists('WordpressPopularPosts') ) {
/**
* Register plugin's activation / deactivation functions
* @since 1.3
*/
register_activation_hook( __FILE__, array( 'WordpressPopularPosts', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'WordpressPopularPosts', 'deactivate' ) );
/**
* Add function to widgets_init that'll load WPP.
* @since 2.0
*/
function load_wpp() {
register_widget( 'WordpressPopularPosts' );
}
add_action( 'widgets_init', 'load_wpp' );
class WordpressPopularPosts extends WP_Widget {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.3.0
* @var string
*/
private $version = '3.3.1';
/**
* Plugin identifier.
*
* @since 3.0.0
* @var string
*/
private $plugin_slug = 'wordpress-popular-posts';
/**
* Instance of this class.
*
* @since 3.0.0
* @var object
*/
protected static $instance = NULL;
/**
* Slug of the plugin screen.
*
* @since 3.0.0
* @var string
*/
protected $plugin_screen_hook_suffix = NULL;
/**
* Flag for singular pages.
*
* @since 3.1.2
* @var int
*/
private $current_post_id = 0;
/**
* Plugin directory.
*
* @since 1.4.6
* @var string
*/
private $plugin_dir = '';
/**
* Plugin uploads directory.
*
* @since 3.0.4
* @var array
*/
private $uploads_dir = array();
/**
* Default thumbnail.
*
* @since 2.2.0
* @var string
*/
private $default_thumbnail = '';
/**
* Default thumbnail sizes
*
* @since 3.2.2
* @var array
*/
private $default_thumbnail_sizes = array();
/**
* Flag to verify if thumbnails can be created or not.
*
* @since 1.4.6
* @var bool
*/
private $thumbnailing = false;
/**
* Flag to verify if qTrans is present.
*
* @since 1.4.6
* @var bool
*/
private $qTrans = false;
/**
* Default charset.
*
* @since 2.1.4
* @var string
*/
private $charset = "UTF-8";
/**
* Plugin defaults.
*
* @since 2.3.3
* @var array
*/
protected $defaults = array(
'title' => '',
'limit' => 10,
'range' => 'daily',
'freshness' => false,
'order_by' => 'views',
'post_type' => 'post,page',
'pid' => '',
'author' => '',
'cat' => '',
'shorten_title' => array(
'active' => false,
'length' => 25,
'words' => false
),
'post-excerpt' => array(
'active' => false,
'length' => 55,
'keep_format' => false,
'words' => false
),
'thumbnail' => array(
'active' => false,
'build' => 'manual',
'width' => 15,
'height' => 15,
'crop' => true
),
'rating' => false,
'stats_tag' => array(
'comment_count' => false,
'views' => true,
'author' => false,
'date' => array(
'active' => false,
'format' => 'F j, Y'
),
'category' => false
),
'markup' => array(
'custom_html' => false,
'wpp-start' => '<ul class="wpp-list">',
'wpp-end' => '</ul>',
'post-html' => '<li>{thumb} {title} {stats}</li>',
'post-start' => '<li>',
'post-end' => '</li>',
'title-start' => '<h2>',
'title-end' => '</h2>'
)
);
/**
* Admin page user settings defaults.
*
* @since 2.3.3
* @var array
*/
protected $default_user_settings = array(
'stats' => array(
'order_by' => 'views',
'limit' => 10,
'post_type' => 'post,page',
'freshness' => false
),
'tools' => array(
'ajax' => false,
'css' => true,
'link' => array(
'target' => '_self'
),
'thumbnail' => array(
'source' => 'featured',
'field' => '',
'resize' => false,
'default' => '',
'responsive' => false
),
'log' => array(
'level' => 1,
'limit' => 0,
'expires_after' => 180
),
'cache' => array(
'active' => false,
'interval' => array(
'time' => 'hour',
'value' => 1
)
),
'sampling' => array(
'active' => false,
'rate' => 100
)
)
);
/**
* Admin page user settings.
*
* @since 2.3.3
* @var array
*/
private $user_settings = array();
/**
* Bots list.
*
* @since 3.0.0
* @var array
*/
protected $botlist = array( 'bot', 'crawl', 'curl', 'facebookexternalhit', 'geturl', 'google', 'java', 'msn', 'perl', 'slurp', 'spider', 'sqworm', 'search', 'wget' );
/*--------------------------------------------------*/
/* Constructor
/*--------------------------------------------------*/
/**
* Initialize the widget by setting localization, filters, and administration functions.
*
* @since 1.0.0
*/
public function __construct() {
// Load plugin text domain
add_action( 'init', array( $this, 'widget_textdomain' ) );
// Upgrade check
add_action( 'init', array( $this, 'upgrade_check' ) );
// Check location on template redirect
add_action( 'template_redirect', array( $this, 'is_single' ) );
// Hook fired when a new blog is activated on WP Multisite
add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
// Notices check
add_action( 'admin_notices', array( $this, 'check_admin_notices' ) );
// Create the widget
parent::__construct(
'wpp',
'WordPress Popular Posts',
array(
'classname' => 'popular-posts',
'description' => __( 'The most Popular Posts on your blog.', $this->plugin_slug )
)
);
// Get user options
$this->user_settings = get_site_option('wpp_settings_config');
if ( !$this->user_settings ) {
add_site_option('wpp_settings_config', $this->default_user_settings);
$this->user_settings = $this->default_user_settings;
} else {
$this->user_settings = $this->__merge_array_r( $this->default_user_settings, $this->user_settings );
}
// Allow WP themers / coders to override data sampling status (active/inactive)
$this->user_settings['tools']['sampling']['active'] = apply_filters( 'wpp_data_sampling', $this->user_settings['tools']['sampling']['active'] );
// Add the options page and menu item.
add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) );
// Register admin styles and scripts
add_action( 'admin_print_styles', array( $this, 'register_admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts' ) );
add_action( 'admin_init', array( $this, 'thickbox_setup' ) );
// Register site styles and scripts
if ( $this->user_settings['tools']['css'] )
add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_styles' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_scripts' ) );
// Add plugin settings link
add_filter( 'plugin_action_links', array( $this, 'add_plugin_settings_link' ), 10, 2 );
// Set plugin directory
$this->plugin_dir = plugin_dir_url(__FILE__);
// Get blog charset
$this->charset = get_bloginfo('charset');
// Add ajax table truncation to wp_ajax_ hook
add_action('wp_ajax_wpp_clear_data', array( $this, 'clear_data' ));
// Add thumbnail cache truncation to wp_ajax_ hook
add_action('wp_ajax_wpp_clear_thumbnail', array( $this, 'clear_thumbnails' ));
// Add ajax hook for widget
add_action('wp_ajax_wpp_get_popular', array( $this, 'get_popular') );
add_action('wp_ajax_nopriv_wpp_get_popular', array( $this, 'get_popular') );
// Check if images can be created
if ( extension_loaded('ImageMagick') || (extension_loaded('GD') && function_exists('gd_info')) ) {
// Enable thumbnail feature
$this->thumbnailing = true;
// Get available thumbnail size(s)
$this->default_thumbnail_sizes = $this->__get_image_sizes();
}
// Set default thumbnail
$this->default_thumbnail = $this->plugin_dir . "no_thumb.jpg";
$this->default_user_settings['tools']['thumbnail']['default'] = $this->default_thumbnail;
if ( !empty($this->user_settings['tools']['thumbnail']['default']) )
$this->default_thumbnail = $this->user_settings['tools']['thumbnail']['default'];
else
$this->user_settings['tools']['thumbnail']['default'] = $this->default_thumbnail;
// Set uploads folder
$wp_upload_dir = wp_upload_dir();
$this->uploads_dir['basedir'] = $wp_upload_dir['basedir'] . "/" . $this->plugin_slug;
$this->uploads_dir['baseurl'] = $wp_upload_dir['baseurl'] . "/" . $this->plugin_slug;
if ( !is_dir($this->uploads_dir['basedir']) ) {
if ( !wp_mkdir_p($this->uploads_dir['basedir']) ) {
$this->uploads_dir['basedir'] = $wp_upload_dir['basedir'];
$this->uploads_dir['baseurl'] = $wp_upload_dir['baseurl'];
}
}
// qTrans plugin support
if ( function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage') )
$this->qTrans = true;
// Remove post/page prefetching!
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
// Add the update hooks only if the logging conditions are met
if ( (0 == $this->user_settings['tools']['log']['level'] && !is_user_logged_in()) || (1 == $this->user_settings['tools']['log']['level']) || (2 == $this->user_settings['tools']['log']['level'] && is_user_logged_in()) ) {
add_action( 'wp_head', array(&$this, 'print_ajax') );
// Register views from everyone and/or connected users
if ( 0 != $this->user_settings['tools']['log']['level'] )
add_action( 'wp_ajax_update_views_ajax', array($this, 'update_views_ajax') );
// Register views from everyone and/or visitors only
if ( 2 != $this->user_settings['tools']['log']['level'] )
add_action( 'wp_ajax_nopriv_update_views_ajax', array($this, 'update_views_ajax') );
}
// Add shortcode
add_shortcode('wpp', array(&$this, 'shortcode'));
// Purge post data from DB on deletion
add_action( 'admin_init', array($this, 'purge_post_init') );
// Enable data purging at midnight
if ( 1 == $this->user_settings['tools']['log']['limit'] ) {
add_action( 'wpp_cache_event', array($this, 'purge_data') );
if ( !wp_next_scheduled('wpp_cache_event') ) {
$tomorrow = time() + 86400;
$midnight = mktime(0, 0, 0,
date("m", $tomorrow),
date("d", $tomorrow),
date("Y", $tomorrow));
wp_schedule_event( $midnight, 'daily', 'wpp_cache_event' );
}
} else {
// Remove the scheduled event if exists
if ( $timestamp = wp_next_scheduled('wpp_cache_event') ) {
wp_unschedule_event( $timestamp, 'wpp_cache_event' );
}
}
} // end constructor
/*--------------------------------------------------*/
/* Widget API Functions
/*--------------------------------------------------*/
/**
* Outputs the content of the widget.
*
* @since 1.0.0
* @param array args The array of form elements
* @param array instance The current instance of the widget
*/
public function widget( $args, $instance ) {
$this->__debug($args);
/**
* @var String $name
* @var String $id
* @var String $description
* @var String $class
* @var String $before_widget
* @var String $after_widget
* @var String $before_title
* @var String $after_title
* @var String $widget_id
* @var String $widget_name
*/
extract( $args, EXTR_SKIP );
$markup = ( $instance['markup']['custom_html'] || has_filter('wpp_custom_html') || has_filter('wpp_post') )
? 'custom'
: 'regular';
echo "\n". "<!-- WordPress Popular Posts Plugin v{$this->version} [W] [{$instance['range']}] [{$instance['order_by']}] [{$markup}]" . ( !empty($instance['pid']) ? " [PID]" : "" ) . ( !empty($instance['cat']) ? " [CAT]" : "" ) . ( !empty($instance['author']) ? " [UID]" : "" ) . " -->" . "\n";
echo $before_widget . "\n";
// has user set a title?
if ( '' != $instance['title'] ) {
$title = apply_filters( 'widget_title', $instance['title'] );
if ($instance['markup']['custom_html'] && $instance['markup']['title-start'] != "" && $instance['markup']['title-end'] != "" ) {
echo htmlspecialchars_decode($instance['markup']['title-start'], ENT_QUOTES) . $title . htmlspecialchars_decode($instance['markup']['title-end'], ENT_QUOTES);
} else {
echo $before_title . $title . $after_title;
}
}
if ( $this->user_settings['tools']['ajax'] ) {
if ( empty($before_widget) || !preg_match('/id="[^"]*"/', $before_widget) ) {
?>
<p><?php _e('Error: cannot ajaxify WordPress Popular Posts on this theme. It\'s missing the <em>id</em> attribute on before_widget (see <a href="http://codex.wordpress.org/Function_Reference/register_sidebar" target="_blank" rel="nofollow">register_sidebar</a> for more).', $this->plugin_slug ); ?></p>
<?php
} else {
?>
<script type="text/javascript">//<![CDATA[
// jQuery is available, so proceed
if ( window.jQuery ) {
jQuery(document).ready(function($){
$.get('<?php echo admin_url('admin-ajax.php'); ?>', {
action: 'wpp_get_popular',
id: '<?php echo $this->number; ?>'
}, function(data){
$('#<?php echo $widget_id; ?>').append(data);
});
});
} else { // jQuery is not defined
if ( window.console && window.console.log )
window.console.log('WordPress Popular Posts: jQuery is not defined!');
}
//]]></script>
<?php
}
} else {
echo $this->__get_popular_posts( $instance );
}
echo $after_widget . "\n";
echo "<!-- End WordPress Popular Posts Plugin v{$this->version} -->"."\n";
} // end widget
/**
* Processes the widget's options to be saved.
*
* @since 1.0.0
* @param array new_instance The previous instance of values before the update.
* @param array old_instance The new instance of values to be generated via the update.
* @return array instance Updated instance.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = htmlspecialchars( stripslashes_deep(strip_tags( $new_instance['title'] )), ENT_QUOTES );
$instance['limit'] = ( $this->__is_numeric($new_instance['limit']) && $new_instance['limit'] > 0 )
? $new_instance['limit']
: 10;
$instance['range'] = $new_instance['range'];
$instance['order_by'] = $new_instance['order_by'];
// FILTERS
// user did not define the custom post type name, so we fall back to default
$instance['post_type'] = ( '' == $new_instance['post_type'] )
? 'post,page'
: $new_instance['post_type'];
$instance['freshness'] = isset( $new_instance['freshness'] );
$instance['pid'] = implode(",", array_filter(explode(",", preg_replace( '|[^0-9,]|', '', $new_instance['pid'] ))));
$instance['cat'] = implode(",", array_filter(explode(",", preg_replace( '|[^0-9,-]|', '', $new_instance['cat'] ))));
$instance['author'] = implode(",", array_filter(explode(",", preg_replace( '|[^0-9,]|', '', $new_instance['uid'] ))));
$instance['shorten_title']['words'] = $new_instance['shorten_title-words'];
$instance['shorten_title']['active'] = isset( $new_instance['shorten_title-active'] );
$instance['shorten_title']['length'] = ( $this->__is_numeric($new_instance['shorten_title-length']) && $new_instance['shorten_title-length'] > 0 )
? $new_instance['shorten_title-length']
: 25;
$instance['post-excerpt']['keep_format'] = isset( $new_instance['post-excerpt-format'] );
$instance['post-excerpt']['words'] = $new_instance['post-excerpt-words'];
$instance['post-excerpt']['active'] = isset( $new_instance['post-excerpt-active'] );
$instance['post-excerpt']['length'] = ( $this->__is_numeric($new_instance['post-excerpt-length']) && $new_instance['post-excerpt-length'] > 0 )
? $new_instance['post-excerpt-length']
: 55;
$instance['thumbnail']['active'] = false;
$instance['thumbnail']['width'] = 15;
$instance['thumbnail']['height'] = 15;
// can create thumbnails
if ( $this->thumbnailing ) {
$instance['thumbnail']['active'] = isset( $new_instance['thumbnail-active'] );
$instance['thumbnail']['build'] = $new_instance['thumbnail-size-source'];
// Use predefined thumbnail sizes
if ( 'predefined' == $new_instance['thumbnail-size-source'] ) {
$size = $this->default_thumbnail_sizes[ $new_instance['thumbnail-size'] ];
$instance['thumbnail']['width'] = $size['width'];
$instance['thumbnail']['height'] = $size['height'];
$instance['thumbnail']['crop'] = $size['crop'];
} // Set thumbnail size manually
else {
if ($this->__is_numeric($new_instance['thumbnail-width']) && $this->__is_numeric($new_instance['thumbnail-height'])) {
$instance['thumbnail']['width'] = $new_instance['thumbnail-width'];
$instance['thumbnail']['height'] = $new_instance['thumbnail-height'];
$instance['thumbnail']['crop'] = true;
}
}
}
$instance['rating'] = isset( $new_instance['rating'] );
$instance['stats_tag']['comment_count'] = isset( $new_instance['comment_count'] );
$instance['stats_tag']['views'] = isset( $new_instance['views'] );
$instance['stats_tag']['author'] = isset( $new_instance['author'] );
$instance['stats_tag']['date']['active'] = isset( $new_instance['date'] );
$instance['stats_tag']['date']['format'] = empty($new_instance['date_format'])
? 'F j, Y'
: $new_instance['date_format'];
$instance['stats_tag']['category'] = isset( $new_instance['category'] );
$instance['markup']['custom_html'] = isset( $new_instance['custom_html'] );
$instance['markup']['wpp-start'] = empty($new_instance['wpp-start'])
? htmlspecialchars( '<ul class="wpp-list">', ENT_QUOTES )
: htmlspecialchars( $new_instance['wpp-start'], ENT_QUOTES );
$instance['markup']['wpp-end'] = empty($new_instance['wpp-end'])
? htmlspecialchars( '</ul>', ENT_QUOTES )
: htmlspecialchars( $new_instance['wpp-end'], ENT_QUOTES );
$instance['markup']['post-html'] = empty($new_instance['post-html'])
? htmlspecialchars( '<li>{thumb} {title} {stats}</li>', ENT_QUOTES )
: htmlspecialchars( $new_instance['post-html'], ENT_QUOTES );
$instance['markup']['title-start'] = empty($new_instance['title-start'])
? ''
: htmlspecialchars( $new_instance['title-start'], ENT_QUOTES );
$instance['markup']['title-end'] = empty($new_instance['title-end'])
? '' :
htmlspecialchars( $new_instance['title-end'], ENT_QUOTES );
return $instance;
} // end widget
/**
* Generates the administration form for the widget.
*
* @since 1.0.0
* @param array instance The array of keys and values for the widget.
*/
public function form( $instance ) {
// parse instance values
$instance = $this->__merge_array_r(
$this->defaults,
$instance
);
// Display the admin form
include( plugin_dir_path(__FILE__) . '/views/form.php' );
} // end form
/*--------------------------------------------------*/
/* Public methods
/*--------------------------------------------------*/
/**
* Loads the Widget's text domain for localization and translation.
*
* @since 1.0.0
*/
public function widget_textdomain() {
$domain = $this->plugin_slug;
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
} // end widget_textdomain
/**
* Registers and enqueues admin-specific styles.
*
* @since 1.0.0
*/
public function register_admin_styles() {
if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
return;
}
$screen = get_current_screen();
if ( $screen->id == $this->plugin_screen_hook_suffix ) {
wp_enqueue_style( $this->plugin_slug .'-admin-styles', plugins_url( 'style/admin.css', __FILE__ ), array(), $this->version );
}
} // end register_admin_styles
/**
* Registers and enqueues admin-specific JavaScript.
*
* @since 2.3.4
*/
public function register_admin_scripts() {
if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
return;
}
$screen = get_current_screen();
if ( $screen->id == $this->plugin_screen_hook_suffix ) {
wp_enqueue_script( 'thickbox' );
wp_enqueue_style( 'thickbox' );
wp_enqueue_script( 'media-upload' );
wp_enqueue_script( $this->plugin_slug .'-admin-script', plugins_url( 'js/admin.js', __FILE__ ), array('jquery'), $this->version, true );
}
} // end register_admin_scripts
/**
* Hooks into getttext to change upload button text when uploader is called by WPP.
*
* @since 2.3.4
*/
function thickbox_setup() {
global $pagenow;
if ( 'media-upload.php' == $pagenow || 'async-upload.php' == $pagenow ) {
add_filter( 'gettext', array( $this, 'replace_thickbox_text' ), 1, 3 );
}
} // end thickbox_setup
/**
* Replaces upload button text when uploader is called by WPP.
*
* @since 2.3.4
* @param string translated_text
* @param string text
* @param string domain
* @return string
*/
function replace_thickbox_text($translated_text, $text, $domain) {
if ('Insert into Post' == $text) {
$referer = strpos( wp_get_referer(), 'wpp_admin' );
if ( $referer != '' ) {
return __('Upload', $this->plugin_slug );
}
}
return $translated_text;
} // end replace_thickbox_text
/**
* Registers and enqueues widget-specific styles.
*
* @since 1.0.0
*/
public function register_widget_styles() {
$theme_file = get_stylesheet_directory() . '/wpp.css';
$plugin_file = plugin_dir_path(__FILE__) . 'style/wpp.css';
if ( @file_exists($theme_file) ) { // user stored a custom wpp.css on theme's directory, so use it
wp_enqueue_style( $this->plugin_slug, get_stylesheet_directory_uri() . "/wpp.css", array(), $this->version );
} elseif ( @file_exists($plugin_file) ) { // no custom wpp.css, use plugin's instead
wp_enqueue_style( $this->plugin_slug, plugins_url( 'style/wpp.css', __FILE__ ), array(), $this->version );
}
} // end register_widget_styles
/**
* Registers and enqueues widget-specific scripts.
*/
public function register_widget_scripts() {
// We need jQuery in the front-end only when ajaxifying the widget
if ( $this->user_settings['tools']['ajax'] )
wp_enqueue_script( 'jquery' );
} // end register_widget_scripts
/**
* Register the administration menu for this plugin into the WordPress Dashboard menu.
*
* @since 1.0.0
*/
public function add_plugin_admin_menu() {
$this->plugin_screen_hook_suffix = add_options_page(
'WordPress Popular Posts',
'WordPress Popular Posts',
'manage_options',
$this->plugin_slug,
array( $this, 'display_plugin_admin_page' )
);
}
/**
* Render the settings page for this plugin.
*
* @since 1.0.0
*/
public function display_plugin_admin_page() {
include_once( 'views/admin.php' );
}
/**
* Registers Settings link on plugin description.
*
* @since 2.3.3
* @param array links
* @param string file
* @return array
*/
public function add_plugin_settings_link( $links, $file ){
$this_plugin = plugin_basename(__FILE__);
if ( is_plugin_active($this_plugin) && $file == $this_plugin ) {
$links[] = '<a href="' . admin_url( 'options-general.php?page=wordpress-popular-posts' ) . '">Settings</a>';
}
return $links;
} // end add_plugin_settings_link
/*--------------------------------------------------*/
/* Install / activation / deactivation methods
/*--------------------------------------------------*/
/**
* Return an instance of this class.
*
* @since 3.0.0
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( NULL == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
} // end get_instance
/**
* Fired when the plugin is activated.
*
* @since 1.0.0
* @global object wpdb
* @param bool network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
*/
public static function activate( $network_wide ) {
global $wpdb;
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
// run activation for each blog in the network
if ( $network_wide ) {
$original_blog_id = get_current_blog_id();
$blogs_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
foreach( $blogs_ids as $blog_id ) {
switch_to_blog( $blog_id );
self::__activate();
}
// switch back to current blog
switch_to_blog( $original_blog_id );
return;
}
}
self::__activate();
} // end activate
/**
* Fired when a new blog is activated on WP Multisite.
*
* @since 3.0.0
* @param int blog_id New blog ID
*/
public function activate_new_site( $blog_id ){
if ( 1 !== did_action( 'wpmu_new_blog' ) )
return;
// run activation for the new blog
switch_to_blog( $blog_id );
self::__activate();
// switch back to current blog
restore_current_blog();
} // end activate_new_site
/**
* On plugin activation, checks that the WPP database tables are present.
*
* @since 2.4.0
* @global object wpdb
*/
private static function __activate() {
global $wpdb;
// set table name
$prefix = $wpdb->prefix . "popularposts";
// fresh setup
if ( $prefix != $wpdb->get_var("SHOW TABLES LIKE '{$prefix}data'") ) {
self::__do_db_tables( $prefix );
}
} // end __activate
/**
* Fired when the plugin is deactivated.
*
* @since 1.0.0
* @global object wpbd
* @param bool network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog
*/
public static function deactivate( $network_wide ) {
global $wpdb;
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
// Run deactivation for each blog in the network
if ( $network_wide ) {
$original_blog_id = get_current_blog_id();
$blogs_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
foreach( $blogs_ids as $blog_id ) {
switch_to_blog( $blog_id );
self::__deactivate();
}
// Switch back to current blog
switch_to_blog( $original_blog_id );
return;
}
}
self::__deactivate();
} // end deactivate
/**
* On plugin deactivation, disables the shortcode and removes the scheduled task.
*
* @since 2.4.0
*/
private static function __deactivate() {
remove_shortcode('wpp');
wp_clear_scheduled_hook('wpp_cache_event');
} // end __deactivate
/**
* Checks if an upgrade procedure is required.
*
* @since 2.4.0
*/
public function upgrade_check(){
// Get WPP version
$wpp_ver = get_site_option('wpp_ver');
if ( !$wpp_ver ) {
add_site_option('wpp_ver', $this->version);
} elseif ( version_compare($wpp_ver, $this->version, '<') ) {
$this->__upgrade();
}
} // end upgrade_check
/**
* On plugin upgrade, performs a number of actions: update WPP database tables structures (if needed),
* run the setup wizard (if needed), and some other checks.
*
* @since 2.4.0
* @global object wpdb
*/
private function __upgrade() {
global $wpdb;
// set table name
$prefix = $wpdb->prefix . "popularposts";
// validate the structure of the tables and create missing tables
self::__do_db_tables( $prefix );
// If summary is empty, import data from popularpostsdatacache
if ( !$wpdb->get_var("SELECT COUNT(*) FROM {$prefix}summary") ) {
// popularpostsdatacache table is still there
if ( $wpdb->get_var("SHOW TABLES LIKE '{$prefix}datacache'") ) {
$sql = "
INSERT INTO {$prefix}summary (postid, pageviews, view_date, last_viewed)
SELECT id, pageviews, day_no_time, day
FROM {$prefix}datacache
GROUP BY day_no_time, id
ORDER BY day_no_time DESC";