PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 152
Lasso Lite – Affiliate Link Manager & Product Displays v152
157 155 156 154 153 152 151 150 149 148 trunk 0.9.9 104 105 106 107 108 109 110 111 112 113 114 115 116 All 56 releases
simple-urls / classes / class-setting.php

class-setting.php in Lasso Lite – Affiliate Link Manager & Product Displays 152, at classes/class-setting.php

501 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $response_body = ( isset( $response['response'] ) && is_object( $response['response'] ) ) ? $response['response'] : null;
348 $is_succeed = null !== $response_body && boolval( $response_body->succeed ?? false );
349 if ( $is_succeed ) {
350 $user_hash = $response_body->user_hash;
351 $intercom_jwt = $response_body->intercom_jwt;
352 $settings[ Enum::SUPPORT_ENABLED_TIME ] = date( 'm/d/Y', time() ); // phpcs:ignore
353 $settings[ Enum::USER_HASH ] = $user_hash;
354 $settings[ Constant::SITE_ID_KEY ] = $response_body->site_id;
355 if ( ! empty( $intercom_jwt ) ) {
356 $settings[ Enum::INTERCOM_JWT ] = $intercom_jwt;
357 }
358 }
359 }
360
361 self::set_settings( $settings );
362 if ( $is_ajax ) {
363 $response_data = array(
364 'success' => true,
365 );
366
367 // Provide Intercom bootstrap payload on AJAX success so frontend can init Intercom immediately
368 if ( ! empty( $settings[ Enum::INTERCOM_JWT ] ) ) {
369 $email_support = $settings[ Enum::EMAIL_SUPPORT ] ?? $email;
370 $email_support_norm = strtolower( trim( $email_support ) );
371
372 $intercom_user_id = Helper::get_intercom_user_id( $email_support_norm, $settings[ Enum::INTERCOM_JWT ] );
373
374 $user = get_user_by( 'email', $email_support_norm );
375 $user_name = isset( $user->display_name ) ? $user->display_name : get_bloginfo( 'name' );
376 $classic_editor = Helper::is_classic_editor() ? 1 : 0;
377 $lasso_lite_user = 1;
378 if ( Helper::is_lasso_pro_plugin_active() ) {
379 $lasso_lite_user = 0;
380 }
381
382 $response_data['intercom'] = array(
383 'app_id' => Constant::LASSO_INTERCOM_APP_ID,
384 'name' => $user_name,
385 'user_id' => $intercom_user_id,
386 'email' => $email_support_norm,
387 'lasso_version' => (int) LASSO_LITE_VERSION,
388 'classic_editor' => $classic_editor,
389 'wp_admin_url' => admin_url(),
390 'lasso_lite_user' => $lasso_lite_user,
391 'intercom_user_jwt' => $settings[ Enum::INTERCOM_JWT ],
392 'site_id' => $settings[ Constant::SITE_ID_KEY ],
393 );
394 }
395
396 wp_send_json_success(
397 $response_data
398 );
399 }
400 }
401
402 /**
403 * Check plugins for importing
404 *
405 * @return string
406 */
407 public function check_plugins_for_import() {
408 // ? import plugin check
409 $plugins_for_import = $this->get_import_sources();
410 $setting_page_link = '';
411 $plugins_for_import_txt = '';
412
413 if ( ! empty( $plugins_for_import ) ) {
414 $verb = count( $plugins_for_import ) > 1 ? 'are' : 'is';
415 $plugins_for_import_txt = implode( ', ', $plugins_for_import ) . ' ' . $verb;
416 }
417
418 return $plugins_for_import_txt;
419 }
420
421 /**
422 * Check whether the plugins are activated
423 *
424 * @return array|false
425 */
426 public static function get_import_sources() {
427 $plugin_list = (array) self::$import_sources;
428
429 $result = array();
430 $plugin_path_abs = dirname( SIMPLE_URLS_DIR );
431
432 try {
433 foreach ( $plugin_list as $plugin ) {
434 $full_plugin_path = $plugin_path_abs . '/' . $plugin;
435 if ( ! file_exists( $full_plugin_path ) ) {
436 continue;
437 }
438
439 // ? Check plugin is activated
440 if ( is_plugin_active( $plugin ) ) {
441 $plugin_name = self::get_plugin_name( $plugin );
442 $key = $plugin;
443 $result[ $key ] = $plugin_name;
444 }
445 }
446 } catch ( \Exception $e ) {
447 return false;
448 }
449
450 return $result;
451 }
452
453 /**
454 * Get plugin name
455 *
456 * @param string $plugin Plugin file path.
457 */
458 public static function get_plugin_name( $plugin ) {
459 $plugin_name = 'The plugin ' . $plugin . ' has been deleted.';
460 $plugin_path = WP_PLUGIN_DIR . '/' . $plugin;
461 if ( file_exists( $plugin_path ) ) {
462 $plugin_data = get_plugin_data( $plugin_path );
463 $plugin_name = $plugin_data['Name'];
464 }
465
466 return $plugin_name;
467 }
468
469 /**
470 * Get report url by tab
471 */
472 public function get_dashboard_page() {
473 $dashboard_url = add_query_arg(
474 array(
475 'post_type' => Constant::LASSO_POST_TYPE,
476 'page' => $this->dashboard_page,
477 ),
478 admin_url( 'edit.php' )
479 );
480
481 return $dashboard_url;
482 }
483
484 /**
485 * Get report url by tab
486 *
487 * @param string $page_slug Page slug.
488 */
489 public static function get_lasso_page_url( $page_slug ) {
490 $dashboard_url = add_query_arg(
491 array(
492 'post_type' => Constant::LASSO_POST_TYPE,
493 'page' => $page_slug,
494 ),
495 admin_url( 'edit.php' )
496 );
497
498 return $dashboard_url;
499 }
500 }
501