| @@ -82,12 +82,20 @@ | ||
| 82 | 82 | 'methods' => \WP_REST_Server::READABLE, |
| 83 | 83 | 'callback' => array( $this, 'get_item' ), |
| 84 | 84 | 'permission_callback' => array( $this, 'permissions_check' ), |
| 85 | 85 | 'args' => array( |
| 86 | - 'id' => array( | |
| 86 | + 'id' => array( | |
| 87 | 87 | 'description' => __( 'Post ID.', 'top-10' ), |
| 88 | 88 | 'type' => 'integer', |
| 89 | 89 | ), |
| 90 | + 'lang' => array( | |
| 91 | + 'description' => __( 'TranslatePress language code to render the response in.', 'top-10' ), | |
| 92 | + 'type' => 'string', | |
| 93 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 94 | + 'validate_callback' => static function ( $value ) { | |
| 95 | + return is_string( $value ) && strlen( $value ) <= 20; | |
| 96 | + }, | |
| 97 | + ), | |
| 90 | 98 | ), |
| 91 | 99 | ) |
| 92 | 100 | ); |
| 93 | 101 | register_rest_route( |
| @@ -129,8 +137,16 @@ | ||
| 129 | 137 | array( 'status' => rest_authorization_required_code() ) |
| 130 | 138 | ); |
| 131 | 139 | } |
| 132 | 140 | |
| 141 | + /** | |
| 142 | + * Filters the result of the permissions check for the Top 10 REST endpoints. | |
| 143 | + * | |
| 144 | + * @since 3.0.0 | |
| 145 | + * | |
| 146 | + * @param bool $permission Whether the request is allowed. | |
| 147 | + * @param \WP_REST_Request $request The REST request object. | |
| 148 | + */ | |
| 133 | 149 | return apply_filters( 'top_ten_rest_api_permissions_check', true, $request ); |
| 134 | 150 | } |
| 135 | 151 | |
| 136 | 152 | /** |
| @@ -145,8 +161,11 @@ | ||
| 145 | 161 | $popular_posts = array(); |
| 146 | 162 | |
| 147 | 163 | $args = $request->get_params(); |
| 148 | 164 | |
| 165 | + // Transport-only: the language must not reach the query or its cache key. | |
| 166 | + unset( $args['lang'] ); | |
| 167 | + | |
| 149 | 168 | /** |
| 150 | 169 | * Filter the REST API arguments before they passed to get_tptn_posts(). |
| 151 | 170 | * |
| 152 | 171 | * @since 3.0.0 |
| @@ -232,15 +251,24 @@ | ||
| 232 | 251 | * @param \WP_REST_Request $request WP Rest request. |
| 233 | 252 | * @return mixed|\WP_REST_Response Array of post objects or post IDs. |
| 234 | 253 | */ |
| 235 | 254 | public function update_post_count( $request ) { |
| 255 | + if ( ! \WebberZone\Top_Ten\Tracker::is_tracking_request_allowed() ) { | |
| 256 | + $response = new \WP_REST_Response( '', 204 ); | |
| 257 | + $response->header( 'Cache-Control', 'max-age=15, s-maxage=0' ); | |
| 258 | + return $response; | |
| 259 | + } | |
| 236 | 260 | |
| 237 | 261 | $id = absint( $request->get_param( 'top_ten_id' ) ); |
| 262 | + $sitewide_context = sanitize_text_field( $request->get_param( 'top_ten_sitewide_context' ) ); | |
| 238 | 263 | $blog_id = absint( $request->get_param( 'top_ten_blog_id' ) ); |
| 239 | 264 | $activate_counter = absint( $request->get_param( 'activate_counter' ) ); |
| 240 | 265 | $top_ten_debug = absint( $request->get_param( 'top_ten_debug' ) ); |
| 241 | 266 | |
| 242 | 267 | $str = \WebberZone\Top_Ten\Tracker::update_count( $id, $blog_id, $activate_counter ); |
| 268 | + if ( '' !== $sitewide_context ) { | |
| 269 | + $str .= \WebberZone\Top_Ten\Tracker::update_sitewide_count( $sitewide_context, $blog_id, $activate_counter ); | |
| 270 | + } | |
| 243 | 271 | |
| 244 | 272 | if ( 1 === $top_ten_debug ) { |
| 245 | 273 | return rest_ensure_response( $str ); |
| 246 | 274 | } else { |
| @@ -314,10 +342,25 @@ | ||
| 314 | 342 | 'type' => 'string', |
| 315 | 343 | 'enum' => array( 'view', 'embed', 'edit' ), |
| 316 | 344 | 'default' => 'view', |
| 317 | 345 | ), |
| 346 | + 'lang' => array( | |
| 347 | + 'description' => esc_html__( 'TranslatePress language code to render the response in.', 'top-10' ), | |
| 348 | + 'type' => 'string', | |
| 349 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 350 | + 'validate_callback' => static function ( $value ) { | |
| 351 | + return is_string( $value ) && strlen( $value ) <= 20; | |
| 352 | + }, | |
| 353 | + ), | |
| 318 | 354 | ); |
| 319 | 355 | |
| 356 | + /** | |
| 357 | + * Filters the query parameters accepted by the popular posts REST endpoint. | |
| 358 | + * | |
| 359 | + * @since 3.0.0 | |
| 360 | + * | |
| 361 | + * @param array $args Collection parameters, keyed by parameter name. | |
| 362 | + */ | |
| 320 | 363 | return apply_filters( 'top_ten_rest_api_get_items_params', $args ); |
| 321 | 364 | } |
| 322 | 365 | |
| 323 | 366 | /** |
| @@ -328,24 +371,29 @@ | ||
| 328 | 371 | * @return array Top 10 REST API popular posts arguments. |
| 329 | 372 | */ |
| 330 | 373 | public function get_tracker_params() { |
| 331 | 374 | $args = array( |
| 332 | - 'top_ten_id' => array( | |
| 375 | + 'top_ten_id' => array( | |
| 333 | 376 | 'description' => esc_html__( 'ID of the post.', 'top-10' ), |
| 334 | 377 | 'type' => 'integer', |
| 335 | 378 | 'sanitize_callback' => 'absint', |
| 336 | 379 | ), |
| 337 | - 'top_ten_blog_id' => array( | |
| 380 | + 'top_ten_sitewide_context' => array( | |
| 381 | + 'description' => esc_html__( 'Site-wide tracking context: front_page, home_page, search, category, tag, taxonomy, author, post_type_archive, date_archive, archive or other. Each key maps to a fixed reserved ID in the shared count tables.', 'top-10' ), | |
| 382 | + 'type' => 'string', | |
| 383 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 384 | + ), | |
| 385 | + 'top_ten_blog_id' => array( | |
| 338 | 386 | 'description' => esc_html__( 'Blog ID of the post.', 'top-10' ), |
| 339 | 387 | 'type' => 'integer', |
| 340 | 388 | 'sanitize_callback' => 'absint', |
| 341 | 389 | ), |
| 342 | - 'activate_counter' => array( | |
| 390 | + 'activate_counter' => array( | |
| 343 | 391 | 'description' => esc_html__( 'Activate counter flag.', 'top-10' ), |
| 344 | 392 | 'type' => 'integer', |
| 345 | 393 | 'sanitize_callback' => 'absint', |
| 346 | 394 | ), |
| 347 | - 'top_ten_debug' => array( | |
| 395 | + 'top_ten_debug' => array( | |
| 348 | 396 | 'description' => esc_html__( 'Debug flag.', 'top-10' ), |
| 349 | 397 | 'type' => 'integer', |
| 350 | 398 | 'sanitize_callback' => 'absint', |
| 351 | 399 | ), |
| @@ -350,8 +398,15 @@ | ||
| 350 | 398 | 'sanitize_callback' => 'absint', |
| 351 | 399 | ), |
| 352 | 400 | ); |
| 353 | 401 | |
| 402 | + /** | |
| 403 | + * Filters the query parameters accepted by the tracker REST endpoint. | |
| 404 | + * | |
| 405 | + * @since 3.0.0 | |
| 406 | + * | |
| 407 | + * @param array $args Collection parameters, keyed by parameter name. | |
| 408 | + */ | |
| 354 | 409 | return apply_filters( 'top_ten_rest_api_get_tracker_params', $args ); |
| 355 | 410 | } |
| 356 | 411 | |
| 357 | 412 | /** |
| @@ -389,8 +444,15 @@ | ||
| 389 | 444 | 'sanitize_callback' => 'sanitize_text_field', |
| 390 | 445 | ), |
| 391 | 446 | ); |
| 392 | 447 | |
| 448 | + /** | |
| 449 | + * Filters the query parameters accepted by the counter REST endpoint. | |
| 450 | + * | |
| 451 | + * @since 3.4.0-RC1 | |
| 452 | + * | |
| 453 | + * @param array $args Collection parameters, keyed by parameter name. | |
| 454 | + */ | |
| 393 | 455 | return apply_filters( 'top_ten_rest_api_get_counter_params', $args ); |
| 394 | 456 | } |
| 395 | 457 | |
| 396 | 458 | /** |