Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableQuery::sanitize_order_orderbyprivateWC 1.0

Parses and sanitizes the 'orderby' query var.

Method of the class: OrdersTableQuery{}

No Hooks.

Returns

String|Array. The sanitized orderby param which can be a string or an array of orderby keys and direction (ASC, DESC).

Usage

// private - for code of main (parent) class only
$result = $this->sanitize_order_orderby( $orderby );
$orderby(string|array) (required)
The unsanitized orderby param which can be a string or an array of orderby keys and direction (ASC, DESC).

OrdersTableQuery::sanitize_order_orderby() code WC 10.3.6

private function sanitize_order_orderby( $orderby ) {
	// No need to sanitize, will be processed in calling function.
	if ( 'include' === $orderby || 'post__in' === $orderby || 'none' === $orderby ) {
		return $orderby;
	}

	// Translate $orderby to a valid field.
	$mapping = array(
		'ID'            => "{$this->tables['orders']}.id",
		'id'            => "{$this->tables['orders']}.id",
		'type'          => "{$this->tables['orders']}.type",
		'date'          => "{$this->tables['orders']}.date_created_gmt",
		'date_created'  => "{$this->tables['orders']}.date_created_gmt",
		'modified'      => "{$this->tables['orders']}.date_updated_gmt",
		'date_modified' => "{$this->tables['orders']}.date_updated_gmt",
		'parent'        => "{$this->tables['orders']}.parent_order_id",
		'total'         => "{$this->tables['orders']}.total_amount",
		'order_total'   => "{$this->tables['orders']}.total_amount",
	);

	$order           = $this->sanitize_order( $this->args['order'] ?? '' );
	$allowed_orderby = array_merge( array_keys( $mapping ), array_values( $mapping ), $this->meta_query ? $this->meta_query->get_orderby_keys() : array() );

	// Convert string orderby to an array of orderby keys and direction (ASC, DESC).
	if ( is_string( $orderby ) ) {
		$orderby_fields = array_map( 'trim', explode( ' ', $orderby ) );
		$orderby        = array();
		foreach ( $orderby_fields as $field ) {
			$orderby[ $field ] = $order;
		}
	}

	$sanitized_orderby = array();

	foreach ( $orderby as $order_key => $order ) {
		if ( ! in_array( $order_key, $allowed_orderby, true ) ) {
			continue;
		}

		if ( isset( $mapping[ $order_key ] ) ) {
			$order_key = $mapping[ $order_key ];
		}

		$sanitized_orderby[ $order_key ] = $this->sanitize_order( $order );
	}

	return $sanitized_orderby;
}