__( 'Searchsmith redirects', 'searchsmith' ), 'public' => false, 'show_ui' => false, 'show_in_rest' => false, 'rewrite' => false, 'query_var' => false, 'can_export' => true, 'supports' => array( 'title' ), 'capability_type' => 'post', ) ); } /** * Sends a visitor on when WordPress found nothing at the address and the * connector has a redirect for it. */ public static function serve() { if ( ! is_404() || ! isset( $_SERVER['REQUEST_URI'] ) ) { return; } $path = self::normalize( wp_parse_url( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ), PHP_URL_PATH ) ); $url = null === $path ? null : self::destination( $path ); if ( $url && wp_safe_redirect( $url, 301, 'Searchsmith' ) ) { exit; } } /** * Where a redirected path leads now, or null. * * @param string $path Normalized path. * @return string|null */ public static function destination( $path ) { $redirect = self::find( $path ); if ( ! $redirect ) { return null; } $target = get_post( (int) get_post_meta( $redirect->ID, self::TARGET_KEY, true ) ); if ( ! $target || 'publish' !== $target->post_status ) { return null; } return get_permalink( $target ); } /** * The redirect stored for a path, or null. * * @param string $path Normalized path. * @return \WP_Post|null */ public static function find( $path ) { $found = get_posts( array( 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'name' => self::slug( $path ), 'posts_per_page' => 1, 'no_found_rows' => true, ) ); return $found ? $found[0] : null; } /** * Every redirect, oldest first. * * @return array[] */ public static function all() { $redirects = get_posts( array( 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'orderby' => 'ID', 'order' => 'ASC', 'posts_per_page' => 1000, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page -- Blaze adds at most one redirect a day per site. 'no_found_rows' => true, ) ); return array_map( array( self::class, 'describe' ), $redirects ); } /** * A redirect as the REST API shows it. * * @param \WP_Post $redirect The redirect's post. * @return array */ public static function describe( \WP_Post $redirect ) { $target_id = (int) get_post_meta( $redirect->ID, self::TARGET_KEY, true ); return array( 'id' => $redirect->ID, 'from' => $redirect->post_title, 'to_post_id' => $target_id, 'to_url' => $target_id ? get_permalink( $target_id ) : null, 'created_gmt' => ContentEditor::rfc3339( $redirect->post_date_gmt ), ); } /** * Adds a redirect. * * @param string $from A path or URL on this site. * @param \WP_Post $target The post to send visitors to. * @param string $op_key The write's op key. * @return array|\WP_Error */ public static function add( $from, \WP_Post $target, $op_key ) { $path = self::path_on_site( $from ); if ( false === $path ) { return self::refusal( 'searchsmith_foreign_host', __( 'The address is not on this site.', 'searchsmith' ), 400 ); } if ( null === $path ) { return self::refusal( 'searchsmith_invalid_path', __( 'The home page cannot be redirected.', 'searchsmith' ), 400 ); } if ( 'publish' !== $target->post_status ) { return self::refusal( 'searchsmith_target_not_published', __( 'Redirects can only lead to a published post.', 'searchsmith' ), 422 ); } if ( self::path_on_site( get_permalink( $target ) ) === $path ) { return self::refusal( 'searchsmith_redirect_loop', __( 'The address is the target post\'s own address.', 'searchsmith' ), 422 ); } $at_path = url_to_postid( self::url( $path ) ); if ( $at_path && 'publish' === get_post_status( $at_path ) ) { return self::refusal( 'searchsmith_path_has_published_post', __( 'A published post is at this address.', 'searchsmith' ), 409, array( 'post_id' => $at_path ) ); } $existing = self::find( $path ); if ( $existing ) { return self::refusal( 'searchsmith_redirect_exists', __( 'The connector already redirects this address.', 'searchsmith' ), 409, array( 'redirect' => self::describe( $existing ) ) ); } $plugin = self::conflict( $path ); if ( $plugin ) { return self::refusal( 'searchsmith_redirect_conflict', __( 'Another plugin already redirects this address.', 'searchsmith' ), 409, array( 'plugin' => $plugin ) ); } $redirect_id = wp_insert_post( wp_slash( array( 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'post_title' => $path, 'post_name' => self::slug( $path ), 'meta_input' => array( self::TARGET_KEY => $target->ID ), ) ), true ); if ( is_wp_error( $redirect_id ) ) { return $redirect_id; } $result = array_merge( self::describe( get_post( $redirect_id ) ), array( 'op_key' => (string) $op_key ) ); OpKeys::record( $op_key, 'add_redirect', $target->ID, $redirect_id, $result ); return $result; } /** * Removes one of the connector's redirects. The op key is stored on the * post the redirect led to, since the redirect itself is gone. * * @param \WP_Post $redirect The redirect's post. * @param string $op_key The write's op key. * @return array|\WP_Error */ public static function remove( \WP_Post $redirect, $op_key ) { $described = self::describe( $redirect ); // The connector's own record, not content: deleting it removes the // redirect. Posts with content are never deleted. if ( ! wp_delete_post( $redirect->ID, true ) ) { return self::refusal( 'searchsmith_remove_failed', __( 'The redirect could not be removed.', 'searchsmith' ), 500 ); } $result = array_merge( $described, array( 'removed' => true, 'op_key' => (string) $op_key, ) ); OpKeys::record( $op_key, 'remove_redirect', $described['to_post_id'], $described['to_post_id'], $result ); return $result; } /** * The redirect plugin that already redirects a path, or null. * * @param string $path Normalized path. * @return string|null `redirection`, `yoast_premium` or `rank_math`. */ public static function conflict( $path ) { $plugin = null; if ( class_exists( '\Red_Item' ) && method_exists( '\Red_Item', 'get_for_url' ) ) { // Redirection answers its candidates for the path: rules for the // exact address, and every regular expression rule, which only // count when they match. foreach ( (array) \Red_Item::get_for_url( $path ) as $item ) { if ( ! is_object( $item ) || ! method_exists( $item, 'is_enabled' ) || ! $item->is_enabled() ) { continue; } if ( method_exists( $item, 'is_regex' ) && $item->is_regex() && class_exists( '\Red_Regex' ) && method_exists( $item, 'get_url' ) ) { $regex = new \Red_Regex( $item->get_url(), true ); if ( ! $regex->is_match( $path ) ) { continue; } } $plugin = 'redirection'; break; } } if ( null === $plugin && defined( 'WPSEO_PREMIUM_VERSION' ) ) { foreach ( array( 'wpseo-premium-redirects-base', 'wpseo-premium-redirects-export-plain' ) as $option ) { foreach ( array_keys( (array) get_option( $option, array() ) ) as $origin ) { if ( self::normalize( '/' . ltrim( (string) $origin, '/' ) ) === $path ) { $plugin = 'yoast_premium'; break 2; } } } } if ( null === $plugin && class_exists( '\RankMath\Redirections\DB' ) && method_exists( '\RankMath\Redirections\DB', 'match_redirections' ) ) { if ( \RankMath\Redirections\DB::match_redirections( ltrim( $path, '/' ) ) ) { $plugin = 'rank_math'; } } /** * Filters which redirect plugin, if any, already redirects a path. * * @param string|null $plugin `redirection`, `yoast_premium`, `rank_math`, or null. * @param string $path The path, starting with `/`. */ $plugin = apply_filters( 'searchsmith_redirect_conflict', $plugin, $path ); return is_string( $plugin ) && '' !== $plugin ? $plugin : null; } /** * Redirect plugins active on the site. * * @return string[] */ public static function active_plugins() { $active = array(); if ( class_exists( '\Red_Item' ) ) { $active[] = 'redirection'; } if ( defined( 'WPSEO_PREMIUM_VERSION' ) ) { $active[] = 'yoast_premium'; } if ( class_exists( '\RankMath\Redirections\DB' ) ) { $active[] = 'rank_math'; } return $active; } /** * The normalized path of a URL or path on this site. * * @param string $from URL or path from the site's root. * @return string|null|false The path, null for the home page, or false * when the URL is on another host. */ public static function path_on_site( $from ) { $from = trim( (string) $from ); $host = wp_parse_url( $from, PHP_URL_HOST ); if ( $host ) { $site = (string) wp_parse_url( home_url(), PHP_URL_HOST ); if ( preg_replace( '/^www\./', '', strtolower( $host ) ) !== preg_replace( '/^www\./', '', strtolower( $site ) ) ) { return false; } } return self::normalize( wp_parse_url( $from, PHP_URL_PATH ) ); } /** * The full URL of a path from the site's root. * * @param string $path Normalized path. * @return string */ private static function url( $path ) { $home = wp_parse_url( home_url() ); $port = isset( $home['port'] ) ? ':' . $home['port'] : ''; return $home['scheme'] . '://' . $home['host'] . $port . $path; } /** * A path as stored and compared: decoded, lowercase, starting with `/` * and without a trailing slash. * * @param string|null $path Path. * @return string|null Null for the home page or no path. */ public static function normalize( $path ) { $path = trim( rawurldecode( (string) $path ), '/' ); return '' === $path ? null : '/' . strtolower( $path ); } /** * The slug a redirect's post has for a path. * * @param string $path Normalized path. * @return string */ private static function slug( $path ) { return md5( $path ); } /** * A refusal. * * @param string $code Error code. * @param string $message Message. * @param int $status HTTP status. * @param array $data Extra data. * @return \WP_Error */ private static function refusal( $code, $message, $status, array $data = array() ) { return new \WP_Error( $code, $message, array_merge( array( 'status' => $status ), $data ) ); } }