| 1 |
<?php |
| 2 |
/** |
| 3 |
* Declare class Affiliate_Link |
| 4 |
* |
| 5 |
* @package Enum |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace LassoLite\Classes; |
| 9 |
|
| 10 |
use LassoLite\Admin\Constant; |
| 11 |
use LassoLite\Classes\Helper; |
| 12 |
use LassoLite\Classes\Lasso_DB; |
| 13 |
use LassoLite\Classes\Meta_Enum; |
| 14 |
use LassoLite\Classes\Setting; |
| 15 |
|
| 16 |
/** |
| 17 |
* Affiliate_Link |
| 18 |
*/ |
| 19 |
class Affiliate_Link { |
| 20 |
const DEFAULT_AMAZON_NAME = 'Amazon'; |
| 21 |
const DEFAULT_TITLE = 'Add a Link Title'; |
| 22 |
const ADD_NEW_LINK_RESPONSE_STATUS = 'lasso_add_new_link_response_status'; |
| 23 |
/** |
| 24 |
* Edit detail page |
| 25 |
* |
| 26 |
* @var string $edit_details_page |
| 27 |
*/ |
| 28 |
public static $edit_details_page = 'surl-url-details'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Get Lasso post detail |
| 32 |
* |
| 33 |
* @param int $post_id Lasso post id. |
| 34 |
* @param bool $is_detail Is detail page. Default to false. |
| 35 |
*/ |
| 36 |
public static function get_lasso_url( $post_id, $is_detail = false ) { |
| 37 |
$post_id = intval( $post_id ); |
| 38 |
$post = get_post( $post_id ); |
| 39 |
$post_type = get_post_type( $post ); |
| 40 |
$post_status = get_post_status( $post ); |
| 41 |
$lasso_lite_settings = Setting::get_settings(); |
| 42 |
|
| 43 |
// ? default data |
| 44 |
$edit_link = ''; |
| 45 |
$link_type = ''; |
| 46 |
$name = $is_detail ? '' : 'The Link Title Goes Here'; |
| 47 |
$slug = ''; |
| 48 |
$guid = ''; |
| 49 |
$permalink = '#'; |
| 50 |
$public_link = '#'; |
| 51 |
$image_src = Constant::DEFAULT_THUMBNAIL; |
| 52 |
$image_src_default = 1; |
| 53 |
$thumbnail_id = ''; |
| 54 |
$target_url = ''; |
| 55 |
$open_new_tab = true; |
| 56 |
$enable_nofollow = true; |
| 57 |
$enable_sponsored = true; |
| 58 |
$description = ''; |
| 59 |
$price = ''; |
| 60 |
$display_last_updated = ''; |
| 61 |
$display_primary_button_text_default = $lasso_lite_settings['primary_button_text']; |
| 62 |
$show_disclosure = $lasso_lite_settings['show_disclosure'] ?? true; |
| 63 |
$disclosure_text = $lasso_lite_settings['disclosure_text'] ?? ''; |
| 64 |
$badge_text = ''; |
| 65 |
$is_amazon_page = false; |
| 66 |
$display_primary_button_text = $display_primary_button_text_default; |
| 67 |
$display_show_price = $lasso_lite_settings['show_price'] ?? true; |
| 68 |
|
| 69 |
// ? real data |
| 70 |
if ( $post_id > 0 && SIMPLE_URLS_SLUG === $post_type && 'publish' === $post_status && $post ) { |
| 71 |
$name = $post->post_title; |
| 72 |
$target_url = get_post_meta( $post_id, '_surl_redirect', true ); |
| 73 |
$slug = $post->post_name; |
| 74 |
$permalink = get_permalink( $post_id ); |
| 75 |
$image_src = get_post_meta( $post_id, Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, true ); |
| 76 |
$display_primary_button_text = get_post_meta( $post_id, Meta_Enum::BUY_BTN_TEXT, true ); |
| 77 |
$open_new_tab = get_post_meta( $post_id, Meta_Enum::OPEN_NEW_TAB, true ); |
| 78 |
$enable_nofollow = get_post_meta( $post_id, Meta_Enum::ENABLE_NOFOLLOW, true ); |
| 79 |
$enable_sponsored = get_post_meta( $post_id, Meta_Enum::ENABLE_SPONSORED, true ); |
| 80 |
$thumbnail_id = get_post_meta( $post_id, Meta_Enum::THUMBNAIL_ID, true ); |
| 81 |
$description = get_post_meta( $post_id, Meta_Enum::DESCRIPTION, true ); |
| 82 |
$description = str_replace( '<p></p>', '', $description ); |
| 83 |
$price = get_post_meta( $post_id, Meta_Enum::PRICE, true ); |
| 84 |
$display_show_price = get_post_meta( $post_id, Meta_Enum::SHOW_PRICE, true ); |
| 85 |
$badge_text = get_post_meta( $post_id, Meta_Enum::BADGE_TEXT, true ); |
| 86 |
$show_disclosure = get_post_meta( $post_id, Meta_Enum::SHOW_DISCLOSURE, true ); |
| 87 |
$edit_link = self::affiliate_edit_link( $post_id ); |
| 88 |
$is_amazon_page = Amazon_Api::is_amazon_url( $target_url ); |
| 89 |
|
| 90 |
if ( '' === $image_src ) { |
| 91 |
$image_src = Constant::DEFAULT_THUMBNAIL; |
| 92 |
} |
| 93 |
|
| 94 |
if ( '' === $display_primary_button_text ) { |
| 95 |
$display_primary_button_text = $display_primary_button_text_default; |
| 96 |
} |
| 97 |
|
| 98 |
if ( '' === $open_new_tab ) { |
| 99 |
$open_new_tab = true; |
| 100 |
} |
| 101 |
|
| 102 |
if ( '' === $enable_nofollow ) { |
| 103 |
$enable_nofollow = true; |
| 104 |
} |
| 105 |
|
| 106 |
if ( '' === $display_show_price ) { |
| 107 |
$display_show_price = true; |
| 108 |
} |
| 109 |
|
| 110 |
if ( '' === $show_disclosure ) { |
| 111 |
$show_disclosure = $lasso_lite_settings['show_disclosure'] ?? true; |
| 112 |
} |
| 113 |
|
| 114 |
if ( $is_amazon_page ) { |
| 115 |
$product_id = Amazon_Api::get_product_id_by_url( $target_url ); |
| 116 |
if ( $product_id ) { |
| 117 |
$lasso_amazon_api = new Amazon_Api(); |
| 118 |
$product = $lasso_amazon_api->get_amazon_product_from_db( $product_id ); |
| 119 |
|
| 120 |
if ( $product ) { |
| 121 |
$price = $product['latest_price']; |
| 122 |
$display_last_updated = gmdate( 'm/d/Y h:i a T', strtotime( $product['last_updated'] ) ); |
| 123 |
if ( Constant::DEFAULT_THUMBNAIL === $image_src ) { |
| 124 |
$image_src = $product['default_image']; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
$target_url = Amazon_Api::get_amazon_product_url( $target_url ); |
| 130 |
$public_link = $target_url; |
| 131 |
} else { |
| 132 |
$public_link = $permalink; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
$url_detail_checkbox_open_new_tab = $open_new_tab ? 'checked' : ''; |
| 137 |
$url_detail_checkbox_enable_nofollow = $enable_nofollow ? 'checked' : ''; |
| 138 |
$url_detail_checkbox_show_price = $display_show_price ? 'checked' : ''; |
| 139 |
$url_detail_checkbox_enable_sponsored = $enable_sponsored ? 'checked' : ''; |
| 140 |
$url_detail_checkbox_show_disclosure = $show_disclosure ? 'checked' : ''; |
| 141 |
|
| 142 |
$rel = $enable_nofollow ? 'nofollow' : ''; |
| 143 |
$rel = $open_new_tab ? trim( $rel . ' noopener' ) : $rel; |
| 144 |
$rel = $enable_sponsored ? trim( $rel . ' sponsored' ) : $rel; |
| 145 |
|
| 146 |
$html_attribute_rel = 'rel="' . $rel . '"'; |
| 147 |
$html_attribute_target = $open_new_tab ? '_blank' : '_self'; |
| 148 |
|
| 149 |
$category = wp_get_post_terms( $post_id, Constant::LASSO_CATEGORY, array( 'fields' => 'ids' ) ); |
| 150 |
$category = is_array( $category ) ? $category : array(); |
| 151 |
|
| 152 |
$lasso_lite_url = (object) array( |
| 153 |
'id' => $post_id, |
| 154 |
'edit_link' => $edit_link, |
| 155 |
'link_type' => $link_type, |
| 156 |
'name' => trim( $name ), |
| 157 |
'slug' => $slug, |
| 158 |
'guid' => $guid, |
| 159 |
'permalink' => $permalink, |
| 160 |
'public_link' => Amazon_Api::get_amazon_product_url( $public_link ), |
| 161 |
'image_src' => trim( $image_src ), |
| 162 |
'image_src_default' => $image_src_default, |
| 163 |
'thumbnail_id' => $thumbnail_id, |
| 164 |
'target_url' => $target_url, |
| 165 |
'open_new_tab' => $open_new_tab, |
| 166 |
'enable_nofollow' => $enable_nofollow, |
| 167 |
'enable_sponsored' => $enable_sponsored, |
| 168 |
'description' => $description, |
| 169 |
'category' => $category, |
| 170 |
'show_disclosure' => $show_disclosure, |
| 171 |
'is_amazon_page' => $is_amazon_page, |
| 172 |
'price' => $price, |
| 173 |
'display' => (object) array( |
| 174 |
'primary_button_text' => $display_primary_button_text, |
| 175 |
'primary_button_text_default' => $display_primary_button_text_default, // phpcs:ignore: use for placeholder |
| 176 |
'theme' => Enum::THEME_CACTUS, |
| 177 |
'show_price' => $display_show_price, |
| 178 |
'badge_text' => $badge_text, |
| 179 |
'disclosure_text' => $disclosure_text, |
| 180 |
'last_updated' => $display_last_updated, |
| 181 |
), |
| 182 |
'url_detail_checkbox' => (object) array( |
| 183 |
'open_new_tab' => $url_detail_checkbox_open_new_tab, |
| 184 |
'enable_nofollow' => $url_detail_checkbox_enable_nofollow, |
| 185 |
'enable_sponsored' => $url_detail_checkbox_enable_sponsored, |
| 186 |
'show_disclosure' => $url_detail_checkbox_show_disclosure, |
| 187 |
'show_price' => $url_detail_checkbox_show_price, |
| 188 |
), |
| 189 |
'html_attribute' => (object) array( |
| 190 |
'rel' => $html_attribute_rel, |
| 191 |
'target' => $html_attribute_target, |
| 192 |
), |
| 193 |
); |
| 194 |
|
| 195 |
return $lasso_lite_url; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Add a new Lasso link |
| 200 |
* |
| 201 |
* @param string $link Link. Default to empty. |
| 202 |
* @return void|string |
| 203 |
*/ |
| 204 |
public function add_a_new_link( $link = '' ) { |
| 205 |
$post = Helper::POST(); |
| 206 |
$link = trim( $link ?? '' ); |
| 207 |
$link = esc_url_raw( $link ); |
| 208 |
$url = trim( $link != '' ? $link : ( $post['link'] ?? '' ) ); // phpcs:ignore |
| 209 |
$url = esc_url_raw( $url ); |
| 210 |
|
| 211 |
$is_ajax_request = wp_doing_ajax() && '' === $link; |
| 212 |
|
| 213 |
if ( '' === $url ) { |
| 214 |
if ( $is_ajax_request ) { |
| 215 |
wp_send_json_error( 'No data to save.' ); |
| 216 |
} else { |
| 217 |
return 'No data to save.'; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
$lasso_amazon_api = new Amazon_Api(); |
| 222 |
|
| 223 |
list( $get_final_url, $page_title ) = Helper::get_redirect_final_target( $url, true, true ); |
| 224 |
|
| 225 |
$url = Helper::add_https( $url ); |
| 226 |
$url = Helper::format_url_before_requesting( $url ); |
| 227 |
$url = Amazon_Api::get_redirect_url( $url ); |
| 228 |
$is_amazon_link = Amazon_Api::is_amazon_url( $url ); |
| 229 |
$get_final_url = $url; |
| 230 |
$permalink = Helper::get_title_by_url( $url ); |
| 231 |
$title = $is_amazon_link ? self::DEFAULT_AMAZON_NAME : $permalink; |
| 232 |
$image = Constant::DEFAULT_THUMBNAIL; |
| 233 |
|
| 234 |
$is_amazon_short_link = Amazon_Api::is_amazon_shortened_url( $url ); |
| 235 |
if ( $is_amazon_link && $is_amazon_short_link ) { |
| 236 |
$title = self::DEFAULT_AMAZON_NAME; |
| 237 |
} |
| 238 |
|
| 239 |
// ? Check whether product is existing |
| 240 |
$lasso_post_id = self::get_lasso_lite_post_id_by_url( $url ); |
| 241 |
|
| 242 |
if ( $lasso_post_id > 0 ) { |
| 243 |
wp_update_post( |
| 244 |
array( |
| 245 |
'ID' => $lasso_post_id, |
| 246 |
'post_status' => 'publish', |
| 247 |
) |
| 248 |
); |
| 249 |
if ( $is_ajax_request ) { |
| 250 |
wp_send_json_success( |
| 251 |
array( |
| 252 |
'success' => true, |
| 253 |
'is_duplicate' => true, |
| 254 |
'post_id' => $lasso_post_id, |
| 255 |
) |
| 256 |
); |
| 257 |
} else { |
| 258 |
return $lasso_post_id; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
$url = Amazon_Api::get_amazon_product_url( $url, true, false ); |
| 263 |
$product_id = Amazon_Api::get_product_id_by_url( $get_final_url ); |
| 264 |
|
| 265 |
if ( $is_amazon_link && $product_id ) { |
| 266 |
$product = $lasso_amazon_api->get_amazon_product_from_db( $product_id ); |
| 267 |
|
| 268 |
if ( $product ) { |
| 269 |
$lasso_amazon_api->update_amazon_product_in_db( |
| 270 |
array( |
| 271 |
'product_id' => $product['amazon_id'], |
| 272 |
'title' => $product['default_product_name'], |
| 273 |
'price' => $product['latest_price'], |
| 274 |
'default_url' => $product['base_url'], |
| 275 |
'url' => $url, |
| 276 |
'image' => $product['default_image'], |
| 277 |
'quantity' => '0' === $product['out_of_stock'] ? 200 : 0, // Deferred: BLS for out-of-stock (product decision — see epic #496). |
| 278 |
'is_manual' => $product['is_manual'], |
| 279 |
'is_prime' => $product['is_prime'], |
| 280 |
'features' => $product['features'], |
| 281 |
'currency' => $product['currency'], |
| 282 |
'savings_amount' => $product['savings_amount'], |
| 283 |
'savings_percent' => $product['savings_percent'], |
| 284 |
'savings_basis' => $product['savings_basis'], |
| 285 |
) |
| 286 |
); |
| 287 |
} |
| 288 |
|
| 289 |
if ( ! $product ) { |
| 290 |
$product_info = $lasso_amazon_api->fetch_product_info( $product_id, true, false, $get_final_url ); |
| 291 |
if ( ! empty( $product_info ) ) { |
| 292 |
$product = $product_info['product']; |
| 293 |
|
| 294 |
if ( 'NotFound' === $product_info['error_code'] ) { |
| 295 |
$res['status_code'] = 404; |
| 296 |
$product['default_product_name'] = self::DEFAULT_AMAZON_NAME; |
| 297 |
$product['default_image'] = Constant::DEFAULT_THUMBNAIL; |
| 298 |
$product['monetized_url'] = $url; |
| 299 |
} else { |
| 300 |
$product['default_product_name'] = $product['title']; |
| 301 |
$product['default_image'] = $product['image']; |
| 302 |
$product['monetized_url'] = $product['url']; |
| 303 |
} |
| 304 |
|
| 305 |
$title = $product['default_product_name']; |
| 306 |
$image = $product['default_image']; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
$url = Amazon_Api::get_amazon_product_url( $get_final_url, true, false ); |
| 311 |
$title = $product['default_product_name'] ?? $title; |
| 312 |
$image = $product['default_image'] ?? $image; |
| 313 |
} |
| 314 |
|
| 315 |
if ( ! $title ) { |
| 316 |
$title = Helper::get_title_by_url( $url ); |
| 317 |
} |
| 318 |
|
| 319 |
$affiliate_link = array( |
| 320 |
'is_amazon' => $is_amazon_link, |
| 321 |
'affiliate_name' => $title, |
| 322 |
'surl_redirect' => trim( $url ), |
| 323 |
'affiliate_desc' => '', |
| 324 |
'permalink' => sanitize_title( $title ), |
| 325 |
'is_opportunity' => 1, |
| 326 |
'buy_btn_text' => '', |
| 327 |
'second_btn_text' => '', |
| 328 |
'price' => '', |
| 329 |
'badge_text' => '', |
| 330 |
'second_btn_url' => '', |
| 331 |
'thumbnail' => $image, |
| 332 |
'description' => '', |
| 333 |
); |
| 334 |
|
| 335 |
$data['settings'] = $affiliate_link; |
| 336 |
$should_complete_onboarding = ( 0 === intval( SURL::total() ) ); |
| 337 |
$post_id = $this->save_lasso_url( $data ); |
| 338 |
$is_first_url = self::is_first_link(); |
| 339 |
if ( $should_complete_onboarding && ! is_wp_error( $post_id ) && intval( $post_id ) > 0 ) { |
| 340 |
Helper::mark_onboarding_welcome_complete(); |
| 341 |
} |
| 342 |
|
| 343 |
if ( '' !== $link ) { |
| 344 |
return $post_id; |
| 345 |
} |
| 346 |
|
| 347 |
wp_send_json_success( |
| 348 |
array( |
| 349 |
'success' => true, |
| 350 |
'url' => $url, |
| 351 |
'title' => $title, |
| 352 |
'post_id' => $post_id, |
| 353 |
'is_first' => $is_first_url, |
| 354 |
) |
| 355 |
); |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Save Lasso data into DB |
| 360 |
* |
| 361 |
* @param array $data Lasso data. Default to null. |
| 362 |
* @param bool $is_ajax Is request ajax. Default to false. |
| 363 |
*/ |
| 364 |
public function save_lasso_url( $data = null, $is_ajax = false ) { |
| 365 |
$lasso_db = new Lasso_DB(); |
| 366 |
$lasso_amazon_api = new Amazon_Api(); |
| 367 |
|
| 368 |
$warning = ''; |
| 369 |
$is_ajax_request = wp_doing_ajax() && ( '' === $data || null === $data ); |
| 370 |
$is_ajax_request = $is_ajax_request || $is_ajax; |
| 371 |
$post = is_array( $data ) ? $data : $_POST; // phpcs:ignore |
| 372 |
|
| 373 |
$post = wp_unslash( $post ); // phpcs:ignore |
| 374 |
$post_id = intval( $post['post_id'] ?? 0 ); |
| 375 |
$is_update = $post_id > 0; |
| 376 |
$is_new = ! $is_update; |
| 377 |
$is_change_primary_link = $post['is_change_primary_link'] ?? false; |
| 378 |
$is_change_primary_link = Helper::cast_to_boolean( $is_change_primary_link ); |
| 379 |
$is_change_primary_link = true === $is_change_primary_link; |
| 380 |
|
| 381 |
$thumbnail_id = $post['thumbnail_id'] ?? 0; |
| 382 |
$post_data = $post['settings'] ?? array(); |
| 383 |
|
| 384 |
if ( empty( $post_data ) || ! is_array( $post_data ) ) { |
| 385 |
$error_message = 'No data to save.'; |
| 386 |
if ( $is_ajax_request ) { |
| 387 |
wp_send_json_error( $error_message ); |
| 388 |
} else { |
| 389 |
return $error_message; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
if ( empty( trim( $post_data['surl_redirect'] ?? '' ) ) || empty( trim( $post_data['affiliate_name'] ?? '' ) ) ) { |
| 394 |
$error_message = 'Name and Target URL are required.'; |
| 395 |
if ( $is_ajax_request ) { |
| 396 |
wp_send_json_error( $error_message ); |
| 397 |
} else { |
| 398 |
return $error_message; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
$lasso_lite_url = self::get_lasso_url( $post_id ); |
| 403 |
$post_title = $post_data['affiliate_name']; |
| 404 |
$post_name = $post_data['permalink'] ?? $lasso_lite_url->slug ?? ''; |
| 405 |
$surl_redirect = trim( $post_data['surl_redirect'] ?? '' ); |
| 406 |
$buy_btn_text = $post_data['buy_btn_text'] ?? $lasso_lite_url->display->primary_button_text; |
| 407 |
$open_new_tab = $post_data['open_new_tab'] ?? $lasso_lite_url->open_new_tab; |
| 408 |
$enable_nofollow = $post_data['enable_nofollow'] ?? $lasso_lite_url->enable_nofollow; |
| 409 |
$enable_sponsored = $post_data['enable_sponsored'] ?? $lasso_lite_url->enable_sponsored; |
| 410 |
$show_disclosure = $post_data['show_disclosure'] ?? $lasso_lite_url->show_disclosure; |
| 411 |
$show_price = $post_data['show_price'] ?? $lasso_lite_url->display->show_price; |
| 412 |
$price = $post_data['price'] ?? $lasso_lite_url->price; |
| 413 |
$thumbnail = $post_data['thumbnail'] ?? Constant::DEFAULT_THUMBNAIL; |
| 414 |
$description = $post_data['description'] ?? $lasso_lite_url->description; |
| 415 |
$term = isset( $post_data['categories'] ) && is_array( $post_data['categories'] ) ? $post_data['categories'] : array(); |
| 416 |
$badge_text = $post_data['badge_text'] ?? ''; |
| 417 |
$term = array_map( |
| 418 |
function ( $val ) { |
| 419 |
$term_id = is_numeric( $val ) ? intval( $val ) : 0; |
| 420 |
$term_id = get_term_by( 'name', $val, Constant::LASSO_CATEGORY )->term_id ?? $term_id; |
| 421 |
// ? Support category name is number and different existed term ids. |
| 422 |
$term_id = $term_id && term_exists( $term_id ) ? $term_id : 0; |
| 423 |
|
| 424 |
if ( 0 === $term_id && ! empty( $val ) ) { // ? add new category |
| 425 |
$result = wp_insert_term( $val, Constant::LASSO_CATEGORY ); |
| 426 |
$term_id = ( ! is_wp_error( $result ) ) ? $result['term_id'] : 0; |
| 427 |
} |
| 428 |
|
| 429 |
return $term_id; |
| 430 |
}, |
| 431 |
$term |
| 432 |
); |
| 433 |
|
| 434 |
$get_final_url = Helper::get_redirect_final_target( $surl_redirect, true, $is_new ? true : false ); // ? If adding the new link, we set param "get_page_title" is true to get result from cache. |
| 435 |
$get_final_url = is_array( $get_final_url ) ? $get_final_url[0] : $get_final_url; |
| 436 |
$get_final_url = Amazon_Api::format_amazon_url( $get_final_url ); |
| 437 |
$surl_redirect = $get_final_url; |
| 438 |
|
| 439 |
// ? Check whether product is existing |
| 440 |
$lasso_post_id = self::get_lasso_lite_post_id_by_url( $surl_redirect ); |
| 441 |
if ( $lasso_post_id > 0 && $is_update && $is_change_primary_link ) { |
| 442 |
wp_update_post( |
| 443 |
array( |
| 444 |
'ID' => $lasso_post_id, |
| 445 |
'post_status' => 'publish', |
| 446 |
) |
| 447 |
); |
| 448 |
if ( $is_ajax_request ) { |
| 449 |
wp_send_json_success( |
| 450 |
array( |
| 451 |
'success' => true, |
| 452 |
'is_duplicate' => true, |
| 453 |
'post_id' => $lasso_post_id, |
| 454 |
) |
| 455 |
); |
| 456 |
} else { |
| 457 |
return $lasso_post_id; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
$affiliate_homepage = Helper::get_base_domain( $surl_redirect ); |
| 462 |
$is_opportunity = 1; |
| 463 |
$product_id = Amazon_Api::get_product_id_by_url( $get_final_url ); |
| 464 |
$product_type = Amazon_Api::is_amazon_url( $get_final_url ) ? Amazon_Api::PRODUCT_TYPE : ''; |
| 465 |
|
| 466 |
if ( Amazon_Api::PRODUCT_TYPE === $product_type ) { |
| 467 |
$product = $lasso_amazon_api->get_amazon_product_from_db( $product_id ); |
| 468 |
if ( ! $product ) { |
| 469 |
$product = $lasso_amazon_api->fetch_product_info( $product_id, true, false, $get_final_url ); |
| 470 |
$product = $product['product'] ?? array(); |
| 471 |
} |
| 472 |
|
| 473 |
$is_importing = $data['is_importing'] ?? false; |
| 474 |
$old_redirect_url = get_post_meta( $post_id, Meta_Enum::SURL_REDIRECT, true ); |
| 475 |
$old_thumbnail = get_post_meta( $post_id, Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, true ); |
| 476 |
$update_title = self::DEFAULT_AMAZON_NAME === $post_title; |
| 477 |
$update_thumbnail = Constant::DEFAULT_THUMBNAIL === $thumbnail || $is_importing; |
| 478 |
$use_defined_affiliate_name = $data['use_defined_affiliate_name'] ?? false; |
| 479 |
|
| 480 |
if ( Amazon_Api::is_amazon_url( $get_final_url ) ) { |
| 481 |
$old_amazon_id = Amazon_Api::get_product_id_by_url( $old_redirect_url ); |
| 482 |
$update_title = ( $product_id !== $old_amazon_id || $update_title ) && ! $use_defined_affiliate_name; |
| 483 |
$update_thumbnail = strpos( $old_thumbnail, 'media-amazon.' ) !== false || Constant::DEFAULT_THUMBNAIL === $thumbnail; |
| 484 |
} else { |
| 485 |
$update_title = $use_defined_affiliate_name ? false : true; |
| 486 |
} |
| 487 |
|
| 488 |
$post_title = $update_title ? ( $product['default_product_name'] ?? $product['title'] ?? $post_title ) : $post_title; |
| 489 |
$thumbnail = $update_thumbnail ? ( $product['default_image'] ?? ( $product['image'] ?? $thumbnail ) ) : $thumbnail; |
| 490 |
$price = ! $price ? ( $product['price'] ?? $price ) : $price; |
| 491 |
} |
| 492 |
|
| 493 |
$lasso_lite_post = array( |
| 494 |
'post_title' => self::save_scalar_string( $post_title ), |
| 495 |
'post_type' => SIMPLE_URLS_SLUG, |
| 496 |
'post_name' => $post_name, |
| 497 |
'post_content' => '', |
| 498 |
'post_status' => 'publish', |
| 499 |
'meta_input' => array( |
| 500 |
Meta_Enum::SURL_REDIRECT => $surl_redirect, |
| 501 |
Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL => self::save_scalar_url( $thumbnail ), |
| 502 |
Meta_Enum::OPEN_NEW_TAB => $open_new_tab, |
| 503 |
Meta_Enum::ENABLE_NOFOLLOW => $enable_nofollow, |
| 504 |
Meta_Enum::BUY_BTN_TEXT => self::save_scalar_string( $buy_btn_text ), |
| 505 |
Meta_Enum::DESCRIPTION => self::save_scalar_html( $description ), |
| 506 |
Meta_Enum::SHOW_PRICE => $show_price, |
| 507 |
Meta_Enum::PRICE => $price, |
| 508 |
Meta_Enum::ENABLE_SPONSORED => $enable_sponsored, |
| 509 |
Meta_Enum::SHOW_DISCLOSURE => $show_disclosure, |
| 510 |
Meta_Enum::BADGE_TEXT => self::save_scalar_string( $badge_text ), |
| 511 |
), |
| 512 |
); |
| 513 |
|
| 514 |
if ( $is_update ) { |
| 515 |
// ? Check duplicate slug |
| 516 |
$duplicate_post = Helper::the_slug_exists( $post_name, $post_id ); |
| 517 |
|
| 518 |
if ( $duplicate_post ) { |
| 519 |
$warning = 'Permalink <a href="' . get_edit_post_link( $duplicate_post['ID'] ) . '" class="white underline" target="_blank"><strong>' . $duplicate_post['post_name'] . '</strong></a> is being used by <strong>' . $duplicate_post['post_type'] . '</strong>. We updated the permalink to avoid a conflict.'; |
| 520 |
} |
| 521 |
// ? END |
| 522 |
|
| 523 |
$lasso_lite_post['ID'] = $post_id; |
| 524 |
wp_update_post( $lasso_lite_post ); |
| 525 |
clean_post_cache( $post_id ); |
| 526 |
} else { |
| 527 |
$post_id = wp_insert_post( $lasso_lite_post, true ); |
| 528 |
} |
| 529 |
|
| 530 |
if ( ! is_wp_error( $post_id ) && $post_id > 0 ) { |
| 531 |
// ? update categories |
| 532 |
wp_set_object_terms( $post_id, $term, Constant::LASSO_CATEGORY ); |
| 533 |
$lasso_db->update_url_details( $post_id, $surl_redirect, $affiliate_homepage, $is_opportunity, $product_id, $product_type ); |
| 534 |
// ? update thumbnail |
| 535 |
if ( $thumbnail_id > 0 ) { |
| 536 |
set_post_thumbnail( $post_id, $thumbnail_id ); |
| 537 |
$image_url = esc_url_raw( wp_get_attachment_url( $thumbnail_id ) ); |
| 538 |
update_post_meta( $post_id, Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, $image_url ); |
| 539 |
} else { |
| 540 |
delete_post_thumbnail( $post_id ); |
| 541 |
} |
| 542 |
$lasso_lite_url = self::get_lasso_url( $post_id ); |
| 543 |
} |
| 544 |
|
| 545 |
if ( $is_ajax_request ) { |
| 546 |
$get_display_html = $post['get_display_html'] ?? false; |
| 547 |
$display_html = ''; |
| 548 |
if ( 'true' === $get_display_html ) { |
| 549 |
$shortcode = '[lasso id="' . $lasso_lite_url->id . '" rel="' . $lasso_lite_url->slug . '"]'; |
| 550 |
$display_html = do_shortcode( $shortcode ); |
| 551 |
} |
| 552 |
|
| 553 |
wp_send_json_success( |
| 554 |
array( |
| 555 |
'success' => 1, |
| 556 |
'warning' => $warning, |
| 557 |
'post' => $lasso_lite_url, |
| 558 |
'display_html' => $display_html, |
| 559 |
) |
| 560 |
); |
| 561 |
} |
| 562 |
|
| 563 |
return $post_id; |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* Get edit url of post |
| 568 |
* |
| 569 |
* @param int $post_id The post id. |
| 570 |
* @return string Edit url of the post id. |
| 571 |
*/ |
| 572 |
public static function affiliate_edit_link( $post_id = 0 ) { |
| 573 |
$query = array( |
| 574 |
'post_type' => SIMPLE_URLS_SLUG, |
| 575 |
'page' => self::$edit_details_page, |
| 576 |
'post_id' => $post_id, |
| 577 |
); |
| 578 |
|
| 579 |
return add_query_arg( |
| 580 |
$query, |
| 581 |
admin_url( 'edit.php' ) |
| 582 |
); |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* Get amazon product if by Lasso post id |
| 587 |
* |
| 588 |
* @param int $lasso_id Lasso post id. |
| 589 |
*/ |
| 590 |
public static function get_amazon_id( $lasso_id ) { |
| 591 |
$lasso_db = new Lasso_DB(); |
| 592 |
$lasso_post_details = $lasso_db->get_url_details( $lasso_id ); |
| 593 |
$details_product_id = $lasso_post_details->product_id ?? ''; |
| 594 |
|
| 595 |
$amazon_product_id = get_post_meta( $lasso_id, 'amazon_product_id', true ); |
| 596 |
|
| 597 |
return '' === $details_product_id ? $amazon_product_id : $details_product_id; |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Get Lasso Lite post id by url |
| 602 |
* |
| 603 |
* @param string $url URL. |
| 604 |
* @param int $default_id Default id. Default to 0. |
| 605 |
*/ |
| 606 |
public static function get_lasso_lite_post_id_by_url( $url, $default_id = 0 ) { |
| 607 |
$lasso_lite_db = new Lasso_DB(); |
| 608 |
|
| 609 |
// ? Get post id from url |
| 610 |
$lasso_lite_id = $default_id; |
| 611 |
$url = trim( $url, '/' ); |
| 612 |
$url = str_replace( '&', '&', $url ); |
| 613 |
$parse = wp_parse_url( $url ); |
| 614 |
$path = ''; |
| 615 |
|
| 616 |
if ( strpos( $url, home_url() ) !== false && isset( $parse['path'] ) ) { |
| 617 |
$path = $parse['path']; |
| 618 |
$path = trim( $path, '/' ); |
| 619 |
|
| 620 |
$explode = explode( '/', $path ); |
| 621 |
$slug = end( $explode ); |
| 622 |
$lasso = get_page_by_path( $slug, OBJECT, Constant::LASSO_POST_TYPE ); |
| 623 |
|
| 624 |
if ( $lasso ) { |
| 625 |
$lasso_lite_id = Constant::LASSO_POST_TYPE === get_post_type( $lasso->ID ) ? $lasso->ID : $lasso_lite_id; |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
if ( 0 === $lasso_lite_id ) { |
| 630 |
$lasso_post = $lasso_lite_db->get_lasso_by_uri( $path ); // ? by redirect url |
| 631 |
$lasso_lite_id = $lasso_post->ID ?? $lasso_lite_id; |
| 632 |
} |
| 633 |
|
| 634 |
if ( 0 === $lasso_lite_id ) { |
| 635 |
$detail = $lasso_lite_db->get_url_details_by_url( $url ); // ? by redirect url |
| 636 |
$lasso_lite_id = $detail->lasso_id ?? $lasso_lite_id; |
| 637 |
} |
| 638 |
|
| 639 |
if ( 0 === $lasso_lite_id && Amazon_Api::is_amazon_url( $url ) ) { |
| 640 |
$amazon_product_id = Amazon_Api::get_product_id_by_url( $url ); |
| 641 |
$product_exist = $lasso_lite_db->check_amazon_product_exist( $amazon_product_id ); |
| 642 |
$lasso_post_id = $lasso_lite_db->get_lasso_id_by_product_id_and_type( $product_exist['amazon_id'] ?? '' ); |
| 643 |
$lasso_lite_id = $lasso_post_id ? $lasso_post_id : $default_id; |
| 644 |
} |
| 645 |
|
| 646 |
if ( 0 === $lasso_lite_id ) { |
| 647 |
$lasso_lite_id = Lasso_DB::get_lasso_lite_id_by_url_from_post_meta( $url ); |
| 648 |
} |
| 649 |
|
| 650 |
$tmp_url = Helper::get_final_url_from_url_param( $url ); |
| 651 |
$tmp_url = $tmp_url ? $tmp_url : $url; |
| 652 |
$k = Amazon_Api::is_amazon_search_page( $tmp_url ); |
| 653 |
if ( 0 === $lasso_lite_id && $k ) { |
| 654 |
$k = rawurlencode( $k ); |
| 655 |
$detail = $lasso_lite_db->get_url_details_by_url( '%/s?k=' . $k . '%' ); // ? by redirect url |
| 656 |
$lasso_lite_id = $detail->lasso_id ?? $lasso_lite_id; |
| 657 |
} |
| 658 |
|
| 659 |
return intval( $lasso_lite_id ); |
| 660 |
} |
| 661 |
|
| 662 |
/** |
| 663 |
* Save-path text field: scalar only; arrays/objects become empty string. |
| 664 |
* |
| 665 |
* @param mixed $value Raw value. |
| 666 |
* @return string |
| 667 |
*/ |
| 668 |
private static function save_scalar_string( $value ) { |
| 669 |
if ( null === $value || is_bool( $value ) || ! is_scalar( $value ) ) { |
| 670 |
return ''; |
| 671 |
} |
| 672 |
|
| 673 |
return sanitize_text_field( wp_unslash( (string) $value ) ); |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* Save-path URL field: scalar only; arrays/objects become empty string. |
| 678 |
* |
| 679 |
* @param mixed $value Raw value. |
| 680 |
* @return string |
| 681 |
*/ |
| 682 |
private static function save_scalar_url( $value ) { |
| 683 |
if ( null === $value || is_bool( $value ) || ! is_scalar( $value ) ) { |
| 684 |
return ''; |
| 685 |
} |
| 686 |
|
| 687 |
return esc_url_raw( wp_unslash( (string) $value ) ); |
| 688 |
} |
| 689 |
|
| 690 |
/** |
| 691 |
* Save-path HTML field: scalar only; arrays/objects become empty string. |
| 692 |
* |
| 693 |
* @param mixed $value Raw value. |
| 694 |
* @return string |
| 695 |
*/ |
| 696 |
private static function save_scalar_html( $value ) { |
| 697 |
if ( null === $value || is_bool( $value ) || ! is_scalar( $value ) ) { |
| 698 |
return ''; |
| 699 |
} |
| 700 |
|
| 701 |
return wp_kses_post( wp_unslash( (string) $value ) ); |
| 702 |
} |
| 703 |
|
| 704 |
/** |
| 705 |
* Check if the first link |
| 706 |
*/ |
| 707 |
public function is_first_link() { |
| 708 |
$lasso_lite_db = new Lasso_DB(); |
| 709 |
if ( intval( $lasso_lite_db->get_total_url_detail()->total_url ) === 1 ) { |
| 710 |
return true; |
| 711 |
} |
| 712 |
return false; |
| 713 |
} |
| 714 |
} |
| 715 |
|