business-hours.php
4 years ago
class-wpcom-rest-api-v2-endpoint-admin-menu.php
3 years ago
class-wpcom-rest-api-v2-endpoint-ai.php
2 years ago
class-wpcom-rest-api-v2-endpoint-external-media.php
3 years ago
class-wpcom-rest-api-v2-endpoint-following.php
2 years ago
class-wpcom-rest-api-v2-endpoint-google-docs.php
4 years ago
class-wpcom-rest-api-v2-endpoint-instagram-gallery.php
3 years ago
class-wpcom-rest-api-v2-endpoint-mailchimp.php
3 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-list.php
2 years ago
class-wpcom-rest-api-v2-endpoint-podcast-player.php
3 years ago
class-wpcom-rest-api-v2-endpoint-publicize-share-post.php
3 years ago
class-wpcom-rest-api-v2-endpoint-related-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-resolve-redirect.php
2 years ago
class-wpcom-rest-api-v2-endpoint-search.php
4 years ago
class-wpcom-rest-api-v2-endpoint-send-email-preview.php
2 years ago
class-wpcom-rest-api-v2-endpoint-template-loader.php
3 years ago
class-wpcom-rest-api-v2-endpoint-transient.php
5 years ago
class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php
3 years ago
class-wpcom-rest-api-v2-endpoint-tweetstorm-parse.php
3 years ago
class-wpcom-rest-api-v3-endpoint-blogging-prompts.php
3 years ago
gutenberg-available-extensions.php
4 years ago
hello.php
4 years ago
memberships.php
3 years ago
publicize-connection-test-results.php
3 years ago
publicize-connections.php
3 years ago
publicize-services.php
3 years ago
service-api-keys.php
3 years ago
sites-posts-featured-media-url.php
4 years ago
subscribers.php
3 years ago
trait-wpcom-rest-api-proxy-request-trait.php
2 years ago
class-wpcom-rest-api-v2-endpoint-transient.php
88 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API endpoint for editing Jetpack Transients. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | * @since 9.7.0 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Jetpack transients API. |
| 11 | * |
| 12 | * @since 9.7.0 |
| 13 | */ |
| 14 | class WPCOM_REST_API_V2_Endpoint_Transient extends WP_REST_Controller { |
| 15 | /** |
| 16 | * Constructor. |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | $this->namespace = 'wpcom/v2'; |
| 20 | $this->rest_base = 'transients'; |
| 21 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Called automatically on `rest_api_init()`. |
| 26 | */ |
| 27 | public function register_routes() { |
| 28 | // DELETE /sites/<blog-id>/transients/$name route. |
| 29 | register_rest_route( |
| 30 | $this->namespace, |
| 31 | '/' . $this->rest_base . '/(?P<name>\w{1,172})', |
| 32 | array( |
| 33 | array( |
| 34 | 'methods' => WP_REST_Server::DELETABLE, |
| 35 | 'callback' => array( $this, 'delete_transient' ), |
| 36 | 'permission_callback' => array( $this, 'delete_transient_permissions_check' ), |
| 37 | 'args' => array( |
| 38 | 'name' => array( |
| 39 | 'description' => __( 'The name of the transient to delete.', 'jetpack' ), |
| 40 | 'required' => true, |
| 41 | 'type' => 'string', |
| 42 | 'sanitize_callback' => 'sanitize_text_field', |
| 43 | ), |
| 44 | ), |
| 45 | ), |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Delete transient callback. |
| 52 | * |
| 53 | * @param \WP_REST_Request $request Full details about the request. |
| 54 | * @return array |
| 55 | */ |
| 56 | public function delete_transient( \WP_REST_Request $request ) { |
| 57 | return array( |
| 58 | 'success' => delete_transient( $request->get_param( 'name' ) ), |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Check if the user has read access, the transient name starts with |
| 64 | * "jetpack_connected_user_data_", and that the user is editing |
| 65 | * their own transient. |
| 66 | * |
| 67 | * @param \WP_REST_Request $request Full details about the request. |
| 68 | * @return bool|WP_Error |
| 69 | */ |
| 70 | public function delete_transient_permissions_check( \WP_REST_Request $request ) { |
| 71 | $transient_name = $request->get_param( 'name' ); |
| 72 | $current_user_id = get_current_user_id(); |
| 73 | |
| 74 | if ( current_user_can( 'read' ) && |
| 75 | "jetpack_connected_user_data_{$current_user_id}" === $transient_name ) { |
| 76 | return true; |
| 77 | } else { |
| 78 | return new WP_Error( |
| 79 | 'authorization_required', |
| 80 | __( 'Sorry, you are not allowed to delete this transient.', 'jetpack' ), |
| 81 | array( 'status' => 403 ) |
| 82 | ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Transient' ); |
| 88 |