PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.2.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.2.0
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / classes / importer / class-cartflows-api.php

class-cartflows-api.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.2.0, at classes/importer/class-cartflows-api.php

450 lines 12.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CartFlows API
4 *
5 * @package CartFlows
6 * @since 1.0.0
7 */
8
9 // Exit if accessed directly.
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13 if ( ! class_exists( 'CartFlows_API' ) ) :
14
15 /**
16 * CartFlows API
17 *
18 * @since 1.0.0
19 */
20 class CartFlows_API {
21
22 /**
23 * Instance
24 *
25 * @access private
26 * @var object Class object.
27 * @since 1.0.0
28 */
29 private static $instance;
30
31 /**
32 * Initiator
33 *
34 * @since 1.0.0
35 * @return object initialized object of class.
36 */
37 public static function get_instance() {
38 if ( ! isset( self::$instance ) ) {
39 self::$instance = new self();
40 }
41 return self::$instance;
42 }
43
44 /**
45 * Constructor
46 *
47 * @since 1.0.0
48 */
49 public function __construct() {
50 }
51
52 /**
53 * Get site URL.
54 *
55 * @since 1.0.0
56 *
57 * @return string Site URL.
58 */
59 public static function get_site_url() {
60 return apply_filters( 'cartflows_templates_url', CARTFLOWS_TEMPLATES_URL . Cartflows_Helper::get_common_setting( 'default_page_builder' ) . '/' );
61 }
62
63 /**
64 * Get Client Site Templates Rest API URL.
65 *
66 * @since 1.0.0
67 *
68 * @return string API site URL.
69 */
70 public static function get_flow_endpoint_url() {
71 return self::get_site_url() . 'wp-json/wp/v2/' . CARTFLOWS_FLOW_POST_TYPE . '/';
72 }
73
74 /**
75 * Get Client Site Templates Rest API URL.
76 *
77 * @since 1.0.0
78 *
79 * @return string API site URL.
80 */
81 public static function get_step_endpoint_url() {
82 return self::get_site_url() . 'wp-json/wp/v2/' . CARTFLOWS_STEP_POST_TYPE . '/';
83 }
84
85 /**
86 * Get Client Site Category Rest API URL.
87 *
88 * @since 1.0.0
89 *
90 * @return string API site URL.
91 */
92 public static function get_category_endpoint_url() {
93 return self::get_site_url() . 'wp-json/wp/v2/' . CARTFLOWS_TAXONOMY_STEP_PAGE_BUILDER . '/';
94 }
95
96 /**
97 * Get API request URL.
98 *
99 * @since 1.0.0
100 *
101 * @param string $api_base base of api request.
102 * @return string API site URL.
103 */
104 public static function get_request_api_url( $api_base = '' ) {
105 return self::get_site_url() . 'wp-json/' . CARTFLOWS_STEP_POST_TYPE . '/v1/' . $api_base;
106 }
107
108 /**
109 * License Args.
110 *
111 * @return array License arguments.
112 */
113 public static function get_licence_args() {
114 return apply_filters( 'cartflows_licence_args', array() );
115 }
116
117 /**
118 * License Args.
119 *
120 * @param int $site_id Site ID.
121 * @return array License arguments.
122 */
123 public static function get_flow( $site_id ) {
124 $request_params = apply_filters(
125 'cartflows_get_flow_params',
126 array(
127 'licence_args' => self::get_licence_args(),
128 '_fields' => 'id,slug,status,type,link,title,featured_media,template,cartflows_flow_page_builder,cartflows_flow_type,cartflows_flow_flow,featured_image_url,licence_status,flow_type,flow_type,page_builder,divi_content,post_meta,content,flow_gcp_meta',
129 'url' => site_url(),
130 'version' => CARTFLOWS_VER,
131 )
132 );
133
134 $url = add_query_arg( $request_params, self::get_flow_endpoint_url() . $site_id );
135 wcf()->logger->import_log( PHP_EOL . 'Template Import URL: ' . $url . PHP_EOL );
136
137 $api_args = array(
138 'timeout' => 15, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
139 );
140
141 $response = self::remote_get( $url, $api_args );
142
143 if ( $response['success'] ) {
144 $template = $response['data'];
145 return array(
146 'title' => ( isset( $template['title']->rendered ) ) ? $template['title']->rendered : '',
147 'post_meta' => ( isset( $template['post_meta'] ) ) ? $template['post_meta'] : '',
148 'flow_gcp_meta' => ( isset( $template['flow_gcp_meta'] ) ) ? $template['flow_gcp_meta'] : '',
149 'data' => $template,
150 'original_content' => isset( $response['data']['divi_content'] ) ? $response['data']['divi_content'] : '',
151 'divi_content' => isset( $response['data']['divi_content'] ) ? $response['data']['divi_content'] : '',
152 'message' => $response['message'], // Your API Key is not valid. Please add valid API Key.
153 'success' => $response['success'],
154 );
155 }
156
157 return array(
158 'title' => '',
159 'post_meta' => array(),
160 'message' => $response['message'],
161 'data' => $response['data'],
162 'divi_content' => '',
163 'success' => $response['success'],
164 );
165 }
166
167 /**
168 * Get single demo.
169 *
170 * @since 1.0.0
171 *
172 * @param string $site_id Template ID of the site.
173 * @return array Template data.
174 */
175 public static function get_template( $site_id ) {
176 $request_params = apply_filters(
177 'cartflows_get_template_params',
178 array(
179 'licence_args' => self::get_licence_args(),
180 '_fields' => 'id,slug,status,type,link,title,featured_media,template,cartflows_step_page_builder,cartflows_step_type,cartflows_step_flow,featured_image_url,licence_status,flow_type,step_type,page_builder,divi_content,post_meta,content',
181 'url' => site_url(),
182 'version' => CARTFLOWS_VER,
183 )
184 );
185
186 $url = add_query_arg( $request_params, self::get_step_endpoint_url() . $site_id );
187 wcf()->logger->import_log( $url );
188
189 $api_args = array(
190 'timeout' => 15, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
191 );
192
193 $response = self::remote_get( $url, $api_args );
194
195 if ( $response['success'] ) {
196 $template = $response['data'];
197 return array(
198 'title' => ( isset( $template['title']->rendered ) ) ? $template['title']->rendered : '',
199 'post_meta' => ( isset( $template['post_meta'] ) ) ? $template['post_meta'] : '',
200 'data' => $template,
201 'original_content' => isset( $response['data']['divi_content'] ) ? $response['data']['divi_content'] : '',
202 'divi_content' => isset( $response['data']['divi_content'] ) ? $response['data']['divi_content'] : '',
203 'message' => $response['message'], // Your API Key is not valid. Please add valid API Key.
204 'success' => $response['success'],
205 );
206 }
207
208 return array(
209 'title' => '',
210 'post_meta' => array(),
211 'message' => $response['message'],
212 'data' => $response['data'],
213 'divi_content' => '',
214 'success' => $response['success'],
215 );
216 }
217
218 /**
219 * Get Cloud Templates
220 *
221 * @since 1.0.0
222 *
223 * @param array $args For selecting the demos (Search terms, pagination etc).
224 * @return array CartFlows list.
225 */
226 public static function get_templates( $args = array() ) {
227
228 $request_params = wp_parse_args(
229 $args,
230 array(
231 'page' => '1',
232 'per_page' => '100',
233 )
234 );
235
236 $url = add_query_arg( $request_params, self::get_step_endpoint_url() );
237
238 $api_args = array(
239 'timeout' => 15, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
240 );
241
242 $response = self::remote_get( $url, $api_args );
243
244 if ( $response['success'] ) {
245 $templates_data = $response['data'];
246 $templates = array();
247 foreach ( $templates_data as $key => $template ) {
248
249 if ( ! isset( $template->id ) ) {
250 continue;
251 }
252
253 $templates[ $key ]['id'] = isset( $template->id ) ? esc_attr( $template->id ) : '';
254 $templates[ $key ]['slug'] = isset( $template->slug ) ? esc_attr( $template->slug ) : '';
255 $templates[ $key ]['link'] = isset( $template->link ) ? esc_url( $template->link ) : '';
256 $templates[ $key ]['date'] = isset( $template->date ) ? esc_attr( $template->date ) : '';
257 $templates[ $key ]['title'] = isset( $template->title->rendered ) ? esc_attr( $template->title->rendered ) : '';
258 $templates[ $key ]['featured_image_url'] = isset( $template->featured_image_url ) ? esc_url( $template->featured_image_url ) : '';
259 $templates[ $key ]['content'] = isset( $template->content->rendered ) ? $template->content->rendered : '';
260 $templates[ $key ]['divi_content'] = isset( $template->divi_content ) ? $template->divi_content : '';
261 $templates[ $key ]['post_meta'] = isset( $template->post_meta ) ? $template->post_meta : '';
262 }
263
264 return array(
265 'templates' => $templates,
266 'templates_count' => $response['count'],
267 'data' => $response,
268 );
269 }
270
271 return array(
272 'templates' => array(),
273 'templates_count' => 0,
274 'data' => $response,
275 );
276 }
277
278 /**
279 * Get categories.
280 *
281 * @since 1.0.0
282 * @param array $args Arguments.
283 * @return array Category data.
284 */
285 public static function get_categories( $args = array() ) {
286
287 $request_params = apply_filters(
288 'cartflows_categories_api_params',
289 wp_parse_args(
290 $args,
291 array(
292 'page' => '1',
293 'per_page' => '100',
294 )
295 )
296 );
297
298 $url = add_query_arg( $request_params, self::get_category_endpoint_url() );
299
300 $api_args = apply_filters(
301 'cartflows_api_args',
302 array(
303 'timeout' => 15, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
304 )
305 );
306
307 $response = self::remote_get( $url, $api_args );
308
309 if ( $response['success'] ) {
310 $categories_data = $response['data'];
311 $categories = array();
312
313 foreach ( $categories_data as $key => $category ) {
314 if ( isset( $category->count ) && ! empty( $category->count ) ) {
315 $categories[] = array(
316 'id' => isset( $category->id ) ? absint( $category->id ) : 0,
317 'count' => isset( $category->count ) ? absint( $category->count ) : 0,
318 'description' => isset( $category->description ) ? $category->description : '',
319 'link' => isset( $category->link ) ? esc_url( $category->link ) : '',
320 'name' => isset( $category->name ) ? $category->name : '',
321 'slug' => isset( $category->slug ) ? sanitize_text_field( $category->slug ) : '',
322 'taxonomy' => isset( $category->taxonomy ) ? $category->taxonomy : '',
323 'parent' => isset( $category->parent ) ? $category->parent : '',
324 );
325 }
326 }
327
328 return array(
329 'categories' => $categories,
330 'categories_count' => $response['count'],
331 'data' => $response,
332 );
333 }
334
335 return array(
336 'categories' => array(),
337 'categories_count' => 0,
338 'data' => $response,
339 );
340 }
341
342 /**
343 * Remote GET API Request
344 *
345 * @since 1.0.0
346 *
347 * @param string $url Target server API URL.
348 * @param array $args Array of arguments for the API request.
349 * @return mixed Return the API request result.
350 */
351 public static function remote_get( $url = '', $args = array() ) {
352 $request = wp_safe_remote_get( $url, $args );
353 return self::request( $request );
354 }
355
356 /**
357 * Remote POST API Request
358 *
359 * @since 1.0.0
360 *
361 * @param string $url Target server API URL.
362 * @param array $args Array of arguments for the API request.
363 * @return mixed Return the API request result.
364 */
365 public static function remote_post( $url = '', $args = array() ) {
366 $request = wp_remote_post( $url, $args );
367
368 return self::request( $request );
369 }
370
371 /**
372 * Site API Request
373 *
374 * @since 1.0.0
375 *
376 * @param boolean $api_base Target server API URL.
377 * @param array $args Array of arguments for the API request.
378 * @return mixed Return the API request result.
379 */
380 public static function site_request( $api_base = '', $args = array() ) {
381
382 $api_url = self::get_request_api_url( $api_base );
383
384 return self::remote_post( $api_url, $args );
385 }
386
387 /**
388 * API Request
389 *
390 * Handle the API request and return the result.
391 *
392 * @since 1.0.0
393 *
394 * @param array $request Array of arguments for the API request.
395 * @return mixed Return the API request result.
396 */
397 public static function request( $request ) {
398
399 // Is WP Error?
400 if ( is_wp_error( $request ) ) {
401 return array(
402 'success' => false,
403 'message' => $request->get_error_message(),
404 'data' => $request,
405 'count' => 0,
406 );
407 }
408
409 // Invalid response code.
410 if ( wp_remote_retrieve_response_code( $request ) != 200 ) {
411 return array(
412 'success' => false,
413 'message' => $request['response'],
414 'data' => $request,
415 'count' => 0,
416 );
417 }
418
419 // Get body data.
420 $body = wp_remote_retrieve_body( $request );
421
422 // Is WP Error?
423 if ( is_wp_error( $body ) ) {
424 return array(
425 'success' => false,
426 'message' => $body->get_error_message(),
427 'data' => $request,
428 'count' => 0,
429 );
430 }
431
432 // Decode body content.
433 $body_decoded = json_decode( $body );
434
435 return array(
436 'success' => true,
437 'message' => __( 'Request successfully processed!', 'cartflows' ),
438 'data' => (array) $body_decoded,
439 'count' => wp_remote_retrieve_header( $request, 'x-wp-total' ),
440 );
441 }
442 }
443
444 /**
445 * Initialize class object with 'get_instance()' method
446 */
447 CartFlows_API::get_instance();
448
449 endif;
450