| 1 |
<?php |
| 2 |
/** |
| 3 |
* Declare class Setting |
| 4 |
* |
| 5 |
* @package Setting |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace LassoLite\Classes; |
| 9 |
|
| 10 |
use LassoLite\Admin\Constant; |
| 11 |
use LassoLite\Models\Model; |
| 12 |
use LassoLiteVendor\Firebase\JWT\JWT; |
| 13 |
|
| 14 |
/** |
| 15 |
* Setting |
| 16 |
*/ |
| 17 |
class Setting { |
| 18 |
const DISPLAY_TYPE_SINGLE = 'Single'; |
| 19 |
const DISPLAY_TYPE_GRID = 'Grid'; |
| 20 |
const DISPLAY_TYPE_LIST = 'List'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Convert WP GET parameters to Lasso GET |
| 24 |
* |
| 25 |
* @var array|string $get |
| 26 |
*/ |
| 27 |
public $get = array(); |
| 28 |
|
| 29 |
/** |
| 30 |
* Current page |
| 31 |
* |
| 32 |
* @var mixed|string |
| 33 |
*/ |
| 34 |
public $current_page = ''; |
| 35 |
|
| 36 |
/** |
| 37 |
* List of plugins |
| 38 |
* |
| 39 |
* @var string $import_sources |
| 40 |
*/ |
| 41 |
public static $import_sources = array( |
| 42 |
'earnist/earnist.php', |
| 43 |
'thirstyaffiliates/thirstyaffiliates.php', |
| 44 |
'pretty-link/pretty-link.php', |
| 45 |
'affiliate-link-automation/affiliate_automation.php', |
| 46 |
'aawp/aawp.php', |
| 47 |
'easyazon/easyazon.php', |
| 48 |
'amalinkspro/amalinkspro.php', |
| 49 |
'easy-affiliate-links/easy-affiliate-links.php', |
| 50 |
); |
| 51 |
|
| 52 |
/** |
| 53 |
* Settings general page |
| 54 |
* |
| 55 |
* @var string $settings_general_page |
| 56 |
*/ |
| 57 |
public $settings_general_page = 'settings-general'; |
| 58 |
|
| 59 |
/** |
| 60 |
* Setting constructor. |
| 61 |
*/ |
| 62 |
public function __construct() { |
| 63 |
$this->get = Helper::GET(); |
| 64 |
$this->current_page = $this->get['page'] ?? ''; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Check whether current page is SURLS page or not. |
| 69 |
*/ |
| 70 |
public function is_surls_page() { |
| 71 |
global $pagenow; |
| 72 |
return 'edit.php' === $pagenow && isset( $this->get['post_type'] ) && SIMPLE_URLS_SLUG === $this->get['post_type'] && isset( $this->get['page'] ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Dashboard page |
| 77 |
* |
| 78 |
* @var string $dashboard_page |
| 79 |
*/ |
| 80 |
public $dashboard_page = 'dashboard'; |
| 81 |
|
| 82 |
/** |
| 83 |
* Check whether current page is WP post page |
| 84 |
*/ |
| 85 |
public function is_wordpress_post() { |
| 86 |
global $pagenow; |
| 87 |
|
| 88 |
$action = $this->get['action'] ?? ''; |
| 89 |
$add_new_page = 'post-new.php' === $pagenow; |
| 90 |
$edit_page = 'post.php' === $pagenow && 'edit' === $action; |
| 91 |
$post_type = $this->get['post_type'] ?? ''; |
| 92 |
|
| 93 |
if ( ( 'edit.php' === $pagenow || $add_new_page ) && '' === $post_type ) { |
| 94 |
$post_type = 'post'; |
| 95 |
} elseif ( $add_new_page ) { |
| 96 |
$post_type = $this->get['post_type'] ?? $post_type; |
| 97 |
} elseif ( $edit_page ) { |
| 98 |
$post_id = intval( $this->get['post'] ?? 0 ); |
| 99 |
$post_type = $post_id > 0 ? get_post_type( $post_id ) : $post_type; |
| 100 |
} |
| 101 |
|
| 102 |
if ( 'term.php' === $pagenow ) { |
| 103 |
$post_type = ''; |
| 104 |
} |
| 105 |
|
| 106 |
return 'post' === $post_type || 'page' === $post_type; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Check whether current page is custom post page |
| 111 |
*/ |
| 112 |
public function is_custom_post() { |
| 113 |
global $pagenow; |
| 114 |
|
| 115 |
$action = $this->get['action'] ?? ''; |
| 116 |
$add_new_page = 'post-new.php' === $pagenow; |
| 117 |
$edit_page = 'post.php' === $pagenow && 'edit' === $action; |
| 118 |
$post_type = $this->get['post_type'] ?? ''; |
| 119 |
|
| 120 |
if ( ( 'edit.php' === $pagenow || $add_new_page ) && '' === $post_type ) { |
| 121 |
$post_type = 'post'; |
| 122 |
} elseif ( $add_new_page ) { |
| 123 |
$post_type = $this->get['post_type'] ?? $post_type; |
| 124 |
} elseif ( $edit_page ) { |
| 125 |
$post_id = intval( $this->get['post'] ?? 0 ); |
| 126 |
$post_type = $post_id > 0 ? get_post_type( $post_id ) : $post_type; |
| 127 |
} |
| 128 |
|
| 129 |
if ( 'term.php' === $pagenow ) { |
| 130 |
$post_type = ''; |
| 131 |
} |
| 132 |
|
| 133 |
return '' !== $post_type && 'post' !== $post_type && 'page' !== $post_type; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Update lasso setting to db |
| 138 |
* |
| 139 |
* @param string $option_name Option name. |
| 140 |
* @param string $option_default Option default. Default to null. |
| 141 |
*/ |
| 142 |
public static function get_setting( $option_name, $option_default = null ) { |
| 143 |
if ( ! is_string( $option_name ) ) { |
| 144 |
return $option_default; |
| 145 |
} |
| 146 |
|
| 147 |
$options = self::get_settings(); |
| 148 |
|
| 149 |
return $options[ $option_name ] ?? $option_default; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Get lasso settings from db |
| 154 |
*/ |
| 155 |
public static function get_settings() { |
| 156 |
$options = get_option( 'lassolite_settings', array() ); |
| 157 |
if ( ! is_array( $options ) ) { |
| 158 |
$options = array(); |
| 159 |
} |
| 160 |
$defaults = Constant::DEFAULT_SETTINGS; |
| 161 |
|
| 162 |
return wp_parse_args( $options, $defaults ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Update lasso setting to db |
| 167 |
* |
| 168 |
* @param string $option_name Option name. |
| 169 |
* @param string $option_value Option value. |
| 170 |
*/ |
| 171 |
public static function set_setting( $option_name, $option_value ) { |
| 172 |
if ( ! is_string( $option_name ) ) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
$options = self::get_settings(); |
| 177 |
$options[ $option_name ] = $option_value; |
| 178 |
|
| 179 |
return update_option( 'lassolite_settings', $options ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Update lasso settings to db |
| 184 |
* |
| 185 |
* @param array $options New options. |
| 186 |
*/ |
| 187 |
public static function set_settings( $options ) { |
| 188 |
if ( ! is_array( $options ) ) { |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
$defaults = self::get_settings(); |
| 193 |
$options = array_merge( $defaults, $options ); |
| 194 |
|
| 195 |
return update_option( 'lassolite_settings', $options ); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Check current page is dashboard page |
| 200 |
* |
| 201 |
* @return bool |
| 202 |
*/ |
| 203 |
public function is_dashboard_page() { |
| 204 |
return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_DASHBOARD ) === $this->current_page; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Check current page is opportunities page |
| 209 |
* |
| 210 |
* @return bool |
| 211 |
*/ |
| 212 |
public function is_opportunities_page() { |
| 213 |
return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_OPPORTUNITIES ) === $this->current_page; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Check current page is tables page |
| 218 |
* |
| 219 |
* @return bool |
| 220 |
*/ |
| 221 |
public function is_tables_page() { |
| 222 |
return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_TABLES ) === $this->current_page; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Check current page is setting page |
| 227 |
* |
| 228 |
* @return bool |
| 229 |
*/ |
| 230 |
public function is_setting_page() { |
| 231 |
$setting_pages = array( |
| 232 |
Helper::add_prefix_page( Enum::PAGE_SETTINGS_GENERAL ), |
| 233 |
Helper::add_prefix_page( Enum::PAGE_SETTINGS_DISPLAY ), |
| 234 |
Helper::add_prefix_page( Enum::PAGE_SETTINGS_AMAZON ), |
| 235 |
); |
| 236 |
|
| 237 |
return $this->is_surls_page() && in_array( $this->current_page, $setting_pages, true ); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Check current page is setting - display page |
| 242 |
* |
| 243 |
* @return bool |
| 244 |
*/ |
| 245 |
public function is_setting_display_page() { |
| 246 |
return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_SETTINGS_DISPLAY ) === $this->current_page; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Check current page is setting - amazon page |
| 251 |
* |
| 252 |
* @return bool |
| 253 |
*/ |
| 254 |
public function is_setting_amazon_page() { |
| 255 |
return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_SETTINGS_AMAZON ) === $this->current_page; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Check current page is onboarding |
| 260 |
* |
| 261 |
* @return bool |
| 262 |
*/ |
| 263 |
public function is_setting_onboarding_page() { |
| 264 |
return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_ONBOARDING ) === $this->current_page; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Update lasso settings to db |
| 269 |
* Check current page is setting - display page |
| 270 |
* |
| 271 |
* @return bool |
| 272 |
*/ |
| 273 |
public function is_setting_general_page() { |
| 274 |
return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_SETTINGS_GENERAL ) === $this->current_page; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Check current page is import page |
| 279 |
* |
| 280 |
* @return bool |
| 281 |
*/ |
| 282 |
public function is_import_page() { |
| 283 |
return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_IMPORT ) === $this->current_page; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Check current page is group detail page |
| 288 |
* |
| 289 |
* @return bool |
| 290 |
*/ |
| 291 |
public function is_group_detail_page() { |
| 292 |
return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_GROUP_DETAIL ) === $this->current_page; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Check current page is group detail page |
| 297 |
* |
| 298 |
* @return bool |
| 299 |
*/ |
| 300 |
public function is_group_page() { |
| 301 |
return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_GROUPS ) === $this->current_page; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Save support |
| 306 |
* |
| 307 |
* @param bool $is_ajax check whether $is_ajax request. |
| 308 |
*/ |
| 309 |
public static function save_support( $is_ajax = true ) { |
| 310 |
$post = Helper::POST(); |
| 311 |
$email = $post['email'] ?? ''; |
| 312 |
$is_subscribe = $post['is_subscribe'] ?? ''; |
| 313 |
$share_diagnostics = $post['share_diagnostics'] ?? 1; // Default to 1 (checked) |
| 314 |
|
| 315 |
|
| 316 |
$settings = self::get_settings(); |
| 317 |
$share_diagnostics_db = $settings[ Enum::SHARE_DIAGNOSTICS ] ?? ''; |
| 318 |
$email_db = $settings[ Enum::EMAIL_SUPPORT ] ?? ''; |
| 319 |
|
| 320 |
if ( $is_ajax ) { |
| 321 |
$settings[ Enum::IS_SUBSCRIBE ] = $is_subscribe; |
| 322 |
$settings[ Enum::EMAIL_SUPPORT ] = $email; |
| 323 |
$settings[ Enum::SHARE_DIAGNOSTICS ] = $share_diagnostics; |
| 324 |
} else { |
| 325 |
$email = $settings[ Enum::EMAIL_SUPPORT ]; |
| 326 |
} |
| 327 |
$support_enable_time = $settings[ Enum::SUPPORT_ENABLED_TIME ] ?? ''; |
| 328 |
$current_date = date( 'm/d/Y', time() ); // phpcs:ignore |
| 329 |
if ( ! $is_ajax || empty( $support_enable_time ) || $current_date > $support_enable_time || $share_diagnostics_db !== $share_diagnostics || $email !== $email_db ) { |
| 330 |
global $wp_version; |
| 331 |
$jwt_data = array( |
| 332 |
'email' => $email, |
| 333 |
'installed_version' => LASSO_LITE_VERSION, |
| 334 |
'datetime' => gmdate( 'Y-m-d H:i:s' ), |
| 335 |
'site_id' => License::get_site_id(), |
| 336 |
'install_url' => site_url(), |
| 337 |
'wordpress_version' => $wp_version, |
| 338 |
'php_version' => phpversion(), |
| 339 |
'mysql_version' => Model::get_wpdb()->db_version(), |
| 340 |
'is_classic_editor' => Helper::is_classic_editor() ? 1 : 0, |
| 341 |
'share_diagnostics' => $share_diagnostics, |
| 342 |
); |
| 343 |
|
| 344 |
$jwt = JWT::encode( $jwt_data, Constant::JWT_SECRET_KEY, 'HS256' ); |
| 345 |
$data['data'] = $jwt; |
| 346 |
$response = Helper::send_request( 'post', Constant::LASSO_LINK . '/lasso-lite/enable-support', $data ); |
| 347 |
$is_succeed = boolval( $response['response']->succeed ); |
| 348 |
if ( $is_succeed ) { |
| 349 |
$user_hash = $response['response']->user_hash; |
| 350 |
$intercom_jwt = $response['response']->intercom_jwt; |
| 351 |
$settings[ Enum::SUPPORT_ENABLED_TIME ] = date( 'm/d/Y', time() ); // phpcs:ignore |
| 352 |
$settings[ Enum::USER_HASH ] = $user_hash; |
| 353 |
$settings[ Constant::SITE_ID_KEY ] = $response['response']->site_id; |
| 354 |
if ( ! empty( $intercom_jwt ) ) { |
| 355 |
$settings[ Enum::INTERCOM_JWT ] = $intercom_jwt; |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
self::set_settings( $settings ); |
| 361 |
if ( $is_ajax ) { |
| 362 |
$response_data = array( |
| 363 |
'success' => true, |
| 364 |
); |
| 365 |
|
| 366 |
// Provide Intercom bootstrap payload on AJAX success so frontend can init Intercom immediately |
| 367 |
if ( ! empty( $settings[ Enum::INTERCOM_JWT ] ) ) { |
| 368 |
$email_support = $settings[ Enum::EMAIL_SUPPORT ] ?? $email; |
| 369 |
$email_support_norm = strtolower( trim( $email_support ) ); |
| 370 |
|
| 371 |
$intercom_user_id = Helper::get_intercom_user_id( $email_support_norm, $settings[ Enum::INTERCOM_JWT ] ); |
| 372 |
|
| 373 |
$user = get_user_by( 'email', $email_support_norm ); |
| 374 |
$user_name = isset( $user->display_name ) ? $user->display_name : get_bloginfo( 'name' ); |
| 375 |
$classic_editor = Helper::is_classic_editor() ? 1 : 0; |
| 376 |
$lasso_lite_user = 1; |
| 377 |
if ( Helper::is_lasso_pro_plugin_active() ) { |
| 378 |
$lasso_lite_user = 0; |
| 379 |
} |
| 380 |
|
| 381 |
$response_data['intercom'] = array( |
| 382 |
'app_id' => Constant::LASSO_INTERCOM_APP_ID, |
| 383 |
'name' => $user_name, |
| 384 |
'user_id' => $intercom_user_id, |
| 385 |
'email' => $email_support_norm, |
| 386 |
'lasso_version' => (int) LASSO_LITE_VERSION, |
| 387 |
'classic_editor' => $classic_editor, |
| 388 |
'wp_admin_url' => admin_url(), |
| 389 |
'lasso_lite_user' => $lasso_lite_user, |
| 390 |
'intercom_user_jwt' => $settings[ Enum::INTERCOM_JWT ], |
| 391 |
'site_id' => $settings[ Constant::SITE_ID_KEY ], |
| 392 |
); |
| 393 |
} |
| 394 |
|
| 395 |
wp_send_json_success( |
| 396 |
$response_data |
| 397 |
); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Check plugins for importing |
| 403 |
* |
| 404 |
* @return string |
| 405 |
*/ |
| 406 |
public function check_plugins_for_import() { |
| 407 |
// ? import plugin check |
| 408 |
$plugins_for_import = $this->get_import_sources(); |
| 409 |
$setting_page_link = ''; |
| 410 |
$plugins_for_import_txt = ''; |
| 411 |
|
| 412 |
if ( ! empty( $plugins_for_import ) ) { |
| 413 |
$verb = count( $plugins_for_import ) > 1 ? 'are' : 'is'; |
| 414 |
$plugins_for_import_txt = implode( ', ', $plugins_for_import ) . ' ' . $verb; |
| 415 |
} |
| 416 |
|
| 417 |
return $plugins_for_import_txt; |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Check whether the plugins are activated |
| 422 |
* |
| 423 |
* @return array|false |
| 424 |
*/ |
| 425 |
public static function get_import_sources() { |
| 426 |
$plugin_list = (array) self::$import_sources; |
| 427 |
|
| 428 |
$result = array(); |
| 429 |
$plugin_path_abs = dirname( SIMPLE_URLS_DIR ); |
| 430 |
|
| 431 |
try { |
| 432 |
foreach ( $plugin_list as $plugin ) { |
| 433 |
$full_plugin_path = $plugin_path_abs . '/' . $plugin; |
| 434 |
if ( ! file_exists( $full_plugin_path ) ) { |
| 435 |
continue; |
| 436 |
} |
| 437 |
|
| 438 |
// ? Check plugin is activated |
| 439 |
if ( is_plugin_active( $plugin ) ) { |
| 440 |
$plugin_name = self::get_plugin_name( $plugin ); |
| 441 |
$key = $plugin; |
| 442 |
$result[ $key ] = $plugin_name; |
| 443 |
} |
| 444 |
} |
| 445 |
} catch ( \Exception $e ) { |
| 446 |
return false; |
| 447 |
} |
| 448 |
|
| 449 |
return $result; |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* Get plugin name |
| 454 |
* |
| 455 |
* @param string $plugin Plugin file path. |
| 456 |
*/ |
| 457 |
public static function get_plugin_name( $plugin ) { |
| 458 |
$plugin_name = 'The plugin ' . $plugin . ' has been deleted.'; |
| 459 |
$plugin_path = WP_PLUGIN_DIR . '/' . $plugin; |
| 460 |
if ( file_exists( $plugin_path ) ) { |
| 461 |
$plugin_data = get_plugin_data( $plugin_path ); |
| 462 |
$plugin_name = $plugin_data['Name']; |
| 463 |
} |
| 464 |
|
| 465 |
return $plugin_name; |
| 466 |
} |
| 467 |
|
| 468 |
/** |
| 469 |
* Get report url by tab |
| 470 |
*/ |
| 471 |
public function get_dashboard_page() { |
| 472 |
$dashboard_url = add_query_arg( |
| 473 |
array( |
| 474 |
'post_type' => Constant::LASSO_POST_TYPE, |
| 475 |
'page' => $this->dashboard_page, |
| 476 |
), |
| 477 |
admin_url( 'edit.php' ) |
| 478 |
); |
| 479 |
|
| 480 |
return $dashboard_url; |
| 481 |
} |
| 482 |
|
| 483 |
/** |
| 484 |
* Get report url by tab |
| 485 |
* |
| 486 |
* @param string $page_slug Page slug. |
| 487 |
*/ |
| 488 |
public static function get_lasso_page_url( $page_slug ) { |
| 489 |
$dashboard_url = add_query_arg( |
| 490 |
array( |
| 491 |
'post_type' => Constant::LASSO_POST_TYPE, |
| 492 |
'page' => $page_slug, |
| 493 |
), |
| 494 |
admin_url( 'edit.php' ) |
| 495 |
); |
| 496 |
|
| 497 |
return $dashboard_url; |
| 498 |
} |
| 499 |
} |
| 500 |
|