API
5 years ago
Composer
5 years ago
DateTimeProvider
5 years ago
Features
5 years ago
Marketing
5 years ago
Notes
5 years ago
Overrides
5 years ago
PluginsProvider
5 years ago
RemoteInboxNotifications
5 years ago
Schedulers
5 years ago
CategoryLookup.php
5 years ago
DeprecatedClassFacade.php
5 years ago
Events.php
5 years ago
FeaturePlugin.php
5 years ago
Install.php
5 years ago
Loader.php
5 years ago
PageController.php
5 years ago
PluginsHelper.php
5 years ago
ReportCSVEmail.php
5 years ago
ReportCSVExporter.php
5 years ago
ReportExporter.php
5 years ago
ReportsSync.php
5 years ago
Survey.php
5 years ago
PluginsHelper.php
117 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PluginsHelper |
| 4 | * |
| 5 | * Helper class for the site's plugins. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | if ( ! function_exists( 'get_plugins' ) ) { |
| 13 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Class PluginsHelper |
| 18 | */ |
| 19 | class PluginsHelper { |
| 20 | |
| 21 | /** |
| 22 | * Get the path to the plugin file relative to the plugins directory from the plugin slug. |
| 23 | * |
| 24 | * E.g. 'woocommerce' returns 'woocommerce/woocommerce.php' |
| 25 | * |
| 26 | * @param string $slug Plugin slug to get path for. |
| 27 | * |
| 28 | * @return string|false |
| 29 | */ |
| 30 | public static function get_plugin_path_from_slug( $slug ) { |
| 31 | $plugins = get_plugins(); |
| 32 | |
| 33 | if ( strstr( $slug, '/' ) ) { |
| 34 | // The slug is already a plugin path. |
| 35 | return $slug; |
| 36 | } |
| 37 | |
| 38 | foreach ( $plugins as $plugin_path => $data ) { |
| 39 | $path_parts = explode( '/', $plugin_path ); |
| 40 | if ( $path_parts[0] === $slug ) { |
| 41 | return $plugin_path; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get an array of installed plugin slugs. |
| 50 | * |
| 51 | * @return array |
| 52 | */ |
| 53 | public static function get_installed_plugin_slugs() { |
| 54 | return array_map( |
| 55 | function( $plugin_path ) { |
| 56 | $path_parts = explode( '/', $plugin_path ); |
| 57 | return $path_parts[0]; |
| 58 | }, |
| 59 | array_keys( get_plugins() ) |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get an array of active plugin slugs. |
| 65 | * |
| 66 | * @return array |
| 67 | */ |
| 68 | public static function get_active_plugin_slugs() { |
| 69 | return array_map( |
| 70 | function( $plugin_path ) { |
| 71 | $path_parts = explode( '/', $plugin_path ); |
| 72 | return $path_parts[0]; |
| 73 | }, |
| 74 | get_option( 'active_plugins', array() ) |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Checks if a plugin is installed. |
| 80 | * |
| 81 | * @param string $plugin Path to the plugin file relative to the plugins directory or the plugin directory name. |
| 82 | * |
| 83 | * @return bool |
| 84 | */ |
| 85 | public static function is_plugin_installed( $plugin ) { |
| 86 | $plugin_path = self::get_plugin_path_from_slug( $plugin ); |
| 87 | return $plugin_path ? array_key_exists( $plugin_path, get_plugins() ) : false; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Checks if a plugin is active. |
| 92 | * |
| 93 | * @param string $plugin Path to the plugin file relative to the plugins directory or the plugin directory name. |
| 94 | * |
| 95 | * @return bool |
| 96 | */ |
| 97 | public static function is_plugin_active( $plugin ) { |
| 98 | $plugin_path = self::get_plugin_path_from_slug( $plugin ); |
| 99 | return $plugin_path ? in_array( $plugin_path, get_option( 'active_plugins', array() ), true ) : false; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get plugin data. |
| 104 | * |
| 105 | * @param string $plugin Path to the plugin file relative to the plugins directory or the plugin directory name. |
| 106 | * |
| 107 | * @return array|false |
| 108 | */ |
| 109 | public static function get_plugin_data( $plugin ) { |
| 110 | $plugin_path = self::get_plugin_path_from_slug( $plugin ); |
| 111 | $plugins = get_plugins(); |
| 112 | |
| 113 | return isset( $plugins[ $plugin_path ] ) ? $plugins[ $plugin_path ] : false; |
| 114 | } |
| 115 | |
| 116 | } |
| 117 |