PluginProbe
WooCommerce Square / 1.0.29
WooCommerce Square v1.0.29
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
421 lines 12.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: 1.0.29
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 * Requires at least: 4.5.0
10 * Tested up to: 4.9
11 * WC requires at least: 2.6
12 * WC tested up to: 3.3
13 * Text Domain: woocommerce-square
14 * Domain Path: /languages
15 *
16 * @package WordPress
17 * @author WooCommerce
18 */
19
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit;
22 }
23
24 if ( ! class_exists( 'Woocommerce_Square' ) ) :
25
26 define( 'WC_SQUARE_VERSION', '1.0.29' );
27
28 /**
29 * Main class.
30 *
31 * @package Woocommerce_Square
32 * @since 1.0.0
33 * @version 1.0.0
34 */
35 class Woocommerce_Square {
36
37 private static $_instance = null;
38
39 /**
40 * @var WC_Integration
41 */
42 public $integration;
43
44 /**
45 * @var WC_Square_Client
46 */
47 public $square_client;
48
49 /**
50 * @var WC_Square_Connect
51 */
52 public $square_connect;
53
54 /**
55 * @var WC_Square_Sync_To_Square_WordPress_Hooks
56 */
57 protected $wc_to_square_wp_hooks;
58
59 /**
60 * Get the single instance aka Singleton
61 *
62 * @access public
63 * @since 1.0.0
64 * @version 1.0.0
65 * @return bool
66 */
67 public static function instance() {
68 if ( is_null( self::$_instance ) ) {
69 self::$_instance = new self();
70 }
71
72 return self::$_instance;
73 }
74
75 /**
76 * Prevent cloning
77 *
78 * @access public
79 * @since 1.0.0
80 * @version 1.0.0
81 * @return bool
82 */
83 public function __clone() {
84 _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce-square' ), WC_SQUARE_VERSION );
85 }
86
87 /**
88 * Prevent unserializing instances
89 *
90 * @access public
91 * @since 1.0.0
92 * @version 1.0.0
93 * @return bool
94 */
95 public function __wakeup() {
96 _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce-square' ), WC_SQUARE_VERSION );
97 }
98
99 /**
100 * Woocommerce_Square constructor.
101 */
102 private function __construct() {
103
104 add_action( 'woocommerce_loaded', array( $this, 'bootstrap' ) );
105
106 }
107
108 public function bootstrap() {
109 add_action( 'admin_notices', array( $this, 'check_environment' ) );
110
111 $this->define_constants();
112 $this->includes();
113 $this->init();
114 $this->init_hooks();
115
116 do_action( 'wc_square_loaded' );
117
118 }
119
120 public function init() {
121 $this->integration = new WC_Square_Integration();
122
123 $square_client = new WC_Square_Client();
124
125 $access_token = get_option( 'woocommerce_square_merchant_access_token' );
126 $square_client->set_access_token( $access_token );
127 $square_client->set_merchant_id( $this->integration->get_option( 'location' ) );
128 $this->square_client = $square_client;
129
130 $this->square_connect = new WC_Square_Connect( $square_client );
131
132 $wc_to_square_sync = new WC_Square_Sync_To_Square( $this->square_connect );
133 $square_to_wc_sync = new WC_Square_Sync_From_Square( $this->square_connect );
134
135 $inventory_poll = new WC_Square_Inventory_Poll( $this->integration, $square_to_wc_sync );
136
137 if ( is_admin() ) {
138
139 $bulk_handler = new WC_Square_Bulk_Sync_Handler( $this->square_connect, $wc_to_square_sync, $square_to_wc_sync );
140
141 }
142
143 $this->wc_to_square_wp_hooks = new WC_Square_Sync_To_Square_WordPress_Hooks( $this->integration, $wc_to_square_sync );
144 }
145
146 /**
147 * Define constants
148 *
149 * @access public
150 * @since 1.0.0
151 * @version 1.0.0
152 * @return bool
153 */
154 public function define_constants() {
155 define( 'WC_SQUARE_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
156 define( 'WC_SQUARE_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
157
158 // if using staging, define this in wp-config.php
159 if ( ! defined( 'WC_SQUARE_ENABLE_STAGING' ) ) {
160 define( 'WC_SQUARE_ENABLE_STAGING', false );
161 }
162
163 return true;
164 }
165
166 /**
167 * Check if country is set to allowed country.
168 *
169 * @since 1.0.10
170 * @version 1.0.10
171 */
172 public function is_allowed_countries() {
173 if ( 'US' !== WC()->countries->get_base_country() && 'CA' !== WC()->countries->get_base_country() && 'AU' !== WC()->countries->get_base_country() && 'GB' !== WC()->countries->get_base_country() && 'JP' !== WC()->countries->get_base_country() ) {
174 return false;
175 }
176
177 return true;
178 }
179
180 /**
181 * Check if currency is set to allowed currency.
182 *
183 * @since 1.0.10
184 * @version 1.0.10
185 */
186 public function is_allowed_currencies() {
187 if ( 'USD' !== get_woocommerce_currency() && 'CAD' !== get_woocommerce_currency() && 'AUD' !== get_woocommerce_currency() && 'GBP' !== get_woocommerce_currency() && 'JPY' !== get_woocommerce_currency() ) {
188 return false;
189 }
190
191 return true;
192 }
193
194 /**
195 * Check required environment
196 *
197 * @access public
198 * @since 1.0.10
199 * @version 1.0.10
200 * @return null
201 */
202 public function check_environment() {
203 if ( ! current_user_can( 'manage_woocommerce' ) ) {
204 return;
205 }
206
207 if ( ! $this->is_allowed_countries() ) {
208 $admin_page = 'wc-settings';
209
210 echo '<div class="error">
211 <p>' . sprintf( __( 'Square requires that the <a href="%s">base country/region</a> is Australia, Canada, Japan, United Kingdom, or United States.', 'woocommerce-square' ), admin_url( 'admin.php?page=' . $admin_page . '&tab=general' ) ) . '</p>
212 </div>';
213 }
214
215 if ( ! $this->is_allowed_currencies() ) {
216 $admin_page = 'wc-settings';
217
218 echo '<div class="error">
219 <p>' . sprintf( __( 'Square requires that the <a href="%s">currency</a> is set to AUD, CAD, GBP, JPY, or USD.', 'woocommerce-square' ), admin_url( 'admin.php?page=' . $admin_page . '&tab=general' ) ) . '</p>
220 </div>';
221 }
222 }
223
224 /**
225 * Include all files needed
226 *
227 * @access public
228 * @since 1.0.0
229 * @version 1.0.0
230 * @return bool
231 */
232 public function includes() {
233 require_once( dirname( __FILE__ ) . '/includes/class-wc-square-install.php' );
234 require_once( dirname( __FILE__ ) . '/includes/class-wc-square-deactivation.php' );
235 require_once( dirname( __FILE__ ) . '/includes/class-wc-square-sync-logger.php' );
236 require_once( dirname( __FILE__ ) . '/includes/class-wc-square-wc-products.php' );
237 require_once( dirname( __FILE__ ) . '/includes/class-wc-square-connect.php' );
238 require_once( dirname( __FILE__ ) . '/includes/class-wc-square-sync-to-square.php' );
239 require_once( dirname( __FILE__ ) . '/includes/class-wc-square-sync-from-square.php' );
240 require_once( dirname( __FILE__ ) . '/includes/admin/class-wc-square-admin-integration.php' );
241 require_once( dirname( __FILE__ ) . '/includes/class-wc-square-sync-to-square-wp-hooks.php' );
242 require_once( dirname( __FILE__ ) . '/includes/class-wc-square-client.php' );
243 require_once( dirname( __FILE__ ) . '/includes/class-wc-square-utils.php' );
244 require_once( dirname( __FILE__ ) . '/includes/class-wc-square-inventory-poll.php' );
245 require_once( dirname( __FILE__ ) . '/includes/payment/class-wc-square-payment-logger.php' );
246 require_once( dirname( __FILE__ ) . '/includes/payment/class-wc-square-payments.php' );
247
248 if ( is_admin() ) {
249 require_once( dirname( __FILE__ ) . '/includes/admin/class-wc-square-bulk-sync-handler.php' );
250 require_once( dirname( __FILE__ ) . '/includes/admin/class-wc-square-admin-product-meta-box.php' );
251 }
252
253 }
254
255 /**
256 * Add integration settings page
257 *
258 * @access public
259 * @since 1.0.0
260 * @version 1.0.0
261 * @return bool
262 */
263 public function include_integration( $integrations ) {
264 // Square only supports US and Canada for now.
265 if ( $this->is_allowed_currencies() && $this->is_allowed_countries() ) {
266 $integrations[] = $this->integration;
267 }
268
269 return $integrations;
270
271 }
272
273 /**
274 * Initializes hooks
275 *
276 * @access public
277 * @since 1.0.0
278 * @version 1.0.0
279 * @return bool
280 */
281 public function init_hooks() {
282
283 register_deactivation_hook( __FILE__, array( 'WC_Square_Deactivation', 'deactivate' ) );
284
285 if ( class_exists( 'WooCommerce' ) ) {
286
287 add_filter( 'woocommerce_integrations', array( $this, 'include_integration' ) );
288
289 add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
290
291 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
292
293 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
294
295 add_action( 'woocommerce_square_bulk_syncing_square_to_wc', array( $this->wc_to_square_wp_hooks, 'disable' ) );
296
297 add_action( 'admin_notices', array( $this, 'is_connected_to_square' ) );
298
299 } else {
300
301 add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
302
303 }
304
305 }
306
307 /**
308 * Loads the admin JS scripts
309 *
310 * @access public
311 * @since 1.0.0
312 * @version 1.0.0
313 * @return bool
314 */
315 public function enqueue_admin_scripts() {
316 $current_screen = get_current_screen();
317
318 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
319
320 wp_register_script( 'wc-square-admin-scripts', WC_SQUARE_PLUGIN_URL . '/assets/js/wc-square-admin-scripts' . $suffix . '.js', array( 'jquery' ), WC_SQUARE_VERSION, true );
321
322 if ( 'woocommerce_page_wc-settings' === $current_screen->id ) {
323
324 wp_enqueue_script( 'wc-square-admin-scripts' );
325
326 $localized_vars = array(
327 'admin_ajax_url' => admin_url( 'admin-ajax.php' ),
328 'ajaxSyncNonce' => wp_create_nonce( 'square-sync' ),
329 'country_currency' => $this->is_allowed_countries() && $this->is_allowed_currencies(),
330 'i18n' => array(
331 'confirm_sync' => __( 'This process may take awhile depending on the amount of items that need to be synced. Please do not close this tab/window or else the sync will terminate. Click OK to continue to sync.', 'woocommerce-square' ),
332 ),
333 );
334
335 wp_localize_script( 'wc-square-admin-scripts', 'wc_square_local', $localized_vars );
336 }
337
338 return true;
339 }
340
341 /**
342 * Loads the admin CSS styles
343 *
344 * @access public
345 * @since 1.0.0
346 * @version 1.0.0
347 * @return bool
348 */
349 public function enqueue_admin_styles() {
350 $current_screen = get_current_screen();
351
352 wp_register_style( 'wc-square-admin-styles', WC_SQUARE_PLUGIN_URL . '/assets/css/wc-square-admin-styles.css', null, WC_SQUARE_VERSION );
353
354 if ( 'woocommerce_page_wc-settings' === $current_screen->id ) {
355
356 wp_enqueue_style( 'wc-square-admin-styles' );
357 }
358
359 return true;
360 }
361
362 /**
363 * Load the plugin text domain for translation.
364 *
365 * @access public
366 * @since 1.0.0
367 * @version 1.0.0
368 * @return bool
369 */
370 public function load_plugin_textdomain() {
371 $locale = apply_filters( 'woocommerce_square_plugin_locale', get_locale(), 'woocommerce-square' );
372
373 load_textdomain( 'woocommerce-square', trailingslashit( WP_LANG_DIR ) . 'woocommerce-square/woocommerce-square' . '-' . $locale . '.mo' );
374
375 load_plugin_textdomain( 'woocommerce-square', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
376
377 return true;
378 }
379
380 /**
381 * WooCommerce fallback notice.
382 *
383 * @access public
384 * @since 1.0.0
385 * @version 1.0.0
386 * @return string
387 */
388 public function woocommerce_missing_notice() {
389 echo '<div class="error"><p>' . sprintf( __( 'WooCommerce Square Plugin requires WooCommerce to be installed and active. You can download %s here.', 'woocommerce-square' ), '<a href="https://woocommerce.com/woocommerce/" target="_blank">WooCommerce</a>' ) . '</p></div>';
390
391 return true;
392 }
393
394 /**
395 * Shows a notice when the site is not yet connected to square.
396 *
397 * @access public
398 * @since 1.0.0
399 * @version 1.0.0
400 * @return string
401 */
402 public function is_connected_to_square() {
403 $settings = get_option( 'woocommerce_squareconnect_settings', '' );
404 $existing_token = get_option( 'woocommerce_square_merchant_access_token' );
405
406 if ( empty( $existing_token ) ) {
407 echo '<div class="error"><p>' . sprintf( __( 'WooCommerce Square is almost ready. To get started, %1$sconnect your Square Account.%2$s', 'woocommerce-square' ), '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=integration&section=squareconnect' ) . '">', '</a>' ) . '</p></div>';
408 }
409
410 if ( empty( $settings ) || empty( $settings['location'] ) ) {
411 echo '<div class="error"><p>' . sprintf( __( 'WooCommerce Square is almost ready. Please %1$sset your business location.%2$s', 'woocommerce-square' ), '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=integration&section=squareconnect' ) . '">', '</a>' ) . '</p></div>';
412 }
413
414 return true;
415 }
416 }
417
418 Woocommerce_Square::instance();
419
420 endif;
421