PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 4.8.0
WooCommerce Square v4.8.0
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Framework / Plugin.php
woocommerce-square / includes / Framework Last commit date
Addresses 3 years ago Api 2 years ago Compatibility 2 years ago PaymentGateway 1 year ago Utilities 2 years ago Admin_Message_Handler.php 3 years ago Admin_Notice_Handler.php 3 years ago Lifecycle.php 2 years ago Plugin.php 2 years ago Plugin_Compatibility.php 2 years ago Plugin_Dependencies.php 2 years ago Square_Helper.php 3 years ago
Plugin.php
887 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_square_onboarding_link() && wc_square()->get_dependency_handler()->meets_php_dependencies() ) {
367 $custom_actions['setup-wizard'] = $this->get_square_onboarding_link();
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 * Returns the "Configure" plugin action link to go directly to the plugin
669 * settings page (if any)
670 *
671 * @since 4.7.0
672 * @see Plugin::get_settings_url()
673 * @param string $step optional step identifier.
674 *
675 * @return string plugin configure link
676 */
677 public function get_square_onboarding_link( $step = '' ) {
678
679 $square_onboarding_url = $this->get_square_onboarding_url( $step );
680
681 if ( $square_onboarding_url ) {
682 return sprintf( '<a href="%s">%s</a>', esc_url( $square_onboarding_url ), esc_html__( 'Setup Wizard', 'woocommerce-square' ) );
683 }
684
685 // no settings
686 return '';
687 }
688
689
690 /**
691 * Gets the plugin configuration URL
692 *
693 * @since 4.7.0
694 * @see Plugin::get_settings_link()
695 * @return string plugin settings URL
696 */
697 public function get_square_onboarding_url() {
698
699 // stub method
700 return '';
701 }
702
703 /**
704 * Gets the plugin configuration URL
705 *
706 * @since 3.0.0
707 * @see Plugin::get_settings_link()
708 * @param string $plugin_id optional plugin identifier. Note that this can be a
709 * sub-identifier for plugins with multiple parallel settings pages
710 * (ie a gateway that supports credit cards)
711 * @return string plugin settings URL
712 */
713 public function get_settings_url( $plugin_id = null ) {
714
715 // stub method
716 return '';
717 }
718
719 /**
720 * Returns the admin configuration url for the admin general configuration page
721 *
722 * @since 3.0.0
723 * @return string admin configuration url for the admin general configuration page
724 */
725 public function get_general_configuration_url() {
726
727 return admin_url( 'admin.php?page=wc-settings&tab=general' );
728 }
729
730
731 /**
732 * Gets the plugin documentation url, used for the 'Docs' plugin action
733 *
734 * @since 3.0.0
735 * @return string documentation URL
736 */
737 public function get_documentation_url() {
738
739 return null;
740 }
741
742
743 /**
744 * Gets the support URL, used for the 'Support' plugin action link
745 *
746 * @since 3.0.0
747 * @return string support url
748 */
749 public function get_support_url() {
750
751 return null;
752 }
753
754
755 /**
756 * Gets the plugin sales page URL.
757 *
758 * @since 3.0.0
759 *
760 * @return string
761 */
762 public function get_sales_page_url() {
763
764 return '';
765 }
766
767
768 /**
769 * Gets the plugin reviews page URL.
770 *
771 * Used for the 'Reviews' plugin action and review prompts.
772 *
773 * @since 3.0.0
774 *
775 * @return string
776 */
777 public function get_reviews_url() {
778
779 return $this->get_sales_page_url() ? $this->get_sales_page_url() . '#comments' : '';
780 }
781
782
783 /**
784 * Returns the plugin's path without a trailing slash, i.e.
785 * /path/to/wp-content/plugins/plugin-directory
786 *
787 * @since 3.0.0
788 * @return string the plugin path
789 */
790 public function get_plugin_path() {
791
792 if ( $this->plugin_path ) {
793 return $this->plugin_path;
794 }
795
796 return $this->plugin_path = untrailingslashit( plugin_dir_path( $this->get_file() ) );
797 }
798
799
800 /**
801 * Returns the plugin's url without a trailing slash, i.e.
802 *
803 * @since 3.0.0
804 * @return string the plugin URL
805 */
806 public function get_plugin_url() {
807
808 if ( $this->plugin_url ) {
809 return $this->plugin_url;
810 }
811
812 return $this->plugin_url = untrailingslashit( plugins_url( '/', $this->get_file() ) );
813 }
814
815 /**
816 * Returns the loaded framework __FILE__
817 *
818 * @since 3.0.0
819 * @return string
820 */
821 public function get_framework_file() {
822
823 return __FILE__;
824 }
825
826
827 /**
828 * Returns the loaded framework path, without trailing slash. Ths is the highest
829 * version framework that was loaded by the bootstrap.
830 *
831 * @since 3.0.0
832 * @return string
833 */
834 public function get_framework_path() {
835
836 return untrailingslashit( plugin_dir_path( $this->get_framework_file() ) );
837 }
838
839 /**
840 * Returns the loaded framework assets URL without a trailing slash
841 *
842 * @since 3.0.0
843 * @return string
844 */
845 public function get_framework_assets_url() {
846
847 return untrailingslashit( plugins_url( '/assets', $this->get_framework_file() ) );
848 }
849
850
851 /**
852 * Helper function to determine whether a plugin is active
853 *
854 * @since 3.0.0
855 * @param string $plugin_name plugin name, as the plugin-filename.php
856 * @return boolean true if the named plugin is installed and active
857 */
858 public function is_plugin_active( $plugin_name ) {
859
860 $active_plugins = (array) get_option( 'active_plugins', array() );
861
862 if ( is_multisite() ) {
863 $active_plugins = array_merge( $active_plugins, array_keys( get_site_option( 'active_sitewide_plugins', array() ) ) );
864 }
865
866 $plugin_filenames = array();
867
868 foreach ( $active_plugins as $plugin ) {
869
870 if ( Square_Helper::str_exists( $plugin, '/' ) ) {
871
872 // normal plugin name (plugin-dir/plugin-filename.php)
873 list( , $filename ) = explode( '/', $plugin );
874
875 } else {
876
877 // no directory, just plugin file
878 $filename = $plugin;
879 }
880
881 $plugin_filenames[] = $filename;
882 }
883
884 return in_array( $plugin_name, $plugin_filenames, true );
885 }
886 }
887