PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.3
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 2.3.1 All 196 releases
← All changes | admin/class-convertkit-admin-refresh-resources.php +104 -15 2.3.13.4.3 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':
@@ -44,13 +81,25 @@
44 81 switch ( $resource ) {
45 82 case 'forms':
46 83 $forms = new ConvertKit_Resource_Forms( 'user_refresh_resource' );
47 84 $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 + }
48 91 break;
49 92
50 93 case 'landing_pages':
51 94 $landing_pages = new ConvertKit_Resource_Landing_Pages( 'user_refresh_resource' );
52 95 $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 + }
53 102 break;
54 103
55 104 case 'tags':
56 105 $tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' );
@@ -62,12 +111,53 @@
62 111 $results = $posts->refresh();
63 112 break;
64 113
65 114 case 'products':
66 - $products = new ConvertKit_Resource_Products();
115 + $products = new ConvertKit_Resource_Products( 'user_refresh_resource' );
67 116 $results = $products->refresh();
68 117 break;
69 118
119 + 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 + // Fetch Tags.
134 + $tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' );
135 + $results_tags = $tags->refresh();
136 +
137 + // Bail if an error occured.
138 + if ( is_wp_error( $results_tags ) ) {
139 + return rest_ensure_response( $results_tags );
140 + }
141 +
142 + // Fetch Products.
143 + $products = new ConvertKit_Resource_Products( 'user_refresh_resource' );
144 + $results_products = $products->refresh();
145 +
146 + // Bail if an error occured.
147 + if ( is_wp_error( $results_products ) ) {
148 + return rest_ensure_response( $results_products );
149 + }
150 +
151 + // Return resources.
152 + return rest_ensure_response(
153 + array(
154 + 'forms' => array_values( $results_forms ),
155 + 'tags' => array_values( $results_tags ),
156 + 'products' => array_values( $results_products ),
157 + )
158 + );
159 +
70 160 default:
71 161 $results = new WP_Error(
72 162 'convertkit_admin_refresh_resources_error',
73 163 sprintf(
@@ -79,13 +169,13 @@
79 169 }
80 170
81 171 // Bail if an error occured.
82 172 if ( is_wp_error( $results ) ) {
83 - wp_send_json_error( $results->get_error_message() );
173 + return rest_ensure_response( $results );
84 174 }
85 175
86 176 // Return resources as a zero based sequential array, so that JS retains the order of resources.
87 - wp_send_json_success( array_values( $results ) );
177 + return rest_ensure_response( array_values( $results ) );
88 178
89 179 }
90 180
91 181 /**
@@ -104,23 +194,22 @@
104 194
105 195 // Get settings.
106 196 $settings = new ConvertKit_Settings();
107 197
108 - // Bail if no API keys are defined.
109 - if ( ! $settings->has_api_key_and_secret() ) {
198 + // Bail if no Access Token is defined.
199 + if ( ! $settings->has_access_and_refresh_token() ) {
110 200 return;
111 201 }
112 202
113 203 // 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 );
204 + wp_enqueue_script( 'convertkit-admin-refresh-resources', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/refresh-resources.js', array(), CONVERTKIT_PLUGIN_VERSION, true );
115 205 wp_localize_script(
116 206 'convertkit-admin-refresh-resources',
117 207 'convertkit_admin_refresh_resources',
118 208 array(
119 - 'action' => 'convertkit_admin_refresh_resources',
120 - 'ajaxurl' => admin_url( 'admin-ajax.php' ),
209 + 'ajaxurl' => rest_url( 'kit/v1/resources/refresh/' ),
121 210 'debug' => $settings->debug_enabled(),
122 - 'nonce' => wp_create_nonce( 'convertkit_admin_refresh_resources' ),
211 + 'nonce' => wp_create_nonce( 'wp_rest' ),
123 212 )
124 213 );
125 214
126 215 }