PageRenderTime 75ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/hybrid.bak/library/extensions/entry-views.php

https://github.com/agilecentrix/Centrix
PHP | 162 lines | 45 code | 29 blank | 88 comment | 5 complexity | 3c3498e96802dc696a97eb71b37b1fee MD5 | raw file
  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 version 2, as published by the Free Software Foundation. You may NOT assume
  23. * that you can use any other version of the GPL.
  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.1.1
  30. * @author Justin Tadlock <justin@justintadlock.com>
  31. * @copyright Copyright (c) 2010, Justin Tadlock
  32. * @link http://justintadlock.com
  33. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  34. */
  35. /* Add the [entry-views] shortcode. */
  36. add_shortcode( 'entry-views', 'entry_views_get' );
  37. /* Registers the entry views extension scripts if we're on the correct page. */
  38. add_action( 'template_redirect', 'entry_views_load' );
  39. /* Add the entry views AJAX actions to the appropriate hooks. */
  40. add_action( 'wp_ajax_entry_views', 'entry_views_update_ajax' );
  41. add_action( 'wp_ajax_nopriv_entry_views', 'entry_views_update_ajax' );
  42. /**
  43. * Checks if we're on a singular post view and if the current post type supports the 'entry-views'
  44. * extension. If so, set the $post_id variable and load the needed JavaScript.
  45. *
  46. * @since 0.1
  47. */
  48. function entry_views_load() {
  49. global $wp_query, $entry_views;
  50. /* Check if we're on a singular post view. */
  51. if ( is_singular() ) {
  52. /* Get the post object. */
  53. $post = $wp_query->get_queried_object();
  54. /* Check if the post type supports the 'entry-views' feature. */
  55. if ( post_type_supports( $post->post_type, 'entry-views' ) ) {
  56. /* Set the post ID for later use because we wouldn't want a custom query to change this. */
  57. $entry_views->post_id = $post->ID;
  58. /* Enqueue the jQuery library. */
  59. wp_enqueue_script( 'jquery' );
  60. /* Load the entry views JavaScript in the footer. */
  61. add_action( 'wp_footer', 'entry_views_load_scripts' );
  62. }
  63. }
  64. }
  65. /**
  66. * Updates the number of views when on a singular view of a post. This function uses post meta to store
  67. * the number of views per post. By default, the meta key is 'Views', but you can filter this with the
  68. * 'entry_views_meta_key' hook.
  69. *
  70. * @since 0.1
  71. */
  72. function entry_views_update( $post_id = '' ) {
  73. global $wp_query;
  74. /* If we're on a singular view of a post, calculate the number of views. */
  75. if ( !empty( $post_id ) ) {
  76. /* Allow devs to override the meta key used. By default, this is 'Views'. */
  77. $meta_key = apply_filters( 'entry_views_meta_key', 'Views' );
  78. /* Get the number of views the post currently has. */
  79. $old_views = get_post_meta( $post_id, $meta_key, true );
  80. /* Add +1 to the number of current views. */
  81. $new_views = absint( $old_views ) + 1;
  82. /* Update the view count with the new view count. */
  83. update_post_meta( $post_id, $meta_key, $new_views, $old_views );
  84. }
  85. }
  86. /**
  87. * Gets the number of views a specific post has. It also doubles as a shortcode, which is called with the
  88. * [entry-views] format.
  89. *
  90. * @since 0.1
  91. * @param array $attr Attributes for use in the shortcode.
  92. */
  93. function entry_views_get( $attr = '' ) {
  94. global $post;
  95. /* Merge the defaults and the given attributes. */
  96. $attr = shortcode_atts( array( 'before' => '', 'after' => '', 'post_id' => $post->ID ), $attr );
  97. /* Allow devs to override the meta key used. */
  98. $meta_key = apply_filters( 'entry_views_meta_key', 'Views' );
  99. /* Get the number of views the post has. */
  100. $views = intval( get_post_meta( $attr['post_id'], $meta_key, true ) );
  101. /* Returns the formatted number of views. */
  102. return $attr['before'] . number_format_i18n( $views ) . $attr['after'];
  103. }
  104. /**
  105. * Callback function hooked to 'wp_ajax_entry_views' and 'wp_ajax_nopriv_entry_views'. It checks the
  106. * AJAX nonce and passes the given $post_id to the entry views update function.
  107. *
  108. * @since 0.1
  109. */
  110. function entry_views_update_ajax() {
  111. /* Check the AJAX nonce to make sure this is a valid request. */
  112. check_ajax_referer( 'entry_views_ajax' );
  113. /* If the post ID is set, set it to the $post_id variable and make sure it's an integer. */
  114. if ( isset( $_POST['post_id'] ) )
  115. $post_id = absint( $_POST['post_id'] );
  116. /* If $post_id isn't empty, pass it to the entry_views_update() function to update the view count. */
  117. if ( !empty( $post_id ) )
  118. entry_views_update( $post_id );
  119. }
  120. /**
  121. * Displays a small script that sends an AJAX request for the page. It passes the $post_id to the AJAX
  122. * callback function for updating the meta.
  123. *
  124. * @since 0.1
  125. */
  126. function entry_views_load_scripts() {
  127. global $entry_views;
  128. /* Create a nonce for the AJAX request. */
  129. $nonce = wp_create_nonce( 'entry_views_ajax' );
  130. /* Display the JavaScript needed. */
  131. 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";
  132. }
  133. ?>