1. <?php
  2. /*
  3. Plugin Name: Wordpress Popular Posts
  4. Plugin URI: http://wordpress.org/extend/plugins/wordpress-popular-posts
  5. Description: Showcases your most popular posts to your visitors on your blog's sidebar. Use Wordpress Popular Posts as a widget or place it anywhere on your theme using <strong>&lt;?php wpp_get_mostpopular(); ?&gt;</strong>
  6. Version: 2.1.4
  7. Author: H&eacute;ctor Cabrera
  8. Author URI: http://wordpress.org/extend/plugins/wordpress-popular-posts
  9. License: GPL2
  10. */
  11.  
  12. if (basename($_SERVER['SCRIPT_NAME']) == basename(__FILE__)) exit('Please do not load this page directly');
  13.  
  14. /**
  15. * Load Wordpress Popular Posts to widgets_init.
  16. * @since 2.0
  17. */
  18. add_action('widgets_init', 'load_wpp');
  19.  
  20. function load_wpp() {
  21. register_widget('WordpressPopularPosts');
  22. }
  23.  
  24. /**
  25. * Wordpress Popular Posts class.
  26. */
  27.  
  28. if ( !class_exists('WordpressPopularPosts') ) {
  29. class WordpressPopularPosts extends WP_Widget {
  30. // plugin global variables
  31. var $version = "2.1.4";
  32. var $qTrans = false;
  33. var $postRating = false;
  34. var $thumb = false;
  35. var $pluginDir = "";
  36. var $charset = "UTF-8";
  37. var $magicquotes = false;
  38.  
  39. // constructor
  40. function WordpressPopularPosts() {
  41. global $wp_version;
  42.  
  43. // widget settings
  44. $widget_ops = array( 'classname' => 'popular-posts', 'description' => 'The most Popular Posts on your blog.' );
  45.  
  46. // widget control settings
  47. $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'wpp' );
  48.  
  49. // create the widget
  50. $this->WP_Widget( 'wpp', 'Wordpress Popular Posts', $widget_ops, $control_ops );
  51.  
  52. // set plugin path
  53. if (empty($this->pluginDir)) $this->pluginDir = WP_PLUGIN_URL . '/wordpress-popular-posts';
  54.  
  55. // set charset
  56. $this->charset = get_bloginfo('charset');
  57.  
  58. // detect PHP magic quotes
  59. $this->magicquotes = get_magic_quotes_gpc();
  60.  
  61. // add ajax update to wp_ajax_ hook
  62. add_action('wp_ajax_nopriv_wpp_update', array(&$this, 'wpp_ajax_update'));
  63. add_action('wp_head', array(&$this, 'wpp_print_ajax'));
  64.  
  65. // add ajax table truncation to wp_ajax_ hook
  66. add_action('wp_ajax_wpp_clear_cache', array(&$this, 'wpp_clear_data'));
  67. add_action('wp_ajax_wpp_clear_all', array(&$this, 'wpp_clear_data'));
  68.  
  69. // print stylesheet
  70. add_action('wp_head', array(&$this, 'wpp_print_stylesheet'));
  71.  
  72. // activate textdomain for translations
  73. add_action('init', array(&$this, 'wpp_textdomain'));
  74.  
  75. // activate maintenance page
  76. add_action('admin_menu', array(&$this, 'add_wpp_maintenance_page'));
  77.  
  78. // database creation
  79. register_activation_hook(__FILE__, $this->wpp_install());
  80.  
  81. // cache maintenance schedule
  82. register_deactivation_hook(__FILE__, array(&$this, 'wpp_deactivation'));
  83. add_action('wpp_cache_event', array(&$this, 'wpp_cache_maintenance'));
  84. if (!wp_next_scheduled('wpp_cache_event')) {
  85. $tomorrow = time() + 86400;
  86. $midnight = mktime(0, 0, 0,
  87. date("m", $tomorrow),
  88. date("d", $tomorrow),
  89. date("Y", $tomorrow));
  90. wp_schedule_event( $midnight, 'daily', 'wpp_cache_event' );
  91. }
  92.  
  93. // Wordpress version check
  94. if (version_compare($wp_version, '2.8.0', '<')) add_action('admin_notices', array(&$this, 'wpp_update_warning'));
  95.  
  96. // qTrans plugin support
  97. if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) $this->qTrans = true;
  98.  
  99. // WP-Post Ratings plugin support
  100. if (function_exists('the_ratings_results')) $this->postRating = true;
  101.  
  102. // Can we create thumbnails?
  103. if (extension_loaded('gd') && function_exists('gd_info') && version_compare(phpversion(), '4.3.0', '>=')) $this->thumb = true;
  104.  
  105. // shortcode
  106. if( function_exists('add_shortcode') ){
  107. add_shortcode('wpp', array(&$this, 'wpp_shortcode'));
  108. add_shortcode('WPP', array(&$this, 'wpp_shortcode'));
  109. }
  110.  
  111. // set version
  112. $wpp_ver = get_option('wpp_ver');
  113. if (!$wp_ver) {
  114. add_option('wpp_ver', $this->version);
  115. } else if (version_compare($wpp_ver, $this->version, '<')) {
  116. update_option('wpp_ver', $this->version);
  117. }
  118.  
  119. // add stats page
  120. add_action('admin_menu', array(&$this, 'wpp_stats'));
  121. }
  122.  
  123. // builds Wordpress Popular Posts' widgets
  124. function widget($args, $instance) {
  125. extract($args);
  126. echo "<!-- Wordpress Popular Posts Plugin v". $this->version ." [W] [".$instance['range']."]". (($instance['markup']['custom_html']) ? ' [custom]' : ' [regular]') ." -->"."\n";
  127. echo $before_widget . "\n";
  128.  
  129. // has user set a title?
  130. if ($instance['title'] != '') {
  131. if ($instance['markup']['custom_html'] && $instance['markup']['title-start'] != "" && $instance['markup']['title-end'] != "" ) {
  132. echo htmlspecialchars_decode($instance['markup']['title-start'], ENT_QUOTES) . $instance['title'], ENT_QUOTES . htmlspecialchars_decode($instance['markup']['title-end'], ENT_QUOTES);
  133. } else {
  134. echo $before_title . $instance['title'] . $after_title;
  135. }
  136. }
  137.  
  138. echo $this->get_popular_posts($instance, false);
  139. echo $after_widget . "\n";
  140. echo "<!-- End Wordpress Popular Posts Plugin v". $this->version ." -->"."\n";
  141. }
  142.  
  143. // updates each widget instance when user clicks the "save" button
  144. function update($new_instance, $old_instance) {
  145.  
  146. $instance = $old_instance;
  147.  
  148. //$instance['title'] = htmlspecialchars( stripslashes(strip_tags( $new_instance['title'] )), ENT_QUOTES, 'UTF-8', FALSE );
  149. $instance['title'] = ($this->magicquotes) ? htmlspecialchars( stripslashes(strip_tags( $new_instance['title'] )), ENT_QUOTES ) : htmlspecialchars( strip_tags( $new_instance['title'] ), ENT_QUOTES );
  150. $instance['limit'] = is_numeric($new_instance['limit']) ? $new_instance['limit'] : 10;
  151. $instance['range'] = $new_instance['range'];
  152. $instance['order_by'] = $new_instance['order_by'];
  153. $instance['pages'] = $new_instance['pages'];
  154. $instance['shorten_title']['active'] = $new_instance['shorten_title-active'];
  155. $instance['shorten_title']['length'] = is_numeric($new_instance['shorten_title-length']) ? $new_instance['shorten_title-length'] : 25;
  156. $instance['post-excerpt']['active'] = $new_instance['post-excerpt-active'];
  157. $instance['post-excerpt']['length'] = is_numeric($new_instance['post-excerpt-length']) ? $new_instance['post-excerpt-length'] : 55;
  158. $instance['post-excerpt']['keep_format'] = $new_instance['post-excerpt-format'];
  159. $instance['exclude-cats']['active'] = $new_instance['exclude-cats'];
  160. $instance['exclude-cats']['cats'] = empty($new_instance['excluded']) ? '' : (ctype_digit(str_replace(",", "", $new_instance['excluded']))) ? $new_instance['excluded'] : '';
  161. /* Restrict Posts to only specific categories - kaynenh */
  162. $instance['include-cats']['active'] = $new_instance['include-cats'];
  163. $instance['include-cats']['cats'] = empty($new_instance['included']) ? '' : (ctype_digit(str_replace(",", "", $new_instance['included']))) ? $new_instance['included'] : '';
  164. if ($this->thumb) { // can create thumbnails
  165. $instance['thumbnail']['active'] = $new_instance['thumbnail-active'];
  166. $instance['thumbnail']['thumb_selection'] = empty($new_instance['thumb_selection']) ? "wppgenerated" : $new_instance['thumb_selection'];
  167. $instance['thumbnail']['width'] = is_numeric($new_instance['thumbnail-width']) ? $new_instance['thumbnail-width'] : 15;
  168. $instance['thumbnail']['height'] = is_numeric($new_instance['thumbnail-height']) ? $new_instance['thumbnail-height'] : 15;
  169. } else { // cannot create thumbnails
  170. $instance['thumbnail']['active'] = false;
  171. $instance['thumbnail']['thumb_selection'] = "wppgenerated";
  172. $instance['thumbnail']['width'] = 15;
  173. $instance['thumbnail']['height'] = 15;
  174. }
  175.  
  176. $instance['rating'] = $new_instance['rating'];
  177. $instance['stats_tag']['comment_count'] = $new_instance['comment_count'];
  178. $instance['stats_tag']['views'] = $new_instance['views'];
  179. $instance['stats_tag']['author'] = $new_instance['author'];
  180. $instance['stats_tag']['date']['active'] = $new_instance['date'];
  181. $instance['stats_tag']['date']['format'] = empty($new_instance['date_format']) ? 'F j, Y' : $new_instance['date_format'];
  182. $instance['markup']['custom_html'] = $new_instance['custom_html'];
  183. $instance['markup']['wpp-start'] = empty($new_instance['wpp-start']) ? '&lt;ul&gt;' : htmlspecialchars( $new_instance['wpp-start'], ENT_QUOTES );
  184. $instance['markup']['wpp-end'] = empty($new_instance['wpp-end']) ? '&lt;/ul&gt;' : htmlspecialchars( $new_instance['wpp-end'], ENT_QUOTES );
  185. $instance['markup']['post-start'] = empty ($new_instance['post-start']) ? '&lt;li&gt;' : htmlspecialchars( $new_instance['post-start'], ENT_QUOTES );
  186. $instance['markup']['post-end'] = empty ($new_instance['post-end']) ? '&lt;/li&gt;' : htmlspecialchars( $new_instance['post-end'], ENT_QUOTES );
  187. $instance['markup']['title-start'] = empty($new_instance['title-start']) ? '' : htmlspecialchars( $new_instance['title-start'], ENT_QUOTES );
  188. $instance['markup']['title-end'] = empty($new_instance['title-end']) ? '' : htmlspecialchars( $new_instance['title-end'], ENT_QUOTES );
  189. $instance['markup']['pattern']['active'] = $new_instance['pattern_active'];
  190. $instance['markup']['pattern']['form'] = empty($new_instance['pattern_form']) ? '{image} {title}: {summary} {stats}' : strip_tags( $new_instance['pattern_form'] );
  191.  
  192. return $instance;
  193. }
  194.  
  195. // widget's form
  196. function form($instance) {
  197. // set default values
  198. $defaults = array(
  199. 'title' => __('Popular Posts', 'wordpress-popular-posts'),
  200. 'limit' => 10,
  201. 'range' => 'daily',
  202. 'order_by' => 'comments',
  203. 'pages' => true,
  204. 'shorten_title' => array(
  205. 'active' => false,
  206. 'length' => 25,
  207. 'keep_format' => false
  208. ),
  209. 'post-excerpt' => array(
  210. 'active' => false,
  211. 'length' => 55
  212. ),
  213. 'exclude-cats' => array(
  214. 'active' => false,
  215. 'cats' => ''
  216. ),
  217. 'thumbnail' => array(
  218. 'active' => false,
  219. 'width' => 15,
  220. 'height' => 15
  221. ),
  222. 'rating' => false,
  223. 'stats_tag' => array(
  224. 'comment_count' => true,
  225. 'views' => false,
  226. 'author' => false,
  227. 'date' => array(
  228. 'active' => false,
  229. 'format' => 'F j, Y'
  230. )
  231. ),
  232. 'markup' => array(
  233. 'custom_html' => false,
  234. 'wpp-start' => '&lt;ul&gt;',
  235. 'wpp-end' => '&lt;/ul&gt;',
  236. 'post-start' => '&lt;li&gt;',
  237. 'post-end' => '&lt;/li&gt;',
  238. 'title-start' => '&lt;h2&gt;',
  239. 'title-end' => '&lt;/h2&gt;',
  240. 'pattern' => array(
  241. 'active' => false,
  242. 'form' => '{image} {title}: {summary} {stats}'
  243. )
  244. )
  245. );
  246.  
  247. // update instance's default options
  248. $instance = wp_parse_args( (array) $instance, $defaults );
  249.  
  250. // form
  251. ?>
  252. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'wordpress-popular-posts'); ?></label>
  253. <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" /></p>
  254. <p><label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e('Show up to:', 'wordpress-popular-posts'); ?></label><br />
  255. <input id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" value="<?php echo $instance['limit']; ?>" class="widefat" style="width:50px!important" /> <?php _e('posts', 'wordpress-popular-posts'); ?></p>
  256. <p><label for="<?php echo $this->get_field_id( 'range' ); ?>"><?php _e('Time Range:', 'wordpress-popular-posts'); ?></label>
  257. <select id="<?php echo $this->get_field_id( 'range' ); ?>" name="<?php echo $this->get_field_name( 'range' ); ?>" class="widefat">
  258. <option value="daily" <?php if ( 'daily' == $instance['range'] ) echo 'selected="selected"'; ?>><?php _e('Today', 'wordpress-popular-posts'); ?></option>
  259. <option value="weekly" <?php if ( 'weekly' == $instance['range'] ) echo 'selected="selected"'; ?>><?php _e('Last 7 days', 'wordpress-popular-posts'); ?></option>
  260. <option value="monthly" <?php if ( 'monthly' == $instance['range'] ) echo 'selected="selected"'; ?>><?php _e('Last 30 days', 'wordpress-popular-posts'); ?></option>
  261. <option value="all" <?php if ( 'all' == $instance['range'] ) echo 'selected="selected"'; ?>><?php _e('All-time', 'wordpress-popular-posts'); ?></option>
  262. </select>
  263. </p>
  264. <p><label for="<?php echo $this->get_field_id( 'order_by' ); ?>"><?php _e('Sort posts by:', 'wordpress-popular-posts'); ?></label>
  265. <select id="<?php echo $this->get_field_id( 'order_by' ); ?>" name="<?php echo $this->get_field_name( 'order_by' ); ?>" class="widefat">
  266. <option value="comments" <?php if ( 'comments' == $instance['order_by'] ) echo 'selected="selected"'; ?>><?php _e('Comments', 'wordpress-popular-posts'); ?></option>
  267. <option value="views" <?php if ( 'views' == $instance['order_by'] ) echo 'selected="selected"'; ?>><?php _e('Total views', 'wordpress-popular-posts'); ?></option>
  268. <option value="avg" <?php if ( 'avg' == $instance['order_by'] ) echo 'selected="selected"'; ?>><?php _e('Avg. daily views', 'wordpress-popular-posts'); ?></option>
  269. </select>
  270. </p>
  271. <input type="checkbox" class="checkbox" <?php echo ($instance['pages']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'pages' ); ?>" name="<?php echo $this->get_field_name( 'pages' ); ?>" /> <label for="<?php echo $this->get_field_id( 'pages' ); ?>"><?php _e('Include pages', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  272. <?php if ($this->postRating) : ?>
  273. <input type="checkbox" class="checkbox" <?php echo ($instance['rating']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'rating' ); ?>" name="<?php echo $this->get_field_name( 'rating' ); ?>" /> <label for="<?php echo $this->get_field_id( 'rating' ); ?>"><?php _e('Display post rating', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  274. <?php endif; ?>
  275. <input type="checkbox" class="checkbox" <?php echo ($instance['shorten_title']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'shorten_title-active' ); ?>" name="<?php echo $this->get_field_name( 'shorten_title-active' ); ?>" /> <label for="<?php echo $this->get_field_id( 'shorten_title-active' ); ?>"><?php _e('Shorten title output', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  276. <?php if ($instance['shorten_title']['active']) : ?>
  277. <label for="<?php echo $this->get_field_id( 'shorten_title-length' ); ?>"><?php _e('Shorten title to', 'wordpress-popular-posts'); ?> <input id="<?php echo $this->get_field_id( 'shorten_title-length' ); ?>" name="<?php echo $this->get_field_name( 'shorten_title-length' ); ?>" value="<?php echo $instance['shorten_title']['length']; ?>" class="widefat" style="width:50px!important" /> <?php _e('characters', 'wordpress-popular-posts'); ?></label><br /><br />
  278. <?php endif; ?>
  279. <input type="checkbox" class="checkbox" <?php echo ($instance['post-excerpt']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'post-excerpt-active' ); ?>" name="<?php echo $this->get_field_name( 'post-excerpt-active' ); ?>" /> <label for="<?php echo $this->get_field_id( 'post-excerpt-active' ); ?>"><?php _e('Display post excerpt', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  280. <?php if ($instance['post-excerpt']['active']) : ?>
  281. <fieldset class="widefat">
  282. <legend><?php _e('Excerpt Properties', 'wordpress-popular-posts'); ?></legend>
  283. &nbsp;&nbsp;<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'post-excerpt-format' ); ?>" name="<?php echo $this->get_field_name( 'post-excerpt-format' ); ?>" <?php echo ($instance['post-excerpt']['keep_format']) ? 'checked="checked"' : ''; ?> /> <label for="<?php echo $this->get_field_id( 'post-excerpt-format' ); ?>"><?php _e('Keep text format and links', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  284. &nbsp;&nbsp;<label for="<?php echo $this->get_field_id( 'post-excerpt-length' ); ?>"><?php _e('Excerpt length:', 'wordpress-popular-posts'); ?> <input id="<?php echo $this->get_field_id( 'post-excerpt-length' ); ?>" name="<?php echo $this->get_field_name( 'post-excerpt-length' ); ?>" value="<?php echo $instance['post-excerpt']['length']; ?>" class="widefat" style="width:30px!important" /> <?php _e('characters', 'wordpress-popular-posts'); ?></label>
  285. </fieldset>
  286. <br />
  287. <?php endif; ?>
  288. <input type="checkbox" class="checkbox" <?php echo ($instance['exclude-cats']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'exclude-cats' ); ?>" name="<?php echo $this->get_field_name( 'exclude-cats' ); ?>" /> <label for="<?php echo $this->get_field_id( 'exclude-cats' ); ?>"><?php _e('Exclude categories', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  289. <?php if ($instance['exclude-cats']['active']) : ?>
  290. <fieldset class="widefat">
  291. <legend><?php _e('Categories to exclude', 'wordpress-popular-posts'); ?></legend>
  292. &nbsp;&nbsp;<label for="<?php echo $this->get_field_id( 'excluded' ); ?>"><?php _e('ID(s) (comma separated, no spaces):', 'wordpress-popular-posts'); ?></label><br />&nbsp;&nbsp;<input id="<?php echo $this->get_field_id( 'excluded' ); ?>" name="<?php echo $this->get_field_name( 'excluded' ); ?>" value="<?php echo $instance['exclude-cats']['cats']; ?>" class="widefat" style="width:150px" /><br /><br />
  293. </fieldset>
  294. <?php endif; ?>
  295.  
  296. <?php /*Restrict popular posts to specific categories - kaynenh*/ ?>
  297. <input type="checkbox" class="checkbox" <?php echo ($instance['include-cats']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'include-cats' ); ?>" name="<?php echo $this->get_field_name( 'include-cats' ); ?>" /> <label for="<?php echo $this->get_field_id( 'include-cats' ); ?>"><?php _e('Restrict posts to categories', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  298. <?php if ($instance['include-cats']['active']) : ?>
  299. <fieldset class="widefat">
  300. <legend><?php _e('Restrict to Categories', 'wordpress-popular-posts'); ?></legend>
  301. &nbsp;&nbsp;<label for="<?php echo $this->get_field_id( 'included' ); ?>"><?php _e('ID(s) (comma separated, no spaces):', 'wordpress-popular-posts'); ?></label><br />&nbsp;&nbsp;<input id="<?php echo $this->get_field_id( 'included' ); ?>" name="<?php echo $this->get_field_name( 'included' ); ?>" value="<?php echo $instance['include-cats']['cats']; ?>" class="widefat" style="width:150px" /><br /><br />
  302. </fieldset>
  303. <?php endif; ?>
  304. <br />
  305.  
  306. <fieldset style="width:214px; padding:5px;" class="widefat">
  307. <legend><?php _e('Thumbnail settings', 'wordpress-popular-posts'); ?></legend>
  308. <input type="checkbox" class="checkbox" <?php echo ($instance['thumbnail']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'thumbnail-active' ); ?>" name="<?php echo $this->get_field_name( 'thumbnail-active' ); ?>" /> <label for="<?php echo $this->get_field_id( 'thumbnail-active' ); ?>"><?php _e('Display post thumbnail', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  309. <?php if($instance['thumbnail']['active']) : ?>
  310.  
  311. <input type="radio" name="<?php echo $this->get_field_name( 'thumb_selection' ); ?>" value="wppgenerated" <?php if ( 'wppgenerated' == $instance['thumbnail']['thumb_selection'] ) echo 'checked="checked"'; ?>> <label for="<?php echo $this->get_field_id( 'thumb_selection' ); ?>"><?php _e('Generate all thumbnails for me', 'wordpress-popular-posts'); ?></label><br />
  312. <input type="radio" name="<?php echo $this->get_field_name( 'thumb_selection' ); ?>" value="usergenerated" <?php if ( 'usergenerated' == $instance['thumbnail']['thumb_selection']) { echo 'checked="checked"'; } if (!function_exists('get_the_post_thumbnail')) { echo 'disabled="disabled"'; } ?>> <label for="<?php echo $this->get_field_id( 'thumb_selection' ); ?>"><?php _e('Use thumbnails selected by me', 'wordpress-popular-posts'); ?></label>
  313.  
  314. <label for="<?php echo $this->get_field_id( 'thumbnail-width' ); ?>"><?php _e('Width:', 'wordpress-popular-posts'); ?></label>
  315. <input id="<?php echo $this->get_field_id( 'thumbnail-width' ); ?>" name="<?php echo $this->get_field_name( 'thumbnail-width' ); ?>" value="<?php echo $instance['thumbnail']['width']; ?>" class="widefat" style="width:30px!important" <?php echo ($this->thumb) ? '' : 'disabled="disabled"' ?> /> <?php _e('px', 'wordpress-popular-posts'); ?> <br />
  316. <label for="<?php echo $this->get_field_id( 'thumbnail-height' ); ?>"><?php _e('Height:', 'wordpress-popular-posts'); ?></label>
  317. <input id="<?php echo $this->get_field_id( 'thumbnail-height' ); ?>" name="<?php echo $this->get_field_name( 'thumbnail-height' ); ?>" value="<?php echo $instance['thumbnail']['height']; ?>" class="widefat" style="width:30px!important" <?php echo ($this->thumb) ? '' : 'disabled="disabled"' ?> /> <?php _e('px', 'wordpress-popular-posts'); ?><br />
  318.  
  319. <?php endif; ?>
  320. </fieldset>
  321.  
  322. <br />
  323. <fieldset style="width:214px; padding:5px;" class="widefat">
  324. <legend><?php _e('Stats Tag settings', 'wordpress-popular-posts'); ?></legend>
  325. <input type="checkbox" class="checkbox" <?php echo ($instance['stats_tag']['comment_count']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'comment_count' ); ?>" name="<?php echo $this->get_field_name( 'comment_count' ); ?>" /> <label for="<?php echo $this->get_field_id( 'comment_count' ); ?>"><?php _e('Display comment count', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  326. <input type="checkbox" class="checkbox" <?php echo ($instance['stats_tag']['views']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'views' ); ?>" name="<?php echo $this->get_field_name( 'views' ); ?>" /> <label for="<?php echo $this->get_field_id( 'views' ); ?>"><?php _e('Display views', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  327. <input type="checkbox" class="checkbox" <?php echo ($instance['stats_tag']['author']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'author' ); ?>" name="<?php echo $this->get_field_name( 'author' ); ?>" /> <label for="<?php echo $this->get_field_id( 'author' ); ?>"><?php _e('Display author', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  328. <input type="checkbox" class="checkbox" <?php echo ($instance['stats_tag']['date']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'date' ); ?>" name="<?php echo $this->get_field_name( 'date' ); ?>" /> <label for="<?php echo $this->get_field_id( 'date' ); ?>"><?php _e('Display date', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small>
  329. <?php if ($instance['stats_tag']['date']['active']) : ?>
  330. <fieldset class="widefat">
  331. <legend><?php _e('Date Format', 'wordpress-popular-posts'); ?></legend>
  332. <label title='F j, Y'><input type='radio' name='<?php echo $this->get_field_name( 'date_format' ); ?>' value='F j, Y' <?php echo ($instance['stats_tag']['date']['format'] == 'F j, Y') ? 'checked="checked"' : ''; ?> /><?php echo date('F j, Y', time()); ?></label><br />
  333. <label title='Y/m/d'><input type='radio' name='<?php echo $this->get_field_name( 'date_format' ); ?>' value='Y/m/d' <?php echo ($instance['stats_tag']['date']['format'] == 'Y/m/d') ? 'checked="checked"' : ''; ?> /><?php echo date('Y/m/d', time()); ?></label><br />
  334. <label title='m/d/Y'><input type='radio' name='<?php echo $this->get_field_name( 'date_format' ); ?>' value='m/d/Y' <?php echo ($instance['stats_tag']['date']['format'] == 'm/d/Y') ? 'checked="checked"' : ''; ?> /><?php echo date('m/d/Y', time()); ?></label><br />
  335. <label title='d/m/Y'><input type='radio' name='<?php echo $this->get_field_name( 'date_format' ); ?>' value='d/m/Y' <?php echo ($instance['stats_tag']['date']['format'] == 'd/m/Y') ? 'checked="checked"' : ''; ?> /><?php echo date('d/m/Y', time()); ?></label><br />
  336. </fieldset>
  337. <?php endif; ?>
  338. </fieldset>
  339. <br />
  340.  
  341. <fieldset style="width:214px; padding:5px;" class="widefat">
  342. <legend><?php _e('HTML Markup settings', 'wordpress-popular-posts'); ?></legend>
  343. <input type="checkbox" class="checkbox" <?php echo ($instance['markup']['custom_html']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'custom_html' ); ?>" name="<?php echo $this->get_field_name( 'custom_html' ); ?>" /> <label for="<?php echo $this->get_field_id( 'custom_html' ); ?>"><?php _e('Use custom HTML Markup', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  344. <?php if ($instance['markup']['custom_html']) : ?>
  345. <br />
  346. <p style="font-size:11px"><label for="<?php echo $this->get_field_id( 'title-start' ); ?>"><?php _e('Before / after title:', 'wordpress-popular-posts'); ?></label> <br />
  347. <input type="text" id="<?php echo $this->get_field_id( 'title-start' ); ?>" name="<?php echo $this->get_field_name( 'title-start' ); ?>" value="<?php echo $instance['markup']['title-start']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /> <input type="text" id="<?php echo $this->get_field_id( 'title-end' ); ?>" name="<?php echo $this->get_field_name( 'title-end' ); ?>" value="<?php echo $instance['markup']['title-end']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /></p>
  348. <p style="font-size:11px"><label for="<?php echo $this->get_field_id( 'wpp_start' ); ?>"><?php _e('Before / after Popular Posts:', 'wordpress-popular-posts'); ?></label> <br />
  349. <input type="text" id="<?php echo $this->get_field_id( 'wpp-start' ); ?>" name="<?php echo $this->get_field_name( 'wpp-start' ); ?>" value="<?php echo $instance['markup']['wpp-start']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /> <input type="text" id="<?php echo $this->get_field_id( 'wpp-end' ); ?>" name="<?php echo $this->get_field_name( 'wpp-end' ); ?>" value="<?php echo $instance['markup']['wpp-end']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /></p>
  350. <p style="font-size:11px"><label for="<?php echo $this->get_field_id( 'post-start' ); ?>"><?php _e('Before / after each post:', 'wordpress-popular-posts'); ?></label> <br />
  351. <input type="text" id="<?php echo $this->get_field_id( 'post-start' ); ?>" name="<?php echo $this->get_field_name( 'post-start' ); ?>" value="<?php echo $instance['markup']['post-start']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /> <input type="text" id="<?php echo $this->get_field_id( 'post-end' ); ?>" name="<?php echo $this->get_field_name( 'post-end' ); ?>" value="<?php echo $instance['markup']['post-end']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /></p>
  352. <hr />
  353. <?php endif; ?>
  354. <input type="checkbox" class="checkbox" <?php echo ($instance['markup']['pattern']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'pattern_active' ); ?>" name="<?php echo $this->get_field_name( 'pattern_active' ); ?>" /> <label for="<?php echo $this->get_field_id( 'pattern_active' ); ?>"><?php _e('Use content formatting tags', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
  355. <?php if ($instance['markup']['pattern']['active']) : ?>
  356. <br />
  357. <p style="font-size:11px"><label for="<?php echo $this->get_field_id( 'pattern_form' ); ?>"><?php _e('Content format:', 'wordpress-popular-posts'); ?></label>
  358. <input type="text" id="<?php echo $this->get_field_id( 'pattern_form' ); ?>" name="<?php echo $this->get_field_name( 'pattern_form' ); ?>" value="<?php echo $instance['markup']['pattern']['form']; ?>" style="width:204px" <?php echo ($instance['markup']['pattern']['active']) ? '' : 'disabled="disabled"' ?> /></p>
  359. <?php endif; ?>
  360. </fieldset>
  361. <?php // end form
  362. }
  363.  
  364. // updates popular posts data table
  365. function wpp_ajax_update() {
  366. $nonce = $_POST['token'];
  367.  
  368. // is this a valid request?
  369. if (! wp_verify_nonce($nonce, 'wpp-token') ) die("Oops!");
  370.  
  371. if (is_numeric($_POST['id']) && (intval($_POST['id']) == floatval($_POST['id'])) && ($_POST['id'] != '')) {
  372. $id = $_POST['id'];
  373. } else {
  374. die("Invalid ID");
  375. }
  376.  
  377. // if we got an ID, let's update the data table
  378.  
  379. global $wpdb;
  380.  
  381. $wpdb->show_errors();
  382.  
  383. $table = $wpdb->prefix . 'popularpostsdata';
  384.  
  385. // update popularpostsdata table
  386. $exists = $wpdb->get_results("SELECT postid FROM $table WHERE postid = '$id'");
  387. if ($exists) {
  388. $result = $wpdb->query("UPDATE $table SET last_viewed = NOW(), pageviews = pageviews + 1 WHERE postid = '$id'");
  389. } else {
  390. $result = $wpdb->query("INSERT INTO $table (postid, day, last_viewed) VALUES ('".$id."', NOW(), NOW())");
  391. }
  392.  
  393. // update popularpostsdatacache table
  394. $isincache = $wpdb->get_results("SELECT id FROM ".$table."cache WHERE id = '".$id."' AND day = CURDATE()");
  395. if ($isincache) {
  396. $result2 = $wpdb->query("UPDATE ".$table."cache SET pageviews = pageviews + 1 WHERE id = '".$id."' AND day = CURDATE()");
  397. } else {
  398. $result2 = $wpdb->query("INSERT INTO ".$table."cache (id, day) VALUES ('".$id."', CURDATE())");
  399. }
  400.  
  401. if (($result == 1) && ($result2 == 1)) {
  402. die("OK");
  403. } else {
  404. die($wpdb->print_error);
  405. }
  406.  
  407. }
  408.  
  409. // clears Wordpress Popular Posts' data
  410. function wpp_clear_data() {
  411. $token = $_POST['token'];
  412. $clear = isset($_POST['clear']) ? $_POST['clear'] : '';
  413. $key = get_option("wpp_rand");
  414.  
  415. if (current_user_can('manage_options') && ($token === $key) && !empty($clear)) {
  416. global $wpdb;
  417. // set table name
  418. $table = $wpdb->prefix . "popularpostsdata";
  419. $cache = $wpdb->prefix . "popularpostsdatacache";
  420.  
  421. if ($clear == 'cache') {
  422. if ( $wpdb->get_var("SHOW TABLES LIKE '$cache'") == $cache ) {
  423. $wpdb->query("TRUNCATE TABLE $cache;");
  424. _e('Success! The cache table has been cleared!', 'wordpress-popular-posts');
  425. } else {
  426. _e('Error: cache table does not exist.', 'wordpress-popular-posts');
  427. }
  428. } else if ($clear == 'all') {
  429. if ( $wpdb->get_var("SHOW TABLES LIKE '$table'") == $table && $wpdb->get_var("SHOW TABLES LIKE '$cache'") == $cache ) {
  430. $wpdb->query("TRUNCATE TABLE $table;");
  431. $wpdb->query("TRUNCATE TABLE $cache;");
  432. _e('Success! All data have been cleared!', 'wordpress-popular-posts');
  433. } else {
  434. _e('Error: one or both data tables are missing.', 'wordpress-popular-posts');
  435. }
  436. } else {
  437. _e('Invalid action.', 'wordpress-popular-posts');
  438. }
  439. } else {
  440. _e('Sorry, you do not have enough permissions to do this. Please contact the site administrator for support.', 'wordpress-popular-posts');
  441. }
  442.  
  443. die();
  444. }
  445.  
  446. // database install
  447. function wpp_install() {
  448. global $wpdb;
  449.  
  450. // set table name
  451. $table = $wpdb->prefix . "popularpostsdata";
  452.  
  453. // does popularpostsdata table exists?
  454. if ( $wpdb->get_var("SHOW TABLES LIKE '$table'") != $table ) { // fresh setup
  455. // create tables popularpostsdata and popularpostsdatacache
  456. $sql = "CREATE TABLE " . $table . " ( UNIQUE KEY id (postid), postid int(10) NOT NULL, day datetime NOT NULL default '0000-00-00 00:00:00', last_viewed datetime NOT NULL default '0000-00-00 00:00:00', pageviews int(10) default 1 ); CREATE TABLE " . $table ."cache ( UNIQUE KEY id (id, day), id int(10) NOT NULL, day date NOT NULL, pageviews int(10) default 1 );";
  457.  
  458. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  459. dbDelta($sql);
  460. } else {
  461. $cache = $table . "cache";
  462. if ( $wpdb->get_var("SHOW TABLES LIKE '$cache'") != $cache ) {
  463. // someone is upgrading from version 1.5.x
  464. $sql = "CREATE TABLE " . $table ."cache ( UNIQUE KEY id (id, day), id int(10) NOT NULL, day date NOT NULL, pageviews int(10) default 1 );";
  465.  
  466. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  467. dbDelta($sql);
  468. }
  469. }
  470. }
  471.  
  472. // prints ajax script to theme's header
  473. function wpp_print_ajax() {
  474. // let's add jQuery
  475. wp_print_scripts('jquery');
  476.  
  477. // create security token
  478. $nonce = wp_create_nonce('wpp-token');
  479.  
  480. // get current post's ID
  481. global $wp_query;
  482. wp_reset_query();
  483.  
  484. // if we're on a page or post, load the script
  485. if ( (is_single() || is_page()) && !is_user_logged_in() ) {
  486. $id = $wp_query->post->ID;
  487. ?>
  488. <!-- Wordpress Popular Posts v<?php echo $this->version; ?> -->
  489. <script type="text/javascript" charset="utf-8">
  490. /* <![CDATA[ */
  491. jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', {action: 'wpp_update', token: '<?php echo $nonce; ?>', id: <?php echo $id; ?>});
  492. /* ]]> */
  493. </script>
  494. <!-- End Wordpress Popular Posts v<?php echo $this->version; ?> -->
  495. <?php
  496. }
  497. }
  498.  
  499. // prints popular posts
  500. function get_popular_posts($instance, $echo = true) {
  501.  
  502. global $wpdb;
  503. $table = $wpdb->prefix . "popularpostsdata";
  504.  
  505. if ( $instance['pages'] ) {
  506. $nopages = '';
  507. } else {
  508. $nopages = "AND $wpdb->posts.post_type = 'post'";
  509. }
  510.  
  511. switch( $instance['range'] ) {
  512. case 'all':
  513. $range = "post_date_gmt < '".gmdate("Y-m-d H:i:s")."'";
  514. break;
  515. case 'yesterday':
  516. $range = $table."cache.day >= '".gmdate("Y-m-d")."' - INTERVAL 1 DAY";
  517. break;
  518. case 'daily':
  519. $range = $table."cache.day = CURDATE()";
  520. break;
  521. case 'weekly':
  522. $range = $table."cache.day >= '".gmdate("Y-m-d")."' - INTERVAL 7 DAY";
  523. break;
  524. case 'monthly':
  525. $range = $table."cache.day >= '".gmdate("Y-m-d")."' - INTERVAL 30 DAY";
  526. break;
  527. default:
  528. $range = "post_date_gmt < '".gmdate("Y-m-d H:i:s")."'";
  529. break;
  530. }
  531.  
  532. // sorting options
  533. switch( $instance['order_by'] ) {
  534. case 'comments':
  535. $sortby = 'comment_count';
  536. break;
  537. case 'views':
  538. $sortby = 'pageviews';
  539. break;
  540. case 'avg':
  541. $sortby = 'avg_views';
  542. break;
  543. default:
  544. $sortby = 'comment_count';
  545. break;
  546. }
  547.  
  548.  
  549. // dynamic query fields
  550. $fields = ', ';
  551. if ( $instance['stats_tag']['views'] || ($sortby != 'comment_count') ) {
  552. if ( $instance['range'] == 'all') {
  553. $fields .= "$table.pageviews AS 'pageviews' ";
  554. } else {
  555. if ( $sortby == 'avg_views' ) {
  556. $fields .= "(SUM(".$table."cache.pageviews)/(IF ( DATEDIFF(CURDATE(), MIN(".$table."cache.day)) > 0, DATEDIFF(CURDATE(), MIN(".$table."cache.day)), 1) )) AS 'avg_views' ";
  557. } else {
  558. $fields .= "(SUM(".$table."cache.pageviews)) AS 'pageviews' ";
  559. }
  560. }
  561. }
  562.  
  563. if ( $instance['stats_tag']['comment_count'] ) {
  564. if ( $fields != ', ' ) {
  565. $fields .= ", $wpdb->posts.comment_count AS 'comment_count' ";
  566. } else {
  567. $fields .= "$wpdb->posts.comment_count AS 'comment_count' ";
  568. }
  569. }
  570.  
  571. if ( $instance['stats_tag']['author'] ) {
  572. if ( $fields != ', ' ) {
  573. $fields .= ", (SELECT $wpdb->users.display_name FROM $wpdb->users WHERE $wpdb->users.ID = $wpdb->posts.post_author ) AS 'display_name'";
  574. } else {
  575. $fields .= "(SELECT $wpdb->users.display_name FROM $wpdb->users WHERE $wpdb->users.ID = $wpdb->posts.post_author ) AS 'display_name'";
  576. }
  577. }
  578. if ( $instance['stats_tag']['date']['active'] ) {
  579. if ( $fields != ', ' ) {
  580. $fields .= ", $wpdb->posts.post_date_gmt AS 'date_gmt'";
  581. } else {
  582. $fields .= "$wpdb->posts.post_date_gmt AS 'date_gmt'";
  583. }
  584. }
  585.  
  586. if (strlen($fields) == 2) $fields = '';
  587.  
  588. if ( $instance['range'] == 'all') {
  589. $join = "LEFT JOIN $table ON $wpdb->posts.ID = $table.postid";
  590. $force_pv = "AND ".$table.".pageviews > 0 ";
  591. } else {
  592. $join = "RIGHT JOIN ".$table."cache ON $wpdb->posts.ID = ".$table."cache.id";
  593. $force_pv = "";
  594. }
  595.  
  596.  
  597. // Category excluding snippet suggested by user raamdev at http://wordpress.org/support/topic/397885
  598. // Thanks, raamdev!
  599. if ( $instance['exclude-cats']['active'] && !empty($instance['exclude-cats']['cats']) ) {
  600. $exclude = " AND $wpdb->posts.ID NOT IN (
  601.  
  602. SELECT object_id
  603. FROM $wpdb->term_relationships AS r
  604. JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  605. JOIN $wpdb->terms AS t ON t.term_id = x.term_id
  606. WHERE x.taxonomy = 'category'
  607. AND object_id IN
  608. (
  609. SELECT object_id
  610. FROM $wpdb->term_relationships AS r
  611. JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  612. JOIN $wpdb->terms AS t ON t.term_id = x.term_id
  613. WHERE x.taxonomy = 'category'
  614. AND t.term_id IN (".$instance['exclude-cats']['cats'].")))";
  615. } else {
  616. $exclude = "";
  617. }
  618.  
  619.  
  620. /* Category including snippet by kaynenh - only show popular posts in a specific category */
  621. $currentcatobj = get_category(get_query_var('cat'),false);
  622. $currentcat = $currentcatobj->term_id;
  623.  
  624.  
  625. if ( $instance['include-cats']['active'] && !empty($instance['include-cats']['cats']) ) {
  626. $includecats = " AND $wpdb->posts.ID IN (
  627.  
  628. SELECT object_id
  629. FROM $wpdb->term_relationships AS r
  630. JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  631. JOIN $wpdb->terms AS t ON t.term_id = x.term_id
  632. WHERE x.taxonomy = 'category'
  633. AND object_id IN
  634. (
  635. SELECT object_id
  636. FROM $wpdb->term_relationships AS r
  637. JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  638. JOIN $wpdb->terms AS t ON t.term_id = x.term_id
  639. WHERE x.taxonomy = 'category'
  640. AND t.term_id IN (".$instance['include-cats']['cats'].")))";
  641. }
  642.  
  643.  
  644. /* Added $includecats variable to show only popular posts from a specific category - kaynenh */
  645. $mostpopular = $wpdb->get_results("SELECT $wpdb->posts.ID, $wpdb->posts.post_title $fields FROM $wpdb->posts $join WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_password = '' AND $range $force_pv $nopages $exclude $includecats GROUP BY $wpdb->posts.ID ORDER BY $sortby DESC LIMIT " . $instance['limit'] . "");
  646.  
  647. /*$cat_name = get_cat_name($currentcat);*/
  648. $content = "";
  649.  
  650. if ( !is_array($mostpopular) || empty($mostpopular) ) {
  651. $content .= "<p>".__('Sorry. No data so far.', 'wordpress-popular-posts').$currentcat."</p>"."\n";
  652. } else {
  653.  
  654. if ($instance['markup']['custom_html']) {
  655. $content .= htmlspecialchars_decode($instance['markup']['wpp-start'], ENT_QUOTES) ."\n";
  656. } else {
  657. $content .= "<ul>" . "\n";
  658. }
  659.  
  660. foreach ($mostpopular as $wppost) {
  661.  
  662. $post_stats = "";
  663. $stats = "";
  664. $thumb = "";
  665. $data = array();
  666.  
  667. // get post title
  668. /* qTranslate integration check */
  669. ($this->qTrans) ? $tit = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($wppost->post_title) : $tit = $wppost->post_title;
  670.  
  671. $tit = ($this->magicquotes) ? stripslashes($tit) : $tit;
  672. $title_attr = htmlentities($tit, ENT_QUOTES, $this->charset);
  673.  
  674. if ( $instance['shorten_title']['active'] && (strlen($tit) > $instance['shorten_title']['length'])) {
  675. $tit = mb_substr($tit, 0, $instance['shorten_title']['length'], $this->charset) . "...";
  676. }
  677.  
  678. $tit = htmlentities($tit, ENT_QUOTES, $this->charset);
  679.  
  680. // get post excerpt
  681. if ( $instance['post-excerpt']['active'] ) {
  682. if ($instance['markup']['pattern']['active']) {
  683. $post_content = "<span class=\"wpp-excerpt\">" . $this->get_summary($wppost->ID, $instance) . "</span>";
  684. } else {
  685. $post_content = ": <span class=\"wpp-excerpt\">" . $this->get_summary($wppost->ID, $instance) . "...</span>";
  686. }
  687. } else {
  688. $post_content = "";
  689. }
  690.  
  691. // build stats tag
  692. if ( $instance['stats_tag']['comment_count'] ) {
  693. $comment_count = (int) $wppost->comment_count;
  694. $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comment(s)', 'wordpress-popular-posts') . "</span>";
  695. }
  696. if ( $instance['stats_tag']['views'] ) {
  697. $views_text = __(' view(s)', 'wordpress-popular-posts');
  698. if ($instance['order_by'] == 'views') {
  699. $pageviews = (int) $wppost->pageviews;
  700. } else if ($instance['order_by'] == 'avg') {
  701. $pageviews = ceil($wppost->avg_views);
  702. if ($instance['range'] != 'daily') $views_text = __(' view(s) per day', 'wordpress-popular-posts');
  703. } else {
  704. $pageviews = (int) $wppost->pageviews;
  705. }
  706.  
  707. if ($post_stats != "") {
  708. $post_stats .= " | <span class=\"wpp-views\">$pageviews $views_text</span>";
  709. } else {
  710. $post_stats .= "<span class=\"wpp-views\">$pageviews $views_text</span>";
  711. }
  712. }
  713. if ( $instance['stats_tag']['author'] ) {
  714. if ($post_stats != "") {
  715. $post_stats .= " | ".__('by', 'wordpress-popular-posts')." <span class=\"wpp-author\">".$wppost->display_name."</span>";
  716. } else {
  717. $post_stats .= __('by', 'wordpress-popular-posts')." <span class=\"wpp-author\">".$wppost->display_name."</span>";
  718. }
  719. }
  720. if ( $instance['stats_tag']['date']['active'] ) {
  721. if ($post_stats != "") {
  722. $post_stats .= " | <span class=\"wpp-date\">".__('posted on', 'wordpress-popular-posts')." ".date($instance['stats_tag']['date']['format'], strtotime($wppost->date_gmt))."</span>";
  723. } else {
  724. $post_stats .= "<span class=\"wpp-date\">".__('posted on', 'wordpress-popular-posts')." ".date($instance['stats_tag']['date']['format'], strtotime($wppost->date_gmt))."</span>";
  725. }
  726. }
  727.  
  728. if (!empty($post_stats)) {
  729. $stats = ' <span class="post-stats">' . $post_stats . '</span> ';
  730. }
  731.  
  732. // get thumbnail
  733. if ($instance['thumbnail']['active'] && $this->thumb ) {
  734. $tbWidth = $instance['thumbnail']['width'];
  735. $tbHeight = $instance['thumbnail']['height'];
  736.  
  737. // default image
  738. $thumb = "<a href=\"".get_permalink($wppost->ID)."\" class=\"wppnothumb\" title=\"". $title_attr ."\"><img src=\"". $this->pluginDir . "/no_thumb.jpg\" alt=\"".$title_attr."\" border=\"0\" class=\"wpp-thumbnail\" width=\"".$tbWidth."\" height=\"".$tbHeight."\" "."/></a>";
  739.  
  740. // let's try to retrieve the post thumbnail!
  741. if ($instance['thumbnail']['thumb_selection'] == "usergenerated") { // use thumbnail selected by user
  742. if (function_exists('get_the_post_thumbnail') && has_post_thumbnail( $wppost->ID )) {
  743. $thumb = "<a href=\"".get_permalink($wppost->ID)."\" title=\"". $title_attr ."\">" . get_the_post_thumbnail($wppost->ID, array($tbWidth), array('class' => 'wpp-thumbnail', 'alt' => $title_attr, 'title' => $title_attr) ) ."</a> <!-- $tbWidth $tbHeight-->";
  744. }
  745. } else if ($instance['thumbnail']['thumb_selection'] == "wppgenerated") { // Wordpress Popular Posts should attempt to create a thumbnail by itself
  746. $img = $this->get_img($wppost->ID);
  747. if ( ($img && !empty($img)) ) {
  748. $thumb = "<a href=\"".get_permalink($wppost->ID)."\" class=\"wppgen\" title=\"". $title_attr ."\"><img src=\"". $this->pluginDir . "/scripts/timthumb.php?src=". $img[1] ."&amp;h=".$tbHeight."&amp;w=".$tbWidth."&amp;zc=1\" alt=\"".$title_attr."\" border=\"0\" class=\"wpp-thumbnail\" width=\"".$tbWidth."\" height=\"".$tbHeight."\" "."/></a>";
  749. }
  750. }
  751. }
  752.  
  753. // get rating
  754. if ($instance['rating'] && $this->postRating) {
  755. $rating = '<span class="wpp-rating">'.the_ratings_results($wppost->ID).'</span>';
  756. } else {
  757. $rating = '';
  758. }
  759. $data = array(
  760. 'title' => '<a href="'.get_permalink($wppost->ID).'" title="'. $title_attr .'"><span class="wpp-post-title">'. $tit .'</span></a>',
  761. 'summary' => $post_content,
  762. 'stats' => $stats,
  763. 'img' => $thumb,
  764. 'id' => $wppost->ID
  765. );
  766.  
  767. // build custom layout
  768. if ($instance['markup']['custom_html']) {
  769. if ($instance['markup']['pattern']['active']) {
  770. $content .= htmlspecialchars_decode($instance['markup']['post-start'], ENT_QUOTES) . $this->format_content($instance['markup']['pattern']['form'], $data, $instance['rating']) . htmlspecialchars_decode($instance['markup']['post-end'], ENT_QUOTES) . "\n";
  771. } else {
  772. $content .= htmlspecialchars_decode($instance['markup']['post-start'], ENT_QUOTES) . $thumb . '<a href="'.get_permalink($wppost->ID).'" title="'. $title_attr .'"><span class="wpp-post-title">'. $tit .'</span></a>'.$post_content.' '. $stats . $rating . htmlspecialchars_decode($instance['markup']['post-end'], ENT_QUOTES) . "\n";
  773. }
  774. } else {
  775. $content .= '<li>'. $thumb .'<a href="'. get_permalink($wppost->ID) .'" title="'. $title_attr .'"><span class="wpp-post-title">'. $tit .'</span></a>'. $post_content .' '. $stats . $rating .'</li>' . "\n";
  776. }
  777. }
  778.  
  779. if ($instance['markup']['custom_html']) {
  780. $content .= htmlspecialchars_decode($instance['markup']['wpp-end'], ENT_QUOTES) ."\n";
  781. } else {
  782. $content .= "\n"."</ul>"."\n";
  783. }
  784.  
  785. }
  786.  
  787. if ($echo) { echo "<noscript>" . $content . "</noscript>"; } else { return $content; }
  788. }
  789.  
  790. // builds posts' excerpt
  791. function get_summary($id, $instance){
  792. if (!is_numeric($id)) return false;
  793. global $wpdb;
  794. $excerpt = "";
  795. $result = "";
  796.  
  797. $result = $wpdb->get_results("SELECT post_excerpt FROM $wpdb->posts WHERE ID = " . $id, ARRAY_A);
  798.  
  799. if (empty($result[0]['post_excerpt'])) {
  800. // no custom excerpt defined, how lazy of you!
  801. $result = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE ID = " . $id, ARRAY_A);
  802. $excerpt = preg_replace("/\[caption.*\[\/caption\]/", "", $result[0]['post_content']);
  803. } else {
  804. // user has defined a custom excerpt, yay!
  805. $excerpt = preg_replace("/\[caption.*\[\/caption\]/", "", $result[0]['post_excerpt']);
  806. }
  807.  
  808. $excerpt = preg_replace("/<object[0-9 a-z_?*=\":\-\/\.#\,\\n\\r\\t]+/smi", "", $excerpt);
  809.  
  810. if ($instance['post-excerpt']['keep_format']) {
  811. $excerpt = strip_tags($excerpt, '<a><b><i><strong><em>');
  812. } else {
  813. $excerpt = strip_tags($excerpt);
  814. }
  815.  
  816. if (strlen($excerpt) > $instance['post-excerpt']['length']) {
  817. $excerpt = $this->truncate($excerpt, $instance['post-excerpt']['length'], '', true, true);
  818. }
  819.  
  820. return $excerpt;
  821. }
  822.  
  823. // gets the first image of post / page
  824. function get_img($id = "", $print = false) {
  825. if ( empty($id) || !is_numeric($id) ) return false;
  826.  
  827. // get post attachments
  828. $attachments = get_children(array('post_parent' => $id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
  829.  
  830. // no image has been found
  831. if ( ! is_array($attachments) ) return false;
  832.  
  833. $count = count($attachments);
  834. $first_attachment = array_shift($attachments);
  835. $img = wp_get_attachment_image($first_attachment->ID);
  836.  
  837. preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'\s>]*)/i', $img, $imgm);
  838.  
  839. if ($print)
  840. echo $imgm[1];
  841. else
  842. return $imgm;
  843.  
  844. }
  845.  
  846. // parses content structure defined by user
  847. function format_content ($string, $data = array(), $rating) {
  848. if (empty($string) || (empty($data) || !is_array($data))) return false;
  849.  
  850. $params = array();
  851. $pattern = '/\{(summary|stats|title|image|rating)\}/i';
  852. preg_match_all($pattern, $string, $matches);
  853.  
  854. for ($i=0; $i < count($matches[0]); $i++) {
  855. if (strtolower($matches[0][$i]) == "{title}") {
  856. $params[$matches[0][$i]] = $data['title'];
  857. continue;
  858. }
  859. if (strtolower($matches[0][$i]) == "{stats}") {
  860. $params[$matches[0][$i]] = $data['stats'];
  861. continue;
  862. }
  863. if (strtolower($matches[0][$i]) == "{summary}") {
  864. $params[$matches[0][$i]] = $data['summary'];
  865. continue;
  866. }
  867. if (strtolower($matches[0][$i]) == "{image}") {
  868. $params[$matches[0][$i]] = $data['img'];
  869. continue;
  870. }
  871. // WP-PostRatings check
  872. if ($rating) {
  873. if (strtolower($matches[0][$i]) == "{rating}") {
  874. $params[$matches[0][$i]] = the_ratings_results($data['id']);
  875. continue;
  876. }
  877. }
  878. }
  879.  
  880. for ($i=0; $i < count($params); $i++) {
  881. $string = str_replace($matches[0][$i], $params[$matches[0][$i]], $string);
  882. }
  883.  
  884. return $string;
  885. }
  886.  
  887. // code seen at http://www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags/
  888. // Since 2.0.1
  889. /**
  890. * Truncates text.
  891. *
  892. * Cuts a string to the length of $length and replaces the last characters
  893. * with the ending if the text is longer than length.
  894. *
  895. * @param string $text String to truncate.
  896. * @param integer $length Length of returned string, including ellipsis.
  897. * @param string $ending Ending to be appended to the trimmed string.
  898. * @param boolean $exact If false, $text will not be cut mid-word
  899. * @param boolean $considerHtml If true, HTML tags would be handled correctly
  900. * @return string Trimmed string.
  901. */
  902. function truncate($text, $length = 100, $ending = '...', $exact = true, $considerHtml = false) {
  903. if ($considerHtml) {
  904. // if the plain text is shorter than the maximum length, return the whole text
  905. if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
  906. return $text;
  907. }
  908. // splits all html-tags to scanable lines
  909. preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
  910. $total_length = strlen($ending);
  911. $open_tags = array();
  912. $truncate = '';
  913. foreach ($lines as $line_matchings) {
  914. // if there is any html-tag in this line, handle it and add it (uncounted) to the output
  915. if (!empty($line_matchings[1])) {
  916. // if it's an "empty element" with or without xhtml-conform closing slash (f.e. <br/>)
  917. if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
  918. // do nothing
  919. // if tag is a closing tag (f.e. </b>)
  920. } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
  921. // delete tag from $open_tags list
  922. $pos = array_search($tag_matchings[1], $open_tags);
  923. if ($pos !== false) {
  924. unset($open_tags[$pos]);
  925. }
  926. // if tag is an opening tag (f.e. <b>)
  927. } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
  928. // add tag to the beginning of $open_tags list
  929. array_unshift($open_tags, strtolower($tag_matchings[1]));
  930. }
  931. // add html-tag to $truncate'd text
  932. $truncate .= $line_matchings[1];
  933. }
  934. // calculate the length of the plain text part of the line; handle entities as one character
  935. $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
  936. if ($total_length+$content_length> $length) {
  937. // the number of characters which are left
  938. $left = $length - $total_length;
  939. $entities_length = 0;
  940. // search for html entities
  941. if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
  942. // calculate the real length of all entities in the legal range
  943. foreach ($entities[0] as $entity) {
  944. if ($entity[1]+1-$entities_length <= $left) {
  945. $left--;
  946. $entities_length += strlen($entity[0]);
  947. } else {
  948. // no more characters left
  949. break;
  950. }
  951. }
  952. }
  953. $truncate .= substr($line_matchings[2], 0, $left+$entities_length);
  954. // maximum lenght is reached, so get off the loop
  955. break;
  956. } else {
  957. $truncate .= $line_matchings[2];
  958. $total_length += $content_length;
  959. }
  960. // if the maximum length is reached, get off the loop
  961. if($total_length>= $length) {
  962. break;
  963. }
  964. }
  965. } else {
  966. if (strlen($text) <= $length) {
  967. return $text;
  968. } else {
  969. $truncate = substr($text, 0, $length - strlen($ending));
  970. }
  971. }
  972. // if the words shouldn't be cut in the middle...
  973. if (!$exact) {
  974. // ...search the last occurance of a space...
  975. $spacepos = strrpos($truncate, ' ');
  976. if (isset($spacepos)) {
  977. // ...and cut the text in this position
  978. $truncate = substr($truncate, 0, $spacepos);
  979. }
  980. }
  981. // add the defined ending to the text
  982. $truncate .= $ending;
  983. if($considerHtml) {
  984. // close all unclosed html-tags
  985. foreach ($open_tags as $tag) {
  986. $truncate .= '</' . $tag . '>';
  987. }
  988. }
  989. return $truncate;
  990. }
  991.  
  992. // plugin localization (Credits: Aleksey Timkov at@uadeveloper.com)
  993. function wpp_textdomain() {
  994. load_plugin_textdomain('wordpress-popular-posts', 'wp-content/plugins/wordpress-popular-posts');
  995. }
  996.  
  997. // insert Wordpress Popular Posts' stylesheet in theme's head section, just in case someone needs it
  998. function wpp_print_stylesheet() {
  999. echo "\n"."<!-- Wordpress Popular Posts v". $this->version ." -->"."\n".'<link rel="stylesheet" href="'.$this->pluginDir.'/style/wpp.css" type="text/css" media="screen" />'."\n"."<!-- End Wordpress Popular Posts v". $this->version ." -->"."\n";
  1000. }
  1001.  
  1002. // create Wordpress Popular Posts' maintenance page
  1003. function wpp_maintenance_page() {
  1004. require (dirname(__FILE__) . '/maintenance.php');
  1005. }
  1006. function add_wpp_maintenance_page() {
  1007. add_submenu_page('options-general.php', 'Wordpress Popular Posts', 'Wordpress Popular Posts', 10, __FILE__, array(&$this, 'wpp_maintenance_page'));
  1008. }
  1009.  
  1010. // version update warning
  1011. function wpp_update_warning() {
  1012. $msg = '<div id="wpp-message" class="error fade"><p>'.__('Your Wordpress version is too old. Wordpress Popular Posts Plugin requires at least version 2.8 to function correctly. Please update your blog via Tools &gt; Upgrade.', 'wordpress-popular-posts').'</p></div>';
  1013. echo trim($msg);
  1014. }
  1015.  
  1016. // cache maintenance
  1017. function wpp_cache_maintenance() {
  1018. global $wpdb;
  1019. $wpdb->query("DELETE FROM ".$wpdb->prefix."popularpostsdatacache WHERE day < DATE_SUB(CURDATE(), INTERVAL 30 DAY);");
  1020. }
  1021.  
  1022. // plugin deactivation
  1023. function wpp_deactivation() {
  1024. wp_clear_scheduled_hook('wpp_cache_event');
  1025. remove_shortcode('wpp');
  1026. remove_shortcode('WPP');
  1027.  
  1028. delete_option('wpp_ver');
  1029. }
  1030.  
  1031. // shortcode handler
  1032. function wpp_shortcode($atts = NULL, $content = NULL) {
  1033. /* Added cats_to_include shortcode to include only specific categories - kaynenh */
  1034. extract( shortcode_atts( array(
  1035. 'header' => '',
  1036. 'limit' => 10,
  1037. 'range' => 'daily',
  1038. 'order_by' => 'comments',
  1039. 'pages' => true,
  1040. 'title_length' => 0,
  1041. 'excerpt_length' => 0,
  1042. 'excerpt_format' => 0,
  1043. 'cats_to_exclude' => '',
  1044. 'cats_to_include' => '',
  1045. 'thumbnail_width' => 0,
  1046. 'thumbnail_height' => 0,
  1047. 'thumbnail_selection' => 'wppgenerated',
  1048. 'rating' => false,
  1049. 'stats_comments' => true,
  1050. 'stats_views' => false,
  1051. 'stats_author' => false,
  1052. 'stats_date' => false,
  1053. 'stats_date_format' => 'F j, Y',
  1054. 'wpp_start' => '<ul>',
  1055. 'wpp_end' => '</ul>',
  1056. 'post_start' => '<li>',
  1057. 'post_end' => '</li>',
  1058. 'header_start' => '<h2>',
  1059. 'header_end' => '</h2>',
  1060. 'do_pattern' => false,
  1061. 'pattern_form' => '{image} {title}: {summary} {stats}'
  1062. ), $atts ) );
  1063.  
  1064. // possible values for "Time Range" and "Order by"
  1065. $range_values = array("yesterday", "daily", "weekly", "monthly", "all");
  1066. $order_by_values = array("comments", "views", "avg");
  1067. $thumbnail_selector = array("wppgenerated", "usergenerated");
  1068.  
  1069. $shortcode_ops = array(
  1070. 'title' => strip_tags($header),
  1071. 'limit' => empty($limit) ? 10 : (is_numeric($limit)) ? (($limit > 0) ? $limit : 10) : 10,
  1072. 'range' => (in_array($range, $range_values)) ? $range : 'daily',
  1073. 'order_by' => (in_array($order_by, $order_by_values)) ? $order_by : 'comments',
  1074. 'pages' => empty($pages) ? false : $pages,
  1075. 'shorten_title' => array(
  1076. 'active' => empty($title_length) ? false : (is_numeric($title_length)) ? (($title_length > 0) ? true : false) : false,
  1077. 'length' => empty($title_length) ? 0 : (is_numeric($title_length)) ? $title_length : 0
  1078. ),
  1079. 'post-excerpt' => array(
  1080. 'active' => empty($excerpt_length) ? false : (is_numeric($excerpt_length)) ? (($excerpt_length > 0) ? true : false) : false,
  1081. 'length' => empty($excerpt_length) ? 0 : (is_numeric($excerpt_length)) ? $excerpt_length : 0,
  1082. 'keep_format' => empty($excerpt_format) ? false : (is_numeric($excerpt_format)) ? (($excerpt_format > 0) ? true : false) : false,
  1083. ),
  1084. 'exclude-cats' => array(
  1085. 'active' => empty($cats_to_exclude) ? false : (ctype_digit(str_replace(",", "", $cats_to_exclude))) ? true : false,
  1086. 'cats' => empty($cats_to_exclude) ? '' : (ctype_digit(str_replace(",", "", $cats_to_exclude))) ? $cats_to_exclude : ''
  1087. ),
  1088. /* Added to show only popular posts in a specific category - kaynenh */
  1089. 'include-cats' => array(
  1090. 'active' => empty($cats_to_include) ? false : (ctype_digit(str_replace(",", "", $cats_to_include))) ? true : false,
  1091. 'cats' => empty($cats_to_include) ? '' : (ctype_digit(str_replace(",", "", $cats_to_include))) ? $cats_to_include : ''
  1092. ),
  1093. 'thumbnail' => array(
  1094. 'active' => empty($thumbnail_width) ? false : (is_numeric($thumbnail_width)) ? (($thumbnail_width > 0) ? true : false) : false,
  1095. 'thumb_selection' => (in_array($thumbnail_selection, $thumbnail_selector)) ? $thumbnail_selection : 'wppgenerated',
  1096. 'width' => empty($thumbnail_width) ? 0 : (is_numeric($thumbnail_width)) ? $thumbnail_width : 0,
  1097. 'height' => empty($thumbnail_height) ? 0 : (is_numeric($thumbnail_height)) ? $thumbnail_height : 0
  1098. ),
  1099. 'rating' => empty($rating) ? false : (bool)$rating,
  1100. 'stats_tag' => array(
  1101. 'comment_count' => empty($stats_comments) ? false : $stats_comments,
  1102. 'views' => empty($stats_views) ? false : $stats_views,
  1103. 'author' => empty($stats_author) ? false : $stats_author,
  1104. 'date' => array(
  1105. 'active' => empty($stats_date) ? false : $stats_date,
  1106. 'format' => empty($stats_date_format) ? 'F j, Y' : $stats_date_format
  1107. )
  1108. ),
  1109. 'markup' => array(
  1110. 'custom_html' => true,
  1111. 'wpp-start' => empty($wpp_start) ? '<ul>' : $wpp_start,
  1112. 'wpp-end' => empty($wpp_end) ? '</ul>' : $wpp_end,
  1113. 'post-start' => empty($post_start) ? '<li>;' : $post_start,
  1114. 'post-end' => empty($post_end) ? '</li>' : $post_end,
  1115. 'title-start' => empty($header_start) ? '' : $header_start,
  1116. 'title-end' => empty($header_end) ? '' : $header_end,
  1117. 'pattern' => array(
  1118. 'active' => empty($do_pattern) ? false : (bool)$do_pattern,
  1119. 'form' => empty($pattern_form) ? '{image} {title}: {summary} {stats}' : $pattern_form
  1120. )
  1121. )
  1122. );
  1123.  
  1124. $shortcode_content = "";
  1125.  
  1126. $shortcode_content .= "<!-- Wordpress Popular Posts Plugin v". $this->version ." [SC] [".$shortcode_ops['range']."]". (($shortcode_ops['markup']['custom_html']) ? ' [custom]' : ' [regular]') ." -->"."\n";
  1127.  
  1128. // is there a title defined by user?
  1129. if (!empty($header) && !empty($header_start) && !empty($header_end)) {
  1130. $shortcode_content .= $header_start . $header . $header_end;
  1131. }
  1132.  
  1133. // print popular posts list
  1134. $shortcode_content .= $this->get_popular_posts($shortcode_ops, false);
  1135.  
  1136. $shortcode_content .= "<!-- End Wordpress Popular Posts Plugin v". $this->version ." -->"."\n";
  1137.  
  1138. return $shortcode_content;
  1139. }
  1140.  
  1141. // stats page
  1142. // Since 2.0.3
  1143. function wpp_stats() {
  1144. if ( function_exists('add_submenu_page') ) add_submenu_page('index.php', __('Wordpress Popular Posts Stats'), __('Wordpress Popular Posts Stats'), 'manage_options', 'wpp-stats-display', array(&$this, 'wpp_stats_display'));
  1145. }
  1146.  
  1147. function wpp_stats_display() {
  1148. require (dirname(__FILE__) . '/stats.php');
  1149. }
  1150. }
  1151. }
  1152.  
  1153. /**
  1154. * Wordpress Popular Posts template tags for use in themes.
  1155. */
  1156.  
  1157. // gets views count
  1158. // Since 2.0.0
  1159. function wpp_get_views($id = NULL) {
  1160. // have we got an id?
  1161. if ( empty($id) || is_null($id) || !is_numeric($id) ) {
  1162. return "-1";
  1163. } else {
  1164. global $wpdb;
  1165.  
  1166. $table_name = $wpdb->prefix . "popularpostsdata";
  1167. $result = $wpdb->get_results("SELECT pageviews FROM $table_name WHERE postid = '$id'", ARRAY_A);
  1168.  
  1169. if ( !is_array($result) || empty($result) ) {
  1170. return "0";
  1171. } else {
  1172. return $result[0]['pageviews'];
  1173. }
  1174. }
  1175. }
  1176.  
  1177. // gets popular posts
  1178. // Since 2.0.3
  1179. function wpp_get_mostpopular($args = NULL) {
  1180.  
  1181. if (is_null($args)) {
  1182. echo do_shortcode('[wpp]');
  1183. } else {
  1184. $atts = trim(str_replace("&", " ", $args));
  1185. echo do_shortcode('[wpp '.$atts.']');
  1186. }
  1187. }
  1188.  
  1189. // gets popular posts
  1190. /**
  1191. * Deprecated in 2.0.3.
  1192. * Use wpp_get_mostpopular instead.
  1193. */
  1194. function get_mostpopular($args = NULL) {
  1195. return wpp_get_mostpopular($args);
  1196. }
  1197.  
  1198.  
  1199. /**
  1200. * Wordpress Popular Posts 2.1.4 Changelog.
  1201. */
  1202.  
  1203. /*
  1204. = 2.1.4 =
  1205. * Added charset detection.
  1206. */