Skip to content

Commit

Permalink
Merge pull request #2696 from YOURLS/plugin_headers
Browse files Browse the repository at this point in the history
More flexible plugin headers, and tests

Fixes #2685
  • Loading branch information
ozh committed May 24, 2020
2 parents 6dc5423 + c2cc75c commit 2627f9c
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 3 deletions.
26 changes: 23 additions & 3 deletions includes/functions-plugins.php
Expand Up @@ -377,6 +377,27 @@ function yourls_is_active_plugin( $plugin ) {
/**
* Parse a plugin header
*
* The plugin header has the following form:
* /*
* Plugin Name: <plugin name>
* Plugin URI: <plugin home page>
* Description: <plugin description>
* Version: <plugin version number>
* Author: <author name>
* Author URI: <author home page>
* * /
*
* Or in the form of a phpdoc block
* /**
* * Plugin Name: <plugin name>
* * Plugin URI: <plugin home page>
* * Description: <plugin description>
* * Version: <plugin version number>
* * Author: <author name>
* * Author URI: <author home page>
* * /
*
* @since 1.5
* @param string $file Physical path to plugin file
* @return array Array of 'Field'=>'Value' from plugin comment header lines of the form "Field: Value"
*/
Expand All @@ -396,12 +417,11 @@ function yourls_get_plugin_data( $file ) {

$plugin_data = [];
foreach ( $lines as $line ) {
if ( !preg_match( '!(.*?):\s+(.*)!', $line, $matches ) ) {
if ( !preg_match( '!(\s*)?\*?(\s*)?(.*?):\s+(.*)!', $line, $matches ) ) {
continue;
}

list( $null, $field, $value ) = array_map( 'trim', $matches );
$plugin_data[ $field ] = $value;
$plugin_data[ trim($matches[3]) ] = trim($matches[4]);
}

return $plugin_data;
Expand Down
5 changes: 5 additions & 0 deletions tests/data/plugins/headers/header_incomplete.php
@@ -0,0 +1,5 @@
<?php
/**
* Plugin Name: incomplete
* Description: incomplete
*/
5 changes: 5 additions & 0 deletions tests/data/plugins/headers/header_incorrect.php
@@ -0,0 +1,5 @@
<?php
/**
* Plugin Name
* Description
*/
6 changes: 6 additions & 0 deletions tests/data/plugins/headers/header_missing.php
@@ -0,0 +1,6 @@
<?php
/*
*/
9 changes: 9 additions & 0 deletions tests/data/plugins/headers/header_phpdoc.php
@@ -0,0 +1,9 @@
<?php
/**
* Plugin Name: phpdoc
* Plugin URI: phpdoc
* Description: phpdoc
* Version: phpdoc
* Author: phpdoc
* Author URI: phpdoc
*/
9 changes: 9 additions & 0 deletions tests/data/plugins/headers/header_regular.php
@@ -0,0 +1,9 @@
<?php
/*
Plugin Name: regular
Plugin URI: regular
Description: regular
Version: regular
Author: regular
Author URI: regular
*/
67 changes: 67 additions & 0 deletions tests/tests/plugins/headers.php
@@ -0,0 +1,67 @@
<?php

/**
* Header plugin functions
*
* @group plugins
* @since 0.1
*/
class Plugin_Header_Tests extends PHPUnit_Framework_TestCase {

/**
* Regular header
*/
public function test_regular_header() {
$expected = array(
'Plugin Name' => 'regular',
'Plugin URI' => 'regular',
'Description' => 'regular',
'Version' => 'regular',
'Author' => 'regular',
'Author URI' => 'regular',
);
$this->assertEquals($expected, yourls_get_plugin_data( YOURLS_PLUGINDIR . '/headers/header_regular.php' ) );
}

/**
* PHPDoc header
*/
public function test_phpdoc_header() {
$expected = array(
'Plugin Name' => 'phpdoc',
'Description' => 'phpdoc',
'Plugin URI' => 'phpdoc',
'Version' => 'phpdoc',
'Author' => 'phpdoc',
'Author URI' => 'phpdoc',
);
$this->assertEquals($expected, yourls_get_plugin_data( YOURLS_PLUGINDIR . '/headers/header_phpdoc.php' ) );
}

/**
* Incomplete header
*/
public function test_incomplete_header() {
$expected = array(
'Plugin Name' => 'incomplete',
'Description' => 'incomplete',
);
$this->assertEquals($expected, yourls_get_plugin_data( YOURLS_PLUGINDIR . '/headers/header_incomplete.php' ) );

}

/**
* Incorrect header
*/
public function test_incorrect_header() {
$this->assertEquals( [] , yourls_get_plugin_data( YOURLS_PLUGINDIR . '/headers/header_incorrect.php' ) );
}

/**
* Missing header
*/
public function test_missing_header() {
$this->assertEquals( [] , yourls_get_plugin_data( YOURLS_PLUGINDIR . '/headers/header_missing.php' ) );
}

}

0 comments on commit 2627f9c

Please sign in to comment.