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-settings-controller.php

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

565 lines 18.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings controller
4 *
5 * @package WPFunnels\Rest\Controllers
6 */
7 namespace WPFunnels\Rest\Controllers;
8
9 use GuzzleHttp\Psr7\Request;
10 use WP_Error;
11 use WP_REST_Request;
12 use WP_REST_Response;
13 use WP_REST_Server;
14 use WPFunnels\Wpfnl_functions;
15 use WPFunnels\lms\helper\Wpfnl_lms_learndash_functions;
16 use WPFunnels\Wpfnl;
17 use Elementor\Core\Kits\Manager;
18 class SettingsController extends Wpfnl_REST_Controller {
19
20 /**
21 * Endpoint namespace.
22 *
23 * @var string
24 */
25 protected $namespace = 'wpfunnels/v1';
26
27 /**
28 * Route base.
29 *
30 * @var string
31 */
32 protected $rest_base = 'settings/(?P<group_id>[\w-]+)';
33
34
35 protected $rest_wc_plugin_active_base = 'settings/activate_wc_plugins';
36 protected $rest_mrm_plugin_active_base = 'settings/activate_mrm_plugins';
37
38 protected $rest_funnel_settings = 'settings';
39
40 public function update_items_permissions_check( $request ) {
41 $permission = current_user_can('manage_options');
42 if ( ! Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'settings' ) ) {
43 return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'wpfnl' ), array( 'status' => rest_authorization_required_code() ) );
44 }
45 return true;
46 }
47
48 /**
49 * Makes sure the current user has access to READ the settings APIs.
50 *
51 * @param WP_REST_Request $request Full data about the request.
52 *
53 * @return WP_Error|boolean
54 * @since 3.0.0
55 */
56 public function get_items_permissions_check( $request ) {
57 if ( ! Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'settings' ) ) {
58 return new WP_Error( 'wpfunnels_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'wpfnl' ), array( 'status' => rest_authorization_required_code() ) );
59 }
60 return true;
61 }
62
63 public function register_routes()
64 {
65 register_rest_route(
66 $this->namespace, '/' . $this->rest_base . '/(?P<settings_id>[\w-]+)', array(
67 'args' => array(
68 'settings_id' => array(
69 'description' => __( 'Settings group ID.', 'wpfnl' ),
70 'type' => 'string',
71 )
72 ),
73 // array(
74 // 'methods' => WP_REST_Server::READABLE,
75 // 'callback' => array( $this, 'get_item' ),
76 // 'permission_callback' => array( $this, 'get_items_permissions_check' ),
77 // ),
78 array(
79 'methods' => WP_REST_Server::EDITABLE,
80 'callback' => array( $this, 'update_settings' ),
81 'permission_callback' => array( $this, 'update_items_permissions_check' ),
82 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
83 )
84 )
85 );
86
87 register_rest_route(
88 $this->namespace, '/' . $this->rest_wc_plugin_active_base , array(
89 array(
90 'methods' => WP_REST_Server::EDITABLE,
91 'callback' => array( $this, 'activate_wc_plugins' ),
92 'permission_callback' => array( $this, 'update_items_permissions_check' ),
93 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
94 )
95 )
96 );
97
98 register_rest_route(
99 $this->namespace, '/' . $this->rest_mrm_plugin_active_base , array(
100 array(
101 'methods' => WP_REST_Server::EDITABLE,
102 'callback' => array( $this, 'activate_mrm_plugins' ),
103 'permission_callback' => array( $this, 'update_items_permissions_check' ),
104 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
105 )
106 )
107 );
108
109
110 register_rest_route(
111 $this->namespace, '/' . $this->rest_funnel_settings . '/is-lms/(?P<funnel_id>[\w-]+)' , array(
112 array(
113 'methods' => 'GET',
114 'callback' => array( $this, 'is_lms' ),
115 'permission_callback' => array( $this, 'update_items_permissions_check' ),
116 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
117 )
118 )
119 );
120
121 register_rest_route(
122 $this->namespace, '/' . $this->rest_funnel_settings . '/update-funnel-type' , array(
123 array(
124 'methods' => 'POST',
125 'callback' => array( $this, 'update_funnel_type' ),
126 'permission_callback' => array( $this, 'update_items_permissions_check' ),
127 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
128 )
129 )
130 );
131
132 register_rest_route(
133 $this->namespace, '/' . $this->rest_funnel_settings . '/update-builder-type' , array(
134 array(
135 'methods' => 'POST',
136 'callback' => array( $this, 'update_builder_type' ),
137 'permission_callback' => array( $this, 'update_items_permissions_check' ),
138 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
139 )
140 )
141 );
142
143 register_rest_route(
144 $this->namespace, '/' . $this->rest_funnel_settings . '/get-funnel-type-id/(?P<funnel_id>[\w-]+)' , array(
145 array(
146 'methods' => 'GET',
147 'callback' => array( $this, 'get_funnel_type_id' ),
148 'permission_callback' => array( $this, 'update_items_permissions_check' ),
149 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
150 )
151 )
152 );
153
154 }
155
156
157 /**
158 * Get all settings in a group.
159 *
160 * @param string $group_id Group ID.
161 *
162 * @return array|WP_Error
163 */
164 public function get_group_settings( $group_id ) {
165 if ( empty( $group_id ) ) {
166 return new WP_Error( 'rest_setting_setting_group_invalid', __( 'Invalid setting group.', 'wpfnl' ), array( 'status' => 404 ) );
167 }
168 $settings = Wpfnl_functions::get_admin_settings( $group_id );
169 if ( empty( $settings ) ) {
170 return new WP_Error( 'rest_setting_setting_group_invalid', __( 'Invalid setting group.', 'wpfnl' ), array( 'status' => 404 ) );
171 }
172
173 $filtered_settings = array();
174 foreach ($settings as $key => $setting) {
175 $filtered_settings[] = array(
176 'id' => $key,
177 'value' => $setting
178 );
179 }
180 return $filtered_settings;
181 }
182
183
184 /**
185 * Get setting data.
186 *
187 * @param string $group_id Group ID.
188 * @param string $setting_id Setting ID.
189 *
190 * @return stdClass|WP_Error
191 * @since 3.0.0
192 */
193 public function get_setting( $group_id, $setting_id ) {
194 if ( empty( $setting_id ) ) {
195 return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) );
196 }
197
198 $settings = $this->get_group_settings( $group_id );
199
200 if ( is_wp_error( $settings ) ) {
201 return $settings;
202 }
203
204 $array_key = array_keys( wp_list_pluck( $settings, 'id' ), $setting_id );
205
206 if ( empty( $array_key ) ) {
207 return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) );
208 }
209
210 $setting = $settings[ $array_key[0] ];
211 return $setting;
212 }
213
214
215 /**
216 * Active required plugin based on slug
217 *
218 * @param $plugin_name
219 *
220 * @return WP_Error|WP_REST_Response
221 * @since 1.0.0
222 */
223 public function activate_wc_plugins( \WP_REST_Request $request ) {
224 if( !function_exists('activate_plugin') ) {
225 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
226 }
227
228
229
230 $permission = $request['permission'];
231 $islms = $request['islms'];
232 $isWc = $request['isWc'];
233
234 $plugin_slug_arr = array(
235 'woocommerce/woocommerce.php' => false,
236 'cart-lift/cart-lift.php' => false,
237 'sfwd-lms/sfwd_lms.php' => false,
238 );
239 $should_activated = array(
240 'woocommerce/woocommerce.php' => false,
241 'cart-lift/cart-lift.php' => false,
242 'sfwd-lms/sfwd_lms.php' => false,
243 );
244 if($permission == '' ){
245 unset($should_activated[ 'cart-lift/cart-lift.php' ]);
246 unset($plugin_slug_arr[ 'cart-lift/cart-lift.php' ]);
247 }
248
249 if($permission == '' ){
250 unset($should_activated[ 'cart-lift/cart-lift.php' ]);
251 unset($plugin_slug_arr[ 'cart-lift/cart-lift.php' ]);
252 }
253
254 if( $isWc == 'no' ){
255 unset($should_activated[ 'woocommerce/woocommerce.php' ]);
256 unset($plugin_slug_arr[ 'woocommerce/woocommerce.php' ]);
257 }
258 if( $islms == '' ){
259 unset($should_activated[ 'sfwd-lms/sfwd_lms.php' ]);
260 unset($plugin_slug_arr[ 'sfwd-lms/sfwd_lms.php' ]);
261 }
262
263 foreach ( $plugin_slug_arr as $slug => $is_silent ) {
264 $data = activate_plugin( $slug, '', false, $is_silent );
265 if ( !is_wp_error( $data ) ) {
266 $should_activated[ $slug ] = true;
267
268 }
269 }
270 delete_transient( '_wc_activation_redirect' );
271
272 foreach ( $should_activated as $slug => $is_activated ) {
273 if(!$is_activated) {
274 $settings['success'] = false;
275 return rest_ensure_response( $settings );
276 }
277 }
278
279 $settings['success'] = true;
280 return rest_ensure_response( $settings );
281 }
282 /**
283 * Active required plugin based on slug
284 *
285 * @param $plugin_name
286 *
287 * @return WP_Error|WP_REST_Response
288 * @since 1.0.0
289 */
290 public function activate_mrm_plugins( \WP_REST_Request $request ) {
291 if( !function_exists('activate_plugin') ) {
292 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
293 }
294 $plugins = isset( $request['plugins'] ) ? $request['plugins'] : [];
295 $plugin_slug_arr = [];
296 $should_activated = [];
297 if( !empty($plugins) ){
298 foreach( $plugins as $plugin ){
299 if( 'mail-mint' == $plugin ) {
300 $plugin_slug_arr['mail-mint/mail-mint.php'] = false;
301 $should_activated['mail-mint/mail-mint.php'] = false;
302 }
303 }
304 }
305 foreach ( $plugin_slug_arr as $slug => $is_silent ) {
306 $data = activate_plugin( $slug, '', false, $is_silent );
307
308 if ( !is_wp_error( $data ) ) {
309 $should_activated[ $slug ] = true;
310 }
311 }
312 delete_transient( '_wc_activation_redirect' );
313 foreach ( $should_activated as $slug => $is_activated ) {
314 if(!$is_activated) {
315 $settings['success'] = false;
316 return rest_ensure_response( $settings );
317 }
318 }
319
320 $settings['success'] = true;
321 return rest_ensure_response( $settings );
322 }
323
324
325 /**
326 * Disable activation redirect
327 */
328 public function disable_wc_activation_redirect() {
329 delete_transient( '_wc_activation_redirect' );
330 }
331
332
333
334 /**
335 * Activate required plugins
336 *
337 * @param $plugin_name
338 */
339 public function active_plugin($plugin_slug,$permission = 1) {
340 $plugin_slug_arr = array();
341
342 switch ($plugin_slug) {
343 case 'elementor':
344 $plugin_slug_arr = array(
345 'elementor/elementor.php' => true,
346 );
347 break;
348 case 'gutenberg':
349 if($permission == 1){
350 $plugin_slug_arr = array(
351 'qubely/qubely.php' => true,
352 );
353 }
354 break;
355 case 'divi-builder':
356 $is_divi_installed = Wpfnl_functions::wpfnl_check_is_plugin_installed( $plugin_slug.'/divi-builder.php' );
357 $is_divi_theme_active = Wpfnl_functions::wpfnl_is_theme_active( 'Divi' );
358 if( $is_divi_installed ){
359 $plugin_slug_arr = array(
360 'divi-builder/divi-builder.php' => true,
361 );
362 }
363 break;
364
365 }
366 if($plugin_slug_arr) {
367 foreach ( $plugin_slug_arr as $slug => $is_silent ) {
368 $activate[ $slug ] = activate_plugin( $slug, '', false, $is_silent );
369 }
370 }
371
372 if(is_plugin_active( 'elementor/elementor.php' )){
373 Manager::create_default_kit();
374 }
375 }
376
377
378 /**
379 * Update a single setting in a group.
380 *
381 * @param WP_REST_Request $request Request data.
382 *
383 * @return WP_Error|WP_REST_Response
384 * @since 1.0.0
385 */
386 public function update_settings( $request ) {
387 if( isset( $request['settings_id'], $request['group_id'] )){
388 if( $request['settings_id'] === 'permalink') {
389 $default = Wpfnl_functions::get_permalink_settings();
390 $settings = Wpfnl_functions::get_admin_settings( '_wpfunnels_permalink_settings', $default);
391 } elseif ($request['settings_id'] === 'funnel_type') {
392 $default = Wpfnl_functions::get_general_settings();
393 $settings = Wpfnl_functions::get_admin_settings( $request['group_id'], $default);
394 }
395 else {
396 $default = Wpfnl_functions::get_general_settings();
397 $settings = Wpfnl_functions::get_admin_settings( $request['group_id'], $default );
398
399
400 }
401
402 if ( !$settings ) {
403 return new WP_Error( 'rest_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) );
404 }
405 if ( empty( $settings ) ) {
406 return new WP_Error( 'rest_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) );
407 }
408
409 if($request['type'] !== 'ignore_activation' ) {
410 $this->active_plugin($request['slug'],$request['permission']);
411 }
412
413 if($request['settings_id'] === 'permalink') {
414 $settings['structure'] = $request['settings'];
415 $settings['funnel_base'] = $request['funnelBase'];
416 $settings['step_base'] = $request['stepBase'];
417 Wpfnl_functions::update_admin_settings('_wpfunnels_permalink_saved', true);
418 Wpfnl_functions::update_admin_settings($request['group_id'], $settings);
419
420 }else {
421 if( 'funnel_type' === $request['settings_id'] ) {
422 $general_settings = get_option( '_wpfunnels_general_settings' );
423 $settings[$request['settings_id']] = isset($general_settings['funnel_type']) ? $general_settings['funnel_type'] : 'sales';
424 }else{
425 $settings[$request['settings_id']] = $request['value'];
426 }
427
428 Wpfnl_functions::update_admin_settings($request['group_id'], $settings);
429
430 }
431 }
432 $settings['success'] = true;
433 $settings = $this->prepare_item_for_response( $settings, $request );
434 delete_option(WPFNL_TEMPLATES_OPTION_KEY);
435 delete_transient('wpfunnels_remote_template_data_' . WPFNL_VERSION);
436 return rest_ensure_response( $settings );
437 }
438
439
440 /**
441 * Check lms or not
442 */
443 public function is_lms( $request ){
444
445 $response = [
446 'success' => false,
447 'is_lms' => false,
448 ];
449
450 if( $request['funnel_id'] ){
451 $funnel_id = $request['funnel_id'];
452 $type = get_post_meta( $funnel_id, '_wpfnl_funnel_type', true );
453 if( Wpfnl_functions::is_lms_addon_active() && $type == 'lms' ){
454 $response['success'] = true;
455 $response['is_lms'] = true;
456 }else{
457 $response['success'] = true;
458 $response['is_lms'] = false;
459 }
460 }
461 return rest_ensure_response( $response );
462 }
463
464
465 /**
466 * Update funnel type
467 *
468 * @param Array $request
469 *
470 * @return Array
471 * @since 2.5.3
472 */
473 public function update_funnel_type( $request ){
474
475 $response = [
476 'success' => false,
477 ];
478
479 if( $request['type'] ){
480 $type = $request['type'];
481 $general_settings = get_option( '_wpfunnels_general_settings' );
482 $general_settings['funnel_type'] = $type;
483 update_option('_wpfunnels_general_settings', $general_settings );
484 $response = [
485 'success' => true,
486 ];
487 }
488 return rest_ensure_response( $response );
489 }
490
491
492 /**
493 * Update funnel builder
494 *
495 * @param Array $request
496 *
497 * @return Array
498 * @since 2.5.3
499 */
500 public function update_builder_type( $request ){
501
502 $response = [
503 'success' => false,
504 ];
505
506 if( $request['builder'] ){
507 $builder = $request['builder'];
508 $general_settings = get_option( '_wpfunnels_general_settings' );
509 $general_settings['builder'] = $builder;
510 update_option('_wpfunnels_general_settings', $general_settings );
511 $response = [
512 'success' => true,
513 ];
514 }
515 return rest_ensure_response( $response );
516 }
517
518
519
520 /**
521 * Get funnel type id
522 */
523 public function get_funnel_type_id( $request ){
524
525 $response = [
526 'success' => false,
527 'type_id' => '',
528 ];
529
530 if( $request['funnel_id'] ){
531 $funnel_id = $request['funnel_id'];
532 $type = get_post_meta( $funnel_id, '_wpfnl_funnel_type', true );
533 $term = get_term_by('slug', $type, 'template_type');
534 if( $term ){
535 $response['success'] = true;
536 $response['type_id'] = $term->term_id;
537 }
538 }
539
540 return rest_ensure_response( $response );
541 }
542
543
544
545
546 /**
547 * Prepare a single setting object for response.
548 *
549 * @param object $item Setting object.
550 * @param WP_REST_Request $request Request object.
551 *
552 * @return WP_REST_Response $response Response data.
553 * @since 3.0.0
554 */
555 public function prepare_item_for_response( $item, $request ) {
556 $data = $this->add_additional_fields_to_object( $item, $request );
557 $response = rest_ensure_response( $data );
558 $response->add_links( $this->prepare_links( $request['settings_id'] ) );
559 return $response;
560 }
561
562
563
564 }
565