'Header Name'). * * @return array Array of file headers in `HeaderKey => Header Value` format. */ public static function get_file_data( $file, $headers ) { // We don't need to write to the file, so just open for reading. $fp = fopen( $file, 'rb' ); // 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 ); // Make sure we catch CR-only line endings. $file_data = str_replace( "\r", "\n", $file_data ); return static::get_file_data_from_string( $file_data, $headers ); } /** * Retrieves metadata from a string. * * @param string $string String to look for metadata in. * @param array $headers List of headers. * * @return array Array of file headers in `HeaderKey => Header Value` format. */ public static function get_file_data_from_string( $string, $headers ) { foreach ( $headers as $field => $regex ) { if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $string, $match ) && $match[1] ) { $headers[ $field ] = static::_cleanup_header_comment( $match[1] ); } else { $headers[ $field ] = ''; } } return $headers; } /** * Strip close comment and close php tags from file headers used by WP. * * @see _cleanup_header_comment() * * @param string $str Header comment to clean up. * * @return string */ protected static function _cleanup_header_comment( $str ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore -- Not changing because third-party commands might use/extend. return trim( preg_replace( '/\s*(?:\*\/|\?>).*/', '', $str ) ); } }