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

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