-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclass-wpcomsp-blocks-self-update.php
More file actions
85 lines (72 loc) · 2.22 KB
/
class-wpcomsp-blocks-self-update.php
File metadata and controls
85 lines (72 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Plugin Autoupdate Filter Self Update class.
* sets up autoupdates for this GitHub-hosted plugin.
*
* @package wpcomsp
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class WPCOMSP_Blocks_Self_Update {
public static $instance;
/**
* Get instance of this class.
*
* @return WPCOMSP_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.wpspecialprojects.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.wpspecialprojects.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 );
// Validate JSON and required fields.
if ( ! is_array( $updated_array ) || ! isset( $updated_array['slug'], $updated_array['version'], $updated_array['package_url'] ) ) {
return $update;
}
return array(
'slug' => $updated_array['slug'],
'version' => $updated_array['version'],
'url' => $updated_array['package_url'],
'package' => $updated_array['package_url'],
);
}
}