/**
  * Detect if the slug changed, hooked into 'post_updated'
  *
  * @param $term_id
  * @param $tt_id
  * @param $taxonomy
  */
 public function detect_slug_change($term_id, $tt_id, $taxonomy)
 {
     // Check if the old page is set
     if (!isset($_POST['wpseo_old_url'])) {
         return;
     }
     // Get the new URL
     $new_url = parse_url(get_term_link($term_id, $taxonomy));
     $new_url = $new_url['path'];
     // Get the old URL
     $old_url = esc_url($_POST['wpseo_old_url']);
     // Get the site URL
     $site = parse_url(get_site_url());
     // Check if we should create a redirect
     if ($old_url != $new_url && $old_url != '/' && (!isset($site['path']) || isset($site['path']) && $old_url != $site['path'] . '/')) {
         // The URL redirect manager
         $redirect_manager = new WPSEO_URL_Redirect_Manager();
         // Create the redirect
         $redirect_manager->create_redirect($old_url, $new_url, 301);
         // Format the message
         $message = sprintf(__("WordPress SEO Premium created a <a href='%s'>redirect</a> from the old term URL to the new term URL. <a href='%s'>Click here to undo this</a>.", 'wordpress-seo'), admin_url('admin.php?page=wpseo_redirects&s=' . urlencode($old_url)), 'javascript:wpseo_undo_redirect("' . urlencode($old_url) . '", "' . wp_create_nonce('wpseo-redirects-ajax-security') . '");');
         // Add the message to the notifications center
         Yoast_Notification_Center::get()->add_notification(new Yoast_Notification($message));
     }
 }
 /**
  * Opens the redirect manager and create the redirect
  *
  * @param string $old_url
  * @param string $new_url
  * @param int    $header_code
  */
 protected function create_redirect($old_url, $new_url, $header_code = 301)
 {
     // The URL redirect manager.
     $redirect_manager = new WPSEO_URL_Redirect_Manager();
     // Create the redirect.
     $redirect_manager->create_redirect($old_url, $new_url, $header_code);
 }
 /**
  * Handling the request to create a new redirect from the issued URL
  */
 public function ajax_create_redirect()
 {
     if ($this->valid_nonce() && class_exists('WPSEO_URL_Redirect_Manager') && defined('WPSEO_PREMIUM_PATH')) {
         $redirect_manager = new WPSEO_URL_Redirect_Manager();
         $old_url = filter_input(INPUT_POST, 'old_url');
         // Creates the redirect.
         if ($redirect_manager->create_redirect($old_url, filter_input(INPUT_POST, 'new_url'), filter_input(INPUT_POST, 'type'))) {
             if (filter_input(INPUT_POST, 'mark_as_fixed') === 'true') {
                 new WPSEO_GSC_Marker($old_url);
             }
             wp_die('true');
         }
     }
     wp_die('false');
 }
 /**
  * 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);
         }
     }
 }
 /**
  * Look up if url does exists in the current redirects
  *
  * @param string $url url to search for
  *
  * @return bool
  */
 public function check_if_redirect_exists($url)
 {
     $redirect_manager = new WPSEO_URL_Redirect_Manager();
     $redirects = $redirect_manager->get_redirects();
     return array_key_exists($url, $redirects);
 }
 /**
  * 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'));
         }
     }
 }
 /**
  * Setup the table variables, fetch the items from the database, search, sort and format the items.
  * Set the items as the WPSEO_Redirect_Table items variable.
  */
 public function prepare_items()
 {
     // Setup the columns
     $columns = $this->get_columns();
     $hidden = array();
     $sortable = $this->get_sortable_columns();
     $this->_column_headers = array($columns, $hidden, $sortable);
     // Vies
     $this->views();
     // Get current view
     $current_view = $this->get_current_view();
     // Build crawl issues args
     $ci_args = array();
     // Set the post status
     $ci_args['post_status'] = 'publish';
     // Set the orderby
     $orderby = !empty($_GET['orderby']) ? $_GET['orderby'] : 'title';
     $ci_args['orderby'] = $orderby;
     // Set the order
     $order = !empty($_GET['order']) ? $_GET['order'] : 'asc';
     $ci_args['order'] = $order;
     // Prepares the issue filter
     $this->prepare_issue_filter();
     // Set the issue filter
     if ($issue_filter = get_transient('gwt-issue_filter')) {
         $filter_posts = $this->issue_filter($issue_filter);
         if (is_array($filter_posts) && !empty($filter_posts)) {
             $ci_args['post__in'] = $filter_posts;
         }
     }
     // Get variables needed for pagination
     $per_page = $this->get_items_per_page('errors_per_page', 25);
     $current_page = intval(isset($_GET['paged']) ? $_GET['paged'] : 1);
     // Set query pagination
     $ci_args['posts_per_page'] = $per_page;
     $ci_args['offset'] = ($current_page - 1) * $per_page;
     // Check current view
     if ('ignored' == $current_view) {
         $ci_args['post_status'] = 'trash';
     }
     // Filter crawl errors
     if ('not-redirected' == $current_view) {
         $url_redirect_manager = new WPSEO_URL_Redirect_Manager();
         $redirects = $url_redirect_manager->get_redirects();
         $wpseo_urls = array();
         if (count($redirects) > 0) {
             foreach ($redirects as $old_url => $new_url) {
                 $wpseo_urls[] = $old_url;
             }
         }
         $ci_args['wpseo_urls'] = $wpseo_urls;
     }
     // Get the crawl issues
     $crawl_issue_manager = new WPSEO_Crawl_Issue_Manager();
     $crawl_issues = $crawl_issue_manager->get_crawl_issues($this->gwt, $ci_args);
     // Get the total items
     $total_items = $crawl_issue_manager->get_latest_query()->found_posts;
     // Set table pagination
     $this->set_pagination_args(array('total_items' => $total_items, 'total_pages' => ceil($total_items / $per_page), 'per_page' => $per_page));
     // Set items
     $items_array = array();
     if (is_array($crawl_issues) && count($crawl_issues) > 0) {
         foreach ($crawl_issues as $crawl_issue) {
             $items_array[] = $crawl_issue->to_array();
         }
     }
     $this->items = $items_array;
 }
 /**
  * Determine which model box type should be rendered
  *
  * @param string $url
  * @param string $current_redirect
  *
  * @return string
  */
 private function modal_box_type($url, &$current_redirect)
 {
     if (defined('WPSEO_PREMIUM_FILE') && class_exists('WPSEO_URL_Redirect_Manager')) {
         static $redirect_manager;
         if (!$redirect_manager) {
             $redirect_manager = new WPSEO_URL_Redirect_Manager();
         }
         if ($current_redirect = $redirect_manager->search_url($url)) {
             return 'already_exists';
         }
         return 'create';
     }
     return 'no_premium';
 }
 /**
  * Check if redirects should be imported from the free version
  *
  * @since 2.3
  */
 public function import_redirects_2_3()
 {
     $wp_query = new WP_Query('post_type=any&meta_key=_yoast_wpseo_redirect&order=ASC');
     if (!empty($wp_query->posts)) {
         $redirect_manager = new WPSEO_URL_Redirect_Manager();
         foreach ($wp_query->posts as $post) {
             $old_url = '/' . $post->post_name . '/';
             $new_url = get_post_meta($post->ID, '_yoast_wpseo_redirect', true);
             // Create redirect.
             $redirect_manager->create_redirect($old_url, $new_url, 301);
             // Remove post meta value.
             delete_post_meta($post->ID, '_yoast_wpseo_redirect');
         }
     }
 }