PluginProbe
ManageWP Worker / 4.9.36
ManageWP Worker v4.9.36
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MMB / Core.php

Core.php in ManageWP Worker 4.9.36, at src/MMB/Core.php

516 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*************************************************************
4 * core.class.php
5 * Upgrade Plugins
6 * Copyright (c) 2011 Prelovac Media
7 * www.prelovac.com
8 **************************************************************/
9 class MMB_Core
10 {
11 /** @var MMB_Comment */
12 private $comment_instance;
13
14 /** @var MMB_Stats */
15 private $stats_instance;
16
17 /** @var MMB_User */
18 private $user_instance;
19
20 /** @var MMB_Installer */
21 private $installer_instance;
22
23 protected $mmb_multisite = false;
24
25 protected $network_admin_install;
26
27 public function __construct()
28 {
29 global $blog_id;
30
31 if (is_multisite()) {
32 $this->mmb_multisite = $blog_id;
33 $this->network_admin_install = get_option('mmb_network_admin_install');
34
35 add_action('wpmu_new_blog', array(&$this, 'updateKeys'));
36 }
37
38 // admin notices
39 if (!get_option('_worker_public_key')) {
40 if ($this->mmb_multisite) {
41 if (is_network_admin() && $this->network_admin_install == '1') {
42 add_action('network_admin_notices', array(&$this, 'network_admin_notice'));
43 } else {
44 if ($this->network_admin_install != '1') {
45 $parent_key = $this->get_parent_blog_option('_worker_public_key');
46 if (empty($parent_key)) {
47 add_action('admin_notices', array(&$this, 'admin_notice'));
48 }
49 }
50 }
51 } else {
52 add_action('admin_notices', array(&$this, 'admin_notice'));
53 }
54 }
55 }
56
57 public function network_admin_notice()
58 {
59 $enabledNotice = $this->isNoticeEnabled();
60
61 $keys = mwp_get_communication_keys();
62 if (is_array($keys) && count($keys) > 0 || !$enabledNotice) {
63 return;
64 }
65
66 $configurationService = new MWP_Configuration_Service();
67 $configuration = $configurationService->getConfiguration();
68 $notice = $configuration->getNetworkNotice();
69
70 echo $notice;
71 }
72
73 public function admin_notice()
74 {
75 $enabledNotice = $this->isNoticeEnabled();
76 $keys = mwp_get_communication_keys();
77 if (is_array($keys) && count($keys) > 0 || !$enabledNotice) {
78 return;
79 }
80
81 $configurationService = new MWP_Configuration_Service();
82 $configuration = $configurationService->getConfiguration();
83 $notice = is_multisite() ? $configuration->getNetworkNotice() : $configuration->getNotice();
84
85 echo $notice;
86 }
87
88 private function isNoticeEnabled()
89 {
90 return apply_filters('mwp_admin_notice_enabled', true);
91 }
92
93 /**
94 * Get parent blog options
95 */
96 private function get_parent_blog_option($option_name = '')
97 {
98 /** @var wpdb $wpdb */
99 global $wpdb;
100 $option = $wpdb->get_var($wpdb->prepare("SELECT `option_value` FROM {$wpdb->base_prefix}options WHERE option_name = '%s' LIMIT 1", $option_name));
101
102 return $option;
103 }
104
105 /**
106 * @return MMB_Comment
107 */
108 public function get_comment_instance()
109 {
110 if (!isset($this->comment_instance)) {
111 $this->comment_instance = new MMB_Comment();
112 }
113
114 return $this->comment_instance;
115 }
116
117 /**
118 * @return MMB_User
119 */
120 public function get_user_instance()
121 {
122 if (!isset($this->user_instance)) {
123 $this->user_instance = new MMB_User();
124 }
125
126 return $this->user_instance;
127 }
128
129 /**
130 * @return MMB_Stats
131 */
132 public function get_stats_instance()
133 {
134 if (!isset($this->stats_instance)) {
135 $this->stats_instance = new MMB_Stats();
136 }
137
138 return $this->stats_instance;
139 }
140
141 /**
142 * @return MMB_Installer
143 */
144 public function get_installer_instance()
145 {
146 if (!isset($this->installer_instance)) {
147 $this->installer_instance = new MMB_Installer();
148 }
149
150 return $this->installer_instance;
151 }
152
153 public function buildLoaderContent($pluginBasename)
154 {
155 $loader = <<<EOF
156 <?php
157
158 /*
159 Plugin Name: ManageWP - Worker Loader
160 Plugin URI: https://managewp.com
161 Description: This is automatically generated by the ManageWP Worker plugin to increase performance and reliability. It is automatically disabled when disabling the main plugin.
162 Author: GoDaddy
163 Version: 1.0.0
164 Author URI: https://godaddy.com
165 License: GPL2
166 Network: true
167 */
168
169 if (!function_exists('untrailingslashit') || !defined('WP_PLUGIN_DIR')) {
170 // WordPress is probably not bootstrapped.
171 exit;
172 }
173
174 if (file_exists(untrailingslashit(WP_PLUGIN_DIR).'/$pluginBasename')) {
175 if (in_array('$pluginBasename', (array) get_option('active_plugins')) ||
176 (function_exists('get_site_option') && array_key_exists('worker/init.php', (array) get_site_option('active_sitewide_plugins')))) {
177 \$GLOBALS['mwp_is_mu'] = true;
178 include_once untrailingslashit(WP_PLUGIN_DIR).'/$pluginBasename';
179 }
180 }
181
182 EOF;
183
184 return $loader;
185 }
186
187 public function registerMustUse($loaderName, $loaderContent)
188 {
189 $mustUsePluginDir = rtrim(WPMU_PLUGIN_DIR, '/');
190 $loaderPath = $mustUsePluginDir.'/'.$loaderName;
191
192 if (file_exists($loaderPath) && md5($loaderContent) === md5_file($loaderPath)) {
193 return;
194 }
195
196 if (!is_dir($mustUsePluginDir)) {
197 $dirMade = @mkdir($mustUsePluginDir);
198
199 if (!$dirMade) {
200 $error = error_get_last();
201 throw new Exception(sprintf('Unable to create loader directory: %s', $error['message']));
202 }
203 }
204
205 if (!is_writable($mustUsePluginDir)) {
206 throw new Exception('MU-plugin directory is not writable.');
207 }
208
209 $loaderWritten = @file_put_contents($loaderPath, $loaderContent);
210
211 if (!$loaderWritten) {
212 $error = error_get_last();
213 throw new Exception(sprintf('Unable to write loader: %s', $error['message']));
214 }
215 }
216
217 /**
218 * Plugin install callback function
219 * Check PHP version
220 */
221 public function install()
222 {
223 update_option('mwp_recovering', '');
224 update_option('mwp_incremental_update_active', '');
225 update_option('mwp_core_autoupdate', '');
226 update_option('mwp_container_parameters', array());
227 update_option('mwp_container_site_parameters', array());
228 update_option('mwp_maintenace_mode', array());
229 mwp_container()->getMigration()->migrate();
230 try {
231 $this->registerMustUse('0-worker.php', $this->buildLoaderContent('worker/init.php'));
232 } catch (Exception $e) {
233 mwp_logger()->error('Unable to write ManageWP loader', array('exception' => $e));
234 }
235
236 /** @var wpdb $wpdb */
237 global $wpdb;
238
239 //delete plugin options, just in case
240 if ($this->mmb_multisite != false) {
241 $network_blogs = $wpdb->get_results("select `blog_id`, `site_id` from `{$wpdb->blogs}`");
242 if (!empty($network_blogs)) {
243 if (is_network_admin()) {
244 update_option('mmb_network_admin_install', 1);
245 $mainBlogId = defined('BLOG_ID_CURRENT_SITE') ? BLOG_ID_CURRENT_SITE : false;
246 foreach ($network_blogs as $details) {
247 if (($mainBlogId !== false && $details->blog_id == $mainBlogId) || ($mainBlogId === false && $details->site_id == $details->blog_id)) {
248 update_blog_option($details->blog_id, 'mmb_network_admin_install', 1);
249 } else {
250 update_blog_option($details->blog_id, 'mmb_network_admin_install', -1);
251 }
252
253 update_blog_option($details->blog_id, '_worker_nossl_key', '');
254 update_blog_option($details->blog_id, '_worker_public_key', '');
255 delete_blog_option($details->blog_id, '_action_message_id');
256 }
257 } else {
258 update_option('mmb_network_admin_install', -1);
259 update_option('_worker_nossl_key', '');
260 update_option('_worker_public_key', '');
261 delete_option('_action_message_id');
262 }
263 }
264 } else {
265 update_option('_worker_nossl_key', '');
266 update_option('_worker_public_key', '');
267 delete_option('_action_message_id');
268 }
269
270 delete_option('mwp_notifications');
271 delete_option('mwp_worker_brand');
272 delete_option('mwp_worker_configuration');
273 $path = realpath(dirname(__FILE__)."/../../worker.json");
274 if (file_exists($path)) {
275 $configuration = file_get_contents($path);
276 $jsonConfiguration = json_decode($configuration, true);
277 if ($jsonConfiguration !== null) {
278 update_option("mwp_worker_configuration", $jsonConfiguration);
279 }
280 }
281 }
282
283 public function deactivate($networkDeactivation = false, $workerDeactivation = true)
284 {
285 if ($workerDeactivation) {
286 mwp_uninstall();
287 }
288
289 /** @var wpdb $wpdb */
290 global $current_user, $wpdb;
291
292 if ($this->mmb_multisite !== false) {
293 $network_blogs = $wpdb->get_col("select `blog_id` from `{$wpdb->blogs}`");
294 if (!empty($network_blogs)) {
295 if (is_network_admin()) {
296 if ($networkDeactivation) {
297 delete_option('mmb_network_admin_install');
298 foreach ($network_blogs as $blog_id) {
299 delete_blog_option($blog_id, 'mmb_network_admin_install');
300 delete_blog_option($blog_id, '_worker_nossl_key');
301 delete_blog_option($blog_id, '_worker_public_key');
302 delete_blog_option($blog_id, '_action_message_id');
303 delete_blog_option($blog_id, 'mwp_notifications');
304
305 if ($workerDeactivation) {
306 delete_blog_option($blog_id, 'mwp_maintenace_mode');
307 delete_blog_option($blog_id, 'mwp_worker_brand');
308 }
309 }
310 }
311 } else {
312 if ($networkDeactivation) {
313 delete_option('mmb_network_admin_install');
314 }
315
316 delete_option('_worker_nossl_key');
317 delete_option('_worker_public_key');
318 delete_option('_action_message_id');
319 }
320 }
321 } else {
322 delete_option('_worker_nossl_key');
323 delete_option('_worker_public_key');
324 delete_option('_action_message_id');
325 }
326
327 //Delete options
328 delete_option('mwp_notifications');
329 wp_clear_scheduled_hook('mwp_notifications');
330 wp_clear_scheduled_hook('mwp_datasend');
331
332 if ($workerDeactivation) {
333 delete_option('mwp_maintenace_mode');
334 delete_option('mwp_worker_brand');
335 delete_option('mwp_worker_configuration');
336 }
337 }
338
339 /**
340 * Worker update
341 */
342 public function update_worker_plugin($params)
343 {
344 if ($params['download_url']) {
345 @include_once ABSPATH.'wp-admin/includes/file.php';
346 @include_once ABSPATH.'wp-admin/includes/misc.php';
347 @include_once ABSPATH.'wp-admin/includes/template.php';
348 @include_once ABSPATH.'wp-admin/includes/class-wp-upgrader.php';
349 @include_once ABSPATH.'wp-admin/includes/screen.php';
350 @include_once ABSPATH.'wp-admin/includes/plugin.php';
351
352 if (!$this->is_server_writable()) {
353 return array(
354 'error' => 'Failed, please <a target="_blank" href="http://managewp.com/user-guide/faq/my-pluginsthemes-fail-to-update-or-i-receive-a-yellow-ftp-warning">add FTP details for automatic upgrades.</a>',
355 );
356 }
357
358 mwp_load_required_components();
359
360 ob_start();
361 @unlink(dirname(__FILE__));
362 /** @handled class */
363 $upgrader = new Plugin_Upgrader(mwp_container()->getUpdaterSkin());
364 $result = $upgrader->run(
365 array(
366 'package' => $params['download_url'],
367 'destination' => WP_PLUGIN_DIR,
368 'clear_destination' => true,
369 'clear_working' => true,
370 'hook_extra' => array(
371 'plugin' => 'worker/init.php',
372 ),
373 )
374 );
375 ob_end_clean();
376 if (is_wp_error($result) || !$result) {
377 return array(
378 'error' => 'ManageWP Worker plugin could not be updated.',
379 );
380 } else {
381 return array(
382 'success' => 'ManageWP Worker plugin successfully updated.',
383 );
384 }
385 }
386
387 return array(
388 'error' => 'Bad download path for worker installation file.',
389 );
390 }
391
392 public function updateKeys()
393 {
394 if (!$this->mmb_multisite) {
395 return;
396 }
397
398 global $wpdb;
399
400 $publicKey = $this->get_parent_blog_option('_worker_public_key');
401
402 if (empty($publicKey)) {
403 return;
404 }
405
406 $networkBlogs = $wpdb->get_results("select `blog_id` from `{$wpdb->blogs}`");
407
408 if (empty($networkBlogs)) {
409 return;
410 }
411
412 foreach ($networkBlogs as $details) {
413 update_blog_option($details->blog_id, '_worker_public_key', $publicKey);
414 }
415
416 return;
417 }
418
419 public function mmb_get_user_info($user_info = false, $info = 'login')
420 {
421 if ($user_info === false) {
422 return false;
423 }
424
425 if (strlen(trim($user_info)) == 0) {
426 return false;
427 }
428
429 return get_user_by($info, $user_info);
430 }
431
432 public function mmb_get_transient($option_name)
433 {
434 global $wp_version;
435
436 if (trim($option_name) == '') {
437 return false;
438 }
439
440 if (version_compare($wp_version, '3.4', '>')) {
441 return get_site_transient($option_name);
442 }
443
444 if (!empty($this->mmb_multisite)) {
445 return $this->mmb_get_sitemeta_transient($option_name);
446 }
447
448 $transient = get_option('_site_transient_'.$option_name);
449
450 return apply_filters("site_transient_".$option_name, $transient);
451 }
452
453 public function mmb_get_sitemeta_transient($option_name)
454 {
455 /** @var wpdb $wpdb */
456 global $wpdb;
457 $option_name = '_site_transient_'.$option_name;
458
459 $result = $wpdb->get_var($wpdb->prepare("SELECT `meta_value` FROM `{$wpdb->sitemeta}` WHERE meta_key = '%s' AND `site_id` = '%s'", $option_name, $this->mmb_multisite));
460 $result = maybe_unserialize($result);
461
462 return $result;
463 }
464
465 public function get_master_public_key()
466 {
467 if (!get_option('_worker_public_key')) {
468 return false;
469 }
470
471 return base64_decode(get_option('_worker_public_key'));
472 }
473
474 public function mmb_get_error($error_object)
475 {
476 if (!is_wp_error($error_object)) {
477 return $error_object != '' ? $error_object : '';
478 } else {
479 $errors = array();
480 if (!empty($error_object->error_data)) {
481 foreach ($error_object->error_data as $error_key => $error_string) {
482 $errors[] = str_replace('_', ' ', ucfirst($error_key)).': '.$error_string;
483 }
484 } elseif (!empty($error_object->errors)) {
485 foreach ($error_object->errors as $error_key => $err) {
486 $errors[] = 'Error: '.str_replace('_', ' ', strtolower($error_key));
487 }
488 }
489
490 return implode('<br />', $errors);
491 }
492 }
493
494 public function check_if_pantheon()
495 {
496 return !empty($_ENV['PANTHEON_ENVIRONMENT']) && $_ENV['PANTHEON_ENVIRONMENT'] !== 'dev';
497 }
498
499 public function is_server_writable()
500 {
501 if ($this->check_if_pantheon()) {
502 return false;
503 }
504
505 if (!function_exists('get_filesystem_method')) {
506 include_once ABSPATH.'wp-admin/includes/file.php';
507 }
508
509 if ((!defined('FTP_HOST') || !defined('FTP_USER')) && (get_filesystem_method(array(), false) != 'direct')) {
510 return false;
511 } else {
512 return true;
513 }
514 }
515 }
516