PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 149
Lasso Lite – Affiliate Link Manager & Product Displays v149
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 149, at classes/class-import.php

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