PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.12
Post Views Counter v1.3.12
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / includes / class-admin.php
post-views-counter / includes Last commit date
class-admin.php 3 years ago class-columns.php 3 years ago class-counter.php 3 years ago class-crawler-detect.php 3 years ago class-cron.php 3 years ago class-dashboard.php 3 years ago class-frontend.php 3 years ago class-functions.php 3 years ago class-query.php 3 years ago class-settings-api.php 3 years ago class-settings.php 3 years ago class-update.php 3 years ago class-widgets.php 3 years ago functions.php 3 years ago
class-admin.php
162 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Admin class.
8 *
9 * @class Post_Views_Counter_Admin
10 */
11 class Post_Views_Counter_Admin {
12
13 /**
14 * Class constructor.
15 *
16 * @return void
17 */
18 public function __construct() {
19 // actions
20 add_action( 'plugins_loaded', [ $this, 'init_block_editor' ] );
21 add_action( 'admin_enqueue_scripts', [ $this, 'register_chartjs' ], 9 );
22 }
23
24 /**
25 * Register Chart.js.
26 *
27 * @return void
28 */
29 public function register_chartjs() {
30 wp_register_script( 'pvc-chartjs', POST_VIEWS_COUNTER_URL . '/assets/chartjs/chart.min.js', [ 'jquery' ], '3.7.1', true );
31 }
32
33 /**
34 * Init block editor actions.
35 *
36 * @return void
37 */
38 public function init_block_editor() {
39 add_action( 'rest_api_init', [ $this, 'block_editor_rest_api_init' ] );
40 add_action( 'enqueue_block_editor_assets', [ $this, 'block_editor_enqueue_scripts' ] );
41 }
42
43 /**
44 * Register REST API block editor endpoints.
45 *
46 * @return void
47 */
48 public function block_editor_rest_api_init() {
49 // get views route
50 register_rest_route(
51 'post-views-counter',
52 '/update-post-views/',
53 [
54 'methods' => [ 'POST' ],
55 'callback' => [ $this, 'block_editor_update_callback' ],
56 'permission_callback' => [ $this, 'check_rest_route_permissions' ],
57 'args' => [
58 'id' => [
59 'sanitize_callback' => 'absint',
60 ]
61 ]
62 ]
63 );
64 }
65
66 /**
67 * Check whether user has permissions to perform post views update in block editor.
68 *
69 * @param object $request WP_REST_Request
70 * @return bool|WP_Error
71 */
72 public function check_rest_route_permissions( $request ) {
73 // break if current user can't edit this post
74 if ( ! current_user_can( 'edit_post', (int) $request->get_param( 'id' ) ) )
75 return new WP_Error( 'pvc-user-not-allowed', __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
76
77 // break if views editing is restricted
78 if ( (bool) Post_Views_Counter()->options['general']['restrict_edit_views'] === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
79 return new WP_Error( 'pvc-user-not-allowed', __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
80
81 return true;
82 }
83
84 /**
85 * REST API callback for block editor endpoint.
86 *
87 * @param array $data
88 * @return string|int
89 */
90 public function block_editor_update_callback( $data ) {
91 // cast post ID
92 $post_id = ! empty( $data['id'] ) ? (int) $data['id'] : 0;
93
94 // cast post views
95 $post_views = ! empty( $data['post_views'] ) ? (int) $data['post_views'] : 0;
96
97 // get countable post types
98 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
99
100 // check if post exists
101 $post = get_post( $post_id );
102
103 // whether to count this post type or not
104 if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) )
105 return wp_send_json_error( __( 'Invalid post ID.', 'post-views-counter' ) );
106
107 // break if current user can't edit this post
108 if ( ! current_user_can( 'edit_post', $post_id ) )
109 return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
110
111 // break if views editing is restricted
112 if ( (bool) Post_Views_Counter()->options['general']['restrict_edit_views'] === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
113 return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
114
115 // update post views
116 pvc_update_post_views( $post_id, $post_views );
117
118 do_action( 'pvc_after_update_post_views_count', $post_id );
119
120 return $post_id;
121 }
122
123 /**
124 * Enqueue frontend and editor JavaScript and CSS.
125 *
126 * @global string $pagenow
127 * @global string $wp_version
128 *
129 * @return void
130 */
131 public function block_editor_enqueue_scripts() {
132 global $pagenow, $wp_version;
133
134 // skip widgets and customizer pages
135 if ( $pagenow === 'widgets.php' || $pagenow === 'customize.php' )
136 return;
137
138 // enqueue the bundled block JS file
139 wp_enqueue_script( 'pvc-block-editor', POST_VIEWS_COUNTER_URL . '/js/block-editor.min.js', [ 'wp-element', 'wp-components', 'wp-edit-post', 'wp-data', 'wp-plugins' ], Post_Views_Counter()->defaults['version'] );
140
141 // restrict editing
142 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
143
144 wp_localize_script(
145 'pvc-block-editor',
146 'pvcEditorArgs',
147 [
148 'postID' => get_the_ID(),
149 'postViews' => pvc_get_post_views( get_the_ID() ),
150 'canEdit' => ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ),
151 'nonce' => wp_create_nonce( 'wp_rest' ),
152 'wpGreater53' => version_compare( $wp_version, '5.3', '>=' ),
153 'textPostViews' => __( 'Post Views', 'post-views-counter' ),
154 'textHelp' => __( 'Adjust the views count for this post.', 'post-views-counter' ),
155 'textCancel' => __( 'Cancel', 'post-views-counter' )
156 ]
157 );
158
159 // enqueue frontend and editor block styles
160 wp_enqueue_style( 'pvc-block-editor', POST_VIEWS_COUNTER_URL . '/css/block-editor.min.css', '', Post_Views_Counter()->defaults['version'] );
161 }
162 }