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