2
2
3
3
namespace Cbx \Careertoolkit ;
4
4
5
- class PDUpdater {
5
+ class PDUpdater
6
+ {
6
7
private $ file ;
7
8
private $ plugin ;
8
9
private $ basename ;
@@ -12,138 +13,152 @@ class PDUpdater {
12
13
private $ authorize_token ;
13
14
private $ github_response ;
14
15
15
- public function __construct ( $ file ) {
16
+ public function __construct ($ file )
17
+ {
16
18
$ this ->file = $ file ;
17
- add_action ( 'admin_init ' , [ $ this , 'set_plugin_properties ' ] );
19
+ add_action ('admin_init ' , [$ this , 'set_plugin_properties ' ] );
18
20
19
21
return $ this ;
20
22
}//end function __construct
21
23
22
- public function set_plugin_properties () {
23
- $ this ->plugin = get_plugin_data ( $ this ->file );
24
- $ this ->basename = plugin_basename ( $ this ->file );
25
- $ this ->active = is_plugin_active ( $ this ->basename );
24
+ public function set_plugin_properties ()
25
+ {
26
+ $ this ->plugin = get_plugin_data ($ this ->file );
27
+ $ this ->basename = plugin_basename ($ this ->file );
28
+ $ this ->active = is_plugin_active ($ this ->basename );
26
29
}//end function set_plugin_properties
27
30
28
- public function set_username ( $ username ) {
31
+ public function set_username ($ username )
32
+ {
29
33
$ this ->username = $ username ;
30
34
}//end function set_username
31
35
32
- public function set_repository ( $ repository ) {
36
+ public function set_repository ($ repository )
37
+ {
33
38
$ this ->repository = $ repository ;
34
39
}//end function set_repository
35
40
36
- public function authorize ( $ token ) {
41
+ public function authorize ($ token )
42
+ {
37
43
$ this ->authorize_token = $ token ;
38
44
}//end function authorize
39
45
40
- private function get_repository_info () {
41
- if ( is_null ( $ this ->github_response ) ) {
42
- $ request_uri = sprintf ( 'https://api.github.com/repos/%s/%s/releases ' , $ this ->username , $ this ->repository );
46
+ private function get_repository_info ()
47
+ {
48
+ if (is_null ($ this ->github_response )) {
49
+ $ request_uri = sprintf ('https://api.github.com/repos/%s/%s/releases ' , $ this ->username , $ this ->repository );
43
50
44
51
// Switch to HTTP Basic Authentication for GitHub API v3
45
52
$ curl = curl_init ();
46
53
47
- curl_setopt_array ( $ curl , [
48
- CURLOPT_URL => $ request_uri ,
54
+ curl_setopt_array ($ curl , [
55
+ CURLOPT_URL => $ request_uri ,
49
56
CURLOPT_RETURNTRANSFER => true ,
50
- CURLOPT_ENCODING => "" ,
51
- CURLOPT_MAXREDIRS => 10 ,
52
- CURLOPT_TIMEOUT => 0 ,
57
+ CURLOPT_ENCODING => "" ,
58
+ CURLOPT_MAXREDIRS => 10 ,
59
+ CURLOPT_TIMEOUT => 0 ,
53
60
CURLOPT_FOLLOWLOCATION => true ,
54
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 ,
55
- CURLOPT_CUSTOMREQUEST => "GET " ,
56
- CURLOPT_HTTPHEADER => [
61
+ CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 ,
62
+ CURLOPT_CUSTOMREQUEST => "GET " ,
63
+ CURLOPT_HTTPHEADER => [
57
64
"Authorization: token " . $ this ->authorize_token ,
58
65
"User-Agent: PDUpdater/1.2.3 "
59
66
]
60
- ] );
67
+ ]);
61
68
62
- $ response = curl_exec ( $ curl );
69
+ $ response = curl_exec ($ curl );
63
70
64
- curl_close ( $ curl );
71
+ curl_close ($ curl );
65
72
66
- $ response = json_decode ( $ response , true );
73
+ $ response = json_decode ($ response , true );
67
74
68
- if ( is_array ( $ response ) ) {
69
- $ response = current ( $ response );
75
+ if (is_array ($ response) ) {
76
+ $ response = current ($ response );
70
77
}
71
78
72
- if ( $ this ->authorize_token ) {
73
- $ response ['zipball_url ' ] = add_query_arg ( 'access_token ' , $ this ->authorize_token , $ response ['zipball_url ' ] );
79
+ if ($ this ->authorize_token && isset ($ response ['zipball_url ' ])) {
80
+ $ response ['zipball_url ' ] = add_query_arg ('access_token ' , $ this ->authorize_token , $ response ['zipball_url ' ]);
81
+ $ this ->github_response = $ response ;
74
82
}
75
-
76
- $ this ->github_response = $ response ;
77
83
}
78
84
}//end function get_repository_info
79
85
80
- public function initialize () {
81
- add_filter ( 'pre_set_site_transient_update_plugins ' , [ $ this , 'modify_transient ' ], 10 , 1 );
82
- add_filter ( 'plugins_api ' , [ $ this , 'plugin_popup ' ], 10 , 3 );
83
- add_filter ( 'upgrader_post_install ' , [ $ this , 'after_install ' ], 10 , 3 );
84
- add_filter ( "http_request_args " , [ $ this , "addHeaders " ], 10 , 3 );
86
+ public function initialize ()
87
+ {
88
+ add_filter ('pre_set_site_transient_update_plugins ' , [$ this , 'modify_transient ' ], 10 , 1 );
89
+ add_filter ('plugins_api ' , [$ this , 'plugin_popup ' ], 10 , 3 );
90
+ add_filter ('upgrader_post_install ' , [$ this , 'after_install ' ], 10 , 3 );
91
+ add_filter ("http_request_args " , [$ this , "addHeaders " ], 10 , 3 );
85
92
}//end function initialize
86
93
87
- public function modify_transient ( $ transient ) {
88
- if ( property_exists ( $ transient , 'checked ' ) ) {
89
- if ( $ checked = $ transient ->checked ) {
94
+ public function modify_transient ($ transient )
95
+ {
96
+ if (property_exists ($ transient , 'checked ' )) {
97
+ if ($ checked = $ transient ->checked ) {
90
98
$ this ->get_repository_info ();
91
-
92
- $ tag_name = str_replace ( "v " , "" , $ this ->github_response ['tag_name ' ] );
93
- $ tag_name = str_replace ( "V " , "" , $ tag_name );
94
- $ out_of_date = version_compare ( $ tag_name , $ checked [ $ this ->basename ], 'gt ' );
95
-
96
- if ( $ out_of_date ) {
97
- $ new_files = $ this ->github_response ['zipball_url ' ];
98
- $ slug = current ( explode ( '/ ' , $ this ->basename ) );
99
-
100
- $ plugin = [
101
- 'url ' => $ this ->plugin ['PluginURI ' ],
102
- 'slug ' => $ slug ,
103
- 'package ' => $ new_files ,
104
- 'new_version ' => $ tag_name
105
- ];
106
-
107
- $ transient ->response [ $ this ->basename ] = (object ) $ plugin ;
99
+ if (isset ($ this ->github_response ['tag_name ' ])) {
100
+ $ tag_name = str_replace ("v " , "" , $ this ->github_response ['tag_name ' ]);
101
+ $ tag_name = str_replace ("V " , "" , $ tag_name );
102
+ $ out_of_date = version_compare ($ tag_name , $ checked [$ this ->basename ], 'gt ' );
103
+
104
+ if ($ out_of_date ) {
105
+ $ new_files = $ this ->github_response ['zipball_url ' ];
106
+ $ slug = current (explode ('/ ' , $ this ->basename ));
107
+
108
+ $ plugin = [
109
+ 'url ' => $ this ->plugin ['PluginURI ' ],
110
+ 'slug ' => $ slug ,
111
+ 'package ' => $ new_files ,
112
+ 'new_version ' => $ tag_name
113
+ ];
114
+
115
+ $ transient ->response [$ this ->basename ] = (object ) $ plugin ;
116
+ }
108
117
}
118
+
109
119
}
110
120
}
111
121
112
122
return $ transient ;
113
123
}//end function modify_transient
114
124
115
- public function plugin_popup ( $ result , $ action , $ args ) {
116
- if ( $ action !== 'plugin_information ' ) {
125
+ public function plugin_popup ($ result , $ action , $ args )
126
+ {
127
+ if ($ action !== 'plugin_information ' ) {
117
128
return false ;
118
129
}
119
130
120
- if ( ! empty ( $ args ->slug ) ) {
121
- if ( $ args ->slug == current ( explode ( '/ ' , $ this ->basename ) ) ) {
131
+ if (! empty ($ args ->slug ) ) {
132
+ if ($ args ->slug == current (explode ('/ ' , $ this ->basename )) ) {
122
133
$ this ->get_repository_info ();
123
- $ slug = current ( explode ( '/ ' , $ this ->basename ) );
124
-
125
- $ tag_name = str_replace ( "v " , "" , $ this ->github_response ['tag_name ' ] );
126
- $ tag_name = str_replace ( "V " , "" , $ tag_name );
127
-
128
- $ plugin = [
129
- 'name ' => isset ( $ this ->plugin ['Name ' ] ) ? $ this ->plugin ['Name ' ] : '' ,
130
- 'slug ' => $ slug ,
131
- 'requires ' => '5.3 ' ,
132
- 'tested ' => '5.4 ' ,
133
- 'version ' => $ tag_name ,
134
- 'author ' => $ this ->plugin ['Author ' ],
135
- 'author_profile ' => $ this ->plugin ['AuthorURI ' ],
136
- 'last_updated ' => $ this ->github_response ['published_at ' ],
137
- 'homepage ' => $ this ->plugin ['PluginURI ' ],
138
- 'short_description ' => isset ( $ this ->plugin ['Description ' ] ) ? $ this ->plugin ['Description ' ] : '' ,
139
- 'sections ' => [
140
- 'Description ' => isset ( $ this ->plugin ['Description ' ] ) ? $ this ->plugin ['Description ' ] : '' ,
141
- 'Updates ' => isset ( $ this ->github_response ['body ' ] ) ? $ this ->github_response ['body ' ] : '' ,
142
- ],
143
- 'download_link ' => $ this ->github_response ['zipball_url ' ]
144
- ];
145
-
146
- return (object ) $ plugin ;
134
+
135
+ if (isset ($ this ->github_response ['zipball_url ' ])) {
136
+ $ slug = current (explode ('/ ' , $ this ->basename ));
137
+
138
+ $ tag_name = str_replace ("v " , "" , $ this ->github_response ['tag_name ' ]);
139
+ $ tag_name = str_replace ("V " , "" , $ tag_name );
140
+
141
+ $ plugin = [
142
+ 'name ' => isset ($ this ->plugin ['Name ' ]) ? $ this ->plugin ['Name ' ] : '' ,
143
+ 'slug ' => $ slug ,
144
+ 'requires ' => '5.3 ' ,
145
+ 'tested ' => '5.4 ' ,
146
+ 'version ' => $ tag_name ,
147
+ 'author ' => $ this ->plugin ['Author ' ],
148
+ 'author_profile ' => $ this ->plugin ['AuthorURI ' ],
149
+ 'last_updated ' => $ this ->github_response ['published_at ' ],
150
+ 'homepage ' => $ this ->plugin ['PluginURI ' ],
151
+ 'short_description ' => isset ($ this ->plugin ['Description ' ]) ? $ this ->plugin ['Description ' ] : '' ,
152
+ 'sections ' => [
153
+ 'Description ' => isset ($ this ->plugin ['Description ' ]) ? $ this ->plugin ['Description ' ] : '' ,
154
+ 'Updates ' => isset ($ this ->github_response ['body ' ]) ? $ this ->github_response ['body ' ] : '' ,
155
+ ],
156
+ 'download_link ' => $ this ->github_response ['zipball_url ' ]
157
+ ];
158
+
159
+ return (object ) $ plugin ;
160
+ }
161
+
147
162
}
148
163
}
149
164
@@ -159,15 +174,16 @@ public function plugin_popup( $result, $action, $args ) {
159
174
*
160
175
* @return mixed
161
176
*/
162
- public function after_install ( $ response , $ hook_extra , $ result ) {
177
+ public function after_install ($ response , $ hook_extra , $ result )
178
+ {
163
179
global $ wp_filesystem ;
164
180
165
- $ install_directory = plugin_dir_path ( $ this ->file );
166
- $ wp_filesystem ->move ( $ result ['destination ' ], $ install_directory );
181
+ $ install_directory = plugin_dir_path ($ this ->file );
182
+ $ wp_filesystem ->move ($ result ['destination ' ], $ install_directory );
167
183
$ result ['destination ' ] = $ install_directory ;
168
184
169
- if ( $ this ->active ) {
170
- activate_plugin ( $ this ->basename );
185
+ if ($ this ->active ) {
186
+ activate_plugin ($ this ->basename );
171
187
}
172
188
173
189
return $ result ;
@@ -181,12 +197,13 @@ public function after_install( $response, $hook_extra, $result ) {
181
197
*
182
198
* @return mixed
183
199
*/
184
- public function addHeaders ( $ parsed_args , $ url ) {
185
- if ( empty ( $ parsed_args ['headers ' ] ) ) {
200
+ public function addHeaders ($ parsed_args , $ url )
201
+ {
202
+ if (empty ($ parsed_args ['headers ' ])) {
186
203
$ parsed_args ['headers ' ] = [];
187
204
}
188
205
189
- if ( strpos ( $ url , "https://api.github.com/repos/ {$ this ->username }/ {$ this ->repository }" ) !== false ) {
206
+ if (strpos ($ url , "https://api.github.com/repos/ {$ this ->username }/ {$ this ->repository }" ) !== false ) {
190
207
$ parsed_args ['headers ' ]['Authorization ' ] = "token $ this ->authorize_token " ;
191
208
192
209
}
0 commit comments