| 1 |
<?php |
| 2 |
/** |
| 3 |
* Models |
| 4 |
* |
| 5 |
* @package Models |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace LassoLite\Classes; |
| 9 |
|
| 10 |
use LassoLite\Admin\Constant; |
| 11 |
use LassoLite\Classes\Amazon_Api; |
| 12 |
use LassoLite\Classes\Meta_Enum; |
| 13 |
use LassoLite\Models\Model; |
| 14 |
use WP_Query; |
| 15 |
|
| 16 |
/** |
| 17 |
* Model |
| 18 |
*/ |
| 19 |
class SURL extends LL_Object { |
| 20 |
/** |
| 21 |
* Get permalink |
| 22 |
*/ |
| 23 |
public function get_permalink() { |
| 24 |
return esc_url( get_permalink( $this->get_id() ) ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get edit URL |
| 29 |
*/ |
| 30 |
public function get_link_detail() { |
| 31 |
return get_edit_post_link( $this->get_id() ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get thumbnail URL |
| 36 |
*/ |
| 37 |
public function get_thumbnail_url() { |
| 38 |
$img_src = get_post_meta( $this->get_id(), Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, true ); |
| 39 |
|
| 40 |
return ( ! empty( $img_src ) ) ? $img_src : Constant::DEFAULT_THUMBNAIL; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get redirect URL |
| 45 |
*/ |
| 46 |
public function get_redirect_url() { |
| 47 |
$redirect = get_post_meta( $this->get_id(), Meta_Enum::SURL_REDIRECT, true ); |
| 48 |
$redirect = Helper::has_protocol( $redirect ) ? $redirect : '#'; |
| 49 |
|
| 50 |
if ( Amazon_Api::is_amazon_url( $redirect ) ) { |
| 51 |
$redirect = Amazon_Api::get_amazon_product_url( $redirect ); |
| 52 |
} |
| 53 |
|
| 54 |
return $redirect; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get public URL |
| 59 |
*/ |
| 60 |
public function get_public_url() { |
| 61 |
$redirect = $this->get_redirect_url(); |
| 62 |
if ( Amazon_Api::is_amazon_url( $redirect ) ) { |
| 63 |
return $redirect; |
| 64 |
} |
| 65 |
|
| 66 |
return $this->get_permalink(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get clicks |
| 71 |
*/ |
| 72 |
public function get_clicks() { |
| 73 |
$clicks = get_post_meta( $this->get_id(), Meta_Enum::SURL_COUNT, true ); |
| 74 |
$clicks = ! empty( $clicks ) ? $clicks : 0; |
| 75 |
|
| 76 |
return $clicks; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get list SURL object |
| 81 |
* |
| 82 |
* @param string $keyword Keyword. |
| 83 |
* @param int $page Page name. |
| 84 |
* @param int $limit Limit items. |
| 85 |
* @return SURL[] |
| 86 |
*/ |
| 87 |
public static function get_list( $keyword = '', $page = 1, $limit = Enum::LIMIT_ON_PAGE ) { |
| 88 |
if ( 1 === $page ) { |
| 89 |
$offset = 0; |
| 90 |
} else { |
| 91 |
$offset = ( $page * $limit ) - $limit; |
| 92 |
} |
| 93 |
$surls = get_posts( |
| 94 |
array( |
| 95 |
'posts_per_page' => $limit, |
| 96 |
'offset' => $offset, |
| 97 |
'post_type' => SIMPLE_URLS_SLUG, |
| 98 |
'post_status' => 'publish', |
| 99 |
's' => $keyword, |
| 100 |
'orderby' => 'post_modified', |
| 101 |
'order' => 'DESC', |
| 102 |
) |
| 103 |
); |
| 104 |
$list = array(); |
| 105 |
foreach ( $surls as $surl ) { |
| 106 |
$surl->id = $surl->ID; |
| 107 |
$list[] = new self( $surl ); |
| 108 |
} |
| 109 |
return $list; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get total |
| 114 |
* |
| 115 |
* @param string $keyword Keyword. |
| 116 |
* @return integer |
| 117 |
*/ |
| 118 |
public static function total( $keyword = '' ) { |
| 119 |
if ( empty( $keyword ) ) { |
| 120 |
return (int) wp_count_posts( SIMPLE_URLS_SLUG )->publish; |
| 121 |
} |
| 122 |
$args = array( |
| 123 |
'post_status' => 'publish', |
| 124 |
'post_type' => SIMPLE_URLS_SLUG, |
| 125 |
's' => $keyword, |
| 126 |
); |
| 127 |
$the_query = new WP_Query( $args ); |
| 128 |
return $the_query->post_count; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get urls by group |
| 133 |
* |
| 134 |
* @param Group $group Group object. |
| 135 |
* |
| 136 |
* @return array |
| 137 |
*/ |
| 138 |
public static function get_urls_by_group( $group ) { |
| 139 |
$terms = Model::get_wpdb()->terms; |
| 140 |
$term_taxonomy = Model::get_wpdb()->term_taxonomy; |
| 141 |
$term_relationships = Model::get_wpdb()->term_relationships; |
| 142 |
$posts = Model::get_wpdb()->posts; |
| 143 |
|
| 144 |
$sql = ' |
| 145 |
SELECT |
| 146 |
t.term_id, |
| 147 |
t.name as term_name, |
| 148 |
t.slug as term_slug, |
| 149 |
t.term_group, |
| 150 |
p.ID, |
| 151 |
p.post_title, |
| 152 |
p.post_name |
| 153 |
FROM |
| 154 |
' . $terms . ' as t |
| 155 |
INNER JOIN |
| 156 |
' . $term_taxonomy . " as tt |
| 157 |
ON t.term_id = tt.term_id |
| 158 |
AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "' |
| 159 |
LEFT JOIN |
| 160 |
" . $term_relationships . ' as tr |
| 161 |
ON tt.term_taxonomy_id = tr.term_taxonomy_id |
| 162 |
INNER JOIN |
| 163 |
' . $posts . " as p |
| 164 |
ON tr.object_id = p.ID |
| 165 |
WHERE |
| 166 |
t.term_id = %d |
| 167 |
AND t.slug = %s |
| 168 |
AND p.post_type = '" . Constant::LASSO_POST_TYPE . "' |
| 169 |
AND p.post_status = 'publish' |
| 170 |
AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "'"; |
| 171 |
$sql = Model::prepare( $sql, $group->get_id(), $group->get_slug() ); |
| 172 |
|
| 173 |
$results = Model::get_results( $sql ); |
| 174 |
$list = array(); |
| 175 |
foreach ( $results as $result ) { |
| 176 |
$result->id = $result->ID; |
| 177 |
$list[] = new self( $result ); |
| 178 |
} |
| 179 |
return $list; |
| 180 |
} |
| 181 |
} |
| 182 |
|