PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/jannah/framework/functions/post-views.php

https://bitbucket.org/satheesh_krossark/news
PHP | 286 lines | 142 code | 73 blank | 71 comment | 49 complexity | 94a8c4f2fba4770407862dd4d84796e7 MD5 | raw file
  1. <?php
  2. /**
  3. * Post views module
  4. *
  5. * @package Jannah
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  8. /*-----------------------------------------------------------------------------------*/
  9. # Post Views Class
  10. /*-----------------------------------------------------------------------------------*/
  11. if( ! class_exists( 'TIE_POST_VIEWS' )){
  12. class TIE_POST_VIEWS{
  13. /**
  14. * __construct
  15. *
  16. * Class constructor where we will call our filter and action hooks.
  17. */
  18. function __construct(){
  19. add_filter( 'jannah_views_meta_field', array( $this, '_custom_views_meta_field' ));
  20. add_filter( 'manage_posts_columns', array( $this, '_posts_column_views' ));
  21. add_filter( 'manage_edit-post_sortable_columns', array( $this, '_sort_postviews_column' ));
  22. add_action( 'manage_posts_custom_column', array( $this, '_posts_custom_column_views' ), 5, 2 );
  23. add_action( 'pre_get_posts', array( $this, '_sort_postviews' ));
  24. add_action( 'wp_head', array( $this, '_set_post_views' ));
  25. add_action( 'wp_enqueue_scripts', array( $this, '_postview_cache_enqueue' ), 20 );
  26. add_action( 'wp_ajax_tie_postviews', array( $this, '_increment_views' ));
  27. add_action( 'wp_ajax_nopriv_tie_postviews', array( $this, '_increment_views' ));
  28. }
  29. /**
  30. * _set_post_views
  31. *
  32. * Count number of views
  33. */
  34. function _set_post_views(){
  35. # Run only if the post views option is set to THEME's post views module ----------
  36. if( jannah_get_option( 'tie_post_views' ) != 'theme' || ! is_single() || jannah_is_bot() ){
  37. return;
  38. }
  39. # Run only on the first page of the post ----------
  40. $page = get_query_var( 'paged', 1 );
  41. if( $page > 1 ){
  42. return false;
  43. }
  44. # Increase number of views +1 ----------
  45. $count = 0;
  46. $post_id = get_the_ID();
  47. $count_key = apply_filters( 'jannah_views_meta_field', 'tie_views' );
  48. $count = (int) get_post_meta( $post_id, $count_key, true );
  49. # The Starter Number ----------
  50. if( ( empty( $count ) || $count == 0 ) && jannah_get_option( 'views_starter_number' ) ){
  51. $count = (int) jannah_get_option( 'views_starter_number' );
  52. }
  53. if( ! defined( 'WP_CACHE' ) || ! WP_CACHE ){
  54. $count++;
  55. update_post_meta( $post_id, $count_key, (int)$count );
  56. }
  57. }
  58. /**
  59. * _postview_cache_enqueue
  60. *
  61. * Calculate Post Views With WP_CACHE Enabled
  62. */
  63. function _postview_cache_enqueue(){
  64. # Run only if the post views option is set to THEME's post views module ----------
  65. if( jannah_get_option( 'tie_post_views' ) != 'theme' ){
  66. return;
  67. }
  68. # Add the js code ----------
  69. if ( is_singular( 'post' ) && ( defined( 'WP_CACHE' ) && WP_CACHE ) && jannah_get_option( 'tie_post_views' ) ){
  70. $cache_js = '
  71. jQuery.ajax({
  72. type : "GET",
  73. url : "'. esc_url( admin_url('admin-ajax.php') ) .'",
  74. data : "postviews_id='. get_the_ID() .'&action=tie_postviews",
  75. cache: !1
  76. });
  77. ';
  78. jannah_add_inline_script( 'jannah-scripts', $cache_js );
  79. }
  80. }
  81. /**
  82. * _increment_views
  83. *
  84. * Increment Post Views With WP_CACHE Enabled
  85. */
  86. function _increment_views(){
  87. # Run only if the post views option is set to THEME's post views module ----------
  88. if( jannah_get_option( 'tie_post_views' ) != 'theme' || jannah_is_bot() ){
  89. return;
  90. }
  91. # Increase number of views +1 ----------
  92. if( ! empty( $_GET['postviews_id'] ) && jannah_get_option( 'tie_post_views' ) && defined( 'WP_CACHE' ) && WP_CACHE ){
  93. $post_id = intval($_GET['postviews_id']);
  94. if( $post_id > 0 ){
  95. $count = 0;
  96. $count_key = apply_filters( 'jannah_views_meta_field', 'tie_views' );
  97. $count = (int) get_post_meta( $post_id, $count_key, true );
  98. # The Starter Number ----------
  99. if( ( empty( $count ) || $count == 0 ) && jannah_get_option( 'views_starter_number' ) ){
  100. $count = (int) jannah_get_option( 'views_starter_number' );
  101. }
  102. $count++;
  103. update_post_meta( $post_id, $count_key, (int)$count );
  104. echo esc_html( $count );
  105. }
  106. }
  107. exit();
  108. }
  109. /**
  110. * _custom_views_meta_field
  111. *
  112. * Custom meta_field name
  113. */
  114. function _custom_views_meta_field( $field ){
  115. return jannah_get_option( 'views_meta_field' ) ? jannah_get_option( 'views_meta_field' ) : $field;
  116. }
  117. /**
  118. * _posts_column_views
  119. *
  120. * Dashboared column title
  121. */
  122. function _posts_column_views( $defaults ){
  123. # Run only if the post views option is set to THEME's post views module ----------
  124. if( jannah_get_option( 'tie_post_views' ) == 'theme' ){
  125. $defaults['tie_post_views'] = _ti( 'Views' );
  126. }
  127. return $defaults;
  128. }
  129. /**
  130. * _posts_custom_column_views
  131. *
  132. * Dashboared column content
  133. */
  134. function _posts_custom_column_views( $column_name, $id ){
  135. # Run only if the post views option is set to THEME's post views module ----------
  136. if( jannah_get_option( 'tie_post_views' ) != 'theme' ){
  137. return;
  138. }
  139. if( $column_name === 'tie_post_views' ){
  140. echo jannah_views( '', get_the_ID() );
  141. }
  142. }
  143. /**
  144. * _sort_postviews_column
  145. *
  146. * Sort Post views column in the dashboared
  147. */
  148. function _sort_postviews_column( $defaults ){
  149. # Run only if the post views option is set to THEME's post views module ----------
  150. if( jannah_get_option( 'tie_post_views' ) == 'theme' ){
  151. $defaults['tie_post_views'] = 'tie-views';
  152. }
  153. return $defaults;
  154. }
  155. /**
  156. * _sort_postviews
  157. *
  158. * Sort Post views in the dashboared
  159. */
  160. function _sort_postviews( $query ) {
  161. if( ! is_admin() ){
  162. return;
  163. }
  164. $orderby = $query->get('orderby');
  165. $count_key = apply_filters( 'jannah_views_meta_field', 'tie_views' );
  166. if( $orderby == 'tie-views' ) {
  167. $query->set( 'meta_key', $count_key );
  168. $query->set( 'orderby', 'meta_value_num' );
  169. }
  170. }
  171. }
  172. # Instantiate the class ----------
  173. new TIE_POST_VIEWS();
  174. }
  175. /*-----------------------------------------------------------------------------------*/
  176. # Display number of views
  177. /*-----------------------------------------------------------------------------------*/
  178. if( ! function_exists( 'jannah_views' )){
  179. function jannah_views( $text = '', $post_id = 0 ){
  180. # Return if thr post views module is disabled ----------
  181. $post_views_type = jannah_get_option( 'tie_post_views' );
  182. if( ! $post_views_type ){
  183. return;
  184. }
  185. if( empty( $post_id )){
  186. $post_id = get_the_ID();
  187. }
  188. $views_class = '';
  189. $formated = $count = 0;
  190. # Jetpack plugin by Automattic ----------
  191. if( $post_views_type == 'jetpack' && JANNAH_JETPACK_IS_ACTIVE ){
  192. $count = jannah_jetpack_get_post_views( $post_id );
  193. }
  194. else{
  195. $count_key = apply_filters( 'jannah_views_meta_field', 'tie_views' );
  196. $count = get_post_meta( $post_id, $count_key, true );
  197. $count = empty( $count ) ? 0 : $count;
  198. }
  199. $formated = number_format_i18n( $count );
  200. if( jannah_get_option( 'views_colored' ) ){
  201. if( $count > jannah_get_option( 'views_veryhot_color', 5000 ) ){
  202. $views_class = 'very-hot';
  203. }
  204. elseif( $count > jannah_get_option( 'views_hot_color', 2000 ) ){
  205. $views_class = 'hot';
  206. }
  207. elseif( $count > jannah_get_option( 'views_warm_color', 500 ) ){
  208. $views_class = 'warm';
  209. }
  210. }
  211. return '<span class="meta-views meta-item '. $views_class .'"><span class="tie-icon-fire" aria-hidden="true"></span> '.$formated.' '.$text.'</span> ';
  212. }
  213. }