PluginProbe
WooCommerce / 11.0.1
WooCommerce v11.0.1
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / packages / blueprint / src / ResourceStorages / OrgPluginResourceStorage.php
OrgPluginResourceStorage.php
87 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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