MPSUM_Admin.php
7 years ago
MPSUM_Admin_Advanced.php
7 years ago
MPSUM_Admin_Advanced_Preview.php
7 years ago
MPSUM_Admin_Ajax.php
7 years ago
MPSUM_Admin_Bar.php
7 years ago
MPSUM_Admin_Core.php
7 years ago
MPSUM_Admin_Dashboard.php
7 years ago
MPSUM_Admin_Help.php
7 years ago
MPSUM_Admin_Logs.php
7 years ago
MPSUM_Admin_Plugins.php
7 years ago
MPSUM_Admin_Screen_Options.php
7 years ago
MPSUM_Admin_Themes.php
7 years ago
MPSUM_Advanced_Premium.php
7 years ago
MPSUM_Check_Plugin_Install_Status.php
7 years ago
MPSUM_Check_Theme_Install_Status.php
7 years ago
MPSUM_Commands.php
7 years ago
MPSUM_Disable_Updates.php
7 years ago
MPSUM_Disable_Updates_All.php
7 years ago
MPSUM_Disable_Updates_Plugins.php
7 years ago
MPSUM_Disable_Updates_Themes.php
7 years ago
MPSUM_Disable_Updates_Translations.php
7 years ago
MPSUM_Disable_Updates_WordPress.php
7 years ago
MPSUM_Exclude_Users.php
7 years ago
MPSUM_Force_Updates.php
7 years ago
MPSUM_List_Table.php
7 years ago
MPSUM_Logs.php
7 years ago
MPSUM_Logs_List_Table.php
7 years ago
MPSUM_Plugins_List_Table.php
7 years ago
MPSUM_Reset_Options.php
7 years ago
MPSUM_Themes_List_Table.php
7 years ago
MPSUM_UpdraftCentral.php
7 years ago
MPSUM_UpdraftCentral_EUM_Commands.php
7 years ago
MPSUM_Utils.php
7 years ago
easy-updates-manager-notices.php
7 years ago
updraft-notices.php
7 years ago
MPSUM_Admin_Ajax.php
1181 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) die('No direct access.'); |
| 3 | |
| 4 | /** |
| 5 | * Easy Updates Manager Admin Ajax |
| 6 | * Handles ajax requests |
| 7 | * |
| 8 | * @package WordPress |
| 9 | * @since 7.0.1 |
| 10 | */ |
| 11 | class MPSUM_Admin_Ajax { |
| 12 | |
| 13 | /** |
| 14 | * Holds the class instance. |
| 15 | * |
| 16 | * @access static |
| 17 | * @var MPSUM_Admin_Ajax $instance |
| 18 | */ |
| 19 | private static $instance = null; |
| 20 | |
| 21 | /** |
| 22 | * Set a class instance. |
| 23 | * |
| 24 | * @access static |
| 25 | */ |
| 26 | public static function get_instance() { |
| 27 | if (null == self::$instance) { |
| 28 | self::$instance = new self; |
| 29 | } |
| 30 | return self::$instance; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Class constructor. |
| 35 | * |
| 36 | * Initialize the class |
| 37 | * |
| 38 | * @access private |
| 39 | */ |
| 40 | private function __construct() { |
| 41 | add_action('wp_ajax_eum_axios_ajax', array($this, 'axios_ajax_handler')); |
| 42 | add_action('wp_ajax_eum_ajax', array($this, 'ajax_handler')); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Updates plugins tab |
| 47 | */ |
| 48 | public function update_plugins_tab() { |
| 49 | if (!current_user_can('manage_options')) return; |
| 50 | $this->render_plugins_tab(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Updates themes tab |
| 55 | */ |
| 56 | public function update_themes_tab() { |
| 57 | if (!current_user_can('manage_options')) return; |
| 58 | $this->render_themes_tab(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Updates logs tab |
| 63 | */ |
| 64 | public function update_logs_tab() { |
| 65 | if (!current_user_can('manage_options')) return; |
| 66 | $this->render_logs_tab(); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Prepares and return content to render plugins tab via ajax call |
| 72 | */ |
| 73 | private function render_plugins_tab() { |
| 74 | if (!current_user_can('manage_options')) return; |
| 75 | $plugins_table = new MPSUM_Plugins_List_Table(); |
| 76 | $plugins_table->ajax_response(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Prepares and return content to render themes tab via ajax call |
| 81 | */ |
| 82 | private function render_themes_tab() { |
| 83 | if (!current_user_can('manage_options')) return; |
| 84 | $themes_table = new MPSUM_Themes_List_Table(); |
| 85 | $themes_table->ajax_response(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Prepares and return content to render logs tab via ajax call |
| 90 | */ |
| 91 | private function render_logs_tab() { |
| 92 | if (!current_user_can('manage_options')) return; |
| 93 | $logs_table = new MPSUM_Logs_List_Table(); |
| 94 | $logs_table->ajax_response(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Handles ajax all calls |
| 99 | */ |
| 100 | public function axios_ajax_handler() { |
| 101 | |
| 102 | if (!current_user_can('manage_options')) return; |
| 103 | |
| 104 | parse_str(file_get_contents('php://input'), $data); |
| 105 | $sub_action = isset($data['sub_action']) ? $data['sub_action'] : 'get_core_options'; |
| 106 | $nonce = isset($data['_wpnonce']) ? $data['_wpnonce'] : ''; |
| 107 | if (! wp_verify_nonce($nonce, 'eum_nonce') || empty($sub_action)) die('Security check'); |
| 108 | |
| 109 | if (method_exists($this, $sub_action)) { |
| 110 | $results = call_user_func(array($this, $sub_action ), $data); |
| 111 | |
| 112 | if (is_wp_error($results)) { |
| 113 | $results = array( |
| 114 | 'result' => false, |
| 115 | 'error_code' => $results->get_error_code(), |
| 116 | 'error_message' => $results->get_error_message(), |
| 117 | 'error_data' => $results->get_error_data(), |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | wp_send_json($results); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Handles ajax all calls |
| 127 | */ |
| 128 | public function ajax_handler() { |
| 129 | |
| 130 | if (empty($_REQUEST)) return; |
| 131 | |
| 132 | extract($_REQUEST); |
| 133 | |
| 134 | if (!wp_verify_nonce($nonce, 'eum_nonce') || empty($subaction) || 'axios_ajax_handler' == $subaction) die('Security check'); |
| 135 | |
| 136 | $results = array(); |
| 137 | $data = isset($data) ? $data : array(); |
| 138 | if (!method_exists($this, $subaction)) { |
| 139 | do_action('eum_premium_ajax_handler', $subaction, $data); |
| 140 | error_log("EUM: ajax_handler: no such command (".$subaction.")"); |
| 141 | die('No such command'); |
| 142 | } else { |
| 143 | $results = call_user_func(array($this, $subaction), $data); |
| 144 | |
| 145 | // For WP List Table extended class (plugins, themes) result is already returned. |
| 146 | if (is_wp_error($results)) { |
| 147 | $results = array( |
| 148 | 'result' => false, |
| 149 | 'error_code' => $results->get_error_code(), |
| 150 | 'error_message' => $results->get_error_message(), |
| 151 | 'error_data' => $results->get_error_data(), |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | // if nothing was returned for some reason, set as result null. |
| 156 | if (empty($results)) { |
| 157 | $results = array( |
| 158 | 'result' => null |
| 159 | ); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | $result = json_encode($results); |
| 164 | |
| 165 | $json_last_error = json_last_error(); |
| 166 | |
| 167 | // if json_encode returned error then return error. |
| 168 | if ($json_last_error) { |
| 169 | $result = array( |
| 170 | 'result' => false, |
| 171 | 'error_code' => $json_last_error, |
| 172 | 'error_message' => 'json_encode error : '.$json_last_error, |
| 173 | 'error_data' => '', |
| 174 | ); |
| 175 | |
| 176 | $result = json_encode($result); |
| 177 | } |
| 178 | |
| 179 | echo $result; |
| 180 | |
| 181 | die; |
| 182 | |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /** |
| 187 | * Saves core options. Ajax call method from React/Axios |
| 188 | * |
| 189 | * @param array $data An array of option to be saved |
| 190 | * |
| 191 | * @return array An array of core options |
| 192 | */ |
| 193 | public function save_core_options($data) { |
| 194 | |
| 195 | if (!current_user_can('manage_options')) return; |
| 196 | |
| 197 | $id = $data['id']; |
| 198 | $value = $data['value']; |
| 199 | |
| 200 | // Get options |
| 201 | $options = MPSUM_Updates_Manager::get_options('core', true); |
| 202 | if (empty($options)) { |
| 203 | $options = MPSUM_Admin_Core::get_defaults(); |
| 204 | } else { |
| 205 | $options = wp_parse_args($options, MPSUM_Admin_Core::get_defaults()); |
| 206 | } |
| 207 | if (!$this->user_can_update()) return $options; |
| 208 | |
| 209 | $id = sanitize_text_field($id); |
| 210 | $value = sanitize_text_field($value); |
| 211 | |
| 212 | $email_errors = false; |
| 213 | switch ($id) { |
| 214 | case 'automatic-updates-default': |
| 215 | $options['automatic_development_updates'] = 'off'; |
| 216 | $options['automatic_major_updates'] = 'off'; |
| 217 | $options['automatic_minor_updates'] = 'on'; |
| 218 | $options['automatic_plugin_updates'] = 'default'; |
| 219 | $options['automatic_theme_updates'] = 'default'; |
| 220 | $options['automatic_translation_updates'] = 'on'; |
| 221 | $options['automatic_updates'] = 'default'; |
| 222 | break; |
| 223 | case 'automatic-updates-on': |
| 224 | $options['automatic_development_updates'] = 'off'; |
| 225 | $options['automatic_major_updates'] = 'on'; |
| 226 | $options['automatic_minor_updates'] = 'on'; |
| 227 | $options['automatic_plugin_updates'] = 'on'; |
| 228 | $options['automatic_theme_updates'] = 'on'; |
| 229 | $options['automatic_translation_updates'] = 'on'; |
| 230 | $options['automatic_updates'] = 'on'; |
| 231 | break; |
| 232 | case 'automatic-updates-off': |
| 233 | $options['automatic_development_updates'] = 'off'; |
| 234 | $options['automatic_major_updates'] = 'off'; |
| 235 | $options['automatic_minor_updates'] = 'off'; |
| 236 | $options['automatic_plugin_updates'] = 'off'; |
| 237 | $options['automatic_theme_updates'] = 'off'; |
| 238 | $options['automatic_translation_updates'] = 'off'; |
| 239 | $options['automatic_updates'] = 'off'; |
| 240 | break; |
| 241 | case 'automatic-updates-custom': |
| 242 | $options['automatic_updates'] = 'custom'; |
| 243 | break; |
| 244 | case 'automatic-major-updates': |
| 245 | if ('on' == $value) { |
| 246 | $options['automatic_major_updates'] = 'on'; |
| 247 | } else { |
| 248 | $options['automatic_major_updates'] = 'off'; |
| 249 | } |
| 250 | $options['automatic_updates'] = 'custom'; |
| 251 | break; |
| 252 | case 'automatic-minor-updates': |
| 253 | if ('on' == $value) { |
| 254 | $options['automatic_minor_updates'] = 'on'; |
| 255 | } else { |
| 256 | $options['automatic_minor_updates'] = 'off'; |
| 257 | } |
| 258 | $options['automatic_updates'] = 'custom'; |
| 259 | break; |
| 260 | case 'automatic-development-updates': |
| 261 | if ('on' == $value) { |
| 262 | $options['automatic_development_updates'] = 'on'; |
| 263 | } else { |
| 264 | $options['automatic_development_updates'] = 'off'; |
| 265 | } |
| 266 | $options['automatic_updates'] = 'custom'; |
| 267 | break; |
| 268 | case 'automatic-translation-updates': |
| 269 | if ('on' == $value) { |
| 270 | $options['automatic_translation_updates'] = 'on'; |
| 271 | } else { |
| 272 | $options['automatic_translation_updates'] = 'off'; |
| 273 | } |
| 274 | $options['automatic_updates'] = 'custom'; |
| 275 | break; |
| 276 | case 'automatic-plugin-updates-default': |
| 277 | $options['automatic_plugin_updates'] = 'default'; |
| 278 | $options['automatic_updates'] = 'custom'; |
| 279 | break; |
| 280 | case 'automatic-plugin-updates-on': |
| 281 | $options['automatic_plugin_updates'] = 'on'; |
| 282 | $options['automatic_updates'] = 'custom'; |
| 283 | break; |
| 284 | case 'automatic-plugin-updates-off': |
| 285 | $options['automatic_plugin_updates'] = 'off'; |
| 286 | $options['automatic_updates'] = 'custom'; |
| 287 | break; |
| 288 | case 'automatic-plugin-updates-individual': |
| 289 | $options['automatic_plugin_updates'] = 'individual'; |
| 290 | $options['automatic_updates'] = 'custom'; |
| 291 | break; |
| 292 | case 'automatic-theme-updates-default': |
| 293 | $options['automatic_theme_updates'] = 'default'; |
| 294 | $options['automatic_updates'] = 'custom'; |
| 295 | break; |
| 296 | case 'automatic-theme-updates-on': |
| 297 | $options['automatic_theme_updates'] = 'on'; |
| 298 | $options['automatic_updates'] = 'custom'; |
| 299 | break; |
| 300 | case 'automatic-theme-updates-off': |
| 301 | $options['automatic_theme_updates'] = 'off'; |
| 302 | $options['automatic_updates'] = 'custom'; |
| 303 | break; |
| 304 | case 'automatic-theme-updates-individual': |
| 305 | $options['automatic_theme_updates'] = 'individual'; |
| 306 | $options['automatic_updates'] = 'custom'; |
| 307 | break; |
| 308 | case 'disable-updates': |
| 309 | if ('on' == $value) { |
| 310 | $options['all_updates'] = 'on'; |
| 311 | } else { |
| 312 | $options['all_updates'] = 'off'; |
| 313 | } |
| 314 | break; |
| 315 | case 'logs': |
| 316 | if ('on' == $value) { |
| 317 | update_site_option('mpsum_log_table_version', 0); |
| 318 | $options['logs'] = 'on'; |
| 319 | } else { |
| 320 | MPSUM_Logs::drop(); |
| 321 | $options['logs'] = 'off'; |
| 322 | } |
| 323 | break; |
| 324 | case 'core-updates': |
| 325 | if ('on' == $value) { |
| 326 | $options['core_updates'] = 'on'; |
| 327 | } else { |
| 328 | $options['core_updates'] = 'off'; |
| 329 | } |
| 330 | break; |
| 331 | case 'plugin-updates': |
| 332 | if ('on' == $value) { |
| 333 | $options['plugin_updates'] = 'on'; |
| 334 | } else { |
| 335 | $options['plugin_updates'] = 'off'; |
| 336 | } |
| 337 | break; |
| 338 | case 'theme-updates': |
| 339 | if ('on' == $value) { |
| 340 | $options['theme_updates'] = 'on'; |
| 341 | } else { |
| 342 | $options['theme_updates'] = 'off'; |
| 343 | } |
| 344 | break; |
| 345 | case 'translation-updates': |
| 346 | if ('on' == $value) { |
| 347 | $options['translation_updates'] = 'on'; |
| 348 | } else { |
| 349 | $options['translation_updates'] = 'off'; |
| 350 | } |
| 351 | break; |
| 352 | case 'browser-nag': |
| 353 | if ('on' == $value) { |
| 354 | $options['misc_browser_nag'] = 'on'; |
| 355 | } else { |
| 356 | $options['misc_browser_nag'] = 'off'; |
| 357 | } |
| 358 | break; |
| 359 | case 'version-footer': |
| 360 | if ('on' == $value) { |
| 361 | $options['misc_wp_footer'] = 'on'; |
| 362 | } else { |
| 363 | $options['misc_wp_footer'] = 'off'; |
| 364 | } |
| 365 | break; |
| 366 | case 'email-notifications': |
| 367 | if ('on' == $value) { |
| 368 | $options['notification_core_update_emails'] = 'on'; |
| 369 | } else { |
| 370 | $options['notification_core_update_emails'] = 'off'; |
| 371 | } |
| 372 | break; |
| 373 | case 'notification-emails': |
| 374 | if ('unset' === $value) { |
| 375 | $options['email_addresses'] = array(); |
| 376 | break; |
| 377 | } |
| 378 | $emails = explode(',', $value); |
| 379 | foreach ($emails as $index => &$email) { |
| 380 | $email = trim($email); |
| 381 | if (!is_email($email)) { |
| 382 | |
| 383 | // Email error. Get out. |
| 384 | $email_errors = true; |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (! $email_errors) { |
| 390 | $options['email_addresses'] = $emails; |
| 391 | } |
| 392 | break; |
| 393 | case 'update-notification-emails': |
| 394 | if ('weekly' === $value) { |
| 395 | $options['update_notification_updates'] = 'weekly'; |
| 396 | wp_clear_scheduled_hook('eum_notification_updates_monthly'); |
| 397 | if (false === wp_next_scheduled('eum_notification_updates_weekly')) { |
| 398 | wp_schedule_event(time() + 7 * 86400, 'eum_notification_updates_weekly', 'eum_notification_updates_weekly'); |
| 399 | } |
| 400 | } elseif ('monthly' === $value) { |
| 401 | $options['update_notification_updates'] = 'monthly'; |
| 402 | wp_clear_scheduled_hook('eum_notification_updates_weekly'); |
| 403 | if (false === wp_next_scheduled('eum_notification_updates_monthly')) { |
| 404 | wp_schedule_event(time() + 365.25 * 86400 / 12, 'eum_notification_updates_monthly', 'eum_notification_updates_monthly'); |
| 405 | } |
| 406 | } else { |
| 407 | $options['update_notification_updates'] = 'off'; |
| 408 | wp_clear_scheduled_hook('eum_notification_updates_weekly'); |
| 409 | wp_clear_scheduled_hook('eum_notification_updates_monthly'); |
| 410 | } |
| 411 | break; |
| 412 | case 'notification-emails-send_now': |
| 413 | MPSUM_Update_Notifications::get_instance()->maybe_send_update_notification_email(); |
| 414 | break; |
| 415 | } |
| 416 | // Save options |
| 417 | MPSUM_Updates_Manager::update_options($options, 'core'); |
| 418 | |
| 419 | // Return email addresses in format |
| 420 | $value = trim($value); |
| 421 | if ('unset' === $value) { |
| 422 | $options['email_addresses'] = array(); |
| 423 | } |
| 424 | if (is_array($options['email_addresses'])) { |
| 425 | $options['email_addresses'] = implode(',', $options['email_addresses']); |
| 426 | } else { |
| 427 | $options['email_addresses'] = ''; |
| 428 | } |
| 429 | |
| 430 | |
| 431 | // Check automatic updates for fresh installation |
| 432 | if (! isset($options['automatic_updates']) || 'unset' == $options['automatic_updates']) { |
| 433 | $options['automatic_updates'] = 'default'; |
| 434 | } |
| 435 | |
| 436 | // Check if update notification emails is set |
| 437 | if (!isset($options['update_notification_updates'])) { |
| 438 | $options['update_notification_updates'] = 'off'; |
| 439 | } |
| 440 | |
| 441 | // Add error to options for returning |
| 442 | if ($email_errors) { |
| 443 | $options['errors'] = true; |
| 444 | $options['email_addresses'] = $options['email_addresses']; |
| 445 | $options['success'] = false; |
| 446 | } else { |
| 447 | $options['errors'] = false; |
| 448 | $options['email_addresses'] = $options['email_addresses']; |
| 449 | $options['success'] = true; |
| 450 | } |
| 451 | return $options; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Get all core options |
| 456 | * |
| 457 | * @param array $data Data for get options |
| 458 | * @return array - An array of core options |
| 459 | */ |
| 460 | public function get_core_options($data) { |
| 461 | if (!current_user_can('manage_options')) return array(); |
| 462 | // Get options |
| 463 | $options = MPSUM_Updates_Manager::get_options('core', true); |
| 464 | if (empty($options)) { |
| 465 | $options = MPSUM_Admin_Core::get_defaults(); |
| 466 | } else { |
| 467 | $options = wp_parse_args($options, MPSUM_Admin_Core::get_defaults()); |
| 468 | } |
| 469 | |
| 470 | // Set automatic updates defaults if none is selected |
| 471 | if (! isset($options['automatic_updates']) || 'unset' === $options['automatic_updates']) { |
| 472 | |
| 473 | // Check to see if automatic updates are on |
| 474 | // Prepare for mad conditionals |
| 475 | if ('off' == $options['automatic_development_updates'] && 'on' == $options['automatic_major_updates'] && 'on' == $options['automatic_minor_updates'] && 'on' == $options['automatic_plugin_updates'] && 'on' == $options['automatic_theme_updates'] && 'on' == $options['automatic_translation_updates']) { |
| 476 | $options['automatic_updates'] = 'on'; |
| 477 | } elseif ('off' == $options['automatic_development_updates'] && 'off' == $options['automatic_major_updates'] && 'off' == $options['automatic_minor_updates'] && 'off' == $options['automatic_plugin_updates'] && 'off' == $options['automatic_theme_updates'] && 'off' == $options['automatic_translation_updates']) { |
| 478 | $options['automatic_updates'] = 'off'; |
| 479 | } elseif ('off' == $options['automatic_development_updates'] && 'off' == $options['automatic_major_updates'] && 'on' == $options['automatic_minor_updates'] && 'default' == $options['automatic_plugin_updates'] && 'default' == $options['automatic_theme_updates'] && 'on' == $options['automatic_translation_updates']) { |
| 480 | $options['automatic_updates'] = 'default'; |
| 481 | } else { |
| 482 | $options['automatic_updates'] = 'custom'; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // Check if update notification emails is set |
| 487 | if (!isset($options['update_notification_updates'])) { |
| 488 | $options['update_notification_updates'] = 'off'; |
| 489 | } |
| 490 | |
| 491 | if (isset($options['email_addresses']) && ! is_array($options['email_addresses'])) { |
| 492 | $options['email_addresses'] = array(); |
| 493 | } |
| 494 | $options['email_addresses'] = implode(',', $options['email_addresses']); |
| 495 | |
| 496 | // return |
| 497 | return $options; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Save the plugin options based on the passed data. |
| 502 | * |
| 503 | * @param string $data Action to take action on |
| 504 | */ |
| 505 | public function save_plugins_update_options_and_render($data) { |
| 506 | if (!current_user_can('update_plugins')) return ''; |
| 507 | parse_str($data, $updated_options); |
| 508 | $this->save_plugins_update_options($updated_options); |
| 509 | $this->render_plugins_tab(); |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Saves plugin updated options |
| 514 | * |
| 515 | * @param array $updated_options - Updated options from the remote call |
| 516 | */ |
| 517 | public function save_plugins_update_options($updated_options) { |
| 518 | if (!current_user_can('manage_options')) return array(); |
| 519 | $plugins = isset($updated_options['plugins']) ? (array) $updated_options['plugins'] : array(); |
| 520 | $plugins_automatic = isset($updated_options['plugins_automatic']) ? (array) $updated_options['plugins_automatic'] : array(); |
| 521 | $plugin_options = MPSUM_Updates_Manager::get_options('plugins'); |
| 522 | $plugin_automatic_options = MPSUM_Updates_Manager::get_options('plugins_automatic'); |
| 523 | foreach ($plugins as $plugin => $choice) { |
| 524 | if ("false" === $choice) { |
| 525 | $plugin_options[] = $plugin; |
| 526 | if (($key = array_search($plugin, $plugin_automatic_options)) !== false) { |
| 527 | unset($plugin_automatic_options[$key]); |
| 528 | } |
| 529 | } else { |
| 530 | if (($key = array_search($plugin, $plugin_options)) !== false) { |
| 531 | unset($plugin_options[$key]); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | |
| 537 | foreach ($plugins_automatic as $plugin => $choice) { |
| 538 | if ("true" === $choice) { |
| 539 | $plugin_automatic_options[] = $plugin; |
| 540 | if (($key = array_search($plugin, $plugin_options)) !== false) { |
| 541 | unset($plugin_options[$key]); |
| 542 | } |
| 543 | } else { |
| 544 | if (($key = array_search($plugin, $plugin_automatic_options)) !== false) { |
| 545 | unset($plugin_automatic_options[$key]); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | $this->plugins_update_all_options($plugin_options, $plugin_automatic_options); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Save the plugin options based on the passed action. |
| 555 | * |
| 556 | * @since 7.0.2 |
| 557 | * @param string $data Data from ajax call |
| 558 | */ |
| 559 | public function bulk_action_plugins_update_options_and_render($data) { |
| 560 | if (!current_user_can('update_plugins')) return ''; |
| 561 | parse_str($data, $updated_options); |
| 562 | $this->bulk_action_plugins_update_options($updated_options); |
| 563 | $this->render_plugins_tab(); |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Saves plugin options which are updated using bulk actions in UC |
| 568 | * |
| 569 | * @param array $updated_options - Updated options from the remote call |
| 570 | */ |
| 571 | public function bulk_action_plugins_update_options($updated_options) { |
| 572 | if (!current_user_can('update_plugins')) return array(); |
| 573 | |
| 574 | if (isset($updated_options['action']) && -1 != $updated_options['action']) { |
| 575 | $action = $updated_options['action']; |
| 576 | } |
| 577 | if (isset($updated_options['action2']) && -1 != $updated_options['action2']) { |
| 578 | $action = $updated_options['action2']; |
| 579 | } |
| 580 | |
| 581 | $plugins = isset($updated_options['checked']) ? (array) $updated_options['checked'] : array(); |
| 582 | $plugin_options = MPSUM_Updates_Manager::get_options('plugins'); |
| 583 | $plugin_automatic_options = MPSUM_Updates_Manager::get_options('plugins_automatic'); |
| 584 | |
| 585 | switch ($action) { |
| 586 | case 'disallow-update-selected': |
| 587 | foreach ($plugins as $plugin) { |
| 588 | $plugin_options[] = $plugin; |
| 589 | if (($key = array_search($plugin, $plugin_automatic_options)) !== false) { |
| 590 | unset($plugin_automatic_options[$key]); |
| 591 | } |
| 592 | } |
| 593 | break; |
| 594 | case 'allow-update-selected': |
| 595 | foreach ($plugins as $plugin) { |
| 596 | if (($key = array_search($plugin, $plugin_options)) !== false) { |
| 597 | unset($plugin_options[$key]); |
| 598 | } |
| 599 | } |
| 600 | break; |
| 601 | case 'allow-automatic-selected': |
| 602 | foreach ($plugins as $plugin) { |
| 603 | $plugin_automatic_options[] = $plugin; |
| 604 | if (($key = array_search($plugin, $plugin_options)) !== false) { |
| 605 | unset($plugin_options[$key]); |
| 606 | } |
| 607 | } |
| 608 | break; |
| 609 | case 'disallow-automatic-selected': |
| 610 | foreach ($plugins as $plugin) { |
| 611 | if (($key = array_search($plugin, $plugin_automatic_options)) !== false) { |
| 612 | unset($plugin_automatic_options[$key]); |
| 613 | } |
| 614 | } |
| 615 | break; |
| 616 | default: |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | $this->plugins_update_all_options($plugin_options, $plugin_automatic_options); |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Updates all plugin update options |
| 625 | * |
| 626 | * @param array $plugin_options An array of plugin update options |
| 627 | * @param array $plugin_automatic_options An array of plugin automatic update options |
| 628 | * |
| 629 | * @return array |
| 630 | */ |
| 631 | private function plugins_update_all_options($plugin_options, $plugin_automatic_options) { |
| 632 | if (!current_user_can('update_plugins')) return array(); |
| 633 | $plugin_options = array_values(array_unique($plugin_options)); |
| 634 | $plugin_automatic_options = array_values(array_unique($plugin_automatic_options)); |
| 635 | $options = MPSUM_Updates_Manager::get_options(); |
| 636 | $options['plugins'] = $plugin_options; |
| 637 | $options['plugins_automatic'] = $plugin_automatic_options; |
| 638 | MPSUM_Updates_Manager::update_options($options); |
| 639 | return $options; |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Save the theme options based on the passed data. |
| 644 | * |
| 645 | * @param string $data Action to take action on |
| 646 | */ |
| 647 | public function save_themes_update_options_and_render($data) { |
| 648 | if (!current_user_can('update_themes')) return ''; |
| 649 | parse_str($data, $updated_options); |
| 650 | $this->save_themes_update_options($updated_options); |
| 651 | $this->render_themes_tab(); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Saves theme updated options |
| 656 | * |
| 657 | * @param array $updated_options - Updated options from the remote call |
| 658 | */ |
| 659 | public function save_themes_update_options($updated_options) { |
| 660 | if (!current_user_can('update_themes')) return array(); |
| 661 | $themes = isset($updated_options['themes']) ? (array) $updated_options['themes'] : array(); |
| 662 | $themes_automatic = isset($updated_options['themes_automatic']) ? (array) $updated_options['themes_automatic'] : array(); |
| 663 | $theme_options = MPSUM_Updates_Manager::get_options('themes'); |
| 664 | $theme_automatic_options = MPSUM_Updates_Manager::get_options('themes_automatic'); |
| 665 | foreach ($themes as $theme => $choice) { |
| 666 | if ("false" === $choice) { |
| 667 | $theme_options[] = $theme; |
| 668 | if (($key = array_search($theme, $theme_automatic_options)) !== false) { |
| 669 | unset($theme_automatic_options[$key]); |
| 670 | } |
| 671 | } else { |
| 672 | if (($key = array_search($theme, $theme_options)) !== false) { |
| 673 | unset($theme_options[$key]); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | foreach ($themes_automatic as $theme => $choice) { |
| 679 | if ("true" === $choice) { |
| 680 | $theme_automatic_options[] = $theme; |
| 681 | if (($key = array_search($theme, $theme_options)) !== false) { |
| 682 | unset($theme_options[$key]); |
| 683 | } |
| 684 | } else { |
| 685 | if (($key = array_search($theme, $theme_automatic_options)) !== false) { |
| 686 | unset($theme_automatic_options[$key]); |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | $this->themes_update_all_options($theme_options, $theme_automatic_options); |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Save the theme options based on the passed action. |
| 696 | * |
| 697 | * @since 7.0.2 |
| 698 | * @param string $data Data from ajax call |
| 699 | */ |
| 700 | public function bulk_action_themes_update_options_and_render($data) { |
| 701 | if (!current_user_can('update_themes')) return ''; |
| 702 | parse_str($data, $updated_options); |
| 703 | $this->bulk_action_themes_update_options($updated_options); |
| 704 | $this->render_themes_tab(); |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Saves plugin options which are updated using bulk actions in UC |
| 709 | * |
| 710 | * @param array $updated_options - Updated options from the remote call |
| 711 | */ |
| 712 | public function bulk_action_themes_update_options($updated_options) { |
| 713 | if (!current_user_can('update_themes')) return array(); |
| 714 | |
| 715 | if (isset($updated_options['action']) && -1 != $updated_options['action']) { |
| 716 | $action = $updated_options['action']; |
| 717 | } |
| 718 | if (isset($updated_options['action2']) && -1 != $updated_options['action2']) { |
| 719 | $action = $updated_options['action2']; |
| 720 | } |
| 721 | |
| 722 | $themes = isset($updated_options['checked']) ? (array) $updated_options['checked'] : array(); |
| 723 | $theme_options = MPSUM_Updates_Manager::get_options('themes'); |
| 724 | $theme_automatic_options = MPSUM_Updates_Manager::get_options('themes_automatic'); |
| 725 | |
| 726 | switch ($action) { |
| 727 | |
| 728 | case 'disallow-update-selected': |
| 729 | foreach ($themes as $theme) { |
| 730 | $theme_options[] = $theme; |
| 731 | if (($key = array_search($theme, $theme_automatic_options)) !== false) { |
| 732 | unset($theme_automatic_options[$key]); |
| 733 | } |
| 734 | } |
| 735 | break; |
| 736 | case 'allow-update-selected': |
| 737 | foreach ($themes as $theme) { |
| 738 | if (($key = array_search($theme, $theme_options)) !== false) { |
| 739 | unset($theme_options[$key]); |
| 740 | } |
| 741 | } |
| 742 | break; |
| 743 | case 'allow-automatic-selected': |
| 744 | foreach ($themes as $theme) { |
| 745 | $theme_automatic_options[] = $theme; |
| 746 | if (($key = array_search($theme, $theme_options)) !== false) { |
| 747 | unset($theme_options[$key]); |
| 748 | } |
| 749 | } |
| 750 | break; |
| 751 | case 'disallow-automatic-selected': |
| 752 | foreach ($themes as $theme) { |
| 753 | if (($key = array_search($theme, $theme_automatic_options)) !== false) { |
| 754 | unset($theme_automatic_options[$key]); |
| 755 | } |
| 756 | } |
| 757 | break; |
| 758 | default: |
| 759 | return; |
| 760 | } |
| 761 | $this->themes_update_all_options($theme_options, $theme_automatic_options); |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Updates all theme update options |
| 766 | * |
| 767 | * @param array $theme_options An array of theme update options |
| 768 | * @param array $theme_automatic_options An array of theme automatic update options |
| 769 | * |
| 770 | * @return array |
| 771 | */ |
| 772 | private function themes_update_all_options($theme_options, $theme_automatic_options) { |
| 773 | if (!current_user_can('update_themes')) return array(); |
| 774 | $theme_options = array_values(array_unique($theme_options)); |
| 775 | $theme_automatic_options = array_values(array_unique($theme_automatic_options)); |
| 776 | $options = MPSUM_Updates_Manager::get_options(); |
| 777 | $options['themes'] = $theme_options; |
| 778 | $options['themes_automatic'] = $theme_automatic_options; |
| 779 | MPSUM_Updates_Manager::update_options($options); |
| 780 | return $options; |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * Saves excluded users in options |
| 785 | * |
| 786 | * @param array $data An array of excludes users and other data |
| 787 | * |
| 788 | * @return mixed|string Returns update message if successful |
| 789 | */ |
| 790 | public function save_excluded_users($data) { |
| 791 | if (!current_user_can('promote_users')) return ''; |
| 792 | parse_str($data, $updated_options); |
| 793 | $users = $updated_options['mpsum_excluded_users']; |
| 794 | $advanced_options = MPSUM_Updates_Manager::get_options('advanced'); |
| 795 | if (!is_array($users) || empty($users)) return; |
| 796 | $users_to_save = array(); |
| 797 | foreach ($users as $index => $user_id) { |
| 798 | $user_id = absint($user_id); |
| 799 | if (0 === $user_id) continue; |
| 800 | $users_to_save[] = $user_id; |
| 801 | } |
| 802 | $advanced_options['excluded_users'] = $users_to_save; |
| 803 | MPSUM_Updates_Manager::update_options($advanced_options, 'advanced'); |
| 804 | $message = __('The exclusion of users option has been updated.', 'stops-core-theme-and-plugin-updates'); |
| 805 | return $message; |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * Checks what sites a plugin is installed for on multisite |
| 810 | * |
| 811 | * @param array $data An array with the filename of the plugin |
| 812 | */ |
| 813 | public function get_multisite_installs_from_plugin($data) { |
| 814 | |
| 815 | if (!current_user_can('manage_options')) return array(); |
| 816 | |
| 817 | $plugin_file = $data['plugin_file']; |
| 818 | |
| 819 | // Load up constructor and fire transient checker |
| 820 | $instance = MPSUM_Check_Plugin_Install_Status::get_instance(); |
| 821 | |
| 822 | // Multisite transient with site data |
| 823 | $transient = get_site_transient('eum_all_sites_active_plugins'); |
| 824 | if (empty($transient) || false === $transient || !is_array($transient)) { |
| 825 | wp_send_json(array('message' => __('This plugin is not installed on any sites.', 'stops-core-theme-and-plugin-updates'))); |
| 826 | } |
| 827 | |
| 828 | // Get sites |
| 829 | $sites = $instance->get_sites(); |
| 830 | |
| 831 | // Get all Plugins |
| 832 | $plugins = get_plugins(); |
| 833 | |
| 834 | // Get blank html |
| 835 | $html = ''; |
| 836 | |
| 837 | // Get HTML Placeeholder |
| 838 | $html_ul = ''; |
| 839 | |
| 840 | foreach ($transient as $site_id => $plugins_installed) { |
| 841 | $site_name = ''; |
| 842 | $site_url = ''; |
| 843 | $plugins_stored = array(); |
| 844 | |
| 845 | foreach ($sites as $site) { |
| 846 | if ($site_id == $site->blog_id) { |
| 847 | $site_name = $site_id . ': ' . $site->domain . $site->path; |
| 848 | $site_url = get_admin_url($site->blog_id); |
| 849 | break; |
| 850 | } |
| 851 | } |
| 852 | foreach ($plugins_installed as $plugin_file_installed) { |
| 853 | if ($plugin_file === $plugin_file_installed) { |
| 854 | $plugins_stored[] = $plugin_file_installed; |
| 855 | } |
| 856 | } |
| 857 | if (!empty($plugins_stored)) { |
| 858 | $html .= sprintf('<li><a href="%s">%s</a></li>', esc_url($site_url), esc_html($site_name)); |
| 859 | } |
| 860 | } |
| 861 | if (empty($html)) { |
| 862 | wp_send_json(array('message' => '<div class="mpsum-error mpsum-bold">' . __('This plugin is not installed on any sites. Consider removing it.', 'stops-core-theme-and-plugin-updates') . '</div>')); |
| 863 | } else { |
| 864 | $html_ul = '<ul>'; |
| 865 | $html_ul .= $html; |
| 866 | $html_ul .= '</ul>'; |
| 867 | $html = '<div class="mpsum-notice mpsum-bold">' . __('The following sites have this plugin installed', 'stops-core-theme-and-plugin-updates') . $html_ul . '</div>'; |
| 868 | wp_send_json(array('message' => $html)); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Checks what sites a plugin is isntalled for on multisite |
| 874 | * |
| 875 | * @param array $data An array with the filename of the plugin |
| 876 | */ |
| 877 | public function get_multisite_installs_from_theme($data) { |
| 878 | |
| 879 | if (!current_user_can('manage_options')) return array(); |
| 880 | |
| 881 | $stylesheet = $data['stylesheet']; |
| 882 | |
| 883 | // Load up constructor and fire transient checker |
| 884 | $instance = MPSUM_Check_Theme_Install_Status::get_instance(); |
| 885 | |
| 886 | // Multisite transient with site data |
| 887 | $transient = get_site_transient('eum_all_sites_active_themes'); |
| 888 | if (empty($transient) || false === $transient || !is_array($transient)) { |
| 889 | wp_send_json(array('message' => __('This theme is not installed on any sites.', 'stops-core-theme-and-plugin-updates'))); |
| 890 | } |
| 891 | |
| 892 | // Get sites |
| 893 | $sites = $instance->get_sites(); |
| 894 | |
| 895 | // Get blank html |
| 896 | $html = ''; |
| 897 | |
| 898 | // Get HTML Placeeholder |
| 899 | $html_ul = ''; |
| 900 | |
| 901 | foreach ($transient as $site_id => $theme_installed) { |
| 902 | $site_name = ''; |
| 903 | $site_url = ''; |
| 904 | $themes_stored = array(); |
| 905 | foreach ($sites as $site) { |
| 906 | if ($site_id == $site->blog_id) { |
| 907 | $site_id = $site->blog_id; |
| 908 | $site_name = $site_id . ': ' . $site->domain . $site->path; |
| 909 | $site_url = get_admin_url($site->blog_id); |
| 910 | break; |
| 911 | } |
| 912 | } |
| 913 | $themes = wp_get_themes(array('allowed' => 'true', 'blog_id' => $site_id)); |
| 914 | if (!empty($themes)) { |
| 915 | if (array_key_exists($stylesheet, $themes)) { |
| 916 | // Determine of theme is active on the site |
| 917 | global $wpdb; |
| 918 | switch_to_blog($site_id); |
| 919 | $option = get_option('stylesheet'); |
| 920 | if ($stylesheet == $option) { |
| 921 | $html .= sprintf('<li><a href="%s">%s</a></li>', esc_url($site_url), esc_html($site_name)); |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | restore_current_blog(); |
| 927 | if (empty($html)) { |
| 928 | wp_send_json(array('message' => '<div class="mpsum-error mpsum-bold">' . __('This theme is not active on any sites. Consider removing it.', 'stops-core-theme-and-plugin-updates') . '</div>')); |
| 929 | } else { |
| 930 | $html_ul = '<ul>'; |
| 931 | $html_ul .= $html; |
| 932 | $html_ul .= '</ul>'; |
| 933 | $html = '<div class="mpsum-notice mpsum-bold">' . __('The following sites have this theme activated.', 'stops-core-theme-and-plugin-updates') . $html_ul . '</div>'; |
| 934 | wp_send_json(array('message' => $html)); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * Disables the admin bar |
| 940 | * |
| 941 | * @return json array with message. |
| 942 | */ |
| 943 | public function disable_admin_bar() { |
| 944 | if (!current_user_can('manage_options')) return array(); |
| 945 | $options = MPSUM_Updates_Manager::get_options('options'); |
| 946 | $options['enable_admin_bar'] = 'off'; |
| 947 | MPSUM_Updates_Manager::update_options($options, 'core'); |
| 948 | wp_send_json(array('message' => __('The admin bar has been disabled.', 'stops-core-theme-and-plugin-updates'))); |
| 949 | } |
| 950 | |
| 951 | /** |
| 952 | * Enables the admin bar |
| 953 | * |
| 954 | * @return json array with message. |
| 955 | */ |
| 956 | public function enable_admin_bar() { |
| 957 | if (!current_user_can('manage_options')) return array(); |
| 958 | $options = MPSUM_Updates_Manager::get_options('options'); |
| 959 | $options['enable_admin_bar'] = 'on'; |
| 960 | MPSUM_Updates_Manager::update_options($options, 'core'); |
| 961 | wp_send_json(array('message' => __('The admin bar has been enabled. Please refresh to see the admn bar menu.', 'stops-core-theme-and-plugin-updates'))); |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Resets all update options |
| 966 | * |
| 967 | * @return mixed|string Returns update message, if successful. |
| 968 | */ |
| 969 | public function reset_options() { |
| 970 | if (!current_user_can('delete_plugins')) return; |
| 971 | global $wpdb; |
| 972 | // Reset options |
| 973 | MPSUM_Updates_Manager::update_options(array()); |
| 974 | |
| 975 | // Remove table version |
| 976 | delete_site_option('mpsum_log_table_version'); |
| 977 | |
| 978 | // Remove Webhook |
| 979 | delete_site_option('easy_updates_manager_webhook'); |
| 980 | |
| 981 | // Remove whitelist |
| 982 | delete_site_option('easy_updates_manager_enable_notices'); |
| 983 | delete_site_option('easy_updates_manager_name'); |
| 984 | delete_site_option('easy_updates_manager_author'); |
| 985 | delete_site_option('easy_updates_manager_url'); |
| 986 | |
| 987 | // Remove notices timeout |
| 988 | delete_site_option('easy_updates_manager_dismiss_dash_notice_until'); |
| 989 | delete_site_option('easy_updates_manager_dismiss_eum_notice_until'); |
| 990 | delete_site_option('easy_updates_manager_dismiss_page_notice_until'); |
| 991 | delete_site_option('easy_updates_manager_dismiss_season_notice_until'); |
| 992 | delete_site_option('easy_updates_manager_dismiss_survey_notice_until'); |
| 993 | |
| 994 | // Update option to show options are reset |
| 995 | update_site_option('easy_updates_manager_reset', 'true'); |
| 996 | |
| 997 | // Remove multisite plugins and themes transient |
| 998 | delete_site_transient('eum_all_sites_active_plugins'); |
| 999 | delete_site_transient('eum_all_sites_active_themes'); |
| 1000 | |
| 1001 | // Remove logs table |
| 1002 | MPSUM_Logs::drop(); |
| 1003 | |
| 1004 | // Remove Plugin Check Options and Transients |
| 1005 | delete_site_transient('eum_plugins_removed_from_directory'); |
| 1006 | if (is_multisite()) { |
| 1007 | $options_sql = "delete from {$wpdb->sitemeta} where meta_key like 'eum_plugin_removed_%'"; |
| 1008 | $wpdb->query($options_sql); |
| 1009 | } else { |
| 1010 | $options_sql = "delete from {$wpdb->options} where option_name like 'eum_plugin_removed_%'"; |
| 1011 | $wpdb->query($options_sql); |
| 1012 | } |
| 1013 | |
| 1014 | // Remove plugin safe mode options |
| 1015 | if (is_multisite()) { |
| 1016 | $safe_mode_sql = "delete from {$wpdb->sitemeta} where meta_key like '%eum_plugin_safe_mode_%'"; |
| 1017 | $wpdb->query($safe_mode_sql); |
| 1018 | } else { |
| 1019 | $safe_mode_sql = "delete from {$wpdb->options} where option_name like '%eum_plugin_safe_mode_%'"; |
| 1020 | $wpdb->query($safe_mode_sql); |
| 1021 | } |
| 1022 | |
| 1023 | // Remove active plugins option |
| 1024 | delete_site_option('eum_active_pre_restore_plugins'); |
| 1025 | delete_site_option('eum_active_pre_restore_plugins_multisite'); |
| 1026 | |
| 1027 | // Remove transients when someone disables plugin, theme, or core updates |
| 1028 | delete_site_transient('eum_core_checked'); |
| 1029 | delete_site_transient('eum_themes_checked'); |
| 1030 | delete_site_transient('eum_plugins_checked'); |
| 1031 | |
| 1032 | $message = __('The plugin settings have now been reset.', 'stops-core-theme-and-plugin-updates'); |
| 1033 | return $message; |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * Forces update to take place immediately |
| 1038 | * |
| 1039 | * @return mixed|string Returns update initialized message, if successful. |
| 1040 | */ |
| 1041 | public function force_updates() { |
| 1042 | if (!$this->user_can_update()) return; |
| 1043 | $ran_immediately = false; |
| 1044 | delete_site_transient('MPSUM_PLUGINS'); |
| 1045 | delete_site_transient('MPSUM_THEMES'); |
| 1046 | if (function_exists('wp_maybe_auto_update')) { |
| 1047 | /** |
| 1048 | * Whether to delete the auto update lock file |
| 1049 | * |
| 1050 | * Whether to delete the auto update lock file. |
| 1051 | * |
| 1052 | * @since 8.0.1 |
| 1053 | * |
| 1054 | * @param bool Ignore deleting the lock file (default false) |
| 1055 | * @param string The lock file expiration if exists |
| 1056 | */ |
| 1057 | $disable_lock = apply_filters('eum_force_updates_disable_lock', false, get_option('auto_updater.lock')); |
| 1058 | if (true === $disable_lock) { |
| 1059 | delete_option('auto_updater.lock'); |
| 1060 | } |
| 1061 | wp_maybe_auto_update(); |
| 1062 | $ran_immediately = true; |
| 1063 | } else { |
| 1064 | $overdue = $this->howmany_overdue_crons(); |
| 1065 | if ($overdue >=4) { |
| 1066 | wp_schedule_single_event(time() + 45, 'wp_maybe_auto_update'); |
| 1067 | } else { |
| 1068 | wp_schedule_single_event(time(), 'wp_maybe_auto_update'); |
| 1069 | } |
| 1070 | } |
| 1071 | $result = array( |
| 1072 | 'message' => __('Force update checks have been initialized.', 'stops-core-theme-and-plugin-updates'), |
| 1073 | 'ran_immediately' => $ran_immediately, |
| 1074 | 'update_data' => $this->wp_get_update_data() |
| 1075 | ); |
| 1076 | return $result; |
| 1077 | } |
| 1078 | |
| 1079 | /** |
| 1080 | * Gets update data. |
| 1081 | * |
| 1082 | * Checks core, plugin, theme and translations has updates or not. If it has it includes its count |
| 1083 | * |
| 1084 | * @return array An array of update data |
| 1085 | */ |
| 1086 | private function wp_get_update_data() { |
| 1087 | |
| 1088 | $update_data = wp_get_update_data(); |
| 1089 | if ($update_data['counts']['total'] > 0) { |
| 1090 | $update_data['admin_bar_link'] = sprintf('<a class="ab-item" href="%1$s" title="%2$s"><span class="ab-icon"></span><span class="ab-label">%3$s</span><span class="screen-reader-text">%2$s</span></a>', esc_url(self_admin_url('update-core.php')), $this->get_admin_bar_title($update_data), $update_data['counts']['total']); |
| 1091 | $update_data['updates_link'] = sprintf('%1$s <span class="update-plugins count-%2$s"><span class="update-count">%2$s</span></span>', __('Updates', 'stops-core-theme-and-plugin-updates'), $update_data['counts']['total']); |
| 1092 | $update_data['plugins_link'] = sprintf('%1$s <span class="update-plugins count-%2$s"><span class="plugin-count">%2$s</span></span>', __('Plugins', 'stops-core-theme-and-plugin-updates'), $update_data['counts']['plugins']); |
| 1093 | $update_data['themes_link'] = sprintf('%1$s <span class="update-plugins count-%2$s"><span class="plugin-count">%2$s</span></span>', __('Themes', 'stops-core-theme-and-plugin-updates'), $update_data['counts']['themes']); |
| 1094 | } |
| 1095 | $update_data['footer_upgrade_link'] = core_update_footer(); // This isn't working return old version |
| 1096 | return $update_data; |
| 1097 | } |
| 1098 | |
| 1099 | /** |
| 1100 | * Constructs title attribute string based on update data |
| 1101 | * |
| 1102 | * @param array $update_data An array of update data |
| 1103 | * |
| 1104 | * @return string A string to be displayed as title attribute |
| 1105 | */ |
| 1106 | private function get_admin_bar_title($update_data) { |
| 1107 | $title = array(); |
| 1108 | if ($update_data['counts']['wordpress'] > 0) { |
| 1109 | $title[] = sprintf(_n('%s WordPress Update', '%s WordPress Updates', $update_data['counts']['wordpress'], 'stops-core-theme-and-plugin-updates'), number_format_i18n($update_data['counts']['wordpress'])); |
| 1110 | } |
| 1111 | if ($update_data['counts']['plugins'] > 0) { |
| 1112 | $title[] = sprintf(_n('%s Plugin Update', '%s Plugin Updates', $update_data['counts']['plugins'], 'stops-core-theme-and-plugin-updates'), number_format_i18n($update_data['counts']['plugins'])); |
| 1113 | } |
| 1114 | if ($update_data['counts']['themes'] > 0) { |
| 1115 | $title[] = sprintf(_n('%s Theme Update', '%s Theme Updates', $update_data['counts']['themes'], 'stops-core-theme-and-plugin-updates'), number_format_i18n($update_data['counts']['themes'])); |
| 1116 | } |
| 1117 | if ($update_data['counts']['translations'] > 0) { |
| 1118 | $title[] = __('Translation Updates', 'stops-core-theme-and-plugin-updates'); |
| 1119 | } |
| 1120 | return implode(',', $title); |
| 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * Get a count for the number of overdue cron jobs |
| 1125 | * |
| 1126 | * @return Integer - how many cron jobs are overdue |
| 1127 | */ |
| 1128 | private function howmany_overdue_crons() { |
| 1129 | $how_many_overdue = 0; |
| 1130 | if (function_exists('_get_cron_array') || (is_file(ABSPATH.WPINC.'/cron.php') && include_once(ABSPATH.WPINC.'/cron.php') && function_exists('_get_cron_array'))) { |
| 1131 | $crons = _get_cron_array(); |
| 1132 | if (is_array($crons)) { |
| 1133 | $timenow = time(); |
| 1134 | foreach ($crons as $jt => $job) { |
| 1135 | if ($jt < $timenow) $how_many_overdue++; |
| 1136 | } |
| 1137 | } |
| 1138 | } |
| 1139 | return $how_many_overdue; |
| 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | * Enables logs from advanced tab |
| 1144 | * |
| 1145 | * @return mixed|string Returns enabled message, if successful. |
| 1146 | */ |
| 1147 | public function enable_logs() { |
| 1148 | if (!current_user_can('publish_posts')) return; |
| 1149 | $options = MPSUM_Updates_Manager::get_options('core'); |
| 1150 | if (empty($options)) { |
| 1151 | $options = MPSUM_Admin_Core::get_defaults(); |
| 1152 | } |
| 1153 | $options['logs'] = 'on'; |
| 1154 | MPSUM_Updates_Manager::update_options($options, 'core'); |
| 1155 | $message = __('Logs are now enabled', 'stops-core-theme-and-plugin-updates'); |
| 1156 | return $message; |
| 1157 | } |
| 1158 | |
| 1159 | /** |
| 1160 | * Clears logs |
| 1161 | * |
| 1162 | * @return mixed|string Return logs emptied message, if successful. |
| 1163 | */ |
| 1164 | public function clear_logs() { |
| 1165 | if (!current_user_can('delete_posts')) return; |
| 1166 | MPSUM_Logs::clear(); |
| 1167 | $message = __('Logs have been emptied', 'stops-core-theme-and-plugin-updates'); |
| 1168 | return $message; |
| 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * Decides whether current user can update core, plugin and themes |
| 1173 | * |
| 1174 | * @return bool Returns true if user can update otherwise returns false. |
| 1175 | */ |
| 1176 | private function user_can_update() { |
| 1177 | if (!current_user_can('update_core') || !current_user_can('update_plugins') || !current_user_can('update_themes')) return false; |
| 1178 | return true; |
| 1179 | } |
| 1180 | } |
| 1181 |