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內,那麼作者應該糾正他們的外掛檔案,並將資料標題移到頂部。

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
檢視更多 >