Changeset 656170
- Timestamp:
- 01/21/2013 09:21:28 AM (13 years ago)
- Location:
- jetpack-post-views/trunk
- Files:
-
- 1 added
- 2 edited
-
jetpack-post-views.php (modified) (11 diffs)
-
readme.txt (modified) (2 diffs)
-
uninstall.php (added)
Legend:
- Unmodified
- Added
- Removed
-
jetpack-post-views/trunk/jetpack-post-views.php
r656056 r656170 6 6 * Description: Adds a widget that displays your most popular posts using Jetpack stats. 7 7 * Author: Steven Lambert 8 * Version: 1.0. 08 * Version: 1.0.1 9 9 * Author URI: http://sklambert.com 10 10 * License: GPL2+ … … 15 15 global $jetpack_post_view_apiUrlAfterKey; 16 16 $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'; 18 18 19 19 // Define plugin version 20 if ( !defined(' MYPLUGIN_VERSION_KEY' ) ) {20 if ( !defined(' JETPACK_POST_VIEWS_VERSION_KEY' ) ) { 21 21 define( 'JETPACK_POST_VIEWS_VERSION_KEY', 'jetpack-post-views_version' ); 22 22 } 23 if ( !defined( ' MYPLUGIN_VERSION_NUM' ) ) {24 define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0. 0' );23 if ( !defined( 'JETPACK_POST_VIEWS_VERSION_NUM' ) ) { 24 define( 'JETPACK_POST_VIEWS_VERSION_NUM', '1.0.1' ); 25 25 } 26 26 … … 61 61 62 62 echo "<ul>"; 63 foreach( $posts as $post) : setup_postdata($post);?>63 foreach( $posts as $post) { ?> 64 64 <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 65 65 <?php if ( $instance['show_views'] ) { … … 68 68 </li> 69 69 <?php 70 endforeach;70 } 71 71 echo "</ul>"; 72 72 … … 78 78 $instance = $old_instance; 79 79 80 // Check nonce 81 check_admin_referer('jetpack-post-views-widget-form-submition'); 82 80 83 $instance['title'] = strip_tags( $new_instance['title'] ); 81 84 $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'] ); 83 86 $num_posts = intval( strip_tags( $new_instance['num_posts'] ) ); 84 87 … … 104 107 $url = $jetpack_post_view_apiUrlBeforeKey.'?api_key='.$instance['api_key'].'&'.$jetpack_post_view_apiUrlAfterKey; 105 108 $json = file_get_contents( $url ); 106 $data = json_decode( $json );107 108 if ( $data ->views) { // We got data back, so the API Key is correct109 $this->get_stats( $ instance); // Retrieve all post stats and add the post meta109 $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 110 113 111 114 // Validate the key … … 135 138 $defaults = array( 'title' => __('Most Popular Posts', 'jetpack-post-views'), 'api_key' => '', 'valid_key' => false, 'error' => '', 'num_posts' => 5, 'show_views' => false); 136 139 $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 } 137 146 ?> 138 147 <p> … … 164 173 /* 165 174 * GET STATS 166 * Quries the Jetpack API for each postand adds or updates the 'jetpack-post-views' post meta175 * Quries the Jetpack API for all posts and adds or updates the 'jetpack-post-views' post meta 167 176 */ 168 private function get_stats( $ instance) {177 private function get_stats( $postviews ) { 169 178 if ( get_option( 'jetpack_post_views_stats_has_run' ) == "yes" ) { // Only run this function once 170 179 return; … … 172 181 173 182 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 } 180 188 181 189 // Grab all posts and update them 182 190 $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 } 192 197 193 198 update_option( 'jetpack_post_views_stats_has_run', 'yes' ); … … 197 202 /* REGISTER WIDGET */ 198 203 function jetpack_post_views_register_widget() { 199 register_widget( 'Jetpack_Post_Views' ); 204 register_widget( 'Jetpack_Post_Views' ); 200 205 } 201 206 … … 205 210 */ 206 211 function jetpack_post_views_scheduled_update() { 207 global $post;208 global $jetpack_post_view_apiUrlBeforeKey;209 global $jetpack_post_view_apiUrlAfterKey;210 211 212 $apikey = get_option( 'jetpack_post_views_wp_api_key', -1 ); 212 213 213 214 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 214 219 $url = $jetpack_post_view_apiUrlBeforeKey.'?api_key='.$apikey.'&'.$jetpack_post_view_apiUrlAfterKey; 220 221 // Get stats data 215 222 $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 } 217 230 218 231 // Grab all posts and update them 219 232 $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 } 232 239 } 233 240 } -
jetpack-post-views/trunk/readme.txt
r656062 r656170 1 === Jetpack Post Views ===1 === Jetpack Post Views === 2 2 Contributors: straker503 3 3 Tags: jetpack, post views 4 4 Requires at least: 3.5 5 5 Tested up to: 3.5 6 Stable tag: 1.0. 06 Stable tag: 1.0.1 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 50 50 == Changelog == 51 51 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 52 58 = 1.0.0 (2013-01-19) = 53 59 * Public beta released
Note: See TracChangeset
for help on using the changeset viewer.