| 1 |
<?php |
| 2 |
/** |
| 3 |
* Libraries handling |
| 4 |
* |
| 5 |
* Handles all libraries (vendor) operations and versioning. |
| 6 |
* |
| 7 |
* @package Libraries |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace POSessions\Library; |
| 13 |
|
| 14 |
use POSessions\System\L10n; |
| 15 |
|
| 16 |
/** |
| 17 |
* Define the libraries functionality. |
| 18 |
* |
| 19 |
* Handles all libraries (vendor) operations and versioning. |
| 20 |
* |
| 21 |
* @package System |
| 22 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
class Libraries { |
| 26 |
|
| 27 |
/** |
| 28 |
* The array of PSR-4 libraries used by the plugin. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* @access private |
| 32 |
* @var array $libraries The PSR-4 libraries used by the plugin. |
| 33 |
*/ |
| 34 |
private static $psr4_libraries; |
| 35 |
|
| 36 |
/** |
| 37 |
* The array of mono libraries used by the plugin. |
| 38 |
* |
| 39 |
* @since 1.0.0 |
| 40 |
* @access private |
| 41 |
* @var array $libraries The mono libraries used by the plugin. |
| 42 |
*/ |
| 43 |
private static $mono_libraries; |
| 44 |
|
| 45 |
/** |
| 46 |
* Initializes the class and set its properties. |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
*/ |
| 50 |
public function __construct() { |
| 51 |
self::init(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Defines all needed libraries. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
*/ |
| 59 |
public static function init() { |
| 60 |
self::$psr4_libraries = []; |
| 61 |
self::$psr4_libraries['feather'] = [ |
| 62 |
'name' => 'Feather', |
| 63 |
'prefix' => 'Feather', |
| 64 |
'base' => POSE_VENDOR_DIR . 'feather/', |
| 65 |
'version' => '4.24.1', |
| 66 |
// phpcs:ignore |
| 67 |
'author' => sprintf( esc_html__( '%s & contributors', 'sessions' ), 'Cole Bemis' ), |
| 68 |
'url' => 'https://feathericons.com', |
| 69 |
'license' => 'mit', |
| 70 |
'langs' => 'en', |
| 71 |
]; |
| 72 |
self::$psr4_libraries['markdown'] = [ |
| 73 |
'name' => 'Markdown Parser', |
| 74 |
'prefix' => 'cebe\markdownparser', |
| 75 |
'base' => POSE_VENDOR_DIR . 'markdown/', |
| 76 |
'version' => '1.2.1', |
| 77 |
// phpcs:ignore |
| 78 |
'author' => sprintf( esc_html__( '%s & contributors', 'sessions' ), 'Carsten Brandt' ), |
| 79 |
'url' => 'https://github.com/cebe/markdown', |
| 80 |
'license' => 'mit', |
| 81 |
'langs' => 'en', |
| 82 |
]; |
| 83 |
self::$psr4_libraries['psr-03'] = [ |
| 84 |
'name' => 'PSR-3', |
| 85 |
'prefix' => 'Psr\\Log', |
| 86 |
'base' => POSE_VENDOR_DIR . 'psr/log/', |
| 87 |
'version' => '', |
| 88 |
'author' => 'PHP Framework Interop Group', |
| 89 |
'url' => 'https://www.php-fig.org/', |
| 90 |
'license' => 'mit', |
| 91 |
'langs' => 'en', |
| 92 |
]; |
| 93 |
self::$psr4_libraries['psr-07'] = [ |
| 94 |
'name' => 'PSR-7', |
| 95 |
'prefix' => 'Psr\\Http\\Message', |
| 96 |
'base' => POSE_VENDOR_DIR . 'psr/http-message/', |
| 97 |
'version' => '', |
| 98 |
'author' => 'PHP Framework Interop Group', |
| 99 |
'url' => 'https://www.php-fig.org/', |
| 100 |
'license' => 'mit', |
| 101 |
'langs' => 'en', |
| 102 |
]; |
| 103 |
self::$psr4_libraries['psr-18'] = [ |
| 104 |
'name' => 'PSR-18', |
| 105 |
'prefix' => 'Psr\\Http\\Client', |
| 106 |
'base' => POSE_VENDOR_DIR . 'psr/http-client/', |
| 107 |
'version' => '', |
| 108 |
'author' => 'PHP Framework Interop Group', |
| 109 |
'url' => 'https://www.php-fig.org/', |
| 110 |
'license' => 'mit', |
| 111 |
'langs' => 'en', |
| 112 |
]; |
| 113 |
self::$psr4_libraries['decalog-sdk'] = [ |
| 114 |
'name' => 'DecaLog SDK', |
| 115 |
'prefix' => 'DecaLog', |
| 116 |
'base' => POSE_VENDOR_DIR . 'decalog-sdk/', |
| 117 |
'version' => '1.2.0', |
| 118 |
'author' => 'Pierre Lannoy', |
| 119 |
'url' => 'https://github.com/Pierre-Lannoy/wp-decalog-sdk', |
| 120 |
'license' => 'mit', |
| 121 |
'langs' => 'en', |
| 122 |
]; |
| 123 |
self::$mono_libraries = []; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get PSR-4 libraries. |
| 128 |
* |
| 129 |
* @return array The list of defined PSR-4 libraries. |
| 130 |
* @since 1.0.0 |
| 131 |
*/ |
| 132 |
public static function get_psr4() { |
| 133 |
return self::$psr4_libraries; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get mono libraries. |
| 138 |
* |
| 139 |
* @return array The list of defined mono libraries. |
| 140 |
* @since 1.0.0 |
| 141 |
*/ |
| 142 |
public static function get_mono() { |
| 143 |
return self::$mono_libraries; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Get the full license name. |
| 148 |
* |
| 149 |
* @param string $license The license id. |
| 150 |
* @return string The full license name. |
| 151 |
* @since 1.0.0 |
| 152 |
*/ |
| 153 |
private function license_name( $license ) { |
| 154 |
switch ( $license ) { |
| 155 |
case 'mit': |
| 156 |
$result = esc_html__( 'MIT license', 'sessions' ); |
| 157 |
break; |
| 158 |
case 'apl2': |
| 159 |
$result = esc_html__( 'Apache license, version 2.0', 'sessions' ); |
| 160 |
break; |
| 161 |
case 'gpl2': |
| 162 |
$result = esc_html__( 'GPL-2.0 license', 'sessions' ); |
| 163 |
break; |
| 164 |
case 'gpl3': |
| 165 |
$result = esc_html__( 'GPL-3.0 license', 'sessions' ); |
| 166 |
break; |
| 167 |
case 'lgpl3': |
| 168 |
$result = esc_html__( 'LGPL-3.0 license', 'sessions' ); |
| 169 |
break; |
| 170 |
default: |
| 171 |
$result = esc_html__( 'unknown license', 'sessions' ); |
| 172 |
break; |
| 173 |
} |
| 174 |
return $result; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Get the libraries list. |
| 179 |
* |
| 180 |
* @param array $attributes 'style' => 'html'. |
| 181 |
* @return string The output of the shortcode, ready to print. |
| 182 |
* @since 1.0.0 |
| 183 |
*/ |
| 184 |
public function sc_get_list( $attributes ) { |
| 185 |
$_attributes = shortcode_atts( |
| 186 |
[ |
| 187 |
'style' => 'html', |
| 188 |
], |
| 189 |
$attributes |
| 190 |
); |
| 191 |
$style = $_attributes['style']; |
| 192 |
$result = ''; |
| 193 |
$list = []; |
| 194 |
foreach ( array_merge( self::get_psr4(), self::get_mono() ) as $library ) { |
| 195 |
$item = []; |
| 196 |
$item['name'] = $library['name']; |
| 197 |
$item['version'] = $library['version']; |
| 198 |
$item['author'] = $library['author']; |
| 199 |
$item['url'] = $library['url']; |
| 200 |
$item['license'] = $this->license_name( $library['license'] ); |
| 201 |
$item['langs'] = L10n::get_language_markup( explode( ',', $library['langs'] ) ); |
| 202 |
$list[] = $item; |
| 203 |
} |
| 204 |
$item = []; |
| 205 |
$item['name'] = 'Plugin Boilerplate'; |
| 206 |
$item['version'] = '1.0.0'; |
| 207 |
$item['author'] = 'Pierre Lannoy'; |
| 208 |
$item['url'] = 'https://github.com/Pierre-Lannoy/wp-' . 'plugin-' . 'boilerplate'; |
| 209 |
$item['license'] = $this->license_name( 'gpl3' ); |
| 210 |
$item['langs'] = L10n::get_language_markup( [ 'en' ] ); |
| 211 |
$list[] = $item; |
| 212 |
$item = []; |
| 213 |
$item['name'] = 'Date Range Picker'; |
| 214 |
$item['version'] = '3.0.5'; |
| 215 |
$item['author'] = sprintf( esc_html__( '%s & contributors', 'sessions' ), 'Dan Grossman' ); |
| 216 |
$item['url'] = 'https://github.com/dangrossman/daterangepicker'; |
| 217 |
$item['license'] = $this->license_name( 'mit' ); |
| 218 |
$item['langs'] = L10n::get_language_markup( [ 'en' ] ); |
| 219 |
$list[] = $item; |
| 220 |
$item = []; |
| 221 |
$item['name'] = 'Moment'; |
| 222 |
$item['version'] = '2.24.0'; |
| 223 |
$item['author'] = sprintf( esc_html__( '%s & contributors', 'sessions' ), 'Tim Wood' ); |
| 224 |
$item['url'] = 'https://github.com/moment/moment'; |
| 225 |
$item['license'] = $this->license_name( 'mit' ); |
| 226 |
$item['langs'] = L10n::get_language_markup( [ 'en' ] ); |
| 227 |
$list[] = $item; |
| 228 |
$item['name'] = 'SVG-Loaders'; |
| 229 |
$item['version'] = '1.0.2'; |
| 230 |
$item['author'] = sprintf( esc_html__( '%s & contributors', 'sessions' ), 'Sam Herbert' ); |
| 231 |
$item['url'] = 'https://github.com/SamHerbert/SVG-Loaders'; |
| 232 |
$item['license'] = $this->license_name( 'mit' ); |
| 233 |
$item['langs'] = L10n::get_language_markup( [ 'en' ] ); |
| 234 |
$list[] = $item; |
| 235 |
$item['name'] = 'Chartist-JS'; |
| 236 |
$item['version'] = '0.11.4'; |
| 237 |
$item['author'] = sprintf( esc_html__( '%s & contributors', 'sessions' ), 'Gion Kunz' ); |
| 238 |
$item['url'] = 'https://github.com/gionkunz/chartist-js'; |
| 239 |
$item['license'] = $this->license_name( 'mit' ); |
| 240 |
$item['langs'] = L10n::get_language_markup( [ 'en' ] ); |
| 241 |
$list[] = $item; |
| 242 |
usort( $list, function ( $a, $b ) { return strcmp( strtolower( $a['name'] ), strtolower( $b['name'] ) );} ); |
| 243 |
if ( 'html' === $style ) { |
| 244 |
$items = []; |
| 245 |
foreach ( $list as $library ) { |
| 246 |
/* translators: as in the sentence "Product W version X by author Y (license Z)" */ |
| 247 |
$items[] = sprintf( __( '<a href="%1$s">%2$s %3$s</a>%4$s by %5$s (%6$s)', 'sessions' ), $library['url'], $library['name'], $library['version'], $library['langs'], $library['author'], $library['license'] ); |
| 248 |
} |
| 249 |
$result = implode( ', ', $items ); |
| 250 |
} |
| 251 |
return $result; |
| 252 |
} |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
Libraries::init(); |
| 257 |
|