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

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

634 lines 23.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Declare class Affiliate_Link
4 *
5 * @package Enum
6 */
7
8 namespace LassoLite\Classes;
9
10 use LassoLite\Admin\Constant;
11 use LassoLite\Classes\Helper;
12 use LassoLite\Classes\Lasso_DB;
13 use LassoLite\Classes\Meta_Enum;
14 use LassoLite\Classes\Setting;
15
16 /**
17 * Affiliate_Link
18 */
19 class Affiliate_Link {
20 const DEFAULT_AMAZON_NAME = 'Amazon';
21 /**
22 * Edit detail page
23 *
24 * @var string $edit_details_page
25 */
26 public static $edit_details_page = 'surl-url-details';
27
28 /**
29 * Get Lasso post detail
30 *
31 * @param int $post_id Lasso post id.
32 * @param bool $is_detail Is detail page. Default to false.
33 */
34 public static function get_lasso_url( $post_id, $is_detail = false ) {
35 $post_id = intval( $post_id );
36 $post = get_post( $post_id );
37 $post_type = get_post_type( $post );
38 $post_status = get_post_status( $post );
39 $lasso_lite_settings = Setting::get_settings();
40
41 // ? default data
42 $edit_link = '';
43 $link_type = '';
44 $name = $is_detail ? '' : 'The Link Title Goes Here';
45 $slug = '';
46 $guid = '';
47 $permalink = '#';
48 $public_link = '#';
49 $image_src = Constant::DEFAULT_THUMBNAIL;
50 $image_src_default = 1;
51 $thumbnail_id = '';
52 $target_url = '';
53 $open_new_tab = true;
54 $enable_nofollow = true;
55 $enable_sponsored = true;
56 $description = '';
57 $price = '';
58 $display_last_updated = '';
59 $display_primary_button_text_default = $lasso_lite_settings['primary_button_text'];
60 $show_disclosure = $lasso_lite_settings['show_disclosure'] ?? true;
61 $disclosure_text = $lasso_lite_settings['disclosure_text'] ?? '';
62 $badge_text = '';
63 $is_amazon_page = false;
64 $display_primary_button_text = $display_primary_button_text_default;
65 $display_show_price = $lasso_lite_settings['show_price'] ?? true;
66
67 // ? real data
68 if ( $post_id > 0 && SIMPLE_URLS_SLUG === $post_type && 'publish' === $post_status && $post ) {
69 $name = $post->post_title;
70 $target_url = get_post_meta( $post_id, '_surl_redirect', true );
71 $slug = $post->post_name;
72 $permalink = get_permalink( $post_id );
73 $image_src = get_post_meta( $post_id, Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, true );
74 $display_primary_button_text = get_post_meta( $post_id, Meta_Enum::BUY_BTN_TEXT, true );
75 $open_new_tab = get_post_meta( $post_id, Meta_Enum::OPEN_NEW_TAB, true );
76 $enable_nofollow = get_post_meta( $post_id, Meta_Enum::ENABLE_NOFOLLOW, true );
77 $enable_sponsored = get_post_meta( $post_id, Meta_Enum::ENABLE_SPONSORED, true );
78 $thumbnail_id = get_post_meta( $post_id, Meta_Enum::THUMBNAIL_ID, true );
79 $description = get_post_meta( $post_id, Meta_Enum::DESCRIPTION, true );
80 $description = str_replace( '<p></p>', '', $description );
81 $price = get_post_meta( $post_id, Meta_Enum::PRICE, true );
82 $display_show_price = get_post_meta( $post_id, Meta_Enum::SHOW_PRICE, true );
83 $badge_text = get_post_meta( $post_id, Meta_Enum::BADGE_TEXT, true );
84 $show_disclosure = get_post_meta( $post_id, Meta_Enum::SHOW_DISCLOSURE, true );
85 $edit_link = self::affiliate_edit_link( $post_id );
86 $is_amazon_page = Amazon_Api::is_amazon_url( $target_url );
87
88 if ( '' === $image_src ) {
89 $image_src = Constant::DEFAULT_THUMBNAIL;
90 }
91
92 if ( '' === $display_primary_button_text ) {
93 $display_primary_button_text = $display_primary_button_text_default;
94 }
95
96 if ( '' === $open_new_tab ) {
97 $open_new_tab = true;
98 }
99
100 if ( '' === $enable_nofollow ) {
101 $enable_nofollow = true;
102 }
103
104 if ( '' === $display_show_price ) {
105 $display_show_price = true;
106 }
107
108 if ( '' === $show_disclosure ) {
109 $show_disclosure = $lasso_lite_settings['show_disclosure'] ?? true;
110 }
111
112 if ( $is_amazon_page ) {
113 $product_id = Amazon_Api::get_product_id_by_url( $target_url );
114 if ( $product_id ) {
115 $lasso_amazon_api = new Amazon_Api();
116 $product = $lasso_amazon_api->get_amazon_product_from_db( $product_id );
117
118 if ( $product ) {
119 $price = $product['latest_price'];
120 $display_last_updated = gmdate( 'm/d/Y h:i a T', strtotime( $product['last_updated'] ) );
121 }
122 }
123
124 $target_url = Amazon_Api::get_amazon_product_url( $target_url );
125 $public_link = $target_url;
126 } else {
127 $public_link = $permalink;
128 }
129 }
130
131 $url_detail_checkbox_open_new_tab = $open_new_tab ? 'checked' : '';
132 $url_detail_checkbox_enable_nofollow = $enable_nofollow ? 'checked' : '';
133 $url_detail_checkbox_show_price = $display_show_price ? 'checked' : '';
134 $url_detail_checkbox_enable_sponsored = $enable_sponsored ? 'checked' : '';
135 $url_detail_checkbox_show_disclosure = $show_disclosure ? 'checked' : '';
136
137 $rel = $enable_nofollow ? 'nofollow' : '';
138 $rel = $open_new_tab ? trim( $rel . ' noopener' ) : $rel;
139 $rel = $enable_sponsored ? trim( $rel . ' sponsored' ) : $rel;
140
141 $html_attribute_rel = 'rel="' . $rel . '"';
142 $html_attribute_target = $open_new_tab ? '_blank' : '_self';
143
144 $category = wp_get_post_terms( $post_id, Constant::LASSO_CATEGORY, array( 'fields' => 'ids' ) );
145 $category = is_array( $category ) ? $category : array();
146
147 $lasso_lite_url = (object) array(
148 'id' => $post_id,
149 'edit_link' => $edit_link,
150 'link_type' => $link_type,
151 'name' => trim( $name ),
152 'slug' => $slug,
153 'guid' => $guid,
154 'permalink' => $permalink,
155 'public_link' => Amazon_Api::get_amazon_product_url( $public_link ),
156 'image_src' => trim( $image_src ),
157 'image_src_default' => $image_src_default,
158 'thumbnail_id' => $thumbnail_id,
159 'target_url' => $target_url,
160 'open_new_tab' => $open_new_tab,
161 'enable_nofollow' => $enable_nofollow,
162 'enable_sponsored' => $enable_sponsored,
163 'description' => $description,
164 'category' => $category,
165 'show_disclosure' => $show_disclosure,
166 'is_amazon_page' => $is_amazon_page,
167 'price' => $price,
168 'display' => (object) array(
169 'primary_button_text' => $display_primary_button_text,
170 'primary_button_text_default' => $display_primary_button_text_default, // phpcs:ignore: use for placeholder
171 'theme' => Enum::THEME_CACTUS,
172 'show_price' => $display_show_price,
173 'badge_text' => $badge_text,
174 'disclosure_text' => $disclosure_text,
175 'last_updated' => $display_last_updated,
176 ),
177 'url_detail_checkbox' => (object) array(
178 'open_new_tab' => $url_detail_checkbox_open_new_tab,
179 'enable_nofollow' => $url_detail_checkbox_enable_nofollow,
180 'enable_sponsored' => $url_detail_checkbox_enable_sponsored,
181 'show_disclosure' => $url_detail_checkbox_show_disclosure,
182 'show_price' => $url_detail_checkbox_show_price,
183 ),
184 'html_attribute' => (object) array(
185 'rel' => $html_attribute_rel,
186 'target' => $html_attribute_target,
187 ),
188 );
189
190 return $lasso_lite_url;
191 }
192
193 /**
194 * Add a new Lasso link
195 *
196 * @param string $link Link. Default to empty.
197 * @return void|string
198 */
199 public function add_a_new_link( $link = '' ) {
200 $link = trim( $link ?? '' );
201 $url = trim( $link != '' ? $link : ( $_POST['link'] ?? '' ) ); // phpcs:ignore
202
203 $is_ajax_request = wp_doing_ajax() && '' === $link;
204
205 if ( '' === $url ) {
206 if ( $is_ajax_request ) {
207 wp_send_json_error( 'No data to save.' );
208 } else {
209 return 'No data to save.';
210 }
211 }
212
213 $lasso_amazon_api = new Amazon_Api();
214
215 $url = Helper::add_https( $url );
216 $url = Helper::format_url_before_requesting( $url );
217 $url = Amazon_Api::get_redirect_url( $url );
218 $is_amazon_link = Amazon_Api::is_amazon_url( $url );
219 $get_final_url = $url;
220 $permalink = Helper::get_title_by_url( $url );
221 $title = $is_amazon_link ? self::DEFAULT_AMAZON_NAME : $permalink;
222 $default_title = $title;
223 $image = Constant::DEFAULT_THUMBNAIL;
224
225 // ? Check whether product is existing
226 $lasso_post_id = self::get_lasso_lite_post_id_by_url( $url );
227
228 if ( $lasso_post_id > 0 ) {
229 wp_update_post(
230 array(
231 'ID' => $lasso_post_id,
232 'post_status' => 'publish',
233 )
234 );
235 if ( $is_ajax_request ) {
236 wp_send_json_success(
237 array(
238 'success' => true,
239 'is_duplicate' => true,
240 'post_id' => $lasso_post_id,
241 )
242 );
243 } else {
244 return $lasso_post_id;
245 }
246 }
247
248 $url = Amazon_Api::get_amazon_product_url( $url, true, false );
249 $product_id = Amazon_Api::get_product_id_by_url( $get_final_url );
250
251 if ( $is_amazon_link && $product_id ) {
252 $product = $lasso_amazon_api->get_amazon_product_from_db( $product_id );
253
254 if ( $product ) {
255 $lasso_amazon_api->update_amazon_product_in_db(
256 array(
257 'product_id' => $product['amazon_id'],
258 'title' => $product['default_product_name'],
259 'price' => $product['latest_price'],
260 'default_url' => $product['base_url'],
261 'url' => $url,
262 'image' => $product['default_image'],
263 'quantity' => '0' === $product['out_of_stock'] ? 200 : 0, // ? Manual checks won't show out of stock for now. TODO: Add BLS to out of stock checks.
264 'is_manual' => $product['is_manual'],
265 'is_prime' => $product['is_prime'],
266 'features' => $product['features'],
267 'currency' => $product['currency'],
268 'savings_amount' => $product['savings_amount'],
269 'savings_percent' => $product['savings_percent'],
270 'savings_basis' => $product['savings_basis'],
271 )
272 );
273 }
274
275 if ( ! $product ) {
276 $product_info = $lasso_amazon_api->fetch_product_info( $product_id, true, false, $get_final_url );
277 if ( ! empty( $product_info ) ) {
278 $product = $product_info['product'];
279
280 if ( 'NotFound' === $product_info['error_code'] ) {
281 $res['status_code'] = 404;
282 $product['default_product_name'] = self::DEFAULT_AMAZON_NAME;
283 $product['default_image'] = Constant::DEFAULT_THUMBNAIL;
284 $product['monetized_url'] = $url;
285 } else {
286 $product['default_product_name'] = $product['title'];
287 $product['default_image'] = $product['image'];
288 $product['monetized_url'] = $product['url'];
289 }
290
291 $title = $product['default_product_name'];
292 $image = $product['default_image'];
293 }
294 }
295
296 $url = Amazon_Api::get_amazon_product_url( $get_final_url, true, false );
297 $title = $product['default_product_name'] ?? $title;
298 $image = $product['default_image'] ?? $image;
299 }
300
301 if ( ! $title ) {
302 $title = Helper::get_title_by_url( $url );
303 }
304
305 $affiliate_link = array(
306 'is_amazon' => $is_amazon_link,
307 'affiliate_name' => $title,
308 'surl_redirect' => trim( $url ),
309 'affiliate_desc' => '',
310 'permalink' => sanitize_title( $title ),
311 'is_opportunity' => 1,
312 'buy_btn_text' => '',
313 'second_btn_text' => '',
314 'price' => '',
315 'badge_text' => '',
316 'second_btn_url' => '',
317 'thumbnail' => $image,
318 'description' => '',
319 );
320
321 $data['settings'] = $affiliate_link;
322 $post_id = $this->save_lasso_url( $data );
323
324 if ( '' !== $link ) {
325 return $post_id;
326 }
327
328 wp_send_json_success(
329 array(
330 'success' => true,
331 'url' => $url,
332 'title' => $title,
333 'post_id' => $post_id,
334 )
335 );
336 }
337
338 /**
339 * Save Lasso data into DB
340 *
341 * @param array $data Lasso data. Default to null.
342 * @param bool $is_ajax Is request ajax. Default to false.
343 */
344 public function save_lasso_url( $data = null, $is_ajax = false ) {
345 $lasso_db = new Lasso_DB();
346 $lasso_amazon_api = new Amazon_Api();
347
348 $warning = '';
349 $is_ajax_request = wp_doing_ajax() && ( '' === $data || null === $data );
350 $is_ajax_request = $is_ajax_request || $is_ajax;
351 $post = is_array( $data ) ? $data : $_POST; // phpcs:ignore
352
353 $post = wp_unslash( $post ); // phpcs:ignore
354 $post_id = intval( $post['post_id'] ?? 0 );
355 $is_update = $post_id > 0;
356 $is_change_primary_link = $post['is_change_primary_link'] ?? false;
357 $is_change_primary_link = Helper::cast_to_boolean( $is_change_primary_link );
358 $is_change_primary_link = true === $is_change_primary_link;
359
360 $thumbnail_id = $post['thumbnail_id'] ?? 0;
361 $post_data = $post['settings'] ?? array();
362
363 if ( empty( $post_data ) || ! is_array( $post_data ) ) {
364 $error_message = 'No data to save.';
365 if ( $is_ajax_request ) {
366 wp_send_json_error( $error_message );
367 } else {
368 return $error_message;
369 }
370 }
371
372 if ( empty( trim( $post_data['surl_redirect'] ?? '' ) ) || empty( trim( $post_data['affiliate_name'] ?? '' ) ) ) {
373 $error_message = 'Name and Target URL are required.';
374 if ( $is_ajax_request ) {
375 wp_send_json_error( $error_message );
376 } else {
377 return $error_message;
378 }
379 }
380
381 $lasso_lite_url = self::get_lasso_url( $post_id );
382 $post_title = $post_data['affiliate_name'];
383 $post_name = $post_data['permalink'] ?? $lasso_lite_url->slug ?? '';
384 $surl_redirect = trim( $post_data['surl_redirect'] ?? '' );
385 $buy_btn_text = $post_data['buy_btn_text'] ?? $lasso_lite_url->display->primary_button_text;
386 $open_new_tab = $post_data['open_new_tab'] ?? $lasso_lite_url->open_new_tab;
387 $enable_nofollow = $post_data['enable_nofollow'] ?? $lasso_lite_url->enable_nofollow;
388 $enable_sponsored = $post_data['enable_sponsored'] ?? $lasso_lite_url->enable_sponsored;
389 $show_disclosure = $post_data['show_disclosure'] ?? $lasso_lite_url->show_disclosure;
390 $show_price = $post_data['show_price'] ?? $lasso_lite_url->display->show_price;
391 $price = $post_data['price'] ?? $lasso_lite_url->price;
392 $thumbnail = $post_data['thumbnail'] ?? Constant::DEFAULT_THUMBNAIL;
393 $description = $post_data['description'] ?? $lasso_lite_url->description;
394 $term = isset( $post_data['categories'] ) && is_array( $post_data['categories'] ) ? $post_data['categories'] : array();
395 $badge_text = $post_data['badge_text'] ?? '';
396 $term = array_map(
397 function( $val ) {
398 $term_id = is_numeric( $val ) ? intval( $val ) : 0;
399 $term_id = get_term_by( 'name', $val, Constant::LASSO_CATEGORY )->term_id ?? $term_id;
400 // ? Support category name is number and different existed term ids.
401 $term_id = $term_id && term_exists( $term_id ) ? $term_id : 0;
402
403 if ( 0 === $term_id && ! empty( $val ) ) { // ? add new category
404 $result = wp_insert_term( $val, Constant::LASSO_CATEGORY );
405 $term_id = ( ! is_wp_error( $result ) ) ? $result['term_id'] : 0;
406 }
407
408 return $term_id;
409 },
410 $term
411 );
412
413 // ? Check whether product is existing
414 $lasso_post_id = self::get_lasso_lite_post_id_by_url( $surl_redirect );
415 if ( $lasso_post_id > 0 && $is_update && $is_change_primary_link ) {
416 wp_update_post(
417 array(
418 'ID' => $lasso_post_id,
419 'post_status' => 'publish',
420 )
421 );
422 if ( $is_ajax_request ) {
423 wp_send_json_success(
424 array(
425 'success' => true,
426 'is_duplicate' => true,
427 'post_id' => $lasso_post_id,
428 )
429 );
430 } else {
431 return $lasso_post_id;
432 }
433 }
434
435 $affiliate_homepage = Helper::get_base_domain( $surl_redirect );
436 $is_opportunity = 1;
437 $product_id = Amazon_Api::get_product_id_by_url( $surl_redirect );
438 $product_type = Amazon_Api::is_amazon_url( $surl_redirect ) ? Amazon_Api::PRODUCT_TYPE : '';
439
440 if ( Amazon_Api::PRODUCT_TYPE === $product_type ) {
441 $product = $lasso_amazon_api->get_amazon_product_from_db( $product_id );
442 if ( ! $product ) {
443 $product = $lasso_amazon_api->fetch_product_info( $product_id, true, false, $surl_redirect );
444 }
445
446 $is_importing = $data['is_importing'] ?? false;
447 $old_redirect_url = get_post_meta( $post_id, Meta_Enum::SURL_REDIRECT, true );
448 $old_thumbnail = get_post_meta( $post_id, Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, true );
449 $update_title = 'Amazon' === $post_title;
450 $update_thumbnail = Constant::DEFAULT_THUMBNAIL === $thumbnail || $is_importing;
451 $use_defined_affiliate_name = $data['use_defined_affiliate_name'] ?? false;
452
453 if ( Amazon_Api::is_amazon_url( $old_redirect_url ) ) {
454 $old_amazon_id = Amazon_Api::get_product_id_by_url( $old_redirect_url );
455 $update_title = ( $product_id !== $old_amazon_id || $update_title ) && ! $use_defined_affiliate_name;
456 $update_thumbnail = strpos( $old_thumbnail, 'media-amazon.' ) !== false || Constant::DEFAULT_THUMBNAIL === $thumbnail;
457 } else {
458 $update_title = $use_defined_affiliate_name ? false : true;
459 }
460
461 $post_title = $update_title ? ( $product['default_product_name'] ?? $post_title ) : $post_title;
462 $thumbnail = $update_thumbnail ? ( $product['default_image'] ?? ( $product['product']['image'] ?? $thumbnail ) ) : $thumbnail;
463 }
464
465 $lasso_lite_post = array(
466 'post_title' => $post_title,
467 'post_type' => SIMPLE_URLS_SLUG,
468 'post_name' => $post_name,
469 'post_content' => '',
470 'post_status' => 'publish',
471 'meta_input' => array(
472 Meta_Enum::SURL_REDIRECT => $surl_redirect,
473 Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL => $thumbnail,
474 Meta_Enum::OPEN_NEW_TAB => $open_new_tab,
475 Meta_Enum::ENABLE_NOFOLLOW => $enable_nofollow,
476 Meta_Enum::BUY_BTN_TEXT => $buy_btn_text,
477 Meta_Enum::DESCRIPTION => $description,
478 Meta_Enum::SHOW_PRICE => $show_price,
479 Meta_Enum::PRICE => $price,
480 Meta_Enum::ENABLE_SPONSORED => $enable_sponsored,
481 Meta_Enum::SHOW_DISCLOSURE => $show_disclosure,
482 Meta_Enum::BADGE_TEXT => $badge_text,
483 ),
484 );
485
486 if ( $is_update ) {
487 // ? Check duplicate slug
488 $duplicate_post = Helper::the_slug_exists( $post_name, $post_id );
489
490 if ( $duplicate_post ) {
491 $warning = 'Permalink <a href="' . get_edit_post_link( $duplicate_post['ID'] ) . '" class="white underline" target="_blank"><strong>' . $duplicate_post['post_name'] . '</strong></a> is being used by <strong>' . $duplicate_post['post_type'] . '</strong>. We updated the permalink to avoid a conflict.';
492 }
493 // ? END
494
495 $lasso_lite_post['ID'] = $post_id;
496 wp_update_post( $lasso_lite_post );
497 clean_post_cache( $post_id );
498 } else {
499 $post_id = wp_insert_post( $lasso_lite_post, true );
500 }
501
502 if ( ! is_wp_error( $post_id ) && $post_id > 0 ) {
503 // ? update categories
504 wp_set_object_terms( $post_id, $term, Constant::LASSO_CATEGORY );
505 $lasso_db->update_url_details( $post_id, $surl_redirect, $affiliate_homepage, $is_opportunity, $product_id, $product_type );
506 // ? update thumbnail
507 if ( $thumbnail_id > 0 ) {
508 set_post_thumbnail( $post_id, $thumbnail_id );
509 $image_url = wp_get_attachment_url( $thumbnail_id );
510 update_post_meta( $post_id, Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, $image_url );
511 } else {
512 delete_post_thumbnail( $post_id );
513 }
514 $lasso_lite_url = self::get_lasso_url( $post_id );
515 }
516
517 if ( $is_ajax_request ) {
518 $get_display_html = $post['get_display_html'] ?? false;
519 $display_html = '';
520 if ( 'true' === $get_display_html ) {
521 $shortcode = '[lasso id="' . $lasso_lite_url->id . '" rel="' . $lasso_lite_url->slug . '"]';
522 $display_html = do_shortcode( $shortcode );
523 }
524
525 wp_send_json_success(
526 array(
527 'success' => 1,
528 'warning' => $warning,
529 'post' => $lasso_lite_url,
530 'display_html' => $display_html,
531 )
532 );
533 }
534
535 return $post_id;
536 }
537
538 /**
539 * Get edit url of post
540 *
541 * @param int $post_id The post id.
542 * @return string Edit url of the post id.
543 */
544 public static function affiliate_edit_link( $post_id = 0 ) {
545 $query = array(
546 'post_type' => SIMPLE_URLS_SLUG,
547 'page' => self::$edit_details_page,
548 'post_id' => $post_id,
549 );
550
551 return add_query_arg(
552 $query,
553 admin_url( 'edit.php' )
554 );
555 }
556
557 /**
558 * Get amazon product if by Lasso post id
559 *
560 * @param int $lasso_id Lasso post id.
561 */
562 public static function get_amazon_id( $lasso_id ) {
563 $lasso_db = new Lasso_DB();
564 $lasso_post_details = $lasso_db->get_url_details( $lasso_id );
565 $details_product_id = $lasso_post_details->product_id ?? '';
566
567 $amazon_product_id = get_post_meta( $lasso_id, 'amazon_product_id', true );
568
569 return '' === $details_product_id ? $amazon_product_id : $details_product_id;
570 }
571
572 /**
573 * Get Lasso Lite post id by url
574 *
575 * @param string $url URL.
576 * @param int $default_id Default id. Default to 0.
577 */
578 public static function get_lasso_lite_post_id_by_url( $url, $default_id = 0 ) {
579 $lasso_lite_db = new Lasso_DB();
580
581 // ? Get post id from url
582 $lasso_lite_id = $default_id;
583 $url = trim( $url, '/' );
584 $url = str_replace( '&amp;', '&', $url );
585 $parse = wp_parse_url( $url );
586 $path = '';
587
588 if ( strpos( $url, home_url() ) !== false && isset( $parse['path'] ) ) {
589 $path = $parse['path'];
590 $path = trim( $path, '/' );
591
592 $explode = explode( '/', $path );
593 $slug = end( $explode );
594 $lasso = get_page_by_path( $slug, OBJECT, Constant::LASSO_POST_TYPE );
595
596 if ( $lasso ) {
597 $lasso_lite_id = Constant::LASSO_POST_TYPE === get_post_type( $lasso->ID ) ? $lasso->ID : $lasso_lite_id;
598 }
599 }
600
601 if ( 0 === $lasso_lite_id ) {
602 $lasso_post = $lasso_lite_db->get_lasso_by_uri( $path ); // ? by redirect url
603 $lasso_lite_id = $lasso_post->ID ?? $lasso_lite_id;
604 }
605
606 if ( 0 === $lasso_lite_id ) {
607 $detail = $lasso_lite_db->get_url_details_by_url( $url ); // ? by redirect url
608 $lasso_lite_id = $detail->lasso_id ?? $lasso_lite_id;
609 }
610
611 if ( 0 === $lasso_lite_id && Amazon_Api::is_amazon_url( $url ) ) {
612 $amazon_product_id = Amazon_Api::get_product_id_by_url( $url );
613 $product_exist = $lasso_lite_db->check_amazon_product_exist( $amazon_product_id );
614 $lasso_post_id = $lasso_lite_db->get_lasso_id_by_product_id_and_type( $product_exist['amazon_id'] ?? '' );
615 $lasso_lite_id = $lasso_post_id ? $lasso_post_id : $default_id;
616 }
617
618 if ( 0 === $lasso_lite_id ) {
619 $lasso_lite_id = Lasso_DB::get_lasso_lite_id_by_url_from_post_meta( $url );
620 }
621
622 $tmp_url = Helper::get_final_url_from_url_param( $url );
623 $tmp_url = $tmp_url ? $tmp_url : $url;
624 $k = Amazon_Api::is_amazon_search_page( $tmp_url );
625 if ( 0 === $lasso_lite_id && $k ) {
626 $k = rawurlencode( $k );
627 $detail = $lasso_lite_db->get_url_details_by_url( '%/s?k=' . $k . '%' ); // ? by redirect url
628 $lasso_lite_id = $detail->lasso_id ?? $lasso_lite_id;
629 }
630
631 return intval( $lasso_lite_id );
632 }
633 }
634