Advertisement
Guest User

Untitled

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