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