PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.13.3
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.13.3
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / includes / core / rest-api / Controllers / class-templatelibrary-controller.php

class-templatelibrary-controller.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.3, at includes/core/rest-api/Controllers/class-templatelibrary-controller.php

850 lines 25.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Template library controller
4 *
5 * @package WPFunnels\Rest\Controllers
6 */
7
8 namespace WPFunnels\Rest\Controllers;
9
10 use WP_Error;
11 use WP_REST_Request;
12 use WP_REST_Response;
13 use WP_REST_Server;
14 use WPFunnels\Wpfnl;
15 use WPFunnels\Wpfnl_functions;
16 use WPFunnels\lms\helper\Wpfnl_lms_learndash_functions;
17
18 /**
19 * Class TemplateLibraryController
20 * This class represents the controller for the template library in the WP Funnels plugin
21 * It extends the Wpfnl_REST_Controller class
22 *
23 * @package WPFunnels\Rest\Controllers
24 * @since 1.0.0
25 */
26 class TemplateLibraryController extends Wpfnl_REST_Controller {
27
28 /**
29 * The URL for the WP Funnels API.
30 *
31 * This variable holds the URL for the WP Funnels API, which is used to retrieve templates.
32 *
33 * @var string
34 */
35 public static $funnel_api_url = 'https://templates.getwpfunnels.com/wp-json/wp/v2/wpfunnels/';
36
37 /**
38 * The URL for the WP Funnels API.
39 *
40 * This variable holds the URL for the WP Funnels API, which is used to retrieve templates.
41 *
42 * @var string
43 */
44 public static $funnel_categories_api_url = 'https://templates.getwpfunnels.com/wp-json/wp/v2/template_industries/';
45
46 /**
47 * The URL for the WP Funnels API.
48 *
49 * This variable holds the URL for the WP Funnels API, which is used to retrieve templates.
50 *
51 * @var string
52 */
53 public static $funnel_steps_api_url = 'https://templates.getwpfunnels.com/wp-json/wp/v2/wpfunnel_steps/';
54
55 /**
56 * The URL for the WP Funnels API.
57 *
58 * This variable holds the URL for the WP Funnels API, which is used to retrieve templates.
59 *
60 * @var string
61 */
62 public static $all_funnels_api_url = 'https://templates.getwpfunnels.com/wp-json/wpfunnels/v1/get_all_funnels/';
63
64
65 /**
66 * Endpoint namespace.
67 *
68 * @var string
69 */
70 protected $namespace = 'wpfunnels/v1';
71
72 /**
73 * Route base.
74 *
75 * @var string
76 */
77 protected $rest_base = 'templates/';
78
79
80 /**
81 * Get remote funnel api url
82 *
83 * @return string
84 * @since 2.2.8
85 */
86 private static function get_remote_funnel_api_url() {
87 if ( 'oxygen' === Wpfnl_functions::get_builder_type() ) {
88 return 'https://oxygentemplates.getwpfunnels.com/wp-json/wp/v2/wpfunnels/';
89 } elseif ( 'bricks' === Wpfnl_functions::get_builder_type() ) {
90 return 'https://brickstemplates.getwpfunnels.com/wp-json/wp/v2/wpfunnels/';
91 }
92 return self::$funnel_api_url;
93 }
94
95
96 /**
97 * Get remote funnel builders categories url
98 *
99 * @return string
100 *
101 * @since 2.2.8
102 */
103 private static function get_remote_funnel_categories_api_url() {
104 if ( 'oxygen' === Wpfnl_functions::get_builder_type() ) {
105 return 'https://oxygentemplates.getwpfunnels.com/wp-json/wp/v2/template_industries/';
106 } elseif ( 'bricks' === Wpfnl_functions::get_builder_type() ) {
107 return 'https://brickstemplates.getwpfunnels.com/wp-json/wp/v2/template_industries/';
108 }
109 return self::$funnel_categories_api_url;
110 }
111
112
113 /**
114 * Get remote funnel steps categories url
115 *
116 * @return string
117 *
118 * @since 2.2.8
119 */
120 private static function get_remote_funnel_steps_api_url() {
121 if ( 'oxygen' === Wpfnl_functions::get_builder_type() ) {
122 return 'https://oxygentemplates.getwpfunnels.com/wp-json/wp/v2/wpfunnel_steps/';
123 } elseif ( 'bricks' === Wpfnl_functions::get_builder_type() ) {
124 return 'https://brickstemplates.getwpfunnels.com/wp-json/wp/v2/wpfunnel_steps/';
125 }
126 return self::$funnel_steps_api_url;
127 }
128
129
130 /**
131 * Get all templates API url
132 *
133 * @return string
134 * @since 2.2.8
135 */
136 private static function get_all_templates_api_url() {
137 if ( 'oxygen' === Wpfnl_functions::get_builder_type() ) {
138 return 'https://oxygentemplates.getwpfunnels.com/wp-json/wpfunnels/v1/get_all_funnels/';
139 } elseif ( 'bricks' === Wpfnl_functions::get_builder_type() ) {
140 return 'https://brickstemplates.getwpfunnels.com/wp-json/wpfunnels/v1/get_all_funnels/';
141 }
142 return self::$all_funnels_api_url;
143 }
144
145
146 /**
147 * Check user permission
148 *
149 * @param $request
150 *
151 * @return bool|WP_Error
152 */
153 public function update_items_permissions_check( $request ) {
154 $permission = current_user_can( 'wpf_manage_funnels' );
155 if ( ! Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'templates' ) ) {
156 return new WP_Error( 'wpfunnels_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'wpfnl' ), array( 'status' => rest_authorization_required_code() ) );
157 }
158 return true;
159 }
160
161
162 /**
163 * Makes sure the current user has access to READ the settings APIs.
164 *
165 * @param WP_REST_Request $request Full data about the request.
166 *
167 * @return WP_Error|boolean
168 * @since 3.0.0
169 */
170 public function get_items_permissions_check( $request ) {
171 if ( ! Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'templates' ) ) {
172 return new WP_Error( 'wpfunnels_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'wpfnl' ), array( 'status' => rest_authorization_required_code() ) );
173 }
174 return true;
175 }
176
177
178
179 public function register_routes() {
180 register_rest_route(
181 $this->namespace,
182 '/' . $this->rest_base . 'get_templates',
183 array(
184 array(
185 'methods' => WP_REST_Server::READABLE,
186 'callback' => array( $this, 'get_templates' ),
187 'permission_callback' => array( $this, 'get_items_permissions_check' ),
188 ),
189 )
190 );
191
192 /*
193 * Building a funnel out of a template, and clearing what the library
194 * has cached. The template library screen is React (see
195 * src/admin/pages/FunnelTemplates) and talks REST; the Vue library
196 * still embedded in the funnel canvas and Store Checkout posts the same
197 * work to admin-ajax, so both routes here hand straight over to the
198 * handlers those calls already run rather than restating them.
199 */
200 register_rest_route(
201 $this->namespace,
202 '/' . $this->rest_base . 'import-funnel',
203 array(
204 array(
205 'methods' => WP_REST_Server::CREATABLE,
206 'callback' => array( $this, 'import_funnel' ),
207 'permission_callback' => array( $this, 'edit_items_permissions_check' ),
208 ),
209 )
210 );
211
212 register_rest_route(
213 $this->namespace,
214 '/' . $this->rest_base . 'finish-import',
215 array(
216 array(
217 'methods' => WP_REST_Server::CREATABLE,
218 'callback' => array( $this, 'finish_import' ),
219 'permission_callback' => array( $this, 'edit_items_permissions_check' ),
220 ),
221 )
222 );
223
224 register_rest_route(
225 $this->namespace,
226 '/' . $this->rest_base . 'sync',
227 array(
228 array(
229 'methods' => WP_REST_Server::CREATABLE,
230 'callback' => array( $this, 'sync_library' ),
231 'permission_callback' => array( $this, 'edit_items_permissions_check' ),
232 ),
233 )
234 );
235
236 register_rest_route(
237 $this->namespace,
238 '/' . $this->rest_base . 'get_template_type_id',
239 array(
240 array(
241 'methods' => WP_REST_Server::READABLE,
242 'callback' => array( $this, 'get_template_type_id' ),
243 'permission_callback' => array( $this, 'get_items_permissions_check' ),
244
245 ),
246 )
247 );
248
249 }
250
251
252 /**
253 * Prepare a single setting object for response.
254 *
255 * @param $request
256 *
257 * @return WP_REST_Response
258 */
259 /**
260 * Prepare a single setting object for response.
261 *
262 * @param $request
263 *
264 * @return WP_REST_Response
265 */
266 /**
267 * Makes sure the current user may build a funnel from a template.
268 *
269 * @param WP_REST_Request $request Full data about the request.
270 *
271 * @return WP_Error|boolean
272 * @since 3.13.5
273 */
274 public function edit_items_permissions_check( $request ) {
275 if ( ! Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'templates', 'edit' ) ) {
276 return new WP_Error( 'wpfunnels_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'wpfnl' ), array( 'status' => rest_authorization_required_code() ) );
277 }
278 return true;
279 }
280
281
282 /**
283 * Create a funnel from a library template.
284 *
285 * First of the three calls that import a template: this creates the funnel
286 * post and answers with its id, the steps are then created one by one
287 * against the step controller, and `finish_import()` below closes the
288 * import off.
289 *
290 * @param WP_REST_Request $request Full data about the request.
291 *
292 * @return WP_REST_Response The importer's own response, carrying `funnelID`.
293 * @since 3.13.5
294 */
295 public function import_funnel( $request ) {
296 $payload = $this->get_import_payload( $request );
297
298 return rest_ensure_response( Wpfnl::$instance->template_manager->import_funnel( $payload ) );
299 }
300
301
302 /**
303 * Close off a template import.
304 *
305 * Answers with the `redirectLink` the caller should follow — where the
306 * funnel wants to be opened depends on what was imported, so the server
307 * decides it.
308 *
309 * @param WP_REST_Request $request Full data about the request.
310 *
311 * @return WP_REST_Response The importer's own response.
312 * @since 3.13.5
313 */
314 public function finish_import( $request ) {
315 $payload = $this->get_import_payload( $request );
316
317 return rest_ensure_response( Wpfnl::$instance->template_manager->after_funnel_creation( $payload ) );
318 }
319
320
321 /**
322 * Drop everything the library has cached, so the next listing is fetched
323 * fresh from the template server.
324 *
325 * @param WP_REST_Request $request Full data about the request.
326 *
327 * @return WP_REST_Response
328 * @since 3.13.5
329 */
330 public function sync_library( $request ) {
331 $settings = Wpfnl::$instance->module_manager->get_admin_modules( 'settings' );
332
333 if ( ! $settings ) {
334 return rest_ensure_response( array( 'success' => false ) );
335 }
336
337 return rest_ensure_response( $settings->clear_templates_data( array() ) );
338 }
339
340
341 /**
342 * The request body as the importer expects to read it.
343 *
344 * The importer was written against `wp_ajax_helper`, which handed it a
345 * plain array; REST params are the same shape, so this only unwraps them.
346 *
347 * @param WP_REST_Request $request Full data about the request.
348 *
349 * @return array Payload for the importer.
350 * @since 3.13.5
351 */
352 private function get_import_payload( $request ) {
353 $payload = $request->get_json_params();
354
355 if ( ! is_array( $payload ) ) {
356 $payload = $request->get_params();
357 }
358
359 // `source` names which registered source to import through, and there
360 // is only one the library offers.
361 if ( empty( $payload['source'] ) ) {
362 $payload['source'] = 'remote';
363 }
364
365 return $payload;
366 }
367
368
369 public function get_templates( $request ) {
370 $type_param = isset( $_GET['type'] ) ? sanitize_key( $_GET['type'] ) : 'wc';
371 $store_checkout_param = isset( $_GET['store_checkout'] ) ? sanitize_key( $_GET['store_checkout'] ) : 'no';
372
373 // Map frontend goal types to API template types.
374 // 'store_checkout' (improve-checkout goal) passes through as-is.
375 // 'sales' / 'order-value' goals send 'wc'; keep backward-compat mapping here too.
376 $type_map = array(
377 'sales' => 'wc',
378 'lead' => 'lead',
379 );
380 $funnel_template_type = isset( $type_map[ $type_param ] ) ? $type_map[ $type_param ] : $type_param;
381
382 $step = isset( $_GET['step'] ) ? $_GET['step'] : false;
383 $builder = isset( $_GET['builder'] ) ? $_GET['builder'] : false;
384 if( 'yes' === $store_checkout_param ) {
385 $funnel_template_type = 'store_checkout';
386 }
387
388 $templates = $this->get_funnels_data( $funnel_template_type, $step, array(), false, $builder );
389 $templates = $this->prepare_custom_step($templates);
390 $templates['success'] = true;
391 return $this->prepare_item_for_response( $templates, $request );
392 }
393
394
395 /**
396 * Prepares custom steps.
397 * Allow user to create custom step from landing page.
398 *
399 * @param array $templates The templates to prepare.
400 * @return array
401 *
402 * @since 3.4.15
403 */
404 public function prepare_custom_step($templates){
405 if( is_array( $templates ) ){
406 if( isset( $templates['steps'] ) && is_array( $templates['steps'] ) ){
407 foreach( $templates['steps'] as $key => $step ){
408 if( is_array( $step ) && isset( $step['step_type'] ) && 'landing' === $step['step_type'] ){
409 $custom_step = $step;
410 $custom_step['step_type'] = 'custom';
411 if( isset( $custom_step['title'] ) ){
412 $custom_step['title'] = str_ireplace('Landing', '', $custom_step['title']);
413 }
414 $templates['steps'][] = $custom_step;
415 }
416 }
417 }
418 }
419 return $templates;
420 }
421
422
423
424 /**
425 * Send http request
426 *
427 * @param $url
428 * @param $args
429 *
430 * @return array
431 */
432 public static function remote_get( $url, $args ) {
433 $response = wp_remote_get( $url, $args );
434 // bail if there is an error
435 if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
436 return array(
437 'success' => false,
438 'message' => is_wp_error( $response ) ? $response->get_error_message() : __( 'Unexpected response from the template server.', 'wpfnl' ),
439 'data' => $response,
440 );
441 }
442
443 // Decode the results.
444 $results = json_decode( $response['body'], true );
445
446 // bail if there is no result found
447 if ( ! is_array( $results ) ) {
448 return new \WP_Error( 'unexpected_data_format', 'Data was not returned in the expected format.' );
449 }
450
451 return array(
452 'success' => true,
453 'message' => 'Data successfully retrieved',
454 'data' => json_decode( wp_remote_retrieve_body( $response ), true ),
455 );
456 }
457
458
459 /**
460 * Get all funnels of the specific builder
461 *
462 * @param bool $type
463 * @param bool $isStep
464 * @param array $args
465 * @param bool $force_update
466 * @param bool $builder
467 *
468 * @return bool|mixed|void
469 */
470 public static function get_funnels( $type = false, $isStep = false, $args = array(), $force_update = false, $builder = false ) {
471 $builder_type = $builder ? $builder : Wpfnl_functions::get_builder_type();
472 $cache_key = 'wpfunnels_remote_template_data_' . $type . '_' . $builder_type . '_' . WPFNL_VERSION;
473 $data = get_transient( $cache_key );
474
475 // A cached miss is stored as an empty array, which is falsy, so check
476 // against false explicitly or a failed fetch would be retried on every
477 // single request. An explicit refresh always goes out to the network.
478 if ( false !== $data && ! $force_update ) {
479 return;
480 }
481 if ( $type && ( $force_update || false === $data ) ) {
482 if ( false === $data ) {
483 $data = array();
484 }
485 // Keep the blocking request well inside the web server's FastCGI/proxy
486 // timeout: this runs during an admin REST request, and up to four of
487 // these calls can happen back to back.
488 $timeout = ( $force_update ) ? 30 : 15;
489
490 $template_url = self::$all_funnels_api_url;
491 if ( 'oxygen' === $builder_type ) {
492 $template_url = 'https://oxygentemplates.getwpfunnels.com/wp-json/wpfunnels/v1/get_all_funnels/';
493 } elseif ( 'bricks' === $builder_type ) {
494 $template_url = 'https://brickstemplates.getwpfunnels.com/wp-json/wpfunnels/v1/get_all_funnels/';
495 }
496
497 // get all templates
498 $params = array(
499 'per_page' => 100,
500 'offset' => 0,
501 'builder' => $builder_type,
502 'template_type' => $type,
503 'template_url' => $template_url,
504 );
505 if( !defined('QUBELY_VERSION') ){
506 $params['gutenberg_type'] = 'wpf_native_gutenberg';
507 }
508 $url = add_query_arg( $params, $template_url );
509
510 $template_data = self::remote_get(
511 $url,
512 array(
513 'timeout' => $timeout,
514 )
515 );
516
517 if ( ! is_array( $template_data ) || ! $template_data['success'] ) {
518 $url = add_query_arg( $params, WPFNL_MIDDLEMAN_TEMPLATE_URL );
519 $template_data = self::remote_get(
520 $url,
521 array(
522 'timeout' => $timeout,
523 )
524 );
525
526 if ( ! is_array( $template_data ) || ! $template_data['success'] ) {
527 set_transient( $cache_key, array(), 15 * MINUTE_IN_SECONDS );
528 return false;
529 }
530 }
531
532 // get all steps
533 $steps = array();
534 $templates = array();
535 if ( $template_data['data'] ) {
536 foreach ( $template_data['data'] as $key => $template ) {
537
538 $i = 0;
539 $thankyou_count = 0;
540 if( isset($template['page_builder']) && 'gutenberg' === $template['page_builder'] ){
541 if( !defined('QUBELY_VERSION') && 'wpf_native_gutenberg' !== $template['gutenberg_type'] ){
542 continue;
543 }
544 }
545 if( isset($template['is_store_checkout']) && $template['is_store_checkout'] && 'store_checkout' !== $type ) {
546 continue;
547 }
548
549 foreach ( $template['steps'] as $_step ) {
550 if ( $thankyou_count && 'thankyou' === $_step['step_type'] ) {
551 continue;
552 }
553 if ( 0 == $i ) {
554 $template_data['data'][ $key ]['link'] = $_step['link'];
555 }
556 $_step['funnel_name'] = $template['title'];
557 $_step['template_type'] = isset( $template['templateType'] ) ? ( 'free' === $template['templateType'] ? 'free' : 'pro' ) : 'free';
558 $steps[] = $_step;
559 if ( 'thankyou' === $_step['step_type'] ) {
560 $thankyou_count++;
561 }
562 $i++;
563 }
564 array_push($templates, $template_data['data'][ $key ]);
565 }
566 }
567
568 // fetch the funnel categories from the remote server
569 $cat_url = self::$funnel_categories_api_url;
570 if ( 'oxygen' === $builder_type ) {
571 $cat_url = 'https://oxygentemplates.getwpfunnels.com/wp-json/wp/v2/template_industries/';
572 } elseif ( 'bricks' === $builder_type ) {
573 $cat_url = 'https://brickstemplates.getwpfunnels.com/wp-json/wp/v2/template_industries/';
574 }
575
576 $params = array(
577 'per_page' => 100,
578 'category_url' => $cat_url,
579 );
580
581 $url = add_query_arg( $params, $cat_url );
582 $categories_data = self::remote_get(
583 $url,
584 array(
585 'timeout' => $timeout,
586 )
587 );
588
589 if ( ! is_array( $categories_data ) || ! $categories_data['success'] ) {
590 $url = add_query_arg( $params, WPFNL_MIDDLEMAN_TEMPLATE_CATEGORY_URL );
591 $categories_data = self::remote_get(
592 $url,
593 array(
594 'timeout' => $timeout,
595 )
596 );
597
598 if ( ! is_array( $categories_data ) || ! $categories_data['success'] ) {
599 set_transient( $cache_key, array(), 15 * MINUTE_IN_SECONDS );
600 return false;
601 }
602 }
603
604 $data['templates'] = $templates;
605 $data['steps'] = $steps;
606 $data['categories'] = $categories_data['data'];
607 update_option( WPFNL_TEMPLATES_OPTION_KEY . '_' . $type . '_' . $builder_type, $data, 'no' );
608 set_transient( $cache_key, $data, 24 * HOUR_IN_SECONDS );
609 return false;
610 }
611 }
612
613
614
615 /**
616 * Get funnel templates data
617 *
618 * @param array $args
619 * @param bool $force_update
620 *
621 * @return array|mixed|void
622 * @since 1.0.0
623 */
624 public function get_funnels_data( $type = false, $step = false, $args = array(), $force_update = false, $builder = false ) {
625
626 self::get_funnels( $type, $step, $args, $force_update, $builder );
627 $builder_type = $builder ? $builder : Wpfnl_functions::get_builder_type();
628 $template_data = get_option( WPFNL_TEMPLATES_OPTION_KEY . '_' . $type . '_' . $builder_type );
629
630 // Fallback for backward compatibility if data is not found with builder
631 if ( empty( $template_data ) && !$builder ) {
632 $template_data = get_option( WPFNL_TEMPLATES_OPTION_KEY . '_' . $type );
633 }
634
635 if ( empty( $template_data ) ) {
636 return array();
637 }
638 return $template_data;
639 }
640
641
642 /**
643 * Get step from template library site by step ID
644 *
645 * @param $step_id
646 * @param bool $force_update
647 *
648 * @return array
649 * @since 1.0.0
650 */
651 public static function get_step( $step_id, $force_update = false ) {
652 $timeout = ( $force_update ) ? 25 : 8;
653
654 $params = array(
655 '_fields' => 'id,title,link,slug,featured_media,post_meta,rawData,steps,divi_content,featured_image,steps_order,builder,industry,is_pro,type,step_type,funnel_id,funnel_name,_qubely_css,_qubely_interaction_json,__qubely_available_blocks',
656 'url' => self::get_remote_funnel_steps_api_url() . $step_id,
657 );
658
659 $url = add_query_arg( $params, self::get_remote_funnel_steps_api_url() . $step_id );
660 $api_args = array(
661 'timeout' => $timeout,
662 );
663 $response = ( new TemplateLibraryController() )->remote_get( $url, $api_args );
664
665 if ( is_array($response) && isset( $response['success'] ) ) {
666 $step = $response['data'];
667 return array(
668 'title' => ( isset( $step['title']['rendered'] ) ) ? $step['title']['rendered'] : '',
669 'post_meta' => ( isset( $step['post_meta'] ) ) ? $step['post_meta'] : '',
670 'data' => $step,
671 'content' => isset( $response['data']['content']['rendered'] ) ? $response['data']['content']['rendered'] : '',
672 'rawData' => isset( $response['data']['rawData'] ) ? $response['data']['rawData'] : '',
673 'divi_content' => isset( $response['data']['divi_content'] ) ? $response['data']['divi_content'] : '',
674 'message' => $response['message'],
675 'success' => $response['success'],
676 );
677 }
678
679 $url = add_query_arg( $params, WPFNL_MIDDLEMAN_STEP_URL );
680 $response = ( new TemplateLibraryController() )->remote_get( $url, $api_args );
681
682 if ( is_array($response) && isset( $response['success'] ) ) {
683 $step = $response['data'];
684 return array(
685 'title' => ( isset( $step['title']['rendered'] ) ) ? $step['title']['rendered'] : '',
686 'post_meta' => ( isset( $step['post_meta'] ) ) ? $step['post_meta'] : '',
687 'data' => $step,
688 'content' => isset( $response['data']['content']['rendered'] ) ? $response['data']['content']['rendered'] : '',
689 'rawData' => isset( $response['data']['rawData'] ) ? $response['data']['rawData'] : '',
690 'divi_content' => isset( $response['data']['divi_content'] ) ? $response['data']['divi_content'] : '',
691 'message' => $response['message'],
692 'success' => $response['success'],
693 );
694 }
695
696 if ( is_wp_error( $response ) ) {
697 return array(
698 'title' => '',
699 'post_meta' => array(),
700 'message' => $response->get_error_message(),
701 'data' => array(),
702 'success' => false,
703 'content' => '',
704 );
705 }
706
707 return array(
708 'title' => '',
709 'post_meta' => array(),
710 'message' => isset( $response['message'] ) ? $response['message'] : '',
711 'data' => isset( $response['data'] ) ? $response['data'] : array(),
712 'success' => isset( $response['success'] ) ? $response['success'] : false,
713 'content' => '',
714 );
715 }
716
717
718
719
720 public static function get_funnel( $funnel_id, $force_update = false ) {
721 $timeout = ( $force_update ) ? 25 : 8;
722 $params = array(
723 '_fields' => 'id,title,link,slug,featured_media,funnel_data,_funnel_data',
724 'url' => self::get_remote_funnel_api_url().$funnel_id,
725 );
726
727 $url = add_query_arg( $params, self::get_remote_funnel_api_url() . $funnel_id );
728
729 $api_args = array(
730 'timeout' => $timeout,
731 );
732 $response = ( new TemplateLibraryController() )->remote_get( $url, $api_args );
733
734 if (is_array($response) && isset( $response['success'] )) {
735 $funnel = $response['data'];
736 return array(
737 'funnel_id' => $funnel_id,
738 'funnel_data' => $funnel['funnel_data'] ?? '',
739 '_funnel_data' => $funnel['_funnel_data'] ?? '',
740 'title' => ( isset( $funnel['title']['rendered'] ) ) ? $funnel['title']['rendered'] : '',
741 'success' => $response['success'],
742 );
743 }
744
745 $url = add_query_arg( $params, WPFNL_MIDDLEMAN_SINGLE_TEMPLATE_URL );
746 $response = ( new TemplateLibraryController() )->remote_get( $url, $api_args );
747
748 if ( is_array($response) && isset( $response['success'] ) ) {
749 $funnel = $response['data'];
750 return array(
751 'funnel_id' => $funnel_id,
752 'funnel_data' => $funnel['funnel_data'] ?? '',
753 '_funnel_data' => $funnel['_funnel_data'] ?? '',
754 'title' => ( isset( $funnel['title']['rendered'] ) ) ? $funnel['title']['rendered'] : '',
755 'success' => $response['success'],
756 );
757 }
758
759 if ( is_wp_error( $response ) ) {
760 return array(
761 'title' => '',
762 'post_meta' => array(),
763 'message' => $response->get_error_message(),
764 'data' => array(),
765 'success' => false,
766 'content' => '',
767 );
768 }
769
770 return array(
771 'title' => '',
772 'post_meta' => array(),
773 'message' => isset( $response['message'] ) ? $response['message'] : '',
774 'data' => isset( $response['data'] ) ? $response['data'] : array(),
775 'success' => isset( $response['success'] ) ? $response['success'] : false,
776 'content' => '',
777 );
778 }
779
780
781 /**
782 * Get funnel type id
783 */
784 public function get_template_type_id( $request ) {
785
786 $response = array(
787 'success' => false,
788 'type_id' => '',
789 );
790
791 if ( $request['type'] ) {
792 $type = $request['type'];
793 $force_update = false;
794 $timeout = ( $force_update ) ? 40 : 55;
795 $teplate_type_url = self::get_remote_funnel_template_type_api_url();
796 $response = self::remote_get(
797 $teplate_type_url,
798 array(
799 'timeout' => $timeout,
800 )
801 );
802
803 if ( isset( $response['data'] ) && is_array( $response['data'] ) && !empty($response['data'])) {
804 foreach ( $response['data'] as $types ) {
805 if ( $type === $types['name'] ) {
806 $response['success'] = true;
807 $response['type_id'] = $types['id'];
808 }
809 }
810 }
811
812 $params = array(
813 'url' => self::get_remote_funnel_template_type_api_url()
814 );
815
816 $url = add_query_arg( $params, WPFNL_MIDDLEMAN_TEMPLATE_TYPE_URL );
817 $response = ( new TemplateLibraryController() )->remote_get( $url , array(
818 'timeout' => $timeout,
819 ) );
820
821 if ( isset( $response['data'] ) && is_array( $response['data'] ) && !empty($response['data'])) {
822 foreach ( $response['data'] as $types ) {
823 if ( $type === $types['name'] ) {
824 $response['success'] = true;
825 $response['type_id'] = $types['id'];
826 }
827 }
828 }
829
830
831 }
832 return rest_ensure_response( $response );
833 }
834
835
836 /**
837 * Prepare a single setting object for response.
838 *
839 * @param object $item Setting object.
840 * @param WP_REST_Request $request Request object.
841 *
842 * @return WP_REST_Response $response Response data.
843 * @since 1.0.0
844 */
845 public function prepare_item_for_response( $item, $request ) {
846 $data = $this->add_additional_fields_to_object( $item, $request );
847 return rest_ensure_response( $data );
848 }
849 }
850