| 1 |
<?php |
| 2 |
|
| 3 |
class Mmb_Core extends Mmb_Helper |
| 4 |
{ |
| 5 |
var $name; |
| 6 |
var $slug; |
| 7 |
var $settings; |
| 8 |
var $remote_client; |
| 9 |
var $comment_instance; |
| 10 |
var $plugin_instance; |
| 11 |
var $theme_instance; |
| 12 |
var $category_instance; |
| 13 |
var $wp_instance; |
| 14 |
var $page_instance; |
| 15 |
var $post_instance; |
| 16 |
var $stats_instance; |
| 17 |
var $user_instance; |
| 18 |
var $tag_instance; |
| 19 |
var $backup_instance; |
| 20 |
// var $ende_instance; |
| 21 |
// protected $secret_key; |
| 22 |
|
| 23 |
function __construct(){ |
| 24 |
global $mmb_plugin_dir; |
| 25 |
// get_option(); |
| 26 |
// $this->secret_key = trim(get_option('siteurl'), ' /'); |
| 27 |
$this->name = 'Manage Multiple Blogs'; |
| 28 |
$this->slug = 'manage-multiple-blogs'; |
| 29 |
$this->settings = get_option($this->slug); |
| 30 |
// $this->ende_instance = new Mmb_EnDe($this->secret_key); |
| 31 |
if (!$this->settings) |
| 32 |
{ |
| 33 |
$this->settings = array( |
| 34 |
'blogs' => array(), |
| 35 |
'current_blog' => array( |
| 36 |
'type' => null, |
| 37 |
), |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
add_action('rightnow_end', array($this, 'add_right_now_info')); |
| 42 |
add_action('wp_footer', array('Mmb_Stats', 'set_hit_count')); |
| 43 |
// add_action('xmlrpc_call', array($this, 'extend_xmlrpc_methods'), 0, 1); |
| 44 |
add_filter('xmlrpc_methods', array($this, 'add_xmlrpc_methods')); |
| 45 |
register_activation_hook($mmb_plugin_dir.'/init.php', array($this, 'install')); |
| 46 |
add_action('init', array($this, 'mmb_test_fix')); |
| 47 |
} |
| 48 |
|
| 49 |
function mmb_test_fix() { |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Add an item into the Right Now Dashboard widget |
| 55 |
* to inform that the blog can be managed remotely |
| 56 |
* |
| 57 |
*/ |
| 58 |
function add_right_now_info() |
| 59 |
{ |
| 60 |
echo '<div class="mmb-slave-info"> |
| 61 |
<p>This site can be managed remotely.</p> |
| 62 |
</div>'; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Add custom XMLRPC methods to fit our needs |
| 67 |
* This function should only be called if the current blog is a Slave |
| 68 |
* |
| 69 |
* @param mixed $methods |
| 70 |
*/ |
| 71 |
function add_xmlrpc_methods($methods) |
| 72 |
{ |
| 73 |
|
| 74 |
$methods['mmbUpgradeWorker'] = 'mmb_worker_upgrade'; |
| 75 |
// stats |
| 76 |
$methods['mmbGetStats'] = 'mmb_stats_get'; |
| 77 |
$methods['mmbGetServerStatus'] = 'mmb_stats_server_get'; |
| 78 |
$methods['mmbGetUserHitStats'] = 'mmb_stats_hit_count_get'; |
| 79 |
|
| 80 |
// plugins |
| 81 |
$methods['mmbGetPluginList'] = 'mmb_plugin_get_list'; |
| 82 |
$methods['mmbActivatePlugin'] = 'mmb_plugin_activate'; |
| 83 |
$methods['mmbDeactivatePlugin'] = 'mmb_plugin_deactivate'; |
| 84 |
$methods['mmbUpgradePlugin'] = 'mmb_plugin_upgrade'; |
| 85 |
$methods['mmbUpgradePlugins'] = 'mmb_plugin_upgrade_multiple'; |
| 86 |
$methods['mmbUpgradeAllPlugins'] = 'mmb_plugin_upgrade_all'; |
| 87 |
$methods['mmbDeletePlugin'] = 'mmb_plugin_delete'; |
| 88 |
$methods['mmbInstallPlugin'] = 'mmb_plugin_install'; |
| 89 |
$methods['mmbUploadPluginByURL'] = 'mmb_plugin_upload_by_url'; |
| 90 |
|
| 91 |
//themes |
| 92 |
$methods['mmbGetThemeList'] = 'mmb_theme_get_list'; |
| 93 |
$methods['mmbActivateTheme'] = 'mmb_theme_activate'; |
| 94 |
$methods['mmbDeleteTheme'] = 'mmb_theme_delete'; |
| 95 |
$methods['mmbInstallTheme'] = 'mmb_theme_install'; |
| 96 |
$methods['mmbUpgradeTheme'] = 'mmb_theme_upgrade'; |
| 97 |
$methods['mmbUpgradeThemes'] = 'mmb_themes_upgrade'; |
| 98 |
$methods['mmbUploadThemeByURL'] = 'mmb_theme_upload_by_url'; |
| 99 |
|
| 100 |
// wordpress update |
| 101 |
$methods['mmbWPCheckVersion'] = 'mmb_wp_checkversion'; |
| 102 |
$methods['mmbWPUpgrade'] = 'mmb_wp_upgrade'; |
| 103 |
$methods['mmbWPGetUpdates'] = 'mmb_wp_get_updates'; |
| 104 |
|
| 105 |
// categories |
| 106 |
// native XMLRPC method to get category list is not good enough |
| 107 |
// so we make our own |
| 108 |
$methods['mmbGetCategoryList'] = 'mmb_cat_get_list'; |
| 109 |
$methods['mmbUpdateCategory'] = 'mmb_cat_update'; |
| 110 |
$methods['mmbAddCategory'] = 'mmb_cat_add'; |
| 111 |
// (category deleting can be handled well by native XMLRPC method) |
| 112 |
|
| 113 |
//tags by ashish |
| 114 |
$methods['mmbGetTagList'] = 'mmb_tag_get_list'; |
| 115 |
$methods['mmbUpdateTag'] = 'mmb_tag_update'; |
| 116 |
$methods['mmbAddTag'] = 'mmb_tag_add'; |
| 117 |
$methods['mmbDeleteTag'] = 'mmb_tag_delete'; |
| 118 |
|
| 119 |
|
| 120 |
// pages |
| 121 |
$methods['mmbGetPageEditData'] = 'mmb_page_get_edit_data'; |
| 122 |
$methods['mmbGetPageNewData'] = 'mmb_page_get_new_data'; |
| 123 |
$methods['mmbUpdatePage'] = 'mmb_page_update'; |
| 124 |
$methods['mmbCreatePage'] = 'mmb_page_create'; |
| 125 |
|
| 126 |
// posts |
| 127 |
$methods['mmbGetPostList'] = 'mmb_post_get_list'; |
| 128 |
$methods['mmbGetPostNewData'] = 'mmb_post_get_new_data'; |
| 129 |
$methods['mmbGetPostEditData'] = 'mmb_post_get_edit_data'; |
| 130 |
$methods['mmbUpdatePost'] = 'mmb_post_update'; |
| 131 |
$methods['mmbCreatePost'] = 'mmb_post_create'; |
| 132 |
$methods['mmbPublishPost'] = 'mmb_post_publish'; |
| 133 |
$methods['mmbPostChecksum'] = 'mmb_post_checksum'; |
| 134 |
|
| 135 |
//comments |
| 136 |
$methods['mmbRestoreComment'] = 'mmb_restore_comment'; |
| 137 |
$methods['mmbGetCommentCount'] = 'mmb_get_comment_count'; |
| 138 |
$methods['mmbBulkEditComment'] = 'mmb_bulk_edit_comment'; |
| 139 |
|
| 140 |
//users |
| 141 |
$methods['mmbUserChangePassword'] = 'mmb_user_change_password'; |
| 142 |
|
| 143 |
//Backup/Restore |
| 144 |
$methods['mmbBackupNow'] = 'mmb_backup_now'; |
| 145 |
$methods['mmbRestoreNow'] = 'mmb_restore_now'; |
| 146 |
$methods['mmbGetBackupUrl'] = 'mmb_get_backup_url'; |
| 147 |
$methods['mmbWeeklyBackup'] = 'mmb_weekly_backup'; |
| 148 |
$methods['mmbLastWorkerMessage'] = 'mmb_geet_last_worker_message'; |
| 149 |
return $methods; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Gets an instance of the Comment class |
| 154 |
* |
| 155 |
*/ |
| 156 |
function get_comment_instance() |
| 157 |
{ |
| 158 |
if (!isset($this->comment_instance)) |
| 159 |
{ |
| 160 |
$this->comment_instance = new Mmb_Comment(); |
| 161 |
} |
| 162 |
|
| 163 |
return $this->comment_instance; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Gets an instance of the Plugin class |
| 168 |
* |
| 169 |
*/ |
| 170 |
function get_plugin_instance() |
| 171 |
{ |
| 172 |
if (!isset($this->plugin_instance)) |
| 173 |
{ |
| 174 |
$this->plugin_instance = new Mmb_Plugin(); |
| 175 |
} |
| 176 |
|
| 177 |
return $this->plugin_instance; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Gets an instance of the Theme class |
| 182 |
* |
| 183 |
*/ |
| 184 |
function get_theme_instance() |
| 185 |
{ |
| 186 |
if (!isset($this->theme_instance)) |
| 187 |
{ |
| 188 |
$this->theme_instance = new Mmb_Theme(); |
| 189 |
} |
| 190 |
|
| 191 |
return $this->theme_instance; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Gets an instance of Mmb_Page class |
| 196 |
* |
| 197 |
*/ |
| 198 |
function get_page_instance() |
| 199 |
{ |
| 200 |
if (!isset($this->page_instance)) |
| 201 |
{ |
| 202 |
$this->page_instance = new Mmb_Page(); |
| 203 |
} |
| 204 |
|
| 205 |
return $this->page_instance; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Gets an instance of Mmb_Post class |
| 210 |
* |
| 211 |
*/ |
| 212 |
function get_post_instance() |
| 213 |
{ |
| 214 |
if (!isset($this->post_instance)) |
| 215 |
{ |
| 216 |
$this->post_instance = new Mmb_Post(); |
| 217 |
} |
| 218 |
|
| 219 |
return $this->post_instance; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Gets an instance of Category class |
| 224 |
* |
| 225 |
*/ |
| 226 |
function get_category_instance() |
| 227 |
{ |
| 228 |
if (!isset($this->category_instance)) |
| 229 |
{ |
| 230 |
$this->category_instance = new Mmb_Category(); |
| 231 |
} |
| 232 |
|
| 233 |
return $this->category_instance; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Gets an instance of Tag class |
| 238 |
* |
| 239 |
*/ |
| 240 |
function get_tag_instance() |
| 241 |
{ |
| 242 |
if (!isset($this->tag_instance)) |
| 243 |
{ |
| 244 |
$this->tag_instance = new Mmb_Tags(); |
| 245 |
} |
| 246 |
|
| 247 |
return $this->tag_instance; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Gets an instance of the WP class |
| 252 |
* |
| 253 |
*/ |
| 254 |
function get_wp_instance() |
| 255 |
{ |
| 256 |
if (!isset($this->wp_instance)) |
| 257 |
{ |
| 258 |
$this->wp_instance = new Mmb_WP(); |
| 259 |
} |
| 260 |
|
| 261 |
return $this->wp_instance; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Gets an instance of User |
| 266 |
* |
| 267 |
*/ |
| 268 |
function get_user_instance() |
| 269 |
{ |
| 270 |
if (!isset($this->user_instance)) |
| 271 |
{ |
| 272 |
$this->user_instance = new Mmb_User(); |
| 273 |
} |
| 274 |
|
| 275 |
return $this->user_instance; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Gets an instance of stats class |
| 280 |
* |
| 281 |
*/ |
| 282 |
function get_stats_instance() |
| 283 |
{ |
| 284 |
if (!isset($this->stats_instance)) |
| 285 |
{ |
| 286 |
$this->stats_instance = new Mmb_Stats(); |
| 287 |
} |
| 288 |
|
| 289 |
return $this->stats_instance; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Gets an instance of stats class |
| 294 |
* |
| 295 |
*/ |
| 296 |
function get_backup_instance() |
| 297 |
{ |
| 298 |
if (!isset($this->backup_instance)) |
| 299 |
{ |
| 300 |
$this->backup_instance = new Mmb_Backup(); |
| 301 |
} |
| 302 |
|
| 303 |
return $this->backup_instance; |
| 304 |
} |
| 305 |
|
| 306 |
function install() |
| 307 |
{ |
| 308 |
//no need to check just update the table will run only when plugin installs |
| 309 |
update_option('enable_xmlrpc', 1); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Saves the (modified) options into the database |
| 314 |
* |
| 315 |
*/ |
| 316 |
function _save_options() |
| 317 |
{ |
| 318 |
if (get_option($this->slug)) |
| 319 |
{ |
| 320 |
update_option($this->slug, $this->settings); |
| 321 |
} |
| 322 |
else |
| 323 |
{ |
| 324 |
add_option($this->slug, $this->settings); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Constructs a url (for ajax purpose) |
| 330 |
* |
| 331 |
* @param mixed $base_page |
| 332 |
*/ |
| 333 |
function _construct_url($params = array(), $base_page = 'index.php') |
| 334 |
{ |
| 335 |
$url = "$base_page?_wpnonce=" . wp_create_nonce($this->slug); |
| 336 |
foreach ($params as $key => $value) |
| 337 |
{ |
| 338 |
$url .= "&$key=$value"; |
| 339 |
} |
| 340 |
|
| 341 |
return $url; |
| 342 |
} |
| 343 |
|
| 344 |
function update_this_plugin($args) { |
| 345 |
$this->_escape($args); |
| 346 |
|
| 347 |
$username = $args[0]; |
| 348 |
$password = $args[1]; |
| 349 |
$url = $args[2]; |
| 350 |
// return array('test' => 'hello there'); |
| 351 |
|
| 352 |
if (!$user = $this->login($username, $password)) |
| 353 |
{ |
| 354 |
return $this->error; |
| 355 |
} |
| 356 |
if (!current_user_can('administrator')) |
| 357 |
{ |
| 358 |
return new IXR_Error(401, 'Sorry, Only administrators can upgrade this plugin on the remote blog.'); |
| 359 |
} |
| 360 |
|
| 361 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 362 |
|
| 363 |
ob_start(); |
| 364 |
@unlink(WP_PLUGIN_DIR.'/worker'); |
| 365 |
$upgrader = new Plugin_Upgrader(); |
| 366 |
//$deactivate = $upgrader->deactivate_plugin_before_upgrade(false, 'worker/init.php'); |
| 367 |
$result = $upgrader->run(array( |
| 368 |
'package' => $url, |
| 369 |
'destination' => WP_PLUGIN_DIR, |
| 370 |
'clear_destination' => true, |
| 371 |
'clear_working' => true, |
| 372 |
'hook_extra' => array( |
| 373 |
'plugin' => 'worker/init.php' |
| 374 |
))); |
| 375 |
ob_end_clean(); |
| 376 |
if(is_wp_error($result) || !$result){ |
| 377 |
$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>' ; |
| 378 |
$this->_last_worker_message(array('error' => print_r($error, true))); |
| 379 |
}else { |
| 380 |
$data = get_plugin_data(WP_PLUGIN_DIR . '/' . $upgrader->plugin_info()); |
| 381 |
$this->_last_worker_message(array('success' => $upgrader->plugin_info(), 'name' => $data['Name'], 'activate' => print_r($activate, true))); |
| 382 |
} |
| 383 |
|
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Logs a user int |
| 388 |
* |
| 389 |
* @param mixed $username |
| 390 |
* @param mixed $password |
| 391 |
* @return WP_Error|WP_User |
| 392 |
*/ |
| 393 |
function login($username, $password) |
| 394 |
{ |
| 395 |
if (!get_option( 'enable_xmlrpc')) |
| 396 |
{ |
| 397 |
update_option('enable_xmlrpc', 1); |
| 398 |
// return new IXR_Error(405, 'XML-RPC services are disabled on this blog.'); |
| 399 |
} |
| 400 |
|
| 401 |
$user = wp_authenticate($username, $password); |
| 402 |
|
| 403 |
if (is_wp_error($user)) { |
| 404 |
$this->error = new IXR_Error(403, __('Bad login/pass combination.')); |
| 405 |
return false; |
| 406 |
} |
| 407 |
|
| 408 |
set_current_user( $user->ID ); |
| 409 |
return $user; |
| 410 |
} |
| 411 |
} |