Skip to content

Commit a581f73

Browse files
committed
credential error solved
1 parent b8c2900 commit a581f73

File tree

1 file changed

+109
-92
lines changed

1 file changed

+109
-92
lines changed

includes/PDUpdater.php

Lines changed: 109 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace Cbx\Careertoolkit;
44

5-
class PDUpdater {
5+
class PDUpdater
6+
{
67
private $file;
78
private $plugin;
89
private $basename;
@@ -12,138 +13,152 @@ class PDUpdater {
1213
private $authorize_token;
1314
private $github_response;
1415

15-
public function __construct( $file ) {
16+
public function __construct($file)
17+
{
1618
$this->file = $file;
17-
add_action( 'admin_init', [ $this, 'set_plugin_properties' ] );
19+
add_action('admin_init', [$this, 'set_plugin_properties']);
1820

1921
return $this;
2022
}//end function __construct
2123

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);
2629
}//end function set_plugin_properties
2730

28-
public function set_username( $username ) {
31+
public function set_username($username)
32+
{
2933
$this->username = $username;
3034
}//end function set_username
3135

32-
public function set_repository( $repository ) {
36+
public function set_repository($repository)
37+
{
3338
$this->repository = $repository;
3439
}//end function set_repository
3540

36-
public function authorize( $token ) {
41+
public function authorize($token)
42+
{
3743
$this->authorize_token = $token;
3844
}//end function authorize
3945

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);
4350

4451
// Switch to HTTP Basic Authentication for GitHub API v3
4552
$curl = curl_init();
4653

47-
curl_setopt_array( $curl, [
48-
CURLOPT_URL => $request_uri,
54+
curl_setopt_array($curl, [
55+
CURLOPT_URL => $request_uri,
4956
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,
5360
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 => [
5764
"Authorization: token " . $this->authorize_token,
5865
"User-Agent: PDUpdater/1.2.3"
5966
]
60-
] );
67+
]);
6168

62-
$response = curl_exec( $curl );
69+
$response = curl_exec($curl);
6370

64-
curl_close( $curl );
71+
curl_close($curl);
6572

66-
$response = json_decode( $response, true );
73+
$response = json_decode($response, true);
6774

68-
if ( is_array( $response ) ) {
69-
$response = current( $response );
75+
if (is_array($response)) {
76+
$response = current($response);
7077
}
7178

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;
7482
}
75-
76-
$this->github_response = $response;
7783
}
7884
}//end function get_repository_info
7985

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);
8592
}//end function initialize
8693

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) {
9098
$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+
}
108117
}
118+
109119
}
110120
}
111121

112122
return $transient;
113123
}//end function modify_transient
114124

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') {
117128
return false;
118129
}
119130

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))) {
122133
$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+
147162
}
148163
}
149164

@@ -159,15 +174,16 @@ public function plugin_popup( $result, $action, $args ) {
159174
*
160175
* @return mixed
161176
*/
162-
public function after_install( $response, $hook_extra, $result ) {
177+
public function after_install($response, $hook_extra, $result)
178+
{
163179
global $wp_filesystem;
164180

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);
167183
$result['destination'] = $install_directory;
168184

169-
if ( $this->active ) {
170-
activate_plugin( $this->basename );
185+
if ($this->active) {
186+
activate_plugin($this->basename);
171187
}
172188

173189
return $result;
@@ -181,12 +197,13 @@ public function after_install( $response, $hook_extra, $result ) {
181197
*
182198
* @return mixed
183199
*/
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'])) {
186203
$parsed_args['headers'] = [];
187204
}
188205

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) {
190207
$parsed_args['headers']['Authorization'] = "token $this->authorize_token";
191208

192209
}

0 commit comments

Comments
 (0)