PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 109
Lasso Lite – Affiliate Link Manager & Product Displays v109
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 109, at classes/class-affiliate-link.php

516 lines 19.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\Meta_Enum;
12 use LassoLite\Classes\Setting;
13
14 /**
15 * Affiliate_Link
16 */
17 class Affiliate_Link {
18 const DEFAULT_AMAZON_NAME = 'Amazon';
19 /**
20 * Edit detail page
21 *
22 * @var string $edit_details_page
23 */
24 public static $edit_details_page = 'surl-url-details';
25
26 /**
27 * Get Lasso post detail
28 *
29 * @param int $post_id Lasso post id.
30 * @param bool $is_detail Is detail page. Default to false.
31 */
32 public static function get_lasso_url( $post_id, $is_detail = false ) {
33 $post_id = intval( $post_id );
34 $post = get_post( $post_id );
35 $post_type = get_post_type( $post );
36 $post_status = get_post_status( $post );
37 $lasso_lite_settings = Setting::get_settings();
38
39 // ? default data
40 $edit_link = '';
41 $link_type = '';
42 $name = $is_detail ? '' : 'The Link Title Goes Here';
43 $slug = '';
44 $guid = '';
45 $permalink = '#';
46 $public_link = '#';
47 $image_src = Constant::DEFAULT_THUMBNAIL;
48 $image_src_default = 1;
49 $thumbnail_id = '';
50 $target_url = '';
51 $open_new_tab = true;
52 $enable_nofollow = true;
53 $enable_sponsored = true;
54 $description = '';
55 $price = '';
56 $display_primary_button_text_default = $lasso_lite_settings['primary_button_text'];
57 $show_disclosure = $lasso_lite_settings['show_disclosure'] ?? true;
58 $disclosure_text = $lasso_lite_settings['disclosure_text'] ?? '';
59 $badge_text = '';
60 $is_amazon_page = false;
61 $display_primary_button_text = $display_primary_button_text_default;
62 $display_show_price = $lasso_lite_settings['show_price'] ?? true;
63
64 // ? real data
65 if ( $post_id > 0 && SIMPLE_URLS_SLUG === $post_type && 'publish' === $post_status && $post ) {
66 $name = $post->post_title;
67 $target_url = get_post_meta( $post_id, '_surl_redirect', true );
68 $slug = $post->post_name;
69 $permalink = get_permalink( $post_id );
70 $image_src = get_post_meta( $post_id, Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, true );
71 $display_primary_button_text = get_post_meta( $post_id, Meta_Enum::BUY_BTN_TEXT, true );
72 $open_new_tab = get_post_meta( $post_id, Meta_Enum::OPEN_NEW_TAB, true );
73 $enable_nofollow = get_post_meta( $post_id, Meta_Enum::ENABLE_NOFOLLOW, true );
74 $enable_sponsored = get_post_meta( $post_id, Meta_Enum::ENABLE_SPONSORED, true );
75 $thumbnail_id = get_post_meta( $post_id, Meta_Enum::THUMBNAIL_ID, true );
76 $description = get_post_meta( $post_id, Meta_Enum::DESCRIPTION, true );
77 $description = str_replace( '<p></p>', '', $description );
78 $price = get_post_meta( $post_id, Meta_Enum::PRICE, true );
79 $display_show_price = get_post_meta( $post_id, Meta_Enum::SHOW_PRICE, true );
80 $badge_text = get_post_meta( $post_id, Meta_Enum::BADGE_TEXT, true );
81 $show_disclosure = get_post_meta( $post_id, Meta_Enum::SHOW_DISCLOSURE, true );
82 $edit_link = self::affiliate_edit_link( $post_id );
83 $is_amazon_page = Amazon_Api::is_amazon_url( $target_url );
84
85 if ( '' === $image_src ) {
86 $image_src = Constant::DEFAULT_THUMBNAIL;
87 }
88
89 if ( '' === $display_primary_button_text ) {
90 $display_primary_button_text = $display_primary_button_text_default;
91 }
92
93 if ( '' === $open_new_tab ) {
94 $open_new_tab = true;
95 }
96
97 if ( '' === $enable_nofollow ) {
98 $enable_nofollow = true;
99 }
100
101 if ( '' === $display_show_price ) {
102 $display_show_price = true;
103 }
104
105 if ( '' === $show_disclosure ) {
106 $show_disclosure = $lasso_lite_settings['show_disclosure'] ?? true;
107 }
108
109 if ( $is_amazon_page ) {
110 $product_id = Amazon_Api::get_product_id_by_url( $target_url );
111 if ( $product_id ) {
112 $lasso_amazon_api = new Amazon_Api();
113 $product = $lasso_amazon_api->get_amazon_product_from_db( $product_id );
114
115 if ( $product ) {
116 $price = $product['latest_price'];
117 }
118 }
119
120 $target_url = Amazon_Api::get_amazon_product_url( $target_url );
121 $public_link = $target_url;
122 } else {
123 $public_link = $permalink;
124 }
125 }
126
127 $url_detail_checkbox_open_new_tab = $open_new_tab ? 'checked' : '';
128 $url_detail_checkbox_enable_nofollow = $enable_nofollow ? 'checked' : '';
129 $url_detail_checkbox_show_price = $display_show_price ? 'checked' : '';
130 $url_detail_checkbox_enable_sponsored = $enable_sponsored ? 'checked' : '';
131 $url_detail_checkbox_show_disclosure = $show_disclosure ? 'checked' : '';
132
133 $rel = $enable_nofollow ? 'nofollow' : '';
134 $rel = $open_new_tab ? trim( $rel . ' noopener' ) : $rel;
135 $rel = $enable_sponsored ? trim( $rel . ' sponsored' ) : $rel;
136
137 $html_attribute_rel = 'rel="' . $rel . '"';
138 $html_attribute_target = $open_new_tab ? '_blank' : '_self';
139
140 $category = wp_get_post_terms( $post_id, Constant::LASSO_CATEGORY, array( 'fields' => 'ids' ) );
141 $category = is_array( $category ) ? $category : array();
142
143 $lasso_lite_url = (object) array(
144 'id' => $post_id,
145 'edit_link' => $edit_link,
146 'link_type' => $link_type,
147 'name' => trim( $name ),
148 'slug' => $slug,
149 'guid' => $guid,
150 'permalink' => $permalink,
151 'public_link' => Amazon_Api::get_amazon_product_url( $public_link ),
152 'image_src' => trim( $image_src ),
153 'image_src_default' => $image_src_default,
154 'thumbnail_id' => $thumbnail_id,
155 'target_url' => $target_url,
156 'open_new_tab' => $open_new_tab,
157 'enable_nofollow' => $enable_nofollow,
158 'enable_sponsored' => $enable_sponsored,
159 'description' => $description,
160 'category' => $category,
161 'show_disclosure' => $show_disclosure,
162 'is_amazon_page' => $is_amazon_page,
163 'price' => $price,
164 'display' => (object) array(
165 'primary_button_text' => $display_primary_button_text,
166 'primary_button_text_default' => $display_primary_button_text_default, // phpcs:ignore: use for placeholder
167 'theme' => Enum::THEME_CACTUS,
168 'show_price' => $display_show_price,
169 'badge_text' => $badge_text,
170 'disclosure_text' => $disclosure_text,
171 ),
172 'url_detail_checkbox' => (object) array(
173 'open_new_tab' => $url_detail_checkbox_open_new_tab,
174 'enable_nofollow' => $url_detail_checkbox_enable_nofollow,
175 'enable_sponsored' => $url_detail_checkbox_enable_sponsored,
176 'show_disclosure' => $url_detail_checkbox_show_disclosure,
177 'show_price' => $url_detail_checkbox_show_price,
178 ),
179 'html_attribute' => (object) array(
180 'rel' => $html_attribute_rel,
181 'target' => $html_attribute_target,
182 ),
183 );
184
185 return $lasso_lite_url;
186 }
187
188 /**
189 * Add a new Lasso link
190 *
191 * @param string $link Link. Default to empty.
192 * @return void|string
193 */
194 public function add_a_new_link( $link = '' ) {
195 $link = trim( $link ?? '' );
196 $url = trim( $link != '' ? $link : ( $_POST['link'] ?? '' ) ); // phpcs:ignore
197
198 $is_ajax_request = wp_doing_ajax() && '' === $link;
199
200 if ( '' === $url ) {
201 if ( $is_ajax_request ) {
202 wp_send_json_error( 'No data to save.' );
203 } else {
204 return 'No data to save.';
205 }
206 }
207
208 $lasso_amazon_api = new Amazon_Api();
209
210 $url = Helper::add_https( $url );
211 $url = Helper::format_url_before_requesting( $url );
212 $url = Amazon_Api::get_redirect_url( $url );
213 $is_amazon_link = Amazon_Api::is_amazon_url( $url );
214 $get_final_url = $url;
215 $permalink = Helper::get_title_by_url( $url );
216 $title = $is_amazon_link ? self::DEFAULT_AMAZON_NAME : $permalink;
217 $default_title = $title;
218 $image = Constant::DEFAULT_THUMBNAIL;
219
220 $url = Amazon_Api::get_amazon_product_url( $url, true, false );
221 $product_id = Amazon_Api::get_product_id_by_url( $get_final_url );
222
223 if ( $is_amazon_link && $product_id ) {
224 $product = $lasso_amazon_api->get_amazon_product_from_db( $product_id );
225
226 if ( $product ) {
227 $lasso_amazon_api->update_amazon_product_in_db(
228 array(
229 'product_id' => $product['amazon_id'],
230 'title' => $product['default_product_name'],
231 'price' => $product['latest_price'],
232 'default_url' => $product['base_url'],
233 'url' => $url,
234 'image' => $product['default_image'],
235 '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.
236 'is_manual' => $product['is_manual'],
237 'is_prime' => $product['is_prime'],
238 'features' => $product['features'],
239 'currency' => $product['currency'],
240 'savings_amount' => $product['savings_amount'],
241 'savings_percent' => $product['savings_percent'],
242 'savings_basis' => $product['savings_basis'],
243 )
244 );
245 }
246
247 if ( ! $product ) {
248 $product_info = $lasso_amazon_api->fetch_product_info( $product_id, true, false, $get_final_url );
249 if ( ! empty( $product_info ) ) {
250 $product = $product_info['product'];
251
252 if ( 'NotFound' === $product_info['error_code'] ) {
253 $res['status_code'] = 404;
254 $product['default_product_name'] = self::DEFAULT_AMAZON_NAME;
255 $product['default_image'] = Constant::DEFAULT_THUMBNAIL;
256 $product['monetized_url'] = $url;
257 } else {
258 $product['default_product_name'] = $product['title'];
259 $product['default_image'] = $product['image'];
260 $product['monetized_url'] = $product['url'];
261 }
262
263 $title = $product['default_product_name'];
264 $image = $product['default_image'];
265 }
266 }
267
268 $url = Amazon_Api::get_amazon_product_url( $get_final_url, true, false );
269 $title = $product['default_product_name'] ?? $title;
270 $image = $product['default_image'] ?? $image;
271 }
272
273 if ( ! $title ) {
274 $title = Helper::get_title_by_url( $url );
275 }
276
277 $affiliate_link = array(
278 'is_amazon' => $is_amazon_link,
279 'affiliate_name' => $title,
280 'surl_redirect' => trim( $url ),
281 'affiliate_desc' => '',
282 'permalink' => sanitize_title( $title ),
283 'is_opportunity' => 1,
284 'buy_btn_text' => '',
285 'second_btn_text' => '',
286 'price' => '',
287 'badge_text' => '',
288 'second_btn_url' => '',
289 'thumbnail' => $image,
290 'description' => '',
291 );
292
293 $data['settings'] = $affiliate_link;
294 $post_id = $this->save_lasso_url( $data );
295
296 if ( '' !== $link ) {
297 return $post_id;
298 }
299
300 wp_send_json_success(
301 array(
302 'success' => true,
303 'url' => $url,
304 'title' => $title,
305 'post_id' => $post_id,
306 )
307 );
308 }
309
310 /**
311 * Save Lasso data into DB
312 *
313 * @param array $data Lasso data. Default to null.
314 * @param bool $is_ajax Is request ajax. Default to false.
315 */
316 public function save_lasso_url( $data = null, $is_ajax = false ) {
317 $lasso_db = new Lasso_DB();
318 $lasso_amazon_api = new Amazon_Api();
319
320 $warning = '';
321 $is_ajax_request = wp_doing_ajax() && ( '' === $data || null === $data );
322 $is_ajax_request = $is_ajax_request || $is_ajax;
323 $post = is_array( $data ) ? $data : $_POST; // phpcs:ignore
324
325 $post = wp_unslash( $post ); // phpcs:ignore
326 $post_id = intval( $post['post_id'] ?? 0 );
327 $is_update = $post_id > 0;
328
329 $thumbnail_id = $post['thumbnail_id'] ?? 0;
330 $post_data = $post['settings'] ?? array();
331
332 if ( empty( $post_data ) || ! is_array( $post_data ) ) {
333 $error_message = 'No data to save.';
334 if ( $is_ajax_request ) {
335 wp_send_json_error( $error_message );
336 } else {
337 return $error_message;
338 }
339 }
340
341 if ( empty( trim( $post_data['surl_redirect'] ?? '' ) ) || empty( trim( $post_data['affiliate_name'] ?? '' ) ) ) {
342 $error_message = 'Name and Target URL are required.';
343 if ( $is_ajax_request ) {
344 wp_send_json_error( $error_message );
345 } else {
346 return $error_message;
347 }
348 }
349
350 $lasso_lite_url = self::get_lasso_url( $post_id );
351 $post_title = $post_data['affiliate_name'];
352 $post_name = $post_data['permalink'] ?? $lasso_lite_url->slug ?? '';
353 $surl_redirect = trim( $post_data['surl_redirect'] ?? '' );
354 $buy_btn_text = $post_data['buy_btn_text'] ?? $lasso_lite_url->display->primary_button_text;
355 $open_new_tab = $post_data['open_new_tab'] ?? $lasso_lite_url->open_new_tab;
356 $enable_nofollow = $post_data['enable_nofollow'] ?? $lasso_lite_url->enable_nofollow;
357 $enable_sponsored = $post_data['enable_sponsored'] ?? $lasso_lite_url->enable_sponsored;
358 $show_disclosure = $post_data['show_disclosure'] ?? $lasso_lite_url->show_disclosure;
359 $show_price = $post_data['show_price'] ?? $lasso_lite_url->display->show_price;
360 $price = $post_data['price'] ?? $lasso_lite_url->price;
361 $thumbnail = $post_data['thumbnail'] ?? Constant::DEFAULT_THUMBNAIL;
362 $description = $post_data['description'] ?? $lasso_lite_url->description;
363 $term = isset( $post_data['categories'] ) && is_array( $post_data['categories'] ) ? $post_data['categories'] : array();
364 $badge_text = $post_data['badge_text'] ?? '';
365 $term = array_map(
366 function( $val ) {
367 $term_id = is_numeric( $val ) ? intval( $val ) : 0;
368 $term_id = get_term_by( 'name', $val, Constant::LASSO_CATEGORY )->term_id ?? $term_id;
369 // ? Support category name is number and different existed term ids.
370 $term_id = $term_id && term_exists( $term_id ) ? $term_id : 0;
371
372 if ( 0 === $term_id && ! empty( $val ) ) { // ? add new category
373 $result = wp_insert_term( $val, Constant::LASSO_CATEGORY );
374 $term_id = ( ! is_wp_error( $result ) ) ? $result['term_id'] : 0;
375 }
376
377 return $term_id;
378 },
379 $term
380 );
381
382 $affiliate_homepage = Helper::get_base_domain( $surl_redirect );
383 $is_opportunity = 1;
384 $product_id = Amazon_Api::get_product_id_by_url( $surl_redirect );
385 $product_type = Amazon_Api::is_amazon_url( $surl_redirect ) ? Amazon_Api::PRODUCT_TYPE : '';
386
387 if ( Amazon_Api::PRODUCT_TYPE === $product_type ) {
388 $product = $lasso_amazon_api->get_amazon_product_from_db( $product_id );
389 if ( ! $product ) {
390 $product = $lasso_amazon_api->fetch_product_info( $product_id, true, false, $surl_redirect );
391 }
392
393 $old_redirect_url = get_post_meta( $post_id, Meta_Enum::SURL_REDIRECT, true );
394 $old_thumbnail = get_post_meta( $post_id, Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, true );
395 $update_title = 'Amazon' === $post_title;
396 $update_thumbnail = Constant::DEFAULT_THUMBNAIL === $thumbnail;
397 if ( Amazon_Api::is_amazon_url( $old_redirect_url ) ) {
398 $old_amazon_id = Amazon_Api::get_product_id_by_url( $old_redirect_url );
399 $update_title = $product_id !== $old_amazon_id || $update_title;
400 $update_thumbnail = strpos( $old_thumbnail, 'media-amazon.' ) !== false || Constant::DEFAULT_THUMBNAIL === $thumbnail;
401 } else {
402 $update_title = true;
403 }
404
405 $post_title = $update_title ? ( $product['default_product_name'] ?? $post_title ) : $post_title;
406 $thumbnail = $update_thumbnail ? ( $product['default_image'] ?? $thumbnail ) : $thumbnail;
407 }
408
409 $lasso_lite_post = array(
410 'post_title' => $post_title,
411 'post_type' => SIMPLE_URLS_SLUG,
412 'post_name' => $post_name,
413 'post_content' => '',
414 'post_status' => 'publish',
415 'meta_input' => array(
416 Meta_Enum::SURL_REDIRECT => $surl_redirect,
417 Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL => $thumbnail,
418 Meta_Enum::OPEN_NEW_TAB => $open_new_tab,
419 Meta_Enum::ENABLE_NOFOLLOW => $enable_nofollow,
420 Meta_Enum::BUY_BTN_TEXT => $buy_btn_text,
421 Meta_Enum::DESCRIPTION => $description,
422 Meta_Enum::SHOW_PRICE => $show_price,
423 Meta_Enum::PRICE => $price,
424 Meta_Enum::ENABLE_SPONSORED => $enable_sponsored,
425 Meta_Enum::SHOW_DISCLOSURE => $show_disclosure,
426 Meta_Enum::BADGE_TEXT => $badge_text,
427 ),
428 );
429
430 if ( $is_update ) {
431 // ? Check duplicate slug
432 $duplicate_post = Helper::the_slug_exists( $post_name, $post_id );
433
434 if ( $duplicate_post ) {
435 $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.';
436 }
437 // ? END
438
439 $lasso_lite_post['ID'] = $post_id;
440 wp_update_post( $lasso_lite_post );
441 clean_post_cache( $post_id );
442 } else {
443 $post_id = wp_insert_post( $lasso_lite_post, true );
444 }
445
446 if ( ! is_wp_error( $post_id ) && $post_id > 0 ) {
447 // ? update categories
448 wp_set_object_terms( $post_id, $term, Constant::LASSO_CATEGORY );
449 $lasso_db->update_url_details( $post_id, $surl_redirect, $affiliate_homepage, $is_opportunity, $product_id, $product_type );
450 // ? update thumbnail
451 if ( $thumbnail_id > 0 ) {
452 set_post_thumbnail( $post_id, $thumbnail_id );
453 $image_url = wp_get_attachment_url( $thumbnail_id );
454 update_post_meta( $post_id, Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, $image_url );
455 } else {
456 delete_post_thumbnail( $post_id );
457 }
458 $lasso_lite_url = self::get_lasso_url( $post_id );
459 }
460
461 if ( $is_ajax_request ) {
462 $get_display_html = $post['get_display_html'] ?? false;
463 $display_html = '';
464 if ( 'true' === $get_display_html ) {
465 $shortcode = '[lasso id="' . $lasso_lite_url->id . '" rel="' . $lasso_lite_url->slug . '"]';
466 $display_html = do_shortcode( $shortcode );
467 }
468
469 wp_send_json_success(
470 array(
471 'success' => 1,
472 'warning' => $warning,
473 'post' => $lasso_lite_url,
474 'display_html' => $display_html,
475 )
476 );
477 }
478
479 return $post_id;
480 }
481
482 /**
483 * Get edit url of post
484 *
485 * @param int $post_id The post id.
486 * @return string Edit url of the post id.
487 */
488 public static function affiliate_edit_link( $post_id = 0 ) {
489 $query = array(
490 'post_type' => SIMPLE_URLS_SLUG,
491 'page' => self::$edit_details_page,
492 'post_id' => $post_id,
493 );
494
495 return add_query_arg(
496 $query,
497 admin_url( 'edit.php' )
498 );
499 }
500
501 /**
502 * Get amazon product if by Lasso post id
503 *
504 * @param int $lasso_id Lasso post id.
505 */
506 public static function get_amazon_id( $lasso_id ) {
507 $lasso_db = new Lasso_DB();
508 $lasso_post_details = $lasso_db->get_url_details( $lasso_id );
509 $details_product_id = $lasso_post_details->product_id ?? '';
510
511 $amazon_product_id = get_post_meta( $lasso_id, 'amazon_product_id', true );
512
513 return '' === $details_product_id ? $amazon_product_id : $details_product_id;
514 }
515 }
516