diff --git a/dynamic-table-of-contents/classes/class-wpsp-blocks-self-update.php b/dynamic-table-of-contents/classes/class-wpsp-blocks-self-update.php index dded063..4a16e88 100644 --- a/dynamic-table-of-contents/classes/class-wpsp-blocks-self-update.php +++ b/dynamic-table-of-contents/classes/class-wpsp-blocks-self-update.php @@ -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 ); } /** @@ -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; } } diff --git a/dynamic-table-of-contents/dynamic-table-of-contents.php b/dynamic-table-of-contents/dynamic-table-of-contents.php index 8cb066d..b867bca 100644 --- a/dynamic-table-of-contents/dynamic-table-of-contents.php +++ b/dynamic-table-of-contents/dynamic-table-of-contents.php @@ -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 @@ -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. diff --git a/hello-world/classes/class-wpsp-blocks-self-update.php b/hello-world/classes/class-wpsp-blocks-self-update.php new file mode 100644 index 0000000..ea58d26 --- /dev/null +++ b/hello-world/classes/class-wpsp-blocks-self-update.php @@ -0,0 +1,89 @@ + 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; + } +} diff --git a/hello-world/hello-world.php b/hello-world/hello-world.php index 7492a73..16cb1be 100644 --- a/hello-world/hello-world.php +++ b/hello-world/hello-world.php @@ -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 @@ -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