PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
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 2.8.16, at includes/core/rest-api/Controllers/class-templatelibrary-controller.php

442 lines 12.6 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 namespace WPFunnels\Rest\Controllers;
8
9 use WP_Error;
10 use WP_REST_Request;
11 use WP_REST_Response;
12 use WP_REST_Server;
13 use WPFunnels\Wpfnl_functions;
14 use WPFunnels\lms\helper\Wpfnl_lms_learndash_functions;
15
16 class TemplateLibraryController extends Wpfnl_REST_Controller {
17
18 public static $funnel_api_url = 'https://templates.getwpfunnels.com/wp-json/wp/v2/wpfunnels/';
19 public static $funnel_categories_api_url = 'https://templates.getwpfunnels.com/wp-json/wp/v2/template_industries/';
20 public static $funnel_steps_api_url = 'https://templates.getwpfunnels.com/wp-json/wp/v2/wpfunnel_steps/';
21 public static $all_funnels_api_url = 'https://templates.getwpfunnels.com/wp-json/wpfunnels/v1/get_all_funnels/';
22
23
24 /**
25 * Endpoint namespace.
26 *
27 * @var string
28 */
29 protected $namespace = 'wpfunnels/v1';
30
31 /**
32 * Route base.
33 *
34 * @var string
35 */
36 protected $rest_base = 'templates/';
37
38
39 /**
40 * Get remote funnel api url
41 *
42 * @return string
43 * @since 2.2.8
44 */
45 private static function get_remote_funnel_api_url() {
46 if( 'oxygen' === Wpfnl_functions::get_builder_type() ) {
47 return 'https://oxygentemplates.getwpfunnels.com/wp-json/wp/v2/wpfunnels/';
48 }
49 return self::$funnel_api_url;
50 }
51
52
53 /**
54 * Get remote funnel builders categories url
55 *
56 * @return string
57 *
58 * @since 2.2.8
59 */
60 private static function get_remote_funnel_categories_api_url() {
61 if( 'oxygen' === Wpfnl_functions::get_builder_type() ) {
62 return 'https://oxygentemplates.getwpfunnels.com/wp-json/wp/v2/template_industries/';
63 }
64 return self::$funnel_categories_api_url;
65 }
66
67
68 /**
69 * Get remote funnel steps categories url
70 *
71 * @return string
72 *
73 * @since 2.2.8
74 */
75 private static function get_remote_funnel_steps_api_url() {
76 if( 'oxygen' === Wpfnl_functions::get_builder_type() ) {
77 return 'https://oxygentemplates.getwpfunnels.com/wp-json/wp/v2/wpfunnel_steps/';
78 }
79 return self::$funnel_steps_api_url;
80 }
81
82
83 /**
84 * Get all templates API url
85 *
86 * @return string
87 * @since 2.2.8
88 */
89 private static function get_all_templates_api_url() {
90 if( 'oxygen' === Wpfnl_functions::get_builder_type() ) {
91 return 'https://oxygentemplates.getwpfunnels.com/wp-json/wpfunnels/v1/get_all_funnels/';
92 }
93 return self::$all_funnels_api_url;
94 }
95
96
97 /**
98 * Check user permission
99 *
100 * @param $request
101 *
102 * @return bool|WP_Error
103 */
104 public function update_items_permissions_check( $request ) {
105 $permission = current_user_can('manage_options');
106 if ( ! Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'templates' ) ) {
107 return new WP_Error( 'wpfunnels_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'wpfnl' ), array( 'status' => rest_authorization_required_code() ) );
108 }
109 return true;
110 }
111
112
113 /**
114 * Makes sure the current user has access to READ the settings APIs.
115 *
116 * @param WP_REST_Request $request Full data about the request.
117 *
118 * @return WP_Error|boolean
119 * @since 3.0.0
120 */
121 public function get_items_permissions_check( $request ) {
122 if ( ! Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'templates' ) ) {
123 return new WP_Error( 'wpfunnels_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'wpfnl' ), array( 'status' => rest_authorization_required_code() ) );
124 }
125 return true;
126 }
127
128
129
130 public function register_routes()
131 {
132 register_rest_route(
133 $this->namespace, '/' . $this->rest_base . 'get_templates' , array(
134 array(
135 'methods' => WP_REST_Server::READABLE,
136 'callback' => array( $this, 'get_templates' ),
137 'permission_callback' => array( $this, 'get_items_permissions_check' ),
138 ),
139 )
140 );
141
142 register_rest_route(
143 $this->namespace, '/' . $this->rest_base . 'get_template_type_id' , array(
144 array(
145 'methods' => WP_REST_Server::READABLE,
146 'callback' => array( $this, 'get_template_type_id' ),
147 'permission_callback' => array( $this, 'get_items_permissions_check' ),
148
149 )
150 )
151 );
152
153 }
154
155
156 /**
157 * Prepare a single setting object for response.
158 *
159 * @param $request
160 *
161 * @return WP_REST_Response
162 */
163 public function get_templates($request) {
164 $funnel_template_type = isset( $_GET['type'] ) ? $_GET['type'] : 'wc';
165 $step = isset( $_GET['step'] ) ? $_GET['step'] : false;
166
167 $templates = $this->get_funnels_data($funnel_template_type, $step , [] ,false);
168 $templates['success'] = true;
169 return $this->prepare_item_for_response( $templates, $request );
170 }
171
172
173
174 /**
175 * Send http request
176 *
177 * @param $url
178 * @param $args
179 *
180 * @return array
181 */
182 private function remote_get($url, $args)
183 {
184 $response = wp_remote_get($url, $args);
185
186 // bail if there is an error
187 if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
188 return [
189 'success' => false,
190 'message' => $response->get_error_message(),
191 'data' => $response,
192 ];
193 }
194
195 // Decode the results.
196 $results = json_decode( $response['body'], true );
197
198 // bail if there is no result found
199 if ( ! is_array( $results ) ) {
200 return new \WP_Error( 'unexpected_data_format', 'Data was not returned in the expected format.' );
201 }
202
203 return [
204 'success' => true,
205 'message' => 'Data successfully retrieved',
206 'data' => json_decode(wp_remote_retrieve_body($response), true),
207 ];
208 }
209
210
211 /**
212 * Get all funnels of the specific builder
213 *
214 * @param bool $type
215 * @param bool $isStep
216 * @param array $args
217 * @param bool $force_update
218 *
219 * @return bool|mixed|void
220 */
221 private function get_funnels($type = false, $isStep= false,$args = [], $force_update = false)
222 {
223 $builder_type = Wpfnl_functions::get_builder_type();
224 $cache_key = 'wpfunnels_remote_template_data_'.$type.'_'. WPFNL_VERSION;
225 $data = get_transient($cache_key);
226 if ( $data ) {
227 return;
228 }
229 if ($type && ($force_update || false === $data) ) {
230 if( false === $data ){
231 $data = [];
232 }
233 $timeout = ($force_update) ? 40 : 55;
234 // get all templates
235 $params = [
236 'per_page' => 100,
237 'offset' => 0,
238 'builder' => $builder_type,
239 'template_type' => $type
240 ];
241 $url = add_query_arg($params, self::get_all_templates_api_url());
242
243 $template_data = self::remote_get($url, [
244 'timeout' => $timeout,
245 ]);
246 if ( !is_array($template_data) || !$template_data['success'] ) {
247 set_transient( $cache_key, [], 24 * HOUR_IN_SECONDS );
248 return false;
249 }
250
251
252 // get all steps
253 $steps = [];
254 if ( $template_data['data'] ) {
255 foreach ( $template_data['data'] as $key=>$template ) {
256 $i=0;
257 foreach ($template['steps'] as $_step) {
258 if( 0 == $i ){
259 $template_data['data'][$key]['link'] = $_step['link'];
260 }
261 $_step['funnel_name'] = $template['title'];
262 $steps[] = $_step;
263 $i++;
264 }
265 }
266 }
267 // fetch the funnel categories from the remote server
268 $params = [
269 'per_page' => 100,
270 ];
271 $url = add_query_arg($params, self::get_remote_funnel_categories_api_url());
272 $categories_data = self::remote_get( $url, [
273 'timeout' => $timeout,
274 ]);
275 if ( !is_array($categories_data) || !$categories_data['success'] ) {
276 set_transient($cache_key, [], 24 * HOUR_IN_SECONDS);
277 return false;
278 }
279
280 $data['templates'] = $template_data['data'];
281 $data['steps'] = $steps;
282 $data['categories'] = $categories_data['data'];
283 update_option(WPFNL_TEMPLATES_OPTION_KEY.'_'.$type, $data, 'no');
284 set_transient($cache_key, $data, 24 * HOUR_IN_SECONDS);
285 return false;
286 }
287 }
288
289
290
291 /**
292 * Get funnel templates data
293 *
294 * @param array $args
295 * @param bool $force_update
296 *
297 * @return array|mixed|void
298 * @since 1.0.0
299 */
300 public function get_funnels_data($type = false, $step = false, $args = [], $force_update = false)
301 {
302
303 self::get_funnels($type,$step,$args, $force_update);
304 $template_data = get_option(WPFNL_TEMPLATES_OPTION_KEY.'_'.$type);
305
306 if (empty($template_data)) {
307 return [];
308 }
309 return $template_data;
310 }
311
312
313 /**
314 * Get step from template library site by step ID
315 *
316 * @param $step_id
317 * @param bool $force_update
318 *
319 * @return array
320 * @since 1.0.0
321 */
322 public static function get_step( $step_id, $force_update = false )
323 {
324 $timeout = ($force_update) ? 25 : 8;
325
326 $params = [
327 '_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',
328 ];
329
330 $url = add_query_arg($params, self::get_remote_funnel_steps_api_url() . $step_id);
331 $api_args = [
332 'timeout' => $timeout,
333 ];
334 $response = (new TemplateLibraryController)->remote_get($url, $api_args);
335
336 if ($response['success']) {
337 $step = $response['data'];
338 return [
339 'title' => (isset($step['title']['rendered'])) ? $step['title']['rendered'] : '',
340 'post_meta' => (isset($step['post_meta'])) ? $step['post_meta'] : '',
341 'data' => $step,
342 'content' => isset($response['data']['content']['rendered']) ? $response['data']['content']['rendered'] : '',
343 'rawData' => isset($response['data']['rawData']) ? $response['data']['rawData'] : '',
344 'divi_content' => isset($response['data']['divi_content']) ? $response['data']['divi_content'] : '',
345 'message' => $response['message'],
346 'success' => $response['success'],
347 ];
348 }
349
350 return [
351 'title' => '',
352 'post_meta' => [],
353 'message' => $response['message'],
354 'data' => $response['data'],
355 'success' => $response['success'],
356 'content' => '',
357 ];
358 }
359
360
361
362
363 public static function get_funnel( $funnel_id, $force_update = false ) {
364 $timeout = ($force_update) ? 25 : 8;
365 $params = [
366 '_fields' => 'id,title,link,slug,featured_media,funnel_data',
367 ];
368
369 $url = add_query_arg( $params, self::get_remote_funnel_api_url() . $funnel_id );
370 $api_args = [
371 'timeout' => $timeout
372 ];
373 $response = (new TemplateLibraryController)->remote_get( $url, $api_args );
374 if ($response['success']) {
375 $funnel = $response['data'];
376 return [
377 'funnel_id' => $funnel_id,
378 'funnel_data' => $funnel['funnel_data'],
379 'title' => (isset($funnel['title']['rendered'])) ? $funnel['title']['rendered'] : '',
380 'success' => $response['success'],
381 ];
382 }
383
384 return [
385 'title' => '',
386 'post_meta' => [],
387 'message' => $response['message'],
388 'data' => $response['data'],
389 'success' => $response['success'],
390 'content' => '',
391 ];
392 }
393
394
395 /**
396 * Get funnel type id
397 */
398 public function get_template_type_id( $request ){
399
400 $response = [
401 'success' => false,
402 'type_id' => '',
403 ];
404
405
406 if( $request['type'] ){
407 $type = $request['type'];
408 $force_update = false;
409 $timeout = ($force_update) ? 40 : 55;
410 $teplate_type_url = self::get_remote_funnel_template_type_api_url();
411 $response = self::remote_get($teplate_type_url, [
412 'timeout' => $timeout,
413 ]);
414
415 if( isset($response['data']) && is_array($response['data'])){
416 foreach( $response['data'] as $types ){
417 if( $type === $types['name'] ){
418 $response['success'] = true;
419 $response['type_id'] = $types['id'];
420 }
421 }
422 }
423 }
424 return rest_ensure_response( $response );
425 }
426
427
428 /**
429 * Prepare a single setting object for response.
430 *
431 * @param object $item Setting object.
432 * @param WP_REST_Request $request Request object.
433 *
434 * @return WP_REST_Response $response Response data.
435 * @since 1.0.0
436 */
437 public function prepare_item_for_response( $item, $request ) {
438 $data = $this->add_additional_fields_to_object( $item, $request );
439 return rest_ensure_response( $data );
440 }
441 }
442