| @@ -510,12 +510,8 @@ | ||
| 510 | 510 | $format = $prepared_data['format']; |
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | $result = $this->wpdb->insert( $this->get_tablename(), $prepared_data['data'], $format ); |
| 514 | - | |
| 515 | - // Reset cache so subsequent queries in the same request include the new row. | |
| 516 | - $this->cache_reset(); | |
| 517 | - | |
| 518 | 514 | return $result ? $this->wpdb->insert_id : false; |
| 519 | 515 | } |
| 520 | 516 | |
| 521 | 517 | /** |
| @@ -628,57 +624,8 @@ | ||
| 628 | 624 | return Helper::get_array_value( $this->cache_set( $query, $results ) ); |
| 629 | 625 | } |
| 630 | 626 | |
| 631 | 627 | /** |
| 632 | - * Retrieves a list of records based on the provided arguments. | |
| 633 | - * | |
| 634 | - * This method fetches results from the database, allowing for various | |
| 635 | - * customization options such as filtering, pagination, and sorting. | |
| 636 | - * | |
| 637 | - * @param array<string,mixed> $args { | |
| 638 | - * Optional. An array of arguments to customize the query. | |
| 639 | - * | |
| 640 | - * @type array $where An associative array of conditions to filter the results. | |
| 641 | - * @type int $limit The maximum number of results to return. Default is 10. | |
| 642 | - * @type int $offset The number of records to skip before starting to collect results. Default is 0. | |
| 643 | - * @type string $orderby The column by which to order the results. Default is 'created_at'. | |
| 644 | - * @type string $order The direction of the order (ASC or DESC). Default is 'DESC'. | |
| 645 | - * } | |
| 646 | - * @param bool $set_limit Whether to set the limit on the query. Default is true. | |
| 647 | - * | |
| 648 | - * @since 1.13.0 | |
| 649 | - * @return array<mixed> The results of the query, typically an array of objects or associative arrays. | |
| 650 | - */ | |
| 651 | - public function get_records_by_args( $args = [], $set_limit = true ) { | |
| 652 | - $_args = wp_parse_args( | |
| 653 | - $args, | |
| 654 | - [ | |
| 655 | - 'where' => [], | |
| 656 | - 'columns' => '*', | |
| 657 | - 'limit' => 10, | |
| 658 | - 'offset' => 0, | |
| 659 | - 'orderby' => 'created_at', | |
| 660 | - 'order' => 'DESC', | |
| 661 | - ] | |
| 662 | - ); | |
| 663 | - $allowed_orderby = $this->get_allowed_orderby_columns(); | |
| 664 | - $orderby = in_array( $_args['orderby'], $allowed_orderby, true ) ? $_args['orderby'] : 'created_at'; | |
| 665 | - $order = 'ASC' === strtoupper( Helper::get_string_value( $_args['order'] ) ) ? 'ASC' : 'DESC'; | |
| 666 | - $extra_queries = [ | |
| 667 | - sprintf( 'ORDER BY `%1$s` %2$s', $orderby, $order ), | |
| 668 | - ]; | |
| 669 | - | |
| 670 | - if ( $set_limit ) { | |
| 671 | - $extra_queries[] = sprintf( 'LIMIT %1$d, %2$d', absint( $_args['offset'] ), absint( $_args['limit'] ) ); | |
| 672 | - } | |
| 673 | - return $this->get_results( | |
| 674 | - $_args['where'], | |
| 675 | - $_args['columns'], | |
| 676 | - $extra_queries | |
| 677 | - ); | |
| 678 | - } | |
| 679 | - | |
| 680 | - /** | |
| 681 | 628 | * Get the total number of rows in the table. |
| 682 | 629 | * |
| 683 | 630 | * @param array<mixed> $where_clauses Optional. An associative array of WHERE clauses for the SQL query. |
| 684 | 631 | * @since 0.0.13 |
| @@ -711,19 +658,8 @@ | ||
| 711 | 658 | return Helper::get_integer_value( $this->cache_set( $query, $results ) ); |
| 712 | 659 | } |
| 713 | 660 | |
| 714 | 661 | /** |
| 715 | - * Get the allowed column names for ORDER BY clauses. | |
| 716 | - * Child classes may override this method to restrict orderable columns further. | |
| 717 | - * | |
| 718 | - * @since 2.6.0 | |
| 719 | - * @return array<string> | |
| 720 | - */ | |
| 721 | - protected function get_allowed_orderby_columns() { | |
| 722 | - return array_merge( array_keys( $this->get_schema() ), [ 'updated_at' ] ); | |
| 723 | - } | |
| 724 | - | |
| 725 | - /** | |
| 726 | 662 | * Retrieve a cached value by its key. |
| 727 | 663 | * |
| 728 | 664 | * @param string $key The cache key. |
| 729 | 665 | * @since 0.0.10 |
| @@ -794,9 +730,9 @@ | ||
| 794 | 730 | $wpdb = $this->wpdb; |
| 795 | 731 | |
| 796 | 732 | // If there are WHERE clauses, prepare and append them to the query. |
| 797 | 733 | if ( is_array( $where_clauses ) ) { |
| 798 | - $groups = []; | |
| 734 | + $where = ''; | |
| 799 | 735 | $values = []; |
| 800 | 736 | $schema = $this->get_schema(); |
| 801 | 737 | |
| 802 | 738 | foreach ( $where_clauses as $key => $value ) { |
| @@ -801,12 +737,10 @@ | ||
| 801 | 737 | |
| 802 | 738 | foreach ( $where_clauses as $key => $value ) { |
| 803 | 739 | |
| 804 | 740 | $relation = ! empty( $value['RELATION'] ) ? trim( $value['RELATION'] ) : 'AND'; |
| 805 | - $relation = in_array( strtoupper( $relation ), [ 'AND', 'OR' ], true ) ? strtoupper( $relation ) : 'AND'; | |
| 806 | 741 | |
| 807 | 742 | if ( is_int( $key ) ) { |
| 808 | - $clause_parts = []; | |
| 809 | 743 | foreach ( $value as $_key => $_value ) { |
| 810 | 744 | if ( is_int( $_key ) ) { |
| 811 | 745 | // Check if the operator is allowed. |
| 812 | 746 | if ( ! in_array( $_value['compare'], $this->allowed_where_operators, true ) ) { |
| @@ -812,37 +746,28 @@ | ||
| 812 | 746 | if ( ! in_array( $_value['compare'], $this->allowed_where_operators, true ) ) { |
| 813 | 747 | continue; |
| 814 | 748 | } |
| 815 | 749 | |
| 816 | - // Skip if key is not in schema. | |
| 817 | - if ( ! isset( $schema[ $_value['key'] ] ) ) { | |
| 818 | - continue; | |
| 819 | - } | |
| 820 | - | |
| 821 | 750 | switch ( $_value['compare'] ) { |
| 822 | 751 | case 'LIKE': |
| 823 | - $clause_parts[] = $_value['key'] . ' ' . $_value['compare'] . ' "%%' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ) . '%%"'; | |
| 824 | - $values[] = $_value['value']; | |
| 752 | + $where .= ' ' . $_value['key'] . ' ' . $_value['compare'] . ' "%%' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ) . '%%" ' . $relation; | |
| 753 | + $values[] = $_value['value']; | |
| 825 | 754 | break; |
| 826 | 755 | |
| 827 | 756 | case 'IN': |
| 828 | 757 | // Based on the number of values and datatype, it will create WHERE clause for $wpdb::prepare method. Eg: for ID with three values column: ID IN (%d, %d, %d). |
| 829 | - $datatype = $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ); | |
| 830 | - $clause_parts[] = $_value['key'] . ' ' . $_value['compare'] . ' (' . implode( ', ', array_fill( 0, count( $_value['value'] ), $datatype ) ) . ')'; | |
| 831 | - $values = array_merge( $values, $_value['value'] ); | |
| 758 | + $datatype = $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ); | |
| 759 | + $where .= ' ' . $_value['key'] . ' ' . $_value['compare'] . ' (' . implode( ', ', array_fill( 0, count( $_value['value'] ), $datatype ) ) . ') ' . $relation; | |
| 760 | + $values = array_merge( $values, $_value['value'] ); | |
| 832 | 761 | break; |
| 833 | 762 | |
| 834 | 763 | default: |
| 835 | - $clause_parts[] = $_value['key'] . ' ' . $_value['compare'] . ' ' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ); | |
| 836 | - $values[] = $_value['value']; | |
| 764 | + $where .= ' ' . $_value['key'] . ' ' . $_value['compare'] . ' ' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ) . ' ' . $relation; | |
| 765 | + $values[] = $_value['value']; | |
| 837 | 766 | break; |
| 838 | 767 | } |
| 839 | 768 | } |
| 840 | 769 | } |
| 841 | - | |
| 842 | - if ( ! empty( $clause_parts ) ) { | |
| 843 | - $groups[] = '(' . implode( ' ' . $relation . ' ', $clause_parts ) . ')'; | |
| 844 | - } | |
| 845 | 770 | continue; |
| 846 | 771 | } |
| 847 | 772 | |
| 848 | 773 | if ( ! isset( $schema[ $key ] ) ) { |
| @@ -849,17 +774,17 @@ | ||
| 849 | 774 | // Skip strictly if current key is not in our schema. |
| 850 | 775 | continue; |
| 851 | 776 | } |
| 852 | 777 | |
| 853 | - $groups[] = '(' . $key . ' = ' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $key ]['type'] ) ) . ')'; | |
| 778 | + $where .= ' ' . $key . ' = ' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $key ]['type'] ) ) . ' ' . $relation; | |
| 854 | 779 | $values[] = $value; |
| 855 | 780 | } |
| 856 | 781 | |
| 857 | - if ( empty( $groups ) ) { | |
| 782 | + if ( ! $where ) { | |
| 858 | 783 | return ''; |
| 859 | 784 | } |
| 860 | 785 | |
| 861 | - $where = ' WHERE ' . implode( ' AND ', $groups ); | |
| 786 | + $where = ' WHERE ' . trim( trim( $where, $relation ) ); | |
| 862 | 787 | |
| 863 | 788 | // Prepare the query with placeholders. |
| 864 | 789 | // @phpstan-ignore-next-line -- We are already assigning non-literal string above using "get_format_by_datatype" methods. |
| 865 | 790 | return $wpdb->prepare( $where, ...$values ); // phpcs:ignore -- We are returning prepared sql query here. We are already using necessary placeholders in $where variable. |