Skip to content

Commit 0ec3cfa

Browse files
committed
cbxdevtoolkit updater on process
1 parent f62c941 commit 0ec3cfa

File tree

4 files changed

+356
-1
lines changed

4 files changed

+356
-1
lines changed

PDUpdater.php

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

cbxcareertoolkit.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ function run_cbxcareer_toolkit()
7272
}
7373

7474

75-
add_action("plugin_loaded", "run_cbxcareer_toolkit");
75+
add_action("plugin_loaded", "run_cbxcareer_toolkit");
76+

includes/Hooks.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
use Cbx\Careertoolkit\Factories\Resume\DummyResumeGenerate;
66
use Cbx\Careertoolkit\Factories\Job\DummyJobGenerate;
77

8+
use Cbx\Careertoolkit\PDUpdater;
9+
810
class Hooks
911
{
1012

1113
public function __construct()
1214
{
1315
$this->init_commands();
16+
$this->update_checker();
1417
}
1518

1619
public function init_commands()
@@ -20,4 +23,15 @@ public function init_commands()
2023
$jobs = new DummyJobGenerate();
2124
}
2225
}//end method init_commands
26+
27+
public function update_checker()
28+
{
29+
$updater = new PDUpdater(__FILE__);
30+
$updater->set_username('codeboxrcodehub');
31+
$updater->set_repository('cbxphpspreadsheet');
32+
$updater->authorize('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');
33+
$updater->initialize();
34+
35+
36+
}//end method update_checker
2337
}//end class Hooks

includes/PDUpdater.php

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

0 commit comments

Comments
 (0)