ExportInstallPluginSteps.php
1 year ago
ExportInstallThemeSteps.php
1 year ago
HasAlias.php
1 year ago
StepExporter.php
1 year ago
ExportInstallPluginSteps.php
170 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Blueprint\Exporters; |
| 4 | |
| 5 | use Automattic\WooCommerce\Blueprint\Steps\InstallPlugin; |
| 6 | use Automattic\WooCommerce\Blueprint\UseWPFunctions; |
| 7 | |
| 8 | /** |
| 9 | * Class ExportInstallPluginSteps |
| 10 | * |
| 11 | * @package Automattic\WooCommerce\Blueprint\Exporters |
| 12 | */ |
| 13 | class ExportInstallPluginSteps implements StepExporter { |
| 14 | use UseWPFunctions; |
| 15 | |
| 16 | /** |
| 17 | * Filter callback. |
| 18 | * |
| 19 | * @var callable |
| 20 | */ |
| 21 | private $filter_callback; |
| 22 | |
| 23 | /** |
| 24 | * Whether to include private plugins in the export. |
| 25 | * |
| 26 | * @var bool Whether to include private plugins in the export. |
| 27 | */ |
| 28 | private bool $include_private_plugins = false; |
| 29 | |
| 30 | /** |
| 31 | * Set whether to include private plugins in the export. |
| 32 | * |
| 33 | * @param bool $boolean Whether to include private plugins. |
| 34 | */ |
| 35 | public function include_private_plugins( bool $boolean ) { |
| 36 | $this->include_private_plugins = $boolean; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Register a filter callback to filter the plugins to export. |
| 41 | * |
| 42 | * @param callable $callback Filter callback. |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function filter( callable $callback ) { |
| 47 | $this->filter_callback = $callback; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Export the steps required to install plugins. |
| 52 | * |
| 53 | * @return array The array of InstallPlugin steps. |
| 54 | */ |
| 55 | public function export() { |
| 56 | $plugins = $this->sort_plugins_by_dep( $this->wp_get_plugins() ); |
| 57 | |
| 58 | if ( is_callable( $this->filter_callback ) ) { |
| 59 | $plugins = call_user_func( $this->filter_callback, $plugins ); |
| 60 | } |
| 61 | |
| 62 | // @todo temporary fix for JN site -- it includes WooCommerce as a custom plugin |
| 63 | // since JN sites are using a different slug. |
| 64 | $exclude = array( 'WooCommerce Beta Tester' ); |
| 65 | $steps = array(); |
| 66 | foreach ( $plugins as $path => $plugin ) { |
| 67 | if ( in_array( $plugin['Name'], $exclude, true ) ) { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | $slug = dirname( $path ); |
| 72 | // single-file plugin. |
| 73 | if ( '.' === $slug ) { |
| 74 | $slug = pathinfo( $path )['filename']; |
| 75 | } |
| 76 | $info = $this->wp_plugins_api( |
| 77 | 'plugin_information', |
| 78 | array( |
| 79 | 'slug' => $slug, |
| 80 | 'fields' => array( |
| 81 | 'sections' => false, |
| 82 | ), |
| 83 | ) |
| 84 | ); |
| 85 | |
| 86 | $has_download_link = isset( $info->download_link ); |
| 87 | if ( false === $this->include_private_plugins && ! $has_download_link ) { |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | $resource = $has_download_link ? 'wordpress.org/plugins' : 'self/plugins'; |
| 92 | $steps[] = new InstallPlugin( |
| 93 | $slug, |
| 94 | $resource, |
| 95 | array( |
| 96 | 'activate' => true, |
| 97 | ) |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | return $steps; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Sort plugins by dependencies -- put the dependencies at the top. |
| 106 | * |
| 107 | * @param array $plugins List of plugins to sort (from wp_get_plugins function). |
| 108 | * |
| 109 | * @return array |
| 110 | */ |
| 111 | public function sort_plugins_by_dep( array $plugins ) { |
| 112 | $sorted = array(); |
| 113 | $visited = array(); |
| 114 | |
| 115 | // Create a mapping of lowercase titles to plugin keys for quick lookups. |
| 116 | $title_map = array_reduce( |
| 117 | array_keys( $plugins ), |
| 118 | function ( $carry, $key ) use ( $plugins ) { |
| 119 | $title = strtolower( $plugins[ $key ]['Title'] ?? '' ); |
| 120 | if ( $title ) { |
| 121 | $carry[ $title ] = $key; |
| 122 | } |
| 123 | return $carry; |
| 124 | }, |
| 125 | array() |
| 126 | ); |
| 127 | |
| 128 | // Recursive function for topological sort. |
| 129 | $visit = function ( $plugin_key ) use ( &$visit, &$sorted, &$visited, $plugins, $title_map ) { |
| 130 | if ( isset( $visited[ $plugin_key ] ) ) { |
| 131 | return; |
| 132 | } |
| 133 | $visited[ $plugin_key ] = true; |
| 134 | |
| 135 | $requires = $plugins[ $plugin_key ]['RequiresPlugins'] ?? array(); |
| 136 | foreach ( (array) $requires as $dependency ) { |
| 137 | $dependency_key = $title_map[ strtolower( $dependency ) ] ?? null; |
| 138 | if ( $dependency_key ) { |
| 139 | $visit( $dependency_key ); |
| 140 | } |
| 141 | } |
| 142 | $sorted[ $plugin_key ] = $plugins[ $plugin_key ]; |
| 143 | }; |
| 144 | |
| 145 | // Perform sort for each plugin. |
| 146 | foreach ( array_keys( $plugins ) as $plugin_key ) { |
| 147 | $visit( $plugin_key ); |
| 148 | } |
| 149 | |
| 150 | return $sorted; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get the name of the step. |
| 155 | * |
| 156 | * @return string The step name. |
| 157 | */ |
| 158 | public function get_step_name() { |
| 159 | return InstallPlugin::get_step_name(); |
| 160 | } |
| 161 | /** |
| 162 | * Check if the current user has the required capabilities for this step. |
| 163 | * |
| 164 | * @return bool True if the user has the required capabilities. False otherwise. |
| 165 | */ |
| 166 | public function check_step_capabilities(): bool { |
| 167 | return current_user_can( 'activate_plugins' ); |
| 168 | } |
| 169 | } |
| 170 |