PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/post-views-counter/includes/columns.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 448 lines | 259 code | 86 blank | 103 comment | 53 complexity | 88f7c5102711e4cbab8137ef1418f029 MD5 | raw file
  1. <?php
  2. // exit if accessed directly
  3. if ( ! defined( 'ABSPATH' ) )
  4. exit;
  5. /**
  6. * Post_Views_Counter_Columns class.
  7. *
  8. * @class Post_Views_Counter_Columns
  9. */
  10. class Post_Views_Counter_Columns {
  11. public function __construct() {
  12. // actions
  13. add_action( 'admin_init', array( $this, 'register_new_column' ) );
  14. add_action( 'post_submitbox_misc_actions', array( $this, 'submitbox_views' ) );
  15. add_action( 'attachment_submitbox_misc_actions', array( $this, 'submitbox_views' ) );
  16. add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
  17. add_action( 'edit_attachment', array( $this, 'save_post' ), 10 );
  18. add_action( 'bulk_edit_custom_box', array( $this, 'quick_edit_custom_box' ), 10, 2 );
  19. add_action( 'quick_edit_custom_box', array( $this, 'quick_edit_custom_box' ), 10, 2 );
  20. add_action( 'wp_ajax_save_bulk_post_views', array( $this, 'save_bulk_post_views' ) );
  21. // gutenberg
  22. add_action( 'plugins_loaded', array( $this, 'init_gutemberg' ) );
  23. }
  24. /**
  25. * Init Gutenberg
  26. */
  27. public function init_gutemberg() {
  28. $block_editor = has_action( 'enqueue_block_assets' );
  29. $gutenberg = function_exists( 'gutenberg_can_edit_post_type' );
  30. if ( ! $block_editor && ! $gutenberg ) {
  31. return;
  32. }
  33. add_action( 'add_meta_boxes', array( $this, 'gutenberg_add_meta_box' ) );
  34. add_action( 'rest_api_init', array( $this, 'gutenberg_rest_api_init' ) );
  35. add_action( 'enqueue_block_editor_assets', array( $this, 'gutenberg_enqueue_scripts' ) );
  36. }
  37. /**
  38. * Register Gutenberg Metabox.
  39. */
  40. public function gutenberg_add_meta_box() {
  41. add_meta_box( 'post_views_meta_box', __( 'Post Views', 'post-views-counter' ), '', 'post', '', '', array(
  42. '__back_compat_meta_box' => false,
  43. '__block_editor_compatible_meta_box' => true
  44. ) );
  45. }
  46. /**
  47. * Register REST API Gutenberg endpoints.
  48. */
  49. public function gutenberg_rest_api_init() {
  50. // get views route
  51. register_rest_route(
  52. 'post-views-counter',
  53. '/update-post-views/',
  54. array(
  55. 'methods' => array( 'POST' ),
  56. 'callback' => array( $this, 'gutenberg_update_callback' ),
  57. 'args' => array(
  58. 'id' => array(
  59. 'sanitize_callback' => 'absint',
  60. )
  61. )
  62. )
  63. );
  64. }
  65. /**
  66. * REST API Callback for Gutenberg endpoint.
  67. *
  68. * @param array $data
  69. * @return array|int
  70. */
  71. public function gutenberg_update_callback( $data ) {
  72. $post_id = ! empty( $data['id'] ) ? (int) $data['id'] : 0;
  73. $post_views = ! empty( $data['post_views'] ) ? (int) $data['post_views'] : 0;
  74. // get countable post types
  75. $post_types = Post_Views_Counter()->options['general']['post_types_count'];
  76. // check if post exists
  77. $post = get_post( $post_id );
  78. // whether to count this post type or not
  79. if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) )
  80. return wp_send_json_error( __( 'Invalid post ID.', 'post-views-counter' ) );
  81. // break if current user can't edit this post
  82. if ( ! current_user_can( 'edit_post', $post_id ) )
  83. return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
  84. // break if views editing is restricted
  85. $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
  86. if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
  87. return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
  88. global $wpdb;
  89. pvc_update_post_views( $post_id, $post_views );
  90. do_action( 'pvc_after_update_post_views_count', $post_id );
  91. return $post_id;
  92. }
  93. /**
  94. * Enqueue front end and editor JavaScript and CSS
  95. */
  96. public function gutenberg_enqueue_scripts() {
  97. // enqueue the bundled block JS file
  98. wp_enqueue_script(
  99. 'pvc-gutenberg',
  100. POST_VIEWS_COUNTER_URL . '/js/gutenberg.min.js',
  101. array( 'wp-i18n', 'wp-edit-post', 'wp-element', 'wp-editor', 'wp-components', 'wp-data', 'wp-plugins', 'wp-api' ),
  102. Post_Views_Counter()->defaults['version']
  103. );
  104. // restrict editing
  105. $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
  106. $can_edit = $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) );
  107. $js_args = array(
  108. 'postID' => get_the_ID(),
  109. 'postViews' => pvc_get_post_views( get_the_ID() ),
  110. 'canEdit' => $can_edit,
  111. 'nonce' => wp_create_nonce( 'wp_rest' ),
  112. 'textPostViews' => __( 'Post Views', 'post-views-counter' ),
  113. 'textHelp' => __( 'Adjust the views count for this post.', 'post-views-counter' ),
  114. 'textCancel' => __( 'Cancel', 'post-views-counter' )
  115. );
  116. wp_localize_script(
  117. 'pvc-gutenberg',
  118. 'pvcEditorArgs',
  119. $js_args
  120. );
  121. // enqueue frontend and editor block styles
  122. wp_enqueue_style(
  123. 'pvc-gutenberg',
  124. POST_VIEWS_COUNTER_URL . '/css/gutenberg.min.css', '',
  125. Post_Views_Counter()->defaults['version']
  126. );
  127. }
  128. /**
  129. * Output post views for single post.
  130. *
  131. * @global object $post
  132. * @return mixed
  133. */
  134. public function submitbox_views() {
  135. global $post;
  136. if ( ! in_array( $post->post_type, (array) Post_Views_Counter()->options['general']['post_types_count'] ) )
  137. return;
  138. // break if current user can't edit this post
  139. if ( ! current_user_can( 'edit_post', $post->ID ) )
  140. return;
  141. // get total post views
  142. $count = (int) pvc_get_post_views( $post->ID ); ?>
  143. <div class="misc-pub-section" id="post-views">
  144. <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
  145. <span id="post-views-display">
  146. <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( $count ) . '</b>'; ?>
  147. </span>
  148. <?php
  149. // restrict editing
  150. $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
  151. if ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ) {
  152. ?>
  153. <a href="#post-views" class="edit-post-views hide-if-no-js"><?php _e( 'Edit', 'post-views-counter' ); ?></a>
  154. <div id="post-views-input-container" class="hide-if-js">
  155. <p><?php _e( 'Adjust the views count for this post.', 'post-views-counter' ); ?></p>
  156. <input type="hidden" name="current_post_views" id="post-views-current" value="<?php echo $count; ?>" />
  157. <input type="text" name="post_views" id="post-views-input" value="<?php echo $count; ?>"/><br />
  158. <p>
  159. <a href="#post-views" class="save-post-views hide-if-no-js button"><?php _e( 'OK', 'post-views-counter' ); ?></a>
  160. <a href="#post-views" class="cancel-post-views hide-if-no-js"><?php _e( 'Cancel', 'post-views-counter' ); ?></a>
  161. </p>
  162. </div>
  163. <?php
  164. }
  165. ?>
  166. </div>
  167. <?php
  168. }
  169. /**
  170. * Save post views data.
  171. *
  172. * @param int $post_id
  173. * @param object $post
  174. */
  175. public function save_post( $post_id, $post = null ) {
  176. if ( is_null( $post ) )
  177. $post_type = get_post_type( $post_id );
  178. else
  179. $post_type = $post->post_type;
  180. // break if doing autosave
  181. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  182. return $post_id;
  183. // break if current user can't edit this post
  184. if ( ! current_user_can( 'edit_post', $post_id ) )
  185. return $post_id;
  186. // is post views set
  187. if ( ! isset( $_POST['post_views'] ) )
  188. return $post_id;
  189. // cast numeric post views
  190. $post_views = (int) $_POST['post_views'];
  191. // unchanged post views value?
  192. if ( isset( $_POST['current_post_views'] ) && $post_views === (int) $_POST['current_post_views'] )
  193. return $post_id;
  194. // break if post views in not one of the selected
  195. $post_types = Post_Views_Counter()->options['general']['post_types_count'];
  196. if ( ! in_array( $post_type, (array) $post_types ) )
  197. return $post_id;
  198. // break if views editing is restricted
  199. $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
  200. if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
  201. return $post_id;
  202. // validate data
  203. if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) )
  204. return $post_id;
  205. pvc_update_post_views( $post_id, $post_views );
  206. do_action( 'pvc_after_update_post_views_count', $post_id );
  207. }
  208. /**
  209. * Register post views column for specific post types
  210. */
  211. public function register_new_column() {
  212. $post_types = Post_Views_Counter()->options['general']['post_types_count'];
  213. if ( ! empty( $post_types ) ) {
  214. foreach ( $post_types as $post_type ) {
  215. if ( $post_type === 'attachment' ) {
  216. // actions
  217. add_action( 'manage_media_custom_column', array( $this, 'add_new_column_content' ), 10, 2 );
  218. // filters
  219. add_filter( 'manage_media_columns', array( $this, 'add_new_column' ) );
  220. add_filter( 'manage_upload_sortable_columns', array( $this, 'register_sortable_custom_column' ) );
  221. } else {
  222. // actions
  223. add_action( 'manage_' . $post_type . '_posts_custom_column', array( $this, 'add_new_column_content' ), 10, 2 );
  224. // filters
  225. add_filter( 'manage_' . $post_type . '_posts_columns', array( $this, 'add_new_column' ) );
  226. add_filter( 'manage_edit-' . $post_type . '_sortable_columns', array( $this, 'register_sortable_custom_column' ) );
  227. if ( class_exists( 'bbPress' ) ) {
  228. if ( $post_type === 'forum' )
  229. add_filter( 'bbp_admin_forums_column_headers', array( $this, 'add_new_column' ) );
  230. elseif ( $post_type === 'topic' )
  231. add_filter( 'bbp_admin_topics_column_headers', array( $this, 'add_new_column' ) );
  232. }
  233. }
  234. }
  235. }
  236. }
  237. /**
  238. * Register sortable post views column.
  239. *
  240. * @param array $columns
  241. * @return array
  242. */
  243. public function register_sortable_custom_column( $columns ) {
  244. // add new sortable column
  245. $columns['post_views'] = 'post_views';
  246. return $columns;
  247. }
  248. /**
  249. * Add post views column.
  250. *
  251. * @param array $columns
  252. * @return array
  253. */
  254. public function add_new_column( $columns ) {
  255. $offset = 0;
  256. if ( isset( $columns['date'] ) )
  257. $offset++;
  258. if ( isset( $columns['comments'] ) )
  259. $offset++;
  260. if ( $offset > 0 ) {
  261. $date = array_slice( $columns, -$offset, $offset, true );
  262. foreach ( $date as $column => $name ) {
  263. unset( $columns[$column] );
  264. }
  265. $columns['post_views'] = '<span class="dash-icon dashicons dashicons-chart-bar" title="' . __( 'Post Views', 'post-views-counter' ) . '"></span><span class="dash-title">' . __( 'Post Views', 'post-views-counter' ) . '</span>';
  266. foreach ( $date as $column => $name ) {
  267. $columns[$column] = $name;
  268. }
  269. } else
  270. $columns['post_views'] = '<span class="dash-icon dashicons dashicons-chart-bar" title="' . __( 'Post Views', 'post-views-counter' ) . '"></span><span class="dash-title">' . __( 'Post Views', 'post-views-counter' ) . '</span>';
  271. return $columns;
  272. }
  273. /**
  274. * Add post views column content.
  275. *
  276. * @param string $column_name
  277. * @param int $id
  278. * @return muxed
  279. */
  280. public function add_new_column_content( $column_name, $id ) {
  281. if ( $column_name === 'post_views' ) {
  282. // get total post views
  283. $count = pvc_get_post_views( $id );
  284. echo $count;
  285. }
  286. }
  287. /**
  288. * Handle quick edit.
  289. *
  290. * @global string $pagenow
  291. * @param string $column_name
  292. * @return mixed
  293. */
  294. function quick_edit_custom_box( $column_name, $post_type ) {
  295. global $pagenow, $post;
  296. if ( $pagenow !== 'edit.php' )
  297. return;
  298. if ( $column_name !== 'post_views' )
  299. return;
  300. if ( ! Post_Views_Counter()->options['general']['post_views_column'] || ! in_array( $post_type, Post_Views_Counter()->options['general']['post_types_count'] ) )
  301. return;
  302. // break if views editing is restricted
  303. $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
  304. if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
  305. return;
  306. ?>
  307. <fieldset class="inline-edit-col-left">
  308. <div id="inline-edit-post_views" class="inline-edit-col">
  309. <label class="inline-edit-group">
  310. <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span>
  311. <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value=""></span>
  312. <input type="hidden" name="current_post_views" value="" />
  313. <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
  314. </label>
  315. </div>
  316. </fieldset>
  317. <?php
  318. }
  319. /**
  320. * Bulk save post views.
  321. *
  322. * @global object $wpdb;
  323. * @return type
  324. */
  325. function save_bulk_post_views() {
  326. if ( ! isset( $_POST['post_views'] ) )
  327. $count = null;
  328. else {
  329. $count = trim( $_POST['post_views'] );
  330. if ( is_numeric( $_POST['post_views'] ) ) {
  331. $count = (int) $_POST['post_views'];
  332. if ( $count < 0 )
  333. $count = 0;
  334. } else
  335. $count = null;
  336. }
  337. $post_ids = ( ! empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', $_POST['post_ids'] ) : array();
  338. if ( is_null( $count ) )
  339. exit;
  340. // break if views editing is restricted
  341. $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
  342. if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
  343. exit;
  344. if ( ! empty( $post_ids ) ) {
  345. foreach ( $post_ids as $post_id ) {
  346. // break if current user can't edit this post
  347. if ( ! current_user_can( 'edit_post', $post_id ) )
  348. continue;
  349. global $wpdb;
  350. // insert or update db post views count
  351. $wpdb->query(
  352. $wpdb->prepare( "
  353. INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count)
  354. VALUES (%d, %d, %s, %d)
  355. ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count
  356. )
  357. );
  358. }
  359. }
  360. exit;
  361. }
  362. }