PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.1.2
JetFormBuilder — Dynamic Blocks Form Builder v3.1.2
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
462 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.OutputNotEscaped
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.OutputNotEscaped
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::find( array( 'id' => $primary_id ) )
304 ->set_limit( array( 1 ) )
305 ->query()
306 ->query_one();
307 }
308
309 /**
310 * @return array
311 * @throws Query_Builder_Exception
312 */
313 public static function all(): array {
314 return ( new static() )->query()->query_all();
315 }
316
317 /**
318 * @return array
319 * @throws Query_Builder_Exception
320 */
321 public static function one(): array {
322 return ( new static() )->query()->query_one();
323 }
324
325 /**
326 * @return array
327 * @throws Query_Builder_Exception
328 */
329 public static function values(): array {
330 return ( new static() )->query()->query_col();
331 }
332
333 /**
334 * @param $where
335 *
336 * @return int
337 * @throws Query_Builder_Exception
338 */
339 public static function delete( $where ): int {
340 return Execution_Builder::instance()->view_delete( static::create( $where ) );
341 }
342
343 /**
344 * @param array $columns
345 * @param array $where
346 *
347 * @return int
348 * @throws Sql_Exception
349 */
350 public static function update( array $columns, array $where ): int {
351 return Execution_Builder::instance()->view_update( $columns, static::create( $where ) );
352 }
353
354 /**
355 * @param array $columns
356 *
357 * @return string
358 */
359 public function build_set( array $columns ): string {
360 $columns = $this->attach_columns( $columns );
361
362 return Query_Builder::build_set( $columns );
363 }
364
365 /**
366 * Prepare columns for build conditions
367 *
368 * @param $columns
369 *
370 * @return array
371 */
372 public static function prepare_columns( $columns ): array {
373 $conditions = array();
374
375 if ( ! is_array( $columns ) ) {
376 return array(
377 array(
378 'type' => 'equal_column',
379 'values' => array( 'id', $columns ),
380 ),
381 );
382 }
383
384 foreach ( $columns as $column => $value ) {
385 if ( is_numeric( $column ) ) {
386 $conditions[] = $value;
387 } else {
388 $conditions[] = array(
389 'type' => 'equal_column',
390 'values' => array( $column, $value ),
391 );
392 }
393 }
394
395 return $conditions;
396 }
397
398 /**
399 * @param array $columns
400 *
401 * @return array
402 */
403 public function attach_columns( array $columns ): array {
404 foreach ( $columns as $name => $value ) {
405 if ( is_numeric( $name ) ) {
406 continue;
407 }
408 $columns[ $this->column( $name ) ] = $value;
409 unset( $columns[ $name ] );
410 }
411
412 return $columns;
413 }
414
415 /**
416 * @param mixed $where
417 *
418 * @return View_Base
419 */
420 public static function create( $where ): View_Base {
421 $conditions = static::prepare_columns( $where );
422
423 return ( new static() )->set_conditions( $conditions );
424 }
425
426
427 /**
428 * @return Query_Builder
429 * @throws Query_Builder_Exception
430 */
431 public function query(): Query_Builder {
432 $this->prepare_dependencies();
433
434 if ( ! $this->select ) {
435 $this->set_select( array( '*' ) );
436 }
437
438 return ( new Query_Builder() )->set_view( $this );
439 }
440
441 /**
442 * @throws Query_Builder_Exception
443 */
444 public function prepare_dependencies() {
445 foreach ( $this->get_dependencies() as $model ) {
446 $model->create();
447
448 foreach ( $model->get_migrations() as $migration ) {
449 throw new Query_Builder_Exception( esc_html( get_class( $model ) . ' is not updated' ) );
450 }
451 }
452 }
453
454 /**
455 * @param string[] $select
456 */
457 public function set_select( array $select ) {
458 $this->select = $select;
459 }
460
461 }
462