Plugin Directory

Changeset 681931


Ignore:
Timestamp:
03/14/2013 07:35:00 PM (13 years ago)
Author:
straker503
Message:

Checking in version 1.0.4

  • Added the JPV_display_top_posts() function to display top posts in a template
  • Added widget options to exclude posts by ID and to display a different time frame
Location:
jetpack-post-views
Files:
4 added
3 edited

Legend:

Unmodified
Added
Removed
  • jetpack-post-views/trunk/jetpack-post-views.php

    r666100 r681931  
    66 * 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.
    77 * Author: Steven Lambert
    8  * Version: 1.0.3
     8 * Version: 1.0.4
    99 * Author URI: http://sklambert.com
    1010 * License: GPL2+
     
    1313// Define plugin version
    1414if ( !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' );
    1616}
    1717
     
    110110
    111111
    112     // ADD A "SETTINGS" LINK TO PLUGINS PAGE
     112    /* ADD A "SETTINGS" LINK TO PLUGINS PAGE */
    113113    function settings_link( $links, $file ) {
    114114        static $this_plugin;
     
    214214            <h3><?php _e( 'Connections', 'jetpack-post-views' ); ?></h3>
    215215
    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 are updated 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>
    217217
    218218            <?php
     
    305305                else {
    306306                    echo "Function does not exist";
    307                 }*/
     307                }
     308                */
    308309            ?>
    309310            </p>
     
    323324        $title = apply_filters('widget_title', $instance['title'] );
    324325
    325         // Grab the top posts and display them
    326         $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 
    334326        echo $before_widget;
    335327
     
    338330            echo $before_title . $title . $after_title;
    339331
    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        }
    351382
    352383        echo $after_widget;
     
    364395        $instance['show_views'] = strip_tags( $new_instance['show_views'] );
    365396        $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'] );
    366399       
    367400        // Set default number of posts to display if invalid option
     
    382415        // Default widget settings
    383416        $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
    388423        );
    389424        $instance = wp_parse_args( (array) $instance, $defaults );
     
    405440            <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" />
    406441        </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>
    407453       
     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'); ?>" />
    408457        <p>
    409458            <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' ); ?>" />
    410459            <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>
    411464        </p>
    412465
     
    482535    function upgrade() {
    483536        // Delete old options
    484         delete_option( 'jetpack_post_views' );
    485537        delete_option( 'jetpack-post-views_version' );
    486538        delete_option( 'jetpack_post_views_wp_api_key' );
    487539        delete_option( 'jetpack_post_views_stats_has_run' );
    488540
    489         define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0.3' );
     541        define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0.4' );
    490542    }
    491543
     
    535587
    536588    // 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' ) {
    538590        $Jetpack_Post_Views->upgrade();
    539591    }
     
    546598register_activation_hook( __FILE__, 'jetpack_post_views_on_activation' );
    547599register_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 */
     609function 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  
    44Requires at least: 3.5
    55Tested up to: 3.5.1
    6 Stable tag: 1.0.3
     6Stable tag: 1.0.4
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3636== Frequently asked questions ==
    3737
    38 = Why are there no FAQs besides this one? =
     38= How can I display the top posts in my template? =
    3939
    40 Because you haven't asked one yet.
     40Use 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.
    4160
    4261== Screenshots ==
     
    4867
    4968== 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
    5073
    5174= 1.0.3 (2013-02-10) =
Note: See TracChangeset for help on using the changeset viewer.