| 1 |
<?php |
| 2 |
/** |
| 3 |
* Models |
| 4 |
* |
| 5 |
* @package Models |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace LassoLite\Models; |
| 9 |
|
| 10 |
use LassoLite\Admin\Constant; |
| 11 |
use LassoLite\Classes\Amazon_Api; |
| 12 |
use LassoLite\Classes\Meta_Enum; |
| 13 |
|
| 14 |
/** |
| 15 |
* Model |
| 16 |
*/ |
| 17 |
class Url_Details extends Model { |
| 18 |
|
| 19 |
const META_KEY_URL_WITHOUT_ARGUMENTS = 'url_without_arguments'; |
| 20 |
/** |
| 21 |
* Table name |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $table = 'lasso_lite_url_details'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Columns of the table |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected $columns = array( |
| 33 |
'lasso_id', |
| 34 |
'redirect_url', |
| 35 |
'base_domain', |
| 36 |
'is_opportunity', |
| 37 |
'product_id', |
| 38 |
|
| 39 |
'product_type', |
| 40 |
); |
| 41 |
|
| 42 |
/** |
| 43 |
* Primary key of the table |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
protected $primary_key = 'lasso_id'; |
| 48 |
|
| 49 |
/** |
| 50 |
* Create table |
| 51 |
*/ |
| 52 |
public function create_table() { |
| 53 |
$columns_sql = ' |
| 54 |
lasso_id bigint UNSIGNED NOT NULL, |
| 55 |
redirect_url longtext NOT NULL, |
| 56 |
base_domain varchar(128) NOT NULL, |
| 57 |
is_opportunity tinyint NOT NULL DEFAULT 1, |
| 58 |
product_id varchar(150), |
| 59 |
product_type varchar(20), |
| 60 |
PRIMARY KEY (lasso_id), |
| 61 |
KEY ix_base_domain (base_domain) |
| 62 |
'; |
| 63 |
$sql = ' |
| 64 |
CREATE TABLE ' . $this->get_table_name() . ' ( |
| 65 |
' . $columns_sql . ' |
| 66 |
) ' . $this->get_charset_collate(); |
| 67 |
|
| 68 |
return $this->modify_table( $sql, $this->get_table_name() ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get lasso url detail object by product id and product_type |
| 73 |
* |
| 74 |
* @param string $product_id Product id. |
| 75 |
* @param string $product_type Product type. Default is amazon. |
| 76 |
*/ |
| 77 |
public static function get_by_product_id_and_type( $product_id, $product_type = Amazon_Api::PRODUCT_TYPE ) { |
| 78 |
if ( ! $product_id ) { |
| 79 |
return null; |
| 80 |
} |
| 81 |
|
| 82 |
$sql = ' |
| 83 |
SELECT lud.* |
| 84 |
FROM ' . self::get_wp_table_name( 'posts' ) . ' AS wpp |
| 85 |
LEFT JOIN ' . ( new self() )->get_table_name() . ' AS lud |
| 86 |
ON wpp.id = lud.lasso_id |
| 87 |
WHERE wpp.post_type = %s |
| 88 |
AND lud.product_id = %s |
| 89 |
AND lud.product_type = %s |
| 90 |
AND wpp.post_status = "publish" |
| 91 |
'; |
| 92 |
|
| 93 |
$prepare = self::prepare( $sql, Constant::LASSO_POST_TYPE, $product_id, $product_type ); // phpcs:ignore |
| 94 |
$result = self::get_row( $prepare ); |
| 95 |
|
| 96 |
if ( $result ) { |
| 97 |
return ( new self() )->map_properties( $result ); |
| 98 |
} |
| 99 |
|
| 100 |
return null; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get amazon shortlink by base domain amzn.to |
| 105 |
* |
| 106 |
* @param int $limit Limit. Default is 10. |
| 107 |
*/ |
| 108 |
public static function get_amzn_to_shortlinks( $limit = 10 ) { |
| 109 |
$sql = ' |
| 110 |
SELECT DISTINCT lud.lasso_id |
| 111 |
FROM ' . ( new self() )->get_table_name() . ' AS lud |
| 112 |
INNER JOIN ' . self::get_wp_table_name( 'posts' ) . ' AS wp |
| 113 |
ON wp.ID = lud.lasso_id |
| 114 |
LEFT JOIN ' . self::get_wp_table_name( 'postmeta' ) . ' AS pmeta |
| 115 |
ON wp.ID = pmeta.post_id |
| 116 |
WHERE ( |
| 117 |
lud.base_domain = %s |
| 118 |
AND wp.post_type = %s |
| 119 |
AND wp.post_status = "publish" |
| 120 |
) |
| 121 |
OR ( |
| 122 |
pmeta.meta_key = %s |
| 123 |
AND pmeta.meta_value LIKE %s |
| 124 |
) |
| 125 |
ORDER BY wp.post_modified_gmt DESC |
| 126 |
LIMIT %d |
| 127 |
'; |
| 128 |
$prepare = self::prepare( $sql, 'amzn.to', Constant::LASSO_POST_TYPE, Meta_Enum::SURL_REDIRECT, '%amzn.to%', $limit ); // phpcs:ignore |
| 129 |
$result = self::get_results( $prepare ); |
| 130 |
|
| 131 |
return $result; |
| 132 |
} |
| 133 |
} |
| 134 |
|