PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 156
Lasso Lite – Affiliate Link Manager & Product Displays v156
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-group.php

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

220 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\Helper;
12 use LassoLite\Models\Model;
13
14 /**
15 * Model
16 */
17 class Group extends LL_Object {
18
19 /**
20 * Get groups query
21 *
22 * @param int $page Page number.
23 * @param string $search Search text.
24 * @param string $where Where statement. Default to '1=1'.
25 * @param string $limit Limit.
26 * @return Group[]
27 */
28 public static function get_list( $page = null, $search = '', $where = '1=1', $limit = Enum::LIMIT_ON_PAGE ) {
29
30 $terms = Model::get_wpdb()->terms;
31 $term_taxonomy = Model::get_wpdb()->term_taxonomy;
32 $term_relationships = Model::get_wpdb()->term_relationships;
33 $posts = Model::get_wpdb()->posts;
34 if ( ! empty( $search ) ) {
35 $search_body = Helper::esc_like_query( $search );
36 $search = Model::get_wpdb()->prepare( 'AND t.name LIKE %s', $search_body );
37 }
38 $sql = "
39 SELECT
40 t.term_id as `post_id`,
41 t.term_id,
42 t.name as post_title,
43 t.slug,
44 CASE
45 WHEN LENGTH(tt.description) > 237
46 THEN CONCAT(SUBSTRING(tt.description, 1, 237), '...')
47 ELSE tt.description
48 END as description,
49 SUM(
50 CASE
51 WHEN p.ID IS NOT NULL
52 THEN 1
53 ELSE 0
54 END
55 ) as `count`
56 FROM
57 " . $terms . ' as t
58 INNER JOIN
59 ' . $term_taxonomy . " as tt
60 ON t.term_id = tt.term_id
61 AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "'
62 LEFT JOIN
63 " . $term_relationships . ' as tr
64 ON tt.term_taxonomy_id = tr.term_taxonomy_id
65 LEFT JOIN
66 ' . $posts . " as p
67 ON tr.object_id = p.ID
68 AND p.post_type = '" . Constant::LASSO_POST_TYPE . "'
69 AND p.post_status = 'publish'
70 WHERE
71 " . $where . '
72 ' . $search . '
73 GROUP BY
74 t.term_id,
75 t.name,
76 t.slug
77 ';
78
79 if ( ! is_null( $page ) ) {
80 $sql = Helper::paginate( $sql, $page, $limit );
81 }
82
83 $results = Model::get_results( $sql );
84 $list = array();
85 foreach ( $results as $result ) {
86 $list[] = new self( $result );
87 }
88 return $list;
89 }
90
91 /**
92 * Get groups query
93 *
94 * @param string $search Search text.
95 * @param string $where Where statement. Default to '1=1'.
96 * @return Group[]
97 */
98 public static function total( $search = '', $where = '1=1' ) {
99
100 $terms = Model::get_wpdb()->terms;
101 $term_taxonomy = Model::get_wpdb()->term_taxonomy;
102 $term_relationships = Model::get_wpdb()->term_relationships;
103 $posts = Model::get_wpdb()->posts;
104 if ( ! empty( $search ) ) {
105 $search = Helper::esc_like_query( $search );
106 $search = Model::get_wpdb()->prepare( 'AND t.name LIKE %s', $search );
107 }
108 $sql = "
109 SELECT
110 t.term_id as `post_id`,
111 t.term_id,
112 t.name as post_title,
113 t.slug,
114 CASE
115 WHEN LENGTH(tt.description) > 237
116 THEN CONCAT(SUBSTRING(tt.description, 1, 237), '...')
117 ELSE tt.description
118 END as description,
119 SUM(
120 CASE
121 WHEN p.ID IS NOT NULL
122 THEN 1
123 ELSE 0
124 END
125 ) as `count`
126 FROM
127 " . $terms . ' as t
128 INNER JOIN
129 ' . $term_taxonomy . " as tt
130 ON t.term_id = tt.term_id
131 AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "'
132 LEFT JOIN
133 " . $term_relationships . ' as tr
134 ON tt.term_taxonomy_id = tr.term_taxonomy_id
135 LEFT JOIN
136 ' . $posts . " as p
137 ON tr.object_id = p.ID
138 AND p.post_type = '" . Constant::LASSO_POST_TYPE . "'
139 AND p.post_status = 'publish'
140 WHERE
141 " . $where . '
142 ' . $search . '
143 GROUP BY
144 t.term_id,
145 t.name,
146 t.slug
147 ';
148
149 return Model::get_count( $sql );
150 }
151
152 /**
153 * Get edit URL
154 */
155 public function get_link_detail() {
156 return Page::get_page_url( Helper::add_prefix_page( Enum::PAGE_GROUP_DETAIL ) . '&post_id=' . $this->get_post_id() ) . '&subpage=' . Enum::SUB_PAGE_GROUP_URLS;
157 }
158
159 /**
160 * Get by ID
161 *
162 * @param int $id ID.
163 *
164 * @return Group|null
165 */
166 public static function get_by_id( $id ) {
167 $term_data = get_term( $id, Constant::LASSO_CATEGORY );
168 if ( ! empty( $term_data ) && ! is_wp_error( $term_data ) ) {
169 return new self( $term_data );
170 }
171 return null;
172 }
173
174 /**
175 * Get group id
176 *
177 * @return int
178 */
179 public function get_id() {
180 return (int) $this->get_term_id();
181 }
182
183 /**
184 * Get total links of group
185 *
186 * @return int|mixed
187 */
188 public function get_total_links() {
189 $terms = Model::get_wpdb()->terms;
190 $term_taxonomy = Model::get_wpdb()->term_taxonomy;
191 $term_relationships = Model::get_wpdb()->term_relationships;
192 $posts = Model::get_wpdb()->posts;
193
194 $sql = '
195 SELECT
196 count(*) as `count`
197 FROM
198 ' . $terms . ' as t
199 INNER JOIN
200 ' . $term_taxonomy . " as tt
201 ON t.term_id = tt.term_id
202 AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "'
203 LEFT JOIN
204 " . $term_relationships . ' as tr
205 ON tt.term_taxonomy_id = tr.term_taxonomy_id
206 INNER JOIN
207 ' . $posts . " as p
208 ON tr.object_id = p.ID
209 WHERE
210 t.term_id = %d
211 AND t.slug = %s
212 AND p.post_type = '" . Constant::LASSO_POST_TYPE . "'
213 AND p.post_status = 'publish'
214 AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "'";
215 $sql = Model::prepare( $sql, $this->get_id(), $this->get_slug() );
216 $result = Model::get_row( $sql );
217 return $result->count;
218 }
219 }
220