Plugin.php
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square; |
| 25 | |
| 26 | defined( 'ABSPATH' ) || exit; |
| 27 | |
| 28 | use WooCommerce\Square\Framework\PaymentGateway\Payment_Gateway_Plugin; |
| 29 | use WooCommerce\Square\Framework\Square_Helper; |
| 30 | use WooCommerce\Square\Handlers\Background_Job; |
| 31 | use WooCommerce\Square\Handlers\Email; |
| 32 | use WooCommerce\Square\Handlers\Order; |
| 33 | use WooCommerce\Square\Handlers\Product; |
| 34 | use WooCommerce\Square\Handlers\Sync; |
| 35 | use WooCommerce\Square\Handlers\Products; |
| 36 | |
| 37 | /** |
| 38 | * The main plugin class. |
| 39 | * |
| 40 | * @since 2.0.0 |
| 41 | */ |
| 42 | class Plugin extends Payment_Gateway_Plugin { |
| 43 | |
| 44 | |
| 45 | /** plugin version number */ |
| 46 | const VERSION = WC_SQUARE_PLUGIN_VERSION; |
| 47 | |
| 48 | /** plugin ID */ |
| 49 | const PLUGIN_ID = 'square'; |
| 50 | |
| 51 | /** string gateway ID */ |
| 52 | const GATEWAY_ID = 'square_credit_card'; |
| 53 | |
| 54 | |
| 55 | /** @var Plugin plugin instance */ |
| 56 | protected static $instance; |
| 57 | |
| 58 | /** @var Settings settings handler instance */ |
| 59 | private $settings_handler; |
| 60 | |
| 61 | /** @var Handlers\Connection connection handler instance */ |
| 62 | private $connection_handler; |
| 63 | |
| 64 | /** @var Admin admin handler instance */ |
| 65 | private $admin_handler; |
| 66 | |
| 67 | /** @var Sync sync handler instance */ |
| 68 | private $sync_handler; |
| 69 | |
| 70 | /** @var Background_Job background handler instance */ |
| 71 | private $background_job_handler; |
| 72 | |
| 73 | /** @var AJAX handler instance */ |
| 74 | private $ajax_handler; |
| 75 | |
| 76 | /** @var Email emails handler */ |
| 77 | private $email_handler; |
| 78 | |
| 79 | /** @var Order orders handler */ |
| 80 | private $order_handler; |
| 81 | |
| 82 | /** @var Products products handler */ |
| 83 | private $products_handler; |
| 84 | |
| 85 | /** |
| 86 | * Constructs the plugin. |
| 87 | * |
| 88 | * @since 2.0.0 |
| 89 | */ |
| 90 | public function __construct() { |
| 91 | |
| 92 | parent::__construct( |
| 93 | self::PLUGIN_ID, |
| 94 | self::VERSION, |
| 95 | array( |
| 96 | 'text_domain' => 'woocommerce-square', |
| 97 | 'gateways' => array( self::GATEWAY_ID => Gateway::class ), |
| 98 | 'require_ssl' => true, |
| 99 | 'supports' => array( |
| 100 | self::FEATURE_CAPTURE_CHARGE, |
| 101 | self::FEATURE_CUSTOMER_ID, |
| 102 | self::FEATURE_MY_PAYMENT_METHODS, |
| 103 | ), |
| 104 | 'dependencies' => array( |
| 105 | 'php_extensions' => array( 'curl', 'json', 'mbstring' ), |
| 106 | ), |
| 107 | ) |
| 108 | ); |
| 109 | |
| 110 | $this->includes(); |
| 111 | |
| 112 | /** |
| 113 | * Fires upon plugin loaded (legacy hook). |
| 114 | * |
| 115 | * @since 1.0.0 |
| 116 | */ |
| 117 | do_action( 'wc_square_loaded' ); |
| 118 | |
| 119 | add_action( 'woocommerce_register_taxonomy', array( $this, 'init_taxonomies' ) ); |
| 120 | add_action( 'admin_notices', array( $this, 'add_admin_notices' ) ); |
| 121 | add_filter( 'woocommerce_locate_template', array( $this, 'locate_template' ), 20, 3 ); |
| 122 | add_filter( 'woocommerce_locate_core_template', array( $this, 'locate_template' ), 20, 3 ); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /** |
| 127 | * Includes required classes. |
| 128 | * |
| 129 | * @since 2.0.0 |
| 130 | */ |
| 131 | private function includes() { |
| 132 | |
| 133 | $this->connection_handler = new Handlers\Connection( $this ); |
| 134 | |
| 135 | $this->sync_handler = new Sync( $this ); |
| 136 | |
| 137 | // background export must be loaded all the time, because otherwise background jobs simply won't work |
| 138 | require_once $this->get_framework_path() . '/Utilities/WP_Background_Job_Handler.php'; |
| 139 | require_once $this->get_framework_path() . '/Utilities/WP_Job_Batch_Handler.php'; |
| 140 | |
| 141 | $this->background_job_handler = new Background_Job(); |
| 142 | |
| 143 | $this->ajax_handler = new AJAX(); |
| 144 | |
| 145 | $this->email_handler = new Email(); |
| 146 | |
| 147 | $this->order_handler = new Order(); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * Adds API request logging. |
| 153 | * |
| 154 | * @internal |
| 155 | * |
| 156 | * @since 2.0.0 |
| 157 | */ |
| 158 | public function add_api_request_logging() { |
| 159 | |
| 160 | if ( ! has_action( 'wc_square_api_request_performed' ) ) { |
| 161 | add_action( 'wc_square_api_request_performed', array( $this, 'log_api_request' ), 10, 2 ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * Logs an API request & response. |
| 168 | * |
| 169 | * @since 2.0.0 |
| 170 | * |
| 171 | * @param array $request request data |
| 172 | * @param array $response response data |
| 173 | * @param string|null $log_id log ID |
| 174 | */ |
| 175 | public function log_api_request( $request, $response, $log_id = null ) { |
| 176 | |
| 177 | if ( $this->get_settings_handler() && $this->get_settings_handler()->is_debug_enabled() ) { |
| 178 | parent::log_api_request( $request, $response, $log_id ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * If debug logging is enabled, saves errors or messages to Square Log |
| 185 | * |
| 186 | * @since 2.2.4 |
| 187 | * @param string $message error or message to save to log |
| 188 | * @param string $log_id optional log id to segment the files by, defaults to plugin id |
| 189 | */ |
| 190 | public function log( $message, $log_id = null ) { |
| 191 | |
| 192 | if ( $this->get_settings_handler() && $this->get_settings_handler()->is_debug_enabled() ) { |
| 193 | parent::log( $message, $log_id ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /** |
| 199 | * Initializes the lifecycle handler. |
| 200 | * |
| 201 | * @since 2.0.0 |
| 202 | */ |
| 203 | public function init_lifecycle_handler() { |
| 204 | |
| 205 | $this->lifecycle_handler = new Lifecycle( $this ); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /** |
| 210 | * Registers custom taxonomies. |
| 211 | * |
| 212 | * @internal |
| 213 | * |
| 214 | * @since 2.0.0 |
| 215 | */ |
| 216 | public function init_taxonomies() { |
| 217 | |
| 218 | Product::init_taxonomies(); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * Initializes the general plugin functionality. |
| 224 | * |
| 225 | * @since 2.0.0 |
| 226 | */ |
| 227 | public function init_plugin() { |
| 228 | |
| 229 | $this->settings_handler = new Settings( $this ); |
| 230 | $this->products_handler = new Products( $this ); |
| 231 | |
| 232 | if ( ! $this->admin_handler && is_admin() ) { |
| 233 | $this->admin_handler = new Admin( $this ); |
| 234 | } |
| 235 | |
| 236 | do_action( 'wc_square_initialized' ); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /** |
| 241 | * Locates the WooCommerce template files from our templates directory. |
| 242 | * |
| 243 | * @internal |
| 244 | * |
| 245 | * @since 2.0.0 |
| 246 | * |
| 247 | * @param string $template already found template |
| 248 | * @param string $template_name searchable template name |
| 249 | * @param string $template_path template path |
| 250 | * @return string search result for the template |
| 251 | */ |
| 252 | public function locate_template( $template, $template_name, $template_path ) { |
| 253 | |
| 254 | // only keep looking if no custom theme template was found |
| 255 | // or if a default WooCommerce template was found |
| 256 | if ( ! $template || Square_Helper::str_starts_with( $template, WC()->plugin_path() ) ) { |
| 257 | |
| 258 | // set the path to our templates directory |
| 259 | $plugin_path = $this->get_plugin_path() . '/templates/'; |
| 260 | |
| 261 | // if a template is found, make it so |
| 262 | if ( is_readable( $plugin_path . $template_name ) ) { |
| 263 | $template = $plugin_path . $template_name; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return $template; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /** Admin methods *************************************************************************************************/ |
| 272 | |
| 273 | |
| 274 | /** |
| 275 | * Adds admin notices. |
| 276 | * |
| 277 | * @since 2.0.0 |
| 278 | */ |
| 279 | public function add_admin_notices() { |
| 280 | |
| 281 | // show any one-off messages |
| 282 | $this->get_message_handler()->show_messages(); |
| 283 | |
| 284 | // display a notice if the auto-refresh failed |
| 285 | if ( get_option( 'wc_square_refresh_failed', false ) ) { |
| 286 | |
| 287 | $message = sprintf( |
| 288 | __( 'Heads up! There may be a problem with your connection to Square. In order to continue accepting payments, please %1$sdisconnect and re-connect your site%2$s.', 'woocommerce-square' ), |
| 289 | '<a href="' . esc_url( $this->get_settings_url() ) . '">', |
| 290 | '</a>' |
| 291 | ); |
| 292 | |
| 293 | $this->get_admin_notice_handler()->add_admin_notice( |
| 294 | $message, |
| 295 | 'refresh-failed', |
| 296 | array( |
| 297 | 'dismissible' => false, |
| 298 | 'notice_class' => 'notice-warning', |
| 299 | ) |
| 300 | ); |
| 301 | } |
| 302 | |
| 303 | if ( $this->get_settings_handler()->is_connected() ) { |
| 304 | |
| 305 | $message = '<strong>' . __( 'You are connected to Square!', 'woocommerce-square' ) . '</strong>'; |
| 306 | |
| 307 | // prompt to set a location if not set |
| 308 | if ( ! $this->get_settings_handler()->get_location_id() ) { |
| 309 | |
| 310 | if ( $this->is_plugin_settings() ) { |
| 311 | |
| 312 | $instruction = __( 'To get started, set your business location.', 'woocommerce-square' ); |
| 313 | |
| 314 | } else { |
| 315 | |
| 316 | $instruction = sprintf( |
| 317 | /* translators: Placeholders: %1$s - <a> tag, %2$s - </a> tag */ |
| 318 | __( 'Visit the %1$splugin settings%2$s to set your business location.', 'woocommerce-square' ), |
| 319 | '<a href="' . esc_url( $this->get_settings_url() ) . '">', |
| 320 | '</a>' |
| 321 | ); |
| 322 | } |
| 323 | |
| 324 | $this->get_admin_notice_handler()->add_admin_notice( $message . ' ' . $instruction, 'set-location' ); |
| 325 | |
| 326 | } elseif ( ! $this->get_sync_handler()->get_last_synced_at() && $this->get_settings_handler()->is_product_sync_enabled() ) { |
| 327 | |
| 328 | $message = __( 'You are ready to sync products!', 'woocommerce-square' ); |
| 329 | |
| 330 | if ( ! empty( Product::get_products_synced_with_square() ) ) { |
| 331 | |
| 332 | $instruction = sprintf( |
| 333 | /* translators: Placeholders: %1$s - <strong> tag, %2$s - product count, %3$s - </strong> tag, %4$s - <a> tag, %5$s - </a> tag */ |
| 334 | __( '%1$s%2$d products%3$s are marked "sync with Square". %4$sStart a new sync now »%5$s', 'woocommerce-square' ), |
| 335 | '<strong>', |
| 336 | count( Product::get_products_synced_with_square() ), |
| 337 | '</strong>', |
| 338 | '<a href="' . esc_url( add_query_arg( 'section', 'update', $this->get_settings_url() ) ) . '">', |
| 339 | '</a>' |
| 340 | ); |
| 341 | |
| 342 | } else { |
| 343 | |
| 344 | $instruction = sprintf( |
| 345 | /* translators: Placeholders: %1$s - <strong> tag, %2$s - </strong> tag, %3$s - <a> tag, %4$s - </a> tag */ |
| 346 | __( '%1$sNo products%2$s are marked "sync with Square". %3$sUpdate your products to sync data »%4$s', 'woocommerce-square' ), |
| 347 | '<strong>', |
| 348 | '</strong>', |
| 349 | '<a href="' . esc_url( admin_url( 'edit.php?post_type=product' ) ) . '">', |
| 350 | '</a>' |
| 351 | ); |
| 352 | } |
| 353 | |
| 354 | $this->get_admin_notice_handler()->add_admin_notice( $message . ' ' . $instruction, 'set-location' ); |
| 355 | } |
| 356 | |
| 357 | // a notice for when WC stock handling is globally disabled |
| 358 | if ( 'yes' !== get_option( 'woocommerce_manage_stock' ) && $this->get_settings_handler()->is_inventory_sync_enabled() ) { |
| 359 | |
| 360 | $message = sprintf( |
| 361 | __( 'Heads up! Square is configured to sync product inventory, but WooCommerce stock management is disabled. Please %1$senable stock management%2$s to ensure product inventory counts are kept in sync.', 'woocommerce-square' ), |
| 362 | '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=products§ion=inventory' ) ) . '">', |
| 363 | '</a>' |
| 364 | ); |
| 365 | |
| 366 | $this->get_admin_notice_handler()->add_admin_notice( |
| 367 | $message, |
| 368 | 'enable-wc-sync', |
| 369 | array( |
| 370 | 'notice_class' => 'notice-warning', |
| 371 | ) |
| 372 | ); |
| 373 | } |
| 374 | } else { |
| 375 | |
| 376 | if ( $this->is_plugin_settings() ) { |
| 377 | |
| 378 | $instruction = __( 'To get started, connect with Square.', 'woocommerce-square' ); |
| 379 | |
| 380 | } else { |
| 381 | |
| 382 | $instruction = sprintf( |
| 383 | /* translators: Placeholders: %1$s - <a> tag, %2$s - </a> tag */ |
| 384 | __( 'To get started, %1$sconnect with Square »%2$s', 'woocommerce-square' ), |
| 385 | '<a href="' . esc_url( $this->get_settings_url() ) . '">', |
| 386 | '</a>' |
| 387 | ); |
| 388 | } |
| 389 | |
| 390 | $message = sprintf( |
| 391 | /* translators: Placeholders: %1$s - plugin name */ |
| 392 | __( 'Thanks for installing %1$s!', 'woocommerce-square' ), |
| 393 | esc_html( $this->get_plugin_name() ) |
| 394 | ); |
| 395 | |
| 396 | $this->get_admin_notice_handler()->add_admin_notice( $message . ' ' . $instruction, 'connect' ); |
| 397 | } |
| 398 | |
| 399 | // add a notice for out-of-bounds base locations |
| 400 | $this->add_base_location_admin_notice(); |
| 401 | |
| 402 | // add a notice when no refresh token is available |
| 403 | $this->add_missing_refresh_token_notice(); |
| 404 | |
| 405 | // add a tax-inclusive warning to product pages |
| 406 | $this->add_tax_inclusive_pricing_notice(); |
| 407 | |
| 408 | if ( get_option( 'wc_square_updated_to_2_0_0' ) ) { |
| 409 | |
| 410 | $this->get_admin_notice_handler()->add_admin_notice( |
| 411 | sprintf( |
| 412 | /* translators: Placeholders: %1$s - plugin name, %2$ - plugin version number, %3$s - opening <a> HTML link tag, %4$s - closing </a> HTML link tag, %5$s - opening <a> HTML link tag, %6$s - closing </a> HTML link tag*/ |
| 413 | esc_html__( '%1$s has been updated to version %2$s. In order to continue syncing product inventory, please make sure to disconnect and reconnect with Square from the %3$splugin settings%4$s and re-sync your products. Read more in the %5$supdated documentation%6$s.', 'woocommerce-square' ), |
| 414 | '<strong>' . esc_html( $this->get_plugin_name() ) . '</strong>', |
| 415 | $this->get_version(), |
| 416 | '<a href="' . esc_url( $this->get_settings_url() ) . '">', |
| 417 | '</a>', |
| 418 | '<a href="' . esc_url( $this->get_documentation_url() ) . '">', |
| 419 | '</a>' |
| 420 | ), |
| 421 | 'updated-to-v2', |
| 422 | array( 'notice_class' => 'notice-warning' ) |
| 423 | ); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | |
| 428 | /** |
| 429 | * Adds a notice for out-of-bounds base locations. |
| 430 | * |
| 431 | * @since 2.0.0 |
| 432 | */ |
| 433 | protected function add_base_location_admin_notice() { |
| 434 | |
| 435 | $accepted_countries = array( |
| 436 | 'US', |
| 437 | 'CA', |
| 438 | 'GB', |
| 439 | 'AU', |
| 440 | 'JP', |
| 441 | 'IE', |
| 442 | 'FR', |
| 443 | 'ES', |
| 444 | ); |
| 445 | |
| 446 | $base_location = wc_get_base_location(); |
| 447 | |
| 448 | if ( isset( $base_location['country'] ) && ! in_array( $base_location['country'], $accepted_countries, true ) ) { |
| 449 | |
| 450 | $this->get_admin_notice_handler()->add_admin_notice( |
| 451 | sprintf( |
| 452 | /* translators: Placeholders: %1$s - <strong> tag, %2$s - </strong> tag, %3$s - 2-character country code, %4$s - comma separated list of 2-character country codes */ |
| 453 | __( '%1$sWooCommerce Square:%2$s Your base country is %3$s, but Square can’t accept transactions from merchants outside of %4$s.', 'woocommerce-square' ), |
| 454 | '<strong>', |
| 455 | '</strong>', |
| 456 | esc_html( $base_location['country'] ), |
| 457 | esc_html( Square_Helper::list_array_items( $accepted_countries ) ) |
| 458 | ), |
| 459 | 'wc-square-base-location', |
| 460 | array( |
| 461 | 'notice_class' => 'notice-error', |
| 462 | ) |
| 463 | ); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Adds a notice if no refresh token has been cached. |
| 469 | * |
| 470 | * @since 2.0.5 |
| 471 | */ |
| 472 | protected function add_missing_refresh_token_notice() { |
| 473 | if ( $this->get_settings_handler()->is_sandbox() ) { |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | $refresh_token = ''; |
| 478 | $settings_handler = $this->get_settings_handler(); |
| 479 | |
| 480 | if ( method_exists( $settings_handler, 'get_access_token' ) ) { |
| 481 | $access_token = $settings_handler->get_access_token(); |
| 482 | if ( empty( $access_token ) ) { |
| 483 | // We are already in a disconnected state, don't show the warning. |
| 484 | return; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | if ( method_exists( $settings_handler, 'get_refresh_token' ) ) { |
| 489 | $refresh_token = $settings_handler->get_refresh_token(); |
| 490 | } |
| 491 | |
| 492 | if ( empty( $refresh_token ) ) { |
| 493 | $this->get_admin_notice_handler()->add_admin_notice( |
| 494 | sprintf( |
| 495 | /* translators: Placeholders: %1$s - <strong> tag, %2$s - </strong> tag, %3$s - <a> tag, %4$s - </a> tag */ |
| 496 | __( '%1$sWooCommerce Square:%2$s Automatic refreshing of the connection to Square is inactive. Please disconnect and reconnect to resolve.', 'woocommerce-square' ), |
| 497 | '<strong>', |
| 498 | '</strong>' |
| 499 | ), |
| 500 | 'wc-square-missing-refresh-token', |
| 501 | array( |
| 502 | 'dismissible' => false, |
| 503 | 'notice_class' => 'notice-error', |
| 504 | ) |
| 505 | ); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | |
| 510 | /** |
| 511 | * Adds a tax-inclusive admin warning to product pages. |
| 512 | * |
| 513 | * @since 2.0.0 |
| 514 | */ |
| 515 | protected function add_tax_inclusive_pricing_notice() { |
| 516 | global $typenow; |
| 517 | |
| 518 | // only show on product edit pages when configured that prices include tax |
| 519 | if ( 'product' === $typenow && isset( $_GET['action'], $_GET['post'] ) && 'edit' === $_GET['action'] && wc_prices_include_tax() && $this->get_settings_handler()->is_product_sync_enabled() ) { |
| 520 | |
| 521 | $product = wc_get_product( (int) $_GET['post'] ); |
| 522 | |
| 523 | // only show for products configured as taxable and sync with Square |
| 524 | if ( $product instanceof \WC_Product && $product->is_taxable() && Product::is_synced_with_square( $product ) ) { |
| 525 | |
| 526 | $this->get_admin_notice_handler()->add_admin_notice( |
| 527 | sprintf( |
| 528 | __( '%1$sWooCommerce Square:%2$s Product prices are entered inclusive of tax, but Square does not support syncing tax-inclusive prices. Please make sure your Square tax rates match your WooCommerce tax rates.', 'woocommerce-square' ), |
| 529 | '<strong>', |
| 530 | '</strong>' |
| 531 | ), |
| 532 | 'wc-square-tax-inclusive', |
| 533 | array( |
| 534 | 'notice_class' => 'notice-warning', |
| 535 | ) |
| 536 | ); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | |
| 542 | /** |
| 543 | * Adds admin notices for currency issues. |
| 544 | * |
| 545 | * @since 2.0.0 |
| 546 | */ |
| 547 | protected function add_currency_admin_notices() { |
| 548 | |
| 549 | parent::add_currency_admin_notices(); |
| 550 | |
| 551 | if ( isset( $_GET['page'] ) && 'wc-settings' === $_GET['page'] && $this->get_settings_handler()->is_connected() ) { |
| 552 | |
| 553 | foreach ( $this->get_settings_handler()->get_locations() as $location ) { |
| 554 | |
| 555 | if ( $this->get_settings_handler()->get_location_id() === $location->getId() && get_woocommerce_currency() !== $location->getCurrency() ) { |
| 556 | |
| 557 | $this->get_admin_notice_handler()->add_admin_notice( |
| 558 | sprintf( |
| 559 | __( 'Heads up! Your store currency is %1$s but your configured Square business location currency is %2$s, so payments cannot be processed. Please %3$schoose a different business location%4$s or change your %5$sshop currency%6$s.', 'woocommerce-square' ), |
| 560 | '<strong>' . esc_html( get_woocommerce_currency() ) . '</strong>', |
| 561 | '<strong>' . esc_html( $location->getCurrency() ) . '</strong>', |
| 562 | '<a href="' . esc_url( $this->get_settings_url() ) . '">', |
| 563 | '</a>', |
| 564 | '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings' ) ) . '">', |
| 565 | '</a>' |
| 566 | ), |
| 567 | 'wc-square-currency-mismatch', |
| 568 | array( |
| 569 | 'notice_class' => 'notice-error', |
| 570 | ) |
| 571 | ); |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | |
| 578 | /** Helper methods ************************************************************************************************/ |
| 579 | |
| 580 | |
| 581 | /** |
| 582 | * Returns an idempotency key to be used in Square API requests. |
| 583 | * |
| 584 | * @since 2.0.0 |
| 585 | * |
| 586 | * @param string $key_input |
| 587 | * @param bool $append_key_input |
| 588 | * @return string |
| 589 | */ |
| 590 | public function get_idempotency_key( $key_input = '', $append_key_input = true ) { |
| 591 | |
| 592 | if ( '' === $key_input ) { |
| 593 | $key_input = uniqid( '', false ); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Filters an idempotency key. |
| 598 | * |
| 599 | * @since 2.0.0 |
| 600 | * |
| 601 | * @param string $key_input |
| 602 | */ |
| 603 | return apply_filters( 'wc_square_idempotency_key', sha1( get_option( 'siteurl' ) . $key_input ) . ( $append_key_input ? ':' . $key_input : '' ) ); |
| 604 | } |
| 605 | |
| 606 | |
| 607 | /** Conditional methods *******************************************************************************************/ |
| 608 | |
| 609 | |
| 610 | /** |
| 611 | * Determines if viewing the plugin settings. |
| 612 | * |
| 613 | * @since 2.0.0 |
| 614 | * |
| 615 | * @return bool |
| 616 | */ |
| 617 | public function is_plugin_settings() { |
| 618 | |
| 619 | return parent::is_plugin_settings() || ( isset( $_GET['page'], $_GET['tab'] ) && 'wc-settings' === $_GET['page'] && self::PLUGIN_ID === $_GET['tab'] ); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Determines if viewing the gateway settings. |
| 624 | * |
| 625 | * @since 2.3.0 |
| 626 | * |
| 627 | * @return bool |
| 628 | */ |
| 629 | public function is_gateway_settings() { |
| 630 | |
| 631 | return isset( $_GET['page'], $_GET['tab'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 'checkout' === $_GET['tab'] && self::GATEWAY_ID === $_GET['section']; |
| 632 | } |
| 633 | |
| 634 | |
| 635 | /** Getter methods ************************************************************************************************/ |
| 636 | |
| 637 | |
| 638 | /** |
| 639 | * Gets the main Square API handler. |
| 640 | * |
| 641 | * @since 2.0.0 |
| 642 | * |
| 643 | * @param string|null $access_token API access token |
| 644 | * @return API |
| 645 | */ |
| 646 | public function get_api( $access_token = null, $is_sandbox = null ) { |
| 647 | |
| 648 | if ( ! $access_token ) { |
| 649 | $access_token = $this->get_settings_handler()->get_access_token(); |
| 650 | } |
| 651 | |
| 652 | if ( is_null( $is_sandbox ) ) { |
| 653 | $is_sandbox = $this->get_settings_handler()->is_sandbox(); |
| 654 | } |
| 655 | |
| 656 | return new API( $access_token, $is_sandbox ); |
| 657 | } |
| 658 | |
| 659 | |
| 660 | /** |
| 661 | * Gets the connection handler. |
| 662 | * |
| 663 | * @since 2.0.0 |
| 664 | * |
| 665 | * @return Handlers\Connection |
| 666 | */ |
| 667 | public function get_connection_handler() { |
| 668 | |
| 669 | return $this->connection_handler; |
| 670 | } |
| 671 | |
| 672 | |
| 673 | /** |
| 674 | * Gets the sync handler instance. |
| 675 | * |
| 676 | * @since 2.0.0 |
| 677 | * |
| 678 | * @return Sync |
| 679 | */ |
| 680 | public function get_sync_handler() { |
| 681 | |
| 682 | return $this->sync_handler; |
| 683 | } |
| 684 | |
| 685 | |
| 686 | /** |
| 687 | * Gets the background sync handler instance. |
| 688 | * |
| 689 | * @since 2.0.0 |
| 690 | * |
| 691 | * @return Background_Job |
| 692 | */ |
| 693 | public function get_background_job_handler() { |
| 694 | |
| 695 | return $this->background_job_handler; |
| 696 | } |
| 697 | |
| 698 | |
| 699 | /** |
| 700 | * Gets the settings handler instance. |
| 701 | * |
| 702 | * @since 2.0.0 |
| 703 | * |
| 704 | * @return Settings |
| 705 | */ |
| 706 | public function get_settings_handler() { |
| 707 | |
| 708 | return $this->settings_handler; |
| 709 | } |
| 710 | |
| 711 | |
| 712 | |
| 713 | /** |
| 714 | * Gets the admin handler instance. |
| 715 | * |
| 716 | * @since 2.0.0 |
| 717 | * |
| 718 | * @return Admin|null |
| 719 | */ |
| 720 | public function get_admin_handler() { |
| 721 | |
| 722 | // throw a notice if calling before admin_init |
| 723 | Square_Helper::maybe_doing_it_early( 'admin_init', __METHOD__, '2.0.0' ); |
| 724 | |
| 725 | return $this->admin_handler; |
| 726 | } |
| 727 | |
| 728 | |
| 729 | /** |
| 730 | * Gets the email handler instance. |
| 731 | * |
| 732 | * @since 2.0.0 |
| 733 | * |
| 734 | * @return Email |
| 735 | */ |
| 736 | public function get_email_handler() { |
| 737 | |
| 738 | return $this->email_handler; |
| 739 | } |
| 740 | |
| 741 | |
| 742 | /** |
| 743 | * Gets the order handler instance. |
| 744 | * |
| 745 | * @since 2.0.0 |
| 746 | * |
| 747 | * @return Order |
| 748 | */ |
| 749 | public function get_order_handler() { |
| 750 | |
| 751 | return $this->order_handler; |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Get the products handler instance/ |
| 756 | * |
| 757 | * @since 2.0.8 |
| 758 | * |
| 759 | * @return Products |
| 760 | */ |
| 761 | public function get_products_handler() { |
| 762 | return $this->products_handler; |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Gets the plugin name. |
| 767 | * |
| 768 | * @since 2.0.0 |
| 769 | * |
| 770 | * @return string |
| 771 | */ |
| 772 | public function get_plugin_name() { |
| 773 | |
| 774 | return __( 'WooCommerce Square', 'woocommerce-square' ); |
| 775 | } |
| 776 | |
| 777 | |
| 778 | /** |
| 779 | * Gets the settings URL. |
| 780 | * |
| 781 | * @since 2.0.0 |
| 782 | * |
| 783 | * @param null|string $gateway_id gateway ID |
| 784 | * @return string |
| 785 | */ |
| 786 | public function get_settings_url( $gateway_id = null ) { |
| 787 | |
| 788 | $params = array( |
| 789 | 'page' => 'wc-settings', |
| 790 | 'tab' => self::PLUGIN_ID, |
| 791 | ); |
| 792 | |
| 793 | return add_query_arg( $params, admin_url( 'admin.php' ) ); |
| 794 | } |
| 795 | |
| 796 | |
| 797 | /** |
| 798 | * Gets the sale page URL. |
| 799 | * |
| 800 | * @since 2.0.0 |
| 801 | * |
| 802 | * @return string |
| 803 | */ |
| 804 | public function get_sales_page_url() { |
| 805 | |
| 806 | return 'https://woocommerce.com/products/square/'; |
| 807 | } |
| 808 | |
| 809 | |
| 810 | /** |
| 811 | * Gets the documentation URL. |
| 812 | * |
| 813 | * @since 2.0.0 |
| 814 | * |
| 815 | * @return string |
| 816 | */ |
| 817 | public function get_documentation_url() { |
| 818 | |
| 819 | return 'https://docs.woocommerce.com/document/woocommerce-square/'; |
| 820 | } |
| 821 | |
| 822 | |
| 823 | /** |
| 824 | * Gets the plugin reviews page URL. |
| 825 | * |
| 826 | * Used for the 'Reviews' plugin action and review prompts. |
| 827 | * |
| 828 | * @since 2.1.7 |
| 829 | * |
| 830 | * @return string |
| 831 | */ |
| 832 | public function get_reviews_url() { |
| 833 | |
| 834 | return $this->get_sales_page_url() ? $this->get_sales_page_url() . '#comments' : ''; |
| 835 | } |
| 836 | |
| 837 | |
| 838 | /** |
| 839 | * Gets the support URL. |
| 840 | * |
| 841 | * @since 2.0.0 |
| 842 | * |
| 843 | * @return string |
| 844 | */ |
| 845 | public function get_support_url() { |
| 846 | |
| 847 | return 'https://woocommerce.com/my-account/create-a-ticket/?select=1770503'; |
| 848 | } |
| 849 | |
| 850 | |
| 851 | /** |
| 852 | * Gets __DIR__. |
| 853 | * |
| 854 | * @since 2.0.0 |
| 855 | * |
| 856 | * @return string |
| 857 | */ |
| 858 | protected function get_file() { |
| 859 | |
| 860 | return __DIR__; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | /** |
| 865 | * Gets the singleton instance of the plugin. |
| 866 | * |
| 867 | * @since 2.0.0 |
| 868 | * |
| 869 | * @return Plugin |
| 870 | */ |
| 871 | public static function instance() { |
| 872 | |
| 873 | if ( null === self::$instance ) { |
| 874 | self::$instance = new self(); |
| 875 | } |
| 876 | |
| 877 | return self::$instance; |
| 878 | } |
| 879 | |
| 880 | |
| 881 | } |
| 882 |