PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.4.7
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.4.7
3.4.4 3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 All 197 releases
← All changes | admin/class-convertkit-admin-refresh-resources.php +17 -78 3.4.22.4.7 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 = sanitize_text_field( $_REQUEST['resource'] );
79 42
80 43 // Fetch resources.
81 44 switch ( $resource ) {
82 45 case 'forms':
@@ -81,25 +44,13 @@
81 44 switch ( $resource ) {
82 45 case 'forms':
83 46 $forms = new ConvertKit_Resource_Forms( 'user_refresh_resource' );
84 47 $results = $forms->refresh();
85 -
86 - // Only return non-legacy forms.
87 - if ( ! is_wp_error( $results ) ) {
88 - $non_legacy = $forms->get_non_legacy();
89 - $results = is_array( $non_legacy ) ? $non_legacy : array();
90 - }
91 48 break;
92 49
93 50 case 'landing_pages':
94 51 $landing_pages = new ConvertKit_Resource_Landing_Pages( 'user_refresh_resource' );
95 52 $results = $landing_pages->refresh();
96 -
97 - // Only return non-legacy landing pages.
98 - if ( ! is_wp_error( $results ) ) {
99 - $non_legacy = $landing_pages->get_non_legacy();
100 - $results = is_array( $non_legacy ) ? $non_legacy : array();
101 - }
102 53 break;
103 54
104 55 case 'tags':
105 56 $tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' );
@@ -116,21 +67,8 @@
116 67 $results = $products->refresh();
117 68 break;
118 69
119 70 case 'restrict_content':
120 - // Fetch Forms.
121 - $forms = new ConvertKit_Resource_Forms( 'user_refresh_resource' );
122 - $results_forms = $forms->refresh();
123 -
124 - // Bail if an error occured.
125 - if ( is_wp_error( $results_forms ) ) {
126 - return rest_ensure_response( $results_forms );
127 - }
128 -
129 - // Only return non-legacy forms.
130 - $non_legacy_forms = $forms->get_non_legacy();
131 - $results_forms = is_array( $non_legacy_forms ) ? $non_legacy_forms : array();
132 -
133 71 // Fetch Tags.
134 72 $tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' );
135 73 $results_tags = $tags->refresh();
136 74
@@ -135,9 +73,9 @@
135 73 $results_tags = $tags->refresh();
136 74
137 75 // Bail if an error occured.
138 76 if ( is_wp_error( $results_tags ) ) {
139 - return rest_ensure_response( $results_tags );
77 + wp_send_json_error( $results_tags->get_error_message() );
140 78 }
141 79
142 80 // Fetch Products.
143 81 $products = new ConvertKit_Resource_Products( 'user_refresh_resource' );
@@ -144,19 +82,19 @@
144 82 $results_products = $products->refresh();
145 83
146 84 // Bail if an error occured.
147 85 if ( is_wp_error( $results_products ) ) {
148 - return rest_ensure_response( $results_products );
86 + wp_send_json_error( $results_products->get_error_message() );
149 87 }
150 88
151 89 // Return resources.
152 - return rest_ensure_response(
90 + wp_send_json_success(
153 91 array(
154 - 'forms' => array_values( $results_forms ),
155 92 'tags' => array_values( $results_tags ),
156 93 'products' => array_values( $results_products ),
157 94 )
158 95 );
96 + // no break as wp_send_json_success terminates.
159 97
160 98 default:
161 99 $results = new WP_Error(
162 100 'convertkit_admin_refresh_resources_error',
@@ -169,13 +107,13 @@
169 107 }
170 108
171 109 // Bail if an error occured.
172 110 if ( is_wp_error( $results ) ) {
173 - return rest_ensure_response( $results );
111 + wp_send_json_error( $results->get_error_message() );
174 112 }
175 113
176 114 // Return resources as a zero based sequential array, so that JS retains the order of resources.
177 - return rest_ensure_response( array_values( $results ) );
115 + wp_send_json_success( array_values( $results ) );
178 116
179 117 }
180 118
181 119 /**
@@ -194,10 +132,10 @@
194 132
195 133 // Get settings.
196 134 $settings = new ConvertKit_Settings();
197 135
198 - // Bail if no Access Token is defined.
199 - if ( ! $settings->has_access_and_refresh_token() ) {
136 + // Bail if no API keys are defined.
137 + if ( ! $settings->has_api_key_and_secret() ) {
200 138 return;
201 139 }
202 140
203 141 // Enqueue JS to perform AJAX request to refresh resources.
@@ -205,11 +143,12 @@
205 143 wp_localize_script(
206 144 'convertkit-admin-refresh-resources',
207 145 'convertkit_admin_refresh_resources',
208 146 array(
209 - 'ajaxurl' => rest_url( 'kit/v1/resources/refresh/' ),
147 + 'action' => 'convertkit_admin_refresh_resources',
148 + 'ajaxurl' => admin_url( 'admin-ajax.php' ),
210 149 'debug' => $settings->debug_enabled(),
211 - 'nonce' => wp_create_nonce( 'wp_rest' ),
150 + 'nonce' => wp_create_nonce( 'convertkit_admin_refresh_resources' ),
212 151 )
213 152 );
214 153
215 154 }