Addresses
3 years ago
Api
3 years ago
Compatibility
2 years ago
PaymentGateway
2 years ago
Utilities
3 years ago
Admin_Message_Handler.php
3 years ago
Admin_Notice_Handler.php
3 years ago
Lifecycle.php
3 years ago
Plugin.php
3 years ago
Plugin_Compatibility.php
3 years ago
Plugin_Dependencies.php
3 years ago
Square_Helper.php
3 years ago
Plugin.php
852 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Plugin Framework |
| 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 GNU General Public License v3.0 or later |
| 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@skyverge.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * @since 3.0.0 |
| 14 | * @author WooCommerce / SkyVerge |
| 15 | * @copyright Copyright (c) 2013-2019, SkyVerge, Inc. |
| 16 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 17 | * |
| 18 | * Modified by WooCommerce on 15 December 2021. |
| 19 | */ |
| 20 | |
| 21 | namespace WooCommerce\Square\Framework; |
| 22 | |
| 23 | defined( 'ABSPATH' ) || exit; |
| 24 | |
| 25 | /** |
| 26 | * # WooCommerce Plugin Framework |
| 27 | * |
| 28 | * This framework class provides a base level of configurable and overrideable |
| 29 | * functionality and features suitable for the implementation of a WooCommerce |
| 30 | * plugin. This class handles all the "non-feature" support tasks such |
| 31 | * as verifying dependencies are met, loading the text domain, etc. |
| 32 | */ |
| 33 | abstract class Plugin { |
| 34 | |
| 35 | /** @var object single instance of plugin */ |
| 36 | protected static $instance; |
| 37 | |
| 38 | /** @var string plugin id */ |
| 39 | private $id; |
| 40 | |
| 41 | /** @var string version number */ |
| 42 | private $version; |
| 43 | |
| 44 | /** @var string plugin path without trailing slash */ |
| 45 | private $plugin_path; |
| 46 | |
| 47 | /** @var string plugin uri */ |
| 48 | private $plugin_url; |
| 49 | |
| 50 | /** @var \WC_Logger instance */ |
| 51 | private $logger; |
| 52 | |
| 53 | /** @var Admin_Message_Handler instance */ |
| 54 | private $message_handler; |
| 55 | |
| 56 | /** @var string the plugin text domain */ |
| 57 | private $text_domain; |
| 58 | |
| 59 | /** @var Plugin_Dependencies dependency handler instance */ |
| 60 | private $dependency_handler; |
| 61 | |
| 62 | /** @var Plugin\Lifecycle lifecycle handler instance */ |
| 63 | protected $lifecycle_handler; |
| 64 | |
| 65 | /** @var REST_API REST API handler instance */ |
| 66 | protected $rest_api_handler; |
| 67 | |
| 68 | /** @var Admin\Setup_Wizard handler instance */ |
| 69 | protected $setup_wizard_handler; |
| 70 | |
| 71 | /** @var Admin_Notice_Handler the admin notice handler class */ |
| 72 | private $admin_notice_handler; |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * Initialize the plugin. |
| 77 | * |
| 78 | * Child plugin classes may add their own optional arguments. |
| 79 | * |
| 80 | * @since 3.0.0 |
| 81 | * |
| 82 | * @param string $id plugin id |
| 83 | * @param string $version plugin version number |
| 84 | * @param array $args { |
| 85 | * optional plugin arguments |
| 86 | * |
| 87 | * @type string $text_domain the plugin textdomain, used to set up translations |
| 88 | * @type array $dependencies { |
| 89 | * PHP extension, function, and settings dependencies |
| 90 | * |
| 91 | * @type array $php_extensions PHP extension dependencies |
| 92 | * @type array $php_functions PHP function dependencies |
| 93 | * @type array $php_settings PHP settings dependencies |
| 94 | * } |
| 95 | * } |
| 96 | */ |
| 97 | public function __construct( $id, $version, $args = array() ) { |
| 98 | |
| 99 | // required params |
| 100 | $this->id = $id; |
| 101 | $this->version = $version; |
| 102 | |
| 103 | $args = wp_parse_args( |
| 104 | $args, |
| 105 | array( |
| 106 | 'text_domain' => '', |
| 107 | 'dependencies' => array(), |
| 108 | ) |
| 109 | ); |
| 110 | |
| 111 | $this->text_domain = $args['text_domain']; |
| 112 | |
| 113 | // includes that are required to be available at all times |
| 114 | $this->includes(); |
| 115 | |
| 116 | // initialize the dependencies manager |
| 117 | $this->init_dependencies( $args['dependencies'] ); |
| 118 | |
| 119 | // build the admin message handler instance |
| 120 | $this->init_admin_message_handler(); |
| 121 | |
| 122 | // build the admin notice handler instance |
| 123 | $this->init_admin_notice_handler(); |
| 124 | |
| 125 | // build the lifecycle handler instance |
| 126 | $this->init_lifecycle_handler(); |
| 127 | |
| 128 | // add the action & filter hooks |
| 129 | $this->add_hooks(); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** Init methods **********************************************************/ |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * Initializes the plugin dependency handler. |
| 138 | * |
| 139 | * @since 3.0.0 |
| 140 | * |
| 141 | * @param array $dependencies { |
| 142 | * PHP extension, function, and settings dependencies |
| 143 | * |
| 144 | * @type array $php_extensions PHP extension dependencies |
| 145 | * @type array $php_functions PHP function dependencies |
| 146 | * @type array $php_settings PHP settings dependencies |
| 147 | * } |
| 148 | */ |
| 149 | protected function init_dependencies( $dependencies ) { |
| 150 | |
| 151 | $this->dependency_handler = new Plugin_Dependencies( $this, $dependencies ); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | * Builds the admin message handler instance. |
| 157 | * |
| 158 | * Plugins can override this with their own handler. |
| 159 | * |
| 160 | * @since 3.0.0 |
| 161 | */ |
| 162 | protected function init_admin_message_handler() { |
| 163 | |
| 164 | $this->message_handler = new Admin_Message_Handler( 'square' ); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /** |
| 169 | * Builds the admin notice handler instance. |
| 170 | * |
| 171 | * Plugins can override this with their own handler. |
| 172 | * |
| 173 | * @since 3.0.0 |
| 174 | */ |
| 175 | protected function init_admin_notice_handler() { |
| 176 | |
| 177 | $this->admin_notice_handler = new Admin_Notice_Handler( $this ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Builds the lifecycle handler instance. |
| 182 | * |
| 183 | * Plugins can override this with their own handler to perform install and |
| 184 | * upgrade routines. |
| 185 | * |
| 186 | * @since 3.0.0 |
| 187 | */ |
| 188 | protected function init_lifecycle_handler() { |
| 189 | |
| 190 | $this->lifecycle_handler = new Lifecycle( $this ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Adds the action & filter hooks. |
| 195 | * |
| 196 | * @since 3.0.0 |
| 197 | */ |
| 198 | private function add_hooks() { |
| 199 | |
| 200 | // initialize the plugin |
| 201 | add_action( 'plugins_loaded', array( $this, 'init_plugin' ), 15 ); |
| 202 | |
| 203 | // initialize the plugin admin |
| 204 | add_action( 'admin_init', array( $this, 'init_admin' ), 0 ); |
| 205 | |
| 206 | // hook for translations seperately to ensure they're loaded |
| 207 | add_action( 'init', array( $this, 'load_translations' ) ); |
| 208 | |
| 209 | add_action( 'admin_footer', array( $this, 'add_delayed_admin_notices' ) ); |
| 210 | |
| 211 | // add a 'Configure' link to the plugin action links |
| 212 | add_filter( 'plugin_action_links_' . plugin_basename( $this->get_plugin_file() ), array( $this, 'plugin_action_links' ) ); |
| 213 | |
| 214 | // automatically log HTTP requests from Base |
| 215 | $this->add_api_request_logging(); |
| 216 | |
| 217 | // add any PHP incompatibilities to the system status report |
| 218 | add_filter( 'woocommerce_system_status_environment_rows', array( $this, 'add_system_status_php_information' ) ); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * Cloning instances is forbidden due to singleton pattern. |
| 224 | * |
| 225 | * @since 3.0.0 |
| 226 | */ |
| 227 | public function __clone() { |
| 228 | /* translators: Placeholders: %s - plugin name */ |
| 229 | _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'You cannot clone instances of %s.', 'woocommerce-square' ), esc_html( $this->get_plugin_name() ) ), '3.1.0' ); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /** |
| 234 | * Unserializing instances is forbidden due to singleton pattern. |
| 235 | * |
| 236 | * @since 3.0.0 |
| 237 | */ |
| 238 | public function __wakeup() { |
| 239 | /* translators: Placeholders: %s - plugin name */ |
| 240 | _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'You cannot unserialize instances of %s.', 'woocommerce-square' ), esc_html( $this->get_plugin_name() ) ), '3.1.0' ); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /** |
| 245 | * Load plugin & framework text domains. |
| 246 | * |
| 247 | * @internal |
| 248 | * |
| 249 | * @since 3.0.0 |
| 250 | */ |
| 251 | public function load_translations() { |
| 252 | |
| 253 | $this->load_framework_textdomain(); |
| 254 | |
| 255 | // if this plugin passes along its text domain, load its translation files |
| 256 | if ( $this->text_domain ) { |
| 257 | $this->load_plugin_textdomain(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /** |
| 263 | * Loads the framework textdomain. |
| 264 | * |
| 265 | * @since 3.0.0 |
| 266 | */ |
| 267 | protected function load_framework_textdomain() { |
| 268 | $this->load_textdomain( 'woocommerce-square', dirname( plugin_basename( $this->get_framework_file() ) ) ); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /** |
| 273 | * Loads the plugin textdomain. |
| 274 | * |
| 275 | * @since 3.0.0 |
| 276 | */ |
| 277 | protected function load_plugin_textdomain() { |
| 278 | $this->load_textdomain( $this->text_domain, dirname( plugin_basename( $this->get_plugin_file() ) ) ); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /** |
| 283 | * Loads the plugin textdomain. |
| 284 | * |
| 285 | * @since 3.0.0 |
| 286 | * @param string $textdomain the plugin textdomain |
| 287 | * @param string $path the i18n path |
| 288 | */ |
| 289 | protected function load_textdomain( $textdomain, $path ) { |
| 290 | |
| 291 | // user's locale if in the admin for WP 4.7+, or the site locale otherwise |
| 292 | $locale = is_admin() && is_callable( 'get_user_locale' ) ? get_user_locale() : get_locale(); |
| 293 | |
| 294 | /** |
| 295 | * @see https://developer.wordpress.org/reference/hooks/plugin_locale/ plugin_locale |
| 296 | * @since 3.0.0 |
| 297 | */ |
| 298 | $locale = apply_filters( 'plugin_locale', $locale, $textdomain ); |
| 299 | |
| 300 | load_textdomain( $textdomain, WP_LANG_DIR . '/' . $textdomain . '/' . $textdomain . '-' . $locale . '.mo' ); |
| 301 | |
| 302 | load_plugin_textdomain( $textdomain, false, untrailingslashit( $path ) . '/i18n/languages' ); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Include any critical files which must be available as early as possible, |
| 307 | * |
| 308 | * @since 3.0.0 |
| 309 | */ |
| 310 | private function includes() { |
| 311 | |
| 312 | $framework_path = $this->get_framework_path(); |
| 313 | |
| 314 | // addresses |
| 315 | require_once $framework_path . '/Addresses/Address.php'; |
| 316 | require_once $framework_path . '/Addresses/Customer_Address.php'; |
| 317 | |
| 318 | // common utility methods |
| 319 | require_once $framework_path . '/Square_Helper.php'; |
| 320 | |
| 321 | // backwards compatibility for older WC versions |
| 322 | require_once $framework_path . '/Plugin_Compatibility.php'; |
| 323 | require_once $framework_path . '/Compatibility/Data_Compatibility.php'; |
| 324 | require_once $framework_path . '/Compatibility/Order_Compatibility.php'; |
| 325 | |
| 326 | // generic API base |
| 327 | require_once $framework_path . '/Api/Base.php'; |
| 328 | require_once $framework_path . '/Api/API_Request.php'; |
| 329 | require_once $framework_path . '/Api/API_Response.php'; |
| 330 | |
| 331 | // JSON API base |
| 332 | require_once $framework_path . '/Api/API_JSON_Request.php'; |
| 333 | require_once $framework_path . '/Api/API_JSON_Response.php'; |
| 334 | |
| 335 | // Handlers |
| 336 | require_once $framework_path . '/Plugin_Dependencies.php'; |
| 337 | require_once $framework_path . '/Admin_Message_Handler.php'; |
| 338 | require_once $framework_path . '/Admin_Notice_Handler.php'; |
| 339 | require_once $framework_path . '/Lifecycle.php'; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Returns true if on the admin plugin settings page, if any |
| 344 | * |
| 345 | * @since 3.0.0 |
| 346 | * @return boolean true if on the admin plugin settings page |
| 347 | */ |
| 348 | public function is_plugin_settings() { |
| 349 | // optional method, not all plugins *have* a settings page |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Return the plugin action links. This will only be called if the plugin |
| 355 | * is active. |
| 356 | * |
| 357 | * @since 3.0.0 |
| 358 | * @param array $actions associative array of action names to anchor tags |
| 359 | * @return array associative array of plugin action links |
| 360 | */ |
| 361 | public function plugin_action_links( $actions ) { |
| 362 | |
| 363 | $custom_actions = array(); |
| 364 | |
| 365 | // settings url(s) |
| 366 | if ( $this->get_settings_link( 'square' ) ) { |
| 367 | $custom_actions['configure'] = $this->get_settings_link( 'square' ); |
| 368 | } |
| 369 | |
| 370 | // documentation url if any |
| 371 | if ( $this->get_documentation_url() ) { |
| 372 | /* translators: Docs as in Documentation */ |
| 373 | $custom_actions['docs'] = sprintf( '<a href="%s" target="_blank">%s</a>', $this->get_documentation_url(), esc_html__( 'Docs', 'woocommerce-square' ) ); |
| 374 | } |
| 375 | |
| 376 | // support url if any |
| 377 | if ( $this->get_support_url() ) { |
| 378 | $custom_actions['support'] = sprintf( '<a href="%s">%s</a>', $this->get_support_url(), esc_html_x( 'Support', 'noun', 'woocommerce-square' ) ); |
| 379 | } |
| 380 | |
| 381 | // review url if any |
| 382 | if ( $this->get_reviews_url() ) { |
| 383 | $custom_actions['review'] = sprintf( '<a href="%s">%s</a>', $this->get_reviews_url(), esc_html_x( 'Review', 'verb', 'woocommerce-square' ) ); |
| 384 | } |
| 385 | |
| 386 | // add the links to the front of the actions list |
| 387 | return array_merge( $custom_actions, $actions ); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Automatically log API requests/responses when using Base |
| 392 | * |
| 393 | * @since 3.0.0 |
| 394 | * @see Base::broadcast_request() |
| 395 | */ |
| 396 | public function add_api_request_logging() { |
| 397 | |
| 398 | if ( ! has_action( 'wc_square_api_request_performed' ) ) { |
| 399 | add_action( 'wc_square_api_request_performed', array( $this, 'log_api_request' ), 10, 2 ); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | |
| 404 | /** |
| 405 | * Log API requests/responses |
| 406 | * |
| 407 | * @since 3.0.0 |
| 408 | * @param array $request request data, see Base::broadcast_request() for format |
| 409 | * @param array $response response data |
| 410 | * @param string|null $log_id log to write data to |
| 411 | */ |
| 412 | public function log_api_request( $request, $response, $log_id = null ) { |
| 413 | |
| 414 | $this->log( sprintf( "Request\n %s", $this->get_api_log_message( $request ) ), $log_id ); |
| 415 | |
| 416 | if ( ! empty( $response ) ) { |
| 417 | $this->log( sprintf( "Response\n %s", $this->get_api_log_message( $response ) ), $log_id ); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | |
| 422 | /** |
| 423 | * Transform the API request/response data into a string suitable for logging |
| 424 | * |
| 425 | * @since 3.0.0 |
| 426 | * @param array $data |
| 427 | * @return string |
| 428 | */ |
| 429 | public function get_api_log_message( $data ) { |
| 430 | |
| 431 | $messages = array(); |
| 432 | |
| 433 | $messages[] = isset( $data['uri'] ) && $data['uri'] ? 'Request' : 'Response'; |
| 434 | |
| 435 | foreach ( (array) $data as $key => $value ) { |
| 436 | // print_r here is necessary to dump request / response data for logging purposes. |
| 437 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 438 | $messages[] = trim( sprintf( '%s: %s', $key, is_array( $value ) || ( is_object( $value ) && 'stdClass' === get_class( $value ) ) ? print_r( (array) $value, true ) : $value ) ); |
| 439 | } |
| 440 | |
| 441 | return implode( "\n", $messages ) . "\n"; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | /** |
| 446 | * Adds any PHP incompatibilities to the system status report. |
| 447 | * |
| 448 | * @since 3.0.0 |
| 449 | * |
| 450 | * @param array $rows WooCommerce system status rows |
| 451 | * @return array |
| 452 | */ |
| 453 | public function add_system_status_php_information( $rows ) { |
| 454 | |
| 455 | foreach ( $this->get_dependency_handler()->get_incompatible_php_settings() as $setting => $values ) { |
| 456 | |
| 457 | if ( isset( $values['type'] ) && 'min' === $values['type'] ) { |
| 458 | |
| 459 | // if this setting already has a higher minimum from another plugin, skip it |
| 460 | if ( isset( $rows[ $setting ]['expected'] ) && $values['expected'] < $rows[ $setting ]['expected'] ) { |
| 461 | continue; |
| 462 | } |
| 463 | |
| 464 | /* translators: Placeholders: %1$s = Current PHP setting value, %2$s = Minimum required PHP setting value */ |
| 465 | $note = __( '%1$s - A minimum of %2$s is required.', 'woocommerce-square' ); |
| 466 | |
| 467 | } else { |
| 468 | |
| 469 | // if this requirement is already listed, skip it |
| 470 | if ( isset( $rows[ $setting ] ) ) { |
| 471 | continue; |
| 472 | } |
| 473 | |
| 474 | /* translators: Placeholders: %1$s = Current PHP setting value set as, %2$s = Minimum required PHP setting value */ |
| 475 | $note = __( 'Set as %1$s - %2$s is required.', 'woocommerce-square' ); |
| 476 | } |
| 477 | |
| 478 | $note = sprintf( $note, esc_html( $values['actual'] ), esc_html( $values['expected'] ) ); |
| 479 | |
| 480 | $rows[ $setting ] = array( |
| 481 | 'name' => $setting, |
| 482 | 'note' => $note, |
| 483 | 'success' => false, |
| 484 | 'expected' => $values['expected'], // WC doesn't use this, but it's useful for us |
| 485 | ); |
| 486 | } |
| 487 | |
| 488 | return $rows; |
| 489 | } |
| 490 | |
| 491 | |
| 492 | /** |
| 493 | * Saves errors or messages to WooCommerce Log (woocommerce/logs/plugin-id-xxx.txt) |
| 494 | * |
| 495 | * @since 3.0.0 |
| 496 | * @param string $message error or message to save to log |
| 497 | * @param string $log_id optional log id to segment the files by, defaults to plugin id |
| 498 | */ |
| 499 | public function log( $message, $log_id = null ) { |
| 500 | |
| 501 | if ( is_null( $log_id ) ) { |
| 502 | $log_id = 'square'; |
| 503 | } |
| 504 | |
| 505 | if ( ! is_object( $this->logger ) ) { |
| 506 | $this->logger = new \WC_Logger(); |
| 507 | } |
| 508 | |
| 509 | $this->logger->add( $log_id, $message ); |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Gets the main plugin file. |
| 514 | * |
| 515 | * @since 3.0.0 |
| 516 | * |
| 517 | * @return string |
| 518 | */ |
| 519 | public function get_plugin_file() { |
| 520 | |
| 521 | $slug = dirname( plugin_basename( $this->get_file() ) ); |
| 522 | |
| 523 | return trailingslashit( $slug ) . $slug . '.php'; |
| 524 | } |
| 525 | |
| 526 | |
| 527 | /** |
| 528 | * The implementation for this abstract method should simply be: |
| 529 | * |
| 530 | * return __FILE__; |
| 531 | * |
| 532 | * @since 3.0.0 |
| 533 | * @return string the full path and filename of the plugin file |
| 534 | */ |
| 535 | abstract protected function get_file(); |
| 536 | |
| 537 | |
| 538 | /** |
| 539 | * Returns the plugin id |
| 540 | * |
| 541 | * @since 3.0.0 |
| 542 | * @return string plugin id |
| 543 | */ |
| 544 | public function get_id() { |
| 545 | return $this->id; |
| 546 | } |
| 547 | |
| 548 | |
| 549 | /** |
| 550 | * Returns the plugin id with dashes in place of underscores, and |
| 551 | * appropriate for use in frontend element names, classes and ids |
| 552 | * |
| 553 | * @since 3.0.0 |
| 554 | * @return string plugin id with dashes in place of underscores |
| 555 | */ |
| 556 | public function get_id_dasherized() { |
| 557 | return str_replace( '_', '-', 'square' ); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | /** |
| 562 | * Returns the plugin full name including "WooCommerce", ie |
| 563 | * "WooCommerce X". This method is defined abstract for localization purposes |
| 564 | * |
| 565 | * @since 3.0.0 |
| 566 | * @return string plugin name |
| 567 | */ |
| 568 | abstract public function get_plugin_name(); |
| 569 | |
| 570 | /** |
| 571 | * Gets the dependency handler. |
| 572 | * |
| 573 | * @since 3.0.0 |
| 574 | * |
| 575 | * @return Plugin_Dependencies |
| 576 | */ |
| 577 | public function get_dependency_handler() { |
| 578 | |
| 579 | return $this->dependency_handler; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | /** |
| 584 | * Gets the lifecycle handler instance. |
| 585 | * |
| 586 | * @since 3.0.0 |
| 587 | * |
| 588 | * @return Plugin\Lifecycle |
| 589 | */ |
| 590 | public function get_lifecycle_handler() { |
| 591 | |
| 592 | return $this->lifecycle_handler; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Gets the admin message handler. |
| 597 | * |
| 598 | * @since 3.0.0 |
| 599 | * |
| 600 | * @return Admin_Message_Handler |
| 601 | */ |
| 602 | public function get_message_handler() { |
| 603 | |
| 604 | return $this->message_handler; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | /** |
| 609 | * Gets the admin notice handler instance. |
| 610 | * |
| 611 | * @since 3.0.0 |
| 612 | * |
| 613 | * @return Admin_Notice_Handler |
| 614 | */ |
| 615 | public function get_admin_notice_handler() { |
| 616 | |
| 617 | return $this->admin_notice_handler; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | /** |
| 622 | * Returns the plugin version name. Defaults to wc_{plugin id}_version |
| 623 | * |
| 624 | * @since 3.0.0 |
| 625 | * @return string the plugin version name |
| 626 | */ |
| 627 | public function get_plugin_version_name() { |
| 628 | |
| 629 | return 'wc_square_version'; |
| 630 | } |
| 631 | |
| 632 | |
| 633 | /** |
| 634 | * Returns the current version of the plugin |
| 635 | * |
| 636 | * @since 3.0.0 |
| 637 | * @return string plugin version |
| 638 | */ |
| 639 | public function get_version() { |
| 640 | return $this->version; |
| 641 | } |
| 642 | |
| 643 | |
| 644 | /** |
| 645 | * Returns the "Configure" plugin action link to go directly to the plugin |
| 646 | * settings page (if any) |
| 647 | * |
| 648 | * @since 3.0.0 |
| 649 | * @see Plugin::get_settings_url() |
| 650 | * @param string $plugin_id optional plugin identifier. Note that this can be a |
| 651 | * sub-identifier for plugins with multiple parallel settings pages |
| 652 | * (ie a gateway that supports credit cards) |
| 653 | * @return string plugin configure link |
| 654 | */ |
| 655 | public function get_settings_link( $plugin_id = null ) { |
| 656 | |
| 657 | $settings_url = $this->get_settings_url( $plugin_id ); |
| 658 | |
| 659 | if ( $settings_url ) { |
| 660 | return sprintf( '<a href="%s">%s</a>', esc_url( $settings_url ), esc_html__( 'Configure', 'woocommerce-square' ) ); |
| 661 | } |
| 662 | |
| 663 | // no settings |
| 664 | return ''; |
| 665 | } |
| 666 | |
| 667 | |
| 668 | /** |
| 669 | * Gets the plugin configuration URL |
| 670 | * |
| 671 | * @since 3.0.0 |
| 672 | * @see Plugin::get_settings_link() |
| 673 | * @param string $plugin_id optional plugin identifier. Note that this can be a |
| 674 | * sub-identifier for plugins with multiple parallel settings pages |
| 675 | * (ie a gateway that supports credit cards) |
| 676 | * @return string plugin settings URL |
| 677 | */ |
| 678 | public function get_settings_url( $plugin_id = null ) { |
| 679 | |
| 680 | // stub method |
| 681 | return ''; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Returns the admin configuration url for the admin general configuration page |
| 686 | * |
| 687 | * @since 3.0.0 |
| 688 | * @return string admin configuration url for the admin general configuration page |
| 689 | */ |
| 690 | public function get_general_configuration_url() { |
| 691 | |
| 692 | return admin_url( 'admin.php?page=wc-settings&tab=general' ); |
| 693 | } |
| 694 | |
| 695 | |
| 696 | /** |
| 697 | * Gets the plugin documentation url, used for the 'Docs' plugin action |
| 698 | * |
| 699 | * @since 3.0.0 |
| 700 | * @return string documentation URL |
| 701 | */ |
| 702 | public function get_documentation_url() { |
| 703 | |
| 704 | return null; |
| 705 | } |
| 706 | |
| 707 | |
| 708 | /** |
| 709 | * Gets the support URL, used for the 'Support' plugin action link |
| 710 | * |
| 711 | * @since 3.0.0 |
| 712 | * @return string support url |
| 713 | */ |
| 714 | public function get_support_url() { |
| 715 | |
| 716 | return null; |
| 717 | } |
| 718 | |
| 719 | |
| 720 | /** |
| 721 | * Gets the plugin sales page URL. |
| 722 | * |
| 723 | * @since 3.0.0 |
| 724 | * |
| 725 | * @return string |
| 726 | */ |
| 727 | public function get_sales_page_url() { |
| 728 | |
| 729 | return ''; |
| 730 | } |
| 731 | |
| 732 | |
| 733 | /** |
| 734 | * Gets the plugin reviews page URL. |
| 735 | * |
| 736 | * Used for the 'Reviews' plugin action and review prompts. |
| 737 | * |
| 738 | * @since 3.0.0 |
| 739 | * |
| 740 | * @return string |
| 741 | */ |
| 742 | public function get_reviews_url() { |
| 743 | |
| 744 | return $this->get_sales_page_url() ? $this->get_sales_page_url() . '#comments' : ''; |
| 745 | } |
| 746 | |
| 747 | |
| 748 | /** |
| 749 | * Returns the plugin's path without a trailing slash, i.e. |
| 750 | * /path/to/wp-content/plugins/plugin-directory |
| 751 | * |
| 752 | * @since 3.0.0 |
| 753 | * @return string the plugin path |
| 754 | */ |
| 755 | public function get_plugin_path() { |
| 756 | |
| 757 | if ( $this->plugin_path ) { |
| 758 | return $this->plugin_path; |
| 759 | } |
| 760 | |
| 761 | return $this->plugin_path = untrailingslashit( plugin_dir_path( $this->get_file() ) ); |
| 762 | } |
| 763 | |
| 764 | |
| 765 | /** |
| 766 | * Returns the plugin's url without a trailing slash, i.e. |
| 767 | * |
| 768 | * @since 3.0.0 |
| 769 | * @return string the plugin URL |
| 770 | */ |
| 771 | public function get_plugin_url() { |
| 772 | |
| 773 | if ( $this->plugin_url ) { |
| 774 | return $this->plugin_url; |
| 775 | } |
| 776 | |
| 777 | return $this->plugin_url = untrailingslashit( plugins_url( '/', $this->get_file() ) ); |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Returns the loaded framework __FILE__ |
| 782 | * |
| 783 | * @since 3.0.0 |
| 784 | * @return string |
| 785 | */ |
| 786 | public function get_framework_file() { |
| 787 | |
| 788 | return __FILE__; |
| 789 | } |
| 790 | |
| 791 | |
| 792 | /** |
| 793 | * Returns the loaded framework path, without trailing slash. Ths is the highest |
| 794 | * version framework that was loaded by the bootstrap. |
| 795 | * |
| 796 | * @since 3.0.0 |
| 797 | * @return string |
| 798 | */ |
| 799 | public function get_framework_path() { |
| 800 | |
| 801 | return untrailingslashit( plugin_dir_path( $this->get_framework_file() ) ); |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Returns the loaded framework assets URL without a trailing slash |
| 806 | * |
| 807 | * @since 3.0.0 |
| 808 | * @return string |
| 809 | */ |
| 810 | public function get_framework_assets_url() { |
| 811 | |
| 812 | return untrailingslashit( plugins_url( '/assets', $this->get_framework_file() ) ); |
| 813 | } |
| 814 | |
| 815 | |
| 816 | /** |
| 817 | * Helper function to determine whether a plugin is active |
| 818 | * |
| 819 | * @since 3.0.0 |
| 820 | * @param string $plugin_name plugin name, as the plugin-filename.php |
| 821 | * @return boolean true if the named plugin is installed and active |
| 822 | */ |
| 823 | public function is_plugin_active( $plugin_name ) { |
| 824 | |
| 825 | $active_plugins = (array) get_option( 'active_plugins', array() ); |
| 826 | |
| 827 | if ( is_multisite() ) { |
| 828 | $active_plugins = array_merge( $active_plugins, array_keys( get_site_option( 'active_sitewide_plugins', array() ) ) ); |
| 829 | } |
| 830 | |
| 831 | $plugin_filenames = array(); |
| 832 | |
| 833 | foreach ( $active_plugins as $plugin ) { |
| 834 | |
| 835 | if ( Square_Helper::str_exists( $plugin, '/' ) ) { |
| 836 | |
| 837 | // normal plugin name (plugin-dir/plugin-filename.php) |
| 838 | list( , $filename ) = explode( '/', $plugin ); |
| 839 | |
| 840 | } else { |
| 841 | |
| 842 | // no directory, just plugin file |
| 843 | $filename = $plugin; |
| 844 | } |
| 845 | |
| 846 | $plugin_filenames[] = $filename; |
| 847 | } |
| 848 | |
| 849 | return in_array( $plugin_name, $plugin_filenames, true ); |
| 850 | } |
| 851 | } |
| 852 |