| 1 |
<?php |
| 2 |
/** |
| 3 |
* Trait WordPress\Plugin_Check\Traits\License_Utils |
| 4 |
* |
| 5 |
* @package plugin-check |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WordPress\Plugin_Check\Traits; |
| 9 |
|
| 10 |
/** |
| 11 |
* Trait for license utilities. |
| 12 |
* |
| 13 |
* @since 1.3.0 |
| 14 |
*/ |
| 15 |
trait License_Utils { |
| 16 |
|
| 17 |
/** |
| 18 |
* Returns normalized license. |
| 19 |
* |
| 20 |
* @since 1.3.0 |
| 21 |
* |
| 22 |
* @param string $license The license to normalize. |
| 23 |
* @return string Normalized license. |
| 24 |
*/ |
| 25 |
protected function get_normalized_license( $license ) { |
| 26 |
$license = trim( $license ); |
| 27 |
$license = str_replace( ' ', ' ', $license ); |
| 28 |
|
| 29 |
// Remove some strings at the end. |
| 30 |
$strings_to_remove = array( |
| 31 |
'.', |
| 32 |
'http://www.gnu.org/licenses/old-licenses/gpl-2.0.html', |
| 33 |
'https://www.gnu.org/licenses/old-licenses/gpl-2.0.html', |
| 34 |
'https://www.gnu.org/licenses/gpl-3.0.html', |
| 35 |
' or later', |
| 36 |
'-or-later', |
| 37 |
'+', |
| 38 |
); |
| 39 |
|
| 40 |
foreach ( $strings_to_remove as $string_to_remove ) { |
| 41 |
$position = strrpos( $license, $string_to_remove ); |
| 42 |
|
| 43 |
if ( false !== $position ) { |
| 44 |
// To remove from the end, the string to remove must be at the end. |
| 45 |
if ( $position + strlen( $string_to_remove ) === strlen( $license ) ) { |
| 46 |
$license = trim( substr( $license, 0, $position ) ); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// Versions. |
| 52 |
$license = str_replace( '-', '', $license ); |
| 53 |
$license = str_replace( 'GNU General Public License (GPL)', 'GPL', $license ); |
| 54 |
$license = str_replace( 'GNU General Public License', 'GPL', $license ); |
| 55 |
$license = str_replace( ' version ', 'v', $license ); |
| 56 |
$license = preg_replace( '/GPL\s*[-|\.]*\s*[v]?([0-9])(\.[0])?/i', 'GPL$1', $license, 1 ); |
| 57 |
$license = preg_replace( '/Apache.*?([0-9])(\.[0])?/i', 'Apache$1', $license ); |
| 58 |
$license = str_replace( '.', '', $license ); |
| 59 |
|
| 60 |
return $license; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Checks if the license is valid identifier. |
| 65 |
* |
| 66 |
* @since 1.3.0 |
| 67 |
* |
| 68 |
* @param string $license License text. |
| 69 |
* @return bool true if the license is valid identifier, otherwise false. |
| 70 |
*/ |
| 71 |
protected function is_license_valid_identifier( $license ) { |
| 72 |
$match = preg_match( '/^([a-z0-9\-\+\.]+)(\sor\s([a-z0-9\-\+\.]+))*$/i', $license ); |
| 73 |
|
| 74 |
return ( false === $match || 0 === $match ) ? false : true; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Checks if the license is GPL compatible. |
| 79 |
* |
| 80 |
* @since 1.3.0 |
| 81 |
* |
| 82 |
* @param string $license License text. |
| 83 |
* @return bool true if the license is GPL compatible, otherwise false. |
| 84 |
*/ |
| 85 |
protected function is_license_gpl_compatible( $license ) { |
| 86 |
$match = preg_match( '/GPL|GNU|MIT|FreeBSD|New BSD|BSD-3-Clause|BSD 3 Clause|OpenLDAP|Expat|Apache2|MPL20|ISC/im', $license ); |
| 87 |
|
| 88 |
return ( false === $match || 0 === $match ) ? false : true; |
| 89 |
} |
| 90 |
} |
| 91 |
|