wp的get_file_data获取css文件申明信息

通过get_file_data函数读取css文件内头部申明。

示例:style.css文件内的申明信息。

/*
Theme Name: Twenty Ten
Theme URI: wordpress.org
Description: The 2010 theme for WordPress is stylish
Author: the WordPress team
Version: 1.1
Tags: black, blue, white
*/

get_file_data函数:

/**
* Parse the file contents to retrieve its metadata.
*
* Searches for metadata for a file, such as a plugin or theme. Each piece of
* metadata must be on its own line. For a field spanning multple lines, it
* must not have any newlines or only parts of it will be displayed.
*
* Some users have issues with opening large files and manipulating the contents
* for want is usually the first 1kiB or 2kiB. This function stops pulling in
* the file contents when it has all of the required data.
*
* The first 8kiB of the file will be pulled in and if the file data is not
* within that first 8kiB, then the author should correct their plugin file
* and move the data headers to the top.
*
* The file is assumed to have permissions to allow for scripts to read
* the file. This is not checked however and the file is only opened for
* reading.
*
* @since 2.9.0
*
* @param string $file Path to the file
* @param bool $markup If the returned data should have HTML markup applied
* @param string $context If specified adds filter hook “extra_<$context>_headers”
*/

function get_file_data( $file, $default_headers, $context = '' ) {
	// We don't need to write to the file, so just open for reading.
	$fp = fopen( $file, 'r' );

	// Pull only the first 8kiB of the file in.
	$file_data = fread( $fp, 8192 );

	// PHP will close file handle, but we are good citizens.
	fclose( $fp );

	if ( $context != '' ) {
		$extra_headers = apply_filters( "extra_$context".'_headers', array() );

		$extra_headers = array_flip( $extra_headers );
		foreach( $extra_headers as $key=>$value ) {
			$extra_headers[$key] = $key;
		}
		$all_headers = array_merge($extra_headers, $default_headers);
	} else {
		$all_headers = $default_headers;
	}

	foreach ( $all_headers as $field => $regex ) {
		preg_match( '/' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, ${$field});
		if ( !empty( ${$field} ) )
			${$field} = _cleanup_header_comment( ${$field}[1] );
		else
			${$field} = '';
	}

	$file_data = compact( array_keys( $all_headers ) );

	return $file_data;
}

/**
 * Strip close comment and close php tags from file headers used by WP
 * See http://core.trac.wordpress.org/ticket/8497
 *
 * @since 2.8.0
 *
 * @param string $str
 * @return string
 */
function _cleanup_header_comment($str) {
	return trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $str));
}


/**
* Retrieve theme data from parsed theme file.
*
* The description will have the tags filtered with the following HTML elements
* whitelisted. The ‘a’ element with the href and title
* attributes. The abbr element with the title attribute. The
* acronym element with the title attribute allowed. The
*
code, em, and strong elements also allowed.
*
* The style.css file must contain theme name, theme URI, and description. The
* data can also contain author URI, author, template (parent template),
* version, status, and finally tags. Some of these are not used by WordPress
* administration panels, but are used by theme directory web sites which list
* the theme.
*
* @since 1.5.0
*
* @param string $theme_file Theme file path.
* @return array Theme data.
*/


function get_theme_data( $theme_file ) {
	$default_headers = array(
		'Name' => 'Theme Name',
		'URI' => 'Theme URI',
		'Description' => 'Description',
		'Author' => 'Author',
		'AuthorURI' => 'Author URI',
		'Version' => 'Version',
		'Template' => 'Template',
		'Status' => 'Status',
		'Tags' => 'Tags'
		);

	$themes_allowed_tags = array(
		'a' => array(
			'href' => array(),'title' => array()
			),
		'abbr' => array(
			'title' => array()
			),
		'acronym' => array(
			'title' => array()
			),
		'code' => array(),
		'em' => array(),
		'strong' => array()
	);

	$theme_data = get_file_data( $theme_file, $default_headers, 'theme' );

	$theme_data['Name'] = $theme_data['Title'] = wp_kses( $theme_data['Name'], $themes_allowed_tags );

	$theme_data['URI'] = esc_url( $theme_data['URI'] );

	$theme_data['Description'] = wptexturize( wp_kses( $theme_data['Description'], $themes_allowed_tags ) );

	$theme_data['AuthorURI'] = esc_url( $theme_data['AuthorURI'] );

	$theme_data['Template'] = wp_kses( $theme_data['Template'], $themes_allowed_tags );

	$theme_data['Version'] = wp_kses( $theme_data['Version'], $themes_allowed_tags );

	if ( $theme_data['Status'] == '' )
		$theme_data['Status'] = 'publish';
	else
		$theme_data['Status'] = wp_kses( $theme_data['Status'], $themes_allowed_tags );

	if ( $theme_data['Tags'] == '' )
		$theme_data['Tags'] = array();
	else
		$theme_data['Tags'] = array_map( 'trim', explode( ',', wp_kses( $theme_data['Tags'], array() ) ) );

	if ( $theme_data['Author'] == '' ) {
		$theme_data['Author'] = $theme_data['AuthorName'] = __('Anonymous');
	} else {
		$theme_data['AuthorName'] = wp_kses( $theme_data['Author'], $themes_allowed_tags );
		if ( empty( $theme_data['AuthorURI'] ) ) {
			$theme_data['Author'] = $theme_data['AuthorName'];
		} else {
			$theme_data['Author'] = sprintf( '%3$s', $theme_data['AuthorURI'], __( 'Visit author homepage' ), $theme_data['AuthorName'] );
		}
	}

	return $theme_data;
}

转载请注明出处:https://www.onexin.net/wp-get_file_data-css/

相关文章:

1、WordPress shortcuts keyboard
https://www.onexin.net/wordpress-shortcuts-keyboard/

2、远程图片本地化 For wordpress
https://www.onexin.net/remote-image-localization-for-wordpress/

3、Wordpress自定义时间和日期
https://www.onexin.net/wordpress-custom-time-and-date/

Leave a Reply