ArrayUtil.php
2 years ago
FeaturesUtil.php
3 years ago
I18nUtil.php
3 years ago
NumberUtil.php
5 years ago
OrderUtil.php
2 years ago
PluginUtil.php
2 years ago
StringUtil.php
2 years ago
PluginUtil.php
236 lines
| 1 | <?php |
| 2 | /** |
| 3 | * A class of utilities for dealing with plugins. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Utilities; |
| 7 | |
| 8 | use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods; |
| 9 | use Automattic\WooCommerce\Proxies\LegacyProxy; |
| 10 | |
| 11 | /** |
| 12 | * A class of utilities for dealing with plugins. |
| 13 | */ |
| 14 | class PluginUtil { |
| 15 | |
| 16 | use AccessiblePrivateMethods; |
| 17 | |
| 18 | /** |
| 19 | * The LegacyProxy instance to use. |
| 20 | * |
| 21 | * @var LegacyProxy |
| 22 | */ |
| 23 | private $proxy; |
| 24 | |
| 25 | /** |
| 26 | * The cached list of WooCommerce aware plugin ids. |
| 27 | * |
| 28 | * @var null|array |
| 29 | */ |
| 30 | private $woocommerce_aware_plugins = null; |
| 31 | |
| 32 | /** |
| 33 | * The cached list of enabled WooCommerce aware plugin ids. |
| 34 | * |
| 35 | * @var null|array |
| 36 | */ |
| 37 | private $woocommerce_aware_active_plugins = null; |
| 38 | |
| 39 | /** |
| 40 | * Creates a new instance of the class. |
| 41 | */ |
| 42 | public function __construct() { |
| 43 | self::add_action( 'activated_plugin', array( $this, 'handle_plugin_de_activation' ), 10, 0 ); |
| 44 | self::add_action( 'deactivated_plugin', array( $this, 'handle_plugin_de_activation' ), 10, 0 ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Initialize the class instance. |
| 49 | * |
| 50 | * @internal |
| 51 | * |
| 52 | * @param LegacyProxy $proxy The instance of LegacyProxy to use. |
| 53 | */ |
| 54 | final public function init( LegacyProxy $proxy ) { |
| 55 | $this->proxy = $proxy; |
| 56 | require_once ABSPATH . WPINC . '/plugin.php'; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get a list with the names of the WordPress plugins that are WooCommerce aware |
| 61 | * (they have a "WC tested up to" header). |
| 62 | * |
| 63 | * @param bool $active_only True to return only active plugins, false to return all the active plugins. |
| 64 | * @return string[] A list of plugin ids (path/file.php). |
| 65 | */ |
| 66 | public function get_woocommerce_aware_plugins( bool $active_only = false ): array { |
| 67 | if ( is_null( $this->woocommerce_aware_plugins ) ) { |
| 68 | // In case `get_plugins` was called much earlier in the request (before our headers could be injected), we |
| 69 | // invalidate the plugin cache list. |
| 70 | wp_cache_delete( 'plugins', 'plugins' ); |
| 71 | $all_plugins = $this->proxy->call_function( 'get_plugins' ); |
| 72 | |
| 73 | $this->woocommerce_aware_plugins = |
| 74 | array_keys( |
| 75 | array_filter( |
| 76 | $all_plugins, |
| 77 | array( $this, 'is_woocommerce_aware_plugin' ) |
| 78 | ) |
| 79 | ); |
| 80 | |
| 81 | $this->woocommerce_aware_active_plugins = |
| 82 | array_values( |
| 83 | array_filter( |
| 84 | $this->woocommerce_aware_plugins, |
| 85 | function ( $plugin_name ) { |
| 86 | return $this->proxy->call_function( 'is_plugin_active', $plugin_name ); |
| 87 | } |
| 88 | ) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | return $active_only ? $this->woocommerce_aware_active_plugins : $this->woocommerce_aware_plugins; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the printable name of a plugin. |
| 97 | * |
| 98 | * @param string $plugin_id Plugin id (path/file.php). |
| 99 | * @return string Printable plugin name, or the plugin id itself if printable name is not available. |
| 100 | */ |
| 101 | public function get_plugin_name( string $plugin_id ): string { |
| 102 | $plugin_data = $this->proxy->call_function( 'get_plugin_data', WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_id ); |
| 103 | return $plugin_data['Name'] ?? $plugin_id; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Check if a plugin is WooCommerce aware. |
| 108 | * |
| 109 | * @param string|array $plugin_file_or_data Plugin id (path/file.php) or plugin data (as returned by get_plugins). |
| 110 | * @return bool True if the plugin exists and is WooCommerce aware. |
| 111 | * @throws \Exception The input is neither a string nor an array. |
| 112 | */ |
| 113 | public function is_woocommerce_aware_plugin( $plugin_file_or_data ): bool { |
| 114 | if ( is_string( $plugin_file_or_data ) ) { |
| 115 | return in_array( $plugin_file_or_data, $this->get_woocommerce_aware_plugins(), true ); |
| 116 | } elseif ( is_array( $plugin_file_or_data ) ) { |
| 117 | return '' !== ( $plugin_file_or_data['WC tested up to'] ?? '' ); |
| 118 | } else { |
| 119 | throw new \Exception( 'is_woocommerce_aware_plugin requires a plugin name or an array of plugin data as input' ); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Match plugin identifier passed as a parameter with the output from `get_plugins()`. |
| 125 | * |
| 126 | * @param string $plugin_file Plugin identifier, either 'my-plugin/my-plugin.php', or output from __FILE__. |
| 127 | * |
| 128 | * @return string|false Key from the array returned by `get_plugins` if matched. False if no match. |
| 129 | */ |
| 130 | public function get_wp_plugin_id( $plugin_file ) { |
| 131 | $wp_plugins = array_keys( $this->proxy->call_function( 'get_plugins' ) ); |
| 132 | |
| 133 | // Try to match plugin_basename(). |
| 134 | $plugin_basename = $this->proxy->call_function( 'plugin_basename', $plugin_file ); |
| 135 | if ( in_array( $plugin_basename, $wp_plugins, true ) ) { |
| 136 | return $plugin_basename; |
| 137 | } |
| 138 | |
| 139 | // Try to match by the my-file/my-file.php (dir + file name), then by my-file.php (file name only). |
| 140 | $plugin_file = str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $plugin_file ); |
| 141 | $file_name_parts = explode( DIRECTORY_SEPARATOR, $plugin_file ); |
| 142 | $file_name = array_pop( $file_name_parts ); |
| 143 | $directory_name = array_pop( $file_name_parts ); |
| 144 | $full_matches = array(); |
| 145 | $partial_matches = array(); |
| 146 | foreach ( $wp_plugins as $wp_plugin ) { |
| 147 | if ( false !== strpos( $wp_plugin, $directory_name . DIRECTORY_SEPARATOR . $file_name ) ) { |
| 148 | $full_matches[] = $wp_plugin; |
| 149 | } |
| 150 | |
| 151 | if ( false !== strpos( $wp_plugin, $file_name ) ) { |
| 152 | $partial_matches[] = $wp_plugin; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if ( 1 === count( $full_matches ) ) { |
| 157 | return $full_matches[0]; |
| 158 | } |
| 159 | |
| 160 | if ( 1 === count( $partial_matches ) ) { |
| 161 | return $partial_matches[0]; |
| 162 | } |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Handle plugin activation and deactivation by clearing the WooCommerce aware plugin ids cache. |
| 169 | */ |
| 170 | private function handle_plugin_de_activation(): void { |
| 171 | $this->woocommerce_aware_plugins = null; |
| 172 | $this->woocommerce_aware_active_plugins = null; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Util function to generate warning string for incompatible features based on active plugins. |
| 177 | * |
| 178 | * @param string $feature_id Feature id. |
| 179 | * @param array $plugin_feature_info Array of plugin feature info. See FeaturesControllers->get_compatible_plugins_for_feature() for details. |
| 180 | * |
| 181 | * @return string Warning string. |
| 182 | */ |
| 183 | public function generate_incompatible_plugin_feature_warning( string $feature_id, array $plugin_feature_info ) : string { |
| 184 | $feature_warning = ''; |
| 185 | $incompatibles = array_merge( $plugin_feature_info['incompatible'], $plugin_feature_info['uncertain'] ); |
| 186 | $incompatibles = array_filter( $incompatibles, 'is_plugin_active' ); |
| 187 | $incompatible_count = count( $incompatibles ); |
| 188 | if ( $incompatible_count > 0 ) { |
| 189 | if ( 1 === $incompatible_count ) { |
| 190 | /* translators: %s = printable plugin name */ |
| 191 | $feature_warning = sprintf( __( '⚠ 1 Incompatible plugin detected (%s).', 'woocommerce' ), $this->get_plugin_name( $incompatibles[0] ) ); |
| 192 | } elseif ( 2 === $incompatible_count ) { |
| 193 | $feature_warning = sprintf( |
| 194 | /* translators: %1\$s, %2\$s = printable plugin names */ |
| 195 | __( '⚠ 2 Incompatible plugins detected (%1$s and %2$s).', 'woocommerce' ), |
| 196 | $this->get_plugin_name( $incompatibles[0] ), |
| 197 | $this->get_plugin_name( $incompatibles[1] ) |
| 198 | ); |
| 199 | } else { |
| 200 | |
| 201 | $feature_warning = sprintf( |
| 202 | /* translators: %1\$s, %2\$s = printable plugin names, %3\$d = plugins count */ |
| 203 | _n( |
| 204 | '⚠ Incompatible plugins detected (%1$s, %2$s and %3$d other).', |
| 205 | '⚠ Incompatible plugins detected (%1$s and %2$s plugins and %3$d others).', |
| 206 | $incompatible_count - 2, |
| 207 | 'woocommerce' |
| 208 | ), |
| 209 | $this->get_plugin_name( $incompatibles[0] ), |
| 210 | $this->get_plugin_name( $incompatibles[1] ), |
| 211 | $incompatible_count - 2 |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | $incompatible_plugins_url = add_query_arg( |
| 216 | array( |
| 217 | 'plugin_status' => 'incompatible_with_feature', |
| 218 | 'feature_id' => $feature_id, |
| 219 | ), |
| 220 | admin_url( 'plugins.php' ) |
| 221 | ); |
| 222 | $extra_desc_tip = '<br>' . sprintf( |
| 223 | /* translators: %1$s opening link tag %2$s closing link tag. */ |
| 224 | __( '%1$sView and manage%2$s', 'woocommerce' ), |
| 225 | '<a href="' . esc_url( $incompatible_plugins_url ) . '">', |
| 226 | '</a>' |
| 227 | ); |
| 228 | |
| 229 | $feature_warning .= $extra_desc_tip; |
| 230 | |
| 231 | } |
| 232 | |
| 233 | return $feature_warning; |
| 234 | } |
| 235 | } |
| 236 |