get_file_data

函数
get_file_data ( $file, $default_headers, $context = '' )
参数
  • (string) $file Absolute path to the file.
    Required:
  • (array) $default_headers List of headers, in the format `array( 'HeaderKey' => 'Header Name' )`.
    Required:
  • (string) $context Optional. If specified adds filter hook {@see 'extra_$context_headers'}. Default empty.
    Required:
    Default: (empty)
返回值
  • (string[]) Array of file header values keyed by header name.
定义位置
相关方法
get_inline_datawp_get_pomo_file_dataget_theme_dataget_userdataget_metadata
引入
2.9.0
弃用
-

get_file_data: 这个函数用来检索一个文件的头信息。头部信息通常包括元数据,如文件的名称、描述和版本。

从文件中检索元数据。

在一个文件的前8KB中搜索元数据,例如一个插件或主题。每条元数据必须在自己的行上。字段不能跨越多行,值将在第一行的末尾被切断。

如果文件数据不在前8KB内,那么作者应该纠正他们的插件文件,并将数据标题移到顶部。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_file_data( $file, $default_headers, $context = '' ) {
// Pull only the first 8 KB of the file in.
$file_data = file_get_contents( $file, false, null, 0, 8 * KB_IN_BYTES );
if ( false === $file_data ) {
$file_data = '';
}
// Make sure we catch CR-only line endings.
$file_data = str_replace( "r", "n", $file_data );
/**
* Filters extra file headers by context.
*
* The dynamic portion of the hook name, `$context`, refers to
* the context where extra headers might be loaded.
*
* @since 2.9.0
*
* @param array $extra_context_headers Empty array by default.
*/
$extra_headers = $context ? apply_filters( "extra_{$context}_headers", array() ) : array();
if ( $extra_headers ) {
$extra_headers = array_combine( $extra_headers, $extra_headers ); // Keys equal values.
$all_headers = array_merge( $extra_headers, (array) $default_headers );
} else {
$all_headers = $default_headers;
}
foreach ( $all_headers as $field => $regex ) {
if ( preg_match( '/^(?:[ t]*<?php)?[ t/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) {
$all_headers[ $field ] = _cleanup_header_comment( $match[1] );
} else {
$all_headers[ $field ] = '';
}
}
return $all_headers;
}
function get_file_data( $file, $default_headers, $context = '' ) { // Pull only the first 8 KB of the file in. $file_data = file_get_contents( $file, false, null, 0, 8 * KB_IN_BYTES ); if ( false === $file_data ) { $file_data = ''; } // Make sure we catch CR-only line endings. $file_data = str_replace( "r", "n", $file_data ); /** * Filters extra file headers by context. * * The dynamic portion of the hook name, `$context`, refers to * the context where extra headers might be loaded. * * @since 2.9.0 * * @param array $extra_context_headers Empty array by default. */ $extra_headers = $context ? apply_filters( "extra_{$context}_headers", array() ) : array(); if ( $extra_headers ) { $extra_headers = array_combine( $extra_headers, $extra_headers ); // Keys equal values. $all_headers = array_merge( $extra_headers, (array) $default_headers ); } else { $all_headers = $default_headers; } foreach ( $all_headers as $field => $regex ) { if ( preg_match( '/^(?:[ t]*<?php)?[ t/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) { $all_headers[ $field ] = _cleanup_header_comment( $match[1] ); } else { $all_headers[ $field ] = ''; } } return $all_headers; }
function get_file_data( $file, $default_headers, $context = '' ) {
	// Pull only the first 8 KB of the file in.
	$file_data = file_get_contents( $file, false, null, 0, 8 * KB_IN_BYTES );

	if ( false === $file_data ) {
		$file_data = '';
	}

	// Make sure we catch CR-only line endings.
	$file_data = str_replace( "r", "n", $file_data );

	/**
	 * Filters extra file headers by context.
	 *
	 * The dynamic portion of the hook name, `$context`, refers to
	 * the context where extra headers might be loaded.
	 *
	 * @since 2.9.0
	 *
	 * @param array $extra_context_headers Empty array by default.
	 */
	$extra_headers = $context ? apply_filters( "extra_{$context}_headers", array() ) : array();
	if ( $extra_headers ) {
		$extra_headers = array_combine( $extra_headers, $extra_headers ); // Keys equal values.
		$all_headers   = array_merge( $extra_headers, (array) $default_headers );
	} else {
		$all_headers = $default_headers;
	}

	foreach ( $all_headers as $field => $regex ) {
		if ( preg_match( '/^(?:[ t]*<?php)?[ t/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) {
			$all_headers[ $field ] = _cleanup_header_comment( $match[1] );
		} else {
			$all_headers[ $field ] = '';
		}
	}

	return $all_headers;
}

常见问题

FAQs
查看更多 >