Make WordPress Core

Ticket #42517: refactor_get_file_data_regex.patch

File refactor_get_file_data_regex.patch, 3.4 KB (added by gschoppe, 7 years ago)

refactoring of get_file_data regex to support single line headers, as seen in template files

  • src/wp-includes/functions.php

    From 867bb01a76500482abcea69d6930e8086d7650af Mon Sep 17 00:00:00 2001
    From: Gregory Schoppe <gschoppe@gmail.com>
    Date: Sun, 12 Nov 2017 00:32:38 -0500
    Subject: [PATCH 1/2] add support for single line headers to get_file_data()
    
    ---
     src/wp-includes/functions.php | 9 ++++++++-
     1 file changed, 8 insertions(+), 1 deletion(-)
    
    diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php
    index b5d21285b6..59215e2510 100644
    a b function get_file_data( $file, $default_headers, $context = '' ) { 
    48584858        }
    48594859
    48604860        foreach ( $all_headers as $field => $regex ) {
    4861                 if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] )
     4861                /* ^(?:\s*<\?(?:php)?\s*)?[ \t\/*#@]*Template Name:([^\/*?]*)(?:\*\/)?\s*(?:\?>)?\s*$ */
     4862                $rexex  = '\s*(?:<\?(?:php)?)?\s*'; // optional opening php tag (long or short)
     4863                $regex .= '[ \t\/*#@]*'; // optional comments, tabs or @ symbols
     4864                $regex .= preg_quote( $regex, '/' ) . ':'; // the header we are searching for
     4865                $regex .= '([^\/*?]*)'; // capture group for header names
     4866                $regex .= '\s*(?:\*\/)?'; // optional end of multiline comment
     4867                $regex .= '\s*(?:\?>)?\s*'; //optional closing php tag, long or short
     4868                if ( preg_match( '/^' . $regex . '$/mi', $file_data, $match ) && $match[1] )
    48624869                        $all_headers[ $field ] = _cleanup_header_comment( $match[1] );
    48634870                else
    48644871                        $all_headers[ $field ] = '';
  • src/wp-includes/functions.php

    -- 
    2.12.0.windows.1
    
    
    From ea0cfa80861d15dcd9439d05f1f9c54b6e95c217 Mon Sep 17 00:00:00 2001
    From: Gregory Schoppe <gschoppe@gmail.com>
    Date: Sun, 12 Nov 2017 08:04:02 -0500
    Subject: [PATCH 2/2] fix regex
    
    ---
     src/wp-includes/functions.php | 16 ++++++++--------
     1 file changed, 8 insertions(+), 8 deletions(-)
    
    diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php
    index 59215e2510..0f4be879f6 100644
    a b function get_file_data( $file, $default_headers, $context = '' ) { 
    48584858        }
    48594859
    48604860        foreach ( $all_headers as $field => $regex ) {
    4861                 /* ^(?:\s*<\?(?:php)?\s*)?[ \t\/*#@]*Template Name:([^\/*?]*)(?:\*\/)?\s*(?:\?>)?\s*$ */
    4862                 $rexex  = '\s*(?:<\?(?:php)?)?\s*'; // optional opening php tag (long or short)
    4863                 $regex .= '[ \t\/*#@]*'; // optional comments, tabs or @ symbols
    4864                 $regex .= preg_quote( $regex, '/' ) . ':'; // the header we are searching for
    4865                 $regex .= '([^\/*?]*)'; // capture group for header names
    4866                 $regex .= '\s*(?:\*\/)?'; // optional end of multiline comment
    4867                 $regex .= '\s*(?:\?>)?\s*'; //optional closing php tag, long or short
    4868                 if ( preg_match( '/^' . $regex . '$/mi', $file_data, $match ) && $match[1] )
     4861                $pattern  = '[ \t]*(?:<\?(?:php)?)?[ \t]*'; // optional opening php tag (long or short)
     4862                $pattern .= '[ \t\/*#@]*'; // optional comments, tabs or @ symbols
     4863                $pattern .= preg_quote( $regex, '/' ); // the header we are searching for
     4864                $pattern .= '[ \t]*:[ \t]*'; // our colon, wrapped in optional whitespace
     4865                $pattern .= '(.*?)'; // capture group for header names (non-greedy)
     4866                $pattern .= '[ \t]*(?:\*\/)?'; // optional end of multiline comment
     4867                $pattern .= '[ \t]*(?:\?>)?[ \t]*'; //optional closing php tag, long or short
     4868                if ( preg_match( '/^' . $pattern . '$/mi', $file_data, $match ) && $match[1] )
    48694869                        $all_headers[ $field ] = _cleanup_header_comment( $match[1] );
    48704870                else
    48714871                        $all_headers[ $field ] = '';