PluginProbe
WooCommerce Square / 4.2.0
WooCommerce Square v4.2.0
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
458 lines 11.2 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.2.0
5 * Plugin URI: https://woocommerce.com/products/square/
6 * Requires at least: 6.0
7 * Tested up to: 6.2.0
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: 7.7
24 * WC tested up to: 7.9.0
25 */
26
27 defined( 'ABSPATH' ) || exit;
28
29 require_once plugin_dir_path( __FILE__ ) . 'vendor/woocommerce/action-scheduler/action-scheduler.php';
30
31 if ( ! defined( 'WC_SQUARE_PLUGIN_VERSION' ) ) {
32 define( 'WC_SQUARE_PLUGIN_VERSION', '4.2.0' ); // 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.0';
56
57 /** minimum WooCommerce version required by this plugin */
58 const MINIMUM_WC_VERSION = '7.7';
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_init', array( $this, 'add_plugin_notices' ) );
84 add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
85
86 // if the environment check fails, don't initialize the plugin.
87 if ( $this->is_environment_compatible() ) {
88 add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
89 add_action( 'woocommerce_blocks_payment_method_type_registration', array( $this, 'register_payment_method_block_integrations' ), 5, 1 );
90 add_action( 'before_woocommerce_init', array( $this, 'declare_hpos_compatibility' ) );
91 }
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->plugins_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
144
145 /**
146 * Loads the base framework classes.
147 *
148 * @since 2.0.0
149 */
150 protected function load_framework() {
151 require_once plugin_dir_path( __FILE__ ) . 'includes/Framework/Plugin.php';
152 require_once plugin_dir_path( __FILE__ ) . 'includes/Framework/PaymentGateway/Payment_Gateway_Plugin.php';
153 }
154
155
156 /**
157 * Gets the framework version in namespace form.
158 *
159 * @since 2.0.0
160 *
161 * @return string
162 */
163 protected function get_framework_version_namespace() {
164
165 return 'v' . str_replace( '.', '_', $this->get_framework_version() );
166 }
167
168
169 /**
170 * Gets the framework version used by this plugin.
171 *
172 * @since 2.0.0
173 *
174 * @return string
175 */
176 protected function get_framework_version() {
177
178 return self::FRAMEWORK_VERSION;
179 }
180
181 /**
182 * Adds notices for out-of-date WordPress and/or WooCommerce versions.
183 *
184 * @since 2.0.0
185 */
186 public function add_plugin_notices() {
187
188 if ( ! $this->is_wp_compatible() ) {
189
190 $this->add_admin_notice(
191 'update_wordpress',
192 'error',
193 sprintf(
194 '%s requires WordPress version %s or higher. Please %supdate WordPress &raquo;%s',
195 '<strong>' . self::PLUGIN_NAME .
196 '</strong>',
197 self::MINIMUM_WP_VERSION,
198 '<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">',
199 '</a>'
200 )
201 );
202 }
203
204 if ( ! $this->is_wc_compatible() ) {
205
206 $this->add_admin_notice(
207 'update_woocommerce',
208 'error',
209 sprintf(
210 '%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',
211 '<strong>' . self::PLUGIN_NAME . '</strong>',
212 self::MINIMUM_WC_VERSION,
213 '<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">',
214 '</a>',
215 '<a href="' . esc_url( 'https://downloads.wordpress.org/plugin/woocommerce.' . self::MINIMUM_WC_VERSION . '.zip' ) . '">',
216 '</a>'
217 )
218 );
219 }
220 }
221
222
223 /**
224 * Determines if the required plugins are compatible.
225 *
226 * @since 2.0.0
227 *
228 * @return bool
229 */
230 protected function plugins_compatible() {
231
232 return $this->is_wp_compatible() && $this->is_wc_compatible();
233 }
234
235
236 /**
237 * Determines if the WordPress compatible.
238 *
239 * @since 2.0.0
240 *
241 * @return bool
242 */
243 protected function is_wp_compatible() {
244
245 if ( ! self::MINIMUM_WP_VERSION ) {
246 return true;
247 }
248
249 return version_compare( get_bloginfo( 'version' ), self::MINIMUM_WP_VERSION, '>=' );
250 }
251
252
253 /**
254 * Determines if the WooCommerce compatible.
255 *
256 * @since 2.0.0
257 *
258 * @return bool
259 */
260 protected function is_wc_compatible() {
261
262 if ( ! self::MINIMUM_WC_VERSION ) {
263 return true;
264 }
265
266 return defined( 'WC_VERSION' ) && version_compare( WC_VERSION, self::MINIMUM_WC_VERSION, '>=' );
267 }
268
269
270 /**
271 * Deactivates the plugin.
272 *
273 * @since 2.0.0
274 */
275 protected function deactivate_plugin() {
276
277 deactivate_plugins( plugin_basename( __FILE__ ) );
278
279 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
280 if ( isset( $_GET['activate'] ) ) {
281 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
282 unset( $_GET['activate'] );
283 }
284 }
285
286
287 /**
288 * Adds an admin notice to be displayed.
289 *
290 * @since 2.0.0
291 *
292 * @param string $slug the slug for the notice
293 * @param string $class the css class for the notice
294 * @param string $message the notice message
295 */
296 public function add_admin_notice( $slug, $class, $message ) {
297
298 $this->notices[ $slug ] = array(
299 'class' => $class,
300 'message' => $message,
301 );
302 }
303
304
305 /**
306 * Displays any admin notices added with \WooCommerce_Square_Loader::add_admin_notice()
307 *
308 * @since 2.0.0
309 */
310 public function admin_notices() {
311
312 foreach ( (array) $this->notices as $notice_key => $notice ) {
313
314 ?>
315 <div class="<?php echo esc_attr( $notice['class'] ); ?>">
316 <p>
317 <?php
318 echo wp_kses(
319 $notice['message'],
320 array(
321 'a' => array(
322 'href' => array(),
323 'target' => array(),
324 ),
325 'code' => array(),
326 'strong' => array(),
327 'br' => array(),
328 )
329 );
330 ?>
331 </p>
332 </div>
333 <?php
334 }
335 }
336
337
338 /**
339 * Determines if the server environment is compatible with this plugin.
340 *
341 * Override this method to add checks for more than just the PHP version.
342 *
343 * @since 2.0.0
344 *
345 * @return bool
346 */
347 public function is_environment_compatible() {
348 $is_php_valid = $this->is_php_version_valid();
349 $is_opcache_config_valid = $this->is_opcache_save_message_enabled();
350 $error_message = '';
351
352 if ( ! $is_php_valid || ! $is_opcache_config_valid ) {
353 $error_message .= sprintf(
354 // translators: plugin name
355 __( '<strong>All features in %1$s have been disabled</strong> due to unsupported settings:<br>', 'woocommerce-square' ),
356 self::PLUGIN_NAME
357 );
358 }
359
360 if ( ! $is_php_valid ) {
361 $error_message .= sprintf(
362 // translators: minimum PHP version, current PHP version
363 __( '&bull;&nbsp;<strong>Invalid PHP version: </strong>The minimum PHP version required is %1$s. You are running %2$s.<br>', 'woocommerce-square' ),
364 self::MINIMUM_PHP_VERSION,
365 PHP_VERSION
366 );
367 }
368
369 if ( ! $is_opcache_config_valid ) {
370 $error_message .= sprintf(
371 // translators: link to documentation
372 __( '&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' ),
373 'https://woocommerce.com/document/woocommerce-square/troubleshooting/#section-3'
374 );
375 }
376
377 if ( ! empty( $error_message ) ) {
378 $this->add_admin_notice(
379 'bad_environment',
380 'error',
381 $error_message
382 );
383 }
384
385 return $is_php_valid && $is_opcache_config_valid;
386 }
387
388 /**
389 * Declares support for HPOS.
390 */
391 public function declare_hpos_compatibility() {
392 if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
393 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
394 }
395 }
396
397 /**
398 * Returns true if opcache.save_comments is enabled.
399 *
400 * @since 3.0.2
401 *
402 * @return boolean
403 */
404 protected function is_opcache_save_message_enabled() {
405 $zend_optimizer_plus = extension_loaded( 'Zend Optimizer+' ) && '0' === ( ini_get( 'zend_optimizerplus.save_comments' ) || '0' === ini_get( 'opcache.save_comments' ) );
406 $zend_opcache = extension_loaded( 'Zend OPcache' ) && '0' === ini_get( 'opcache.save_comments' );
407
408 return ! ( $zend_optimizer_plus || $zend_opcache );
409 }
410
411 /**
412 * Returns true if the PHP version of the environment
413 * meets the requirement.
414 *
415 * @since 3.0.2
416 *
417 * @return boolean
418 */
419 protected function is_php_version_valid() {
420 return version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '>=' );
421 }
422
423 /**
424 * Register the Square Credit Card checkout block integration class
425 *
426 * @since 2.5.0
427 */
428 public function register_payment_method_block_integrations( $payment_method_registry ) {
429 if ( class_exists( '\Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
430 $payment_method_registry->register( new WooCommerce\Square\Gateway\Blocks_Handler() );
431 }
432 }
433
434
435 /**
436 * Gets the main plugin loader instance.
437 *
438 * Ensures only one instance can be loaded.
439 *
440 * @since 2.0.0
441 *
442 * @return \WooCommerce_Square_Loader
443 */
444 public static function instance() {
445
446 if ( null === self::$instance ) {
447 self::$instance = new self();
448 }
449
450 return self::$instance;
451 }
452
453
454 }
455
456 // fire it up!
457 WooCommerce_Square_Loader::instance();
458