/**
  * Updates old site options to new format, if necessary.
  *
  * @param string $to_version Version threshold we are updating "to". So, old versions of plugin BELOW this
  *                        	 parameter will have their options updated "to" that version, and so on until
  *                           the current version.
  * @return void
  * @author Paul Hughes
  * @since 3.0
  */
 public function updateOptions($to_version)
 {
     switch ($to_version) {
         case '2.0.13':
             update_option('muut_forum_name', get_option('moot_forum_name', ''));
             update_option('muut_api_key', get_option('moot_api_key', ''));
             update_option('muut_secret_key', get_option('moot_secret_key', ''));
             update_option('muut_language', get_option('moot_language', ''));
             update_option('muut_generate', get_option('moot_generate', ''));
             update_option('muut_comments_under_forums', get_option('moot_comments_under_forums', ''));
             delete_option('moot_forum_name');
             delete_option('moot_api_key');
             delete_option('moot_secret_key');
             delete_option('moot_language');
             delete_option('moot_generate');
             delete_option('moot_comments_under_forums');
             break;
         case '3.0':
             if (get_option('muut_forum_name', '') != '' && $this->oldVersion == '0') {
                 $this->oldVersion = '2.0.13';
             }
             $new_settings = array('forum_name' => get_option('muut_forum_name', ''), 'language' => get_option('muut_language', ''), 'replace_comments' => get_option('muut_generate', '') == '' ? '0' : '1', 'show_comments_in_forum_default' => get_option('muut_comments_under_forums', '') == '' ? '0' : '1', 'subscription_api_key' => get_option('muut_api_key', ''), 'subscription_secret_key' => get_option('muut_secret_key', ''), 'subscription_use_sso' => get_option('muut_api_key', false) && get_option('muut_secret_key', false) ? true : false, 'comments_base_domain' => $this->oldVersion == '0' ? substr(get_home_url(null, null, 'http'), 7) : 'wordpress');
             // muut()->setOptions() is a protected method, so we have to do it one-by-one.
             foreach ($new_settings as $setting => $value) {
                 if ($value != '') {
                     $value = $value === 'true' ? 1 : $value;
                     muut()->setOption($setting, $value);
                 }
             }
             delete_option('muut_forum_name');
             delete_option('muut_language');
             delete_option('muut_generate');
             delete_option('muut_api_key');
             delete_option('muut_secret_key');
             delete_option('muut_comments_under_forums');
             break;
         case '3.0.2':
             if (!is_null(muut()->getOption('use_custom_s3_bucket'))) {
                 if (muut()->getOption('use_custom_s3_bucket')) {
                     muut()->setOption('removed_s3_support', '1');
                     flush_rewrite_rules(true);
                 }
                 muut()->deleteOption('use_custom_s3_bucket');
             }
             break;
         case '3.0.2.3':
             if (muut()->getOption('subscription_use_sso')) {
                 muut()->setOption('subscription_use_signed_setup', 1);
             }
             break;
         case '3.0.3':
             muut()->setOption('dismissed_review_request', true);
             $dismissed_review_request = muut()->getOption('dismissed_review_request', false);
             $dismissed_notices = (array) muut()->getOption('dismissed_notices', array());
             if ($dismissed_review_request) {
                 $dismissed_notices['review_request'] = $dismissed_review_request;
             }
             $dismissed_notices['update_notice'] = false;
             muut()->setOption('dismissed_notices', $dismissed_notices);
             muut()->deleteOption('dismissed_review_request');
             break;
         case '3.0.4':
             $use_s3_bucket = muut()->getOption('use_custom_s3_bucket', '0');
             $s3_bucket = muut()->getOption('custom_s3_bucket_name', '');
             if ($use_s3_bucket == 1 && $s3_bucket) {
                 $forum_name = muut()->getForumName();
                 if (substr($s3_bucket, -34, 34) == 's3-website-us-east-1.amazonaws.com') {
                     $s3_bucket = str_replace(array('http://', 'https://', '.s3-website-us-east-1.amazonaws.com'), '', $s3_bucket);
                 }
                 $url_protocol = apply_filters('muut_s3_bucket_url_protocol', 'http');
                 $url = $url_protocol . '://' . $s3_bucket . '.' . apply_filters('muut_amazon_s3_url', Muut::AMAZONS3URL) . '/' . $forum_name;
                 $valid = Muut_Field_Validation::validateExternalUri($url);
                 if (!$valid) {
                     muut()->setOption('use_custom_s3_bucket', '0');
                     $error_notice = sprintf(__('The %sMuut Plugin%s has been updated to version 3.0.4, but there was an error converting your S3 bucket to the new format (just the bucket name, not the full URL). Your S3 bucket has been disabled; would you like to %sgo fix it%s right now?', 'muut'), '<b>', '</b>', '<a href="' . admin_url('admin.php?page=muut') . '">', '</a>');
                     muut()->queueAdminNotice('error', $error_notice);
                 } else {
                     muut()->setOption('custom_s3_bucket_name', $s3_bucket);
                     flush_rewrite_rules();
                 }
             }
             break;
     }
 }
 /**
  * Validates the settings (the default validation).
  *
  * @param mixed $value The value of the submitted setting.
  * @param string $name The name (and key) of the submitted setting.
  * @return mixed The filtered value.
  * @author Paul Hughes
  * @since 3.0.1
  */
 public function validateSettings($value, $name)
 {
     switch ($name) {
         //This first case is deprecated and should no longer be used. Delete after next release.
         case 'custom_s3_bucket_name':
             $value = trim($value);
             $submitted_settings = $this->getSubmittedSettings();
             if (isset($submitted_settings['use_custom_s3_bucket']) && isset($submitted_settings['enable_proxy_rewrites']) && $submitted_settings['use_custom_s3_bucket'] && $submitted_settings['enable_proxy_rewrites']) {
                 if (isset($submitted_settings['forum_name'])) {
                     $forum_name = $submitted_settings['forum_name'];
                 } elseif (muut()->getForumName() != '') {
                     $forum_name = muut()->getForumName();
                 } else {
                     $forum_name = '';
                 }
                 $url_protocol = apply_filters('muut_s3_bucket_url_protocol', 'http');
                 $url = $url_protocol . '://' . $value . '.' . apply_filters('muut_amazon_s3_url', Muut::AMAZONS3URL) . '/' . $forum_name;
                 $valid = Muut_Field_Validation::validateExternalUri($url);
                 if (!$valid) {
                     $error_args = array('name' => $name, 'message' => __('Custom S3 Bucket name must be associated with a valid server. Make sure it matches the bucket you set up with Muut from your forum page settings.'), 'field' => 'muut_custom_s3_bucket_name', 'new_value' => $value, 'old_value' => muut()->getOption($name));
                     $this->addErrorToQueue($error_args);
                     // Unset the value, so nothing *breaks* (defaults to Muut.com indexing).
                     $value = '';
                 } else {
                     $value = sanitize_text_field($value);
                 }
             }
             break;
         case 'forum_name':
             $value = trim($value);
             // Make sure the forum name has no whitespace.
             $valid = Muut_Field_Validation::validateHasNoWhitespace($value) && Muut_Field_Validation::validateNoRegexEscaping($value, array('-'));
             if (!$valid) {
                 $error_args = array('name' => $name, 'message' => __('Forum name must contain no spaces or special characters of any kind. Make sure the forum name is the name you registered with Muut when you set up the forum.', 'muut'), 'field' => 'muut_forum_name', 'new_value' => stripslashes($value), 'old_value' => muut()->getForumName());
                 $this->addErrorToQueue($error_args);
                 $value = muut()->getForumName();
             } else {
                 $value = sanitize_text_field($value);
             }
             break;
         case 'subscription_api_key':
             $validations = array('validateHasNoWhitespace', 'validateAlphaNumeric');
             $value = trim($value);
             $valid = true;
             foreach ($validations as $validation) {
                 if (method_exists('Muut_Field_Validation', $validation) && $valid) {
                     $valid = Muut_Field_Validation::$validation($value);
                 }
             }
             if (!$valid) {
                 $error_args = array('name' => $name, 'message' => __('API Key must have no spaces and only alphanumeric characters.', 'muut'), 'field' => 'muut_subscription_api_key', 'new_value' => $value, 'old_value' => muut()->getOption($name));
                 $this->addErrorToQueue($error_args);
                 $value = muut()->getOption('subscription_api_key', '');
             } else {
                 $value = sanitize_text_field($value);
             }
             break;
         case 'subscription_secret_key':
             $validations = array('validateHasNoWhitespace', 'validateAlphaNumeric');
             $value = trim($value);
             $valid = true;
             foreach ($validations as $validation) {
                 if (method_exists('Muut_Field_Validation', $validation) && $valid) {
                     $valid = Muut_Field_Validation::$validation($value);
                 }
             }
             if (!$valid) {
                 $error_args = array('name' => $name, 'message' => __('Secret Key must have no spaces and only alphanumeric characters.', 'muut'), 'field' => 'muut_subscription_secret_key', 'new_value' => $value, 'old_value' => muut()->getOption($name));
                 $this->addErrorToQueue($error_args);
                 $value = muut()->getOption('subscription_secret_key', '');
             } else {
                 $value = sanitize_text_field($value);
             }
             break;
     }
     return $value;
 }