class-admin.php
4 years ago
class-columns.php
4 years ago
class-counter.php
4 years ago
class-crawler-detect.php
4 years ago
class-cron.php
4 years ago
class-dashboard.php
4 years ago
class-frontend.php
4 years ago
class-functions.php
4 years ago
class-query.php
4 years ago
class-settings-api.php
4 years ago
class-settings.php
4 years ago
class-update.php
4 years ago
class-widgets.php
4 years ago
functions.php
4 years ago
class-admin.php
149 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 | } |
| 22 | |
| 23 | /** |
| 24 | * Init block editor actions. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function init_block_editor() { |
| 29 | add_action( 'rest_api_init', [ $this, 'block_editor_rest_api_init' ] ); |
| 30 | add_action( 'enqueue_block_editor_assets', [ $this, 'block_editor_enqueue_scripts' ] ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Register REST API block editor endpoints. |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public function block_editor_rest_api_init() { |
| 39 | // get views route |
| 40 | register_rest_route( |
| 41 | 'post-views-counter', |
| 42 | '/update-post-views/', |
| 43 | [ |
| 44 | 'methods' => [ 'POST' ], |
| 45 | 'callback' => [ $this, 'block_editor_update_callback' ], |
| 46 | 'permission_callback' => [ $this, 'check_rest_route_permissions' ], |
| 47 | 'args' => [ |
| 48 | 'id' => [ |
| 49 | 'sanitize_callback' => 'absint', |
| 50 | ] |
| 51 | ] |
| 52 | ] |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Check whether user has permissions to perform post views update in block editor. |
| 58 | * |
| 59 | * @param object $request WP_REST_Request |
| 60 | * @return bool|WP_Error |
| 61 | */ |
| 62 | public function check_rest_route_permissions( $request ) { |
| 63 | // break if current user can't edit this post |
| 64 | if ( ! current_user_can( 'edit_post', (int) $request->get_param( 'id' ) ) ) |
| 65 | return new WP_Error( 'pvc-user-not-allowed', __( 'You are not allowed to edit this item.', 'post-views-counter' ) ); |
| 66 | |
| 67 | // break if views editing is restricted |
| 68 | if ( (bool) Post_Views_Counter()->options['general']['restrict_edit_views'] === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) |
| 69 | return new WP_Error( 'pvc-user-not-allowed', __( 'You are not allowed to edit this item.', 'post-views-counter' ) ); |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * REST API callback for block editor endpoint. |
| 76 | * |
| 77 | * @param array $data |
| 78 | * @return string|int |
| 79 | */ |
| 80 | public function block_editor_update_callback( $data ) { |
| 81 | // cast post ID |
| 82 | $post_id = ! empty( $data['id'] ) ? (int) $data['id'] : 0; |
| 83 | |
| 84 | // cast post views |
| 85 | $post_views = ! empty( $data['post_views'] ) ? (int) $data['post_views'] : 0; |
| 86 | |
| 87 | // get countable post types |
| 88 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 89 | |
| 90 | // check if post exists |
| 91 | $post = get_post( $post_id ); |
| 92 | |
| 93 | // whether to count this post type or not |
| 94 | if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) ) |
| 95 | return wp_send_json_error( __( 'Invalid post ID.', 'post-views-counter' ) ); |
| 96 | |
| 97 | // break if current user can't edit this post |
| 98 | if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 99 | return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) ); |
| 100 | |
| 101 | // break if views editing is restricted |
| 102 | if ( (bool) Post_Views_Counter()->options['general']['restrict_edit_views'] === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) |
| 103 | return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) ); |
| 104 | |
| 105 | // update post views |
| 106 | pvc_update_post_views( $post_id, $post_views ); |
| 107 | |
| 108 | do_action( 'pvc_after_update_post_views_count', $post_id ); |
| 109 | |
| 110 | return $post_id; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Enqueue frontend and editor JavaScript and CSS. |
| 115 | * |
| 116 | * @return void |
| 117 | */ |
| 118 | public function block_editor_enqueue_scripts() { |
| 119 | global $pagenow, $wp_version; |
| 120 | |
| 121 | // skip widgets and customizer pages |
| 122 | if ( $pagenow === 'widgets.php' || $pagenow === 'customize.php' ) |
| 123 | return; |
| 124 | |
| 125 | // enqueue the bundled block JS file |
| 126 | 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'] ); |
| 127 | |
| 128 | // restrict editing |
| 129 | $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views']; |
| 130 | |
| 131 | wp_localize_script( |
| 132 | 'pvc-block-editor', |
| 133 | 'pvcEditorArgs', |
| 134 | [ |
| 135 | 'postID' => get_the_ID(), |
| 136 | 'postViews' => pvc_get_post_views( get_the_ID() ), |
| 137 | 'canEdit' => ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ), |
| 138 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 139 | 'wpGreater53' => version_compare( $wp_version, '5.3', '>=' ), |
| 140 | 'textPostViews' => __( 'Post Views', 'post-views-counter' ), |
| 141 | 'textHelp' => __( 'Adjust the views count for this post.', 'post-views-counter' ), |
| 142 | 'textCancel' => __( 'Cancel', 'post-views-counter' ) |
| 143 | ] |
| 144 | ); |
| 145 | |
| 146 | // enqueue frontend and editor block styles |
| 147 | wp_enqueue_style( 'pvc-block-editor', POST_VIEWS_COUNTER_URL . '/css/block-editor.min.css', '', Post_Views_Counter()->defaults['version'] ); |
| 148 | } |
| 149 | } |