|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cerberus\AdminPluginsManager; |
| 4 | + |
| 5 | +class PluginUpdater |
| 6 | +{ |
| 7 | + private $file; |
| 8 | + |
| 9 | + private $plugin; |
| 10 | + |
| 11 | + private $basename; |
| 12 | + |
| 13 | + private $active; |
| 14 | + |
| 15 | + private $username; |
| 16 | + |
| 17 | + private $repository; |
| 18 | + |
| 19 | + private $authorize_token; |
| 20 | + |
| 21 | + private $github_response; |
| 22 | + |
| 23 | + public function __construct( $file, $repository, $token ) |
| 24 | + { |
| 25 | + $this->file = $file; |
| 26 | + $this->username = 'Trizelos'; |
| 27 | + $this->repository = $repository; |
| 28 | + $this->authorize_token = $token; |
| 29 | + |
| 30 | + $this->set_plugin_properties(); |
| 31 | + |
| 32 | + $this->initialize(); |
| 33 | + } |
| 34 | + |
| 35 | + private function set_plugin_properties(): void |
| 36 | + { |
| 37 | + if ( ! function_exists( 'get_plugin_data' ) ) { |
| 38 | + require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 39 | + } |
| 40 | + $this->plugin = get_plugin_data( $this->file ); |
| 41 | + $this->basename = plugin_basename( $this->file ); |
| 42 | + $this->active = is_plugin_active( $this->basename ); |
| 43 | + } |
| 44 | + |
| 45 | + private function initialize(): void |
| 46 | + { |
| 47 | + add_filter( 'site_transient_update_plugins', array( $this, 'modify_transient' ), 10, 1 ); |
| 48 | + add_filter( 'plugins_api', array( $this, 'plugin_popup' ), 10, 3 ); |
| 49 | + add_filter( 'upgrader_post_install', array( &$this, 'after_install' ), 10, 3 ); |
| 50 | + |
| 51 | + // Add Authorization Token to download_package |
| 52 | + add_filter( 'upgrader_pre_download', function () |
| 53 | + { |
| 54 | + add_filter( 'http_request_args', [ $this, 'download_package' ], 15, 2 ); |
| 55 | + |
| 56 | + return false; // upgrader_pre_download filter default return value. |
| 57 | + } ); |
| 58 | + } |
| 59 | + |
| 60 | + public function modify_transient( $transient ): mixed |
| 61 | + { |
| 62 | + if ( empty( $checked = $transient->checked ) ) { |
| 63 | + return $transient; |
| 64 | + } |
| 65 | + |
| 66 | + if ( ! array_key_exists( $this->basename, $checked ) ) { |
| 67 | + return $transient; |
| 68 | + } |
| 69 | + |
| 70 | + $this->get_repository_info(); // Get the repo info |
| 71 | + if ( ! isset( $this->github_response['tag_name'] ) ) { |
| 72 | + return $transient; |
| 73 | + } |
| 74 | + |
| 75 | + $out_of_date = version_compare( $this->github_response['tag_name'], $checked[ $this->basename ], 'gt' ); // Check if we're out of date |
| 76 | + |
| 77 | + if ( $out_of_date ) { |
| 78 | + |
| 79 | + $new_files = $this->github_response['zipball_url']; // Get the ZIP |
| 80 | + |
| 81 | + $slug = current( explode( '/', $this->basename ) ); // Create valid slug |
| 82 | + |
| 83 | + $plugin = array( // setup our plugin info |
| 84 | + 'url' => $this->plugin["PluginURI"], |
| 85 | + 'slug' => $slug, |
| 86 | + 'package' => $new_files, |
| 87 | + 'new_version' => $this->github_response['tag_name'] |
| 88 | + ); |
| 89 | + |
| 90 | + $transient->response[ $this->basename ] = (object) $plugin; // Return it in response |
| 91 | + } |
| 92 | + |
| 93 | + return $transient; // Return filtered transient |
| 94 | + } |
| 95 | + |
| 96 | + private function get_repository_info(): void |
| 97 | + { |
| 98 | + if ( is_null( $this->github_response ) ) { // Do we have a response? |
| 99 | + $args = array(); |
| 100 | + $request_uri = sprintf( 'https://api.github.com/repos/%s/%s/releases', $this->username, $this->repository ); // Build URI |
| 101 | + |
| 102 | + if ( $this->authorize_token ) { // Is there an access token? |
| 103 | + $args['headers']['Authorization'] = "bearer {$this->authorize_token}"; // Set the headers |
| 104 | + } |
| 105 | + |
| 106 | + $response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_uri, $args ) ), true ); // Get JSON and parse it |
| 107 | + |
| 108 | + if ( is_array( $response ) ) { // If it is an array |
| 109 | + $response = current( $response ); // Get the first item |
| 110 | + } |
| 111 | + |
| 112 | + $this->github_response = $response; // Set it to our property |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public function plugin_popup( $result, $action, $args ): mixed |
| 117 | + { |
| 118 | + if ( 'plugin_information' !== $action ) { |
| 119 | + return $result; |
| 120 | + } |
| 121 | + if ( $args->slug !== current( explode( '/', $this->basename ) ) ) { // And it's our slug |
| 122 | + return $result; |
| 123 | + } |
| 124 | + $this->get_repository_info(); // Get our repo info |
| 125 | + |
| 126 | + if ( isset( $this->github_response['tag_name'] ) ) { |
| 127 | + return $result; |
| 128 | + } |
| 129 | + |
| 130 | + // Set it to an array |
| 131 | + $plugin = array( |
| 132 | + 'name' => $this->plugin["Name"], |
| 133 | + 'slug' => $this->basename, |
| 134 | + 'requires' => '5.3', |
| 135 | +// 'tested' => '5.*', |
| 136 | +// 'rating' => '100.0', |
| 137 | +// 'num_ratings' => '1', |
| 138 | +// 'downloaded' => '1', |
| 139 | +// 'added' => '2016-01-05', |
| 140 | + 'version' => $this->github_response['tag_name'], |
| 141 | + 'author' => $this->plugin["AuthorName"], |
| 142 | + 'author_profile' => $this->plugin["AuthorURI"], |
| 143 | + 'last_updated' => $this->github_response['published_at'], |
| 144 | + 'homepage' => $this->plugin["PluginURI"], |
| 145 | + 'short_description' => $this->plugin["Description"], |
| 146 | + 'sections' => array( |
| 147 | + 'Updates' => $this->github_response['body'], |
| 148 | + 'Description' => $this->plugin["Description"], |
| 149 | + ), |
| 150 | + 'download_link' => $this->github_response['zipball_url'] |
| 151 | + ); |
| 152 | + |
| 153 | + return (object) $plugin; // Return the data |
| 154 | + } |
| 155 | + |
| 156 | + public function download_package( $args, $url ): mixed |
| 157 | + { |
| 158 | + |
| 159 | + if ( null !== $args['filename'] ) { |
| 160 | + if ( $this->authorize_token ) { |
| 161 | + $args = array_merge( $args, array( "headers" => array( "Authorization" => "token {$this->authorize_token}" ) ) ); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + remove_filter( 'http_request_args', [ $this, 'download_package' ] ); |
| 166 | + |
| 167 | + return $args; |
| 168 | + } |
| 169 | + |
| 170 | + public function after_install( $response, $hook_extra, $result ): mixed |
| 171 | + { |
| 172 | + global $wp_filesystem; // Get global FS object |
| 173 | + |
| 174 | + $install_directory = plugin_dir_path( $this->file ); // Our plugin directory |
| 175 | + $wp_filesystem->move( $result['destination'], $install_directory ); // Move files to the plugin dir |
| 176 | + $result['destination'] = $install_directory; // Set the destination for the rest of the stack |
| 177 | + |
| 178 | + if ( $this->active ) { // If it was active |
| 179 | + activate_plugin( $this->basename ); // Reactivate |
| 180 | + } |
| 181 | + |
| 182 | + $this->composer_dump_autoload(); |
| 183 | + |
| 184 | + return $result; |
| 185 | + } |
| 186 | + |
| 187 | + private function composer_dump_autoload(): void |
| 188 | + { |
| 189 | + $cwd = getcwd(); |
| 190 | + $path = plugin_dir_path( $this->file ); |
| 191 | + |
| 192 | + exec( "cd $path && composer dump-autoload -o" ); |
| 193 | + exec( "cd $cwd" ); |
| 194 | + |
| 195 | + prew( 'updated plugin: ' . $this->file ); |
| 196 | + } |
| 197 | + |
| 198 | + public function cerberus_core_repos_settings( array $responses ): array |
| 199 | + { |
| 200 | + $this->get_repository_info(); |
| 201 | + $responses[ $this->basename ]['response'] = $this->github_response; |
| 202 | + $responses[ $this->basename ]['plugin_data'] = get_plugin_data( $this->file ); |
| 203 | + |
| 204 | + return $responses; |
| 205 | + } |
| 206 | +} |
| 207 | + |
0 commit comments