/**
  * Fixes environment once event occurs
  *
  * @param W3_Config $config
  * @param string $event
  * @param W3_Config|null $old_config
  * @throws SelfTestExceptions
  **/
 public function fix_on_event($config, $event, $old_config = null)
 {
     if ($config->get_boolean('cdn.enabled') && !w3_is_cdn_mirror($config->get_string('cdn.engine'))) {
         if ($old_config != null && $config->get_integer('cdn.queue.interval') != $old_config->get_integer('cdn.queue.interval')) {
             $this->unschedule_queue_process();
         }
         if (!wp_next_scheduled('w3_cdn_cron_queue_process')) {
             wp_schedule_event(current_time('timestamp'), 'w3_cdn_cron_queue_process', 'w3_cdn_cron_queue_process');
         }
     } else {
         $this->unschedule_queue_process();
     }
     if ($config->get_boolean('cdn.enabled') && $config->get_boolean('cdn.autoupload.enabled') && !w3_is_cdn_mirror($config->get_string('cdn.engine'))) {
         if ($old_config != null && $config->get_integer('cdn.autoupload.interval') != $old_config->get_integer('cdn.autoupload.interval')) {
             $this->unschedule_upload();
         }
         if (!wp_next_scheduled('w3_cdn_cron_upload')) {
             wp_schedule_event(current_time('timestamp'), 'w3_cdn_cron_upload', 'w3_cdn_cron_upload');
         }
     } else {
         $this->unschedule_upload();
     }
     $exs = new SelfTestExceptions();
     if ($config->get_boolean('cdn.enabled')) {
         try {
             $this->table_create($event == 'activate');
         } catch (Exception $ex) {
             $exs->push($ex);
         }
     }
     if (count($exs->exceptions()) > 0) {
         throw $exs;
     }
 }
 /**
  * Fixes environment after plugin deactivation
  * @throws SelfTestExceptions
  * @return array
  */
 public function fix_after_deactivation()
 {
     $exs = new SelfTestExceptions();
     try {
         $this->delete_addin();
     } catch (FilesystemOperationException $ex) {
         $exs->push($ex);
     }
     $this->unschedule();
     if (count($exs->exceptions()) > 0) {
         throw $exs;
     }
 }
 /**
  * Fixes environment after plugin deactivation
  * @param W3_Config $config
  * @throws SelfTestExceptions
  */
 public function fix_after_deactivation($config)
 {
     $exs = new SelfTestExceptions();
     // call plugin-related handlers
     foreach ($this->get_handlers($config) as $h) {
         try {
             $h->fix_after_deactivation();
         } catch (SelfTestExceptions $ex) {
             $exs->push($ex);
         }
     }
     if (count($exs->exceptions()) > 0) {
         throw $exs;
     }
 }
/**
 * Used to display SelfTestExceptions in UI
 * @param SelfTestExceptions $exs
 * @return array(before_errors = [], required_changes =>, later_errors => [])
 **/
function w3_parse_selftest_exceptions($exs)
{
    $exceptions = $exs->exceptions();
    $commands = '';
    $required_changes = '';
    $before_errors = array();
    $later_errors = array();
    $operation_error_already_shown = false;
    foreach ($exceptions as $ex) {
        if ($ex instanceof FilesystemOperationException) {
            if (!$operation_error_already_shown) {
                $m = $ex->getMessage();
                if (strlen($m) > 0) {
                    $before_errors[] = $m;
                    // if multiple operations failed when
                    // they tried to fix environment - show only first
                    // otherwise can duplication information about
                    // absense of permissions
                    $operation_error_already_shown = true;
                }
                if ($ex instanceof FilesystemWriteException) {
                    $required_changes .= sprintf(__('Create the <strong>%s</strong> file and paste the following text into it:
                    <textarea>%s</textarea> <br />', 'w3-total-cache'), $ex->filename(), esc_textarea($ex->file_contents()));
                } else {
                    if ($ex instanceof FilesystemModifyException) {
                        $modification_content = $ex->file_contents();
                        if (strlen($modification_content) > 0) {
                            $modification_content = '<textarea style="height: 100px; width: 100%;">' . esc_textarea($modification_content) . '</textarea>';
                        }
                        $required_changes .= $ex->modification_description() . $modification_content . '<br />';
                    } else {
                        if ($ex instanceof FilesystemCopyException) {
                            $commands .= 'cp ' . $ex->source_filename() . ' ' . $ex->destination_filename() . '<br />';
                        } else {
                            if ($ex instanceof FilesystemMkdirException) {
                                $commands .= 'mkdir ' . $ex->folder() . '<br />';
                                $commands .= 'chmod 777 ' . $ex->folder() . '<br />';
                            } else {
                                if ($ex instanceof FilesystemRmException) {
                                    $commands .= 'rm ' . $ex->filename() . '<br />';
                                } else {
                                    if ($ex instanceof FilesystemRmdirException) {
                                        $commands .= 'rm -rf ' . $ex->folder() . '<br />';
                                    } else {
                                        if ($ex instanceof FilesystemChmodException) {
                                            $commands .= 'chmod 777 ' . $ex->filename() . '<br />';
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else {
            if ($ex instanceof SelfTestFailedException) {
                $t = $ex->technical_message();
                if (strlen($t) > 0) {
                    $t = '<br />' . '<a class="w3tc_read_technical_info" href="#">' . __('Technical info', 'w3-total-cache') . '</a>' . '<div class="w3tc_technical_info" style="display: none">' . $t . '</div>';
                }
                $later_errors[] = $ex->getMessage() . $t;
            } else {
                // unknown command
                $later_errors[] = $ex->getMessage();
            }
        }
    }
    if (strlen($commands) > 0) {
        $required_changes .= __('Execute next commands in a shell:', 'w3-total-cache') . '<br><strong>' . $commands . '</strong>';
    }
    return array('before_errors' => $before_errors, 'required_changes' => $required_changes, 'later_errors' => $later_errors);
}