PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.0.83
OttoKit: All-in-One Automation Platform v1.0.83
1.1.34 1.1.33 1.1.32 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 1 year ago SaasApiToken.php 1 year ago Utilities.php 2 years ago
Model.php
346 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 $valid_outputs = [ 'ARRAY_A', 'ARRAY_N', 'OBJECT', 'OBJECT_K' ];
184 $output = in_array( $this->output, $valid_outputs, true ) ? $this->output : 'OBJECT';
185
186 return $this->db->get_results( "SELECT * FROM {$this->table}", $output );
187 }
188
189 /**
190 * Prepare where query on given array.
191 *
192 * @param array $data where data.
193 * @return $this
194 */
195 public function where( $data ) {
196 foreach ( $data as $key => $value ) {
197 $this->where[] = "AND {$key} = '{$value}'";
198 }
199 return $this;
200 }
201
202 /**
203 * Get the table name.
204 *
205 * @return string
206 */
207 public function table() {
208 return $this->table;
209 }
210
211 /**
212 * Add selections.
213 *
214 * @param string $select select string.
215 * @return $this
216 */
217 public function select( $select ) {
218 $this->select = $select;
219 return $this;
220 }
221
222 /**
223 * Get specific results.
224 *
225 * @return array|object|void|null
226 */
227 public function get() {
228 $valid_outputs = [ 'ARRAY_A', 'ARRAY_N', 'OBJECT', 'OBJECT_K' ];
229 $output = in_array( $this->output, $valid_outputs, true ) ? $this->output : 'OBJECT';
230
231 return $this->db->get_results( "SELECT {$this->get_select()} FROM {$this->table} {$this->get_left_join()} {$this->get_where_sql()} ", $output );
232 }
233
234 /**
235 * Prepare WHERE conditions for sql.
236 *
237 * @return string
238 */
239 public function get_where_sql() {
240 $where_clause = '';
241 if ( count( $this->where ) ) {
242 $where_clause .= implode( ' ', $this->where );
243 }
244
245 return " WHERE 1 = 1 {$where_clause} ";
246 }
247
248 /**
249 * Get var from the db.
250 *
251 * @return string|null|int
252 */
253 public function get_var() {
254 return $this->db->get_var( "SELECT {$this->get_select()} FROM {$this->table} {$this->get_left_join()} {$this->get_where_sql()} " );
255 }
256
257 /**
258 * Find the result by id.
259 *
260 * @param int $id id.
261 * @return array|object|void|null
262 */
263 public function find( $id ) {
264 $valid_outputs = [ 'ARRAY_A', 'ARRAY_N', 'OBJECT' ];
265 $output = in_array( $this->output, $valid_outputs, true ) ? $this->output : 'OBJECT';
266 return $this->db->get_row(
267 $this->db->prepare(
268 "SELECT * FROM {$this->table} WHERE {$this->primary_id} = %d",
269 $id
270 ),
271 $output
272 );
273 }
274
275 /**
276 * Get rows.
277 *
278 * @param string|null $query SQL query.
279 * @param int $y Optional. Row to return. Indexed from 0.
280 *
281 * @return array|object|void|null
282 */
283 public function get_row( $query = null, $y = 0 ) {
284 $valid_outputs = [ 'ARRAY_A', 'ARRAY_N', 'OBJECT' ];
285 $output = in_array( $this->output, $valid_outputs, true ) ? $this->output : 'OBJECT';
286 $y = max( 0, (int) $y );
287 return $this->db->get_row( $query, $output, $y );
288 }
289
290 /**
291 * Prepare query.
292 *
293 * @param string $query query string.
294 * @param array ...$args arguments.
295 * @return string|void|null
296 */
297 public function prepare( $query, ...$args ) {
298 return $this->db->prepare( $query, ...$args );
299 }
300
301 /**
302 * Generate search query
303 *
304 * @param string $search_term Search Term.
305 *
306 * @return $this
307 */
308 public function search( $search_term = '' ) {
309 $this->search_term = $search_term;
310
311 return $this;
312 }
313
314 /**
315 * Set order by value DESC
316 *
317 * @param string $col Table Column.
318 *
319 * @return $this
320 */
321 public function desc( $col = '' ) {
322 if ( ! empty( $col ) ) {
323 $this->order_by_col = $col;
324 }
325 $this->order_by = 'DESC';
326
327 return $this;
328 }
329
330 /**
331 * Set order by value ASC
332 *
333 * @param string $col Order by Column.
334 *
335 * @return $this
336 */
337 public function asc( $col = '' ) {
338 if ( ! empty( $col ) ) {
339 $this->order_by_col = $col;
340 }
341 $this->order_by = 'ASC';
342
343 return $this;
344 }
345 }
346