PluginProbe
WooCommerce Square / 4.8.3
WooCommerce Square v4.8.3
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 132 releases
woocommerce-square / woocommerce-square.php

woocommerce-square.php in WooCommerce Square 4.8.3, at woocommerce-square.php

517 lines 14.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: WooCommerce Square
4 * Requires Plugins: woocommerce
5 * Version: 4.8.3
6 * Plugin URI: https://woocommerce.com/products/square/
7 * Requires at least: 6.5
8 * Tested up to: 6.7
9 * Requires PHP: 7.4
10 * PHP tested up to: 8.3
11 *
12 * Description: Securely accept payments, synchronize sales, and seamlessly manage inventory and product data between WooCommerce and Square POS.
13 * Author: WooCommerce
14 * Author URI: https://www.woocommerce.com/
15 * Text Domain: woocommerce-square
16 * Domain Path: /i18n/languages/
17 *
18 * License: GPL-3.0-or-later
19 * License URI: http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
20 *
21 * @author WooCommerce
22 * @copyright Copyright (c) 2019, Automattic, Inc.
23 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
24 *
25 * WC requires at least: 9.2
26 * WC tested up to: 9.4
27 */
28
29 defined( 'ABSPATH' ) || exit;
30
31 if ( ! defined( 'WC_SQUARE_PLUGIN_VERSION' ) ) {
32 define( 'WC_SQUARE_PLUGIN_VERSION', '4.8.3' ); // WRCS: DEFINED_VERSION.
33 }
34
35 if ( ! defined( 'WC_SQUARE_PLUGIN_URL' ) ) {
36 define( 'WC_SQUARE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
37 }
38
39 if ( ! defined( 'WC_SQUARE_PLUGIN_PATH' ) ) {
40 define( 'WC_SQUARE_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
41 }
42
43 /**
44 * The plugin loader class.
45 *
46 * @since 2.0.0
47 */
48 class WooCommerce_Square_Loader {
49
50
51 /** minimum PHP version required by this plugin */
52 const MINIMUM_PHP_VERSION = '7.4.0';
53
54 /** minimum WordPress version required by this plugin */
55 const MINIMUM_WP_VERSION = '6.5';
56
57 /** minimum WooCommerce version required by this plugin */
58 const MINIMUM_WC_VERSION = '9.2';
59
60 /**
61 * SkyVerge plugin framework version used by this plugin
62 * Constant is left as it is for legacy purposes.
63 **/
64 const FRAMEWORK_VERSION = '5.4.0';
65
66 /** the plugin name, for displaying notices */
67 const PLUGIN_NAME = 'Square for WooCommerce';
68
69
70 /** @var WooCommerce_Square_Loader single instance of this class */
71 private static $instance;
72
73 /** @var array the admin notices to add */
74 private $notices = array();
75
76
77 /**
78 * Constructs the class.
79 *
80 * @since 2.0.0
81 */
82 protected function __construct() {
83 add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
84 add_action( 'before_woocommerce_init', array( $this, 'declare_features_compatibility' ) );
85 /*
86 * Bootstrap the extension on plugins_loaded.
87 *
88 * This ensures that the extension is loaded after WooCommerce Core and to
89 * ensure the WC_VERSION constant is defined prior to checking for compatibility.
90 */
91 add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
92 }
93
94
95 /**
96 * Cloning instances is forbidden due to singleton pattern.
97 *
98 * @since 2.0.0
99 */
100 public function __clone() {
101
102 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
103 _doing_it_wrong( __FUNCTION__, sprintf( 'You cannot clone instances of %s.', get_class( $this ) ), '2.0.0' );
104 }
105
106
107 /**
108 * Unserializing instances is forbidden due to singleton pattern.
109 *
110 * @since 2.0.0
111 */
112 public function __wakeup() {
113
114 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
115 _doing_it_wrong( __FUNCTION__, sprintf( 'You cannot unserialize instances of %s.', get_class( $this ) ), '2.0.0' );
116 }
117
118
119 /**
120 * Initializes the plugin.
121 *
122 * @since 2.0.0
123 */
124 public function init_plugin() {
125
126 if ( ! $this->is_environment_compatible() ) {
127 return;
128 }
129
130 $this->load_framework();
131
132 // autoload plugin and vendor files
133 $loader = require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
134
135 // register plugin namespace with autoloader
136 $loader->addPsr4( 'WooCommerce\\Square\\', __DIR__ . '/includes' );
137
138 require_once plugin_dir_path( __FILE__ ) . 'includes/Functions.php';
139
140 // fire it up!
141 wc_square();
142
143 add_action( 'woocommerce_blocks_payment_method_type_registration', array( $this, 'register_payment_method_block_integrations' ), 5, 1 );
144 }
145
146
147 /**
148 * Loads the base framework classes.
149 *
150 * @since 2.0.0
151 */
152 protected function load_framework() {
153 require_once plugin_dir_path( __FILE__ ) . 'includes/Framework/Plugin.php';
154 require_once plugin_dir_path( __FILE__ ) . 'includes/Framework/PaymentGateway/Payment_Gateway_Plugin.php';
155 }
156
157
158 /**
159 * Gets the framework version in namespace form.
160 *
161 * @since 2.0.0
162 *
163 * @return string
164 */
165 protected function get_framework_version_namespace() {
166
167 return 'v' . str_replace( '.', '_', $this->get_framework_version() );
168 }
169
170
171 /**
172 * Gets the framework version used by this plugin.
173 *
174 * @since 2.0.0
175 *
176 * @return string
177 */
178 protected function get_framework_version() {
179
180 return self::FRAMEWORK_VERSION;
181 }
182
183 /**
184 * Adds notices for out-of-date WordPress and/or WooCommerce versions.
185 *
186 * @since 2.0.0
187 * @deprecated 4.6.3 Use \WooCommerce_Square_Loader::is_environment_compatible() instead.
188 */
189 public function add_plugin_notices() {
190 _deprecated_function( __METHOD__, '4.6.3', '\WooCommerce_Square_Loader::is_environment_compatible()' );
191 $this->is_environment_compatible();
192 }
193
194
195 /**
196 * Determines if the required plugins are compatible.
197 *
198 * @since 2.0.0
199 * @deprecated 4.6.3 Use \WooCommerce_Square_Loader::is_environment_compatible() instead.
200 *
201 * @return bool
202 */
203 protected function plugins_compatible() {
204 _deprecated_function( __METHOD__, '4.6.3', '\WooCommerce_Square_Loader::is_environment_compatible()' );
205 return $this->is_environment_compatible();
206 }
207
208
209 /**
210 * Determines if the WordPress compatible.
211 *
212 * @since 2.0.0
213 *
214 * @return bool
215 */
216 protected function is_wp_compatible() {
217
218 if ( ! self::MINIMUM_WP_VERSION ) {
219 return true;
220 }
221
222 return version_compare( get_bloginfo( 'version' ), self::MINIMUM_WP_VERSION, '>=' );
223 }
224
225
226 /**
227 * Determines if the WooCommerce compatible.
228 *
229 * @since 2.0.0
230 *
231 * @return bool
232 */
233 protected function is_wc_compatible() {
234
235 if ( ! self::MINIMUM_WC_VERSION ) {
236 return true;
237 }
238
239 return defined( 'WC_VERSION' ) && version_compare( WC_VERSION, self::MINIMUM_WC_VERSION, '>=' );
240 }
241
242
243 /**
244 * Deactivates the plugin.
245 *
246 * @since 2.0.0
247 */
248 protected function deactivate_plugin() {
249
250 deactivate_plugins( plugin_basename( __FILE__ ) );
251
252 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
253 if ( isset( $_GET['activate'] ) ) {
254 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
255 unset( $_GET['activate'] );
256 }
257 }
258
259
260 /**
261 * Adds an admin notice to be displayed.
262 *
263 * @since 2.0.0
264 *
265 * @param string $slug the slug for the notice
266 * @param string $class the css class for the notice
267 * @param string $message the notice message
268 */
269 public function add_admin_notice( $slug, $class, $message ) {
270
271 $this->notices[ $slug ] = array(
272 'class' => $class,
273 'message' => $message,
274 );
275 }
276
277
278 /**
279 * Displays any admin notices added with \WooCommerce_Square_Loader::add_admin_notice()
280 *
281 * @since 2.0.0
282 */
283 public function admin_notices() {
284
285 foreach ( (array) $this->notices as $notice_key => $notice ) {
286
287 ?>
288 <div class="<?php echo esc_attr( $notice['class'] ); ?>">
289 <p>
290 <?php
291 echo wp_kses(
292 $notice['message'],
293 array(
294 'a' => array(
295 'href' => array(),
296 'target' => array(),
297 ),
298 'code' => array(),
299 'strong' => array(),
300 'br' => array(),
301 )
302 );
303 ?>
304 </p>
305 </div>
306 <?php
307 }
308 }
309
310
311 /**
312 * Determines if the server environment is compatible with this plugin.
313 *
314 * Override this method to add checks for more than just the PHP version.
315 *
316 * @since 2.0.0
317 *
318 * @return bool
319 */
320 public function is_environment_compatible() {
321 $is_wc_compatible = $this->is_wc_compatible();
322 $is_wp_compatible = $this->is_wp_compatible();
323 $is_php_valid = $this->is_php_version_valid();
324 $is_opcache_config_valid = $this->is_opcache_save_message_enabled();
325 $error_message = '';
326
327 if ( ! $is_php_valid || ! $is_opcache_config_valid || ! $is_wc_compatible || ! $is_wp_compatible ) {
328 $error_message .= sprintf(
329 // translators: plugin name
330 __( '<strong>All features in %1$s have been disabled</strong> due to unsupported settings:<br>', 'woocommerce-square' ),
331 self::PLUGIN_NAME
332 );
333 }
334
335 if ( ! $is_php_valid ) {
336 $error_message .= sprintf(
337 // translators: minimum PHP version, current PHP version
338 __( '&bull;&nbsp;<strong>Invalid PHP version: </strong>The minimum PHP version required is %1$s. You are running %2$s.<br>', 'woocommerce-square' ),
339 self::MINIMUM_PHP_VERSION,
340 PHP_VERSION
341 );
342 }
343
344 if ( ! $is_opcache_config_valid ) {
345 $error_message .= sprintf(
346 // translators: link to documentation
347 __( '&bull;&nbsp;<strong>Invalid OPcache config: </strong><a href="%s" target="_blank">Please ensure the <code>save_comments</code> PHP option is enabled.</a> You may need to contact your hosting provider to change caching options.', 'woocommerce-square' ),
348 'https://woocommerce.com/document/woocommerce-square/troubleshooting/#section-3'
349 );
350 }
351
352 if ( ! $is_wc_compatible ) {
353 if ( ! defined( 'WC_VERSION' ) ) {
354 $error_message .= sprintf(
355 // translators: 1: Minimum required WooCommerce version.
356 __( '&bull;&nbsp;<strong>WooCommerce is not installed: </strong>The plugin requires WooCommerce version %1$s or later be installed.<br>', 'woocommerce-square' ),
357 self::MINIMUM_WC_VERSION
358 );
359 } else {
360 $error_message .= sprintf(
361 // translators: 1: Plugin name, 2: Minimum required WooCommerce version, 3: Opening link to upgrade screen, 4: Closing link, 5: Opening link to download page, 6: Closing link.
362 '&bull;&nbsp;%1$s requires WooCommerce version %2$s or higher. Please %3$supdate WooCommerce%4$s to the latest version, or %5$sdownload the minimum required version &raquo;%6$s<br>',
363 '<strong>' . self::PLUGIN_NAME . '</strong>',
364 self::MINIMUM_WC_VERSION,
365 '<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">',
366 '</a>',
367 '<a href="' . esc_url( $this->get_woocommerce_download_link( 'minimum' ) ) . '">',
368 '</a>'
369 );
370 }
371 }
372
373 if ( ! $is_wp_compatible ) {
374 $error_message .= sprintf(
375 // translators: 1: Plugin name, 2: Minimum required WordPress version, 3: Opening link to upgrade screen, 4: Closing link.
376 '&bull;&nbsp;%s requires WordPress version %s or higher. Please %supdate WordPress &raquo;%s<br>',
377 '<strong>' . self::PLUGIN_NAME . '</strong>',
378 self::MINIMUM_WP_VERSION,
379 '<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">',
380 '</a>'
381 );
382 }
383
384 if ( ! empty( $error_message ) ) {
385 $this->add_admin_notice(
386 'bad_environment',
387 'error',
388 $error_message
389 );
390 }
391
392 return $is_php_valid && $is_opcache_config_valid && $is_wc_compatible && $is_wp_compatible;
393 }
394
395 /**
396 * Get the WooCommerce download link for a specific version.
397 *
398 * Ensures the download link is correct for major versions of WooCommerce by
399 * ensuring that specific download links match the tagged version and include
400 * three parts in the version number.
401 *
402 * @since 4.6.3
403 *
404 * @param string|int|float $version The version of WooCommerce to get the download link for.
405 * Accepts a version number in the forms of 'x', 'x.x' or 'x.x.x'.
406 * Accepts the strings 'minimum' and 'latest'.
407 * Defaults to 'latest'.
408 * @return string The download link for the WooCommerce version.
409 */
410 public function get_woocommerce_download_link( $version = 'latest' ) {
411 $version = (string) $version;
412 $version = strtolower( $version );
413
414 if ( preg_match( '/^(\d+\.)*(\d+)$/', $version ) || 'minimum' === $version ) {
415 if ( 'minimum' === $version ) {
416 $version_download_string = self::MINIMUM_WC_VERSION;
417 } else {
418 $version_download_string = $version;
419 }
420 $version_parts = explode( '.', $version_download_string );
421 /*
422 * Ensure the version string has at least 3 parts.
423 *
424 * Publicly major versions are listed as two parts, eg 6.7, but the
425 * tag published to the WordPress.org repository uses three parts,
426 * eg 6.7.0.
427 *
428 * This is to ensure the download link is correct.
429 */
430 $version_part_count = count( $version_parts ); // Coding standards don't allow for count() in the while condition.
431 while ( $version_part_count < 3 ) {
432 $version_parts[] = '0';
433 $version_part_count = count( $version_parts );
434 }
435 $version_download_string = implode( '.', $version_parts );
436 } else {
437 // Default to latest version.
438 $version_download_string = 'latest-stable';
439 }
440
441 return "https://downloads.wordpress.org/plugin/woocommerce.{$version_download_string}.zip";
442 }
443
444 /**
445 * Declares support for WooCommerce features.
446 */
447 public function declare_features_compatibility() {
448 if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
449 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
450
451 new \WooCommerce\Square\Admin\Product_Editor_Compatibility();
452 }
453 }
454
455 /**
456 * Returns true if opcache.save_comments is enabled.
457 *
458 * @since 3.0.2
459 *
460 * @return boolean
461 */
462 protected function is_opcache_save_message_enabled() {
463 $zend_optimizer_plus = extension_loaded( 'Zend Optimizer+' ) && '0' === ( ini_get( 'zend_optimizerplus.save_comments' ) || '0' === ini_get( 'opcache.save_comments' ) );
464 $zend_opcache = extension_loaded( 'Zend OPcache' ) && '0' === ini_get( 'opcache.save_comments' );
465
466 return ! ( $zend_optimizer_plus || $zend_opcache );
467 }
468
469 /**
470 * Returns true if the PHP version of the environment
471 * meets the requirement.
472 *
473 * @since 3.0.2
474 *
475 * @return boolean
476 */
477 protected function is_php_version_valid() {
478 return version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '>=' );
479 }
480
481 /**
482 * Register the Square Credit Card checkout block integration class
483 *
484 * @since 2.5.0
485 */
486 public function register_payment_method_block_integrations( $payment_method_registry ) {
487 if ( class_exists( '\Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
488 $payment_method_registry->register( new WooCommerce\Square\Gateway\Blocks_Handler() );
489 $payment_method_registry->register( new WooCommerce\Square\Gateway\Cash_App_Pay_Blocks_Handler() );
490 }
491 }
492
493
494 /**
495 * Gets the main plugin loader instance.
496 *
497 * Ensures only one instance can be loaded.
498 *
499 * @since 2.0.0
500 *
501 * @return \WooCommerce_Square_Loader
502 */
503 public static function instance() {
504
505 if ( null === self::$instance ) {
506 self::$instance = new self();
507 }
508
509 return self::$instance;
510 }
511
512
513 }
514
515 // fire it up!
516 WooCommerce_Square_Loader::instance();
517