commit be275ebd28b732d0340c3c2ee93ae10b73b288ba Author: Pau Capó Date: Mon Mar 13 21:50:57 2017 +0100 first commit diff --git a/pau-updater.php b/pau-updater.php new file mode 100644 index 0000000..e3c3438 --- /dev/null +++ b/pau-updater.php @@ -0,0 +1,221 @@ +update_data = (array) get_option( 'ghu_update_data' ); + $active = (array) get_option( 'active_plugins' ); + + foreach ( $active as $slug ) { + $this->active_plugins[ $slug ] = true; + } + + // transient expiration + if ( ( $now - $last_checked ) > $check_interval ) { + $this->update_data = $this->get_gitea_updates(); + + update_option( 'ghu_update_data', $this->update_data ); + update_option( 'ghu_last_checked', $now ); + } + } + + function get_gitea_updates() { + $plugin_data = array(); + $plugins = get_plugins(); + foreach ( $plugins as $slug => $info ) { + if ( isset( $this->active_plugins[ $slug ] ) && ! empty( $info['Gitea URI'] ) && ! empty( $info['Gitea Host'] ) ) { + $temp = array( + 'plugin' => $slug, + 'slug' => trim( dirname( $slug ), '/' ), + 'file' => basename($slug), + 'name' => $info['Name'], + 'gitea_host' => $info['Gitea Host'], + 'gitea_repo' => $info['Gitea URI'], + 'description' => $info['Description'], + ); + + + // TODO + $access_token = '9ebc78834767d2ce0f95ec8a5ab597bc1fbc9ed6'; + + $url = $temp['gitea_host'].'api/v1/repos/'.$temp['gitea_repo'].'/raw/master/'.$temp['file'].'?access_token='.$access_token; + $request = wp_remote_get( $url ); + + // WP error or rate limit exceeded + if ( is_wp_error( $request ) || 200 != wp_remote_retrieve_response_code( $request ) ) { + break; + } + + $headers = $this->get_file_headers($request['body'], 'plugin'); + + $temp['new_version'] = $headers['Version']; + $temp['url'] = $temp['gitea_host'].$temp['gitea_repo']; + $temp['package'] = $temp['gitea_host'].'api/v1/repos/'.$temp['gitea_repo'].'/archive/master.zip?access_token='.$access_token; + + $plugin_data[ $slug ] = $temp; + } + } + + return $plugin_data; + } + + function get_file_headers( $contents, $type ) { + + $gitea_headers = array( + 'GiteaHost' => 'Gitea Host', + 'GiteaUri' => 'Gitea URI', + ); + + $default_plugin_headers = array( + 'Name' => 'Plugin Name', + 'PluginURI' => 'Plugin URI', + 'Version' => 'Version', + 'Description' => 'Description', + 'Author' => 'Author', + 'AuthorURI' => 'Author URI', + 'TextDomain' => 'Text Domain', + 'DomainPath' => 'Domain Path', + 'Network' => 'Network', + ); + + $default_theme_headers = array( + 'Name' => 'Theme Name', + 'ThemeURI' => 'Theme URI', + 'Description' => 'Description', + 'Author' => 'Author', + 'AuthorURI' => 'Author URI', + 'Version' => 'Version', + 'Template' => 'Template', + 'Status' => 'Status', + 'Tags' => 'Tags', + 'TextDomain' => 'Text Domain', + 'DomainPath' => 'Domain Path', + ); + + if ( false !== strpos( $type, 'plugin' ) ) { + $all_headers = $default_plugin_headers; + } + + if ( false !== strpos( $type, 'theme' ) ) { + $all_headers = $default_theme_headers; + } + + $file_data = str_replace( "\r", "\n", $contents ); + + $all_headers = array_merge( $gitea_headers, (array) $all_headers ); + $all_headers = array_unique( $all_headers ); + + foreach ( $all_headers as $field => $regex ) { + if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) { + $all_headers[ $field ] = _cleanup_header_comment( $match[1] ); + } else { + $all_headers[ $field ] = ''; + } + } + + $all_headers = array_filter( $all_headers, + function( $e ) use ( &$all_headers ) { + return ! empty( $e ); + } ); + + return $all_headers; + } + + function plugins_api( $default = false, $action, $args ) { + if ( 'plugin_information' == $action ) { + if ( isset( $this->update_data[ $args->slug ] ) ) { + $plugin = $this->update_data[ $args->slug ]; + + return (object) array( + 'name' => $plugin['name'], + 'slug' => $plugin['plugin'], + 'version' => $plugin['new_version'], + 'requires' => '4.4', + 'tested' => get_bloginfo( 'version' ), + 'last_updated' => date( 'Y-m-d' ), + 'sections' => array( + 'description' => $plugin['description'] + ) + ); + } + } + + return $default; + } + + + function set_update_data( $transient ) { + if ( empty( $transient->checked ) ) { + return $transient; + } + + foreach ( $this->update_data as $plugin => $info ) { + if ( isset( $this->active_plugins[ $plugin ] ) ) { + $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); + $version = $plugin_data['Version']; + + if ( version_compare( $version, $info['new_version'], '<' ) ) { + $transient->response[ $plugin ] = (object) $info; + } + } + } + + return $transient; + } + + + function upgrader_source_selection( $source, $remote_source, $upgrader, $hook_extra = null ) { + global $wp_filesystem; + + $plugin = isset( $hook_extra['plugin'] ) ? $hook_extra['plugin'] : false; + if ( isset( $this->update_data[ $plugin ] ) && $plugin ) { + $new_source = trailingslashit( $remote_source ) . dirname( $plugin ); + $wp_filesystem->move( $source, $new_source ); + return trailingslashit( $new_source ); + } + + return $source; + } + + function extra_plugin_headers( $headers ) { + $headers[] = 'Gitea URI'; + $headers[] = 'Gitea Host'; + return $headers; + } + +} + +new Pau_Updater(); \ No newline at end of file