Skip to content

Commit

Permalink
fixing updater
Browse files Browse the repository at this point in the history
  • Loading branch information
pattyok committed Mar 8, 2021
1 parent 4468385 commit bb935cd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 156 deletions.
86 changes: 70 additions & 16 deletions includes/class-carkeekblocks-updater.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
<?php

class Carkeek_Blocks_Updater {
protected $file;
protected $plugin;
protected $basename;
protected $active;

private $file;

private $plugin;

private $basename;

private $active;

private $username;

private $repository;

private $authorize_token;

private $github_response;

public function __construct( $file ) {

$this->file = $file;

add_action( 'admin_init', array( $this, 'set_plugin_properties' ) );

return $this;
}

Expand All @@ -25,26 +36,32 @@ public function set_plugin_properties() {
public function set_username( $username ) {
$this->username = $username;
}

public function set_repository( $repository ) {
$this->repository = $repository;
}

public function authorize( $token ) {
$this->authorize_token = $token;
}

private function get_repository_info() {
if ( is_null( $this->github_response ) ) { // Do we have a response?
$args = array();
$request_uri = sprintf( 'https://api.github.com/repos/%s/%s/releases', $this->username, $this->repository ); // Build URI

$args = array();

if ( $this->authorize_token ) { // Is there an access token?
$request_uri = add_query_arg( 'access_token', $this->authorize_token, $request_uri ); // Append it
$args['headers']['Authorization'] = "token {$this->authorize_token}"; // Set the headers
}
$response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_uri ) ), true ); // Get JSON and parse it

$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
}
if ( $this->authorize_token ) { // Is there an access token?
$response['zipball_url'] = add_query_arg( 'access_token', $this->authorize_token, $response['zipball_url'] ); // Update our zip url with token
}

$this->github_response = $response; // Set it to our property
}
}
Expand All @@ -53,38 +70,64 @@ public function initialize() {
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'modify_transient' ), 10, 1 );
add_filter( 'plugins_api', array( $this, 'plugin_popup' ), 10, 3 );
add_filter( 'upgrader_post_install', array( $this, 'after_install' ), 10, 3 );

// Add Authorization Token to download_package
add_filter(
'upgrader_pre_download',
function() {
add_filter( 'http_request_args', array( $this, 'download_package' ), 15, 2 );
return false; // upgrader_pre_download filter default return value.
}
);
}

public function modify_transient( $transient ) {

if ( property_exists( $transient, 'checked' ) ) { // Check if transient has a checked property

if ( $checked = $transient->checked ) { // Did WordPress check for updates?
$this->get_repository_info(); // Get the repo info

$out_of_date = version_compare( $this->github_response['tag_name'], $checked[ $this->basename ], 'gt' ); // Check if we're out of date

if ( $out_of_date ) {
$new_files = $this->github_response['zipball_url']; // Get the ZIP
$slug = current( explode( '/', $this->basename ) ); // Create valid slug
$plugin = array( // setup our plugin info

$new_files = $this->github_response['zipball_url']; // Get the ZIP

$slug = current( explode( '/', $this->basename ) ); // Create valid slug

$plugin = array( // setup our plugin info
'url' => $this->plugin['PluginURI'],
'slug' => $slug,
'package' => $new_files,
'new_version' => $this->github_response['tag_name'],
);

$transient->response[ $this->basename ] = (object) $plugin; // Return it in response
}
}
}

return $transient; // Return filtered transient
}

public function plugin_popup( $result, $action, $args ) {
error_log(print_r($args, true));
if ( ! empty( $args->slug ) ) { // If there is a slug

if ( $args->slug == current( explode( '/', $this->basename ) ) ) { // And it's our slug

$this->get_repository_info(); // Get our repo info

// Set it to an array
$plugin = array(
'name' => $this->plugin['Name'],
'slug' => $this->basename,
'requires' => '5.6',
'tested' => '5.6.1',
'rating' => '100.0',
'added' => '2021-03-07',
'version' => $this->github_response['tag_name'],
'author' => $this->plugin['AuthorName'],
'author_profile' => $this->plugin['AuthorURI'],
Expand All @@ -97,12 +140,26 @@ public function plugin_popup( $result, $action, $args ) {
),
'download_link' => $this->github_response['zipball_url'],
);

return (object) $plugin; // Return the data
}
}
return $result; // Otherwise return default
}

public function download_package( $args, $url ) {

if ( null !== $args['filename'] ) {
if ( $this->authorize_token ) {
$args = array_merge( $args, array( 'headers' => array( 'Authorization' => "token {$this->authorize_token}" ) ) );
}
}

remove_filter( 'http_request_args', array( $this, 'download_package' ) );

return $args;
}

public function after_install( $response, $hook_extra, $result ) {
global $wp_filesystem; // Get global FS object

Expand All @@ -113,10 +170,7 @@ public function after_install( $response, $hook_extra, $result ) {
if ( $this->active ) { // If it was active
activate_plugin( $this->basename ); // Reactivate
}

return $result;
}


}


25 changes: 13 additions & 12 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin URI: https://github.com/pattyok/carkeek-blocks
* Description: Series of blocks designed to work with this site's custom Theme
* Author: Patty O'Hara
* Version: 1.0.2
* Version: 1.0.3
* Author URI https://carkeekstudios.com/
* Text Domain: carkeek-blocks
*/
Expand Down Expand Up @@ -131,16 +131,7 @@ private function includes() {
private function init() {
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ), 99 );
add_action( 'enqueue_block_editor_assets', array( $this, 'block_localization' ) );

/** Update via GitHub */
if( ! class_exists( 'Carkeek_Blocks_Updater' ) ){
include_once( plugin_dir_path( __FILE__ ) . 'includes/class-carkeekblocks-updater.php' );
}
$updater = new Carkeek_Blocks_Updater( __FILE__ ); // instantiate our class
$updater->set_username( 'pattyok' ); // set username
$updater->set_repository( 'carkeek-blocks' ); // set repo
$updater->authorize( '08a7b3d1e53c8ab3b8f5ece9f2878cb7e7ddccfd' );
$updater->initialize(); // initialize the updater
add_action( 'init', array( $this, 'init_updater' ) );
}

/**
Expand Down Expand Up @@ -197,7 +188,17 @@ public function block_localization() {
}
}


public function init_updater() {
/** Update via GitHub */
if ( ! class_exists( 'Carkeek_Blocks_Updater' ) ) {
include_once plugin_dir_path( __FILE__ ) . 'includes/class-carkeekblocks-updater.php';
}
$updater = new Carkeek_Blocks_Updater( __FILE__ ); // instantiate our class.
$updater->set_username( 'pattyok' ); // set username.
$updater->set_repository( 'carkeek-blocks' ); // set repo.
$updater->authorize( '08a7b3d1e53c8ab3b8f5ece9f2878cb7e7ddccfd' );
$updater->initialize(); // initialize the updater.
}

}

Expand Down
128 changes: 0 additions & 128 deletions webpack.config.js

This file was deleted.

0 comments on commit bb935cd

Please sign in to comment.