Skip to content

Commit 551fe3e

Browse files
Copilotswissspidy
andcommitted
Add support for GitHub Gist page URLs (gist.github.com)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 82f7819 commit 551fe3e

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

‎features/plugin-install.feature‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,35 @@ Feature: Install WordPress plugins
334334
Plugin installed successfully.
335335
"""
336336
And the wp-content/plugins/pwa-manifest-short-name.php file should exist
337+
338+
Scenario: Install plugin from a GitHub Gist page URL
339+
Given a WP install
340+
341+
When I run `wp plugin install https://gist.github.com/westonruter/dec7d190060732e29a09751ab99cc549 --activate`
342+
Then STDOUT should contain:
343+
"""
344+
Gist resolved to raw file URL.
345+
"""
346+
And STDOUT should contain:
347+
"""
348+
Installing
349+
"""
350+
And STDOUT should contain:
351+
"""
352+
Downloading plugin file from
353+
"""
354+
And STDOUT should contain:
355+
"""
356+
Plugin installed successfully.
357+
"""
358+
And STDOUT should contain:
359+
"""
360+
Activating
361+
"""
362+
And the wp-content/plugins/pwa-manifest-short-name.php file should exist
363+
364+
When I run `wp plugin list --name=pwa-manifest-short-name --field=status`
365+
Then STDOUT should be:
366+
"""
367+
active
368+
"""

‎src/WP_CLI/CommandWithUpgrade.php‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ public function install( $args, $assoc_args ) {
212212

213213
WP_CLI::log( 'Latest release resolved to ' . $version['name'] );
214214
}
215+
216+
// Check if it's a GitHub Gist page URL and convert to raw URL
217+
$gist_id = $this->get_gist_id_from_url( $slug );
218+
if ( $gist_id && 'plugin' === $this->item_type ) {
219+
$raw_url = $this->get_raw_url_from_gist( $gist_id );
220+
221+
if ( is_wp_error( $raw_url ) ) {
222+
WP_CLI::error( $raw_url->get_error_message() );
223+
}
224+
225+
WP_CLI::log( 'Gist resolved to raw file URL.' );
226+
$slug = $raw_url;
227+
}
215228
}
216229

217230
// Check if a URL to a remote or local PHP file has been specified (plugins only).
@@ -1140,4 +1153,75 @@ protected function get_github_repo_from_releases_url( $url ) {
11401153
private function build_rate_limiting_error_message( $decoded_body ) {
11411154
return $decoded_body->message . PHP_EOL . $decoded_body->documentation_url . PHP_EOL . 'In order to pass the token to WP-CLI, you need to use the GITHUB_TOKEN environment variable.';
11421155
}
1156+
1157+
/**
1158+
* Check if a URL is a GitHub Gist page URL (not the raw URL).
1159+
*
1160+
* @param string $url The URL to check.
1161+
* @return string|null The gist ID if it's a gist URL, null otherwise.
1162+
*/
1163+
protected function get_gist_id_from_url( $url ) {
1164+
// Match gist.github.com URLs but not gist.githubusercontent.com (raw URLs)
1165+
if ( preg_match( '#^https?://gist\.github\.com/[^/]+/([a-f0-9]+)/?$#i', $url, $matches ) ) {
1166+
return $matches[1];
1167+
}
1168+
return null;
1169+
}
1170+
1171+
/**
1172+
* Convert a GitHub Gist page URL to a raw PHP file URL.
1173+
*
1174+
* @param string $gist_id The gist ID.
1175+
* @return string|WP_Error The raw URL of the first PHP file in the gist, or WP_Error on failure.
1176+
*/
1177+
protected function get_raw_url_from_gist( $gist_id ) {
1178+
$api_url = 'https://api.github.com/gists/' . $gist_id;
1179+
$token = getenv( 'GITHUB_TOKEN' );
1180+
1181+
$request_arguments = $token ? [ 'headers' => [ 'Authorization' => 'Bearer ' . $token ] ] : [];
1182+
1183+
$response = \wp_remote_get( $api_url, $request_arguments );
1184+
1185+
if ( \is_wp_error( $response ) ) {
1186+
return $response;
1187+
}
1188+
1189+
$response_code = wp_remote_retrieve_response_code( $response );
1190+
$body = \wp_remote_retrieve_body( $response );
1191+
$decoded_body = json_decode( $body );
1192+
1193+
if ( 403 === $response_code ) {
1194+
return new \WP_Error(
1195+
403,
1196+
$this->build_rate_limiting_error_message( $decoded_body )
1197+
);
1198+
}
1199+
1200+
if ( 404 === $response_code ) {
1201+
return new \WP_Error( 404, 'Gist not found.' );
1202+
}
1203+
1204+
if ( null === $decoded_body || ! is_object( $decoded_body ) || ! isset( $decoded_body->files ) ) {
1205+
return new \WP_Error( 500, 'Invalid response from GitHub Gist API.' );
1206+
}
1207+
1208+
// Find PHP files in the gist
1209+
$php_files = [];
1210+
$files = (array) $decoded_body->files;
1211+
foreach ( $files as $filename => $file_data ) {
1212+
if ( is_object( $file_data ) && isset( $file_data->raw_url ) && pathinfo( $filename, PATHINFO_EXTENSION ) === 'php' ) {
1213+
$php_files[] = [
1214+
'name' => $filename,
1215+
'raw_url' => $file_data->raw_url,
1216+
];
1217+
}
1218+
}
1219+
1220+
if ( empty( $php_files ) ) {
1221+
return new \WP_Error( 'no_php_files', 'No PHP files found in the gist.' );
1222+
}
1223+
1224+
// Return the first PHP file found
1225+
return $php_files[0]['raw_url'];
1226+
}
11431227
}

0 commit comments

Comments
 (0)