| 1 |
<?php |
| 2 |
|
| 3 |
class Mmb_Theme extends Mmb_Core |
| 4 |
{ |
| 5 |
function __construct() |
| 6 |
{ |
| 7 |
parent::__construct(); |
| 8 |
} |
| 9 |
|
| 10 |
/************************************************************* |
| 11 |
* FACADE functions |
| 12 |
* (functions to be called after a remote XMLRPC from Master) |
| 13 |
**************************************************************/ |
| 14 |
|
| 15 |
function get_list($args) |
| 16 |
{ |
| 17 |
$this->_escape($args); |
| 18 |
$username = $args[0]; |
| 19 |
$password = $args[1]; |
| 20 |
|
| 21 |
if (!$user = $this->login($username, $password)) |
| 22 |
{ |
| 23 |
return $this->error; |
| 24 |
} |
| 25 |
|
| 26 |
if(!current_user_can('switch_themes')) |
| 27 |
return new IXR_Error(401, 'Sorry, you are not allowed to manage themes on the remote blog.'); |
| 28 |
|
| 29 |
$all_themes = get_themes(); |
| 30 |
$theme_updates = $this->mmb_get_transient('update_themes'); |
| 31 |
$current_theme = current_theme_info(); |
| 32 |
|
| 33 |
foreach($all_themes as $theme_name => $theme_data){ |
| 34 |
if(isset($theme_updates->response[$theme_data['Template']])){ |
| 35 |
$all_themes[$theme_name]['new_version'] = $theme_updates->response[$theme_data['Template']]['new_version']; |
| 36 |
$all_themes[$theme_name]['new_url'] = $theme_updates->response[$theme_data['Template']]['url']; |
| 37 |
} |
| 38 |
} |
| 39 |
$activated_theme = $all_themes[$current_theme->name]; |
| 40 |
unset($all_themes[$current_theme->name]); |
| 41 |
|
| 42 |
// I don't bother paging |
| 43 |
// who would have 100's of themes anyway? |
| 44 |
return array( |
| 45 |
'current' => $activated_theme, |
| 46 |
'inactive' => $all_themes, |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Activates a theme locally |
| 52 |
* |
| 53 |
* @param mixed $args |
| 54 |
*/ |
| 55 |
function activate($args) |
| 56 |
{ |
| 57 |
$this->_escape($args); |
| 58 |
$username = $args[0]; |
| 59 |
$password = $args[1]; |
| 60 |
$template = $args[2]; |
| 61 |
$stylesheet = $args[3]; |
| 62 |
|
| 63 |
if (!$user = $this->login($username, $password)) |
| 64 |
{ |
| 65 |
return $this->error; |
| 66 |
} |
| 67 |
|
| 68 |
if(!current_user_can('switch_themes')) |
| 69 |
return new IXR_Error(401, 'Sorry, you are not allowed to activate themes on the remote blog.'); |
| 70 |
|
| 71 |
switch_theme($template, $stylesheet); |
| 72 |
|
| 73 |
// get the new updated theme list |
| 74 |
return $this->get_list($args); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Deletes a theme locally |
| 79 |
* |
| 80 |
* @param mixed $args |
| 81 |
*/ |
| 82 |
function delete($args) |
| 83 |
{ |
| 84 |
$this->_escape($args); |
| 85 |
$username = $args[0]; |
| 86 |
$password = $args[1]; |
| 87 |
$template = $args[2]; |
| 88 |
|
| 89 |
if (!$user = $this->login($username, $password)) |
| 90 |
{ |
| 91 |
return $this->error; |
| 92 |
} |
| 93 |
|
| 94 |
if(!current_user_can('update_themes')) |
| 95 |
{ |
| 96 |
return new IXR_Error(401, 'Sorry, you are not allowed to delete themes from the remote blog.'); |
| 97 |
} |
| 98 |
|
| 99 |
ob_start(); |
| 100 |
$result = delete_theme($template); |
| 101 |
ob_end_clean(); |
| 102 |
if (is_wp_error($result)) |
| 103 |
{ |
| 104 |
return new IXR_Error(401, 'Theme could not be deleted. ' . $result->get_error_message()); |
| 105 |
} |
| 106 |
|
| 107 |
return TRUE; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Installs a theme locally |
| 112 |
* |
| 113 |
* @param mixed $args |
| 114 |
*/ |
| 115 |
function install($args) |
| 116 |
{ |
| 117 |
$this->_escape($args); |
| 118 |
$username = $args[0]; |
| 119 |
$password = $args[1]; |
| 120 |
$theme = $args[2]; |
| 121 |
$activate = (bool)$args[3]; |
| 122 |
|
| 123 |
if (!$user = $this->login($username, $password)) |
| 124 |
{ |
| 125 |
return $this->error; |
| 126 |
} |
| 127 |
|
| 128 |
if (!current_user_can('install_themes')) |
| 129 |
{ |
| 130 |
return new IXR_Error(401, 'Sorry, you are not allowed to install themes on the remote blog.'); |
| 131 |
} |
| 132 |
|
| 133 |
ob_start(); |
| 134 |
|
| 135 |
// include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
| 136 |
include_once(ABSPATH . 'wp-admin/includes/theme-install.php'); |
| 137 |
|
| 138 |
$api = themes_api('theme_information', array('slug' => $theme, 'fields' => array('sections' => false))); |
| 139 |
|
| 140 |
if (is_wp_error($api)) |
| 141 |
{ |
| 142 |
return new IXR_Error(401, 'Could not install theme. ' . $api->get_error_message()); |
| 143 |
} |
| 144 |
|
| 145 |
$upgrader = new Mmb_Theme_Upgrader(); |
| 146 |
$result = $upgrader->install($api->download_link); |
| 147 |
|
| 148 |
if (is_wp_error($result)) |
| 149 |
{ |
| 150 |
return new IXR_Error(401, 'Theme could not be installed. ' . $result->get_error_message()); |
| 151 |
} |
| 152 |
|
| 153 |
// activate! |
| 154 |
if ($activate && $theme_info = $upgrader->theme_info()) |
| 155 |
{ |
| 156 |
$stylesheet = $upgrader->result['destination_name']; |
| 157 |
$template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet; |
| 158 |
|
| 159 |
$this->activate(array($username, $password, $template, $stylesheet)); |
| 160 |
} |
| 161 |
|
| 162 |
ob_end_clean(); |
| 163 |
|
| 164 |
// get the updated theme list |
| 165 |
return $this->get_list($args); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Uploads a theme given its URL |
| 170 |
* |
| 171 |
* @param mixed $args |
| 172 |
*/ |
| 173 |
function upload_by_url($args) |
| 174 |
{ |
| 175 |
$this->_escape($args); |
| 176 |
$username = $args[0]; |
| 177 |
$password = $args[1]; |
| 178 |
$url = $args[2]; |
| 179 |
|
| 180 |
if (!$user = $this->login($username, $password)) |
| 181 |
{ |
| 182 |
return $this->error; |
| 183 |
} |
| 184 |
|
| 185 |
if (!current_user_can('install_themes')) |
| 186 |
{ |
| 187 |
return new IXR_Error(401, 'Sorry, you are not allowed to install themes on the remote blog.'); |
| 188 |
} |
| 189 |
|
| 190 |
if (!$this->_init_filesystem()) |
| 191 |
return new IXR_Error(401, 'Theme could not be installed: Failed to initialize file system.'); |
| 192 |
|
| 193 |
|
| 194 |
ob_start(); |
| 195 |
$tmp_file = download_url($url); |
| 196 |
|
| 197 |
if(is_wp_error($tmp_file)) |
| 198 |
return new IXR_Error(401, 'Theme could not be installed. ' . $response->get_error_message()); |
| 199 |
|
| 200 |
$result = unzip_file($tmp_file, WP_CONTENT_DIR . '/themes'); |
| 201 |
unlink($tmp_file); |
| 202 |
|
| 203 |
if(is_wp_error($result)) |
| 204 |
{ |
| 205 |
return new IXR_Error(401, 'Theme could not be extracted. ' . $result->get_error_message()); |
| 206 |
} |
| 207 |
|
| 208 |
unset($args[2]); |
| 209 |
|
| 210 |
return $this->get_list($args); |
| 211 |
} |
| 212 |
function upload_theme_by_url($args){ |
| 213 |
|
| 214 |
$this->_escape($args); |
| 215 |
$username = $args[0]; |
| 216 |
$password = $args[1]; |
| 217 |
$url = $args[2]; |
| 218 |
|
| 219 |
if (!$user = $this->login($username, $password)) |
| 220 |
{ |
| 221 |
return $this->error; |
| 222 |
} |
| 223 |
if(!current_user_can('install_themes')){ |
| 224 |
return new IXR_Error( 401, 'Sorry, you are not allowed to manage theme install on the remote blog.'); |
| 225 |
} |
| 226 |
if (!$this->_init_filesystem()) |
| 227 |
return new IXR_Error(401, 'Theme could not be installed: Failed to initialize file system.'); |
| 228 |
|
| 229 |
ob_start(); |
| 230 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 231 |
$upgrader = new Theme_Upgrader(); |
| 232 |
$result = $upgrader->install($url); |
| 233 |
ob_end_clean(); |
| 234 |
if(is_wp_error($upgrader->skin->result) || !$upgrader->skin->result){ |
| 235 |
$error = is_wp_error($upgrader->skin->result) ? $upgrader->skin->result->get_error_message() : 'Check your FTP details. <a href="http://managewp.com/user-guide#ftp" title="More Info" target="_blank">More Info</a>' ; |
| 236 |
$this->_last_worker_message(array('error' => print_r($error,true))); |
| 237 |
}else { |
| 238 |
$theme = $upgrader->theme_info(); |
| 239 |
$this->_last_worker_message(array('success' => 'true', 'name' => $theme['Name'])); |
| 240 |
} |
| 241 |
|
| 242 |
} |
| 243 |
function upgrade($args){ |
| 244 |
$this->_escape($args); |
| 245 |
$username = $args[0]; |
| 246 |
$password = $args[1]; |
| 247 |
$template = $args[2]; |
| 248 |
$stylesheet = $args[3]; |
| 249 |
$directory = $args[3]; |
| 250 |
$chmod = false; |
| 251 |
|
| 252 |
if (!$user = $this->login($username, $password)) |
| 253 |
{ |
| 254 |
return $this->error; |
| 255 |
} |
| 256 |
if(!current_user_can('install_themes')){ |
| 257 |
return new IXR_Error( 401, 'Sorry, you are not allowed to manage theme install on the remote blog.'); |
| 258 |
} |
| 259 |
$chmod = fileperms($directory); |
| 260 |
if($chmod != 0755 ){ |
| 261 |
chmod($directory, 0755); |
| 262 |
} |
| 263 |
ob_start(); |
| 264 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 265 |
$upgrader = new Theme_Upgrader( new Theme_Upgrader_Skin( compact('title', 'nonce', 'url', 'theme') ) ); |
| 266 |
$result = $upgrader->upgrade($stylesheet); |
| 267 |
ob_end_clean(); |
| 268 |
|
| 269 |
if(is_wp_error($upgrader->skin->result) || !$upgrader->theme_info()){ |
| 270 |
$error = is_wp_error($upgrader->skin->result) ? $upgrader->skin->result->get_error_message() : 'Check your FTP details. <a href="http://managewp.com/user-guide#ftp" title="More Info" target="_blank">More Info</a>' ; |
| 271 |
$this->_last_worker_message(array('error' => print_r($error))); |
| 272 |
}else { |
| 273 |
$theme = $upgrader->theme_info(); |
| 274 |
$this->_last_worker_message(array('success' => 'true', 'name' => $theme['Name'])); |
| 275 |
} |
| 276 |
chmod($directory, $chmod); |
| 277 |
} |
| 278 |
function upgrade_all($args){ |
| 279 |
$this->_escape($args); |
| 280 |
$username = $args[0]; |
| 281 |
$password = $args[1]; |
| 282 |
$themes = $args[2]; |
| 283 |
$chmod = false; |
| 284 |
|
| 285 |
if (!$user = $this->login($username, $password)) |
| 286 |
{ |
| 287 |
return $this->error; |
| 288 |
} |
| 289 |
if(!current_user_can('install_themes')){ |
| 290 |
return new IXR_Error( 401, 'Sorry, you are not allowed to manage theme install on the remote blog.'); |
| 291 |
} |
| 292 |
|
| 293 |
if(!empty($themes)){ |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
ob_start(); |
| 298 |
|
| 299 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 300 |
$upgrader = new Theme_Upgrader(new Mmb_Bulk_Theme_Upgrader_Skin( compact('title', 'nonce', 'url', 'theme') )); |
| 301 |
$result = $upgrader->bulk_upgrade($themes); |
| 302 |
|
| 303 |
ob_end_clean(); |
| 304 |
if(is_wp_error($result) || !$result){ |
| 305 |
$error = is_wp_error($result) ? $result->get_error_message() : 'Check your FTP details. <a href="http://managewp.com/user-guide#ftp" title="More Info" target="_blank">More Info</a>' ; |
| 306 |
$this->_last_worker_message(array('error' => print_r($error, true))); |
| 307 |
} |
| 308 |
else { |
| 309 |
$message = ''; |
| 310 |
foreach($result as $theme_tmp => $info){ |
| 311 |
$message .= '<code>'.$theme_tmp.'</code><br />'; |
| 312 |
} |
| 313 |
$this->_last_worker_message(array('success' => 'true', 'message' => $message)); |
| 314 |
} |
| 315 |
|
| 316 |
}else { |
| 317 |
$this->_last_worker_message(array('error' => 'No themes to upgrade.')); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
function get_upgradable_themes(){ |
| 322 |
|
| 323 |
$all_themes = get_themes(); |
| 324 |
$upgrade_themes = array(); |
| 325 |
|
| 326 |
//$this->refresh_transient(); |
| 327 |
|
| 328 |
// $current = get_transient('update_plugins'); |
| 329 |
$current = $this->mmb_get_transient('update_themes'); |
| 330 |
// $test = $this->mmb_get_transient('update_plugins'); |
| 331 |
// $this->_log($test); |
| 332 |
foreach ((array)$all_themes as $theme_template => $theme_data){ |
| 333 |
foreach ($current->response as $current_themes => $theme){ |
| 334 |
if ($theme_data['Template'] == $current_themes) |
| 335 |
{ |
| 336 |
$current->response[$current_themes]['name'] = $theme_data['Name']; |
| 337 |
$current->response[$current_themes]['old_version'] = $theme_data['Version']; |
| 338 |
$current->response[$current_themes]['theme_tmp'] = $theme_data['Template']; |
| 339 |
$upgrade_themes[] = $current->response[$current_themes]; |
| 340 |
continue; |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
return $upgrade_themes; |
| 346 |
} |
| 347 |
} |
| 348 |
|