PluginProbe
WooCommerce Square / 2.1.1
WooCommerce Square v2.1.1
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
388 lines 9.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: 2.1.1
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: (c) 2019, Automattic, Inc.
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: 4.0
23 */
24
25 defined( 'ABSPATH' ) or 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
80
81 /**
82 * Cloning instances is forbidden due to singleton pattern.
83 *
84 * @since 2.0.0
85 */
86 public function __clone() {
87
88 _doing_it_wrong( __FUNCTION__, sprintf( 'You cannot clone instances of %s.', get_class( $this ) ), '2.0.0' );
89 }
90
91
92 /**
93 * Unserializing instances is forbidden due to singleton pattern.
94 *
95 * @since 2.0.0
96 */
97 public function __wakeup() {
98
99 _doing_it_wrong( __FUNCTION__, sprintf( 'You cannot unserialize instances of %s.', get_class( $this ) ), '2.0.0' );
100 }
101
102
103 /**
104 * Initializes the plugin.
105 *
106 * @since 2.0.0
107 */
108 public function init_plugin() {
109
110 if ( ! $this->plugins_compatible() ) {
111 return;
112 }
113
114 $this->load_framework();
115
116 // autoload plugin and vendor files
117 $loader = require_once( plugin_dir_path( __FILE__ ) . 'vendor/autoload.php' );
118
119 // register plugin namespace with autoloader
120 $loader->addPsr4( 'WooCommerce\\Square\\', __DIR__ . '/includes' );
121
122 require_once( plugin_dir_path( __FILE__ ) . 'includes/Functions.php' );
123
124 // fire it up!
125 wc_square();
126 }
127
128
129 /**
130 * Loads the base framework classes.
131 *
132 * @since 2.0.0
133 */
134 protected function load_framework() {
135
136 if ( ! class_exists( '\\SkyVerge\\WooCommerce\\PluginFramework\\' . $this->get_framework_version_namespace() . '\\SV_WC_Plugin' ) ) {
137 require_once( plugin_dir_path( __FILE__ ) . 'vendor/skyverge/wc-plugin-framework/woocommerce/class-sv-wc-plugin.php' );
138 }
139
140 if ( ! class_exists( '\\SkyVerge\\WooCommerce\\PluginFramework\\' . $this->get_framework_version_namespace() . '\\SV_WC_Payment_Gateway_Plugin' ) ) {
141 require_once( plugin_dir_path( __FILE__ ) . 'vendor/skyverge/wc-plugin-framework/woocommerce/payment-gateway/class-sv-wc-payment-gateway-plugin.php' );
142 }
143 }
144
145
146 /**
147 * Gets the framework version in namespace form.
148 *
149 * @since 2.0.0
150 *
151 * @return string
152 */
153 protected function get_framework_version_namespace() {
154
155 return 'v' . str_replace( '.', '_', $this->get_framework_version() );
156 }
157
158
159 /**
160 * Gets the framework version used by this plugin.
161 *
162 * @since 2.0.0
163 *
164 * @return string
165 */
166 protected function get_framework_version() {
167
168 return self::FRAMEWORK_VERSION;
169 }
170
171
172 /**
173 * Checks the server environment and other factors and deactivates plugins as necessary.
174 *
175 * Based on http://wptavern.com/how-to-prevent-wordpress-plugins-from-activating-on-sites-with-incompatible-hosting-environments
176 *
177 * @since 2.0.0
178 */
179 public function activation_check() {
180
181 if ( ! $this->is_environment_compatible() ) {
182
183 $this->deactivate_plugin();
184
185 wp_die( self::PLUGIN_NAME . ' could not be activated. ' . $this->get_environment_message() );
186 }
187 }
188
189 /**
190 * Checks the environment on loading WordPress, just in case the environment changes after activation.
191 *
192 * @since 2.0.0
193 */
194 public function check_environment() {
195
196 if ( ! $this->is_environment_compatible() && is_plugin_active( plugin_basename( __FILE__ ) ) ) {
197
198 $this->deactivate_plugin();
199
200 $this->add_admin_notice( 'bad_environment', 'error', self::PLUGIN_NAME . ' has been deactivated. ' . $this->get_environment_message() );
201 }
202 }
203
204
205 /**
206 * Adds notices for out-of-date WordPress and/or WooCommerce versions.
207 *
208 * @since 2.0.0
209 */
210 public function add_plugin_notices() {
211
212 if ( ! $this->is_wp_compatible() ) {
213
214 $this->add_admin_notice( 'update_wordpress', 'error', sprintf(
215 '%s requires WordPress version %s or higher. Please %supdate WordPress &raquo;%s',
216 '<strong>' . self::PLUGIN_NAME . '</strong>',
217 self::MINIMUM_WP_VERSION,
218 '<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">', '</a>'
219 ) );
220 }
221
222 if ( ! $this->is_wc_compatible() ) {
223
224 $this->add_admin_notice( 'update_woocommerce', 'error', sprintf(
225 '%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',
226 '<strong>' . self::PLUGIN_NAME . '</strong>',
227 self::MINIMUM_WC_VERSION,
228 '<a href="' . esc_url( admin_url( 'update-core.php' ) ) . '">', '</a>',
229 '<a href="' . esc_url( 'https://downloads.wordpress.org/plugin/woocommerce.' . self::MINIMUM_WC_VERSION . '.zip' ) . '">', '</a>'
230 ) );
231 }
232 }
233
234
235 /**
236 * Determines if the required plugins are compatible.
237 *
238 * @since 2.0.0
239 *
240 * @return bool
241 */
242 protected function plugins_compatible() {
243
244 return $this->is_wp_compatible() && $this->is_wc_compatible();
245 }
246
247
248 /**
249 * Determines if the WordPress compatible.
250 *
251 * @since 2.0.0
252 *
253 * @return bool
254 */
255 protected function is_wp_compatible() {
256
257 if ( ! self::MINIMUM_WP_VERSION ) {
258 return true;
259 }
260
261 return version_compare( get_bloginfo( 'version' ), self::MINIMUM_WP_VERSION, '>=' );
262 }
263
264
265 /**
266 * Determines if the WooCommerce compatible.
267 *
268 * @since 2.0.0
269 *
270 * @return bool
271 */
272 protected function is_wc_compatible() {
273
274 if ( ! self::MINIMUM_WC_VERSION ) {
275 return true;
276 }
277
278 return defined( 'WC_VERSION' ) && version_compare( WC_VERSION, self::MINIMUM_WC_VERSION, '>=' );
279 }
280
281
282 /**
283 * Deactivates the plugin.
284 *
285 * @since 2.0.0
286 */
287 protected function deactivate_plugin() {
288
289 deactivate_plugins( plugin_basename( __FILE__ ) );
290
291 if ( isset( $_GET['activate'] ) ) {
292 unset( $_GET['activate'] );
293 }
294 }
295
296
297 /**
298 * Adds an admin notice to be displayed.
299 *
300 * @since 2.0.0
301 *
302 * @param string $slug the slug for the notice
303 * @param string $class the css class for the notice
304 * @param string $message the notice message
305 */
306 public function add_admin_notice( $slug, $class, $message ) {
307
308 $this->notices[ $slug ] = array(
309 'class' => $class,
310 'message' => $message
311 );
312 }
313
314
315 /**
316 * Displays any admin notices added with \WooCommerce_Square_Loader::add_admin_notice()
317 *
318 * @since 2.0.0
319 */
320 public function admin_notices() {
321
322 foreach ( (array) $this->notices as $notice_key => $notice ) {
323
324 ?>
325 <div class="<?php echo esc_attr( $notice['class'] ); ?>">
326 <p>
327 <?php echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) ); ?>
328 </p>
329 </div>
330 <?php
331 }
332 }
333
334
335 /**
336 * Determines if the server environment is compatible with this plugin.
337 *
338 * Override this method to add checks for more than just the PHP version.
339 *
340 * @since 2.0.0
341 *
342 * @return bool
343 */
344 protected function is_environment_compatible() {
345
346 return version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '>=' );
347 }
348
349
350 /**
351 * Gets the message for display when the environment is incompatible with this plugin.
352 *
353 * @since 2.0.0
354 *
355 * @return string
356 */
357 protected function get_environment_message() {
358
359 $message = sprintf( 'The minimum PHP version required for this plugin is %1$s. You are running %2$s.', self::MINIMUM_PHP_VERSION, PHP_VERSION );
360
361 return $message;
362 }
363
364
365 /**
366 * Gets the main plugin loader instance.
367 *
368 * Ensures only one instance can be loaded.
369 *
370 * @since 2.0.0
371 *
372 * @return \WooCommerce_Square_Loader
373 */
374 public static function instance() {
375
376 if ( null === self::$instance ) {
377 self::$instance = new self();
378 }
379
380 return self::$instance;
381 }
382
383
384 }
385
386 // fire it up!
387 WooCommerce_Square_Loader::instance();
388