Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableQuery::sanitize_order_orderby()privateWC 1.0

Parses and sanitizes the 'orderby' query var.

{} It's a method of the class: OrdersTableQuery{}

No Hooks.

Return

null. Nothing.

Usage

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

Code of OrdersTableQuery::sanitize_order_orderby() WC 7.1.0

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;
	}

	if ( is_string( $orderby ) ) {
		$orderby = array( $orderby => $order );
	}

	$this->args['orderby'] = array();
	foreach ( $orderby as $order_key => $order ) {
		if ( isset( $mapping[ $order_key ] ) ) {
			$this->args['orderby'][ $mapping[ $order_key ] ] = $this->sanitize_order( $order );
		}
	}
}