PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.14
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.14
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 / class-cartflows-api.php

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

382 lines 9.9 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 if ( ! class_exists( 'CartFlows_API' ) ) :
10
11 /**
12 * CartFlows API
13 *
14 * @since 1.0.0
15 */
16 class CartFlows_API {
17
18 /**
19 * Instance
20 *
21 * @access private
22 * @var object Class object.
23 * @since 1.0.0
24 */
25 private static $instance;
26
27 /**
28 * Initiator
29 *
30 * @since 1.0.0
31 * @return object initialized object of class.
32 */
33 public static function get_instance() {
34 if ( ! isset( self::$instance ) ) {
35 self::$instance = new self;
36 }
37 return self::$instance;
38 }
39
40 /**
41 * Constructor
42 *
43 * @since 1.0.0
44 */
45 public function __construct() {
46 }
47
48 /**
49 * Get site URL.
50 *
51 * @since 1.0.0
52 *
53 * @return string Site URL.
54 */
55 public static function get_site_url() {
56 return apply_filters( 'cartflows_templates_url', CARTFLOWS_TEMPLATES_URL );
57 }
58
59 /**
60 * Get Client Site Templates Rest API URL.
61 *
62 * @since 1.0.0
63 *
64 * @return string API site URL.
65 */
66 public static function get_step_endpoint_url() {
67 return self::get_site_url() . 'wp-json/wp/v2/' . CARTFLOWS_STEP_POST_TYPE . '/';
68 }
69
70 /**
71 * Get Client Site Category Rest API URL.
72 *
73 * @since 1.0.0
74 *
75 * @return string API site URL.
76 */
77 public static function get_category_endpoint_url() {
78 return self::get_site_url() . 'wp-json/wp/v2/' . CARTFLOWS_TAXONOMY_STEP_PAGE_BUILDER . '/';
79 }
80
81 /**
82 * Get API request URL.
83 *
84 * @since 1.0.0
85 *
86 * @param string $api_base base of api request.
87 * @return string API site URL.
88 */
89 public static function get_request_api_url( $api_base = '' ) {
90 return self::get_site_url() . 'wp-json/' . CARTFLOWS_STEP_POST_TYPE . '/v1/' . $api_base;
91 }
92
93 /**
94 * License Args.
95 *
96 * @return array License arguments.
97 */
98 public static function get_licence_args() {
99 return apply_filters( 'cartflows_licence_args', array() );
100 }
101
102 /**
103 * Get single demo.
104 *
105 * @since 1.0.0
106 *
107 * @param string $site_id Template ID of the site.
108 * @return array Template data.
109 */
110 public static function get_template( $site_id ) {
111 // @codingStandardsIgnoreStart
112 $request_params = array(
113 'licence_args' => self::get_licence_args(),
114 '_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',
115 );
116 // @codingStandardsIgnoreEnd
117
118 $url = add_query_arg( $request_params, self::get_step_endpoint_url() . $site_id );
119
120 $api_args = array(
121 'timeout' => 15,
122 );
123
124 $response = self::remote_get( $url, $api_args );
125
126 if ( $response['success'] ) {
127 $template = $response['data'];
128 return array(
129 'title' => ( isset( $template['title']->rendered ) ) ? $template['title']->rendered : '',
130 'post_meta' => ( isset( $template['post_meta'] ) ) ? $template['post_meta'] : '',
131 'data' => $template,
132 'divi_content' => isset( $response['data']['divi_content'] ) ? $response['data']['divi_content'] : '',
133 'message' => $response['message'], // __( 'Your API Key is not valid. Please add valid API Key.', 'cartflows' ),
134 'success' => $response['success'],
135 );
136 }
137
138 return array(
139 'title' => '',
140 'post_meta' => array(),
141 'message' => $response['message'],
142 'data' => $response['data'],
143 'divi_content' => '',
144 'success' => $response['success'],
145 );
146 }
147
148 /**
149 * Get Cloud Templates
150 *
151 * @since 1.0.0
152 *
153 * @param array $args For selecting the demos (Search terms, pagination etc).
154 * @return array CartFlows list.
155 */
156 public static function get_templates( $args = array() ) {
157
158 $request_params = wp_parse_args(
159 $args,
160 array(
161 'page' => '1',
162 'per_page' => '15',
163 )
164 );
165
166 $url = add_query_arg( $request_params, self::get_step_endpoint_url() );
167
168 $api_args = array(
169 'timeout' => 15,
170 );
171
172 $response = self::remote_get( $url, $api_args );
173
174 if ( $response['success'] ) {
175 $templates_data = $response['data'];
176 $templates = array();
177 foreach ( $templates_data as $key => $template ) {
178
179 if ( ! isset( $template->id ) ) {
180 continue;
181 }
182
183 $templates[ $key ]['id'] = isset( $template->id ) ? esc_attr( $template->id ) : '';
184 $templates[ $key ]['slug'] = isset( $template->slug ) ? esc_attr( $template->slug ) : '';
185 $templates[ $key ]['link'] = isset( $template->link ) ? esc_url( $template->link ) : '';
186 $templates[ $key ]['date'] = isset( $template->date ) ? esc_attr( $template->date ) : '';
187 $templates[ $key ]['title'] = isset( $template->title->rendered ) ? esc_attr( $template->title->rendered ) : '';
188 $templates[ $key ]['featured_image_url'] = isset( $template->featured_image_url ) ? esc_url( $template->featured_image_url ) : '';
189 $templates[ $key ]['content'] = isset( $template->content->rendered ) ? $template->content->rendered : '';
190 $templates[ $key ]['divi_content'] = isset( $template->divi_content ) ? $template->divi_content : '';
191 $templates[ $key ]['post_meta'] = isset( $template->post_meta ) ? $template->post_meta : '';
192 }
193
194 return array(
195 'templates' => $templates,
196 'templates_count' => $response['count'],
197 'data' => $response,
198 );
199 }
200
201 return array(
202 'templates' => array(),
203 'templates_count' => 0,
204 'data' => $response,
205 );
206
207 }
208
209 /**
210 * Get categories.
211 *
212 * @since 1.0.0
213 * @param array $args Arguments.
214 * @return array Category data.
215 */
216 public static function get_categories( $args = array() ) {
217
218 $request_params = apply_filters(
219 'cartflows_categories_api_params',
220 wp_parse_args(
221 $args,
222 array(
223 'page' => '1',
224 'per_page' => '15',
225 )
226 )
227 );
228
229 $url = add_query_arg( $request_params, self::get_category_endpoint_url() );
230
231 $api_args = apply_filters(
232 'cartflows_api_args',
233 array(
234 'timeout' => 15,
235 )
236 );
237
238 $response = self::remote_get( $url, $api_args );
239
240 if ( $response['success'] ) {
241 $categories_data = $response['data'];
242 $categories = array();
243
244 foreach ( $categories_data as $key => $category ) {
245 if ( isset( $category->count ) && ! empty( $category->count ) ) {
246 $categories[] = array(
247 'id' => isset( $category->id ) ? absint( $category->id ) : 0,
248 'count' => isset( $category->count ) ? absint( $category->count ) : 0,
249 'description' => isset( $category->description ) ? $category->description : '',
250 'link' => isset( $category->link ) ? esc_url( $category->link ) : '',
251 'name' => isset( $category->name ) ? $category->name : '',
252 'slug' => isset( $category->slug ) ? sanitize_text_field( $category->slug ) : '',
253 'taxonomy' => isset( $category->taxonomy ) ? $category->taxonomy : '',
254 'parent' => isset( $category->parent ) ? $category->parent : '',
255 );
256 }
257 }
258
259 return array(
260 'categories' => $categories,
261 'categories_count' => $response['count'],
262 'data' => $response,
263 );
264 }
265
266 return array(
267 'categories' => array(),
268 'categories_count' => 0,
269 'data' => $response,
270 );
271 }
272
273 /**
274 * Remote GET API Request
275 *
276 * @since 1.0.0
277 *
278 * @param string $url Target server API URL.
279 * @param array $args Array of arguments for the API request.
280 * @return mixed Return the API request result.
281 */
282 public static function remote_get( $url = '', $args = array() ) {
283 $request = wp_remote_get( $url, $args );
284 return self::request( $request );
285 }
286
287 /**
288 * Remote POST API Request
289 *
290 * @since 1.0.0
291 *
292 * @param string $url Target server API URL.
293 * @param array $args Array of arguments for the API request.
294 * @return mixed Return the API request result.
295 */
296 public static function remote_post( $url = '', $args = array() ) {
297 $request = wp_remote_post( $url, $args );
298
299 return self::request( $request );
300 }
301
302 /**
303 * Site API Request
304 *
305 * @since 1.0.0
306 *
307 * @param boolean $api_base Target server API URL.
308 * @param array $args Array of arguments for the API request.
309 * @return mixed Return the API request result.
310 */
311 public static function site_request( $api_base = '', $args = array() ) {
312
313 $api_url = self::get_request_api_url( $api_base );
314
315 return self::remote_post( $api_url, $args );
316 }
317
318 /**
319 * API Request
320 *
321 * Handle the API request and return the result.
322 *
323 * @since 1.0.0
324 *
325 * @param array $request Array of arguments for the API request.
326 * @return mixed Return the API request result.
327 */
328 public static function request( $request ) {
329
330 // Is WP Error?
331 if ( is_wp_error( $request ) ) {
332 return array(
333 'success' => false,
334 'message' => $request->get_error_message(),
335 'data' => $request,
336 'count' => 0,
337 );
338 }
339
340 // Invalid response code.
341 if ( wp_remote_retrieve_response_code( $request ) != 200 ) {
342 return array(
343 'success' => false,
344 'message' => $request['response'],
345 'data' => $request,
346 'count' => 0,
347 );
348 }
349
350 // Get body data.
351 $body = wp_remote_retrieve_body( $request );
352
353 // Is WP Error?
354 if ( is_wp_error( $body ) ) {
355 return array(
356 'success' => false,
357 'message' => $body->get_error_message(),
358 'data' => $request,
359 'count' => 0,
360 );
361 }
362
363 // Decode body content.
364 $body_decoded = json_decode( $body );
365
366 return array(
367 'success' => true,
368 'message' => __( 'Request successfully processed!', 'cartflows' ),
369 'data' => (array) $body_decoded,
370 'count' => wp_remote_retrieve_header( $request, 'x-wp-total' ),
371 );
372 }
373
374 }
375
376 /**
377 * Initialize class object with 'get_instance()' method
378 */
379 CartFlows_API::get_instance();
380
381 endif;
382