Skip to content

Commit

Permalink
Updated self-updater script
Browse files Browse the repository at this point in the history
  • Loading branch information
tommusrhodus committed Mar 23, 2024
1 parent d487482 commit 3fc63f9
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 43 deletions.
84 changes: 55 additions & 29 deletions dynamic-table-of-contents/classes/class-wpsp-blocks-self-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,26 @@

class WPSP_Blocks_Self_Update {

public static $instance;

/**
* Get instance of this class.
*
* @return WPSP_Blocks_Self_Update
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Initialize WordPress hooks
*/
public function init() {
add_filter( 'update_plugins_github.com', array( $this, 'self_update' ), 10, 3 );
public function hooks() {
add_filter( 'update_plugins_opsoasis.mystagingwebsite.com', array( $this, 'self_update' ), 10, 3 );
}

/**
Expand All @@ -29,47 +44,58 @@ public function init() {
* @return array|bool Array of update data or false if no update available.
*/
public function self_update( $update, array $plugin_data, string $plugin_file ) {
$wpsp_blocks = apply_filters( 'wpsp_installed_blocks', array() );

// Only check if this is a plugin we manage.
if ( ! in_array( $plugin_file, $wpsp_blocks, true ) ) {
// Already completed update check elsewhere.
if ( ! empty( $update ) ) {
return $update;
}

$plugin_slug = $plugin_data['TextDomain'];
$current_version = (float) $plugin_data['Version'];
$updated_version = (float) $current_version + 0.1;
// Check this is a plugin we actually manage.
$wpsp_blocks = array_map(
function( $block ) {
return "{$block}/{$block}.php";
},
apply_filters( 'wpsp_installed_blocks', array() )
);

// only check this plugin
if ( "{$plugin_slug}/{$plugin_slug}.php" !== $plugin_file ) {
if ( ! in_array( $plugin_file, $wpsp_blocks, true ) ) {
return $update;
}

// already completed update check elsewhere
if ( ! empty( $update ) ) {
return $update;
}
$plugin_filename_parts = explode( '/', $plugin_file );

// Check if we have a plugin update based on .zip existence.
// Ask opsoasis.mystagingwebsite.com if there's an update.
$response = wp_remote_get(
"https://github.com/a8cteam51/special-projects-blocks-monorepo/releases/download/{$plugin_slug}-v{$updated_version}/{$plugin_slug}.zip",
'https://opsoasis.mystagingwebsite.com/wp-json/opsoasis-blocks-version-manager/v1/update-check',
array(
'user-agent' => 'wpspecialprojects',
'body' => array(
'plugin' => $plugin_filename_parts[0],
'version' => $plugin_data['Version'],
),
)
);

// If we found a .zip then there's an update available!
if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
$zip = wp_remote_retrieve_body( $response );
} else {
return false;
// Bail if this plugin wasn't found on opsoasis.mystagingwebsite.com.
if ( 404 === wp_remote_retrieve_response_code( $response ) || 202 === wp_remote_retrieve_response_code( $response ) ) {
return $update;
}

return array(
'slug' => $plugin_slug,
'version' => $updated_version,
'url' => "https://github.com/a8cteam51/special-projects-blocks-monorepo/releases/tag/{$plugin_slug}-v{$updated_version}",
'package' => $zip,
);
$updated_version = wp_remote_retrieve_body( $response );
$updated_array = json_decode( $updated_version, true );

// Get the zip file.
$zip = wp_remote_get( $updated_array['package_url'] );

// If the zip file was found, return the update.
if ( 200 === wp_remote_retrieve_response_code( $zip ) ) {
return array(
'slug' => $updated_array['slug'],
'version' => $updated_array['version'],
'url' => $updated_array['package_url'],
'package' => wp_remote_retrieve_body( $zip ),
);
}

// If we're here, nothing was found on github.
return $update;
}
}
31 changes: 18 additions & 13 deletions dynamic-table-of-contents/dynamic-table-of-contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* Description: Creates a table of contents that's dynamically (PHP) rendered.
* Requires at least: 6.1
* Requires PHP: 8.0
* Version: 0.3
* Version: 0.1.4
* Author: WordPress Special Projects Team
* Author URI: https://wpspecialprojects.wordpress.com/
* Update URI: https://github.com/a8cteam51/special-projects-blocks-monorepo/
* Update URI: https://opsoasis.mystagingwebsite.com/dynamic-table-of-contents/
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: dynamic-table-of-contents
Expand All @@ -20,31 +20,36 @@
exit;
}

// Define the plugin folder name.


// If no other WPSP Block Plugin added the self update class, add it.
if ( ! class_exists( 'WPSP_Blocks_Self_Update' ) ) {
require __DIR__ . '/classes/class-wpsp-blocks-self-update.php';

$wpsp_blocks_self_update = new WPSP_Blocks_Self_Update();
$wpsp_blocks_self_update->init();
$wpsp_blocks_self_update = WPSP_Blocks_Self_Update::get_instance();
$wpsp_blocks_self_update->hooks();
}

/**
* Setup auto-updates for this plugin from our monorepo.
* Done in an anonymous function for simplicity in making this a drop-in snippet.
*
* @param array $blocks Array of plugin files.
*
* @return array
*/
function wpsp_dynamic_table_of_contents_block_self_update( $blocks ): array {
$plugin_data = get_plugin_data( __FILE__ );
add_filter(
'wpsp_installed_blocks',
function( $blocks ) {
$plugin_data = get_plugin_data( __FILE__ );

$blocks[] = sprintf(
'%1$s/%1$s.php',
$plugin_data['TextDomain']
);
// Add the plugin slug here to enable autoupdates.
$blocks[] = 'dynamic-table-of-contents';

return $blocks;
}
add_filter( 'wpsp_installed_blocks', 'wpsp_dynamic_table_of_contents_block_self_update' );
return $blocks;
}
);

/**
* Registers the block using the metadata loaded from the `block.json` file.
Expand Down
89 changes: 89 additions & 0 deletions hello-world/classes/class-wpsp-blocks-self-update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Plugin Autoupdate Filter Self Update class.
* sets up autoupdates for this GitHub-hosted plugin.
*
* @package wpsp
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

class WPSP_Blocks_Self_Update {

public static $instance;

/**
* Get instance of this class.
*
* @return WPSP_Blocks_Self_Update
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Initialize WordPress hooks
*/
public function hooks() {
add_filter( 'update_plugins_opsoasis.mystagingwebsite.com', array( $this, 'self_update' ), 10, 3 );
}

/**
* Check for updates to this plugin
*
* @param array $update Array of update data.
* @param array $plugin_data Array of plugin data.
* @param string $plugin_file Path to plugin file.
*
* @return array|bool Array of update data or false if no update available.
*/
public function self_update( $update, array $plugin_data, string $plugin_file ) {
// Already completed update check elsewhere.
if ( ! empty( $update ) ) {
return $update;
}

$plugin_filename_parts = explode( '/', $plugin_file );

// Ask opsoasis.mystagingwebsite.com if there's an update.
$response = wp_remote_get(
'https://opsoasis.mystagingwebsite.com/wp-json/opsoasis-blocks-version-manager/v1/update-check',
array(
'body' => array(
'plugin' => $plugin_filename_parts[0],
'version' => $plugin_data['Version'],
),
)
);

// Bail if this plugin wasn't found on opsoasis.mystagingwebsite.com.
if ( 404 === wp_remote_retrieve_response_code( $response ) || 202 === wp_remote_retrieve_response_code( $response ) ) {
return $update;
}

$updated_version = wp_remote_retrieve_body( $response );
$updated_array = json_decode( $updated_version, true );

// Get the zip file.
$zip = wp_remote_get( $updated_array['package_url'] );

// If the zip file was found, return the update.
if ( 200 === wp_remote_retrieve_response_code( $zip ) ) {
return array(
'slug' => $updated_array['slug'],
'version' => $updated_array['version'],
'url' => $updated_array['package_url'],
'package' => wp_remote_retrieve_body( $zip ),
);
}

// If we're here, nothing was found on github.
return $update;
}
}
31 changes: 30 additions & 1 deletion hello-world/hello-world.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* Description: Example block scaffolded with Create Block tool.
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: 0.1.0
* Version: 0.1.1
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Update URI: https://opsoasis.mystagingwebsite.com/hello-world/
* Text Domain: hello-world
*
* @package wpsp
Expand All @@ -17,6 +18,34 @@
exit; // Exit if accessed directly.
}

// If no other WPSP Block Plugin added the self update class, add it.
if ( ! class_exists( 'WPSP_Blocks_Self_Update' ) ) {
require __DIR__ . '/classes/class-wpsp-blocks-self-update.php';

$wpsp_blocks_self_update = WPSP_Blocks_Self_Update::get_instance();
$wpsp_blocks_self_update->hooks();
}

/**
* Setup auto-updates for this plugin from our monorepo.
* Done in an anonymous function for simplicity in making this a drop-in snippet.
*
* @param array $blocks Array of plugin files.
*
* @return array
*/
add_filter(
'wpsp_installed_blocks',
function( $blocks ) {
$plugin_data = get_plugin_data( __FILE__ );

// Add the plugin slug here to enable autoupdates.
$blocks[] = 'hello-world';

return $blocks;
}
);

/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
Expand Down

0 comments on commit 3fc63f9

Please sign in to comment.