PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 152
Lasso Lite – Affiliate Link Manager & Product Displays v152
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 116 All 56 releases
simple-urls / classes / class-import.php

class-import.php in Lasso Lite – Affiliate Link Manager & Product Displays 152, at classes/class-import.php

857 lines 28.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Declare class Import
4 *
5 * @package Import
6 */
7
8 namespace LassoLite\Classes;
9
10 use LassoLite\Admin\Constant;
11 use LassoLite\Classes\Amazon_Api as Lasso_Amazon_Api;
12 use LassoLite\Classes\Affiliate_Link as Lasso_Affiliate_Link;
13 use LassoLite\Classes\Cache_Per_Process as Lasso_Cache_Per_Process;
14 use LassoLite\Classes\Setting_Enum;
15 use LassoLite\Classes\Helper as Lasso_Helper;
16 use LassoLite\Classes\Lasso_DB;
17 use LassoLite\Classes\Setting as Lasso_Setting;
18
19 use LassoLite\Models\Model;
20 use LassoLite\Models\Revert;
21
22 use stdClass;
23
24 /**
25 * Import
26 */
27 class Import {
28 const OBJECT_KEY = 'lasso_import';
29 const PRETTY_LINK_CATEGORY_SLUG = 'pretty-link-category';
30 const PRETTY_LINK_TAG_SLUG = 'pretty-link-tag';
31 const THIRSTY_LINK_CATEGORY_SLUG = 'thirstylink-category';
32 const AFFILIATE_URLS_CATEGORY_SLUG = 'affiliate_url_category';
33
34 /**
35 * Revert a single post from Lasso to original plugin
36 *
37 * @param int $import_id Post id.
38 * @param string $import_source Type of post (AAWP, earnist,...).
39 * @param string $post_type Type of post (aawp, earnist,...). Default to empty.
40 */
41 public function process_single_link_revert( $import_id, $import_source, $post_type = '' ) {
42 $lasso_db = new Lasso_DB();
43
44 if ( '' === $import_id || '' === $import_source ) {
45 return false;
46 }
47
48 // Bulk revert passes plugin slugs from the revert table; UI passes display labels.
49 if ( isset( Setting_Enum::SUPPORT_IMPORT_PLUGINS[ $import_source ] ) ) {
50 $import_source = Setting_Enum::SUPPORT_IMPORT_PLUGINS[ $import_source ];
51 }
52
53 if ( 'AAWP' === $import_source && 'aawp_list' === $post_type ) {
54 $aawp_list = $lasso_db->get_aawp_list( $import_id );
55 $aawp_amazon_ids = $aawp_list->product_asins ?? '';
56 $aawp_amazon_ids = '' !== $aawp_amazon_ids ? explode( ',', $aawp_amazon_ids ) : array();
57
58 foreach ( $aawp_amazon_ids as $amazon_id ) {
59 $url_detail = $lasso_db->get_url_details_by_product_id( $amazon_id, Amazon_Api::PRODUCT_TYPE );
60 if ( $url_detail ) {
61 $import_id = $url_detail->lasso_id;
62 wp_update_post(
63 array(
64 'ID' => $import_id,
65 'post_status' => 'trash',
66 )
67 );
68 $status = $lasso_db->process_revert( $import_id, false );
69 }
70 }
71 } elseif ( isset( $import_source ) && in_array( $import_source, array( 'AAWP', 'AmaLinks Pro', 'EasyAzon' ), true ) ) {
72 wp_update_post(
73 array(
74 'ID' => $import_id,
75 'post_status' => 'trash',
76 )
77 );
78 $status = $lasso_db->process_revert( $import_id, false );
79 } else {
80 $status = $lasso_db->process_revert( $import_id );
81 delete_post_meta( $import_id, 'affiliate_link_type' );
82 }
83
84 // ? clear cache after reverting
85 Lasso_Cache_Per_Process::get_instance()->un_set( self::OBJECT_KEY . '_' . $import_id );
86 Lasso_Cache_Per_Process::get_instance()->un_set( 'wp_post_' . $import_id );
87
88 return $status;
89 }
90
91 /**
92 * Import a single post into Lasso
93 *
94 * @param int $import_id Post id.
95 * @param string $post_type Type of post (earnist, thirstylink, affiliate_url, aawp,...).
96 * @param string $post_title Title.
97 * @param string $import_permalink Import permalink.
98 */
99 public function process_single_link_data( $import_id, $post_type, $post_title = '', $import_permalink = '' ) {
100 if ( 'pretty-link' === $post_type ) {
101 $import_data = $this->get_pretty_link_data( $import_id );
102 } elseif ( 'thirstylink' === $post_type ) {
103 $import_data = $this->get_thirsty_affiliates_data( $import_id );
104 } elseif ( 'earnist' === $post_type ) {
105 $import_data = $this->get_earnist_data( $import_id );
106 } elseif ( 'affiliate_url' === $post_type ) {
107 $import_data = $this->get_affiliate_urls_data( $import_id );
108 } elseif ( 'aawp' === $post_type ) {
109 $import_data = $this->get_aawp_data( $import_id );
110 } elseif ( 'aawp_list' === $post_type ) {
111 return $this->import_aawp_list_data( $import_id );
112 } elseif ( 'easyazon' === $post_type ) {
113 $import_data = $this->get_easyazon_data( $import_id );
114 } elseif ( 'amalinkspro' === $post_type ) {
115 $import_data = $this->get_amalinkspro_data( $import_id, $post_title, $import_permalink );
116 } elseif ( 'easy_affiliate_link' === $post_type ) {
117 $import_data = $this->get_easy_affiliate_link_data( $import_id, $post_title, $import_permalink );
118 } elseif ( Setting_Enum::LASSO_PRO_SLUG === $post_type ) {
119 $import_data = $this->get_lasso_pro_data( $import_id );
120 }
121 $import_data['post_type'] = $post_type;
122
123 // ? Make a Lasso Link
124 list($status, $import_data) = $this->import_into_lasso( $import_data, $post_type );
125
126 // ? Return if status is false
127 if ( ! $status ) {
128 return array( $status, $import_data );
129 }
130
131 $lasso_url = Lasso_Affiliate_Link::get_lasso_url( $import_data['post']->ID );
132 $lasso_id = $lasso_url->id ?? 0;
133
134 // ? Flip all old links to Lasso
135 if ( $lasso_id > 0 ) {
136 $import_data['new_url'] = $lasso_url->public_link ?? '';
137 } elseif ( Constant::LASSO_AMAZON_PRODUCT_TYPE !== $lasso_url->link_type ) {
138 $import_post_name = $import_data['post']->post_name ?? '';
139 if ( strpos( $import_post_name, '/' ) ) {
140 $start_pos = strpos( $import_post_name, '/' ) + 1;
141 } else {
142 $start_pos = 0;
143 }
144
145 $post_name = substr( $import_post_name, $start_pos );
146 $import_data['new_url'] = get_home_url() . '/' . $post_name . '/';
147 } else {
148 $import_data['new_url'] = $import_data['amazon_product']['product']['url'] ?? '';
149 }
150
151 return array( $status, $import_data );
152 }
153
154 /**
155 * Import post data from other plugins into Lasso
156 *
157 * @param array $import_data Array contains post data.
158 * @param string $post_type Type of post (post, page, surl, simple_url,...).
159 */
160 private function import_into_lasso( $import_data, $post_type ) {
161 $lasso_db = new Lasso_DB();
162 $lasso_affiliate_link = new Lasso_Affiliate_Link();
163
164 $lasso_settings = Setting::get_settings();
165
166 // ? Make sure slug is correct
167 $post_id = $import_data['post']->ID ?? '';
168 $slug = $import_data['post']->post_name ?? '';
169 $slug = Lasso_Helper::lasso_unique_post_name( $post_id, $slug );
170 $title = $import_data['post']->post_title ?? '';
171 if ( 'pretty-link' === $post_type ) {
172 $slug = $import_data['pretty_link_data']->slug ?? $slug;
173 $post_id = $import_data['pretty_link_data']->link_cpt_id ?? $post_id;
174 $title = $import_data['pretty_link_data']->name ?? $title;
175
176 $slug = trim( $slug, '/' );
177 if ( false !== strpos( $slug, '/' ) ) {
178 $tmp = explode( '/', $slug );
179 $slug = end( $tmp );
180 }
181 }
182
183 // ? prepare data for saving Lasso post
184 $default_btn_txt = Lasso_Setting::get_setting( 'primary_button_text', 'Buy Now' );
185 $affiliate_link = array(
186 'affiliate_name' => $title,
187 'affiliate_url' => $import_data['redirect_url'] ?? '',
188 'post_name' => $slug,
189 'price' => $import_data['price'] ?? '',
190 'description' => $import_data['description'] ?? '',
191 'buy_btn_text' => $import_data['button_text'] ?? $default_btn_txt,
192
193 'open_new_tab' => $import_data['open_new_tab'] ?? $lasso_settings['open_new_tab'] ?? 1,
194 'enable_nofollow' => $import_data['enable_nofollow'] ?? $lasso_settings['enable_nofollow'] ?? 1,
195 'enable_sponsored' => $import_data['enable_sponsored'] ?? $lasso_settings['enable_sponsored'] ?? 1,
196 'show_disclosure' => $import_data['show_disclosure'] ?? $lasso_settings['show_disclosure'] ?? 1,
197 'is_opportunity' => $import_data['is_opportunity'] ?? 1,
198 'badge_text' => $import_data['badge_text'] ?? '',
199 'thumbnail' => $import_data['thumbnail'] ?? '',
200 );
201
202 $cat_ids = $import_data['cat_ids'] ?? array();
203 if ( count( $cat_ids ) > 0 ) {
204 $affiliate_link['categories'] = $cat_ids;
205 }
206
207 $data['post_id'] = $post_id;
208 $data['settings'] = $affiliate_link;
209 $data['thumbnail_id'] = $import_data['thumbnail_id'][0] ?? '';
210 $data['old_uri'] = $import_data['old_uri'] ?? '';
211 $data['is_importing'] = true;
212
213 // ? Use defined "affiliate name" as Lasso Post's name
214 if ( in_array( $post_type, array( Setting_Enum::EASY_AFFILIATE_LINK_SLUG, Setting_Enum::LASSO_PRO_SLUG ), true ) ) {
215 $data['use_defined_affiliate_name'] = 1;
216 }
217
218 // ? Flip post type and log import
219 $import_data['post'] = $import_data['post'] ?? new stdClass();
220 $import_data['post'] = is_object( $import_data['post'] ) ? $import_data['post'] : new stdClass();
221
222 // ? Check affiliate_name and affiliate_url and old_uri before function save_lasso_url
223 if ( empty( $affiliate_link['affiliate_name'] ) || empty( $affiliate_link['affiliate_url'] ) || empty( $data['old_uri'] ) ) {
224 return array( false, $import_data );
225 }
226
227 // ? Fix key in Lite
228 $data['settings']['surl_redirect'] = $data['settings']['affiliate_url'] ?? '';
229
230 $post_id = $lasso_affiliate_link->save_lasso_url( $data );
231 $import_data['post']->ID = $post_id;
232
233 $post = get_post( $post_id );
234 $post_name = $post->post_name ?? '';
235 $slug = ! empty( $post_name ) && empty( $slug ) ? $post_name : $slug;
236
237 $old_uri = $import_data['old_uri'] ?? '';
238
239 $status = $lasso_db->process_import( $post_id, $slug, $old_uri, $post_type );
240
241 // ? clear cache after importing
242 if ( $status ) {
243 Lasso_Cache_Per_Process::get_instance()->un_set( self::OBJECT_KEY . '_' . $post_id );
244 Lasso_Cache_Per_Process::get_instance()->un_set( 'wp_post_' . $post_id );
245 }
246
247 return array( $status, $import_data );
248 }
249
250 /**
251 * Get post data of Pretty Link plugin
252 *
253 * @param int $import_id Post id.
254 */
255 private function get_pretty_link_data( $import_id ) {
256 $lasso_db = new Lasso_DB();
257
258 $post = get_post( $import_id );
259 $pretty_link_data = $lasso_db->get_pretty_link_by_id( $import_id );
260
261 if ( ! $pretty_link_data ) {
262 return array();
263 }
264
265 $post->post_name = $pretty_link_data->slug;
266 $redirect_url = $pretty_link_data->url;
267 $cat_names = $this->get_post_category_names( $import_id, self::PRETTY_LINK_CATEGORY_SLUG );
268 $tag_names = $this->get_post_category_names( $import_id, self::PRETTY_LINK_TAG_SLUG );
269 $final_category_names = array_merge( $cat_names, $tag_names );
270 $final_category_names = array_unique( $final_category_names );
271
272 $data = array(
273 'post' => $post,
274 'pretty_link_data' => $pretty_link_data,
275 'redirect_url' => $redirect_url,
276 'cat_ids' => $final_category_names,
277 'old_uri' => $this->get_import_permalink( $import_id, $post->post_name, $post->post_type ),
278 'thumbnail_id' => array( '' ),
279 'description' => $pretty_link_data->description,
280 'enable_nofollow' => $pretty_link_data->nofollow ?? '0',
281 'enable_sponsored' => $pretty_link_data->sponsored ?? '0',
282 );
283
284 return $data;
285 }
286
287 /**
288 * Get post data of Thirsty Affiliate plugin
289 *
290 * @param int $import_id Post id.
291 */
292 private function get_thirsty_affiliates_data( $import_id ) {
293 $post = get_post( $import_id );
294 $redirect_url = get_post_meta( $import_id, '_ta_destination_url', true );
295 $rel_tag = get_post_meta( $import_id, '_ta_rel_tags', true ); // ? get rel tag for Thirsty Affiliates
296 $cat_ids = $this->get_post_category_names( $import_id, self::THIRSTY_LINK_CATEGORY_SLUG );
297
298 $data = array(
299 'post' => $post,
300 'redirect_url' => $redirect_url,
301 'cat_ids' => $cat_ids,
302 'old_uri' => $this->get_import_permalink( $import_id, $post->post_name, $post->post_type ),
303 'thumbnail_id' => get_post_meta( $import_id, '_ta_image_ids', true ),
304 'description' => '',
305 'enable_sponsored' => ( false === strpos( strtolower( $rel_tag ), 'sponsored' ) ) ? Lasso_Helper::cast_to_boolean( Setting::get_setting( 'enable_sponsored' ) ) : 1,
306 );
307
308 return $data;
309 }
310
311 /**
312 * Get post data of Earnist plugin
313 *
314 * @param int $import_id Post id.
315 */
316 private function get_earnist_data( $import_id ) {
317 $post = get_post( $import_id );
318 $redirect_url = get_post_meta( $import_id, '_earn_url', true );
319 $cat_ids = $this->get_categories_id_of_post( $import_id );
320 $earnist_description = get_post_meta( $import_id, '_earn_description', true );
321 $earnist_price = get_post_meta( $import_id, '_earn_price', true );
322 $earnist_button_text = get_post_meta( $import_id, '_earn_button_text', true );
323
324 $earnist_image_id = '';
325 $earnist_image_url = '';
326 $post_thumbnail = get_post_thumbnail_id( $import_id );
327 if ( ! empty( $post_thumbnail ) ) {
328 $earnist_image_id = $post_thumbnail;
329 } else {
330 $earnist_image_url = get_post_meta( $import_id, '_earn_image_url', true );
331 if ( 0 === strpos( $earnist_image_url, '/wp-content/' ) ) {
332 $earnist_image_url = get_home_url() . $earnist_image_url;
333 }
334 }
335
336 $data = array(
337 'post' => $post,
338 'redirect_url' => $redirect_url,
339 'cat_ids' => $cat_ids,
340 'old_uri' => $this->get_import_permalink( $import_id, $post->post_name, $post->post_type ),
341 'thumbnail_id' => array( $earnist_image_id ),
342 'thumbnail_url' => $earnist_image_url,
343 'description' => $earnist_description,
344 'price' => $earnist_price,
345 'button_text' => $earnist_button_text,
346 );
347
348 return $data;
349 }
350
351 /**
352 * Get post data of Affiliate Url plugin
353 *
354 * @param int $import_id Post id.
355 */
356 private function get_affiliate_urls_data( $import_id ) {
357 $post = get_post( $import_id );
358 $cat_ids = $this->get_post_category_names( $import_id, self::AFFILIATE_URLS_CATEGORY_SLUG );
359 $redirect_url = get_post_meta( $import_id, '_affiliate_url_redirect', true );
360
361 $data = array(
362 'post' => $post,
363 'redirect_url' => $redirect_url,
364 'cat_ids' => $cat_ids,
365 'old_uri' => $this->get_import_permalink( $import_id, $post->post_name, $post->post_type ),
366 'thumbnail_id' => array( get_post_thumbnail_id( $import_id ) ),
367 'description' => get_the_excerpt( $import_id ),
368 );
369
370 return $data;
371 }
372
373 /**
374 * Import amazon product from other plugins into Lasso
375 *
376 * @param string $amazon_id Amazon product id.
377 */
378 private function import_aawp_amazon_product_into_lasso( $amazon_id ) {
379 $lasso_db = new Lasso_DB();
380 $lasso_amazon_api = new Lasso_Amazon_Api();
381
382 $product_data = array();
383
384 $product = $lasso_db->get_aawp_product( $amazon_id );
385 if ( $product ) {
386 $base_url = $lasso_amazon_api->get_amazon_product_url( $product->url, false );
387 $product_data = array(
388 'product_id' => $product->asin,
389 'title' => $product->title,
390 'price' => Lasso_Amazon_Api::format_price( $product->price, $product->currency ),
391 'default_url' => $base_url,
392 'url' => $product->url,
393 'image' => $product->image_url,
394 'is_prime' => $product->is_prime,
395 'currency' => $product->currency,
396 'features' => $product->features,
397 'savings_amount' => $product->savings,
398 'savings_percent' => $product->savings_percentage,
399 'savings_basis' => $product->savings_basis,
400 'rating' => $product->rating,
401 'reviews' => $product->reviews,
402 'is_manual' => 1,
403 );
404
405 $lasso_amazon_api->update_amazon_product_in_db( $product_data, $product->date_updated );
406 }
407
408 return $product_data;
409 }
410
411 /**
412 * Get post data of Aawp plugin
413 *
414 * @param int $import_id Post id.
415 */
416 private function get_aawp_data( $import_id ) {
417 $lasso_amazon_api = new Lasso_Amazon_Api();
418
419 $product_data = $lasso_amazon_api->get_amazon_product_from_db( $import_id );
420 if ( ! $product_data ) {
421 $product_data = $this->import_aawp_amazon_product_into_lasso( $import_id );
422 } else {
423 $product_data['url'] = $product_data['monetized_url'] ?? '';
424 $product_data['title'] = $product_data['default_product_name'] ?? '';
425 }
426
427 $redirect_url = $product_data['url'] ?? '';
428 $post_title = $product_data['title'] ?? '';
429 $post = new stdClass();
430 $post->post_title = $post_title;
431
432 $aawp_options = get_option( 'aawp_output' );
433 $button_text = $aawp_options['button_text'] ?? '';
434
435 $data = array(
436 'post' => $post,
437 'redirect_url' => $redirect_url,
438 'old_uri' => $import_id,
439 'description' => '',
440 'enable_sponsored' => 1,
441 'button_text' => $button_text,
442 );
443
444 return $data;
445 }
446
447 /**
448 * Get Lasso Pro data to import.
449 *
450 * @param int $import_id Lasso Pro post id.
451 * @return array
452 */
453 private function get_lasso_pro_data( $import_id ) {
454 $target_url = self::get_lasso_pro_target_url( $import_id );
455 $amazon_id = Lasso_Amazon_Api::get_product_id_by_url( $target_url );
456 $terms = get_the_terms( $import_id, 'lasso-cat' );
457 $cat_ids = $terms && ! is_wp_error( $terms ) ? wp_list_pluck( $terms, 'name' ) : null;
458
459 $lasso_amazon_api = new Lasso_Amazon_Api();
460 if ( $amazon_id && ! $lasso_amazon_api->get_amazon_product_from_db( $amazon_id ) ) {
461 $this->import_lasso_pro_amazon_product_into_lasso_lite( $amazon_id );
462 }
463
464 $data = array(
465 'post' => get_post( $import_id ),
466 'redirect_url' => $target_url,
467 'cat_ids' => $cat_ids,
468 'old_uri' => $target_url,
469 'button_text' => get_post_meta( $import_id, 'buy_btn_text', true ),
470 'price' => get_post_meta( $import_id, 'price', true ),
471 'thumbnail_id' => get_post_meta( $import_id, 'lasso_thumbnail_id', true ),
472 'thumbnail' => get_post_meta( $import_id, 'lasso_custom_thumbnail', true ),
473 'description' => get_post_meta( $import_id, 'affiliate_desc', true ),
474 'open_new_tab' => get_post_meta( $import_id, 'open_new_tab', true ),
475 'enable_nofollow' => get_post_meta( $import_id, 'enable_nofollow', true ),
476 'enable_sponsored' => get_post_meta( $import_id, 'enable_sponsored', true ),
477 'show_disclosure' => get_post_meta( $import_id, 'show_disclosure', true ),
478 'badge_text' => get_post_meta( $import_id, 'badge_text', true ),
479 );
480
481 return $data;
482 }
483
484 /**
485 * Import Lasso Pro amazon product data to Lite
486 *
487 * @param string $amazon_id Amazon Product id.
488 * @return array|bool|mixed|object|void|null
489 */
490 private function import_lasso_pro_amazon_product_into_lasso_lite( $amazon_id ) {
491 $lasso_db = new Lasso_DB();
492 $lasso_amazon_api = new Lasso_Amazon_Api();
493
494 $product = $lasso_db->get_lasso_pro_amazon_product( $amazon_id );
495 if ( $product ) {
496 $product_data = array(
497 'product_id' => $product->amazon_id,
498 'title' => $product->default_product_name,
499 'price' => $product->latest_price,
500 'default_url' => $product->base_url,
501 'url' => $product->url,
502 'image' => $product->image_url,
503 'is_prime' => $product->is_prime,
504 'currency' => $product->currency,
505 'features' => $product->features,
506 'savings_amount' => $product->savings_amount,
507 'savings_percent' => $product->savings_percent,
508 'savings_basis' => $product->savings_basis,
509 'quantity' => $product->quantity,
510 'rating' => $product->rating,
511 'reviews' => $product->reviews,
512 'is_manual' => 1,
513 );
514
515 $lasso_amazon_api->update_amazon_product_in_db( $product_data, $product->last_updated );
516 }
517
518 return $product;
519 }
520
521 /**
522 * Get post data of Aawp plugin
523 *
524 * @param int $import_id Post id.
525 */
526 private function import_aawp_list_data( $import_id ) {
527 $lasso_db = new Lasso_DB();
528 $lasso_amazon_api = new Lasso_Amazon_Api();
529
530 $list = $lasso_db->get_aawp_list( $import_id );
531 if ( ! $list ) {
532 return array( false, array() );
533 }
534
535 $cat_name = $list->keywords;
536 $cat = term_exists( $cat_name, Constant::LASSO_CATEGORY );
537 if ( null === $cat || 0 === $cat ) {
538 $cat = wp_insert_term( $cat_name, Constant::LASSO_CATEGORY );
539 }
540 $cat_id = (int) $cat['term_id'];
541
542 $asins = $list->product_asins;
543 $asins = explode( ',', $asins );
544 foreach ( $asins as $asin ) {
545 $product_data = $lasso_amazon_api->get_amazon_product_from_db( $asin );
546 if ( ! $product_data ) {
547 $this->import_aawp_amazon_product_into_lasso( $asin );
548 }
549
550 list($status, $import_data) = $this->process_single_link_data( $asin, 'aawp' );
551 $post_id = $import_data['post']->ID ?? 0;
552
553 // ? update categories
554 wp_set_object_terms( $post_id, array( $cat_id ), Constant::LASSO_CATEGORY );
555 }
556
557 return array( true, array() );
558 }
559
560 /**
561 * Get post data of EasyAzon plugin
562 *
563 * @param string $import_id Post id.
564 */
565 private function get_easyazon_data( $import_id ) {
566 $lasso_db = new Lasso_DB();
567 $lasso_amazon_api = new Lasso_Amazon_Api();
568
569 $product = $lasso_db->get_easyazon_product( $import_id );
570 $product_serial = $product->option_value ?? '';
571 $product = maybe_unserialize( $product_serial ); // phpcs:ignore
572
573 // ? insert amazon product data from easyazon into lasso
574 $db_product = $lasso_amazon_api->get_amazon_product_from_db( $product['identifier'] );
575 if ( ! $db_product ) {
576 $store_data = array(
577 'product_id' => $product['identifier'],
578 'title' => $product['title'],
579 'price' => $product['attributes']['ListPrice'] ?? $product['lowest_price_n'] ?? '',
580 'default_url' => $product['url'],
581 'url' => $product['url'],
582 'image' => $product['images'][4]['url'] ?? $product['images'][ count( $product['images'] ) - 1 ]['url'],
583 'quantity' => 200, // Deferred: BLS quantity for out-of-stock checks (product decision — see epic #496).
584 'is_manual' => 1,
585 );
586 $lasso_amazon_api->update_amazon_product_in_db( $store_data );
587 }
588
589 $redirect_url = $product['url'] ?? '';
590 $post_title = $product['title'] ?? '';
591 $post_title = $post_title ? $post_title : 'Amazon';
592 $post = new stdClass();
593 $post->post_title = $post_title;
594
595 $data = array(
596 'post' => $post,
597 'redirect_url' => $redirect_url,
598 'old_uri' => $import_id,
599 'description' => '',
600 );
601
602 return $data;
603 }
604
605 /**
606 * Get post data of AmaLinks Pro plugin
607 *
608 * @param string $import_id Post id.
609 * @param string $post_title Post title.
610 * @param string $import_permalink Import permalink.
611 */
612 private function get_amalinkspro_data( $import_id, $post_title, $import_permalink ) {
613 $post = new stdClass();
614 $post->post_title = $post_title;
615
616 if ( empty( $post->post_title ) ) {
617 $lasso_amazon = new Lasso_Amazon_Api();
618 $product = $lasso_amazon->get_amazon_product_from_db( $import_id );
619 $post->post_title = $product['default_product_name'] ?? $post->post_title;
620 if ( ! $product && empty( $post->post_title ) ) {
621 $result = $lasso_amazon->fetch_product_info( $import_id, true, false, $import_permalink );
622 $post->post_title = $result['product']['title'] ?? $post->post_title;
623 }
624 }
625
626 $data = array(
627 'post' => $post,
628 'redirect_url' => $import_permalink,
629 'old_uri' => $import_id,
630 'description' => '',
631 );
632
633 return $data;
634 }
635
636 /**
637 * Get post data of Easy Affiliate Links plugin
638 *
639 * @param string $import_id Post id.
640 * @param string $post_title Post title.
641 * @param string $import_permalink Import permalink.
642 */
643 public function get_easy_affiliate_link_data( $import_id, $post_title, $import_permalink ) {
644 $post = get_post( $import_id );
645 $redirect_url = get_post_meta( $import_id, 'eafl_url', true );
646 $terms = get_the_terms( $import_id, 'eafl_category' );
647 $cat_ids = $terms && ! is_wp_error( $terms ) ? wp_list_pluck( $terms, 'name' ) : null;
648
649 $default_settings = get_option( 'eafl_settings' );
650 $target = get_post_meta( $import_id, 'eafl_target', true );
651 $description = get_post_meta( $import_id, 'eafl_description', true );
652 if ( 'default' === $target ) {
653 $target = $default_settings['default_target'] ?? '_blank';
654 }
655 $nofollow = get_post_meta( $import_id, 'eafl_nofollow', true );
656 if ( 'default' === $nofollow ) {
657 $nofollow = $default_settings['default_nofollow'] ?? 'nofollow';
658 }
659 $sponsored = get_post_meta( $import_id, 'eafl_sponsored', true );
660
661 $data = array(
662 'post' => $post,
663 'redirect_url' => $redirect_url,
664 'cat_ids' => $cat_ids,
665 'old_uri' => get_permalink( $import_id ),
666 'description' => $description,
667 'open_new_tab' => '_blank' === $target ? 1 : 0,
668 'enable_nofollow' => 'nofollow' === $nofollow ? 1 : 0,
669 'enable_sponsored' => '1' === $sponsored ? 1 : 0,
670 );
671
672 return $data;
673 }
674
675 /**
676 * Get import permalink
677 *
678 * @param int $id Post id.
679 * @param string $post_name Post name.
680 * @param string $post_type Type of post (post, page, pretty-link, surl,...).
681 */
682 private function get_import_permalink( $id, $post_name, $post_type ) {
683 if ( 'pretty-link' === $post_type ) {
684 $prlipro = get_option( 'prlipro_options', array() );
685 if ( ! is_array( $prlipro ) ) {
686 $prlipro = array();
687 }
688
689 $home_url = get_home_url();
690 $base_slug_prefix = $prlipro['base_slug_prefix'] ?? false;
691
692 if ( $base_slug_prefix && '' !== $base_slug_prefix && strpos( $post_name, $base_slug_prefix ) === false ) {
693 $post_name = substr( $post_name, strpos( $post_name, '/' ) );
694 $import_permalink = $home_url . '/' . $base_slug_prefix . '/' . $post_name . '/';
695 } else {
696 $import_permalink = $home_url . '/' . $post_name . '/';
697 }
698 } else {
699 $import_permalink = get_the_permalink( $id );
700 }
701
702 return $import_permalink;
703 }
704
705 /**
706 * Get all categories of a Lasso post
707 *
708 * @param int $post_id Lasso post id.
709 */
710 public function get_categories_id_of_post( $post_id ) {
711 $lasso_db = new Lasso_DB();
712
713 $sql = '
714 SELECT `term_taxonomy_id`
715 FROM ' . $lasso_db->term_relationships . '
716 WHERE `object_id` = %d
717 ';
718 $prepare = Model::prepare( $sql, $post_id );
719 $result = Model::get_results( $prepare, ARRAY_A );
720
721 $cat_ids = array();
722
723 if ( count( $result ) > 0 ) {
724 foreach ( $result as $value ) {
725 $cat_ids[] = $value['term_taxonomy_id'];
726 }
727 }
728
729 return $cat_ids;
730 }
731
732 /**
733 * Check whether a post was imported into Lasso or not
734 *
735 * @param int $post_id Lasso post id.
736 */
737 public static function is_post_imported_into_lasso( $post_id ) {
738 $post_id = intval( $post_id );
739 $cache_key = self::OBJECT_KEY . '_' . $post_id;
740 $results = Lasso_Cache_Per_Process::get_instance()->get_cache( $cache_key, null );
741 if ( null !== $results ) {
742 return $results;
743 }
744
745 if ( 0 === $post_id ) {
746 Lasso_Cache_Per_Process::get_instance()->set_cache( $cache_key, false );
747 return false;
748 }
749
750 // ? Cache post by Lasso_Cache_Per_Process
751 $post = Lasso_Cache_Per_Process::get_instance()->get_cache( 'wp_post_' . $post_id );
752 if ( false === $post ) {
753 $post = get_post( $post_id );
754 Lasso_Cache_Per_Process::get_instance()->set_cache( 'wp_post_' . $post_id, $post );
755 }
756 $post_type = get_post_type( $post );
757 $post_status = get_post_status( $post );
758
759 if ( Constant::LASSO_POST_TYPE !== $post_type || 'publish' !== $post_status ) {
760 Lasso_Cache_Per_Process::get_instance()->set_cache( $cache_key, false );
761 return false;
762 }
763
764 $sql = '
765 select DISTINCT plugin
766 from ' . ( new Revert() )->get_table_name() . '
767 where lasso_id = ' . $post_id . '
768 ';
769 $results = Model::get_results( $sql );
770
771 if ( is_null( $results ) ) {
772 Lasso_Cache_Per_Process::get_instance()->set_cache( $cache_key, false );
773 return false;
774 }
775
776 $results = array_map(
777 function( $v ) {
778 return $v->plugin;
779 },
780 $results
781 );
782
783 if ( empty( $results ) ) {
784 Lasso_Cache_Per_Process::get_instance()->set_cache( $cache_key, false );
785 return false;
786 }
787
788 // ? Set cache to keep results loaded
789 Lasso_Cache_Per_Process::get_instance()->set_cache( $cache_key, $results );
790 return $results;
791 }
792
793 /**
794 * Get category names by post id
795 *
796 * @param int $post_id Post id.
797 * @param string $taxonomy Taxonomy.
798 * @return array
799 */
800 public function get_post_category_names( $post_id, $taxonomy ) {
801 $result = array();
802 $cat_ids = $this->get_categories_id_of_post( $post_id );
803
804 foreach ( $cat_ids as $key => $cat_id ) {
805 $term = self::get_term_by_taxonomy( $cat_id, $taxonomy );
806 $cat_name = $term->name ?? '';
807 if ( ! empty( $cat_name ) ) {
808 $result[ $key ] = $cat_name;
809 }
810 }
811
812 return $result;
813 }
814
815 /**
816 * Get term by taxonomy id and slug
817 *
818 * @param string $taxonomy_id Taxonomy id.
819 * @param string $taxonomy_slug Taxonomy slug.
820 */
821 public static function get_term_by_taxonomy( $taxonomy_id, $taxonomy_slug ) {
822 $sql = '
823 SELECT tt.term_id, name, slug, taxonomy
824 FROM ' . Model::get_wp_table_name( 'term_taxonomy' ) . ' AS tt
825 LEFT JOIN
826 ' . Model::get_wp_table_name( 'terms' ) . ' AS t
827 ON tt.term_id = t.term_id
828 WHERE
829 tt.term_taxonomy_id = %d
830 AND tt.taxonomy = %s
831 LIMIT 1
832 ';
833 $prepare = Model::prepare( $sql, $taxonomy_id, $taxonomy_slug );
834
835 return Model::get_row( $prepare );
836 }
837
838 /**
839 * Get Lasso Pro target url
840 *
841 * @param int $lasso_pro_id Lasso Pro post Id.
842 * @return mixed
843 */
844 public static function get_lasso_pro_target_url( $lasso_pro_id ) {
845 $sql = '
846 SELECT redirect_url
847 FROM ' . Model::get_wp_table_name( 'lasso_url_details' ) . '
848 WHERE lasso_id = %d
849 ';
850 $sql = Model::prepare( $sql, $lasso_pro_id );
851
852 $target_url = Model::get_var( $sql );
853
854 return $target_url;
855 }
856 }
857