Automattic\WooCommerce\Admin\API\Reports\Customers

DataStore::add_time_period_sql_params │ protected │ WC 1.0

Fills WHERE clause of SQL request with date-related constraints.

Method of the class: DataStore{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->add_time_period_sql_params( $query_args, $table_name );
$query_args(array) (required)
Parameters supplied by the user.
$table_name(string) (required)
Name of the db table relevant for the date constraint.

DataStore::add_time_period_sql_params() code WC 11.2.2

protected function add_time_period_sql_params( $query_args, $table_name ) {
	global $wpdb;

	$this->clear_sql_clause( array( 'where', 'where_time', 'having' ) );
	$date_param_mapping  = array(
		'registered'  => array(
			'clause' => 'where',
			'column' => $table_name . '.date_registered',
		),
		'order'       => array(
			'clause' => 'where',
			'column' => $wpdb->prefix . 'wc_order_stats.date_created',
		),
		'last_active' => array(
			'clause' => 'where',
			'column' => $table_name . '.date_last_active',
		),
		'last_order'  => array(
			'clause' => 'having',
			'column' => "MAX( {$wpdb->prefix}wc_order_stats.date_created )",
		),
	);
	$match_operator      = $this->get_match_operator( $query_args );
	$where_time_clauses  = array();
	$having_time_clauses = array();

	foreach ( $date_param_mapping as $query_param => $param_info ) {
		$subclauses  = array();
		$before_arg  = $query_param . '_before';
		$after_arg   = $query_param . '_after';
		$column_name = $param_info['column'];

		if ( ! empty( $query_args[ $before_arg ] ) ) {
			$datetime     = new \DateTime( $query_args[ $before_arg ] );
			$datetime_str = $datetime->format( TimeInterval::$sql_datetime_format );
			$subclauses[] = "{$column_name} <= '$datetime_str'";
		}

		if ( ! empty( $query_args[ $after_arg ] ) ) {
			$datetime     = new \DateTime( $query_args[ $after_arg ] );
			$datetime_str = $datetime->format( TimeInterval::$sql_datetime_format );
			$subclauses[] = "{$column_name} >= '$datetime_str'";
		}

		if ( $subclauses && ( 'where' === $param_info['clause'] ) ) {
			$where_time_clauses[] = '(' . implode( ' AND ', $subclauses ) . ')';
		}

		if ( $subclauses && ( 'having' === $param_info['clause'] ) ) {
			$having_time_clauses[] = '(' . implode( ' AND ', $subclauses ) . ')';
		}
	}

	if ( $where_time_clauses ) {
		$this->subquery->add_sql_clause( 'where_time', 'AND ' . implode( " {$match_operator} ", $where_time_clauses ) );
	}

	if ( $having_time_clauses ) {
		$this->subquery->add_sql_clause( 'having', 'AND ' . implode( " {$match_operator} ", $having_time_clauses ) );
	}
}