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