class-admin.php
1 year ago
class-columns.php
1 year ago
class-counter.php
1 year ago
class-crawler-detect.php
1 year ago
class-cron.php
1 year ago
class-dashboard.php
1 year ago
class-frontend.php
1 year ago
class-functions.php
1 year ago
class-query.php
1 year ago
class-settings-api.php
1 year ago
class-settings.php
1 year ago
class-update.php
1 year ago
class-widgets.php
1 year ago
functions.php
1 year ago
class-admin.php
183 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.8', 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 | $can_edit = false; |
| 149 | |
| 150 | $restrict = (bool) $pvc->options['general']['restrict_edit_views']; |
| 151 | |
| 152 | if ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ) |
| 153 | $can_edit = true; |
| 154 | |
| 155 | // get total post views |
| 156 | $id = get_the_ID(); |
| 157 | $count = pvc_get_post_views( $id ); |
| 158 | |
| 159 | // disable views display and editing? |
| 160 | if ( apply_filters( 'pvc_admin_display_views', true, $id ) === false ) { |
| 161 | $count = '—'; |
| 162 | $can_edit = false; |
| 163 | } |
| 164 | |
| 165 | // prepare script data |
| 166 | $script_data = [ |
| 167 | 'postID' => $id, |
| 168 | 'postViews' => $count, |
| 169 | 'canEdit' => $can_edit, |
| 170 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 171 | 'wpGreater53' => version_compare( $wp_version, '5.3', '>=' ), |
| 172 | 'textPostViews' => esc_html__( 'Post Views', 'post-views-counter' ), |
| 173 | 'textHelp' => esc_html__( 'Adjust the views count for this post.', 'post-views-counter' ), |
| 174 | 'textCancel' => esc_html__( 'Cancel', 'post-views-counter' ) |
| 175 | ]; |
| 176 | |
| 177 | wp_add_inline_script( 'pvc-block-editor', 'var pvcEditorArgs = ' . wp_json_encode( $script_data ) . ";\n", 'before' ); |
| 178 | |
| 179 | // enqueue frontend and editor block styles |
| 180 | wp_enqueue_style( 'pvc-block-editor', POST_VIEWS_COUNTER_URL . '/css/block-editor.min.css', '', $pvc->defaults['version'] ); |
| 181 | } |
| 182 | } |
| 183 |