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