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
+ }
0 commit comments