Skip to content

Commit 4f34cf5

Browse files
committed
feat: Introduce support for sub query clauses
1 parent 2ab1125 commit 4f34cf5

1 file changed

Lines changed: 73 additions & 22 deletions

File tree

src/Schema/Traits/Custom_Table_Query_Methods.php

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ public static function paginate( array $args, int $per_page = 20, int $page = 1,
468468
* @param array<string,mixed> $args The query arguments.
469469
* @param class-string $class The class name.
470470
*/
471-
do_action( 'tec_common_custom_table_query_pre_results', $args, static::class );
471+
do_action( 'stellarwp_schema_custom_table_query_pre_results', $args, static::class );
472472

473473
$database = Config::get_db();
474474

@@ -498,7 +498,7 @@ public static function paginate( array $args, int $per_page = 20, int $page = 1,
498498
* @param array<string,mixed> $args The query arguments.
499499
* @param class-string $class The class name.
500500
*/
501-
do_action( 'tec_common_custom_table_query_post_results', $results, $args, static::class );
501+
do_action( 'stellarwp_schema_custom_table_query_post_results', $results, $args, static::class );
502502

503503
/**
504504
* Filters the results of the query.
@@ -509,13 +509,14 @@ public static function paginate( array $args, int $per_page = 20, int $page = 1,
509509
* @param array<string,mixed> $args The query arguments.
510510
* @param class-string $class The class name.
511511
*/
512-
return apply_filters( 'tec_common_custom_table_query_results', $results, $args, static::class );
512+
return apply_filters( 'stellarwp_schema_custom_table_query_results', $results, $args, static::class );
513513
}
514514

515515
/**
516516
* Builds a WHERE clause from the provided arguments.
517517
*
518518
* @since 3.0.0
519+
* @since 3.2.0 Now sub where clauses are supported.
519520
*
520521
* @param array<string,mixed> $args The query arguments.
521522
*
@@ -552,12 +553,58 @@ protected static function build_where_from_args( array $args = [] ): string {
552553

553554
$columns = static::get_columns()->get_names();
554555

556+
$sub_wheres = self::build_sub_wheres_from_args(
557+
array_filter( $args,static fn( $arg ) => is_array( $arg ) ),
558+
$columns,
559+
$joined_prefix
560+
);
561+
562+
$where = array_merge( $where, $sub_wheres );
563+
564+
/**
565+
* Filters the WHERE clause.
566+
*
567+
* @since 3.0.0
568+
*
569+
* @param array<string> $where The WHERE clause parts.
570+
* @param array<string,mixed> $args The query arguments.
571+
* @param class-string $class The class name.
572+
*/
573+
$where = apply_filters( 'stellarwp_schema_custom_table_query_where', array_filter( $where ), $args, static::class );
574+
575+
if ( empty( $where ) ) {
576+
return '';
577+
}
578+
579+
return 'WHERE ' . implode( " {$query_operator} ", $where );
580+
}
581+
582+
/**
583+
* Builds the sub WHERE clauses from the provided arguments.
584+
*
585+
* @since 3.2.0
586+
*
587+
* @param array<string,mixed> $args The query arguments.
588+
* @param array<string> $columns The columns to select.
589+
* @param string $joined_prefix The prefix to use for the joined columns.
590+
*
591+
* @return array<string> The sub WHERE clauses.
592+
*/
593+
private static function build_sub_wheres_from_args( array $args = [], array $columns = [], string $joined_prefix = '' ): array {
594+
$sub_wheres = [];
595+
555596
foreach ( $args as $arg ) {
556597
if ( ! is_array( $arg ) ) {
557598
continue;
558599
}
559600

560601
if ( empty( $arg['column'] ) ) {
602+
if ( ! empty( $arg[0]['column'] ) ) {
603+
$sub_wheres[] = [
604+
'queries' => self::build_sub_wheres_from_args( $arg, $columns, $joined_prefix ),
605+
'operator' => ! empty( $arg['query_operator'] ) && in_array( strtoupper( $arg['query_operator'] ), [ 'AND', 'OR' ], true ) ? strtoupper( $arg['query_operator'] ) : 'AND',
606+
];
607+
}
561608
continue;
562609
}
563610

@@ -589,34 +636,38 @@ protected static function build_where_from_args( array $args = [] ): string {
589636
$query = "{$joined_prefix}{$column} {$operator} {$placeholder}";
590637

591638
if ( is_array( $value ) ) {
592-
$where[] = $database::prepare( $query, ...$value );
639+
$sub_wheres[] = $database::prepare( $query, ...$value );
593640
continue;
594641
}
595642

596643
if ( 'NULL' === $placeholder ) {
597-
$where[] = $query;
644+
$sub_wheres[] = $query;
598645
continue;
599646
}
600647

601-
$where[] = $database::prepare( $query, $value );
602-
}
603-
604-
/**
605-
* Filters the WHERE clause.
606-
*
607-
* @since 3.0.0
608-
*
609-
* @param array<string> $where The WHERE clause parts.
610-
* @param array<string,mixed> $args The query arguments.
611-
* @param class-string $class The class name.
612-
*/
613-
$where = apply_filters( 'tec_common_custom_table_query_where', array_filter( $where ), $args, static::class );
614-
615-
if ( empty( $where ) ) {
616-
return '';
648+
$sub_wheres[] = $database::prepare( $query, $value );
617649
}
618650

619-
return 'WHERE ' . implode( " {$query_operator} ", $where );
651+
return array_filter(
652+
array_map(
653+
static function ( $sub_where ) {
654+
if ( ! is_array( $sub_where ) ) {
655+
return $sub_where;
656+
}
657+
658+
if ( empty( $sub_where['queries'] ) ) {
659+
return '';
660+
}
661+
662+
if ( ! isset( $sub_where['operator'] ) || ! in_array( strtoupper( $sub_where['operator'] ), [ 'AND', 'OR' ], true ) ) {
663+
$sub_where['operator'] = 'AND';
664+
}
665+
666+
return '(' . implode( " {$sub_where['operator']} ", $sub_where['queries'] ) . ')';
667+
},
668+
$sub_wheres
669+
)
670+
);
620671
}
621672

622673
/**

0 commit comments

Comments
 (0)