PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.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.1.0, at classes/importer/class-cartflows-api.php

440 lines 12.5 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 = array(
125 'licence_args' => self::get_licence_args(),
126 '_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',
127 );
128
129 $url = add_query_arg( $request_params, self::get_flow_endpoint_url() . $site_id );
130 wcf()->logger->import_log( PHP_EOL . 'Template Import URL: ' . $url . PHP_EOL );
131
132 $api_args = array(
133 'timeout' => 15, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
134 );
135
136 $response = self::remote_get( $url, $api_args );
137
138 if ( $response['success'] ) {
139 $template = $response['data'];
140 return array(
141 'title' => ( isset( $template['title']->rendered ) ) ? $template['title']->rendered : '',
142 'post_meta' => ( isset( $template['post_meta'] ) ) ? $template['post_meta'] : '',
143 'flow_gcp_meta' => ( isset( $template['flow_gcp_meta'] ) ) ? $template['flow_gcp_meta'] : '',
144 'data' => $template,
145 'original_content' => isset( $response['data']['divi_content'] ) ? $response['data']['divi_content'] : '',
146 'divi_content' => isset( $response['data']['divi_content'] ) ? $response['data']['divi_content'] : '',
147 'message' => $response['message'], // Your API Key is not valid. Please add valid API Key.
148 'success' => $response['success'],
149 );
150 }
151
152 return array(
153 'title' => '',
154 'post_meta' => array(),
155 'message' => $response['message'],
156 'data' => $response['data'],
157 'divi_content' => '',
158 'success' => $response['success'],
159 );
160 }
161
162 /**
163 * Get single demo.
164 *
165 * @since 1.0.0
166 *
167 * @param string $site_id Template ID of the site.
168 * @return array Template data.
169 */
170 public static function get_template( $site_id ) {
171 $request_params = array(
172 'licence_args' => self::get_licence_args(),
173 '_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',
174 );
175
176 $url = add_query_arg( $request_params, self::get_step_endpoint_url() . $site_id );
177 wcf()->logger->import_log( $url );
178
179 $api_args = array(
180 'timeout' => 15, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
181 );
182
183 $response = self::remote_get( $url, $api_args );
184
185 if ( $response['success'] ) {
186 $template = $response['data'];
187 return array(
188 'title' => ( isset( $template['title']->rendered ) ) ? $template['title']->rendered : '',
189 'post_meta' => ( isset( $template['post_meta'] ) ) ? $template['post_meta'] : '',
190 'data' => $template,
191 'original_content' => isset( $response['data']['divi_content'] ) ? $response['data']['divi_content'] : '',
192 'divi_content' => isset( $response['data']['divi_content'] ) ? $response['data']['divi_content'] : '',
193 'message' => $response['message'], // Your API Key is not valid. Please add valid API Key.
194 'success' => $response['success'],
195 );
196 }
197
198 return array(
199 'title' => '',
200 'post_meta' => array(),
201 'message' => $response['message'],
202 'data' => $response['data'],
203 'divi_content' => '',
204 'success' => $response['success'],
205 );
206 }
207
208 /**
209 * Get Cloud Templates
210 *
211 * @since 1.0.0
212 *
213 * @param array $args For selecting the demos (Search terms, pagination etc).
214 * @return array CartFlows list.
215 */
216 public static function get_templates( $args = array() ) {
217
218 $request_params = wp_parse_args(
219 $args,
220 array(
221 'page' => '1',
222 'per_page' => '100',
223 )
224 );
225
226 $url = add_query_arg( $request_params, self::get_step_endpoint_url() );
227
228 $api_args = array(
229 'timeout' => 15, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
230 );
231
232 $response = self::remote_get( $url, $api_args );
233
234 if ( $response['success'] ) {
235 $templates_data = $response['data'];
236 $templates = array();
237 foreach ( $templates_data as $key => $template ) {
238
239 if ( ! isset( $template->id ) ) {
240 continue;
241 }
242
243 $templates[ $key ]['id'] = isset( $template->id ) ? esc_attr( $template->id ) : '';
244 $templates[ $key ]['slug'] = isset( $template->slug ) ? esc_attr( $template->slug ) : '';
245 $templates[ $key ]['link'] = isset( $template->link ) ? esc_url( $template->link ) : '';
246 $templates[ $key ]['date'] = isset( $template->date ) ? esc_attr( $template->date ) : '';
247 $templates[ $key ]['title'] = isset( $template->title->rendered ) ? esc_attr( $template->title->rendered ) : '';
248 $templates[ $key ]['featured_image_url'] = isset( $template->featured_image_url ) ? esc_url( $template->featured_image_url ) : '';
249 $templates[ $key ]['content'] = isset( $template->content->rendered ) ? $template->content->rendered : '';
250 $templates[ $key ]['divi_content'] = isset( $template->divi_content ) ? $template->divi_content : '';
251 $templates[ $key ]['post_meta'] = isset( $template->post_meta ) ? $template->post_meta : '';
252 }
253
254 return array(
255 'templates' => $templates,
256 'templates_count' => $response['count'],
257 'data' => $response,
258 );
259 }
260
261 return array(
262 'templates' => array(),
263 'templates_count' => 0,
264 'data' => $response,
265 );
266 }
267
268 /**
269 * Get categories.
270 *
271 * @since 1.0.0
272 * @param array $args Arguments.
273 * @return array Category data.
274 */
275 public static function get_categories( $args = array() ) {
276
277 $request_params = apply_filters(
278 'cartflows_categories_api_params',
279 wp_parse_args(
280 $args,
281 array(
282 'page' => '1',
283 'per_page' => '100',
284 )
285 )
286 );
287
288 $url = add_query_arg( $request_params, self::get_category_endpoint_url() );
289
290 $api_args = apply_filters(
291 'cartflows_api_args',
292 array(
293 'timeout' => 15, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
294 )
295 );
296
297 $response = self::remote_get( $url, $api_args );
298
299 if ( $response['success'] ) {
300 $categories_data = $response['data'];
301 $categories = array();
302
303 foreach ( $categories_data as $key => $category ) {
304 if ( isset( $category->count ) && ! empty( $category->count ) ) {
305 $categories[] = array(
306 'id' => isset( $category->id ) ? absint( $category->id ) : 0,
307 'count' => isset( $category->count ) ? absint( $category->count ) : 0,
308 'description' => isset( $category->description ) ? $category->description : '',
309 'link' => isset( $category->link ) ? esc_url( $category->link ) : '',
310 'name' => isset( $category->name ) ? $category->name : '',
311 'slug' => isset( $category->slug ) ? sanitize_text_field( $category->slug ) : '',
312 'taxonomy' => isset( $category->taxonomy ) ? $category->taxonomy : '',
313 'parent' => isset( $category->parent ) ? $category->parent : '',
314 );
315 }
316 }
317
318 return array(
319 'categories' => $categories,
320 'categories_count' => $response['count'],
321 'data' => $response,
322 );
323 }
324
325 return array(
326 'categories' => array(),
327 'categories_count' => 0,
328 'data' => $response,
329 );
330 }
331
332 /**
333 * Remote GET API Request
334 *
335 * @since 1.0.0
336 *
337 * @param string $url Target server API URL.
338 * @param array $args Array of arguments for the API request.
339 * @return mixed Return the API request result.
340 */
341 public static function remote_get( $url = '', $args = array() ) {
342 $request = wp_safe_remote_get( $url, $args );
343 return self::request( $request );
344 }
345
346 /**
347 * Remote POST API Request
348 *
349 * @since 1.0.0
350 *
351 * @param string $url Target server API URL.
352 * @param array $args Array of arguments for the API request.
353 * @return mixed Return the API request result.
354 */
355 public static function remote_post( $url = '', $args = array() ) {
356 $request = wp_remote_post( $url, $args );
357
358 return self::request( $request );
359 }
360
361 /**
362 * Site API Request
363 *
364 * @since 1.0.0
365 *
366 * @param boolean $api_base Target server API URL.
367 * @param array $args Array of arguments for the API request.
368 * @return mixed Return the API request result.
369 */
370 public static function site_request( $api_base = '', $args = array() ) {
371
372 $api_url = self::get_request_api_url( $api_base );
373
374 return self::remote_post( $api_url, $args );
375 }
376
377 /**
378 * API Request
379 *
380 * Handle the API request and return the result.
381 *
382 * @since 1.0.0
383 *
384 * @param array $request Array of arguments for the API request.
385 * @return mixed Return the API request result.
386 */
387 public static function request( $request ) {
388
389 // Is WP Error?
390 if ( is_wp_error( $request ) ) {
391 return array(
392 'success' => false,
393 'message' => $request->get_error_message(),
394 'data' => $request,
395 'count' => 0,
396 );
397 }
398
399 // Invalid response code.
400 if ( wp_remote_retrieve_response_code( $request ) != 200 ) {
401 return array(
402 'success' => false,
403 'message' => $request['response'],
404 'data' => $request,
405 'count' => 0,
406 );
407 }
408
409 // Get body data.
410 $body = wp_remote_retrieve_body( $request );
411
412 // Is WP Error?
413 if ( is_wp_error( $body ) ) {
414 return array(
415 'success' => false,
416 'message' => $body->get_error_message(),
417 'data' => $request,
418 'count' => 0,
419 );
420 }
421
422 // Decode body content.
423 $body_decoded = json_decode( $body );
424
425 return array(
426 'success' => true,
427 'message' => __( 'Request successfully processed!', 'cartflows' ),
428 'data' => (array) $body_decoded,
429 'count' => wp_remote_retrieve_header( $request, 'x-wp-total' ),
430 );
431 }
432 }
433
434 /**
435 * Initialize class object with 'get_instance()' method
436 */
437 CartFlows_API::get_instance();
438
439 endif;
440