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 SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework; |
| 29 | use WooCommerce\Square\Handlers\Background_Job; |
| 30 | use WooCommerce\Square\Handlers\Email; |
| 31 | use WooCommerce\Square\Handlers\Order; |
| 32 | use WooCommerce\Square\Handlers\Product; |
| 33 | use WooCommerce\Square\Handlers\Sync; |
| 34 | use WooCommerce\Square\Handlers\Products; |
| 35 | |
| 36 | /** |
| 37 | * The main plugin class. |
| 38 | * |
| 39 | * @since 2.0.0 |
| 40 | */ |
| 41 | class Plugin extends Framework\SV_WC_Payment_Gateway_Plugin { |
| 42 | |
| 43 | |
| 44 | /** plugin version number */ |
| 45 | const VERSION = '2.8.0'; |
| 46 | |
| 47 | /** plugin ID */ |
| 48 | const PLUGIN_ID = 'square'; |
| 49 | |
| 50 | /** string gateway ID */ |
| 51 | const GATEWAY_ID = 'square_credit_card'; |
| 52 | |
| 53 | |
| 54 | /** @var Plugin plugin instance */ |
| 55 | protected static $instance; |
| 56 | |
| 57 | /** @var Settings settings handler instance */ |
| 58 | private $settings_handler; |
| 59 | |
| 60 | /** @var Handlers\Connection connection handler instance */ |
| 61 | private $connection_handler; |
| 62 | |
| 63 | /** @var Admin admin handler instance */ |
| 64 | private $admin_handler; |
| 65 | |
| 66 | /** @var Sync sync handler instance */ |
| 67 | private $sync_handler; |
| 68 | |
| 69 | /** @var Background_Job background handler instance */ |
| 70 | private $background_job_handler; |
| 71 | |
| 72 | /** @var AJAX handler instance */ |
| 73 | private $ajax_handler; |
| 74 | |
| 75 | /** @var Email emails handler */ |
| 76 | private $email_handler; |
| 77 | |
| 78 | /** @var Order orders handler */ |
| 79 | private $order_handler; |
| 80 | |
| 81 | /** @var Products products handler */ |
| 82 | private $products_handler; |
| 83 | |
| 84 | /** |
| 85 | * Constructs the plugin. |
| 86 | * |
| 87 | * @since 2.0.0 |
| 88 | */ |
| 89 | public function __construct() { |
| 90 | |
| 91 | parent::__construct( |
| 92 | self::PLUGIN_ID, |
| 93 | self::VERSION, |
| 94 | array( |
| 95 | 'text_domain' => 'woocommerce-square', |
| 96 | 'gateways' => array( self::GATEWAY_ID => Gateway::class ), |
| 97 | 'require_ssl' => true, |
| 98 | 'supports' => array( |
| 99 | self::FEATURE_CAPTURE_CHARGE, |
| 100 | self::FEATURE_CUSTOMER_ID, |
| 101 | self::FEATURE_MY_PAYMENT_METHODS, |
| 102 | ), |
| 103 | 'dependencies' => array( |
| 104 | 'php_extensions' => array( 'curl', 'json', 'mbstring' ), |
| 105 | ), |
| 106 | ) |
| 107 | ); |
| 108 | |
| 109 | $this->includes(); |
| 110 | |
| 111 | /** |
| 112 | * Fires upon plugin loaded (legacy hook). |
| 113 | * |
| 114 | * @since 1.0.0 |
| 115 | */ |
| 116 | do_action( 'wc_square_loaded' ); |
| 117 | |
| 118 | add_action( 'woocommerce_register_taxonomy', array( $this, 'init_taxonomies' ) ); |
| 119 | |
| 120 | add_filter( 'woocommerce_locate_template', array( $this, 'locate_template' ), 20, 3 ); |
| 121 | add_filter( 'woocommerce_locate_core_template', array( $this, 'locate_template' ), 20, 3 ); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Includes required classes. |
| 127 | * |
| 128 | * @since 2.0.0 |
| 129 | */ |
| 130 | private function includes() { |
| 131 | |
| 132 | $this->connection_handler = new Handlers\Connection( $this ); |
| 133 | |
| 134 | $this->sync_handler = new Sync( $this ); |
| 135 | |
| 136 | // background export must be loaded all the time, because otherwise background jobs simply won't work |
| 137 | require_once $this->get_framework_path() . '/utilities/class-sv-wp-async-request.php'; |
| 138 | require_once $this->get_framework_path() . '/utilities/class-sv-wp-background-job-handler.php'; |
| 139 | require_once $this->get_framework_path() . '/utilities/class-sv-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_' . $this->get_id() . '_api_request_performed' ) ) { |
| 161 | add_action( 'wc_' . $this->get_id() . '_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 || Framework\SV_WC_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 | parent::add_admin_notices(); |
| 282 | |
| 283 | // show any one-off messages |
| 284 | $this->get_message_handler()->show_messages(); |
| 285 | |
| 286 | // display a notice if the auto-refresh failed |
| 287 | if ( get_option( 'wc_' . $this->get_id() . '_refresh_failed', false ) ) { |
| 288 | |
| 289 | $message = sprintf( |
| 290 | __( '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' ), |
| 291 | '<a href="' . esc_url( $this->get_settings_url() ) . '">', |
| 292 | '</a>' |
| 293 | ); |
| 294 | |
| 295 | $this->get_admin_notice_handler()->add_admin_notice( |
| 296 | $message, |
| 297 | 'refresh-failed', |
| 298 | array( |
| 299 | 'dismissible' => false, |
| 300 | 'notice_class' => 'notice-warning', |
| 301 | ) |
| 302 | ); |
| 303 | } |
| 304 | |
| 305 | if ( $this->get_settings_handler()->is_connected() ) { |
| 306 | |
| 307 | $message = '<strong>' . __( 'You are connected to Square!', 'woocommerce-square' ) . '</strong>'; |
| 308 | |
| 309 | // prompt to set a location if not set |
| 310 | if ( ! $this->get_settings_handler()->get_location_id() ) { |
| 311 | |
| 312 | if ( $this->is_plugin_settings() ) { |
| 313 | |
| 314 | $instruction = __( 'To get started, set your business location.', 'woocommerce-square' ); |
| 315 | |
| 316 | } else { |
| 317 | |
| 318 | $instruction = sprintf( |
| 319 | /* translators: Placeholders: %1$s - <a> tag, %2$s - </a> tag */ |
| 320 | __( 'Visit the %1$splugin settings%2$s to set your business location.', 'woocommerce-square' ), |
| 321 | '<a href="' . esc_url( $this->get_settings_url() ) . '">', |
| 322 | '</a>' |
| 323 | ); |
| 324 | } |
| 325 | |
| 326 | $this->get_admin_notice_handler()->add_admin_notice( $message . ' ' . $instruction, 'set-location' ); |
| 327 | |
| 328 | } elseif ( ! $this->get_sync_handler()->get_last_synced_at() && $this->get_settings_handler()->is_product_sync_enabled() ) { |
| 329 | |
| 330 | $message = __( 'You are ready to sync products!', 'woocommerce-square' ); |
| 331 | |
| 332 | if ( ! empty( Product::get_products_synced_with_square() ) ) { |
| 333 | |
| 334 | $instruction = sprintf( |
| 335 | /* translators: Placeholders: %1$s - <strong> tag, %2$s - product count, %3$s - </strong> tag, %4$s - <a> tag, %5$s - </a> tag */ |
| 336 | __( '%1$s%2$d products%3$s are marked "sync with Square". %4$sStart a new sync now »%5$s', 'woocommerce-square' ), |
| 337 | '<strong>', |
| 338 | count( Product::get_products_synced_with_square() ), |
| 339 | '</strong>', |
| 340 | '<a href="' . esc_url( add_query_arg( 'section', 'update', $this->get_settings_url() ) ) . '">', |
| 341 | '</a>' |
| 342 | ); |
| 343 | |
| 344 | } else { |
| 345 | |
| 346 | $instruction = sprintf( |
| 347 | /* translators: Placeholders: %1$s - <strong> tag, %2$s - </strong> tag, %3$s - <a> tag, %4$s - </a> tag */ |
| 348 | __( '%1$sNo products%2$s are marked "sync with Square". %3$sUpdate your products to sync data »%4$s', 'woocommerce-square' ), |
| 349 | '<strong>', |
| 350 | '</strong>', |
| 351 | '<a href="' . esc_url( admin_url( 'edit.php?post_type=product' ) ) . '">', |
| 352 | '</a>' |
| 353 | ); |
| 354 | } |
| 355 | |
| 356 | $this->get_admin_notice_handler()->add_admin_notice( $message . ' ' . $instruction, 'set-location' ); |
| 357 | } |
| 358 | |
| 359 | // a notice for when WC stock handling is globally disabled |
| 360 | if ( 'yes' !== get_option( 'woocommerce_manage_stock' ) && $this->get_settings_handler()->is_inventory_sync_enabled() ) { |
| 361 | |
| 362 | $message = sprintf( |
| 363 | __( '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' ), |
| 364 | '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=products§ion=inventory' ) ) . '">', |
| 365 | '</a>' |
| 366 | ); |
| 367 | |
| 368 | $this->get_admin_notice_handler()->add_admin_notice( |
| 369 | $message, |
| 370 | 'enable-wc-sync', |
| 371 | array( |
| 372 | 'notice_class' => 'notice-warning', |
| 373 | ) |
| 374 | ); |
| 375 | } |
| 376 | } else { |
| 377 | |
| 378 | if ( $this->is_plugin_settings() ) { |
| 379 | |
| 380 | $instruction = __( 'To get started, connect with Square.', 'woocommerce-square' ); |
| 381 | |
| 382 | } else { |
| 383 | |
| 384 | $instruction = sprintf( |
| 385 | /* translators: Placeholders: %1$s - <a> tag, %2$s - </a> tag */ |
| 386 | __( 'To get started, %1$sconnect with Square »%2$s', 'woocommerce-square' ), |
| 387 | '<a href="' . esc_url( $this->get_settings_url() ) . '">', |
| 388 | '</a>' |
| 389 | ); |
| 390 | } |
| 391 | |
| 392 | $message = sprintf( |
| 393 | /* translators: Placeholders: %1$s - plugin name */ |
| 394 | __( 'Thanks for installing %1$s!', 'woocommerce-square' ), |
| 395 | esc_html( $this->get_plugin_name() ) |
| 396 | ); |
| 397 | |
| 398 | $this->get_admin_notice_handler()->add_admin_notice( $message . ' ' . $instruction, 'connect' ); |
| 399 | } |
| 400 | |
| 401 | // add a notice for out-of-bounds base locations |
| 402 | $this->add_base_location_admin_notice(); |
| 403 | |
| 404 | // add a notice when background processing is not supported |
| 405 | $this->add_background_processing_notice(); |
| 406 | |
| 407 | // add a notice when no refresh token is available |
| 408 | $this->add_missing_refresh_token_notice(); |
| 409 | |
| 410 | // add a tax-inclusive warning to product pages |
| 411 | $this->add_tax_inclusive_pricing_notice(); |
| 412 | |
| 413 | if ( get_option( 'wc_square_updated_to_2_0_0' ) ) { |
| 414 | |
| 415 | $this->get_admin_notice_handler()->add_admin_notice( |
| 416 | sprintf( |
| 417 | /* 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*/ |
| 418 | 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' ), |
| 419 | '<strong>' . esc_html( $this->get_plugin_name() ) . '</strong>', |
| 420 | $this->get_version(), |
| 421 | '<a href="' . esc_url( $this->get_settings_url() ) . '">', |
| 422 | '</a>', |
| 423 | '<a href="' . esc_url( $this->get_documentation_url() ) . '">', |
| 424 | '</a>' |
| 425 | ), |
| 426 | 'updated-to-v2', |
| 427 | array( 'notice_class' => 'notice-warning' ) |
| 428 | ); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /** |
| 434 | * Adds a notice for out-of-bounds base locations. |
| 435 | * |
| 436 | * @since 2.0.0 |
| 437 | */ |
| 438 | protected function add_base_location_admin_notice() { |
| 439 | |
| 440 | $accepted_countries = array( |
| 441 | 'US', |
| 442 | 'CA', |
| 443 | 'GB', |
| 444 | 'AU', |
| 445 | 'JP', |
| 446 | 'IE', |
| 447 | 'FR', |
| 448 | 'ES', |
| 449 | ); |
| 450 | |
| 451 | $base_location = wc_get_base_location(); |
| 452 | |
| 453 | if ( isset( $base_location['country'] ) && ! in_array( $base_location['country'], $accepted_countries, true ) ) { |
| 454 | |
| 455 | $this->get_admin_notice_handler()->add_admin_notice( |
| 456 | sprintf( |
| 457 | /* 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 */ |
| 458 | __( '%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' ), |
| 459 | '<strong>', |
| 460 | '</strong>', |
| 461 | esc_html( $base_location['country'] ), |
| 462 | esc_html( Framework\SV_WC_Helper::list_array_items( $accepted_countries ) ) |
| 463 | ), |
| 464 | 'wc-square-base-location', |
| 465 | array( |
| 466 | 'notice_class' => 'notice-error', |
| 467 | ) |
| 468 | ); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | |
| 473 | /** |
| 474 | * Adds a notice when background processing is not supported. |
| 475 | * |
| 476 | * @since 2.0.0 |
| 477 | */ |
| 478 | protected function add_background_processing_notice() { |
| 479 | |
| 480 | if ( $this->get_settings_handler()->is_product_sync_enabled() && ! $this->get_background_job_handler()->test_connection() ) { |
| 481 | |
| 482 | $this->get_admin_notice_handler()->add_admin_notice( |
| 483 | sprintf( |
| 484 | /* translators: Placeholders: %1$s - <strong> tag, %2$s - </strong> tag, %3$s - <a> tag, %4$s - </a> tag */ |
| 485 | __( '%1$sWooCommerce Square:%2$s It looks like your site does not support background processing, which means large numbers of products may not sync successfully with Square. %3$sRead more here%4$s on how to resolve this.', 'woocommerce-square' ), |
| 486 | '<strong>', |
| 487 | '</strong>', |
| 488 | '<a href="https://docs.woocommerce.com/document/woocommerce-square/#sync-issues" target="_blank">', |
| 489 | '</a>' |
| 490 | ), |
| 491 | 'wc-square-background-processing', |
| 492 | array( |
| 493 | 'notice_class' => 'notice-warning', |
| 494 | ) |
| 495 | ); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Adds a notice if no refresh token has been cached. |
| 501 | * |
| 502 | * @since 2.0.5 |
| 503 | */ |
| 504 | protected function add_missing_refresh_token_notice() { |
| 505 | if ( $this->get_settings_handler()->is_sandbox() ) { |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | $refresh_token = ''; |
| 510 | $settings_handler = $this->get_settings_handler(); |
| 511 | |
| 512 | if ( method_exists( $settings_handler, 'get_access_token' ) ) { |
| 513 | $access_token = $settings_handler->get_access_token(); |
| 514 | if ( empty( $access_token ) ) { |
| 515 | // We are already in a disconnected state, don't show the warning. |
| 516 | return; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | if ( method_exists( $settings_handler, 'get_refresh_token' ) ) { |
| 521 | $refresh_token = $settings_handler->get_refresh_token(); |
| 522 | } |
| 523 | |
| 524 | if ( empty( $refresh_token ) ) { |
| 525 | $this->get_admin_notice_handler()->add_admin_notice( |
| 526 | sprintf( |
| 527 | /* translators: Placeholders: %1$s - <strong> tag, %2$s - </strong> tag, %3$s - <a> tag, %4$s - </a> tag */ |
| 528 | __( '%1$sWooCommerce Square:%2$s Automatic refreshing of the connection to Square is inactive. Please disconnect and reconnect to resolve.', 'woocommerce-square' ), |
| 529 | '<strong>', |
| 530 | '</strong>' |
| 531 | ), |
| 532 | 'wc-square-missing-refresh-token', |
| 533 | array( |
| 534 | 'dismissible' => false, |
| 535 | 'notice_class' => 'notice-error', |
| 536 | ) |
| 537 | ); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | |
| 542 | /** |
| 543 | * Adds a tax-inclusive admin warning to product pages. |
| 544 | * |
| 545 | * @since 2.0.0 |
| 546 | */ |
| 547 | protected function add_tax_inclusive_pricing_notice() { |
| 548 | global $typenow; |
| 549 | |
| 550 | // only show on product edit pages when configured that prices include tax |
| 551 | if ( 'product' === $typenow && isset( $_GET['action'], $_GET['post'] ) && 'edit' === $_GET['action'] && wc_prices_include_tax() && $this->get_settings_handler()->is_product_sync_enabled() ) { |
| 552 | |
| 553 | $product = wc_get_product( (int) $_GET['post'] ); |
| 554 | |
| 555 | // only show for products configured as taxable and sync with Square |
| 556 | if ( $product instanceof \WC_Product && $product->is_taxable() && Product::is_synced_with_square( $product ) ) { |
| 557 | |
| 558 | $this->get_admin_notice_handler()->add_admin_notice( |
| 559 | sprintf( |
| 560 | __( '%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' ), |
| 561 | '<strong>', |
| 562 | '</strong>' |
| 563 | ), |
| 564 | 'wc-square-tax-inclusive', |
| 565 | array( |
| 566 | 'notice_class' => 'notice-warning', |
| 567 | ) |
| 568 | ); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | |
| 574 | /** |
| 575 | * Adds admin notices for currency issues. |
| 576 | * |
| 577 | * @since 2.0.0 |
| 578 | */ |
| 579 | protected function add_currency_admin_notices() { |
| 580 | |
| 581 | parent::add_currency_admin_notices(); |
| 582 | |
| 583 | if ( isset( $_GET['page'] ) && 'wc-settings' === $_GET['page'] && $this->get_settings_handler()->is_connected() ) { |
| 584 | |
| 585 | foreach ( $this->get_settings_handler()->get_locations() as $location ) { |
| 586 | |
| 587 | if ( $this->get_settings_handler()->get_location_id() === $location->getId() && get_woocommerce_currency() !== $location->getCurrency() ) { |
| 588 | |
| 589 | $this->get_admin_notice_handler()->add_admin_notice( |
| 590 | sprintf( |
| 591 | __( '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' ), |
| 592 | '<strong>' . esc_html( get_woocommerce_currency() ) . '</strong>', |
| 593 | '<strong>' . esc_html( $location->getCurrency() ) . '</strong>', |
| 594 | '<a href="' . esc_url( $this->get_settings_url() ) . '">', |
| 595 | '</a>', |
| 596 | '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings' ) ) . '">', |
| 597 | '</a>' |
| 598 | ), |
| 599 | 'wc-square-currency-mismatch', |
| 600 | array( |
| 601 | 'notice_class' => 'notice-error', |
| 602 | ) |
| 603 | ); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | |
| 610 | /** Helper methods ************************************************************************************************/ |
| 611 | |
| 612 | |
| 613 | /** |
| 614 | * Returns an idempotency key to be used in Square API requests. |
| 615 | * |
| 616 | * @since 2.0.0 |
| 617 | * |
| 618 | * @param string $key_input |
| 619 | * @param bool $append_key_input |
| 620 | * @return string |
| 621 | */ |
| 622 | public function get_idempotency_key( $key_input = '', $append_key_input = true ) { |
| 623 | |
| 624 | if ( '' === $key_input ) { |
| 625 | $key_input = uniqid( '', false ); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Filters an idempotency key. |
| 630 | * |
| 631 | * @since 2.0.0 |
| 632 | * |
| 633 | * @param string $key_input |
| 634 | */ |
| 635 | return apply_filters( 'wc_square_idempotency_key', sha1( get_option( 'siteurl' ) . $key_input ) . ( $append_key_input ? ':' . $key_input : '' ) ); |
| 636 | } |
| 637 | |
| 638 | |
| 639 | /** Conditional methods *******************************************************************************************/ |
| 640 | |
| 641 | |
| 642 | /** |
| 643 | * Determines if viewing the plugin settings. |
| 644 | * |
| 645 | * @since 2.0.0 |
| 646 | * |
| 647 | * @return bool |
| 648 | */ |
| 649 | public function is_plugin_settings() { |
| 650 | |
| 651 | return parent::is_plugin_settings() || ( isset( $_GET['page'], $_GET['tab'] ) && 'wc-settings' === $_GET['page'] && self::PLUGIN_ID === $_GET['tab'] ); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Determines if viewing the gateway settings. |
| 656 | * |
| 657 | * @since 2.3.0 |
| 658 | * |
| 659 | * @return bool |
| 660 | */ |
| 661 | public function is_gateway_settings() { |
| 662 | |
| 663 | return isset( $_GET['page'], $_GET['tab'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 'checkout' === $_GET['tab'] && self::GATEWAY_ID === $_GET['section']; |
| 664 | } |
| 665 | |
| 666 | |
| 667 | /** Getter methods ************************************************************************************************/ |
| 668 | |
| 669 | |
| 670 | /** |
| 671 | * Gets the main Square API handler. |
| 672 | * |
| 673 | * @since 2.0.0 |
| 674 | * |
| 675 | * @param string|null $access_token API access token |
| 676 | * @return API |
| 677 | */ |
| 678 | public function get_api( $access_token = null, $is_sandbox = null ) { |
| 679 | |
| 680 | if ( ! $access_token ) { |
| 681 | $access_token = $this->get_settings_handler()->get_access_token(); |
| 682 | } |
| 683 | |
| 684 | if ( is_null( $is_sandbox ) ) { |
| 685 | $is_sandbox = $this->get_settings_handler()->is_sandbox(); |
| 686 | } |
| 687 | |
| 688 | return new API( $access_token, $is_sandbox ); |
| 689 | } |
| 690 | |
| 691 | |
| 692 | /** |
| 693 | * Gets the connection handler. |
| 694 | * |
| 695 | * @since 2.0.0 |
| 696 | * |
| 697 | * @return Handlers\Connection |
| 698 | */ |
| 699 | public function get_connection_handler() { |
| 700 | |
| 701 | return $this->connection_handler; |
| 702 | } |
| 703 | |
| 704 | |
| 705 | /** |
| 706 | * Gets the sync handler instance. |
| 707 | * |
| 708 | * @since 2.0.0 |
| 709 | * |
| 710 | * @return Sync |
| 711 | */ |
| 712 | public function get_sync_handler() { |
| 713 | |
| 714 | return $this->sync_handler; |
| 715 | } |
| 716 | |
| 717 | |
| 718 | /** |
| 719 | * Gets the background sync handler instance. |
| 720 | * |
| 721 | * @since 2.0.0 |
| 722 | * |
| 723 | * @return Background_Job |
| 724 | */ |
| 725 | public function get_background_job_handler() { |
| 726 | |
| 727 | return $this->background_job_handler; |
| 728 | } |
| 729 | |
| 730 | |
| 731 | /** |
| 732 | * Gets the settings handler instance. |
| 733 | * |
| 734 | * @since 2.0.0 |
| 735 | * |
| 736 | * @return Settings |
| 737 | */ |
| 738 | public function get_settings_handler() { |
| 739 | |
| 740 | return $this->settings_handler; |
| 741 | } |
| 742 | |
| 743 | |
| 744 | /** |
| 745 | * Gets the admin handler instance. |
| 746 | * |
| 747 | * @since 2.0.0 |
| 748 | * |
| 749 | * @return Admin|null |
| 750 | */ |
| 751 | public function get_admin_handler() { |
| 752 | |
| 753 | // throw a notice if calling before admin_init |
| 754 | Framework\SV_WC_Helper::maybe_doing_it_early( 'admin_init', __METHOD__, '2.0.0' ); |
| 755 | |
| 756 | return $this->admin_handler; |
| 757 | } |
| 758 | |
| 759 | |
| 760 | /** |
| 761 | * Gets the email handler instance. |
| 762 | * |
| 763 | * @since 2.0.0 |
| 764 | * |
| 765 | * @return Email |
| 766 | */ |
| 767 | public function get_email_handler() { |
| 768 | |
| 769 | return $this->email_handler; |
| 770 | } |
| 771 | |
| 772 | |
| 773 | /** |
| 774 | * Gets the order handler instance. |
| 775 | * |
| 776 | * @since 2.0.0 |
| 777 | * |
| 778 | * @return Order |
| 779 | */ |
| 780 | public function get_order_handler() { |
| 781 | |
| 782 | return $this->order_handler; |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * Get the products handler instance/ |
| 787 | * |
| 788 | * @since 2.0.8 |
| 789 | * |
| 790 | * @return Products |
| 791 | */ |
| 792 | public function get_products_handler() { |
| 793 | return $this->products_handler; |
| 794 | } |
| 795 | |
| 796 | |
| 797 | /** |
| 798 | * Gets the deprecated hook details. |
| 799 | * |
| 800 | * @see Framework\SV_WC_Hook_Deprecator |
| 801 | * |
| 802 | * @since 2.0.0 |
| 803 | * |
| 804 | * @return array |
| 805 | */ |
| 806 | protected function get_deprecated_hooks() { |
| 807 | |
| 808 | // the following are filters, except when an action is explicitly mentioned |
| 809 | $v2_0_0_removed_hooks = array( |
| 810 | |
| 811 | // to filter the locale, the default WordPress filter should be used: |
| 812 | 'woocommerce_square_plugin_locale' => array( |
| 813 | 'replacement' => 'plugin_locale', |
| 814 | 'map' => true, |
| 815 | ), |
| 816 | |
| 817 | // sync square product variation properties |
| 818 | 'woocommerce_square_currency' => array(), // used when passing a WooCommerce product price into a Square ItemVariation |
| 819 | 'wc_square_sync_to_square_price' => array(), // used when passing a WooCommerce product price into a Square ItemVariation |
| 820 | 'woocommerce_square_format_price' => array(), // formats the price coming from Square |
| 821 | 'woocommerce_square_sync_from_square_description' => array(), // flag whether to add a description to created item |
| 822 | |
| 823 | // we no longer filter inventory type in v2.0.0 and timeout is handled by background job differently |
| 824 | 'woocommerce_square_inventory_type' => array(), |
| 825 | 'woocommerce_square_inventory_sync_timeout_limit' => array(), |
| 826 | 'woocommerce_square_inventory_poll_frequency' => array(), |
| 827 | |
| 828 | // Square payment |
| 829 | 'woocommerce_square_payment_form_trigger_element' => array(), |
| 830 | 'woocommerce_square_payment_order_note' => array( |
| 831 | 'replacement' => 'wc_square_payment_order_note', |
| 832 | 'map' => true, |
| 833 | ), |
| 834 | 'woocommerce_square_description' => array(), // front end payment fields description |
| 835 | |
| 836 | // most gateway properties and settings can be mapped to new filters: |
| 837 | 'woocommerce_square_api_url' => array( |
| 838 | 'replacement' => 'wc_square_api_url', |
| 839 | 'map' => true, |
| 840 | ), // filter is reinstated with name change for consistency |
| 841 | 'woocommerce_square_payment_gateway_is_available' => array( |
| 842 | 'replacement' => 'wc_gateway_square_credit_card_is_available', |
| 843 | 'map' => true, |
| 844 | ), // filter handled by SkyVerge Framework |
| 845 | 'woocommerce_square_integration_settings_args' => array( |
| 846 | 'replacement' => 'woocommerce_settings_api_form_fields_square', |
| 847 | 'map' => true, |
| 848 | ), // filters gateway settings |
| 849 | 'woocommerce_square_integration_custom_settings' => array( |
| 850 | 'replacement' => 'woocommerce_settings_tabs_square', |
| 851 | 'map' => true, |
| 852 | ), // settings action hook |
| 853 | |
| 854 | // API requests filters, these are handled differently and can't be mapped: |
| 855 | 'woocommerce_square_request_args' => array(), |
| 856 | 'woocommerce_square_request_retries' => array(), |
| 857 | |
| 858 | // transients are no longer used to handle these cache types: |
| 859 | 'woocommerce_square_business_location_cache' => array(), |
| 860 | 'woocommerce_square_item_sku_cache' => array(), |
| 861 | 'woocommerce_square_inventory_cache' => array(), |
| 862 | 'woocommerce_square_sync_processing_ids_cache' => array(), |
| 863 | 'woocommerce_square_manual_sync_processing_cache' => array(), |
| 864 | 'woocommerce_square_syncing_square_ids_cache' => array(), |
| 865 | 'woocommerce_square_syncing_wc_product_ids_cache' => array(), |
| 866 | |
| 867 | // when a bulk sync action is triggered (action hook): |
| 868 | 'woocommerce_square_bulk_syncing_square_to_wc' => array(), |
| 869 | |
| 870 | // get_posts args filter, the new implementation has different scope: |
| 871 | 'woocommerce_square_get_all_product_ids_args' => array(), |
| 872 | |
| 873 | // idempotency key |
| 874 | 'woocommerce_square_idempotency_key' => array( |
| 875 | 'replacement' => 'wc_square_idempotency_key', |
| 876 | 'map' => true, |
| 877 | ), |
| 878 | |
| 879 | ); |
| 880 | |
| 881 | // add common array data for all removed hooks in version 2.0.0 |
| 882 | foreach ( array_keys( $v2_0_0_removed_hooks ) as $hook_name ) { |
| 883 | $v2_0_0_removed_hooks[ $hook_name ]['version'] = '2.0.0'; |
| 884 | $v2_0_0_removed_hooks[ $hook_name ]['removed'] = true; |
| 885 | } |
| 886 | |
| 887 | return $v2_0_0_removed_hooks; |
| 888 | } |
| 889 | |
| 890 | |
| 891 | /** |
| 892 | * Gets the plugin name. |
| 893 | * |
| 894 | * @since 2.0.0 |
| 895 | * |
| 896 | * @return string |
| 897 | */ |
| 898 | public function get_plugin_name() { |
| 899 | |
| 900 | return __( 'WooCommerce Square', 'woocommerce-square' ); |
| 901 | } |
| 902 | |
| 903 | |
| 904 | /** |
| 905 | * Gets the settings URL. |
| 906 | * |
| 907 | * @since 2.0.0 |
| 908 | * |
| 909 | * @param null|string $gateway_id gateway ID |
| 910 | * @return string |
| 911 | */ |
| 912 | public function get_settings_url( $gateway_id = null ) { |
| 913 | |
| 914 | $params = array( |
| 915 | 'page' => 'wc-settings', |
| 916 | 'tab' => self::PLUGIN_ID, |
| 917 | ); |
| 918 | |
| 919 | return add_query_arg( $params, admin_url( 'admin.php' ) ); |
| 920 | } |
| 921 | |
| 922 | |
| 923 | /** |
| 924 | * Gets the sale page URL. |
| 925 | * |
| 926 | * @since 2.0.0 |
| 927 | * |
| 928 | * @return string |
| 929 | */ |
| 930 | public function get_sales_page_url() { |
| 931 | |
| 932 | return 'https://woocommerce.com/products/square/'; |
| 933 | } |
| 934 | |
| 935 | |
| 936 | /** |
| 937 | * Gets the documentation URL. |
| 938 | * |
| 939 | * @since 2.0.0 |
| 940 | * |
| 941 | * @return string |
| 942 | */ |
| 943 | public function get_documentation_url() { |
| 944 | |
| 945 | return 'https://docs.woocommerce.com/document/woocommerce-square/'; |
| 946 | } |
| 947 | |
| 948 | |
| 949 | /** |
| 950 | * Gets the plugin reviews page URL. |
| 951 | * |
| 952 | * Used for the 'Reviews' plugin action and review prompts. |
| 953 | * |
| 954 | * @since 2.1.7 |
| 955 | * |
| 956 | * @return string |
| 957 | */ |
| 958 | public function get_reviews_url() { |
| 959 | |
| 960 | return $this->get_sales_page_url() ? $this->get_sales_page_url() . '#comments' : ''; |
| 961 | } |
| 962 | |
| 963 | |
| 964 | /** |
| 965 | * Gets the support URL. |
| 966 | * |
| 967 | * @since 2.0.0 |
| 968 | * |
| 969 | * @return string |
| 970 | */ |
| 971 | public function get_support_url() { |
| 972 | |
| 973 | return 'https://woocommerce.com/my-account/create-a-ticket/?select=1770503'; |
| 974 | } |
| 975 | |
| 976 | |
| 977 | /** |
| 978 | * Gets __DIR__. |
| 979 | * |
| 980 | * @since 2.0.0 |
| 981 | * |
| 982 | * @return string |
| 983 | */ |
| 984 | protected function get_file() { |
| 985 | |
| 986 | return __DIR__; |
| 987 | } |
| 988 | |
| 989 | |
| 990 | /** |
| 991 | * Gets the singleton instance of the plugin. |
| 992 | * |
| 993 | * @since 2.0.0 |
| 994 | * |
| 995 | * @return Plugin |
| 996 | */ |
| 997 | public static function instance() { |
| 998 | |
| 999 | if ( null === self::$instance ) { |
| 1000 | self::$instance = new self(); |
| 1001 | } |
| 1002 | |
| 1003 | return self::$instance; |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | } |
| 1008 |