| @@ -624,8 +624,54 @@ | ||
| 624 | 624 | return Helper::get_array_value( $this->cache_set( $query, $results ) ); |
| 625 | 625 | } |
| 626 | 626 | |
| 627 | 627 | /** |
| 628 | + * Retrieves a list of records based on the provided arguments. | |
| 629 | + * | |
| 630 | + * This method fetches results from the database, allowing for various | |
| 631 | + * customization options such as filtering, pagination, and sorting. | |
| 632 | + * | |
| 633 | + * @param array<string,mixed> $args { | |
| 634 | + * Optional. An array of arguments to customize the query. | |
| 635 | + * | |
| 636 | + * @type array $where An associative array of conditions to filter the results. | |
| 637 | + * @type int $limit The maximum number of results to return. Default is 10. | |
| 638 | + * @type int $offset The number of records to skip before starting to collect results. Default is 0. | |
| 639 | + * @type string $orderby The column by which to order the results. Default is 'created_at'. | |
| 640 | + * @type string $order The direction of the order (ASC or DESC). Default is 'DESC'. | |
| 641 | + * } | |
| 642 | + * @param bool $set_limit Whether to set the limit on the query. Default is true. | |
| 643 | + * | |
| 644 | + * @since 1.13.0 | |
| 645 | + * @return array<mixed> The results of the query, typically an array of objects or associative arrays. | |
| 646 | + */ | |
| 647 | + public function get_records_by_args( $args = [], $set_limit = true ) { | |
| 648 | + $_args = wp_parse_args( | |
| 649 | + $args, | |
| 650 | + [ | |
| 651 | + 'where' => [], | |
| 652 | + 'columns' => '*', | |
| 653 | + 'limit' => 10, | |
| 654 | + 'offset' => 0, | |
| 655 | + 'orderby' => 'created_at', | |
| 656 | + 'order' => 'DESC', | |
| 657 | + ] | |
| 658 | + ); | |
| 659 | + $extra_queries = [ | |
| 660 | + sprintf( 'ORDER BY `%1$s` %2$s', Helper::get_string_value( esc_sql( $_args['orderby'] ) ), Helper::get_string_value( esc_sql( $_args['order'] ) ) ), | |
| 661 | + ]; | |
| 662 | + | |
| 663 | + if ( $set_limit ) { | |
| 664 | + $extra_queries[] = sprintf( 'LIMIT %1$d, %2$d', absint( $_args['offset'] ), absint( $_args['limit'] ) ); | |
| 665 | + } | |
| 666 | + return $this->get_results( | |
| 667 | + $_args['where'], | |
| 668 | + $_args['columns'], | |
| 669 | + $extra_queries | |
| 670 | + ); | |
| 671 | + } | |
| 672 | + | |
| 673 | + /** | |
| 628 | 674 | * Get the total number of rows in the table. |
| 629 | 675 | * |
| 630 | 676 | * @param array<mixed> $where_clauses Optional. An associative array of WHERE clauses for the SQL query. |
| 631 | 677 | * @since 0.0.13 |