| 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 |
if ( in_array( |
| 361 |
$filter_plugin, |
| 362 |
array( |
| 363 |
$support_plugin[ Setting_Enum::PRETTY_LINK_SLUG ], |
| 364 |
$support_plugin[ Setting_Enum::THIRSTYLINK_SLUG ], |
| 365 |
$support_plugin[ Setting_Enum::EARNIST_SLUG ], |
| 366 |
$support_plugin[ Setting_Enum::AFFILIATE_URL_SLUG ], |
| 367 |
), |
| 368 |
true |
| 369 |
) ) { |
| 370 |
$post_type_sql = "post_type in ('%s')"; |
| 371 |
$post_type_sql = sprintf( $post_type_sql, $support_plugin_flip[ $filter_plugin ] ); |
| 372 |
} else { |
| 373 |
$post_type_sql = "post_type in ('%s', '%s', '%s', '%s')"; |
| 374 |
$post_type_sql = sprintf( |
| 375 |
$post_type_sql, |
| 376 |
Setting_Enum::PRETTY_LINK_SLUG, |
| 377 |
Setting_Enum::THIRSTYLINK_SLUG, |
| 378 |
Setting_Enum::EARNIST_SLUG, |
| 379 |
Setting_Enum::AFFILIATE_URL_SLUG |
| 380 |
); |
| 381 |
} |
| 382 |
|
| 383 |
$sql = $sql . " |
| 384 |
SELECT |
| 385 |
p.id, |
| 386 |
p.post_type, |
| 387 |
CASE |
| 388 |
WHEN p.post_type = 'pretty-link' |
| 389 |
THEN 'Pretty Links' |
| 390 |
WHEN p.post_type = 'thirstylink' |
| 391 |
THEN 'Thirsty Affiliates' |
| 392 |
WHEN p.post_type = 'earnist' |
| 393 |
THEN 'Earnist' |
| 394 |
WHEN p.post_type = 'affiliate_url' |
| 395 |
THEN 'Affiliate URLs' |
| 396 |
WHEN p.post_type = 'aawp' |
| 397 |
THEN 'AAWP' |
| 398 |
WHEN p.post_type = 'easyazon' |
| 399 |
THEN 'EasyAzon' |
| 400 |
WHEN p.post_type = 'amalinks' |
| 401 |
THEN 'Amalinks Pro' |
| 402 |
ELSE 'Unknown' |
| 403 |
END AS import_source, |
| 404 |
" . $prlipro_post_name . " |
| 405 |
CONVERT(p.post_title USING utf8) AS post_title, |
| 406 |
'' AS check_status, |
| 407 |
'' AS check_disabled |
| 408 |
FROM " . $this->posts . ' AS p |
| 409 |
' . $prlipro_join . " |
| 410 |
WHERE |
| 411 |
$post_type_sql |
| 412 |
AND p.ID NOT IN ( |
| 413 |
SELECT post_id |
| 414 |
FROM " . $this->postmeta . " |
| 415 |
WHERE meta_key = 'old_status' |
| 416 |
AND meta_value != '' |
| 417 |
) |
| 418 |
AND post_status IN ('publish', 'pending', 'draft', 'future', 'private', 'inherit', 'trash') |
| 419 |
"; |
| 420 |
|
| 421 |
// ? Easyazon plugin |
| 422 |
if ( empty( $filter_plugin ) || $support_plugin[ Setting_Enum::EASYAZON_SLUG ] === $filter_plugin ) { |
| 423 |
$sql = $sql . " |
| 424 |
UNION |
| 425 |
|
| 426 |
SELECT |
| 427 |
CONVERT(option_name USING utf8) as id, |
| 428 |
'easyazon' as post_type, |
| 429 |
'EasyAzon' as import_source, |
| 430 |
CONVERT(option_name USING utf8) as post_name, |
| 431 |
CONVERT(option_value USING utf8) as post_title, |
| 432 |
'' as check_status, |
| 433 |
'' as check_disabled |
| 434 |
FROM " . $this->options . ' as ap |
| 435 |
WHERE option_name like \'easyazon_item_%\' |
| 436 |
'; |
| 437 |
} |
| 438 |
|
| 439 |
// ? Easy Affiliate Link plugin - EAL is having 2 types "HTML code" and "Text Link", we only get "text" value. |
| 440 |
if ( empty( $filter_plugin ) || $support_plugin[ Setting_Enum::AFFILIATE_URL_SLUG ] === $filter_plugin ) { |
| 441 |
$sql = $sql . " |
| 442 |
UNION |
| 443 |
|
| 444 |
SELECT |
| 445 |
po.ID as id, |
| 446 |
po.post_type, |
| 447 |
'Easy Affiliate Links' as import_source, |
| 448 |
CONVERT(po.post_name USING utf8) as post_name, |
| 449 |
CONVERT(po.post_title USING utf8) as post_title, |
| 450 |
'' as check_status, |
| 451 |
'' as check_disabled |
| 452 |
FROM " . $this->posts . ' as po |
| 453 |
INNER JOIN ' . $this->postmeta . ' as pom ON po.ID = pom.post_id |
| 454 |
WHERE po.post_type = "easy_affiliate_link" and pom.meta_value = "text" |
| 455 |
'; |
| 456 |
} |
| 457 |
|
| 458 |
if ( $include_imported ) { |
| 459 |
if ( in_array( |
| 460 |
$filter_plugin, |
| 461 |
array( |
| 462 |
$support_plugin[ Setting_Enum::PRETTY_LINK_SLUG ], |
| 463 |
$support_plugin[ Setting_Enum::THIRSTYLINK_SLUG ], |
| 464 |
$support_plugin[ Setting_Enum::EARNIST_SLUG ], |
| 465 |
$support_plugin[ Setting_Enum::AFFILIATE_URL_SLUG ], |
| 466 |
$support_plugin[ Setting_Enum::AAWP_SLUG ], |
| 467 |
$support_plugin[ Setting_Enum::EASY_AFFILIATE_LINK_SLUG ], |
| 468 |
), |
| 469 |
true |
| 470 |
) |
| 471 |
) { |
| 472 |
$r_plugin_where = "r.plugin IN ('%s')"; |
| 473 |
$r_plugin_where = sprintf( $r_plugin_where, $support_plugin_flip[ $filter_plugin ] ); |
| 474 |
} else { |
| 475 |
$r_plugin_where = "r.plugin IN ('%s', '%s', '%s', '%s', '%s', '%s')"; |
| 476 |
$r_plugin_where = sprintf( |
| 477 |
$r_plugin_where, |
| 478 |
Setting_Enum::PRETTY_LINK_SLUG, |
| 479 |
Setting_Enum::THIRSTYLINK_SLUG, |
| 480 |
Setting_Enum::EARNIST_SLUG, |
| 481 |
Setting_Enum::AFFILIATE_URL_SLUG, |
| 482 |
Setting_Enum::AAWP_SLUG, |
| 483 |
Setting_Enum::EASY_AFFILIATE_LINK_SLUG |
| 484 |
); |
| 485 |
} |
| 486 |
$sql = $sql . " |
| 487 |
UNION |
| 488 |
|
| 489 |
SELECT |
| 490 |
p.id, |
| 491 |
p.post_type, |
| 492 |
CASE |
| 493 |
WHEN r.plugin = 'pretty-link' |
| 494 |
THEN 'Pretty Links' |
| 495 |
WHEN r.plugin = 'thirstylink' |
| 496 |
THEN 'Thirsty Affiliates' |
| 497 |
WHEN r.plugin = 'earnist' |
| 498 |
THEN 'Earnist' |
| 499 |
WHEN r.plugin = 'affiliate_url' |
| 500 |
THEN 'Affiliate URLs' |
| 501 |
WHEN r.plugin = 'aawp' |
| 502 |
THEN 'AAWP' |
| 503 |
WHEN r.plugin = 'easyazon' |
| 504 |
THEN 'EasyAzon' |
| 505 |
WHEN r.plugin = 'amalinks' |
| 506 |
THEN 'Amalinks Pro' |
| 507 |
WHEN r.plugin = 'easy_affiliate_link' |
| 508 |
THEN 'Easy Affiliate Links' |
| 509 |
ELSE 'Unknown' |
| 510 |
END as import_source, |
| 511 |
CASE |
| 512 |
WHEN r.plugin = 'aawp' |
| 513 |
THEN CONVERT(r.old_uri USING utf8) |
| 514 |
ELSE CONVERT(p.post_name USING utf8) |
| 515 |
END as post_name, |
| 516 |
CONVERT(p.post_title USING utf8) as post_title, |
| 517 |
'checked' as check_status, |
| 518 |
'' as check_disabled |
| 519 |
FROM " . $this->posts . ' as p |
| 520 |
INNER JOIN |
| 521 |
' . $revert_table . " as r |
| 522 |
ON p.id = r.lasso_id |
| 523 |
WHERE |
| 524 |
$r_plugin_where |
| 525 |
"; |
| 526 |
|
| 527 |
// ? AmaLinks Pro plugin - imported |
| 528 |
if ( empty( $filter_plugin ) || $support_plugin[ Setting_Enum::AFFILIATE_URL_SLUG ] === $filter_plugin ) { |
| 529 |
$sql = $sql . " |
| 530 |
UNION |
| 531 |
|
| 532 |
SELECT DISTINCT |
| 533 |
CONVERT(r.lasso_id USING utf8) as id, |
| 534 |
'amalinkspro' as post_type, |
| 535 |
'AmaLinks Pro' as import_source, |
| 536 |
CASE |
| 537 |
WHEN CONVERT(la.monetized_url USING utf8) != '' |
| 538 |
THEN CONVERT(la.monetized_url USING utf8) |
| 539 |
WHEN CONVERT(wpp.guid USING utf8) != '' |
| 540 |
THEN CONVERT(wpp.guid USING utf8) |
| 541 |
ELSE '' |
| 542 |
END as post_name, |
| 543 |
CASE |
| 544 |
WHEN CONVERT(la.default_product_name USING utf8) != '' |
| 545 |
THEN CONVERT(la.default_product_name USING utf8) |
| 546 |
WHEN CONVERT(wpp.post_title USING utf8) != '' |
| 547 |
THEN CONVERT(wpp.post_title USING utf8) |
| 548 |
ELSE '' |
| 549 |
END as post_title, |
| 550 |
'checked' as check_status, |
| 551 |
'' as check_disabled |
| 552 |
FROM " . $revert_table . ' as r |
| 553 |
LEFT JOIN ' . $this->posts . ' as wpp |
| 554 |
ON r.lasso_id = wpp.ID |
| 555 |
LEFT JOIN ' . $this->amazon_products . " as la |
| 556 |
ON r.old_uri = la.amazon_id |
| 557 |
WHERE r.plugin = 'amalinkspro' |
| 558 |
"; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
$include_imported_where = $include_imported ? '' : 'AND check_status != "checked"'; |
| 563 |
|
| 564 |
$sql = ' |
| 565 |
SELECT ' . $select . ' |
| 566 |
FROM |
| 567 |
( |
| 568 |
' . $sql . ' |
| 569 |
) as BASE |
| 570 |
WHERE |
| 571 |
1=1 |
| 572 |
' . $where . ' |
| 573 |
' . $include_imported_where . ' |
| 574 |
' . $search . ' |
| 575 |
GROUP BY ' . $group_by . ' |
| 576 |
ORDER BY ' . $order_by; |
| 577 |
|
| 578 |
return $sql; |
| 579 |
} |
| 580 |
|
| 581 |
/** |
| 582 |
* Check whether pretty links pro plugin is install or not |
| 583 |
*/ |
| 584 |
public function is_pretty_links_pro_installed() { |
| 585 |
return Model::column_exists( $this->pretty_links, 'link_cpt_id' ); |
| 586 |
} |
| 587 |
|
| 588 |
/** |
| 589 |
* Check whether aawp plugin is installed or not |
| 590 |
*/ |
| 591 |
public function is_aawp_installed() { |
| 592 |
return Model::table_exists( $this->aawp ); |
| 593 |
} |
| 594 |
|
| 595 |
/** |
| 596 |
* Get pretty link by id |
| 597 |
* |
| 598 |
* @param int $id Id of pretty link. |
| 599 |
*/ |
| 600 |
public function get_pretty_link_by_id( $id ) { |
| 601 |
$sql = ' |
| 602 |
SELECT * |
| 603 |
FROM ' . $this->pretty_links . ' |
| 604 |
WHERE link_cpt_id = ' . $id . '; |
| 605 |
'; |
| 606 |
|
| 607 |
return $this->get_row( $sql ); |
| 608 |
} |
| 609 |
|
| 610 |
/** |
| 611 |
* Get aawp product |
| 612 |
* |
| 613 |
* @param string $product_id Amazon product id. |
| 614 |
*/ |
| 615 |
public function get_aawp_product( $product_id ) { |
| 616 |
$sql = ' |
| 617 |
SELECT * |
| 618 |
FROM ' . $this->aawp . " |
| 619 |
WHERE asin = '" . $product_id . "' |
| 620 |
"; |
| 621 |
|
| 622 |
$row = $this->get_row( $sql ); |
| 623 |
|
| 624 |
if ( $row ) { |
| 625 |
$lasso_amazon_api = new Amazon_Api(); |
| 626 |
|
| 627 |
$row_url = maybe_unserialize( $row->urls ?? $row->url ?? '' ); |
| 628 |
$url = $row_url['basic'] ?? $row_url; |
| 629 |
$url = is_string( $url ) ? $url : ''; |
| 630 |
|
| 631 |
$row->url = $lasso_amazon_api->get_amazon_product_url( $url, true, false ); |
| 632 |
|
| 633 |
$images = $row->image_ids; |
| 634 |
$images = explode( ',', $images ); |
| 635 |
$image_id = $images[0]; |
| 636 |
$image_url = 'https://m.media-amazon.com/images/I/' . $image_id . '.jpg'; |
| 637 |
|
| 638 |
$row->image_url = $image_url; |
| 639 |
$row->features = maybe_unserialize( $row->features ?? array() ); // phpcs:ignore |
| 640 |
} |
| 641 |
|
| 642 |
return $row; |
| 643 |
} |
| 644 |
|
| 645 |
/** |
| 646 |
* Get aawp lists |
| 647 |
* |
| 648 |
* @param string $id Id. |
| 649 |
*/ |
| 650 |
public function get_aawp_list( $id ) { |
| 651 |
$sql = ' |
| 652 |
SELECT * |
| 653 |
FROM ' . $this->aawp_list . ' |
| 654 |
WHERE id = ' . $id . ' |
| 655 |
'; |
| 656 |
|
| 657 |
return $this->get_row( $sql ); |
| 658 |
} |
| 659 |
|
| 660 |
/** |
| 661 |
* Check EasyAzon product id is imported or not |
| 662 |
* |
| 663 |
* @param string $asin Amazon product id. |
| 664 |
*/ |
| 665 |
public function is_easyazon_product_imported( $asin ) { |
| 666 |
global $wpdb; |
| 667 |
|
| 668 |
$sql = ' |
| 669 |
select id, lasso_id |
| 670 |
from ' . ( new Revert() )->get_table_name() . ' |
| 671 |
where old_uri = %s and plugin = \'easyazon\' |
| 672 |
'; |
| 673 |
$prepare = $wpdb->prepare( $sql, $asin ); // phpcs:ignore |
| 674 |
|
| 675 |
return $this->get_row( $prepare ); |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Get url detail by product id (Amazon/Extend product) |
| 680 |
* |
| 681 |
* @param string $product_id Product id. |
| 682 |
* @param string $product_type Product type. |
| 683 |
*/ |
| 684 |
public function get_url_details_by_product_id( $product_id, $product_type ) { |
| 685 |
if ( ! $product_id ) { |
| 686 |
return null; |
| 687 |
} |
| 688 |
|
| 689 |
global $wpdb; |
| 690 |
|
| 691 |
$sql = ' |
| 692 |
SELECT * |
| 693 |
FROM ' . ( new Url_Details() )->get_table_name() . ' as lud |
| 694 |
LEFT JOIN ' . $this->posts . ' as wpp |
| 695 |
ON lud.lasso_id = wpp.ID |
| 696 |
WHERE lud.product_id = %s |
| 697 |
AND wpp.ID is not null |
| 698 |
AND wpp.post_status = "publish" |
| 699 |
AND wpp.post_type = %s |
| 700 |
AND lud.product_type = %s |
| 701 |
ORDER BY lasso_id desc |
| 702 |
'; |
| 703 |
$prepare = $wpdb->prepare( $sql, $product_id, Constant::LASSO_POST_TYPE, $product_type ); // phpcs:ignore |
| 704 |
|
| 705 |
return $this->get_row( $prepare ); |
| 706 |
} |
| 707 |
|
| 708 |
/** |
| 709 |
* Process import |
| 710 |
* |
| 711 |
* @param int $id Post id. |
| 712 |
* @param string $slug Link slug. |
| 713 |
* @param string $old_uri Old URI. |
| 714 |
* @param string $plugin Plugin name. |
| 715 |
*/ |
| 716 |
public function process_import( $id, $slug, $old_uri, $plugin ) { |
| 717 |
if ( empty( $id ) || empty( $slug ) ) { |
| 718 |
return false; |
| 719 |
} |
| 720 |
|
| 721 |
global $wpdb; |
| 722 |
clean_post_cache( $id ); |
| 723 |
|
| 724 |
$result1 = true; |
| 725 |
if ( ! in_array( $plugin, array( 'aawp', 'amalinkspro', 'easyazon' ), true ) ) { |
| 726 |
// ? Flip post time and potentially the slug |
| 727 |
$update_sql = ' |
| 728 |
UPDATE ' . $this->posts . ' |
| 729 |
SET |
| 730 |
post_name = %s, |
| 731 |
post_type = %s, |
| 732 |
post_modified = NOW(), |
| 733 |
post_modified_gmt = NOW() |
| 734 |
WHERE id = %d; |
| 735 |
'; |
| 736 |
$update_sql = $wpdb->prepare( $update_sql, $slug, SIMPLE_URLS_SLUG, $id ); // phpcs:ignore |
| 737 |
Model::query( $update_sql ); |
| 738 |
$result1 = SIMPLE_URLS_SLUG === get_post_type( $id ); |
| 739 |
} |
| 740 |
|
| 741 |
// ? Log what we imported for potential reverts |
| 742 |
$insert_sql = ' |
| 743 |
INSERT INTO ' . ( new Revert() )->get_table_name() . ' (lasso_id, old_uri, plugin, revert_dt) |
| 744 |
VALUES (%d, %s, %s, NOW()); |
| 745 |
'; |
| 746 |
$prepare = Model::prepare( $insert_sql, $id, $old_uri, $plugin ); |
| 747 |
$result2 = Model::query( $prepare ); |
| 748 |
|
| 749 |
clean_post_cache( $id ); |
| 750 |
|
| 751 |
return $result1 && $result2; |
| 752 |
} |
| 753 |
|
| 754 |
/** |
| 755 |
* Process revert |
| 756 |
* |
| 757 |
* @param int $id Post id. |
| 758 |
* @param bool $custom_post_type It is custom post type or not. Default to true. |
| 759 |
*/ |
| 760 |
public function process_revert( $id, $custom_post_type = true ) { |
| 761 |
// ? Get post type from revert table |
| 762 |
$result1 = true; |
| 763 |
if ( empty( $id ) ) { |
| 764 |
return false; |
| 765 |
} |
| 766 |
|
| 767 |
if ( $custom_post_type ) { |
| 768 |
$revert_data = $this->get_revert_by_id( $id ); |
| 769 |
if ( ! empty( $revert_data ) ) { |
| 770 |
// ? Switch back |
| 771 |
if ( isset( $revert_data->plugin ) && 'pretty-link' === $revert_data->plugin ) { |
| 772 |
$pretty_link_data = $this->get_pretty_link_by_id( $id ); |
| 773 |
$update_sql = ' |
| 774 |
UPDATE ' . $this->posts . ' |
| 775 |
SET |
| 776 |
post_name = %s, |
| 777 |
post_type = %s |
| 778 |
WHERE id = %d; |
| 779 |
'; |
| 780 |
$update_sql = Model::prepare( $update_sql, $pretty_link_data->slug, $revert_data->plugin, $id ); |
| 781 |
} else { |
| 782 |
$update_sql = ' |
| 783 |
UPDATE ' . $this->posts . ' |
| 784 |
SET post_type = %s |
| 785 |
WHERE id = %d; |
| 786 |
'; |
| 787 |
$update_sql = Model::prepare( $update_sql, $revert_data->plugin, $id ); |
| 788 |
} |
| 789 |
$result1 = Model::query( $update_sql ); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
// ? Delete tracking record |
| 794 |
$delete_sql = ' |
| 795 |
DELETE FROM ' . ( new Revert() )->get_table_name() . ' |
| 796 |
WHERE lasso_id = %d; |
| 797 |
'; |
| 798 |
$delete_sql = Model::prepare( $delete_sql, $id ); |
| 799 |
$result2 = Model::query( $delete_sql ); |
| 800 |
|
| 801 |
clean_post_cache( $id ); |
| 802 |
|
| 803 |
return $result1 && $result2; |
| 804 |
} |
| 805 |
|
| 806 |
/** |
| 807 |
* Get revert by id |
| 808 |
* |
| 809 |
* @param int $id Id in revert table. |
| 810 |
*/ |
| 811 |
public function get_revert_by_id( $id ) { |
| 812 |
$sql = ' |
| 813 |
SELECT * |
| 814 |
FROM ' . ( new Revert() )->get_table_name() . ' |
| 815 |
WHERE lasso_id = %d; |
| 816 |
'; |
| 817 |
$sql = Model::prepare( $sql, $id ); |
| 818 |
|
| 819 |
return Model::get_row( $sql ); |
| 820 |
} |
| 821 |
|
| 822 |
/** |
| 823 |
* Delete url detail |
| 824 |
* |
| 825 |
* @param int $lasso_id Lasso post id. |
| 826 |
*/ |
| 827 |
public function delete_url_details( $lasso_id ) { |
| 828 |
$sql = ' |
| 829 |
DELETE FROM ' . ( new Url_Details() )->get_table_name() . ' |
| 830 |
WHERE lasso_id = %d'; |
| 831 |
$sql = Model::prepare( $sql, $lasso_id ); |
| 832 |
|
| 833 |
return Model::query( $sql ); |
| 834 |
} |
| 835 |
|
| 836 |
/** |
| 837 |
* Get EasyAzon product |
| 838 |
* |
| 839 |
* @param string $asin Amazon product id. |
| 840 |
*/ |
| 841 |
public function get_easyazon_product( $asin ) { |
| 842 |
if ( ! $asin ) { |
| 843 |
return null; |
| 844 |
} |
| 845 |
|
| 846 |
$search = 'easyazon_item_' . $asin . '%'; |
| 847 |
$sql = ' |
| 848 |
select option_value |
| 849 |
from ' . $this->options . ' |
| 850 |
where option_name like %s |
| 851 |
order by option_id desc |
| 852 |
limit 1 |
| 853 |
'; |
| 854 |
$prepare = Model::prepare( $sql, $search ); // phpcs:ignore |
| 855 |
|
| 856 |
return Model::get_row( $prepare ); |
| 857 |
} |
| 858 |
|
| 859 |
/** |
| 860 |
* Remove: all background processes of Lasso |
| 861 |
*/ |
| 862 |
public function remove_all_lasso_processes() { |
| 863 |
$sql = ' |
| 864 |
DELETE FROM ' . $this->options . " |
| 865 |
WHERE option_name like '%lassolite_%_batch_%' |
| 866 |
"; |
| 867 |
$this->query( $sql ); |
| 868 |
} |
| 869 |
|
| 870 |
/** |
| 871 |
* Check whether the process is empty |
| 872 |
*/ |
| 873 |
public function check_empty_process() { |
| 874 |
$count_query = ' |
| 875 |
SELECT count(option_id) as `total` |
| 876 |
FROM ' . $this->options . " |
| 877 |
WHERE |
| 878 |
option_name LIKE '%lassolite_%_batch_%' |
| 879 |
AND option_value LIKE 'a:1:{i:0;i:%' |
| 880 |
"; |
| 881 |
$total = $this->get_row( $count_query ); |
| 882 |
$total = $total->total ?? 0; |
| 883 |
$total = intval( $total ); |
| 884 |
|
| 885 |
// ? delete empty processes if there are more 10 empty processes |
| 886 |
if ( $total > 10 ) { |
| 887 |
$this->remove_empty_process(); |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
/** |
| 892 |
* Remove: empty process |
| 893 |
*/ |
| 894 |
public function remove_empty_process() { |
| 895 |
$sql = ' |
| 896 |
DELETE FROM ' . $this->options . " |
| 897 |
WHERE |
| 898 |
option_name LIKE '%lassolite_%_batch_%' |
| 899 |
AND option_value LIKE 'a:1:{i:0;i:%' |
| 900 |
"; |
| 901 |
$this->query( $sql ); |
| 902 |
} |
| 903 |
|
| 904 |
/** |
| 905 |
* Paginate items by a sql query |
| 906 |
* |
| 907 |
* @param string $sql Sql query. |
| 908 |
* @param int $page Number of page. |
| 909 |
* @param int $limit Number of results. Default to 10. |
| 910 |
*/ |
| 911 |
public function paginate( $sql, $page, $limit = 10 ) { |
| 912 |
$start_index = ( $page - 1 ) * $limit; |
| 913 |
return $sql . ' LIMIT ' . $start_index . ', ' . $limit; |
| 914 |
} |
| 915 |
|
| 916 |
/** |
| 917 |
* Get revertable url query |
| 918 |
* |
| 919 |
* @param string $filter_plugin Plugin name. Default to null (all plugins). |
| 920 |
*/ |
| 921 |
public function get_revertable_urls_query( $filter_plugin = null ) { |
| 922 |
$key = array_search( $filter_plugin, Setting_Enum::SUPPORT_IMPORT_PLUGINS, true ); |
| 923 |
|
| 924 |
if ( false === $key ) { |
| 925 |
$where = "`plugin` IN ('pretty-link', 'thirstylink', 'earnist', 'affiliate_url', 'aawp', 'easyazon', 'amalinkspro')"; |
| 926 |
} else { |
| 927 |
$where = '`plugin` = %s'; |
| 928 |
$where = Model::prepare( $where, $key ); |
| 929 |
} |
| 930 |
|
| 931 |
$sql = ' |
| 932 |
SELECT lasso_id AS import_id, `plugin` AS import_source |
| 933 |
FROM ' . ( new Revert() )->get_table_name() . ' |
| 934 |
WHERE ' . $where . ' |
| 935 |
'; |
| 936 |
|
| 937 |
return $sql; |
| 938 |
} |
| 939 |
|
| 940 |
/** |
| 941 |
* Get import plugin options to filter |
| 942 |
* |
| 943 |
* @param bool $get_count Get result count. |
| 944 |
* @return array|int|mixed |
| 945 |
*/ |
| 946 |
public function get_import_plugins( $get_count = false ) { |
| 947 |
$sql = $this->get_importable_urls_query( true, '', 'BASE.import_source' ); |
| 948 |
|
| 949 |
if ( $get_count ) { |
| 950 |
return Model::get_count( $sql ); |
| 951 |
} |
| 952 |
|
| 953 |
$import_results = Model::get_results( $sql ); |
| 954 |
|
| 955 |
$result = array(); |
| 956 |
|
| 957 |
foreach ( $import_results as $import_result ) { |
| 958 |
$result[] = $import_result->import_source; |
| 959 |
} |
| 960 |
|
| 961 |
return $result; |
| 962 |
} |
| 963 |
|
| 964 |
} |
| 965 |
|