← All changes
|
admin/class-convertkit-admin-refresh-resources.php
+15
-60
3.3.2
→
2.8.6
View file →
| @@ -21,62 +21,25 @@ | ||
| 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' ) ); | |
| 25 | 26 | 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 | - /** | |
| 68 | 31 | * Refreshes resources (forms, landing pages or tags) from the API, returning them as a JSON string. |
| 69 | 32 | * |
| 70 | 33 | * @since 1.9.8.0 |
| 71 | - * | |
| 72 | - * @param WP_REST_Request $request Request object. | |
| 73 | - * @return WP_REST_Response|WP_Error Response object. | |
| 74 | 34 | */ |
| 75 | - public function refresh_resources( $request ) { | |
| 35 | + public function refresh_resources() { | |
| 76 | 36 | |
| 37 | + // Check nonce. | |
| 38 | + check_ajax_referer( 'convertkit_admin_refresh_resources', 'nonce' ); | |
| 39 | + | |
| 77 | 40 | // Get resource type. |
| 78 | - $resource = $request->get_param( 'resource' ); | |
| 41 | + $resource = ( isset( $_REQUEST['resource'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['resource'] ) ) : '' ); | |
| 79 | 42 | |
| 80 | 43 | // Fetch resources. |
| 81 | 44 | switch ( $resource ) { |
| 82 | 45 | case 'forms': |
| @@ -104,17 +67,8 @@ | ||
| 104 | 67 | $results = $products->refresh(); |
| 105 | 68 | break; |
| 106 | 69 | |
| 107 | 70 | case 'restrict_content': |
| 108 | - // Fetch Forms. | |
| 109 | - $forms = new ConvertKit_Resource_Forms( 'user_refresh_resource' ); | |
| 110 | - $results_forms = $forms->refresh(); | |
| 111 | - | |
| 112 | - // Bail if an error occured. | |
| 113 | - if ( is_wp_error( $results_forms ) ) { | |
| 114 | - return rest_ensure_response( $results_forms ); | |
| 115 | - } | |
| 116 | - | |
| 117 | 71 | // Fetch Tags. |
| 118 | 72 | $tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' ); |
| 119 | 73 | $results_tags = $tags->refresh(); |
| 120 | 74 | |
| @@ -119,9 +73,9 @@ | ||
| 119 | 73 | $results_tags = $tags->refresh(); |
| 120 | 74 | |
| 121 | 75 | // Bail if an error occured. |
| 122 | 76 | if ( is_wp_error( $results_tags ) ) { |
| 123 | - return rest_ensure_response( $results_tags ); | |
| 77 | + wp_send_json_error( $results_tags->get_error_message() ); | |
| 124 | 78 | } |
| 125 | 79 | |
| 126 | 80 | // Fetch Products. |
| 127 | 81 | $products = new ConvertKit_Resource_Products( 'user_refresh_resource' ); |
| @@ -128,19 +82,19 @@ | ||
| 128 | 82 | $results_products = $products->refresh(); |
| 129 | 83 | |
| 130 | 84 | // Bail if an error occured. |
| 131 | 85 | if ( is_wp_error( $results_products ) ) { |
| 132 | - return rest_ensure_response( $results_products ); | |
| 86 | + wp_send_json_error( $results_products->get_error_message() ); | |
| 133 | 87 | } |
| 134 | 88 | |
| 135 | 89 | // Return resources. |
| 136 | - return rest_ensure_response( | |
| 90 | + wp_send_json_success( | |
| 137 | 91 | array( |
| 138 | - 'forms' => array_values( $results_forms ), | |
| 139 | 92 | 'tags' => array_values( $results_tags ), |
| 140 | 93 | 'products' => array_values( $results_products ), |
| 141 | 94 | ) |
| 142 | 95 | ); |
| 96 | + // no break as wp_send_json_success terminates. | |
| 143 | 97 | |
| 144 | 98 | default: |
| 145 | 99 | $results = new WP_Error( |
| 146 | 100 | 'convertkit_admin_refresh_resources_error', |
| @@ -153,13 +107,13 @@ | ||
| 153 | 107 | } |
| 154 | 108 | |
| 155 | 109 | // Bail if an error occured. |
| 156 | 110 | if ( is_wp_error( $results ) ) { |
| 157 | - return rest_ensure_response( $results ); | |
| 111 | + wp_send_json_error( $results->get_error_message() ); | |
| 158 | 112 | } |
| 159 | 113 | |
| 160 | 114 | // Return resources as a zero based sequential array, so that JS retains the order of resources. |
| 161 | - return rest_ensure_response( array_values( $results ) ); | |
| 115 | + wp_send_json_success( array_values( $results ) ); | |
| 162 | 116 | |
| 163 | 117 | } |
| 164 | 118 | |
| 165 | 119 | /** |
| @@ -189,11 +143,12 @@ | ||
| 189 | 143 | wp_localize_script( |
| 190 | 144 | 'convertkit-admin-refresh-resources', |
| 191 | 145 | 'convertkit_admin_refresh_resources', |
| 192 | 146 | array( |
| 193 | - 'ajaxurl' => rest_url( 'kit/v1/resources/refresh/' ), | |
| 147 | + 'action' => 'convertkit_admin_refresh_resources', | |
| 148 | + 'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
| 194 | 149 | 'debug' => $settings->debug_enabled(), |
| 195 | - 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 150 | + 'nonce' => wp_create_nonce( 'convertkit_admin_refresh_resources' ), | |
| 196 | 151 | ) |
| 197 | 152 | ); |
| 198 | 153 | |
| 199 | 154 | } |