/**
  * An update is required, do it
  *
  * @param $current_version
  */
 private function do_update($current_version)
 {
     // < 1.0.4
     if ($current_version < 5) {
         /**
          * Upgrade to version 1.0.4
          *
          * - Save the old license to the new license option
          */
         // Save the old license to the new license option
         $license_manager = new Yoast_Plugin_License_Manager(new WPSEO_Product_Premium());
         $license_manager->set_license_key(trim(get_option('wpseo_license_key', '')));
         $license_manager->set_license_status(trim(get_option('wpseo_license_status', '')));
         // Remove old license options
         delete_option('wpseo_license_key');
         delete_option('wpseo_license_status');
     }
     // Upgrade to version 1.2.0
     if ($current_version < 15) {
         /**
          * Upgrade redirects
          */
         // URL Redirects
         $url_redirect_manager = new WPSEO_URL_Redirect_Manager();
         $url_redirects = $url_redirect_manager->get_redirects();
         // Loop through the redirects
         foreach ($url_redirects as $old_url => $redirect) {
             // Check if the redirect is not an array yet
             if (!is_array($redirect)) {
                 $url_redirects[$old_url] = array('url' => $redirect, 'type' => '301');
             }
         }
         // Save the URL redirects
         $url_redirect_manager->save_redirects($url_redirects);
         // Regex Redirects
         $regex_redirect_manager = new WPSEO_REGEX_Redirect_Manager();
         $regex_redirects = $regex_redirect_manager->get_redirects();
         // Loop through the redirects
         foreach ($regex_redirects as $old_url => $redirect) {
             // Check if the redirect is not an array yet
             if (!is_array($redirect)) {
                 $regex_redirects[$old_url] = array('url' => $redirect, 'type' => '301');
             }
         }
         // Save the URL redirects
         $regex_redirect_manager->save_redirects($regex_redirects);
     }
 }
 /**
  * Generate file content
  *
  * @return string
  */
 protected function generate_file_content()
 {
     $file_content = "";
     // Generate URL redirects
     $url_redirect_manager = new WPSEO_URL_Redirect_Manager();
     $url_redirects = $url_redirect_manager->get_redirects();
     if (count($url_redirects) > 0) {
         foreach ($url_redirects as $old_url => $redirect) {
             $file_content .= $this->format_url_redirect($old_url, $redirect['url'], $redirect['type']) . "\n";
         }
     }
     // Generate REGEX redirects
     $regex_redirect_manager = new WPSEO_REGEX_Redirect_Manager();
     $regex_redirects = $regex_redirect_manager->get_redirects();
     if (count($regex_redirects) > 0) {
         foreach ($regex_redirects as $regex => $redirect) {
             $file_content .= $this->format_regex_redirect($regex, $redirect['url'], $redirect['type']) . "\n";
         }
     }
     return $file_content;
 }
 /**
  * Do custom action when the redirect option is saved
  */
 public function catch_option_redirect_save()
 {
     if (filter_input(INPUT_POST, 'option_page') === 'yoast_wpseo_redirect_options') {
         if (current_user_can('manage_options')) {
             $wpseo_redirect = filter_input(INPUT_POST, 'wpseo_redirect', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
             $enable_autoload = !empty($wpseo_redirect['disable_php_redirect']) ? false : true;
             // Change the normal redirect autoload option.
             $normal_redirect_manager = new WPSEO_URL_Redirect_Manager();
             $normal_redirect_manager->redirects_change_autoload($enable_autoload);
             // Change the regex redirect autoload option.
             $regex_redirect_manager = new WPSEO_REGEX_Redirect_Manager();
             $regex_redirect_manager->redirects_change_autoload($enable_autoload);
         }
     }
 }
 /**
  * Do custom action when the redirect option is saved
  */
 public function catch_option_redirect_save()
 {
     if (isset($_POST['option_page']) && $_POST['option_page'] == 'yoast_wpseo_redirect_options') {
         if (current_user_can('manage_options')) {
             $enable_autoload = isset($_POST['wpseo_redirect']['disable_php_redirect']) ? false : true;
             // Change the normal redirect autoload option
             $normal_redirect_manager = new WPSEO_URL_Redirect_Manager();
             $normal_redirect_manager->redirects_change_autoload($enable_autoload);
             // Change the regex redirect autoload option
             $regex_redirect_manager = new WPSEO_REGEX_Redirect_Manager();
             $regex_redirect_manager->redirects_change_autoload($enable_autoload);
         }
     }
 }
 /**
  * Do .htaccess file import.
  */
 private function htaccess_import()
 {
     global $wp_filesystem;
     if ($htaccess = filter_input(INPUT_POST, 'htaccess')) {
         // The htaccess post.
         $htaccess = stripcslashes($htaccess);
         // The new .htaccess file.
         $new_htaccess = $htaccess;
         // Regexpressions.
         $regex_patterns = array('url' => '`[^# ]Redirect ([0-9]+) ([^\\s]+) ([^\\s]+)`i', 'regex' => '`[^# ]RedirectMatch ([0-9]+) ([^\\s]+) ([^\\s]+)`i');
         // Create redirect manager objects.
         $url_redirection_manager = new WPSEO_URL_Redirect_Manager();
         $regex_redirection_manager = new WPSEO_REGEX_Redirect_Manager();
         // Bool if we've imported redirects.
         $redirects_imported = false;
         // Loop through patterns.
         foreach ($regex_patterns as $regex_type => $regex_pattern) {
             // Get all redirects.
             if (preg_match_all($regex_pattern, $htaccess, $redirects)) {
                 if (count($redirects) > 0) {
                     // Loop through redirects.
                     for ($i = 0; $i < count($redirects[1]); $i++) {
                         // Get source && target.
                         $type = trim($redirects[1][$i]);
                         $source = trim($redirects[2][$i]);
                         $target = trim($redirects[3][$i]);
                         // Check if both source and target are not empty.
                         if ('' != $source && '' != $target) {
                             // Check redirect type.
                             if ('regex' == $regex_type) {
                                 $regex_redirection_manager->create_redirect($source, $target, $type);
                             } else {
                                 $url_redirection_manager->create_redirect($source, $target, $type);
                             }
                             $redirects_imported = true;
                             // Trim the original redirect.
                             $original_redirect = trim($redirects[0][$i]);
                             // Comment out added redirect in our new .htaccess file.
                             $new_htaccess = str_ireplace($original_redirect, '#' . $original_redirect, $new_htaccess);
                         }
                     }
                 }
             }
         }
         // Check if we've imported any redirects.
         if ($redirects_imported) {
             // Set the filesystem URL.
             $url = wp_nonce_url('admin.php?page=wpseo_import', 'update-htaccess');
             // Get the credentials.
             $credentials = request_filesystem_credentials($url, '', false, ABSPATH);
             // Check if WP_Filesystem is working.
             if (!WP_Filesystem($credentials, ABSPATH)) {
                 // WP_Filesystem not working, request filesystem credentials.
                 request_filesystem_credentials($url, '', true, ABSPATH);
             } else {
                 // Update the .htaccess file.
                 $wp_filesystem->put_contents(ABSPATH . '.htaccess', $new_htaccess, FS_CHMOD_FILE);
             }
             // Display success message.
             add_filter('wpseo_import_message', array($this, 'message_htaccess_success'));
         } else {
             // Display fail message.
             add_filter('wpseo_import_message', array($this, 'message_htaccess_no_redirects'));
         }
     }
 }