Skip to content

Commit 4f9b8cd

Browse files
jffngDAreRodz
authored andcommitted
Implement auto-updater.
1 parent 0bb63b2 commit 4f9b8cd

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Plugin Autoupdate Filter Self Update class.
4+
* sets up autoupdates for this GitHub-hosted plugin.
5+
*
6+
* @package wpsp
7+
*/
8+
9+
if ( ! defined( 'ABSPATH' ) ) {
10+
exit; // Exit if accessed directly.
11+
}
12+
13+
class WPSP_Blocks_Self_Update {
14+
15+
public static $instance;
16+
17+
/**
18+
* Get instance of this class.
19+
*
20+
* @return WPSP_Blocks_Self_Update
21+
*/
22+
public static function get_instance() {
23+
if ( ! self::$instance ) {
24+
self::$instance = new self();
25+
}
26+
27+
return self::$instance;
28+
}
29+
30+
/**
31+
* Initialize WordPress hooks
32+
*/
33+
public function hooks() {
34+
add_filter( 'update_plugins_opsoasis-develop.mystagingwebsite.com', array( $this, 'self_update' ), 10, 3 );
35+
}
36+
37+
/**
38+
* Check for updates to this plugin
39+
*
40+
* @param array $update Array of update data.
41+
* @param array $plugin_data Array of plugin data.
42+
* @param string $plugin_file Path to plugin file.
43+
*
44+
* @return array|bool Array of update data or false if no update available.
45+
*/
46+
public function self_update( $update, array $plugin_data, string $plugin_file ) {
47+
// Already completed update check elsewhere.
48+
if ( ! empty( $update ) ) {
49+
return $update;
50+
}
51+
52+
$plugin_filename_parts = explode( '/', $plugin_file );
53+
54+
// Ask opsoasis.mystagingwebsite.com if there's an update.
55+
$response = wp_remote_get(
56+
'https://opsoasis-develop.mystagingwebsite.com/wp-json/opsoasis-blocks-version-manager/v1/update-check',
57+
array(
58+
'body' => array(
59+
'plugin' => $plugin_filename_parts[0],
60+
'version' => $plugin_data['Version'],
61+
),
62+
)
63+
);
64+
65+
// Bail if this plugin wasn't found on opsoasis.mystagingwebsite.com.
66+
if ( 404 === wp_remote_retrieve_response_code( $response ) || 202 === wp_remote_retrieve_response_code( $response ) ) {
67+
return $update;
68+
}
69+
70+
$updated_version = wp_remote_retrieve_body( $response );
71+
$updated_array = json_decode( $updated_version, true );
72+
73+
return array(
74+
'slug' => $updated_array['slug'],
75+
'version' => $updated_array['version'],
76+
'url' => $updated_array['package_url'],
77+
'package' => $updated_array['package_url'],
78+
);
79+
}
80+
}

stretchy-type/stretchy-type.php

+27
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@
1717
exit; // Exit if accessed directly.
1818
}
1919

20+
if ( ! class_exists( 'WPSP_Blocks_Self_Update' ) ) {
21+
require __DIR__ . '/classes/class-wpsp-blocks-self-update.php';
22+
23+
$wpsp_blocks_self_update = WPSP_Blocks_Self_Update::get_instance();
24+
$wpsp_blocks_self_update->hooks();
25+
}
26+
27+
/**
28+
* Setup auto-updates for this plugin from our monorepo.
29+
* Done in an anonymous function for simplicity in making this a drop-in snippet.
30+
*
31+
* @param array $blocks Array of plugin files.
32+
*
33+
* @return array
34+
*/
35+
add_filter(
36+
'wpsp_installed_blocks',
37+
function( $blocks ) {
38+
$plugin_data = get_plugin_data( __FILE__ );
39+
40+
// Add the plugin slug here to enable autoupdates.
41+
$blocks[] = 'stretchy-type';
42+
43+
return $blocks;
44+
}
45+
);
46+
2047
/**
2148
* Registers the block using the metadata loaded from the `block.json` file.
2249
* Behind the scenes, it registers also all assets so they can be enqueued

0 commit comments

Comments
 (0)