Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up the code #16

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 85 additions & 85 deletions admin/src/RepoInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,103 +6,103 @@

class RepoInstaller
{
private RepoManager $repoManager;
private RepoManager $repoManager;

public function __construct()
{
$this->repoManager = new RepoManager();
}
public function __construct()
{
$this->repoManager = new RepoManager();
}

public function downloadExtractRepo( string $repository ): void
{
$response = $this->repoManager->getRepoInfoByRepoName( $repository );
public function downloadExtractRepo(string $repository): void
{
$response = $this->repoManager->getRepoInfoByRepoName($repository);

if ( ! isset( $response['zipball_url'] ) ) {
wp_redirect( wp_get_referer() );
if (!isset($response['zipball_url'])) {
wp_redirect(wp_get_referer());

return;
}
return;
}

$zip_url = $response['zipball_url'];
$destination_path = WP_PLUGIN_DIR . '/' . $repository . '.zip';
$zip_url = $response['zipball_url'];
$destination_path = WP_PLUGIN_DIR . '/' . $repository . '.zip';

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

return;
}
return;
}

$zip = new ZipArchive();
if ( $zip->open( $destination_path ) === true ) {
$pluginDestination = WP_PLUGIN_DIR;
$zip = new ZipArchive();
if ($zip->open($destination_path) === true) {
$pluginDestination = WP_PLUGIN_DIR;

$name = $zip->getNameIndex( 0 );
$name = $zip->getNameIndex(0);

// extract the plugin
$success = $zip->extractTo( $pluginDestination );
$zip->close();
// extract the plugin
$success = $zip->extractTo($pluginDestination);
$zip->close();

$pluginRepoPath = $pluginDestination . '/' . $repository;
$pluginRepoGeneratedName = $pluginDestination . '/' . $name;
$pluginRepoPath = $pluginDestination . '/' . $repository;
$pluginRepoGeneratedName = $pluginDestination . '/' . $name;

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

// rename the plugin to the correct name
if ( is_dir( $pluginRepoGeneratedName ) ) {
rename( $pluginRepoGeneratedName, $pluginRepoPath );
}

// removes the zip file
unlink( $destination_path );

// generate autoload files
$this->composer_dump_autoload( $pluginRepoPath );
}
}


private function downloadZipFile( $url, $filepath ): bool
{
$token = get_option( 'repo-key' );

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
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' );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $token
) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );

$result = curl_exec( $ch );
$status_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); //get status code
curl_close( $ch );

file_put_contents( $filepath, $result );

return ( filesize( $filepath ) > 0 ) ? true : false;
}

private function delTree( $dir ): bool
{
$files = array_diff( scandir( $dir ), array( '.', '..' ) );
foreach ( $files as $file ) {
( is_dir( "$dir/$file" ) ) ? $this->delTree( "$dir/$file" ) : unlink( "$dir/$file" );
}

return rmdir( $dir );

}

private function composer_dump_autoload( string $filePath ): void
{
exec( "cd $filePath && composer dump-autoload -o" );
}
if (is_dir($pluginRepoGeneratedName)) {
rename($pluginRepoGeneratedName, $pluginRepoPath);
}

// removes the zip file
unlink($destination_path);

// generate autoload files
$this->composer_dump_autoload($pluginRepoPath);
}
}


private function downloadZipFile($url, $filepath): bool
{
$token = get_option('repo-key');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
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');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $token
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close($ch);

file_put_contents($filepath, $result);

return filesize($filepath) > 0;
}

private function delTree($dir): bool
{
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->delTree("$dir/$file") : unlink("$dir/$file");
}

return rmdir($dir);

}

private function composer_dump_autoload(string $filePath): void
{
exec("cd $filePath && composer dump-autoload -o");
}
}
7 changes: 3 additions & 4 deletions admin/src/RepoManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,14 @@ private function getRepos(): array

private function getResponse(string $request_uri): mixed
{
$args = array();
$args = [];

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

$response = json_decode(wp_remote_retrieve_body(wp_remote_get($request_uri, $args)), true); // Get JSON and parse it

return $response;
// Get JSON and parse it
return json_decode(wp_remote_retrieve_body(wp_remote_get($request_uri, $args)), true);
}

public function getRepoInfoByRepoName(string $repoName): array|bool
Expand Down
2 changes: 1 addition & 1 deletion cerberus-plugins-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Plugin Name: Cerberus Plugins Manager
* Description: Manages the cerberus plugins
* Version: 1.1.1
* Version: 1.1.2
* Requires at least: 5.7
* Author: Casey
*/
Expand Down