PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.2.3
JetFormBuilder — Dynamic Blocks Form Builder v3.2.3
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / db-queries / views / view-base.php
jetformbuilder / includes / db-queries / views Last commit date
post-meta-view.php 2 years ago relation.php 2 years ago view-base-count-trait.php 2 years ago view-base.php 2 years ago view-custom-table-base.php 2 years ago
view-base.php
461 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Db_Queries\Views;
5
6 use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception;
7 use Jet_Form_Builder\Db_Queries\Execution_Builder;
8 use Jet_Form_Builder\Db_Queries\Query_Builder;
9 use Jet_Form_Builder\Db_Queries\Query_Cache_Builder;
10 use Jet_Form_Builder\Db_Queries\Traits\Model_Dependencies;
11 use Jet_Form_Builder\Db_Queries\Traits\Model_Dependencies_Interface;
12 use Jet_Form_Builder\Exceptions\Query_Builder_Exception;
13
14 // If this file is called directly, abort.
15 if ( ! defined( 'WPINC' ) ) {
16 die;
17 }
18
19 abstract class View_Base implements Model_Dependencies_Interface {
20
21 use Model_Dependencies;
22
23 const FROM_HIGH_TO_LOW = 'DESC';
24 const FROM_LOW_TO_HIGH = 'ASC';
25
26 protected $limit = array();
27 protected $conditions = array();
28 protected $order_by = array();
29 protected $select;
30
31 /** @var Relation[] */
32 protected $relations = array();
33 protected $filters = array();
34
35 abstract public function table(): string;
36
37 public function select_columns(): array {
38 return $this->select;
39 }
40
41 public function always_conditions(): array {
42 return array();
43 }
44
45 public function custom_where(): string {
46 return '';
47 }
48
49 public function set_table_args( array $table_args ) {
50 $offset = $table_args['offset'] ?? 0;
51 $limit = $table_args['limit'] ?? 15;
52 $filters = $table_args['filters'] ?? array();
53
54 if ( -1 === $limit ) {
55 $this->set_limit( array() );
56 } else {
57 $this->set_limit( array( $offset, $limit ) );
58 }
59 $this->set_filters( $filters );
60
61 return $this;
62 }
63
64 public function set_filters( array $filters ) {
65 $this->filters = esc_sql( $filters );
66
67 return $this;
68 }
69
70 /**
71 * @param $callback
72 *
73 * @return $this
74 * @throws Query_Builder_Exception
75 */
76 public function with( $callback ): View_Base {
77 if ( is_string( $callback ) ) {
78 $callback = array( $this, $callback );
79 }
80 if ( ! is_callable( $callback ) ) {
81 throw new Query_Builder_Exception( 'Relation is not callable.' );
82 }
83 $this->relations[] = call_user_func( $callback );
84
85 return $this;
86 }
87
88 public function get_prepared_join( Query_Builder $builder ) {
89 }
90
91 /**
92 * @param array $conditions
93 *
94 * @return $this
95 */
96 public function set_conditions( array $conditions ): View_Base {
97 $this->conditions = array_merge( $this->always_conditions(), $conditions );
98
99 return $this;
100 }
101
102 public function add_conditions( array $conditions ): View_Base {
103 if ( empty( $this->conditions ) ) {
104 return $this->set_conditions( $conditions );
105 }
106
107 $this->conditions = array_merge( $this->conditions, $conditions );
108
109 return $this;
110 }
111
112 public function conditions(): array {
113 if ( empty( $this->conditions ) ) {
114 $this->set_conditions( array() );
115 }
116
117 return $this->conditions;
118 }
119
120 public function set_limit( array $limit ): View_Base {
121 $this->limit = array_map( 'intval', $limit );
122
123 return $this;
124 }
125
126 /**
127 * @return int[]
128 */
129 public function limit(): array {
130 return $this->limit;
131 }
132
133 public function set_order_by( array $order_by ): View_Base {
134 if ( ! in_array( $order_by, array( self::FROM_HIGH_TO_LOW, self::FROM_LOW_TO_HIGH ), true ) ) {
135 return $this;
136 }
137
138 $this->order_by = $order_by;
139
140 return $this;
141 }
142
143 /**
144 * @return string[]
145 */
146 public function order_by(): array {
147 return $this->order_by;
148 }
149
150 /**
151 * @since 3.1.0 Added $raw argument
152 *
153 * Get the column name with table prefix
154 *
155 * @param $column
156 * @param bool $raw
157 *
158 * @return string
159 * @since 3.1.0 Added $raw argument
160 *
161 * Get the column name with table prefix
162 */
163 public function column( $column, bool $raw = false ): string {
164 if ( is_string( $column ) ) {
165 return $raw ? "{$this->table()}.{$column}" : "`{$this->table()}`.`{$column}`";
166 }
167
168 if ( isset( $column['as'] ) ) {
169 return $column['as'];
170 }
171
172 $name = $column['column'] ?? false;
173 $table = empty( $column['table'] ) ? $this->table() : $column['table'];
174
175 if ( ! $name ) {
176 wp_die( 'Please set the column name.', 'View Base Error' );
177 }
178
179 return $raw ? "{$table}.{$name}" : "`{$table}`.`{$name}`";
180 }
181
182 protected function prepare_row( $row ) {
183 if ( ! $row ) {
184 return array();
185 }
186 foreach ( $row as $column => $value ) {
187 $parts = explode( '.', $column );
188
189 if ( ! isset( $parts[1] ) ) {
190 continue;
191 }
192
193 $row[ $parts[0] ][ $parts[1] ] = $value;
194 unset( $row[ $column ] );
195 }
196
197 return $row;
198 }
199
200 protected function prepare_values( $values ) {
201 return $values;
202 }
203
204 /**
205 * @param $row
206 *
207 * @return array
208 * @throws Query_Builder_Exception
209 */
210 public function get_prepared_row( $row ) {
211 $prepared = $this->prepare_row( $row );
212
213 if ( empty( $prepared ) ) {
214 throw new Query_Builder_Exception(
215 esc_html( $this->empty_message() ),
216 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
217 $row
218 );
219 }
220
221 return $prepared;
222 }
223
224 /**
225 * @param $values
226 *
227 * @return array
228 * @throws Query_Builder_Exception
229 */
230 public function get_prepared_values( $values ) {
231 $prepared = $this->prepare_values( $values );
232
233 if ( empty( $prepared ) ) {
234 throw new Query_Builder_Exception(
235 esc_html( $this->empty_message() ),
236 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
237 $values
238 );
239 }
240
241 return $prepared;
242 }
243
244 protected function empty_message(): string {
245 return __( 'Empty row.', 'jet-form-builder' );
246 }
247
248 public static function get_paginated_args( $args ): array {
249 $base = array_merge(
250 array(
251 'limit' => 25,
252 'sort' => self::FROM_HIGH_TO_LOW,
253 'page' => 1,
254 ),
255 $args
256 );
257
258 $base['offset'] = static::get_offset( $args );
259
260 return $base;
261 }
262
263 /**
264 * @param int|array $args
265 * @param int $limit
266 *
267 * @return int
268 */
269 public static function get_offset( $args, int $limit = 0 ): int {
270 if ( is_array( $args ) ) {
271 $page = (int) $args['page'];
272 $limit = (int) $args['limit'];
273 } else {
274 $page = (int) $args;
275 }
276
277 return 1 === $page ? 0 : ( ( $page - 1 ) * $limit );
278 }
279
280 /**
281 * @param $columns
282 *
283 * @return View_Base
284 */
285 public static function find( $columns ): View_Base {
286 $conditions = static::prepare_columns( $columns );
287
288 return ( new static() )->set_conditions( $conditions );
289 }
290
291
292 public static function findOne( $columns ): View_Base {
293 return static::find( $columns )->set_limit( array( 1 ) );
294 }
295
296 /**
297 * @param $primary_id
298 *
299 * @return array
300 * @throws Query_Builder_Exception
301 */
302 public static function findById( $primary_id ): array {
303 return static::findOne( array( 'id' => $primary_id ) )
304 ->query()
305 ->query_one();
306 }
307
308 /**
309 * @return array
310 * @throws Query_Builder_Exception
311 */
312 public static function all(): array {
313 return ( new static() )->query()->query_all();
314 }
315
316 /**
317 * @return array
318 * @throws Query_Builder_Exception
319 */
320 public static function one(): array {
321 return ( new static() )->query()->query_one();
322 }
323
324 /**
325 * @return array
326 * @throws Query_Builder_Exception
327 */
328 public static function values(): array {
329 return ( new static() )->query()->query_col();
330 }
331
332 /**
333 * @param $where
334 *
335 * @return int
336 * @throws Query_Builder_Exception
337 */
338 public static function delete( $where ): int {
339 return Execution_Builder::instance()->view_delete( static::create( $where ) );
340 }
341
342 /**
343 * @param array $columns
344 * @param array $where
345 *
346 * @return int
347 * @throws Sql_Exception
348 */
349 public static function update( array $columns, array $where ): int {
350 return Execution_Builder::instance()->view_update( $columns, static::create( $where ) );
351 }
352
353 /**
354 * @param array $columns
355 *
356 * @return string
357 */
358 public function build_set( array $columns ): string {
359 $columns = $this->attach_columns( $columns );
360
361 return Query_Builder::build_set( $columns );
362 }
363
364 /**
365 * Prepare columns for build conditions
366 *
367 * @param $columns
368 *
369 * @return array
370 */
371 public static function prepare_columns( $columns ): array {
372 $conditions = array();
373
374 if ( ! is_array( $columns ) ) {
375 return array(
376 array(
377 'type' => 'equal_column',
378 'values' => array( 'id', $columns ),
379 ),
380 );
381 }
382
383 foreach ( $columns as $column => $value ) {
384 if ( is_numeric( $column ) ) {
385 $conditions[] = $value;
386 } else {
387 $conditions[] = array(
388 'type' => 'equal_column',
389 'values' => array( $column, $value ),
390 );
391 }
392 }
393
394 return $conditions;
395 }
396
397 /**
398 * @param array $columns
399 *
400 * @return array
401 */
402 public function attach_columns( array $columns ): array {
403 foreach ( $columns as $name => $value ) {
404 if ( is_numeric( $name ) ) {
405 continue;
406 }
407 $columns[ $this->column( $name ) ] = $value;
408 unset( $columns[ $name ] );
409 }
410
411 return $columns;
412 }
413
414 /**
415 * @param mixed $where
416 *
417 * @return View_Base
418 */
419 public static function create( $where ): View_Base {
420 $conditions = static::prepare_columns( $where );
421
422 return ( new static() )->set_conditions( $conditions );
423 }
424
425
426 /**
427 * @return Query_Builder
428 * @throws Query_Builder_Exception
429 */
430 public function query(): Query_Builder {
431 $this->prepare_dependencies();
432
433 if ( ! $this->select ) {
434 $this->set_select( array( '*' ) );
435 }
436
437 return ( new Query_Builder() )->set_view( $this );
438 }
439
440 /**
441 * @throws Query_Builder_Exception
442 */
443 public function prepare_dependencies() {
444 foreach ( $this->get_dependencies() as $model ) {
445 $model->create();
446
447 foreach ( $model->get_migrations() as $migration ) {
448 throw new Query_Builder_Exception( esc_html( get_class( $model ) . ' is not updated' ) );
449 }
450 }
451 }
452
453 /**
454 * @param string[] $select
455 */
456 public function set_select( array $select ) {
457 $this->select = $select;
458 }
459
460 }
461