Automattic\WooCommerce\Internal\Admin\Reports

HposLegacyOrderReportQueryBuilder::translate_legacy_sql_fragment │ private │ WC 1.0

Translate legacy posts.<col> (and bare ID / post_date) references in an arbitrary SQL fragment.

Matches are bounded by \b so tokens embedded in longer identifiers (e.g. product_ID, posts.post_date_gmt) are left untouched.

Method of the class: HposLegacyOrderReportQueryBuilder{}

No Hooks.

Returns

string. Translated fragment safe to drop into an HPOS query.

Usage

// private - for code of main (parent) class only
$result = $this->translate_legacy_sql_fragment( $fragment ): string;
$fragment(string) (required)
Caller-supplied SQL fragment.

HposLegacyOrderReportQueryBuilder::translate_legacy_sql_fragment() code WC 11.1.2

private function translate_legacy_sql_fragment( string $fragment ): string {
	$map = $this->legacy_to_hpos_column_map();

	// Qualified `posts.<col>` references first, then the bare tokens legacy
	// callers use unqualified. Callbacks avoid `$`/`\` replacement-string pitfalls.
	$fragment = (string) preg_replace_callback(
		'/\bposts\.(ID|post_date|post_parent|post_status|post_type)\b/',
		static function ( $matches ) use ( $map ) {
			return $map[ $matches[1] ];
		},
		$fragment
	);

	return (string) preg_replace_callback(
		'/\b(ID|post_date)\b/',
		static function ( $matches ) use ( $map ) {
			return $map[ $matches[1] ];
		},
		$fragment
	);
}