Changeset 681931
- Timestamp:
- 03/14/2013 07:35:00 PM (13 years ago)
- Location:
- jetpack-post-views
- Files:
-
- 4 added
- 3 edited
-
assets/screenshot-1.png (modified) (previous)
-
tags/1.0.4 (added)
-
tags/1.0.4/jetpack-post-views.php (added)
-
tags/1.0.4/readme.txt (added)
-
tags/1.0.4/uninstall.php (added)
-
trunk/jetpack-post-views.php (modified) (13 diffs)
-
trunk/readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
jetpack-post-views/trunk/jetpack-post-views.php
r666100 r681931 6 6 * Description: Adds a widget that displays your most popular posts using Jetpack stats. <strong>NOTE:</strong> If the plugin does not work, visit the <a href="options-general.php?page=jetpack_post_views">Settings</a> page to enter a WordPress API Key. 7 7 * Author: Steven Lambert 8 * Version: 1.0. 38 * Version: 1.0.4 9 9 * Author URI: http://sklambert.com 10 10 * License: GPL2+ … … 13 13 // Define plugin version 14 14 if ( !defined( 'JETPACK_POST_VIEWS_VERSION_NUM' ) ) { 15 define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0. 3' );15 define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0.4' ); 16 16 } 17 17 … … 110 110 111 111 112 / / ADD A "SETTINGS" LINK TO PLUGINS PAGE112 /* ADD A "SETTINGS" LINK TO PLUGINS PAGE */ 113 113 function settings_link( $links, $file ) { 114 114 static $this_plugin; … … 214 214 <h3><?php _e( 'Connections', 'jetpack-post-views' ); ?></h3> 215 215 216 <p><?php _e( 'Shows the status of connections to the Jetpack API. If at least one of the connections below shows green, the plugin should work properly.<br>Stats areupdated using the first connection available in the order that they are listed.', 'jetpack-post-views' ); ?></p>216 <p><?php _e( 'Shows the status of connections to the Jetpack API. If at least one of the below connections shows green, the plugin should work properly.<br>Stats updated using the first connection available in the order that they are listed.', 'jetpack-post-views' ); ?></p> 217 217 218 218 <?php … … 305 305 else { 306 306 echo "Function does not exist"; 307 }*/ 307 } 308 */ 308 309 ?> 309 310 </p> … … 323 324 $title = apply_filters('widget_title', $instance['title'] ); 324 325 325 // Grab the top posts and display them326 $args = array(327 'numberposts' => $instance['num_posts'],328 'orderby' => 'meta_value_num',329 'order' => 'DESC',330 'meta_key' => 'jetpack-post-views'331 );332 $posts = get_posts( $args );333 334 326 echo $before_widget; 335 327 … … 338 330 echo $before_title . $title . $after_title; 339 331 340 // Print top posts in order 341 echo "<ul>"; 342 foreach( $posts as $post) { ?> 343 <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 344 <?php if ( $instance['show_views'] ) { 345 echo " - ".number_format_i18n( get_post_meta( $post->ID, 'jetpack-post-views', true ) )." views"; 346 } ?> 347 </li> 348 <?php 349 } 350 echo "</ul>"; 332 // Grab the top posts and display them using the stats_get_csv function 333 if ($instance['days'] != '-1' && function_exists('stats_get_csv') ) { 334 $posts = stats_get_csv('postviews', 'days='.$instance['days'].'&limit=-1' ); 335 $exclude_posts = explode( ',', $instance['exclude_posts'] ); 336 $count = 0; 337 338 // Print top posts in order 339 echo "<ul>"; 340 foreach( $posts as $post ) { 341 // Stop printing posts if we reach the limit 342 if ( $count >= intval( $instance['num_posts'] ) ) { 343 break; 344 } 345 346 // Only display posts 347 if ( $post['post_id'] && get_post( $post['post_id'] ) && 'post' === get_post_type( $post['post_id'] ) && !in_array( $post['post_id'], $exclude_posts) ) { ?> 348 <li><a href="<?php echo get_permalink( $post['post_id'] ) ?>"><?php echo get_the_title( $post['post_id'] ) ?></a> 349 <?php if ( $instance['show_views'] ) { 350 echo " - ".number_format_i18n( $post['views'] )." views"; 351 } ?> 352 </li> 353 <?php 354 $count++; 355 } 356 } 357 echo "</ul>"; 358 } 359 // Else grab the top posts using the post meta 360 else { 361 $args = array( 362 'numberposts' => $instance['num_posts'], 363 'orderby' => 'meta_value_num', 364 'order' => 'DESC', 365 'exclude' => $instance['exclude_posts'], 366 'meta_key' => 'jetpack-post-views' 367 ); 368 $posts = get_posts( $args ); 369 370 // Print top posts in order 371 echo "<ul>"; 372 foreach( $posts as $post ) { ?> 373 <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 374 <?php if ( $instance['show_views'] ) { 375 echo " - ".number_format_i18n( get_post_meta( $post->ID, 'jetpack-post-views', true ) )." views"; 376 } ?> 377 </li> 378 <?php 379 } 380 echo "</ul>"; 381 } 351 382 352 383 echo $after_widget; … … 364 395 $instance['show_views'] = strip_tags( $new_instance['show_views'] ); 365 396 $num_posts = intval( strip_tags( $new_instance['num_posts'] ) ); 397 $instance['days'] = strip_tags( $new_instance['days'] ); 398 $instance['exclude_posts'] = strip_tags( $new_instance['exclude_posts'] ); 366 399 367 400 // Set default number of posts to display if invalid option … … 382 415 // Default widget settings 383 416 $defaults = array( 384 'title' => __('Most Popular Posts', 'jetpack-post-views'), 385 'error' => '', 386 'num_posts' => 5, 387 'show_views' => false 417 'title' => __('Most Popular Posts', 'jetpack-post-views'), 418 'error' => '', 419 'num_posts' => 5, 420 'days' => '-1', 421 'exclude_posts' => '', 422 'show_views' => false 388 423 ); 389 424 $instance = wp_parse_args( (array) $instance, $defaults ); … … 405 440 <input id="<?php echo $this->get_field_id( 'num_posts' ); ?>" name="<?php echo $this->get_field_name( 'num_posts' ); ?>" value="<?php echo $instance['num_posts']; ?>" type="text" size="3" /> 406 441 </p> 442 443 <p> 444 <label for="<?php echo $this->get_field_id( 'days' ); ?>"><?php _e('<span class="error">*</span>Time interval:', 'jetpack-post-views'); ?></label> 445 <select id="<?php echo $this->get_field_id( 'days' ); ?>" name="<?php echo $this->get_field_name( 'days' ); ?>"> 446 <option value="-1" <?php echo ($instance['days'] == '-1' ? 'selected' : '') ?> ><?php _e('Unlimited', 'jetpack-post-views'); ?></option> 447 <option value="1" <?php echo ($instance['days'] == '1' ? 'selected' : '') ?> ><?php _e('Day', 'jetpack-post-views'); ?></option> 448 <option value="7" <?php echo ($instance['days'] == '7' ? 'selected' : '') ?> ><?php _e('Week', 'jetpack-post-views'); ?></option> 449 <option value="30" <?php echo ($instance['days'] == '30' ? 'selected' : '') ?> ><?php _e('Month', 'jetpack-post-views'); ?></option> 450 <option value="366" <?php echo ($instance['days'] == '366' ? 'selected' : '') ?> ><?php _e('Year', 'jetpack-post-views'); ?></option> 451 </select> 452 </p> 407 453 454 <p> 455 <label for="<?php echo $this->get_field_id( 'exclude_posts' ); ?>"><?php _e('Exclude Posts:', 'jetpack-post-views'); ?></label> 456 <input class="widefat" id="<?php echo $this->get_field_id( 'exclude_posts' ); ?>" name="<?php echo $this->get_field_name( 'exclude_posts' ); ?>" value="<?php echo $instance['exclude_posts']; ?>" type="text" placeholder="<?php _e('Comma-separated list of post IDs', 'jetpack-post-views'); ?>" /> 408 457 <p> 409 458 <input class="checkbox" type="checkbox" <?php checked( $instance['show_views'], 'on' ); ?> id="<?php echo $this->get_field_id( 'show_views' ); ?>" name="<?php echo $this->get_field_name( 'show_views' ); ?>" /> 410 459 <label for="<?php echo $this->get_field_id( 'show_views' ); ?>"><?php _e('Display number of views?', 'jetpack-post-views'); ?></label> 460 </p> 461 462 <p> 463 <div><?php _e('<span class="error">*</span>Only works if the function <code>stats_get_csv()</code> exists', 'jetpack-post-views'); ?></div> 411 464 </p> 412 465 … … 482 535 function upgrade() { 483 536 // Delete old options 484 delete_option( 'jetpack_post_views' );485 537 delete_option( 'jetpack-post-views_version' ); 486 538 delete_option( 'jetpack_post_views_wp_api_key' ); 487 539 delete_option( 'jetpack_post_views_stats_has_run' ); 488 540 489 define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0. 3' );541 define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0.4' ); 490 542 } 491 543 … … 535 587 536 588 // Upgrade the plugin if necessary 537 if ( JETPACK_POST_VIEWS_VERSION_NUM != '1.0. 3' ) {589 if ( JETPACK_POST_VIEWS_VERSION_NUM != '1.0.4' ) { 538 590 $Jetpack_Post_Views->upgrade(); 539 591 } … … 546 598 register_activation_hook( __FILE__, 'jetpack_post_views_on_activation' ); 547 599 register_deactivation_hook( __FILE__, 'jetpack_post_views_on_deactivation' ); 600 601 /* 602 * DISPLAY TOP POSTS 603 * Use the Jetpack stats_get_csv() function to create a list of the top posts 604 * args: days - number of days of the desired time frame. '-1' means unlimited 605 * limit - number of posts to display. '-1' means unlimited. If days is -1, then limit is capped at 500 606 * exclude - comma-separated list of post IDs to exclude form displaying 607 * displayViews - flag to display the post views 608 */ 609 function JPV_display_top_posts( $args = array( 'days' => '-1', 'limit' => '5', 'exclude' => '', 'displayViews' => false ) ) { 610 // Ensure that the stats_get_csv() function exists and returns posts 611 if ( function_exists('stats_get_csv') && $posts = stats_get_csv('postviews', 'days='.($args['days'] ? $args['days'] : '-1').'&limit=-1' ) ) { 612 $count = 0; 613 $exclude_posts = explode( ',', $args['exclude'] ); 614 615 // Print top posts in order 616 echo "<ul class='JVP-top-posts'>"; 617 foreach( $posts as $post ) { 618 619 // Stop printing posts if we reach the limit 620 if ( $args['limit'] && $count >= intval( $args['limit'] ) ) { 621 break; 622 } 623 624 // Only display posts 625 if ( $post['post_id'] && get_post( $post['post_id'] ) && 'post' === get_post_type( $post['post_id'] ) && !in_array( $post['post_id'], $exclude_posts) ) { ?> 626 <li><a href="<?php echo get_permalink( $post['post_id'] ) ?>"><?php echo get_the_title( $post['post_id'] ) ?></a> 627 <?php if ( $args['displayViews'] ) { 628 echo " - ".number_format_i18n( $post['views'] )." views"; 629 } ?> 630 </li> 631 <?php 632 $count++; 633 } 634 } 635 echo "</ul>"; 636 } 637 } -
jetpack-post-views/trunk/readme.txt
r666103 r681931 4 4 Requires at least: 3.5 5 5 Tested up to: 3.5.1 6 Stable tag: 1.0. 36 Stable tag: 1.0.4 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 36 36 == Frequently asked questions == 37 37 38 = Why are there no FAQs besides this one? =38 = How can I display the top posts in my template? = 39 39 40 Because you haven't asked one yet. 40 Use the function 'JPV_display_top_posts()' 41 42 *Usage* 43 `<?php if ( function_exists('JPV_display_top_posts') ) { JPV_display_top_posts( $args ); } ?>` 44 45 *Default Usage* 46 `<?php $args = array( 47 'days' => '-1', 48 'limit' => '5', 49 'exclude' => '', 50 'displayViews' => false ); ?>` 51 *Parameters* 52 53 **days** - (*string*) The number of days of the desired time frame. '-1' means unlimited. 54 55 **limit** - (*string*) The number of posts to display. '-1' means unlimited. If days is -1, then limit is capped at 500. 56 57 **exclude** - (*string*) A comma-separated list of Post Ids to be excluded from displaying. 58 59 **displayViews** - (*boolean*) Displays the post views. 41 60 42 61 == Screenshots == … … 48 67 49 68 == Changelog == 69 70 = 1.0.4 (2013-03-14) = 71 * Added the `JPV_display_top_posts()` function to display top posts in a template 72 * Added widget options to exclude posts by ID and to display a different time frame 50 73 51 74 = 1.0.3 (2013-02-10) =
Note: See TracChangeset
for help on using the changeset viewer.