Skip to content

Instantly share code, notes, and snippets.

@atwellpub
Created April 15, 2022 02:41
Show Gist options
  • Save atwellpub/07896b0f3f1da2e8c17cca48d908db81 to your computer and use it in GitHub Desktop.
Save atwellpub/07896b0f3f1da2e8c17cca48d908db81 to your computer and use it in GitHub Desktop.
<?php
/**
*
*/
public static function get_rule_logs( $query ) {
if (!isset($query['rule_id']) || !$query['rule_id']) {
return [];
}
global $wpdb;
$table_name = $wpdb->prefix . "logs WHERE 1=1 ";
$query = 'SELECT * FROM '.$table_name;
$args = [];
if (isset($query['rule_id']) && $query['rule_id']) {
$query .= ' AND rule_id = %d ';
$args[] = $query['rule_id'];
}
if (isset($query['begin_date']) && $query['begin_date']) {
$query .= ' AND datetime >= %s ';
$args[] = $query['begin_date'];
}
if (isset($query['end_date']) && $query['end_date']) {
$query .= ' AND datetime <= %s ';
$args[] = $query['end_date'];
}
return $wpdb->get_results( $wpdb->prepare( $query , $args ) , ARRAY_A );
}
@otakupahp
Copy link

The difference between append and implode is more visual :)

The dummy 1 = 1 looks horrible while the implode solution is more "object-oriented", but is just a personal taste

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment