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