Skip to content

Commit a0edecd

Browse files
authored
refactor: clean up the code (#16)
1 parent 3083dff commit a0edecd

File tree

3 files changed

+89
-90
lines changed

3 files changed

+89
-90
lines changed

admin/src/RepoInstaller.php

Lines changed: 85 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,103 +6,103 @@
66

77
class RepoInstaller
88
{
9-
private RepoManager $repoManager;
9+
private RepoManager $repoManager;
1010

11-
public function __construct()
12-
{
13-
$this->repoManager = new RepoManager();
14-
}
11+
public function __construct()
12+
{
13+
$this->repoManager = new RepoManager();
14+
}
1515

16-
public function downloadExtractRepo( string $repository ): void
17-
{
18-
$response = $this->repoManager->getRepoInfoByRepoName( $repository );
16+
public function downloadExtractRepo(string $repository): void
17+
{
18+
$response = $this->repoManager->getRepoInfoByRepoName($repository);
1919

20-
if ( ! isset( $response['zipball_url'] ) ) {
21-
wp_redirect( wp_get_referer() );
20+
if (!isset($response['zipball_url'])) {
21+
wp_redirect(wp_get_referer());
2222

23-
return;
24-
}
23+
return;
24+
}
2525

26-
$zip_url = $response['zipball_url'];
27-
$destination_path = WP_PLUGIN_DIR . '/' . $repository . '.zip';
26+
$zip_url = $response['zipball_url'];
27+
$destination_path = WP_PLUGIN_DIR . '/' . $repository . '.zip';
2828

29-
if ( ! $this->downloadZipFile( $zip_url, $destination_path ) ) {
30-
wp_redirect( wp_get_referer() );
29+
if (!$this->downloadZipFile($zip_url, $destination_path)) {
30+
wp_redirect(wp_get_referer());
3131

32-
return;
33-
}
32+
return;
33+
}
3434

35-
$zip = new ZipArchive();
36-
if ( $zip->open( $destination_path ) === true ) {
37-
$pluginDestination = WP_PLUGIN_DIR;
35+
$zip = new ZipArchive();
36+
if ($zip->open($destination_path) === true) {
37+
$pluginDestination = WP_PLUGIN_DIR;
3838

39-
$name = $zip->getNameIndex( 0 );
39+
$name = $zip->getNameIndex(0);
4040

41-
// extract the plugin
42-
$success = $zip->extractTo( $pluginDestination );
43-
$zip->close();
41+
// extract the plugin
42+
$success = $zip->extractTo($pluginDestination);
43+
$zip->close();
4444

45-
$pluginRepoPath = $pluginDestination . '/' . $repository;
46-
$pluginRepoGeneratedName = $pluginDestination . '/' . $name;
45+
$pluginRepoPath = $pluginDestination . '/' . $repository;
46+
$pluginRepoGeneratedName = $pluginDestination . '/' . $name;
4747

48-
// if old repo data exists delete it
49-
if ( $success && is_dir( $pluginRepoPath ) ) {
50-
$deletedOldRepo = $this->delTree( $pluginRepoPath );
51-
}
48+
// if old repo data exists delete it
49+
if ($success && is_dir($pluginRepoPath)) {
50+
$deletedOldRepo = $this->delTree($pluginRepoPath);
51+
}
5252

5353
// rename the plugin to the correct name
54-
if ( is_dir( $pluginRepoGeneratedName ) ) {
55-
rename( $pluginRepoGeneratedName, $pluginRepoPath );
56-
}
57-
58-
// removes the zip file
59-
unlink( $destination_path );
60-
61-
// generate autoload files
62-
$this->composer_dump_autoload( $pluginRepoPath );
63-
}
64-
}
65-
66-
67-
private function downloadZipFile( $url, $filepath ): bool
68-
{
69-
$token = get_option( 'repo-key' );
70-
71-
$ch = curl_init( $url );
72-
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
73-
curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763' );
74-
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
75-
'Content-Type: application/json',
76-
'Authorization: Bearer ' . $token
77-
) );
78-
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
79-
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
80-
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
81-
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
82-
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
83-
84-
$result = curl_exec( $ch );
85-
$status_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); //get status code
86-
curl_close( $ch );
87-
88-
file_put_contents( $filepath, $result );
89-
90-
return ( filesize( $filepath ) > 0 ) ? true : false;
91-
}
92-
93-
private function delTree( $dir ): bool
94-
{
95-
$files = array_diff( scandir( $dir ), array( '.', '..' ) );
96-
foreach ( $files as $file ) {
97-
( is_dir( "$dir/$file" ) ) ? $this->delTree( "$dir/$file" ) : unlink( "$dir/$file" );
98-
}
99-
100-
return rmdir( $dir );
101-
102-
}
103-
104-
private function composer_dump_autoload( string $filePath ): void
105-
{
106-
exec( "cd $filePath && composer dump-autoload -o" );
107-
}
54+
if (is_dir($pluginRepoGeneratedName)) {
55+
rename($pluginRepoGeneratedName, $pluginRepoPath);
56+
}
57+
58+
// removes the zip file
59+
unlink($destination_path);
60+
61+
// generate autoload files
62+
$this->composer_dump_autoload($pluginRepoPath);
63+
}
64+
}
65+
66+
67+
private function downloadZipFile($url, $filepath): bool
68+
{
69+
$token = get_option('repo-key');
70+
71+
$ch = curl_init($url);
72+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
73+
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763');
74+
curl_setopt($ch, CURLOPT_HTTPHEADER, [
75+
'Content-Type: application/json',
76+
'Authorization: Bearer ' . $token
77+
]);
78+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
79+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
80+
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
81+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
82+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
83+
84+
$result = curl_exec($ch);
85+
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
86+
curl_close($ch);
87+
88+
file_put_contents($filepath, $result);
89+
90+
return filesize($filepath) > 0;
91+
}
92+
93+
private function delTree($dir): bool
94+
{
95+
$files = array_diff(scandir($dir), ['.', '..']);
96+
foreach ($files as $file) {
97+
(is_dir("$dir/$file")) ? $this->delTree("$dir/$file") : unlink("$dir/$file");
98+
}
99+
100+
return rmdir($dir);
101+
102+
}
103+
104+
private function composer_dump_autoload(string $filePath): void
105+
{
106+
exec("cd $filePath && composer dump-autoload -o");
107+
}
108108
}

admin/src/RepoManager.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,14 @@ private function getRepos(): array
5757

5858
private function getResponse(string $request_uri): mixed
5959
{
60-
$args = array();
60+
$args = [];
6161

6262
if ($this->authorizeToken) { // Is there an access token?
6363
$args['headers']['Authorization'] = "bearer {$this->authorizeToken}"; // Set the headers
6464
}
6565

66-
$response = json_decode(wp_remote_retrieve_body(wp_remote_get($request_uri, $args)), true); // Get JSON and parse it
67-
68-
return $response;
66+
// Get JSON and parse it
67+
return json_decode(wp_remote_retrieve_body(wp_remote_get($request_uri, $args)), true);
6968
}
7069

7170
public function getRepoInfoByRepoName(string $repoName): array|bool

cerberus-plugins-manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Plugin Name: Cerberus Plugins Manager
55
* Description: Manages the cerberus plugins
6-
* Version: 1.1.1
6+
* Version: 1.1.2
77
* Requires at least: 5.7
88
* Author: Casey
99
*/

0 commit comments

Comments
 (0)