| 1 |
<?php |
| 2 |
/** |
| 3 |
* Settings module |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPFunnels\Modules\Admin\Settings; |
| 9 |
|
| 10 |
use WPFunnels\Admin\Module\Wpfnl_Admin_Module; |
| 11 |
use WPFunnels\Rollback; |
| 12 |
use WPFunnels\Traits\SingletonTrait; |
| 13 |
use WPFunnels\Wpfnl_functions; |
| 14 |
|
| 15 |
class Module extends Wpfnl_Admin_Module |
| 16 |
{ |
| 17 |
use SingletonTrait; |
| 18 |
|
| 19 |
protected $validations; |
| 20 |
|
| 21 |
protected $prefix = '_wpfunnels_'; |
| 22 |
|
| 23 |
protected $general_settings; |
| 24 |
|
| 25 |
protected $permalink_settings; |
| 26 |
|
| 27 |
protected $offer_settings; |
| 28 |
|
| 29 |
protected $user_roles; |
| 30 |
|
| 31 |
protected $gtm_events; |
| 32 |
|
| 33 |
protected $gtm_settings; |
| 34 |
|
| 35 |
protected $facebook_pixel_events; |
| 36 |
|
| 37 |
protected $facebook_pixel_settings; |
| 38 |
|
| 39 |
protected $recaptcha_settings; |
| 40 |
|
| 41 |
protected $utm_params; |
| 42 |
|
| 43 |
protected $utm_settings; |
| 44 |
|
| 45 |
protected $is_allow_sales; |
| 46 |
|
| 47 |
protected $settings_meta_keys = [ |
| 48 |
'_wpfunnels_funnel_type' => 'sales', |
| 49 |
'_wpfunnels_builder' => 'elementor', |
| 50 |
'_wpfunnels_paypal_reference' => '', |
| 51 |
'_wpfunnels_order_bump' => '', |
| 52 |
'_wpfunnels_ab_testing' => '', |
| 53 |
'_wpfunnels_allow_funnels' => [ |
| 54 |
'administrator' => true, |
| 55 |
], |
| 56 |
'_wpfunnels_permalink_settings' => '', |
| 57 |
'_wpfunnels_permalink_step_base' => 'wpfunnels', |
| 58 |
'_wpfunnels_permalink_flow_base' => 'step', |
| 59 |
'_wpfunnels_set_permalink' => 'step', |
| 60 |
]; |
| 61 |
|
| 62 |
public function __construct() |
| 63 |
{ |
| 64 |
add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); |
| 65 |
add_action('admin_init', [$this, 'after_permalink_settings_saved']); |
| 66 |
add_action( 'admin_post_wpfunnels_rollback', [ $this, 'post_wpfunnels_rollback' ] ); |
| 67 |
$this->init_ajax(); |
| 68 |
} |
| 69 |
|
| 70 |
public function is_wc_installed() |
| 71 |
{ |
| 72 |
$path = 'woocommerce/woocommerce.php'; |
| 73 |
$plugins = get_plugins(); |
| 74 |
|
| 75 |
return isset($plugins[ $path ]); |
| 76 |
} |
| 77 |
|
| 78 |
public function is_ff_installed() |
| 79 |
{ |
| 80 |
$path = 'fluentform/fluentform.php'; |
| 81 |
$plugins = get_plugins(); |
| 82 |
|
| 83 |
return isset($plugins[ $path ]); |
| 84 |
} |
| 85 |
|
| 86 |
public function is_elementor_installed() |
| 87 |
{ |
| 88 |
$path = 'elementor/elementor.php'; |
| 89 |
$plugins = get_plugins(); |
| 90 |
|
| 91 |
return isset($plugins[ $path ]); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
public function enqueue_scripts() |
| 96 |
{ |
| 97 |
wp_enqueue_script('settings', plugin_dir_url(__FILE__) . 'js/settings.js', ['jquery'], WPFNL_VERSION, true); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
public function get_view() |
| 102 |
{ |
| 103 |
$this->init_settings(); |
| 104 |
$is_pro_activated = Wpfnl_functions::is_wpfnl_pro_activated(); |
| 105 |
$global_funnel_type = Wpfnl_functions::get_global_funnel_type(); |
| 106 |
require_once WPFNL_DIR . '/admin/modules/settings/views/view.php'; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Init ajax hooks for |
| 111 |
* saving metas |
| 112 |
* |
| 113 |
* @since 1.0.0 |
| 114 |
*/ |
| 115 |
public function init_ajax() |
| 116 |
{ |
| 117 |
$this->validations = [ |
| 118 |
'logged_in' => true, |
| 119 |
'user_can' => 'manage_options', |
| 120 |
]; |
| 121 |
wp_ajax_helper()->handle('update-general-settings') |
| 122 |
->with_callback([ $this, 'update_general_settings' ]) |
| 123 |
->with_validation($this->validations); |
| 124 |
|
| 125 |
wp_ajax_helper()->handle('clear-templates') |
| 126 |
->with_callback([ $this, 'clear_templates_data' ]) |
| 127 |
->with_validation($this->validations); |
| 128 |
|
| 129 |
wp_ajax_helper()->handle('clear-transient') |
| 130 |
->with_callback([ $this, 'clear_transient_cache_data' ]) |
| 131 |
->with_validation($this->validations); |
| 132 |
|
| 133 |
wp_ajax_helper()->handle('wpfnl-show-log') |
| 134 |
->with_callback([ $this, 'wpfnl_show_log' ]) |
| 135 |
->with_validation($this->validations); |
| 136 |
|
| 137 |
wp_ajax_helper()->handle('wpfnl-delete-log') |
| 138 |
->with_callback([ $this, 'wpfnl_delete_log' ]) |
| 139 |
->with_validation($this->validations); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* Update handler for settings |
| 145 |
* page |
| 146 |
* |
| 147 |
* @param $payload |
| 148 |
* |
| 149 |
* @return array |
| 150 |
* @since 1.0.0 |
| 151 |
*/ |
| 152 |
public function update_general_settings($payload) |
| 153 |
{ |
| 154 |
do_action('wpfunnels/before_settings_saved', $payload); |
| 155 |
|
| 156 |
$general_settings = [ |
| 157 |
'funnel_type' => $payload['funnel_type'], |
| 158 |
'builder' => $payload['builder'], |
| 159 |
'disable_analytics' => isset($payload['analytics_roles']) ? $payload['analytics_roles'] : '', |
| 160 |
'allow_funnels' => isset($payload['permission_role']) ? $payload['permission_role'] : [], |
| 161 |
'paypal_reference' => $payload['paypal_reference'], |
| 162 |
'order_bump' => $payload['order_bump'], |
| 163 |
'ab_testing' => $payload['ab_testing'], |
| 164 |
'disable_theme_style' => $payload['disable_theme_style'], |
| 165 |
'enable_log_status' => $payload['enable_log_status'], |
| 166 |
]; |
| 167 |
|
| 168 |
$permalink_settings = [ |
| 169 |
'structure' => $payload['permalink_settings'], |
| 170 |
'step_base' => $payload['permalink_step_base'], |
| 171 |
'funnel_base' => $payload['permalink_funnel_base'], |
| 172 |
]; |
| 173 |
foreach ($payload as $key => $value) { |
| 174 |
|
| 175 |
switch ($key) { |
| 176 |
case 'funnel_type': |
| 177 |
case 'builder': |
| 178 |
$cache_key = 'wpfunnels_remote_template_data_' . WPFNL_VERSION; |
| 179 |
delete_transient($cache_key); |
| 180 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY); |
| 181 |
break; |
| 182 |
case 'permalink_settings': |
| 183 |
Wpfnl_functions::update_admin_settings($this->prefix.'permalink_saved', true); |
| 184 |
break; |
| 185 |
case 'advanced_settings': |
| 186 |
Wpfnl_functions::update_admin_settings($this->prefix.'advanced_settings', $value); |
| 187 |
break; |
| 188 |
default: |
| 189 |
break; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
Wpfnl_functions::update_admin_settings($this->prefix.'general_settings', $general_settings); |
| 194 |
Wpfnl_functions::update_admin_settings($this->prefix.'permalink_settings', $permalink_settings); |
| 195 |
$this->save_recaptcha_settings($payload); |
| 196 |
|
| 197 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY.'_74'); |
| 198 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY.'_73'); |
| 199 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY.'_52'); |
| 200 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY.'_53'); |
| 201 |
|
| 202 |
delete_transient('wpfunnels_remote_template_data_74_' . WPFNL_VERSION); |
| 203 |
delete_transient('wpfunnels_remote_template_data_73_' . WPFNL_VERSION); |
| 204 |
delete_transient('wpfunnels_remote_template_data_52_' . WPFNL_VERSION); |
| 205 |
delete_transient('wpfunnels_remote_template_data_53_' . WPFNL_VERSION); |
| 206 |
|
| 207 |
do_action('wpfunnels/after_settings_saved', $payload ); |
| 208 |
return [ |
| 209 |
'success' => true |
| 210 |
]; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Save Facebook pixel settings |
| 215 |
* |
| 216 |
* @param $settings |
| 217 |
* |
| 218 |
* @since 1.0.0 |
| 219 |
*/ |
| 220 |
public function save_recaptcha_settings( $settings ) { |
| 221 |
$enable_recapcha = isset($settings['enable_recaptcha']) ? $settings['enable_recaptcha'] : 'no'; |
| 222 |
$recapcha_site_key = isset($settings['recaptcha_site_key']) ? $settings['recaptcha_site_key'] : ''; |
| 223 |
$recapcha_site_secret = isset($settings['recaptcha_site_secret']) ? $settings['recaptcha_site_secret'] : ''; |
| 224 |
$recaptcha_settings = array( |
| 225 |
'enable_recaptcha' => $enable_recapcha, |
| 226 |
'recaptcha_site_key' => $recapcha_site_key, |
| 227 |
'recaptcha_site_secret' => $recapcha_site_secret |
| 228 |
); |
| 229 |
Wpfnl_functions::update_admin_settings('_wpfunnels_recaptcha_setting', $recaptcha_settings ); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
/** |
| 234 |
* Initialize all the settings value |
| 235 |
* |
| 236 |
* @since 1.0.0 |
| 237 |
*/ |
| 238 |
public function init_settings() |
| 239 |
{ |
| 240 |
|
| 241 |
$this->general_settings = Wpfnl_functions::get_general_settings(); |
| 242 |
|
| 243 |
$this->is_allow_sales = Wpfnl_functions::maybe_allow_sales_funnel(); |
| 244 |
$this->permalink_settings = Wpfnl_functions::get_permalink_settings(); |
| 245 |
$this->offer_settings = Wpfnl_functions::get_offer_settings(); |
| 246 |
$this->user_roles = Wpfnl_functions::get_user_roles(); |
| 247 |
$this->gtm_events = Wpfnl_functions::get_gtm_events(); |
| 248 |
$this->gtm_settings = Wpfnl_functions::get_gtm_settings(); |
| 249 |
$this->facebook_pixel_events = Wpfnl_functions::get_facebook_events(); |
| 250 |
$this->facebook_pixel_settings = Wpfnl_functions::get_facebook_pixel_settings(); |
| 251 |
$this->utm_params = Wpfnl_functions::get_utm_params(); |
| 252 |
$this->utm_settings = Wpfnl_functions::get_utm_settings(); |
| 253 |
$this->utm_settings = Wpfnl_functions::get_utm_settings(); |
| 254 |
$this->recaptcha_settings = Wpfnl_functions::get_recaptcha_settings(); |
| 255 |
|
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* Clear saved templates data |
| 264 |
* |
| 265 |
* @param $payload |
| 266 |
* |
| 267 |
* @return array |
| 268 |
* @since 1.0.0 |
| 269 |
*/ |
| 270 |
public function clear_templates_data($payload) { |
| 271 |
|
| 272 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY.'_wc'); |
| 273 |
delete_option(WPFNL_TEMPLATES_OPTION_KEY.'_lms'); |
| 274 |
|
| 275 |
|
| 276 |
delete_transient('wpfunnels_remote_template_data_wc_' . WPFNL_VERSION); |
| 277 |
delete_transient('wpfunnels_remote_template_data_lms_' . WPFNL_VERSION); |
| 278 |
delete_transient('wpfunnels_remote_template_data_lead_' . WPFNL_VERSION); |
| 279 |
return [ |
| 280 |
'success' => true |
| 281 |
]; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
/** |
| 286 |
* Clear transient |
| 287 |
* |
| 288 |
* @param $payload |
| 289 |
* |
| 290 |
* @return array |
| 291 |
*/ |
| 292 |
public function clear_transient_cache_data($payload) { |
| 293 |
|
| 294 |
delete_transient('wpfunnels_remote_template_data_' . WPFNL_VERSION); |
| 295 |
delete_transient('wpfunnels_remote_template_data_wc_' . WPFNL_VERSION); |
| 296 |
delete_transient('wpfunnels_remote_template_data_lms_' . WPFNL_VERSION); |
| 297 |
delete_transient('wpfunnels_remote_template_data_lead_' . WPFNL_VERSION); |
| 298 |
delete_transient('wpfunnels_rollback_versions_' . WPFNL_VERSION); |
| 299 |
do_action('wpfunnels/after_clear_transient'); |
| 300 |
return array( |
| 301 |
'success' => true |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
/** |
| 307 |
* After settings saved hooks |
| 308 |
* |
| 309 |
* @since 1.0.0 |
| 310 |
*/ |
| 311 |
public function after_permalink_settings_saved() |
| 312 |
{ |
| 313 |
$is_permalink_saved = get_option('_wpfunnels_permalink_saved'); |
| 314 |
if ($is_permalink_saved) { |
| 315 |
flush_rewrite_rules(); |
| 316 |
delete_option('_wpfunnels_permalink_saved'); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
/** |
| 322 |
* Get settings by meta key |
| 323 |
* |
| 324 |
* @param $key |
| 325 |
* |
| 326 |
* @return mixed|string |
| 327 |
* @since 1.0.0 |
| 328 |
*/ |
| 329 |
public function get_settings_by_key($key) |
| 330 |
{ |
| 331 |
return isset($this->settings_meta_keys[$key]) ? $this->settings_meta_keys[$key]: ''; |
| 332 |
} |
| 333 |
|
| 334 |
public function get_name() |
| 335 |
{ |
| 336 |
return __('settings','wpfnl'); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
/** |
| 341 |
* Get rollback version of WPF |
| 342 |
* |
| 343 |
* @return array|mixed |
| 344 |
* @since 2.3.0 |
| 345 |
*/ |
| 346 |
public function get_roll_back_versions() { |
| 347 |
$rollback_versions = get_transient( 'wpfunnels_rollback_versions_' . WPFNL_VERSION ); |
| 348 |
if ( false === $rollback_versions ) { |
| 349 |
$max_versions = 10; |
| 350 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 351 |
$plugin_information = plugins_api( |
| 352 |
'plugin_information', [ |
| 353 |
'slug' => 'wpfunnels', |
| 354 |
] |
| 355 |
); |
| 356 |
if ( empty( $plugin_information->versions ) || ! is_array( $plugin_information->versions ) ) { |
| 357 |
return []; |
| 358 |
} |
| 359 |
|
| 360 |
natsort( $plugin_information->versions ); |
| 361 |
$plugin_information->versions = array_reverse($plugin_information->versions); |
| 362 |
$rollback_versions = []; |
| 363 |
|
| 364 |
$current_index = 0; |
| 365 |
foreach ( $plugin_information->versions as $version => $download_link ) { |
| 366 |
if ( $max_versions <= $current_index ) { |
| 367 |
break; |
| 368 |
} |
| 369 |
|
| 370 |
$lowercase_version = strtolower( $version ); |
| 371 |
$is_valid_rollback_version = ! preg_match( '/(trunk|beta|rc|dev)/i', $lowercase_version ); |
| 372 |
|
| 373 |
/** |
| 374 |
* Is rollback version is valid. |
| 375 |
* |
| 376 |
* Filters the check whether the rollback version is valid. |
| 377 |
* |
| 378 |
* @param bool $is_valid_rollback_version Whether the rollback version is valid. |
| 379 |
*/ |
| 380 |
$is_valid_rollback_version = apply_filters( |
| 381 |
'wpfunnels/is_valid_rollback_version', |
| 382 |
$is_valid_rollback_version, |
| 383 |
$lowercase_version |
| 384 |
); |
| 385 |
|
| 386 |
if ( ! $is_valid_rollback_version ) { |
| 387 |
continue; |
| 388 |
} |
| 389 |
|
| 390 |
if ( version_compare( $version, WPFNL_VERSION, '>=' ) ) { |
| 391 |
continue; |
| 392 |
} |
| 393 |
|
| 394 |
$current_index++; |
| 395 |
$rollback_versions[] = $version; |
| 396 |
} |
| 397 |
|
| 398 |
set_transient( 'wpfunnels_rollback_versions_' . WPFNL_VERSION, $rollback_versions, DAY_IN_SECONDS ); |
| 399 |
} |
| 400 |
|
| 401 |
return $rollback_versions; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
public function post_wpfunnels_rollback() { |
| 406 |
check_admin_referer( 'wpfunnels_rollback' ); |
| 407 |
|
| 408 |
$rollback_versions = $this->get_roll_back_versions(); |
| 409 |
if ( empty( $_GET['version'] ) || ! in_array( $_GET['version'], $rollback_versions ) ) { |
| 410 |
wp_die( esc_html__( 'Error occurred, The version selected is invalid. Try selecting different version.', 'wpfnl' ) ); |
| 411 |
} |
| 412 |
|
| 413 |
$plugin_slug = basename( 'wpfunnels', '.php' ); |
| 414 |
|
| 415 |
$rollback = new Rollback( |
| 416 |
[ |
| 417 |
'version' => $_GET['version'], |
| 418 |
'plugin_name' => WPFNL_BASE, |
| 419 |
'plugin_slug' => $plugin_slug, |
| 420 |
'package_url' => sprintf( 'https://downloads.wordpress.org/plugin/%1s.%2s.zip', $plugin_slug, $_GET['version'] ), |
| 421 |
] |
| 422 |
); |
| 423 |
|
| 424 |
$rollback->run(); |
| 425 |
|
| 426 |
wp_die( |
| 427 |
'', esc_html__( 'Rollback to Previous Version', 'wpfnl' ), [ |
| 428 |
'response' => 200, |
| 429 |
] |
| 430 |
); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
/** |
| 435 |
* WPFunnels log |
| 436 |
* |
| 437 |
* @param $payload |
| 438 |
* |
| 439 |
* @return array |
| 440 |
*/ |
| 441 |
public static function wpfnl_show_log( $payload ) { |
| 442 |
if( isset($payload['logKey']) ){ |
| 443 |
$key = $payload['logKey']; |
| 444 |
$upload_dir = wp_upload_dir( null, false ); |
| 445 |
$log_url = $upload_dir['basedir'].'/wpfunnels-logs/'; |
| 446 |
$file_url = $log_url . $key; |
| 447 |
|
| 448 |
ob_start(); |
| 449 |
include_once $file_url; |
| 450 |
$out = ob_get_clean(); |
| 451 |
ob_end_clean(); |
| 452 |
|
| 453 |
return array( |
| 454 |
'success' => true, |
| 455 |
'content' => $out, |
| 456 |
'file_url' => $log_url. $key |
| 457 |
); |
| 458 |
} |
| 459 |
|
| 460 |
return array( |
| 461 |
'success' => false, |
| 462 |
'content' => '', |
| 463 |
'file_url' => '' |
| 464 |
); |
| 465 |
|
| 466 |
|
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
/** |
| 471 |
* WPFunnels log |
| 472 |
* |
| 473 |
* @param $payload |
| 474 |
* |
| 475 |
* @return array |
| 476 |
*/ |
| 477 |
public static function wpfnl_delete_log( $payload ) { |
| 478 |
if( isset($payload['logKey']) ){ |
| 479 |
$key = $payload['logKey']; |
| 480 |
$upload_dir = wp_upload_dir( null, false ); |
| 481 |
$file_name = $upload_dir['basedir'].'/wpfunnels-logs/'.$key; |
| 482 |
|
| 483 |
$response = \Wpfnl_Logger::delete_log_file( $file_name ); |
| 484 |
if( $response ){ |
| 485 |
return array( |
| 486 |
'success' => true, |
| 487 |
); |
| 488 |
} |
| 489 |
|
| 490 |
} |
| 491 |
|
| 492 |
return array( |
| 493 |
'success' => false, |
| 494 |
'content' => '', |
| 495 |
'file_url' => '' |
| 496 |
); |
| 497 |
|
| 498 |
|
| 499 |
} |
| 500 |
} |
| 501 |
|