PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.7.15
Post Views Counter v1.7.15
1.7.15 1.7.14 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 weeks ago class-columns-modal.php 2 weeks ago class-columns.php 2 weeks ago class-counter.php 2 weeks ago class-crawler-detect.php 2 weeks ago class-cron.php 2 weeks ago class-dashboard.php 2 weeks ago class-emails-mailer.php 2 weeks ago class-emails-period.php 2 weeks ago class-emails-query.php 2 weeks ago class-emails-scheduler.php 2 weeks ago class-emails-template.php 2 weeks ago class-emails.php 2 weeks ago class-frontend.php 2 weeks ago class-functions.php 2 weeks ago class-import.php 2 weeks ago class-integration-gutenberg.php 2 weeks ago class-integrations.php 2 weeks ago class-query.php 2 weeks ago class-settings-api.php 2 weeks ago class-settings-display.php 2 weeks ago class-settings-emails.php 2 weeks ago class-settings-general.php 2 weeks ago class-settings-integrations.php 2 weeks ago class-settings-other.php 2 weeks ago class-settings-reports.php 2 weeks ago class-settings.php 2 weeks ago class-toolbar.php 2 weeks ago class-traffic-signals.php 2 weeks ago class-update.php 2 weeks ago class-widgets.php 2 weeks ago functions.php 2 weeks ago
class-admin.php
185 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.5.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['display']['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 = (array) $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['display']['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 screens without a single post context: widgets, customizer and the site editor
141 // wp.editor.PluginPostStatusInfo renders in the site editor too, where get_the_ID() is unavailable
142 if ( $pagenow === 'widgets.php' || $pagenow === 'customize.php' || $pagenow === 'site-editor.php' )
143 return;
144
145 // enqueue the bundled block JS file
146 // wp-editor: PluginPostStatusInfo, wp-api-request: wp.apiRequest used on save
147 wp_enqueue_script( 'pvc-block-editor', POST_VIEWS_COUNTER_URL . '/js/block-editor.js', [ 'wp-element', 'wp-components', 'wp-editor', 'wp-data', 'wp-plugins', 'wp-api-request' ], $pvc->defaults['version'], false );
148
149 // restrict editing
150 $can_edit = false;
151
152 $restrict = (bool) $pvc->options['display']['restrict_edit_views'];
153
154 if ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) )
155 $can_edit = true;
156
157 // get total post views
158 $id = get_the_ID();
159 $count = pvc_get_post_views( $id );
160
161 // disable views display and editing?
162 if ( apply_filters( 'pvc_admin_display_views', true, $id ) === false ) {
163 $count = '';
164 $can_edit = false;
165 }
166
167 // prepare script data
168 $script_data = [
169 'postID' => $id,
170 'postViews' => $count,
171 'canEdit' => $can_edit,
172 'nonce' => wp_create_nonce( 'wp_rest' ),
173 'wpGreater53' => version_compare( $wp_version, '5.3', '>=' ),
174 'textPostViews' => esc_html__( 'Post Views', 'post-views-counter' ),
175 'textHelp' => esc_html__( 'Adjust the views count for this post.', 'post-views-counter' ),
176 'textCancel' => esc_html__( 'Cancel', 'post-views-counter' )
177 ];
178
179 wp_add_inline_script( 'pvc-block-editor', 'var pvcEditorArgs = ' . wp_json_encode( $script_data ) . ";\n", 'before' );
180
181 // enqueue frontend and editor block styles
182 wp_enqueue_style( 'pvc-block-editor', POST_VIEWS_COUNTER_URL . '/css/block-editor.css', '', $pvc->defaults['version'] );
183 }
184 }
185