feeds, true ) ) { return $override; } // Reinvoke core's existing filters to preserve other plugins' reservations. // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound if ( 'attachment' === $type ) { $sql = "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; $args = array( $slug, $post_id ); $bad_slug = apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ); } elseif ( $hierarchical ) { // Match core's sibling namespace, including attachments of this parent. $sql = "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type IN (%s, 'attachment') AND ID != %d AND post_parent = %d LIMIT 1"; $args = array( $slug, $type, $post_id, $parent ); $bad_slug = apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $type, $parent ); } else { $sql = "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; $args = array( $slug, $type, $post_id ); $bad_slug = apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $type ); } // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound $collision = $wpdb->get_var( $wpdb->prepare( $sql, $args ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery -- Fresh, prepared uniqueness reads must see writes in the current request, as in core. // Core checks the colliding name's truthiness, which misses the string "0". if ( '0' === $slug && $collision && ! $bad_slug ) { $suffix = 2; do { $args[0] = '0-' . $suffix++; } while ( $wpdb->get_var( $wpdb->prepare( $sql, $args ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery -- Same fresh, scoped collision query. return $args[0]; } return ( $collision || $bad_slug ) ? $override : $slug; } /** Restore an explicitly supplied zero after core treats it as an empty slug. */ public static function preserve_zero_input( $data, $postarr ) { if ( ! isset( $postarr['post_name'] ) || '0' !== (string) $postarr['post_name'] || 'trash' === $data['post_status'] || 'revision' === $data['post_type'] ) { return $data; } $id = isset( $postarr['ID'] ) ? (int) $postarr['ID'] : 0; $data['post_name'] = wp_unique_post_slug( '0', $id, $data['post_status'], $data['post_type'], (int) $data['post_parent'] ); if ( '0' === $data['post_name'] && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) ) { $key = self::zero_key( $id, $data['post_type'], $data['post_parent'], wp_unslash( $data['post_title'] ) ); self::$zero_inserts[ $key ] = array( 'id' => $id, 'type' => $data['post_type'], 'parent' => (int) $data['post_parent'], 'title' => wp_unslash( $data['post_title'] ) ); } return $data; } /** Preserve zero during core's second uniqueness check after the database write. */ public static function preserve_zero_fallback( $override, $slug, $id, $status, $type, $parent ) { if ( null !== $override || '0' === $slug || ! self::$zero_inserts || ! $id ) { return $override; } $post = get_post( $id ); if ( ! $post ) { return $override; } foreach ( array_reverse( self::$zero_inserts, true ) as $key => $pending ) { if ( ( $id === $pending['id'] || ( 0 === $pending['id'] && '0' === $post->post_name ) ) && $type === $pending['type'] && (int) $parent === $pending['parent'] && sanitize_title( $pending['title'], $id ) === $slug ) { unset( self::$zero_inserts[ $key ] ); return wp_unique_post_slug( '0', $id, $status, $type, $parent ); } } return $override; } /** Identify the one in-flight insert, including nested inserts by other plugins. */ private static function zero_key( $id, $type, $parent, $title ) { return $id . ':' . $type . ':' . $parent . ':' . $title; } /** Core omits an ancestor named "0" when building hierarchical paths. */ public static function preserve_zero_ancestors( $uri, $page ) { $path = $page->post_name; $has_zero = false; foreach ( $page->ancestors as $id ) { $parent = get_post( $id ); if ( $parent && '' !== $parent->post_name ) { $has_zero = $has_zero || '0' === $parent->post_name; $path = $parent->post_name . '/' . $path; } } return $has_zero ? $path : $uri; } /** home_url( '0' ) discards the path; explicitly prefix its slash. */ public static function preserve_zero_page_link( $link, $id ) { global $wp_rewrite; $post = get_post( $id ); if ( $post && '0' === $post->post_name && ! $post->post_parent && $wp_rewrite->get_page_permastruct() && untrailingslashit( $link ) === untrailingslashit( home_url() ) ) { $link = home_url( '/' . ltrim( str_replace( '%pagename%', '0', $wp_rewrite->get_page_permastruct() ), '/' ) ); return user_trailingslashit( $link, 'page' ); } return $link; } /** * Prefer a real, exact numeric permalink over date/pagination interpretations. * Does not make drafts public, invent URLs, or change non-numeric requests. * * @param array $vars Parsed WordPress query variables. * @return array */ public static function resolve_request( $vars ) { global $wp; if ( is_admin() || ! $wp instanceof \WP || ! get_option( 'permalink_structure' ) ) { return $vars; } $path = trim( rawurldecode( $wp->request ), '/' ); if ( ! preg_match( '~(?:^|/)[0-9]+(?:/|$)~', $path ) ) { return $vars; } // Explicit queries (search, previews, REST, IDs, feeds, etc.) retain control. foreach ( $wp->public_query_vars as $key ) { if ( isset( $_GET[ $key ] ) || isset( $_POST[ $key ] ) || isset( $wp->extra_query_vars[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification -- Read-only routing; no submitted values are used. return $vars; } } $types = array_filter( get_post_types( array(), 'objects' ), 'is_post_type_viewable' ); if ( ! $types ) { return $vars; } $routes = self::request_routes( $path ); $slugs = array(); foreach ( array_keys( $routes ) as $route_path ) { $slugs[] = sanitize_title_for_query( basename( (string) $route_path ) ); } // Query by exact leaf slug, including "0" and leading zeros; never cast to int. $candidates = get_posts( array( 'post_type' => array_keys( $types ), 'post_status' => array_merge( get_post_stati( array( 'public' => true ) ), get_post_stati( array( 'private' => true ) ) ), 'post_name__in' => array_unique( $slugs ), 'posts_per_page' => -1, 'orderby' => 'ID', 'order' => 'ASC', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); // Pages have precedence in the shared root namespace, as in verbose rules. usort( $candidates, static function ( $left, $right ) { return ( (int) ( 'page' !== $left->post_type ) <=> (int) ( 'page' !== $right->post_type ) ) ?: ( $left->ID <=> $right->ID ); } ); foreach ( $routes as $route_path => $endpoint ) { $request_path = self::url_path( home_url( '/' . $route_path ) ); foreach ( $candidates as $candidate ) { if ( ! is_post_publicly_viewable( $candidate ) && ! current_user_can( 'read_post', $candidate->ID ) ) { continue; } if ( isset( $endpoint['page'] ) && ( $endpoint['page'] < 1 || $endpoint['page'] > substr_count( $candidate->post_content, '' ) + 1 ) ) { continue; } $permalink = get_permalink( $candidate ); if ( ! $permalink || wp_parse_url( $permalink, PHP_URL_QUERY ) || self::url_path( $permalink ) !== $request_path || wp_parse_url( $permalink, PHP_URL_HOST ) !== wp_parse_url( home_url(), PHP_URL_HOST ) ) { continue; } // Remove the conflicting rule's selectors while preserving unrelated vars. parse_str( $wp->matched_query, $matched ); $keys = array_merge( array_keys( $matched ), array( 'error', 'm', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'pagename', 'p', 'page_id', 'attachment', 'attachment_id', 'post_type', 'page', 'paged', 'feed', 'embed', ) ); foreach ( $keys as $key ) { unset( $vars[ $key ] ); } $vars['post_type'] = $candidate->post_type; $vars[ 'page' === $candidate->post_type ? 'page_id' : 'p' ] = $candidate->ID; return array_merge( $vars, $endpoint ); } } return $vars; } /** Exact child URLs win; then consider standard singular-content endpoints. */ private static function request_routes( $path ) { global $wp_rewrite; $routes = array( $path => array() ); if ( preg_match( '~^(.+)/([0-9]+)$~', $path, $matches ) ) { $routes[ $matches[1] ] = array( 'page' => (int) $matches[2] ); } if ( preg_match( '~^(.+)/embed$~', $path, $matches ) ) { $routes[ $matches[1] ] = array( 'embed' => 'true' ); } $feeds = implode( '|', array_map( static function ( $feed ) { return preg_quote( $feed, '~' ); }, (array) $wp_rewrite->feeds ) ); if ( $feeds && preg_match( '~^(.+?)/(?:feed/)?(' . $feeds . ')$~', $path, $matches ) ) { $routes[ $matches[1] ] = array( 'feed' => $matches[2] ); } return $routes; } /** Normalize paths without changing numeric spelling or encoded slugs. */ private static function url_path( $url ) { return trim( rawurldecode( (string) wp_parse_url( $url, PHP_URL_PATH ) ), '/' ); } }