PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 154
Lasso Lite – Affiliate Link Manager & Product Displays v154
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 / pages / class-hook.php

class-hook.php in Lasso Lite – Affiliate Link Manager & Product Displays 154, at pages/class-hook.php

1,427 lines 48.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * SURLs - Hook.
4 *
5 * @package Pages
6 */
7
8 namespace LassoLite\Pages;
9
10 use LassoLite\Admin\Constant;
11
12 use LassoLite\Classes\Amazon_Api;
13 use LassoLite\Classes\Enum;
14 use LassoLite\Classes\Helper;
15 use LassoLite\Classes\Page;
16 use LassoLite\Classes\Setting;
17 use LassoLite\Classes\Shortcode;
18 use LassoLite\Classes\License;
19 use LassoLite\Classes\Realtime_Click;
20
21 /**
22 * Hook.
23 */
24 class Hook {
25 /**
26 * Current template
27 *
28 * @var string $current_template
29 */
30 public $current_template = '';
31
32 /**
33 * Is show old menus
34 *
35 * @var bool $show_old_menus
36 */
37 private $show_old_menus = false;
38
39 /**
40 * Register hooks
41 */
42 public function register_hooks() {
43 add_action( 'admin_init', array( $this, 'amazon_api_pre_populated_automatically' ) );
44 add_action( 'init', array( $this, 'register_taxonomy' ) );
45 add_action( 'admin_menu', array( $this, 'build_admin_menu' ), 2 );
46 add_action( 'init', array( $this, 'lasso_register_connect_snippet_rewrite' ) );
47 add_action( 'upgrader_process_complete', array( $this, 'lasso_connect_snippet_flush_on_upgrade' ), 10, 2 );
48
49 $lasso_shortcode = new Shortcode();
50 add_shortcode( 'lasso', array( $lasso_shortcode, 'lasso_lite_core_shortcode' ) );
51 add_filter( 'pre_do_shortcode_tag', array( $this, 'filter_pre_do_shortcode_lasso_lite' ), 10, 4 );
52
53 add_action( 'wp_head', array( $this, 'lasso_custom_css' ) ); // ? frontend
54 add_action( 'admin_head', array( $this, 'lasso_custom_css' ) ); // ? admin
55 add_action( 'admin_head', array( $this, 'lasso_custom_menu' ) ); // ? admin
56 add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts_frontend' ) ); // ? frontend
57 add_filter( 'body_class', array( $this, 'filter_body_class_lasso_lite_version' ) ); // ? frontend
58
59 add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ) );
60 add_action( 'admin_enqueue_scripts', array( $this, 'add_styles' ) );
61
62 // ? re-order submenu pages
63 add_filter( 'custom_menu_order', array( $this, 'lasso_order_submenu' ) );
64
65 // ? lasso gutenberg block
66 add_action( 'enqueue_block_assets', array( $this, 'lasso_block_editor_css_variables' ) );
67 add_action( 'enqueue_block_editor_assets', array( $this, 'lasso_lite_gutenberg_block' ) );
68
69 // ? Elementor
70 add_action( 'elementor/init', array( $this, 'elementor_init' ) );
71
72 // ? add Lasso button into TinyMCE
73 add_filter( 'mce_external_plugins', array( $this, 'lasso_add_tinymce_plugin' ) );
74 add_filter( 'mce_buttons', array( $this, 'lasso_lite_register_my_tc_button' ) );
75
76 add_filter( 'simple_urls_redirect_url', array( $this, 'lasso_lite_redirect' ), 10, 1 );
77
78 add_action( 'admin_footer', array( $this, 'lasso_lite_admin_footer_editor_gutenberg' ) );
79
80 add_filter( 'rest_pre_echo_response', array( $this, 'update_post_type_gutenberg' ), 200, 3 );
81
82 add_filter( 'wp_link_query', array( $this, 'update_post_type_classic_editor' ), 10, 1 );
83 add_filter( 'update_footer', '__return_empty_string', 11 );
84
85 // ? WP Rocket
86 add_filter( 'rocket_exclude_js', array( $this, 'exclude_lasso_performance_js_from_rocket_cache' ) );
87
88 add_filter( 'query_vars', array( $this, 'lasso_connect_snippet_query_var' ) );
89
90 // Serve vanity JS path /js/snippet.min.js via plugin handler (run as early as possible)
91 add_action( 'template_redirect', array( $this, 'serve_connect_snippet' ), 0 );
92
93 $setting = new Setting();
94 if ( $setting->is_surls_page() ) {
95 // ? plugin: Pretty Links Pro
96 if ( class_exists( 'PrliUpdateController' ) ) {
97 Helper::remove_action( 'admin_notices', array( 'PrliUpdateController', 'activation_warning' ) );
98 }
99 }
100
101 // ? FIX: CONFLICT WITH OTHER PLUGINS
102 // ? remove js files from other plugins in Lasso pages
103 if ( $setting->is_surls_page() ) {
104 // ? plugin: SEO Booster
105 if ( class_exists( 'Seobooster2' ) ) {
106 remove_action( 'admin_print_footer_scripts', array( 'Seobooster2', 'admin_print_footer_scripts' ) );
107 }
108
109 // ? plugin: ShortPixel Adaptive Images
110 if ( class_exists( 'ShortPixel\AI\Notice' ) && class_exists( 'ShortPixelAI' ) ) {
111 $spai = \ShortPixelAI::_();
112 $spain = \ShortPixel\AI\Notice::_( $spai );
113 remove_action( 'admin_footer', array( $spain, 'enqueueAdminScripts' ) );
114 remove_action( 'admin_bar_menu', array( $spai, 'toolbar_styles' ), 999 );
115 }
116
117 // ? plugin: WZone - WooCommerce Amazon Affiliates (WooZone)
118 if ( class_exists( 'WooZone' ) ) {
119 global $WooZone; // phpcs:ignore
120 remove_action( 'init', array( $WooZone, 'initThePlugin' ), 5 ); // phpcs:ignore
121 }
122
123 // ? plugin: Client Portal
124 $lasso_page = Helper::GET()['page'] ?? ''; // phpcs:ignore
125 if ( class_exists( 'CCGClientPortal' ) && Helper::add_prefix_page( Enum::PAGE_DASHBOARD ) === $lasso_page ) {
126 global $zohopwp; // phpcs:ignore
127 remove_action( 'admin_menu', array( $zohopwp, 'ccgclientportal_admin_menu' ) ); // phpcs:ignore
128 }
129
130 if ( class_exists( 'wpe_admin_pointers' ) ) {
131 global $wpe_admin_pointers; // phpcs:ignore
132 remove_action( 'admin_enqueue_scripts', array( $wpe_admin_pointers, 'custom_admin_pointers_header' ) ); // phpcs:ignore
133 }
134
135 // ? plugin: tagDiv Composer
136 if ( function_exists( 'td_change_backbone_js_hook' ) ) {
137 remove_action( 'print_media_templates', 'td_change_backbone_js_hook' );
138 }
139
140 // ? plugin: Pretty Links Pro
141 if ( class_exists( 'PrliUpdateController' ) ) {
142 Helper::remove_action( 'admin_notices', array( 'PrliUpdateController', 'activation_warning' ) );
143 }
144
145 if ( Helper::is_earnist_plugin_loaded() ) {
146 Helper::remove_action( 'admin_print_footer_scripts', array( 'EarnistProductPicker', 'register_tinymce_quicktags' ) );
147 }
148 if ( Helper::is_shortcode_start_rating_plugin_loaded() ) {
149 global $ShortcodeStarRating; // phpcs:ignore
150 remove_action( 'admin_print_footer_scripts', array( $ShortcodeStarRating, 'appthemes_add_quicktags' ) ); // phpcs:ignore
151 }
152 if ( Helper::is_plugin_easy_table_of_contents_activated() ) {
153 add_action( 'admin_enqueue_scripts', array( $this, 'remove_easy_table_of_content_action_admin_print_footer_scripts' ), 20 ); // ? admin
154 }
155
156 remove_all_actions( 'admin_footer' );
157 add_action( 'admin_footer', array( $this, 'lasso_print_media_templates' ) );
158 add_action( 'admin_footer', array( $this, 'lasso_organize_menu' ), 100 );
159 }
160
161 // ? fix conflict with plugin Affiliate URL Automation
162 if ( class_exists( 'AffiliateURLs' ) ) {
163 global $AffiliateURLs; // phpcs:ignore
164 remove_filter( 'the_content', array( &$AffiliateURLs, 'the_content' ), 12 ); // phpcs:ignore
165 }
166
167 // ? remove js files from other plugins in WP post/page pages
168 if ( Helper::is_wordpress_post() ) {
169 if ( class_exists( 'WooZone' ) ) {
170 global $WooZone; // phpcs:ignore
171 remove_action( 'init', array( $WooZone, 'initThePlugin' ), 5 ); // phpcs:ignore
172 }
173 }
174
175 if ( Helper::is_gravity_perks_plugin_active() ) {
176 remove_action( 'admin_print_footer_scripts', array( 'GWPerks', 'welcome_pointer_script' ), 10 ); // phpcs:ignore
177 }
178
179 $license_active = License::get_license_status();
180 if ( false === $license_active ) {
181 add_action( 'admin_notices', array( $this, 'lasso_lite_custom_dashboard_banner' ) );
182 }
183
184 add_action( 'wp_footer', array( $this, 'lasso_lite_event_tracking' ) );
185 add_action( 'admin_footer', array( $this, 'open_links_in_new_tab' ) );
186 }
187
188
189 /**
190 * Check if Lasso Lite shortcode then render, else return false to running the next shortcode processes
191 *
192 * @param boolean $false This is the default value that will be returned if the shortcode is not found.
193 * @param string $tag The shortcode tag.
194 * @param array $attr The attributes passed to the shortcode like so: [shortcode attr1="value" /].
195 * @param array $m The regex for the shortcode.
196 *
197 * @return string|boolean return value is the output of the lasso_lite_core_shortcode function.
198 */
199 public function filter_pre_do_shortcode_lasso_lite( $false, $tag, $attr, $m ) {
200 if ( 'lasso' === $tag ) {
201 return ( new Shortcode() )->lasso_lite_core_shortcode( $attr );
202 }
203
204 return false;
205 }
206
207 /**
208 * Admin menu
209 */
210 public function build_admin_menu() {
211 $get_page = Helper::GET()['page'] ?? '';
212 $post_type = Helper::GET()['post_type'] ?? '';
213
214 $dashboard_slug = Helper::add_prefix_page( Enum::PAGE_DASHBOARD );
215 // ? Switch to new/old UI: We should easy redirect to the suitable dashboard.
216 if ( 'switch-new-ui' === $get_page ) {
217 update_option( Enum::SWITCH_TO_NEW_UI, 1 );
218 wp_redirect( Page::get_page_url( $dashboard_slug ) ); // phpcs:ignore
219 exit;
220 } elseif ( 'switch-old-ui' === $get_page ) {
221 update_option( Enum::LASSO_LITE_ACTIVE, 0 ); // ? fix conflict with L.235
222 update_option( Enum::SWITCH_TO_NEW_UI, 0 );
223 wp_redirect( Page::get_page_url() ); // phpcs:ignore
224 exit;
225 }
226
227 // ? Reset onboarding for testing (one-shot: strip QA param so step persistence works on reload)
228 $reset_onboarding = Helper::GET()[ Enum::RESET_WELCOME_PAGE ] ?? '';
229 if ( $reset_onboarding ) {
230 Helper::reset_onboarding_for_testing();
231
232 wp_safe_redirect( remove_query_arg( Enum::RESET_WELCOME_PAGE ) ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
233 exit;
234 }
235
236 // ? Reset request review for testing
237 $reset_review = Helper::GET()[ Enum::RESET_REQUEST_REVIEW ] ?? '';
238 if ( $reset_review ) {
239 Helper::update_option( Constant::LASSO_OPTION_REVIEW_ALLOW, 1 );
240 Helper::update_option( Constant::LASSO_OPTION_REVIEW_SNOOZE, 0 );
241 Helper::update_option( Constant::LASSO_OPTION_REVIEW_LINK_COUNT, 0 );
242 }
243
244 // ? The new Lasso Lite active should be redirect to welcome page
245 $onboarding_page = Helper::add_prefix_page( Enum::PAGE_ONBOARDING );
246 $should_redirect_to_welcome_page = self::should_redirect_to_welcome_page();
247 if ( SIMPLE_URLS_SLUG === $post_type && $onboarding_page !== $get_page && $should_redirect_to_welcome_page ) {
248 wp_redirect( Page::get_page_url( $onboarding_page ) ); // phpcs:ignore
249 exit;
250 } elseif ( $onboarding_page === $get_page && ! $should_redirect_to_welcome_page ) {
251 wp_redirect( Page::get_page_url( $dashboard_slug ) ); // phpcs:ignore
252 exit;
253 }
254
255 // ? Redirect to dashboard page if page is "surl-dashboard" and missing "post_type" parameter
256 if ( $dashboard_slug === $get_page && ! $post_type ) {
257 wp_redirect( Page::get_page_url( $dashboard_slug ) ); // phpcs:ignore
258 exit;
259 }
260
261 // ? If the customer install from new UI, we should only show the new UI's menus without switch UI feature
262 if ( get_option( Enum::LASSO_LITE_ACTIVE ) ) {
263 update_option( Enum::SWITCH_TO_NEW_UI, 1 );
264 $this->apply_new_ui_menus();
265 } else { // ? The old client that upgrade to the new UI
266 $switch_ui = 'switch-old-ui';
267 $page_title = 'Switch to old UI';
268
269 // ? The client was switched to new UI by click "Switch To New UI"
270 if ( Helper::is_lite_using_new_ui() ) {
271 $this->apply_new_ui_menus();
272 } else { // ? The client was still using the old UI
273 $this->show_old_menus = true;
274 $switch_ui = 'switch-new-ui';
275 $page_title = 'Switch to new UI';
276 }
277
278 add_submenu_page(
279 'edit.php?post_type=' . SIMPLE_URLS_SLUG,
280 $page_title,
281 $page_title,
282 'manage_options',
283 Page::get_page_url( $switch_ui )
284 );
285 }
286 }
287
288 /**
289 * Apply new UI's menus
290 */
291 private function apply_new_ui_menus() {
292 $get_page = Helper::GET()['page'] ?? '';
293 $post_type = Helper::GET()['post_type'] ?? '';
294
295 // ? Redirect to new UI's Dashboard if new UI enabled and the link is old UI
296 if ( SIMPLE_URLS_SLUG === $post_type && ! $get_page ) {
297 wp_redirect( Page::get_lite_page_url( Enum::PAGE_DASHBOARD ) ); // phpcs:ignore
298 exit;
299 }
300
301 $new_ui_pages = Helper::available_pages();
302 $parent_slug = 'edit.php?post_type=' . SIMPLE_URLS_SLUG;
303 foreach ( $new_ui_pages as $page ) {
304 if ( $get_page === $page->slug ) {
305 $this->current_template = $page->template;
306 }
307
308 add_submenu_page(
309 $parent_slug,
310 $page->title,
311 $page->title,
312 'manage_options',
313 $page->slug,
314 array( $this, 'render_html' )
315 );
316 }
317 }
318
319
320 /**
321 * Render html for pages
322 */
323 public function render_html() {
324 Helper::include_with_variables( Helper::get_path_views_folder() . $this->current_template, array(), false );
325 }
326
327 /**
328 * Load css files
329 */
330 public function add_styles() {
331 if ( ! Helper::is_lite_using_new_ui() ) {
332 return;
333 }
334
335 $setting = new Setting();
336 // @codingStandardsIgnoreStart
337
338 // ? Everywhere in the Lasso Lite post-type (add/edit/reports/settings/wizard)
339 if ( $setting->is_surls_page() ) {
340 Helper::enqueue_style( 'bootstrap-css', 'bootstrap.min.css' );
341 Helper::enqueue_style( 'bootstrap-select-css', 'bootstrap-select.min.css' );
342 Helper::enqueue_style( 'simple-panigation-css', 'simplePagination.css' );
343 Helper::enqueue_style( 'select2-css', 'select2.min.css' );
344 Helper::enqueue_style( 'lasso-quill', 'quill.snow.css' );
345 Helper::enqueue_style( 'lasso-lite', 'lasso-lite.css' );
346 Helper::enqueue_style( 'lasso-display-modal', 'lasso-display-modal.css' );
347 Helper::enqueue_style( 'lasso-dashboard', 'lasso-dashboard.css' );
348 Helper::enqueue_style( 'lasso-lite-custom', 'lite-custom.css' );
349 }
350
351 if ( $setting->is_setting_amazon_page() ) {
352 Helper::enqueue_style( 'settings-amazon-modal', 'settings-amazon-modal.css', array( SIMPLE_URLS_SLUG . '-lasso-lite-custom' ) );
353 }
354
355 if ( $setting->is_setting_display_page() || $setting->is_setting_onboarding_page() ) {
356 Helper::enqueue_style( 'spectrum', 'spectrum.min.css' );
357 }
358
359 // ? LOAD LASSO DISPLAY MODAL CSS ON POST AND PAGE EDIT ONLY
360 if ( $setting->is_wordpress_post() || $setting->is_custom_post() ) {
361 Helper::enqueue_style( 'bootstrap-grid-css', 'bootstrap-grid.min.css' );
362 Helper::enqueue_style( 'lasso-display-modal', 'lasso-display-modal.css' );
363 Helper::enqueue_style( 'simple-pagination', 'simplePagination.css' );
364 Helper::enqueue_style( 'lasso-quill', 'quill.snow.css' );
365 Helper::enqueue_style( 'lasso-lite', 'lasso-lite.css' );
366 Helper::enqueue_style( 'lasso-modal-css', 'lasso-modal.css' );
367 }
368
369 global $pagenow; // phpcs:ignore
370 if ( 'index.php' === $pagenow && $this->should_show_dashboard_promo_banner() ) {
371 Helper::enqueue_style( 'lasso-lite-admin', 'lasso-lite-admin.css' );
372 }
373
374 // @codingStandardsIgnoreEnd
375 }
376
377 /**
378 * Load js files
379 */
380 public function add_scripts() {
381 if ( ! Helper::is_lite_using_new_ui() ) {
382 return;
383 }
384 global $pagenow; // phpcs:ignore
385
386 $get = Helper::GET(); // phpcs:ignore
387 $page = $get['page'] ?? false;
388 $setting = new Setting();
389 $post_type = $setting->get['post_type'] ?? false;
390 $setting_data = Setting::get_settings();
391 $support_enabled = $setting_data[ Enum::SUPPORT_ENABLED ] ?? false;
392 $support_enabled = ! $support_enabled ? 1 : 0;
393 $data_passed_to_js = array(
394 'ajax_url' => admin_url( 'admin-ajax.php' ),
395 'site_url' => site_url(),
396 'plugin_url' => SIMPLE_URLS_URL,
397 'lasso_link' => Constant::LASSO_LINK,
398 'lasso_hub_url' => Constant::get_lasso_hub_url(),
399 'upgrade_url' => Constant::LASSO_UPGRADE_URL,
400 'rewrite_slug_default' => Enum::REWRITE_SLUG_DEFAULT,
401 'simple_urls_slug' => SIMPLE_URLS_SLUG,
402 'page_url_details' => Enum::PAGE_URL_DETAILS,
403 'setup_progress' => Helper::get_setup_progress_information(),
404 'optionsNonce' => wp_create_nonce( Constant::LASSO_LITE_NONCE . wp_salt() ),
405 'should_open_support_modal' => $support_enabled,
406 'amazon_tracking_id_regex' => Amazon_Api::TRACKING_ID_REGEX,
407 'is_onboard_page' => $setting->is_setting_onboarding_page(),
408 'onboarding_current_step' => $setting->is_setting_onboarding_page()
409 ? Helper::get_onboarding_current_step( Helper::should_show_import_page() )
410 : 'welcome',
411 'block_customize' => Constant::BLOCK_CUSTOMIZE,
412 'userId' => Helper::get_option( Constant::LASSO_ACCOUNT_USER_ID ),
413 );
414 $data_passed_to_js['realtime'] = Realtime_Click::get_js_config();
415
416 if ( $setting->is_setting_amazon_page() ) {
417 // Amazon settings only: edit.php?post_type=surl&page=surl-settings-amazon
418 $data_passed_to_js['lite_account_existing_always_post'] = true;
419 $data_passed_to_js['lite_amazon_settings_post_signup_verify_creators'] = true;
420 }
421
422 if ( SIMPLE_URLS_SLUG === $post_type ) {
423 wp_dequeue_script( 'up_admin_script' ); // ? fix js conflict with plugin: Download plugin
424 }
425
426 // @codingStandardsIgnoreStart
427 if ( 'index.php' === $pagenow && $this->should_show_dashboard_promo_banner() ) {
428 $data_passed_to_js = array(
429 'ajax_url' => admin_url( 'admin-ajax.php' ),
430 'optionsNonce' => wp_create_nonce( Constant::LASSO_LITE_NONCE . wp_salt() ),
431 );
432 wp_enqueue_script( 'jquery' );
433 wp_localize_script( 'jquery', 'lassoLiteOptionsData', $data_passed_to_js );
434 Helper::enqueue_script( 'lasso-lite-admin-js', 'lasso-lite-admin.js', array( 'jquery' ), false );
435 }
436
437 if ( $setting->is_wordpress_post() || $setting->is_surls_page() || $setting->is_custom_post() ) {
438 wp_enqueue_script( 'jquery-migrate' ); // ? fix jQuery(...).live is not a function
439 wp_enqueue_script( 'jquery' );
440 wp_enqueue_script( 'jquery-effects-core' );
441 wp_enqueue_script( 'jquery-ui-core' );
442 wp_enqueue_script( 'jquery-ui-sortable' );
443 wp_enqueue_script( 'jquery-ui-tooltip' );
444
445 Helper::enqueue_script( 'lasso-icons', 'fontawesome.min.js', array( 'jquery' ) );
446 Helper::enqueue_script( 'lasso-icons-regular', 'regular.min.js', array( 'jquery' ) );
447 Helper::enqueue_script( 'lasso-icons-solid', 'solid.min.js', array( 'jquery' ) );
448 Helper::enqueue_script( 'lasso-icons-brands', 'brands.min.js', array( 'jquery' ) );
449 Helper::enqueue_script( 'circle-progress', 'circle-progress.min.js', array( 'jquery' ) );
450
451 Helper::enqueue_script( 'lasso-quill', 'quill.min.js' );
452 Helper::enqueue_script( 'lasso-modal-js', 'lasso-modal.js' );
453
454 if ( ! Helper::is_classic_editor() && ( $setting->is_wordpress_post() || $setting->is_custom_post() ) ) {
455 Helper::enqueue_script( 'lasso-lite-display-modal', 'lasso-lite-display-modal.js' );
456 }
457
458 wp_enqueue_media();
459 Helper::enqueue_script( 'moment-js', 'moment.min.js', array( 'jquery' ) );
460 Helper::enqueue_script( 'select2-js', 'select2.full.min.js', array( 'jquery' ) );
461 Helper::enqueue_script( SIMPLE_URLS_SLUG . '-jq-auto-complete-js', 'jquery-autocomplete.js' );
462
463 Helper::enqueue_script( 'popper-js', 'popper.min.js', array( 'jquery' ) );
464 Helper::enqueue_script( 'bootstrap-js', 'bootstrap.min.js', array( 'jquery' ) );
465 Helper::enqueue_script( 'bootstrap-select-js', 'bootstrap-select.min.js', array( 'jquery' ) );
466 Helper::enqueue_script( 'pagination-js', 'jquery.simplePagination.js', array( 'jquery' ) );
467 Helper::enqueue_script( 'jsrender', 'jsrender.min.js' );
468
469 wp_localize_script( 'jquery', 'lassoLiteOptionsData', $data_passed_to_js );
470 Helper::enqueue_script( 'lasso-helper', 'lasso-helper.js', array( 'jquery' ) );
471 Helper::enqueue_script( 'url-add', 'url-add.js', array( 'jquery' ) );
472 Helper::enqueue_script( 'support', 'support.js', array( 'jquery' ) );
473
474 if ( ! $setting->is_setting_onboarding_page() && ! $setting->is_setting_amazon_page() ) {
475 Helper::enqueue_script( 'lasso-signup', 'lasso-signup.js', array( 'jquery' ) );
476 }
477
478 if ( SIMPLE_URLS_SLUG . '-' . Enum::PAGE_URL_DETAILS === $page ) {
479 Helper::enqueue_script( 'url-details', 'url-details.js', array( 'jquery' ) );
480 }
481 }
482
483 if ( $setting->is_dashboard_page() ) {
484 Helper::enqueue_script( 'dashboard', 'dashboard.js', array( 'jquery' ) );
485 }
486
487 if ( $setting->is_opportunities_page() ) {
488 Helper::enqueue_script( 'opportunities', 'opportunities.js', array( 'jquery' ) );
489 }
490
491 if ( $setting->is_tables_page() ) {
492 Helper::enqueue_script( 'tables', 'tables.js', array( 'jquery' ) );
493 }
494
495 if ( $setting->is_setting_display_page() || $setting->is_setting_onboarding_page() ) {
496 Helper::enqueue_script( 'spectrum-js', 'spectrum.min.js', array( 'jquery' ) );
497 Helper::enqueue_script( 'settings-display-js', 'settings-display.js', array( 'jquery' ) );
498 }
499
500 if ( $setting->is_setting_amazon_page() ) {
501 Helper::enqueue_script( 'lasso-signup', 'lasso-signup.js', array( 'jquery' ) );
502 Helper::enqueue_script( 'settings-amazon', 'settings-amazon.js', array( 'jquery', SIMPLE_URLS_SLUG . '-lasso-signup' ) );
503 } elseif ( $setting->is_setting_onboarding_page() ) {
504 Helper::enqueue_script( 'settings-amazon', 'settings-amazon.js', array( 'jquery' ) );
505 }
506
507 if ( $setting->is_import_page() || $setting->is_setting_onboarding_page() ) {
508 Helper::enqueue_script( 'settings-import', 'settings-import.js', array( 'jquery' ) );
509 }
510
511 if ( $setting->is_setting_onboarding_page() ) {
512 Helper::enqueue_script( 'onboarding-js', 'onboarding.js', array( 'jquery' ) );
513 Helper::enqueue_script( 'settings-js', 'settings.js', array( 'jquery' ) );
514 Helper::enqueue_script( 'settings-display-js', 'settings-display.js', array( 'jquery' ) );
515 Helper::enqueue_script( 'lasso-signup', 'lasso-signup.js', array( 'jquery' ) );
516 }
517
518 if ( $setting->is_setting_general_page() ) {
519 Helper::enqueue_script( 'settings-general', 'settings-general.js', array( 'jquery' ) );
520 }
521
522 if ( $setting->is_setting_page() ) {
523 Helper::enqueue_script( 'settings-unsaved-guard', 'settings-unsaved-guard.js', array( 'jquery', SIMPLE_URLS_SLUG . '-lasso-helper' ) );
524 }
525
526 if ( $setting->is_group_detail_page() || $setting->is_group_page() ) {
527 Helper::enqueue_script( 'groups', 'groups.js', array( 'jquery' ) );
528 }
529
530 // @codingStandardsIgnoreEnd
531 }
532
533 /**
534 * DISPLAYS CSS FOR FRONTEND OF SITE
535 */
536 public function add_scripts_frontend() {
537 Helper::enqueue_style( 'lasso-lite', 'lasso-lite.css' );
538 }
539
540 /**
541 * DISPLAYS CUSTOM CSS FOR FRONTEND OF SITE
542 */
543 public function lasso_custom_css() {
544 if ( ! Helper::is_lite_using_new_ui() ) {
545 return;
546 }
547
548 // @codingStandardsIgnoreStart
549 echo '<style type="text/css">' . Helper::get_lasso_display_css_variables( true ) . '</style>';
550
551 // fix fontawesome js render svg (from other plugins) instead of using css
552 echo '
553 <script type="text/javascript">
554 // Notice how this gets configured before we load Font Awesome
555 window.FontAwesomeConfig = { autoReplaceSvg: false }
556 </script>
557 ';
558 // @codingStandardsIgnoreEnd
559 }
560
561 /**
562 * Order menu and submenu of Lasso
563 *
564 * @param boolean $custom_menu Whether custom ordering is enabled. Default false.
565 */
566 public function lasso_order_submenu( $custom_menu ) {
567 global $submenu;
568
569 $new_submenu = array();
570 $menu_key = 'edit.php?post_type=' . SIMPLE_URLS_SLUG;
571 $lasso_lite_submenu = @$submenu[ $menu_key ]; // phpcs:ignore
572
573 $parent_slug = 'edit.php?post_type=' . SIMPLE_URLS_SLUG;
574 $hide_menu = array(
575 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_SETTINGS_GENERAL,
576 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_SETTINGS_AMAZON,
577 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_URL_DETAILS,
578 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_OPPORTUNITIES,
579 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_TABLES,
580 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_GROUPS,
581 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_GROUP_DETAIL,
582 );
583 $surl_dashboard = array(
584 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_DASHBOARD,
585 'post-new.php?post_type=' . SIMPLE_URLS_SLUG,
586 );
587
588 if ( ! empty( $lasso_lite_submenu ) ) {
589 foreach ( $lasso_lite_submenu as $subpage ) {
590 // ? Show the new menus
591 if ( ! $this->show_old_menus && in_array( $subpage[2], $surl_dashboard, true ) ) { // ? Hide "Add new" and SURL Dashboard
592 continue;
593 } elseif ( ! $this->show_old_menus && $parent_slug === $subpage[2] ) { // ? Change title of root dashboard(all posts) menu
594 $subpage[0] = 'Dashboard';
595 $new_submenu[] = $subpage;
596 } elseif ( ! in_array( $subpage[2], $hide_menu, true ) ) {
597 if ( SIMPLE_URLS_SLUG . '-' . Enum::PAGE_SETTINGS_DISPLAY === $subpage[2] ) {
598 $subpage[0] = 'Settings';
599 }
600
601 $new_submenu[] = $subpage;
602 }
603 }
604
605 $new_submenu[90] = array(
606 'Support',
607 'manage_options',
608 Constant::LASSO_SUPPORT_URL,
609 );
610
611 $new_submenu[93] = array(
612 'Analytics',
613 'manage_options',
614 Constant::LASSO_ANALYTICS_URL,
615 );
616
617 $new_submenu[95] = array(
618 'Marketplace',
619 'manage_options',
620 Constant::LASSO_AFFILIATE_PLUS_URL,
621 );
622
623 $new_submenu[100] = array(
624 '<b class="green">Upgrade to Pro</b>',
625 'manage_options',
626 Constant::LASSO_UPGRADE_URL,
627 );
628 }
629
630 // ? ERROR | Overriding WordPress globals is prohibited. Found assignment to $submenu
631 // phpcs:ignore
632 $submenu[ $menu_key ] = $new_submenu;
633 }
634
635 /**
636 * Lasso display styles + color variables inside the block editor iframe (WP 7+).
637 *
638 * admin_enqueue_scripts only loads into the parent editor chrome; shortcode HTML
639 * preview renders in the editor canvas iframe and needs enqueue_block_assets.
640 *
641 * @codeCoverageIgnore Coverage ignore.
642 */
643 public function lasso_block_editor_css_variables() {
644 if ( ! is_admin() || Helper::is_lasso_pro_installed() || ! Helper::is_lite_using_new_ui() ) {
645 return;
646 }
647
648 $setting = new Setting();
649 if ( ! $setting->is_wordpress_post() && ! $setting->is_custom_post() ) {
650 return;
651 }
652
653 Helper::enqueue_style( 'bootstrap-grid-css', 'bootstrap-grid.min.css' );
654 Helper::enqueue_style( 'lasso-lite', 'lasso-lite.css' );
655 wp_add_inline_style( SIMPLE_URLS_SLUG . '-lasso-lite', Helper::get_lasso_display_css_variables( true ) );
656 }
657
658 /**
659 * Load lasso js file in Gutenberg editor
660 */
661 public function lasso_lite_gutenberg_block() {
662 if ( ! Helper::is_lasso_pro_installed() ) {
663 Helper::enqueue_script(
664 'lasso-lite-gutenberg-block',
665 'lasso-lite-gutenberg-block.js',
666 array( 'wp-blocks', 'wp-block-editor', 'wp-element', 'wp-components', 'wp-compose' ),
667 true
668 );
669 Helper::enqueue_script( 'display-add', 'display-add.js', array( 'jquery' ) );
670 Helper::enqueue_script( 'url-add', 'url-add.js', array( 'jquery' ) );
671 }
672 }
673
674 /**
675 * Load lasso js file in Classic editor (TinyMCE)
676 *
677 * @param array $plugin_array An array of external TinyMCE plugins.
678 */
679 public function lasso_add_tinymce_plugin( $plugin_array ) {
680 if ( ! Helper::is_lasso_pro_plugin_active() ) {
681 $lasso_setting = new Setting();
682 if ( Helper::is_classic_editor() && ( $lasso_setting->is_wordpress_post() || $lasso_setting->is_custom_post() ) ) {
683 $plugin_array['lasso_lite_tc_button'] = SIMPLE_URLS_URL . '/admin/assets/js/lasso-lite-display-modal.js?v=' . strval( @filemtime( SIMPLE_URLS_DIR . '/admin/assets/js/lasso-lite-display-modal.js' ) ); // phpcs:ignore
684 Helper::enqueue_script( 'display-add', 'display-add.js', array( 'jquery' ) );
685 Helper::enqueue_script( 'url-add', 'url-add.js', array( 'jquery' ) );
686 }
687 }
688
689 return $plugin_array;
690 }
691
692 /**
693 * Add Lasso button to Classic editor (TinyMCE)
694 *
695 * @param array $buttons First-row list of buttons.
696 */
697 public function lasso_lite_register_my_tc_button( $buttons ) {
698 if ( ! Helper::is_lasso_pro_plugin_active() ) {
699 array_push( $buttons, 'lasso_lite_tc_button' );
700 array_push( $buttons, 'lasso_grid_button' );
701 }
702
703 return $buttons;
704 }
705
706 /**
707 * Render template in admin footer on screen editor Gutenberg
708 */
709 public function lasso_lite_admin_footer_editor_gutenberg() {
710 if ( ! Helper::is_lasso_pro_plugin_active() ) {
711 $lasso_setting = new Setting();
712
713 if ( $lasso_setting->is_wordpress_post() || $lasso_setting->is_custom_post() ) {
714 $current_screen = get_current_screen();
715 // ? Check to make sure render html only block editor Gutenberg
716 if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() )
717 || ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() )
718 ) {
719 echo Helper::get_display_modal_html(); // phpcs:ignore
720 }
721 }
722 }
723 }
724
725 /**
726 * Custom target menu
727 */
728 public function lasso_custom_menu() {
729 ?>
730 <script type="text/javascript">
731 jQuery( document ).ready( function( $ ) {
732 jQuery( "ul#adminmenu a[href$='<?php echo Constant::LASSO_SUPPORT_URL; // phpcs:ignore ?>']" ).attr( 'target', '_blank' );
733 jQuery( "ul#adminmenu a[href$='<?php echo Constant::LASSO_UPGRADE_URL; // phpcs:ignore ?>']" ).attr( 'target', '_blank' );
734 } );
735 </script>
736 <?php
737 }
738
739 /**
740 * Add Amazon tracking id to redirect link
741 *
742 * @param string $redirect Redirect link.
743 */
744 public function lasso_lite_redirect( $redirect ) {
745 if ( Amazon_Api::is_amazon_url( $redirect ) ) {
746 $redirect = Amazon_Api::get_amazon_product_url( $redirect );
747 }
748
749 return $redirect;
750 }
751
752 /**
753 * Pull Amazon API information is stored in AAWP, or Amalinks Pro automatically.
754 *
755 * @return $this
756 */
757 public function amazon_api_pre_populated_automatically() {
758 if ( Helper::get_option( Enum::IS_PRE_POPULATED_AMAZON_API ) ) {
759 return $this;
760 }
761
762 $api_key = '';
763 $api_secret = '';
764 $country = '';
765 $tracking_id = '';
766
767 // ? AAWP plugin
768 if ( Helper::is_aawp_active() ) {
769 $aawp_api = get_option( 'aawp_api' );
770
771 $api_key = $aawp_api['key'] ?? '';
772 $api_secret = $aawp_api['secret'] ?? '';
773 $country = $aawp_api['country'] ?? '';
774 $tracking_id = $aawp_api['associate_tag'] ?? '';
775 }
776
777 // ? AmaLinksPro plugin
778 if ( empty( $api_key ) && empty( $api_secret ) && empty( $tracking_id ) && Helper::is_amalinks_pro_active() ) {
779 $api_key = get_option( 'amalinkspro-options_amazon_api_access_key', '' );
780 $api_secret = get_option( 'amalinkspro-options_amazon_api_secret_key', '' );
781 $country = get_option( 'amalinkspro-options_default_amazon_search_locale', '' );
782 if ( ! empty( $country ) ) {
783 $tracking_id = get_option( 'amalinkspro-options_' . $country . '_amazon_associate_ids_0_associate_id', '' );
784 }
785 }
786
787 $lasso_options = Setting::get_settings();
788 $amazon_access_key_id = $lasso_options['amazon_access_key_id'] ?? '';
789 $amazon_secret_key = $lasso_options['amazon_secret_key'] ?? '';
790 $amazon_tracking_id = $lasso_options['amazon_tracking_id'] ?? '';
791
792 if ( empty( $amazon_access_key_id ) && empty( $amazon_secret_key ) && empty( $amazon_tracking_id )
793 && ! empty( $api_key ) && ! empty( $api_secret ) && ! empty( $tracking_id ) && ! empty( $country )
794 ) {
795 $country = strtolower( $country );
796
797 Setting::set_setting( 'amazon_access_key_id', $api_key );
798 Setting::set_setting( 'amazon_secret_key', $api_secret );
799 Setting::set_setting( 'amazon_default_tracking_country', $country );
800 Setting::set_setting( 'amazon_tracking_id', $tracking_id );
801
802 Helper::update_option( Enum::IS_PRE_POPULATED_AMAZON_API, 1 );
803 }
804
805 return $this;
806 }
807
808 /**
809 * Remove action "admin_print_footer_scripts" of "Easy Table of Contents" plugin
810 */
811 public function remove_easy_table_of_content_action_admin_print_footer_scripts() {
812 Helper::remove_action( 'admin_print_footer_scripts', array( 'eztoc_pointers', 'admin_print_footer_scripts' ) );
813 }
814
815 /**
816 * Prints the templates used in the media manager.
817 */
818 public function lasso_print_media_templates() {
819 if ( file_exists( ABSPATH . WPINC . '/media-template.php' ) && ! function_exists( 'wp_print_media_templates' ) ) {
820 require_once ABSPATH . WPINC . '/media-template.php';
821 }
822 wp_print_media_templates();
823 }
824
825 /**
826 * Add active class to the current page
827 */
828 public function lasso_organize_menu() {
829 $setting_page = ( new Setting() )->is_setting_page();
830 ?>
831 <script>
832 jQuery(document).ready(function(){
833 var lasso_menu = jQuery('#menu-posts-surl');
834 if(lasso_menu.is('.wp-has-current-submenu, .wp-menu-open')){
835 var submenu = lasso_menu.find('ul.wp-submenu').find('li');
836 if(submenu != undefined) {
837 if(!submenu.hasClass('current') || submenu.hasClass('current').length == 0) {
838 submenu.eq(1).addClass('current');
839 }
840
841 // add class `current` for Settings menu
842 if('<?php echo (int) $setting_page; ?>' == '1') {
843 submenu.removeClass('current');
844 lasso_menu.find('ul.wp-submenu').find('li:contains("Settings")').addClass('current');
845 }
846 }
847 }
848 });
849 </script>
850 <?php
851 }
852
853 /**
854 * Register a category
855 */
856 public function register_taxonomy() {
857 // ? register custom taxonomy
858 register_taxonomy(
859 Constant::LASSO_CATEGORY,
860 Constant::LASSO_POST_TYPE,
861 array(
862 'label' => __( 'Lasso Categories' ),
863 'rewrite' => array( 'slug' => Constant::LASSO_CATEGORY ),
864 'hierarchical' => false,
865 'public' => false,
866 'labels' => array(
867 'add_new_item' => __( 'Add New Category' ),
868 'edit_item' => __( 'Edit Categories' ),
869 ),
870 )
871 );
872 }
873
874 /**
875 * Use Amazon product url instead Lasso url when search/insert a link into a post/page
876 *
877 * @param array $response Response data to send to the client.
878 * @param object $server (WP_REST_Server) Server instance.
879 * @param object $request (WP_REST_Request) Request used to generate the response.
880 */
881 public function update_post_type_gutenberg( $response, $server, $request ) {
882 $params = $request->get_params();
883 $type = $params['type'] ?? '';
884 if ( 'post' === $type && is_array( $response ) && '/wp/v2/search' === $request->get_route() && count( $response ) > 0 ) {
885 foreach ( $response as $key => $post ) {
886 if ( 'post' === $post['type'] && Constant::LASSO_POST_TYPE === $post['subtype'] ) {
887 $response[ $key ]['subtype'] = Constant::LASSO_PRO_POST_TYPE; // ? update post type to Pro
888 }
889 }
890 }
891
892 return $response;
893 }
894
895 /**
896 * Custom post type to Lasso Branding
897 *
898 * @param array $results Search results.
899 *
900 * @return mixed
901 */
902 public function update_post_type_classic_editor( $results ) {
903 if ( ! empty( $results ) ) {
904 foreach ( $results as &$result ) {
905 $type = get_post_type( $result['ID'] );
906 if ( Constant::LASSO_POST_TYPE === $type ) {
907 $result['info'] = Constant::LASSO_BRAND;
908 }
909 }
910 }
911 return $results;
912 }
913
914 /**
915 * Update the text in the footer
916 *
917 * @param string $text Text.
918 */
919 public function admin_footer( $text ) {
920 global $current_screen;
921
922 $setting = new Setting();
923
924 if ( ! empty( $current_screen->id ) && $setting->is_surls_page() ) {
925 $url = Enum::LASSO_REVIEW_URL;
926 $text = sprintf(
927 wp_kses(
928 'Enjoying %1$s? Please rate <a href="%2$s" target="_blank" rel="noopener noreferrer">&#9733;&#9733;&#9733;&#9733;&#9733;</a> on <a href="%3$s" target="_blank" rel="noopener">WordPress.org</a> to help us spread the word. Thanks from the Lasso team!',
929 array(
930 'a' => array(
931 'href' => array(),
932 'target' => array(),
933 'rel' => array(),
934 ),
935 )
936 ),
937 '<strong>Lasso Lite</strong>',
938 $url,
939 $url
940 );
941 }
942
943 return $text;
944 }
945
946 /**
947 * Open Affiliate+ in new tab
948 */
949 public function open_links_in_new_tab() {
950 ?>
951 <script type="text/javascript">
952 jQuery(document).ready(function($) {
953 $('a[href="https://app.getlasso.co/plus/"]').attr('target', '_blank');
954 });
955 </script>
956 <?php
957 }
958
959 /**
960 * Whether Lite onboarding should redirect admin users to the welcome flow.
961 *
962 * @return bool
963 */
964 public static function should_redirect_to_welcome_page() {
965 return (bool) get_option( Enum::LASSO_LITE_ACTIVE ) && ! Helper::get_option( Enum::IS_VISITED_WELCOME_PAGE );
966 }
967
968 /**
969 * Whether the affiliate promo dashboard banner should render.
970 *
971 * @return bool
972 */
973 private function should_show_dashboard_promo_banner() {
974 global $pagenow;
975
976 if ( 'index.php' !== $pagenow || ! Helper::is_lite_using_new_ui() ) {
977 return false;
978 }
979
980 $license_active = License::get_license_status();
981 $is_connected_aff = intval( Helper::get_option( Constant::LASSO_OPTION_IS_CONNECTED_AFFILIATE, '0' ) );
982
983 return ! $license_active
984 && 0 === $is_connected_aff
985 && ! intval( Helper::get_option( Constant::LASSO_OPTION_DISMISS_PROMOTIONS, 0 ) );
986 }
987
988 /**
989 * Show Performance promotion
990 *
991 * @return void
992 */
993 public function lasso_lite_custom_dashboard_banner() {
994 if ( ! $this->should_show_dashboard_promo_banner() ) {
995 echo ''; // phpcs:ignore
996 return;
997 }
998
999 $setting_data = Setting::get_settings();
1000 $support_enabled = $setting_data[ Enum::SUPPORT_ENABLED ] ?? false;
1001 if ( ! $support_enabled ) {
1002 $template_path = '/admin/views/notifications/affiliate-promotions-no-intercom.php';
1003 } else {
1004 $template_path = '/admin/views/notifications/affiliate-promotions-intercom.php';
1005 }
1006
1007 $html = Helper::include_with_variables(
1008 SIMPLE_URLS_DIR . $template_path,
1009 array(
1010 'dismiss' => 0,
1011 'option_name' => Constant::LASSO_OPTION_DISMISS_PROMOTIONS,
1012 )
1013 );
1014
1015 echo $html; // phpcs:ignore
1016 }
1017
1018 /**
1019 * Print JS script for Lasso event tracking.
1020 */
1021 public function lasso_lite_event_tracking() {
1022 if ( is_admin() || strpos( Helper::get_server_param( 'REQUEST_URI' ), 'wp-admin' )
1023 || strpos( Helper::get_server_param( 'REQUEST_URI' ), 'elementor-preview' )
1024 || Helper::is_lasso_pro_installed()
1025 ) {
1026 return;
1027 }
1028
1029 $process_lasso_event_tracking = apply_filters( 'lasso_lite_event_tracking', true );
1030
1031 if ( ! $process_lasso_event_tracking ) {
1032 return;
1033 }
1034
1035 $lasso_options = Setting::get_settings();
1036 $is_ip_anonymization = false;
1037 $lsid = Helper::build_lsid();
1038 $realtime_ingest = null;
1039 if ( Realtime_Click::is_ingest_enabled() ) {
1040 $ingest_secret = Realtime_Click::get_ingest_secret();
1041 $channel_id = Realtime_Click::get_channel_id();
1042 if ( is_string( $ingest_secret ) && '' !== $ingest_secret && is_string( $channel_id ) && '' !== $channel_id ) {
1043 $beacon = Realtime_Click::get_beacon_credentials( $channel_id, $ingest_secret );
1044 $realtime_ingest = array(
1045 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
1046 'action' => 'lasso_lite_realtime_ingest',
1047 'channelId' => $channel_id,
1048 'beaconToken' => $beacon['token'],
1049 'beaconBucket' => $beacon['bucket'],
1050 );
1051 }
1052 }
1053
1054 $current_date = gmdate( 'Ymd' );
1055 $js_version = LASSO_LITE_VERSION . '.' . $current_date;
1056
1057 // Prefer clean path when permalinks are enabled; fall back to query when using Plain
1058 $snippet_query = Helper::get_snippet_query();
1059 $performance_url_hash = add_query_arg(
1060 array(
1061 $snippet_query => '1',
1062 'ver' => $js_version,
1063 ),
1064 home_url( '/' )
1065 );
1066
1067 $default_js_domain = 'https://js.codedrink.com';
1068
1069 $dynamic_js_domain = Helper::get_option( 'js_domain', $default_js_domain );
1070 $full_dynamic_js_domain = Helper::get_option( 'full_js_domain', "$dynamic_js_domain/snippet.min.js" );
1071
1072 $dynamic_performance_url_external = $full_dynamic_js_domain . '?ver=' . $js_version; // load external js
1073
1074 // @codeCoverageIgnoreStart
1075 if ( $lasso_options['performance_event_tracking'] ) :
1076 ?>
1077
1078 <script type="text/javascript">
1079 (function () {
1080 // Prevent double-insert
1081 if (window.LS_AFF_IS_LOADED || window.__LS_SEQ_LOADER__) {
1082 return;
1083 }
1084 window.__LS_SEQ_LOADER__ = true;
1085 var lsSources = [
1086 <?php echo wp_json_encode( $dynamic_performance_url_external ); ?>,
1087 <?php echo wp_json_encode( $performance_url_hash ); ?>
1088 ];
1089
1090 var lsScriptLoadTimeoutMs = 2500;
1091 var lsIndex = 0, lsTimeoutMs = lsScriptLoadTimeoutMs;
1092
1093 function lsLoadNext() {
1094 if (window.LS_AFF_IS_LOADED || lsIndex >= lsSources.length) {
1095 return;
1096 }
1097
1098 var lsUrl = lsSources[lsIndex++];
1099 var lsScript = document.createElement('script');
1100 lsScript.src = lsUrl;
1101 lsScript.onerror = function () {
1102 try { lsScript.remove(); } catch (_) {}
1103 if (!window.LS_AFF_IS_LOADED) lsLoadNext();
1104 };
1105 var lsTimer = setTimeout(function () {
1106 if (!window.LS_AFF_IS_LOADED) {
1107 try { lsScript.remove(); } catch (_) {}
1108 lsLoadNext();
1109 }
1110 }, lsTimeoutMs);
1111 lsScript.onload = (function (orig) {
1112 return function () {
1113 clearTimeout(lsTimer);
1114 if (orig) orig();
1115 };
1116 })(lsScript.onload);
1117 (document.head || document.documentElement).appendChild(lsScript);
1118 }
1119
1120 lsLoadNext();
1121 })();
1122 </script>
1123 <script type="text/javascript">
1124 (function(){
1125 <?php if ( null !== $realtime_ingest ) : ?>
1126 window.LASSO_LITE_REALTIME_INGEST = <?php echo wp_json_encode( $realtime_ingest ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>;
1127 <?php endif; ?>
1128 var lsInitialized = false;
1129 function lsDoInit(detail){
1130 if (lsInitialized) return;
1131 lsInitialized = true;
1132 try {
1133 (detail && detail.init ? detail : (window.LSAFFEvents || {})).init({
1134 'lsid': '<?php echo $lsid; // phpcs:ignore ?>',
1135 'pid': '<?php echo get_the_ID(); // phpcs:ignore ?>',
1136 'ipa': '<?php echo $is_ip_anonymization; // phpcs:ignore ?>',
1137 'performance': '1',
1138 'matching': '<?php echo $lasso_options['auto_upgrade_eligible_links'] ? 1 : 0; ?>',
1139 });
1140 } catch (err) {
1141 }
1142 }
1143
1144 var lsPollCount = 0;
1145 var lsMaxPollAttempts = 50;
1146 var lsPollTimer = null;
1147 function lsStartPolling() {
1148 if (lsPollTimer) return;
1149 lsPollTimer = setInterval(function(){
1150 if (lsInitialized) { clearInterval(lsPollTimer); return; }
1151 if (window.LSAFFEvents && typeof window.LSAFFEvents.init === 'function') {
1152 lsDoInit(window.LSAFFEvents);
1153 clearInterval(lsPollTimer);
1154 return;
1155 }
1156 lsPollCount++;
1157 if (lsPollCount > lsMaxPollAttempts) { // ~5s at 100ms
1158 clearInterval(lsPollTimer);
1159 }
1160 }, 100);
1161 }
1162
1163 if (window.LSAFFEvents && typeof window.LSAFFEvents.init === 'function') {
1164 lsDoInit(window.LSAFFEvents);
1165 } else {
1166 document.addEventListener('LSAFFEventLoaded', function(e){
1167 lsDoInit(e.detail);
1168 }, { once: true });
1169 lsStartPolling();
1170 }
1171 })();
1172 </script>
1173 <?php
1174 endif;
1175 // @codeCoverageIgnoreEnd
1176 }
1177
1178 /**
1179 * Elementor Init Hook
1180 *
1181 * @codeCoverageIgnore Coverage ignore.
1182 */
1183 public function elementor_init() {
1184 // ? This runs on elementor/init only — Pro owns Elementor when affiliate-plugin is active.
1185 if ( Helper::is_lasso_pro_installed() ) {
1186 return;
1187 }
1188
1189 // ? Do not gate on is_plugin_active( 'elementor/elementor.php' ): custom paths / load order can false-negative while Elementor is clearly bootstrapped (this hook).
1190 if ( class_exists( 'Elementor\Widget_Base' ) && class_exists( 'Elementor\Controls_Manager' ) ) {
1191 add_action( 'elementor/widgets/register', array( $this, 'register_widget_lasso_shortcode' ) );
1192 add_action( 'elementor/editor/before_enqueue_styles', array( $this, 'elementor_editor_styles' ) );
1193 add_action( 'elementor/editor/before_enqueue_scripts', array( $this, 'elementor_editor_scripts' ) );
1194 }
1195
1196 // ? Move the scan post into the "elementor/document/after_save" action
1197 add_action( 'elementor/document/after_save', array( $this, 'after_elementor_document_save' ), 10, 1 );
1198 }
1199
1200 /**
1201 * Register lasso widget
1202 *
1203 * @param object $widgets_manager Widget manager.
1204 *
1205 * @codeCoverageIgnore Coverage ignore.
1206 */
1207 public function register_widget_lasso_shortcode( $widgets_manager ) {
1208 require_once SIMPLE_URLS_PLUGIN_PATH . '/libs/elementor/widgets/lasso-shortcode.php';
1209 $widgets_manager->register( new \Widget_Lasso_Shortcode() );
1210 }
1211
1212 /**
1213 * Elementor load css
1214 *
1215 * @codeCoverageIgnore Coverage ignore.
1216 */
1217 public function elementor_editor_styles() {
1218 wp_enqueue_style( 'wp-pointer' );
1219 Helper::enqueue_style( 'lasso-elementor', 'lasso-elementor.css' );
1220 }
1221
1222 /**
1223 * Elementor editor scripts (parity with Pro wp-pointer).
1224 *
1225 * @codeCoverageIgnore Coverage ignore.
1226 */
1227 public function elementor_editor_scripts() {
1228 wp_enqueue_script( 'wp-pointer' );
1229 if ( ! Helper::is_lite_using_new_ui() ) {
1230 return;
1231 }
1232 wp_enqueue_script( 'jquery' );
1233 Helper::enqueue_script( 'lasso-modal-js', 'lasso-modal.js', array( 'jquery' ), true );
1234 Helper::enqueue_script( 'lasso-elementor', 'lasso-elementor.js', array( 'jquery', SIMPLE_URLS_SLUG . '-lasso-modal-js' ), true );
1235 Helper::enqueue_script( 'lasso-lite-display-modal', 'lasso-lite-display-modal.js', array( 'jquery', SIMPLE_URLS_SLUG . '-lasso-elementor' ), true );
1236 }
1237
1238 /**
1239 * Move the scan post into the "elementor/document/after_save" action
1240 *
1241 * @param object $document Elementor document.
1242 *
1243 * @codeCoverageIgnore Coverage ignore.
1244 */
1245 public function after_elementor_document_save( $document ) {
1246 $post = $document->get_post();
1247 }
1248
1249 /**
1250 * Exclude Lasso performance JS from WP Rocket cache
1251 *
1252 * @param array $excluded_js Excluded JS.
1253 * @return array
1254 */
1255 public function exclude_lasso_performance_js_from_rocket_cache( $excluded_js ) {
1256 $excluded_js[] = 'lasso-performance.min.js';
1257 return $excluded_js;
1258 }
1259
1260 /**
1261 * Check is Elementor editor page
1262 *
1263 * @return bool
1264 */
1265 public static function is_editor() {
1266 $http_referer = Helper::get_server_param( 'HTTP_REFERER' );
1267 $query_str = wp_parse_url( $http_referer, PHP_URL_QUERY );
1268 parse_str( $query_str, $query_params );
1269
1270 return isset( $query_params['action'] ) && 'elementor' === $query_params['action'];
1271 }
1272
1273 /**
1274 * Register rewrite rule for /js/connect-snippet.min.js
1275 */
1276 public static function lasso_register_connect_snippet_rewrite() {
1277 $snippet_query = Helper::get_snippet_query();
1278 add_rewrite_rule( '^js/snippet\.min\.js$', 'index.php?' . $snippet_query . '=1', 'top' );
1279 }
1280
1281 /**
1282 * Flush rewrites after plugin upgrades via WP upgrader
1283 *
1284 * @param object $upgrader Upgrader instance.
1285 * @param array $options Upgrader options.
1286 * @return void
1287 */
1288 public function lasso_connect_snippet_flush_on_upgrade( $upgrader, $options ) {
1289 if ( empty( $options['type'] ) || 'plugin' !== $options['type'] ) {
1290 return;
1291 }
1292
1293 // ? Supported actions: update (bulk/single) and install (upload .zip)
1294 $action = isset( $options['action'] ) ? $options['action'] : '';
1295 if ( ! in_array( $action, array( 'update', 'install' ), true ) ) {
1296 return;
1297 }
1298
1299 $targets = array();
1300 if ( ! empty( $options['plugins'] ) && is_array( $options['plugins'] ) ) {
1301 $targets = $options['plugins'];
1302 } elseif ( ! empty( $options['plugin'] ) && is_string( $options['plugin'] ) ) {
1303 $targets = array( $options['plugin'] );
1304 }
1305
1306 // ? Fallback detection using Plugin_Upgrader->new_plugin_data when targets are missing
1307 if ( empty( $targets ) && is_object( $upgrader ) && $upgrader instanceof \Plugin_Upgrader ) {
1308 $plugin_data = isset( $upgrader->new_plugin_data ) ? $upgrader->new_plugin_data : array();
1309 $plugin_name = is_array( $plugin_data ) && isset( $plugin_data['Name'] ) ? $plugin_data['Name'] : '';
1310 if ( 'Lasso Lite' === $plugin_name ) {
1311 $this->lasso_register_connect_snippet_rewrite();
1312 flush_rewrite_rules();
1313 return;
1314 }
1315 }
1316
1317 if ( empty( $targets ) ) {
1318 return;
1319 }
1320
1321 if ( in_array( SIMPLE_URLS_SLUG, $targets, true ) ) {
1322 $this->lasso_register_connect_snippet_rewrite();
1323 flush_rewrite_rules();
1324 }
1325 }
1326
1327 /**
1328 * Allow lasso_connect_snippet as query var
1329 *
1330 * @param array $vars Query vars.
1331 * @return array
1332 */
1333 public function lasso_connect_snippet_query_var( $vars ) {
1334 $snippet_query = Helper::get_snippet_query();
1335 $vars[] = $snippet_query;
1336 return $vars;
1337 }
1338
1339 /**
1340 * Serve vanity JS: /js/connect-snippet.min.js
1341 *
1342 * Outputs the connect-snippet.min.js from plugin assets with proper caching headers.
1343 */
1344 public function serve_connect_snippet() {
1345 $snippet_query = Helper::get_snippet_query();
1346 // Allow serving by pretty path even if rewrite rules aren't flushed yet
1347 $req_uri = Helper::get_server_param( 'REQUEST_URI' );
1348 $req_path = is_string( $req_uri ) ? wp_parse_url( $req_uri, PHP_URL_PATH ) : '';
1349 $is_snippet_path = is_string( $req_path ) && rtrim( $req_path, '/' ) === LASSO_SNIPPET_VANITY_PATH_LITE;
1350 if ( intval( get_query_var( $snippet_query ) ) !== 1 && ! $is_snippet_path ) {
1351 return;
1352 }
1353
1354 $file_path = LASSO_CONNECT_SNIPPET_FILE_LITE;
1355 if ( ! file_exists( $file_path ) ) {
1356 status_header( 404 );
1357 exit;
1358 }
1359
1360 // Headers
1361 header( 'Content-Type: application/javascript; charset=UTF-8' );
1362 header( 'X-Content-Type-Options: nosniff' );
1363
1364 // Cache headers with support for 304
1365 $last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file_path ) ) . ' GMT';
1366 $etag = 'W/"' . md5( $last_modified . filesize( $file_path ) ) . '"';
1367
1368 header( 'Last-Modified: ' . $last_modified );
1369 header( 'ETag: ' . $etag );
1370
1371 // Cache: If version query is present, cache for 1 day; else short TTL
1372 $ver = isset( $_GET['ver'] ) ? sanitize_text_field( wp_unslash( $_GET['ver'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1373 if ( ! empty( $ver ) ) {
1374 header( 'Cache-Control: public, max-age=86400' );
1375 } else {
1376 header( 'Cache-Control: public, max-age=300' );
1377 }
1378
1379 // Handle conditional requests
1380 $if_none_match = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? sanitize_text_field( trim( wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) ) : '';
1381 $if_modified_since = isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ? sanitize_text_field( trim( wp_unslash( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) ) : '';
1382
1383 if ( $if_none_match === $etag || $if_modified_since === $last_modified ) {
1384 status_header( 304 );
1385 exit;
1386 }
1387
1388 ob_start();
1389 $read_result = @readfile( $file_path );
1390 if ( false === $read_result ) {
1391 if ( ob_get_length() ) {
1392 ob_end_clean();
1393 }
1394 status_header( 500 );
1395 exit;
1396 }
1397 ob_end_flush();
1398 exit;
1399 }
1400
1401 /**
1402 * Add stable frontend body class from LASSO_LITE_VERSION (e.g. lasso-lite-v145).
1403 *
1404 * @param array<int, string> $classes Body classes.
1405 * @return array<int, string>
1406 */
1407 public function filter_body_class_lasso_lite_version( $classes ) {
1408 $extra = $this->get_lasso_lite_version_body_class();
1409 if ( '' !== $extra ) {
1410 $classes[] = $extra;
1411 }
1412 return $classes;
1413 }
1414
1415 /**
1416 * CSS-safe class segment: lasso-lite-v{version}.
1417 *
1418 * @return string
1419 */
1420 private function get_lasso_lite_version_body_class() {
1421 if ( ! defined( 'LASSO_LITE_VERSION' ) ) {
1422 return '';
1423 }
1424 return 'lasso-lite-v' . sanitize_html_class( str_replace( '.', '-', (string) LASSO_LITE_VERSION ) );
1425 }
1426 }
1427