source: trunk/www.guidonia.net/wp/wp-content/plugins/popularity-contest/popularity-contest.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 84.9 KB
RevLine 
[44]1<?php
2
3/*
4Plugin Name: Popularity Contest
5Plugin URI: http://alexking.org/projects/wordpress
6Description: This will enable ranking of your posts by popularity; using the behavior of your visitors to determine each post's popularity. You set a value (or use the default value) for every post view, comment, etc. and the popularity of your posts is calculated based on those values. Once you have activated the plugin, you can configure the Popularity Values and View Reports. You can also use the included Widgets and Template Tags to display post popularity and lists of popular posts on your blog.
7Version: 2.0b2
8Author: Crowd Favorite
9Author URI: http://crowdfavorite.com
10*/
11
12// Copyright (c) 2005-2009 Alex King, Crowd Favorite, Ltd. All rights reserved.
13//
14// Released under the GPL license
15// http://www.opensource.org/licenses/gpl-license.php
16//
17// This is an add-on for WordPress
18// http://wordpress.org/
19//
20// **********************************************************************
21// This program is distributed in the hope that it will be useful, but
22// WITHOUT ANY WARRANTY; without even the implied warranty of
23// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24// **********************************************************************
25
26// Special thanks to Martijn Stegink for help with WordPress 2.3 compatibility.
27
28/*
29@TODO
30- run reports by join/order by using WP_Query?
31- convert to 2.8 widgets
32- display upgrade needed message on admin pages if upgrade needed
33*/
34
35if (!defined('AKPC_LOADED')) : // LOADED CHECK
36
37@define('AKPC_LOADED', true);
38
39/* -- INSTALLATION --------------------- */
40
41// To hide the popularity score on a per post/page basis, add a custom field to the post/page as follows:
42// name: hide_popularity
43// value: 1
44
45
46// When this is set to 1, WPMU will auto-install popularity contest for each installed blog when installed in the mu-plugins folder
47
48@define('AKPC_MU_AUTOINSTALL', 1);
49
50// Change this to 1 if you want popularity contest to pull its config from this file instead of the database
51// This option hides most of the Popularity Contest admin page
52
53@define('AKPC_CONFIG_FILE', 0);
54
55// By default the view is recorded via an Ajax call from the page. If you want Popularity Contest to do this on the
56// back end set this to 0. Setting this to 0 will cause popularity contest results to improperly tally when caching is
57// turned on. It is recommended to use the API.
58
59@define('AKPC_USE_API', 1);
60
61
62// if pulling settings from this file, set weight values below
63$akpc_settings['show_pop'] = 1; // clickthrough from feed
64$akpc_settings['show_help'] = 1; // clickthrough from feed
65$akpc_settings['ignore_authors'] = 1; // clickthrough from feed
66$akpc_settings['feed_value'] = 1; // clickthrough from feed
67$akpc_settings['home_value'] = 2; // clickthrough from home
68$akpc_settings['archive_value'] = 4; // clickthrough from archive page
69$akpc_settings['category_value'] = 6; // clickthrough from category page
70$akpc_settings['single_value'] = 10; // full article page view
71$akpc_settings['comment_value'] = 20; // comment on article
72$akpc_settings['pingback_value'] = 50; // pingback on article
73$akpc_settings['trackback_value'] = 80; // trackback on article
74$akpc_settings['searcher_names'] = 'google.com yahoo.com bing.com'; // serach engine bot names, space separated
75
76
77// If you would like to show lists of popular posts in the sidebar,
78// take a look at how it is implemented in the included sidebar.php.
79
80/* ------------------------------------- */
81
82load_plugin_textdomain('popularity-contest');
83
84if (is_file(trailingslashit(ABSPATH.PLUGINDIR).'popularity-contest.php')) {
85 define('AKPC_FILE', trailingslashit(ABSPATH.PLUGINDIR).'popularity-contest.php');
86}
87else if (is_file(trailingslashit(ABSPATH.PLUGINDIR).'popularity-contest/popularity-contest.php')) {
88 define('AKPC_FILE', trailingslashit(ABSPATH.PLUGINDIR).'popularity-contest/popularity-contest.php');
89}
90
91register_activation_hook(AKPC_FILE, 'akpc_install');
92
93function akpc_install() {
94 global $akpc;
95 if (!is_a($akpc, 'ak_popularity_contest')) {
96 $akpc = new ak_popularity_contest();
97 }
98 $akpc->install();
99 $akpc->upgrade();
100 $akpc->mine_gap_data();
101}
102
103// -- MAIN FUNCTIONALITY
104
105class ak_popularity_contest {
106 var $feed_value;
107 var $home_value;
108 var $archive_value;
109 var $category_value;
110 var $single_value;
111 var $comment_value;
112 var $pingback_value;
113 var $trackback_value;
114 var $searcher_names;
115 var $logged;
116 var $options;
117 var $top_ranked;
118 var $current_posts;
119 var $show_pop;
120 var $show_help;
121 var $ignore_authors;
122
123 var $report_types;
124
125 function ak_popularity_contest() {
126 $this->options = array(
127 'feed_value'
128 ,'home_value'
129 ,'archive_value'
130 ,'category_value'
131 ,'tag_value'
132 ,'single_value'
133 ,'searcher_value'
134 ,'comment_value'
135 ,'pingback_value'
136 ,'trackback_value'
137 ,'searcher_names'
138 ,'show_pop'
139 ,'show_help'
140 ,'ignore_authors'
141 );
142 $this->feed_value = 1;
143 $this->home_value = 2;
144 $this->archive_value = 4;
145 $this->category_value = 6;
146 $this->tag_value = 6;
147 $this->single_value = 10;
148 $this->searcher_value = 2;
149 $this->comment_value = 20;
150 $this->pingback_value = 50;
151 $this->trackback_value = 80;
152 $this->searcher_names = 'google.com yahoo.com bing.com';
153 $this->logged = 0;
154 $this->show_pop = 1;
155 $this->show_help = 1;
156 $this->ignore_authors = 1;
157 $this->top_ranked = array();
158 $this->current_posts = array();
159 }
160
161 function get_settings() {
162 global $wpdb;
163 if (AKPC_CONFIG_FILE == 1) { // use hard coded settings
164 global $akpc_settings;
165 foreach($akpc_settings as $key => $value) {
166 if (in_array($key, $this->options)) {
167 $this->$key = $value;
168 }
169 }
170 }
171 else { // pull settings from db
172 // This checks to see if the tables are in the DB for this blog
173 $settings = $this->query_settings();
174
175 // If the DB tables are not in place, lets check to see if we can install
176 if (!count($settings)) {
177 // This checks to see if we need to install, then checks if we can install
178 // For the can install to work in MU the AKPC_MU_AUTOINSTALL variable must be set to 1
179 if (!$this->check_install() && $this->can_autoinstall()) {
180 $this->install();
181 }
182 if (!$this->check_install()) {
183 $error = __('
184<h2>Popularity Contest Installation Failed</h2>
185<p>Sorry, Popularity Contest was not successfully installed. Please try again, or try one of the following options for support:</p>
186<ul>
187 <li><a href="http://wphelpcenter.com">WordPress HelpCenter</a> (the official support provider for Popularity Contest)</li>
188 <li><a href="http://wordpress.org">WordPress Forums</a> (community support forums)</li>
189</ul>
190<p>If you are having trouble and need to disable Popularity Contest immediately, simply delete the popularity-contest.php file from within your wp-content/plugins directory.</p>
191 ', 'popularity-contest');
192 wp_die($error);
193 }
194 else {
195 $settings = $this->query_settings();
196 }
197 }
198 if (count($settings)) {
199 foreach ($settings as $setting) {
200 if (in_array($setting->option_name, $this->options)) {
201 $this->{$setting->option_name} = $setting->option_value;
202 }
203 }
204 }
205 }
206 return true;
207 }
208
209 function query_settings() {
210 global $wpdb;
211 return @$wpdb->get_results("
212 SELECT *
213 FROM $wpdb->ak_popularity_options
214 ");
215 }
216
217 /**
218 * check_install - This function checks to see if the proper tables have been added to the DB for the blog the plugin is being activated for
219 *
220 * @return void
221 */
222 function check_install() {
223 global $wpdb;
224 $result = mysql_query("SHOW TABLES LIKE '{$wpdb->prefix}ak_popularity%'", $wpdb->dbh);
225 return mysql_num_rows($result) == 2;
226 }
227
228 /**
229 * can_autoinstall - This function checks to see whether the tables can be installed
230 *
231 * @return void - Checks to see if the blog is MU, if not returns true
232 * - Checks to see if the blog is MU, if it is also checks to see if the function can install and returns true if it can
233 * - (For the second condition to work: ie. if the plugin is installed in MU: AKPC_MU_AUTOINSTALL must be set to 1)
234 */
235 function can_autoinstall() {
236 global $wpmu_version;
237 return (is_null($wpmu_version) || (!is_null($wpmu_version) && AKPC_MU_AUTOINSTALL == 1));
238 }
239
240 /**
241 * install - This function installs the proper tables in the DB for handling popularity contest items
242 *
243 * @return void - Returns whether the table creation was successful
244 */
245 function install() {
246 global $wpdb;
247 if ($this->check_install()) {
248 return;
249 }
250 $result = mysql_query("
251 CREATE TABLE `$wpdb->ak_popularity_options` (
252 `option_name` VARCHAR( 50 ) NOT NULL,
253 `option_value` VARCHAR( 50 ) NOT NULL
254 )
255 ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__);
256 if (!$result) {
257 return false;
258 }
259
260 $this->default_values();
261
262 $result = mysql_query("
263 CREATE TABLE `$wpdb->ak_popularity` (
264 `post_id` INT( 11 ) NOT NULL ,
265 `total` INT( 11 ) NOT NULL ,
266 `feed_views` INT( 11 ) NOT NULL ,
267 `home_views` INT( 11 ) NOT NULL ,
268 `archive_views` INT( 11 ) NOT NULL ,
269 `category_views` INT( 11 ) NOT NULL ,
270 `tag_views` INT( 11 ) NOT NULL ,
271 `single_views` INT( 11 ) NOT NULL ,
272 `searcher_views` INT( 11 ) NOT NULL ,
273 `comments` INT( 11 ) NOT NULL ,
274 `pingbacks` INT( 11 ) NOT NULL ,
275 `trackbacks` INT( 11 ) NOT NULL ,
276 `last_modified` DATETIME NOT NULL ,
277 KEY `post_id` ( `post_id` )
278 )
279 ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__);
280 if (!$result) {
281 return false;
282 }
283
284 $this->mine_data();
285
286 return true;
287 }
288
289 function upgrade() {
290 $this->upgrade_20();
291 }
292
293 function upgrade_20() {
294 global $wpdb;
295
296 $cols = $wpdb->get_col("
297 SHOW COLUMNS FROM $wpdb->ak_popularity
298 ");
299
300 //2.0 Schema
301 if (!in_array('tag_views', $cols)) {
302 $wpdb->query("
303 ALTER TABLE `$wpdb->ak_popularity`
304 ADD `tag_views` INT( 11 ) NOT NULL
305 AFTER `category_views`
306 ");
307 }
308 if (!in_array('searcher_views', $cols)) {
309 $wpdb->query("
310 ALTER TABLE `$wpdb->ak_popularity`
311 ADD `searcher_views` INT( 11 ) NOT NULL
312 AFTER `single_views`
313 ");
314 }
315 $temp = new ak_popularity_contest;
316 $cols = $wpdb->get_col("
317 SELECT `option_name`
318 FROM `$wpdb->ak_popularity_options`
319 ");
320 if (!in_array('searcher_names', $cols)) {
321 $wpdb->query("
322 INSERT
323 INTO `$wpdb->ak_popularity_options` (
324 `option_name`,
325 `option_value`
326 )
327 VALUES (
328 'searcher_names',
329 '$temp->searcher_names'
330 )
331
332 ");
333 }
334 if (!in_array('show_pop', $cols)) {
335 $wpdb->query("
336 INSERT
337 INTO `$wpdb->ak_popularity_options` (
338 `option_name`,
339 `option_value`
340 )
341 VALUES (
342 'show_pop',
343 '$temp->show_pop'
344 )
345
346 ");
347 }
348 if (!in_array('show_help', $cols)) {
349 $wpdb->query("
350 INSERT
351 INTO `$wpdb->ak_popularity_options` (
352 `option_name`,
353 `option_value`
354 )
355 VALUES (
356 'show_help',
357 '$temp->show_help'
358 )
359
360 ");
361 }
362 if (!in_array('ignore_authors', $cols)) {
363 $wpdb->query("
364 INSERT
365 INTO `$wpdb->ak_popularity_options` (
366 `option_name`,
367 `option_value`
368 )
369 VALUES (
370 'ignore_authors',
371 '$temp->ignore_authors'
372 )
373
374 ");
375 }
376 }
377
378 function default_values() {
379 global $wpdb;
380 foreach ($this->options as $option) {
381 $result = $wpdb->query("
382 INSERT
383 INTO $wpdb->ak_popularity_options
384 VALUES (
385 '$option',
386 '{$this->$option}'
387 )
388 ");
389 if (!$result) {
390 return false;
391 }
392 }
393 return true;
394 }
395
396 function update_settings() {
397 if (!current_user_can('manage_options')) { wp_die('Unauthorized.'); }
398 global $wpdb;
399 $this->upgrade();
400 foreach ($this->options as $option) {
401 if (isset($_POST[$option])) {
402 $option != 'searcher_names' ? $this->$option = intval($_POST[$option]) : $this->$option = stripslashes($_POST[$option]);
403 $wpdb->query("
404 UPDATE $wpdb->ak_popularity_options
405 SET option_value = '{$this->$option}'
406 WHERE option_name = '".$wpdb->escape($option)."'
407 ");
408 }
409 }
410 $this->recalculate_popularity();
411 $this->mine_gap_data();
412 header('Location: '.get_bloginfo('wpurl').'/wp-admin/options-general.php?page='.basename(__FILE__).'&updated=true');
413 die();
414 }
415
416 function recalculate_popularity() {
417 global $wpdb;
418 $result = $wpdb->query("
419 UPDATE $wpdb->ak_popularity
420 SET total = (home_views * $this->home_value)
421 + (feed_views * $this->feed_value)
422 + (archive_views * $this->archive_value)
423 + (category_views * $this->category_value)
424 + (tag_views * $this->tag_value)
425 + (single_views * $this->single_value)
426 + (searcher_views * $this->searcher_value)
427 + (comments * $this->comment_value)
428 + (pingbacks * $this->pingback_value)
429 + (trackbacks * $this->trackback_value)
430 ");
431 }
432
433 function reset_data() {
434 global $wpdb;
435 $result = $wpdb->query("
436 TRUNCATE $wpdb->ak_popularity
437 ");
438 if (!$result) {
439 return false;
440 }
441
442 $result = $wpdb->query("
443 TRUNCATE $wpdb->ak_popularity_options
444 ");
445 if (!$result) {
446 return false;
447 }
448
449 $this->default_values();
450 return true;
451 }
452
453 function create_post_record($post_id = -1) {
454 global $wpdb;
455 if ($post_id == -1) {
456 global $post_id;
457 }
458 $post_id = intval($post_id);
459 $count = $wpdb->get_var("
460 SELECT COUNT(post_id)
461 FROM $wpdb->ak_popularity
462 WHERE post_id = '$post_id'
463 ");
464 if (!intval($count)) {
465 $result = $wpdb->query("
466 INSERT
467 INTO $wpdb->ak_popularity (
468 `post_id`,
469 `last_modified`
470 )
471 VALUES (
472 '$post_id',
473 '".date('Y-m-d H:i:s')."'
474 )
475 ");
476 }
477 }
478
479 function delete_post_record($post_id = -1) {
480 global $wpdb;
481 if ($post_id == -1) {
482 global $post_id;
483 }
484 $result = $wpdb->query("
485 DELETE
486 FROM $wpdb->ak_popularity
487 WHERE post_id = '$post_id'
488 ");
489
490 }
491
492 function mine_data() {
493 global $wpdb;
494 $posts = $wpdb->get_results("
495 SELECT ID
496 FROM $wpdb->posts
497 WHERE post_status = 'publish'
498 ");
499 if ($posts && count($posts) > 0) {
500 foreach ($posts as $post) {
501 $this->create_post_record($post->ID);
502 $this->populate_post_data($post->ID);
503 }
504 }
505 return true;
506 }
507
508 function mine_gap_data() {
509 global $wpdb;
510 $posts = $wpdb->get_results("
511 SELECT p.ID
512 FROM $wpdb->posts p
513 LEFT JOIN $wpdb->ak_popularity pop
514 ON p.ID = pop.post_id
515 WHERE pop.post_id IS NULL
516 AND (
517 p.post_type = 'post'
518 OR p.post_type = 'page'
519 )
520 AND p.post_status = 'publish'
521 ");
522 if ($posts && count($posts) > 0) {
523 foreach ($posts as $post) {
524 $this->create_post_record($post->ID);
525 $this->populate_post_data($post->ID);
526 }
527 }
528 }
529
530 function populate_post_data($post_id) {
531 global $wpdb;
532// grab existing comments
533 $count = intval($wpdb->get_var("
534 SELECT COUNT(*)
535 FROM $wpdb->comments
536 WHERE comment_post_ID = '$post_id'
537 AND comment_type = ''
538 AND comment_approved = '1'
539 "));
540 if ($count > 0) {
541 $result = $wpdb->query("
542 UPDATE $wpdb->ak_popularity
543 SET comments = comments + $count
544 , total = total + ".($this->comment_value * $count)."
545 WHERE post_id = '$post_id'
546 ");
547 if (!$result) {
548 return false;
549 }
550 }
551
552// grab existing trackbacks
553 $count = intval($wpdb->get_var("
554 SELECT COUNT(*)
555 FROM $wpdb->comments
556 WHERE comment_post_ID = '$post_id'
557 AND comment_type = 'trackback'
558 AND comment_approved = '1'
559 "));
560 if ($count > 0) {
561 $result = $wpdb->query("
562 UPDATE $wpdb->ak_popularity
563 SET trackbacks = trackbacks + $count
564 , total = total + ".($this->trackback_value * $count)."
565 WHERE post_id = '$post_id'
566 ");
567 if (!$result) {
568 return false;
569 }
570 }
571
572// grab existing pingbacks
573 $count = intval($wpdb->get_var("
574 SELECT COUNT(*)
575 FROM $wpdb->comments
576 WHERE comment_post_ID = '$post_id'
577 AND comment_type = 'pingback'
578 AND comment_approved = '1'
579 "));
580 if ($count > 0) {
581 $result = $wpdb->query("
582 UPDATE $wpdb->ak_popularity
583 SET pingbacks = pingbacks + $count
584 , total = total + ".($this->pingback_value * $count)."
585 WHERE post_id = '$post_id'
586 ");
587 if (!$result) {
588 return false;
589 }
590 }
591 }
592
593 function record_view($api = false, $ids = false, $type = false) {
594 if ($this->logged > 0 || ($this->ignore_authors && current_user_can('publish_posts'))) {
595 return true;
596 }
597
598 global $wpdb;
599
600 if ($api == false) {
601 global $posts;
602
603 if (!isset($posts) || !is_array($posts) || count($posts) == 0 || is_admin()) {
604 return;
605 }
606
607 $ids = array();
608 $ak_posts = $posts;
609 foreach ($ak_posts as $post) {
610 $ids[] = $post->ID;
611 }
612 }
613 if (!$ids || !count($ids)) {
614 return;
615 }
616 if (($api && $type == 'feed') || is_feed()) {
617 $result = $wpdb->query("
618 UPDATE $wpdb->ak_popularity
619 SET feed_views = feed_views + 1
620 , total = total + $this->feed_value
621 WHERE post_id IN (".implode(',', $ids).")
622 ");
623 if (!$result) {
624 return false;
625 }
626 }
627 else if (($api && $type == 'archive') || (is_archive() && !is_category())) {
628 $result = $wpdb->query("
629 UPDATE $wpdb->ak_popularity
630 SET archive_views = archive_views + 1
631 , total = total + $this->archive_value
632 WHERE post_id IN (".implode(',', $ids).")
633 ");
634 if (!$result) {
635 return false;
636 }
637 }
638 else if (($api && $type == 'category') || is_category()) {
639 $result = $wpdb->query("
640 UPDATE $wpdb->ak_popularity
641 SET category_views = category_views + 1
642 , total = total + $this->category_value
643 WHERE post_id IN (".implode(',', $ids).")
644 ");
645 if (!$result) {
646 return false;
647 }
648 }
649 else if (($api && $type == 'tag') || is_tag()) {
650 $result = $wpdb->query("
651 UPDATE $wpdb->ak_popularity
652 SET tag_views = tag_views + 1
653 , total = total + $this->tag_views
654 WHERE post_id IN (".implode(',', $ids).")
655 ");
656 if (!$result) {
657 return false;
658 }
659 }
660 else if (($api && in_array($type, array('single', 'page'))) || is_single() || is_singular() || is_page()) {
661 if (($api && $type == 'searcher') || akpc_is_searcher()) {
662 $result = $wpdb->query("
663 UPDATE $wpdb->ak_popularity
664 SET searcher_views = searcher_views + 1
665 , total = total + $this->searcher_value
666 WHERE post_id = '".$ids[0]."'
667 ");
668 if (!$result) {
669 return false;
670 }
671 }
672 $result = $wpdb->query("
673 UPDATE $wpdb->ak_popularity
674 SET single_views = single_views + 1
675 , total = total + $this->single_value
676 WHERE post_id = '".$ids[0]."'
677 ");
678 if (!$result) {
679 return false;
680 }
681 }
682 else {
683 $result = $wpdb->query("
684 UPDATE $wpdb->ak_popularity
685 SET home_views = home_views + 1
686 , total = total + $this->home_value
687 WHERE post_id IN (".implode(',', $ids).")
688 ");
689 if (!$result) {
690 return false;
691 }
692 }
693 $this->logged++;
694 return true;
695 }
696
697 function record_feedback($type, $action = '+', $comment_id = null) {
698 global $wpdb, $comment_post_ID;
699 if ($comment_id) {
700 $comment_post_ID = $comment_id;
701 }
702 switch ($type) {
703 case 'trackback':
704 $result = $wpdb->query("
705 UPDATE $wpdb->ak_popularity
706 SET trackbacks = trackbacks $action 1
707 , total = total $action $this->trackback_value
708 WHERE post_id = '$comment_post_ID'
709 ");
710 if (!$result) {
711 return false;
712 }
713 break;
714 case 'pingback':
715 $result = $wpdb->query("
716 UPDATE $wpdb->ak_popularity
717 SET pingbacks = pingbacks $action 1
718 , total = total $action $this->pingback_value
719 WHERE post_id = '$comment_post_ID'
720 ");
721 if (!$result) {
722 return false;
723 }
724 break;
725 default:
726 $result = $wpdb->query("
727 UPDATE $wpdb->ak_popularity
728 SET comments = comments $action 1
729 , total = total $action $this->comment_value
730 WHERE post_id = '$comment_post_ID'
731 ");
732 if (!$result) {
733 return false;
734 }
735 break;
736 }
737 return true;
738 }
739
740 function edit_feedback($comment_id, $action, $status = null) {
741 $comment = get_comment($comment_id);
742 switch ($action) {
743 case 'delete':
744 $this->record_feedback($comment->comment_type, '-', $comment_id);
745 break;
746 case 'status':
747 if ($status == 'spam') {
748 $this->record_feedback($comment->comment_type, '-', $comment_id);
749 return;
750 }
751 break;
752 }
753 }
754
755 function recount_feedback() {
756 global $wpdb;
757 $post_ids = $wpdb->get_results("
758 SELECT ID
759 FROM $wpdb->posts
760 WHERE post_status = 'publish'
761 OR post_status = 'static'
762 ");
763
764 if (count($post_ids)) {
765 $result = $wpdb->query("
766 UPDATE $wpdb->ak_popularity
767 SET comments = 0
768 , trackbacks = 0
769 , pingbacks = 0
770 ");
771 foreach ($post_ids as $post_id) {
772 $this->populate_post_data($post_id);
773 }
774 }
775 $this->recalculate_popularity();
776
777 header('Location: '.get_bloginfo('wpurl').'/wp-admin/options-general.php?page='.basename(__FILE__).'&updated=true');
778 die();
779 }
780
781 function options_form() {
782 if (!AKPC_CONFIG_FILE) { // don't show options update functions if we're running from a config file
783 $temp = new ak_popularity_contest;
784 print('<div class="wrap">');
785 $yes_no = array(
786 'show_pop',
787 'show_help',
788 'ignore_authors',
789 );
790 foreach ($yes_no as $key) {
791 $var = $key.'_options';
792 if ($this->$key == '0') {
793 $$var = '
794 <option value="1">'.__('Yes', 'popularity-contest').'</option>
795 <option value="0" selected="selected">'.__('No', 'popularity-contest').'</option>
796 ';
797 }
798 else {
799 $$var = '
800 <option value="1" selected="selected">'.__('Yes', 'popularity-contest').'</option>
801 <option value="0">'.__('No', 'popularity-contest').'</option>
802 ';
803 }
804 }
805
806 print('
807 <h2>'.__('Popularity Contest Options', 'popularity-contest').'</h2>
808 <form name="ak_popularity" action="'.get_bloginfo('wpurl').'/wp-admin/options-general.php" method="post">
809 <fieldset class="options">
810 <h3>'.__('Settings', 'popularity-contest').'</h3>
811 <p>
812 <label for="akpc_ignore_authors">'.__('Ignore views by site authors:', 'popularity-contest').'</label>
813 <select name="ignore_authors" id="akpc_ignore_authors">
814 '.$ignore_authors_options.'
815 </select>
816 </p>
817 <p>
818 <label for="akpc_show_pop">'.__('Show popularity rank for posts:', 'popularity-contest').'</label>
819 <select name="show_pop" id="akpc_show_pop">
820 '.$show_pop_options.'
821 </select>
822 </p>
823 <p>
824 <label for="akpc_show_help">'.__('Show the [?] help link:', 'popularity-contest').'</label>
825 <select name="show_help" id="akpc_show_help">
826 '.$show_help_options.'
827 </select>
828 </p>
829 <p>
830 <label>'.__('Search Engine Domains (space separated):', 'popularity-contest').'</label><br/>
831 <textarea name="searcher_names" id="searcher_names" rows="2" cols="50">'.htmlspecialchars($this->searcher_names).'</textarea>
832 </p>
833 </fieldset>
834 <fieldset class="options">
835 <h3>'.__('Popularity Values', 'popularity-contest').'</h3>
836 <p>'.__('Adjust the values below as you see fit. When you save the new options the <a href="index.php?page=popularity-contest.php"><strong>popularity rankings</strong></a> for your posts will be automatically updated to reflect the new values you have chosen.', 'popularity-contest').'</p>
837 <table width="100%" cellspacing="2" cellpadding="5" class="editform" id="akpc_options">
838 <tr valign="top">
839 <th width="33%" scope="row"><label for="single_value">'.__('Permalink Views:', 'popularity-contest').'</label></th>
840 <td><input type="text" class="number" name="single_value" id="single_value" value="'.$this->single_value.'" /> '.__("(default: $temp->single_value)", 'popularity-contest').'</td>
841 </tr>
842 <tr valign="top">
843 <th width="33%" scope="row"><label for="searcher_value">'.__('Permalink Views from Search Engines:', 'popularity-contest').'</label></th>
844 <td><input type="text" class="number" name="searcher_value" id="searcher_value" value="'.$this->searcher_value.'" /> '.__("(default: $temp->searcher_value)", 'popularity-contest').'</td>
845 </tr>
846 <tr valign="top">
847 <th width="33%" scope="row"><label for="home_value">'.__('Home Views:', 'popularity-contest').'</label></th>
848 <td><input type="text" class="number" name="home_value" id="home_value" value="'.$this->home_value.'" /> '.__("(default: $temp->home_value)", 'popularity-contest').'</td>
849 </tr>
850 <tr valign="top">
851 <th width="33%" scope="row"><label for="archive_value">'.__('Archive Views:', 'popularity-contest').'</label></th>
852 <td><input type="text" class="number" name="archive_value" id="archive_value" value="'.$this->archive_value.'" /> '.__("(default: $temp->archive_value)", 'popularity-contest').'</td>
853 </tr>
854 <tr valign="top">
855 <th width="33%" scope="row"><label for="category_value">'.__('Category Views:', 'popularity-contest').'</label></th>
856 <td><input type="text" class="number" name="category_value" id="category_value" value="'.$this->category_value.'" /> '.__("(default: $temp->category_value)", 'popularity-contest').'</td>
857 </tr>
858 <tr valign="top">
859 <th width="33%" scope="row"><label for="tag_value">'.__('Tag Views:', 'popularity-contest').'</label></th>
860 <td><input type="text" class="number" name="tag_value" id="tag_value" value="'.$this->tag_value.'" /> '.__("(default: $temp->tag_value)", 'popularity-contest').'</td>
861 </tr>
862 <tr valign="top">
863 <th width="33%" scope="row"><label for="feed_value">'.__('Feed Views (full content only):', 'popularity-contest').'</label></th>
864 <td><input type="text" class="number" name="feed_value" id="feed_value" value="'.$this->feed_value.'" /> '.__("(default: $temp->feed_value)", 'popularity-contest').'</td>
865 </tr>
866 <tr valign="top">
867 <th width="33%" scope="row"><label for="comment_value">'.__('Comments:', 'popularity-contest').'</label></th>
868 <td><input type="text" class="number" name="comment_value" id="comment_value" value="'.$this->comment_value.'" /> '.__("(default: $temp->comment_value)", 'popularity-contest').'</td>
869 </tr>
870 <tr valign="top">
871 <th width="33%" scope="row"><label for="pingback_value">'.__('Pingbacks:', 'popularity-contest').'</label></th>
872 <td><input type="text" class="number" name="pingback_value" id="pingback_value" value="'.$this->pingback_value.'" /> '.__("(default: $temp->pingback_value)", 'popularity-contest').'</td>
873 </tr>
874 <tr valign="top">
875 <th width="33%" scope="row"><label for="trackback_value">'.__('Trackbacks:', 'popularity-contest').'</label></th>
876 <td><input type="text" class="number" name="trackback_value" id="trackback_value" value="'.$this->trackback_value.'" /> '.__("(default: $temp->trackback_value)", 'popularity-contest').'</td>
877 </tr>
878 </table>
879 <h3>'.__('Example', 'popularity-contest').'</h3>
880 <ul>
881 <li>'.__('Post #1 receives 11 Home Page Views (11 * 2 = 22), 6 Permalink Views (6 * 10 = 60) and 3 Comments (3 * 20 = 60) for a total value of: <strong>142</strong>', 'popularity-contest').'</li>
882 <li>'.__('Post #2 receives 7 Home Page Views (7 * 2 = 14), 10 Permalink Views (10 * 10 = 100), 7 Comments (7 * 20 = 140) and 3 Trackbacks (3 * 80 = 240) for a total value of: <strong>494</strong>', 'popularity-contest').'</li>
883 </ul>
884 <hr style="margin: 20px 40px; border: 0; border-top: 1px solid #ccc;" />
885 <input type="hidden" name="ak_action" value="update_popularity_values" />
886 </fieldset>
887 <p class="submit">
888 <input type="submit" name="submit" value="'.__('Save Popularity Contest Options', 'popularity-contest').'" class="button-primary" />
889 <input type="button" name="recount" value="'.__('Reset Comments/Trackback/Pingback Counts', 'popularity-contest').'" onclick="location.href=\''.get_bloginfo('wpurl').'/wp-admin/options-general.php?ak_action=recount_feedback\';" />
890 </p>
891 </form>
892 ');
893 }
894 print('
895 <div id="akpc_template_tags">
896 <h2>'.__('Popularity Contest Template Tags', 'popularity-contest').'</h2>
897 <dl>
898 <dt><code>akpc_the_popularity()</code></dt>
899 <dd>
900 <p>'.__('Put this tag within <a href="http://codex.wordpress.org/The_Loop">The Loop</a> to show the popularity of the post being shown. The popularity is shown as a percentage of your most popular post. For example, if the popularity total for Post #1 is 500 and your popular post has a total of 1000, this tag will show a value of <strong>50%</strong>.', 'popularity-contest').'</p>
901 <p>Example:</p>
902 <ul>
903 <li><code>&lt;?php if (function_exists(\'akpc_the_popularity\')) { akpc_the_popularity(); } ?></code></li>
904 </ul>
905 </dd>
906 <dt><code>akpc_most_popular($limit = 10, $before = &lt;li>, $after = &lt;/li>)</code></dt>
907 <dd>
908 <p>'.__('Put this tag outside of <a href="http://codex.wordpress.org/The_Loop">The Loop</a> (perhaps in your sidebar?) to show a list (like the archives/categories/links list) of your most popular posts. All arguments are optional, the defaults are included in the example above.', 'popularity-contest').'</p>
909 <p>Examples:</p>
910 <ul>
911 <li><code>&lt;?php if (function_exists(\'akpc_most_popular\')) { akpc_most_popular(); } ?></code></li>
912 <li><code>
913 &lt;?php if (function_exists(\'akpc_most_popular\')) { ?><br />
914 &lt;li>&lt;h2>Most Popular Posts&lt;/h2><br />
915 &nbsp;&nbsp; &lt;ul><br />
916 &nbsp;&nbsp; &lt;?php akpc_most_popular(); ?><br />
917 &nbsp;&nbsp; &lt;/ul><br />
918 &lt;/li><br />
919 &lt;?php } ?>
920 </code></li>
921 </ul>
922 </dd>
923 <dt><code>akpc_most_popular_in_cat($limit = 10, $before = &lt;li>, $after = &lt;/li>, $cat_ID = current category)</code></dt>
924 <dd>
925 <p>'.__('Put this tag outside of <a href="http://codex.wordpress.org/The_Loop">The Loop</a> (perhaps in your sidebar?) to show a list of the most popular posts in a specific category. You may want to use this on category archive pages. All arguments are', 'popularity-contest').'</p>
926 <p>Examples:</p>
927 <ul>
928 <li><code>&lt;?php if (function_exists(\'akpc_most_popular_in_cat\')) { akpc_most_popular_in_cat(); } ?></code></li>
929 <li><code>&lt;php if (is_category() && function_exists(\'akpc_most_popular_in_cat\')) { akpc_most_popular_in_cat(); } ?></code></li>
930 <li><code>
931 &lt;?php if (is_category() && function_exists(\'akpc_most_popular_in_cat\')) { ?><br />
932 &lt;li>&lt;h2>Most Popular in \'&lt;?php single_cat_title(); ?>\'&lt;/h2><br />
933 &nbsp;&nbsp; &lt;ul><br />
934 &nbsp;&nbsp; &lt;?php akpc_most_popular_in_cat(); ?><br />
935 &nbsp;&nbsp; &lt;/ul><br />
936 &lt;/li><br />
937 &lt;?php } ?>
938 </code></li>
939 </ul>
940 </dd>
941 <dt><code>akpc_most_popular_in_month($limit, $before, $after, $m = YYYYMM)</code></dt>
942 <dd>
943 <p>'.__('Put this tag outside of <a href="http://codex.wordpress.org/The_Loop">The Loop</a> (perhaps in your sidebar?) to show a list of the most popular posts in a specific month. You may want to use this on monthly archive pages.', 'popularity-contest').'</p>
944 <p>Examples:</p>
945 <ul>
946 <li><code>&lt;?php if (function_exists(\'akpc_most_popular_in_month\')) { akpc_most_popular_in_month(); } ?></code></li>
947 <li><code>&lt;php if (is_archive() && is_month() && function_exists(\'akpc_most_popular_in_month\')) { akpc_most_popular_in_month(); } ?></code></li>
948 <li><code>
949 &lt;?php if (is_archive() && is_month() && function_exists(\'akpc_most_popular_in_month\')) { ?><br />
950 &lt;li>&lt;h2>Most Popular in &lt;?php the_time(\'F, Y\'); ?>&lt;/h2><br />
951 &nbsp;&nbsp; &lt;ul><br />
952 &nbsp;&nbsp; &lt;?php akpc_most_popular_in_month(); ?><br />
953 &nbsp;&nbsp; &lt;/ul><br />
954 &lt;/li><br />
955 &lt;?php } ?>
956 </code></li>
957 </ul>
958 </dd>
959 <dt><code>akpc_most_popular_in_last_days($limit, $before, $after, $days = 45)</code></dt>
960 <dd>
961 <p>'.__('Put this tag outside of <a href="http://codex.wordpress.org/The_Loop">The Loop</a> (perhaps in your sidebar?) to show a list of the most popular posts in the last (your chosen number, default = 45) days.', 'popularity-contest').'</p>
962 <p>Examples:</p>
963 <ul>
964
965 <li><code>&lt;?php if (function_exists(\'akpc_most_popular_in_last_days\')) { akpc_most_popular_in_last_days(); } ?></code></li>
966 <li><code>
967 &lt;?php if (function_exists(\'akpc_most_popular_in_last_days\')) { ?><br />
968 &lt;li>&lt;h2>Recent Popular Posts&lt;/h2><br />
969 &nbsp;&nbsp; &lt;ul><br />
970 &nbsp;&nbsp; &lt;?php akpc_most_popular_in_last_days(); ?><br />
971 &nbsp;&nbsp; &lt;/ul><br />
972 &lt;/li><br />
973 &lt;?php } ?>
974 </code></li>
975 </ul>
976 </dd>
977 </dl>
978 </div>
979 </div>
980 ');
981 }
982
983 function get_popular_posts($type = 'popular', $limit, $exclude_pages = 'yes', $custom = array()) {
984 global $wpdb;
985 $items = array();
986 switch($type) {
987 case 'category':
988 $temp = "
989 SELECT p.ID AS ID, p.post_title AS post_title, pop.total AS total
990 FROM $wpdb->posts p
991 LEFT JOIN $wpdb->ak_popularity pop
992 ON p.ID = pop.post_id
993 LEFT JOIN $wpdb->term_relationships tr
994 ON p.ID = tr.object_id
995 LEFT JOIN $wpdb->term_taxonomy tt
996 ON tt.term_taxonomy_id = tr.term_taxonomy_id
997 WHERE tt.term_id = ".$custom['cat_ID']."
998 AND p.post_status = 'publish'
999 ";
1000 if ($exclude_pages == 'yes') { $temp .= " AND p.post_type != 'page' "; }
1001 $temp .= "
1002 ORDER BY pop.total DESC
1003 LIMIT $limit
1004 ";
1005 $items = $wpdb->get_results($temp);
1006 break;
1007 case 'tag':
1008 $temp = "
1009 SELECT p.ID AS ID, p.post_title AS post_title, pop.total AS total
1010 FROM $wpdb->posts p
1011 LEFT JOIN $wpdb->ak_popularity pop
1012 ON p.ID = pop.post_id
1013 LEFT JOIN $wpdb->term_relationships tr
1014 ON p.ID = tr.object_id
1015 LEFT JOIN $wpdb->term_taxonomy tt
1016 ON tt.term_taxonomy_id = tr.term_taxonomy_id
1017 WHERE tt.term_id = ".$custom['term_id']."
1018 AND p.post_status = 'publish'
1019 ";
1020 if ($exclude_pages == 'yes') { $temp .= " AND p.post_type != 'page' "; }
1021 $temp .= "
1022 ORDER BY pop.total DESC
1023 LIMIT $limit
1024 ";
1025 $items = $wpdb->get_results($temp);
1026 break;
1027 case 'category_popularity':
1028 $temp = "
1029 SELECT DISTINCT name, AVG(pop.total) AS avg
1030 FROM $wpdb->posts p
1031 LEFT JOIN $wpdb->ak_popularity pop
1032 ON p.ID = pop.post_id
1033 LEFT JOIN $wpdb->term_relationships tr
1034 ON p.ID = tr.object_id
1035 LEFT JOIN $wpdb->term_taxonomy tt
1036 ON tr.term_taxonomy_id = tt.term_taxonomy_id
1037 LEFT JOIN $wpdb->terms t
1038 ON tt.term_id = t.term_id
1039 WHERE tt.taxonomy = 'category'
1040 ";
1041 if ($exclude_pages == 'yes') { $temp .= " AND p.post_type != 'page' "; }
1042 $temp .= "
1043 GROUP BY name
1044 ORDER BY avg DESC
1045 LIMIT 50
1046 ";
1047 $items = $wpdb->get_results($temp);
1048 break;
1049 case 'tag_popularity':
1050 $temp = "
1051 SELECT DISTINCT name, AVG(pop.total) AS avg
1052 FROM $wpdb->posts p
1053 LEFT JOIN $wpdb->ak_popularity pop
1054 ON p.ID = pop.post_id
1055 LEFT JOIN $wpdb->term_relationships tr
1056 ON p.ID = tr.object_id
1057 LEFT JOIN $wpdb->term_taxonomy tt
1058 ON tr.term_taxonomy_id = tt.term_taxonomy_id
1059 LEFT JOIN $wpdb->terms t
1060 ON tt.term_id = t.term_id
1061 WHERE tt.taxonomy = 'post_tag'
1062 ";
1063 if ($exclude_pages == 'yes') { $temp .= " AND p.post_type != 'page' "; }
1064 $temp .= "
1065 GROUP BY name
1066 ORDER BY avg DESC
1067 LIMIT 50
1068 ";
1069 $items = $wpdb->get_results($temp);
1070 break;
1071 case 'year':
1072 $temp = "
1073 SELECT MONTH(p.post_date) AS month, AVG(pop.total) AS avg
1074 FROM $wpdb->posts p
1075 LEFT JOIN $wpdb->ak_popularity pop
1076 ON p.ID = pop.post_id
1077 WHERE YEAR(p.post_date) = '".$custom['y']."'
1078 AND p.post_status = 'publish'
1079 ";
1080 if ($exclude_pages == 'yes') { $temp .= " AND p.post_type != 'page' "; }
1081 $temp .= "
1082 GROUP BY month
1083 ORDER BY avg DESC
1084 ";
1085 $items = $wpdb->get_results($temp);
1086 break;
1087 case 'views_wo_feedback':
1088 $temp = "
1089 SELECT p.ID AS ID, p.post_title AS post_title, pop.total AS total
1090 FROM $wpdb->posts p
1091 LEFT JOIN $wpdb->ak_popularity pop
1092 ON p.ID = pop.post_id
1093 WHERE pop.comments = 0
1094 AND pop.pingbacks = 0
1095 AND pop.trackbacks = 0
1096 AND p.post_status = 'publish'
1097 ";
1098 if ($exclude_pages == 'yes') { $temp .= " AND p.post_type != 'page' "; }
1099 $temp .= "
1100 ORDER BY pop.total DESC
1101 LIMIT $limit
1102 ";
1103 $items = $wpdb->get_results($temp);
1104 break;
1105 case 'most_feedback':
1106 // in progress, should probably be combination of comment, pingback & trackback scores
1107 $temp = "
1108 SELECT p.ID, p.post_title, p.comment_count
1109 FROM $wpdb->posts p
1110 LEFT JOIN $wpdb->ak_popularity pop ON p.ID = pop.post_id
1111 WHERE p.post_status = 'publish'
1112 AND p.comment_count > 0";
1113 if ($exclude_pages == 'yes') { $temp .= " AND p.post_type != 'page' "; }
1114 $temp = "
1115 ORDER BY p.comment_count DESC
1116 LIMIT $limit;
1117 ";
1118 $items = $wpdb->get_results($temp);
1119 break;
1120 case 'date':
1121 $temp = "
1122 SELECT p.ID AS ID, p.post_title AS post_title, pop.total AS total
1123 FROM $wpdb->posts p
1124 LEFT JOIN $wpdb->ak_popularity pop
1125 ON p.ID = pop.post_id
1126 WHERE DATE_ADD(p.post_date, INTERVAL ".intval($custom['days'])." DAY) {$custom['compare']} DATE_ADD(NOW(), INTERVAL ".intval($custom['offset'])." DAY)
1127 AND p.post_status = 'publish'
1128 ";
1129 if ($exclude_pages == 'yes') { $temp .= " AND p.post_type != 'page' "; }
1130 $temp .= "
1131 ORDER BY pop.total DESC
1132 LIMIT $limit
1133 ";
1134 $items = $wpdb->get_results($temp);
1135 break;
1136 case 'most':
1137 $temp = "
1138 SELECT p.ID AS ID, p.post_title AS post_title, pop.{$custom['column']} AS {$custom['column']}
1139 FROM $wpdb->posts p
1140 LEFT JOIN $wpdb->ak_popularity pop
1141 ON p.ID = pop.post_id
1142 WHERE p.post_status = 'publish'
1143 ";
1144 if ($exclude_pages == 'yes') { $temp .= " AND p.post_type != 'page' "; }
1145 $temp .= "
1146 ORDER BY pop.{$custom['column']} DESC
1147 LIMIT $limit
1148 ";
1149 $items = $wpdb->get_results($temp);
1150 break;
1151 case 'popular':
1152 $temp = "
1153 SELECT p.ID AS ID, p.post_title AS post_title, pop.{$custom['column']} AS {$custom['column']}
1154 FROM $wpdb->posts p
1155 LEFT JOIN $wpdb->ak_popularity pop
1156 ON p.ID = pop.post_id
1157 WHERE p.post_status = 'publish'
1158 ";
1159 if ($exclude_pages == 'yes') { $temp .= " AND p.post_type != 'page' "; }
1160 $temp .= "
1161 ORDER BY pop.{$custom['column']} DESC
1162 LIMIT $limit
1163 ";
1164 $items = $wpdb->get_results($temp);
1165 break;
1166 case 'popular_pages':
1167 $temp = "
1168 SELECT p.ID AS ID, p.post_title AS post_title, pop.single_views AS single_views
1169 FROM $wpdb->posts p
1170 LEFT JOIN $wpdb->ak_popularity pop
1171 ON p.ID = pop.post_id
1172 WHERE p.post_status = 'publish'
1173 AND p.post_type = 'page'
1174 ORDER BY pop.single_views DESC
1175 LIMIT $limit
1176 ";
1177 $items = $wpdb->get_results($temp);
1178 break;
1179 }
1180
1181 do_action('akpc_get_popular_posts',$items);
1182
1183 if (count($items)) {
1184 return $items;
1185 }
1186 return false;
1187 }
1188
1189 /**
1190 * Show a popularity report
1191 * @var string $type - type of report to show
1192 * @var int $limit - num posts to show
1193 * @var array $custom - pre-defined list of posts to show
1194 * @var bool $hide_title - wether to echo the list title
1195 */
1196 function show_report($type = 'popular', $limit = 10, $exclude_pages = 'yes', $custom = array(), $before_title = '<h3>', $after_title = '</h3>', $hide_title = false) {
1197 global $wpdb;
1198
1199 if (count($custom) > 0 && 1 == 0) {
1200 }
1201 else {
1202 $query = '';
1203 $column = '';
1204 $list = '';
1205 $items = array();
1206 $rel = '';
1207 switch ($type) {
1208 case 'category':
1209 $title = $custom['cat_name'];
1210 $items = $this->get_popular_posts($type, $limit, $exclude_pages, $custom);
1211 $list = $this->report_list_items($items, $before = '<li>', $after = '</li>');
1212 break;
1213 case 'tag':
1214 $title = $custom['term_name'];
1215 $rel = sanitize_title($title);
1216 $items = $this->get_popular_posts($type, $limit, $exclude_pages, $custom);
1217 $list = $this->report_list_items($items, $before = '<li>', $after = '</li>');
1218 break;
1219 case 'pop_by_category':
1220 $cats = get_categories();
1221 if (count($cats)) {
1222 foreach ($cats as $cat) {
1223 $this->show_report('category', 10, $exclude_pages, array('cat_ID' => $cat->term_id, 'cat_name' => $cat->name));
1224 }
1225 }
1226 break;
1227 case 'pop_by_tag':
1228 $tags = maybe_unserialize(get_option('akpc_tag_reports'));
1229 if (is_array($tags) && count($tags)) {
1230 foreach ($tags as $tag) {
1231 $term = get_term_by('slug', $tag, 'post_tag');
1232 $this->show_report('tag', 10, $exclude_pages, array('term_id' => $term->term_id, 'term_name' => $term->name));
1233 }
1234 }
1235 break;
1236 case 'category_popularity':
1237 $title = __('Average by Category', 'popularity-contest');
1238 $items = $this->get_popular_posts($type, $limit, $exclude_pages);
1239 if (is_array($items) && count($items)) {
1240 foreach ($items as $item) {
1241 $list .= ' <li>
1242 <span>'.$this->get_rank(ceil($item->avg)).'</span>
1243 '.$item->name.'
1244 </li>'."\n";
1245 }
1246 }
1247 break;
1248 case 'tag_popularity':
1249 $title = __('Average by Tag', 'popularity-contest');
1250 $items = $this->get_popular_posts($type, $limit, $exclude_pages);
1251 if (is_array($items) && count($items)) {
1252 foreach ($items as $item) {
1253 $list .= ' <li>
1254 <span>'.$this->get_rank(ceil($item->avg)).'</span>
1255 '.$item->name.'
1256 </li>'."\n";
1257 }
1258 }
1259 break;
1260 case 'year':
1261 global $month;
1262 $title = $custom['y'].__(' Average by Month', 'popularity-contest');
1263 $items = $this->get_popular_posts($type,$limit, $exclude_pages,$custom);
1264 if (is_array($items) && count($items)) {
1265 foreach ($items as $item) {
1266 $list .= ' <li>
1267 <span>'.$this->get_rank(ceil($item->avg)).'</span>
1268 '.$month[str_pad($item->month, 2, '0', STR_PAD_LEFT)].'
1269 </li>'."\n";
1270 }
1271 }
1272 break;
1273 case 'month_popularity':
1274 $years = array();
1275 $years = $wpdb->get_results("
1276 SELECT DISTINCT YEAR(post_date) AS year
1277 FROM $wpdb->posts
1278 ORDER BY year DESC
1279 ");
1280 $i = 2;
1281 if (count($years) > 0) {
1282 foreach ($years as $year) {
1283 $this->show_report('year', 10, $exclude_pages, array('y' => $year->year));
1284 if ($i == 3) {
1285 print('
1286 <div class="clear"></div>
1287 ');
1288 $i = 0;
1289 }
1290 $i++;
1291 }
1292 }
1293 break;
1294 case 'views_wo_feedback':
1295 $title = __('Views w/o Feedback', 'popularity-contest');
1296 $items = $this->get_popular_posts($type, $limit, $exclude_pages);
1297 $list = $this->report_list_items($items, $before = '<li>', $after = '</li>');
1298 break;
1299 case 'most_feedback':
1300 $query = 'sum';
1301 $column = 'pop.comments + pop.pingbacks + pop.trackbacks AS feedback';
1302 $title = __('Feedback', 'popularity-contest');
1303 break;
1304 case '365_plus':
1305 $offset = -365;
1306 $compare = '<';
1307 $title = __('Older Than 1 Year', 'popularity-contest');
1308 $items = $this->get_popular_posts('date', $limit, $exclude_pages, array('days' => $days, 'offset' => $offset, 'compare' => $compare));
1309 $list = $this->report_list_items($items, $before = '<li>', $after = '</li>');
1310 break;
1311 case 'last_30':
1312 case 'last_60':
1313 case 'last_90':
1314 case 'last_365':
1315 case 'last_n':
1316 $compare = '>';
1317 $offset = $days = '0';
1318 switch(str_replace('last_','',$type)) {
1319 case '30':
1320 $days = 30;
1321 $title = __('Last 30 Days', 'popularity-contest');
1322 break;
1323 case '60':
1324 $days = 60;
1325 $title = __('Last 60 Days', 'popularity-contest');
1326 break;
1327 case '90':
1328 $days = 90;
1329 $title = __('Last 90 Days', 'popularity-contest');
1330 break;
1331 case '365':
1332 $days = 365;
1333 $title = __('Last Year', 'popularity-contest');
1334 break;
1335 case 'n':
1336 $days = $custom['days'];
1337 if ($days == 1) {
1338 $title = __('Last Day', 'popularity-contest');
1339 }
1340 else {
1341 $title = sprintf(__('Last %s Days', 'popularity-contest'), $days);
1342 }
1343 break;
1344 }
1345 $items = $this->get_popular_posts('date', $limit, $exclude_pages, array('days' => $days, 'offset' => $offset, 'compare' => $compare));
1346 $list = $this->report_list_items($items, $before = '<li>', $after = '</li>');
1347 break;
1348 case 'most_feed_views':
1349 case 'most_home_views':
1350 case 'most_archive_views':
1351 case 'most_category_views':
1352 case 'most_tag_views':
1353 case 'most_single_views':
1354 case 'most_searcher_views':
1355 case 'most_comments':
1356 case 'most_pingbacks':
1357 case 'most_trackbacks':
1358 switch($type) {
1359 case 'most_feed_views':
1360 $query = 'most';
1361 $column = 'feed_views';
1362 $title = __('Feed Views', 'popularity-contest');
1363 break;
1364 case 'most_home_views':
1365 $query = 'most';
1366 $column = 'home_views';
1367 $title = __('Home Page Views', 'popularity-contest');
1368 break;
1369 case 'most_archive_views':
1370 $query = 'most';
1371 $column = 'archive_views';
1372 $title = __('Archive Views', 'popularity-contest');
1373 break;
1374 case 'most_category_views':
1375 $query = 'most';
1376 $column = 'category_views';
1377 $title = __('Category Views', 'popularity-contest');
1378 break;
1379 case 'most_tag_views':
1380 $query = 'most';
1381 $column = 'tag_views';
1382 $title = __('Tag Views', 'popularity-contest');
1383 break;
1384 case 'most_single_views':
1385 $query = 'most';
1386 $column = 'single_views';
1387 $title = __('Single Post Views', 'popularity-contest');
1388 break;
1389 case 'most_searcher_views':
1390 $query = 'most';
1391 $column = 'searcher_views';
1392 $title = __('Search Engine Traffic', 'popularity-contest');
1393 break;
1394 case 'most_comments':
1395 $query = 'most';
1396 $column = 'comments';
1397 $title = __('Comments', 'popularity-contest');
1398 break;
1399 case 'most_pingbacks':
1400 $query = 'most';
1401 $column = 'pingbacks';
1402 $title = __('Pingbacks', 'popularity-contest');
1403 break;
1404 case 'most_trackbacks':
1405 $query = 'most';
1406 $column = 'trackbacks';
1407 $title = __('Trackbacks', 'popularity-contest');
1408 break;
1409 }
1410 $items = $this->get_popular_posts('most', $limit, $exclude_pages, array('column' => $column));
1411 if (is_array($items) && count($items)) {
1412 foreach ($items as $item) {
1413 $list .= ' <li>
1414 <span>'.$item->$column.'</span>
1415 <a href="'.get_permalink($item->ID).'">'.$item->post_title.'</a>
1416 </li>'."\n";
1417 }
1418 }
1419 else {
1420 $list = '<li>'.__('(none)', 'popularity-contest').'</li>';
1421 }
1422 break;
1423 case 'most_page_views':
1424 $column = 'single_views';
1425 $title = __('Page Views', 'popularity-contest');
1426 $items = $this->get_popular_posts('popular_pages', $limit, $exclude_pages, array('column' => $column));
1427 if (is_array($items) && count($items)) {
1428 foreach ($items as $item) {
1429 $list .= ' <li>
1430 <span>'.$item->$column.'</span>
1431 <a href="'.get_permalink($item->ID).'">'.$item->post_title.'</a>
1432 </li>'."\n";
1433 }
1434 }
1435 else {
1436 $list = '<li>'.__('(none)', 'popularity-contest').'</li>';
1437 }
1438 break;
1439 case 'popular':
1440 $query = 'popular';
1441 $column = 'total';
1442 $title = __('Most Popular', 'popularity-contest');
1443 $items = $this->get_popular_posts($type, $limit, $exclude_pages, array('column' => $column));
1444 if (is_array($items) && count($items)) {
1445 foreach ($items as $item) {
1446 $list .= ' <li>
1447 <span>'.$this->get_post_rank(null, $item->total).'</span>
1448 <a href="'.get_permalink($item->ID).'">'.$item->post_title.'</a>
1449 </li>'."\n";
1450 }
1451 }
1452 else {
1453 $list = '<li>'.__('(none)', 'popularity-contest').'</li>';
1454 }
1455 break;
1456 }
1457 }
1458
1459 if (!empty($list)) {
1460 $html = '
1461 <div class="akpc_report" rel="'.$rel.'">
1462 '.($hide_title ? '' : $before_title.$title.$after_title).'
1463 <ol>
1464 '.$list.'
1465 </ol>
1466 </div>
1467 ';
1468 echo apply_filters('akpc_show_report', $html, $items);
1469 }
1470 }
1471
1472 /**
1473 * create a list of popular items for a report
1474 * @var array $items
1475 * @return string - HTML
1476 */
1477 function report_list_items($items, $before = '<li>', $after = '<li>') {
1478 if (!$items || !count($items)) { return false; }
1479
1480 $html = '';
1481 foreach($items as $item) {
1482 $html .= $before.
1483 '<span>'.$this->get_post_rank(null, $item->total).'</span><a href="'.get_permalink($item->ID).'">'.$item->post_title.'</a>'.
1484 $after;
1485 }
1486 return $html;
1487 }
1488
1489 function show_report_extended($type = 'popular', $limit = 50) {
1490 global $wpdb, $post;
1491 $columns = array(
1492 'popularity' => __('', 'popularity-contest')
1493 ,'title' => __('Title', 'popularity-contest')
1494 ,'categories' => __('Categories', 'popularity-contest')
1495 ,'single_views' => __('Single', 'popularity-contest')
1496 ,'searcher_views' => __('Search', 'popularity-contest')
1497 ,'category_views' => __('Cat', 'popularity-contest')
1498 ,'tag_views' => __('Tag', 'popularity-contest')
1499 ,'archive_views' => __('Arch', 'popularity-contest')
1500 ,'home_views' => __('Home', 'popularity-contest')
1501 ,'feed_views' => __('Feed', 'popularity-contest')
1502 ,'comments' => __('Com', 'popularity-contest')
1503 ,'pingbacks' => __('Ping', 'popularity-contest')
1504 ,'trackbacks' => __('Track', 'popularity-contest')
1505 );
1506?>
1507<div id="akpc_most_popular">
1508 <table width="100%" cellpadding="3" cellspacing="2">
1509 <tr>
1510<?php
1511 foreach($columns as $column_display_name) {
1512?>
1513 <th scope="col"><?php echo $column_display_name; ?></th>
1514<?php
1515 }
1516?>
1517 </tr>
1518<?php
1519 $posts = $wpdb->get_results("
1520 SELECT p.*, pop.*
1521 FROM $wpdb->posts p
1522 LEFT JOIN $wpdb->ak_popularity pop
1523 ON p.ID = pop.post_id
1524 WHERE p.post_status = 'publish'
1525 ORDER BY pop.total DESC
1526 LIMIT ".intval($limit)
1527 );
1528 if ($posts) {
1529 $bgcolor = '';
1530 foreach ($posts as $post) {
1531 $class = ('alternate' == $class) ? '' : 'alternate';
1532?>
1533 <tr class='<?php echo $class; ?>'>
1534<?php
1535 foreach($columns as $column_name => $column_display_name) {
1536 switch($column_name) {
1537 case 'popularity':
1538?>
1539 <td class="right"><?php $this->show_post_rank(null, $post->total); ?></td>
1540<?php
1541 break;
1542 case 'title':
1543?>
1544 <td><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></td>
1545<?php
1546 break;
1547 case 'categories':
1548?>
1549 <td><?php if ($post->post_type == 'post') { the_category(','); } ?></td>
1550<?php
1551 break;
1552 case 'single_views':
1553?>
1554 <td class="right"><?php print($post->single_views); ?></td>
1555<?php
1556 break;
1557 case 'searcher_views':
1558?>
1559 <td class="right"><?php print($post->searcher_views); ?></td>
1560<?php
1561 break;
1562 case 'category_views':
1563?>
1564 <td class="right"><?php print($post->category_views); ?></td>
1565<?php
1566 break;
1567 case 'tag_views':
1568?>
1569 <td class="right"><?php print($post->tag_views); ?></td>
1570<?php
1571 break;
1572 case 'archive_views':
1573?>
1574 <td class="right"><?php print($post->archive_views); ?></td>
1575<?php
1576 break;
1577 case 'home_views':
1578?>
1579 <td class="right"><?php print($post->home_views); ?></td>
1580<?php
1581 break;
1582 case 'feed_views':
1583?>
1584 <td class="right"><?php print($post->feed_views); ?></td>
1585<?php
1586 break;
1587 case 'comments':
1588?>
1589 <td class="right"><?php print($post->comments); ?></td>
1590<?php
1591 break;
1592 case 'pingbacks':
1593?>
1594 <td class="right"><?php print($post->pingbacks); ?></td>
1595<?php
1596 break;
1597 case 'trackbacks':
1598?>
1599 <td class="right"><?php print($post->trackbacks); ?></td>
1600<?php
1601 break;
1602 }
1603 }
1604?>
1605 </tr>
1606<?php
1607 }
1608 }
1609 else {
1610?>
1611 <tr style='background-color: <?php echo $bgcolor; ?>'>
1612 <td colspan="8"><?php _e('No posts found.') ?></td>
1613 </tr>
1614<?php
1615 } // end if ($posts)
1616?>
1617 </table>
1618</div>
1619<?php
1620 }
1621
1622 function view_stats($limit = 100) {
1623 global $wpdb, $post;
1624 print('
1625 <div class="wrap ak_wrap">
1626 <h2>'.__('Most Popular', 'popularity-contest').'</h2>
1627 ');
1628
1629 $this->show_report_extended('popular', 50);
1630
1631 print('
1632 <p id="akpc_options_link"><a href="options-general.php?page=popularity-contest.php">Change Popularity Values</a></p>
1633
1634 <div class="pop_group">
1635 <h2>'.__('Date Range', 'popularity-contest').'</h2>
1636 ');
1637
1638 $this->show_report('last_30');
1639 $this->show_report('last_60');
1640 $this->show_report('last_90');
1641 $this->show_report('last_365');
1642 $this->show_report('365_plus');
1643
1644 print('
1645 </div>
1646 <div class="clear"></div>
1647 <div class="pop_group">
1648 <h2>'.__('Views', 'popularity-contest').'</h2>
1649 ');
1650
1651 $this->show_report('most_single_views');
1652 $this->show_report('most_page_views');
1653 $this->show_report('most_searcher_views');
1654 $this->show_report('most_category_views');
1655 $this->show_report('most_tag_views');
1656 $this->show_report('most_archive_views');
1657 $this->show_report('most_home_views');
1658 $this->show_report('most_feed_views');
1659
1660 print('
1661 </div>
1662 <div class="clear"></div>
1663 <div class="pop_group">
1664 <h2>'.__('Feedback', 'popularity-contest').'</h2>
1665 ');
1666
1667 $this->show_report('most_comments');
1668 $this->show_report('most_pingbacks');
1669 $this->show_report('most_trackbacks');
1670 $this->show_report('views_wo_feedback');
1671
1672 print('
1673 </div>
1674 <div class="clear"></div>
1675 <h2>'.__('Averages', 'popularity-contest').'</h2>
1676 ');
1677
1678 $this->show_report('category_popularity');
1679 $this->show_report('tag_popularity');
1680 $this->show_report('month_popularity');
1681
1682 print('
1683 <div class="clear"></div>
1684 <div class="pop_group" id="akpc_tag_reports">
1685 <h2>'.__('Tags', 'popularity-contest').'
1686 <form action="'.site_url('index.php').'" method="post" id="akpc_report_tag_form">
1687 <label for="akpc_tag_add">'.__('Add report for tag:', 'popularity-contest').'</label>
1688 <input type="text" name="akpc_tag_add" id="akpc_tag_add" value="" />
1689 <input type="submit" name="submit_button" value="'.__('Add', 'popularity-contest').'" />
1690 <input type="hidden" name="ak_action" value="akpc_add_tag" />
1691 <span class="akpc_saving">'.__('Adding tag...'. 'popularity-contest').'</span>
1692 </form>
1693 </h2>
1694 ');
1695
1696 $this->show_report('pop_by_tag');
1697
1698 print('
1699 <div class="akpc_padded none">'.__('No tag reports chosen.', 'popularity-contest').'</div>
1700 </div>
1701 <div class="clear"></div>
1702 <div class="pop_group">
1703 <h2>'.__('Categories', 'popularity-contest').'</h2>
1704 ');
1705
1706 $this->show_report('pop_by_category');
1707
1708 print('
1709 </div>
1710 <div class="clear"></div>
1711 </div>
1712 ');
1713?>
1714<script type="text/javascript">
1715akpc_flow_reports = function() {
1716 var reports = jQuery('div.akpc_report').css('visibility', 'hidden');
1717 jQuery('div.akpc-auto-insert').remove();
1718 var akpc_reports_per_row = Math.floor(jQuery('div.pop_group').width()/250);
1719 jQuery('div.pop_group').each(function() {
1720 var i = 1;
1721 jQuery(this).find('div.akpc_report').each(function() {
1722 if (i % akpc_reports_per_row == 0) {
1723 jQuery(this).after('<div class="clear akpc-auto-insert"></div>');
1724 }
1725 i++;
1726 });
1727 });
1728 akpc_tag_reports_none();
1729 reports.css('visibility', 'visible');
1730}
1731akpc_tag_report_remove_links = function() {
1732 jQuery('#akpc_tag_reports a.remove').remove();
1733 jQuery('#akpc_tag_reports .akpc_report').each(function() {
1734 jQuery(this).prepend('<a href="<?php echo site_url('index.php?ak_action=akpc_remove_tag&tag='); ?>' + jQuery(this).attr('rel') + '" class="remove"><?php _e('[X]', 'popuarity-contest'); ?></a>');
1735 });
1736 jQuery('#akpc_tag_reports a.remove').click(function() {
1737 report = jQuery(this).parents('#akpc_tag_reports .akpc_report');
1738 report.html('<div class="akpc_padded"><?php _e('Removing...', 'popularity-contest'); ?></div>');
1739 jQuery.post(
1740 '<?php echo site_url('index.php'); ?>',
1741 {
1742 'ak_action': 'akpc_remove_tag',
1743 'tag': report.attr('rel')
1744 },
1745 function(response) {
1746 report.remove();
1747 akpc_flow_reports();
1748 },
1749 'html'
1750 );
1751 return false;
1752 });
1753}
1754akpc_tag_reports_none = function() {
1755 none_msg = jQuery('#akpc_tag_reports .none');
1756 if (jQuery('#akpc_tag_reports .akpc_report').size()) {
1757 none_msg.hide();
1758 }
1759 else {
1760 none_msg.show();
1761 }
1762}
1763jQuery(function($) {
1764 akpc_flow_reports();
1765 akpc_tag_report_remove_links();
1766 $('#akpc_tag_add').suggest( 'admin-ajax.php?action=ajax-tag-search&tax=post_tag', { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } );
1767 $('#akpc_report_tag_form').submit(function() {
1768 var tag = $('#akpc_tag_add').val();
1769 if (tag.length > 0) {
1770 var add_button = $(this).find('input[type="submit"]');
1771 var saving_msg = $(this).find('span.akpc_saving');
1772 add_button.hide();
1773 saving_msg.show();
1774 $.post(
1775 '<?php echo site_url('index.php'); ?>',
1776 {
1777 'ak_action': 'akpc_add_tag',
1778 'tag': tag
1779 },
1780 function(response) {
1781 $('#akpc_tag_add').val('');
1782 $('#akpc_tag_reports').append(response);
1783 akpc_flow_reports();
1784 akpc_tag_report_remove_links()
1785 saving_msg.hide();
1786 add_button.show();
1787 },
1788 'html'
1789 );
1790 }
1791 return false;
1792 });
1793});
1794jQuery(window).bind('resize', akpc_flow_reports);
1795</script>
1796<?php
1797 }
1798
1799 function get_post_total($post_id) {
1800 if (!isset($this->current_posts['id_'.$post_id])) {
1801 $this->get_current_posts(array($post_id));
1802 }
1803 return $this->current_posts['id_'.$post_id];
1804 }
1805
1806 function get_rank($item, $total = null) {
1807 if (is_null($total)) {
1808 $total = $this->top_rank();
1809 }
1810 return ceil(($item/$total) * 100).'%';
1811 }
1812
1813 function get_post_rank($post_id = null, $total = -1) {
1814 if (count($this->top_ranked) == 0) {
1815 $this->get_top_ranked();
1816 }
1817 if ($total > -1 && !$post_id) {
1818 return ceil(($total/$this->top_rank()) * 100).'%';
1819 }
1820 if (isset($this->top_ranked['id_'.$post_id])) {
1821 $rank = $this->top_ranked['id_'.$post_id];
1822 }
1823 else {
1824 $rank = $this->get_post_total($post_id);
1825 }
1826 $show_help = apply_filters('akpc_show_help', $this->show_help, $post_id);
1827 if ($show_help) {
1828 $suffix = ' <span class="akpc_help">[<a href="http://alexking.org/projects/wordpress/popularity-contest" title="'.__('What does this mean?', 'popularity-contest').'">?</a>]</span>';
1829 }
1830 else {
1831 $suffix = '';
1832 }
1833 if (isset($rank) && $rank != false) {
1834 return __('Popularity:', 'popularity-contest').' '.$this->get_rank($rank).$suffix;
1835 }
1836 else {
1837 return __('Popularity:', 'popularity-contest').' '.__('unranked', 'popularity-contest').$suffix;
1838 }
1839 }
1840
1841 function show_post_rank($post_id = -1, $total = -1) {
1842 print($this->get_post_rank($post_id, $total));
1843 }
1844
1845 function top_rank() {
1846 if (count($this->top_ranked) == 0) {
1847 $this->get_top_ranked();
1848 }
1849 foreach ($this->top_ranked as $id => $rank) {
1850 $top = $rank;
1851 break;
1852 }
1853 // handle edge case - div by zero
1854 if (intval($top) == 0) {
1855 $top = 1;
1856 }
1857 return $top;
1858 }
1859
1860 function get_current_posts($post_ids = array()) {
1861 global $wpdb, $wp_query;
1862 $posts = $wp_query->get_posts();
1863 $ids = array();
1864 foreach ($posts as $post) {
1865 $ids[] = $post->ID;
1866 }
1867 $ids = array_unique(array_merge($ids, $post_ids));
1868 if (count($ids)) {
1869 $result = $wpdb->get_results("
1870 SELECT post_id, total
1871 FROM $wpdb->ak_popularity
1872 WHERE post_id IN (".implode(',', $ids).")
1873 ");
1874
1875 if (count($result)) {
1876 foreach ($result as $data) {
1877 $this->current_posts['id_'.$data->post_id] = $data->total;
1878 }
1879 }
1880 }
1881 return true;
1882 }
1883
1884
1885 function get_top_ranked() {
1886 global $wpdb;
1887 $result = $wpdb->get_results("
1888 SELECT post_id, total
1889 FROM $wpdb->ak_popularity
1890 ORDER BY total DESC
1891 LIMIT 10
1892 ");
1893
1894 if (!count($result)) {
1895 return false;
1896 }
1897
1898 foreach ($result as $data) {
1899 $this->top_ranked['id_'.$data->post_id] = $data->total;
1900 }
1901
1902 return true;
1903 }
1904
1905 function show_top_ranked($limit, $before, $after) {
1906 if ($posts=$this->get_top_ranked_posts($limit)) {
1907 foreach ($posts as $post) {
1908 print(
1909 $before.'<a href="'.get_permalink($post->ID).'">'
1910 .$post->post_title.'</a>'.$after
1911 );
1912 }
1913 }
1914 else {
1915 print($before.'(none)'.$after);
1916 }
1917 }
1918
1919 function get_top_ranked_posts($limit) {
1920 global $wpdb;
1921 $temp = $wpdb;
1922
1923 $join = apply_filters('posts_join', '');
1924 $where = apply_filters('posts_where', '');
1925 $groupby = apply_filters('posts_groupby', '');
1926 if (!empty($groupby)) {
1927 $groupby = ' GROUP BY '.$groupby;
1928 }
1929 else {
1930 $groupby = ' GROUP BY '.$wpdb->posts.'.ID ';
1931 }
1932
1933 $posts = $wpdb->get_results("
1934 SELECT ID, post_title
1935 FROM $wpdb->posts
1936 LEFT JOIN $wpdb->ak_popularity pop
1937 ON $wpdb->posts.ID = pop.post_id
1938 $join
1939 WHERE post_status = 'publish'
1940 AND post_date < NOW()
1941 $where
1942 $groupby
1943 ORDER BY pop.total DESC
1944 LIMIT ".intval($limit)
1945 );
1946
1947 $wpdb = $temp;
1948
1949 return $posts;
1950 }
1951
1952 function show_top_ranked_in_cat($limit, $before, $after, $cat_ID = '') {
1953 if (empty($cat_ID) && is_category()) {
1954 global $cat;
1955 $cat_ID = $cat;
1956 }
1957 if (empty($cat_ID)) {
1958 return;
1959 }
1960 global $wpdb;
1961 $temp = $wpdb;
1962
1963 $join = apply_filters('posts_join', '');
1964 $where = apply_filters('posts_where', '');
1965 $groupby = apply_filters('posts_groupby', '');
1966 if (!empty($groupby)) {
1967 $groupby = ' GROUP BY '.$groupby;
1968 }
1969 else {
1970 $groupby = ' GROUP BY p.ID ';
1971 }
1972
1973 $posts = $wpdb->get_results("
1974 SELECT ID, post_title
1975 FROM $wpdb->posts p
1976 LEFT JOIN $wpdb->term_relationships tr
1977 ON p.ID = tr.object_id
1978 LEFT JOIN $wpdb->term_taxonomy tt
1979 ON tr.term_taxonomy_id = tt.term_taxonomy_id
1980 LEFT JOIN $wpdb->ak_popularity pop
1981 ON p.ID = pop.post_id
1982 $join
1983 WHERE tt.term_id = '".intval($cat_ID)."'
1984 AND tt.taxonomy = 'category'
1985 AND post_status = 'publish'
1986 AND post_type = 'post'
1987 AND post_date < NOW()
1988 $where
1989 $groupby
1990 ORDER BY pop.total DESC
1991 LIMIT ".intval($limit)
1992 );
1993 if ($posts) {
1994 foreach ($posts as $post) {
1995 print(
1996 $before.'<a href="'.get_permalink($post->ID).'">'
1997 .$post->post_title.'</a>'.$after
1998 );
1999 }
2000 }
2001 else {
2002 print($before.'(none)'.$after);
2003 }
2004 $wpdb = $temp;
2005 }
2006
2007 function show_top_ranked_in_month($limit, $before, $after, $m = '') {
2008 if (empty($m) && is_archive()) {
2009 global $m;
2010 }
2011 if (empty($m)) {
2012 global $post;
2013 $m = get_the_time('Ym');
2014 }
2015 if (empty($m)) {
2016 return;
2017 }
2018 $year = substr($m, 0, 4);
2019 $month = substr($m, 4, 2);
2020 global $wpdb;
2021 $temp = $wpdb;
2022
2023 $join = apply_filters('posts_join', '');
2024 $where = apply_filters('posts_where', '');
2025 $groupby = apply_filters('posts_groupby', '');
2026 if (!empty($groupby)) {
2027 $groupby = ' GROUP BY '.$groupby;
2028 }
2029 else {
2030 $groupby = ' GROUP BY '.$wpdb->posts.'.ID ';
2031 }
2032
2033 $posts = $wpdb->get_results("
2034 SELECT ID, post_title
2035 FROM $wpdb->posts
2036 LEFT JOIN $wpdb->ak_popularity pop
2037 ON $wpdb->posts.ID = pop.post_id
2038 $join
2039 WHERE YEAR(post_date) = '$year'
2040 AND MONTH(post_date) = '$month'
2041 AND post_status = 'publish'
2042 AND post_date < NOW()
2043 $where
2044 $groupby
2045 ORDER BY pop.total DESC
2046 LIMIT ".intval($limit)
2047 );
2048 if ($posts) {
2049 foreach ($posts as $post) {
2050 print(
2051 $before.'<a href="'.get_permalink($post->ID).'">'
2052 .$post->post_title.'</a>'.$after
2053 );
2054 }
2055 }
2056 else {
2057 print($before.'(none)'.$after);
2058 }
2059 $wpdb = $temp;
2060 }
2061
2062 function show_top_ranked_in_last_days($limit, $before, $after, $days = 45) {
2063 global $wpdb;
2064 $temp = $wpdb;
2065
2066 $join = apply_filters('posts_join', '');
2067 $where = apply_filters('posts_where', '');
2068 $groupby = apply_filters('posts_groupby', '');
2069 if (!empty($groupby)) { $groupby = ' GROUP BY '.$groupby; }
2070
2071 $offset = 0;
2072 $compare = '>';
2073
2074 $posts = $wpdb->get_results("
2075 SELECT ID, post_title
2076 FROM $wpdb->posts
2077 LEFT JOIN $wpdb->ak_popularity pop
2078 ON $wpdb->posts.ID = pop.post_id
2079 $join
2080 WHERE DATE_ADD($wpdb->posts.post_date, INTERVAL $days DAY) $compare DATE_ADD(NOW(), INTERVAL $offset DAY)
2081 AND post_status = 'publish'
2082 AND post_date < NOW()
2083 $where
2084 $groupby
2085 ORDER BY pop.total DESC
2086 LIMIT ".intval($limit)
2087 );
2088 if ($posts) {
2089 foreach ($posts as $post) {
2090 print(
2091 $before.'<a href="'.get_permalink($post->ID).'">'
2092 .$post->post_title.'</a>'.$after
2093 );
2094 }
2095 }
2096 else {
2097 print($before.'(none)'.$after);
2098 }
2099 $wpdb = $temp;
2100 }
2101
2102}
2103
2104// -- "HOOKABLE" FUNCTIONS
2105
2106function akpc_init() {
2107 global $wpdb, $akpc;
2108
2109 $wpdb->ak_popularity = $wpdb->prefix.'ak_popularity';
2110 $wpdb->ak_popularity_options = $wpdb->prefix.'ak_popularity_options';
2111
2112 $akpc = new ak_popularity_contest;
2113 $akpc->get_settings();
2114}
2115
2116function akpc_view($content) {
2117 global $akpc;
2118 $akpc->record_view();
2119 return $content;
2120}
2121
2122function akpc_feedback_comment() {
2123 global $akpc;
2124 $akpc->record_feedback('comment');
2125}
2126
2127function akpc_comment_status($comment_id, $status = 'approved') {
2128 global $akpc;
2129 $akpc->edit_feedback($comment_id, 'status', $status);
2130}
2131
2132function akpc_comment_delete($comment_id) {
2133 global $akpc;
2134 $akpc->edit_feedback($comment_id, 'delete');
2135}
2136
2137function akpc_feedback_pingback() {
2138 global $akpc;
2139 $akpc->record_feedback('pingback');
2140}
2141
2142function akpc_feedback_trackback() {
2143 global $akpc;
2144 $akpc->record_feedback('trackback');
2145}
2146
2147function akpc_publish($post_id) {
2148 global $akpc;
2149 $akpc->create_post_record($post_id);
2150}
2151
2152function akpc_post_delete($post_id) {
2153 global $akpc;
2154 $akpc->delete_post_record($post_id);
2155}
2156
2157function akpc_options_form() {
2158 global $akpc;
2159 $akpc->options_form();
2160}
2161
2162function akpc_view_stats() {
2163 global $akpc;
2164 $akpc->view_stats();
2165}
2166
2167function akpc_plugin_action_links($links, $file) {
2168 $plugin_file = basename(__FILE__);
2169 if (basename($file) == $plugin_file) {
2170 $settings_link = '<a href="options-general.php?page='.$plugin_file.'">'.__('Settings', 'popularity-contest').'</a>';
2171 array_unshift($links, $settings_link);
2172 $reports_link = '<a href="index.php?page='.$plugin_file.'">'.__('Reports', 'popularity-contest').'</a>';
2173 array_unshift($links, $reports_link);
2174 }
2175 return $links;
2176}
2177add_filter('plugin_action_links', 'akpc_plugin_action_links', 10, 2);
2178
2179function akpc_options() {
2180 if (function_exists('add_options_page')) {
2181 add_options_page(
2182 __('Popularity Contest Options', 'popularity-contest')
2183 , __('Popularity', 'popularity-contest')
2184 , 10
2185 , basename(__FILE__)
2186 , 'akpc_options_form'
2187 );
2188 }
2189 if (function_exists('add_submenu_page')) {
2190 add_submenu_page(
2191 'index.php'
2192 , __('Most Popular Posts', 'popularity-contest')
2193 , __('Most Popular Posts', 'popularity-contest')
2194 , 0
2195 , basename(__FILE__)
2196 , 'akpc_view_stats'
2197 );
2198 }
2199}
2200function akpc_options_css() {
2201 print('<link rel="stylesheet" type="text/css" href="'.site_url('?ak_action=akpc_css').'" />');
2202}
2203function akpc_widget_js() {
2204 echo '<script type="text/javascript" src="'.site_url('?ak_action=akpc_js').'"></script>';
2205}
2206
2207// -- TEMPLATE FUNCTIONS
2208
2209function akpc_the_popularity($post_id = null) {
2210 global $akpc;
2211 if (!$post_id) {
2212 global $post;
2213 $post_id = $post->ID;
2214 }
2215 $akpc->show_post_rank($post_id);
2216}
2217
2218function akpc_most_popular($limit = 10, $before = '<li>', $after = '</li>', $report = false, $echo = true) {
2219 global $akpc;
2220 if(!$report) {
2221 $akpc->show_top_ranked($limit, $before, $after);
2222 }
2223 else {
2224 return $akpc->show_report($report, $limit);
2225 }
2226}
2227
2228/**
2229 * Show a single report
2230 * @var string $type - type of report to show
2231 * @var int $limit - number of results to display
2232 * @return mixed echo/array
2233 */
2234function akpc_show_report($type = 'popular', $limit = 10, $exclude_pages = 'no', $custom = array(), $before_title = '<h3>', $after_title = '</h3>', $hide_title = false) {
2235 global $akpc;
2236 return $akpc->show_report($type, $limit, $exclude_pages, $custom, $before_title, $after_title, $hide_title);
2237}
2238
2239/**
2240 * Get raw post data for a report type
2241 * @var string $type - type of report to show
2242 * @var int $limit - number of posts to display
2243 * @var array $custom - any custom report attributes needed
2244 * @return bool/array - returns false if no posts in report
2245 */
2246function akpc_get_popular_posts_array($type, $limit, $custom = array()) {
2247 global $akpc;
2248 return $akpc->get_popular_posts($type, $limit, $custom);
2249}
2250
2251function akpc_most_popular_in_cat($limit = 10, $before = '<li>', $after = '</li>', $cat_ID = '') {
2252 global $akpc;
2253 $akpc->show_top_ranked_in_cat($limit, $before, $after, $cat_ID);
2254}
2255
2256function akpc_most_popular_in_month($limit = 10, $before = '<li>', $after = '</li>', $m = '') {
2257 global $akpc;
2258 $akpc->show_top_ranked_in_month($limit, $before, $after, $m);
2259}
2260
2261function akpc_most_popular_in_last_days($limit = 10, $before = '<li>', $after = '</li>', $days = 45) {
2262 global $akpc;
2263 $akpc->show_top_ranked_in_last_days($limit, $before, $after, $days);
2264}
2265
2266function akpc_content_pop($str) {
2267 global $akpc, $post;
2268 if (is_admin()) {
2269 return $str;
2270 }
2271 else if (is_feed()) {
2272 $str .= '<img src="'.site_url('?ak_action=api_record_view&id='.$post->ID.'&type=feed').'" alt="" />';
2273 }
2274 else {
2275 if (AKPC_USE_API) {
2276 $str .= '<script type="text/javascript">AKPC_IDS += "'.$post->ID.',";</script>';
2277 }
2278 $show = apply_filters('akpc_display_popularity', $akpc->show_pop, $post);
2279 if (!get_post_meta($post->ID, 'hide_popularity', true) && $show) {
2280 $str .= '<p class="akpc_pop">'.$akpc->get_post_rank($post->ID).'</p>';
2281 }
2282 }
2283 return $str;
2284}
2285
2286function akpc_excerpt_compat_pre($output) {
2287 remove_filter('the_content', 'akpc_content_pop');
2288 return $output;
2289}
2290add_filter('get_the_excerpt', 'akpc_excerpt_compat_pre', 1);
2291
2292function akpc_excerpt_compat_post($output) {
2293 add_filter('the_content', 'akpc_content_pop');
2294 return $output;
2295}
2296add_filter('get_the_excerpt', 'akpc_excerpt_compat_post', 999);
2297
2298// -- WIDGET
2299
2300/**
2301 * do widget init functionality
2302 */
2303function akpc_widget_init() {
2304 if(!function_exists('register_sidebar_widget') || !function_exists('register_widget_control')) { return; }
2305
2306 // get existing widget options
2307 $options = maybe_unserialize(get_option('akpc_widget_options'));
2308
2309 // if no existing widgets, fake one
2310 if(!is_array($options)) { $options[-1] = array('title'=>'','type'=>'','limit'=>''); }
2311
2312 // base set of options for widget type
2313 $base_options = array('classname' => 'akpc-widget', 'description' => __('Show popular posts as ranked by Popularity Contest', 'popularity-contest'));
2314 $widget_name = __('Popularity Contest', 'popularity-contest');
2315
2316 // register widgets & controls for each existing widget
2317 foreach($options as $number => $option) {
2318 $widget_id = 'akpc-widget-'.($number === -1 ? 1 : $number); // not needed, but avoids duplicate dashes for new widgets
2319 wp_register_sidebar_widget($widget_id, $widget_name,'akpc_widget', $base_options, array('number' => $number));
2320 wp_register_widget_control($widget_id, $widget_name,'akpc_widget_control', array('id_base' => 'akpc-widget'), array('number' => $number));
2321 }
2322}
2323
2324/**
2325 * Widget display
2326 */
2327function akpc_widget($args, $widget_args = 1) {
2328 // find out which widget we're working on
2329 if(is_numeric($widget_args)) {
2330 $widget_args = array('number' => $widget_args);
2331 }
2332 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
2333 extract($widget_args, EXTR_SKIP);
2334
2335 // get passed args
2336 extract($args);
2337
2338 // get saved options
2339 $options = maybe_unserialize(get_option('akpc_widget_options'));
2340 extract($options[$number]);
2341 $type = (!isset($type) || empty($type)) ? 'popular' : $type;
2342 $days = (!isset($days) || empty($days)) ? '' : intval($days);
2343 $limit = (!isset($limit) || empty($limit)) ? 10 : intval($limit);
2344 $title_string = (!isset($title) || empty($title)) ? '' : $before_title.htmlspecialchars(stripslashes($title)).$after_title;
2345 $exclude_pages = (!isset($exclude_pages) || empty($exclude_pages)) ? 'no' : $exclude_pages;
2346 $custom = array();
2347
2348 // Check to see if we have the custom type of "last_n" and pass the day amount in the custom array
2349 if ($type == 'last_n') {
2350 $custom['days'] = $days;
2351 }
2352
2353 // output
2354 echo $before_widget.$title_string;
2355 akpc_show_report($type, $limit, $exclude_pages, $custom, '<h4>', '<h4>', true);
2356 echo $after_widget;
2357}
2358
2359/**
2360 * Controls for creating and saving multiple PC widgets
2361 */
2362function akpc_widget_control($widget_args = 1) {
2363 global $wp_registered_widgets;
2364 static $updated = false; // set this after updating so update only happens once
2365
2366 // get individual widget ID
2367 if(is_numeric($widget_args)) {
2368 $widget_args = array('number' => $widget_args);
2369 }
2370 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
2371 extract( $widget_args, EXTR_SKIP );
2372
2373 // get existing widget options
2374 $options = maybe_unserialize(get_option('akpc_widget_options'));
2375
2376 /* UPDATE OPTIONS ON PRESENCE OF POST DATA */
2377 if(isset($_POST['akpc']) && isset($_POST['sidebar'])) {
2378 // get current sidebar data
2379 $sidebar = strval($_POST['sidebar']);
2380 $sidebar_widgets = wp_get_sidebars_widgets();
2381 $this_sidebar = isset($sidebar_widgets[$sidebar]) ? $sidebar_widgets[$sidebar] : array();
2382
2383 // check to see if this sidebar item needs to be deleted
2384 // code pulled directly from the Plain Text widget native to wordpress
2385 foreach ($this_sidebar as $_widget_id) {
2386 if ('akpc_widget' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number'])) {
2387 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
2388 // if we had a previously registered widget ID but nothing in the post args, the widget was removed, so kill it
2389 if(!in_array("akpc-widget-$widget_number", $_POST['widget-id'])) {
2390 unset($options[$widget_number]);
2391 }
2392 }
2393 }
2394
2395 // save this widget's options
2396 foreach((array) $_POST['akpc'] as $widget_number => $widget_options) {
2397 $options[$widget_number]['title'] = $widget_options['title'];
2398 $options[$widget_number]['type'] = $widget_options['type'];
2399 $options[$widget_number]['days'] = intval($widget_options['days']);
2400 $options[$widget_number]['limit'] = intval($widget_options['limit']);
2401 $options[$widget_number]['exclude_pages'] = $widget_options['exclude_pages'];
2402 }
2403 update_option('akpc_widget_options',serialize($options));
2404 $updated = true;
2405 }
2406
2407 // new widget prep
2408 if($number == -1) {
2409 $options[$number] = array('title'=>'','type'=>'','days'=>'','limit'=>'');
2410 $number = '%i%'; // required for new widgets so WP auto-generates a new ID
2411 }
2412
2413 /* START CONTROLS OUTPUT */
2414 // Widget Title
2415 echo '<p>
2416 <label for="akpc['.$number.'][title]">'.__('Widget Title', 'popularity-contest').'</label>
2417 <input type="text" id="akpc['.$number.'][title]" name="akpc['.$number.'][title]" value="'.htmlspecialchars(stripslashes($options[$number]['title'])).'" />
2418 </p>'.PHP_EOL;
2419
2420 // report type select list
2421 $report_types = array('popular' => __('Most Popular', 'popularity-contest'),
2422 'pop_by_category' => __('By Category', 'popularity-contest'),
2423 'category_popularity' => __('Average by Category', 'popularity-contest'),
2424 'last_30' => __('Last 30 Days', 'popularity-contest'),
2425 'last_60' => __('Last 60 Days', 'popularity-contest'),
2426 'last_90' => __('Last 90 Days', 'popularity-contest'),
2427 'last_n' => __('Last (n) Days', 'popularity-contest'),
2428 '365_plus' => __('Older than 1 Year', 'popularity-contest'),
2429 'year' => __('Average by Year', 'popularity-contest'),
2430 'views_wo_feedback' => __('Views w/o Feedback', 'popularity-contest'),
2431 'most_feedback' => __('Most Feedback', 'popularity-contest'),
2432 'most_comments' => __('Most Commented', 'popularity-contest'),
2433 'most_feed_views' => __('Feed Views', 'popularity-contest'),
2434 'most_home_views' => __('Home Page Views', 'popularity-contest'),
2435 'most_archive_views' => __('Archive Views', 'popularity-contest'),
2436 'most_single_views' => __('Permalink Views', 'popularity-contest'),
2437 'most_pingbacks' => __('Pingbacks', 'popularity-contest'),
2438 'most_trackbacks' => __('Trackbacks', 'popularity-contest')
2439 );
2440 echo '<p>
2441 <label for="akpc['.$number.'][type]">'.__('Report Type', 'popularity-contest').'</label>
2442 <select id="akpc['.$number.'][type]" name="akpc['.$number.'][type]" class="akpc_pop_widget_type">'.PHP_EOL;
2443 foreach($report_types as $key => $value) {
2444 echo '<option value="'.$key.'"'.($key == $options[$number]['type'] ? ' selected="selected"' : '').'>'.$value.'</option>'.PHP_EOL;
2445 }
2446 echo '</select>
2447 </p>'.PHP_EOL;
2448 // Number of days to get data from
2449 $hide_days = '';
2450 if ($options[$number]['type'] != 'last_n' || (!is_int($options[$number]['days']) && $options[$number]['days'] != 0)) {
2451 $hide_days = ' style="display:none;"';
2452 }
2453 echo '<p class="akpc_pop_widget_days"'.$hide_days.'>
2454 <label for="akpc['.$number.'][days]">'.__('Number of days', 'popularity-contest').': </label>
2455 <input type="text" id="akpc['.$number.'][days]" name="akpc['.$number.'][days]" size="3" value="'.$options[$number]['days'].'" />
2456 </p>'.PHP_EOL;
2457
2458 // number of posts to display
2459 echo '<p>
2460 <label for="akpc['.$number.'][limit]">'.__('Number of posts to display', 'popularity-contest').': </label>
2461 <input type="text" id="akpc['.$number.'][limit]" name="akpc['.$number.'][limit]" size="3" value="'.$options[$number]['limit'].'" />
2462 </p>'.PHP_EOL;
2463 // exclude pages
2464 echo '<p>
2465 <label for="akpc['.$number.'][limit]">'.__('Exclude pages', 'popularity-contest').': </label>
2466 <select id="akpc['.$number.'][exclude_pages]" name="akpc['.$number.'][exclude_pages]">
2467 <option value="yes"'.('yes' == $options[$number]['exclude_pages'] ? ' selected="selected"' : '').'>Yes</option>
2468 <option value="no"'.('no' == $options[$number]['exclude_pages'] ? ' selected="selected"' : '').'>No</option>
2469 </select>
2470 </p>'.PHP_EOL;
2471 // submit hidden field, really necessary? may be needed for legacy compatability
2472 echo '<input type="hidden" id="akpc['.$number.'][submit]" name="akpc['.$number.'][submit]" value="1" />';
2473}
2474
2475function akpc_show_error($type, $info = null) {
2476 switch ($type) {
2477 case 'tag_report_not_found':
2478 echo '<div class="akpc_report"><div class="akpc_padded">'.sprintf(__('Sorry, could not find the requested tag: %s', 'popularity-contest'), htmlspecialchars($info)).'</div></div>';
2479 break;
2480 case 'tag_report_already_added':
2481 echo '<div class="akpc_report"><div class="akpc_padded">'.sprintf(__('Looks like you already have a report for tag: %s', 'popularity-contest'), htmlspecialchars($info)).'</div></div>';
2482 break;
2483 }
2484}
2485
2486// -- API FUNCTIONS
2487
2488function akpc_api_head_javascript() {
2489 echo '
2490<script type="text/javascript">var AKPC_IDS = "";</script>
2491 ';
2492}
2493
2494function akpc_api_footer_javascript() {
2495 if (function_exists('akpc_is_searcher') && akpc_is_searcher()) {
2496 $type = 'searcher';
2497 }
2498 else if (is_archive() && !is_category()) {
2499 $type = 'archive';
2500 }
2501 else if (is_category()) {
2502 $type = 'category';
2503 }
2504 else if (is_single()) {
2505 $type = 'single';
2506 }
2507 else if (is_tag()) {
2508 $type = 'tag';
2509 }
2510 else if (is_page()) {
2511 $type = 'page';
2512 }
2513 else {
2514 $type = 'home';
2515 }
2516 echo '
2517<script type="text/javascript">
2518jQuery(function() {
2519
2520 jQuery.post("index.php",{ak_action:"api_record_view", ids: AKPC_IDS, type:"'.$type.'"}, false, "json");
2521});
2522</script>
2523 ';
2524}
2525
2526function akpc_is_searcher() {
2527 global $akpc;
2528
2529 $temp = parse_url($_SERVER['HTTP_REFERER']);
2530 $referring_domain = $temp['host'];
2531 $searchers = ereg_replace("\n|\r|\r\n|\n\r", " ", $akpc->searcher_names);
2532 $searchers = explode(" ", $searchers);
2533 foreach ($searchers as $searcher) {
2534 if ($referring_domain == $searcher) { return true; }
2535 }
2536 return false;
2537}
2538
2539function akpc_api_record_view($id = null) {
2540 global $wpdb;
2541 $akpc = new ak_popularity_contest;
2542 $akpc->get_settings();
2543
2544 $ids = array();
2545 if ($id) {
2546 $ids[] = $id;
2547 }
2548 else {
2549 foreach (explode(',', $_POST['ids']) as $id) {
2550 if ($id = intval($id)) {
2551 $ids[] = $id;
2552 }
2553 }
2554 }
2555 array_unique($ids);
2556
2557 if (!empty($_GET['type'])) {
2558 $type = $_GET['type'];
2559 $response = 'img';
2560 }
2561 else {
2562 $type = $_POST['type'];
2563 $response = 'json';
2564 }
2565 if (count($ids) && $akpc->record_view(true, $ids, $type)) {
2566 $json = '{"result":true,"ids":"'.implode(',',$ids).'","type":"'.sanitize_title($type).'"}';
2567 }
2568 else {
2569 $json = '{"result":false,"ids":"'.implode(',',$ids).'","type":"'.sanitize_title($type).'"}';
2570 }
2571 switch ($response) {
2572 case 'img':
2573 header('Content-type: image/jpeg');
2574 break;
2575 case 'json':
2576 header('Content-type: application/json');
2577 echo $json;
2578 break;
2579 }
2580 exit();
2581}
2582
2583// -- HANDLE ACTIONS
2584
2585function akpc_request_handler() {
2586 if (!empty($_POST['ak_action'])) {
2587 switch($_POST['ak_action']) {
2588 case 'update_popularity_values':
2589 if (current_user_can('manage_options')) {
2590 $akpc = new ak_popularity_contest;
2591 $akpc->get_settings();
2592 $akpc->update_settings();
2593 }
2594 break;
2595 case 'api_record_view':
2596 akpc_api_record_view();
2597 break;
2598 case 'akpc_add_tag':
2599 if (!empty($_POST['tag']) && current_user_can('manage_options')) {
2600 $akpc = new ak_popularity_contest;
2601 if (strpos($_POST['tag'], ',')) {
2602 $added_tags = explode(',', $_POST['tag']);
2603 }
2604 else {
2605 $added_tags = array($_POST['tag']);
2606 }
2607 $tag_reports = get_option('akpc_tag_reports');
2608 if ($tag_reports == '') {
2609 add_option('akpc_tag_reports');
2610 }
2611 $tags = maybe_unserialize($tag_reports);
2612 if (!is_array($tags)) {
2613 $tags = array();
2614 }
2615 foreach ($added_tags as $tag) {
2616 $tag = sanitize_title_with_dashes(trim($tag));
2617 if (!empty($tag)) {
2618 if (in_array($tag, $tags)) {
2619 akpc_show_error('tag_report_already_added', $tag);
2620 }
2621 else if ($term = get_term_by('slug', $tag, 'post_tag')) {
2622 $tags[] = $tag;
2623 $akpc->show_report('tag', 10, 'yes', array('term_id' => $term->term_id, 'term_name' => $term->name));
2624 }
2625 else {
2626 akpc_show_error('tag_report_not_found', $tag);
2627 }
2628 }
2629 }
2630 $tags = array_unique($tags);
2631 update_option('akpc_tag_reports', $tags);
2632 }
2633 die();
2634 break;
2635 case 'akpc_remove_tag':
2636 if (!empty($_POST['tag']) && current_user_can('manage_options')) {
2637 $tag = sanitize_title(trim($_POST['tag']));
2638 if (!empty($tag)) {
2639 $tags = maybe_unserialize(get_option('akpc_tag_reports'));
2640 if (is_array($tags) && count($tags)) {
2641 $new_tags = array();
2642 foreach ($tags as $existing_tag) {
2643 if ($existing_tag != $tag) {
2644 $new_tags[] = $existing_tag;
2645 }
2646 }
2647 $tags = array_unique($new_tags);
2648 update_option('akpc_tag_reports', $tags);
2649 }
2650 }
2651 }
2652 die();
2653 break;
2654 }
2655 }
2656 if (!empty($_GET['ak_action'])) {
2657 switch($_GET['ak_action']) {
2658 case 'api_record_view':
2659 if (isset($_GET['id']) && $id = intval($_GET['id'])) {
2660 akpc_api_record_view($id);
2661 }
2662 break;
2663 case 'recount_feedback':
2664 if (current_user_can('manage_options')) {
2665 $akpc = new ak_popularity_contest;
2666 $akpc->get_settings();
2667 $akpc->recount_feedback();
2668 }
2669 break;
2670 case 'akpc_css':
2671 header("Content-type: text/css");
2672?>
2673.ak_wrap {
2674 padding-bottom: 40px;
2675}
2676#akpc_most_popular {
2677 height: 250px;
2678 overflow: auto;
2679 margin-bottom: 10px;
2680}
2681#akpc_most_popular .alternate {
2682 background: #efefef;
2683}
2684#akpc_most_popular td.right, #akpc_options_link {
2685 text-align: right;
2686}
2687#akpc_most_popular td {
2688 padding: 3px;
2689}
2690#akpc_most_popular td a {
2691 border: 0;
2692}
2693.akpc_report {
2694 float: left;
2695 margin: 5px 30px 20px 0;
2696 width: 220px;
2697}
2698.akpc_report h3 {
2699 border-bottom: 1px solid #999;
2700 color #333;
2701 margin: 0 0 4px 0;
2702 padding: 0 0 2px 0;
2703}
2704.akpc_report ol {
2705 margin: 0;
2706 padding: 0 0 0 30px;
2707}
2708.akpc_report ol li span {
2709 float: right;
2710}
2711.akpc_report ol li a {
2712 border: 0;
2713 display: block;
2714 margin: 0 30px 0 0;
2715}
2716.clear {
2717 clear: both;
2718 float: none;
2719}
2720#akpc_template_tags dl {
2721 margin-left: 10px;
2722}
2723#akpc_template_tags dl dt {
2724 font-weight: bold;
2725 margin: 0 0 5px 0;
2726}
2727#akpc_template_tags dl dd {
2728 margin: 0 0 15px 0;
2729 padding: 0 0 0 15px;
2730}
2731#akpc_options th {
2732 font-weight: normal;
2733 text-align: left;
2734}
2735#akpc_options input.number {
2736 width: 40px;
2737}
2738#akpc_report_tag_form {
2739 display: inline;
2740 padding-left: 20px;
2741}
2742#akpc_report_tag_form label, .akpc_saving {
2743 font: normal normal 12px "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
2744}
2745.akpc_saving {
2746 color: #999;
2747 display: none;
2748 padding: 5px;
2749}
2750#akpc_tag_reports h3 {
2751 padding-right: 20px;
2752}
2753#akpc_tag_reports a.remove {
2754 float: right;
2755}
2756#akpc_tag_reports .akpc_padded {
2757 color: #999;
2758 padding: 20px;
2759 text-align: center;
2760}
2761#akpc_tag_reports .none {
2762 background: #eee;
2763 text-align: left;
2764}
2765<?php
2766 die();
2767 break;
2768 case 'akpc_js':
2769 header('Content-type: text/javascript');
2770 ?>
2771 var cf_widget_count = 0;
2772 jQuery(function($) {
2773 akpc_widget_js();
2774 setInterval('akpc_widget_check()', 500);
2775 });
2776 akpc_widget_js = function() {
2777 jQuery('select.akpc_pop_widget_type').unbind().change(function() {
2778 if (jQuery(this).val() == 'last_n') {
2779 jQuery(this).parents('div.widget-content, div.widget-control').find('p.akpc_pop_widget_days:hidden').slideDown();
2780 }
2781 else {
2782 jQuery(this).parents('div.widget-content, div.widget-control').find('p.akpc_pop_widget_days:visible').slideUp();
2783 }
2784 });
2785 }
2786 akpc_widget_check = function() {
2787 var current_count = jQuery('#widgets-right .widget-inside:visible, .widget-control-list .widget-list-control-item').size();
2788 if (current_count != cf_widget_count) {
2789 akpc_widget_js();
2790 cf_widget_count = current_count;
2791 }
2792 }
2793<?php
2794 die();
2795 break;
2796 }
2797 }
2798}
2799
2800// -- GET HOOKED
2801
2802if (is_admin() && $_GET['page'] == 'popularity-contest.php') {
2803 wp_enqueue_script('suggest');
2804}
2805
2806add_filter('the_content', 'akpc_content_pop');
2807
2808add_action('init', 'akpc_init', 1);
2809add_action('init', 'akpc_request_handler', 2);
2810add_action('admin_menu', 'akpc_options');
2811add_action('admin_head', 'akpc_options_css');
2812
2813
2814// Use the global pagenow so we only load the Widget JS on the widget page
2815global $pagenow;
2816if ($pagenow == 'widgets.php') {
2817 add_action('admin_head', 'akpc_widget_js');
2818}
2819
2820if (AKPC_USE_API == 0) {
2821 // work cache unfriendly
2822 add_action('the_content', 'akpc_view');
2823}
2824else {
2825 // do view updates via API
2826 add_action('wp_head','akpc_api_head_javascript');
2827 add_action('wp_footer','akpc_api_footer_javascript');
2828 wp_enqueue_script('jquery');
2829}
2830add_action('comment_post', 'akpc_feedback_comment');
2831add_action('pingback_post', 'akpc_feedback_pingback');
2832add_action('trackback_post', 'akpc_feedback_trackback');
2833
2834add_action('publish_post', 'akpc_publish');
2835add_action('delete_post', 'akpc_post_delete');
2836
2837add_action('publish_page', 'akpc_publish');
2838add_action('delete_page', 'akpc_post_delete');
2839
2840add_action('wp_set_comment_status', 'akpc_comment_status', 10, 2);
2841add_action('delete_comment', 'akpc_comment_delete');
2842
2843add_action('plugins_loaded','akpc_widget_init');
2844
2845endif; // LOADED CHECK
2846
2847?>
Note: See TracBrowser for help on using the repository browser.