-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme-updater-bitbucket.php
174 lines (133 loc) · 4.87 KB
/
theme-updater-bitbucket.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
<?php
/**
* Theme Updater
*
* Allow auto update of themes hosted on BitBucket.
*/
if ( ! class_exists( 'Fluidweb_ThemeUpdater_Bitbucket' ) ) {
class Fluidweb_ThemeUpdater_Bitbucket {
private $slug;
private $current_theme_slug;
private $themeData;
private $repo;
private $bitbucketAPIResult;
private $bitbucketUsername;
private $bitbucketPassword;
private $allow_beta_updates;
/**
* Initialize class
*/
function __construct( $slug, $repo, $bbUsername, $bbPassword, $allowBetaUpdates ) {
// Bail if user doesn't have the right permissions
if ( ! current_user_can( 'update_themes' ) ) { return; }
add_filter( 'pre_set_site_transient_update_themes', array( $this, 'setTransient' ) );
add_filter( 'upgrader_post_install', array( $this, 'postInstall' ), 5, 3 );
add_filter( 'upgrader_process_complete', array( $this, 'updatesComplete' ), 5, 2 );
add_filter( 'http_request_args', array($this, 'addAuthRequestArgs'), 10, 2);
$this->slug = $slug;
$this->current_theme_slug = wp_get_theme()->exists() ? wp_get_theme()->stylesheet : null;
$this->repo = $repo;
$this->bitbucketUsername = $bbUsername;
$this->bitbucketPassword = $bbPassword;
$this->allow_beta_updates = $allowBetaUpdates;
}
/**
* Get information regarding the current theme version
*/
private function initThemeData() {
$this->themeData = wp_get_theme( $this->slug );
}
/**
* Call repository API for data
*/
private function callRepositoryAPI( $url ) {
$process = curl_init( $url );
curl_setopt( $process, CURLOPT_RETURNTRANSFER, TRUE );
// Maybe add password
if ( ! empty( $this->bitbucketUsername ) && ! empty( $this->bitbucketPassword ) ) {
curl_setopt( $process, CURLOPT_USERPWD, sprintf( '%s:%s', $this->bitbucketUsername, $this->bitbucketPassword ) );
}
// Send request
if( ! $response = curl_exec( $process ) ) {
trigger_error( curl_error( $process ) );
}
curl_close( $process );
return $response;
}
/**
* Get information regarding theme releases from repository
*/
private function getRepoReleaseInfo() {
// Only do this once
if ( ! empty( $this->bitbucketAPIResult ) ) { return; }
$url = sprintf('https://api.bitbucket.org/2.0/repositories/%s/refs/tags?sort=-target.date', $this->repo);
// Filter OUT beta and development versions
if ( strval( $this->allow_beta_updates ) !== 'true' ) { $url .= '&q=%28name%21%7E%22beta%22+AND+name%21%7E%22dev%22%29'; }
$response = $this->callRepositoryAPI( $url );
if ( $response ) {
$data = json_decode( $response );
if ( isset( $data, $data->values ) && is_array( $data->values ) ) {
$tag = reset( $data->values );
if ( isset( $tag->name ) ) { $this->bitbucketAPIResult = $tag; }
}
}
}
/**
* Push in theme version information to get the update notification
*/
public function setTransient( $transient ) {
// Get theme & git release information
$this->initThemeData();
$this->getRepoReleaseInfo();
// Nothing found.
if ( empty( $this->bitbucketAPIResult ) ) { return $transient; }
$repo_version = ltrim( $this->bitbucketAPIResult->name, 'v' );
// Check the versions if we need to do an update
$doUpdate = version_compare( $repo_version, $this->themeData->get('Version') );
// Update the transient to include our updated plugin data
if ( $doUpdate == 1 ) {
$package = sprintf( 'https://bitbucket.org/%s/get/%s.zip',
$this->repo,
$this->bitbucketAPIResult->name
);
$obj = array(
'slug' => $this->slug,
'new_version' => $repo_version,
'url' => $this->themeData->get('ThemeURI'),
'package' => $package,
);
$transient->response[ $this->slug ] = $obj;
}
return $transient;
}
/**
* Add repository authentication request arguments when request targets the repository
*/
public function addAuthRequestArgs( $args, $url ) {
if ( preg_match( '/bitbucket.org(.+)' . str_replace( '/', '\/', $this->repo ) . '/', $url ) ) {
if ( empty( $args['headers'] ) ) { $args['headers'] = array(); }
$args['headers']['Authorization'] = 'Basic ' . base64_encode( $this->bitbucketUsername . ':' . $this->bitbucketPassword );
}
return $args;
}
/**
* Perform additional actions to successfully install our theme
*/
public function postInstall( $true, $hook_extra, $result ) {
global $wp_filesystem;
$themeFolder = get_theme_root() . DIRECTORY_SEPARATOR . $this->slug;
$wp_filesystem->move( $result['destination'], $themeFolder );
$result['destination'] = $themeFolder;
$result['destination_name'] = $this->slug;
return $result;
}
/**
* Perform additional actions after update completes
*/
public function updatesComplete( $upgrader, $hook_extra ) {
if ( is_a( $upgrader, 'Theme_Upgrader' && ! empty( $this->current_theme_slug ) ) ) {
switch_theme( $this->current_theme_slug );
}
}
}
}