Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableQuery::process_date_args()privateWC 1.0

Processes date-related query args and merges the result into 'date_query'.

Method of the class: OrdersTableQuery{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->process_date_args(): void;

OrdersTableQuery::process_date_args() code WC 9.3.3

private function process_date_args(): void {
	if ( $this->arg_isset( 'date_query' ) ) {
		// Process already passed date queries args.
		$this->args['date_query'] = $this->map_gmt_and_post_keys_to_hpos_keys( $this->args['date_query'] );
	}

	$valid_operators        = array( '>', '>=', '=', '<=', '<', '...' );
	$date_queries           = array();
	$local_to_gmt_date_keys = array(
		'date_created'   => 'date_created_gmt',
		'date_updated'   => 'date_updated_gmt',
		'date_paid'      => 'date_paid_gmt',
		'date_completed' => 'date_completed_gmt',
	);

	$gmt_date_keys   = array_values( $local_to_gmt_date_keys );
	$local_date_keys = array_keys( $local_to_gmt_date_keys );

	$valid_date_keys = array_merge( $gmt_date_keys, $local_date_keys );
	$date_keys       = array_filter( $valid_date_keys, array( $this, 'arg_isset' ) );

	foreach ( $date_keys as $date_key ) {
		$is_local   = in_array( $date_key, $local_date_keys, true );
		$date_value = $this->args[ $date_key ];

		$operator   = '=';
		$dates_raw  = array();
		$dates      = array();

		if ( is_string( $date_value ) && preg_match( self::REGEX_SHORTHAND_DATES, $date_value, $matches ) ) {
			$operator = in_array( $matches[2], $valid_operators, true ) ? $matches[2] : '';

			if ( ! empty( $matches[1] ) ) {
				$dates_raw[] = $matches[1];
			}

			$dates_raw[] = $matches[3];
		} else {
			$dates_raw[] = $date_value;
		}

		if ( empty( $dates_raw ) || ! $operator || ( '...' === $operator && count( $dates_raw ) < 2 ) ) {
			throw new \Exception( 'Invalid date_query' );
		}

		if ( $is_local ) {
			$date_key = $local_to_gmt_date_keys[ $date_key ];

			if ( ! is_numeric( $dates_raw[0] ) && ( ! isset( $dates_raw[1] ) || ! is_numeric( $dates_raw[1] ) ) ) {
				// Only non-numeric args can be considered local time. Timestamps are assumed to be UTC per https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query#date.
				$date_queries[] = array_merge(
					array(
						'column' => $date_key,
					),
					$this->local_time_to_gmt_date_query( $dates_raw, $operator )
				);

				continue;
			}
		}

		$operator_to_keys = array();

		if ( in_array( $operator, array( '>', '>=', '...' ), true ) ) {
			$operator_to_keys[] = 'after';
		}

		if ( in_array( $operator, array( '<', '<=', '...' ), true ) ) {
			$operator_to_keys[] = 'before';
		}

		$dates          = array_map( array( $this, 'date_to_date_query_arg' ), $dates_raw );
		$date_queries[] = array_merge(
			array(
				'column'    => $date_key,
				'inclusive' => ! in_array( $operator, array( '<', '>' ), true ),
			),
			'=' === $operator
				? end( $dates )
				: array_combine( $operator_to_keys, $dates )
		);
	}

	// Add top-level date parameters to the date_query.
	$tl_query = array();
	foreach ( array( 'hour', 'minute', 'second', 'year', 'monthnum', 'week', 'day', 'year' ) as $tl_key ) {
		if ( $this->arg_isset( $tl_key ) ) {
			$tl_query[ $tl_key ] = $this->args[ $tl_key ];
			unset( $this->args[ $tl_key ] );
		}
	}

	if ( $tl_query ) {
		$tl_query['column'] = 'date_created_gmt';
		$date_queries[]     = $tl_query;
	}

	if ( $date_queries ) {
		if ( ! $this->arg_isset( 'date_query' ) ) {
			$this->args['date_query'] = array();
		}

		$this->args['date_query'] = array_merge(
			array( 'relation' => 'AND' ),
			$date_queries,
			$this->args['date_query']
		);
	}

	$this->process_date_query_columns();
}