| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: WooCommerce Square |
| 4 |
* Version: 1.0.30 |
| 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.4 |
| 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.30' ); |
| 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’ 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’ 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/admin/class-wc-square-privacy.php' ); |
| 234 |
require_once( dirname( __FILE__ ) . '/includes/class-wc-square-install.php' ); |
| 235 |
require_once( dirname( __FILE__ ) . '/includes/class-wc-square-deactivation.php' ); |
| 236 |
require_once( dirname( __FILE__ ) . '/includes/class-wc-square-sync-logger.php' ); |
| 237 |
require_once( dirname( __FILE__ ) . '/includes/class-wc-square-wc-products.php' ); |
| 238 |
require_once( dirname( __FILE__ ) . '/includes/class-wc-square-connect.php' ); |
| 239 |
require_once( dirname( __FILE__ ) . '/includes/class-wc-square-sync-to-square.php' ); |
| 240 |
require_once( dirname( __FILE__ ) . '/includes/class-wc-square-sync-from-square.php' ); |
| 241 |
require_once( dirname( __FILE__ ) . '/includes/admin/class-wc-square-admin-integration.php' ); |
| 242 |
require_once( dirname( __FILE__ ) . '/includes/class-wc-square-sync-to-square-wp-hooks.php' ); |
| 243 |
require_once( dirname( __FILE__ ) . '/includes/class-wc-square-client.php' ); |
| 244 |
require_once( dirname( __FILE__ ) . '/includes/class-wc-square-utils.php' ); |
| 245 |
require_once( dirname( __FILE__ ) . '/includes/class-wc-square-inventory-poll.php' ); |
| 246 |
require_once( dirname( __FILE__ ) . '/includes/payment/class-wc-square-payment-logger.php' ); |
| 247 |
require_once( dirname( __FILE__ ) . '/includes/payment/class-wc-square-payments.php' ); |
| 248 |
|
| 249 |
if ( is_admin() ) { |
| 250 |
require_once( dirname( __FILE__ ) . '/includes/admin/class-wc-square-bulk-sync-handler.php' ); |
| 251 |
require_once( dirname( __FILE__ ) . '/includes/admin/class-wc-square-admin-product-meta-box.php' ); |
| 252 |
} |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Add integration settings page |
| 258 |
* |
| 259 |
* @access public |
| 260 |
* @since 1.0.0 |
| 261 |
* @version 1.0.0 |
| 262 |
* @return bool |
| 263 |
*/ |
| 264 |
public function include_integration( $integrations ) { |
| 265 |
// Square only supports US and Canada for now. |
| 266 |
if ( $this->is_allowed_currencies() && $this->is_allowed_countries() ) { |
| 267 |
$integrations[] = $this->integration; |
| 268 |
} |
| 269 |
|
| 270 |
return $integrations; |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Initializes hooks |
| 276 |
* |
| 277 |
* @access public |
| 278 |
* @since 1.0.0 |
| 279 |
* @version 1.0.0 |
| 280 |
* @return bool |
| 281 |
*/ |
| 282 |
public function init_hooks() { |
| 283 |
|
| 284 |
register_deactivation_hook( __FILE__, array( 'WC_Square_Deactivation', 'deactivate' ) ); |
| 285 |
|
| 286 |
if ( class_exists( 'WooCommerce' ) ) { |
| 287 |
|
| 288 |
add_filter( 'woocommerce_integrations', array( $this, 'include_integration' ) ); |
| 289 |
|
| 290 |
add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) ); |
| 291 |
|
| 292 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); |
| 293 |
|
| 294 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); |
| 295 |
|
| 296 |
add_action( 'woocommerce_square_bulk_syncing_square_to_wc', array( $this->wc_to_square_wp_hooks, 'disable' ) ); |
| 297 |
|
| 298 |
add_action( 'admin_notices', array( $this, 'is_connected_to_square' ) ); |
| 299 |
|
| 300 |
} else { |
| 301 |
|
| 302 |
add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) ); |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Loads the admin JS scripts |
| 310 |
* |
| 311 |
* @access public |
| 312 |
* @since 1.0.0 |
| 313 |
* @version 1.0.0 |
| 314 |
* @return bool |
| 315 |
*/ |
| 316 |
public function enqueue_admin_scripts() { |
| 317 |
$current_screen = get_current_screen(); |
| 318 |
|
| 319 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 320 |
|
| 321 |
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 ); |
| 322 |
|
| 323 |
if ( 'woocommerce_page_wc-settings' === $current_screen->id ) { |
| 324 |
|
| 325 |
wp_enqueue_script( 'wc-square-admin-scripts' ); |
| 326 |
|
| 327 |
$localized_vars = array( |
| 328 |
'admin_ajax_url' => admin_url( 'admin-ajax.php' ), |
| 329 |
'ajaxSyncNonce' => wp_create_nonce( 'square-sync' ), |
| 330 |
'country_currency' => $this->is_allowed_countries() && $this->is_allowed_currencies(), |
| 331 |
'i18n' => array( |
| 332 |
'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' ), |
| 333 |
), |
| 334 |
); |
| 335 |
|
| 336 |
wp_localize_script( 'wc-square-admin-scripts', 'wc_square_local', $localized_vars ); |
| 337 |
} |
| 338 |
|
| 339 |
return true; |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Loads the admin CSS styles |
| 344 |
* |
| 345 |
* @access public |
| 346 |
* @since 1.0.0 |
| 347 |
* @version 1.0.0 |
| 348 |
* @return bool |
| 349 |
*/ |
| 350 |
public function enqueue_admin_styles() { |
| 351 |
$current_screen = get_current_screen(); |
| 352 |
|
| 353 |
wp_register_style( 'wc-square-admin-styles', WC_SQUARE_PLUGIN_URL . '/assets/css/wc-square-admin-styles.css', null, WC_SQUARE_VERSION ); |
| 354 |
|
| 355 |
if ( 'woocommerce_page_wc-settings' === $current_screen->id ) { |
| 356 |
|
| 357 |
wp_enqueue_style( 'wc-square-admin-styles' ); |
| 358 |
} |
| 359 |
|
| 360 |
return true; |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Load the plugin text domain for translation. |
| 365 |
* |
| 366 |
* @access public |
| 367 |
* @since 1.0.0 |
| 368 |
* @version 1.0.0 |
| 369 |
* @return bool |
| 370 |
*/ |
| 371 |
public function load_plugin_textdomain() { |
| 372 |
$locale = apply_filters( 'woocommerce_square_plugin_locale', get_locale(), 'woocommerce-square' ); |
| 373 |
|
| 374 |
load_textdomain( 'woocommerce-square', trailingslashit( WP_LANG_DIR ) . 'woocommerce-square/woocommerce-square' . '-' . $locale . '.mo' ); |
| 375 |
|
| 376 |
load_plugin_textdomain( 'woocommerce-square', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
| 377 |
|
| 378 |
return true; |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* WooCommerce fallback notice. |
| 383 |
* |
| 384 |
* @access public |
| 385 |
* @since 1.0.0 |
| 386 |
* @version 1.0.0 |
| 387 |
* @return string |
| 388 |
*/ |
| 389 |
public function woocommerce_missing_notice() { |
| 390 |
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>'; |
| 391 |
|
| 392 |
return true; |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Shows a notice when the site is not yet connected to square. |
| 397 |
* |
| 398 |
* @access public |
| 399 |
* @since 1.0.0 |
| 400 |
* @version 1.0.0 |
| 401 |
* @return string |
| 402 |
*/ |
| 403 |
public function is_connected_to_square() { |
| 404 |
$settings = get_option( 'woocommerce_squareconnect_settings', '' ); |
| 405 |
$existing_token = get_option( 'woocommerce_square_merchant_access_token' ); |
| 406 |
|
| 407 |
if ( empty( $existing_token ) ) { |
| 408 |
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§ion=squareconnect' ) . '">', '</a>' ) . '</p></div>'; |
| 409 |
} |
| 410 |
|
| 411 |
if ( empty( $settings ) || empty( $settings['location'] ) ) { |
| 412 |
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§ion=squareconnect' ) . '">', '</a>' ) . '</p></div>'; |
| 413 |
} |
| 414 |
|
| 415 |
return true; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
Woocommerce_Square::instance(); |
| 420 |
|
| 421 |
endif; |
| 422 |
|