diff --git a/README.md b/README.md new file mode 100644 index 0000000..f996a1d --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# cerberus-plugins-manager diff --git a/admin/src/SettingsPage.php b/admin/src/SettingsPage.php index 8aa87e4..4da4026 100644 --- a/admin/src/SettingsPage.php +++ b/admin/src/SettingsPage.php @@ -6,71 +6,71 @@ class SettingsPage { - private $file_name; - - public function __construct(string $file_name) - { - $this->file_name = $file_name; - add_action('admin_menu', [$this, 'admin_menu'], 1000); - add_action('admin_post_cpm_settings', [$this, 'admin_post_cpm_settings']); - add_action('admin_post_nopriv_cpm_settings', [$this, 'admin_post_cpm_settings']); - - add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_scripts']); - - add_action('admin_post_download_and_extract_repo', [$this, 'download_and_extract_repo']); - add_action('admin_post_nopriv_download_and_extract_repo', [$this, 'download_and_extract_repo']); - } - - public final function admin_enqueue_scripts(): void - { - wp_enqueue_style('cpm-admin', plugin_dir_url($this->file_name) . 'admin/assets/css/admin.css'); - } - - public final function admin_menu(): void - { - $title = __('Cerberus Plugins Manager Settings', 'cerberus-plugins-manager'); - add_submenu_page('woocommerce', $title, $title, 'administrator', 'cerberus_plugins_manager', array( - $this, - 'settingsPage' - )); - } - - public final function settingsPage(): void - { - $repoKey = get_option('repo-key', ''); - $title = __('Cerberus Plugins Manager Settings', 'cerberus-plugins-manager'); - - ?> + private $file_name; + + public function __construct( string $file_name ) + { + $this->file_name = $file_name; + add_action( 'admin_menu', [ $this, 'admin_menu' ], 1000 ); + add_action( 'admin_post_cpm_settings', [ $this, 'admin_post_cpm_settings' ] ); + add_action( 'admin_post_nopriv_cpm_settings', [ $this, 'admin_post_cpm_settings' ] ); + + add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); + + add_action( 'admin_post_download_and_extract_repo', [ $this, 'download_and_extract_repo' ] ); + add_action( 'admin_post_nopriv_download_and_extract_repo', [ $this, 'download_and_extract_repo' ] ); + } + + public final function admin_enqueue_scripts(): void + { + wp_enqueue_style( 'cpm-admin', plugin_dir_url( $this->file_name ) . 'admin/assets/css/admin.css' ); + } + + public final function admin_menu(): void + { + $title = __( 'Cerberus Plugins Manager Settings', 'cerberus-plugins-manager' ); + add_submenu_page( 'woocommerce', $title, $title, 'administrator', 'cerberus_plugins_manager', array( + $this, + 'settingsPage' + ) ); + } + + public final function settingsPage(): void + { + $repoKey = get_option( 'repo-key', '' ); + $title = __( 'Cerberus Plugins Manager Settings', 'cerberus-plugins-manager' ); + + ?>

-
- Your repo key is set but hidden away.'; - } ?> + Your repo key is set but hidden away.'; + } ?>
- getRepos('Trizelos', $repoKey); + $repoKey = get_option( 'repo-key' ); + $repos = $this->getRepos( 'Trizelos', $repoKey ); - $responses = apply_filters('cerberus-core-repos-settings', $repos); + $responses = apply_filters( 'cerberus-core-repos-settings', $repos ); - ?> + ?>
-
@@ -85,195 +85,201 @@ public final function settingsPage(): void - $data) { - if ($name == 'cerberus-plugins-manager/cerberus-plugins-manager.php') { - continue; - } - $response = $data['response']; - $plugin_data = $data['plugin_data']; - $installed = !empty($response); - - $out_of_date = false; - if (!empty($response['tag_name'])) { - $out_of_date = version_compare($response['tag_name'], $plugin_data['Version'], 'gt'); - } - ?> - + $data ) { + if ( $name == 'cerberus-plugins-manager/cerberus-plugins-manager.php' ) { + continue; + } + $response = $data['response']; + $plugin_data = $data['plugin_data']; + $state = empty( $response ) ? 'install' : 'update'; + + if ( ! empty( $response['tag_name'] ) ) { + $state = version_compare( $response['tag_name'], $plugin_data['Version'], 'gt' ) ? 'out-of-date' : $state; + } + ?> + - + - + - + - +
- getRepoResponse($repository); + $repository = $_POST['repo-to-install']; - if (!isset($response["zipball_url"])) { - wp_redirect(wp_get_referer()); - return; - } + $response = $this->getRepoResponse( $repository ); - $zip_url = $response["zipball_url"]; - $destination_path = WP_PLUGIN_DIR . '/' . $repository . ".zip"; + if ( ! isset( $response["zipball_url"] ) ) { + wp_redirect( wp_get_referer() ); - if (!$this->downloadZipFile($zip_url, $destination_path)) { - wp_redirect(wp_get_referer()); - return; - } + return; + } + $zip_url = $response["zipball_url"]; + $destination_path = WP_PLUGIN_DIR . '/' . $repository . ".zip"; - $zip = new ZipArchive(); - if ($zip->open($destination_path) === true) { - $pluginDestination = WP_PLUGIN_DIR; + if ( ! $this->downloadZipFile( $zip_url, $destination_path ) ) { + wp_redirect( wp_get_referer() ); - $name = $zip->getNameIndex(0); + return; + } - // extract the plugin - $success = $zip->extractTo($pluginDestination); - $zip->close(); - $pluginRepoPath = $pluginDestination . '/' . $repository; - $pluginRepoGeneratedName = $pluginDestination . '/' . $name; + $zip = new ZipArchive(); + if ( $zip->open( $destination_path ) === true ) { + $pluginDestination = WP_PLUGIN_DIR; - // if old repo data exists delete it - if ($success && is_dir($pluginRepoPath)) { - $deletedOldRepo = $this->delTree($pluginRepoPath); - } + $name = $zip->getNameIndex( 0 ); -// 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); - } - wp_redirect(wp_get_referer()); - } - - private function getRepoResponse(string $repository): mixed - { - $username = 'Trizelos'; - $authorize_token = get_option('repo-key'); - $args = array(); - $request_uri = sprintf('https://api.github.com/repos/%s/%s/releases', $username, $repository); // Build URI - - if ($authorize_token) { // Is there an access token? - $args['headers']['Authorization'] = "bearer {$authorize_token}"; // Set the headers - } - - $response = json_decode(wp_remote_retrieve_body(wp_remote_get($request_uri, $args)), true); // Get JSON and parse it - - if (is_array($response)) { // If it is an array - $response = current($response); // Get the first item - } - - return $response; - } - - 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); + // extract the plugin + $success = $zip->extractTo( $pluginDestination ); + $zip->close(); - $result = curl_exec($ch); - $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code - curl_close($ch); + $pluginRepoPath = $pluginDestination . '/' . $repository; + $pluginRepoGeneratedName = $pluginDestination . '/' . $name; - file_put_contents($filepath, $result); + // if old repo data exists delete it + if ( $success && is_dir( $pluginRepoPath ) ) { + $deletedOldRepo = $this->delTree( $pluginRepoPath ); + } - 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"); - - prew('updated plugin: ' . $filePath); - } - - public final function admin_post_cpm_settings(): void - { - $data = $_POST; - if (!empty($data['repo-key'])) { - update_option('repo-key', $data['repo-key']); - } - - wp_redirect(wp_get_referer()); - } +// 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 ); + } + wp_redirect( wp_get_referer() ); + } + + private function getRepoResponse( string $repository ): mixed + { + $username = 'Trizelos'; + $authorize_token = get_option( 'repo-key' ); + $args = array(); + $request_uri = sprintf( 'https://api.github.com/repos/%s/%s/releases', $username, $repository ); // Build URI + + if ( $authorize_token ) { // Is there an access token? + $args['headers']['Authorization'] = "bearer {$authorize_token}"; // Set the headers + } + + $response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_uri, $args ) ), true ); // Get JSON and parse it + + if ( is_array( $response ) ) { // If it is an array + $response = current( $response ); // Get the first item + } + + return $response; + } + + 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" ); + + prew( 'updated plugin: ' . $filePath ); + } + + public final function admin_post_cpm_settings(): void + { + $data = $_POST; + if ( ! empty( $data['repo-key'] ) ) { + update_option( 'repo-key', $data['repo-key'] ); + } + + wp_redirect( wp_get_referer() ); + } } diff --git a/cerberus-plugins-manager.php b/cerberus-plugins-manager.php index 468153f..559a052 100644 --- a/cerberus-plugins-manager.php +++ b/cerberus-plugins-manager.php @@ -3,7 +3,7 @@ /** * Plugin Name: Cerberus Plugins Manager * Description: Manages the cerberus plugins - * Version: 1.0.3 + * Version: 1.0.4 * Requires at least: 5.7 * Author: Casey */ @@ -12,4 +12,4 @@ require_once "vendor/autoload.php"; -$settingsPage = new SettingsPage(__FILE__); +$settingsPage = new SettingsPage( __FILE__ );