PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 114
Lasso Lite – Affiliate Link Manager & Product Displays v114
158 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 All 57 releases
simple-urls / classes / class-helper.php

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

1,448 lines 42.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Declare class Helper
4 *
5 * @package Helper
6 */
7
8 namespace LassoLite\Classes;
9
10 use LassoLite\Admin\Constant;
11
12 use LassoLite\Classes\Amazon_Api;
13 use LassoLite\Classes\Cache_Per_Process;
14 use LassoLite\Classes\Enum;
15 use LassoLite\Classes\Import;
16 use LassoLite\Classes\Setting;
17 use LassoLite\Classes\SURL;
18
19 use LassoLite\Models\Model;
20 use LassoLite\Models\Url_Details;
21
22
23 use Exception;
24
25 /**
26 * Lasso_Helper
27 */
28 class Helper {
29
30 /**
31 * User agent
32 *
33 * @var string $user_agent
34 */
35 public static $user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0';
36
37 /**
38 * GET PHP POST
39 *
40 * @return array|string
41 */
42 public static function POST() { // phpcs:ignore
43 return wp_unslash( $_POST ); // phpcs:ignore
44 }
45
46 /**
47 * GET PHP GET
48 *
49 * @return array|string
50 */
51 public static function GET() { // phpcs:ignore
52 return wp_unslash( $_GET ); // phpcs:ignore
53 }
54
55 /**
56 * Include variables
57 *
58 * @param string $file_path File path.
59 * @param array $variables List of variables.
60 * @param bool $output_ajax_html Output a ajax html string or not.
61 */
62 public static function include_with_variables( $file_path, $variables = array(), $output_ajax_html = true ) {
63 $output = null;
64 if ( file_exists( $file_path ) ) {
65 extract( $variables ); // phpcs:ignore
66 if ( $output_ajax_html ) {
67 ob_start();
68 include $file_path;
69 $output = ob_get_clean();
70 return $output;
71 } else {
72 require $file_path;
73 }
74 }
75
76 return $output;
77 }
78
79 /**
80 * Get path to views folder
81 *
82 * @return string
83 */
84 public static function get_path_views_folder() {
85 return SIMPLE_URLS_DIR . 'admin/views/';
86 }
87
88 /**
89 * Enqueue a Lasso script.
90 *
91 * @param string $handle Name of the script. Should be unique.
92 * @param string $file_name Lasso script file name.
93 * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
94 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. Default 'false'.
95 */
96 public static function enqueue_script( $handle, $file_name, $deps = array(), $in_footer = false ) {
97 $handle = SIMPLE_URLS_SLUG . '-' . $handle;
98 $file_path = SIMPLE_URLS_DIR . '/admin/assets/js/' . $file_name;
99
100 if ( file_exists( $file_path ) ) {
101 $src = SIMPLE_URLS_URL . '/admin/assets/js/' . $file_name;
102 $ver = strval( @filemtime( $file_path ) ); // phpcs:ignore
103
104 wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
105 }
106 }
107
108 /**
109 * Enqueue a Lasso CSS stylesheet.
110 *
111 * @param string $handle Name of the stylesheet. Should be unique.
112 * @param string $file_name Lasso stylesheet file name.
113 * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
114 * @param string $media Optional. The media for which this stylesheet has been defined.
115 * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
116 * '(orientation: portrait)' and '(max-width: 640px)'.
117 * @param bool $apply_handle_prefix Is apply prefix for handle. Default to true.
118 */
119 public static function enqueue_style( $handle, $file_name, $deps = array(), $media = 'all', $apply_handle_prefix = true ) {
120 $handle = $apply_handle_prefix ? SIMPLE_URLS_SLUG . '-' . $handle : $handle;
121 $file_path = SIMPLE_URLS_DIR . '/admin/assets/css/' . $file_name;
122
123 if ( file_exists( $file_path ) ) {
124 $src = SIMPLE_URLS_URL . '/admin/assets/css/' . $file_name;
125 $ver = strval( @filemtime( $file_path ) ); // phpcs:ignore
126
127 wp_enqueue_style( $handle, $src, $deps, $ver, $media );
128 }
129 }
130
131 /**
132 * Get list page
133 *
134 * @return Page[]
135 */
136 public static function available_pages() {
137 $pages[ Enum::PAGE_DASHBOARD ] = new Page( 'Dashboard', Enum::PAGE_DASHBOARD, 'dashboard/index.php' );
138 $pages[ Enum::PAGE_OPPORTUNITIES ] = new Page( 'Opportunities', Enum::PAGE_OPPORTUNITIES, 'opportunities/index.php' );
139 $pages[ Enum::PAGE_IMPORT ] = new Page( 'Import', Enum::PAGE_IMPORT, 'import/index.php' );
140 $pages[ Enum::PAGE_TABLES ] = new Page( 'Tables', Enum::PAGE_TABLES, 'tables/index.php' );
141 $pages[ Enum::PAGE_URL_DETAILS ] = new Page( 'Link Details', Enum::PAGE_URL_DETAILS, '/dashboard/url-details.php' );
142
143 $pages[ Enum::PAGE_SETTINGS_GENERAL ] = new Page( 'General', Enum::PAGE_SETTINGS_GENERAL, 'settings/general.php' );
144 $pages[ Enum::PAGE_SETTINGS_DISPLAY ] = new Page( 'Display', Enum::PAGE_SETTINGS_DISPLAY, 'settings/display.php' );
145 $pages[ Enum::PAGE_SETTINGS_AMAZON ] = new Page( 'Amazon', Enum::PAGE_SETTINGS_AMAZON, 'settings/amazon.php' );
146 $pages[ Enum::PAGE_GROUPS ] = new Page( 'Groups', Enum::PAGE_GROUPS, '/groups/index.php' );
147 $pages[ Enum::PAGE_GROUP_DETAIL ] = new Page( 'Group Detail', Enum::PAGE_GROUP_DETAIL, '/groups/detail.php' );
148
149 if ( get_option( Enum::LASSO_LITE_ACTIVE ) && ! self::get_option( Enum::IS_VISITED_WELCOME_PAGE ) ) {
150 $pages[ Enum::PAGE_ONBOARDING ] = new Page( 'Onboarding', Enum::PAGE_ONBOARDING, 'onboarding/index.php' );
151 }
152
153 return $pages;
154 }
155
156 /**
157 * Convert date time to WordPress format
158 *
159 * @param string $datetime Date time. Format must be 'Y-m-d H:i:s' (example: 2018-09-14 10:34:54).
160 * @param bool $time Is it time. Default to true.
161 */
162 public static function convert_datetime_format( $datetime, $time = true ) {
163 if ( ! $datetime ) {
164 return $datetime;
165 }
166
167 $date_format = get_option( 'date_format' );
168 $time_format = 'g:i a T';
169 $datetime_format = $date_format . ' ' . $time_format;
170 $format = ( $time ) ? $datetime_format : $date_format;
171
172 try {
173 $result = date_create_from_format( 'Y-m-d H:i:s', $datetime );
174 $result = $result->format( $format );
175 } catch ( \Exception $e ) {
176 $result = $datetime;
177 }
178
179 return $result;
180 }
181
182 /**
183 * Add surl prefix
184 *
185 * @param string $page Page name.
186 * @return string
187 */
188 public static function add_prefix_page( $page ) {
189 return SIMPLE_URLS_SLUG . '-' . $page;
190 }
191
192 /**
193 * Print a wrapper for js render library
194 *
195 * @param string $html_id_selector Html id selector.
196 * @param string $file_path Absolute path file.
197 * @return string|null
198 */
199 public static function wrapper_js_render( $html_id_selector, $file_path ) {
200 $output = '<script id="' . $html_id_selector . '" type="text/x-jsrender">';
201 $output .= self::include_with_variables( $file_path, array(), true );
202 $output .= '</script>';
203 return $output;
204 }
205
206 /**
207 * Check a url has protocol or not
208 *
209 * @param string $url URL.
210 * @return bool
211 */
212 public static function has_protocol( $url ) {
213 if ( strpos( $url, 'http' ) === 0 || strpos( $url, 'https' ) === 0 ) {
214 return true;
215 }
216 return false;
217 }
218
219 /**
220 * Check if Classic Editor plugin is active.
221 *
222 * @return bool
223 */
224 public static function is_classic_editor_plugin_active() {
225 return self::get_is_plugin_active( 'classic-editor/classic-editor.php' );
226 }
227
228 /**
229 * If the Lasso Pro plugin is installed, return true. Otherwise, return false
230 */
231 public static function is_lasso_pro_installed() {
232 return self::get_is_plugin_active( 'lasso/affiliate-plugin.php' );
233 }
234
235 /**
236 * If the Lasso Pro plugin is active, return true. Otherwise, return false
237 */
238 public static function is_lasso_pro_plugin_active() {
239 return self::is_lasso_pro_installed() && self::get_license_status();
240 }
241
242 /**
243 * Get license status in DB
244 */
245 public static function get_license_status() {
246 $db_status = get_option( 'lasso_license_status', '' );
247 $active_license = boolval( $db_status );
248
249 return $active_license;
250 }
251
252 /**
253 * Check whether slug exists or not
254 *
255 * @param string $post_name Post name.
256 * @param int $post_id Post id. Default to 0.
257 */
258 public static function the_slug_exists( $post_name, $post_id = 0 ) {
259 if ( empty( $post_name ) ) {
260 return false;
261 }
262
263 $posts_tbl = Model::get_wp_table_name( 'posts' );
264 $sql = '
265 SELECT
266 ID,
267 post_name,
268 post_type
269 FROM '
270 . $posts_tbl . '
271 WHERE
272 post_name = %s
273 AND ID != %d
274 AND post_status <> "trash"
275 AND post_type = %s
276 LIMIT 1
277 ';
278
279 $prepare = Model::prepare( $sql, $post_name, $post_id, Constant::LASSO_POST_TYPE ); // phpcs:ignore
280 $row = Model::get_row( $prepare, 'ARRAY_A' ); // phpcs:ignore
281
282 return $row ? $row : false;
283 }
284
285 /**
286 * Add https to the url
287 *
288 * @param string $url URL.
289 */
290 public static function add_https( $url ) {
291 $invalid_url = array(
292 'https://%20https:/',
293 'https://xhttps://',
294 'http:/https://',
295 'http://https://',
296 'https://https://',
297 'https://hhttps://',
298 'https://]https://',
299 'https://&quot;https://',
300 '[gift_item link=&quot;https://',
301 ']https://',
302 );
303 $url = trim( $url );
304 $url = str_replace( $invalid_url, 'https://', $url );
305
306 // ? fix mailto in <a> href
307 if ( strpos( $url, 'mailto:' ) !== false || filter_var( $url, FILTER_VALIDATE_EMAIL ) ) {
308 $email = explode( 'mailto:', $url )[1] ?? '';
309 if ( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
310 $url = 'mailto:' . $email;
311 }
312
313 return $url;
314 }
315
316 if ( '' === $url || is_null( $url ) || strpos( $url, '[' ) === 0 ) {
317 return $url;
318 }
319
320 if ( strpos( $url, 'http://' ) !== 0 && strpos( $url, 'https://' ) !== 0 && strpos( $url, '.' ) !== false && '#' !== $url ) {
321 $url = 'https://' . $url;
322 }
323
324 return $url;
325 }
326
327 /**
328 * Format URL before sending request
329 *
330 * @param string $url URL.
331 * @param bool $encode Encode url or not. Default to false.
332 */
333 public static function format_url_before_requesting( $url, $encode = false ) {
334 $url = trim( $url );
335 $url = $encode ? rawurlencode( $url ) : $url;
336
337 return $url;
338 }
339
340 /**
341 * Get title by url
342 *
343 * @param string $url URL.
344 */
345 public static function get_title_by_url( $url ) {
346 $url = self::add_https( $url );
347 $parse = wp_parse_url( $url );
348 $host = $parse['host'] ?? '';
349 $host = str_replace( 'www.', '', $host );
350 $host = explode( '.', $host );
351 $host = $host[ count( $host ) - 2 ] ?? '';
352 $host = str_replace( '-', ' ', $host );
353 $host = ucwords( $host );
354
355 return $host;
356 }
357
358 /**
359 * Remove a action out of WordPress hook
360 *
361 * Example 1: Lasso_Helper::remove_action('admin_print_footer_scripts', 'register_tinymce_quicktags'); $callback is a function name.
362 * Example 2: Lasso_Helper::remove_action('admin_print_footer_scripts', array('EarnistProductPicker', 'register_tinymce_quicktags')); $callback is array.
363 *
364 * @param string $hook_name Hook name.
365 * @param array|string $callback Callback function. $callback[0] is a class name, $callback[1] is a function name.
366 * @param int $priority Priority.
367 */
368 public static function remove_action( $hook_name, $callback, $priority = 10 ) {
369 global $wp_filter;
370
371 if ( ! isset( $wp_filter[ $hook_name ]->callbacks[ $priority ] ) ) {
372 return;
373 }
374
375 foreach ( $wp_filter[ $hook_name ]->callbacks[ $priority ] as $key_function_name_wp => $data ) {
376 $should_remove = false;
377 $obj_name_wp = null;
378 $function_name_wp = is_array( $data['function'] ) ? $data['function'][1] : $data['function'];
379 if ( ! is_array( $callback ) ) {
380 $function_name = $callback;
381 if ( $function_name_wp === $function_name ) {
382 $should_remove = true;
383 }
384 } else {
385 list( $object_name, $function_name ) = $callback;
386 if ( gettype( $object_name ) === 'object' ) {
387 $object_name = get_class( $object_name );
388 }
389
390 if ( ! $data['function'] instanceof \Closure ) {
391 $obj_name_wp = $data['function'][0];
392 if ( gettype( $obj_name_wp ) === 'object' ) {
393 $obj_name_wp = get_class( $obj_name_wp );
394 if ( $obj_name_wp === $object_name && $function_name_wp === $function_name ) {
395 $should_remove = true;
396 }
397 }
398 }
399 }
400
401 if ( $should_remove ) {
402 unset( $wp_filter[ $hook_name ]->callbacks[ $priority ][ $key_function_name_wp ] );
403 break;
404 }
405 }
406 }
407
408 /**
409 * It includes the HTML for the display modal dialogs
410 *
411 * @return string the html for the modal.
412 */
413 public static function get_display_modal_html() {
414 $html = self::include_with_variables( Helper::get_path_views_folder() . 'modals/display-add.php' ); // phpcs:ignore
415 $html .= self::include_with_variables( Helper::get_path_views_folder() . 'modals/url-add.php', array( 'is_from_editor' => true, ) ); // phpcs:ignore
416 $html .= self::include_with_variables( Helper::get_path_views_folder() . 'modals/url-quick-detail.php' ); // phpcs:ignore
417 $html .= self::wrapper_js_render( 'single-list', Helper::get_path_views_folder() . 'modals/single-jsrender.html' ); // phpcs:ignore
418 $html .= self::wrapper_js_render( 'url-quick-detail-jsrender', Helper::get_path_views_folder() . 'components/url-quick-detail-jsrender.html' ); // phpcs:ignore
419
420 return $html;
421 }
422
423 /**
424 * Get countries of Amazon
425 *
426 * @param string $selected_country Country code.
427 */
428 public static function get_countries_dd( $selected_country ) {
429 $countries = Amazon_Api::get_amazon_api_countries();
430
431 $countries_dd = '<select id="amazon_default_tracking_country" name="amazon_default_tracking_country" class="form-control">';
432 foreach ( $countries as $key => $country ) {
433 if ( strlen( $key ) !== 2 ) {
434 continue;
435 }
436
437 $selected = '';
438 if ( $selected_country === $key ) {
439 $selected = 'selected';
440 }
441 $countries_dd .= '<option value="' . $key . '" ' . $selected . ' >' . $country['name'] . '</option>';
442 }
443 $countries_dd .= '</select>';
444
445 return $countries_dd;
446 }
447
448 /**
449 * Get setup progress information
450 *
451 * @return array
452 */
453 public static function get_setup_progress_information() {
454 $enable_support = boolval( Setting::get_setting( Enum::SUPPORT_ENABLED ) ) ? 20 : 0;
455 $total_links = SURL::total();
456 $links = $total_links > 20 ? 20 : $total_links;
457 $links_percent = 20 === $links ? 100 : ( $links / 20 ) * 100;
458 $setup_amz_tracking_id = boolval( get_option( Enum::SETUP_AMZ_TRACKING_ID ) ) ? 15 : 0;
459 $follow_on_twitter = boolval( get_option( Enum::FOLLOW_ON_TWITTER ) ) ? 10 : 0;
460 $share_on_twitter = boolval( get_option( Enum::SHARE_ON_TWITTER ) ) ? 10 : 0;
461 $leave_a_review = boolval( get_option( Enum::LEAVE_A_REVIEW ) ) ? 5 : 0;
462 $is_show_review_note = ! $leave_a_review && $total_links >= 20 && $enable_support && $setup_amz_tracking_id && $follow_on_twitter && $share_on_twitter ? 1 : 0;
463 $progress = $enable_support + $setup_amz_tracking_id + $follow_on_twitter + $share_on_twitter + ( $links * 2 ) + $leave_a_review;
464 $progress = $progress ? $progress / 100 : 0;
465 $open_modal_add_link = $links < 20 ? 'btn-add-20-links' : '';
466
467 $data = array(
468 'progress' => round( $progress, 2 ),
469 'progress_percent' => round( $progress * 100 ),
470 'links' => $links,
471 'links_percent' => $links_percent,
472 'setup_amz_tracking_id' => $setup_amz_tracking_id,
473 'follow_on_twitter' => $follow_on_twitter,
474 'share_on_twitter' => $share_on_twitter,
475 'leave_a_review' => $leave_a_review,
476 'is_show_review_note' => $is_show_review_note,
477 'enable_support' => $enable_support,
478 'setting_amz_url' => Page::get_lite_page_url( Enum::PAGE_SETTINGS_AMAZON ),
479 'follow_twitter_url' => home_url() . '?' . Enum::SLUG_CLOAK_FOLLOW_TWITTER,
480 'share_twitter_url' => home_url() . '?' . Enum::SLUG_CLOAK_SHARE_TWITTER,
481 'review_url' => home_url() . '?' . Enum::SLUG_CLOAK_LASSO_REVIEW_URL,
482 'open_modal_add_link' => $open_modal_add_link,
483 );
484
485 return $data;
486 }
487
488 /**
489 * Send request
490 *
491 * @param string $method Method (get or post). Default to get.
492 * @param string $url URL. Default to empty.
493 * @param array $data Post data. Default to empty array.
494 * @param array $headers Headers. Default to empty array.
495 * @param bool $is_lasso_save Is Lasso save data action. Default to false.
496 */
497 public static function send_request( $method = 'get', $url = '', $data = array(), $headers = array(), $is_lasso_save = false ) {
498 $method = strtolower( $method );
499 $request_options = array(
500 'headers' => $headers,
501 'timeout' => Constant::TIME_OUT,
502 'sslverify' => Constant::SSL_VERIFY,
503 );
504 $body = wp_json_encode( $data );
505 $headers_expect = ! empty( $body ) && strlen( $body ) > 1048576 ? '100-Continue' : '';
506 if ( 'get' === $method ) {
507 $res = wp_remote_get( $url, $request_options );
508 } elseif ( 'post' === $method ) {
509 $request_options['headers']['expect'] = $headers_expect;
510 $request_options['body'] = $body;
511 $res = wp_remote_post( $url, $request_options );
512 } elseif ( 'put' === $method ) {
513 $request_options['headers']['expect'] = $headers_expect;
514 $request_options['body'] = $body;
515 $request_options['method'] = 'PUT';
516 $res = wp_remote_request( $url, $request_options );
517 }
518
519 if ( is_wp_error( $res ) ) {
520 return array(
521 'status_code' => 500,
522 'response' => array(),
523 );
524 }
525
526 $body = wp_remote_retrieve_body( $res );
527 $status = wp_remote_retrieve_response_code( $res );
528
529 return array(
530 'status_code' => $status,
531 'response' => json_decode( $body ),
532 );
533 }
534
535 /**
536 * Get Lasso Lite - WP option
537 *
538 * @param string $option_name Option name.
539 * @param mixed $default Default value.
540 * @return mixed|void
541 */
542 public static function get_option( $option_name, $default = false ) {
543 return get_option( SIMPLE_URLS_SLUG . '_' . $option_name, $default );
544 }
545
546 /**
547 * Update Lasso Lite - WP option
548 *
549 * @param string $option_name Option name.
550 * @param mixed $option_value Option value.
551 * @param bool $autoload Autoload.
552 * @return bool
553 */
554 public static function update_option( $option_name, $option_value, $autoload = null ) {
555 return update_option( SIMPLE_URLS_SLUG . '_' . $option_name, $option_value, $autoload );
556 }
557
558 /**
559 * Validate URL
560 *
561 * @param string $url URL.
562 */
563 public static function validate_url( $url ) {
564 if ( ! is_string( $url ) ) {
565 return false;
566 }
567
568 $url = str_replace( ' ', '%20', $url );
569 $url = preg_replace( '/[^\00-\255]+/u', '', $url );
570
571 return ( ( strpos( $url, 'http://' ) === 0 || strpos( $url, 'https://' ) === 0 ) &&
572 filter_var( $url, FILTER_VALIDATE_URL ) !== false );
573 }
574
575 /**
576 * Get argument from url
577 *
578 * @param string $link Amazon link.
579 * @param string $argument URL argument.
580 * @return string
581 */
582 public static function get_argument_from_url( $link, $argument ) {
583 if ( ! $argument ) {
584 return '';
585 }
586
587 $link = str_replace( '&amp;', '&', $link );
588 $parse = wp_parse_url( $link );
589 $queries = array();
590 parse_str( $parse['query'] ?? '', $queries );
591
592 return $queries[ $argument ] ?? '';
593 }
594
595 /**
596 * Build url parameter string
597 * Parameter example: array( 'rel_=abc', 'maas=def' );
598 *
599 * @param array $parameters Parameter key=value array.
600 * @return string|mixed
601 */
602 public static function build_url_parameter_string( $parameters = array() ) {
603 $result = implode( '&', $parameters );
604 $result = preg_replace( '!\&+!', '&', $result );
605 $result = trim( $result, '&' );
606
607 return $result;
608 }
609
610 /**
611 * Convert query array to a string (in a url)
612 *
613 * @param array $query Query.
614 */
615 public static function get_query_from_array( $query ) {
616 $result = array();
617 foreach ( $query as $key => $value ) {
618 $result[] = $key . '=' . rawurlencode( $value );
619 }
620 $result = implode( '&', $result );
621 return $result;
622 }
623
624 /**
625 * Convert parse_url() to a url
626 *
627 * @param array $parse Parse data from a URL.
628 * @param bool $host Is it host. Default to false.
629 */
630 public static function get_url_from_parse( $parse, $host = false ) {
631 $parse['host'] = $parse['host'] ?? '';
632 $host = false !== $host ? $host : $parse['host'];
633 $parse['host'] = '://' . $host;
634 $parse['query'] = isset( $parse['query'] ) ? '?' . $parse['query'] : '';
635
636 return implode( '', $parse );
637 }
638
639 /**
640 * Get param of $_SERVER
641 *
642 * @param string $name Name of param.
643 */
644 public static function get_server_param( $name ) {
645 return wp_unslash( $_SERVER[ $name ] ?? '' ); // phpcs:ignore
646 }
647
648 /**
649 * Check if Lasso URL description is empty.
650 *
651 * @param string $description Lasso URL description.
652 * @return bool
653 */
654 public static function is_description_empty( $description ) {
655 if ( empty( $description ) || '<p><br></p>' === $description ) {
656 return true;
657 }
658
659 return false;
660 }
661
662 /**
663 * Get base domain
664 * Ex: "http://domain.com" would return "domain.com"
665 *
666 * @param string $domain Domain. It must be passed WITH protocol. Default to empty.
667 */
668 public static function get_base_domain( $domain = '' ) {
669 $domain = self::add_https( $domain );
670 if ( ! self::validate_url( $domain ) ) {
671 return '';
672 }
673
674 $url = @wp_parse_url( $domain ); // phpcs:ignore
675 $host = $url['host'] ?? '';
676 $host = str_replace( 'www.', '', $host );
677 $host = trim( $host );
678
679 return $host ? $host : '';
680 }
681
682 /**
683 * Convert stdclass object to array
684 *
685 * @param bool $obj StdClass object.
686 */
687 public static function convert_stdclass_to_array( $obj ) {
688 $array = json_decode( wp_json_encode( $obj ), true );
689
690 return $array;
691 }
692
693 /**
694 * Check if mysql error relative to Lasso's table does not exist
695 *
696 * @param string $mysql_error mysql error.
697 * @return boolean
698 */
699 public static function is_lasso_tables_does_not_exist_error( $mysql_error ) {
700 return (bool) preg_match( "/(\.)(.)*lasso_(.)*doesn\'t(\s)exist/", $mysql_error );
701 }
702
703 /**
704 * If the AAWP plugin is active, return true. Otherwise, return false
705 */
706 public static function is_aawp_active() {
707 return is_plugin_active( 'aawp/aawp.php' );
708 }
709
710 /**
711 * If the Amalinks Pro plugin is active, return true. Otherwise, return false
712 */
713 public static function is_amalinks_pro_active() {
714 return is_plugin_active( 'amalinkspro/amalinkspro.php' );
715 }
716
717 /**
718 * Format importable data before showing/importing/reverting
719 *
720 * @param object $p Importable post.
721 */
722 public static function format_importable_data( $p ) {
723 $lasso_db = new Lasso_DB();
724 $lasso_helper = new Helper();
725 $lasso_amazon_api = new Amazon_Api();
726
727 $home_url = home_url();
728 $p->import_permalink = get_permalink( $p->id );
729
730 if ( 'Pretty Links' === $p->import_source ) {
731 $pretty_link_data = $lasso_db->get_pretty_link_by_id( $p->id );
732 $defaul_permalink = $home_url . '/' . $pretty_link_data->slug . '/';
733
734 $prlipro = get_option( 'prlipro_options', array() );
735 $prlipro = is_array( $prlipro ) ? $prlipro : array();
736 $base_slug_prefix = $prlipro['base_slug_prefix'] ?? '';
737 $p->import_permalink = '' !== $base_slug_prefix && strpos( $p->post_name, $base_slug_prefix ) === false
738 ? $home_url . '/' . $base_slug_prefix . '/' . $p->post_name . '/'
739 : $defaul_permalink;
740 } elseif ( 'AAWP' === $p->import_source ) {
741 $aawp_row = $lasso_db->get_aawp_product( $p->id );
742 $p->import_permalink = $aawp_row->url ?? '';
743 $shortcode = '[amazon link="' . $p->post_name . '"]';
744 $p->shortcode = $shortcode;
745
746 // ? AAWP list
747 if ( 'aawp_list' === $p->post_type ) {
748 $p->import_permalink = 'https://amazon.com/s?k=' . $p->post_title;
749 $p->check_status = self::check_aawp_list_is_imported( $p->id ) ? 'checked' : '';
750
751 $cat = term_exists( $p->post_title, Constant::LASSO_CATEGORY );
752 $p->check_status = $cat ? 'checked' : $p->check_status;
753
754 $aawp_list = $lasso_db->get_aawp_list( $p->id );
755 if ( $aawp_list ) {
756 $items_count = $aawp_list->items_count ?? 0;
757 $attr_type = 'bestseller' === $aawp_list->type ? 'bestseller' : 'link';
758 $attr_type = 'new_releases' === $aawp_list->type ? 'new' : $attr_type;
759 $shortcode = '[amazon ' . $attr_type . '="' . $aawp_list->keywords . '" items="' . $items_count . '"]';
760 $p->shortcode = $shortcode;
761 }
762 }
763 } elseif ( 'EasyAzon' === $p->import_source ) {
764 $product = Lasso_DB::get_easyazon_option( $p->post_title );
765 $p->post_title = $product['title'];
766 $p->id = $product['identifier'];
767 $p->import_permalink = $product['url'];
768 $p->post_name = strtolower( $product['identifier'] );
769 $shortcode = '[easyazon_link identifier="' . $p->id . '"]' . $p->post_title . '[/easyazon_link]';
770 $p->shortcode = $shortcode;
771
772 $revert = $lasso_db->is_easyazon_product_imported( $product['identifier'] );
773 if ( $revert ) {
774 $p->id = $revert->lasso_id;
775 }
776 } elseif ( 'AmaLinks Pro' === $p->import_source ) {
777 $shortcode = $p->post_name;
778 $attributes = $lasso_helper->get_attributes( $shortcode );
779 $p->shortcode = $shortcode;
780 $p->import_permalink = $attributes['apilink'] ?? '';
781 if ( empty( $p->id ) ) {
782 $p->id = $attributes['asin'] ?? $p->id;
783
784 $url_details = $lasso_db->get_url_details_by_product_id( $p->id, Amazon_Api::PRODUCT_TYPE );
785 $lasso_id = $url_details->lasso_id ?? 0;
786 $p->check_status = $lasso_id > 0 ? 'checked' : '';
787
788 }
789 if ( empty( $p->import_permalink ) ) {
790 $p->import_permalink = $lasso_amazon_api->get_amazon_link_by_product_id( $p->id );
791 }
792 } elseif ( 'Lasso Pro' === $p->import_source ) {
793 $target_url = Import::get_lasso_pro_target_url( $p->id );
794 $p->import_permalink = $target_url;
795 $p->shortcode = '[lasso rel="' . $p->post_name . '" id="' . $p->id . '"]';
796 }
797
798 return $p;
799 }
800
801 /**
802 * Get attributes of shortcode
803 *
804 * @param string $tags_data Shortcode string.
805 */
806 public static function get_attributes( $tags_data ) {
807 // ? fix shortcode contains content: [shortcode]content[/shortcode]
808 preg_match_all(
809 '/' . get_shortcode_regex() . '/',
810 $tags_data,
811 $matches,
812 PREG_SET_ORDER
813 );
814 $content_between = $matches[0][5] ?? '';
815 $shortcode_name = $matches[0][2] ?? '';
816 if ( ! empty( $shortcode_name ) ) {
817 // ? remove content between content/anchor text
818 $tags_data = str_replace( ']' . $content_between . '[', '][', $tags_data );
819 // ? remove the end shortcode
820 $tags_data = str_replace( '[/' . $shortcode_name . ']', '', $tags_data );
821 }
822 $tags_data = str_replace( '/]', ']', $tags_data );
823
824 $attributes = array();
825 $parse = shortcode_parse_atts( esc_html( $tags_data ) );
826
827 $temp_key = 'temp';
828 foreach ( $parse as $key => $value ) {
829 if ( ! is_integer( $key ) ) {
830 $temp_key = $key;
831 $attributes[ $key ] = self::remove_special_character_in_attributes( $value );
832 } else {
833 // ? join data with old key.
834 $attributes[ $temp_key ] = trim( ( $attributes[ $temp_key ] ?? '' ) . ' ' . self::remove_special_character_in_attributes( $value ) );
835 }
836 }
837 unset( $attributes['temp'] );
838
839 return $attributes;
840 }
841
842 /**
843 * Remove special character in attributes
844 *
845 * @param string $text Text string.
846 */
847 public static function remove_special_character_in_attributes( $text ) {
848 $text = str_replace( 'u0026', '&', $text );
849 $text = str_replace( array( '\\', 'u0022', '&quot;', 'u003c', 'u003e', '&lt;', '&gt;' ), '', $text );
850 return htmlspecialchars_decode( trim( $text, ']' ), ENT_QUOTES );
851 }
852
853 /**
854 * Check whether aawp list id is imported to Lasso or not
855 *
856 * @param int $id Aawp list id.
857 */
858 public static function check_aawp_list_is_imported( $id ) {
859 $lasso_db = new Lasso_DB();
860
861 $sql = '
862 SELECT lud.product_id
863 FROM ' . ( new Url_Details() )->get_table_name() . ' AS lud
864 LEFT JOIN ' . $lasso_db->posts . ' AS p
865 ON lud.lasso_id = p.ID
866 WHERE lud.product_id != \'\'
867 AND p.ID is not null
868 AND lud.product_type = \'' . Amazon_Api::PRODUCT_TYPE . '\'
869 ';
870 $row = $lasso_db->get_col( $sql );
871 $amazon_ids = $row ? $row : array();
872
873 $aawp_list = $lasso_db->get_aawp_list( $id );
874 $aawp_amazon_ids = $aawp_list->product_asins ?? '';
875 $aawp_amazon_ids = '' !== $aawp_amazon_ids ? explode( ',', $aawp_amazon_ids ) : array();
876
877 $same_elements = array_intersect( $aawp_amazon_ids, $amazon_ids );
878
879 return $aawp_amazon_ids === $same_elements;
880 }
881
882 /**
883 * Paginate items by a sql query
884 * Reset page number if results is empty.
885 *
886 * @param string $sql Sql query.
887 * @param int $page Number of page.
888 * @param int $limit Number of results. Default to 10.
889 */
890 public static function paginate( $sql, &$page, $limit = 10 ) {
891 $start_index = ( $page - 1 ) * $limit;
892 $pagination_sql = $sql . ' LIMIT ' . $start_index . ', ' . $limit;
893
894 if ( $page > 1 ) {
895 $result = Model::get_row( $pagination_sql );
896
897 if ( ! $result ) {
898 $page = 1;
899 $pagination_sql = $sql . ' LIMIT 0, ' . $limit;
900 }
901 }
902
903 return $pagination_sql;
904 }
905
906 /**
907 * Format "post title" to escape html and apply limit length.
908 *
909 * @param Title $title lasso urls title.
910 * @param int $limit_length limit length of title.
911 */
912 public static function format_post_title( $title, $limit_length = 200 ) {
913 $title = esc_html( $title );
914
915 if ( strlen( $title ) > $limit_length ) {
916 $title = substr( $title, 0, $limit_length ) . '...';
917 }
918
919 return $title;
920 }
921
922 /**
923 * Get unique post name of lasso post
924 *
925 * @param int $post_id Post id.
926 * @param string $post_name Post name.
927 */
928 public static function lasso_unique_post_name( $post_id, $post_name ) {
929 if ( intval( $post_id ) > 0 && ! empty( $post_name ) && self::the_slug_exists( $post_name, $post_id ) ) {
930 $post_name .= '-link';
931 $post_name = wp_unique_post_slug( $post_name, $post_id, 'publish', Constant::LASSO_POST_TYPE, 0 );
932 }
933
934 return $post_name;
935 }
936
937 /**
938 * Get plugin status result
939 *
940 * @param string $plugin Plugin key.
941 * @return bool
942 */
943 public static function get_is_plugin_active( $plugin ) {
944 $cache_result = Cache_Per_Process::get_instance()->get_cache( 'is_plugin_active_' . md5( $plugin ), null );
945 if ( null !== $cache_result ) {
946 return $cache_result;
947 }
948
949 if ( ! function_exists( 'is_plugin_active' ) ) {
950 include_once ABSPATH . 'wp-admin/includes/plugin.php';
951 }
952
953 $result = \is_plugin_active( $plugin );
954 Cache_Per_Process::get_instance()->set_cache( 'is_plugin_active_' . md5( $plugin ), $result );
955
956 return $result;
957 }
958
959 /**
960 *
961 * Check plugin earnist is loaded https://www.getearnist.com.
962 *
963 * @return bool
964 */
965 public static function is_earnist_plugin_loaded() {
966 return self::get_is_plugin_active( 'earnist/earnist.php' );
967 }
968
969 /**
970 *
971 * Check plugin Shortcode Star Rating is loaded https://github.com/modshrink/shortcode-star-rating.
972 *
973 * @return bool
974 */
975 public static function is_shortcode_start_rating_plugin_loaded() {
976 return self::get_is_plugin_active( 'shortcode-star-rating/shortcode-star-rating.php' );
977 }
978
979 /**
980 * Check plugin "Easy Table of Contents" is activated
981 *
982 * @return bool
983 */
984 public static function is_plugin_easy_table_of_contents_activated() {
985 return self::get_is_plugin_active( 'easy-table-of-contents/easy-table-of-contents.php' );
986 }
987
988 /**
989 * Check if Gravity Perks plugin is active.
990 *
991 * @return bool
992 */
993 public static function is_gravity_perks_plugin_active() {
994 return self::get_is_plugin_active( 'gravityperks/gravityperks.php' );
995 }
996
997 /**
998 * Check if Ezoic plugin is active.
999 *
1000 * @return bool
1001 */
1002 public static function is_ezoic_plugin_active() {
1003 return self::get_is_plugin_active( 'ezoic-integration/ezoic-integration.php' );
1004 }
1005
1006 /**
1007 * Check if WP Rocket - Lazyload is enabled.
1008 *
1009 * @return bool
1010 */
1011 public static function is_wp_rocket_lazyload_image_enabled() {
1012 if ( self::get_is_plugin_active( 'wp-rocket/wp-rocket.php' ) ) {
1013 // ? Check if layzyload enabled
1014 $wp_rocket_settings = get_option( 'wp_rocket_settings', array() );
1015 if ( $wp_rocket_settings['lazyload'] ?? false ) {
1016 return true;
1017 }
1018 }
1019
1020 return false;
1021 }
1022
1023 /**
1024 * Check whether current page is WP post page
1025 */
1026 public static function is_wordpress_post() {
1027 global $pagenow;
1028
1029 $get = wp_unslash( $_GET ); // phpcs:ignore
1030 $action = $get['action'] ?? '';
1031 $add_new_page = 'post-new.php' === $pagenow;
1032 $edit_page = 'post.php' === $pagenow && 'edit' === $action;
1033 $post_type = $get['post_type'] ?? '';
1034
1035 if ( ( 'edit.php' === $pagenow || $add_new_page ) && '' === $post_type ) {
1036 $post_type = 'post';
1037 } elseif ( $add_new_page ) {
1038 $post_type = $get['post_type'] ?? $post_type;
1039 } elseif ( $edit_page ) {
1040 $post_id = intval( $get['post'] ?? 0 );
1041 $post_type = $post_id > 0 ? get_post_type( $post_id ) : $post_type;
1042 }
1043
1044 if ( 'term.php' === $pagenow ) {
1045 $post_type = '';
1046 }
1047
1048 return 'post' === $post_type || 'page' === $post_type;
1049 }
1050
1051 /**
1052 * Cast the value to boolean
1053 *
1054 * @param bool|string $value A string boolean like "true" or "false".
1055 *
1056 * @return bool
1057 */
1058 public static function cast_to_boolean( $value ) {
1059 return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
1060 }
1061
1062 /**
1063 * Get CPU load of server/hosting
1064 */
1065 public static function get_cpu_load() {
1066 $load = null;
1067
1068 if ( stristr( PHP_OS, 'win' ) ) {
1069 $cmd = 'wmic cpu get loadpercentage /all';
1070 @exec( $cmd, $output ); // phpcs:ignore
1071
1072 if ( $output ) {
1073 foreach ( $output as $line ) {
1074 if ( $line && preg_match( '/^[0-9]+$/', $line ) ) {
1075 $load = $line;
1076 break;
1077 }
1078 }
1079 }
1080 } else {
1081 try {
1082 if ( @is_readable( '/proc/stat' ) ) { // phpcs:ignore
1083 $cached_cpu_load = Cache_Per_Process::get_instance()->get_cache( 'cpu_load', null );
1084 if ( $cached_cpu_load ) {
1085 $stat_data1 = $cached_cpu_load;
1086 } else {
1087 $stat_data1 = self::get_server_load_linux_data();
1088 }
1089
1090 // ? Collect 2 samples - each with 1 second period
1091 // ? See: https://de.wikipedia.org/wiki/Load#Der_Load_Average_auf_Unix-Systemen
1092 sleep( 1 );
1093
1094 $stat_data2 = self::get_server_load_linux_data();
1095 Cache_Per_Process::get_instance()->set_cache( 'cpu_load', $stat_data2 );
1096
1097 if ( ( ! is_null( $stat_data1 ) ) && ( ! is_null( $stat_data2 ) ) ) {
1098 // ? Get difference
1099 $stat_data2[0] -= $stat_data1[0];
1100 $stat_data2[1] -= $stat_data1[1];
1101 $stat_data2[2] -= $stat_data1[2];
1102 $stat_data2[3] -= $stat_data1[3];
1103
1104 // ? Sum up the 4 values for User, Nice, System and Idle and calculate
1105 // ? the percentage of idle time (which is part of the 4 values!)
1106 $cpu_time = $stat_data2[0] + $stat_data2[1] + $stat_data2[2] + $stat_data2[3];
1107
1108 // ? Invert percentage to get CPU time, not idle time
1109 $load = 100 - ( $stat_data2[3] * 100 / max( $cpu_time, 1 ) );
1110 }
1111 }
1112 } catch ( Exception $e ) {
1113 $load = 0; // Just run because we can't detect CPU load.
1114 }
1115 }
1116
1117 return round( $load, 2 );
1118 }
1119
1120 /**
1121 * Get server load linux data
1122 */
1123 private static function get_server_load_linux_data() {
1124 if ( @is_readable( '/proc/stat' ) ) { // phpcs:ignore
1125 $stats = @file_get_contents( '/proc/stat' ); // phpcs:ignore
1126
1127 if ( false !== $stats ) {
1128 // ? Remove double spaces to make it easier to extract values with explode()
1129 $stats = preg_replace( '/[[:blank:]]+/', ' ', $stats );
1130
1131 // ? Separate lines
1132 $stats = str_replace( array( "\r\n", "\n\r", "\r" ), "\n", $stats );
1133 $stats = explode( "\n", $stats );
1134
1135 // ? Separate values and find line for main CPU load
1136 foreach ( $stats as $stat_line ) {
1137 $stat_line_data = explode( ' ', trim( $stat_line ) );
1138
1139 // ? Found
1140 if ( count( $stat_line_data ) >= 5 && 'cpu' === $stat_line_data[0] ) {
1141 return array(
1142 $stat_line_data[1],
1143 $stat_line_data[2],
1144 $stat_line_data[3],
1145 $stat_line_data[4],
1146 );
1147 }
1148 }
1149 }
1150 }
1151
1152 return null;
1153 }
1154
1155 /**
1156 * Remove unexpected character from post title
1157 *
1158 * @param string $post_title Post title.
1159 * @return string
1160 */
1161 public static function remove_unexpected_characters_from_post_title( $post_title ) {
1162 // ? Remove unexpected character.
1163 $post_title = preg_replace( "/[^A-Za-z0-9\s`~!@#$%^&:;\/\?\"\'\+\=\.\,\-\_\*\(\)\|\[\]\<\>\{\}\\\]/", ' ', $post_title );
1164 // ? Remove duplicated space.
1165 $post_title = preg_replace( '/\s\s+/', ' ', $post_title );
1166
1167 return $post_title;
1168 }
1169
1170 /**
1171 * Check whether this install is new
1172 */
1173 public static function is_new_install() {
1174 return boolval( get_option( Enum::LASSO_LITE_ACTIVE ) );
1175 }
1176
1177 /**
1178 * Build image lazyload attributes
1179 *
1180 * @return string
1181 */
1182 public static function build_img_lazyload_attributes() {
1183 $result = 'loading="lazy"'; // ? WP default lazyload.
1184
1185 if ( self::is_ezoic_plugin_active() ) {
1186 $result = 'class="ezlazyload"';
1187 } elseif ( self::is_wp_rocket_lazyload_image_enabled() ) {
1188 $result = 'class="rocket-lazyload"';
1189 }
1190
1191 return $result;
1192 }
1193
1194 /**
1195 * Check whether import page should display
1196 *
1197 * @return bool
1198 */
1199 public static function should_show_import_page() {
1200 return ! empty( ( new Lasso_DB() )->get_import_plugins( true ) ) ? true : false;
1201 }
1202
1203 /**
1204 * Get brag icon
1205 *
1206 * @param bool $force_to_show Force to show the brag. Default to false.
1207 */
1208 public static function get_brag_icon( $force_to_show = false ) {
1209 $cache_key = 'lasso_lite_brag_icon';
1210 $brag_cache = Cache_Per_Process::get_instance()->get_cache( $cache_key, '' );
1211
1212 if ( $brag_cache ) {
1213 return $brag_cache;
1214 }
1215
1216 $lasso_settings = Setting::get_settings();
1217
1218 $enable_brag_mode = $lasso_settings['enable_brag_mode'] ?? false;
1219 $lasso_url = $lasso_settings['lasso_affiliate_URL'] ?? false;
1220
1221 if ( $lasso_url && ( $force_to_show || $enable_brag_mode ) ) {
1222 $icon_brag = esc_url( SIMPLE_URLS_URL . '/admin/assets/images/lasso-icon-brag.svg' );
1223 $lasso_affiliate_url = self::add_params_to_url( $lasso_url, array( 'utm_source' => 'brag' ) );
1224 $img_attr = self::build_img_lazyload_attributes();
1225 $icon = '
1226 <a class="lasso-brag" href="' . $lasso_affiliate_url . '" target="_blank" rel="nofollow noindex">
1227 <img src="' . $icon_brag . '" ' . $img_attr . ' alt="Lasso Brag" width="30" height="30">
1228 </a>
1229 ';
1230
1231 Cache_Per_Process::get_instance()->set_cache( $cache_key, $icon );
1232
1233 return $icon;
1234 }
1235
1236 return '';
1237 }
1238
1239 /**
1240 * Add params to URL
1241 *
1242 * @param string $url URL.
1243 * @param array $params Params.
1244 */
1245 public static function add_params_to_url( $url, $params ) {
1246 // ? parse url
1247 $parse = wp_parse_url( $url );
1248 parse_str( $parse['query'] ?? '', $query );
1249
1250 $query = array_merge( $query, $params );
1251 $query = self::get_query_from_array( $query );
1252 $parse['query'] = $query;
1253
1254 return self::get_url_from_parse( $parse );
1255
1256 }
1257
1258 /**
1259 * Get final url in the url
1260 * Example: https://affiliate.com/redirect?url=https://getlasso.co
1261 *
1262 * @param string $url URL.
1263 */
1264 public static function get_final_url_from_url_param( $url ) {
1265 if ( ! self::validate_url( $url ) ) {
1266 return false;
1267 }
1268
1269 $final_url = false;
1270 $base_domain = self::get_base_domain( $url );
1271
1272 if ( self::is_shareasale_url( $url ) ) {
1273 $final_url = self::get_argument_from_url( $url, 'urllink' );
1274 } elseif ( 'pntra.com' === $base_domain ) {
1275 $final_url = self::get_argument_from_url( $url, 'url' );
1276 } elseif ( 'wordseed.com' === $base_domain ) {
1277 $final_url = self::get_argument_from_url( $url, 'url' );
1278 } elseif ( 'titan.fitness' === $base_domain ) {
1279 $final_url = 'https://www.titan.fitness' . self::get_argument_from_url( $url, 'redirect' );
1280 } else { // ? other urls
1281 $parse = wp_parse_url( $url );
1282 $queries = array();
1283 parse_str( $parse['query'] ?? '', $queries );
1284 foreach ( $queries as $key => $param ) {
1285 if ( 'referrer' === $key ) {
1286 continue;
1287 }
1288
1289 $param = str_replace( ' ', '%20', $param );
1290 if ( self::validate_url( $param ) ) {
1291 $final_url = $param;
1292 break;
1293 }
1294 }
1295 }
1296 $final_url = self::add_https( $final_url );
1297
1298 if ( empty( $final_url ) || ! self::validate_url( $final_url ) ) {
1299 $final_url = false;
1300 } else {
1301 $final_url = trim( $final_url, '/' );
1302 }
1303
1304 return $final_url;
1305 }
1306
1307 /**
1308 * Check whether url is shareasale domain or not
1309 *
1310 * @param string $url URL.
1311 * @return bool
1312 */
1313 public static function is_shareasale_url( $url ) {
1314 $allow_domains = array( 'shareasale.com', 'shareasale-analytics.com' );
1315 $domain = self::get_base_domain( $url );
1316
1317 return in_array( $domain, $allow_domains, true );
1318 }
1319
1320 /**
1321 * Check importable
1322 *
1323 * @return bool
1324 */
1325 public static function is_importable() {
1326 $lasso_lite_db = new Lasso_DB();
1327 $sql = $lasso_lite_db->get_importable_urls_query( true );
1328 $post = Model::get_row( $sql );
1329
1330 if ( ! empty( $post ) ) {
1331 $post->post_title = self::format_post_title( $post->post_title ?? '' );
1332 $post->shortcode = '';
1333
1334 // ? Get import target permalinks
1335 $post = self::format_importable_data( $post );
1336
1337 // ? Check first record from list import
1338 return 'checked' !== $post->check_status;
1339 }
1340
1341 return false;
1342 }
1343
1344 /**
1345 * Check whether Lite is using new or old UI
1346 *
1347 * @return bool true: new UI. false: old UI.
1348 */
1349 public static function is_lite_using_new_ui() {
1350 $new_ui = get_option( Enum::SWITCH_TO_NEW_UI );
1351 $new_ui = self::cast_to_boolean( $new_ui );
1352
1353 if ( ! $new_ui ) {
1354 return false;
1355 }
1356
1357 return true;
1358 }
1359
1360 /**
1361 * Whether show Request Review at the top of the page
1362 */
1363 public static function show_request_review() {
1364 $link_count = SURL::total();
1365
1366 $lasso_review_allow = self::cast_to_boolean( self::get_option( Constant::LASSO_OPTION_REVIEW_ALLOW, '1' ) );
1367 $lasso_review_snooze = self::cast_to_boolean( self::get_option( Constant::LASSO_OPTION_REVIEW_SNOOZE, '0' ) );
1368 $lasso_review_link_count = intval( self::get_option( Constant::LASSO_OPTION_REVIEW_LINK_COUNT, $link_count ) );
1369
1370 $show = ! $lasso_review_snooze && $link_count >= 20;
1371 $snooze_but_show = $lasso_review_snooze && $link_count - $lasso_review_link_count >= 20;
1372
1373 if ( ! $lasso_review_allow ) {
1374 return false;
1375 }
1376
1377 if ( $show || $snooze_but_show ) {
1378 return true;
1379 }
1380
1381 return false;
1382 }
1383
1384 /**
1385 * Get Ajax URL
1386 */
1387 public static function get_ajax_url() {
1388 return admin_url( 'admin-ajax.php' );
1389 }
1390
1391 /**
1392 * Get price value from price text including currency symbol.
1393 *
1394 * @param string $price_text Price text.
1395 * @param string $price_symbol Price symbol.
1396 * @return mixed|string
1397 */
1398 public static function get_price_value_from_price_text( $price_text, $price_symbol = '' ) {
1399 if ( preg_match( '/[]|R\$|TL|kr|zł/', $price_text ) || in_array( $price_symbol, array( '', 'R$', 'TL', 'kr', '' ), true ) ) {
1400 // ? For price use , as decimal separator and . as thousands separator.
1401 $replace_character = '.';
1402 $reg_pattern = '/\d+,?\d*/';
1403 } else {
1404 // ? For price use . as decimal separator and , as thousands separator.
1405 $replace_character = ',';
1406 $reg_pattern = '/\d+\.?\d*/';
1407 }
1408
1409 $price_without_thousands_separator = str_replace( $replace_character, '', $price_text );
1410 preg_match( $reg_pattern, $price_without_thousands_separator, $matches );
1411
1412 // ? Final format to general float number by replace ',' to '.'.
1413 return isset( $matches[0] ) ? str_replace( ',', '.', $matches[0] ) : '';
1414 }
1415
1416 /**
1417 * Get currency symbol from ISO currency code
1418 *
1419 * @param string $iso Iso currency code.
1420 * @return string
1421 */
1422 public static function get_currency_symbol_from_iso_code( $iso ) {
1423 $iso = strtoupper( $iso );
1424 $result = '$';
1425
1426 $currencies = array(
1427 'USD' => '$',
1428 'AUD' => '$',
1429 'CAD' => '$',
1430 'EUR' => '',
1431 'MXN' => '$',
1432 'CNY' => '¥',
1433 'JPY' => '¥',
1434 'INR' => '',
1435 'SEK' => 'kr',
1436 'BRL' => 'R$',
1437 'TRY' => 'TL',
1438 'GBP' => '£',
1439 'PLN' => '',
1440 'EGP' => '',
1441 'SGD' => 'S$',
1442 'AED' => 'AED',
1443 );
1444
1445 return isset( $currencies[ $iso ] ) ? $currencies[ $iso ] : $result;
1446 }
1447 }
1448