PageRenderTime 69ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/content/plugins/baw-post-views-count/bawpv.php

https://gitlab.com/jshimko/pbfuniversity
PHP | 568 lines | 497 code | 51 blank | 20 comment | 64 complexity | 08aefe7823f93cf0b5d14d1966b2cbfc MD5 | raw file
  1. <?php
  2. /* Plugin Name: BAW Post Views Count
  3. * Plugin URI: http://www.boiteaweb.fr/pvc
  4. * Description: Count views for post and pages, shortcode [post_view] and [most_view] available, widget "Most Viewed Posts" available.
  5. * Version: 2.19.11
  6. * Author: Juliobox
  7. * Author URI: http://www.boiteaweb.fr
  8. * License: GPLv2
  9. **/
  10. DEFINE( 'BAWPVC_FULLNAME', 'Post Views Count' );
  11. DEFINE( 'BAWPVC_VERSION', '2.19.10' );
  12. add_action( 'wp_head', 'baw_pvc_main' );
  13. function baw_pvc_main()
  14. {
  15. global $post, $bawpvc_options;
  16. $bots = array( 'wordpress', 'googlebot', 'google', 'msnbot', 'ia_archiver', 'lycos', 'jeeves', 'scooter', 'fast-webcrawler', 'slurp@inktomi', 'turnitinbot', 'technorati',
  17. 'yahoo', 'findexa', 'findlinks', 'gaisbo', 'zyborg', 'surveybot', 'bloglines', 'blogsearch', 'pubsub', 'syndic8', 'userland', 'gigabot', 'become.com' );
  18. if( !( ( $bawpvc_options['no_members']=='on' && is_user_logged_in() ) || ( $bawpvc_options['no_admins']=='on' && current_user_can( 'administrator' ) ) ) &&
  19. !empty( $_SERVER['HTTP_USER_AGENT'] ) &&
  20. is_singular( $bawpvc_options['post_types'] ) &&
  21. !preg_match( '/' . implode( '|', $bots ) . '/i', $_SERVER['HTTP_USER_AGENT'] )
  22. )
  23. {
  24. global $timings;
  25. $IP = substr( md5( getenv( 'HTTP_X_FORWARDED_FOR' ) ? getenv( 'HTTP_X_FORWARDED_FOR' ) : getenv( 'REMOTE_ADDR' ) ), 0, 16 );
  26. $time_to_go = $bawpvc_options['time']; // Default: no time between count
  27. if( (int)$time_to_go == 0 || !get_transient( 'baw_count_views-' . $IP . $post->ID ) ) {
  28. foreach( $timings as $time=>$date )
  29. {
  30. if( $time != 'all' )
  31. $date = '-' . date( $date );
  32. // Filtered meta key name
  33. $meta_key_filtered = apply_filters( 'baw_count_views_meta_key', '_count-views_' . $time . $date, $time, $date );
  34. $count = (int)get_post_meta( $post->ID, $meta_key_filtered, true );
  35. $count++;
  36. update_post_meta( $post->ID, $meta_key_filtered, $count );
  37. // Normal meta key name
  38. $meta_key = '_count-views_' . $time . $date;
  39. if( $meta_key_filtered != $meta_key ):
  40. $count = (int)get_post_meta( $post->ID, $meta_key, true );
  41. $count++;
  42. update_post_meta( $post->ID, $meta_key, $count );
  43. endif;
  44. //// I update 2 times with 2 different meta names because i need to keep my own count too, in bonus of hacked/filtered count.
  45. }
  46. if( (int)$time_to_go > 0 )
  47. set_transient( 'baw_count_views-' . $IP . $post->ID, $IP, $time_to_go );
  48. }
  49. }
  50. }
  51. add_shortcode( 'post_view', 'bawpvc_views_sc' );
  52. add_shortcode( 'post_views', 'bawpvc_views_sc' );
  53. function bawpvc_views_sc( $atts, $content = null )
  54. {
  55. global $post;
  56. extract(shortcode_atts(array(
  57. "id" => isset( $post->ID ) ? (int)$post->ID : 0,
  58. "time" => 'all',
  59. "date" => '' //YYYYMMDD format
  60. ), $atts));
  61. if( (int)$id > 0 ) {
  62. global $timings, $time_shortcuts;
  63. $date = $date != '' ? $date : date( $timings[$time] );
  64. $date = $time == 'all' ? '' : '-' . $date;
  65. $meta_key = apply_filters( 'baw_count_views_meta_key', '_count-views_' . $time . $date, $time, $date );
  66. $count = (int)get_post_meta( $id, $meta_key, true );
  67. do_action( 'baw_count_views_count_action', $count, $meta_key, $time, $date, $id );
  68. $count = apply_filters( 'baw_count_views_count', $count, $meta_key, $time, $date, $id );
  69. return $count;
  70. }
  71. return '';
  72. }
  73. add_filter( 'baw_count_views_count', 'bawpvc_format', 1 );
  74. function bawpvc_format( $count )
  75. {
  76. global $bawpvc_options;
  77. return sprintf( str_replace( '%count%', '%d', $bawpvc_options['format'] ), $count );
  78. }
  79. function bawmrp_wp_dropdown_cats( $output )
  80. {
  81. $output = str_replace( '<select ', '<select size="4" multiple ', $output );
  82. $output = str_replace( '[exclude_cat]"', '[exclude_cat][]"', $output );
  83. return $output;
  84. }
  85. class WP_Widget_Most_Viewed_Posts extends WP_Widget {
  86. function __construct() {
  87. $widget_ops = array('classname' => 'widget_most_viewed_entries', 'description' => __( 'The most viewed posts on your site', 'bawpvc' ) );
  88. parent::__construct('most-viewed-posts', __( 'Most Viewed Posts', 'bawpvc' ), $widget_ops);
  89. $this->alt_option_name = 'widget_most_viewed_entries';
  90. add_action( 'save_post', array(&$this, 'flush_widget_cache') );
  91. add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
  92. add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
  93. }
  94. function widget($args, $instance) {
  95. $cache = wp_cache_get('widget_most_viewed_entries', 'widget');
  96. if ( !is_array($cache) )
  97. $cache = array();
  98. if ( ! isset( $args['widget_id'] ) )
  99. $args['widget_id'] = $this->id;
  100. if ( isset( $cache[ $args['widget_id'] ] ) ) {
  101. echo $cache[ $args['widget_id'] ];
  102. return;
  103. }
  104. ob_start();
  105. extract($args);
  106. $title = apply_filters('widget_title', empty($instance['title']) ? __('Most Viewed Posts') : $instance['title'], $instance, $this->id_base);
  107. if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
  108. $number = 10;
  109. global $timings;
  110. $date = $instance['date'] != '' ? $instance['date'] : date( $timings[$instance['time']] );
  111. $date = $instance['time'] == 'all' ? '' : '-' . $date;
  112. $time = $instance['time'];
  113. $exclude_cat = $instance['exclude_cat'];
  114. $order = $instance['order'] == 'ASC' ? 'ASC' : 'DESC';
  115. $author_id = $instance['author'];
  116. $meta_key = apply_filters( 'baw_count_views_meta_key', '_count-views_' . $time . $date, $time, $date );
  117. global $bawpvc_options;
  118. $r = new WP_Query( array( 'posts_per_page' => $number,
  119. 'no_found_rows' => true,
  120. 'post_status' => 'publish',
  121. 'ignore_sticky_posts' => true,
  122. 'meta_key' => $meta_key, 'meta_value_num' => '0',
  123. 'meta_compare' => '>',
  124. 'orderby'=>'meta_value_num',
  125. 'order'=>$order,
  126. 'author'=>$author_id,
  127. 'category__not_in'=>$exclude_cat,
  128. 'post_type'=> apply_filters( 'baw_count_views_widget_post_types', $bawpvc_options['post_types'] )
  129. )
  130. );
  131. if ($r->have_posts()) :
  132. ?>
  133. <?php echo $before_widget; ?>
  134. <?php if ( $title ) echo $before_title . $title . $after_title; ?>
  135. <ul>
  136. <?php while ($r->have_posts()) : $r->the_post(); ?>
  137. <?php
  138. $count = '';
  139. if( $instance['show'] ):
  140. $count = (int)get_post_meta( get_the_ID(), $meta_key, true );
  141. do_action( 'baw_count_views_count_action', $count, $meta_key, $time, $date, get_the_ID() );
  142. $count = apply_filters( 'baw_count_views_count', $count, $meta_key, $time, $date );
  143. endif;
  144. ?>
  145. <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); echo $count; ?></a></li>
  146. <?php endwhile; ?>
  147. </ul>
  148. <?php echo $after_widget; ?>
  149. <?php
  150. // Reset the global $the_post as this query will have stomped on it
  151. wp_reset_postdata();
  152. endif;
  153. $cache[$args['widget_id']] = ob_get_flush();
  154. wp_cache_set('widget_most_viewed_entries', $cache, 'widget');
  155. }
  156. function update( $new_instance, $old_instance ) {
  157. $instance = $old_instance;
  158. $instance['exclude_cat'] = $new_instance['exclude_cat'];
  159. $instance['title'] = strip_tags($new_instance['title']);
  160. $instance['author'] = $new_instance['author'];
  161. $instance['time'] = $new_instance['time'];
  162. $instance['date'] = $new_instance['date'];
  163. $instance['number'] = (int) $new_instance['number'];
  164. $instance['show'] = (bool)$new_instance['show'];
  165. $instance['order'] = $new_instance['order'] == 'ASC' ? 'ASC' : 'DESC';
  166. $this->flush_widget_cache();
  167. $alloptions = wp_cache_get( 'alloptions', 'options' );
  168. if ( isset($alloptions['widget_most_viewed_entries']) )
  169. delete_option('widget_most_viewed_entries');
  170. return $instance;
  171. }
  172. function flush_widget_cache() {
  173. wp_cache_delete('widget_most_viewed_entries', 'widget');
  174. }
  175. function form( $instance ) {
  176. $exclude_cat = isset($instance['exclude_cat']) ? $instance['exclude_cat'] : '';
  177. $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  178. $number = isset($instance['number']) ? absint($instance['number']) : 5;
  179. $time = isset($instance['time']) ? ($instance['time']) : 'all';
  180. $author_id = isset($instance['author']) ? ($instance['author']) : '';
  181. $date = isset($instance['date']) ? ($instance['date']) : '';
  182. $show = isset($instance['show']) ? $instance['show'] == 'on' : true;
  183. if( isset( $instance['order'] ) )
  184. $order = $new_instance['order'] == 'ASC' ? 'ASC' : 'DESC';
  185. else
  186. $order = 'DESC';
  187. ?>
  188. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'bawpvc' ); ?></label>
  189. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  190. <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e( 'How many posts:', 'bawpvc' ); ?></label>
  191. <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
  192. <p><label for="<?php echo $this->get_field_id('time'); ?>"><?php _e( 'Which top do you want:', 'bawpvc' ); ?></label>
  193. <select id="<?php echo $this->get_field_id('time'); ?>" name="<?php echo $this->get_field_name('time'); ?>">
  194. <?php global $timings;
  195. foreach( $timings as $timing=>$dummy ) { ?>
  196. <option value="<?php echo esc_attr( $timing ); ?>" <?php selected( $timing, $time ); ?>><?php echo ucwords( esc_html( $timing ) ); ?></option>
  197. <?php } ?>
  198. </select>
  199. <p><label for="<?php echo $this->get_field_id('author'); ?>"><?php _e( 'Top for this author only:', 'bawpvc' ); ?></label>
  200. <select id="<?php echo $this->get_field_id('author'); ?>" name="<?php echo $this->get_field_name('author'); ?>">
  201. <option value=""><?php _e( 'All authors', 'bawpvc' ); ?></option>
  202. <?php foreach( get_users() as $u ) { ?>
  203. <option value="<?php echo $u->ID; ?>" <?php selected( $author_id, $u->ID ); ?>><?php echo ucwords( esc_html( $u->display_name ) ); ?></option>
  204. <?php } ?>
  205. </select>
  206. <?php /* /// soon
  207. <p><label for="<?php echo $this->get_field_id('author'); ?>"><?php _e( 'Exclude categories: (Multiple choise possible)', 'bawpvc' ); ?></label>
  208. <?php add_filter( 'wp_dropdown_cats', 'bawmrp_wp_dropdown_cats' ); ?>
  209. <?php wp_dropdown_categories( array( 'name'=>$this->get_field_name('exclude_cat').'[]' ) ); //// ?>
  210. <?php remove_filter( 'wp_dropdown_cats', 'bawmrp_wp_dropdown_cats' ); ?>
  211. <?php print_r( $exclude_cat ); ?>
  212. */ ?>
  213. <p><label for="<?php echo $this->get_field_id('date'); ?>"><?php _e( 'Date format', 'bawpvc' ); ?> <code>YYYYMMAA</code></label>
  214. <input id="<?php echo $this->get_field_id('date'); ?>" name="<?php echo $this->get_field_name('date'); ?>" type="text" value="<?php echo esc_attr( $date ); ?>" size="6" maxlength="8" /><br />
  215. <code><?php _e( 'If you leave blank the actual time will be used.', 'bawpvc' ); ?></code></p>
  216. <p><label for="<?php echo $this->get_field_id('show'); ?>"><?php _e( 'Show posts count:', 'bawpvc' ); ?></label>
  217. <input id="<?php echo $this->get_field_id('show'); ?>" name="<?php echo $this->get_field_name('show'); ?>" type="checkbox" <?php checked( $show == true, true ); ?> /></p>
  218. <p><label for="<?php echo $this->get_field_id('order'); ?>"><?php _e( 'Order', 'bawpvc' ); ?></label>
  219. <select id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>">
  220. <option value="DESC" <?php selected( $order, 'DESC' ); ?>><?php _e( 'From most viewed to less viewed', 'bawpvc' ); ?></option>
  221. <option value="ASC" <?php selected( $order, 'ASC' ); ?>><?php _e( 'From less viewed to most viewed', 'bawpvc' ); ?></option>
  222. </select>
  223. </p>
  224. <?php
  225. }
  226. }
  227. add_shortcode( 'most_views', 'bawpvc_shortcode_top_most' );
  228. add_shortcode( 'most_view', 'bawpvc_shortcode_top_most' );
  229. add_shortcode( 'most_viewed', 'bawpvc_shortcode_top_most' );
  230. function bawpvc_shortcode_top_most( $atts, $content )
  231. {
  232. global $timings, $time_shortcuts, $bawpvc_options;
  233. extract(shortcode_atts(array(
  234. 'number' => 10,
  235. 'show' => 1,
  236. 'time' => 'all',
  237. 'date' => '',
  238. 'before' => '',
  239. 'after' => '',
  240. 'ul_class' => 'pvc',
  241. 'li_class' => 'pvc',
  242. 'order' => 'DESC',
  243. 'author' => '',
  244. 'post_type' => $bawpvc_options['post_types']
  245. ), $atts));
  246. $date = $date != '' ? $date : date( $timings[$time] );
  247. $date = $time == 'all' ? '' : '-' . $date;
  248. $ul_class = $ul_class!='' ? ' class="' . sanitize_html_class( $ul_class ) . '"' : '';
  249. $li_class = $ul_class!='' ? ' class="' . sanitize_html_class( $li_class ) . '"' : '';
  250. $order = $order == 'ASC' ? 'ASC' : 'DESC';
  251. $author_name = '';
  252. if( !is_numeric( $author ) ):
  253. $author_name = $author;
  254. $author = '';
  255. endif;
  256. $meta_key = apply_filters( 'baw_count_views_meta_key', '_count-views_' . $time . $date, $time, $date );
  257. $post_type = is_array( $post_type ) ? $post_type : explode( ',', $post_type );
  258. ob_start();
  259. $r = new WP_Query( array( 'posts_per_page' => $number,
  260. 'no_found_rows' => true,
  261. 'post_status' => 'publish',
  262. 'post_type' => $post_type,
  263. 'ignore_sticky_posts' => true,
  264. 'meta_key' => $meta_key,
  265. 'meta_value_num' => '0',
  266. 'meta_compare' => '>',
  267. 'orderby' => 'meta_value_num',
  268. 'author' => $author,
  269. 'author_name' => $author_name,
  270. 'order' => $order )
  271. );
  272. if ($r->have_posts()) :
  273. echo $before; ?>
  274. <ul<?php echo $ul_class; ?>>
  275. <?php while ($r->have_posts()) : $r->the_post(); ?>
  276. <?php
  277. $count = '';
  278. if( $show ):
  279. $count = (int)get_post_meta( get_the_ID(), $meta_key, true );
  280. do_action( 'baw_count_views_count_action', $count, $meta_key, $time, $date, get_the_ID() );
  281. $count = apply_filters( 'baw_count_views_count', $count, $meta_key, $time, $date );
  282. endif;
  283. ?>
  284. <li<?php echo $li_class; ?>><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); echo $count; ?></a></li>
  285. <?php endwhile; ?>
  286. </ul>
  287. <?php echo $after;
  288. // Reset the global $the_post as this query will have stomped on it
  289. wp_reset_postdata();
  290. endif;
  291. $result = ob_get_contents();
  292. ob_end_clean();
  293. return $result;
  294. }
  295. add_action( 'init', 'bawpvc_widgets_init', 1 );
  296. function bawpvc_widgets_init()
  297. {
  298. global $timings, $bawpvc_options;
  299. load_plugin_textdomain( 'bawpvc', '', dirname( plugin_basename( __FILE__ ) ) . '/lang' );
  300. register_widget( 'WP_Widget_Most_Viewed_Posts' );
  301. $timings = apply_filters( 'baw_count_views_timings', array( 'all'=>'', 'day'=>'Ymd', 'week'=>'YW', 'month'=>'Ym', 'year'=>'Y' ) );
  302. $bawpvc_args = array( 'time' => 0,
  303. 'format' => ' (%count%)',
  304. 'in_content' => false,
  305. 'no_members' => false,
  306. 'no_admins' => false,
  307. 'post_types' => array( 'post' )
  308. );
  309. $bawpvc_options = wp_parse_args( get_option( 'bawpvc_options' ), $bawpvc_args );
  310. }
  311. function bawpvc_settings_page()
  312. {
  313. add_settings_section( 'bawpvc_settings_page', sprintf( __( 'General', 'bawpvc' ), BAWPVC_FULLNAME ), '__return_false', 'bawpvc_settings' );
  314. add_settings_field( 'bawpvc_field_time_count', __( 'Time between counts', 'bawpvc' ), 'bawpvc_field_time_count', 'bawpvc_settings', 'bawpvc_settings_page' );
  315. add_settings_field( 'bawpvc_field_format', __( 'Count format', 'bawpvc' ), 'bawpvc_field_format', 'bawpvc_settings', 'bawpvc_settings_page' );
  316. add_settings_field( 'bawpvc_field_in_content', __( 'Display counter in post content', 'bawpvc' ), 'bawpvc_field_in_content', 'bawpvc_settings', 'bawpvc_settings_page' );
  317. add_settings_field( 'bawpvc_field_no_members', __( 'Do not count views for logged members', 'bawpvc' ), 'bawpvc_field_no_members', 'bawpvc_settings', 'bawpvc_settings_page' );
  318. add_settings_field( 'bawpvc_field_no_admins', __( 'Do not count views for admins', 'bawpvc' ), 'bawpvc_field_no_admins', 'bawpvc_settings', 'bawpvc_settings_page' );
  319. add_settings_field( 'bawpvc_field_post_types', __( 'Select post types', 'bawpvc' ), 'bawpvc_field_post_types', 'bawpvc_settings', 'bawpvc_settings_page' );
  320. add_settings_section( 'bawpvc_settings_page', sprintf( __( 'About', 'bawpvc' ), BAWPVC_FULLNAME ), '__return_false', 'bawpvc_settings2' );
  321. add_settings_field( 'bawpvc_field_about', '', create_function( '', "include( dirname( __FILE__ ) . '/about.php' );" ), 'bawpvc_settings2', 'bawpvc_settings_page' );
  322. ?>
  323. <div class="wrap">
  324. <?php screen_icon( 'options-general' ); ?>
  325. <h2><?php echo BAWPVC_FULLNAME; ?> v<?php echo BAWPVC_VERSION; ?></h2>
  326. <form action="options.php" method="post">
  327. <?php settings_fields( 'bawpvc_settings' ); ?>
  328. <?php do_settings_sections( 'bawpvc_settings' ); ?>
  329. <?php submit_button(); ?>
  330. <?php do_settings_sections( 'bawpvc_settings2' ); ?>
  331. </form>
  332. <?php
  333. }
  334. function bawpvc_field_post_types()
  335. {
  336. global $bawpvc_options;
  337. foreach( get_post_types( array( ), 'objects' ) as $cpt ):
  338. $checked = checked( in_array($cpt->name, $bawpvc_options['post_types']) ? 'on' : '', 'on', false );
  339. $name = esc_attr( $cpt->name );
  340. $label = esc_html( $cpt->label );
  341. $public = $cpt->public ? __( 'Public', 'bawpvc' ) : __( 'Not public', 'bawpvc' );
  342. printf( '<label><input type="checkbox" %s name="bawpvc_options[post_types][]" value="%s" /> %s <em>(%s)</em></label><br />', $checked, $name, $label, $public );
  343. endforeach;
  344. ?>
  345. <code>
  346. <?php _e( 'Public: Whether a post type is intended to be used publicly either via the admin interface or by front-end users.<br />Not public: The opposite...', 'bawpvc' ); ?>
  347. </code>
  348. <?php
  349. }
  350. function bawpvc_field_no_members()
  351. {
  352. global $bawpvc_options;
  353. ?>
  354. <label><input type="checkbox" name="bawpvc_options[no_members]" <?php checked( $bawpvc_options['no_members'], 'on' ); ?> /> <em><?php _e( 'Check me to avoid counting views for logged members.', 'bawpvc' ); ?></em></label>
  355. <?php
  356. }
  357. function bawpvc_field_no_admins()
  358. {
  359. global $bawpvc_options;
  360. ?>
  361. <label><input type="checkbox" name="bawpvc_options[no_admins]" <?php checked( $bawpvc_options['no_admins'], 'on' ); ?> /> <em><?php _e( 'Check me to avoid counting views for admins.', 'bawpvc' ); ?></em></label>
  362. <?php
  363. }
  364. function bawpvc_field_time_count()
  365. {
  366. global $bawpvc_options;
  367. ?>
  368. <input type="number" size="3" maxlength="3" name="bawpvc_options[time]" value="<?php echo absint( $bawpvc_options['time'] ); ?>" /> <em><?php _e( 'seconds', 'bawpvc' ); ?></em>
  369. <?php
  370. }
  371. function bawpvc_field_format()
  372. {
  373. global $bawpvc_options;
  374. ?>
  375. <input type="text" name="bawpvc_options[format]" size="40" value="<?php echo esc_attr( $bawpvc_options['format'] ); ?>" /> <em><?php _e( 'Use <code>%count%</code> to display the counter.', 'bawpvc' ); ?></em>
  376. <?php
  377. }
  378. function bawpvc_field_in_content()
  379. {
  380. global $bawpvc_options;
  381. ?>
  382. <label><input type="checkbox" name="bawpvc_options[in_content]" <?php checked( $bawpvc_options['in_content'], 'on' ); ?> /> <em><?php _e( 'Will be displayed in bottom of content.', 'bawpvc' ); ?></em></label>
  383. <?php
  384. }
  385. add_action( 'admin_menu', 'bawpvc_create_menu' );
  386. function bawpvc_create_menu()
  387. {
  388. add_options_page( BAWPVC_FULLNAME, BAWPVC_FULLNAME , 'manage_options', 'bawpvc_settings', 'bawpvc_settings_page' );
  389. register_setting( 'bawpvc_settings', 'bawpvc_options' );
  390. }
  391. register_activation_hook( __FILE__, 'bawpvc_activation' );
  392. function bawpvc_activation()
  393. {
  394. add_option( 'bawpvc_options',array( 'time' => 0,
  395. 'format' => ' (%count%)',
  396. 'in_content' => false
  397. ) );
  398. }
  399. register_uninstall_hook( __FILE__, 'bawpvc_uninstaller' );
  400. function bawpvc_uninstaller()
  401. {
  402. global $wpdb;
  403. $wpdb->query( 'DELETE FROM ' . $wpdb->postmeta . ' WHERE meta_key LIKE "_count-views%"' );
  404. delete_option( 'bawpvc_options' );
  405. }
  406. add_filter( 'the_content', 'bawpvc_in_content', 1 );
  407. function bawpvc_in_content( $content )
  408. {
  409. global $bawpvc_options, $post;
  410. if( $bawpvc_options['in_content']=='on' && in_array( $post->post_type, $bawpvc_options['post_types'] ) )
  411. return $content . do_shortcode( '[post_views]' );
  412. else
  413. return $content;
  414. }
  415. add_action( 'add_meta_boxes', 'bawpvc_add_meta_boxes' );
  416. function bawpvc_add_meta_boxes()
  417. {
  418. foreach ( get_post_types( array( ), 'names' ) as $cpt )
  419. add_meta_box(
  420. 'bawpvc_meta_box',
  421. BAWPVC_FULLNAME,
  422. 'bawpvc_add_metabox',
  423. $cpt,
  424. 'side'
  425. );
  426. }
  427. function bawpvc_add_metabox()
  428. {
  429. ?>
  430. <div id="bawpvc_box">
  431. <?php _e( '(Clic value to edit it)', 'bawpvc' ); ?>
  432. <br />
  433. <ul>
  434. <?php
  435. global $post, $timings;
  436. foreach( $timings as $time=>$date ):
  437. if( $date != '' ) $date = '-' . date( $date );
  438. $meta_key = apply_filters( 'baw_count_views_meta_key', '_count-views_' . $time . $date, $time, $date );
  439. $count = (int)get_post_meta( $post->ID, $meta_key, true );
  440. $capa = apply_filters( 'baw_count_views_capa_role', 'edit_posts' );
  441. if( current_user_can( $capa, $post->ID ) )
  442. printf( '<li>' . __(
  443. '%1$s: <span class="hide-if-no-js toggle_views" onclick="jQuery(this).hide().next(\'span\').show().find(\'input:visible\').select();" title="%4$s">%2$d</span>'.
  444. '<span class="hide-if-js">'.
  445. '<input type="hidden" name="old_views_%1$s" value="%2$d" />'.
  446. '<input type="hidden" name="old_views_date_%1$s" value="%3$s" />'.
  447. '<input onblur="jQuery(jQuery(this).parent()).hide().prev(\'span\').text(jQuery(this).val()).show();" type="number" min="0" size="2" name="new_views_%1$s" value="%2$d" />'.
  448. '</span> views', 'bawpvc' .
  449. '</li>', 'bawpvc' ),
  450. esc_html( $time ), (int)$count, $date, __( 'Clic to edit me!', 'bawpvc' )
  451. );
  452. // else
  453. // printf( '<li>' . __( '%s: %d views', 'bawpvc' ) . '</li>', esc_html( $time ), (int)$count );
  454. endforeach;
  455. ?><br />
  456. <label><input type="checkbox" name="bawpvc_reset" value="on" /> <?php _e( 'Check me to reset all views', 'bawpvc' ); ?></label>
  457. <?php wp_nonce_field( 'bawpvc-reset_' . $post->ID, 'bawpvc_reset_nonce', true, true ); ?>
  458. </ul>
  459. </div>
  460. <?php
  461. }
  462. add_action( 'save_post', 'bawpvc_reset_from_meta_box' );
  463. function bawpvc_reset_from_meta_box()
  464. {
  465. $capa = apply_filters( 'baw_count_views_capa_role', 'edit_posts' );
  466. if( isset( $_POST['bawpvc_reset_nonce'], $_POST['post_ID'] ) && current_user_can( $capa, (int)$_POST['post_ID'] ) && (int)$_POST['post_ID']>0 ):
  467. check_admin_referer( 'bawpvc-reset_' . $_POST['post_ID'], 'bawpvc_reset_nonce' );
  468. global $wpdb, $timings;
  469. if( isset( $_POST['bawpvc_reset'] ) && $_POST['bawpvc_reset']=='on' ):
  470. $wpdb->query( 'DELETE FROM ' . $wpdb->postmeta . ' WHERE post_id = ' . (int)$_POST['post_ID'] . ' AND meta_key LIKE "_count-views%"' );
  471. return;
  472. endif;
  473. foreach( $timings as $time=>$date ):
  474. $date = isset( $_POST['old_views_date_' . $time] ) ? $_POST['old_views_date_' . $time] : '';
  475. if( isset( $_POST['old_views_date_' . $time], $_POST['old_views_' . $time], $_POST['new_views_' . $time] ) && (int)$_POST['old_views_' . $time]!=(int)$_POST['new_views_' . $time] )
  476. if( (int)$_POST['new_views_' . $time]==0 )
  477. delete_post_meta( (int)$_POST['post_ID'], '_count-views_'.$time.$date );
  478. else
  479. update_post_meta( (int)$_POST['post_ID'], '_count-views_'.$time.$date, (int)$_POST['new_views_' . $time] );
  480. endforeach;
  481. endif;
  482. }
  483. function bawpvc_add_post_columns( $columns )
  484. {
  485. $columns['bawpvc'] = BAWPVC_FULLNAME;
  486. return $columns;
  487. }
  488. add_action( 'baw_count_views_render_post_columns', 'bawpv_render_post_columns_action' );
  489. function bawpv_render_post_columns_action( $post_id )
  490. {
  491. echo (int)get_post_meta( $post_id, '_count-views_all', true );
  492. }
  493. function bawpvc_render_post_columns( $column_name, $post_id )
  494. {
  495. $capa = apply_filters( 'baw_count_views_capa_role', 'edit_posts' );
  496. if( $column_name == 'bawpvc' && current_user_can( $capa ) )
  497. do_action( 'baw_count_views_render_post_columns', $post_id );
  498. }
  499. add_action( 'load-edit.php', 'bawpvc_admin_init' );
  500. function bawpvc_admin_init()
  501. {
  502. global $bawpvc_options;
  503. $capa = apply_filters( 'baw_count_views_capa_role', 'edit_posts' );
  504. if( current_user_can( $capa ) )
  505. foreach ( $bawpvc_options['post_types'] as $cpt ):
  506. add_action( 'manage_' . $cpt . '_posts_columns', 'bawpvc_add_post_columns', 10, 2 );
  507. add_action( 'manage_' . $cpt . '_posts_custom_column', 'bawpvc_render_post_columns', 10, 2 );
  508. endforeach;
  509. }
  510. add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'bawpvc_settings_action_links', 10, 2 );
  511. function bawpvc_settings_action_links( $links, $file )
  512. {
  513. if( current_user_can( 'manage_options' ) )
  514. array_unshift( $links, '<a href="' . admin_url( 'admin.php?page=bawpvc_settings' ) . '">' . __( 'Settings' ) . '</a>' );
  515. return $links;
  516. }
  517. remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); // avoid bad counts for non viewed posts