PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 157
Lasso Lite – Affiliate Link Manager & Product Displays v157
157 155 156 154 153 152 151 150 149 148 trunk 0.9.9 104 105 106 107 108 109 110 111 112 113 114 115 116 All 56 releases
simple-urls / classes / class-surl.php

class-surl.php in Lasso Lite – Affiliate Link Manager & Product Displays 157, at classes/class-surl.php

227 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Helper;
13 use LassoLite\Classes\Meta_Enum;
14 use LassoLite\Models\Model;
15
16 use Lasso\Models\Revert;
17 use WP_Query;
18
19 /**
20 * Model
21 */
22 class SURL extends LL_Object {
23 /**
24 * Get permalink
25 */
26 public function get_permalink() {
27 return esc_url( get_permalink( $this->get_id() ) );
28 }
29
30 /**
31 * Get edit URL
32 */
33 public function get_link_detail() {
34 return get_edit_post_link( $this->get_id() );
35 }
36
37 /**
38 * Get thumbnail URL
39 */
40 public function get_thumbnail_url() {
41 $img_src = get_post_meta( $this->get_id(), Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, true );
42
43 $public_url = $this->get_public_url();
44 $is_amazon_page = Amazon_Api::is_amazon_url( $public_url );
45 if ( $is_amazon_page && ( Constant::DEFAULT_THUMBNAIL === $img_src || ! $img_src ) ) {
46 $product_id = Amazon_Api::get_product_id_by_url( $public_url );
47 if ( $product_id ) {
48 $lasso_amazon_api = new Amazon_Api();
49 $product = $lasso_amazon_api->get_amazon_product_from_db( $product_id );
50 if ( $product ) {
51 $img_src = $product['default_image'];
52 }
53 }
54 }
55
56 return ( ! empty( $img_src ) ) ? $img_src : Constant::DEFAULT_THUMBNAIL;
57 }
58
59 /**
60 * Get redirect URL
61 */
62 public function get_redirect_url() {
63 $redirect = get_post_meta( $this->get_id(), Meta_Enum::SURL_REDIRECT, true );
64 $redirect = Helper::has_protocol( $redirect ) ? $redirect : '#';
65
66 if ( Amazon_Api::is_amazon_url( $redirect ) ) {
67 $redirect = Amazon_Api::get_amazon_product_url( $redirect );
68 }
69
70 return $redirect;
71 }
72
73 /**
74 * Get public URL
75 */
76 public function get_public_url() {
77 $redirect = $this->get_redirect_url();
78 if ( Amazon_Api::is_amazon_url( $redirect ) ) {
79 return $redirect;
80 }
81
82 return $this->get_permalink();
83 }
84
85 /**
86 * Get clicks
87 */
88 public function get_clicks() {
89 $clicks = get_post_meta( $this->get_id(), Meta_Enum::SURL_COUNT, true );
90 $clicks = ! empty( $clicks ) ? $clicks : 0;
91
92 return $clicks;
93 }
94
95 /**
96 * Get dashboard SQL query
97 *
98 * @param string $keyword Keyword.
99 */
100 private static function get_dashboard_query( $keyword ) {
101 $keyword = '%' . Model::esc_like( $keyword ) . '%';
102 $where = '';
103
104 if ( Helper::is_lasso_pro_installed() && class_exists( 'Lasso\Models\Revert' ) ) {
105 $revert = new Revert();
106 $where = '
107 AND ID NOT IN (
108 SELECT r.post_data
109 FROM ' . $revert->get_table_name() . ' AS r
110 INNER JOIN ' . Model::get_wp_table_name( 'posts' ) . ' AS p
111 ON p.ID = r.post_data
112 WHERE r.plugin = "' . SIMPLE_URLS_SLUG . '"
113 AND p.post_type = "' . Constant::LASSO_PRO_POST_TYPE . '"
114 )
115 ';
116 }
117
118 $sql = '
119 SELECT *
120 FROM
121 ' . Model::get_wp_table_name( 'posts' ) . '
122 WHERE
123 post_type = %s
124 AND post_status = %s
125 AND post_title LIKE %s
126 ' . $where . '
127 ORDER BY post_modified DESC
128 ';
129 $sql = Model::prepare( $sql, SIMPLE_URLS_SLUG, 'publish', $keyword );
130
131 return $sql;
132 }
133
134 /**
135 * Get list SURL object
136 *
137 * @param string $keyword Keyword.
138 * @param int $page Page name.
139 * @param int $limit Limit items.
140 * @return SURL[]
141 */
142 public static function get_list( $keyword = '', $page = 1, $limit = Enum::LIMIT_ON_PAGE ) {
143 if ( 1 === $page ) {
144 $offset = 0;
145 } else {
146 $offset = ( $page * $limit ) - $limit;
147 }
148
149 $sql = self::get_dashboard_query( $keyword );
150 $sql = $sql . ' LIMIT %d OFFSET %d';
151 $sql = Model::prepare( $sql, $limit, $offset );
152
153 $surls = Model::get_results( $sql );
154
155 $list = array();
156 foreach ( $surls as $surl ) {
157 $surl->id = $surl->ID;
158 $list[] = new self( $surl );
159 }
160 return $list;
161 }
162
163 /**
164 * Get total
165 *
166 * @param string $keyword Keyword.
167 * @return integer
168 */
169 public static function total( $keyword = '' ) {
170 $sql = self::get_dashboard_query( $keyword );
171 $total = Model::get_count( $sql );
172
173 return $total;
174 }
175
176 /**
177 * Get urls by group
178 *
179 * @param Group $group Group object.
180 *
181 * @return array
182 */
183 public static function get_urls_by_group( $group ) {
184 $terms = Model::get_wpdb()->terms;
185 $term_taxonomy = Model::get_wpdb()->term_taxonomy;
186 $term_relationships = Model::get_wpdb()->term_relationships;
187 $posts = Model::get_wpdb()->posts;
188
189 $sql = '
190 SELECT
191 t.term_id,
192 t.name as term_name,
193 t.slug as term_slug,
194 t.term_group,
195 p.ID,
196 p.post_title,
197 p.post_name
198 FROM
199 ' . $terms . ' as t
200 INNER JOIN
201 ' . $term_taxonomy . " as tt
202 ON t.term_id = tt.term_id
203 AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "'
204 LEFT JOIN
205 " . $term_relationships . ' as tr
206 ON tt.term_taxonomy_id = tr.term_taxonomy_id
207 INNER JOIN
208 ' . $posts . " as p
209 ON tr.object_id = p.ID
210 WHERE
211 t.term_id = %d
212 AND t.slug = %s
213 AND p.post_type = '" . Constant::LASSO_POST_TYPE . "'
214 AND p.post_status = 'publish'
215 AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "'";
216 $sql = Model::prepare( $sql, $group->get_id(), $group->get_slug() );
217
218 $results = Model::get_results( $sql );
219 $list = array();
220 foreach ( $results as $result ) {
221 $result->id = $result->ID;
222 $list[] = new self( $result );
223 }
224 return $list;
225 }
226 }
227