Cli
1 year ago
Exporters
1 year ago
Importers
11 months ago
ResourceStorages
1 year ago
ResultFormatters
1 year ago
Schemas
1 year ago
Steps
1 year ago
docs
1 year ago
BuiltInExporters.php
1 year ago
BuiltInStepProcessors.php
1 year ago
ClassExtractor.php
1 year ago
Cli.php
1 year ago
ExportSchema.php
1 year ago
ImportSchema.php
1 year ago
ImportStep.php
1 year ago
Logger.php
1 year ago
ResourceStorages.php
1 year ago
StepProcessor.php
1 year ago
StepProcessorResult.php
1 year ago
UsePluginHelpers.php
1 year ago
UsePubSub.php
1 year ago
UseWPFunctions.php
1 year ago
Util.php
1 year ago
UsePluginHelpers.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Blueprint; |
| 4 | |
| 5 | trait UsePluginHelpers { |
| 6 | use UseWPFunctions; |
| 7 | |
| 8 | /** |
| 9 | * Activate a plugin by its slug. |
| 10 | * |
| 11 | * Searches for the plugin with the specified slug in the installed plugins |
| 12 | * and activates it. |
| 13 | * |
| 14 | * @param string $slug The slug of the plugin to activate. |
| 15 | * |
| 16 | * @return false|null|WP_Error Null on success, WP_Error on invalid file, false if not found. |
| 17 | */ |
| 18 | public function activate_plugin_by_slug( $slug ) { |
| 19 | // Get all installed plugins. |
| 20 | $all_plugins = $this->wp_get_plugins(); |
| 21 | |
| 22 | // Loop through all plugins to find the one with the specified slug. |
| 23 | foreach ( $all_plugins as $plugin_path => $plugin_info ) { |
| 24 | // Check if the plugin path contains the slug. |
| 25 | if ( strpos( $plugin_path, $slug . '/' ) === 0 ) { |
| 26 | // Deactivate the plugin. |
| 27 | return $this->wp_activate_plugin( $plugin_path ); |
| 28 | } |
| 29 | } |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Check if a plugin with the specified slug is installed. |
| 35 | * |
| 36 | * @param string $slug The slug of the plugin to check. |
| 37 | * |
| 38 | * @return bool |
| 39 | */ |
| 40 | public function is_plugin_dir( $slug ) { |
| 41 | $all_plugins = $this->wp_get_plugins(); |
| 42 | foreach ( $all_plugins as $plugin_file => $plugin_data ) { |
| 43 | // Extract the directory name from the plugin file path. |
| 44 | $plugin_dir = explode( '/', $plugin_file )[0]; |
| 45 | |
| 46 | // Check for an exact match with the slug. |
| 47 | if ( $plugin_dir === $slug ) { |
| 48 | return true; |
| 49 | } |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Deactivate and delete a plugin by its slug. |
| 56 | * |
| 57 | * Searches for the plugin with the specified slug in the installed plugins, |
| 58 | * deactivates it if active, and then deletes it. |
| 59 | * |
| 60 | * @param string $slug The slug of the plugin to delete. |
| 61 | * |
| 62 | * @return bool|WP_Error True if the plugin was deleted, false otherwise. |
| 63 | */ |
| 64 | public function delete_plugin_by_slug( $slug ) { |
| 65 | // Get all installed plugins. |
| 66 | $all_plugins = $this->wp_get_plugins(); |
| 67 | |
| 68 | // Loop through all plugins to find the one with the specified slug. |
| 69 | foreach ( $all_plugins as $plugin_path => $plugin_info ) { |
| 70 | // Check if the plugin path contains the slug. |
| 71 | if ( strpos( $plugin_path, $slug . '/' ) === 0 ) { |
| 72 | // Deactivate the plugin. |
| 73 | if ( $this->deactivate_plugin_by_slug( $slug ) ) { |
| 74 | // Delete the plugin. |
| 75 | return $this->wp_delete_plugins( array( $plugin_path ) ); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Deactivate a plugin by its slug. |
| 84 | * |
| 85 | * Searches for the plugin with the specified slug in the installed plugins |
| 86 | * and deactivates it. |
| 87 | * |
| 88 | * @param string $slug The slug of the plugin to deactivate. |
| 89 | * |
| 90 | * @return bool True if the plugin was deactivated, false otherwise. |
| 91 | */ |
| 92 | public function deactivate_plugin_by_slug( $slug ) { |
| 93 | // Get all installed plugins. |
| 94 | $all_plugins = $this->wp_get_plugins(); |
| 95 | |
| 96 | // Loop through all plugins to find the one with the specified slug. |
| 97 | foreach ( $all_plugins as $plugin_path => $plugin_info ) { |
| 98 | // Check if the plugin path contains the slug. |
| 99 | if ( strpos( $plugin_path, $slug . '/' ) === 0 ) { |
| 100 | // Deactivate the plugin. |
| 101 | deactivate_plugins( $plugin_path ); |
| 102 | |
| 103 | // Check if the plugin has been deactivated. |
| 104 | if ( ! is_plugin_active( $plugin_path ) ) { |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 |