← All changes
|
admin/class-convertkit-admin-refresh-resources.php
+78
-15
2.2.6
→
3.3.0
View file →
| @@ -21,25 +21,62 @@ | ||
| 21 | 21 | * @since 1.9.8.0 |
| 22 | 22 | */ |
| 23 | 23 | public function __construct() { |
| 24 | 24 | |
| 25 | - add_action( 'wp_ajax_convertkit_admin_refresh_resources', array( $this, 'refresh_resources' ) ); | |
| 26 | 25 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 26 | + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); | |
| 27 | 27 | |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | + * Register REST API routes. | |
| 32 | + * | |
| 33 | + * @since 3.1.0 | |
| 34 | + */ | |
| 35 | + public function register_routes() { | |
| 36 | + | |
| 37 | + // Register route to return all blocks registered by the Plugin. | |
| 38 | + register_rest_route( | |
| 39 | + 'kit/v1', | |
| 40 | + '/resources/refresh/(?P<resource>[a-zA-Z0-9-_]+)', | |
| 41 | + array( | |
| 42 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 43 | + 'args' => array( | |
| 44 | + // Resource: Validate resource is included in the request, a valid resource | |
| 45 | + // and sanitize the resource. | |
| 46 | + 'resource' => array( | |
| 47 | + 'required' => true, | |
| 48 | + 'validate_callback' => function ( $param ) { | |
| 49 | + | |
| 50 | + return is_string( $param ) && in_array( $param, array( 'forms', 'landing_pages', 'tags', 'posts', 'products', 'restrict_content' ), true ); | |
| 51 | + | |
| 52 | + }, | |
| 53 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 54 | + ), | |
| 55 | + ), | |
| 56 | + 'callback' => array( $this, 'refresh_resources' ), | |
| 57 | + | |
| 58 | + // Only refresh resources for users who can edit posts. | |
| 59 | + 'permission_callback' => function () { | |
| 60 | + return current_user_can( 'edit_posts' ); | |
| 61 | + }, | |
| 62 | + ) | |
| 63 | + ); | |
| 64 | + | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 31 | 68 | * Refreshes resources (forms, landing pages or tags) from the API, returning them as a JSON string. |
| 32 | 69 | * |
| 33 | 70 | * @since 1.9.8.0 |
| 71 | + * | |
| 72 | + * @param WP_REST_Request $request Request object. | |
| 73 | + * @return WP_REST_Response|WP_Error Response object. | |
| 34 | 74 | */ |
| 35 | - public function refresh_resources() { | |
| 75 | + public function refresh_resources( $request ) { | |
| 36 | 76 | |
| 37 | - // Check nonce. | |
| 38 | - check_ajax_referer( 'convertkit_admin_refresh_resources', 'nonce' ); | |
| 39 | - | |
| 40 | 77 | // Get resource type. |
| 41 | - $resource = sanitize_text_field( $_REQUEST['resource'] ); | |
| 78 | + $resource = $request->get_param( 'resource' ); | |
| 42 | 79 | |
| 43 | 80 | // Fetch resources. |
| 44 | 81 | switch ( $resource ) { |
| 45 | 82 | case 'forms': |
| @@ -62,12 +99,39 @@ | ||
| 62 | 99 | $results = $posts->refresh(); |
| 63 | 100 | break; |
| 64 | 101 | |
| 65 | 102 | case 'products': |
| 66 | - $products = new ConvertKit_Resource_Products(); | |
| 103 | + $products = new ConvertKit_Resource_Products( 'user_refresh_resource' ); | |
| 67 | 104 | $results = $products->refresh(); |
| 68 | 105 | break; |
| 69 | 106 | |
| 107 | + case 'restrict_content': | |
| 108 | + // Fetch Tags. | |
| 109 | + $tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' ); | |
| 110 | + $results_tags = $tags->refresh(); | |
| 111 | + | |
| 112 | + // Bail if an error occured. | |
| 113 | + if ( is_wp_error( $results_tags ) ) { | |
| 114 | + return rest_ensure_response( $results_tags ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + // Fetch Products. | |
| 118 | + $products = new ConvertKit_Resource_Products( 'user_refresh_resource' ); | |
| 119 | + $results_products = $products->refresh(); | |
| 120 | + | |
| 121 | + // Bail if an error occured. | |
| 122 | + if ( is_wp_error( $results_products ) ) { | |
| 123 | + return rest_ensure_response( $results_products ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + // Return resources. | |
| 127 | + return rest_ensure_response( | |
| 128 | + array( | |
| 129 | + 'tags' => array_values( $results_tags ), | |
| 130 | + 'products' => array_values( $results_products ), | |
| 131 | + ) | |
| 132 | + ); | |
| 133 | + | |
| 70 | 134 | default: |
| 71 | 135 | $results = new WP_Error( |
| 72 | 136 | 'convertkit_admin_refresh_resources_error', |
| 73 | 137 | sprintf( |
| @@ -79,13 +143,13 @@ | ||
| 79 | 143 | } |
| 80 | 144 | |
| 81 | 145 | // Bail if an error occured. |
| 82 | 146 | if ( is_wp_error( $results ) ) { |
| 83 | - wp_send_json_error( $results->get_error_message() ); | |
| 147 | + return rest_ensure_response( $results ); | |
| 84 | 148 | } |
| 85 | 149 | |
| 86 | 150 | // Return resources as a zero based sequential array, so that JS retains the order of resources. |
| 87 | - wp_send_json_success( array_values( $results ) ); | |
| 151 | + return rest_ensure_response( array_values( $results ) ); | |
| 88 | 152 | |
| 89 | 153 | } |
| 90 | 154 | |
| 91 | 155 | /** |
| @@ -104,23 +168,22 @@ | ||
| 104 | 168 | |
| 105 | 169 | // Get settings. |
| 106 | 170 | $settings = new ConvertKit_Settings(); |
| 107 | 171 | |
| 108 | - // Bail if no API keys are defined. | |
| 109 | - if ( ! $settings->has_api_key_and_secret() ) { | |
| 172 | + // Bail if no Access Token is defined. | |
| 173 | + if ( ! $settings->has_access_and_refresh_token() ) { | |
| 110 | 174 | return; |
| 111 | 175 | } |
| 112 | 176 | |
| 113 | 177 | // Enqueue JS to perform AJAX request to refresh resources. |
| 114 | - wp_enqueue_script( 'convertkit-admin-refresh-resources', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/refresh-resources.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 178 | + wp_enqueue_script( 'convertkit-admin-refresh-resources', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/refresh-resources.js', array(), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 115 | 179 | wp_localize_script( |
| 116 | 180 | 'convertkit-admin-refresh-resources', |
| 117 | 181 | 'convertkit_admin_refresh_resources', |
| 118 | 182 | array( |
| 119 | - 'action' => 'convertkit_admin_refresh_resources', | |
| 120 | - 'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
| 183 | + 'ajaxurl' => rest_url( 'kit/v1/resources/refresh/' ), | |
| 121 | 184 | 'debug' => $settings->debug_enabled(), |
| 122 | - 'nonce' => wp_create_nonce( 'convertkit_admin_refresh_resources' ), | |
| 185 | + 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 123 | 186 | ) |
| 124 | 187 | ); |
| 125 | 188 | |
| 126 | 189 | } |