WordPress.org

Make WordPress Core

Opened 4 months ago

Last modified 2 months ago

#40933 new enhancement

Improvements for get_file_data() function

Reported by: Tkama Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.5
Component: Formatting Keywords: has-patch
Focuses: template Cc:

Description

It seams as a bug or as imperfection at least.

I try to explain in example.

Assume we have such comments in file:

<?php
/*
Plugin Name: My plug
Version: 1.0
*/

// and trying to get file data like:
$data = get_file_data( __FILE__, ['ver'=>'Version'] );
echo $data['ver']; //> 1.0

This is works as we need.

BUT if we change comments a little, the get_file_data() becomes broken:

<?php
/*
Plugin Name : My plug
Version     : 1.0
*/

// and trying to get file data like^
$data = get_file_data( __FILE__, ['ver'=>'Version'] );
echo $data['ver']; //> ''

All we do just add spaces before : to make comments more readable...

-

Solution

I think it's better to improve regular expression of the get_file_data() and add [\t ]* before :

<?php

if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] )

to 

if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . '[\t ]*:(.*)$/mi', $file_data, $match ) && $match[1] )

Attachments (1)

40933.patch (593 bytes) - added by subrataemfluence 3 months ago.

Download all attachments as: .zip

Change History (7)

#1 @Tkama
4 months ago

The same applies to function get_plugin_data(), bacause it relies on get_file_data()

#2 @subrataemfluence
3 months ago

  • Keywords has-patch added

#3 @subrataemfluence
3 months ago

Good catch! :)

#4 follow-up: @Tkama
3 months ago

Thanks a lot! :)

#5 in reply to: ↑ 4 @subrataemfluence
3 months ago

Replying to Tkama:

Thanks a lot! :)

You are welcome:) Would you mind testing the patch and see if it works as desired?
Thank you.

#6 @Tkama
3 months ago

I already tested code I suggested - it works fine!

But, off course if I will see troubles about this issue and give to know...

Last edited 2 months ago by Tkama (previous) (diff)
Note: See TracTickets for help on using tickets.