| 1 |
<?php |
| 2 |
/** |
| 3 |
* Declare class DB |
| 4 |
* |
| 5 |
* @package DB |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace LassoLite\Classes; |
| 9 |
|
| 10 |
use LassoLite\Classes\Setting_Enum; |
| 11 |
|
| 12 |
use LassoLite\Admin\Constant; |
| 13 |
use LassoLite\Models\Url_Details; |
| 14 |
use LassoLite\Models\Model; |
| 15 |
use LassoLite\Models\Revert; |
| 16 |
|
| 17 |
/** |
| 18 |
* Lasso_DB |
| 19 |
*/ |
| 20 |
class Lasso_DB { |
| 21 |
/** |
| 22 |
* Construction of Lasso_DB |
| 23 |
*/ |
| 24 |
public function __construct() { |
| 25 |
global $wpdb; |
| 26 |
|
| 27 |
$this->dbname = $wpdb->dbname; |
| 28 |
$this->prefix = $wpdb->prefix; |
| 29 |
$this->current_domain = str_replace( 'https://', '', str_replace( 'http://', '', strtolower( get_site_url() ) ) ); |
| 30 |
|
| 31 |
// ? WP Tables |
| 32 |
$this->posts = $wpdb->posts; |
| 33 |
$this->postmeta = $wpdb->postmeta; |
| 34 |
$this->terms = $wpdb->terms; |
| 35 |
$this->term_taxonomy = $wpdb->term_taxonomy; |
| 36 |
$this->term_relationships = $wpdb->term_relationships; |
| 37 |
$this->options = $wpdb->options; |
| 38 |
|
| 39 |
// ? Lasso Tables |
| 40 |
$this->amazon_products = $wpdb->prefix . Constant::LASSO_AMAZON_PRODUCTS_DB; |
| 41 |
|
| 42 |
// ? Import Tables (external products) |
| 43 |
$this->pretty_links = $wpdb->prefix . 'prli_links'; |
| 44 |
$this->aawp = $wpdb->prefix . 'aawp_products'; |
| 45 |
$this->aawp_list = $wpdb->prefix . 'aawp_lists'; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get url detail |
| 50 |
* |
| 51 |
* @param int $lasso_id Lasso post id. |
| 52 |
*/ |
| 53 |
public function get_url_details( $lasso_id ) { |
| 54 |
$sql = ' |
| 55 |
SELECT * |
| 56 |
FROM ' . ( new Url_Details() )->get_table_name() . ' |
| 57 |
WHERE lasso_id = %s |
| 58 |
'; |
| 59 |
$sql = Url_Details::prepare( $sql, $lasso_id ); |
| 60 |
|
| 61 |
return $this->get_row( $sql ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Run query |
| 66 |
* |
| 67 |
* @param string $sql Sql query. |
| 68 |
*/ |
| 69 |
public function query( $sql ) { |
| 70 |
global $wpdb; |
| 71 |
|
| 72 |
$results = $wpdb->query( $sql ); // phpcs:ignore |
| 73 |
$this->log_error( $wpdb->last_error ); |
| 74 |
|
| 75 |
return $results; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get row |
| 80 |
* Get row from cache if existed |
| 81 |
* |
| 82 |
* @param string $sql Sql query. |
| 83 |
* @param string $output Type of results. |
| 84 |
* @param boolean $is_use_cache Is use cache. |
| 85 |
*/ |
| 86 |
public function get_row( $sql, $output = 'OBJECT', $is_use_cache = false ) { |
| 87 |
$results = false; |
| 88 |
$cache_string = md5( trim( (string) $sql ) . $output ); |
| 89 |
|
| 90 |
if ( $is_use_cache ) { |
| 91 |
$results = Cache_Per_Process::get_instance()->get_cache( $cache_string ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( false === $results ) { |
| 95 |
global $wpdb; |
| 96 |
|
| 97 |
$results = $wpdb->get_row( $sql, $output ); // phpcs:ignore |
| 98 |
$this->log_error( $wpdb->last_error ); |
| 99 |
|
| 100 |
if ( $is_use_cache ) { |
| 101 |
Cache_Per_Process::get_instance()->set_cache( $cache_string, $results ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return $results; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get var |
| 110 |
* Get var from cache if existed |
| 111 |
* |
| 112 |
* @param string $sql Sql query. |
| 113 |
* @param boolean $is_use_cache Is use cache. |
| 114 |
*/ |
| 115 |
public function get_var( $sql, $is_use_cache = false ) { |
| 116 |
$results = false; |
| 117 |
$cache_string = md5( trim( (string) $sql ) ); |
| 118 |
|
| 119 |
if ( $is_use_cache ) { |
| 120 |
$results = Cache_Per_Process::get_instance()->get_cache( $cache_string ); |
| 121 |
} |
| 122 |
|
| 123 |
if ( false === $results ) { |
| 124 |
global $wpdb; |
| 125 |
|
| 126 |
$results = $wpdb->get_var( $sql ); // phpcs:ignore |
| 127 |
$this->log_error( $wpdb->last_error ); |
| 128 |
|
| 129 |
if ( $is_use_cache ) { |
| 130 |
Cache_Per_Process::get_instance()->set_cache( $cache_string, $results ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return $results; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get col |
| 139 |
* Get col from cache if existed |
| 140 |
* |
| 141 |
* @param string $sql Sql query. |
| 142 |
* @param boolean $is_use_cache Is use cache. |
| 143 |
*/ |
| 144 |
public function get_col( $sql, $is_use_cache = false ) { |
| 145 |
$results = false; |
| 146 |
$cache_string = md5( trim( (string) $sql ) ); |
| 147 |
|
| 148 |
if ( $is_use_cache ) { |
| 149 |
$results = Cache_Per_Process::get_instance()->get_cache( $cache_string ); |
| 150 |
} |
| 151 |
|
| 152 |
if ( false === $results ) { |
| 153 |
global $wpdb; |
| 154 |
|
| 155 |
$results = $wpdb->get_col( $sql ); // phpcs:ignore |
| 156 |
$this->log_error( $wpdb->last_error ); |
| 157 |
|
| 158 |
if ( $is_use_cache ) { |
| 159 |
Cache_Per_Process::get_instance()->set_cache( $cache_string, $results ); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return $results; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Print error log message to log file |
| 168 |
* |
| 169 |
* @param string $error Error message. |
| 170 |
*/ |
| 171 |
public function log_error( $error ) { |
| 172 |
if ( ! empty( $error ) ) { |
| 173 |
if ( strpos( $error, 'Illegal mix of collations' ) !== false |
| 174 |
|| strpos( $error, 'Unknown column' ) !== false |
| 175 |
) { |
| 176 |
Update_DB::create_tables(); |
| 177 |
} |
| 178 |
|
| 179 |
// ? Add force write log for lasso_debug, to see what happen when Lasso call query. |
| 180 |
trigger_error( $error, E_USER_NOTICE ); // phpcs:ignore |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get lasso post id by product id and product_type |
| 186 |
* |
| 187 |
* @param string $product_id Product id. |
| 188 |
* @param string $product_type Product type. Default is amazon. |
| 189 |
*/ |
| 190 |
public function get_lasso_id_by_product_id_and_type( $product_id, $product_type = Amazon_Api::PRODUCT_TYPE ) { |
| 191 |
global $wpdb; |
| 192 |
|
| 193 |
if ( ! $product_id ) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
$sql = ' |
| 198 |
SELECT DISTINCT lud.lasso_id as post_id |
| 199 |
FROM ' . $this->posts . ' as wpp |
| 200 |
LEFT JOIN ' . ( new Url_Details() )->get_table_name() . ' as lud |
| 201 |
ON wpp.id = lud.lasso_id |
| 202 |
WHERE wpp.post_type = %s |
| 203 |
AND lud.product_id = %s |
| 204 |
AND lud.product_type = %s |
| 205 |
AND wpp.post_status = "publish" |
| 206 |
'; |
| 207 |
|
| 208 |
$prepare = $wpdb->prepare( $sql, Constant::LASSO_POST_TYPE, $product_id, $product_type ); // phpcs:ignore |
| 209 |
$post = $this->get_row( $prepare ); |
| 210 |
|
| 211 |
if ( $post && get_post( $post->post_id ) ) { |
| 212 |
return $post->post_id; |
| 213 |
} |
| 214 |
|
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Update data in url details table |
| 220 |
* |
| 221 |
* @param int $lasso_id Lasso post id. |
| 222 |
* @param string $redirect_url Redirect url. |
| 223 |
* @param string $base_domain Base domain. |
| 224 |
* @param int $is_opportunity Is Opportunity. |
| 225 |
* @param string $product_id Product id. Default to empty. |
| 226 |
* @param string $product_type Product type. Default to empty. |
| 227 |
*/ |
| 228 |
public function update_url_details( $lasso_id, $redirect_url, $base_domain, $is_opportunity, $product_id = '', $product_type = '' ) { |
| 229 |
$url_detail_model = new Url_Details(); |
| 230 |
$redirect_url = trim( $redirect_url ); |
| 231 |
$redirect_url = addcslashes( $redirect_url, "'" ); |
| 232 |
$sql = ' |
| 233 |
INSERT INTO ' . $url_detail_model->get_table_name() . ' (lasso_id, redirect_url, base_domain, is_opportunity, product_id, product_type) |
| 234 |
VALUES (%d, %s, %s, %d, %s, %s) |
| 235 |
ON DUPLICATE KEY |
| 236 |
UPDATE |
| 237 |
redirect_url = %s, |
| 238 |
base_domain = %s, |
| 239 |
is_opportunity = %d, |
| 240 |
product_id = %s, |
| 241 |
product_type = %s |
| 242 |
'; |
| 243 |
$prepare = Url_Details::prepare( |
| 244 |
$sql, |
| 245 |
// ? insert |
| 246 |
$lasso_id, |
| 247 |
$redirect_url, |
| 248 |
$base_domain, |
| 249 |
$is_opportunity, |
| 250 |
$product_id, |
| 251 |
$product_type, |
| 252 |
// ? on duplicate update |
| 253 |
$redirect_url, |
| 254 |
$base_domain, |
| 255 |
$is_opportunity, |
| 256 |
$product_id, |
| 257 |
$product_type |
| 258 |
); |
| 259 |
|
| 260 |
Url_Details::query( $prepare ); |
| 261 |
|
| 262 |
// ? Unset deprecated cache |
| 263 |
Cache_Per_Process::get_instance()->un_set( Amazon_Api::OBJECT_KEY . '_' . Amazon_Api::FUNCTION_NAME_GET_LASSO_ID_BY_PRODUCT_ID_AND_TYPE . '_' . $product_id . '_' . $product_type ); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Get importable url query |
| 268 |
* |
| 269 |
* @param bool $include_imported Include imported or not. Default to true. |
| 270 |
* @param string $search Search text. Default to empty. |
| 271 |
* @param string $group_by Group by column. Default to empty. |
| 272 |
* @param string $filter_plugin Plugin name. |
| 273 |
*/ |
| 274 |
public function get_importable_urls_query( $include_imported = true, $search = '', $group_by = '', $filter_plugin = null ) { |
| 275 |
$is_prlipro_installed = $this->is_pretty_links_pro_installed(); |
| 276 |
$is_aawp_installed = $this->is_aawp_installed(); |
| 277 |
|
| 278 |
if ( '' === $group_by ) { |
| 279 |
$group_by = 'BASE.import_source, BASE.id, BASE.post_type, BASE.post_name, BASE.post_title, BASE.check_status, BASE.check_disabled'; |
| 280 |
$select = '*'; |
| 281 |
$order_by = 'BASE.check_status, BASE.import_source, BASE.post_title'; |
| 282 |
} else { |
| 283 |
$select = $group_by; |
| 284 |
$order_by = $group_by; |
| 285 |
} |
| 286 |
|
| 287 |
if ( ! $filter_plugin ) { |
| 288 |
$where = ''; |
| 289 |
} else { |
| 290 |
$where = 'AND import_source = %s'; |
| 291 |
$where = Model::prepare( $where, $filter_plugin ); |
| 292 |
} |
| 293 |
|
| 294 |
$support_plugin = Setting_Enum::SUPPORT_IMPORT_PLUGINS; |
| 295 |
$support_plugin_flip = array_flip( $support_plugin ); |
| 296 |
$sql = ''; |
| 297 |
|
| 298 |
$revert_table = ( new Revert() )->get_table_name(); |
| 299 |
|
| 300 |
// ? SQL pre-processing |
| 301 |
if ( $is_prlipro_installed && $support_plugin[ Setting_Enum::PRETTY_LINK_SLUG ] === $filter_plugin ) { |
| 302 |
$prlipro_post_name = " |
| 303 |
CASE |
| 304 |
WHEN p.post_type = 'pretty-link' |
| 305 |
THEN CONVERT(pl.slug USING utf8) |
| 306 |
ELSE CONVERT(p.post_name USING utf8) |
| 307 |
END as post_name, |
| 308 |
"; |
| 309 |
$prlipro_join = ' |
| 310 |
LEFT JOIN |
| 311 |
' . $this->pretty_links . " as pl |
| 312 |
ON p.post_type = 'pretty-link' |
| 313 |
AND p.id = pl.link_cpt_id |
| 314 |
"; |
| 315 |
} else { |
| 316 |
$prlipro_post_name = 'CONVERT(p.post_name USING utf8) as post_name,'; |
| 317 |
$prlipro_join = ''; |
| 318 |
} |
| 319 |
|
| 320 |
// ? Start SQL Statement |
| 321 |
// AAWP plugin |
| 322 |
if ( ( $is_aawp_installed && empty( $filter_plugin ) ) || ( $support_plugin[ Setting_Enum::AAWP_SLUG ] === $filter_plugin ) ) { |
| 323 |
// ? aawp products |
| 324 |
$sql .= " |
| 325 |
SELECT |
| 326 |
CONVERT(asin USING utf8) AS id, |
| 327 |
'aawp' AS post_type, |
| 328 |
'AAWP' AS import_source, |
| 329 |
CONVERT(asin USING utf8) AS post_name, |
| 330 |
CONVERT(title USING utf8) AS post_title, |
| 331 |
'' AS check_status, |
| 332 |
'' AS check_disabled |
| 333 |
FROM |
| 334 |
" . $this->aawp . ' AS ap |
| 335 |
LEFT JOIN |
| 336 |
' . $revert_table . ' AS r |
| 337 |
ON CONVERT(ap.asin USING utf8) = CONVERT(r.old_uri USING utf8) |
| 338 |
WHERE |
| 339 |
r.old_uri IS NULL |
| 340 |
|
| 341 |
UNION |
| 342 |
'; |
| 343 |
|
| 344 |
// ? aawp lists |
| 345 |
$sql .= " |
| 346 |
SELECT |
| 347 |
id, |
| 348 |
'aawp_list' AS post_type, |
| 349 |
'AAWP' AS import_source, |
| 350 |
type AS post_name, |
| 351 |
keywords AS post_title, |
| 352 |
'' AS check_status, |
| 353 |
'' AS check_disabled |
| 354 |
FROM " . $this->aawp_list . ' AS ap |
| 355 |
|
| 356 |
UNION |
| 357 |
'; |
| 358 |
} |
| 359 |
|
| 360 |
$post_type_sql = '1 = 0'; // ? default post type sql |
| 361 |
if ( in_array( |
| 362 |
$filter_plugin, |
| 363 |
array( |
| 364 |
$support_plugin[ Setting_Enum::PRETTY_LINK_SLUG ], |
| 365 |
$support_plugin[ Setting_Enum::EARNIST_SLUG ], |
| 366 |
), |
| 367 |
true |
| 368 |
) ) { |
| 369 |
$post_type_sql = "post_type IN ('%s')"; |
| 370 |
$post_type_sql = sprintf( $post_type_sql, $support_plugin_flip[ $filter_plugin ] ); |
| 371 |
} elseif ( empty( $filter_plugin ) ) { |
| 372 |
$post_type_sql = "post_type IN ('%s', '%s')"; |
| 373 |
$post_type_sql = sprintf( |
| 374 |
$post_type_sql, |
| 375 |
Setting_Enum::PRETTY_LINK_SLUG, |
| 376 |
Setting_Enum::EARNIST_SLUG, |
| 377 |
); |
| 378 |
} |
| 379 |
|
| 380 |
$sql = $sql . " |
| 381 |
SELECT |
| 382 |
p.id, |
| 383 |
p.post_type, |
| 384 |
CASE |
| 385 |
WHEN p.post_type = 'pretty-link' |
| 386 |
THEN 'Pretty Links' |
| 387 |
WHEN p.post_type = 'thirstylink' |
| 388 |
THEN 'Thirsty Affiliates' |
| 389 |
WHEN p.post_type = 'earnist' |
| 390 |
THEN 'Earnist' |
| 391 |
WHEN p.post_type = 'affiliate_url' |
| 392 |
THEN 'Affiliate URLs' |
| 393 |
WHEN p.post_type = 'aawp' |
| 394 |
THEN 'AAWP' |
| 395 |
WHEN p.post_type = 'easyazon' |
| 396 |
THEN 'EasyAzon' |
| 397 |
WHEN p.post_type = 'amalinks' |
| 398 |
THEN 'Amalinks Pro' |
| 399 |
WHEN p.post_type = '" . Setting_Enum::LASSO_PRO_SLUG . "' |
| 400 |
THEN 'Lasso Pro' |
| 401 |
ELSE 'Unknown' |
| 402 |
END AS import_source, |
| 403 |
" . $prlipro_post_name . " |
| 404 |
CONVERT(p.post_title USING utf8) AS post_title, |
| 405 |
'' AS check_status, |
| 406 |
'' AS check_disabled |
| 407 |
FROM " . $this->posts . ' AS p |
| 408 |
' . $prlipro_join . " |
| 409 |
WHERE |
| 410 |
$post_type_sql |
| 411 |
AND p.ID NOT IN ( |
| 412 |
SELECT post_id |
| 413 |
FROM " . $this->postmeta . " |
| 414 |
WHERE meta_key = 'old_status' |
| 415 |
AND meta_value != '' |
| 416 |
) |
| 417 |
AND post_status IN ('publish', 'pending', 'draft', 'future', 'private', 'inherit', 'trash') |
| 418 |
"; |
| 419 |
|
| 420 |
// ? Easyazon plugin |
| 421 |
if ( empty( $filter_plugin ) || $support_plugin[ Setting_Enum::EASYAZON_SLUG ] === $filter_plugin ) { |
| 422 |
$sql = $sql . " |
| 423 |
UNION |
| 424 |
|
| 425 |
SELECT DISTINCT |
| 426 |
CONVERT(substring_index( substring_index(option_name, 'easyazon_item_', -1), '_', 1) USING utf8) AS id, |
| 427 |
'easyazon' AS post_type, |
| 428 |
'EasyAzon' AS import_source, |
| 429 |
'' AS post_name, |
| 430 |
CONVERT(substring_index( option_name, '_', 4) USING utf8) AS post_title, |
| 431 |
CASE |
| 432 |
WHEN CONVERT(substring_index( substring_index(option_name, 'easyazon_item_', -1), '_', 1) USING utf8) IN ( |
| 433 |
SELECT CONVERT(old_uri USING utf8) AS old_uri |
| 434 |
FROM " . $revert_table . " |
| 435 |
WHERE plugin = 'easyazon' |
| 436 |
) |
| 437 |
THEN 'checked' |
| 438 |
ELSE '' |
| 439 |
END AS check_status, |
| 440 |
'' AS check_disabled |
| 441 |
FROM " . $this->options . " AS ap |
| 442 |
WHERE option_name LIKE 'easyazon_item_%' |
| 443 |
"; |
| 444 |
} |
| 445 |
|
| 446 |
// ? Easy Affiliate Link plugin - EAL is having 2 types "HTML code" and "Text Link", we only get "text" value. |
| 447 |
if ( empty( $filter_plugin ) || $support_plugin[ Setting_Enum::EASY_AFFILIATE_LINK_SLUG ] === $filter_plugin ) { |
| 448 |
$sql = $sql . " |
| 449 |
UNION |
| 450 |
|
| 451 |
SELECT |
| 452 |
po.ID as id, |
| 453 |
po.post_type, |
| 454 |
'Easy Affiliate Links' as import_source, |
| 455 |
CONVERT(po.post_name USING utf8) as post_name, |
| 456 |
CONVERT(po.post_title USING utf8) as post_title, |
| 457 |
'' as check_status, |
| 458 |
'' as check_disabled |
| 459 |
FROM " . $this->posts . ' as po |
| 460 |
INNER JOIN ' . $this->postmeta . ' as pom ON po.ID = pom.post_id |
| 461 |
WHERE po.post_type = %s |
| 462 |
AND pom.meta_value = %s |
| 463 |
'; |
| 464 |
|
| 465 |
$sql = Model::prepare( $sql, Setting_Enum::EASY_AFFILIATE_LINK_SLUG, 'text' ); |
| 466 |
} |
| 467 |
|
| 468 |
// ? Affiliate URL Automation plugin |
| 469 |
if ( empty( $filter_plugin ) || $support_plugin[ Setting_Enum::AFFILIATE_URL_SLUG ] === $filter_plugin ) { |
| 470 |
$sql = $sql . " |
| 471 |
UNION |
| 472 |
|
| 473 |
SELECT |
| 474 |
po.ID AS id, |
| 475 |
po.post_type, |
| 476 |
'Affiliate URLs' AS import_source, |
| 477 |
CONVERT(po.post_name USING utf8) AS post_name, |
| 478 |
CONVERT(po.post_title USING utf8) AS post_title, |
| 479 |
'' AS check_status, |
| 480 |
'' AS check_disabled |
| 481 |
FROM " . $this->posts . ' AS po |
| 482 |
INNER JOIN ' . $this->postmeta . ' AS pom |
| 483 |
ON po.ID = pom.post_id |
| 484 |
WHERE po.post_type = %s |
| 485 |
AND pom.meta_key = %s |
| 486 |
AND pom.meta_value IS NOT NULL |
| 487 |
AND pom.meta_value <> "" |
| 488 |
'; |
| 489 |
|
| 490 |
$sql = Model::prepare( $sql, Setting_Enum::AFFILIATE_URL_SLUG, '_affiliate_url_redirect' ); |
| 491 |
} |
| 492 |
|
| 493 |
// ? Thirsty Affiliates plugin |
| 494 |
if ( empty( $filter_plugin ) || $support_plugin[ Setting_Enum::THIRSTYLINK_SLUG ] === $filter_plugin ) { |
| 495 |
$sql = $sql . " |
| 496 |
UNION |
| 497 |
|
| 498 |
SELECT |
| 499 |
po.ID AS id, |
| 500 |
po.post_type, |
| 501 |
'Thirsty Affiliates' AS import_source, |
| 502 |
CONVERT(po.post_name USING utf8) AS post_name, |
| 503 |
CONVERT(po.post_title USING utf8) AS post_title, |
| 504 |
'' AS check_status, |
| 505 |
'' AS check_disabled |
| 506 |
FROM " . $this->posts . ' AS po |
| 507 |
WHERE po.post_type = %s |
| 508 |
AND po.ID IN ( |
| 509 |
SELECT post_id |
| 510 |
FROM ' . $this->postmeta . ' |
| 511 |
WHERE meta_key = %s |
| 512 |
AND meta_value IS NOT NULL |
| 513 |
AND meta_value <> "" |
| 514 |
) |
| 515 |
'; |
| 516 |
|
| 517 |
$sql = Model::prepare( $sql, Setting_Enum::THIRSTYLINK_SLUG, '_ta_destination_url' ); |
| 518 |
} |
| 519 |
|
| 520 |
// ? Lasso Pro plugin |
| 521 |
if ( empty( $filter_plugin ) || $support_plugin[ Setting_Enum::LASSO_PRO_SLUG ] === $filter_plugin ) { |
| 522 |
$url_details_table = Model::get_wp_table_name( 'lasso_url_details' ); |
| 523 |
$sql = $sql . " |
| 524 |
UNION |
| 525 |
|
| 526 |
SELECT |
| 527 |
po.ID as id, |
| 528 |
po.post_type, |
| 529 |
'Lasso Pro' as import_source, |
| 530 |
CONVERT(po.post_name USING utf8) as post_name, |
| 531 |
CONVERT(po.post_title USING utf8) as post_title, |
| 532 |
'' as check_status, |
| 533 |
'' as check_disabled |
| 534 |
FROM " . $this->posts . ' as po |
| 535 |
INNER JOIN ' . $url_details_table . ' AS lud ON lud.lasso_id = po.ID |
| 536 |
WHERE po.post_type = %s |
| 537 |
AND lud.redirect_url IS NOT NULL |
| 538 |
AND lud.redirect_url <> "" |
| 539 |
'; |
| 540 |
|
| 541 |
$sql = Model::prepare( $sql, Setting_Enum::LASSO_PRO_SLUG ); |
| 542 |
} |
| 543 |
|
| 544 |
if ( $include_imported ) { |
| 545 |
if ( in_array( |
| 546 |
$filter_plugin, |
| 547 |
array( |
| 548 |
$support_plugin[ Setting_Enum::PRETTY_LINK_SLUG ], |
| 549 |
$support_plugin[ Setting_Enum::THIRSTYLINK_SLUG ], |
| 550 |
$support_plugin[ Setting_Enum::EARNIST_SLUG ], |
| 551 |
$support_plugin[ Setting_Enum::AFFILIATE_URL_SLUG ], |
| 552 |
$support_plugin[ Setting_Enum::AAWP_SLUG ], |
| 553 |
$support_plugin[ Setting_Enum::EASY_AFFILIATE_LINK_SLUG ], |
| 554 |
$support_plugin[ Setting_Enum::LASSO_PRO_SLUG ], |
| 555 |
), |
| 556 |
true |
| 557 |
) |
| 558 |
) { |
| 559 |
$r_plugin_where = "r.plugin IN ('%s')"; |
| 560 |
$r_plugin_where = sprintf( $r_plugin_where, $support_plugin_flip[ $filter_plugin ] ); |
| 561 |
} else { |
| 562 |
$r_plugin_where = "r.plugin IN ('%s', '%s', '%s', '%s', '%s', '%s', '%s')"; |
| 563 |
$r_plugin_where = sprintf( |
| 564 |
$r_plugin_where, |
| 565 |
Setting_Enum::PRETTY_LINK_SLUG, |
| 566 |
Setting_Enum::THIRSTYLINK_SLUG, |
| 567 |
Setting_Enum::EARNIST_SLUG, |
| 568 |
Setting_Enum::AFFILIATE_URL_SLUG, |
| 569 |
Setting_Enum::AAWP_SLUG, |
| 570 |
Setting_Enum::EASY_AFFILIATE_LINK_SLUG, |
| 571 |
Setting_Enum::LASSO_PRO_SLUG, |
| 572 |
); |
| 573 |
} |
| 574 |
$sql = $sql . " |
| 575 |
UNION |
| 576 |
|
| 577 |
SELECT |
| 578 |
p.id, |
| 579 |
p.post_type, |
| 580 |
CASE |
| 581 |
WHEN r.plugin = 'pretty-link' |
| 582 |
THEN 'Pretty Links' |
| 583 |
WHEN r.plugin = 'thirstylink' |
| 584 |
THEN 'Thirsty Affiliates' |
| 585 |
WHEN r.plugin = 'earnist' |
| 586 |
THEN 'Earnist' |
| 587 |
WHEN r.plugin = 'affiliate_url' |
| 588 |
THEN 'Affiliate URLs' |
| 589 |
WHEN r.plugin = 'aawp' |
| 590 |
THEN 'AAWP' |
| 591 |
WHEN r.plugin = 'easyazon' |
| 592 |
THEN 'EasyAzon' |
| 593 |
WHEN r.plugin = 'amalinks' |
| 594 |
THEN 'Amalinks Pro' |
| 595 |
WHEN r.plugin = 'easy_affiliate_link' |
| 596 |
THEN 'Easy Affiliate Links' |
| 597 |
WHEN r.plugin = '" . Setting_Enum::LASSO_PRO_SLUG . "' |
| 598 |
THEN 'Lasso Pro' |
| 599 |
ELSE 'Unknown' |
| 600 |
END as import_source, |
| 601 |
CASE |
| 602 |
WHEN r.plugin = 'aawp' |
| 603 |
THEN CONVERT(r.old_uri USING utf8) |
| 604 |
ELSE CONVERT(p.post_name USING utf8) |
| 605 |
END as post_name, |
| 606 |
CONVERT(p.post_title USING utf8) as post_title, |
| 607 |
'checked' as check_status, |
| 608 |
'' as check_disabled |
| 609 |
FROM " . $this->posts . ' as p |
| 610 |
INNER JOIN |
| 611 |
' . $revert_table . " as r |
| 612 |
ON p.id = r.lasso_id |
| 613 |
WHERE |
| 614 |
$r_plugin_where |
| 615 |
"; |
| 616 |
|
| 617 |
// ? AmaLinks Pro plugin - imported |
| 618 |
if ( empty( $filter_plugin ) || $support_plugin[ Setting_Enum::AFFILIATE_URL_SLUG ] === $filter_plugin ) { |
| 619 |
$sql = $sql . " |
| 620 |
UNION |
| 621 |
|
| 622 |
SELECT DISTINCT |
| 623 |
CONVERT(r.lasso_id USING utf8) as id, |
| 624 |
'amalinkspro' as post_type, |
| 625 |
'AmaLinks Pro' as import_source, |
| 626 |
CASE |
| 627 |
WHEN CONVERT(la.monetized_url USING utf8) != '' |
| 628 |
THEN CONVERT(la.monetized_url USING utf8) |
| 629 |
WHEN CONVERT(wpp.guid USING utf8) != '' |
| 630 |
THEN CONVERT(wpp.guid USING utf8) |
| 631 |
ELSE '' |
| 632 |
END as post_name, |
| 633 |
CASE |
| 634 |
WHEN CONVERT(la.default_product_name USING utf8) != '' |
| 635 |
THEN CONVERT(la.default_product_name USING utf8) |
| 636 |
WHEN CONVERT(wpp.post_title USING utf8) != '' |
| 637 |
THEN CONVERT(wpp.post_title USING utf8) |
| 638 |
ELSE '' |
| 639 |
END as post_title, |
| 640 |
'checked' as check_status, |
| 641 |
'' as check_disabled |
| 642 |
FROM " . $revert_table . ' as r |
| 643 |
LEFT JOIN ' . $this->posts . ' as wpp |
| 644 |
ON r.lasso_id = wpp.ID |
| 645 |
LEFT JOIN ' . $this->amazon_products . " as la |
| 646 |
ON r.old_uri = la.amazon_id |
| 647 |
WHERE r.plugin = 'amalinkspro' |
| 648 |
"; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
$include_imported_where = $include_imported ? '' : 'AND check_status != "checked"'; |
| 653 |
|
| 654 |
$sql = ' |
| 655 |
SELECT ' . $select . ' |
| 656 |
FROM |
| 657 |
( |
| 658 |
' . $sql . ' |
| 659 |
) as BASE |
| 660 |
WHERE |
| 661 |
1=1 |
| 662 |
' . $where . ' |
| 663 |
' . $include_imported_where . ' |
| 664 |
' . $search . ' |
| 665 |
GROUP BY ' . $group_by . ' |
| 666 |
ORDER BY ' . $order_by; |
| 667 |
|
| 668 |
return $sql; |
| 669 |
} |
| 670 |
|
| 671 |
/** |
| 672 |
* Check whether pretty links pro plugin is install or not |
| 673 |
*/ |
| 674 |
public function is_pretty_links_pro_installed() { |
| 675 |
return Model::column_exists( $this->pretty_links, 'link_cpt_id' ); |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Check whether aawp plugin is installed or not |
| 680 |
*/ |
| 681 |
public function is_aawp_installed() { |
| 682 |
return Model::table_exists( $this->aawp ); |
| 683 |
} |
| 684 |
|
| 685 |
/** |
| 686 |
* Get pretty link by id |
| 687 |
* |
| 688 |
* @param int $id Id of pretty link. |
| 689 |
*/ |
| 690 |
public function get_pretty_link_by_id( $id ) { |
| 691 |
$sql = ' |
| 692 |
SELECT * |
| 693 |
FROM ' . $this->pretty_links . ' |
| 694 |
WHERE link_cpt_id = ' . $id . '; |
| 695 |
'; |
| 696 |
|
| 697 |
return $this->get_row( $sql ); |
| 698 |
} |
| 699 |
|
| 700 |
/** |
| 701 |
* Get aawp product |
| 702 |
* |
| 703 |
* @param string $product_id Amazon product id. |
| 704 |
*/ |
| 705 |
public function get_aawp_product( $product_id ) { |
| 706 |
$sql = ' |
| 707 |
SELECT * |
| 708 |
FROM ' . $this->aawp . " |
| 709 |
WHERE asin = '" . $product_id . "' |
| 710 |
"; |
| 711 |
|
| 712 |
$row = $this->get_row( $sql ); |
| 713 |
|
| 714 |
if ( $row ) { |
| 715 |
$lasso_amazon_api = new Amazon_Api(); |
| 716 |
|
| 717 |
$row_url = maybe_unserialize( $row->urls ?? $row->url ?? '' ); |
| 718 |
$url = $row_url['basic'] ?? $row_url; |
| 719 |
$url = is_string( $url ) ? $url : ''; |
| 720 |
|
| 721 |
$row->url = $lasso_amazon_api->get_amazon_product_url( $url, true, false ); |
| 722 |
|
| 723 |
$images = $row->image_ids; |
| 724 |
$images = explode( ',', $images ); |
| 725 |
$image_id = $images[0]; |
| 726 |
$image_url = 'https://m.media-amazon.com/images/I/' . $image_id . '.jpg'; |
| 727 |
|
| 728 |
$row->image_url = $image_url; |
| 729 |
$row->features = maybe_unserialize( $row->features ?? array() ); // phpcs:ignore |
| 730 |
} |
| 731 |
|
| 732 |
return $row; |
| 733 |
} |
| 734 |
|
| 735 |
/** |
| 736 |
* Get aawp lists |
| 737 |
* |
| 738 |
* @param string $id Id. |
| 739 |
*/ |
| 740 |
public function get_aawp_list( $id ) { |
| 741 |
$sql = ' |
| 742 |
SELECT * |
| 743 |
FROM ' . $this->aawp_list . ' |
| 744 |
WHERE id = ' . $id . ' |
| 745 |
'; |
| 746 |
|
| 747 |
return $this->get_row( $sql ); |
| 748 |
} |
| 749 |
|
| 750 |
/** |
| 751 |
* Check EasyAzon product id is imported or not |
| 752 |
* |
| 753 |
* @param string $asin Amazon product id. |
| 754 |
*/ |
| 755 |
public function is_easyazon_product_imported( $asin ) { |
| 756 |
global $wpdb; |
| 757 |
|
| 758 |
$sql = ' |
| 759 |
select id, lasso_id |
| 760 |
from ' . ( new Revert() )->get_table_name() . ' |
| 761 |
where old_uri = %s and plugin = \'easyazon\' |
| 762 |
'; |
| 763 |
$prepare = $wpdb->prepare( $sql, $asin ); // phpcs:ignore |
| 764 |
|
| 765 |
return $this->get_row( $prepare ); |
| 766 |
} |
| 767 |
|
| 768 |
/** |
| 769 |
* Get url detail by product id (Amazon/Extend product) |
| 770 |
* |
| 771 |
* @param string $product_id Product id. |
| 772 |
* @param string $product_type Product type. |
| 773 |
*/ |
| 774 |
public function get_url_details_by_product_id( $product_id, $product_type ) { |
| 775 |
if ( ! $product_id ) { |
| 776 |
return null; |
| 777 |
} |
| 778 |
|
| 779 |
global $wpdb; |
| 780 |
|
| 781 |
$sql = ' |
| 782 |
SELECT * |
| 783 |
FROM ' . ( new Url_Details() )->get_table_name() . ' as lud |
| 784 |
LEFT JOIN ' . $this->posts . ' as wpp |
| 785 |
ON lud.lasso_id = wpp.ID |
| 786 |
WHERE lud.product_id = %s |
| 787 |
AND wpp.ID is not null |
| 788 |
AND wpp.post_status = "publish" |
| 789 |
AND wpp.post_type = %s |
| 790 |
AND lud.product_type = %s |
| 791 |
ORDER BY lasso_id desc |
| 792 |
'; |
| 793 |
$prepare = $wpdb->prepare( $sql, $product_id, Constant::LASSO_POST_TYPE, $product_type ); // phpcs:ignore |
| 794 |
|
| 795 |
return $this->get_row( $prepare ); |
| 796 |
} |
| 797 |
|
| 798 |
/** |
| 799 |
* Process import |
| 800 |
* |
| 801 |
* @param int $id Post id. |
| 802 |
* @param string $slug Link slug. |
| 803 |
* @param string $old_uri Old URI. |
| 804 |
* @param string $plugin Plugin name. |
| 805 |
*/ |
| 806 |
public function process_import( $id, $slug, $old_uri, $plugin ) { |
| 807 |
if ( empty( $id ) || empty( $slug ) ) { |
| 808 |
return false; |
| 809 |
} |
| 810 |
|
| 811 |
global $wpdb; |
| 812 |
clean_post_cache( $id ); |
| 813 |
|
| 814 |
$result1 = true; |
| 815 |
if ( ! in_array( $plugin, array( 'aawp', 'amalinkspro', 'easyazon' ), true ) ) { |
| 816 |
// ? Flip post time and potentially the slug |
| 817 |
$update_sql = ' |
| 818 |
UPDATE ' . $this->posts . ' |
| 819 |
SET |
| 820 |
post_name = %s, |
| 821 |
post_type = %s, |
| 822 |
post_modified = NOW(), |
| 823 |
post_modified_gmt = NOW() |
| 824 |
WHERE ID = %d; |
| 825 |
'; |
| 826 |
$update_sql = $wpdb->prepare( $update_sql, $slug, SIMPLE_URLS_SLUG, $id ); // phpcs:ignore |
| 827 |
Model::query( $update_sql ); |
| 828 |
$result1 = SIMPLE_URLS_SLUG === get_post_type( $id ); |
| 829 |
} |
| 830 |
|
| 831 |
// ? Log what we imported for potential reverts |
| 832 |
$insert_sql = ' |
| 833 |
INSERT INTO ' . ( new Revert() )->get_table_name() . ' (lasso_id, old_uri, plugin, revert_dt) |
| 834 |
VALUES (%d, %s, %s, NOW()); |
| 835 |
'; |
| 836 |
$prepare = Model::prepare( $insert_sql, $id, $old_uri, $plugin ); |
| 837 |
$result2 = Model::query( $prepare ); |
| 838 |
|
| 839 |
clean_post_cache( $id ); |
| 840 |
|
| 841 |
return $result1 && $result2; |
| 842 |
} |
| 843 |
|
| 844 |
/** |
| 845 |
* Process revert |
| 846 |
* |
| 847 |
* @param int $id Post id. |
| 848 |
* @param bool $custom_post_type It is custom post type or not. Default to true. |
| 849 |
*/ |
| 850 |
public function process_revert( $id, $custom_post_type = true ) { |
| 851 |
// ? Get post type from revert table |
| 852 |
$result1 = true; |
| 853 |
if ( empty( $id ) ) { |
| 854 |
return false; |
| 855 |
} |
| 856 |
|
| 857 |
if ( $custom_post_type ) { |
| 858 |
$revert_data = $this->get_revert_by_id( $id ); |
| 859 |
if ( ! empty( $revert_data ) ) { |
| 860 |
// ? Switch back |
| 861 |
if ( isset( $revert_data->plugin ) && 'pretty-link' === $revert_data->plugin ) { |
| 862 |
$pretty_link_data = $this->get_pretty_link_by_id( $id ); |
| 863 |
$update_sql = ' |
| 864 |
UPDATE ' . $this->posts . ' |
| 865 |
SET |
| 866 |
post_name = %s, |
| 867 |
post_type = %s |
| 868 |
WHERE id = %d; |
| 869 |
'; |
| 870 |
$update_sql = Model::prepare( $update_sql, $pretty_link_data->slug, $revert_data->plugin, $id ); |
| 871 |
} else { |
| 872 |
$update_sql = ' |
| 873 |
UPDATE ' . $this->posts . ' |
| 874 |
SET post_type = %s |
| 875 |
WHERE id = %d; |
| 876 |
'; |
| 877 |
$update_sql = Model::prepare( $update_sql, $revert_data->plugin, $id ); |
| 878 |
} |
| 879 |
$result1 = Model::query( $update_sql ); |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
// ? Delete tracking record |
| 884 |
$delete_sql = ' |
| 885 |
DELETE FROM ' . ( new Revert() )->get_table_name() . ' |
| 886 |
WHERE lasso_id = %d; |
| 887 |
'; |
| 888 |
$delete_sql = Model::prepare( $delete_sql, $id ); |
| 889 |
$result2 = Model::query( $delete_sql ); |
| 890 |
|
| 891 |
clean_post_cache( $id ); |
| 892 |
|
| 893 |
return $result1 && $result2; |
| 894 |
} |
| 895 |
|
| 896 |
/** |
| 897 |
* Get revert by id |
| 898 |
* |
| 899 |
* @param int $id Id in revert table. |
| 900 |
*/ |
| 901 |
public function get_revert_by_id( $id ) { |
| 902 |
$sql = ' |
| 903 |
SELECT * |
| 904 |
FROM ' . ( new Revert() )->get_table_name() . ' |
| 905 |
WHERE lasso_id = %d; |
| 906 |
'; |
| 907 |
$sql = Model::prepare( $sql, $id ); |
| 908 |
|
| 909 |
return Model::get_row( $sql ); |
| 910 |
} |
| 911 |
|
| 912 |
/** |
| 913 |
* Delete url detail |
| 914 |
* |
| 915 |
* @param int $lasso_id Lasso post id. |
| 916 |
*/ |
| 917 |
public function delete_url_details( $lasso_id ) { |
| 918 |
$sql = ' |
| 919 |
DELETE FROM ' . ( new Url_Details() )->get_table_name() . ' |
| 920 |
WHERE lasso_id = %d'; |
| 921 |
$sql = Model::prepare( $sql, $lasso_id ); |
| 922 |
|
| 923 |
return Model::query( $sql ); |
| 924 |
} |
| 925 |
|
| 926 |
/** |
| 927 |
* Get EasyAzon product |
| 928 |
* |
| 929 |
* @param string $asin Amazon product id. |
| 930 |
*/ |
| 931 |
public function get_easyazon_product( $asin ) { |
| 932 |
if ( ! $asin ) { |
| 933 |
return null; |
| 934 |
} |
| 935 |
|
| 936 |
$search = 'easyazon_item_' . $asin . '%'; |
| 937 |
$sql = ' |
| 938 |
select option_value |
| 939 |
from ' . $this->options . ' |
| 940 |
where option_name like %s |
| 941 |
order by option_id desc |
| 942 |
limit 1 |
| 943 |
'; |
| 944 |
$prepare = Model::prepare( $sql, $search ); // phpcs:ignore |
| 945 |
|
| 946 |
return Model::get_row( $prepare ); |
| 947 |
} |
| 948 |
|
| 949 |
/** |
| 950 |
* Remove: all background processes of Lasso |
| 951 |
*/ |
| 952 |
public function remove_all_lasso_processes() { |
| 953 |
$sql = ' |
| 954 |
DELETE FROM ' . $this->options . " |
| 955 |
WHERE option_name like '%lassolite_%_batch_%' |
| 956 |
"; |
| 957 |
$this->query( $sql ); |
| 958 |
} |
| 959 |
|
| 960 |
/** |
| 961 |
* Check whether the process is empty |
| 962 |
*/ |
| 963 |
public function check_empty_process() { |
| 964 |
$count_query = ' |
| 965 |
SELECT count(option_id) as `total` |
| 966 |
FROM ' . $this->options . " |
| 967 |
WHERE |
| 968 |
option_name LIKE '%lassolite_%_batch_%' |
| 969 |
AND option_value LIKE 'a:1:{i:0;i:%' |
| 970 |
"; |
| 971 |
$total = $this->get_row( $count_query ); |
| 972 |
$total = $total->total ?? 0; |
| 973 |
$total = intval( $total ); |
| 974 |
|
| 975 |
// ? delete empty processes if there are more 10 empty processes |
| 976 |
if ( $total > 10 ) { |
| 977 |
$this->remove_empty_process(); |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
/** |
| 982 |
* Remove: empty process |
| 983 |
*/ |
| 984 |
public function remove_empty_process() { |
| 985 |
$sql = ' |
| 986 |
DELETE FROM ' . $this->options . " |
| 987 |
WHERE |
| 988 |
option_name LIKE '%lassolite_%_batch_%' |
| 989 |
AND option_value LIKE 'a:1:{i:0;i:%' |
| 990 |
"; |
| 991 |
$this->query( $sql ); |
| 992 |
} |
| 993 |
|
| 994 |
/** |
| 995 |
* Paginate items by a sql query |
| 996 |
* |
| 997 |
* @param string $sql Sql query. |
| 998 |
* @param int $page Number of page. |
| 999 |
* @param int $limit Number of results. Default to 10. |
| 1000 |
*/ |
| 1001 |
public function paginate( $sql, $page, $limit = 10 ) { |
| 1002 |
$start_index = ( $page - 1 ) * $limit; |
| 1003 |
return $sql . ' LIMIT ' . $start_index . ', ' . $limit; |
| 1004 |
} |
| 1005 |
|
| 1006 |
/** |
| 1007 |
* Get revertable url query |
| 1008 |
* |
| 1009 |
* @param string $filter_plugin Plugin name. Default to null (all plugins). |
| 1010 |
*/ |
| 1011 |
public function get_revertable_urls_query( $filter_plugin = null ) { |
| 1012 |
$key = array_search( $filter_plugin, Setting_Enum::SUPPORT_IMPORT_PLUGINS, true ); |
| 1013 |
|
| 1014 |
if ( false === $key ) { |
| 1015 |
$where = "`plugin` IN ('pretty-link', 'thirstylink', 'earnist', 'affiliate_url', 'aawp', 'easyazon', 'amalinkspro', '" . Setting_Enum::LASSO_PRO_SLUG . "')"; |
| 1016 |
} else { |
| 1017 |
$where = '`plugin` = %s'; |
| 1018 |
$where = Model::prepare( $where, $key ); |
| 1019 |
} |
| 1020 |
|
| 1021 |
$sql = ' |
| 1022 |
SELECT lasso_id AS import_id, `plugin` AS import_source |
| 1023 |
FROM ' . ( new Revert() )->get_table_name() . ' |
| 1024 |
WHERE ' . $where . ' |
| 1025 |
'; |
| 1026 |
|
| 1027 |
return $sql; |
| 1028 |
} |
| 1029 |
|
| 1030 |
/** |
| 1031 |
* Get easyazon option by option_name |
| 1032 |
* |
| 1033 |
* @param string $option_name A part of option name. |
| 1034 |
* @return mixed |
| 1035 |
*/ |
| 1036 |
public static function get_easyazon_option( $option_name ) { |
| 1037 |
$sql = ' |
| 1038 |
SELECT option_value |
| 1039 |
FROM ' . Model::get_wp_table_name( 'options' ) . ' |
| 1040 |
WHERE option_name LIKE %s |
| 1041 |
'; |
| 1042 |
$sql = Model::prepare( $sql, $option_name . '%' ); |
| 1043 |
|
| 1044 |
$result = Model::get_var( $sql ); |
| 1045 |
$result = maybe_unserialize( $result ); |
| 1046 |
|
| 1047 |
return $result; |
| 1048 |
} |
| 1049 |
|
| 1050 |
/** |
| 1051 |
* Get import plugin options to filter |
| 1052 |
* |
| 1053 |
* @param bool $get_count Get result count. |
| 1054 |
* @return array|int|mixed |
| 1055 |
*/ |
| 1056 |
public function get_import_plugins( $get_count = false ) { |
| 1057 |
$sql = $this->get_importable_urls_query( true, '', 'BASE.import_source' ); |
| 1058 |
|
| 1059 |
if ( $get_count ) { |
| 1060 |
return Model::get_count( $sql ); |
| 1061 |
} |
| 1062 |
|
| 1063 |
$import_results = Model::get_results( $sql ); |
| 1064 |
|
| 1065 |
$result = array(); |
| 1066 |
|
| 1067 |
foreach ( $import_results as $import_result ) { |
| 1068 |
$result[] = $import_result->import_source; |
| 1069 |
} |
| 1070 |
|
| 1071 |
return $result; |
| 1072 |
} |
| 1073 |
|
| 1074 |
|
| 1075 |
/** |
| 1076 |
* Get lasso lite post by uri |
| 1077 |
* |
| 1078 |
* @param string $uri Uri. |
| 1079 |
*/ |
| 1080 |
public function get_lasso_by_uri( $uri ) { |
| 1081 |
if ( empty( $uri ) ) { |
| 1082 |
return null; |
| 1083 |
} |
| 1084 |
|
| 1085 |
global $wpdb; |
| 1086 |
|
| 1087 |
$explode = explode( '/', $uri ); |
| 1088 |
$post_name = end( $explode ); |
| 1089 |
|
| 1090 |
$sql = ' |
| 1091 |
SELECT * |
| 1092 |
FROM ' . $this->posts . ' |
| 1093 |
WHERE post_name = %s AND post_type = %s |
| 1094 |
'; |
| 1095 |
$prepare = $wpdb->prepare( $sql, $post_name, Constant::LASSO_POST_TYPE ); // phpcs:ignore |
| 1096 |
|
| 1097 |
return $this->get_row( $prepare, 'OBJECT', true ); |
| 1098 |
} |
| 1099 |
|
| 1100 |
/** |
| 1101 |
* Get url detail by url |
| 1102 |
* |
| 1103 |
* @param string $url URL. |
| 1104 |
*/ |
| 1105 |
public function get_url_details_by_url( $url ) { |
| 1106 |
global $wpdb; |
| 1107 |
|
| 1108 |
$sql = ' |
| 1109 |
SELECT |
| 1110 |
lud.* |
| 1111 |
FROM |
| 1112 |
' . ( new Url_Details() )->get_table_name() . ' AS lud |
| 1113 |
LEFT JOIN |
| 1114 |
' . $this->posts . " AS wpp |
| 1115 |
ON |
| 1116 |
lud.lasso_id = wpp.ID |
| 1117 |
WHERE |
| 1118 |
redirect_url LIKE %s |
| 1119 |
AND wpp.ID != '' |
| 1120 |
"; |
| 1121 |
$prepare = $wpdb->prepare( $sql, $url ); // phpcs:ignore |
| 1122 |
|
| 1123 |
return $this->get_row( $prepare, 'OBJECT', true ); |
| 1124 |
} |
| 1125 |
|
| 1126 |
/** |
| 1127 |
* Check whether an amazon product existed in DB or not |
| 1128 |
* |
| 1129 |
* @param string $product_id Amazon product id. |
| 1130 |
*/ |
| 1131 |
public function check_amazon_product_exist( $product_id ) { |
| 1132 |
global $wpdb; |
| 1133 |
|
| 1134 |
$query = ' |
| 1135 |
SELECT `amazon_id` |
| 1136 |
FROM ' . $this->amazon_products . ' |
| 1137 |
WHERE `amazon_id` = %s |
| 1138 |
'; |
| 1139 |
|
| 1140 |
$prepare = $wpdb->prepare( $query, $product_id ); // phpcs:ignore |
| 1141 |
|
| 1142 |
return $this->get_row( $prepare, ARRAY_A ); |
| 1143 |
} |
| 1144 |
|
| 1145 |
/** |
| 1146 |
* Get lasso lite id from url by checking the post-meta key '_surl_redirect' |
| 1147 |
* |
| 1148 |
* @param string $url Url. |
| 1149 |
* @return int |
| 1150 |
*/ |
| 1151 |
public static function get_lasso_lite_id_by_url_from_post_meta( $url ) { |
| 1152 |
$url_without_slash_at_the_end = rtrim( $url, '/' ); |
| 1153 |
$url_with_slash_at_the_end_wildcard = rtrim( $url, '/' ) . '_'; |
| 1154 |
|
| 1155 |
$sql = ' |
| 1156 |
SELECT p.ID |
| 1157 |
FROM ' . Model::get_wp_table_name( 'posts' ) . ' AS p |
| 1158 |
INNER JOIN ' . Model::get_wp_table_name( 'postmeta' ) . ' AS pm |
| 1159 |
ON pm.post_id = p.ID |
| 1160 |
WHERE p.post_type = %s |
| 1161 |
AND pm.meta_key = %s |
| 1162 |
AND ( pm.meta_value = %s OR pm.meta_value LIKE %s ) |
| 1163 |
'; |
| 1164 |
$sql = Model::prepare( $sql, Constant::LASSO_POST_TYPE, '_surl_redirect', $url_without_slash_at_the_end, $url_with_slash_at_the_end_wildcard ); |
| 1165 |
|
| 1166 |
$lasso_id = Model::get_var( $sql ); |
| 1167 |
|
| 1168 |
return intval( $lasso_id ); |
| 1169 |
} |
| 1170 |
|
| 1171 |
/** |
| 1172 |
* Get Lasso Pro amazon product |
| 1173 |
* |
| 1174 |
* @param string $product_id Amazon product id. |
| 1175 |
*/ |
| 1176 |
public function get_lasso_pro_amazon_product( $product_id ) { |
| 1177 |
$sql = ' |
| 1178 |
SELECT amazon_id, base_url, default_image AS image_url, default_product_name, latest_price, is_prime, |
| 1179 |
currency, savings_amount, savings_percent, savings_basis, out_of_stock, last_updated, rating, reviews, features |
| 1180 |
FROM ' . Model::get_wp_table_name( 'lasso_amazon_products' ) . " |
| 1181 |
WHERE amazon_id = '" . $product_id . "' |
| 1182 |
"; |
| 1183 |
|
| 1184 |
$row = $this->get_row( $sql ); |
| 1185 |
|
| 1186 |
if ( $row ) { |
| 1187 |
$lasso_amazon_api = new Amazon_Api(); |
| 1188 |
$row->url = $lasso_amazon_api->get_amazon_product_url( $row->base_url, true, false ); |
| 1189 |
$row->features = $row->features ? json_decode( $row->features ) : array(); |
| 1190 |
$row->quantity = 0 === intval( $row->out_of_stock ) ? 200 : 0; |
| 1191 |
} |
| 1192 |
|
| 1193 |
return $row; |
| 1194 |
} |
| 1195 |
} |
| 1196 |
|