9
9
class WC_Utils_Updater {
10
10
11
11
/**
12
- * GitHub repository raw URL .
12
+ * GitHub repository slug (e.g. "owner/repo") .
13
13
*
14
14
* @var string
15
15
*/
16
- private $ raw_url ;
17
-
18
- /**
19
- * GitHub repository zip URL.
20
- *
21
- * @var string
22
- */
23
- private $ zip_url ;
16
+ private $ repo ;
24
17
25
18
/**
26
19
* Plugin file basename.
@@ -39,11 +32,10 @@ class WC_Utils_Updater {
39
32
/**
40
33
* Constructor.
41
34
*/
42
- public function __construct ( $ repo_raw_url , $ repo_zip_url , $ plugin_basename , $ version ) {
43
- $ this ->raw_url = trailingslashit ( $ repo_raw_url );
44
- $ this ->zip_url = $ repo_zip_url ;
35
+ public function __construct ( $ repo_slug , $ plugin_basename , $ version ) {
36
+ $ this ->repo = $ repo_slug ;
45
37
$ this ->plugin_basename = $ plugin_basename ;
46
- $ this ->version = $ version ;
38
+ $ this ->version = $ version ;
47
39
48
40
add_filter ( 'pre_set_site_transient_update_plugins ' , array ( $ this , 'check_for_update ' ) );
49
41
add_filter ( 'plugins_api ' , array ( $ this , 'plugins_api ' ), 10 , 3 );
@@ -60,7 +52,8 @@ public function check_for_update( $transient ) {
60
52
return $ transient ;
61
53
}
62
54
63
- $ remote = wp_remote_get ( $ this ->raw_url . 'woocommerce-utils.php ' );
55
+ $ api_url = sprintf ( 'https://api.github.com/repos/%s/releases/latest ' , $ this ->repo );
56
+ $ remote = wp_remote_get ( $ api_url , array ( 'headers ' => array ( 'Accept ' => 'application/vnd.github.v3+json ' ) ) );
64
57
65
58
if ( is_wp_error ( $ remote ) ) {
66
59
return $ transient ;
@@ -70,13 +63,13 @@ public function check_for_update( $transient ) {
70
63
return $ transient ;
71
64
}
72
65
73
- $ body = wp_remote_retrieve_body ( $ remote );
74
-
75
- if ( ! preg_match ( '/^\s*\*\s*Version:\s*(.*)$/mi ' , $ body , $ matches ) ) {
66
+ $ release = json_decode ( wp_remote_retrieve_body ( $ remote ), true );
67
+ if ( empty ( $ release ['tag_name ' ] ) || empty ( $ release ['zipball_url ' ] ) ) {
76
68
return $ transient ;
77
69
}
78
70
79
- $ remote_version = trim ( $ matches [1 ] );
71
+ $ remote_tag = ltrim ( $ release ['tag_name ' ], 'v ' );
72
+ $ remote_version = $ remote_tag ;
80
73
81
74
if ( version_compare ( $ this ->version , $ remote_version , '>= ' ) ) {
82
75
return $ transient ;
@@ -85,8 +78,8 @@ public function check_for_update( $transient ) {
85
78
$ plugin = new stdClass ();
86
79
$ plugin ->slug = dirname ( $ this ->plugin_basename );
87
80
$ plugin ->new_version = $ remote_version ;
88
- $ plugin ->url = $ this -> raw_url ;
89
- $ plugin ->package = $ this -> zip_url ;
81
+ $ plugin ->url = $ release [ ' html_url ' ] ;
82
+ $ plugin ->package = $ release [ ' zipball_url ' ] ;
90
83
91
84
$ transient ->response [ $ this ->plugin_basename ] = $ plugin ;
92
85
@@ -110,7 +103,22 @@ public function plugins_api( $result, $action, $args ) {
110
103
return $ result ;
111
104
}
112
105
113
- $ remote = wp_remote_get ( $ this ->raw_url . 'readme.txt ' );
106
+ $ api_url = sprintf ( 'https://api.github.com/repos/%s/releases/latest ' , $ this ->repo );
107
+ $ release = wp_remote_get ( $ api_url , array ( 'headers ' => array ( 'Accept ' => 'application/vnd.github.v3+json ' ) ) );
108
+
109
+ if ( is_wp_error ( $ release ) || 200 !== wp_remote_retrieve_response_code ( $ release ) ) {
110
+ return $ result ;
111
+ }
112
+
113
+ $ release_data = json_decode ( wp_remote_retrieve_body ( $ release ), true );
114
+ if ( empty ( $ release_data ['tag_name ' ] ) || empty ( $ release_data ['zipball_url ' ] ) ) {
115
+ return $ result ;
116
+ }
117
+
118
+ $ tag = $ release_data ['tag_name ' ];
119
+ $ zip = $ release_data ['zipball_url ' ];
120
+
121
+ $ remote = wp_remote_get ( sprintf ( 'https://raw.githubusercontent.com/%s/%s/readme.txt ' , $ this ->repo , $ tag ) );
114
122
115
123
if ( is_wp_error ( $ remote ) || 200 !== wp_remote_retrieve_response_code ( $ remote ) ) {
116
124
return $ result ;
@@ -121,11 +129,11 @@ public function plugins_api( $result, $action, $args ) {
121
129
$ info = new stdClass ();
122
130
$ info ->name = 'WooCommerce Utils ' ;
123
131
$ info ->slug = dirname ( $ this ->plugin_basename );
124
- $ info ->version = $ this -> version ;
132
+ $ info ->version = ltrim ( $ tag , ' v ' ) ;
125
133
$ info ->sections = array (
126
134
'description ' => $ readme ,
127
135
);
128
- $ info ->download_link = $ this -> zip_url ;
136
+ $ info ->download_link = $ zip ;
129
137
130
138
return $ info ;
131
139
}
0 commit comments