Plugin Directory

Changeset 716369


Ignore:
Timestamp:
05/22/2013 03:22:45 AM (13 years ago)
Author:
straker503
Message:

1.0.5 (2013-05-21)

  • Fixed posts not properly displaying post views
  • Fixed database access to tables using 'wp_' table prefix to use prefix defined in wp-config.php
  • Added shortcode
Location:
jetpack-post-views
Files:
1 added
2 edited

Legend:

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

    r681931 r716369  
    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.4
     8 * Version: 1.0.5
    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.4' );
     15    define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0.5' );
    1616}
    1717
     
    3434        // Admin hooks
    3535        add_action( 'admin_init',                          array( &$this, 'register_setting' ) );
    36         add_action( 'admin_menu',                          array( &$this, 'register_settings_page' ) ); 
    37         add_filter( 'plugin_action_links',                 array( &$this, 'settings_link' ), 10, 2 );   
     36        add_action( 'admin_menu',                          array( &$this, 'register_settings_page' ) );
     37        add_filter( 'plugin_action_links',                 array( &$this, 'settings_link' ), 10, 2 );
    3838
    3939        // Shecudled hooks
    4040        add_action( 'jetpack_post_views_scheduled_update', array( &$this, 'get_post_views' ) );
    4141
     42        // Post hooks
     43        add_action( 'publish_post',                        array( &$this, 'add_jetpack_meta' ) );
     44
    4245        // Query the database for the blog_id
    4346        global $wpdb;
    44         $stats_options = $wpdb->get_var( "SELECT option_value
    45                                           FROM   wp_options
     47        $options_table = $wpdb->base_prefix."options";
     48        $stats_options = $wpdb->get_var( "SELECT option_value
     49                                          FROM   $options_table
    4650                                          WHERE  option_name = 'stats_options'" );
    4751        $stats = unserialize($stats_options);
     
    5458
    5559            // Try another possiblility to get the blog_id
    56             $jetpack_options = $wpdb->get_var( "SELECT option_value 
    57                                                 FROM   wp_options
     60            $jetpack_options = $wpdb->get_var( "SELECT option_value
     61                                                FROM   $options_table
    5862                                                WHERE  option_name = 'jetpack_options'" );
    5963            $jetpack = unserialize($jetpack_options);
     
    6266                $this->blog_id = $jetpack['id'];
    6367            }
    64             else { // Jetpack stats really unabailable
     68            else { // Jetpack stats really unavailable
    6569                $this->blog_id = -1;
    6670            }
     
    107111    function register_setting() {
    108112        register_setting( 'jetpack_post_views_settings', 'jetpack_post_views_settings', array( &$this, 'validate_settings' ) );
     113    }
     114
     115
     116    /* ADD JETPACK POST META ON POST PUBLISH */
     117    function add_jetpack_meta() {
     118        global $post;
     119        add_post_meta( $post->ID, 'jetpack-post-views', 0, true );
    109120    }
    110121
     
    348359                    <li><a href="<?php echo get_permalink( $post['post_id'] ) ?>"><?php echo get_the_title( $post['post_id'] ) ?></a>
    349360                        <?php if ( $instance['show_views'] ) {
    350                             echo " - ".number_format_i18n( $post['views'] )." views";
     361                            echo " - ".number_format_i18n( $post['views'] ? $post['views'] : 0 )." views";
    351362                        } ?>
    352363                    </li>
     
    513524            foreach( $allposts as $post) {
    514525                // Ensure that the $post-ID exists as a key in the array before trying to update post information.
    515                 // This prevents the uers from pulling data from another website and trying to add it to their own
     526                // This prevents the user from pulling data from another website and trying to add it to their own
    516527                // posts.
    517528                if ( array_key_exists( $post->ID, $postdata) ) {
     
    525536                    }
    526537                    else {
    527                         add_post_meta( $post->ID, 'jetpack-post-views', $newViews, true );
     538                        add_post_meta( $post->ID, 'jetpack-post-views', ($newViews ? $newViews: 0), true );
    528539                    }
     540                }
     541                else {
     542                    add_post_meta( $post->ID, 'jetpack-post-views', 0, true );
    529543                }
    530544            }
     
    534548    /* UPGRADE PLUGIN TO NEW VERSION */
    535549    function upgrade() {
     550        global $post;
     551
    536552        // Delete old options
    537553        delete_option( 'jetpack-post-views_version' );
     
    539555        delete_option( 'jetpack_post_views_stats_has_run' );
    540556
    541         define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0.4' );
     557        // Add post meta for each post
     558        $args = array( 'numberposts' => -1, 'post_type' => 'post', 'post_status' => 'publish');
     559        $allposts = get_posts( $args );
     560        foreach( $allposts as $post) {
     561            $views = get_post_meta( $post->ID, 'jetpack-post-views', true );
     562
     563            if (!$views) {
     564                add_post_meta( $post->ID, 'jetpack-post-views', 0, true );
     565            }
     566        }
     567
     568        define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0.5' );
    542569    }
    543570
     
    587614
    588615    // Upgrade the plugin if necessary
    589     if ( JETPACK_POST_VIEWS_VERSION_NUM != '1.0.4' ) {
     616    if ( JETPACK_POST_VIEWS_VERSION_NUM != '1.0.5' ) {
    590617        $Jetpack_Post_Views->upgrade();
    591618    }
     
    626653                <li><a href="<?php echo get_permalink( $post['post_id'] ) ?>"><?php echo get_the_title( $post['post_id'] ) ?></a>
    627654                    <?php if ( $args['displayViews'] ) {
    628                         echo " - ".number_format_i18n( $post['views'] )." views";
     655                        echo " - ".number_format_i18n( $post['views'] ? $post['views'] : 0 )." views";
    629656                    } ?>
    630657                </li>
     
    636663    }
    637664}
     665
     666/*
     667 * SHORTCODE
     668 * Use the Jetpack stats_get_csv() function to create a list of the top posts
     669 * atts: days         - number of days of the desired time frame. '-1' means unlimited
     670 *       limit        - number of posts to display. '-1' means unlimited. If days is -1, then limit is capped at 500
     671 *       exclude      - comma-separated list of post IDs to exclude form displaying
     672 *       displayviews - flag to display the post views
     673 */
     674add_shortcode( 'jpv', 'JPV_shortcode' );
     675function JPV_shortcode( $atts ) {
     676    extract( shortcode_atts( array(
     677        'days' => '-1',
     678        'limit' => '5',
     679        'exclude' => '',
     680        'displayviews' => false,
     681    ), $atts ) );
     682
     683    JPV_display_top_posts( $args = array( 'days' => $days, 'limit' => $limit, 'exclude' => $exclude, 'displayViews' => $displayviews ) );
     684}
  • jetpack-post-views/trunk/readme.txt

    r681940 r716369  
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
    99
    10 A plugin that displays your most popular posts using Jetpack stats.
     10Displays your most popular posts using Jetpack stats.
    1111
    1212== Description ==
     
    6161**NOTE** This function only works if the function `stats_get_csv()` exists. If this function is not working probably, it is probably due to the `stats_get_csv()` function not returning the needed results.
    6262
     63= How can I display the top posts in my posts? =
     64
     65Use the shortcode '[jpv]'
     66
     67The shotcode uses the same parameters as the 'JPV_display_top_posts()' function
     68
    6369== Screenshots ==
    6470
     
    6975
    7076== Changelog ==
     77
     78= 1.0.5 (2013-05-21) =
     79* Fixed posts not properly displaying post views
     80* Fixed database access to tables using 'wp_' table prefix to use prefix defined in wp-config.php
     81* Added shortcode
    7182
    7283= 1.0.4 (2013-03-14) =
Note: See TracChangeset for help on using the changeset viewer.