| 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('wpf_manage_funnels'); |
| 42 |
if ( ! Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'settings' ) ) { |
| 43 |
return new WP_Error( 'wpfunnels_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 . '/activate-plugins' , array( |
| 123 |
array( |
| 124 |
'methods' => 'POST', |
| 125 |
'callback' => array( $this, 'activate_plugins' ), |
| 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 . '/create-contact' , array( |
| 134 |
array( |
| 135 |
'methods' => 'POST', |
| 136 |
'callback' => array( $this, 'create_contact' ), |
| 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 . '/save-setup-wizard-settings' , array( |
| 145 |
array( |
| 146 |
'methods' => 'POST', |
| 147 |
'callback' => array( $this, 'save_setup_wizard_settings' ), |
| 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 |
register_rest_route( |
| 155 |
$this->namespace, '/' . $this->rest_funnel_settings . '/update-builder-type' , array( |
| 156 |
array( |
| 157 |
'methods' => 'POST', |
| 158 |
'callback' => array( $this, 'update_builder_type' ), |
| 159 |
'permission_callback' => array( $this, 'update_items_permissions_check' ), |
| 160 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 161 |
) |
| 162 |
) |
| 163 |
); |
| 164 |
|
| 165 |
register_rest_route( |
| 166 |
$this->namespace, '/' . $this->rest_funnel_settings . '/get-funnel-type-id/(?P<funnel_id>[\w-]+)' , array( |
| 167 |
array( |
| 168 |
'methods' => 'GET', |
| 169 |
'callback' => array( $this, 'get_funnel_type_id' ), |
| 170 |
'permission_callback' => array( $this, 'update_items_permissions_check' ), |
| 171 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 172 |
) |
| 173 |
) |
| 174 |
); |
| 175 |
|
| 176 |
register_rest_route( |
| 177 |
$this->namespace, '/' . $this->rest_funnel_settings . '/canvas-view-mode' , array( |
| 178 |
array( |
| 179 |
'methods' => 'POST', |
| 180 |
'callback' => array( $this, 'update_canvas_view_mode' ), |
| 181 |
'permission_callback' => array( $this, 'update_items_permissions_check' ), |
| 182 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 183 |
) |
| 184 |
) |
| 185 |
); |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
* Get all settings in a group. |
| 192 |
* |
| 193 |
* @param string $group_id Group ID. |
| 194 |
* |
| 195 |
* @return array|WP_Error |
| 196 |
*/ |
| 197 |
public function get_group_settings( $group_id ) { |
| 198 |
if ( empty( $group_id ) ) { |
| 199 |
return new WP_Error( 'rest_setting_setting_group_invalid', __( 'Invalid setting group.', 'wpfnl' ), array( 'status' => 404 ) ); |
| 200 |
} |
| 201 |
if ( ! $this->is_allowed_settings_group( $group_id ) ) { |
| 202 |
return new WP_Error( 'rest_setting_setting_group_invalid', __( 'Invalid setting group.', 'wpfnl' ), array( 'status' => 403 ) ); |
| 203 |
} |
| 204 |
$settings = Wpfnl_functions::get_admin_settings( $group_id ); |
| 205 |
if ( empty( $settings ) ) { |
| 206 |
return new WP_Error( 'rest_setting_setting_group_invalid', __( 'Invalid setting group.', 'wpfnl' ), array( 'status' => 404 ) ); |
| 207 |
} |
| 208 |
|
| 209 |
$filtered_settings = array(); |
| 210 |
foreach ($settings as $key => $setting) { |
| 211 |
$filtered_settings[] = array( |
| 212 |
'id' => $key, |
| 213 |
'value' => $setting |
| 214 |
); |
| 215 |
} |
| 216 |
return $filtered_settings; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
/** |
| 221 |
* Get setting data. |
| 222 |
* |
| 223 |
* @param string $group_id Group ID. |
| 224 |
* @param string $setting_id Setting ID. |
| 225 |
* |
| 226 |
* @return stdClass|WP_Error |
| 227 |
* @since 3.0.0 |
| 228 |
*/ |
| 229 |
public function get_setting( $group_id, $setting_id ) { |
| 230 |
if ( empty( $setting_id ) ) { |
| 231 |
return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) ); |
| 232 |
} |
| 233 |
|
| 234 |
$settings = $this->get_group_settings( $group_id ); |
| 235 |
|
| 236 |
if ( is_wp_error( $settings ) ) { |
| 237 |
return $settings; |
| 238 |
} |
| 239 |
|
| 240 |
$array_key = array_keys( wp_list_pluck( $settings, 'id' ), $setting_id ); |
| 241 |
|
| 242 |
if ( empty( $array_key ) ) { |
| 243 |
return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) ); |
| 244 |
} |
| 245 |
|
| 246 |
$setting = $settings[ $array_key[0] ]; |
| 247 |
return $setting; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Active required plugin based on slug |
| 253 |
* |
| 254 |
* @param $plugin_name |
| 255 |
* |
| 256 |
* @return WP_Error|WP_REST_Response |
| 257 |
* @since 1.0.0 |
| 258 |
*/ |
| 259 |
public function activate_wc_plugins( \WP_REST_Request $request ) { |
| 260 |
if( !function_exists('activate_plugin') ) { |
| 261 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
$permission = $request['permission']; |
| 267 |
$islms = $request['islms']; |
| 268 |
$isWc = $request['isWc']; |
| 269 |
|
| 270 |
$plugin_slug_arr = array( |
| 271 |
'woocommerce/woocommerce.php' => false, |
| 272 |
'cart-lift/cart-lift.php' => false, |
| 273 |
'sfwd-lms/sfwd_lms.php' => false, |
| 274 |
); |
| 275 |
$should_activated = array( |
| 276 |
'woocommerce/woocommerce.php' => false, |
| 277 |
'cart-lift/cart-lift.php' => false, |
| 278 |
'sfwd-lms/sfwd_lms.php' => false, |
| 279 |
); |
| 280 |
if($permission == '' ){ |
| 281 |
unset($should_activated[ 'cart-lift/cart-lift.php' ]); |
| 282 |
unset($plugin_slug_arr[ 'cart-lift/cart-lift.php' ]); |
| 283 |
} |
| 284 |
|
| 285 |
if($permission == '' ){ |
| 286 |
unset($should_activated[ 'cart-lift/cart-lift.php' ]); |
| 287 |
unset($plugin_slug_arr[ 'cart-lift/cart-lift.php' ]); |
| 288 |
} |
| 289 |
|
| 290 |
if( $isWc == 'no' ){ |
| 291 |
unset($should_activated[ 'woocommerce/woocommerce.php' ]); |
| 292 |
unset($plugin_slug_arr[ 'woocommerce/woocommerce.php' ]); |
| 293 |
} |
| 294 |
if( $islms == '' ){ |
| 295 |
unset($should_activated[ 'sfwd-lms/sfwd_lms.php' ]); |
| 296 |
unset($plugin_slug_arr[ 'sfwd-lms/sfwd_lms.php' ]); |
| 297 |
} |
| 298 |
|
| 299 |
foreach ( $plugin_slug_arr as $slug => $is_silent ) { |
| 300 |
$data = activate_plugin( $slug, '', false, $is_silent ); |
| 301 |
if ( !is_wp_error( $data ) ) { |
| 302 |
$should_activated[ $slug ] = true; |
| 303 |
|
| 304 |
} |
| 305 |
} |
| 306 |
delete_transient( '_wc_activation_redirect' ); |
| 307 |
|
| 308 |
foreach ( $should_activated as $slug => $is_activated ) { |
| 309 |
if(!$is_activated) { |
| 310 |
$settings['success'] = false; |
| 311 |
return rest_ensure_response( $settings ); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
$settings['success'] = true; |
| 316 |
return rest_ensure_response( $settings ); |
| 317 |
} |
| 318 |
/** |
| 319 |
* Active required plugin based on slug |
| 320 |
* |
| 321 |
* @param $plugin_name |
| 322 |
* |
| 323 |
* @return WP_Error|WP_REST_Response |
| 324 |
* @since 1.0.0 |
| 325 |
*/ |
| 326 |
public function activate_mrm_plugins( \WP_REST_Request $request ) { |
| 327 |
if( !function_exists('activate_plugin') ) { |
| 328 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 329 |
} |
| 330 |
$plugins = isset( $request['plugins'] ) ? $request['plugins'] : []; |
| 331 |
$plugin_slug_arr = []; |
| 332 |
$should_activated = []; |
| 333 |
if( !empty($plugins) ){ |
| 334 |
foreach( $plugins as $plugin ){ |
| 335 |
if( 'mail-mint' == $plugin ) { |
| 336 |
$plugin_slug_arr['mail-mint/mail-mint.php'] = false; |
| 337 |
$should_activated['mail-mint/mail-mint.php'] = false; |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
foreach ( $plugin_slug_arr as $slug => $is_silent ) { |
| 342 |
$data = activate_plugin( $slug, '', false, $is_silent ); |
| 343 |
|
| 344 |
if ( !is_wp_error( $data ) ) { |
| 345 |
$should_activated[ $slug ] = true; |
| 346 |
} |
| 347 |
} |
| 348 |
delete_transient( '_wc_activation_redirect' ); |
| 349 |
foreach ( $should_activated as $slug => $is_activated ) { |
| 350 |
if(!$is_activated) { |
| 351 |
$settings['success'] = false; |
| 352 |
return rest_ensure_response( $settings ); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
$settings['success'] = true; |
| 357 |
return rest_ensure_response( $settings ); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
/** |
| 362 |
* Disable activation redirect |
| 363 |
*/ |
| 364 |
public function disable_wc_activation_redirect() { |
| 365 |
delete_transient( '_wc_activation_redirect' ); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
/** |
| 371 |
* Activate required plugins |
| 372 |
* |
| 373 |
* @param $plugin_name |
| 374 |
*/ |
| 375 |
public function active_plugin($plugin_slug,$permission = 1) { |
| 376 |
$plugin_slug_arr = array(); |
| 377 |
|
| 378 |
switch ($plugin_slug) { |
| 379 |
case 'elementor': |
| 380 |
$plugin_slug_arr = array( |
| 381 |
'elementor/elementor.php' => true, |
| 382 |
); |
| 383 |
break; |
| 384 |
case 'gutenberg': |
| 385 |
if($permission == 1){ |
| 386 |
$plugin_slug_arr = array( |
| 387 |
'qubely/qubely.php' => true, |
| 388 |
); |
| 389 |
} |
| 390 |
break; |
| 391 |
case 'divi-builder': |
| 392 |
$is_divi_installed = Wpfnl_functions::wpfnl_check_is_plugin_installed( $plugin_slug.'/divi-builder.php' ); |
| 393 |
$is_divi_theme_active = Wpfnl_functions::wpfnl_is_theme_active( 'Divi' ); |
| 394 |
if( $is_divi_installed ){ |
| 395 |
$plugin_slug_arr = array( |
| 396 |
'divi-builder/divi-builder.php' => true, |
| 397 |
); |
| 398 |
} |
| 399 |
break; |
| 400 |
|
| 401 |
} |
| 402 |
if($plugin_slug_arr) { |
| 403 |
foreach ( $plugin_slug_arr as $slug => $is_silent ) { |
| 404 |
$activate[ $slug ] = activate_plugin( $slug, '', false, $is_silent ); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
if(is_plugin_active( 'elementor/elementor.php' )){ |
| 409 |
Manager::create_default_kit(); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
/** |
| 415 |
* Validate a settings group_id against WPFunnels-owned option keys. |
| 416 |
* |
| 417 |
* The `group_id` path parameter is passed straight into get_option()/update_option(), |
| 418 |
* so it MUST be restricted to plugin-owned option names. Without this guard a request |
| 419 |
* could target arbitrary WordPress options (e.g. `wp_user_roles`) and escalate privileges. |
| 420 |
* |
| 421 |
* @param string $group_id Option key supplied via the route. |
| 422 |
* |
| 423 |
* @return bool True when the key is a WPFunnels-owned option, false otherwise. |
| 424 |
* @since 3.12.9 |
| 425 |
*/ |
| 426 |
protected function is_allowed_settings_group( $group_id ) { |
| 427 |
if ( ! is_string( $group_id ) || '' === $group_id ) { |
| 428 |
return false; |
| 429 |
} |
| 430 |
|
| 431 |
$allowed = array( |
| 432 |
'_wpfunnels_general_settings', |
| 433 |
'_wpfunnels_permalink_settings', |
| 434 |
'_wpfunnels_optin_settings', |
| 435 |
'_wpfunnels_advanced_settings', |
| 436 |
'_wpfunnels_integrations', |
| 437 |
'_wpfunnels_notification_settings', |
| 438 |
'_wpfunnels_lms_settings', |
| 439 |
'_wpfunnels_recaptcha_setting', |
| 440 |
'_wpfunnels_google_map_api_key', |
| 441 |
'_wpfunnels_user_roles', |
| 442 |
); |
| 443 |
|
| 444 |
if ( in_array( $group_id, $allowed, true ) ) { |
| 445 |
return true; |
| 446 |
} |
| 447 |
|
| 448 |
// Defense in depth: only allow plugin-prefixed option keys so add-ons that |
| 449 |
// register their own `_wpfunnels_*` settings groups keep working, while any |
| 450 |
// WordPress core / third-party option name is rejected. |
| 451 |
return 0 === strpos( $group_id, '_wpfunnels_' ); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
/** |
| 456 |
* Update a single setting in a group. |
| 457 |
* |
| 458 |
* @param WP_REST_Request $request Request data. |
| 459 |
* |
| 460 |
* @return WP_Error|WP_REST_Response |
| 461 |
* @since 1.0.0 |
| 462 |
*/ |
| 463 |
public function update_settings( $request ) { |
| 464 |
if( isset( $request['settings_id'], $request['group_id'] )){ |
| 465 |
if ( ! $this->is_allowed_settings_group( $request['group_id'] ) ) { |
| 466 |
return new WP_Error( 'rest_setting_group_invalid', __( 'Invalid setting group.', 'wpfnl' ), array( 'status' => 403 ) ); |
| 467 |
} |
| 468 |
if( $request['settings_id'] === 'permalink') { |
| 469 |
$default = Wpfnl_functions::get_permalink_settings(); |
| 470 |
$settings = Wpfnl_functions::get_admin_settings( '_wpfunnels_permalink_settings', $default); |
| 471 |
} elseif( $request['settings_id'] === 'optin') { |
| 472 |
$default = Wpfnl_functions::get_optin_settings(); |
| 473 |
$settings = Wpfnl_functions::get_admin_settings( '_wpfunnels_optin_settings', $default); |
| 474 |
} else { |
| 475 |
$default = Wpfnl_functions::get_general_settings(); |
| 476 |
$settings = Wpfnl_functions::get_admin_settings( $request['group_id'], $default ); |
| 477 |
} |
| 478 |
|
| 479 |
if ( !$settings ) { |
| 480 |
return new WP_Error( 'rest_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) ); |
| 481 |
} |
| 482 |
if ( empty( $settings ) ) { |
| 483 |
return new WP_Error( 'rest_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) ); |
| 484 |
} |
| 485 |
|
| 486 |
if($request['type'] !== 'ignore_activation' ) { |
| 487 |
$this->active_plugin($request['slug'],$request['permission']); |
| 488 |
} |
| 489 |
|
| 490 |
if($request['settings_id'] === 'permalink') { |
| 491 |
$settings['structure'] = $request['settings']; |
| 492 |
$settings['funnel_base'] = $request['funnelBase']; |
| 493 |
$settings['step_base'] = $request['stepBase']; |
| 494 |
Wpfnl_functions::update_admin_settings('_wpfunnels_permalink_saved', true); |
| 495 |
Wpfnl_functions::update_admin_settings($request['group_id'], $settings); |
| 496 |
|
| 497 |
}else { |
| 498 |
$settings[$request['settings_id']] = $request['value']; |
| 499 |
|
| 500 |
Wpfnl_functions::update_admin_settings($request['group_id'], $settings); |
| 501 |
|
| 502 |
} |
| 503 |
} |
| 504 |
$settings['success'] = true; |
| 505 |
$settings = $this->prepare_item_for_response( $settings, $request ); |
| 506 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY); |
| 507 |
delete_transient('wpfunnels_remote_template_data_' . WPFNL_VERSION); |
| 508 |
return rest_ensure_response( $settings ); |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
/** |
| 513 |
* Check lms or not |
| 514 |
*/ |
| 515 |
public function is_lms( $request ){ |
| 516 |
|
| 517 |
$response = [ |
| 518 |
'success' => false, |
| 519 |
'is_lms' => false, |
| 520 |
]; |
| 521 |
|
| 522 |
if( $request['funnel_id'] ){ |
| 523 |
$funnel_id = $request['funnel_id']; |
| 524 |
$type = get_post_meta( $funnel_id, '_wpfnl_funnel_type', true ); |
| 525 |
if( Wpfnl_functions::is_lms_addon_active() && $type == 'lms' ){ |
| 526 |
$response['success'] = true; |
| 527 |
$response['is_lms'] = true; |
| 528 |
}else{ |
| 529 |
$response['success'] = true; |
| 530 |
$response['is_lms'] = false; |
| 531 |
} |
| 532 |
} |
| 533 |
return rest_ensure_response( $response ); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
/** |
| 538 |
* Update funnel builder |
| 539 |
* |
| 540 |
* @param Array $request |
| 541 |
* |
| 542 |
* @return Array |
| 543 |
* @since 2.5.3 |
| 544 |
*/ |
| 545 |
public function update_builder_type( $request ){ |
| 546 |
|
| 547 |
$response = [ |
| 548 |
'success' => false, |
| 549 |
]; |
| 550 |
|
| 551 |
if( $request['builder'] ){ |
| 552 |
$builder = $request['builder']; |
| 553 |
$general_settings = get_option( '_wpfunnels_general_settings' ); |
| 554 |
$general_settings['builder'] = $builder; |
| 555 |
update_option('_wpfunnels_general_settings', $general_settings ); |
| 556 |
$response = [ |
| 557 |
'success' => true, |
| 558 |
]; |
| 559 |
} |
| 560 |
return rest_ensure_response( $response ); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
/** |
| 565 |
* Remember which canvas layout the current user last switched to. |
| 566 |
* |
| 567 |
* Stored per user rather than in the shared settings, so one admin flipping |
| 568 |
* the view switcher does not change the canvas for the rest of the team. The |
| 569 |
* site-wide default in Settings still applies to anyone who never switched. |
| 570 |
* |
| 571 |
* @param WP_REST_Request $request The REST request object. |
| 572 |
* |
| 573 |
* @return WP_REST_Response |
| 574 |
* @since 3.12.13 |
| 575 |
*/ |
| 576 |
public function update_canvas_view_mode( $request ) { |
| 577 |
$mode = isset( $request['mode'] ) ? sanitize_text_field( $request['mode'] ) : ''; |
| 578 |
$user_id = get_current_user_id(); |
| 579 |
|
| 580 |
if ( ! $user_id || ! in_array( $mode, Wpfnl_functions::get_funnel_builder_modes(), true ) ) { |
| 581 |
return rest_ensure_response( array( 'success' => false ) ); |
| 582 |
} |
| 583 |
|
| 584 |
update_user_meta( $user_id, Wpfnl_functions::CANVAS_VIEW_MODE_META_KEY, $mode ); |
| 585 |
|
| 586 |
if ( 'vertical' === $mode ) { |
| 587 |
do_action( 'wpfunnels_vertical_mode_used' ); |
| 588 |
} |
| 589 |
|
| 590 |
return rest_ensure_response( |
| 591 |
array( |
| 592 |
'success' => true, |
| 593 |
'mode' => $mode, |
| 594 |
) |
| 595 |
); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
/** |
| 600 |
* Activates plugins. |
| 601 |
* |
| 602 |
* @param WP_REST_Request $request The REST request object. |
| 603 |
*/ |
| 604 |
public function activate_plugins( $request ){ |
| 605 |
$response = [ |
| 606 |
'success' => false, |
| 607 |
]; |
| 608 |
|
| 609 |
if( !isset( $request['plugins'] )){ |
| 610 |
rest_ensure_response( $response ); |
| 611 |
} |
| 612 |
|
| 613 |
$plugins = $request['plugins']; |
| 614 |
$activatePluginInstance = new \WPFunnels\Admin\SetupWizard\ActivatePlugin( $plugins ); |
| 615 |
$response = $activatePluginInstance->activate_plugins(); |
| 616 |
return rest_ensure_response( $response ); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
/** |
| 621 |
* Save setup wizard settings |
| 622 |
* |
| 623 |
* @param WP_REST_Request $request The REST request object containing name and value. |
| 624 |
* |
| 625 |
* @return WP_REST_Response Response object with success status and message. |
| 626 |
* @since 2.5.20 |
| 627 |
*/ |
| 628 |
public function save_setup_wizard_settings( $request ){ |
| 629 |
$response = [ |
| 630 |
'success' => false, |
| 631 |
]; |
| 632 |
|
| 633 |
// Validate required parameters. |
| 634 |
if( !isset( $request['name'],$request['value'] )){ |
| 635 |
rest_ensure_response( $response ); |
| 636 |
} |
| 637 |
|
| 638 |
if( 'builder' === $request['name'] ){ |
| 639 |
// Get and process the value using the appropriate method. |
| 640 |
$name = sanitize_text_field($request['name']); |
| 641 |
$value = sanitize_text_field($request['value']); |
| 642 |
|
| 643 |
// Process the value based on the name. |
| 644 |
if( 'builder' === $name ){ |
| 645 |
$value = 'qubely' === $value ? 'gutenberg' : $value; |
| 646 |
} |
| 647 |
|
| 648 |
// Update the setting. |
| 649 |
$settings = Wpfnl_functions::get_general_settings(); |
| 650 |
$settings[$name] = $value; |
| 651 |
update_option('_wpfunnels_general_settings', $settings ); |
| 652 |
|
| 653 |
if( 'builder' === $name ){ |
| 654 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY.'_wc'); |
| 655 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY.'_lms'); |
| 656 |
delete_transient('wpfunnels_remote_template_data_wc_' . WPFNL_VERSION); |
| 657 |
delete_transient('wpfunnels_remote_template_data_lms_' . WPFNL_VERSION); |
| 658 |
delete_transient('wpfunnels_remote_template_data_lead_' . WPFNL_VERSION); |
| 659 |
delete_transient('wpfunnels_remote_template_data_store_checkout_' . WPFNL_VERSION); |
| 660 |
} |
| 661 |
|
| 662 |
$response = [ |
| 663 |
'success' => true, |
| 664 |
]; |
| 665 |
} |
| 666 |
return rest_ensure_response( $response ); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
/** |
| 671 |
* Create contact to Mail Mint. |
| 672 |
* |
| 673 |
* @param WP_REST_Request $request The REST request object. |
| 674 |
*/ |
| 675 |
public function create_contact( $request ){ |
| 676 |
$response = [ |
| 677 |
'success' => false, |
| 678 |
]; |
| 679 |
|
| 680 |
if( !isset( $request['email'] )){ |
| 681 |
rest_ensure_response( $response ); |
| 682 |
} |
| 683 |
|
| 684 |
$email = sanitize_text_field($request['email']); |
| 685 |
$name = isset($request['name']) ? sanitize_text_field($request['name']) : ''; |
| 686 |
$createContactInstance = new \WPFunnels\Admin\SetupWizard\CreateContact($email, $name ); |
| 687 |
$response = $createContactInstance->create_contact_via_webhook(); |
| 688 |
update_option( 'wpfunnels_activation_email', $email ); |
| 689 |
$createContactInstance->send_contact_to_appsero(); |
| 690 |
return rest_ensure_response( $response ); |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
/** |
| 696 |
* Get funnel type id |
| 697 |
*/ |
| 698 |
public function get_funnel_type_id( $request ){ |
| 699 |
|
| 700 |
$response = [ |
| 701 |
'success' => false, |
| 702 |
'type_id' => '', |
| 703 |
]; |
| 704 |
|
| 705 |
if( $request['funnel_id'] ){ |
| 706 |
$funnel_id = $request['funnel_id']; |
| 707 |
$type = get_post_meta( $funnel_id, '_wpfnl_funnel_type', true ); |
| 708 |
$term = get_term_by('slug', $type, 'template_type'); |
| 709 |
if( $term ){ |
| 710 |
$response['success'] = true; |
| 711 |
$response['type_id'] = $term->term_id; |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
return rest_ensure_response( $response ); |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
/** |
| 722 |
* Prepare a single setting object for response. |
| 723 |
* |
| 724 |
* @param object $item Setting object. |
| 725 |
* @param WP_REST_Request $request Request object. |
| 726 |
* |
| 727 |
* @return WP_REST_Response $response Response data. |
| 728 |
* @since 3.0.0 |
| 729 |
*/ |
| 730 |
public function prepare_item_for_response( $item, $request ) { |
| 731 |
$data = $this->add_additional_fields_to_object( $item, $request ); |
| 732 |
$response = rest_ensure_response( $data ); |
| 733 |
$response->add_links( $this->prepare_links( $request['settings_id'] ) ); |
| 734 |
return $response; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
} |
| 740 |
|