| 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-funnel-type' , array( |
| 156 |
array( |
| 157 |
'methods' => 'POST', |
| 158 |
'callback' => array( $this, 'update_funnel_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 . '/update-builder-type' , array( |
| 167 |
array( |
| 168 |
'methods' => 'POST', |
| 169 |
'callback' => array( $this, 'update_builder_type' ), |
| 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 . '/get-funnel-type-id/(?P<funnel_id>[\w-]+)' , array( |
| 178 |
array( |
| 179 |
'methods' => 'GET', |
| 180 |
'callback' => array( $this, 'get_funnel_type_id' ), |
| 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 |
$settings = Wpfnl_functions::get_admin_settings( $group_id ); |
| 202 |
if ( empty( $settings ) ) { |
| 203 |
return new WP_Error( 'rest_setting_setting_group_invalid', __( 'Invalid setting group.', 'wpfnl' ), array( 'status' => 404 ) ); |
| 204 |
} |
| 205 |
|
| 206 |
$filtered_settings = array(); |
| 207 |
foreach ($settings as $key => $setting) { |
| 208 |
$filtered_settings[] = array( |
| 209 |
'id' => $key, |
| 210 |
'value' => $setting |
| 211 |
); |
| 212 |
} |
| 213 |
return $filtered_settings; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
/** |
| 218 |
* Get setting data. |
| 219 |
* |
| 220 |
* @param string $group_id Group ID. |
| 221 |
* @param string $setting_id Setting ID. |
| 222 |
* |
| 223 |
* @return stdClass|WP_Error |
| 224 |
* @since 3.0.0 |
| 225 |
*/ |
| 226 |
public function get_setting( $group_id, $setting_id ) { |
| 227 |
if ( empty( $setting_id ) ) { |
| 228 |
return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) ); |
| 229 |
} |
| 230 |
|
| 231 |
$settings = $this->get_group_settings( $group_id ); |
| 232 |
|
| 233 |
if ( is_wp_error( $settings ) ) { |
| 234 |
return $settings; |
| 235 |
} |
| 236 |
|
| 237 |
$array_key = array_keys( wp_list_pluck( $settings, 'id' ), $setting_id ); |
| 238 |
|
| 239 |
if ( empty( $array_key ) ) { |
| 240 |
return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) ); |
| 241 |
} |
| 242 |
|
| 243 |
$setting = $settings[ $array_key[0] ]; |
| 244 |
return $setting; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
/** |
| 249 |
* Active required plugin based on slug |
| 250 |
* |
| 251 |
* @param $plugin_name |
| 252 |
* |
| 253 |
* @return WP_Error|WP_REST_Response |
| 254 |
* @since 1.0.0 |
| 255 |
*/ |
| 256 |
public function activate_wc_plugins( \WP_REST_Request $request ) { |
| 257 |
if( !function_exists('activate_plugin') ) { |
| 258 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
$permission = $request['permission']; |
| 264 |
$islms = $request['islms']; |
| 265 |
$isWc = $request['isWc']; |
| 266 |
|
| 267 |
$plugin_slug_arr = array( |
| 268 |
'woocommerce/woocommerce.php' => false, |
| 269 |
'cart-lift/cart-lift.php' => false, |
| 270 |
'sfwd-lms/sfwd_lms.php' => false, |
| 271 |
); |
| 272 |
$should_activated = array( |
| 273 |
'woocommerce/woocommerce.php' => false, |
| 274 |
'cart-lift/cart-lift.php' => false, |
| 275 |
'sfwd-lms/sfwd_lms.php' => false, |
| 276 |
); |
| 277 |
if($permission == '' ){ |
| 278 |
unset($should_activated[ 'cart-lift/cart-lift.php' ]); |
| 279 |
unset($plugin_slug_arr[ 'cart-lift/cart-lift.php' ]); |
| 280 |
} |
| 281 |
|
| 282 |
if($permission == '' ){ |
| 283 |
unset($should_activated[ 'cart-lift/cart-lift.php' ]); |
| 284 |
unset($plugin_slug_arr[ 'cart-lift/cart-lift.php' ]); |
| 285 |
} |
| 286 |
|
| 287 |
if( $isWc == 'no' ){ |
| 288 |
unset($should_activated[ 'woocommerce/woocommerce.php' ]); |
| 289 |
unset($plugin_slug_arr[ 'woocommerce/woocommerce.php' ]); |
| 290 |
} |
| 291 |
if( $islms == '' ){ |
| 292 |
unset($should_activated[ 'sfwd-lms/sfwd_lms.php' ]); |
| 293 |
unset($plugin_slug_arr[ 'sfwd-lms/sfwd_lms.php' ]); |
| 294 |
} |
| 295 |
|
| 296 |
foreach ( $plugin_slug_arr as $slug => $is_silent ) { |
| 297 |
$data = activate_plugin( $slug, '', false, $is_silent ); |
| 298 |
if ( !is_wp_error( $data ) ) { |
| 299 |
$should_activated[ $slug ] = true; |
| 300 |
|
| 301 |
} |
| 302 |
} |
| 303 |
delete_transient( '_wc_activation_redirect' ); |
| 304 |
|
| 305 |
foreach ( $should_activated as $slug => $is_activated ) { |
| 306 |
if(!$is_activated) { |
| 307 |
$settings['success'] = false; |
| 308 |
return rest_ensure_response( $settings ); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
$settings['success'] = true; |
| 313 |
return rest_ensure_response( $settings ); |
| 314 |
} |
| 315 |
/** |
| 316 |
* Active required plugin based on slug |
| 317 |
* |
| 318 |
* @param $plugin_name |
| 319 |
* |
| 320 |
* @return WP_Error|WP_REST_Response |
| 321 |
* @since 1.0.0 |
| 322 |
*/ |
| 323 |
public function activate_mrm_plugins( \WP_REST_Request $request ) { |
| 324 |
if( !function_exists('activate_plugin') ) { |
| 325 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 326 |
} |
| 327 |
$plugins = isset( $request['plugins'] ) ? $request['plugins'] : []; |
| 328 |
$plugin_slug_arr = []; |
| 329 |
$should_activated = []; |
| 330 |
if( !empty($plugins) ){ |
| 331 |
foreach( $plugins as $plugin ){ |
| 332 |
if( 'mail-mint' == $plugin ) { |
| 333 |
$plugin_slug_arr['mail-mint/mail-mint.php'] = false; |
| 334 |
$should_activated['mail-mint/mail-mint.php'] = false; |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
foreach ( $plugin_slug_arr as $slug => $is_silent ) { |
| 339 |
$data = activate_plugin( $slug, '', false, $is_silent ); |
| 340 |
|
| 341 |
if ( !is_wp_error( $data ) ) { |
| 342 |
$should_activated[ $slug ] = true; |
| 343 |
} |
| 344 |
} |
| 345 |
delete_transient( '_wc_activation_redirect' ); |
| 346 |
foreach ( $should_activated as $slug => $is_activated ) { |
| 347 |
if(!$is_activated) { |
| 348 |
$settings['success'] = false; |
| 349 |
return rest_ensure_response( $settings ); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
$settings['success'] = true; |
| 354 |
return rest_ensure_response( $settings ); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
/** |
| 359 |
* Disable activation redirect |
| 360 |
*/ |
| 361 |
public function disable_wc_activation_redirect() { |
| 362 |
delete_transient( '_wc_activation_redirect' ); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
/** |
| 368 |
* Activate required plugins |
| 369 |
* |
| 370 |
* @param $plugin_name |
| 371 |
*/ |
| 372 |
public function active_plugin($plugin_slug,$permission = 1) { |
| 373 |
$plugin_slug_arr = array(); |
| 374 |
|
| 375 |
switch ($plugin_slug) { |
| 376 |
case 'elementor': |
| 377 |
$plugin_slug_arr = array( |
| 378 |
'elementor/elementor.php' => true, |
| 379 |
); |
| 380 |
break; |
| 381 |
case 'gutenberg': |
| 382 |
if($permission == 1){ |
| 383 |
$plugin_slug_arr = array( |
| 384 |
'qubely/qubely.php' => true, |
| 385 |
); |
| 386 |
} |
| 387 |
break; |
| 388 |
case 'divi-builder': |
| 389 |
$is_divi_installed = Wpfnl_functions::wpfnl_check_is_plugin_installed( $plugin_slug.'/divi-builder.php' ); |
| 390 |
$is_divi_theme_active = Wpfnl_functions::wpfnl_is_theme_active( 'Divi' ); |
| 391 |
if( $is_divi_installed ){ |
| 392 |
$plugin_slug_arr = array( |
| 393 |
'divi-builder/divi-builder.php' => true, |
| 394 |
); |
| 395 |
} |
| 396 |
break; |
| 397 |
|
| 398 |
} |
| 399 |
if($plugin_slug_arr) { |
| 400 |
foreach ( $plugin_slug_arr as $slug => $is_silent ) { |
| 401 |
$activate[ $slug ] = activate_plugin( $slug, '', false, $is_silent ); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
if(is_plugin_active( 'elementor/elementor.php' )){ |
| 406 |
Manager::create_default_kit(); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
/** |
| 412 |
* Update a single setting in a group. |
| 413 |
* |
| 414 |
* @param WP_REST_Request $request Request data. |
| 415 |
* |
| 416 |
* @return WP_Error|WP_REST_Response |
| 417 |
* @since 1.0.0 |
| 418 |
*/ |
| 419 |
public function update_settings( $request ) { |
| 420 |
if( isset( $request['settings_id'], $request['group_id'] )){ |
| 421 |
if( $request['settings_id'] === 'permalink') { |
| 422 |
$default = Wpfnl_functions::get_permalink_settings(); |
| 423 |
$settings = Wpfnl_functions::get_admin_settings( '_wpfunnels_permalink_settings', $default); |
| 424 |
} elseif( $request['settings_id'] === 'optin') { |
| 425 |
$default = Wpfnl_functions::get_optin_settings(); |
| 426 |
$settings = Wpfnl_functions::get_admin_settings( '_wpfunnels_optin_settings', $default); |
| 427 |
} elseif ($request['settings_id'] === 'funnel_type') { |
| 428 |
$default = Wpfnl_functions::get_general_settings(); |
| 429 |
$settings = Wpfnl_functions::get_admin_settings( $request['group_id'], $default); |
| 430 |
} |
| 431 |
else { |
| 432 |
$default = Wpfnl_functions::get_general_settings(); |
| 433 |
$settings = Wpfnl_functions::get_admin_settings( $request['group_id'], $default ); |
| 434 |
} |
| 435 |
|
| 436 |
if ( !$settings ) { |
| 437 |
return new WP_Error( 'rest_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) ); |
| 438 |
} |
| 439 |
if ( empty( $settings ) ) { |
| 440 |
return new WP_Error( 'rest_setting_invalid', __( 'Invalid setting.', 'wpfnl' ), array( 'status' => 404 ) ); |
| 441 |
} |
| 442 |
|
| 443 |
if($request['type'] !== 'ignore_activation' ) { |
| 444 |
$this->active_plugin($request['slug'],$request['permission']); |
| 445 |
} |
| 446 |
|
| 447 |
if($request['settings_id'] === 'permalink') { |
| 448 |
$settings['structure'] = $request['settings']; |
| 449 |
$settings['funnel_base'] = $request['funnelBase']; |
| 450 |
$settings['step_base'] = $request['stepBase']; |
| 451 |
Wpfnl_functions::update_admin_settings('_wpfunnels_permalink_saved', true); |
| 452 |
Wpfnl_functions::update_admin_settings($request['group_id'], $settings); |
| 453 |
|
| 454 |
}else { |
| 455 |
if( 'funnel_type' === $request['settings_id'] ) { |
| 456 |
$general_settings = get_option( '_wpfunnels_general_settings' ); |
| 457 |
$settings[$request['settings_id']] = isset($general_settings['funnel_type']) ? $general_settings['funnel_type'] : 'sales'; |
| 458 |
}else{ |
| 459 |
$settings[$request['settings_id']] = $request['value']; |
| 460 |
} |
| 461 |
|
| 462 |
Wpfnl_functions::update_admin_settings($request['group_id'], $settings); |
| 463 |
|
| 464 |
} |
| 465 |
} |
| 466 |
$settings['success'] = true; |
| 467 |
$settings = $this->prepare_item_for_response( $settings, $request ); |
| 468 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY); |
| 469 |
delete_transient('wpfunnels_remote_template_data_' . WPFNL_VERSION); |
| 470 |
return rest_ensure_response( $settings ); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
/** |
| 475 |
* Check lms or not |
| 476 |
*/ |
| 477 |
public function is_lms( $request ){ |
| 478 |
|
| 479 |
$response = [ |
| 480 |
'success' => false, |
| 481 |
'is_lms' => false, |
| 482 |
]; |
| 483 |
|
| 484 |
if( $request['funnel_id'] ){ |
| 485 |
$funnel_id = $request['funnel_id']; |
| 486 |
$type = get_post_meta( $funnel_id, '_wpfnl_funnel_type', true ); |
| 487 |
if( Wpfnl_functions::is_lms_addon_active() && $type == 'lms' ){ |
| 488 |
$response['success'] = true; |
| 489 |
$response['is_lms'] = true; |
| 490 |
}else{ |
| 491 |
$response['success'] = true; |
| 492 |
$response['is_lms'] = false; |
| 493 |
} |
| 494 |
} |
| 495 |
return rest_ensure_response( $response ); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
/** |
| 500 |
* Update funnel type |
| 501 |
* |
| 502 |
* @param Array $request |
| 503 |
* |
| 504 |
* @return Array |
| 505 |
* @since 2.5.3 |
| 506 |
*/ |
| 507 |
public function update_funnel_type( $request ){ |
| 508 |
|
| 509 |
$response = [ |
| 510 |
'success' => false, |
| 511 |
]; |
| 512 |
|
| 513 |
if( $request['type'] ){ |
| 514 |
$type = $request['type']; |
| 515 |
$general_settings = get_option( '_wpfunnels_general_settings' ); |
| 516 |
$general_settings['funnel_type'] = $type; |
| 517 |
update_option('_wpfunnels_general_settings', $general_settings ); |
| 518 |
$response = [ |
| 519 |
'success' => true, |
| 520 |
]; |
| 521 |
} |
| 522 |
return rest_ensure_response( $response ); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
/** |
| 527 |
* Update funnel builder |
| 528 |
* |
| 529 |
* @param Array $request |
| 530 |
* |
| 531 |
* @return Array |
| 532 |
* @since 2.5.3 |
| 533 |
*/ |
| 534 |
public function update_builder_type( $request ){ |
| 535 |
|
| 536 |
$response = [ |
| 537 |
'success' => false, |
| 538 |
]; |
| 539 |
|
| 540 |
if( $request['builder'] ){ |
| 541 |
$builder = $request['builder']; |
| 542 |
$general_settings = get_option( '_wpfunnels_general_settings' ); |
| 543 |
$general_settings['builder'] = $builder; |
| 544 |
update_option('_wpfunnels_general_settings', $general_settings ); |
| 545 |
$response = [ |
| 546 |
'success' => true, |
| 547 |
]; |
| 548 |
} |
| 549 |
return rest_ensure_response( $response ); |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
/** |
| 554 |
* Activates plugins. |
| 555 |
* |
| 556 |
* @param WP_REST_Request $request The REST request object. |
| 557 |
*/ |
| 558 |
public function activate_plugins( $request ){ |
| 559 |
$response = [ |
| 560 |
'success' => false, |
| 561 |
]; |
| 562 |
|
| 563 |
if( !isset( $request['plugins'] )){ |
| 564 |
rest_ensure_response( $response ); |
| 565 |
} |
| 566 |
|
| 567 |
$plugins = $request['plugins']; |
| 568 |
$activatePluginInstance = new \WPFunnels\Admin\SetupWizard\ActivatePlugin( $plugins ); |
| 569 |
$response = $activatePluginInstance->activate_plugins(); |
| 570 |
return rest_ensure_response( $response ); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
/** |
| 575 |
* Save setup wizard settings |
| 576 |
* |
| 577 |
* @param WP_REST_Request $request The REST request object containing name and value. |
| 578 |
* |
| 579 |
* @return WP_REST_Response Response object with success status and message. |
| 580 |
* @since 2.5.20 |
| 581 |
*/ |
| 582 |
public function save_setup_wizard_settings( $request ){ |
| 583 |
$response = [ |
| 584 |
'success' => false, |
| 585 |
]; |
| 586 |
|
| 587 |
// Validate required parameters. |
| 588 |
if( !isset( $request['name'],$request['value'] )){ |
| 589 |
rest_ensure_response( $response ); |
| 590 |
} |
| 591 |
|
| 592 |
if( 'builder' === $request['name'] || 'funnel_type' === $request['name'] ){ |
| 593 |
// Get and process the value using the appropriate method. |
| 594 |
$name = sanitize_text_field($request['name']); |
| 595 |
$value = sanitize_text_field($request['value']); |
| 596 |
|
| 597 |
// Process the value based on the name. |
| 598 |
if( 'builder' === $name ){ |
| 599 |
$value = 'qubely' === $value ? 'gutenberg' : $value; |
| 600 |
} |
| 601 |
|
| 602 |
// Update the setting. |
| 603 |
$settings = Wpfnl_functions::get_general_settings(); |
| 604 |
$settings[$name] = $value; |
| 605 |
update_option('_wpfunnels_general_settings', $settings ); |
| 606 |
|
| 607 |
if( 'builder' === $name ){ |
| 608 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY.'_wc'); |
| 609 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY.'_lms'); |
| 610 |
delete_transient('wpfunnels_remote_template_data_wc_' . WPFNL_VERSION); |
| 611 |
delete_transient('wpfunnels_remote_template_data_lms_' . WPFNL_VERSION); |
| 612 |
delete_transient('wpfunnels_remote_template_data_lead_' . WPFNL_VERSION); |
| 613 |
delete_transient('wpfunnels_remote_template_data_store_checkout_' . WPFNL_VERSION); |
| 614 |
} |
| 615 |
|
| 616 |
$response = [ |
| 617 |
'success' => true, |
| 618 |
]; |
| 619 |
} |
| 620 |
return rest_ensure_response( $response ); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
/** |
| 625 |
* Create contact to Mail Mint. |
| 626 |
* |
| 627 |
* @param WP_REST_Request $request The REST request object. |
| 628 |
*/ |
| 629 |
public function create_contact( $request ){ |
| 630 |
$response = [ |
| 631 |
'success' => false, |
| 632 |
]; |
| 633 |
|
| 634 |
if( !isset( $request['email'] )){ |
| 635 |
rest_ensure_response( $response ); |
| 636 |
} |
| 637 |
|
| 638 |
$email = sanitize_text_field($request['email']); |
| 639 |
$name = isset($request['name']) ? sanitize_text_field($request['name']) : ''; |
| 640 |
$createContactInstance = new \WPFunnels\Admin\SetupWizard\CreateContact($email, $name ); |
| 641 |
$response = $createContactInstance->create_contact_via_webhook(); |
| 642 |
update_option( 'wpfunnels_activation_email', $email ); |
| 643 |
$createContactInstance->send_contact_to_appsero(); |
| 644 |
return rest_ensure_response( $response ); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
/** |
| 650 |
* Get funnel type id |
| 651 |
*/ |
| 652 |
public function get_funnel_type_id( $request ){ |
| 653 |
|
| 654 |
$response = [ |
| 655 |
'success' => false, |
| 656 |
'type_id' => '', |
| 657 |
]; |
| 658 |
|
| 659 |
if( $request['funnel_id'] ){ |
| 660 |
$funnel_id = $request['funnel_id']; |
| 661 |
$type = get_post_meta( $funnel_id, '_wpfnl_funnel_type', true ); |
| 662 |
$term = get_term_by('slug', $type, 'template_type'); |
| 663 |
if( $term ){ |
| 664 |
$response['success'] = true; |
| 665 |
$response['type_id'] = $term->term_id; |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
return rest_ensure_response( $response ); |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
/** |
| 676 |
* Prepare a single setting object for response. |
| 677 |
* |
| 678 |
* @param object $item Setting object. |
| 679 |
* @param WP_REST_Request $request Request object. |
| 680 |
* |
| 681 |
* @return WP_REST_Response $response Response data. |
| 682 |
* @since 3.0.0 |
| 683 |
*/ |
| 684 |
public function prepare_item_for_response( $item, $request ) { |
| 685 |
$data = $this->add_additional_fields_to_object( $item, $request ); |
| 686 |
$response = rest_ensure_response( $data ); |
| 687 |
$response->add_links( $this->prepare_links( $request['settings_id'] ) ); |
| 688 |
return $response; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
} |
| 694 |
|