\WP_REST_Server::READABLE, 'callback' => array( $this, 'resolve' ), 'permission_callback' => static function () { return current_user_can( 'edit_posts' ); }, 'args' => array( 'url' => array( 'type' => 'string', 'format' => 'uri', 'required' => true, ), ), ) ); } /** * Resolves the URL. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function resolve( \WP_REST_Request $request ) { $url = (string) $request['url']; $host = wp_parse_url( $url, PHP_URL_HOST ); if ( ! $host || self::bare_host( $host ) !== self::bare_host( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) ) { return new \WP_Error( 'searchsmith_foreign_host', __( 'The URL is not on this site.', 'searchsmith' ), array( 'status' => 400 ) ); } if ( self::is_home( $url ) ) { $front_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0; if ( $front_page ) { return self::post_response( $front_page ); } return rest_ensure_response( array( 'type' => 'home' ) ); } $post_id = url_to_postid( $url ); if ( $post_id ) { return self::post_response( $post_id ); } $term = self::term_for( $url ); if ( $term ) { return rest_ensure_response( array( 'type' => 'term', 'id' => $term->term_id, 'taxonomy' => $term->taxonomy, ) ); } return self::not_found(); } /** * The post or page at the URL, when the caller may read it. A post the * caller can't read, such as another user's draft or a private post, * answers exactly as a URL with nothing at it, so its existence, status * and modification time never leak. * * @param int $post_id Post id. * @return \WP_REST_Response|\WP_Error */ private static function post_response( $post_id ) { if ( ! current_user_can( 'read_post', $post_id ) ) { return self::not_found(); } return rest_ensure_response( self::post_result( $post_id ) ); } /** * The answer for a URL with nothing the caller may see at it. * * @return \WP_Error */ private static function not_found() { return new \WP_Error( 'searchsmith_not_found', __( 'No content found at this URL.', 'searchsmith' ), array( 'status' => 404 ) ); } /** * The response for a post or page. * * @param int $post_id Post id. * @return array */ private static function post_result( $post_id ) { $post = get_post( $post_id ); return array( 'type' => 'post', 'id' => $post->ID, 'post_type' => $post->post_type, 'status' => $post->post_status, 'modified_gmt' => ContentEditor::modified( $post ), 'builder' => Environment::builder_for_post( $post->ID ), 'can_edit' => current_user_can( 'edit_post', $post->ID ), ); } /** * Whether the URL is the site's home page. * * @param string $url URL on this site. * @return bool */ private static function is_home( $url ) { $path = trim( (string) wp_parse_url( $url, PHP_URL_PATH ), '/' ); $home = trim( (string) wp_parse_url( home_url(), PHP_URL_PATH ), '/' ); return $path === $home && '' === (string) wp_parse_url( $url, PHP_URL_QUERY ); } /** * The category or tag whose archive is at this URL, for pretty permalinks. * * @param string $url URL on this site. * @return \WP_Term|null */ private static function term_for( $url ) { $home = trim( (string) wp_parse_url( home_url(), PHP_URL_PATH ), '/' ); $path = trim( (string) wp_parse_url( $url, PHP_URL_PATH ), '/' ); $path = '' !== $home && 0 === strpos( $path, $home . '/' ) ? substr( $path, strlen( $home ) + 1 ) : $path; $segments = array_values( array_filter( explode( '/', $path ), 'strlen' ) ); if ( count( $segments ) < 2 ) { return null; } $bases = array( 'category' => trim( get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category', '/' ), 'post_tag' => trim( get_option( 'tag_base' ) ? get_option( 'tag_base' ) : 'tag', '/' ), ); $prefix = implode( '/', array_slice( $segments, 0, -1 ) ); $slug = sanitize_title( urldecode( end( $segments ) ) ); foreach ( $bases as $taxonomy => $base ) { // Nested categories appear as base/parent/child. if ( $prefix === $base || 0 === strpos( $prefix, $base . '/' ) ) { $term = get_term_by( 'slug', $slug, $taxonomy ); if ( $term instanceof \WP_Term ) { return $term; } } } return null; } /** * The host without a leading `www.`, lowercased, so both forms of a site * match each other. * * @param string $host Host. * @return string */ private static function bare_host( $host ) { $host = strtolower( $host ); return 0 === strpos( $host, 'www.' ) ? substr( $host, 4 ) : $host; } }