Plugin Directory

Changeset 656170


Ignore:
Timestamp:
01/21/2013 09:21:28 AM (13 years ago)
Author:
straker503
Message:

1.0.1 (2013-01-21)

  • Reduced number of API calls made
  • Considerably sped up process of adding/updating post meta data to each post
  • Added security to widget
  • Added uninstall.php file
Location:
jetpack-post-views/trunk
Files:
1 added
2 edited

Legend:

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

    r656056 r656170  
    66 * Description: Adds a widget that displays your most popular posts using Jetpack stats.
    77 * Author: Steven Lambert
    8  * Version: 1.0.0
     8 * Version: 1.0.1
    99 * Author URI: http://sklambert.com
    1010 * License: GPL2+
     
    1515global $jetpack_post_view_apiUrlAfterKey;
    1616$jetpack_post_view_apiUrlBeforeKey = 'http://stats.wordpress.com/csv.php';
    17 $jetpack_post_view_apiUrlAfterKey = 'blog_uri='.get_bloginfo( 'wpurl' ).'&table=postviews&days=-1&limit=-1&summarize&format=json&post_id=';
     17$jetpack_post_view_apiUrlAfterKey = 'blog_uri='./*get_bloginfo( 'wpurl' )*/'http://www.blog.sklambert.com'.'&table=postviews&days=-1&limit=-1&summarize&format=json';
    1818
    1919// Define plugin version
    20 if ( !defined(' MYPLUGIN_VERSION_KEY' ) ) {
     20if ( !defined(' JETPACK_POST_VIEWS_VERSION_KEY' ) ) {
    2121    define( 'JETPACK_POST_VIEWS_VERSION_KEY', 'jetpack-post-views_version' );
    2222}
    23 if ( !defined( 'MYPLUGIN_VERSION_NUM' ) ) {
    24     define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0.0' );
     23if ( !defined( 'JETPACK_POST_VIEWS_VERSION_NUM' ) ) {
     24    define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0.1' );
    2525}
    2626
     
    6161
    6262        echo "<ul>";
    63         foreach( $posts as $post) : setup_postdata($post); ?>
     63        foreach( $posts as $post) { ?>
    6464            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    6565                <?php if ( $instance['show_views'] ) {
     
    6868            </li>
    6969        <?php
    70         endforeach;
     70        }
    7171        echo "</ul>";
    7272
     
    7878        $instance = $old_instance;
    7979
     80        // Check nonce
     81        check_admin_referer('jetpack-post-views-widget-form-submition');
     82
    8083        $instance['title'] = strip_tags( $new_instance['title'] );
    8184        $instance['api_key'] = strip_tags( $new_instance['api_key'] );
    82         $instance['show_views'] = $new_instance['show_views'];
     85        $instance['show_views'] = strip_tags( $new_instance['show_views'] );
    8386        $num_posts = intval( strip_tags( $new_instance['num_posts'] ) );
    8487       
     
    104107            $url = $jetpack_post_view_apiUrlBeforeKey.'?api_key='.$instance['api_key'].'&'.$jetpack_post_view_apiUrlAfterKey;
    105108            $json = file_get_contents( $url );
    106             $data = json_decode( $json );
    107 
    108             if ( $data->views ) { // We got data back, so the API Key is correct
    109                 $this->get_stats( $instance ); // Retrieve all post stats and add the post meta
     109            $data = json_decode( $json, true );
     110
     111            if ( $data[0]['postviews'] ) { // We got data back, so the API Key is correct
     112                $this->get_stats( $data[0]['postviews'] ); // Retrieve all post stats and add the post meta
    110113
    111114                // Validate the key
     
    135138        $defaults = array( 'title' => __('Most Popular Posts', 'jetpack-post-views'), 'api_key' => '', 'valid_key' => false, 'error' => '', 'num_posts' => 5, 'show_views' => false);
    136139        $instance = wp_parse_args( (array) $instance, $defaults );
     140
     141        // Set nonce
     142        if ( function_exists('wp_nonce_field') ) {
     143            wp_nonce_field('jetpack-post-views-widget-form-submition');
     144            echo "\n<!-- end of wp_nonce_field -->\n";
     145        }
    137146    ?>
    138147        <p>
     
    164173    /*
    165174    * GET STATS
    166     * Quries the Jetpack API for each post and adds or updates the 'jetpack-post-views' post meta
     175    * Quries the Jetpack API for all posts and adds or updates the 'jetpack-post-views' post meta
    167176    */
    168     private function get_stats( $instance ) {
     177    private function get_stats( $postviews ) {
    169178        if ( get_option( 'jetpack_post_views_stats_has_run' ) == "yes" ) { // Only run this function once
    170179            return;
     
    172181
    173182        global $post;
    174         global $jetpack_post_view_apiUrlBeforeKey;
    175         global $jetpack_post_view_apiUrlAfterKey;
    176 
    177         $url = $jetpack_post_view_apiUrlBeforeKey.'?api_key='.$instance['api_key'].'&'.$jetpack_post_view_apiUrlAfterKey;
    178         $json = file_get_contents( $url );
    179         $data = json_decode( $json );
     183       
     184        // Create a temporary array and save all data
     185        foreach ( $postviews as $postinfo ) {
     186            $postdata[ $postinfo['post_id'] ] = strip_tags( $postinfo['views'] );
     187        }
    180188
    181189        // Grab all posts and update them
    182190        $args = array( 'numberposts' => -1, 'post_type' => 'post', 'post_status' => 'publish');
    183         $posts = get_posts( $args );
    184 
    185         foreach( $posts as $post) : setup_postdata($post); ?><?php
    186             $post_id = $post->ID;
    187             $json = file_get_contents( $url.$post_id );
    188             $data = json_decode( $json );
    189 
    190             add_post_meta( $post_id, 'jetpack-post-views', $data->views, true ) or update_post_meta( $post_id, 'jetpack-post-views', $data->views );
    191         endforeach;
     191        $allposts = get_posts( $args );
     192        foreach( $allposts as $post) {
     193            $views = intval( $postdata[ $post->ID ] );
     194
     195            add_post_meta( $post->ID, 'jetpack-post-views', $views, true ) or update_post_meta( $post->ID, 'jetpack-post-views', $views );
     196        }
    192197
    193198        update_option( 'jetpack_post_views_stats_has_run', 'yes' );
     
    197202/* REGISTER WIDGET */
    198203function jetpack_post_views_register_widget() { 
    199     register_widget( 'Jetpack_Post_Views' ); 
     204    register_widget( 'Jetpack_Post_Views' );
    200205}
    201206
     
    205210*/
    206211function jetpack_post_views_scheduled_update() {
    207     global $post;
    208     global $jetpack_post_view_apiUrlBeforeKey;
    209     global $jetpack_post_view_apiUrlAfterKey;
    210 
    211212    $apikey = get_option( 'jetpack_post_views_wp_api_key', -1 );
    212213
    213214    if ( $apikey != -1 ) { // Ensure we have an API Key set
     215        global $post;
     216        global $jetpack_post_view_apiUrlBeforeKey;
     217        global $jetpack_post_view_apiUrlAfterKey;
     218
    214219        $url = $jetpack_post_view_apiUrlBeforeKey.'?api_key='.$apikey.'&'.$jetpack_post_view_apiUrlAfterKey;
     220
     221        // Get stats data
    215222        $json = file_get_contents( $url );
    216         $data = json_decode( $json );
     223        $data = json_decode( $json, true );
     224        $postviews = $data[0]['postviews'];
     225
     226        // Create a temporary array and save all data
     227        foreach ( $postviews as $postinfo ) {
     228            $postdata[ $postinfo['post_id'] ] = strip_tags( $postinfo['views'] );
     229        }
    217230
    218231        // Grab all posts and update them
    219232        $args = array( 'numberposts' => -1, 'post_type' => 'post', 'post_status' => 'publish');
    220         $posts = get_posts( $args );
    221 
    222         foreach( $posts as $post) : setup_postdata($post); ?><?php
    223             $post_id = $post->ID;
    224             $json = file_get_contents( $url.$post_id );
    225             $data = json_decode( $json );
    226 
    227             $views = get_post_meta( $post_id, 'jetpack-post-views', true );
    228             if ( $views != '' && $data->views > $views ) { // Don't update if there are no new views
    229                 add_post_meta( $post_id, 'jetpack-post-views', $data->views, true ) or update_post_meta( $post_id, 'jetpack-post-views', $data->views );
    230             }
    231         endforeach;
     233        $allposts = get_posts( $args );
     234        foreach( $allposts as $post) {
     235            $views = intval( $postdata[ $post->ID ] );
     236
     237            add_post_meta( $post->ID, 'jetpack-post-views', $views, true ) or update_post_meta( $post->ID, 'jetpack-post-views', $views );
     238        }
    232239    }
    233240}
  • jetpack-post-views/trunk/readme.txt

    r656062 r656170  
    1 === Jetpack Post Views ===
     1=== Jetpack Post Views ===
    22Contributors: straker503
    33Tags: jetpack, post views
    44Requires at least: 3.5
    55Tested up to: 3.5
    6 Stable tag: 1.0.0
     6Stable tag: 1.0.1
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    5050== Changelog ==
    5151
     52= 1.0.1 (2013-01-21) =
     53* Reduced number of API calls made
     54* Considerably sped up process of adding/updating post meta data to each post
     55* Added security to widget
     56* Added uninstall.php file
     57
    5258= 1.0.0 (2013-01-19) =
    5359* Public beta released
Note: See TracChangeset for help on using the changeset viewer.