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

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