PluginProbe
WooCommerce Square / 4.4.2
WooCommerce Square v4.4.2
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
456 lines 11.1 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 * Version: 4.4.2
5 * Plugin URI: https://woocommerce.com/products/square/
6 * Requires at least: 6.2
7 * Tested up to: 6.4
8 * Requires PHP: 7.4
9 *
10 * Description: Adds ability to sync inventory between WooCommerce and Square POS. In addition, you can also make purchases through the Square payment gateway.
11 * Author: WooCommerce
12 * Author URI: https://www.woocommerce.com/
13 * Text Domain: woocommerce-square
14 * Domain Path: /i18n/languages/
15 *
16 * License: GPL-3.0-or-later
17 * License URI: http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
18 *
19 * @author WooCommerce
20 * @copyright Copyright (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
22 *
23 * WC requires at least: 8.2
24 * WC tested up to: 8.4
25 */
26
27 defined( 'ABSPATH' ) || exit;
28
29 if ( ! defined( 'WC_SQUARE_PLUGIN_VERSION' ) ) {
30 define( 'WC_SQUARE_PLUGIN_VERSION', '4.4.2' ); // WRCS: DEFINED_VERSION.
31 }
32
33 if ( ! defined( 'WC_SQUARE_PLUGIN_URL' ) ) {
34 define( 'WC_SQUARE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
35 }
36
37 if ( ! defined( 'WC_SQUARE_PLUGIN_PATH' ) ) {
38 define( 'WC_SQUARE_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
39 }
40
41 /**
42 * The plugin loader class.
43 *
44 * @since 2.0.0
45 */
46 class WooCommerce_Square_Loader {
47
48
49 /** minimum PHP version required by this plugin */
50 const MINIMUM_PHP_VERSION = '7.4.0';
51
52 /** minimum WordPress version required by this plugin */
53 const MINIMUM_WP_VERSION = '6.2';
54
55 /** minimum WooCommerce version required by this plugin */
56 const MINIMUM_WC_VERSION = '8.2';
57
58 /**
59 * SkyVerge plugin framework version used by this plugin
60 * Constant is left as it is for legacy purposes.
61 **/
62 const FRAMEWORK_VERSION = '5.4.0';
63
64 /** the plugin name, for displaying notices */
65 const PLUGIN_NAME = 'Square for WooCommerce';
66
67
68 /** @var WooCommerce_Square_Loader single instance of this class */
69 private static $instance;
70
71 /** @var array the admin notices to add */
72 private $notices = array();
73
74
75 /**
76 * Constructs the class.
77 *
78 * @since 2.0.0
79 */
80 protected function __construct() {
81 add_action( 'admin_init', array( $this, 'add_plugin_notices' ) );
82 add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
83
84 // if the environment check fails, don't initialize the plugin.
85 if ( $this->is_environment_compatible() ) {
86 add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
87 add_action( 'woocommerce_blocks_payment_method_type_registration', array( $this, 'register_payment_method_block_integrations' ), 5, 1 );
88 add_action( 'before_woocommerce_init', array( $this, 'declare_hpos_compatibility' ) );
89 }
90 }
91
92
93 /**
94 * Cloning instances is forbidden due to singleton pattern.
95 *
96 * @since 2.0.0
97 */
98 public function __clone() {
99
100 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
101 _doing_it_wrong( __FUNCTION__, sprintf( 'You cannot clone instances of %s.', get_class( $this ) ), '2.0.0' );
102 }
103
104
105 /**
106 * Unserializing instances is forbidden due to singleton pattern.
107 *
108 * @since 2.0.0
109 */
110 public function __wakeup() {
111
112 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
113 _doing_it_wrong( __FUNCTION__, sprintf( 'You cannot unserialize instances of %s.', get_class( $this ) ), '2.0.0' );
114 }
115
116
117 /**
118 * Initializes the plugin.
119 *
120 * @since 2.0.0
121 */
122 public function init_plugin() {
123
124 if ( ! $this->plugins_compatible() ) {
125 return;
126 }
127
128 $this->load_framework();
129
130 // autoload plugin and vendor files
131 $loader = require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
132
133 // register plugin namespace with autoloader
134 $loader->addPsr4( 'WooCommerce\\Square\\', __DIR__ . '/includes' );
135
136 require_once plugin_dir_path( __FILE__ ) . 'includes/Functions.php';
137
138 // fire it up!
139 wc_square();
140 }
141
142
143 /**
144 * Loads the base framework classes.
145 *
146 * @since 2.0.0
147 */
148 protected function load_framework() {
149 require_once plugin_dir_path( __FILE__ ) . 'includes/Framework/Plugin.php';
150 require_once plugin_dir_path( __FILE__ ) . 'includes/Framework/PaymentGateway/Payment_Gateway_Plugin.php';
151 }
152
153
154 /**
155 * Gets the framework version in namespace form.
156 *
157 * @since 2.0.0
158 *
159 * @return string
160 */
161 protected function get_framework_version_namespace() {
162
163 return 'v' . str_replace( '.', '_', $this->get_framework_version() );
164 }
165
166
167 /**
168 * Gets the framework version used by this plugin.
169 *
170 * @since 2.0.0
171 *
172 * @return string
173 */
174 protected function get_framework_version() {
175
176 return self::FRAMEWORK_VERSION;
177 }
178
179 /**
180 * Adds notices for out-of-date WordPress and/or WooCommerce versions.
181 *
182 * @since 2.0.0
183 */
184 public function add_plugin_notices() {
185
186 if ( ! $this->is_wp_compatible() ) {
187
188 $this->add_admin_notice(
189 'update_wordpress',
190 'error',
191 sprintf(
192 '%s requires WordPress version %s or higher. Please %supdate WordPress &raquo;%s',
193 '<strong>' . self::PLUGIN_NAME .
194 '</strong>',
195 self::MINIMUM_WP_VERSION,
196 '<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">',
197 '</a>'
198 )
199 );
200 }
201
202 if ( ! $this->is_wc_compatible() ) {
203
204 $this->add_admin_notice(
205 'update_woocommerce',
206 'error',
207 sprintf(
208 '%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',
209 '<strong>' . self::PLUGIN_NAME . '</strong>',
210 self::MINIMUM_WC_VERSION,
211 '<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">',
212 '</a>',
213 '<a href="' . esc_url( 'https://downloads.wordpress.org/plugin/woocommerce.' . self::MINIMUM_WC_VERSION . '.zip' ) . '">',
214 '</a>'
215 )
216 );
217 }
218 }
219
220
221 /**
222 * Determines if the required plugins are compatible.
223 *
224 * @since 2.0.0
225 *
226 * @return bool
227 */
228 protected function plugins_compatible() {
229
230 return $this->is_wp_compatible() && $this->is_wc_compatible();
231 }
232
233
234 /**
235 * Determines if the WordPress compatible.
236 *
237 * @since 2.0.0
238 *
239 * @return bool
240 */
241 protected function is_wp_compatible() {
242
243 if ( ! self::MINIMUM_WP_VERSION ) {
244 return true;
245 }
246
247 return version_compare( get_bloginfo( 'version' ), self::MINIMUM_WP_VERSION, '>=' );
248 }
249
250
251 /**
252 * Determines if the WooCommerce compatible.
253 *
254 * @since 2.0.0
255 *
256 * @return bool
257 */
258 protected function is_wc_compatible() {
259
260 if ( ! self::MINIMUM_WC_VERSION ) {
261 return true;
262 }
263
264 return defined( 'WC_VERSION' ) && version_compare( WC_VERSION, self::MINIMUM_WC_VERSION, '>=' );
265 }
266
267
268 /**
269 * Deactivates the plugin.
270 *
271 * @since 2.0.0
272 */
273 protected function deactivate_plugin() {
274
275 deactivate_plugins( plugin_basename( __FILE__ ) );
276
277 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
278 if ( isset( $_GET['activate'] ) ) {
279 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
280 unset( $_GET['activate'] );
281 }
282 }
283
284
285 /**
286 * Adds an admin notice to be displayed.
287 *
288 * @since 2.0.0
289 *
290 * @param string $slug the slug for the notice
291 * @param string $class the css class for the notice
292 * @param string $message the notice message
293 */
294 public function add_admin_notice( $slug, $class, $message ) {
295
296 $this->notices[ $slug ] = array(
297 'class' => $class,
298 'message' => $message,
299 );
300 }
301
302
303 /**
304 * Displays any admin notices added with \WooCommerce_Square_Loader::add_admin_notice()
305 *
306 * @since 2.0.0
307 */
308 public function admin_notices() {
309
310 foreach ( (array) $this->notices as $notice_key => $notice ) {
311
312 ?>
313 <div class="<?php echo esc_attr( $notice['class'] ); ?>">
314 <p>
315 <?php
316 echo wp_kses(
317 $notice['message'],
318 array(
319 'a' => array(
320 'href' => array(),
321 'target' => array(),
322 ),
323 'code' => array(),
324 'strong' => array(),
325 'br' => array(),
326 )
327 );
328 ?>
329 </p>
330 </div>
331 <?php
332 }
333 }
334
335
336 /**
337 * Determines if the server environment is compatible with this plugin.
338 *
339 * Override this method to add checks for more than just the PHP version.
340 *
341 * @since 2.0.0
342 *
343 * @return bool
344 */
345 public function is_environment_compatible() {
346 $is_php_valid = $this->is_php_version_valid();
347 $is_opcache_config_valid = $this->is_opcache_save_message_enabled();
348 $error_message = '';
349
350 if ( ! $is_php_valid || ! $is_opcache_config_valid ) {
351 $error_message .= sprintf(
352 // translators: plugin name
353 __( '<strong>All features in %1$s have been disabled</strong> due to unsupported settings:<br>', 'woocommerce-square' ),
354 self::PLUGIN_NAME
355 );
356 }
357
358 if ( ! $is_php_valid ) {
359 $error_message .= sprintf(
360 // translators: minimum PHP version, current PHP version
361 __( '&bull;&nbsp;<strong>Invalid PHP version: </strong>The minimum PHP version required is %1$s. You are running %2$s.<br>', 'woocommerce-square' ),
362 self::MINIMUM_PHP_VERSION,
363 PHP_VERSION
364 );
365 }
366
367 if ( ! $is_opcache_config_valid ) {
368 $error_message .= sprintf(
369 // translators: link to documentation
370 __( '&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' ),
371 'https://woocommerce.com/document/woocommerce-square/troubleshooting/#section-3'
372 );
373 }
374
375 if ( ! empty( $error_message ) ) {
376 $this->add_admin_notice(
377 'bad_environment',
378 'error',
379 $error_message
380 );
381 }
382
383 return $is_php_valid && $is_opcache_config_valid;
384 }
385
386 /**
387 * Declares support for HPOS.
388 */
389 public function declare_hpos_compatibility() {
390 if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
391 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
392 }
393 }
394
395 /**
396 * Returns true if opcache.save_comments is enabled.
397 *
398 * @since 3.0.2
399 *
400 * @return boolean
401 */
402 protected function is_opcache_save_message_enabled() {
403 $zend_optimizer_plus = extension_loaded( 'Zend Optimizer+' ) && '0' === ( ini_get( 'zend_optimizerplus.save_comments' ) || '0' === ini_get( 'opcache.save_comments' ) );
404 $zend_opcache = extension_loaded( 'Zend OPcache' ) && '0' === ini_get( 'opcache.save_comments' );
405
406 return ! ( $zend_optimizer_plus || $zend_opcache );
407 }
408
409 /**
410 * Returns true if the PHP version of the environment
411 * meets the requirement.
412 *
413 * @since 3.0.2
414 *
415 * @return boolean
416 */
417 protected function is_php_version_valid() {
418 return version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '>=' );
419 }
420
421 /**
422 * Register the Square Credit Card checkout block integration class
423 *
424 * @since 2.5.0
425 */
426 public function register_payment_method_block_integrations( $payment_method_registry ) {
427 if ( class_exists( '\Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
428 $payment_method_registry->register( new WooCommerce\Square\Gateway\Blocks_Handler() );
429 }
430 }
431
432
433 /**
434 * Gets the main plugin loader instance.
435 *
436 * Ensures only one instance can be loaded.
437 *
438 * @since 2.0.0
439 *
440 * @return \WooCommerce_Square_Loader
441 */
442 public static function instance() {
443
444 if ( null === self::$instance ) {
445 self::$instance = new self();
446 }
447
448 return self::$instance;
449 }
450
451
452 }
453
454 // fire it up!
455 WooCommerce_Square_Loader::instance();
456