@@ -389,6 +389,11 @@ protected function install_from_php_file( $url, $assoc_args ) {
389389 // Ensure plugin directory exists.
390390 if ( ! is_dir ( WP_PLUGIN_DIR ) ) {
391391 wp_mkdir_p ( WP_PLUGIN_DIR );
392+
393+ // Verify that the plugin directory was successfully created.
394+ if ( ! is_dir ( WP_PLUGIN_DIR ) ) {
395+ return new WP_Error ( 'invalid_path ' , 'Unable to create plugin directory. ' );
396+ }
392397 }
393398
394399 // Validate the destination stays within the plugin directory (prevent directory traversal).
@@ -1163,7 +1168,8 @@ private function build_rate_limiting_error_message( $decoded_body ) {
11631168 */
11641169 protected function get_gist_id_from_url ( $ url ) {
11651170 // Match gist.github.com URLs but not gist.githubusercontent.com (raw URLs)
1166- if ( preg_match ( '#^https?://gist\.github\.com/[^/]+/([a-f0-9]+)/?$#i ' , $ url , $ matches ) ) {
1171+ // Gist IDs are hexadecimal strings that can contain both lowercase and uppercase
1172+ if ( preg_match ( '#^https?://gist\.github\.com/[^/]+/([a-fA-F0-9]+)/?$# ' , $ url , $ matches ) ) {
11671173 return $ matches [1 ];
11681174 }
11691175 return null ;
@@ -1191,10 +1197,27 @@ protected function get_raw_url_from_gist( $gist_id ) {
11911197 $ body = \wp_remote_retrieve_body ( $ response );
11921198 $ decoded_body = json_decode ( $ body );
11931199
1200+ // Handle common HTTP error codes with specific error messages
1201+ if ( 401 === $ response_code ) {
1202+ return new \WP_Error (
1203+ 'github_unauthorized ' ,
1204+ 'Unauthorized: Invalid or missing GitHub token. ' ,
1205+ [ 'status ' => 401 ]
1206+ );
1207+ }
1208+
11941209 if ( 403 === $ response_code ) {
1210+ // Check if decoded_body is valid before using it
1211+ if ( null !== $ decoded_body && is_object ( $ decoded_body ) ) {
1212+ return new \WP_Error (
1213+ 'github_rate_limited ' ,
1214+ $ this ->build_rate_limiting_error_message ( $ decoded_body ),
1215+ [ 'status ' => 403 ]
1216+ );
1217+ }
11951218 return new \WP_Error (
1196- 'api_rate_limit ' ,
1197- $ this -> build_rate_limiting_error_message ( $ decoded_body ) ,
1219+ 'github_forbidden ' ,
1220+ ' Access forbidden. This may be due to rate limiting or insufficient permissions. ' ,
11981221 [ 'status ' => 403 ]
11991222 );
12001223 }
@@ -1207,11 +1230,36 @@ protected function get_raw_url_from_gist( $gist_id ) {
12071230 );
12081231 }
12091232
1233+ if ( 500 === $ response_code ) {
1234+ return new \WP_Error (
1235+ 'github_server_error ' ,
1236+ 'GitHub server error. Please try again later. ' ,
1237+ [ 'status ' => 500 ]
1238+ );
1239+ }
1240+
1241+ if ( 503 === $ response_code ) {
1242+ return new \WP_Error (
1243+ 'github_unavailable ' ,
1244+ 'GitHub service is temporarily unavailable. Please try again later. ' ,
1245+ [ 'status ' => 503 ]
1246+ );
1247+ }
1248+
1249+ // Check for other non-2xx status codes
1250+ if ( $ response_code < 200 || $ response_code >= 300 ) {
1251+ return new \WP_Error (
1252+ 'github_api_error ' ,
1253+ sprintf ( 'GitHub API returned unexpected status code: %d ' , $ response_code ),
1254+ [ 'status ' => $ response_code ]
1255+ );
1256+ }
1257+
12101258 if ( null === $ decoded_body || ! is_object ( $ decoded_body ) || ! isset ( $ decoded_body ->files ) ) {
12111259 return new \WP_Error (
12121260 'invalid_gist_api_response ' ,
12131261 'Invalid response from GitHub Gist API. ' ,
1214- [ 'status ' => 500 ]
1262+ [ 'status ' => $ response_code ]
12151263 );
12161264 }
12171265
0 commit comments