[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-date-query.php (source)

   1  <?php
   2  /**
   3   * Core component classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 2.1.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  if ( class_exists( 'WP_Date_Query' ) ) :
  14  
  15  /**
  16   * BuddyPress date query class.
  17   *
  18   * Extends the {@link WP_Date_Query} class for use with BuddyPress.
  19   *
  20   * @since 2.1.0
  21   *
  22   * @param array $date_query {
  23   *     Date query arguments.  See first parameter of {@link WP_Date_Query::__construct()}.
  24   * }
  25   * @param string $column The DB column to query against.
  26   */
  27  class BP_Date_Query extends WP_Date_Query {
  28      /**
  29       * The column to query against. Can be changed via the query arguments.
  30       *
  31       * @var string
  32       */
  33      public $column;
  34  
  35      /**
  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      /**
  45       * Constructor.
  46       *
  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.
  53       *
  54       * @see WP_Date_Query::__construct()
  55       */
  56  	public function __construct( $date_query, $column = '', $prepend_and = false ) {
  57          if ( ! empty( $column ) ) {
  58              $this->column = $column;
  59              add_filter( 'date_query_valid_columns', array( $this, 'register_date_column' ) );
  60          }
  61  
  62          if ( ! empty( $prepend_and ) ) {
  63              $this->prepend_and = true;
  64          }
  65  
  66          parent::__construct( $date_query, $column );
  67      }
  68  
  69      /**
  70       * Destructor.
  71       */
  72  	public function __destruct() {
  73          remove_filter( 'date_query_valid_columns', array( $this, 'register_date_column' ) );
  74      }
  75  
  76      /**
  77       * Registers our date column with WP Date Query to pass validation.
  78       *
  79       * @param array $retval Current DB columns.
  80       * @return array
  81       */
  82  	public function register_date_column( $retval = array() ) {
  83          $retval[] = $this->column;
  84          return $retval;
  85      }
  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      }
 136  }
 137  endif;


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1