Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/12/2021 02:09:02 PM (3 years ago)
Author:
imath
Message:

Support date queries for Members, Groups and Sites

According to components, date queries can be restricted to a limited list of loop types:

  • For Members, the $type parameter in bp_has_members() needs to be either active, newest, random or online.
  • For Groups, the $type parameter in bp_has_groups() needs to be either active or newest.
  • For sites, the $type parameter in bp_has_blogs() needs to be either active or newest.

Props r-a-y

Fixes #8488

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-date-query.php

    r10417 r13184  
    3434
    3535    /**
     36     * Whether to prepend the 'AND' operator to the WHERE SQL clause.
     37     *
     38     * @since 10.0.0
     39     *
     40     * @var bool
     41     */
     42    public $prepend_and = false;
     43
     44    /**
    3645     * Constructor.
    3746     *
    38      * @param array  $date_query Date query arguments.
    39      * @param string $column     THe DB column to query against.
     47     * @since 2.1.0
     48     * @since 10.0.0 Added $prepend_and argument.
     49     *
     50     * @param array  $date_query  Date query arguments.
     51     * @param string $column      The DB column to query against.
     52     * @param bool   $prepend_and Whether to prepend the 'AND' operator to the WHERE SQL clause.
    4053     *
    4154     * @see WP_Date_Query::__construct()
    4255     */
    43     public function __construct( $date_query, $column = '' ) {
     56    public function __construct( $date_query, $column = '', $prepend_and = false ) {
    4457        if ( ! empty( $column ) ) {
    4558            $this->column = $column;
    4659            add_filter( 'date_query_valid_columns', array( $this, 'register_date_column' ) );
     60        }
     61
     62        if ( ! empty( $prepend_and ) ) {
     63            $this->prepend_and = true;
    4764        }
    4865
     
    6784        return $retval;
    6885    }
     86
     87    /**
     88     * Generate SQL clauses to be appended to a main query.
     89     *
     90     * Since BuddyPress builds its SQL queries differently than WP_Query, we have
     91     * to override the parent method to remove the leading 'AND' operator from the
     92     * WHERE clause.
     93     *
     94     * @since 10.0.0
     95     *
     96     * @return array {
     97     *     Array containing JOIN and WHERE SQL clauses to append to the main query.
     98     *
     99     *     @type string $join  SQL fragment to append to the main JOIN clause.
     100     *     @type string $where SQL fragment to append to the main WHERE clause.
     101     * }
     102     */
     103    protected function get_sql_clauses() {
     104        // If we want to have the leading 'AND' operator, just use parent method.
     105        if ( $this->prepend_and ) {
     106            return parent::get_sql_clauses();
     107        }
     108
     109        // If we're here, that means we do not want the leading 'AND' operator.
     110        return $this->get_sql_for_query( $this->queries );
     111    }
     112
     113    /**
     114     * Helper method to generate and fetch the WHERE SQL clause for a date query.
     115     *
     116     * See {@link BP_Date_Query::__construct()} for all argument documentation.
     117     *
     118     * @since 10.0.0
     119     *
     120     * @param  array  $date_query  Date query arguments.
     121     * @param  string $column      DB column to query against date.
     122     * @param  bool   $prepend_and Whether to prepend the 'AND' operator to the WHERE clause.
     123     * @return string
     124     */
     125    public static function get_where_sql( $date_query = array(), $column = '', $prepend_and = false ) {
     126        $sql = '';
     127
     128        // Generate and fetch the WHERE clause for a date query.
     129        if ( ! empty( $date_query ) && is_array( $date_query ) && ! empty( $column ) ) {
     130            $date_query = new self( $date_query, $column, $prepend_and );
     131            $sql = $date_query->get_sql();
     132        }
     133
     134        return $sql;
     135    }
    69136}
    70137endif;
Note: See TracChangeset for help on using the changeset viewer.