PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.4.7
Post Views Counter v1.4.7
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 2 years ago class-columns.php 2 years ago class-counter.php 2 years ago class-crawler-detect.php 2 years ago class-cron.php 2 years ago class-dashboard.php 2 years ago class-frontend.php 2 years ago class-functions.php 2 years ago class-query.php 2 years ago class-settings-api.php 2 years ago class-settings.php 2 years ago class-update.php 2 years ago class-widgets.php 2 years ago functions.php 2 years ago
class-admin.php
168 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' ], '4.4.0', 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 // get main instance
92 $pvc = Post_Views_Counter();
93
94 // cast post ID
95 $post_id = ! empty( $data['id'] ) ? (int) $data['id'] : 0;
96
97 // cast post views
98 $post_views = ! empty( $data['post_views'] ) ? (int) $data['post_views'] : 0;
99
100 // get countable post types
101 $post_types = $pvc->options['general']['post_types_count'];
102
103 // check if post exists
104 $post = get_post( $post_id );
105
106 // whether to count this post type or not
107 if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) )
108 return wp_send_json_error( __( 'Invalid post ID.', 'post-views-counter' ) );
109
110 // break if current user can't edit this post
111 if ( ! current_user_can( 'edit_post', $post_id ) )
112 return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
113
114 // break if views editing is restricted
115 if ( (bool) $pvc->options['general']['restrict_edit_views'] === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
116 return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
117
118 // update post views
119 pvc_update_post_views( $post_id, $post_views );
120
121 do_action( 'pvc_after_update_post_views_count', $post_id );
122
123 return $post_id;
124 }
125
126 /**
127 * Enqueue frontend and editor JavaScript and CSS.
128 *
129 * @global string $pagenow
130 * @global string $wp_version
131 *
132 * @return void
133 */
134 public function block_editor_enqueue_scripts() {
135 global $pagenow, $wp_version;
136
137 // get main instance
138 $pvc = Post_Views_Counter();
139
140 // skip widgets and customizer pages
141 if ( $pagenow === 'widgets.php' || $pagenow === 'customize.php' )
142 return;
143
144 // enqueue the bundled block JS file
145 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' ], $pvc->defaults['version'] );
146
147 // restrict editing
148 $restrict = (bool) $pvc->options['general']['restrict_edit_views'];
149
150 // prepare script data
151 $script_data = [
152 'postID' => get_the_ID(),
153 'postViews' => pvc_get_post_views( get_the_ID() ),
154 'canEdit' => ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ),
155 'nonce' => wp_create_nonce( 'wp_rest' ),
156 'wpGreater53' => version_compare( $wp_version, '5.3', '>=' ),
157 'textPostViews' => esc_html__( 'Post Views', 'post-views-counter' ),
158 'textHelp' => esc_html__( 'Adjust the views count for this post.', 'post-views-counter' ),
159 'textCancel' => esc_html__( 'Cancel', 'post-views-counter' )
160 ];
161
162 wp_add_inline_script( 'pvc-block-editor', 'var pvcEditorArgs = ' . wp_json_encode( $script_data ) . ";\n", 'before' );
163
164 // enqueue frontend and editor block styles
165 wp_enqueue_style( 'pvc-block-editor', POST_VIEWS_COUNTER_URL . '/css/block-editor.min.css', '', $pvc->defaults['version'] );
166 }
167 }
168