LocalPluginResourceStorage.php
1 year ago
LocalThemeResourceStorage.php
1 year ago
OrgPluginResourceStorage.php
1 year ago
OrgThemeResourceStorage.php
1 year ago
ResourceStorage.php
1 year ago
OrgPluginResourceStorage.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Blueprint\ResourceStorages; |
| 4 | |
| 5 | use Automattic\WooCommerce\Blueprint\UseWPFunctions; |
| 6 | |
| 7 | /** |
| 8 | * Class OrgPluginResourceStorage |
| 9 | * |
| 10 | * This class handles the storage and downloading of plugins from wordpress.org. |
| 11 | * |
| 12 | * @package Automattic\WooCommerce\Blueprint\ResourceStorages |
| 13 | */ |
| 14 | class OrgPluginResourceStorage implements ResourceStorage { |
| 15 | use UseWPFunctions; |
| 16 | |
| 17 | /** |
| 18 | * Download the plugin from wordpress.org |
| 19 | * |
| 20 | * @param string $slug The slug of the plugin to be downloaded. |
| 21 | * |
| 22 | * @return string|false The path to the downloaded plugin file, or false on failure. |
| 23 | */ |
| 24 | public function download( $slug ): ?string { |
| 25 | $download_link = $this->get_download_link( $slug ); |
| 26 | |
| 27 | if ( ! $download_link ) { |
| 28 | return false; |
| 29 | } |
| 30 | $result = $this->download_url( $download_link ); |
| 31 | |
| 32 | if ( is_wp_error( $result ) ) { |
| 33 | return false; |
| 34 | } |
| 35 | return $result; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Download the file from the given URL. |
| 40 | * |
| 41 | * @param string $url The URL to download the file from. |
| 42 | * |
| 43 | * @return string|WP_Error The path to the downloaded file, or WP_Error on failure. |
| 44 | */ |
| 45 | protected function download_url( $url ) { |
| 46 | return $this->wp_download_url( $url ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get the download link for a plugin from wordpress.org. |
| 51 | * |
| 52 | * @param string $slug The slug of the plugin. |
| 53 | * |
| 54 | * @return string|null The download link, or null if not found. |
| 55 | */ |
| 56 | protected function get_download_link( $slug ): ?string { |
| 57 | $info = $this->wp_plugins_api( |
| 58 | 'plugin_information', |
| 59 | array( |
| 60 | 'slug' => $slug, |
| 61 | 'fields' => array( |
| 62 | 'sections' => false, |
| 63 | ), |
| 64 | ) |
| 65 | ); |
| 66 | |
| 67 | if ( is_wp_error( $info ) ) { |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | if ( is_object( $info ) && isset( $info->download_link ) ) { |
| 72 | return $info->download_link; |
| 73 | } |
| 74 | |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the supported resource type. |
| 80 | * |
| 81 | * @return string The supported resource type. |
| 82 | */ |
| 83 | public function get_supported_resource(): string { |
| 84 | return 'wordpress.org/plugins'; |
| 85 | } |
| 86 | } |
| 87 |