PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.0.69
OttoKit: All-in-One Automation Platform v1.0.69
1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Models / Model.php
suretriggers / src / Models Last commit date
Model.php 3 years ago SaasApiToken.php 1 year ago Utilities.php 2 years ago
Model.php
335 lines
1 <?php
2 /**
3 * Base Modal class.
4 * php version 5.6
5 *
6 * @category Model
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Models;
15
16 use ReflectionClass;
17 use wpdb;
18
19 /**
20 * Responsible for interacting with the database.
21 *
22 * Class Model
23 *
24 * @package SureTriggers\Model
25 * @psalm-consistent-constructor
26 */
27 abstract class Model {
28
29 /**
30 * Table name.
31 *
32 * @var string
33 */
34 public $table;
35
36 /**
37 * Join table name.
38 *
39 * @var string
40 */
41 public $join_table;
42
43 /**
44 * Select
45 *
46 * @var string
47 */
48 public $select;
49
50 /**
51 * Query output.
52 *
53 * @var string
54 */
55 public $output = OBJECT;
56
57 /**
58 * Total results count
59 *
60 * @var int
61 */
62 public $total;
63
64 /**
65 * Start from
66 *
67 * @var int
68 */
69 public $start;
70
71 /**
72 * Per page results count
73 *
74 * @var int
75 */
76 public $per_page;
77
78 /**
79 * Pagination Properties
80 */
81
82 /**
83 * Search term
84 *
85 * @var string
86 */
87 public $search_term;
88
89 /**
90 * Current page of the pagination
91 *
92 * @var int
93 */
94 public $current_page;
95
96 /**
97 * Which column should be order
98 *
99 * @var string
100 */
101 public $order_by_col;
102
103 /**
104 * ASC|DESC
105 *
106 * @var string
107 */
108 public $order_by = 'DESC';
109
110 /**
111 * Order by Sql query
112 *
113 * @var string
114 */
115 public $order_by_sql;
116
117 /**
118 * Primary ID.
119 *
120 * @var string
121 */
122 protected $primary_id = 'id';
123
124 /**
125 * Database.
126 *
127 * @var wpdb
128 */
129 protected $db;
130
131 /**
132 * It will generate Where SQL
133 *
134 * @var array
135 */
136 protected $where = [];
137
138 /**
139 * Constructor
140 *
141 * @since 1.0.0
142 */
143 public function __construct() {
144 global $wpdb;
145
146 $this->db = $wpdb;
147
148 if ( ! empty( $this->table ) ) {
149 $this->table = $wpdb->prefix . $this->table;
150 } else {
151 $this->table = $wpdb->prefix . strtolower( ( new ReflectionClass( $this ) )->getShortName() );
152 }
153
154 $this->total = 0;
155 $this->start = 0;
156 $this->per_page = 0;
157 $this->search_term = '';
158 $this->current_page = 1;
159
160 $this->order_by_col = $this->primary_id;
161 $this->order_by_sql = " ORDER BY {$this->order_by_col} {$this->order_by} ";
162 }
163
164 /**
165 * Initialize query.
166 *
167 * @param string $output output.
168 * @return static|null
169 */
170 public static function init( $output = OBJECT ) {
171 $_instance = new static();
172 $_instance->output = $output;
173
174 return $_instance;
175 }
176
177 /**
178 * Get all results.
179 *
180 * @return array|object|void|null
181 */
182 public function all() {
183 return $this->db->get_results( "SELECT * FROM {$this->table}", $this->output );
184 }
185
186 /**
187 * Prepare where query on given array.
188 *
189 * @param array $data where data.
190 * @return $this
191 */
192 public function where( $data ) {
193 foreach ( $data as $key => $value ) {
194 $this->where[] = "AND {$key} = '{$value}'";
195 }
196 return $this;
197 }
198
199 /**
200 * Get the table name.
201 *
202 * @return string
203 */
204 public function table() {
205 return $this->table;
206 }
207
208 /**
209 * Add selections.
210 *
211 * @param string $select select string.
212 * @return $this
213 */
214 public function select( $select ) {
215 $this->select = $select;
216 return $this;
217 }
218
219 /**
220 * Get specific results.
221 *
222 * @return array|object|void|null
223 */
224 public function get() {
225 return $this->db->get_results( "SELECT {$this->get_select()} FROM {$this->table} {$this->get_left_join()} {$this->get_where_sql()} ", $this->output );
226 }
227
228 /**
229 * Prepare WHERE conditions for sql.
230 *
231 * @return string
232 */
233 public function get_where_sql() {
234 $where_clause = '';
235 if ( count( $this->where ) ) {
236 $where_clause .= implode( ' ', $this->where );
237 }
238
239 return " WHERE 1 = 1 {$where_clause} ";
240 }
241
242 /**
243 * Get var from the db.
244 *
245 * @return string|null|int
246 */
247 public function get_var() {
248 return $this->db->get_var( "SELECT {$this->get_select()} FROM {$this->table} {$this->get_left_join()} {$this->get_where_sql()} " );
249 }
250
251 /**
252 * Find the result by id.
253 *
254 * @param int $id id.
255 * @return array|object|void|null
256 */
257 public function find( $id ) {
258 return $this->db->get_row(
259 $this->db->prepare(
260 "SELECT * FROM {$this->table} WHERE {$this->primary_id} = %d",
261 $id
262 ),
263 $this->output
264 );
265 }
266
267 /**
268 * Get rows.
269 *
270 * @param string|null $query SQL query.
271 * @param int $y Optional. Row to return. Indexed from 0.
272 *
273 * @return array|object|void|null
274 */
275 public function get_row( $query = null, $y = 0 ) {
276 return $this->db->get_row( $query, $this->output, $y );
277 }
278
279 /**
280 * Prepare query.
281 *
282 * @param string $query query string.
283 * @param array ...$args arguments.
284 * @return string|void|null
285 */
286 public function prepare( $query, ...$args ) {
287 return $this->db->prepare( $query, ...$args );
288 }
289
290 /**
291 * Generate search query
292 *
293 * @param string $search_term Search Term.
294 *
295 * @return $this
296 */
297 public function search( $search_term = '' ) {
298 $this->search_term = $search_term;
299
300 return $this;
301 }
302
303 /**
304 * Set order by value DESC
305 *
306 * @param string $col Table Column.
307 *
308 * @return $this
309 */
310 public function desc( $col = '' ) {
311 if ( ! empty( $col ) ) {
312 $this->order_by_col = $col;
313 }
314 $this->order_by = 'DESC';
315
316 return $this;
317 }
318
319 /**
320 * Set order by value ASC
321 *
322 * @param string $col Order by Column.
323 *
324 * @return $this
325 */
326 public function asc( $col = '' ) {
327 if ( ! empty( $col ) ) {
328 $this->order_by_col = $col;
329 }
330 $this->order_by = 'ASC';
331
332 return $this;
333 }
334 }
335