Skip to content

Commit

Permalink
Implement auto-updater.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Jul 1, 2024
1 parent cf9d656 commit d525f25
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
80 changes: 80 additions & 0 deletions stretchy-type/classes/class-wpsp-blocks-self-update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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-develop.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-develop.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 );

return array(
'slug' => $updated_array['slug'],
'version' => $updated_array['version'],
'url' => $updated_array['package_url'],
'package' => $updated_array['package_url'],
);
}
}
27 changes: 27 additions & 0 deletions stretchy-type/stretchy-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@
exit; // Exit if accessed directly.
}

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[] = 'stretchy-type';

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 d525f25

Please sign in to comment.