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

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

832 lines 29.2 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
19 /**
20 * Hook.
21 */
22 class Hook {
23 /**
24 * Current template
25 *
26 * @var string $current_template
27 */
28 public $current_template = '';
29
30 /**
31 * Is show old menus
32 *
33 * @var bool $show_old_menus
34 */
35 private $show_old_menus = false;
36
37 /**
38 * Register hooks
39 */
40 public function register_hooks() {
41 add_action( 'admin_init', array( $this, 'amazon_api_pre_populated_automatically' ) );
42 add_action( 'init', array( $this, 'register_taxonomy' ) );
43 add_action( 'admin_menu', array( $this, 'build_admin_menu' ), 2 );
44
45 $lasso_shortcode = new Shortcode();
46 add_shortcode( 'lasso', array( $lasso_shortcode, 'lasso_lite_core_shortcode' ) );
47 add_filter( 'pre_do_shortcode_tag', array( $this, 'filter_pre_do_shortcode_lasso_lite' ), 10, 4 );
48
49 add_action( 'wp_head', array( $this, 'lasso_custom_css' ) ); // ? frontend
50 add_action( 'admin_head', array( $this, 'lasso_custom_css' ) ); // ? admin
51 add_action( 'admin_head', array( $this, 'lasso_custom_menu' ) ); // ? admin
52 add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts_frontend' ) ); // ? frontend
53
54 add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ) );
55 add_action( 'admin_enqueue_scripts', array( $this, 'add_styles' ) );
56
57 // ? re-order submenu pages
58 add_filter( 'custom_menu_order', array( $this, 'lasso_order_submenu' ) );
59
60 // ? lasso gutenberg block
61 add_action( 'enqueue_block_editor_assets', array( $this, 'lasso_lite_gutenberg_block' ) );
62
63 // ? add Lasso button into TinyMCE
64 add_filter( 'mce_external_plugins', array( $this, 'lasso_add_tinymce_plugin' ) );
65 add_filter( 'mce_buttons', array( $this, 'lasso_lite_register_my_tc_button' ) );
66
67 add_filter( 'simple_urls_redirect_url', array( $this, 'lasso_lite_redirect' ), 10, 1 );
68
69 add_action( 'admin_footer', array( $this, 'lasso_lite_admin_footer_editor_gutenberg' ) );
70
71 add_filter( 'rest_pre_echo_response', array( $this, 'update_post_type_gutenberg' ), 200, 3 );
72
73 add_filter( 'wp_link_query', array( $this, 'update_post_type_classic_editor' ), 10, 1 );
74 add_filter( 'admin_footer_text', array( $this, 'admin_footer' ), 100, 2 );
75 add_filter( 'update_footer', '__return_empty_string', 11 );
76
77 $setting = new Setting();
78 if ( $setting->is_surls_page() ) {
79 // ? plugin: Pretty Links Pro
80 if ( class_exists( 'PrliUpdateController' ) ) {
81 Helper::remove_action( 'admin_notices', array( 'PrliUpdateController', 'activation_warning' ) );
82 }
83 }
84
85 // ? FIX: CONFLICT WITH OTHER PLUGINS
86 // ? remove js files from other plugins in Lasso pages
87 if ( $setting->is_surls_page() ) {
88 // ? plugin: SEO Booster
89 if ( class_exists( 'Seobooster2' ) ) {
90 remove_action( 'admin_print_footer_scripts', array( 'Seobooster2', 'admin_print_footer_scripts' ) );
91 }
92
93 // ? plugin: ShortPixel Adaptive Images
94 if ( class_exists( 'ShortPixel\AI\Notice' ) && class_exists( 'ShortPixelAI' ) ) {
95 $spai = \ShortPixelAI::_();
96 $spain = \ShortPixel\AI\Notice::_( $spai );
97 remove_action( 'admin_footer', array( $spain, 'enqueueAdminScripts' ) );
98 remove_action( 'admin_bar_menu', array( $spai, 'toolbar_styles' ), 999 );
99 }
100
101 // ? plugin: WZone - WooCommerce Amazon Affiliates (WooZone)
102 if ( class_exists( 'WooZone' ) ) {
103 global $WooZone; // phpcs:ignore
104 remove_action( 'init', array( $WooZone, 'initThePlugin' ), 5 ); // phpcs:ignore
105 }
106
107 // ? plugin: Client Portal
108 $lasso_page = $_GET['page'] ?? ''; // phpcs:ignore
109 if ( class_exists( 'CCGClientPortal' ) && Helper::add_prefix_page( Enum::PAGE_DASHBOARD ) === $lasso_page ) {
110 global $zohopwp; // phpcs:ignore
111 remove_action( 'admin_menu', array( $zohopwp, 'ccgclientportal_admin_menu' ) ); // phpcs:ignore
112 }
113
114 if ( class_exists( 'wpe_admin_pointers' ) ) {
115 global $wpe_admin_pointers; // phpcs:ignore
116 remove_action( 'admin_enqueue_scripts', array( $wpe_admin_pointers, 'custom_admin_pointers_header' ) ); // phpcs:ignore
117 }
118
119 // ? plugin: tagDiv Composer
120 if ( function_exists( 'td_change_backbone_js_hook' ) ) {
121 remove_action( 'print_media_templates', 'td_change_backbone_js_hook' );
122 }
123
124 // ? plugin: Pretty Links Pro
125 if ( class_exists( 'PrliUpdateController' ) ) {
126 Helper::remove_action( 'admin_notices', array( 'PrliUpdateController', 'activation_warning' ) );
127 }
128
129 if ( Helper::is_earnist_plugin_loaded() ) {
130 Helper::remove_action( 'admin_print_footer_scripts', array( 'EarnistProductPicker', 'register_tinymce_quicktags' ) );
131 }
132 if ( Helper::is_shortcode_start_rating_plugin_loaded() ) {
133 global $ShortcodeStarRating; // phpcs:ignore
134 remove_action( 'admin_print_footer_scripts', array( $ShortcodeStarRating, 'appthemes_add_quicktags' ) ); // phpcs:ignore
135 }
136 if ( Helper::is_plugin_easy_table_of_contents_activated() ) {
137 add_action( 'admin_enqueue_scripts', array( $this, 'remove_easy_table_of_content_action_admin_print_footer_scripts' ), 20 ); // ? admin
138 }
139
140 remove_all_actions( 'admin_footer' );
141 add_action( 'admin_footer', array( $this, 'lasso_print_media_templates' ) );
142 add_action( 'admin_footer', array( $this, 'lasso_organize_menu' ), 100 );
143 }
144
145 // ? fix conflict with plugin Affiliate URL Automation
146 if ( class_exists( 'AffiliateURLs' ) ) {
147 global $AffiliateURLs; // phpcs:ignore
148 remove_filter( 'the_content', array( &$AffiliateURLs, 'the_content' ), 12 ); // phpcs:ignore
149 }
150
151 // ? remove js files from other plugins in WP post/page pages
152 if ( Helper::is_wordpress_post() ) {
153 if ( class_exists( 'WooZone' ) ) {
154 global $WooZone; // phpcs:ignore
155 remove_action( 'init', array( $WooZone, 'initThePlugin' ), 5 ); // phpcs:ignore
156 }
157 }
158
159 if ( Helper::is_gravity_perks_plugin_active() ) {
160 remove_action( 'admin_print_footer_scripts', array( 'GWPerks', 'welcome_pointer_script' ), 10 ); // phpcs:ignore
161 }
162 }
163
164
165 /**
166 * Check if Lasso Lite shortcode then render, else return false to running the next shortcode processes
167 *
168 * @param boolean $false This is the default value that will be returned if the shortcode is not found.
169 * @param string $tag The shortcode tag.
170 * @param array $attr The attributes passed to the shortcode like so: [shortcode attr1="value" /].
171 * @param array $m The regex for the shortcode.
172 *
173 * @return string|boolean return value is the output of the lasso_lite_core_shortcode function.
174 */
175 public function filter_pre_do_shortcode_lasso_lite( $false, $tag, $attr, $m ) {
176 if ( 'lasso' === $tag ) {
177 return ( new Shortcode() )->lasso_lite_core_shortcode( $attr );
178 }
179
180 return false;
181 }
182
183 /**
184 * Admin menu
185 */
186 public function build_admin_menu() {
187 $get_page = Helper::GET()['page'] ?? '';
188 $post_type = Helper::GET()['post_type'] ?? '';
189
190 $dashboard_slug = Helper::add_prefix_page( Enum::PAGE_DASHBOARD );
191 // ? Switch to new/old UI: We should easy redirect to the suitable dashboard.
192 if ( 'switch-new-ui' === $get_page ) {
193 update_option( Enum::SWITCH_TO_NEW_UI, 1 );
194 wp_redirect( Page::get_page_url( $dashboard_slug ) ); // phpcs:ignore
195 exit;
196 } elseif ( 'switch-old-ui' === $get_page ) {
197 update_option( Enum::LASSO_LITE_ACTIVE, 0 ); // ? fix conflict with L.235
198 update_option( Enum::SWITCH_TO_NEW_UI, 0 );
199 wp_redirect( Page::get_page_url() ); // phpcs:ignore
200 exit;
201 }
202
203 // ? Reset onboarding for testing
204 $reset_onboarding = Helper::GET()[ Enum::RESET_WELCOME_PAGE ] ?? '';
205 if ( $reset_onboarding ) {
206 Helper::update_option( Enum::IS_VISITED_WELCOME_PAGE, 0 );
207 update_option( Enum::LASSO_LITE_ACTIVE, 1 );
208 }
209
210 // ? Reset request review for testing
211 $reset_review = Helper::GET()[ Enum::RESET_REQUEST_REVIEW ] ?? '';
212 if ( $reset_review ) {
213 Helper::update_option( Constant::LASSO_OPTION_REVIEW_ALLOW, 1 );
214 Helper::update_option( Constant::LASSO_OPTION_REVIEW_SNOOZE, 0 );
215 Helper::update_option( Constant::LASSO_OPTION_REVIEW_LINK_COUNT, 0 );
216 }
217
218 // ? The new Lasso Lite active should be redirect to welcome page
219 $onboarding_page = Helper::add_prefix_page( Enum::PAGE_ONBOARDING );
220 $should_redirect_to_welcome_page = get_option( Enum::LASSO_LITE_ACTIVE ) && ! Helper::get_option( Enum::IS_VISITED_WELCOME_PAGE );
221 if ( SIMPLE_URLS_SLUG === $post_type && $onboarding_page !== $get_page && $should_redirect_to_welcome_page ) {
222 wp_redirect( Page::get_page_url( $onboarding_page ) ); // phpcs:ignore
223 exit;
224 } elseif ( $onboarding_page === $get_page && ! $should_redirect_to_welcome_page ) {
225 wp_redirect( Page::get_page_url( $dashboard_slug ) ); // phpcs:ignore
226 exit;
227 }
228
229 // ? Redirect to dashboard page if page is "surl-dashboard" and missing "post_type" parameter
230 if ( $dashboard_slug === $get_page && ! $post_type ) {
231 wp_redirect( Page::get_page_url( $dashboard_slug ) ); // phpcs:ignore
232 exit;
233 }
234
235 // ? If the customer install from new UI, we should only show the new UI's menus without switch UI feature
236 if ( get_option( Enum::LASSO_LITE_ACTIVE ) ) {
237 update_option( Enum::SWITCH_TO_NEW_UI, 1 );
238 $this->apply_new_ui_menus();
239 } else { // ? The old client that upgrade to the new UI
240 $switch_ui = 'switch-old-ui';
241 $page_title = 'Switch to old UI';
242
243 // ? The client was switched to new UI by click "Switch To New UI"
244 if ( Helper::is_lite_using_new_ui() ) {
245 $this->apply_new_ui_menus();
246 } else { // ? The client was still using the old UI
247 $this->show_old_menus = true;
248 $switch_ui = 'switch-new-ui';
249 $page_title = 'Switch to new UI';
250 }
251
252 add_submenu_page(
253 'edit.php?post_type=' . SIMPLE_URLS_SLUG,
254 $page_title,
255 $page_title,
256 'manage_options',
257 Page::get_page_url( $switch_ui )
258 );
259 }
260 }
261
262 /**
263 * Apply new UI's menus
264 */
265 private function apply_new_ui_menus() {
266 $get_page = Helper::GET()['page'] ?? '';
267 $post_type = Helper::GET()['post_type'] ?? '';
268
269 // ? Redirect to new UI's Dashboard if new UI enabled and the link is old UI
270 if ( SIMPLE_URLS_SLUG === $post_type && ! $get_page ) {
271 wp_redirect( Page::get_lite_page_url( Enum::PAGE_DASHBOARD ) ); // phpcs:ignore
272 exit;
273 }
274
275 $new_ui_pages = Helper::available_pages();
276 $parent_slug = 'edit.php?post_type=' . SIMPLE_URLS_SLUG;
277 foreach ( $new_ui_pages as $page ) {
278 if ( $get_page === $page->slug ) {
279 $this->current_template = $page->template;
280 }
281
282 add_submenu_page(
283 $parent_slug,
284 $page->title,
285 $page->title,
286 'manage_options',
287 $page->slug,
288 array( $this, 'render_html' )
289 );
290 }
291 }
292
293
294 /**
295 * Render html for pages
296 */
297 public function render_html() {
298 Helper::include_with_variables( Helper::get_path_views_folder() . $this->current_template, array(), false );
299 }
300
301 /**
302 * Load css files
303 */
304 public function add_styles() {
305 if ( ! Helper::is_lite_using_new_ui() ) {
306 return;
307 }
308
309 $setting = new Setting();
310 // @codingStandardsIgnoreStart
311
312 // ? Everywhere in the Lasso Lite post-type (add/edit/reports/settings/wizard)
313 if ( $setting->is_surls_page() ) {
314 Helper::enqueue_style( 'bootstrap-css', 'bootstrap.min.css' );
315 Helper::enqueue_style( 'bootstrap-select-css', 'bootstrap-select.min.css' );
316 Helper::enqueue_style( 'simple-panigation-css', 'simplePagination.css' );
317 Helper::enqueue_style( 'select2-css', 'select2.min.css' );
318 Helper::enqueue_style( 'lasso-quill', 'quill.snow.css' );
319 Helper::enqueue_style( 'lasso-lite', 'lasso-lite.css' );
320 Helper::enqueue_style( 'lasso-display-modal', 'lasso-display-modal.css' );
321 Helper::enqueue_style( 'lasso-dashboard', 'lasso-dashboard.css' );
322 Helper::enqueue_style( 'lasso-lite-custom', 'lite-custom.css' );
323 }
324
325 if ( $setting->is_setting_display_page() || $setting->is_setting_onboarding_page() ) {
326 Helper::enqueue_style( 'spectrum', 'spectrum.min.css' );
327 }
328
329 // ? LOAD LASSO DISPLAY MODAL CSS ON POST AND PAGE EDIT ONLY
330 if ( $setting->is_wordpress_post() || $setting->is_custom_post() ) {
331 Helper::enqueue_style( 'bootstrap-grid-css', 'bootstrap-grid.min.css' );
332 Helper::enqueue_style( 'lasso-display-modal', 'lasso-display-modal.css' );
333 Helper::enqueue_style( 'simple-pagination', 'simplePagination.css' );
334 Helper::enqueue_style( 'lasso-quill', 'quill.snow.css' );
335 Helper::enqueue_style( 'lasso-lite', 'lasso-lite.css' );
336 }
337
338 // @codingStandardsIgnoreEnd
339 }
340
341 /**
342 * Load js files
343 */
344 public function add_scripts() {
345 if ( ! Helper::is_lite_using_new_ui() ) {
346 return;
347 }
348
349 $get = Helper::GET(); // phpcs:ignore
350 $page = $get['page'] ?? false;
351 $setting = new Setting();
352 $post_type = $setting->get['post_type'] ?? false;
353 $setting_data = Setting::get_settings();
354 $support_enabled = $setting_data[ Enum::SUPPORT_ENABLED ] ?? false;
355 $support_enabled = ! $support_enabled ? 1 : 0;
356 $data_passed_to_js = array(
357 'ajax_url' => admin_url( 'admin-ajax.php' ),
358 'site_url' => site_url(),
359 'plugin_url' => SIMPLE_URLS_URL,
360 'rewrite_slug_default' => Enum::REWRITE_SLUG_DEFAULT,
361 'simple_urls_slug' => SIMPLE_URLS_SLUG,
362 'page_url_details' => Enum::PAGE_URL_DETAILS,
363 'setup_progress' => Helper::get_setup_progress_information(),
364 'optionsNonce' => wp_create_nonce( 'lasso_settings_save' ),
365 'should_open_support_modal' => $support_enabled,
366 'amazon_tracking_id_regex' => Amazon_Api::TRACKING_ID_REGEX,
367 'is_onboard_page' => $setting->is_setting_onboarding_page(),
368 'block_customize' => Constant::BLOCK_CUSTOMIZE,
369 );
370
371 if ( SIMPLE_URLS_SLUG === $post_type ) {
372 wp_dequeue_script( 'up_admin_script' ); // ? fix js conflict with plugin: Download plugin
373 }
374
375 // @codingStandardsIgnoreStart
376 if ( $setting->is_wordpress_post() || $setting->is_surls_page() || $setting->is_custom_post() ) {
377 wp_enqueue_script( 'jquery-migrate' ); // ? fix jQuery(...).live is not a function
378 wp_enqueue_script( 'jquery' );
379 wp_enqueue_script( 'jquery-effects-core' );
380 wp_enqueue_script( 'jquery-ui-core' );
381 wp_enqueue_script( 'jquery-ui-sortable' );
382 wp_enqueue_script( 'jquery-ui-tooltip' );
383
384 Helper::enqueue_script( 'lasso-icons', 'fontawesome.min.js', array( 'jquery' ) );
385 Helper::enqueue_script( 'lasso-icons-regular', 'regular.min.js', array( 'jquery' ) );
386 Helper::enqueue_script( 'lasso-icons-solid', 'solid.min.js', array( 'jquery' ) );
387 Helper::enqueue_script( 'lasso-icons-brands', 'brands.min.js', array( 'jquery' ) );
388 Helper::enqueue_script( 'circle-progress', 'circle-progress.min.js', array( 'jquery' ) );
389
390 Helper::enqueue_script( 'lasso-quill', 'quill.min.js' );
391 wp_enqueue_media();
392 Helper::enqueue_script( 'moment-js', 'moment.min.js', array( 'jquery' ) );
393 Helper::enqueue_script( 'select2-js', 'select2.full.min.js', array( 'jquery' ) );
394 Helper::enqueue_script( SIMPLE_URLS_SLUG . '-jq-auto-complete-js', 'jquery-autocomplete.js' );
395
396 Helper::enqueue_script( 'popper-js', 'popper.min.js', array( 'jquery' ) );
397 Helper::enqueue_script( 'bootstrap-js', 'bootstrap.min.js', array( 'jquery' ) );
398 Helper::enqueue_script( 'bootstrap-select-js', 'bootstrap-select.min.js', array( 'jquery' ) );
399 Helper::enqueue_script( 'pagination-js', 'jquery.simplePagination.js', array( 'jquery' ) );
400 Helper::enqueue_script( 'jsrender', 'jsrender.min.js' );
401
402 wp_localize_script( 'jquery', 'lassoLiteOptionsData', $data_passed_to_js );
403 Helper::enqueue_script( 'lasso-helper', 'lasso-helper.js', array( 'jquery' ) );
404 Helper::enqueue_script( 'url-add', 'url-add.js', array( 'jquery' ) );
405 Helper::enqueue_script( 'support', 'support.js', array( 'jquery' ) );
406
407 if ( SIMPLE_URLS_SLUG . '-' . Enum::PAGE_URL_DETAILS === $page ) {
408 Helper::enqueue_script( 'url-details', 'url-details.js', array( 'jquery' ) );
409 }
410 }
411
412 if ( $setting->is_dashboard_page() ) {
413 Helper::enqueue_script( 'dashboard', 'dashboard.js', array( 'jquery' ) );
414 }
415
416 if ( $setting->is_setting_display_page() || $setting->is_setting_onboarding_page() ) {
417 Helper::enqueue_script( 'spectrum-js', 'spectrum.min.js', array( 'jquery' ) );
418 Helper::enqueue_script( 'settings-display-js', 'settings-display.js', array( 'jquery' ) );
419 }
420
421 if ( $setting->is_setting_amazon_page() || $setting->is_setting_onboarding_page() ) {
422 Helper::enqueue_script( 'settings-amazon', 'settings-amazon.js', array( 'jquery' ) );
423 }
424
425 if ( $setting->is_import_page() || $setting->is_setting_onboarding_page() ) {
426 Helper::enqueue_script( 'settings-import', 'settings-import.js', array( 'jquery' ) );
427 }
428
429 if ( $setting->is_setting_onboarding_page() ) {
430 Helper::enqueue_script( 'onboarding-js', 'onboarding.js', array( 'jquery' ) );
431 Helper::enqueue_script( 'settings-js', 'settings.js', array( 'jquery' ) );
432 Helper::enqueue_script( 'settings-display-js', 'settings-display.js', array( 'jquery' ) );
433 }
434
435 if ( $setting->is_setting_general_page() ) {
436 Helper::enqueue_script( 'settings-general', 'settings-general.js', array( 'jquery' ) );
437 }
438
439 if ( $setting->is_group_detail_page() || $setting->is_group_page() ) {
440 Helper::enqueue_script( 'groups', 'groups.js', array( 'jquery' ) );
441 }
442
443 // @codingStandardsIgnoreEnd
444 }
445
446 /**
447 * DISPLAYS CSS FOR FRONTEND OF SITE
448 */
449 public function add_scripts_frontend() {
450 Helper::enqueue_style( 'lasso-lite', 'lasso-lite.css' );
451 }
452
453 /**
454 * DISPLAYS CUSTOM CSS FOR FRONTEND OF SITE
455 */
456 public function lasso_custom_css() {
457 if ( ! Helper::is_lite_using_new_ui() ) {
458 return;
459 }
460
461 $settings = Setting::get_settings();
462
463 // @codingStandardsIgnoreStart
464 echo '<style type="text/css">
465 :root{
466 --lasso-main: ' . $settings['display_color_main'] . ' !important;
467 --lasso-title: ' . $settings['display_color_title'] . ' !important;
468 --lasso-button: ' . $settings['display_color_button'] . ' !important;
469 --lasso-secondary-button: ' . $settings['display_color_secondary_button'] . ' !important;
470 --lasso-button-text: ' . $settings['display_color_button_text'] . ' !important;
471 --lasso-background: ' . $settings['display_color_background'] . ' !important;
472 --lasso-pros: ' . $settings['display_color_pros'] . ' !important;
473 --lasso-cons: ' . $settings['display_color_cons'] . ' !important;
474 }
475 </style>';
476
477 // fix fontawesome js render svg (from other plugins) instead of using css
478 echo '
479 <script type="text/javascript">
480 // Notice how this gets configured before we load Font Awesome
481 window.FontAwesomeConfig = { autoReplaceSvg: false }
482 </script>
483 ';
484 // @codingStandardsIgnoreEnd
485 }
486
487 /**
488 * Order menu and submenu of Lasso
489 *
490 * @param boolean $custom_menu Whether custom ordering is enabled. Default false.
491 */
492 public function lasso_order_submenu( $custom_menu ) {
493 global $submenu;
494
495 $new_submenu = array();
496 $menu_key = 'edit.php?post_type=' . SIMPLE_URLS_SLUG;
497 $lasso_lite_submenu = @$submenu[ $menu_key ]; // phpcs:ignore
498
499 $parent_slug = 'edit.php?post_type=' . SIMPLE_URLS_SLUG;
500 $hide_menu = array(
501 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_SETTINGS_GENERAL,
502 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_SETTINGS_AMAZON,
503 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_URL_DETAILS,
504 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_OPPORTUNITIES,
505 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_TABLES,
506 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_GROUPS,
507 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_GROUP_DETAIL,
508 );
509 $surl_dashboard = array(
510 SIMPLE_URLS_SLUG . '-' . Enum::PAGE_DASHBOARD,
511 'post-new.php?post_type=' . SIMPLE_URLS_SLUG,
512 );
513
514 if ( ! empty( $lasso_lite_submenu ) ) {
515 foreach ( $lasso_lite_submenu as $subpage ) {
516 // ? Show the new menus
517 if ( ! $this->show_old_menus && in_array( $subpage[2], $surl_dashboard, true ) ) { // ? Hide "Add new" and SURL Dashboard
518 continue;
519 } elseif ( ! $this->show_old_menus && $parent_slug === $subpage[2] ) { // ? Change title of root dashboard(all posts) menu
520 $subpage[0] = 'Dashboard';
521 $new_submenu[] = $subpage;
522 } elseif ( ! in_array( $subpage[2], $hide_menu, true ) ) {
523 if ( SIMPLE_URLS_SLUG . '-' . Enum::PAGE_SETTINGS_DISPLAY === $subpage[2] ) {
524 $subpage[0] = 'Settings';
525 }
526
527 $new_submenu[] = $subpage;
528 }
529 }
530
531 $new_submenu[90] = array(
532 'Support',
533 'manage_options',
534 Constant::LASSO_SUPPORT_URL,
535 );
536
537 $new_submenu[100] = array(
538 '<b class="green">Upgrade to Pro</b>',
539 'manage_options',
540 Constant::LASSO_UPGRADE_URL,
541 );
542 }
543
544 // ? ERROR | Overriding WordPress globals is prohibited. Found assignment to $submenu
545 // phpcs:ignore
546 $submenu[ $menu_key ] = $new_submenu;
547 }
548
549 /**
550 * Load lasso js file in Gutenberg editor
551 */
552 public function lasso_lite_gutenberg_block() {
553 if ( ! Helper::is_lasso_pro_installed() ) {
554 Helper::enqueue_script( 'lasso-lite-gutenberg-block', 'lasso-lite-gutenberg-block.js', array( 'wp-blocks', 'wp-editor' ), true );
555 Helper::enqueue_script( 'display-add', 'display-add.js', array( 'jquery' ) );
556 Helper::enqueue_script( 'url-add', 'url-add.js', array( 'jquery' ) );
557 }
558 }
559
560 /**
561 * Load lasso js file in Classic editor (TinyMCE)
562 *
563 * @param array $plugin_array An array of external TinyMCE plugins.
564 */
565 public function lasso_add_tinymce_plugin( $plugin_array ) {
566 if ( ! Helper::is_lasso_pro_plugin_active() ) {
567 $lasso_setting = new Setting();
568 if ( Helper::is_classic_editor_plugin_active() && ( $lasso_setting->is_wordpress_post() || $lasso_setting->is_custom_post() ) ) {
569 $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-display-modal.js' ) ); // phpcs:ignore
570 Helper::enqueue_script( 'display-add', 'display-add.js', array( 'jquery' ) );
571 Helper::enqueue_script( 'url-add', 'url-add.js', array( 'jquery' ) );
572 }
573 }
574
575 return $plugin_array;
576 }
577
578 /**
579 * Add Lasso button to Classic editor (TinyMCE)
580 *
581 * @param array $buttons First-row list of buttons.
582 */
583 public function lasso_lite_register_my_tc_button( $buttons ) {
584 if ( ! Helper::is_lasso_pro_plugin_active() ) {
585 array_push( $buttons, 'lasso_lite_tc_button' );
586 array_push( $buttons, 'lasso_grid_button' );
587 }
588
589 return $buttons;
590 }
591
592 /**
593 * Render template in admin footer on screen editor Gutenberg
594 */
595 public function lasso_lite_admin_footer_editor_gutenberg() {
596 if ( ! Helper::is_lasso_pro_plugin_active() ) {
597 $lasso_setting = new Setting();
598
599 if ( $lasso_setting->is_wordpress_post() || $lasso_setting->is_custom_post() ) {
600 $current_screen = get_current_screen();
601 // ? Check to make sure render html only block editor Gutenberg
602 if ( ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() )
603 || ( function_exists( 'is_gutenberg_page' ) ) && is_gutenberg_page()
604 ) {
605 echo Helper::get_display_modal_html(); // phpcs:ignore
606 }
607 }
608 }
609 }
610
611 /**
612 * Custom target menu
613 */
614 public function lasso_custom_menu() {
615 ?>
616 <script type="text/javascript">
617 jQuery( document ).ready( function( $ ) {
618 jQuery( "ul#adminmenu a[href$='<?php echo Constant::LASSO_SUPPORT_URL; // phpcs:ignore ?>']" ).attr( 'target', '_blank' );
619 jQuery( "ul#adminmenu a[href$='<?php echo Constant::LASSO_UPGRADE_URL; // phpcs:ignore ?>']" ).attr( 'target', '_blank' );
620 } );
621 </script>
622 <?php
623 }
624
625 /**
626 * Add Amazon tracking id to redirect link
627 *
628 * @param string $redirect Redirect link.
629 */
630 public function lasso_lite_redirect( $redirect ) {
631 if ( Amazon_Api::is_amazon_url( $redirect ) ) {
632 $redirect = Amazon_Api::get_amazon_product_url( $redirect );
633 }
634
635 return $redirect;
636 }
637
638 /**
639 * Pull Amazon API information is stored in AAWP, or Amalinks Pro automatically.
640 *
641 * @return $this
642 */
643 public function amazon_api_pre_populated_automatically() {
644 if ( Helper::get_option( Enum::IS_PRE_POPULATED_AMAZON_API ) ) {
645 return $this;
646 }
647
648 $api_key = '';
649 $api_secret = '';
650 $country = '';
651 $tracking_id = '';
652
653 // ? AAWP plugin
654 if ( Helper::is_aawp_active() ) {
655 $aawp_api = get_option( 'aawp_api' );
656
657 $api_key = $aawp_api['key'] ?? '';
658 $api_secret = $aawp_api['secret'] ?? '';
659 $country = $aawp_api['country'] ?? '';
660 $tracking_id = $aawp_api['associate_tag'] ?? '';
661 }
662
663 // ? AmaLinksPro plugin
664 if ( empty( $api_key ) && empty( $api_secret ) && empty( $tracking_id ) && Helper::is_amalinks_pro_active() ) {
665 $api_key = get_option( 'amalinkspro-options_amazon_api_access_key', '' );
666 $api_secret = get_option( 'amalinkspro-options_amazon_api_secret_key', '' );
667 $country = get_option( 'amalinkspro-options_default_amazon_search_locale', '' );
668 if ( ! empty( $country ) ) {
669 $tracking_id = get_option( 'amalinkspro-options_' . $country . '_amazon_associate_ids_0_associate_id', '' );
670 }
671 }
672
673 $lasso_options = Setting::get_settings();
674 $amazon_access_key_id = $lasso_options['amazon_access_key_id'] ?? '';
675 $amazon_secret_key = $lasso_options['amazon_secret_key'] ?? '';
676 $amazon_tracking_id = $lasso_options['amazon_tracking_id'] ?? '';
677
678 if ( empty( $amazon_access_key_id ) && empty( $amazon_secret_key ) && empty( $amazon_tracking_id )
679 && ! empty( $api_key ) && ! empty( $api_secret ) && ! empty( $tracking_id ) && ! empty( $country )
680 ) {
681 $country = strtolower( $country );
682
683 Setting::set_setting( 'amazon_access_key_id', $api_key );
684 Setting::set_setting( 'amazon_secret_key', $api_secret );
685 Setting::set_setting( 'amazon_default_tracking_country', $country );
686 Setting::set_setting( 'amazon_tracking_id', $tracking_id );
687
688 Helper::update_option( Enum::IS_PRE_POPULATED_AMAZON_API, 1 );
689 }
690
691 return $this;
692 }
693
694 /**
695 * Remove action "admin_print_footer_scripts" of "Easy Table of Contents" plugin
696 */
697 public function remove_easy_table_of_content_action_admin_print_footer_scripts() {
698 Helper::remove_action( 'admin_print_footer_scripts', array( 'eztoc_pointers', 'admin_print_footer_scripts' ) );
699 }
700
701 /**
702 * Prints the templates used in the media manager.
703 */
704 public function lasso_print_media_templates() {
705 if ( file_exists( ABSPATH . WPINC . '/media-template.php' ) && ! function_exists( 'wp_print_media_templates' ) ) {
706 require_once ABSPATH . WPINC . '/media-template.php';
707 }
708 wp_print_media_templates();
709 }
710
711 /**
712 * Add active class to the current page
713 */
714 public function lasso_organize_menu() {
715 $setting_page = ( new Setting() )->is_setting_page();
716 ?>
717 <script>
718 jQuery(document).ready(function(){
719 var lasso_menu = jQuery('#menu-posts-surl');
720 if(lasso_menu.is('.wp-has-current-submenu, .wp-menu-open')){
721 var submenu = lasso_menu.find('ul.wp-submenu').find('li');
722 if(submenu != undefined) {
723 if(!submenu.hasClass('current') || submenu.hasClass('current').length == 0) {
724 submenu.eq(1).addClass('current');
725 }
726
727 // add class `current` for Settings menu
728 if('<?php echo (int) $setting_page; ?>' == '1') {
729 submenu.removeClass('current');
730 lasso_menu.find('ul.wp-submenu').find('li:contains("Settings")').addClass('current');
731 }
732 }
733 }
734 });
735 </script>
736 <?php
737 }
738
739 /**
740 * Register a category
741 */
742 public function register_taxonomy() {
743 // ? register custom taxonomy
744 register_taxonomy(
745 Constant::LASSO_CATEGORY,
746 Constant::LASSO_POST_TYPE,
747 array(
748 'label' => __( 'Lasso Categories' ),
749 'rewrite' => array( 'slug' => Constant::LASSO_CATEGORY ),
750 'hierarchical' => false,
751 'public' => false,
752 'labels' => array(
753 'add_new_item' => __( 'Add New Category' ),
754 'edit_item' => __( 'Edit Categories' ),
755 ),
756 )
757 );
758 }
759
760 /**
761 * Use Amazon product url instead Lasso url when search/insert a link into a post/page
762 *
763 * @param array $response Response data to send to the client.
764 * @param object $server (WP_REST_Server) Server instance.
765 * @param object $request (WP_REST_Request) Request used to generate the response.
766 */
767 public function update_post_type_gutenberg( $response, $server, $request ) {
768 $params = $request->get_params();
769 $type = $params['type'] ?? '';
770 if ( 'post' === $type && is_array( $response ) && '/wp/v2/search' === $request->get_route() && count( $response ) > 0 ) {
771 foreach ( $response as $key => $post ) {
772 if ( 'post' === $post['type'] && Constant::LASSO_POST_TYPE === $post['subtype'] ) {
773 $response[ $key ]['subtype'] = Constant::LASSO_PRO_POST_TYPE; // ? update post type to Pro
774 }
775 }
776 }
777
778 return $response;
779 }
780
781 /**
782 * Custom post type to Lasso Branding
783 *
784 * @param array $results Search results.
785 *
786 * @return mixed
787 */
788 public function update_post_type_classic_editor( $results ) {
789 if ( ! empty( $results ) ) {
790 foreach ( $results as &$result ) {
791 $type = get_post_type( $result['ID'] );
792 if ( Constant::LASSO_POST_TYPE === $type ) {
793 $result['info'] = Constant::LASSO_BRAND;
794 }
795 }
796 }
797 return $results;
798 }
799
800 /**
801 * Update the text in the footer
802 *
803 * @param string $text Text.
804 */
805 public function admin_footer( $text ) {
806 global $current_screen;
807
808 $setting = new Setting();
809
810 if ( ! empty( $current_screen->id ) && $setting->is_surls_page() ) {
811 $url = Enum::LASSO_REVIEW_URL;
812 $text = sprintf(
813 wp_kses(
814 '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!',
815 array(
816 'a' => array(
817 'href' => array(),
818 'target' => array(),
819 'rel' => array(),
820 ),
821 )
822 ),
823 '<strong>Lasso Lite</strong>',
824 $url,
825 $url
826 );
827 }
828
829 return $text;
830 }
831 }
832