Make WordPress Core

Ticket #42513: refactor_class_wp_theme_get_post_templates.patch

File refactor_class_wp_theme_get_post_templates.patch, 2.4 KB (added by gschoppe, 7 years ago)

Patch refactoring WP_Theme::get_post_templates() to use get_file_data

  • src/wp-includes/class-wp-theme.php

    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 { 
    10261026         *               with the value of the translated header name.
    10271027         */
    10281028        public function get_post_templates() {
     1029                $template_file_headers = array(
     1030                        'Name'       => 'Template Name',
     1031                        'Post Types' => 'Template Post Type'
     1032                );
    10291033                // If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
    10301034                if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) {
    10311035                        return array();
    10321036                }
    1033 
    10341037                $post_templates = $this->cache_get( 'post_templates' );
    1035 
    10361038                if ( ! is_array( $post_templates ) ) {
    10371039                        $post_templates = array();
    1038 
    10391040                        $files = (array) $this->get_files( 'php', 1, true);
    1040 
    10411041                        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'] ) ) {
    10431044                                        continue;
    10441045                                }
    10451046
    10461047                                $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'] );
    10491050                                }
    10501051
    10511052                                foreach ( $types as $type ) {
    10521053                                        $type = sanitize_key( $type );
     1054                                        if( ! $type ) {
     1055                                                continue;
     1056                                        }
    10531057                                        if ( ! isset( $post_templates[ $type ] ) ) {
    10541058                                                $post_templates[ $type ] = array();
    10551059                                        }
    1056 
    1057                                         $post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] );
     1060                                        $post_templates[ $type ][ $file ] = $headers['Name'];
    10581061                                }
    10591062                        }
    1060 
    10611063                        $this->cache_add( 'post_templates', $post_templates );
    10621064                }
    1063 
    10641065                if ( $this->load_textdomain() ) {
    10651066                        foreach ( $post_templates as &$post_type ) {
    10661067                                foreach ( $post_type as &$post_template ) {