PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.1.8
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.1.8
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 2.3.2 All 195 releases
convertkit / admin / class-convertkit-admin-refresh-resources.php

class-convertkit-admin-refresh-resources.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.1.8, at admin/class-convertkit-admin-refresh-resources.php

192 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Admin Refresh Resources class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Registers scripts, which run when a Refresh button is clicked, to refresh resources
11 * asynchronously whilst editing a Page, Post or Category.
12 *
13 * @package ConvertKit
14 * @author ConvertKit
15 */
16 class ConvertKit_Admin_Refresh_Resources {
17
18 /**
19 * Registers action and filter hooks.
20 *
21 * @since 1.9.8.0
22 */
23 public function __construct() {
24
25 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
26 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
27
28 }
29
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 * Refreshes resources (forms, landing pages or tags) from the API, returning them as a JSON string.
69 *
70 * @since 1.9.8.0
71 *
72 * @param WP_REST_Request $request Request object.
73 * @return WP_REST_Response|WP_Error Response object.
74 */
75 public function refresh_resources( $request ) {
76
77 // Get resource type.
78 $resource = $request->get_param( 'resource' );
79
80 // Fetch resources.
81 switch ( $resource ) {
82 case 'forms':
83 $forms = new ConvertKit_Resource_Forms( 'user_refresh_resource' );
84 $results = $forms->refresh();
85 break;
86
87 case 'landing_pages':
88 $landing_pages = new ConvertKit_Resource_Landing_Pages( 'user_refresh_resource' );
89 $results = $landing_pages->refresh();
90 break;
91
92 case 'tags':
93 $tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' );
94 $results = $tags->refresh();
95 break;
96
97 case 'posts':
98 $posts = new ConvertKit_Resource_Posts( 'user_refresh_resource' );
99 $results = $posts->refresh();
100 break;
101
102 case 'products':
103 $products = new ConvertKit_Resource_Products( 'user_refresh_resource' );
104 $results = $products->refresh();
105 break;
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
134 default:
135 $results = new WP_Error(
136 'convertkit_admin_refresh_resources_error',
137 sprintf(
138 'Resource type %s is not supported in ConvertKit_Admin_Refresh_Resources class.',
139 $resource
140 )
141 );
142 break;
143 }
144
145 // Bail if an error occured.
146 if ( is_wp_error( $results ) ) {
147 return rest_ensure_response( $results );
148 }
149
150 // Return resources as a zero based sequential array, so that JS retains the order of resources.
151 return rest_ensure_response( array_values( $results ) );
152
153 }
154
155 /**
156 * Enqueue JavaScript when editing a Page, Post, Custom Post Type or Category.
157 *
158 * @since 1.9.8.0
159 *
160 * @param string $hook Hook.
161 */
162 public function enqueue_scripts( $hook ) {
163
164 // Bail if we are not on an Edit or Term screen.
165 if ( ! in_array( $hook, array( 'edit.php', 'post-new.php', 'term.php', 'edit-tags.php', 'post.php' ), true ) ) {
166 return;
167 }
168
169 // Get settings.
170 $settings = new ConvertKit_Settings();
171
172 // Bail if no Access Token is defined.
173 if ( ! $settings->has_access_and_refresh_token() ) {
174 return;
175 }
176
177 // Enqueue JS to perform AJAX request to refresh resources.
178 wp_enqueue_script( 'convertkit-admin-refresh-resources', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/refresh-resources.js', array(), CONVERTKIT_PLUGIN_VERSION, true );
179 wp_localize_script(
180 'convertkit-admin-refresh-resources',
181 'convertkit_admin_refresh_resources',
182 array(
183 'ajaxurl' => rest_url( 'kit/v1/resources/refresh/' ),
184 'debug' => $settings->debug_enabled(),
185 'nonce' => wp_create_nonce( 'wp_rest' ),
186 )
187 );
188
189 }
190
191 }
192