From ab48f3ea5144619240a58302812e47a2c4c4cf8a Mon Sep 17 00:00:00 2001
From: Gregory Schoppe <gschoppe@gmail.com>
Date: Sat, 11 Nov 2017 08:52:56 -0500
Subject: [PATCH] refactor WP_Theme->get_post_templates() to use
get_file_data()
---
src/wp-includes/class-wp-theme.php | 23 ++++++++++++-----------
1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/src/wp-includes/class-wp-theme.php b/src/wp-includes/class-wp-theme.php
index 171d8b6c58..577cf82984 100644
a
|
b
|
final class WP_Theme implements ArrayAccess { |
1026 | 1026 | * with the value of the translated header name. |
1027 | 1027 | */ |
1028 | 1028 | public function get_post_templates() { |
| 1029 | $template_file_headers = array( |
| 1030 | 'Name' => 'Template Name', |
| 1031 | 'Post Types' => 'Template Post Type' |
| 1032 | ); |
1029 | 1033 | // If you screw up your current theme and we invalidate your parent, most things still work. Let it slide. |
1030 | 1034 | if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) { |
1031 | 1035 | return array(); |
1032 | 1036 | } |
1033 | | |
1034 | 1037 | $post_templates = $this->cache_get( 'post_templates' ); |
1035 | | |
1036 | 1038 | if ( ! is_array( $post_templates ) ) { |
1037 | 1039 | $post_templates = array(); |
1038 | | |
1039 | 1040 | $files = (array) $this->get_files( 'php', 1, true); |
1040 | | |
1041 | 1041 | foreach ( $files as $file => $full_path ) { |
1042 | | if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) { |
| 1042 | $headers = get_file_data( $full_path, $template_file_headers, 'post_templates' ); |
| 1043 | if ( empty( $headers['Name'] ) ) { |
1043 | 1044 | continue; |
1044 | 1045 | } |
1045 | 1046 | |
1046 | 1047 | $types = array( 'page' ); |
1047 | | if ( preg_match( '|Template Post Type:(.*)$|mi', file_get_contents( $full_path ), $type ) ) { |
1048 | | $types = explode( ',', _cleanup_header_comment( $type[1] ) ); |
| 1048 | if ( ! empty( $headers['Post Types'] ) ) { |
| 1049 | $types = explode( ',', $headers['Post Types'] ); |
1049 | 1050 | } |
1050 | 1051 | |
1051 | 1052 | foreach ( $types as $type ) { |
1052 | 1053 | $type = sanitize_key( $type ); |
| 1054 | if( ! $type ) { |
| 1055 | continue; |
| 1056 | } |
1053 | 1057 | if ( ! isset( $post_templates[ $type ] ) ) { |
1054 | 1058 | $post_templates[ $type ] = array(); |
1055 | 1059 | } |
1056 | | |
1057 | | $post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] ); |
| 1060 | $post_templates[ $type ][ $file ] = $headers['Name']; |
1058 | 1061 | } |
1059 | 1062 | } |
1060 | | |
1061 | 1063 | $this->cache_add( 'post_templates', $post_templates ); |
1062 | 1064 | } |
1063 | | |
1064 | 1065 | if ( $this->load_textdomain() ) { |
1065 | 1066 | foreach ( $post_templates as &$post_type ) { |
1066 | 1067 | foreach ( $post_type as &$post_template ) { |