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

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