PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 115
Lasso Lite – Affiliate Link Manager & Product Displays v115
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 115, at classes/class-setting.php

399 lines 10.1 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 /**
19 * Convert WP GET parameters to Lasso GET
20 *
21 * @var array|string $get
22 */
23 public $get = array();
24
25 /**
26 * Current page
27 *
28 * @var mixed|string
29 */
30 public $current_page = '';
31
32 /**
33 * List of plugins
34 *
35 * @var string $import_sources
36 */
37 public static $import_sources = array(
38 'earnist/earnist.php',
39 'thirstyaffiliates/thirstyaffiliates.php',
40 'pretty-link/pretty-link.php',
41 'affiliate-link-automation/affiliate_automation.php',
42 'aawp/aawp.php',
43 'easyazon/easyazon.php',
44 'amalinkspro/amalinkspro.php',
45 'easy-affiliate-links/easy-affiliate-links.php',
46 );
47
48 /**
49 * Setting constructor.
50 */
51 public function __construct() {
52 $this->get = Helper::GET();
53 $this->current_page = $this->get['page'] ?? '';
54 }
55
56 /**
57 * Check whether current page is SURLS page or not.
58 */
59 public function is_surls_page() {
60 global $pagenow;
61 return 'edit.php' === $pagenow && isset( $this->get['post_type'] ) && SIMPLE_URLS_SLUG === $this->get['post_type'] && isset( $this->get['page'] );
62 }
63
64 /**
65 * Check whether current page is WP post page
66 */
67 public function is_wordpress_post() {
68 global $pagenow;
69
70 $action = $this->get['action'] ?? '';
71 $add_new_page = 'post-new.php' === $pagenow;
72 $edit_page = 'post.php' === $pagenow && 'edit' === $action;
73 $post_type = $this->get['post_type'] ?? '';
74
75 if ( ( 'edit.php' === $pagenow || $add_new_page ) && '' === $post_type ) {
76 $post_type = 'post';
77 } elseif ( $add_new_page ) {
78 $post_type = $this->get['post_type'] ?? $post_type;
79 } elseif ( $edit_page ) {
80 $post_id = intval( $this->get['post'] ?? 0 );
81 $post_type = $post_id > 0 ? get_post_type( $post_id ) : $post_type;
82 }
83
84 if ( 'term.php' === $pagenow ) {
85 $post_type = '';
86 }
87
88 return 'post' === $post_type || 'page' === $post_type;
89 }
90
91 /**
92 * Check whether current page is custom post page
93 */
94 public function is_custom_post() {
95 global $pagenow;
96
97 $action = $this->get['action'] ?? '';
98 $add_new_page = 'post-new.php' === $pagenow;
99 $edit_page = 'post.php' === $pagenow && 'edit' === $action;
100 $post_type = $this->get['post_type'] ?? '';
101
102 if ( ( 'edit.php' === $pagenow || $add_new_page ) && '' === $post_type ) {
103 $post_type = 'post';
104 } elseif ( $add_new_page ) {
105 $post_type = $this->get['post_type'] ?? $post_type;
106 } elseif ( $edit_page ) {
107 $post_id = intval( $this->get['post'] ?? 0 );
108 $post_type = $post_id > 0 ? get_post_type( $post_id ) : $post_type;
109 }
110
111 if ( 'term.php' === $pagenow ) {
112 $post_type = '';
113 }
114
115 return '' !== $post_type && 'post' !== $post_type && 'page' !== $post_type;
116 }
117
118 /**
119 * Update lasso setting to db
120 *
121 * @param string $option_name Option name.
122 * @param string $option_default Option default. Default to null.
123 */
124 public static function get_setting( $option_name, $option_default = null ) {
125 if ( ! is_string( $option_name ) ) {
126 return $option_default;
127 }
128
129 $options = self::get_settings();
130
131 return $options[ $option_name ] ?? $option_default;
132 }
133
134 /**
135 * Get lasso settings from db
136 */
137 public static function get_settings() {
138 $options = get_option( 'lassolite_settings', array() );
139 if ( ! is_array( $options ) ) {
140 $options = array();
141 }
142 $defaults = Constant::DEFAULT_SETTINGS;
143
144 return wp_parse_args( $options, $defaults );
145 }
146
147 /**
148 * Update lasso setting to db
149 *
150 * @param string $option_name Option name.
151 * @param string $option_value Option value.
152 */
153 public static function set_setting( $option_name, $option_value ) {
154 if ( ! is_string( $option_name ) ) {
155 return false;
156 }
157
158 $options = self::get_settings();
159 $options[ $option_name ] = $option_value;
160
161 return update_option( 'lassolite_settings', $options );
162 }
163
164 /**
165 * Update lasso settings to db
166 *
167 * @param array $options New options.
168 */
169 public static function set_settings( $options ) {
170 if ( ! is_array( $options ) ) {
171 return false;
172 }
173
174 $defaults = self::get_settings();
175 $options = array_merge( $defaults, $options );
176
177 return update_option( 'lassolite_settings', $options );
178 }
179
180 /**
181 * Check current page is dashboard page
182 *
183 * @return bool
184 */
185 public function is_dashboard_page() {
186 return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_DASHBOARD ) === $this->current_page;
187 }
188
189 /**
190 * Check current page is setting page
191 *
192 * @return bool
193 */
194 public function is_setting_page() {
195 $setting_pages = array(
196 Helper::add_prefix_page( Enum::PAGE_SETTINGS_GENERAL ),
197 Helper::add_prefix_page( Enum::PAGE_SETTINGS_DISPLAY ),
198 Helper::add_prefix_page( Enum::PAGE_SETTINGS_AMAZON ),
199 );
200
201 return $this->is_surls_page() && in_array( $this->current_page, $setting_pages, true );
202 }
203
204 /**
205 * Check current page is setting - display page
206 *
207 * @return bool
208 */
209 public function is_setting_display_page() {
210 return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_SETTINGS_DISPLAY ) === $this->current_page;
211 }
212
213 /**
214 * Check current page is setting - amazon page
215 *
216 * @return bool
217 */
218 public function is_setting_amazon_page() {
219 return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_SETTINGS_AMAZON ) === $this->current_page;
220 }
221
222 /**
223 * Check current page is onboarding
224 *
225 * @return bool
226 */
227 public function is_setting_onboarding_page() {
228 return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_ONBOARDING ) === $this->current_page;
229 }
230
231 /**
232 * Update lasso settings to db
233 * Check current page is setting - display page
234 *
235 * @return bool
236 */
237 public function is_setting_general_page() {
238 return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_SETTINGS_GENERAL ) === $this->current_page;
239 }
240
241 /**
242 * Check current page is import page
243 *
244 * @return bool
245 */
246 public function is_import_page() {
247 return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_IMPORT ) === $this->current_page;
248 }
249
250 /**
251 * Check current page is group detail page
252 *
253 * @return bool
254 */
255 public function is_group_detail_page() {
256 return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_GROUP_DETAIL ) === $this->current_page;
257 }
258
259 /**
260 * Check current page is group detail page
261 *
262 * @return bool
263 */
264 public function is_group_page() {
265 return $this->is_surls_page() && Helper::add_prefix_page( Enum::PAGE_GROUPS ) === $this->current_page;
266 }
267
268 /**
269 * Save support
270 *
271 * @param bool $is_ajax check whether $is_ajax request.
272 */
273 public static function save_support( $is_ajax = true ) {
274 $post = Helper::POST();
275 $email = $post['email'] ?? '';
276 $is_subscribe = $post['is_subscribe'] ?? '';
277 if ( $is_ajax && false === is_email( $email ) ) {
278 wp_send_json_success(
279 array(
280 'success' => false,
281 'msg' => 'Email is invalid.',
282 )
283 );
284 } else {
285 $settings = self::get_settings();
286
287 if ( $is_ajax ) {
288 $settings[ Enum::IS_SUBSCRIBE ] = $is_subscribe;
289 $settings[ Enum::EMAIL_SUPPORT ] = $email;
290 $settings[ Enum::SUPPORT_ENABLED ] = true;
291 } else {
292 $email = $settings[ Enum::EMAIL_SUPPORT ];
293 }
294 $support_enable_time = $settings[ Enum::SUPPORT_ENABLED_TIME ] ?? '';
295 $current_date = date( 'm/d/Y', time() ); // phpcs:ignore
296 if ( ! $is_ajax || empty( $support_enable_time ) || $current_date > $support_enable_time ) {
297 global $wp_version;
298 $jwt_data = array(
299 'email' => $email,
300 'installed_version' => LASSO_LITE_VERSION,
301 'datetime' => gmdate( 'Y-m-d H:i:s' ),
302 'site_id' => Helper::get_option( Constant::SITE_ID_KEY ),
303 'install_url' => site_url(),
304 'wordpress_version' => $wp_version,
305 'php_version' => phpversion(),
306 'mysql_version' => Model::get_wpdb()->db_version(),
307 'is_classic_editor' => Helper::is_classic_editor() ? 1 : 0,
308 );
309
310 $jwt = JWT::encode( $jwt_data, Constant::JWT_SECRET_KEY, 'HS256' );
311 $data['data'] = $jwt;
312 $response = Helper::send_request( 'post', Constant::LASSO_LINK . 'lasso-lite/enable-support', $data );
313 $is_succeed = boolval( $response['response']->succeed );
314 if ( $is_succeed ) {
315 $user_hash = $response['response']->user_hash;
316 $settings[ Enum::SUPPORT_ENABLED_TIME ] = date( 'm/d/Y', time() ); // phpcs:ignore
317 $settings[ Enum::USER_HASH ] = $user_hash;
318 }
319 }
320
321 self::set_settings( $settings );
322 if ( $is_ajax ) {
323 wp_send_json_success(
324 array(
325 'success' => true,
326 )
327 );
328 }
329 }
330 }
331
332 /**
333 * Check plugins for importing
334 *
335 * @return string
336 */
337 public function check_plugins_for_import() {
338 // ? import plugin check
339 $plugins_for_import = $this->get_import_sources();
340 $setting_page_link = '';
341 $plugins_for_import_txt = '';
342
343 if ( ! empty( $plugins_for_import ) ) {
344 $verb = count( $plugins_for_import ) > 1 ? 'are' : 'is';
345 $plugins_for_import_txt = implode( ', ', $plugins_for_import ) . ' ' . $verb;
346 }
347
348 return $plugins_for_import_txt;
349 }
350
351 /**
352 * Check whether the plugins are activated
353 *
354 * @return array|false
355 */
356 public static function get_import_sources() {
357 $plugin_list = self::$import_sources;
358
359 $result = array();
360 $plugin_path_abs = dirname( SIMPLE_URLS_DIR );
361
362 try {
363 foreach ( $plugin_list as $plugin ) {
364 $full_plugin_path = $plugin_path_abs . '/' . $plugin;
365 if ( ! file_exists( $full_plugin_path ) ) {
366 continue;
367 }
368
369 // ? Check plugin is activated
370 if ( is_plugin_active( $plugin ) ) {
371 $plugin_name = self::get_plugin_name( $plugin );
372 $key = $plugin;
373 $result[ $key ] = $plugin_name;
374 }
375 }
376 } catch ( \Exception $e ) {
377 return false;
378 }
379
380 return $result;
381 }
382
383 /**
384 * Get plugin name
385 *
386 * @param string $plugin Plugin file path.
387 */
388 public static function get_plugin_name( $plugin ) {
389 $plugin_name = 'The plugin ' . $plugin . ' has been deleted.';
390 $plugin_path = WP_PLUGIN_DIR . '/' . $plugin;
391 if ( file_exists( $plugin_path ) ) {
392 $plugin_data = get_plugin_data( $plugin_path );
393 $plugin_name = $plugin_data['Name'];
394 }
395
396 return $plugin_name;
397 }
398 }
399