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