/** * 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); }
/** * 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)); } }
/** * 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'); }
/** * Check if redirects should be imported from the free version */ public static function import_redirects_from_free() { $query_redirects = new WP_Query('post_type=any&meta_key=_yoast_wpseo_redirect&order=ASC'); if (!empty($query_redirects->posts)) { WPSEO_Premium::autoloader(); $redirect_manager = new WPSEO_URL_Redirect_Manager(); foreach ($query_redirects->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'); } } }
/** * 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')); } } }