PluginProbe
WooCommerce Square / 2.8.0
WooCommerce Square v2.8.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
414 lines 9.7 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: 2.8.0
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: © 2020 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: 5.8
23 */
24
25 defined( 'ABSPATH' ) || exit;
26
27 require_once( plugin_dir_path( __FILE__ ) . 'vendor/prospress/action-scheduler/action-scheduler.php' );
28
29 /**
30 * The plugin loader class.
31 *
32 * @since 2.0.0
33 */
34 class WooCommerce_Square_Loader {
35
36
37 /** minimum PHP version required by this plugin */
38 const MINIMUM_PHP_VERSION = '5.6.0';
39
40 /** minimum WordPress version required by this plugin */
41 const MINIMUM_WP_VERSION = '4.6';
42
43 /** minimum WooCommerce version required by this plugin */
44 const MINIMUM_WC_VERSION = '3.0';
45
46 /** SkyVerge plugin framework version used by this plugin */
47 const FRAMEWORK_VERSION = '5.4.0';
48
49 /** the plugin name, for displaying notices */
50 const PLUGIN_NAME = 'WooCommerce Square';
51
52
53 /** @var WooCommerce_Square_Loader single instance of this class */
54 private static $instance;
55
56 /** @var array the admin notices to add */
57 private $notices = array();
58
59
60 /**
61 * Constructs the class.
62 *
63 * @since 2.0.0
64 */
65 protected function __construct() {
66
67 register_activation_hook( __FILE__, array( $this, 'activation_check' ) );
68
69 add_action( 'admin_init', array( $this, 'check_environment' ) );
70 add_action( 'admin_init', array( $this, 'add_plugin_notices' ) );
71
72 add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
73
74 // if the environment check fails, initialize the plugin
75 if ( $this->is_environment_compatible() ) {
76 add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
77 }
78
79 add_action( 'woocommerce_blocks_payment_method_type_registration', array( $this, 'register_payment_method_block_integrations' ), 5, 1 );
80 }
81
82
83 /**
84 * Cloning instances is forbidden due to singleton pattern.
85 *
86 * @since 2.0.0
87 */
88 public function __clone() {
89
90 _doing_it_wrong( __FUNCTION__, sprintf( 'You cannot clone instances of %s.', get_class( $this ) ), '2.0.0' );
91 }
92
93
94 /**
95 * Unserializing instances is forbidden due to singleton pattern.
96 *
97 * @since 2.0.0
98 */
99 public function __wakeup() {
100
101 _doing_it_wrong( __FUNCTION__, sprintf( 'You cannot unserialize instances of %s.', get_class( $this ) ), '2.0.0' );
102 }
103
104
105 /**
106 * Initializes the plugin.
107 *
108 * @since 2.0.0
109 */
110 public function init_plugin() {
111
112 if ( ! $this->plugins_compatible() ) {
113 return;
114 }
115
116 $this->load_framework();
117
118 // autoload plugin and vendor files
119 $loader = require_once( plugin_dir_path( __FILE__ ) . 'vendor/autoload.php' );
120
121 // register plugin namespace with autoloader
122 $loader->addPsr4( 'WooCommerce\\Square\\', __DIR__ . '/includes' );
123
124 require_once( plugin_dir_path( __FILE__ ) . 'includes/Functions.php' );
125
126 // fire it up!
127 wc_square();
128 }
129
130
131 /**
132 * Loads the base framework classes.
133 *
134 * @since 2.0.0
135 */
136 protected function load_framework() {
137
138 if ( ! class_exists( '\\SkyVerge\\WooCommerce\\PluginFramework\\' . $this->get_framework_version_namespace() . '\\SV_WC_Plugin' ) ) {
139 require_once( plugin_dir_path( __FILE__ ) . 'vendor/skyverge/wc-plugin-framework/woocommerce/class-sv-wc-plugin.php' );
140 }
141
142 if ( ! class_exists( '\\SkyVerge\\WooCommerce\\PluginFramework\\' . $this->get_framework_version_namespace() . '\\SV_WC_Payment_Gateway_Plugin' ) ) {
143 require_once( plugin_dir_path( __FILE__ ) . 'vendor/skyverge/wc-plugin-framework/woocommerce/payment-gateway/class-sv-wc-payment-gateway-plugin.php' );
144 }
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 /**
175 * Checks the server environment and other factors and deactivates plugins as necessary.
176 *
177 * Based on http://wptavern.com/how-to-prevent-wordpress-plugins-from-activating-on-sites-with-incompatible-hosting-environments
178 *
179 * @since 2.0.0
180 */
181 public function activation_check() {
182
183 if ( ! $this->is_environment_compatible() ) {
184
185 $this->deactivate_plugin();
186
187 wp_die( self::PLUGIN_NAME . ' could not be activated. ' . $this->get_environment_message() );
188 }
189 }
190
191 /**
192 * Checks the environment on loading WordPress, just in case the environment changes after activation.
193 *
194 * @since 2.0.0
195 */
196 public function check_environment() {
197
198 if ( ! $this->is_environment_compatible() && is_plugin_active( plugin_basename( __FILE__ ) ) ) {
199
200 $this->deactivate_plugin();
201
202 $this->add_admin_notice( 'bad_environment', 'error', self::PLUGIN_NAME . ' has been deactivated. ' . $this->get_environment_message() );
203 }
204 }
205
206
207 /**
208 * Adds notices for out-of-date WordPress and/or WooCommerce versions.
209 *
210 * @since 2.0.0
211 */
212 public function add_plugin_notices() {
213
214 if ( ! $this->is_wp_compatible() ) {
215
216 $this->add_admin_notice(
217 'update_wordpress',
218 'error',
219 sprintf(
220 '%s requires WordPress version %s or higher. Please %supdate WordPress &raquo;%s',
221 '<strong>' . self::PLUGIN_NAME .
222 '</strong>',
223 self::MINIMUM_WP_VERSION,
224 '<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">',
225 '</a>'
226 )
227 );
228 }
229
230 if ( ! $this->is_wc_compatible() ) {
231
232 $this->add_admin_notice(
233 'update_woocommerce',
234 'error',
235 sprintf(
236 '%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',
237 '<strong>' . self::PLUGIN_NAME . '</strong>',
238 self::MINIMUM_WC_VERSION,
239 '<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">',
240 '</a>',
241 '<a href="' . esc_url( 'https://downloads.wordpress.org/plugin/woocommerce.' . self::MINIMUM_WC_VERSION . '.zip' ) . '">',
242 '</a>'
243 )
244 );
245 }
246 }
247
248
249 /**
250 * Determines if the required plugins are compatible.
251 *
252 * @since 2.0.0
253 *
254 * @return bool
255 */
256 protected function plugins_compatible() {
257
258 return $this->is_wp_compatible() && $this->is_wc_compatible();
259 }
260
261
262 /**
263 * Determines if the WordPress compatible.
264 *
265 * @since 2.0.0
266 *
267 * @return bool
268 */
269 protected function is_wp_compatible() {
270
271 if ( ! self::MINIMUM_WP_VERSION ) {
272 return true;
273 }
274
275 return version_compare( get_bloginfo( 'version' ), self::MINIMUM_WP_VERSION, '>=' );
276 }
277
278
279 /**
280 * Determines if the WooCommerce compatible.
281 *
282 * @since 2.0.0
283 *
284 * @return bool
285 */
286 protected function is_wc_compatible() {
287
288 if ( ! self::MINIMUM_WC_VERSION ) {
289 return true;
290 }
291
292 return defined( 'WC_VERSION' ) && version_compare( WC_VERSION, self::MINIMUM_WC_VERSION, '>=' );
293 }
294
295
296 /**
297 * Deactivates the plugin.
298 *
299 * @since 2.0.0
300 */
301 protected function deactivate_plugin() {
302
303 deactivate_plugins( plugin_basename( __FILE__ ) );
304
305 if ( isset( $_GET['activate'] ) ) {
306 unset( $_GET['activate'] );
307 }
308 }
309
310
311 /**
312 * Adds an admin notice to be displayed.
313 *
314 * @since 2.0.0
315 *
316 * @param string $slug the slug for the notice
317 * @param string $class the css class for the notice
318 * @param string $message the notice message
319 */
320 public function add_admin_notice( $slug, $class, $message ) {
321
322 $this->notices[ $slug ] = array(
323 'class' => $class,
324 'message' => $message,
325 );
326 }
327
328
329 /**
330 * Displays any admin notices added with \WooCommerce_Square_Loader::add_admin_notice()
331 *
332 * @since 2.0.0
333 */
334 public function admin_notices() {
335
336 foreach ( (array) $this->notices as $notice_key => $notice ) {
337
338 ?>
339 <div class="<?php echo esc_attr( $notice['class'] ); ?>">
340 <p>
341 <?php echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) ); ?>
342 </p>
343 </div>
344 <?php
345 }
346 }
347
348
349 /**
350 * Determines if the server environment is compatible with this plugin.
351 *
352 * Override this method to add checks for more than just the PHP version.
353 *
354 * @since 2.0.0
355 *
356 * @return bool
357 */
358 protected function is_environment_compatible() {
359
360 return version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '>=' );
361 }
362
363
364 /**
365 * Gets the message for display when the environment is incompatible with this plugin.
366 *
367 * @since 2.0.0
368 *
369 * @return string
370 */
371 protected function get_environment_message() {
372
373 $message = sprintf( 'The minimum PHP version required for this plugin is %1$s. You are running %2$s.', self::MINIMUM_PHP_VERSION, PHP_VERSION );
374
375 return $message;
376 }
377
378
379 /**
380 * Register the Square Credit Card checkout block integration class
381 *
382 * @since 2.5.0
383 */
384 public function register_payment_method_block_integrations( $payment_method_registry ) {
385 if ( class_exists( '\Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
386 $payment_method_registry->register( new WooCommerce\Square\Gateway\Blocks_Handler() );
387 }
388 }
389
390
391 /**
392 * Gets the main plugin loader instance.
393 *
394 * Ensures only one instance can be loaded.
395 *
396 * @since 2.0.0
397 *
398 * @return \WooCommerce_Square_Loader
399 */
400 public static function instance() {
401
402 if ( null === self::$instance ) {
403 self::$instance = new self();
404 }
405
406 return self::$instance;
407 }
408
409
410 }
411
412 // fire it up!
413 WooCommerce_Square_Loader::instance();
414