Addresses
3 years ago
Api
1 year ago
Compatibility
2 years ago
PaymentGateway
1 day ago
Utilities
1 year ago
Admin_Message_Handler.php
3 years ago
Admin_Notice_Handler.php
3 months ago
Lifecycle.php
2 years ago
Plugin.php
11 months ago
Plugin_Compatibility.php
2 years ago
Plugin_Dependencies.php
1 year ago
Square_Helper.php
8 months ago
Plugin_Dependencies.php
470 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Plugin Framework |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@skyverge.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * @since 3.0.0 |
| 14 | * @author WooCommerce / SkyVerge |
| 15 | * @copyright Copyright (c) 2013-2019, SkyVerge, Inc. |
| 16 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 17 | * |
| 18 | * Modified by WooCommerce on 01 December 2021. |
| 19 | */ |
| 20 | |
| 21 | namespace WooCommerce\Square\Framework; |
| 22 | |
| 23 | defined( 'ABSPATH' ) || exit; |
| 24 | |
| 25 | class Plugin_Dependencies { |
| 26 | |
| 27 | /** @var array required PHP extensions */ |
| 28 | protected $php_extensions = array(); |
| 29 | |
| 30 | /** @var array required PHP functions */ |
| 31 | protected $php_functions = array(); |
| 32 | |
| 33 | /** @var array required PHP settings */ |
| 34 | protected $php_settings = array(); |
| 35 | |
| 36 | /** @var Plugin plugin instance */ |
| 37 | protected $plugin; |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Constructs the class. |
| 42 | * |
| 43 | * @param Plugin $plugin plugin instance |
| 44 | * @param array $args { |
| 45 | * PHP extension, function, and settings dependencies |
| 46 | * |
| 47 | * @type array $php_extensions PHP extension dependencies |
| 48 | * @type array $php_functions PHP function dependencies |
| 49 | * @type array $php_settings PHP settings dependencies |
| 50 | * } |
| 51 | */ |
| 52 | public function __construct( Plugin $plugin, $args = array() ) { |
| 53 | |
| 54 | $this->plugin = $plugin; |
| 55 | |
| 56 | $dependencies = $this->parse_dependencies( $args ); |
| 57 | |
| 58 | $this->php_extensions = (array) $dependencies['php_extensions']; |
| 59 | $this->php_functions = (array) $dependencies['php_functions']; |
| 60 | $this->php_settings = (array) $dependencies['php_settings']; |
| 61 | |
| 62 | // add the action & filter hooks |
| 63 | $this->add_hooks(); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Parses the dependency arguments and sets defaults. |
| 69 | * |
| 70 | * @since 3.0.0 |
| 71 | * |
| 72 | * @param array $args dependency args |
| 73 | * @return array |
| 74 | */ |
| 75 | private function parse_dependencies( $args ) { |
| 76 | |
| 77 | $dependencies = wp_parse_args( |
| 78 | $args, |
| 79 | array( |
| 80 | 'php_extensions' => array(), |
| 81 | 'php_functions' => array(), |
| 82 | 'php_settings' => array(), |
| 83 | ) |
| 84 | ); |
| 85 | |
| 86 | $default_settings = array( |
| 87 | 'suhosin.post.max_array_index_length' => 256, |
| 88 | 'suhosin.post.max_totalname_length' => 65535, |
| 89 | 'suhosin.post.max_vars' => 1024, |
| 90 | 'suhosin.request.max_array_index_length' => 256, |
| 91 | 'suhosin.request.max_totalname_length' => 65535, |
| 92 | 'suhosin.request.max_vars' => 1024, |
| 93 | ); |
| 94 | |
| 95 | // override any default settings requirements if the plugin specifies them |
| 96 | if ( ! empty( $dependencies['php_settings'] ) ) { |
| 97 | $dependencies['php_settings'] = array_merge( $default_settings, $dependencies['php_settings'] ); |
| 98 | } |
| 99 | |
| 100 | return $dependencies; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * Adds the action & filter hooks. |
| 106 | * |
| 107 | * @since 3.0.0 |
| 108 | */ |
| 109 | protected function add_hooks() { |
| 110 | |
| 111 | // add the admin dependency notices |
| 112 | add_action( 'admin_init', array( $this, 'add_admin_notices' ) ); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * Adds the admin dependency notices. |
| 118 | * |
| 119 | * @since 3.0.0 |
| 120 | */ |
| 121 | public function add_admin_notices() { |
| 122 | |
| 123 | $this->add_php_extension_notices(); |
| 124 | $this->add_php_function_notices(); |
| 125 | $this->add_php_settings_notices(); |
| 126 | |
| 127 | $this->add_deprecated_notices(); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Adds notices for any missing PHP extensions. |
| 133 | * |
| 134 | * @since 3.0.0 |
| 135 | */ |
| 136 | public function add_php_extension_notices() { |
| 137 | |
| 138 | $missing_extensions = $this->get_missing_php_extensions(); |
| 139 | |
| 140 | if ( count( $missing_extensions ) > 0 ) { |
| 141 | |
| 142 | $message = sprintf( |
| 143 | /* translators: Placeholders: %1$s - plugin name, %2$s - a PHP extension/comma-separated list of PHP extensions */ |
| 144 | _n( |
| 145 | '%1$s requires the %2$s PHP extension to function. Contact your host or server administrator to install and configure the missing extension.', |
| 146 | '%1$s requires the following PHP extensions to function: %2$s. Contact your host or server administrator to install and configure the missing extensions.', |
| 147 | count( $missing_extensions ), |
| 148 | 'woocommerce-square' |
| 149 | ), |
| 150 | esc_html( $this->get_plugin()->get_plugin_name() ), |
| 151 | '<strong>' . implode( ', ', $missing_extensions ) . '</strong>' |
| 152 | ); |
| 153 | |
| 154 | $this->add_admin_notice( 'wc-' . $this->get_plugin()->get_id_dasherized() . '-missing-extensions', $message, 'error' ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /** |
| 160 | * Adds notices for any missing PHP functions. |
| 161 | * |
| 162 | * @since 3.0.0 |
| 163 | */ |
| 164 | public function add_php_function_notices() { |
| 165 | |
| 166 | $missing_functions = $this->get_missing_php_functions(); |
| 167 | |
| 168 | if ( count( $missing_functions ) > 0 ) { |
| 169 | |
| 170 | $message = sprintf( |
| 171 | /* translators: Placeholders: %1$s - plugin name, %2$s - a PHP function/comma-separated list of PHP functions */ |
| 172 | _n( |
| 173 | '%1$s requires the %2$s PHP function to exist. Contact your host or server administrator to install and configure the missing function.', |
| 174 | '%1$s requires the following PHP functions to exist: %2$s. Contact your host or server administrator to install and configure the missing functions.', |
| 175 | count( $missing_functions ), |
| 176 | 'woocommerce-square' |
| 177 | ), |
| 178 | esc_html( $this->get_plugin()->get_plugin_name() ), |
| 179 | '<strong>' . implode( ', ', $missing_functions ) . '</strong>' |
| 180 | ); |
| 181 | |
| 182 | $this->add_admin_notice( 'wc-' . $this->get_plugin()->get_id_dasherized() . '-missing-functions', $message, 'error' ); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /** |
| 188 | * Adds notices for any incompatible PHP settings. |
| 189 | * |
| 190 | * @since 3.0.0 |
| 191 | */ |
| 192 | public function add_php_settings_notices() { |
| 193 | |
| 194 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce none required, only showing a notice. |
| 195 | if ( isset( $_GET['page'] ) && 'wc-settings' === $_GET['page'] ) { |
| 196 | |
| 197 | $bad_settings = $this->get_incompatible_php_settings(); |
| 198 | |
| 199 | if ( count( $bad_settings ) > 0 ) { |
| 200 | |
| 201 | $message = sprintf( |
| 202 | /* translators: Placeholders: %s - plugin name */ |
| 203 | __( '%s may behave unexpectedly because the following PHP configuration settings are required:', 'woocommerce-square' ), |
| 204 | '<strong>' . esc_html( $this->get_plugin()->get_plugin_name() ) . '</strong>' |
| 205 | ); |
| 206 | |
| 207 | $message .= '<ul>'; |
| 208 | |
| 209 | foreach ( $bad_settings as $setting => $values ) { |
| 210 | |
| 211 | $setting_message = '<code>' . $setting . ' = ' . $values['expected'] . '</code>'; |
| 212 | |
| 213 | if ( ! empty( $values['type'] ) && 'min' === $values['type'] ) { |
| 214 | |
| 215 | $setting_message = sprintf( |
| 216 | /* translators: Placeholders: %s - a PHP setting value */ |
| 217 | esc_html__( '%s or higher', 'woocommerce-square' ), |
| 218 | $setting_message |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | $message .= '<li>' . $setting_message . '</li>'; |
| 223 | } |
| 224 | |
| 225 | $message .= '</ul>'; |
| 226 | |
| 227 | $message .= __( 'Please contact your hosting provider or server administrator to configure these settings.', 'woocommerce-square' ); |
| 228 | |
| 229 | $this->add_admin_notice( 'wc-' . $this->get_plugin()->get_id_dasherized() . '-incompatibile-php-settings', $message, 'warning' ); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Gets any deprecated warning notices. |
| 237 | * |
| 238 | * @since 3.0.0 |
| 239 | */ |
| 240 | protected function add_deprecated_notices() { |
| 241 | |
| 242 | // add a notice for PHP < 5.6 |
| 243 | if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) { |
| 244 | |
| 245 | $message = '<p>'; |
| 246 | |
| 247 | $message .= sprintf( |
| 248 | /* translators: Placeholders: %1$s - <strong>, %2$s - </strong> */ |
| 249 | __( |
| 250 | 'Hey there! We\'ve noticed that your server is running %1$san outdated version of PHP%2$s, which is the programming language that WooCommerce and its extensions are built on. |
| 251 | The PHP version that is currently used for your site is no longer maintained, nor %1$sreceives security updates%2$s; newer versions are faster and more secure. |
| 252 | As a result, %3$s no longer supports this version and you should upgrade PHP as soon as possible. |
| 253 | Your hosting provider can do this for you. %4$sHere are some resources to help you upgrade%5$s and to explain PHP versions further.', |
| 254 | 'woocommerce-square' |
| 255 | ), |
| 256 | '<strong>', |
| 257 | '</strong>', |
| 258 | esc_html( $this->get_plugin()->get_plugin_name() ), |
| 259 | '<a href="http://skyver.ge/upgradephp">', |
| 260 | '</a>' |
| 261 | ); |
| 262 | |
| 263 | $message .= '</p>'; |
| 264 | |
| 265 | $this->add_admin_notice( 'sv-wc-deprecated-php-version', $message, 'error' ); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | |
| 270 | /** |
| 271 | * Adds an admin notice. |
| 272 | * |
| 273 | * @since 3.0.0 |
| 274 | * |
| 275 | * @param string $id notice ID |
| 276 | * @param string $message notice message |
| 277 | * @param string $type notice type |
| 278 | */ |
| 279 | protected function add_admin_notice( $id, $message, $type = 'info' ) { |
| 280 | |
| 281 | $notice_class = $type; |
| 282 | |
| 283 | // translate the types into WP notice classes |
| 284 | switch ( $type ) { |
| 285 | |
| 286 | case 'error': |
| 287 | $notice_class = 'notice-error'; |
| 288 | break; |
| 289 | |
| 290 | case 'warning': |
| 291 | $notice_class = 'notice-warning'; |
| 292 | break; |
| 293 | |
| 294 | case 'info': |
| 295 | $notice_class = 'notice-info'; |
| 296 | break; |
| 297 | |
| 298 | case 'success': |
| 299 | $notice_class = 'notice-success'; |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | $this->get_plugin()->get_admin_notice_handler()->add_admin_notice( |
| 304 | $message, |
| 305 | $id, |
| 306 | array( |
| 307 | 'notice_class' => $notice_class, |
| 308 | ) |
| 309 | ); |
| 310 | } |
| 311 | |
| 312 | |
| 313 | /** Getter methods ********************************************************/ |
| 314 | |
| 315 | |
| 316 | /** |
| 317 | * Gets any missing PHP extensions. |
| 318 | * |
| 319 | * @since 3.0.0 |
| 320 | * |
| 321 | * @return array |
| 322 | */ |
| 323 | public function get_missing_php_extensions() { |
| 324 | |
| 325 | $missing_extensions = array(); |
| 326 | |
| 327 | foreach ( $this->get_php_extensions() as $extension ) { |
| 328 | |
| 329 | if ( ! extension_loaded( $extension ) ) { |
| 330 | $missing_extensions[] = $extension; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return $missing_extensions; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Returns true if the plugin meets PHP dependencies, false otherwise. |
| 339 | * |
| 340 | * @return boolean |
| 341 | */ |
| 342 | public function meets_php_dependencies() { |
| 343 | return empty( $this->get_missing_php_extensions() ) && empty( $this->get_missing_php_functions() ); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /** |
| 348 | * Gets the required PHP extensions. |
| 349 | * |
| 350 | * @since 3.0.0 |
| 351 | * |
| 352 | * @return array |
| 353 | */ |
| 354 | public function get_php_extensions() { |
| 355 | |
| 356 | return $this->php_extensions; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | /** |
| 361 | * Gets any missing PHP functions. |
| 362 | * |
| 363 | * @since 3.0.0 |
| 364 | * |
| 365 | * @return array |
| 366 | */ |
| 367 | public function get_missing_php_functions() { |
| 368 | |
| 369 | $missing_functions = array(); |
| 370 | |
| 371 | foreach ( $this->get_php_functions() as $function ) { |
| 372 | |
| 373 | if ( ! extension_loaded( $function ) ) { |
| 374 | $missing_functions[] = $function; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | return $missing_functions; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | /** |
| 383 | * Gets the required PHP functions. |
| 384 | * |
| 385 | * @since 3.0.0 |
| 386 | * |
| 387 | * @return array |
| 388 | */ |
| 389 | public function get_php_functions() { |
| 390 | |
| 391 | return $this->php_functions; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | /** |
| 396 | * Gets any incompatible PHP settings. |
| 397 | * |
| 398 | * @since 3.0.0 |
| 399 | * |
| 400 | * @return array |
| 401 | */ |
| 402 | public function get_incompatible_php_settings() { |
| 403 | |
| 404 | $incompatible_settings = array(); |
| 405 | |
| 406 | if ( function_exists( 'ini_get' ) ) { |
| 407 | |
| 408 | foreach ( $this->get_php_settings() as $setting => $expected ) { |
| 409 | |
| 410 | $actual = ini_get( $setting ); |
| 411 | |
| 412 | if ( ! $actual ) { |
| 413 | continue; |
| 414 | } |
| 415 | |
| 416 | if ( is_integer( $expected ) ) { |
| 417 | |
| 418 | // determine if this is a size string, like "10MB" |
| 419 | $is_size = ! is_numeric( substr( $actual, -1 ) ); |
| 420 | |
| 421 | $actual_num = $is_size ? wc_let_to_num( $actual ) : $actual; |
| 422 | |
| 423 | if ( $actual_num < $expected ) { |
| 424 | |
| 425 | $incompatible_settings[ $setting ] = array( |
| 426 | 'expected' => $is_size ? size_format( $expected ) : $expected, |
| 427 | 'actual' => $is_size ? size_format( $actual_num ) : $actual, |
| 428 | 'type' => 'min', |
| 429 | ); |
| 430 | } |
| 431 | } elseif ( $actual !== $expected ) { |
| 432 | |
| 433 | $incompatible_settings[ $setting ] = array( |
| 434 | 'expected' => $expected, |
| 435 | 'actual' => $actual, |
| 436 | ); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return $incompatible_settings; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | /** |
| 446 | * Gets the required PHP settings. |
| 447 | * |
| 448 | * @since 3.0.0 |
| 449 | * |
| 450 | * @return array |
| 451 | */ |
| 452 | public function get_php_settings() { |
| 453 | |
| 454 | return $this->php_settings; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | /** |
| 459 | * Gets the plugin instance. |
| 460 | * |
| 461 | * @since 3.0.0 |
| 462 | * |
| 463 | * @return Plugin |
| 464 | */ |
| 465 | protected function get_plugin() { |
| 466 | |
| 467 | return $this->plugin; |
| 468 | } |
| 469 | } |
| 470 |