Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableQuery::sanitize_order_orderby()privateWC 1.0

Parses and sanitizes the 'orderby' query var.

Method of the class: OrdersTableQuery{}

No Hooks.

Return

null. Nothing (null).

Usage

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

OrdersTableQuery::sanitize_order_orderby() code WC 9.3.3

private function sanitize_order_orderby(): void {
	// Allowed keys.
	// TODO: rand, meta keys, etc.
	$allowed_keys = array( 'ID', 'id', 'type', 'date', 'modified', 'parent' );

	// 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->args['order'] ?? '';
	$orderby = $this->args['orderby'] ?? '';

	if ( 'none' === $orderby ) {
		return;
	}

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

	if ( is_string( $orderby ) ) {
		$orderby_fields = array_map( 'trim', explode( ' ', $orderby ) );
		$orderby        = array();
		foreach ( $orderby_fields as $field ) {
			$orderby[ $field ] = $order;
		}
	}

	$allowed_orderby = array_merge(
		array_keys( $mapping ),
		array_values( $mapping ),
		$this->meta_query ? $this->meta_query->get_orderby_keys() : array()
	);

	$this->args['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 ];
		}

		$this->args['orderby'][ $order_key ] = $this->sanitize_order( $order );
	}
}