Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails
WCEmailTemplateSyncRegistry::parse_version_header
Parse the @version header from a block email template file.
Mirrors \WC_Admin_Status::get_file_version(): WordPress' native get_file_data() only understands Name: Value headers, but the email-editor templates (and the wider WooCommerce template contract) document their version as a PHPDoc @version X.Y.Z tag, which is whitespace-separated. We reuse the exact regex used by the existing helper so core and third-party templates are parsed consistently.
Method of the class: WCEmailTemplateSyncRegistry{}
No Hooks.
Returns
String. The parsed version, or an empty string if none is declared.
Usage
$result = WCEmailTemplateSyncRegistry::parse_version_header( $file ): string;
- $file(string) (required)
- Absolute path to the template file.
Changelog
| Since 10.8.0 | Introduced. |
WCEmailTemplateSyncRegistry::parse_version_header() WCEmailTemplateSyncRegistry::parse version header code WC 10.8.1
public static function parse_version_header( string $file ): string {
if ( ! is_readable( $file ) ) {
return '';
}
// Only read the first 8KiB — headers are always near the top of the file.
$handle = fopen( $file, 'r' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
if ( false === $handle ) {
return '';
}
$contents = fread( $handle, 8192 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread
fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
if ( false === $contents ) {
return '';
}
// Normalize CR-only line endings so the multi-line regex behaves consistently.
$contents = str_replace( "\r", "\n", $contents );
/*
* Matches a PHPDoc-style `@version X.Y.Z` tag allowing the usual
* comment-leader characters (` * `, `#`, `@`, tabs) before it, on any
* line in the header block. Identical to the long-standing pattern in
* WC_Admin_Status::get_file_version() — see docblock above for why
* get_file_data() isn't a drop-in replacement here.
*/
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( '@version', '/' ) . '(.*)$/mi', $contents, $match ) && ! empty( $match[1] ) ) {
return trim( _cleanup_header_comment( $match[1] ) );
}
return '';
}