PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/news/library/extensions/entry-views.php

https://bitbucket.org/lgorence/quickpress
PHP | 194 lines | 49 code | 34 blank | 111 comment | 5 complexity | 8e97b751dc727211cf1c8e9684974082 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Entry Views - A WordPress script for counting post views.
  4. *
  5. * Entry views is a script for calculating the number of views a post gets. It is meant to be basic and
  6. * not a full-featured solution. The idea is to allow theme/plugin authors to quickly load this file and
  7. * build functions on top of it to suit their project needs. This is an AJAX-based solution, so only visitors
  8. * to your site with JavaScript enabled in their browser will update the view count. It is possible to do this
  9. * without AJAX but not recommend (see notes below).
  10. *
  11. * By default, no post types are supported. You have to register support for 'entry-views' for the post types
  12. * you wish to use this extension with.
  13. *
  14. * Not using AJAX: You can call up entry_views_update() at any time and pass it a post ID to update the
  15. * count, but this has problems. Any links with rel="next" or rel="prefetch" will cause some browsers to prefetch
  16. * the data for that particular page. This can cause the view count to be skewed. To try and avoid this
  17. * issue, you need to disable/remove adjacent_posts_rel_link_wp_head(). However, this is not bullet-proof
  18. * as it cannot control links it doesn't know about.
  19. * @link http://core.trac.wordpress.org/ticket/14568
  20. *
  21. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
  22. * General Public License as published by the Free Software Foundation; either version 2 of the License,
  23. * or (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  26. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  27. *
  28. * @package EntryViews
  29. * @version 0.2.2
  30. * @author Justin Tadlock <justin@justintadlock.com>
  31. * @copyright Copyright (c) 2010 - 2012, Justin Tadlock
  32. * @link http://justintadlock.com
  33. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  34. */
  35. /* Add post type support for 'entry-views'. */
  36. add_action( 'init', 'entry_views_post_type_support' );
  37. /* Add the [entry-views] shortcode. */
  38. add_shortcode( 'entry-views', 'entry_views_get' );
  39. /* Registers the entry views extension scripts if we're on the correct page. */
  40. add_action( 'template_redirect', 'entry_views_load' );
  41. /* Add the entry views AJAX actions to the appropriate hooks. */
  42. add_action( 'wp_ajax_entry_views', 'entry_views_update_ajax' );
  43. add_action( 'wp_ajax_nopriv_entry_views', 'entry_views_update_ajax' );
  44. /**
  45. * Adds support for 'entry-views' to the 'post', 'page', and 'attachment' post types (default WordPress
  46. * post types). For all other post types, the theme should explicitly register support for this feature.
  47. *
  48. * @since 0.2.0
  49. * @access private
  50. * @return void
  51. */
  52. function entry_views_post_type_support() {
  53. /* Add support for entry-views to the 'post' post type. */
  54. add_post_type_support( 'post', array( 'entry-views' ) );
  55. /* Add support for entry-views to the 'page' post type. */
  56. add_post_type_support( 'page', array( 'entry-views' ) );
  57. /* Add support for entry-views to the 'attachment' post type. */
  58. add_post_type_support( 'attachment', array( 'entry-views' ) );
  59. }
  60. /**
  61. * Checks if we're on a singular post view and if the current post type supports the 'entry-views'
  62. * extension. If so, set the $post_id variable and load the needed JavaScript.
  63. *
  64. * @since 0.1.0
  65. * @access private
  66. * @return void
  67. */
  68. function entry_views_load() {
  69. global $_entry_views_post_id;
  70. /* Check if we're on a singular post view. */
  71. if ( is_singular() ) {
  72. /* Get the post object. */
  73. $post = get_queried_object();
  74. /* Check if the post type supports the 'entry-views' feature. */
  75. if ( post_type_supports( $post->post_type, 'entry-views' ) ) {
  76. /* Set the post ID for later use because we wouldn't want a custom query to change this. */
  77. $_entry_views_post_id = get_queried_object_id();
  78. /* Enqueue the jQuery library. */
  79. wp_enqueue_script( 'jquery' );
  80. /* Load the entry views JavaScript in the footer. */
  81. add_action( 'wp_footer', 'entry_views_load_scripts' );
  82. }
  83. }
  84. }
  85. /**
  86. * Updates the number of views when on a singular view of a post. This function uses post meta to store
  87. * the number of views per post. By default, the meta key is 'Views', but you can filter this with the
  88. * 'entry_views_meta_key' hook.
  89. *
  90. * @since 0.1.0
  91. * @access public
  92. * @param int $post_id The ID of the post to update the meta for.
  93. * @return void
  94. */
  95. function entry_views_update( $post_id = '' ) {
  96. /* If we're on a singular view of a post, calculate the number of views. */
  97. if ( !empty( $post_id ) ) {
  98. /* Allow devs to override the meta key used. By default, this is 'Views'. */
  99. $meta_key = apply_filters( 'entry_views_meta_key', 'Views' );
  100. /* Get the number of views the post currently has. */
  101. $old_views = get_post_meta( $post_id, $meta_key, true );
  102. /* Add +1 to the number of current views. */
  103. $new_views = absint( $old_views ) + 1;
  104. /* Update the view count with the new view count. */
  105. update_post_meta( $post_id, $meta_key, $new_views, $old_views );
  106. }
  107. }
  108. /**
  109. * Gets the number of views a specific post has. It also doubles as a shortcode, which is called with the
  110. * [entry-views] format.
  111. *
  112. * @since 0.1.0
  113. * @access public
  114. * @param array $attr Attributes for use in the shortcode.
  115. * @return string
  116. */
  117. function entry_views_get( $attr = '' ) {
  118. /* Merge the defaults and the given attributes. */
  119. $attr = shortcode_atts( array( 'before' => '', 'after' => '', 'post_id' => get_the_ID() ), $attr );
  120. /* Allow devs to override the meta key used. */
  121. $meta_key = apply_filters( 'entry_views_meta_key', 'Views' );
  122. /* Get the number of views the post has. */
  123. $views = intval( get_post_meta( $attr['post_id'], $meta_key, true ) );
  124. /* Returns the formatted number of views. */
  125. return $attr['before'] . number_format_i18n( $views ) . $attr['after'];
  126. }
  127. /**
  128. * Callback function hooked to 'wp_ajax_entry_views' and 'wp_ajax_nopriv_entry_views'. It checks the
  129. * AJAX nonce and passes the given $post_id to the entry views update function.
  130. *
  131. * @since 0.1.0
  132. * @access private
  133. * @return void
  134. */
  135. function entry_views_update_ajax() {
  136. /* Check the AJAX nonce to make sure this is a valid request. */
  137. check_ajax_referer( 'entry_views_ajax' );
  138. /* If the post ID is set, set it to the $post_id variable and make sure it's an integer. */
  139. if ( isset( $_POST['post_id'] ) )
  140. $post_id = absint( $_POST['post_id'] );
  141. /* If $post_id isn't empty, pass it to the entry_views_update() function to update the view count. */
  142. if ( !empty( $post_id ) )
  143. entry_views_update( $post_id );
  144. }
  145. /**
  146. * Displays a small script that sends an AJAX request for the page. It passes the $post_id to the AJAX
  147. * callback function for updating the meta.
  148. *
  149. * @since 0.1.0
  150. * @access private
  151. * @return void
  152. */
  153. function entry_views_load_scripts() {
  154. global $_entry_views_post_id;
  155. /* Create a nonce for the AJAX request. */
  156. $nonce = wp_create_nonce( 'entry_views_ajax' );
  157. /* Display the JavaScript needed. */
  158. echo '<script type="text/javascript">/* <![CDATA[ */ jQuery(document).ready( function() { jQuery.post( "' . admin_url( 'admin-ajax.php' ) . '", { action : "entry_views", _ajax_nonce : "' . $nonce . '", post_id : ' . $_entry_views_post_id . ' } ); } ); /* ]]> */</script>' . "\n";
  159. }
  160. ?>