PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.7.2
Block Animations, Motion & Scroll Effects – Ghost Kit v3.7.2
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
← All changes | classes/class-rest.php +1700 -1524 2.25.03.7.2 View file →
@@ -5,9 +5,9 @@
5 5 * @package ghostkit
6 6 */
7 7
8 8 if ( ! defined( 'ABSPATH' ) ) {
9 - exit;
9 + exit;
10 10 }
11 11
12 12 /**
13 13 * Class GhostKit_Rest
@@ -12,1782 +12,1958 @@
12 12 /**
13 13 * Class GhostKit_Rest
14 14 */
15 15 class GhostKit_Rest extends WP_REST_Controller {
16 - /**
17 - * Namespace.
18 - *
19 - * @var string
20 - */
21 - protected $namespace = 'ghostkit/v';
16 + /**
17 + * Namespace.
18 + *
19 + * @var string
20 + */
21 + protected $namespace = 'ghostkit/v';
22 22
23 - /**
24 - * Version.
25 - *
26 - * @var string
27 - */
28 - protected $version = '1';
23 + /**
24 + * Version.
25 + *
26 + * @var string
27 + */
28 + protected $version = '1';
29 29
30 - /**
31 - * GhostKit_Rest constructor.
32 - */
33 - public function __construct() {
34 - add_action( 'rest_api_init', array( $this, 'register_routes' ) );
35 - }
30 + /**
31 + * GhostKit_Rest constructor.
32 + */
33 + public function __construct() {
34 + add_action( 'rest_api_init', array( $this, 'register_routes' ) );
35 + }
36 36
37 - /**
38 - * Register rest routes.
39 - */
40 - public function register_routes() {
41 - $namespace = $this->namespace . $this->version;
37 + /**
38 + * Register rest routes.
39 + */
40 + public function register_routes() {
41 + $namespace = $this->namespace . $this->version;
42 42
43 - // Get layouts list.
44 - register_rest_route(
45 - $namespace,
46 - '/get_customizer/',
47 - array(
48 - 'methods' => WP_REST_Server::READABLE,
49 - 'callback' => array( $this, 'get_customizer' ),
50 - 'permission_callback' => array( $this, 'get_customizer_permission' ),
51 - )
52 - );
43 + // Get layouts list.
44 + register_rest_route(
45 + $namespace,
46 + '/get_customizer/',
47 + array(
48 + 'methods' => WP_REST_Server::READABLE,
49 + 'callback' => array( $this, 'get_customizer' ),
50 + 'permission_callback' => array( $this, 'get_customizer_permission' ),
51 + )
52 + );
53 53
54 - // Get attachment image <img> tag.
55 - register_rest_route(
56 - $namespace,
57 - '/get_attachment_image/(?P<id>[\d]+)',
58 - array(
59 - 'methods' => WP_REST_Server::READABLE,
60 - 'callback' => array( $this, 'get_attachment_image' ),
61 - 'permission_callback' => array( $this, 'get_attachment_image_permission' ),
62 - )
63 - );
54 + // Get attachment image <img> tag.
55 + register_rest_route(
56 + $namespace,
57 + '/get_attachment_image/(?P<id>[\d]+)',
58 + array(
59 + 'methods' => WP_REST_Server::READABLE,
60 + 'callback' => array( $this, 'get_attachment_image' ),
61 + 'permission_callback' => array( $this, 'get_attachment_image_permission' ),
62 + )
63 + );
64 64
65 - // Get Instagram profile.
66 - register_rest_route(
67 - $namespace,
68 - '/get_instagram_profile/',
69 - array(
70 - 'methods' => WP_REST_Server::READABLE,
71 - 'callback' => array( $this, 'get_instagram_profile' ),
72 - 'permission_callback' => array( $this, 'get_instagram_profile_permission' ),
73 - )
74 - );
65 + // Get Instagram profile.
66 + register_rest_route(
67 + $namespace,
68 + '/get_instagram_profile/',
69 + array(
70 + 'methods' => WP_REST_Server::READABLE,
71 + 'callback' => array( $this, 'get_instagram_profile' ),
72 + 'permission_callback' => array( $this, 'get_instagram_profile_permission' ),
73 + )
74 + );
75 75
76 - // Get Instagram feed.
77 - register_rest_route(
78 - $namespace,
79 - '/get_instagram_feed/',
80 - array(
81 - 'methods' => WP_REST_Server::READABLE,
82 - 'callback' => array( $this, 'get_instagram_feed' ),
83 - 'permission_callback' => array( $this, 'get_instagram_feed_permission' ),
84 - )
85 - );
76 + // Get Instagram feed.
77 + register_rest_route(
78 + $namespace,
79 + '/get_instagram_feed/',
80 + array(
81 + 'methods' => WP_REST_Server::READABLE,
82 + 'callback' => array( $this, 'get_instagram_feed' ),
83 + 'permission_callback' => array( $this, 'get_instagram_feed_permission' ),
84 + )
85 + );
86 86
87 - // Get Twitter profile.
88 - register_rest_route(
89 - $namespace,
90 - '/get_twitter_profile/',
91 - array(
92 - 'methods' => WP_REST_Server::READABLE,
93 - 'callback' => array( $this, 'get_twitter_profile' ),
94 - 'permission_callback' => array( $this, 'get_twitter_profile_permission' ),
95 - )
96 - );
87 + // Get Twitter profile.
88 + register_rest_route(
89 + $namespace,
90 + '/get_twitter_profile/',
91 + array(
92 + 'methods' => WP_REST_Server::READABLE,
93 + 'callback' => array( $this, 'get_twitter_profile' ),
94 + 'permission_callback' => array( $this, 'get_twitter_profile_permission' ),
95 + )
96 + );
97 97
98 - // Get Twitter feed.
99 - register_rest_route(
100 - $namespace,
101 - '/get_twitter_feed/',
102 - array(
103 - 'methods' => WP_REST_Server::READABLE,
104 - 'callback' => array( $this, 'get_twitter_feed' ),
105 - 'permission_callback' => array( $this, 'get_twitter_feed_permission' ),
106 - )
107 - );
98 + // Get Twitter feed.
99 + register_rest_route(
100 + $namespace,
101 + '/get_twitter_feed/',
102 + array(
103 + 'methods' => WP_REST_Server::READABLE,
104 + 'callback' => array( $this, 'get_twitter_feed' ),
105 + 'permission_callback' => array( $this, 'get_twitter_feed_permission' ),
106 + )
107 + );
108 108
109 - // Get TOC.
110 - register_rest_route(
111 - $namespace,
112 - '/get_table_of_contents/',
113 - array(
114 - 'methods' => WP_REST_Server::READABLE,
115 - 'callback' => array( $this, 'get_table_of_contents' ),
116 - 'permission_callback' => '__return_true',
117 - )
118 - );
109 + // Get TOC.
110 + register_rest_route(
111 + $namespace,
112 + '/get_table_of_contents/',
113 + array(
114 + 'methods' => WP_REST_Server::READABLE,
115 + 'callback' => array( $this, 'get_table_of_contents' ),
116 + 'args' => array(
117 + 'headings' => array(
118 + 'default' => array(),
119 + 'sanitize_callback' => array( $this, 'sanitize_toc_headings' ),
120 + ),
121 + 'allowedHeaders' => array(
122 + 'default' => array(),
123 + 'sanitize_callback' => array( $this, 'sanitize_toc_allowed_headers' ),
124 + ),
125 + 'listStyle' => array(
126 + 'default' => 'ol',
127 + 'sanitize_callback' => array( $this, 'sanitize_toc_list_style' ),
128 + ),
129 + ),
130 + 'permission_callback' => '__return_true',
131 + )
132 + );
119 133
120 - // Get Templates.
121 - register_rest_route(
122 - $namespace,
123 - '/get_templates/',
124 - array(
125 - 'methods' => WP_REST_Server::READABLE,
126 - 'callback' => array( $this, 'get_templates' ),
127 - 'permission_callback' => '__return_true',
128 - )
129 - );
134 + // Get Templates.
135 + register_rest_route(
136 + $namespace,
137 + '/get_templates/',
138 + array(
139 + 'methods' => WP_REST_Server::READABLE,
140 + 'callback' => array( $this, 'get_templates' ),
141 + 'permission_callback' => '__return_true',
142 + )
143 + );
130 144
131 - // Get template data.
132 - register_rest_route(
133 - $namespace,
134 - '/get_template_data/',
135 - array(
136 - 'methods' => WP_REST_Server::READABLE,
137 - 'callback' => array( $this, 'get_template_data' ),
138 - 'permission_callback' => '__return_true',
139 - )
140 - );
145 + // Get template data.
146 + register_rest_route(
147 + $namespace,
148 + '/get_template_data/',
149 + array(
150 + 'methods' => WP_REST_Server::READABLE,
151 + 'callback' => array( $this, 'get_template_data' ),
152 + 'permission_callback' => '__return_true',
153 + )
154 + );
141 155
142 - // Get Custom Code.
143 - register_rest_route(
144 - $namespace,
145 - '/get_custom_code/',
146 - array(
147 - 'methods' => WP_REST_Server::READABLE,
148 - 'callback' => array( $this, 'get_custom_code' ),
149 - 'permission_callback' => array( $this, 'get_custom_code_permission' ),
150 - )
151 - );
156 + // Get Custom Code.
157 + register_rest_route(
158 + $namespace,
159 + '/get_custom_code/',
160 + array(
161 + 'methods' => WP_REST_Server::READABLE,
162 + 'callback' => array( $this, 'get_custom_code' ),
163 + 'permission_callback' => array( $this, 'get_custom_code_permission' ),
164 + )
165 + );
152 166
153 - // Update Custom Code.
154 - register_rest_route(
155 - $namespace,
156 - '/update_custom_code/',
157 - array(
158 - 'methods' => WP_REST_Server::EDITABLE,
159 - 'callback' => array( $this, 'update_custom_code' ),
160 - 'permission_callback' => array( $this, 'update_custom_code_permission' ),
161 - )
162 - );
167 + // Update Custom Code.
168 + register_rest_route(
169 + $namespace,
170 + '/update_custom_code/',
171 + array(
172 + 'methods' => WP_REST_Server::EDITABLE,
173 + 'callback' => array( $this, 'update_custom_code' ),
174 + 'permission_callback' => array( $this, 'update_custom_code_permission' ),
175 + )
176 + );
163 177
164 - // Update Color Palette.
165 - register_rest_route(
166 - $namespace,
167 - '/update_color_palette/',
168 - array(
169 - 'methods' => WP_REST_Server::EDITABLE,
170 - 'callback' => array( $this, 'update_color_palette' ),
171 - 'permission_callback' => array( $this, 'update_color_palette_permission' ),
172 - )
173 - );
178 + // Update Color Palette.
179 + register_rest_route(
180 + $namespace,
181 + '/update_color_palette/',
182 + array(
183 + 'methods' => WP_REST_Server::EDITABLE,
184 + 'callback' => array( $this, 'update_color_palette' ),
185 + 'permission_callback' => array( $this, 'update_color_palette_permission' ),
186 + )
187 + );
174 188
175 - // Get Typography.
176 - register_rest_route(
177 - $namespace,
178 - '/get_custom_typography/',
179 - array(
180 - 'methods' => WP_REST_Server::READABLE,
181 - 'callback' => array( $this, 'get_custom_typography' ),
182 - 'permission_callback' => array( $this, 'get_custom_typography_permission' ),
183 - )
184 - );
189 + // Get Typography.
190 + register_rest_route(
191 + $namespace,
192 + '/get_custom_typography/',
193 + array(
194 + 'methods' => WP_REST_Server::READABLE,
195 + 'callback' => array( $this, 'get_custom_typography' ),
196 + 'permission_callback' => array( $this, 'get_custom_typography_permission' ),
197 + )
198 + );
185 199
186 - // Update Typography.
187 - register_rest_route(
188 - $namespace,
189 - '/update_custom_typography/',
190 - array(
191 - 'methods' => WP_REST_Server::EDITABLE,
192 - 'callback' => array( $this, 'update_custom_typography' ),
193 - 'permission_callback' => array( $this, 'update_custom_typography_permission' ),
194 - )
195 - );
200 + // Update Typography.
201 + register_rest_route(
202 + $namespace,
203 + '/update_custom_typography/',
204 + array(
205 + 'methods' => WP_REST_Server::EDITABLE,
206 + 'callback' => array( $this, 'update_custom_typography' ),
207 + 'permission_callback' => array( $this, 'update_custom_typography_permission' ),
208 + )
209 + );
196 210
197 - // Update Google Maps API key.
198 - register_rest_route(
199 - $namespace,
200 - '/update_google_maps_api_key/',
201 - array(
202 - 'methods' => WP_REST_Server::EDITABLE,
203 - 'callback' => array( $this, 'update_google_maps_api_key' ),
204 - 'permission_callback' => array( $this, 'update_google_maps_api_key_permission' ),
205 - )
206 - );
211 + // Update Google Maps API key.
212 + register_rest_route(
213 + $namespace,
214 + '/update_google_maps_api_key/',
215 + array(
216 + 'methods' => WP_REST_Server::EDITABLE,
217 + 'callback' => array( $this, 'update_google_maps_api_key' ),
218 + 'permission_callback' => array( $this, 'update_google_maps_api_key_permission' ),
219 + )
220 + );
207 221
208 - // Update Google reCaptcha keys.
209 - register_rest_route(
210 - $namespace,
211 - '/update_google_recaptcha_keys/',
212 - array(
213 - 'methods' => WP_REST_Server::EDITABLE,
214 - 'callback' => array( $this, 'update_google_recaptcha_keys' ),
215 - 'permission_callback' => array( $this, 'update_google_recaptcha_keys_permission' ),
216 - )
217 - );
222 + // Update Google reCaptcha keys.
223 + register_rest_route(
224 + $namespace,
225 + '/update_google_recaptcha_keys/',
226 + array(
227 + 'methods' => WP_REST_Server::EDITABLE,
228 + 'callback' => array( $this, 'update_google_recaptcha_keys' ),
229 + 'permission_callback' => array( $this, 'update_google_recaptcha_keys_permission' ),
230 + )
231 + );
218 232
219 - // Update Disabled Blocks.
220 - register_rest_route(
221 - $namespace,
222 - '/update_disabled_blocks/',
223 - array(
224 - 'methods' => WP_REST_Server::EDITABLE,
225 - 'callback' => array( $this, 'update_disabled_blocks' ),
226 - 'permission_callback' => array( $this, 'update_disabled_blocks_permission' ),
227 - )
228 - );
233 + // Update Disabled Blocks.
234 + register_rest_route(
235 + $namespace,
236 + '/update_disabled_blocks/',
237 + array(
238 + 'methods' => WP_REST_Server::EDITABLE,
239 + 'callback' => array( $this, 'update_disabled_blocks' ),
240 + 'permission_callback' => array( $this, 'update_disabled_blocks_permission' ),
241 + )
242 + );
229 243
230 - // Update Settings.
231 - register_rest_route(
232 - $namespace,
233 - '/update_settings/',
234 - array(
235 - 'methods' => WP_REST_Server::EDITABLE,
236 - 'callback' => array( $this, 'update_settings' ),
237 - 'permission_callback' => array( $this, 'update_settings_permission' ),
238 - )
239 - );
240 - }
244 + // Update Settings.
245 + register_rest_route(
246 + $namespace,
247 + '/update_settings/',
248 + array(
249 + 'methods' => WP_REST_Server::EDITABLE,
250 + 'callback' => array( $this, 'update_settings' ),
251 + 'permission_callback' => array( $this, 'update_settings_permission' ),
252 + )
253 + );
241 254
242 - /**
243 - * Get read customizer permissions.
244 - *
245 - * @return bool
246 - */
247 - public function get_customizer_permission() {
248 - if ( ! current_user_can( 'edit_theme_options' ) ) {
249 - return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change Customizer options.', 'ghostkit' ), true );
250 - }
251 - return true;
252 - }
255 + // Get Custom Fonts.
256 + register_rest_route(
257 + $namespace,
258 + '/get_custom_fonts/',
259 + array(
260 + 'methods' => WP_REST_Server::READABLE,
261 + 'callback' => array( $this, 'get_custom_fonts' ),
262 + 'permission_callback' => array( $this, 'get_custom_fonts_permission' ),
263 + )
264 + );
253 265
254 - /**
255 - * Get attachment image <img> tag permissions.
256 - *
257 - * @return bool
258 - */
259 - public function get_attachment_image_permission() {
260 - return true;
261 - }
266 + // Update Custom Fonts.
267 + register_rest_route(
268 + $namespace,
269 + '/update_custom_fonts/',
270 + array(
271 + 'methods' => WP_REST_Server::EDITABLE,
272 + 'callback' => array( $this, 'update_custom_fonts' ),
273 + 'permission_callback' => array( $this, 'update_custom_fonts_permission' ),
274 + )
275 + );
276 + }
262 277
263 - /**
264 - * Get Instagram feed permissions.
265 - *
266 - * @return bool
267 - */
268 - public function get_instagram_feed_permission() {
269 - return true;
270 - }
278 + /**
279 + * Get read fonts permissions.
280 + *
281 + * @return bool
282 + */
283 + public function get_custom_fonts_permission() {
284 + if ( ! current_user_can( 'edit_theme_options' ) ) {
285 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
286 + }
271 287
272 - /**
273 - * Get Instagram profile permissions.
274 - *
275 - * @return bool
276 - */
277 - public function get_instagram_profile_permission() {
278 - return true;
279 - }
288 + return true;
289 + }
280 290
281 - /**
282 - * Get Twitter feed permissions.
283 - *
284 - * @return bool
285 - */
286 - public function get_twitter_feed_permission() {
287 - return true;
288 - }
291 + /**
292 + * Get edit fonts permissions.
293 + *
294 + * @return bool
295 + */
296 + public function update_custom_fonts_permission() {
297 + if ( ! current_user_can( 'manage_options' ) ) {
298 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
299 + }
289 300
290 - /**
291 - * Get Twitter profile permissions.
292 - *
293 - * @return bool
294 - */
295 - public function get_twitter_profile_permission() {
296 - return true;
297 - }
301 + return true;
302 + }
298 303
299 - /**
300 - * Get read custom code permissions.
301 - *
302 - * @return bool
303 - */
304 - public function get_custom_code_permission() {
305 - if ( ! current_user_can( 'edit_theme_options' ) ) {
306 - return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
307 - }
304 + /**
305 + * Get read customizer permissions.
306 + *
307 + * @return bool
308 + */
309 + public function get_customizer_permission() {
310 + if ( ! current_user_can( 'edit_theme_options' ) ) {
311 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change Customizer options.', 'ghostkit' ), true );
312 + }
313 + return true;
314 + }
308 315
309 - return true;
310 - }
316 + /**
317 + * Get attachment image <img> tag permissions.
318 + *
319 + * @return bool
320 + */
321 + public function get_attachment_image_permission() {
322 + return true;
323 + }
311 324
312 - /**
313 - * Get edit custom code permissions.
314 - *
315 - * @return bool
316 - */
317 - public function update_custom_code_permission() {
318 - if ( ! current_user_can( 'manage_options' ) ) {
319 - return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
320 - }
325 + /**
326 + * Get Instagram feed permissions.
327 + *
328 + * @return bool
329 + */
330 + public function get_instagram_feed_permission() {
331 + return true;
332 + }
321 333
322 - return true;
323 - }
334 + /**
335 + * Get Instagram profile permissions.
336 + *
337 + * @return bool
338 + */
339 + public function get_instagram_profile_permission() {
340 + return true;
341 + }
324 342
325 - /**
326 - * Get edit color palette permissions.
327 - *
328 - * @return bool
329 - */
330 - public function update_color_palette_permission() {
331 - if ( ! current_user_can( 'manage_options' ) ) {
332 - return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
333 - }
343 + /**
344 + * Get Twitter feed permissions.
345 + *
346 + * @return bool
347 + */
348 + public function get_twitter_feed_permission() {
349 + return true;
350 + }
334 351
335 - return true;
336 - }
352 + /**
353 + * Get Twitter profile permissions.
354 + *
355 + * @return bool
356 + */
357 + public function get_twitter_profile_permission() {
358 + return true;
359 + }
337 360
338 - /**
339 - * Get read typography permissions.
340 - *
341 - * @return bool
342 - */
343 - public function get_custom_typography_permission() {
344 - if ( ! current_user_can( 'edit_theme_options' ) ) {
345 - return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
346 - }
361 + /**
362 + * Get read custom code permissions.
363 + *
364 + * @return bool
365 + */
366 + public function get_custom_code_permission() {
367 + if ( ! current_user_can( 'edit_theme_options' ) ) {
368 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
369 + }
347 370
348 - return true;
349 - }
371 + return true;
372 + }
350 373
351 - /**
352 - * Get edit typography permissions.
353 - *
354 - * @return bool
355 - */
356 - public function update_custom_typography_permission() {
357 - if ( ! current_user_can( 'manage_options' ) ) {
358 - return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
359 - }
374 + /**
375 + * Get edit custom code permissions.
376 + *
377 + * @return bool
378 + */
379 + public function update_custom_code_permission() {
380 + if ( ! current_user_can( 'manage_options' ) ) {
381 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
382 + }
360 383
361 - return true;
362 - }
384 + return true;
385 + }
363 386
364 - /**
365 - * Get read Google Maps API key permissions.
366 - *
367 - * @return bool
368 - */
369 - public function update_google_maps_api_key_permission() {
370 - if ( ! current_user_can( 'manage_options' ) ) {
371 - return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
372 - }
387 + /**
388 + * Get edit color palette permissions.
389 + *
390 + * @return bool
391 + */
392 + public function update_color_palette_permission() {
393 + if ( ! current_user_can( 'manage_options' ) ) {
394 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
395 + }
373 396
374 - return true;
375 - }
397 + return true;
398 + }
376 399
377 - /**
378 - * Get read Google reCaptcha API keys permissions.
379 - *
380 - * @return bool
381 - */
382 - public function update_google_recaptcha_keys_permission() {
383 - if ( ! current_user_can( 'manage_options' ) ) {
384 - return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
385 - }
400 + /**
401 + * Get read typography permissions.
402 + *
403 + * @return bool
404 + */
405 + public function get_custom_typography_permission() {
406 + if ( ! current_user_can( 'edit_theme_options' ) ) {
407 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
408 + }
386 409
387 - return true;
388 - }
410 + return true;
411 + }
389 412
390 - /**
391 - * Get edit options permissions.
392 - *
393 - * @return bool
394 - */
395 - public function update_disabled_blocks_permission() {
396 - if ( ! current_user_can( 'manage_options' ) ) {
397 - return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
398 - }
413 + /**
414 + * Get edit typography permissions.
415 + *
416 + * @return bool
417 + */
418 + public function update_custom_typography_permission() {
419 + if ( ! current_user_can( 'manage_options' ) ) {
420 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
421 + }
399 422
400 - return true;
401 - }
423 + return true;
424 + }
402 425
403 - /**
404 - * Get edit options permissions.
405 - *
406 - * @return bool
407 - */
408 - public function update_settings_permission() {
409 - if ( ! current_user_can( 'manage_options' ) ) {
410 - return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
411 - }
426 + /**
427 + * Get read Google Maps API key permissions.
428 + *
429 + * @return bool
430 + */
431 + public function update_google_maps_api_key_permission() {
432 + if ( ! current_user_can( 'manage_options' ) ) {
433 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
434 + }
412 435
413 - return true;
414 - }
436 + return true;
437 + }
415 438
416 - /**
417 - * Get customizer data.
418 - *
419 - * @return mixed
420 - */
421 - public function get_customizer() {
422 - $options = get_option( 'ghostkit_customizer_fields' );
439 + /**
440 + * Get read Google reCaptcha API keys permissions.
441 + *
442 + * @return bool
443 + */
444 + public function update_google_recaptcha_keys_permission() {
445 + if ( ! current_user_can( 'manage_options' ) ) {
446 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
447 + }
423 448
424 - if ( ! empty( $options ) ) {
425 - return $this->success( $options );
426 - } else {
427 - return $this->error( 'no_options_found', __( 'Options not found.', 'ghostkit' ) );
428 - }
429 - }
449 + return true;
450 + }
430 451
431 - /**
432 - * Get attachment image <img> tag.
433 - *
434 - * @param WP_REST_Request $request request object.
435 - *
436 - * @return mixed
437 - */
438 - public function get_attachment_image( WP_REST_Request $request ) {
439 - $id = $request->get_param( 'id' );
440 - $size = $request->get_param( 'size' );
441 - $icon = $request->get_param( 'icon' );
442 - $attr = $request->get_param( 'attr' );
443 - $div_tag = $request->get_param( 'div_tag' );
452 + /**
453 + * Get edit options permissions.
454 + *
455 + * @return bool
456 + */
457 + public function update_disabled_blocks_permission() {
458 + if ( ! current_user_can( 'manage_options' ) ) {
459 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
460 + }
444 461
445 - if ( ! $id ) {
446 - return $this->error( 'no_id_found', __( 'Provide image ID.', 'ghostkit' ) );
447 - }
462 + return true;
463 + }
448 464
449 - $attr = isset( $attr ) && $attr && is_array( $attr ) ? $attr : array();
465 + /**
466 + * Get edit options permissions.
467 + *
468 + * @return bool
469 + */
470 + public function update_settings_permission() {
471 + if ( ! current_user_can( 'manage_options' ) ) {
472 + return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'ghostkit' ), true );
473 + }
450 474
451 - if ( $div_tag ) {
452 - $image_url = wp_get_attachment_image_url( $id, $size, $icon );
475 + return true;
476 + }
453 477
454 - if ( ! isset( $attr['style'] ) ) {
455 - $attr['style'] = '';
456 - }
478 + /**
479 + * Get custom fonts.
480 + *
481 + * @return mixed
482 + */
483 + public function get_custom_fonts() {
484 + $fonts = get_option( 'ghostkit_fonts_settings', array() );
485 + $fonts = apply_filters( 'gkt_rest_get_custom_fonts', $fonts );
457 486
458 - $attr['style'] .= 'background-image: url("' . esc_url( $image_url ) . '");';
487 + if ( is_array( $fonts ) ) {
488 + return $this->success( $fonts );
489 + } else {
490 + return $this->error( 'no_fonts', __( 'Custom fonts not found.', 'ghostkit' ) );
491 + }
492 + }
459 493
460 - $attr = array_map( 'esc_attr', $attr );
461 - $image = '<div';
494 + /**
495 + * Update fonts.
496 + *
497 + * @param WP_REST_Request $request request object.
498 + *
499 + * @return mixed
500 + */
501 + public function update_custom_fonts( WP_REST_Request $request ) {
502 + $new_fonts = $request->get_param( 'data' );
503 + $updated_option = array();
462 504
463 - foreach ( $attr as $name => $value ) {
464 - $image .= " $name=" . '"' . $value . '"';
465 - }
505 + $new_fonts = apply_filters( 'gkt_rest_update_custom_fonts', $new_fonts );
466 506
467 - $image .= '></div>';
468 - } else {
469 - $image_src = wp_get_attachment_image_src( $id, $size, $icon );
507 + if ( is_array( $new_fonts ) ) {
508 + $current_fonts = get_option( 'ghostkit_fonts_settings', array() );
470 509
471 - if ( $image_src ) {
472 - list( $src, $width, $height ) = $image_src;
510 + $updated_option = array_merge( is_array( $current_fonts ) ? $current_fonts : array(), $new_fonts );
473 511
474 - $alt = trim( wp_strip_all_tags( get_post_meta( $id, '_wp_attachment_image_alt', true ) ) );
512 + update_option( 'ghostkit_fonts_settings', $updated_option );
513 + }
475 514
476 - if ( $alt ) {
477 - $attr['alt'] = $alt;
478 - }
515 + return $this->success( $updated_option );
516 + }
479 517
480 - if ( ! isset( $attr['class'] ) ) {
481 - $attr['class'] = 'wp-image-' . $id;
482 - } else {
483 - $attr['class'] = 'wp-image-' . $id . ' ' . $attr['class'];
484 - }
518 + /**
519 + * Get customizer data.
520 + *
521 + * @return mixed
522 + */
523 + public function get_customizer() {
524 + $options = get_option( 'ghostkit_customizer_fields' );
485 525
486 - $attr['width'] = $width;
487 - $attr['height'] = $height;
526 + if ( ! empty( $options ) ) {
527 + return $this->success( $options );
528 + } else {
529 + return $this->error( 'no_options_found', __( 'Options not found.', 'ghostkit' ) );
530 + }
531 + }
488 532
489 - $attrs_str = '';
533 + /**
534 + * Get attachment image <img> tag.
535 + *
536 + * @param WP_REST_Request $request request object.
537 + *
538 + * @return mixed
539 + */
540 + public function get_attachment_image( WP_REST_Request $request ) {
541 + $id = $request->get_param( 'id' );
542 + $size = $request->get_param( 'size' );
543 + $icon = $request->get_param( 'icon' );
544 + $attr = $request->get_param( 'attr' );
545 + $div_tag = $request->get_param( 'div_tag' );
490 546
491 - if ( isset( $attr ) && is_array( $attr ) ) {
492 - foreach ( $attr as $name => $val ) {
493 - $attrs_str .= ' ' . $name . '="' . esc_attr( $val ) . '"';
494 - }
495 - }
547 + if ( ! $id ) {
548 + return $this->error( 'no_id_found', __( 'Provide image ID.', 'ghostkit' ) );
549 + }
496 550
497 - $image = '<img src="' . esc_url( $src ) . '"' . $attrs_str . ' />';
498 - }
499 - }
551 + $attr = isset( $attr ) && $attr && is_array( $attr ) ? $attr : array();
500 552
501 - if ( isset( $image ) && $image ) {
502 - return $this->success( $image );
503 - } else {
504 - return $this->error( 'no_image_found', __( 'Image not found.', 'ghostkit' ) );
505 - }
506 - }
553 + if ( $div_tag ) {
554 + $image_url = wp_get_attachment_image_url( $id, $size, $icon );
507 555
508 - /**
509 - * Get Instagram feed.
510 - *
511 - * @param WP_REST_Request $request request object.
512 - *
513 - * @return mixed
514 - */
515 - public function get_instagram_feed( WP_REST_Request $request ) {
516 - $cache_name = 'ghostkit_instagram_feed_cache';
517 - $cache_expiration = $request->get_param( 'cache_expiration' ) ? $request->get_param( 'cache_expiration' ) : DAY_IN_SECONDS;
518 - $count = $request->get_param( 'count' ) ? $request->get_param( 'count' ) : 6;
519 - $access_token = $request->get_param( 'access_token' );
556 + if ( ! isset( $attr['style'] ) ) {
557 + $attr['style'] = '';
558 + }
520 559
521 - if ( ! $access_token ) {
522 - return $this->error( 'no_token_found', __( 'Provide Instagram Access Token.', 'ghostkit' ) );
523 - }
560 + $attr['style'] .= 'background-image: url("' . esc_url( $image_url ) . '");';
524 561
525 - $hash = md5(
526 - wp_json_encode(
527 - array(
528 - $access_token,
529 - $count,
530 - $cache_expiration,
531 - )
532 - )
533 - );
562 + $attr = array_map( 'esc_attr', $attr );
563 + $image = '<div';
534 564
535 - $cache_name = $cache_name . '_' . $hash;
565 + foreach ( $attr as $name => $value ) {
566 + $image .= " $name=" . '"' . $value . '"';
567 + }
536 568
537 - // get cached data.
538 - $result = get_transient( $cache_name );
569 + $image .= '></div>';
570 + } else {
571 + $image_src = wp_get_attachment_image_src( $id, $size, $icon );
539 572
540 - // if there is no cache available, request instagram feed.
541 - if ( false === $result && $access_token ) {
542 - // Make Requests.
543 - $feed = wp_remote_get( 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' . $access_token . '&count=' . $count );
573 + if ( $image_src ) {
574 + list( $src, $width, $height ) = $image_src;
544 575
545 - if ( ! is_wp_error( $feed ) && isset( $feed['body'] ) ) {
546 - $feed = json_decode( $feed['body'], true );
547 - } else {
548 - $feed = false;
549 - }
576 + $alt = trim( wp_strip_all_tags( get_post_meta( $id, '_wp_attachment_image_alt', true ) ) );
550 577
551 - if ( $feed && is_array( $feed ) ) {
552 - $result = $feed;
553 - set_transient( $cache_name, $result, $cache_expiration );
554 - }
555 - }
578 + if ( $alt ) {
579 + $attr['alt'] = $alt;
580 + }
556 581
557 - if ( $result ) {
558 - return $this->success( $result );
559 - } else {
560 - return $this->error( 'no_instagram_data_loaded', __( 'Instagram data failed to load.', 'ghostkit' ) );
561 - }
562 - }
582 + if ( ! isset( $attr['class'] ) ) {
583 + $attr['class'] = 'wp-image-' . $id;
584 + } else {
585 + $attr['class'] = 'wp-image-' . $id . ' ' . $attr['class'];
586 + }
563 587
564 - /**
565 - * Get Instagram profile.
566 - *
567 - * @param WP_REST_Request $request request object.
568 - *
569 - * @return mixed
570 - */
571 - public function get_instagram_profile( WP_REST_Request $request ) {
572 - $cache_name = 'ghostkit_instagram_profile_cache';
573 - $cache_expiration = $request->get_param( 'cache_expiration' ) ? $request->get_param( 'cache_expiration' ) : DAY_IN_SECONDS;
574 - $access_token = $request->get_param( 'access_token' );
588 + $attr['width'] = $width;
589 + $attr['height'] = $height;
575 590
576 - if ( ! $access_token ) {
577 - return $this->error( 'no_token_found', __( 'Provide Instagram Access Token.', 'ghostkit' ) );
578 - }
591 + $attrs_str = '';
579 592
580 - $hash = md5(
581 - wp_json_encode(
582 - array(
583 - $access_token,
584 - $cache_expiration,
585 - )
586 - )
587 - );
593 + if ( isset( $attr ) && is_array( $attr ) ) {
594 + foreach ( $attr as $name => $val ) {
595 + $attrs_str .= ' ' . $name . '="' . esc_attr( $val ) . '"';
596 + }
597 + }
588 598
589 - $cache_name = $cache_name . '_' . $hash;
599 + $image = '<img src="' . esc_url( $src ) . '"' . $attrs_str . ' />';
600 + }
601 + }
590 602
591 - // get cached data.
592 - $result = get_transient( $cache_name );
603 + if ( isset( $image ) && $image ) {
604 + return $this->success( $image );
605 + } else {
606 + return $this->error( 'no_image_found', __( 'Image not found.', 'ghostkit' ) );
607 + }
608 + }
593 609
594 - // if there is no cache available, request instagram feed.
595 - if ( false === $result && $access_token ) {
596 - // Make Requests.
597 - $profile = wp_remote_get( 'https://api.instagram.com/v1/users/self/?access_token=' . $access_token );
610 + /**
611 + * Get Instagram feed.
612 + *
613 + * @param WP_REST_Request $request request object.
614 + *
615 + * @return mixed
616 + */
617 + public function get_instagram_feed( WP_REST_Request $request ) {
618 + $cache_name = 'ghostkit_instagram_feed_cache';
619 + $cache_expiration = $request->get_param( 'cache_expiration' ) ? $request->get_param( 'cache_expiration' ) : DAY_IN_SECONDS;
620 + $count = $request->get_param( 'count' ) ? $request->get_param( 'count' ) : 6;
621 + $access_token = $request->get_param( 'access_token' );
598 622
599 - if ( ! is_wp_error( $profile ) && isset( $profile['body'] ) ) {
600 - $profile = json_decode( $profile['body'], true );
601 - } else {
602 - $profile = false;
603 - }
623 + if ( ! $access_token ) {
624 + return $this->error( 'no_token_found', __( 'Provide Instagram Access Token.', 'ghostkit' ) );
625 + }
604 626
605 - if ( $profile && is_array( $profile ) ) {
606 - $result = $profile;
607 - set_transient( $cache_name, $result, $cache_expiration );
608 - }
609 - }
627 + $hash = md5(
628 + wp_json_encode(
629 + array(
630 + $access_token,
631 + $count,
632 + $cache_expiration,
633 + )
634 + )
635 + );
610 636
611 - if ( $result ) {
612 - return $this->success( $result );
613 - } else {
614 - return $this->error( 'no_instagram_data_loaded', __( 'Instagram data failed to load.', 'ghostkit' ) );
615 - }
616 - }
637 + $cache_name = $cache_name . '_' . $hash;
617 638
618 - /**
619 - * Get Twitter profile.
620 - *
621 - * @param WP_REST_Request $request request object.
622 - *
623 - * @return mixed
624 - */
625 - public function get_twitter_profile( WP_REST_Request $request ) {
626 - $cache_name = 'ghostkit_twitter_profile_cache';
627 - $cache_expiration = $request->get_param( 'cache_expiration' ) ? $request->get_param( 'cache_expiration' ) : DAY_IN_SECONDS;
639 + // get cached data.
640 + $result = get_transient( $cache_name );
628 641
629 - $consumer_key = $request->get_param( 'consumer_key' );
630 - $consumer_secret = $request->get_param( 'consumer_secret' );
631 - $access_token = $request->get_param( 'access_token' );
632 - $access_token_secret = $request->get_param( 'access_token_secret' );
633 - $screen_name = $request->get_param( 'screen_name' );
642 + // if there is no cache available, request instagram feed.
643 + if ( false === $result && $access_token ) {
644 + // Make Requests.
645 + $feed = wp_remote_get( 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' . $access_token . '&count=' . $count );
634 646
635 - if ( ! $consumer_key ) {
636 - return $this->error( 'no_consumer_key_found', __( 'Provide Twitter Consumer Key.', 'ghostkit' ) );
637 - }
638 - if ( ! $consumer_secret ) {
639 - return $this->error( 'no_consumer_secret_found', __( 'Provide Twitter Consumer Secret.', 'ghostkit' ) );
640 - }
641 - if ( ! $access_token ) {
642 - return $this->error( 'no_access_token_found', __( 'Provide Twitter Access Token.', 'ghostkit' ) );
643 - }
644 - if ( ! $access_token_secret ) {
645 - return $this->error( 'no_access_token_secret_found', __( 'Provide Twitter Access Token Secret.', 'ghostkit' ) );
646 - }
647 - if ( ! $screen_name ) {
648 - return $this->error( 'no_screen_name_found', __( 'Provide Username.', 'ghostkit' ) );
649 - }
647 + if ( ! is_wp_error( $feed ) && isset( $feed['body'] ) ) {
648 + $feed = json_decode( $feed['body'], true );
649 + } else {
650 + $feed = false;
651 + }
650 652
651 - $api_data_ready = $consumer_key && $consumer_secret && $access_token && $access_token_secret;
653 + if ( $feed && is_array( $feed ) ) {
654 + $result = $feed;
655 + set_transient( $cache_name, $result, $cache_expiration );
656 + }
657 + }
652 658
653 - $hash = md5(
654 - wp_json_encode(
655 - array(
656 - $consumer_key,
657 - $consumer_secret,
658 - $access_token,
659 - $access_token_secret,
660 - $cache_expiration,
661 - )
662 - )
663 - );
659 + if ( $result ) {
660 + return $this->success( $result );
661 + } else {
662 + return $this->error( 'no_instagram_data_loaded', __( 'Instagram data failed to load.', 'ghostkit' ) );
663 + }
664 + }
664 665
665 - $cache_name = $cache_name . '_' . $screen_name . '_' . $hash;
666 + /**
667 + * Get Instagram profile.
668 + *
669 + * @param WP_REST_Request $request request object.
670 + *
671 + * @return mixed
672 + */
673 + public function get_instagram_profile( WP_REST_Request $request ) {
674 + $cache_name = 'ghostkit_instagram_profile_cache';
675 + $cache_expiration = $request->get_param( 'cache_expiration' ) ? $request->get_param( 'cache_expiration' ) : DAY_IN_SECONDS;
676 + $access_token = $request->get_param( 'access_token' );
666 677
667 - // get cached data.
668 - $result = get_transient( $cache_name );
678 + if ( ! $access_token ) {
679 + return $this->error( 'no_token_found', __( 'Provide Instagram Access Token.', 'ghostkit' ) );
680 + }
669 681
670 - // if there is no cache available, request twitter feed.
671 - if ( false === $result && $api_data_ready ) {
672 - // request_api_twitter.
673 - $profile = $this->request_api_twitter(
674 - array(
675 - 'url' => 'https://api.twitter.com/1.1/users/show.json',
676 - 'consumer_key' => $consumer_key,
677 - 'consumer_secret' => $consumer_secret,
678 - 'access_token' => $access_token,
679 - 'access_token_secret' => $access_token_secret,
680 - 'include_entities' => 'true',
681 - 'screen_name' => $screen_name,
682 - )
683 - );
682 + $hash = md5(
683 + wp_json_encode(
684 + array(
685 + $access_token,
686 + $cache_expiration,
687 + )
688 + )
689 + );
684 690
685 - if ( $profile && isset( $profile['screen_name'] ) ) {
686 - $result = $profile;
691 + $cache_name = $cache_name . '_' . $hash;
687 692
688 - // prepare different profile image sizes.
689 - if ( $result['profile_image_url'] ) {
690 - $result['profile_images'] = $this->get_twitter_profile_images( $result['profile_image_url'] );
691 - }
692 - if ( $result['profile_image_url_https'] ) {
693 - $result['profile_images_https'] = $this->get_twitter_profile_images( $result['profile_image_url_https'] );
694 - }
693 + // get cached data.
694 + $result = get_transient( $cache_name );
695 695
696 - // prepare short counts.
697 - $result['followers_count_short'] = $this->convert_number_short( $result['followers_count'] );
698 - $result['friends_count_short'] = $this->convert_number_short( $result['friends_count'] );
699 - $result['listed_count_short'] = $this->convert_number_short( $result['listed_count'] );
700 - $result['favourites_count_short'] = $this->convert_number_short( $result['favourites_count'] );
701 - $result['statuses_count_short'] = $this->convert_number_short( $result['statuses_count'] );
696 + // if there is no cache available, request instagram feed.
697 + if ( false === $result && $access_token ) {
698 + // Make Requests.
699 + $profile = wp_remote_get( 'https://api.instagram.com/v1/users/self/?access_token=' . $access_token );
702 700
703 - // prepare url link.
704 - if ( $result['url'] && isset( $result['entities']['url'] ) ) {
705 - $result['url_entitled'] = $this->add_tweet_entity_links( $result['url'], $result['entities']['url'] );
706 - }
701 + if ( ! is_wp_error( $profile ) && isset( $profile['body'] ) ) {
702 + $profile = json_decode( $profile['body'], true );
703 + } else {
704 + $profile = false;
705 + }
707 706
708 - // description with links.
709 - if ( isset( $result['entities']['description'] ) ) {
710 - $result['description_entitled'] = $this->add_tweet_entity_links( $result['description'], $result['entities']['description'] );
711 - }
707 + if ( $profile && is_array( $profile ) ) {
708 + $result = $profile;
709 + set_transient( $cache_name, $result, $cache_expiration );
710 + }
711 + }
712 712
713 - set_transient( $cache_name, $result, $cache_expiration );
714 - }
715 - }
713 + if ( $result ) {
714 + return $this->success( $result );
715 + } else {
716 + return $this->error( 'no_instagram_data_loaded', __( 'Instagram data failed to load.', 'ghostkit' ) );
717 + }
718 + }
716 719
717 - if ( $result ) {
718 - return $this->success( $result );
719 - } else {
720 - return $this->error( 'no_twitter_data_loaded', __( 'Twitter data failed to load.', 'ghostkit' ) );
721 - }
722 - }
720 + /**
721 + * Get Twitter profile.
722 + *
723 + * @param WP_REST_Request $request request object.
724 + *
725 + * @return mixed
726 + */
727 + public function get_twitter_profile( WP_REST_Request $request ) {
728 + $cache_name = 'ghostkit_twitter_profile_cache';
729 + $cache_expiration = $request->get_param( 'cache_expiration' ) ? $request->get_param( 'cache_expiration' ) : DAY_IN_SECONDS;
723 730
724 - /**
725 - * Get Twitter feed.
726 - *
727 - * @param WP_REST_Request $request request object.
728 - *
729 - * @return mixed
730 - */
731 - public function get_twitter_feed( WP_REST_Request $request ) {
732 - $cache_name = 'ghostkit_twitter_feed_cache';
733 - $cache_expiration = $request->get_param( 'cache_expiration' ) ? $request->get_param( 'cache_expiration' ) : DAY_IN_SECONDS;
731 + $consumer_key = $request->get_param( 'consumer_key' );
732 + $consumer_secret = $request->get_param( 'consumer_secret' );
733 + $access_token = $request->get_param( 'access_token' );
734 + $access_token_secret = $request->get_param( 'access_token_secret' );
735 + $screen_name = $request->get_param( 'screen_name' );
734 736
735 - $count = (int) ( $request->get_param( 'count' ) ? $request->get_param( 'count' ) : 6 );
736 - $consumer_key = $request->get_param( 'consumer_key' );
737 - $consumer_secret = $request->get_param( 'consumer_secret' );
738 - $access_token = $request->get_param( 'access_token' );
739 - $access_token_secret = $request->get_param( 'access_token_secret' );
740 - $screen_name = $request->get_param( 'screen_name' );
741 - $exclude_replies = 'true' === $request->get_param( 'exclude_replies' );
742 - $include_rts = 'true' === $request->get_param( 'include_rts' );
743 - $tweet_mode_extended = 'true' === $request->get_param( 'tweet_mode_extended' );
737 + if ( ! $consumer_key ) {
738 + return $this->error( 'no_consumer_key_found', __( 'Provide Twitter Consumer Key.', 'ghostkit' ) );
739 + }
740 + if ( ! $consumer_secret ) {
741 + return $this->error( 'no_consumer_secret_found', __( 'Provide Twitter Consumer Secret.', 'ghostkit' ) );
742 + }
743 + if ( ! $access_token ) {
744 + return $this->error( 'no_access_token_found', __( 'Provide Twitter Access Token.', 'ghostkit' ) );
745 + }
746 + if ( ! $access_token_secret ) {
747 + return $this->error( 'no_access_token_secret_found', __( 'Provide Twitter Access Token Secret.', 'ghostkit' ) );
748 + }
749 + if ( ! $screen_name ) {
750 + return $this->error( 'no_screen_name_found', __( 'Provide Username.', 'ghostkit' ) );
751 + }
744 752
745 - $api_data_ready = $consumer_key && $consumer_secret && $access_token && $access_token_secret;
753 + $api_data_ready = $consumer_key && $consumer_secret && $access_token && $access_token_secret;
746 754
747 - if ( ! $consumer_key ) {
748 - return $this->error( 'no_consumer_key_found', __( 'Provide Twitter Consumer Key.', 'ghostkit' ) );
749 - }
750 - if ( ! $consumer_secret ) {
751 - return $this->error( 'no_consumer_secret_found', __( 'Provide Twitter Consumer Secret.', 'ghostkit' ) );
752 - }
753 - if ( ! $access_token ) {
754 - return $this->error( 'no_access_token_found', __( 'Provide Twitter Access Token.', 'ghostkit' ) );
755 - }
756 - if ( ! $access_token_secret ) {
757 - return $this->error( 'no_access_token_secret_found', __( 'Provide Twitter Access Token Secret.', 'ghostkit' ) );
758 - }
759 - if ( ! $screen_name ) {
760 - return $this->error( 'no_screen_name_found', __( 'Provide Username.', 'ghostkit' ) );
761 - }
755 + $hash = md5(
756 + wp_json_encode(
757 + array(
758 + $consumer_key,
759 + $consumer_secret,
760 + $access_token,
761 + $access_token_secret,
762 + $cache_expiration,
763 + )
764 + )
765 + );
762 766
763 - $hash = md5(
764 - wp_json_encode(
765 - array(
766 - $consumer_key,
767 - $consumer_secret,
768 - $access_token,
769 - $access_token_secret,
770 - $cache_expiration,
771 - $tweet_mode_extended,
772 - )
773 - )
774 - );
767 + $cache_name = $cache_name . '_' . $screen_name . '_' . $hash;
775 768
776 - $cache_name = $cache_name . '_' . $screen_name . '_' . $hash;
769 + // get cached data.
770 + $result = get_transient( $cache_name );
777 771
778 - // get cached data.
779 - $feed = get_transient( $cache_name );
772 + // if there is no cache available, request twitter feed.
773 + if ( false === $result && $api_data_ready ) {
774 + // request_api_twitter.
775 + $profile = $this->request_api_twitter(
776 + array(
777 + 'url' => 'https://api.twitter.com/1.1/users/show.json',
778 + 'consumer_key' => $consumer_key,
779 + 'consumer_secret' => $consumer_secret,
780 + 'access_token' => $access_token,
781 + 'access_token_secret' => $access_token_secret,
782 + 'include_entities' => 'true',
783 + 'screen_name' => $screen_name,
784 + )
785 + );
780 786
781 - $result = array();
787 + if ( $profile && isset( $profile['screen_name'] ) ) {
788 + $result = $profile;
782 789
783 - if ( $api_data_ready ) {
784 - // if there is no cache available, request twitter feed.
785 - if ( false === $feed ) {
786 - $feed = $this->request_api_twitter(
787 - array(
788 - 'url' => 'https://api.twitter.com/1.1/statuses/user_timeline.json',
789 - 'consumer_key' => $consumer_key,
790 - 'consumer_secret' => $consumer_secret,
791 - 'access_token' => $access_token,
792 - 'access_token_secret' => $access_token_secret,
793 - 'screen_name' => $screen_name,
794 - 'tweet_mode_extended' => $tweet_mode_extended,
795 - 'exclude_replies' => 'false',
796 - 'include_rts' => 'true',
797 - 'count' => 200,
798 - )
799 - );
790 + // prepare different profile image sizes.
791 + if ( $result['profile_image_url'] ) {
792 + $result['profile_images'] = $this->get_twitter_profile_images( $result['profile_image_url'] );
793 + }
794 + if ( $result['profile_image_url_https'] ) {
795 + $result['profile_images_https'] = $this->get_twitter_profile_images( $result['profile_image_url_https'] );
796 + }
800 797
801 - if ( $feed && ! ( isset( $feed['errors'] ) && count( $feed['errors'] ) > 0 ) ) {
802 - set_transient( $cache_name, $feed, $cache_expiration );
803 - } else {
804 - $feed = false;
805 - }
806 - }
798 + // prepare short counts.
799 + $result['followers_count_short'] = $this->convert_number_short( $result['followers_count'] );
800 + $result['friends_count_short'] = $this->convert_number_short( $result['friends_count'] );
801 + $result['listed_count_short'] = $this->convert_number_short( $result['listed_count'] );
802 + $result['favourites_count_short'] = $this->convert_number_short( $result['favourites_count'] );
803 + $result['statuses_count_short'] = $this->convert_number_short( $result['statuses_count'] );
807 804
808 - $tweets_count = $feed ? count( $feed ) : 0;
809 - $limit_to_display = min( $count, $tweets_count );
805 + // prepare url link.
806 + if ( $result['url'] && isset( $result['entities']['url'] ) ) {
807 + $result['url_entitled'] = $this->add_tweet_entity_links( $result['url'], $result['entities']['url'] );
808 + }
810 809
811 - if ( $limit_to_display > 0 ) {
812 - for ( $i = 0; $i < $tweets_count; $i++ ) {
813 - $new_item = $feed[ $i ];
810 + // description with links.
811 + if ( isset( $result['entities']['description'] ) ) {
812 + $result['description_entitled'] = $this->add_tweet_entity_links( $result['description'], $result['entities']['description'] );
813 + }
814 814
815 - // check for replies.
816 - if ( $exclude_replies && $new_item['in_reply_to_user_id'] ) {
817 - continue;
818 - }
815 + set_transient( $cache_name, $result, $cache_expiration );
816 + }
817 + }
819 818
820 - // check for retweets.
821 - if ( ! $include_rts && isset( $new_item['retweeted_status'] ) ) {
822 - continue;
823 - }
819 + if ( $result ) {
820 + return $this->success( $result );
821 + } else {
822 + return $this->error( 'no_twitter_data_loaded', __( 'Twitter data failed to load.', 'ghostkit' ) );
823 + }
824 + }
824 825
825 - // full text.
826 - if ( $tweet_mode_extended || ! isset( $new_item['text'] ) ) {
827 - $text = isset( $new_item['full_text'] ) ? $new_item['full_text'] : null;
826 + /**
827 + * Get Twitter feed.
828 + *
829 + * @param WP_REST_Request $request request object.
830 + *
831 + * @return mixed
832 + */
833 + public function get_twitter_feed( WP_REST_Request $request ) {
834 + $cache_name = 'ghostkit_twitter_feed_cache';
835 + $cache_expiration = $request->get_param( 'cache_expiration' ) ? $request->get_param( 'cache_expiration' ) : DAY_IN_SECONDS;
828 836
829 - if ( null === $text ) {
830 - $text = isset( $new_item['text'] ) ? $new_item['text'] : '';
831 - }
837 + $count = (int) ( $request->get_param( 'count' ) ? $request->get_param( 'count' ) : 6 );
838 + $consumer_key = $request->get_param( 'consumer_key' );
839 + $consumer_secret = $request->get_param( 'consumer_secret' );
840 + $access_token = $request->get_param( 'access_token' );
841 + $access_token_secret = $request->get_param( 'access_token_secret' );
842 + $screen_name = $request->get_param( 'screen_name' );
843 + $exclude_replies = 'true' === $request->get_param( 'exclude_replies' );
844 + $include_rts = 'true' === $request->get_param( 'include_rts' );
845 + $tweet_mode_extended = 'true' === $request->get_param( 'tweet_mode_extended' );
832 846
833 - $new_item['text'] = $text;
834 - }
847 + $api_data_ready = $consumer_key && $consumer_secret && $access_token && $access_token_secret;
835 848
836 - // prepare tweet content.
837 - $new_item = $this->prepare_tweet_content( $new_item );
849 + if ( ! $consumer_key ) {
850 + return $this->error( 'no_consumer_key_found', __( 'Provide Twitter Consumer Key.', 'ghostkit' ) );
851 + }
852 + if ( ! $consumer_secret ) {
853 + return $this->error( 'no_consumer_secret_found', __( 'Provide Twitter Consumer Secret.', 'ghostkit' ) );
854 + }
855 + if ( ! $access_token ) {
856 + return $this->error( 'no_access_token_found', __( 'Provide Twitter Access Token.', 'ghostkit' ) );
857 + }
858 + if ( ! $access_token_secret ) {
859 + return $this->error( 'no_access_token_secret_found', __( 'Provide Twitter Access Token Secret.', 'ghostkit' ) );
860 + }
861 + if ( ! $screen_name ) {
862 + return $this->error( 'no_screen_name_found', __( 'Provide Username.', 'ghostkit' ) );
863 + }
838 864
839 - if ( isset( $new_item['retweeted_status'] ) ) {
840 - // full text.
841 - if ( $tweet_mode_extended || ! isset( $new_item['retweeted_status']['text'] ) ) {
842 - $text = isset( $new_item['retweeted_status']['full_text'] ) ? $new_item['retweeted_status']['full_text'] : null;
865 + $hash = md5(
866 + wp_json_encode(
867 + array(
868 + $consumer_key,
869 + $consumer_secret,
870 + $access_token,
871 + $access_token_secret,
872 + $cache_expiration,
873 + $tweet_mode_extended,
874 + )
875 + )
876 + );
843 877
844 - if ( null === $text ) {
845 - $text = isset( $new_item['retweeted_status']['text'] ) ? $new_item['retweeted_status']['text'] : '';
846 - }
878 + $cache_name = $cache_name . '_' . $screen_name . '_' . $hash;
847 879
848 - $new_item['retweeted_status']['text'] = $text;
849 - }
880 + // get cached data.
881 + $feed = get_transient( $cache_name );
850 882
851 - $new_item['retweeted_status'] = $this->prepare_tweet_content( $new_item['retweeted_status'] );
852 - }
883 + $result = array();
853 884
854 - $result[] = $new_item;
885 + if ( $api_data_ready ) {
886 + // if there is no cache available, request twitter feed.
887 + if ( false === $feed ) {
888 + $feed = $this->request_api_twitter(
889 + array(
890 + 'url' => 'https://api.twitter.com/1.1/statuses/user_timeline.json',
891 + 'consumer_key' => $consumer_key,
892 + 'consumer_secret' => $consumer_secret,
893 + 'access_token' => $access_token,
894 + 'access_token_secret' => $access_token_secret,
895 + 'screen_name' => $screen_name,
896 + 'tweet_mode_extended' => $tweet_mode_extended,
897 + 'exclude_replies' => 'false',
898 + 'include_rts' => 'true',
899 + 'count' => 200,
900 + )
901 + );
855 902
856 - if ( count( $result ) >= $limit_to_display ) {
857 - break;
858 - }
859 - }
860 - }
861 - }
903 + if ( $feed && ! ( isset( $feed['errors'] ) && count( $feed['errors'] ) > 0 ) ) {
904 + set_transient( $cache_name, $feed, $cache_expiration );
905 + } else {
906 + $feed = false;
907 + }
908 + }
862 909
863 - if ( $result ) {
864 - return $this->success( $result );
865 - } else {
866 - return $this->error( 'no_twitter_data_loaded', __( 'Twitter data failed to load.', 'ghostkit' ) );
867 - }
868 - }
910 + $tweets_count = $feed ? count( $feed ) : 0;
911 + $limit_to_display = min( $count, $tweets_count );
869 912
870 - /**
871 - * Get Twitter API url result
872 - *
873 - * @param array $data - api request data.
874 - *
875 - * @return bool|mixed
876 - */
877 - public function request_api_twitter( $data ) {
878 - $data = array_merge(
879 - array(
880 - 'url' => 'https://api.twitter.com/1.1/statuses/user_timeline.json',
881 - 'consumer_key' => '',
882 - 'consumer_secret' => '',
883 - 'access_token' => '',
884 - 'access_token_secret' => '',
885 - 'screen_name' => '',
886 - 'exclude_replies' => '',
887 - 'include_rts' => '',
888 - 'count' => '',
889 - 'include_entities' => '',
890 - 'tweet_mode_extended' => '',
891 - ),
892 - $data
893 - );
913 + if ( $limit_to_display > 0 ) {
914 + for ( $i = 0; $i < $tweets_count; $i++ ) {
915 + $new_item = $feed[ $i ];
894 916
895 - $oauth = array(
896 - 'oauth_consumer_key' => $data['consumer_key'],
897 - 'oauth_nonce' => time(),
898 - 'oauth_signature_method' => 'HMAC-SHA1',
899 - 'oauth_token' => $data['access_token'],
900 - 'oauth_timestamp' => time(),
901 - 'oauth_version' => '1.0',
902 - );
917 + // check for replies.
918 + if ( $exclude_replies && $new_item['in_reply_to_user_id'] ) {
919 + continue;
920 + }
903 921
904 - $base_info_url = $data['url'];
922 + // check for retweets.
923 + if ( ! $include_rts && isset( $new_item['retweeted_status'] ) ) {
924 + continue;
925 + }
905 926
906 - if ( $data['screen_name'] ) {
907 - $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
908 - $data['url'] .= 'screen_name=' . $data['screen_name'];
909 - $oauth['screen_name'] = $data['screen_name'];
910 - }
911 - if ( $data['exclude_replies'] ) {
912 - $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
913 - $data['url'] .= 'exclude_replies=' . $data['exclude_replies'];
914 - $oauth['exclude_replies'] = $data['exclude_replies'];
915 - }
916 - if ( $data['include_rts'] ) {
917 - $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
918 - $data['url'] .= 'include_rts=' . $data['include_rts'];
919 - $oauth['include_rts'] = $data['include_rts'];
920 - }
921 - if ( $data['count'] ) {
922 - $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
923 - $data['url'] .= 'count=' . $data['count'];
924 - $oauth['count'] = $data['count'];
925 - }
926 - if ( $data['include_entities'] ) {
927 - $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
928 - $data['url'] .= 'include_entities=' . $data['include_entities'];
929 - $oauth['include_entities'] = $data['include_entities'];
930 - }
927 + // full text.
928 + if ( $tweet_mode_extended || ! isset( $new_item['text'] ) ) {
929 + $text = isset( $new_item['full_text'] ) ? $new_item['full_text'] : null;
931 930
932 - // Tweet Mode.
933 - if ( $data['tweet_mode_extended'] ) {
934 - $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
935 - $data['url'] .= 'tweet_mode=extended';
936 - $oauth['tweet_mode'] = 'extended';
937 - }
931 + if ( null === $text ) {
932 + $text = isset( $new_item['text'] ) ? $new_item['text'] : '';
933 + }
938 934
939 - $base_info = $this->build_base_string( $base_info_url, 'GET', $oauth );
940 - $composite_key = rawurlencode( $data['consumer_secret'] ) . '&' . rawurlencode( $data['access_token_secret'] );
935 + $new_item['text'] = $text;
936 + }
941 937
938 + // prepare tweet content.
939 + $new_item = $this->prepare_tweet_content( $new_item );
940 +
941 + if ( isset( $new_item['retweeted_status'] ) ) {
942 + // full text.
943 + if ( $tweet_mode_extended || ! isset( $new_item['retweeted_status']['text'] ) ) {
944 + $text = isset( $new_item['retweeted_status']['full_text'] ) ? $new_item['retweeted_status']['full_text'] : null;
945 +
946 + if ( null === $text ) {
947 + $text = isset( $new_item['retweeted_status']['text'] ) ? $new_item['retweeted_status']['text'] : '';
948 + }
949 +
950 + $new_item['retweeted_status']['text'] = $text;
951 + }
952 +
953 + $new_item['retweeted_status'] = $this->prepare_tweet_content( $new_item['retweeted_status'] );
954 + }
955 +
956 + $result[] = $new_item;
957 +
958 + if ( count( $result ) >= $limit_to_display ) {
959 + break;
960 + }
961 + }
962 + }
963 + }
964 +
965 + if ( $result ) {
966 + return $this->success( $result );
967 + } else {
968 + return $this->error( 'no_twitter_data_loaded', __( 'Twitter data failed to load.', 'ghostkit' ) );
969 + }
970 + }
971 +
972 + /**
973 + * Get Twitter API url result
974 + *
975 + * @param array $data - api request data.
976 + *
977 + * @return bool|mixed
978 + */
979 + public function request_api_twitter( $data ) {
980 + $data = array_merge(
981 + array(
982 + 'url' => 'https://api.twitter.com/1.1/statuses/user_timeline.json',
983 + 'consumer_key' => '',
984 + 'consumer_secret' => '',
985 + 'access_token' => '',
986 + 'access_token_secret' => '',
987 + 'screen_name' => '',
988 + 'exclude_replies' => '',
989 + 'include_rts' => '',
990 + 'count' => '',
991 + 'include_entities' => '',
992 + 'tweet_mode_extended' => '',
993 + ),
994 + $data
995 + );
996 +
997 + $oauth = array(
998 + 'oauth_consumer_key' => $data['consumer_key'],
999 + 'oauth_nonce' => time(),
1000 + 'oauth_signature_method' => 'HMAC-SHA1',
1001 + 'oauth_token' => $data['access_token'],
1002 + 'oauth_timestamp' => time(),
1003 + 'oauth_version' => '1.0',
1004 + );
1005 +
1006 + $base_info_url = $data['url'];
1007 +
1008 + if ( $data['screen_name'] ) {
1009 + $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
1010 + $data['url'] .= 'screen_name=' . $data['screen_name'];
1011 + $oauth['screen_name'] = $data['screen_name'];
1012 + }
1013 + if ( $data['exclude_replies'] ) {
1014 + $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
1015 + $data['url'] .= 'exclude_replies=' . $data['exclude_replies'];
1016 + $oauth['exclude_replies'] = $data['exclude_replies'];
1017 + }
1018 + if ( $data['include_rts'] ) {
1019 + $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
1020 + $data['url'] .= 'include_rts=' . $data['include_rts'];
1021 + $oauth['include_rts'] = $data['include_rts'];
1022 + }
1023 + if ( $data['count'] ) {
1024 + $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
1025 + $data['url'] .= 'count=' . $data['count'];
1026 + $oauth['count'] = $data['count'];
1027 + }
1028 + if ( $data['include_entities'] ) {
1029 + $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
1030 + $data['url'] .= 'include_entities=' . $data['include_entities'];
1031 + $oauth['include_entities'] = $data['include_entities'];
1032 + }
1033 +
1034 + // Tweet Mode.
1035 + if ( $data['tweet_mode_extended'] ) {
1036 + $data['url'] .= strpos( $data['url'], '?' ) !== false ? '&' : '?';
1037 + $data['url'] .= 'tweet_mode=extended';
1038 + $oauth['tweet_mode'] = 'extended';
1039 + }
1040 +
1041 + $base_info = $this->build_base_string( $base_info_url, 'GET', $oauth );
1042 + $composite_key = rawurlencode( $data['consumer_secret'] ) . '&' . rawurlencode( $data['access_token_secret'] );
1043 +
942 1044 // phpcs:ignore
943 1045 $oauth_signature = base64_encode( hash_hmac( 'sha1', $base_info, $composite_key, true ) );
944 - $oauth['oauth_signature'] = $oauth_signature;
1046 + $oauth['oauth_signature'] = $oauth_signature;
945 1047
946 - // Make Requests.
947 - $header = array( $this->build_authorization_header( $oauth ), 'Expect:' );
948 - $options_buf = wp_remote_get(
949 - $data['url'],
950 - array(
951 - 'headers' => implode( "\n", $header ),
952 - 'sslverify' => false,
953 - )
954 - );
1048 + // Make Requests.
1049 + $header = array( $this->build_authorization_header( $oauth ), 'Expect:' );
1050 + $options_buf = wp_remote_get(
1051 + $data['url'],
1052 + array(
1053 + 'headers' => implode( "\n", $header ),
1054 + 'sslverify' => false,
1055 + )
1056 + );
955 1057
956 - if ( ! is_wp_error( $options_buf ) && isset( $options_buf['body'] ) ) {
957 - return json_decode( $options_buf['body'], true );
958 - } else {
959 - return false;
960 - }
961 - }
1058 + if ( ! is_wp_error( $options_buf ) && isset( $options_buf['body'] ) ) {
1059 + return json_decode( $options_buf['body'], true );
1060 + } else {
1061 + return false;
1062 + }
1063 + }
962 1064
963 - /**
964 - * Build base string
965 - *
966 - * @param string $base_uri - url.
967 - * @param string $method - method.
968 - * @param array $params - params.
969 - *
970 - * @return string
971 - */
972 - private function build_base_string( $base_uri, $method, $params ) {
973 - $r = array();
974 - ksort( $params );
975 - foreach ( $params as $key => $value ) {
976 - $r[] = "$key=" . rawurlencode( $value );
977 - }
978 - return $method . '&' . rawurlencode( $base_uri ) . '&' . rawurlencode( implode( '&', $r ) );
979 - }
1065 + /**
1066 + * Build base string
1067 + *
1068 + * @param string $base_uri - url.
1069 + * @param string $method - method.
1070 + * @param array $params - params.
1071 + *
1072 + * @return string
1073 + */
1074 + private function build_base_string( $base_uri, $method, $params ) {
1075 + $r = array();
1076 + ksort( $params );
1077 + foreach ( $params as $key => $value ) {
1078 + $r[] = "$key=" . rawurlencode( $value );
1079 + }
1080 + return $method . '&' . rawurlencode( $base_uri ) . '&' . rawurlencode( implode( '&', $r ) );
1081 + }
980 1082
981 - /**
982 - * Build authorization header
983 - *
984 - * @param array $oauth - auth data.
985 - *
986 - * @return string
987 - */
988 - private function build_authorization_header( $oauth ) {
989 - $r = 'Authorization: OAuth ';
990 - $values = array();
1083 + /**
1084 + * Build authorization header
1085 + *
1086 + * @param array $oauth - auth data.
1087 + *
1088 + * @return string
1089 + */
1090 + private function build_authorization_header( $oauth ) {
1091 + $r = 'Authorization: OAuth ';
1092 + $values = array();
991 1093
992 - foreach ( $oauth as $key => $value ) {
993 - $values[] = "$key=\"" . rawurlencode( $value ) . '"';
994 - }
1094 + foreach ( $oauth as $key => $value ) {
1095 + $values[] = "$key=\"" . rawurlencode( $value ) . '"';
1096 + }
995 1097
996 - $r .= implode( ', ', $values );
1098 + $r .= implode( ', ', $values );
997 1099
998 - return $r;
999 - }
1100 + return $r;
1101 + }
1000 1102
1001 - /**
1002 - * Prepare tweet content.
1003 - *
1004 - * @param array $tweet - tweet data.
1005 - *
1006 - * @return array - new tweet data.
1007 - */
1008 - public function prepare_tweet_content( $tweet ) {
1009 - // prepare different profile image sizes.
1010 - if ( $tweet['user']['profile_image_url'] ) {
1011 - $tweet['user']['profile_images'] = $this->get_twitter_profile_images( $tweet['user']['profile_image_url'] );
1012 - }
1013 - if ( $tweet['user']['profile_image_url_https'] ) {
1014 - $tweet['user']['profile_images_https'] = $this->get_twitter_profile_images( $tweet['user']['profile_image_url_https'] );
1015 - }
1103 + /**
1104 + * Prepare tweet content.
1105 + *
1106 + * @param array $tweet - tweet data.
1107 + *
1108 + * @return array - new tweet data.
1109 + */
1110 + public function prepare_tweet_content( $tweet ) {
1111 + // prepare different profile image sizes.
1112 + if ( $tweet['user']['profile_image_url'] ) {
1113 + $tweet['user']['profile_images'] = $this->get_twitter_profile_images( $tweet['user']['profile_image_url'] );
1114 + }
1115 + if ( $tweet['user']['profile_image_url_https'] ) {
1116 + $tweet['user']['profile_images_https'] = $this->get_twitter_profile_images( $tweet['user']['profile_image_url_https'] );
1117 + }
1016 1118
1017 - // prepare short counts.
1018 - $tweet['retweet_count_short'] = $this->convert_number_short( $tweet['retweet_count'] );
1019 - $tweet['favorite_count_short'] = $this->convert_number_short( $tweet['favorite_count'] );
1119 + // prepare short counts.
1120 + $tweet['retweet_count_short'] = $this->convert_number_short( $tweet['retweet_count'] );
1121 + $tweet['favorite_count_short'] = $this->convert_number_short( $tweet['favorite_count'] );
1020 1122
1021 - // user friendly date.
1022 - $date = strtotime( $tweet['created_at'] );
1023 - $now = time();
1024 - $diff_date = $now - $date;
1123 + // user friendly date.
1124 + $date = strtotime( $tweet['created_at'] );
1125 + $now = time();
1126 + $diff_date = $now - $date;
1025 1127
1026 - if ( $diff_date / 60 < 1 ) {
1027 - // seconds.
1028 - // translators: %d - seconds.
1029 - $date = sprintf( esc_html__( '%ds', 'ghostkit' ), intval( $diff_date % 60 ) );
1030 - } elseif ( $diff_date / 60 < 60 ) {
1031 - // minutes.
1032 - // translators: %d - minutes.
1033 - $date = sprintf( esc_html__( '%dm', 'ghostkit' ), intval( $diff_date / 60 ) );
1034 - } elseif ( $diff_date / 3600 < 24 ) {
1035 - // hours.
1036 - // translators: %d - hours.
1037 - $date = sprintf( esc_html__( '%dh', 'ghostkit' ), intval( $diff_date / 3600 ) );
1038 - } elseif ( gmdate( 'Y' ) === gmdate( 'Y', $date ) ) {
1039 - // current year.
1040 - $date = gmdate( esc_html__( 'M j', 'ghostkit' ), $date );
1041 - } else {
1042 - // past years.
1043 - $date = gmdate( esc_html__( 'Y M j', 'ghostkit' ), $date );
1044 - }
1128 + if ( $diff_date / 60 < 1 ) {
1129 + // seconds.
1130 + // translators: %d - seconds.
1131 + $date = sprintf( esc_html__( '%ds', 'ghostkit' ), intval( $diff_date % 60 ) );
1132 + } elseif ( $diff_date / 60 < 60 ) {
1133 + // minutes.
1134 + // translators: %d - minutes.
1135 + $date = sprintf( esc_html__( '%dm', 'ghostkit' ), intval( $diff_date / 60 ) );
1136 + } elseif ( $diff_date / 3600 < 24 ) {
1137 + // hours.
1138 + // translators: %d - hours.
1139 + $date = sprintf( esc_html__( '%dh', 'ghostkit' ), intval( $diff_date / 3600 ) );
1140 + } elseif ( gmdate( 'Y' ) === gmdate( 'Y', $date ) ) {
1141 + // current year.
1142 + $date = gmdate( esc_html__( 'M j', 'ghostkit' ), $date );
1143 + } else {
1144 + // past years.
1145 + $date = gmdate( esc_html__( 'Y M j', 'ghostkit' ), $date );
1146 + }
1045 1147
1046 - $tweet['date_formatted'] = $date;
1148 + $tweet['date_formatted'] = $date;
1047 1149
1048 - // text with links and media.
1049 - $tweet['text_entitled'] = $this->add_tweet_entity_links( $tweet['text'], $tweet['entities'] );
1150 + // text with links and media.
1151 + $tweet['text_entitled'] = $this->add_tweet_entity_links( $tweet['text'], $tweet['entities'] );
1050 1152
1051 - // text with links only.
1052 - $tweet['text_entitled_no_media'] = $this->add_tweet_entity_links( $tweet['text'], $tweet['entities'], false );
1153 + // text with links only.
1154 + $tweet['text_entitled_no_media'] = $this->add_tweet_entity_links( $tweet['text'], $tweet['entities'], false );
1053 1155
1054 - return $tweet;
1055 - }
1156 + return $tweet;
1157 + }
1056 1158
1057 - /**
1058 - * Adds a link around any entities in a twitter feed
1059 - * twitter entities include urls, user mentions, hashtags and media
1060 - * http://stackoverflow.com/a/15390225
1061 - *
1062 - * @param string $text - tweet text.
1063 - * @param array $entities - available entities.
1064 - * @param boolean $show_media - show tweet images.
1065 - *
1066 - * @return string tweet
1067 - */
1068 - public function add_tweet_entity_links( $text, $entities, $show_media = true ) {
1069 - $replacements = array();
1070 - if ( isset( $entities['hashtags'] ) ) {
1071 - foreach ( $entities['hashtags'] as $hashtag ) {
1072 - list ($start, $end) = $hashtag['indices'];
1073 - $replacements[ $start ] = array(
1074 - $start,
1075 - $end,
1076 - "<a href=\"https://twitter.com/hashtag/{$hashtag['text']}\" target=\"_blank\">#{$hashtag['text']}</a>",
1077 - );
1078 - }
1079 - }
1080 - if ( isset( $entities['urls'] ) ) {
1081 - foreach ( $entities['urls'] as $url ) {
1082 - list ($start, $end) = $url['indices'];
1083 - // you can also use $url['expanded_url'] in place of $url['url'].
1084 - $replacements[ $start ] = array(
1085 - $start,
1086 - $end,
1087 - "<a href=\"{$url['url']}\" target=\"_blank\">{$url['display_url']}</a>",
1088 - );
1089 - }
1090 - }
1091 - if ( isset( $entities['user_mentions'] ) ) {
1092 - foreach ( $entities['user_mentions'] as $mention ) {
1093 - list ($start, $end) = $mention['indices'];
1094 - $replacements[ $start ] = array(
1095 - $start,
1096 - $end,
1097 - "<a href=\"https://twitter.com/{$mention['screen_name']}\" target=\"_blank\">@{$mention['screen_name']}</a>",
1098 - );
1099 - }
1100 - }
1101 - if ( isset( $entities['media'] ) ) {
1102 - foreach ( $entities['media'] as $media ) {
1103 - list ($start, $end) = $media['indices'];
1159 + /**
1160 + * Adds a link around any entities in a twitter feed
1161 + * twitter entities include urls, user mentions, hashtags and media
1162 + * http://stackoverflow.com/a/15390225
1163 + *
1164 + * @param string $text - tweet text.
1165 + * @param array $entities - available entities.
1166 + * @param boolean $show_media - show tweet images.
1167 + *
1168 + * @return string tweet
1169 + */
1170 + public function add_tweet_entity_links( $text, $entities, $show_media = true ) {
1171 + $replacements = array();
1172 + if ( isset( $entities['hashtags'] ) ) {
1173 + foreach ( $entities['hashtags'] as $hashtag ) {
1174 + list ($start, $end) = $hashtag['indices'];
1175 + $replacements[ $start ] = array(
1176 + $start,
1177 + $end,
1178 + "<a href=\"https://twitter.com/hashtag/{$hashtag['text']}\" target=\"_blank\">#{$hashtag['text']}</a>",
1179 + );
1180 + }
1181 + }
1182 + if ( isset( $entities['urls'] ) ) {
1183 + foreach ( $entities['urls'] as $url ) {
1184 + list ($start, $end) = $url['indices'];
1185 + // you can also use $url['expanded_url'] in place of $url['url'].
1186 + $replacements[ $start ] = array(
1187 + $start,
1188 + $end,
1189 + "<a href=\"{$url['url']}\" target=\"_blank\">{$url['display_url']}</a>",
1190 + );
1191 + }
1192 + }
1193 + if ( isset( $entities['user_mentions'] ) ) {
1194 + foreach ( $entities['user_mentions'] as $mention ) {
1195 + list ($start, $end) = $mention['indices'];
1196 + $replacements[ $start ] = array(
1197 + $start,
1198 + $end,
1199 + "<a href=\"https://twitter.com/{$mention['screen_name']}\" target=\"_blank\">@{$mention['screen_name']}</a>",
1200 + );
1201 + }
1202 + }
1203 + if ( isset( $entities['media'] ) ) {
1204 + foreach ( $entities['media'] as $media ) {
1205 + list ($start, $end) = $media['indices'];
1104 1206
1105 - if ( $show_media ) {
1106 - $replacements[ $start ] = array(
1107 - $start,
1108 - $end,
1109 - "<div><a href=\"{$media['url']}\" target=\"_blank\"><img src=\"{$media['media_url_https']}\" /></a></div>",
1110 - );
1111 - } else {
1112 - $replacements[ $start ] = array(
1113 - $start,
1114 - $end,
1115 - "<a href=\"{$media['url']}\" target=\"_blank\">{$media['url']}</a>",
1116 - );
1117 - }
1118 - }
1119 - }
1207 + if ( $show_media ) {
1208 + $replacements[ $start ] = array(
1209 + $start,
1210 + $end,
1211 + "<div><a href=\"{$media['url']}\" target=\"_blank\"><img src=\"{$media['media_url_https']}\" /></a></div>",
1212 + );
1213 + } else {
1214 + $replacements[ $start ] = array(
1215 + $start,
1216 + $end,
1217 + "<a href=\"{$media['url']}\" target=\"_blank\">{$media['url']}</a>",
1218 + );
1219 + }
1220 + }
1221 + }
1120 1222
1121 - // sort in reverse order by start location.
1122 - krsort( $replacements );
1223 + // sort in reverse order by start location.
1224 + krsort( $replacements );
1123 1225
1124 - foreach ( $replacements as $replace_data ) {
1125 - list ($start, $end, $replace_text) = $replace_data;
1226 + foreach ( $replacements as $replace_data ) {
1227 + list ($start, $end, $replace_text) = $replace_data;
1126 1228
1127 - $text = mb_substr( $text, 0, $start, 'UTF-8' ) . $replace_text . mb_substr( $text, $end, null, 'UTF-8' );
1128 - }
1229 + $text = mb_substr( $text, 0, $start, 'UTF-8' ) . $replace_text . mb_substr( $text, $end, null, 'UTF-8' );
1230 + }
1129 1231
1130 - return $text;
1131 - }
1232 + return $text;
1233 + }
1132 1234
1133 - /**
1134 - * Convert long numbers to short. eg: 1500 -> 1.5K
1135 - * Thanks https://code.recuweb.com/2018/php-format-numbers-to-nearest-thousands/ .
1136 - *
1137 - * @param number $num - number.
1138 - * @return string
1139 - */
1140 - public function convert_number_short( $num ) {
1141 - if ( $num > 1000 ) {
1142 - $x = round( $num );
1143 - $x_number_format = number_format( $x );
1144 - $x_array = explode( ',', $x_number_format );
1145 - $x_parts = array( 'K', 'M', 'B', 'T' );
1146 - $x_count_parts = count( $x_array ) - 1;
1147 - $x_display = $x;
1148 - $x_display = $x_array[0] . ( (int) 0 !== $x_array[1][0] ? '.' . $x_array[1][0] : '' );
1149 - $x_display .= $x_parts[ $x_count_parts - 1 ];
1235 + /**
1236 + * Convert long numbers to short. eg: 1500 -> 1.5K
1237 + * Thanks https://code.recuweb.com/2018/php-format-numbers-to-nearest-thousands/ .
1238 + *
1239 + * @param number $num - number.
1240 + * @return string
1241 + */
1242 + public function convert_number_short( $num ) {
1243 + if ( $num > 1000 ) {
1244 + $x = round( $num );
1245 + $x_number_format = number_format( $x );
1246 + $x_array = explode( ',', $x_number_format );
1247 + $x_parts = array( 'K', 'M', 'B', 'T' );
1248 + $x_count_parts = count( $x_array ) - 1;
1249 + $x_display = $x;
1250 + $x_display = $x_array[0] . ( (int) 0 !== $x_array[1][0] ? '.' . $x_array[1][0] : '' );
1251 + $x_display .= $x_parts[ $x_count_parts - 1 ];
1150 1252
1151 - return $x_display;
1152 - }
1253 + return $x_display;
1254 + }
1153 1255
1154 - return $num;
1155 - }
1256 + return $num;
1257 + }
1156 1258
1157 - /**
1158 - * Prepare profile image urls
1159 - * https://developer.twitter.com/en/docs/accounts-and-users/user-profile-images-and-banners
1160 - *
1161 - * @param string $url - profile image.
1162 - *
1163 - * @return array images
1164 - */
1165 - public function get_twitter_profile_images( $url ) {
1166 - if ( strpos( $url, '.jpg' ) !== false ) {
1167 - return array(
1168 - 'normal' => $url,
1169 - 'bigger' => str_replace( '_normal.jpg', '_bigger.jpg', $url ),
1170 - 'mini' => str_replace( '_normal.jpg', '_mini.jpg', $url ),
1171 - 'original' => str_replace( '_normal.jpg', '.jpg', $url ),
1172 - );
1173 - }
1259 + /**
1260 + * Prepare profile image urls
1261 + * https://developer.twitter.com/en/docs/accounts-and-users/user-profile-images-and-banners
1262 + *
1263 + * @param string $url - profile image.
1264 + *
1265 + * @return array images
1266 + */
1267 + public function get_twitter_profile_images( $url ) {
1268 + if ( strpos( $url, '.jpg' ) !== false ) {
1269 + return array(
1270 + 'normal' => $url,
1271 + 'bigger' => str_replace( '_normal.jpg', '_bigger.jpg', $url ),
1272 + 'mini' => str_replace( '_normal.jpg', '_mini.jpg', $url ),
1273 + 'original' => str_replace( '_normal.jpg', '.jpg', $url ),
1274 + );
1275 + }
1174 1276
1175 - return array(
1176 - 'normal' => $url,
1177 - 'bigger' => str_replace( '_normal.png', '_bigger.png', $url ),
1178 - 'mini' => str_replace( '_normal.png', '_mini.png', $url ),
1179 - 'original' => str_replace( '_normal.png', '.png', $url ),
1180 - );
1181 - }
1277 + return array(
1278 + 'normal' => $url,
1279 + 'bigger' => str_replace( '_normal.png', '_bigger.png', $url ),
1280 + 'mini' => str_replace( '_normal.png', '_mini.png', $url ),
1281 + 'original' => str_replace( '_normal.png', '.png', $url ),
1282 + );
1283 + }
1182 1284
1183 - /**
1184 - * Get TOC.
1185 - *
1186 - * @param WP_REST_Request $request request object.
1187 - *
1188 - * @return mixed
1189 - */
1190 - public function get_table_of_contents( WP_REST_Request $request ) {
1191 - $headings = $request->get_param( 'headings' );
1192 - $allowed_headers = $request->get_param( 'allowedHeaders' );
1193 - $list_style = $request->get_param( 'listStyle' ) ? $request->get_param( 'listStyle' ) : 'ol';
1285 + /**
1286 + * Sanitize TOC headings list.
1287 + *
1288 + * @param mixed $headings request headings.
1289 + * @return array
1290 + */
1291 + public function sanitize_toc_headings( $headings ) {
1292 + if ( ! is_array( $headings ) ) {
1293 + return array();
1294 + }
1194 1295
1195 - $html = '';
1296 + $sanitized = array();
1196 1297
1197 - if ( ! $allowed_headers || empty( $allowed_headers ) ) {
1198 - return $this->success( $html );
1199 - }
1298 + foreach ( $headings as $heading ) {
1299 + if ( ! is_array( $heading ) ) {
1300 + continue;
1301 + }
1200 1302
1201 - // covert string values to int.
1202 - $allowed_headers = array_map( 'intval', $allowed_headers );
1303 + $level = isset( $heading['level'] ) ? absint( $heading['level'] ) : 0;
1203 1304
1204 - $current_depth = 6;
1205 - $numbered_items = array();
1206 - $numbered_items_min = null;
1207 - $count = count( $headings );
1305 + if ( $level < 1 || $level > 6 ) {
1306 + continue;
1307 + }
1208 1308
1209 - // find the minimum heading to establish our baseline.
1210 - for ( $i = 0; $i < $count; $i++ ) {
1211 - if ( $current_depth > (int) $headings[ $i ]['level'] ) {
1212 - $current_depth = (int) $headings[ $i ]['level'];
1213 - }
1214 - }
1309 + $sanitized[] = array(
1310 + 'level' => $level,
1311 + 'content' => isset( $heading['content'] ) ? wp_kses_post( $heading['content'] ) : '',
1312 + 'anchor' => isset( $heading['anchor'] ) ? sanitize_text_field( $heading['anchor'] ) : '',
1313 + );
1314 + }
1215 1315
1216 - $numbered_items[ $current_depth ] = 0;
1217 - $numbered_items_min = $current_depth;
1218 - for ( $i = 0; $i < $count; $i ++ ) {
1219 - if ( $current_depth === (int) $headings[ $i ]['level'] ) {
1220 - $html .= '<li>';
1221 - }
1316 + return $sanitized;
1317 + }
1222 1318
1223 - // start lists.
1224 - if ( $current_depth !== (int) $headings[ $i ]['level'] ) {
1225 - for ( $current_depth; $current_depth < (int) $headings[ $i ]['level']; $current_depth++ ) {
1226 - $numbered_items[ $current_depth + 1 ] = 0;
1319 + /**
1320 + * Sanitize TOC allowed headers list.
1321 + *
1322 + * @param mixed $allowed_headers request allowed headers.
1323 + * @return array
1324 + */
1325 + public function sanitize_toc_allowed_headers( $allowed_headers ) {
1326 + if ( ! is_array( $allowed_headers ) ) {
1327 + return array();
1328 + }
1227 1329
1228 - if ( ! in_array( $current_depth, $allowed_headers, true ) ) {
1229 - continue;
1230 - }
1330 + $sanitized = array_values(
1331 + array_unique(
1332 + array_filter(
1333 + array_map( 'absint', $allowed_headers ),
1334 + static function ( $level ) {
1335 + return $level >= 1 && $level <= 6;
1336 + }
1337 + )
1338 + )
1339 + );
1231 1340
1232 - $html .= '<ul>';
1233 - $html .= '<li>';
1234 - }
1235 - }
1341 + return $sanitized;
1342 + }
1236 1343
1237 - $html .= '<a href="' . esc_attr( '#' . $headings[ $i ]['anchor'] ) . '">' . wp_kses_post( $headings[ $i ]['content'] ) . '</a>';
1344 + /**
1345 + * Sanitize TOC list style.
1346 + *
1347 + * @param mixed $list_style request list style.
1348 + * @return string
1349 + */
1350 + public function sanitize_toc_list_style( $list_style ) {
1351 + $list_style = is_string( $list_style ) ? sanitize_text_field( $list_style ) : 'ol';
1352 + $allowed = array( 'ol', 'ul', 'ol-styled', 'ul-styled' );
1238 1353
1239 - // end lists.
1240 - if ( $i !== $count - 1 ) {
1241 - if ( $current_depth > (int) $headings[ $i + 1 ]['level'] ) {
1242 - for ( $current_depth; $current_depth > (int) $headings[ $i + 1 ]['level']; $current_depth-- ) {
1243 - $numbered_items[ $current_depth ] = 0;
1354 + return in_array( $list_style, $allowed, true ) ? $list_style : 'ol';
1355 + }
1244 1356
1245 - if ( ! in_array( $current_depth, $allowed_headers, true ) ) {
1246 - continue;
1247 - }
1357 + /**
1358 + * Get TOC.
1359 + *
1360 + * @param WP_REST_Request $request request object.
1361 + *
1362 + * @return mixed
1363 + */
1364 + public function get_table_of_contents( WP_REST_Request $request ) {
1365 + $headings = $request->get_param( 'headings' );
1366 + $allowed_headers = $request->get_param( 'allowedHeaders' );
1367 + $list_style = $request->get_param( 'listStyle' );
1248 1368
1249 - $html .= '</li>';
1250 - $html .= '</ul>';
1251 - }
1252 - }
1253 - if ( isset( $headings[ $i + 1 ]['level'] ) && (int) $headings[ $i + 1 ]['level'] === $current_depth ) {
1254 - $html .= '</li>';
1255 - }
1256 - } else {
1257 - // this is the last item, make sure we close off all tags.
1258 - for ( $current_depth; $current_depth >= $numbered_items_min; $current_depth-- ) {
1259 - if ( ! in_array( $current_depth, $allowed_headers, true ) ) {
1260 - continue;
1261 - }
1369 + $html = '';
1262 1370
1263 - $html .= '</li>';
1264 - if ( $current_depth !== $numbered_items_min ) {
1265 - $html .= '</ul>';
1266 - }
1267 - }
1268 - }
1269 - }
1371 + if ( empty( $allowed_headers ) || empty( $headings ) ) {
1372 + return $this->success( $html );
1373 + }
1270 1374
1271 - // Wrapper.
1272 - if ( $html ) {
1273 - $list = 'ol';
1274 - $list_class_name = '';
1375 + $current_depth = 6;
1376 + $numbered_items = array();
1377 + $numbered_items_min = null;
1378 + $count = count( $headings );
1275 1379
1276 - switch ( $list_style ) {
1277 - case 'ul':
1278 - $list = 'ul';
1279 - break;
1280 - case 'ul-styled':
1281 - $list = 'ul';
1282 - $list_class_name = 'is-style-styled';
1283 - break;
1284 - case 'ol-styled':
1285 - $list_class_name = 'is-style-styled';
1286 - break;
1287 - }
1380 + // find the minimum heading to establish our baseline.
1381 + for ( $i = 0; $i < $count; $i++ ) {
1382 + if ( $current_depth > (int) $headings[ $i ]['level'] ) {
1383 + $current_depth = (int) $headings[ $i ]['level'];
1384 + }
1385 + }
1288 1386
1289 - $html = '<' . $list . ( $list_class_name ? ( ' class="' . $list_class_name . '"' ) : '' ) . '>' . $html . '</' . $list . '>';
1290 - }
1387 + $numbered_items[ $current_depth ] = 0;
1388 + $numbered_items_min = $current_depth;
1389 + for ( $i = 0; $i < $count; $i++ ) {
1390 + if ( $current_depth === (int) $headings[ $i ]['level'] ) {
1391 + $html .= '<li>';
1392 + }
1291 1393
1292 - return $this->success( $html );
1293 - }
1394 + // start lists.
1395 + if ( $current_depth !== (int) $headings[ $i ]['level'] ) {
1396 + for ( $current_depth; $current_depth < (int) $headings[ $i ]['level']; $current_depth++ ) {
1397 + $numbered_items[ $current_depth + 1 ] = 0;
1294 1398
1295 - /**
1296 - * Get templates.
1297 - *
1298 - * @return mixed
1299 - */
1300 - public function get_templates() {
1301 - $url = 'https://library.ghostkit.io/wp-json/ghostkit-library/v1/get_library/';
1302 - $templates = get_transient( 'ghostkit_remote_templates', false );
1399 + if ( ! in_array( $current_depth, $allowed_headers, true ) ) {
1400 + continue;
1401 + }
1303 1402
1304 - /*
1305 - * Get remote templates.
1306 - */
1307 - if ( ! $templates ) {
1308 - $requested_templates = wp_remote_get(
1309 - add_query_arg(
1310 - array(
1311 - 'ghostkit_version' => '2.25.0',
1312 - 'ghostkit_pro' => function_exists( 'ghostkit_pro' ),
1313 - 'ghostkit_pro_version' => function_exists( 'ghostkit_pro' ) ? ghostkit_pro()->plugin_version : null,
1314 - ),
1315 - $url
1316 - )
1317 - );
1403 + $html .= '<ul>';
1404 + $html .= '<li>';
1405 + }
1406 + }
1318 1407
1319 - if ( ! is_wp_error( $requested_templates ) ) {
1320 - $new_templates = wp_remote_retrieve_body( $requested_templates );
1321 - $new_templates = json_decode( $new_templates, true );
1408 + $html .= '<a href="' . esc_attr( '#' . $headings[ $i ]['anchor'] ) . '">' . wp_kses_post( $headings[ $i ]['content'] ) . '</a>';
1322 1409
1323 - if ( $new_templates && isset( $new_templates['response'] ) && is_array( $new_templates['response'] ) ) {
1324 - $templates = $new_templates['response'];
1325 - set_transient( 'ghostkit_remote_templates', $templates, DAY_IN_SECONDS );
1326 - }
1327 - }
1328 - }
1410 + // end lists.
1411 + if ( $i !== $count - 1 ) {
1412 + if ( $current_depth > (int) $headings[ $i + 1 ]['level'] ) {
1413 + for ( $current_depth; $current_depth > (int) $headings[ $i + 1 ]['level']; $current_depth-- ) {
1414 + $numbered_items[ $current_depth ] = 0;
1329 1415
1330 - // Remove Pro templates from array, cause for now there is no way to check if pro addon is activated.
1331 - if ( $templates ) {
1332 - foreach ( $templates as $k => $template ) {
1333 - $is_pro = false;
1416 + if ( ! in_array( $current_depth, $allowed_headers, true ) ) {
1417 + continue;
1418 + }
1334 1419
1335 - if ( isset( $template['types'] ) && is_array( $template['types'] ) ) {
1336 - foreach ( $template['types'] as $type ) {
1337 - $is_pro = $is_pro || 'pro' === $type['slug'];
1338 - }
1339 - }
1420 + $html .= '</li>';
1421 + $html .= '</ul>';
1422 + }
1423 + }
1424 + if ( isset( $headings[ $i + 1 ]['level'] ) && (int) $headings[ $i + 1 ]['level'] === $current_depth ) {
1425 + $html .= '</li>';
1426 + }
1427 + } else {
1428 + // this is the last item, make sure we close off all tags.
1429 + for ( $current_depth; $current_depth >= $numbered_items_min; $current_depth-- ) {
1430 + if ( ! in_array( $current_depth, $allowed_headers, true ) ) {
1431 + continue;
1432 + }
1340 1433
1341 - if ( $is_pro ) {
1342 - unset( $templates[ $k ] );
1343 - }
1344 - }
1345 - }
1434 + $html .= '</li>';
1435 + if ( $current_depth !== $numbered_items_min ) {
1436 + $html .= '</ul>';
1437 + }
1438 + }
1439 + }
1440 + }
1346 1441
1347 - /*
1348 - * Get user templates from db.
1349 - */
1442 + // Wrapper.
1443 + if ( $html ) {
1444 + $list = 'ol';
1445 + $list_class_name = '';
1350 1446
1351 - // Stupid hack.
1352 - // https://core.trac.wordpress.org/ticket/18408.
1353 - global $post;
1354 - $backup_global_post = $post;
1355 - $local_templates = array();
1447 + switch ( $list_style ) {
1448 + case 'ul':
1449 + $list = 'ul';
1450 + break;
1451 + case 'ul-styled':
1452 + $list = 'ul';
1453 + $list_class_name = 'is-style-styled';
1454 + break;
1455 + case 'ol-styled':
1456 + $list_class_name = 'is-style-styled';
1457 + break;
1458 + }
1356 1459
1357 - $local_templates_query = new WP_Query(
1358 - array(
1359 - 'post_type' => 'ghostkit_template',
1360 - 'posts_per_page' => -1,
1361 - 'showposts' => -1,
1362 - 'paged' => -1,
1363 - )
1364 - );
1460 + $html = '<' . $list . ( $list_class_name ? ( ' class="' . $list_class_name . '"' ) : '' ) . '>' . $html . '</' . $list . '>';
1461 + }
1365 1462
1366 - while ( $local_templates_query->have_posts() ) {
1367 - $local_templates_query->the_post();
1368 - $db_template = get_post();
1463 + return $this->success( $html );
1464 + }
1369 1465
1370 - $categories = array();
1371 - $category_terms = get_the_terms( $db_template->ID, 'ghostkit_template_category' );
1466 + /**
1467 + * Get templates.
1468 + *
1469 + * @return mixed
1470 + */
1471 + public function get_templates() {
1472 + $url = 'https://library.ghostkit.io/wp-json/ghostkit-library/v1/get_library/';
1473 + $templates = get_transient( 'ghostkit_remote_templates', false );
1372 1474
1373 - if ( $category_terms ) {
1374 - foreach ( $category_terms as $cat ) {
1375 - $categories[] = array(
1376 - 'slug' => $cat->slug,
1377 - 'name' => $cat->name,
1378 - );
1379 - }
1380 - }
1475 + /*
1476 + * Get remote templates.
1477 + */
1478 + if ( ! $templates ) {
1479 + $requested_templates = wp_remote_get(
1480 + add_query_arg(
1481 + array(
1482 + 'ghostkit_version' => GHOSTKIT_VERSION,
1483 + 'ghostkit_pro' => function_exists( 'ghostkit_pro' ),
1484 + 'ghostkit_pro_version' => function_exists( 'ghostkit_pro' ) ? ghostkit_pro()->plugin_version : null,
1485 + ),
1486 + $url
1487 + )
1488 + );
1381 1489
1382 - $image_id = get_post_thumbnail_id( $db_template->ID );
1383 - $image_data = wp_get_attachment_image_src( $image_id, 'large' );
1490 + if ( ! is_wp_error( $requested_templates ) ) {
1491 + $new_templates = wp_remote_retrieve_body( $requested_templates );
1492 + $new_templates = json_decode( $new_templates, true );
1384 1493
1385 - $local_templates[] = array(
1386 - 'id' => $db_template->ID,
1387 - 'title' => $db_template->post_title,
1388 - 'types' => array(
1389 - array(
1390 - 'slug' => 'local',
1391 - ),
1392 - ),
1393 - 'categories' => empty( $categories ) ? false : $categories,
1394 - 'url' => get_post_permalink( $db_template->ID ),
1395 - 'thumbnail' => isset( $image_data[0] ) ? $image_data[0] : false,
1396 - 'thumbnail_width' => isset( $image_data[1] ) ? $image_data[1] : false,
1397 - 'thumbnail_height' => isset( $image_data[2] ) ? $image_data[2] : false,
1398 - );
1399 - }
1494 + if ( $new_templates && isset( $new_templates['response'] ) && is_array( $new_templates['response'] ) ) {
1495 + $templates = $new_templates['response'];
1496 + set_transient( 'ghostkit_remote_templates', $templates, DAY_IN_SECONDS );
1497 + }
1498 + }
1499 + }
1400 1500
1401 - // Restore the global $post as it was before custom WP_Query.
1501 + // Remove Pro templates from array, cause for now there is no way to check if pro addon is activated.
1502 + if ( $templates ) {
1503 + foreach ( $templates as $k => $template ) {
1504 + $is_pro = false;
1505 +
1506 + if ( isset( $template['types'] ) && is_array( $template['types'] ) ) {
1507 + foreach ( $template['types'] as $type ) {
1508 + $is_pro = $is_pro || 'pro' === $type['slug'];
1509 + }
1510 + }
1511 +
1512 + if ( $is_pro ) {
1513 + unset( $templates[ $k ] );
1514 + }
1515 + }
1516 + }
1517 +
1518 + /*
1519 + * Get user templates from db.
1520 + */
1521 +
1522 + // Stupid hack.
1523 + // https://core.trac.wordpress.org/ticket/18408.
1524 + global $post;
1525 + $backup_global_post = $post;
1526 + $local_templates = array();
1527 +
1528 + $local_templates_query = new WP_Query(
1529 + array(
1530 + 'post_type' => 'ghostkit_template',
1531 + 'posts_per_page' => -1,
1532 + 'showposts' => -1,
1533 + 'paged' => -1,
1534 + )
1535 + );
1536 +
1537 + while ( $local_templates_query->have_posts() ) {
1538 + $local_templates_query->the_post();
1539 + $db_template = get_post();
1540 +
1541 + $categories = array();
1542 + $category_terms = get_the_terms( $db_template->ID, 'ghostkit_template_category' );
1543 +
1544 + if ( $category_terms ) {
1545 + foreach ( $category_terms as $cat ) {
1546 + $categories[] = array(
1547 + 'slug' => $cat->slug,
1548 + 'name' => $cat->name,
1549 + );
1550 + }
1551 + }
1552 +
1553 + $image_id = get_post_thumbnail_id( $db_template->ID );
1554 + $image_data = wp_get_attachment_image_src( $image_id, 'large' );
1555 +
1556 + $local_templates[] = array(
1557 + 'id' => $db_template->ID,
1558 + 'title' => $db_template->post_title,
1559 + 'types' => array(
1560 + array(
1561 + 'slug' => 'local',
1562 + ),
1563 + ),
1564 + 'categories' => empty( $categories ) ? false : $categories,
1565 + 'url' => get_post_permalink( $db_template->ID ),
1566 + 'thumbnail' => isset( $image_data[0] ) ? $image_data[0] : false,
1567 + 'thumbnail_width' => isset( $image_data[1] ) ? $image_data[1] : false,
1568 + 'thumbnail_height' => isset( $image_data[2] ) ? $image_data[2] : false,
1569 + );
1570 + }
1571 +
1572 + // Restore the global $post as it was before custom WP_Query.
1402 1573 // phpcs:ignore
1403 1574 $post = $backup_global_post;
1404 1575
1405 - /*
1406 - * Get theme templates.
1407 - */
1408 - $theme_templates = array();
1409 - $theme_templates_data = array();
1576 + /*
1577 + * Get theme templates.
1578 + */
1579 + $theme_templates = array();
1580 + $theme_templates_data = array();
1410 1581
1411 - foreach ( glob( get_template_directory() . '/ghostkit/templates/*/content.php' ) as $template ) {
1412 - $template_path = dirname( $template );
1413 - $template_url = get_template_directory_uri() . str_replace( get_template_directory(), '', $template_path );
1414 - $slug = basename( $template_path );
1582 + foreach ( glob( get_template_directory() . '/ghostkit/templates/*/content.php' ) as $template ) {
1583 + $template_path = dirname( $template );
1584 + $template_url = get_template_directory_uri() . str_replace( get_template_directory(), '', $template_path );
1585 + $slug = basename( $template_path );
1415 1586
1416 - $theme_templates_data[ $slug ] = array(
1417 - 'slug' => $slug,
1418 - 'path' => $template_path,
1419 - 'url' => $template_url,
1420 - );
1421 - }
1587 + $theme_templates_data[ $slug ] = array(
1588 + 'slug' => $slug,
1589 + 'path' => $template_path,
1590 + 'url' => $template_url,
1591 + );
1592 + }
1422 1593
1423 - // get child theme templates.
1424 - if ( get_stylesheet_directory() !== get_template_directory() ) {
1425 - foreach ( glob( get_stylesheet_directory() . '/ghostkit/templates/*/content.php' ) as $template ) {
1426 - $template_path = dirname( $template );
1427 - $template_url = get_stylesheet_directory_uri() . str_replace( get_stylesheet_directory(), '', $template_path );
1428 - $slug = basename( $template_path );
1594 + // get child theme templates.
1595 + if ( get_stylesheet_directory() !== get_template_directory() ) {
1596 + foreach ( glob( get_stylesheet_directory() . '/ghostkit/templates/*/content.php' ) as $template ) {
1597 + $template_path = dirname( $template );
1598 + $template_url = get_stylesheet_directory_uri() . str_replace( get_stylesheet_directory(), '', $template_path );
1599 + $slug = basename( $template_path );
1429 1600
1430 - $theme_templates_data[ $slug ] = array(
1431 - 'slug' => $slug,
1432 - 'path' => $template_path,
1433 - 'url' => $template_url,
1434 - );
1435 - }
1436 - }
1601 + $theme_templates_data[ $slug ] = array(
1602 + 'slug' => $slug,
1603 + 'path' => $template_path,
1604 + 'url' => $template_url,
1605 + );
1606 + }
1607 + }
1437 1608
1438 - // natural sort.
1439 - array_multisort( array_keys( $theme_templates_data ), SORT_NATURAL, $theme_templates_data );
1609 + // natural sort.
1610 + array_multisort( array_keys( $theme_templates_data ), SORT_NATURAL, $theme_templates_data );
1440 1611
1441 - foreach ( $theme_templates_data as $template_data ) {
1442 - $file_data = get_file_data(
1443 - $template_data['path'] . '/content.php',
1444 - array(
1445 - 'name' => 'Name',
1446 - 'category' => 'Category',
1447 - 'source' => 'Source',
1448 - )
1449 - );
1612 + foreach ( $theme_templates_data as $template_data ) {
1613 + $file_data = get_file_data(
1614 + $template_data['path'] . '/content.php',
1615 + array(
1616 + 'name' => 'Name',
1617 + 'category' => 'Category',
1618 + 'source' => 'Source',
1619 + )
1620 + );
1450 1621
1451 - $thumbnail = false;
1452 - $thumbnail_width = false;
1453 - $thumbnail_height = false;
1622 + $thumbnail = false;
1623 + $thumbnail_width = false;
1624 + $thumbnail_height = false;
1454 1625
1455 - if ( file_exists( $template_data['path'] . '/thumbnail.png' ) ) {
1456 - $thumbnail = $template_data['url'] . '/thumbnail.png';
1626 + if ( file_exists( $template_data['path'] . '/thumbnail.png' ) ) {
1627 + $thumbnail = $template_data['url'] . '/thumbnail.png';
1457 1628
1458 - list($thumbnail_width, $thumbnail_height) = getimagesize( $thumbnail );
1459 - }
1629 + list($thumbnail_width, $thumbnail_height) = getimagesize( $thumbnail );
1630 + }
1460 1631
1461 - if ( file_exists( $template_data['path'] . '/thumbnail.jpg' ) ) {
1462 - $thumbnail = $template_data['url'] . '/thumbnail.jpg';
1463 - }
1632 + if ( file_exists( $template_data['path'] . '/thumbnail.jpg' ) ) {
1633 + $thumbnail = $template_data['url'] . '/thumbnail.jpg';
1634 + }
1464 1635
1465 - $theme_templates[] = array(
1466 - 'id' => basename( $template_data['path'] ),
1467 - 'title' => $file_data['name'],
1468 - 'types' => array(
1469 - array(
1470 - 'slug' => 'theme',
1471 - ),
1472 - ),
1473 - 'categories' => isset( $file_data['category'] ) && $file_data['category'] ? array(
1474 - array(
1475 - 'slug' => $file_data['category'],
1476 - 'name' => $file_data['category'],
1477 - ),
1478 - ) : false,
1479 - 'url' => false,
1480 - 'thumbnail' => $thumbnail,
1481 - 'thumbnail_width' => $thumbnail_width,
1482 - 'thumbnail_height' => $thumbnail_height,
1483 - );
1484 - }
1636 + $theme_templates[] = array(
1637 + 'id' => basename( $template_data['path'] ),
1638 + 'title' => $file_data['name'],
1639 + 'types' => array(
1640 + array(
1641 + 'slug' => 'theme',
1642 + ),
1643 + ),
1644 + 'categories' => isset( $file_data['category'] ) && $file_data['category'] ? array(
1645 + array(
1646 + 'slug' => $file_data['category'],
1647 + 'name' => $file_data['category'],
1648 + ),
1649 + ) : false,
1650 + 'url' => false,
1651 + 'thumbnail' => $thumbnail,
1652 + 'thumbnail_width' => $thumbnail_width,
1653 + 'thumbnail_height' => $thumbnail_height,
1654 + );
1655 + }
1485 1656
1486 - // merge all available templates.
1487 - $templates = array_merge( $templates, $local_templates, $theme_templates );
1657 + // merge all available templates.
1658 + $templates = array_merge( $templates, $local_templates, $theme_templates );
1488 1659
1489 - if ( is_array( $templates ) ) {
1490 - return $this->success( $templates );
1491 - } else {
1492 - return $this->error( 'no_templates', __( 'Templates not found.', 'ghostkit' ) );
1493 - }
1494 - }
1660 + if ( is_array( $templates ) ) {
1661 + return $this->success( $templates );
1662 + } else {
1663 + return $this->error( 'no_templates', __( 'Templates not found.', 'ghostkit' ) );
1664 + }
1665 + }
1495 1666
1496 - /**
1497 - * Get templates.
1498 - *
1499 - * @param WP_REST_Request $request request object.
1500 - *
1501 - * @return mixed
1502 - */
1503 - public function get_template_data( WP_REST_Request $request ) {
1504 - $url = 'https://library.ghostkit.io/wp-json/ghostkit-library/v1/get_library_item/';
1505 - $id = $request->get_param( 'id' );
1506 - $type = $request->get_param( 'type' );
1507 - $template_data = false;
1667 + /**
1668 + * Get templates.
1669 + *
1670 + * @param WP_REST_Request $request request object.
1671 + *
1672 + * @return mixed
1673 + */
1674 + public function get_template_data( WP_REST_Request $request ) {
1675 + $url = 'https://library.ghostkit.io/wp-json/ghostkit-library/v1/get_library_item/';
1676 + $id = $request->get_param( 'id' );
1677 + $type = $request->get_param( 'type' );
1678 + $template_data = false;
1508 1679
1509 - switch ( $type ) {
1510 - case 'remote':
1511 - $template_data = get_transient( 'ghostkit_template_' . $type . '_' . $id, false );
1680 + // Validation and security checks.
1681 + if ( empty( $id ) || ! preg_match( '/^[a-zA-Z0-9_-]+$/', $id ) ) {
1682 + return $this->error( 'invalid_id', __( 'Invalid template ID.', 'ghostkit' ) );
1683 + }
1512 1684
1513 - if ( ! $template_data ) {
1514 - $requested_template_data = wp_remote_get(
1515 - add_query_arg(
1516 - array(
1517 - 'id' => $id,
1518 - 'ghostkit_version' => '2.25.0',
1519 - 'ghostkit_pro' => function_exists( 'ghostkit_pro' ),
1520 - 'ghostkit_pro_version' => function_exists( 'ghostkit_pro' ) ? ghostkit_pro()->plugin_version : null,
1521 - ),
1522 - $url
1523 - )
1524 - );
1685 + switch ( $type ) {
1686 + case 'remote':
1687 + $template_data = get_transient( 'ghostkit_template_' . $type . '_' . $id, false );
1525 1688
1526 - if ( ! is_wp_error( $requested_template_data ) ) {
1527 - $new_template_data = wp_remote_retrieve_body( $requested_template_data );
1528 - $new_template_data = json_decode( $new_template_data, true );
1689 + if ( ! $template_data ) {
1690 + $requested_template_data = wp_remote_get(
1691 + add_query_arg(
1692 + array(
1693 + 'id' => $id,
1694 + 'ghostkit_version' => GHOSTKIT_VERSION,
1695 + 'ghostkit_pro' => function_exists( 'ghostkit_pro' ),
1696 + 'ghostkit_pro_version' => function_exists( 'ghostkit_pro' ) ? ghostkit_pro()->plugin_version : null,
1697 + ),
1698 + $url
1699 + )
1700 + );
1529 1701
1530 - if ( $new_template_data && isset( $new_template_data['response'] ) && is_array( $new_template_data['response'] ) ) {
1531 - $template_data = $new_template_data['response'];
1532 - set_transient( 'ghostkit_template_' . $type . '_' . $id, $template_data, DAY_IN_SECONDS );
1533 - }
1534 - }
1535 - }
1536 - break;
1537 - case 'local':
1538 - $post = get_post( $id );
1702 + if ( ! is_wp_error( $requested_template_data ) ) {
1703 + $new_template_data = wp_remote_retrieve_body( $requested_template_data );
1704 + $new_template_data = json_decode( $new_template_data, true );
1539 1705
1540 - if ( $post && 'ghostkit_template' === $post->post_type ) {
1541 - $template_data = array(
1542 - 'id' => $post->ID,
1543 - 'title' => $post->post_title,
1544 - 'content' => $post->post_content,
1545 - );
1546 - }
1706 + if ( $new_template_data && isset( $new_template_data['response'] ) && is_array( $new_template_data['response'] ) ) {
1707 + $template_data = $new_template_data['response'];
1708 + set_transient( 'ghostkit_template_' . $type . '_' . $id, $template_data, DAY_IN_SECONDS );
1709 + }
1710 + }
1711 + }
1712 + break;
1713 + case 'local':
1714 + $post = get_post( $id );
1547 1715
1548 - break;
1549 - case 'theme':
1550 - $template_content_file = get_stylesheet_directory() . '/ghostkit/templates/' . $id . '/content.php';
1716 + if ( $post && 'ghostkit_template' === $post->post_type ) {
1717 + $template_data = array(
1718 + 'id' => $post->ID,
1719 + 'title' => $post->post_title,
1720 + 'content' => $post->post_content,
1721 + );
1722 + }
1551 1723
1552 - if ( ! file_exists( $template_content_file ) ) {
1553 - $template_content_file = get_template_directory() . '/ghostkit/templates/' . $id . '/content.php';
1554 - }
1724 + break;
1725 + case 'theme':
1726 + $template_content_file = get_stylesheet_directory() . '/ghostkit/templates/' . $id . '/content.php';
1555 1727
1556 - if ( file_exists( $template_content_file ) ) {
1557 - ob_start();
1558 - include $template_content_file;
1559 - $template_content = ob_get_clean();
1728 + if ( ! file_exists( $template_content_file ) ) {
1729 + $template_content_file = get_template_directory() . '/ghostkit/templates/' . $id . '/content.php';
1730 + }
1560 1731
1561 - if ( $template_content ) {
1562 - $template_data = get_file_data(
1563 - $template_content_file,
1564 - array(
1565 - 'name' => 'Name',
1566 - )
1567 - );
1732 + if ( file_exists( $template_content_file ) ) {
1733 + ob_start();
1734 + include $template_content_file;
1735 + $template_content = ob_get_clean();
1568 1736
1569 - $template_data = array(
1570 - 'id' => $id,
1571 - 'title' => $template_data['name'],
1572 - 'content' => $template_content,
1573 - );
1574 - }
1575 - }
1576 - break;
1577 - }
1737 + if ( $template_content ) {
1738 + $template_data = get_file_data(
1739 + $template_content_file,
1740 + array(
1741 + 'name' => 'Name',
1742 + )
1743 + );
1578 1744
1579 - if ( is_array( $template_data ) ) {
1580 - return $this->success( $template_data );
1581 - } else {
1582 - return $this->error( 'no_template_data', __( 'Template data not found.', 'ghostkit' ) );
1583 - }
1584 - }
1745 + $template_data = array(
1746 + 'id' => $id,
1747 + 'title' => $template_data['name'],
1748 + 'content' => $template_content,
1749 + );
1750 + }
1751 + }
1752 + break;
1753 + }
1585 1754
1586 - /**
1587 - * Get custom code.
1588 - *
1589 - * @return mixed
1590 - */
1591 - public function get_custom_code() {
1592 - $custom_code = get_option( 'ghostkit_custom_code', array() );
1755 + if ( is_array( $template_data ) ) {
1756 + return $this->success( $template_data );
1757 + } else {
1758 + return $this->error( 'no_template_data', __( 'Template data not found.', 'ghostkit' ) );
1759 + }
1760 + }
1593 1761
1594 - if ( is_array( $custom_code ) ) {
1595 - return $this->success( $custom_code );
1596 - } else {
1597 - return $this->error( 'no_custom_code', __( 'Custom code not found.', 'ghostkit' ) );
1598 - }
1599 - }
1762 + /**
1763 + * Get custom code.
1764 + *
1765 + * @return mixed
1766 + */
1767 + public function get_custom_code() {
1768 + $custom_code = get_option( 'ghostkit_custom_code', array() );
1600 1769
1601 - /**
1602 - * Update custom code.
1603 - *
1604 - * @param WP_REST_Request $request request object.
1605 - *
1606 - * @return mixed
1607 - */
1608 - public function update_custom_code( WP_REST_Request $request ) {
1609 - $new_code = $request->get_param( 'data' );
1770 + if ( is_array( $custom_code ) ) {
1771 + return $this->success( $custom_code );
1772 + } else {
1773 + return $this->error( 'no_custom_code', __( 'Custom code not found.', 'ghostkit' ) );
1774 + }
1775 + }
1610 1776
1611 - if ( is_array( $new_code ) ) {
1612 - $current_code = get_option( 'ghostkit_custom_code', array() );
1613 - update_option( 'ghostkit_custom_code', array_merge( $current_code, $new_code ) );
1614 - }
1777 + /**
1778 + * Update custom code.
1779 + *
1780 + * @param WP_REST_Request $request request object.
1781 + *
1782 + * @return mixed
1783 + */
1784 + public function update_custom_code( WP_REST_Request $request ) {
1785 + $new_code = $request->get_param( 'data' );
1615 1786
1616 - return $this->success( true );
1617 - }
1787 + if ( is_array( $new_code ) ) {
1788 + $current_code = get_option( 'ghostkit_custom_code', array() );
1789 + update_option( 'ghostkit_custom_code', array_merge( $current_code, $new_code ) );
1790 + }
1618 1791
1619 - /**
1620 - * Update color palette.
1621 - *
1622 - * @param WP_REST_Request $request request object.
1623 - *
1624 - * @return mixed
1625 - */
1626 - public function update_color_palette( WP_REST_Request $request ) {
1627 - $colors = $request->get_param( 'data' );
1792 + return $this->success( true );
1793 + }
1628 1794
1629 - if ( is_array( $colors ) ) {
1630 - update_option( 'ghostkit_color_palette', $colors );
1631 - }
1795 + /**
1796 + * Update color palette.
1797 + *
1798 + * @param WP_REST_Request $request request object.
1799 + *
1800 + * @return mixed
1801 + */
1802 + public function update_color_palette( WP_REST_Request $request ) {
1803 + $colors = $request->get_param( 'data' );
1632 1804
1633 - return $this->success( true );
1634 - }
1805 + if ( is_array( $colors ) ) {
1806 + update_option( 'ghostkit_color_palette', $colors );
1807 + }
1635 1808
1636 - /**
1637 - * Get custom typography.
1638 - *
1639 - * @return mixed
1640 - */
1641 - public function get_custom_typography() {
1642 - $typography = get_option( 'ghostkit_typography', array() );
1809 + return $this->success( true );
1810 + }
1643 1811
1644 - if ( is_array( $typography ) ) {
1645 - return $this->success( $typography );
1646 - } else {
1647 - return $this->error( 'no_typography', __( 'Typography not found.', 'ghostkit' ) );
1648 - }
1649 - }
1812 + /**
1813 + * Get custom typography.
1814 + *
1815 + * @return mixed
1816 + */
1817 + public function get_custom_typography() {
1818 + $typography = get_option( 'ghostkit_typography', array() );
1650 1819
1651 - /**
1652 - * Update typography.
1653 - *
1654 - * @param WP_REST_Request $request request object.
1655 - *
1656 - * @return mixed
1657 - */
1658 - public function update_custom_typography( WP_REST_Request $request ) {
1659 - $new_typography = $request->get_param( 'data' );
1660 - $updated_option = array();
1820 + if ( is_array( $typography ) ) {
1821 + return $this->success( $typography );
1822 + } else {
1823 + return $this->error( 'no_typography', __( 'Typography not found.', 'ghostkit' ) );
1824 + }
1825 + }
1661 1826
1662 - if ( is_array( $new_typography ) ) {
1663 - $current_typography = get_option( 'ghostkit_typography', array() );
1827 + /**
1828 + * Update typography.
1829 + *
1830 + * @param WP_REST_Request $request request object.
1831 + *
1832 + * @return mixed
1833 + */
1834 + public function update_custom_typography( WP_REST_Request $request ) {
1835 + $new_typography = $request->get_param( 'data' );
1836 + $updated_option = array();
1664 1837
1665 - if ( empty( $current_typography ) ) {
1666 - $updated_option = $new_typography;
1667 - } else {
1668 - $current_typography['ghostkit_typography'] = json_decode( $current_typography['ghostkit_typography'], true );
1669 - $new_typography['ghostkit_typography'] = json_decode( $new_typography['ghostkit_typography'], true );
1838 + if ( is_array( $new_typography ) ) {
1839 + $current_typography = get_option( 'ghostkit_typography', array() );
1670 1840
1671 - $updated_option = array_merge( $current_typography, $new_typography );
1841 + if ( empty( $current_typography ) ) {
1842 + $updated_option = $new_typography;
1843 + } else {
1844 + $current_typography['ghostkit_typography'] = json_decode( $current_typography['ghostkit_typography'], true );
1845 + $new_typography['ghostkit_typography'] = json_decode( $new_typography['ghostkit_typography'], true );
1672 1846
1673 - $updated_option['ghostkit_typography'] = wp_json_encode( $updated_option['ghostkit_typography'] );
1674 - }
1847 + $updated_option = array_merge( $current_typography, $new_typography );
1675 1848
1676 - update_option( 'ghostkit_typography', $updated_option );
1677 - }
1849 + $updated_option['ghostkit_typography'] = wp_json_encode( $updated_option['ghostkit_typography'] );
1850 + }
1678 1851
1679 - return $this->success( true );
1680 - }
1852 + update_option( 'ghostkit_typography', $updated_option );
1853 + }
1681 1854
1682 - /**
1683 - * Update Google Maps API key.
1684 - *
1685 - * @param WP_REST_Request $request request object.
1686 - *
1687 - * @return mixed
1688 - */
1689 - public function update_google_maps_api_key( WP_REST_Request $request ) {
1690 - update_option( 'ghostkit_google_maps_api_key', $request->get_param( 'key' ) );
1855 + return $this->success( true );
1856 + }
1691 1857
1692 - return $this->success( true );
1693 - }
1858 + /**
1859 + * Update Google Maps API key.
1860 + *
1861 + * @param WP_REST_Request $request request object.
1862 + *
1863 + * @return mixed
1864 + */
1865 + public function update_google_maps_api_key( WP_REST_Request $request ) {
1866 + update_option( 'ghostkit_google_maps_api_key', $request->get_param( 'key' ) );
1694 1867
1695 - /**
1696 - * Update Google reCaptcha API keys.
1697 - *
1698 - * @param WP_REST_Request $request request object.
1699 - *
1700 - * @return mixed
1701 - */
1702 - public function update_google_recaptcha_keys( WP_REST_Request $request ) {
1703 - update_option( 'ghostkit_google_recaptcha_api_site_key', $request->get_param( 'site_key' ) );
1704 - update_option( 'ghostkit_google_recaptcha_api_secret_key', $request->get_param( 'secret_key' ) );
1868 + return $this->success( true );
1869 + }
1705 1870
1706 - return $this->success( true );
1707 - }
1871 + /**
1872 + * Update Google reCaptcha API keys.
1873 + *
1874 + * @param WP_REST_Request $request request object.
1875 + *
1876 + * @return mixed
1877 + */
1878 + public function update_google_recaptcha_keys( WP_REST_Request $request ) {
1879 + update_option( 'ghostkit_google_recaptcha_api_site_key', $request->get_param( 'site_key' ) );
1880 + update_option( 'ghostkit_google_recaptcha_api_secret_key', $request->get_param( 'secret_key' ) );
1708 1881
1709 - /**
1710 - * Update Disabled Blocks.
1711 - *
1712 - * @param WP_REST_Request $request request object.
1713 - *
1714 - * @return mixed
1715 - */
1716 - public function update_disabled_blocks( WP_REST_Request $request ) {
1717 - $new_disabled_blocks = $request->get_param( 'blocks' );
1882 + return $this->success( true );
1883 + }
1718 1884
1719 - if ( is_array( $new_disabled_blocks ) ) {
1720 - $disabled_blocks = array_merge( get_option( 'ghostkit_disabled_blocks', array() ), $new_disabled_blocks );
1721 - $result = array();
1885 + /**
1886 + * Update Disabled Blocks.
1887 + *
1888 + * @param WP_REST_Request $request request object.
1889 + *
1890 + * @return mixed
1891 + */
1892 + public function update_disabled_blocks( WP_REST_Request $request ) {
1893 + $new_disabled_blocks = $request->get_param( 'blocks' );
1722 1894
1723 - foreach ( $disabled_blocks as $k => $block_disabled ) {
1724 - if ( $block_disabled ) {
1725 - $result[ $k ] = true;
1726 - }
1727 - }
1895 + if ( is_array( $new_disabled_blocks ) ) {
1896 + $disabled_blocks = array_merge( get_option( 'ghostkit_disabled_blocks', array() ), $new_disabled_blocks );
1897 + $result = array();
1728 1898
1729 - update_option( 'ghostkit_disabled_blocks', $result );
1730 - }
1899 + foreach ( $disabled_blocks as $k => $block_disabled ) {
1900 + if ( $block_disabled ) {
1901 + $result[ $k ] = true;
1902 + }
1903 + }
1731 1904
1732 - return $this->success( true );
1733 - }
1905 + update_option( 'ghostkit_disabled_blocks', $result );
1906 + }
1734 1907
1735 - /**
1736 - * Update Settings.
1737 - *
1738 - * @param WP_REST_Request $request request object.
1739 - *
1740 - * @return mixed
1741 - */
1742 - public function update_settings( WP_REST_Request $request ) {
1743 - $new_settings = $request->get_param( 'settings' );
1908 + return $this->success( true );
1909 + }
1744 1910
1745 - if ( is_array( $new_settings ) ) {
1746 - $current_settings = get_option( 'ghostkit_settings', array() );
1747 - update_option( 'ghostkit_settings', array_merge( $current_settings, $new_settings ) );
1748 - }
1911 + /**
1912 + * Update Settings.
1913 + *
1914 + * @param WP_REST_Request $request request object.
1915 + *
1916 + * @return mixed
1917 + */
1918 + public function update_settings( WP_REST_Request $request ) {
1919 + $new_settings = $request->get_param( 'settings' );
1749 1920
1750 - return $this->success( true );
1751 - }
1921 + if ( is_array( $new_settings ) ) {
1922 + $current_settings = get_option( 'ghostkit_settings', array() );
1923 + update_option( 'ghostkit_settings', array_merge( $current_settings, $new_settings ) );
1924 + }
1752 1925
1753 - /**
1754 - * Success rest.
1755 - *
1756 - * @param mixed $response response data.
1757 - * @return mixed
1758 - */
1759 - public function success( $response ) {
1760 - return new WP_REST_Response(
1761 - array(
1762 - 'success' => true,
1763 - 'response' => $response,
1764 - ),
1765 - 200
1766 - );
1767 - }
1926 + return $this->success( true );
1927 + }
1768 1928
1769 - /**
1770 - * Error rest.
1771 - *
1772 - * @param mixed $code error code.
1773 - * @param mixed $response response data.
1774 - * @param boolean $true_error use true error response to stop the code processing.
1775 - * @return mixed
1776 - */
1777 - public function error( $code, $response, $true_error = false ) {
1778 - if ( $true_error ) {
1779 - return new WP_Error( $code, $response, array( 'status' => 401 ) );
1780 - }
1929 + /**
1930 + * Success rest.
1931 + *
1932 + * @param mixed $response response data.
1933 + * @return mixed
1934 + */
1935 + public function success( $response ) {
1936 + return new WP_REST_Response(
1937 + array(
1938 + 'success' => true,
1939 + 'response' => $response,
1940 + ),
1941 + 200
1942 + );
1943 + }
1781 1944
1782 - return new WP_REST_Response(
1783 - array(
1784 - 'error' => true,
1785 - 'success' => false,
1786 - 'error_code' => $code,
1787 - 'response' => $response,
1788 - ),
1789 - 401
1790 - );
1791 - }
1945 + /**
1946 + * Error rest.
1947 + *
1948 + * @param mixed $code error code.
1949 + * @param mixed $response response data.
1950 + * @param boolean $true_error use true error response to stop the code processing.
1951 + * @return mixed
1952 + */
1953 + public function error( $code, $response, $true_error = false ) {
1954 + if ( $true_error ) {
1955 + return new WP_Error( $code, $response, array( 'status' => 401 ) );
1956 + }
1957 +
1958 + return new WP_REST_Response(
1959 + array(
1960 + 'error' => true,
1961 + 'success' => false,
1962 + 'error_code' => $code,
1963 + 'response' => $response,
1964 + ),
1965 + 401
1966 + );
1967 + }
1792 1968 }
1793 1969 new GhostKit_Rest();