| 1 |
<?php |
| 2 |
/** |
| 3 |
* Trait WordPress\Plugin_Check\Traits\Find_Readme |
| 4 |
* |
| 5 |
* @package plugin-check |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WordPress\Plugin_Check\Traits; |
| 9 |
|
| 10 |
/** |
| 11 |
* Trait for find readme. |
| 12 |
* |
| 13 |
* @since 1.0.0 |
| 14 |
*/ |
| 15 |
trait Find_Readme { |
| 16 |
|
| 17 |
/** |
| 18 |
* Filter the given array of files for readme files (readme.txt or readme.md). |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
* |
| 22 |
* @param array $files Array of file files to be filtered. |
| 23 |
* @param string $plugin_relative_path Plugin relative path. |
| 24 |
* @return array An array containing readme.txt or readme.md files, or an empty array if none are found. |
| 25 |
*/ |
| 26 |
protected function filter_files_for_readme( array $files, $plugin_relative_path ) { |
| 27 |
// Find the readme file. |
| 28 |
$readme_list = preg_grep( '/\/readme\.(txt|md)$/i', $files ); |
| 29 |
|
| 30 |
// Filter the readme files located at root. |
| 31 |
$potential_readme_files = array_filter( |
| 32 |
$readme_list, |
| 33 |
function ( $file ) use ( $plugin_relative_path ) { |
| 34 |
$file = str_replace( $plugin_relative_path, '', $file ); |
| 35 |
return ! str_contains( $file, '/' ); |
| 36 |
} |
| 37 |
); |
| 38 |
|
| 39 |
// If the readme file does not exist, then return empty array. |
| 40 |
if ( empty( $potential_readme_files ) ) { |
| 41 |
return array(); |
| 42 |
} |
| 43 |
|
| 44 |
// Find the .txt versions of the readme files. |
| 45 |
$readme_txt = array_filter( |
| 46 |
$potential_readme_files, |
| 47 |
function ( $file ) { |
| 48 |
return preg_match( '/^readme\.txt$/i', basename( $file ) ); |
| 49 |
} |
| 50 |
); |
| 51 |
|
| 52 |
return $readme_txt ? $readme_txt : $potential_readme_files; |
| 53 |
} |
| 54 |
} |
| 55 |
|