Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 53 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Marketplace_Command_Helper | |
0.00% |
0 / 53 |
|
0.00% |
0 / 7 |
210 | |
0.00% |
0 / 1 |
| generate_dependency_install_commands | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| get_installed_plugins_names_command | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_installed_themes_names_command | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| generate_plugin_install_commands | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 | |||
| generate_theme_install_command | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| generate_verify_plugin_installation_commands | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| generate_verify_theme_installation_command | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Helper class for marketplace commands. |
| 4 | * |
| 5 | * @since 5.5.0 |
| 6 | * @package WPCOM_Marketplace |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Helper class for generating WP CLI commands required to install marketplace product. |
| 11 | */ |
| 12 | class Marketplace_Command_Helper { |
| 13 | |
| 14 | /** |
| 15 | * Generate the commands to install the dependencies of a product. |
| 16 | * |
| 17 | * @param array<string> $plugin_dependencies List of plugins the product depends on. |
| 18 | * @param array<string> $theme_dependencies List of themes the product depends on. |
| 19 | * |
| 20 | * @return string[] |
| 21 | */ |
| 22 | public function generate_dependency_install_commands( array $plugin_dependencies, array $theme_dependencies ) { |
| 23 | /** |
| 24 | * This can be extended in the future to support advanced dependency trees or multiple types of products. |
| 25 | * |
| 26 | * We first try to symlink it, if it fails, it reverts to installing the plugin. |
| 27 | */ |
| 28 | $dependency_command_template = '--skip-themes --skip-plugins atomic %2$s use-managed %1$s --remove-existing --activate || --skip-themes --skip-plugins %2$s install %1$s --activate --force'; |
| 29 | $plugin_dependency_commands = \array_map( fn( $plugin ) => \sprintf( $dependency_command_template, $plugin, 'plugin' ), $plugin_dependencies ); |
| 30 | $theme_dependency_commands = \array_map( fn( $theme ) => \sprintf( $dependency_command_template, $theme, 'theme' ), $theme_dependencies ); |
| 31 | |
| 32 | return array_merge( $plugin_dependency_commands, $theme_dependency_commands ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the list of installed plugins. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | public function get_installed_plugins_names_command(): string { |
| 41 | return '--skip-themes --skip-plugins plugin list --field=name'; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the list of installed themes. |
| 46 | * |
| 47 | * @return string |
| 48 | */ |
| 49 | public function get_installed_themes_names_command(): string { |
| 50 | return '--skip-themes --skip-plugins theme list --field=name'; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Generate the commands to install a plugin. |
| 55 | * |
| 56 | * @param string $software_slug_or_url The plugin slug or URL to install. |
| 57 | * @param bool $is_managed Whether to use the managed version of the plugin or not. |
| 58 | * @param array $skip_plugins List of plugins to skip when installing the plugin. |
| 59 | * @param array $skip_themes List of themes to skip when installing the plugin. |
| 60 | * |
| 61 | * @return string[] |
| 62 | */ |
| 63 | public function generate_plugin_install_commands( string $software_slug_or_url, bool $is_managed, array $skip_plugins, array $skip_themes ) { |
| 64 | $skip_plugins = implode( ',', $skip_plugins ); |
| 65 | $skip_themes = implode( ',', $skip_themes ); |
| 66 | |
| 67 | $skip_plugins_parameter = '--skip-plugins'; |
| 68 | if ( ! empty( $skip_plugins ) ) { |
| 69 | $skip_plugins_parameter .= '="' . $skip_plugins . '" '; |
| 70 | } |
| 71 | |
| 72 | $skip_themes_parameter = '--skip-themes'; |
| 73 | if ( ! empty( $skip_themes ) ) { |
| 74 | $skip_themes_parameter .= '="' . $skip_themes . '" '; |
| 75 | } |
| 76 | |
| 77 | $plugin_install_commands = array( |
| 78 | 'add_managed_plugin_command' => ' atomic plugin use-managed %1$s --remove-existing', |
| 79 | 'activate_plugin_command' => ' plugin activate %1$s', |
| 80 | ); |
| 81 | |
| 82 | if ( ! $is_managed ) { |
| 83 | $plugin_install_commands = array( |
| 84 | 'add_remote_plugin_command' => ' plugin install "%1$s" --activate --force', |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | return array( |
| 89 | ...array_map( |
| 90 | fn( $command ) => sprintf( $skip_plugins_parameter . $skip_themes_parameter . $command, $software_slug_or_url ), |
| 91 | array_values( $plugin_install_commands ) |
| 92 | ), |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Generate the command to install a theme. |
| 98 | * |
| 99 | * @param string $theme_slug The theme slug to install. |
| 100 | * @param bool $is_managed Whether to use the managed version of the theme or not. |
| 101 | * @param array $skip_plugins List of plugins to skip when installing the theme. |
| 102 | * @param array $skip_themes List of themes to skip when installing the theme. |
| 103 | * |
| 104 | * @return string |
| 105 | */ |
| 106 | public function generate_theme_install_command( string $theme_slug, bool $is_managed, array $skip_plugins, array $skip_themes ) { |
| 107 | $skip_plugins = implode( ',', $skip_plugins ); |
| 108 | $skip_themes = implode( ',', $skip_themes ); |
| 109 | |
| 110 | $skip_plugins_parameter = '--skip-plugins'; |
| 111 | if ( ! empty( $skip_plugins ) ) { |
| 112 | $skip_plugins_parameter .= '="' . $skip_plugins . '" '; |
| 113 | } |
| 114 | |
| 115 | $skip_themes_parameter = '--skip-themes'; |
| 116 | if ( ! empty( $skip_themes ) ) { |
| 117 | $skip_themes_parameter .= '="' . $skip_themes . '" '; |
| 118 | } |
| 119 | |
| 120 | if ( ! $is_managed ) { |
| 121 | $command = sprintf( $skip_plugins_parameter . $skip_themes_parameter . ' theme install "%s" --force', $theme_slug ); |
| 122 | } else { |
| 123 | $command = sprintf( $skip_plugins_parameter . $skip_themes_parameter . ' atomic theme use-managed %s --remove-existing', $theme_slug ); |
| 124 | } |
| 125 | |
| 126 | return $command; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Generate the commands to verify the installation of the plugins. |
| 131 | * |
| 132 | * @param array $expected_plugins A list of plugins that will be checked. |
| 133 | * @param array $expected_themes A list of themes that will be checked. |
| 134 | * |
| 135 | * @return array |
| 136 | */ |
| 137 | public function generate_verify_plugin_installation_commands( array $expected_plugins, array $expected_themes ) { |
| 138 | $expected_plugins = \array_filter( $expected_plugins ); |
| 139 | |
| 140 | if ( empty( $expected_plugins ) ) { |
| 141 | return array(); |
| 142 | } |
| 143 | |
| 144 | $plugin_commands = \array_map( |
| 145 | fn( $plugin_slug ) => '--skip-themes --skip-plugins plugin get ' . $plugin_slug . ' --field=status', |
| 146 | $expected_plugins |
| 147 | ); |
| 148 | |
| 149 | $theme_commands = \array_map( |
| 150 | fn( $theme_slug ) => '--skip-themes --skip-plugins theme get ' . $theme_slug . ' --field=status', |
| 151 | $expected_themes |
| 152 | ); |
| 153 | |
| 154 | return array( ...$plugin_commands, ...$theme_commands ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Generate the command to verify the theme installation. |
| 159 | * |
| 160 | * @param string $expected_theme The theme that will be checked. |
| 161 | * |
| 162 | * @return string |
| 163 | */ |
| 164 | public function generate_verify_theme_installation_command( string $expected_theme ) { |
| 165 | return '--skip-themes --skip-plugins theme get ' . $expected_theme . ' --field=status'; |
| 166 | } |
| 167 | } |