From 638124bd8115470bf10d2afec7d3ff518accf954 Mon Sep 17 00:00:00 2001 From: Nath Date: Tue, 21 May 2024 09:41:04 +0100 Subject: [PATCH 1/3] feat: add where function to dt-reports class --- dt-reports/reports.php | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/dt-reports/reports.php b/dt-reports/reports.php index af23d1e01b..a4130b0bd6 100755 --- a/dt-reports/reports.php +++ b/dt-reports/reports.php @@ -310,6 +310,54 @@ public static function get( $value, $type ) : array { return $report; } + /** + * Get posts filtered by where clauses + * + * @param array $where An assoc array of key, value pairs + */ + public static function where( $where = [] ) { + global $wpdb; + $report = []; + + $sql = ''; + $args = []; + + foreach ( $where as $key => $value ) { + $new_sql = ''; + if ( !empty( $sql ) ) { + $new_sql .= ' AND'; + } else { + $new_sql .= ' WHERE'; + } + + $new_sql .= $wpdb->prepare( ' %1$s = ', $key ); + + if ( is_numeric( $value ) ) { + $new_sql .= '%d'; + } else if ( is_string( $value ) ) { + $new_sql .= '%s'; + } else { + dt_write_log( __METHOD__ . ' $value is of type ' . gettype( $value ) . ' but should be a string or number' ); + continue; + } + $sql .= $new_sql; + $args[] = $value; + } + + //phpcs:ignore + $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->dt_reports $sql", $args ), ARRAY_A ); + if ( ! empty( $results ) ) { + foreach ( $results as $index => $result ){ + if ( isset( $result['payload'] ) ){ + $results[$index]['payload'] = maybe_unserialize( $result['payload'] ); + } + } + $report = $results; + } + + return $report; + } + /** * Delete report * From 4aa772d3a1503f95dd8e64abaecb00ff7dddb456 Mon Sep 17 00:00:00 2001 From: Nath Date: Wed, 22 May 2024 09:57:20 +0100 Subject: [PATCH 2/3] feat: return error if wrong value type is given --- dt-reports/reports.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dt-reports/reports.php b/dt-reports/reports.php index a4130b0bd6..de26bf5efb 100755 --- a/dt-reports/reports.php +++ b/dt-reports/reports.php @@ -337,8 +337,7 @@ public static function where( $where = [] ) { } else if ( is_string( $value ) ) { $new_sql .= '%s'; } else { - dt_write_log( __METHOD__ . ' $value is of type ' . gettype( $value ) . ' but should be a string or number' ); - continue; + return new WP_Error( 'invalid-value-type', ' $value is of type ' . gettype( $value ) . ' but should be a string or number' ); } $sql .= $new_sql; $args[] = $value; From addf5829a6f6cb3c243861e7bd081df0732c8598 Mon Sep 17 00:00:00 2001 From: Nath Date: Tue, 25 Jun 2024 17:31:57 +0100 Subject: [PATCH 3/3] feat: move value of where clause into array; refactor: where name to list putting the value in an array will mean we can add operators and other upgrades to how the where works later --- dt-reports/reports.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/dt-reports/reports.php b/dt-reports/reports.php index de26bf5efb..c156f185cb 100755 --- a/dt-reports/reports.php +++ b/dt-reports/reports.php @@ -311,18 +311,24 @@ public static function get( $value, $type ) : array { } /** - * Get posts filtered by where clauses + * List reports filtered by where clauses * * @param array $where An assoc array of key, value pairs + * + * The value parts of the array should be arrays, with 'value' => $value. This allows for adding functionality like operators etc. */ - public static function where( $where = [] ) { + public static function list( $where = [] ) { global $wpdb; $report = []; $sql = ''; $args = []; - foreach ( $where as $key => $value ) { + foreach ( $where as $key => $details ) { + if ( !is_array( $details ) || !isset( $details['value'] ) ) { + return new WP_Error( 'missing-value-type', 'value key is missing for ' . $key ); + } + $value = $details['value']; $new_sql = ''; if ( !empty( $sql ) ) { $new_sql .= ' AND';