-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-updater.php
176 lines (128 loc) · 5.69 KB
/
git-updater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Github Updater Class
*/
class Github_Updater {
private $file;
private $plugin;
private $basename;
private $active;
private $username;
private $repository;
private $authorize_token;
private $github_response;
public function __construct( $file ) {
$this->file = $file;
add_action( 'admin_init', array( $this, 'set_plugin_properties' ) );
return $this;
}
public function set_plugin_properties() {
$this->plugin = get_plugin_data( $this->file );
$this->basename = plugin_basename( $this->file );
$this->active = is_plugin_active( $this->basename );
}
public function set_username( $username ) {
$this->username = $username;
}
public function set_repository( $repository ) {
$this->repository = $repository;
}
public function authorize( $token ) {
$this->authorize_token = $token;
}
private function get_repository_info() {
if ( is_null( $this->github_response ) ) { // Do we have a response?
$request_uri = sprintf( 'https://api.github.com/repos/%s/%s/releases', $this->username, $this->repository ); // Build URI
if( $this->authorize_token ) { // Is there an access token?
$request_uri = add_query_arg( 'access_token', $this->authorize_token, $request_uri ); // Append it
}
$git_response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_uri ) ), true ); // Get JSON and parse it
if( is_array( $git_response ) ) { // If it is an array
$git_response = current( $git_response ); // Get the first item
}
if( $this->authorize_token ) { // Is there an access token?
$git_response['zipball_url'] = add_query_arg( 'access_token', $this->authorize_token, $git_response['zipball_url'] ); // Update our zip url with token
}
$this->github_response = $git_response; // Set it to our property
}
}
public function initialize() {
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'modify_transient' ), 10, 1 );
add_filter( 'plugins_api', array( $this, 'plugin_popup' ), 10, 3);
add_filter( 'upgrader_post_install', array( $this, 'after_install' ), 10, 3 );
}
public function modify_transient( $transient ) {
if( property_exists( $transient, 'checked') ) { // Check if transient has a checked property
if( $checked = $transient->checked ) { // Did Wordpress check for updates?
$this->get_repository_info(); // Get the repo info
$out_of_date = version_compare( $this->github_response['tag_name'], $checked[ $this->basename ], 'gt' ); // Check if we're out of date
if( $out_of_date ) {
$new_files = $this->github_response['zipball_url']; // Get the ZIP
$slug = current( explode('/', $this->basename ) ); // Create valid slug
// Setup plugin icons
$plugin['icons'] = array();
$plugin_icons = array(
'svg' => plugin_dir_url( __FILE__ ) . 'assets/'.$slug.'-icon.svg',
'1x' => plugin_dir_url( __FILE__ ) . 'assets/'.$slug.'-icon-128x128.png',
'2x' => plugin_dir_url( __FILE__ ) . 'assets/'.$slug.'-icon-256x256.png',
);
// Setup our plugin info
$plugin = array(
'url' => $this->plugin['PluginURI'],
'slug' => $slug,
'icons' => $plugin_icons,
'package' => $new_files,
'new_version' => $this->github_response['tag_name']
);
$transient->response[$this->basename] = (object) $plugin; // Return it in response
}
}
}
return $transient; // Return filtered transient
}
public function plugin_popup( $result, $action, $args ) {
if( ! empty( $args->slug ) ) { // If there is a slug
if( $args->slug == current( explode( '/' , $this->basename ) ) ) { // And it's our slug
$this->get_repository_info(); // Get our repo info
// Set it to an array
$plugin = array(
'name' => $this->plugin['Name'],
'slug' => $this->basename,
'requires' => "3.8.0",
'tested' => "4.9.2",
'rating' => "100",
'num_ratings' => "226",
'downloaded' => $this->github_response['download_count'],
'added' => $this->github_response['created_at'], //'2017-12-15',
'version' => $this->github_response['tag_name'],
'author' => $this->plugin['AuthorName'],
'author_profile' => $this->plugin['AuthorURI'],
'last_updated' => $this->github_response['published_at'],
'homepage' => $this->plugin['PluginURI'],
'short_description' => $this->plugin['Description'],
'sections' => array(
'description' => class_exists( 'Parsedown' ) ? Parsedown::instance()->parse( plugin_dir_url( __FILE__ ) . 'readme.txt' ) : $this->github_response['body'],
'changelog' => class_exists( 'Parsedown' ) ? Parsedown::instance()->parse( '<h3>' . $this->github_response['name'] . '</h3>' . '<br>' . $this->github_response['body'] ),
),
'banners' => array(
'low' => plugin_dir_url( __FILE__ ) . 'assets/'.$slug.'-banner-772x250.png',
'high' => plugin_dir_url( __FILE__ ) . 'assets/'.$slug.'-banner-1544x500.png',
),
'download_link' => $this->github_response['zipball_url']
);
return (object) $plugin; // Return the data
}
}
return $result; // Otherwise return default
}
public function after_install( $git_response, $hook_extra, $result ) {
global $wp_filesystem; // Get global FS object
$install_directory = plugin_dir_path( $this->file ); // Our plugin directory
$wp_filesystem->move( $result['destination'], $install_directory ); // Move files to the plugin dir
$result['destination'] = $install_directory; // Set the destination for the rest of the stack
if ( $this->active ) { // If it was active
activate_plugin( $this->basename ); // Reactivate
}
return $result;
}
}