Changeset 64402
- Timestamp:
- 10/16/2026 05:03:59 PM (3 weeks ago)
- Location:
- trunk
- Files:
-
- 3 added
- 17 edited
-
src/wp-admin/includes/schema.php (modified) (1 diff)
-
src/wp-admin/includes/upgrade.php (modified) (1 diff)
-
src/wp-admin/network/site-settings.php (modified) (1 diff)
-
src/wp-admin/options-permalink.php (modified) (5 diffs)
-
src/wp-includes/class-wp-post-type.php (modified) (3 diffs)
-
src/wp-includes/class-wp-query.php (modified) (4 diffs)
-
src/wp-includes/class-wp-rewrite.php (modified) (9 diffs)
-
src/wp-includes/class-wp.php (modified) (3 diffs)
-
src/wp-includes/default-filters.php (modified) (2 diffs)
-
src/wp-includes/formatting.php (modified) (1 diff)
-
src/wp-includes/option.php (modified) (1 diff)
-
src/wp-includes/post.php (modified) (4 diffs)
-
src/wp-includes/query.php (modified) (2 diffs)
-
src/wp-includes/rewrite.php (modified) (1 diff)
-
src/wp-includes/version.php (modified) (1 diff)
-
tests/phpstan/baselines/variable.undefined.neon (modified) (1 diff)
-
tests/phpunit/tests/post/randomablePostType.php (added)
-
tests/phpunit/tests/query/randomContentRedirect.php (added)
-
tests/phpunit/tests/query/vars.php (modified) (1 diff)
-
tests/phpunit/tests/rewrite/randomRewriteRules.php (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/schema.php
r64337 r64402 564 564 // 6.9.0 565 565 'wp_notes_notify' => 1, 566 567 // 7.2.0 568 'wp_random_base' => '', 566 569 ); 567 570 -
trunk/src/wp-admin/includes/upgrade.php
r64337 r64402 2893 2893 } 2894 2894 2895 if ( in_array( $setting, array( 'siteurl', 'home', 'category_base', 'tag_base' ), true ) ) {2895 if ( in_array( $setting, array( 'siteurl', 'home', 'category_base', 'tag_base', 'wp_random_base' ), true ) ) { 2896 2896 $option = untrailingslashit( $option ); 2897 2897 } -
trunk/src/wp-admin/network/site-settings.php
r61651 r64402 163 163 'category_base', 164 164 'tag_base', 165 'wp_random_base', 165 166 'upload_path', 166 167 'upload_url_path', -
trunk/src/wp-admin/options-permalink.php
r62710 r64402 70 70 unset( $help_sidebar_content ); 71 71 72 /** @global WP_Rewrite $wp_rewrite WordPress rewrite component. */ 73 global $wp_rewrite; 74 72 75 $home_path = get_home_path(); 73 76 $iis7_permalinks = iis7_supports_permalinks(); … … 95 98 $category_base = get_option( 'category_base' ); 96 99 $tag_base = get_option( 'tag_base' ); 100 $random_base = get_option( 'wp_random_base' ); 101 $random_base = is_string( $random_base ) ? $random_base : ''; 97 102 98 103 $structure_updated = false; … … 144 149 145 150 $wp_rewrite->set_tag_base( $tag_base ); 151 } 152 153 if ( wp_is_random_content_redirect_enabled() && isset( $_POST['wp_random_base'] ) && is_string( $_POST['wp_random_base'] ) ) { 154 $random_base = $_POST['wp_random_base']; 155 156 if ( ! empty( $random_base ) ) { 157 $random_base = $blog_prefix . preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $random_base ) ); 158 } 159 160 $wp_rewrite->set_random_base( $random_base ); 146 161 } 147 162 } … … 237 252 $category_base = preg_replace( '|^/?blog|', '', $category_base ); 238 253 $tag_base = preg_replace( '|^/?blog|', '', $tag_base ); 254 $random_base = preg_replace( '|^/?blog|', '', $random_base ) ?? ''; 239 255 } 240 256 … … 458 474 </td> 459 475 </tr> 476 <?php if ( wp_is_random_content_redirect_enabled() ) : ?> 477 <tr> 478 <th> 479 <label for="wp_random_base"> 480 <?php _e( 'Random base' ); ?> 481 </label> 482 </th> 483 <td> 484 <?php if ( '' === $blog_prefix ) : ?> 485 <input name="wp_random_base" id="wp_random_base" type="text" 486 value="<?php echo esc_attr( $random_base ); ?>" class="regular-text code" 487 /> 488 <?php else : ?> 489 <span class="code permalink-structure-has-blog-prefix"> 490 <code class="no-break"><?php echo $blog_prefix; ?></code> 491 <input name="wp_random_base" id="wp_random_base" type="text" 492 value="<?php echo esc_attr( $random_base ); ?>" class="regular-text code" 493 /> 494 </span> 495 <?php endif; ?> 496 </td> 497 </tr> 498 <?php endif; ?> 460 499 <?php do_settings_fields( 'permalink', 'optional' ); ?> 461 500 </table> -
trunk/src/wp-includes/class-wp-post-type.php
r64388 r64402 123 123 */ 124 124 public $embeddable = null; 125 126 /** 127 * Whether posts of this post type can be selected by random content redirects. 128 * 129 * Random content redirects send requests such as `example.com/random/` to a 130 * randomly selected post. 131 * 132 * Default is true when $publicly_queryable is true and the post type has an archive. 133 * 134 * @since 7.2.0 135 * @var bool $randomable 136 */ 137 public $randomable = null; 125 138 126 139 /** … … 547 560 'publicly_queryable' => null, 548 561 'embeddable' => null, 562 'randomable' => null, 549 563 'show_ui' => null, 550 564 'show_in_menu' => null, … … 590 604 // If not set, default to the setting for 'public'. 591 605 $args['embeddable'] ??= $args['public']; 606 607 // If not set, default to publicly queryable post types with an archive. 608 $args['randomable'] ??= $args['publicly_queryable'] && (bool) $args['has_archive']; 592 609 593 610 // If not set, default rest_namespace to wp/v2 if show_in_rest is true. -
trunk/src/wp-includes/class-wp-query.php
r64132 r64402 417 417 */ 418 418 public bool $is_sitemap = false; 419 420 /** 421 * Signifies whether the current query is for random content. 422 * 423 * True for a blog home, archive or search request that includes the `random` 424 * query variable, for example `example.com/random/` or `example.com/?s=foo&random`. 425 * 426 * @since 7.2.0 427 */ 428 public bool $is_random = false; 419 429 420 430 /** … … 528 538 $this->is_favicon = false; 529 539 $this->is_sitemap = false; 540 $this->is_random = false; 530 541 $this->is_posts_page = false; 531 542 $this->is_post_type_archive = false; … … 1148 1159 $this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 ); 1149 1160 1161 $this->is_random = isset( $query_vars['random'] ) 1162 && ! $this->is_feed 1163 && ( $this->is_home || $this->is_archive || $this->is_search ); 1164 1150 1165 $this->query_vars_hash = md5( serialize( $this->query_vars ) ); 1151 1166 $this->query_vars_changed = false; … … 4655 4670 4656 4671 /** 4672 * Determines whether the query is for random content. 4673 * 4674 * @since 7.2.0 4675 * 4676 * @see wp_is_random_content_query() 4677 * 4678 * @return bool Whether the query is for random content. 4679 */ 4680 public function is_random(): bool { 4681 return $this->is_random; 4682 } 4683 4684 /** 4657 4685 * Determines whether the query is for a search. 4658 4686 * -
trunk/src/wp-includes/class-wp-rewrite.php
r63887 r64402 120 120 */ 121 121 public $feed_base = 'feed'; 122 123 /** 124 * Custom random content permalink base. 125 * 126 * Set from the 'wp_random_base' option by {@see WP_Rewrite::init()}. Empty when no custom 127 * base is set, in which case the translated default is used. 128 * Use {@see WP_Rewrite::get_random_base()} to retrieve the base in use. 129 * 130 * @since 7.2.0 131 * @var string 132 */ 133 public string $random_base = ''; 122 134 123 135 /** … … 870 882 * @param bool $endpoints Optional. Whether endpoints should be applied to the generated rewrite rules. 871 883 * Default true. 872 * @return string[] Array of rewrite rules keyed by their regex pattern. 884 * @return array<string, string> Array of rewrite rules keyed by their regex pattern. 885 * 886 * @phpstan-return array<non-falsy-string, non-falsy-string> 873 887 */ 874 888 public function generate_rewrite_rules( $permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true ) { … … 1254 1268 * @param bool $walk_dirs Optional. Whether to create list of directories to walk over. 1255 1269 * Default false. 1256 * @return array An array of rewrite rules keyed by their regex pattern. 1270 * @return array<string, string> An array of rewrite rules keyed by their regex pattern. 1271 * 1272 * @phpstan-return array<non-falsy-string, non-falsy-string> 1257 1273 */ 1258 1274 public function generate_rewrite_rule( $permalink_structure, $walk_dirs = false ) { … … 1275 1291 * @since 1.5.0 1276 1292 * 1277 * @return string[]An associative array of matches and queries.1293 * @return array<string, string> An associative array of matches and queries. 1278 1294 */ 1279 1295 public function rewrite_rules() { … … 1407 1423 */ 1408 1424 $page_rewrite = apply_filters( 'page_rewrite_rules', $page_rewrite ); 1425 1426 // Random content rewrite rules. 1427 $random_rewrite = $this->random_rewrite_rules(); 1428 1429 /** 1430 * Filters rewrite rules used for random content redirects. 1431 * 1432 * Likely random content rules would include `/random/`, as well as 1433 * term, author and post type archives prefixed with the random base, 1434 * for example `/random/category/news/`. 1435 * 1436 * @since 7.2.0 1437 * 1438 * @param array<string, string> $random_rewrite Array of rewrite rules for random content redirects, keyed by their regex pattern. 1439 */ 1440 $random_rewrite = apply_filters( 'random_rewrite_rules', $random_rewrite ); 1409 1441 1410 1442 // Extra permastructs. … … 1456 1488 // Put them together. 1457 1489 if ( $this->use_verbose_page_rules ) { 1458 $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $sitemap_rewrite, $deprecated_files, $registration_pages, $r oot_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules );1490 $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $sitemap_rewrite, $deprecated_files, $registration_pages, $random_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules ); 1459 1491 } else { 1460 $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $sitemap_rewrite, $deprecated_files, $registration_pages, $r oot_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules );1492 $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $sitemap_rewrite, $deprecated_files, $registration_pages, $random_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules ); 1461 1493 } 1462 1494 … … 1475 1507 * @since 1.5.0 1476 1508 * 1477 * @param string[]$rules The compiled array of rewrite rules, keyed by their regex pattern.1509 * @param array<string, string> $rules The compiled array of rewrite rules, keyed by their regex pattern. 1478 1510 */ 1479 1511 $this->rules = apply_filters( 'rewrite_rules_array', $this->rules ); 1480 1512 1481 1513 return $this->rules; 1514 } 1515 1516 /** 1517 * Retrieves the rewrite rules for random content redirects. 1518 * 1519 * Random content URLs are an existing archive path prefixed with the random base. 1520 * A request to one redirects to a randomly selected post from that archive: 1521 * 1522 * - `/random/` for any post. 1523 * - `/random/{taxonomy-slug}/{term}/` for a post in a term archive, for example `/random/category/news/`. 1524 * - `/random/{author-base}/{author}/` for a post by an author. 1525 * - `/random/{post-type-slug}/` for a post of a post type, using its archive slug if it 1526 * has one, otherwise its rewrite slug. 1527 * 1528 * Only post types registered as `randomable`, and taxonomies used by them, receive rules. 1529 * 1530 * If a published page exists at the random base, the page takes precedence and the 1531 * `/random/` rule is omitted. The same applies to a published post when posts share 1532 * the URL, see {@see WP_Rewrite::post_permalinks_share_random_base()}. The rules are flushed 1533 * when such a page or post changes, see {@see wp_random_content_flush_rewrite_rules_for_post()}. 1534 * 1535 * The `random` query variable can be added to any other archive or search URL, for example 1536 * `/2026/?random`, to request a random post from that archive. 1537 * 1538 * @since 7.2.0 1539 * 1540 * @see wp_is_random_content_redirect_enabled() 1541 * 1542 * @return array<string, string> Array of rewrite rules for random content redirects, keyed by their regex pattern. 1543 * 1544 * @phpstan-return array<non-falsy-string, non-falsy-string> 1545 */ 1546 public function random_rewrite_rules(): array { 1547 if ( ! wp_is_random_content_redirect_enabled() ) { 1548 return array(); 1549 } 1550 1551 $random_slug = $this->get_random_base(); 1552 $random_base = $this->root . $random_slug; 1553 1554 $rules = array(); 1555 1556 // A published page or post at the random base, for example example.com/random/, takes precedence. 1557 $random_base_posts = array( get_page_by_path( $random_slug ) ); 1558 if ( $this->post_permalinks_share_random_base() ) { 1559 $random_base_posts[] = get_page_by_path( $random_slug, OBJECT, 'post' ); 1560 } 1561 1562 $random_base_is_taken = false; 1563 foreach ( $random_base_posts as $random_base_post ) { 1564 if ( $random_base_post instanceof WP_Post && is_post_publicly_viewable( $random_base_post ) ) { 1565 $random_base_is_taken = true; 1566 break; 1567 } 1568 } 1569 1570 if ( ! $random_base_is_taken ) { 1571 $rules[ $random_base . '/?$' ] = $this->index . '?random=1'; 1572 } 1573 1574 $archive_structures = array( 1575 "{$random_base}/{$this->author_base}/%author%", 1576 ); 1577 1578 $randomable_post_types = get_post_types( array( 'randomable' => true ) ); 1579 1580 foreach ( get_taxonomies( array( 'publicly_queryable' => true ), 'objects' ) as $taxonomy ) { 1581 $taxonomy_slug = is_array( $taxonomy->rewrite ) && isset( $taxonomy->rewrite['slug'] ) && is_string( $taxonomy->rewrite['slug'] ) ? $taxonomy->rewrite['slug'] : ''; 1582 1583 // Only taxonomies with pretty permalinks have a registered rewrite tag. 1584 if ( '' === $taxonomy_slug || ! isset( $this->extra_permastructs[ $taxonomy->name ] ) ) { 1585 continue; 1586 } 1587 1588 // Only taxonomies used by randomable post types can contain random content. 1589 if ( ! array_intersect( $taxonomy->object_type, $randomable_post_types ) ) { 1590 continue; 1591 } 1592 1593 $archive_structures[] = "{$random_base}/{$taxonomy_slug}/%{$taxonomy->name}%"; 1594 } 1595 1596 foreach ( $archive_structures as $archive_structure ) { 1597 $archive_rules = $this->generate_rewrite_rules( $archive_structure, EP_NONE, false, false, false, false, false ); 1598 1599 foreach ( $archive_rules as $regex => $query ) { 1600 $rules[ $regex ] = $query . '&random=1'; 1601 } 1602 } 1603 1604 foreach ( get_post_types( array( 'randomable' => true ), 'objects' ) as $post_type ) { 1605 // Post types without rewrites have no pretty permalinks, see {@see WP_Post_Type::add_rewrite_rules()}. 1606 if ( ! is_array( $post_type->rewrite ) ) { 1607 continue; 1608 } 1609 1610 // Try using `has_archive` if it's a string, then try the rewrite slug, otherwise it shouldn't have gotten here, so just skip the post type. 1611 if ( is_string( $post_type->has_archive ) ) { 1612 $post_type_slug = $post_type->has_archive; 1613 } elseif ( isset( $post_type->rewrite['slug'] ) && is_string( $post_type->rewrite['slug'] ) ) { 1614 $post_type_slug = $post_type->rewrite['slug']; 1615 } else { 1616 continue; 1617 } 1618 1619 $rules[ "{$random_base}/{$post_type_slug}/?$" ] = $this->index . "?post_type={$post_type->name}&random=1"; 1620 } 1621 1622 return $rules; 1482 1623 } 1483 1624 … … 1932 2073 } 1933 2074 2075 $random_base = get_option( 'wp_random_base' ); 2076 $this->random_base = is_string( $random_base ) ? $random_base : ''; 2077 1934 2078 unset( $this->author_structure ); 1935 2079 unset( $this->date_structure ); … … 2019 2163 2020 2164 /** 2165 * Sets the random base for random content permalinks. 2166 * 2167 * Will update the 'wp_random_base' option, if there is a difference between the 2168 * current random base and the parameter value. Calls {@see WP_Rewrite::init()} after 2169 * the option is updated. 2170 * 2171 * @since 7.2.0 2172 * 2173 * @param string $random_base Random content permalink base. 2174 */ 2175 public function set_random_base( string $random_base ): void { 2176 if ( get_option( 'wp_random_base' ) !== $random_base ) { 2177 update_option( 'wp_random_base', $random_base ); 2178 $this->init(); 2179 } 2180 } 2181 2182 /** 2183 * Retrieves the random content permalink base. 2184 * 2185 * Requests prefixed with this base redirect to a randomly selected post, 2186 * for example `example.com/random/` or `example.com/random/category/news/`. 2187 * A published page with this slug takes precedence over `example.com/random/`. 2188 * 2189 * Returns the custom base from the 'wp_random_base' option when one is set. 2190 * Otherwise returns the default base, translated into the site language so 2191 * that rewrite rules do not depend on the language of the user who flushes them. 2192 * 2193 * @since 7.2.0 2194 * 2195 * @return string Random content permalink base. 2196 */ 2197 public function get_random_base(): string { 2198 if ( '' !== $this->random_base ) { 2199 return $this->random_base; 2200 } 2201 2202 $switched_locale = switch_to_locale( get_locale() ); 2203 2204 /* 2205 * translators: Default base for random content URLs, such as example.com/random/. 2206 * This string is used in URLs, so it must be lowercase and contain only letters, 2207 * numbers and hyphens. Transliterate it to Latin characters if needed. 2208 */ 2209 $random_base = _x( 'random', 'random content permalink base' ); 2210 2211 if ( $switched_locale ) { 2212 restore_previous_locale(); 2213 } 2214 2215 return '' !== $random_base ? $random_base : 'random'; 2216 } 2217 2218 /** 2219 * Determines whether post permalinks can be at the random content permalink base. 2220 * 2221 * When the permalink structure is only the post name at the root of the site, for 2222 * example `/%postname%/`, a post with the same slug as the random base has the 2223 * same URL as random content requests, for example `example.com/random/`. 2224 * 2225 * Permalink structures with a prefix, such as `/blog/%postname%/` on the main site 2226 * of a subdirectory multisite network, do not conflict with the random base. 2227 * 2228 * @since 7.2.0 2229 * 2230 * @see WP_Rewrite::random_rewrite_rules() 2231 * 2232 * @return bool Whether a post's permalink can be at the random base. 2233 */ 2234 public function post_permalinks_share_random_base(): bool { 2235 if ( ! $this->using_permalinks() ) { 2236 return false; 2237 } 2238 2239 return 1 === preg_match( '#^' . preg_quote( '/' . $this->root, '#' ) . '%postname%/?$#', $this->permalink_structure ); 2240 } 2241 2242 /** 2021 2243 * Constructor - Calls init(), which runs setup. 2022 2244 * -
trunk/src/wp-includes/class-wp.php
r64244 r64402 14 14 * 15 15 * @since 2.0.0 16 * @since 7.2.0 Added the `random` query variable. 16 17 * @var string[] 17 18 */ 18 public $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'pagename', 'page_id', 'error', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'favicon', 'taxonomy', 'term', 'cpage', 'post_type', 'embed' );19 public $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'pagename', 'page_id', 'error', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'favicon', 'taxonomy', 'term', 'cpage', 'post_type', 'embed', 'random' ); 19 20 20 21 /** … … 704 705 705 706 /** 707 * Redirects random content requests to a randomly selected post. 708 * 709 * For random content requests, runs the main query to select a single random post 710 * from the requested archive and redirects to it. For example, `example.com/random/` 711 * redirects to a random post and `example.com/random/category/news/` redirects to a 712 * random post in the "news" category. 713 * 714 * The random post is selected by the main query, so {@see 'pre_get_posts'} callbacks 715 * customizing the archive apply to the selection. While the query runs, callbacks 716 * enforcing the eligible posts, order and limit are added after any other callbacks: 717 * 718 * - {@see wp_random_content_pre_get_posts()} on the {@see 'pre_get_posts'} action. 719 * - {@see wp_random_content_posts_orderby()} on the {@see 'posts_orderby'} filter. 720 * - {@see wp_random_content_post_limits()} on the {@see 'post_limits'} filter. 721 * 722 * If no post is selected, for example on an empty archive, the request is a 404 as 723 * there is no post to redirect to. If a post is selected but the request is not 724 * redirected, the request continues with the results of the main query. The main 725 * query is not run again. 726 * 727 * @since 7.2.0 728 * 729 * @see wp_is_random_content_query() 730 * @global WP_Query $wp_the_query WordPress Query object. 731 * 732 * @return bool Whether the main query was run for a random content request. 733 */ 734 public function handle_random(): bool { 735 global $wp_the_query; 736 737 if ( ! isset( $this->query_vars['random'] ) || ! wp_is_random_content_redirect_enabled() ) { 738 return false; 739 } 740 741 add_action( 'pre_get_posts', 'wp_random_content_pre_get_posts', 20 ); 742 add_filter( 'posts_orderby', 'wp_random_content_posts_orderby', 20, 2 ); 743 add_filter( 'post_limits', 'wp_random_content_post_limits', 20, 2 ); 744 745 $this->query_posts(); 746 747 remove_action( 'pre_get_posts', 'wp_random_content_pre_get_posts', 20 ); 748 remove_filter( 'posts_orderby', 'wp_random_content_posts_orderby', 20 ); 749 remove_filter( 'post_limits', 'wp_random_content_post_limits', 20 ); 750 751 wp_random_content_redirect(); 752 753 if ( wp_is_random_content_query( $wp_the_query ) && ! $wp_the_query->post instanceof WP_Post ) { 754 $wp_the_query->set_404(); 755 status_header( 404 ); 756 nocache_headers(); 757 } 758 759 return true; 760 } 761 762 /** 706 763 * Set the Headers for 404, if nothing is found for requested URL. 707 764 * … … 821 878 822 879 if ( $parsed ) { 823 $this->query_posts(); 880 if ( ! $this->handle_random() ) { 881 $this->query_posts(); 882 } 824 883 $this->handle_404(); 825 884 $this->register_globals(); -
trunk/src/wp-includes/default-filters.php
r64311 r64402 320 320 add_filter( 'option_tag_base', '_wp_filter_taxonomy_base' ); 321 321 add_filter( 'option_category_base', '_wp_filter_taxonomy_base' ); 322 add_filter( 'option_wp_random_base', '_wp_filter_taxonomy_base' ); 322 323 add_filter( 'the_posts', '_close_comments_for_old_posts', 10, 2 ); 323 324 add_filter( 'comments_open', '_close_comments_for_old_post', 10, 2 ); … … 494 495 add_action( 'post_updated', 'wp_check_for_changed_dates', 12, 3 ); 495 496 add_action( 'attachment_updated', 'wp_check_for_changed_dates', 12, 3 ); 497 498 // Random content redirects, see WP::handle_random(). 499 add_filter( 'split_the_query', 'wp_random_content_split_the_query', 10, 2 ); 500 add_action( 'wp_after_insert_post', 'wp_random_content_flush_rewrite_rules_for_post', 10, 4 ); 501 add_action( 'after_delete_post', 'wp_random_content_flush_rewrite_rules_for_post', 10, 2 ); 502 503 // Regenerate rewrite rules in the new site language, they can contain translated slugs. 504 add_action( 'add_option_WPLANG', 'wp_flush_rewrite_rules_on_site_language_change', 10, 0 ); 505 add_action( 'update_option_WPLANG', 'wp_flush_rewrite_rules_on_site_language_change', 10, 0 ); 496 506 497 507 // Nonce check for post previews. -
trunk/src/wp-includes/formatting.php
r64342 r64402 5185 5185 case 'category_base': 5186 5186 case 'tag_base': 5187 case 'wp_random_base': 5187 5188 $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value ); 5188 5189 if ( is_wp_error( $value ) ) { -
trunk/src/wp-includes/option.php
r64311 r64402 237 237 } 238 238 239 if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ), true ) ) {239 if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base', 'wp_random_base' ), true ) ) { 240 240 $value = untrailingslashit( $value ); 241 241 } -
trunk/src/wp-includes/post.php
r64388 r64402 28 28 ), 29 29 'public' => true, 30 'randomable' => true, 30 31 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ 31 32 '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ … … 65 66 'public' => true, 66 67 'publicly_queryable' => false, 68 'randomable' => false, 67 69 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ 68 70 '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ … … 105 107 ), 106 108 'public' => true, 109 'randomable' => false, 107 110 'show_ui' => true, 108 111 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ … … 1726 1729 * * ?{post_type_query_var}={single_post_slug} 1727 1730 * If not set, the default is inherited from $public. 1731 * @type bool $randomable Whether posts of this post type can be selected by random content 1732 * redirects, such as example.com/random/. If not set, the default is 1733 * true when $publicly_queryable is true and $has_archive is set. 1728 1734 * @type bool $show_ui Whether to generate and allow a UI for managing this post type in the 1729 1735 * admin. Default is value of $public. -
trunk/src/wp-includes/query.php
r63906 r64402 699 699 700 700 return $wp_query->is_sitemap(); 701 } 702 703 /** 704 * Determines whether the query is for random content. 705 * 706 * True for a blog home, archive or search request that includes the `random` query 707 * variable, for example `example.com/random/` or `example.com/random/category/news/`. 708 * 709 * @since 7.2.0 710 * 711 * @see wp_is_random_content_query() 712 * @global WP_Query $wp_query WordPress Query object. 713 * 714 * @return bool Whether the query is for random content. 715 */ 716 function is_random(): bool { 717 global $wp_query; 718 719 if ( ! isset( $wp_query ) ) { 720 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '7.2.0' ); 721 return false; 722 } 723 724 return $wp_query->is_random(); 701 725 } 702 726 … … 1241 1265 1242 1266 /** 1267 * Determines whether random content redirects are enabled. 1268 * 1269 * @since 7.2.0 1270 * 1271 * @return bool True if random content redirects are enabled, false otherwise. 1272 */ 1273 function wp_is_random_content_redirect_enabled(): bool { 1274 /** 1275 * Filters whether random content redirects are enabled. 1276 * 1277 * Random content redirects add rewrite rules for URLs such as `/random/`, so the 1278 * rewrite rules need to be flushed after changing the value returned by this filter. 1279 * 1280 * @since 7.2.0 1281 * 1282 * @param bool $enabled Whether random content redirects are enabled. Default true. 1283 */ 1284 return (bool) apply_filters( 'wp_enable_random_content_redirect', true ); 1285 } 1286 1287 /** 1288 * Determines whether a query is a request for random content. 1289 * 1290 * A random content request is a front end, main query for the blog home, an archive, 1291 * or search results that includes the `random` query variable and at least one 1292 * `randomable` post type. For example 1293 * `example.com/random/`, `example.com/random/category/news/` or `example.com/?s=foo&random`. 1294 * 1295 * Singular (including embeds) and feed requests are never random content requests, 1296 * nor are requests using an HTTP method other than GET or HEAD. 1297 * 1298 * @since 7.2.0 1299 * 1300 * @param WP_Query $query The query to check. 1301 * @return bool True if the query is a request for random content, false otherwise. 1302 */ 1303 function wp_is_random_content_query( WP_Query $query ): bool { 1304 if ( 1305 ! $query->is_random() 1306 || ! $query->is_main_query() 1307 || is_admin() 1308 || ! wp_is_random_content_redirect_enabled() 1309 ) { 1310 return false; 1311 } 1312 1313 $request_method = isset( $_SERVER['REQUEST_METHOD'] ) && is_string( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( $_SERVER['REQUEST_METHOD'] ) : ''; 1314 if ( 'GET' !== $request_method && 'HEAD' !== $request_method ) { 1315 return false; 1316 } 1317 1318 return array() !== wp_get_random_content_post_types( $query ); 1319 } 1320 1321 /** 1322 * Retrieves the post types eligible for selection by a random content request. 1323 * 1324 * These are the post types the query would otherwise return, limited to those 1325 * registered as `randomable`. 1326 * 1327 * @since 7.2.0 1328 * 1329 * @see register_post_type() 1330 * 1331 * @param WP_Query $query The query to retrieve the post types for. 1332 * @return string[] Post type names eligible for random selection. 1333 * 1334 * @phpstan-return list<string> 1335 */ 1336 function wp_get_random_content_post_types( WP_Query $query ): array { 1337 $post_types = $query->get( 'post_type' ); 1338 1339 if ( empty( $post_types ) && $query->is_tax() && $query->tax_query instanceof WP_Tax_Query ) { 1340 // Match WP_Query::get_posts(): searchable post types using any of the queried taxonomies. 1341 $taxonomies = array_keys( $query->tax_query->queried_terms ); 1342 $post_types = array(); 1343 1344 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $post_type ) { 1345 $object_taxonomies = 'attachment' === $post_type ? get_taxonomies_for_attachments() : get_object_taxonomies( $post_type ); 1346 if ( array_intersect( $taxonomies, $object_taxonomies ) ) { 1347 $post_types[] = $post_type; 1348 } 1349 } 1350 } elseif ( empty( $post_types ) && ! $query->is_search() ) { 1351 $post_types = array( 'post' ); 1352 } 1353 1354 if ( empty( $post_types ) || 'any' === $post_types ) { 1355 $post_types = get_post_types( array( 'exclude_from_search' => false ) ); 1356 } 1357 1358 $random_post_types = array(); 1359 1360 foreach ( (array) $post_types as $post_type ) { 1361 // Plugins may set invalid values on the `post_type` query variable. 1362 if ( ! is_string( $post_type ) ) { 1363 continue; 1364 } 1365 1366 $post_type_object = get_post_type_object( $post_type ); 1367 1368 if ( $post_type_object && $post_type_object->randomable ) { 1369 $random_post_types[] = $post_type_object->name; 1370 } 1371 } 1372 1373 return $random_post_types; 1374 } 1375 1376 /** 1377 * Limits the main query to posts eligible for random content requests. 1378 * 1379 * The main query is used to select the random post so the request runs the same 1380 * query, and the same filters, as the archive it is randomizing. As such, callbacks 1381 * on the {@see 'pre_get_posts'} action may modify the posts eligible for selection, 1382 * for example to limit the post types or exclude a category: 1383 * 1384 * add_action( 1385 * 'pre_get_posts', 1386 * static function ( WP_Query $query ) { 1387 * if ( wp_is_random_content_query( $query ) ) { 1388 * $query->set( 'category__not_in', array( 1 ) ); 1389 * } 1390 * } 1391 * ); 1392 * 1393 * Only randomable post types and published posts without a password are eligible. 1394 * The number and order of posts are enforced by {@see wp_random_content_post_limits()} and 1395 * {@see wp_random_content_posts_orderby()}, so callbacks modifying them on archives do not 1396 * affect the selection. 1397 * 1398 * Added to the {@see 'pre_get_posts'} action by {@see WP::handle_random()} while the 1399 * random content query runs, so it runs after other callbacks. 1400 * 1401 * @since 7.2.0 1402 * 1403 * @param WP_Query $query The WP_Query instance (passed by reference). 1404 */ 1405 function wp_random_content_pre_get_posts( $query ): void { 1406 if ( ! wp_is_random_content_query( $query ) ) { 1407 return; 1408 } 1409 1410 $query->set( 'post_type', wp_get_random_content_post_types( $query ) ); 1411 $query->set( 'post_status', 'publish' ); 1412 $query->set( 'has_password', false ); 1413 $query->set( 'ignore_sticky_posts', true ); 1414 $query->set( 'no_found_rows', true ); 1415 $query->set( 'update_post_meta_cache', false ); 1416 $query->set( 'update_post_term_cache', false ); 1417 // The order and number of posts are enforced by filters, which a suppressed query skips. 1418 $query->set( 'suppress_filters', false ); 1419 } 1420 1421 /** 1422 * Orders the random content query randomly. 1423 * 1424 * Added to the {@see 'posts_orderby'} filter by {@see WP::handle_random()} while the random 1425 * content query runs. Enforcing the order in SQL prevents {@see 'pre_get_posts'} 1426 * callbacks setting the order of posts on archives from affecting the selection. 1427 * 1428 * @since 7.2.0 1429 * 1430 * @param string $orderby The ORDER BY clause of the query. 1431 * @param WP_Query $query The WP_Query instance. 1432 * @return string The ORDER BY clause of the query. 1433 */ 1434 function wp_random_content_posts_orderby( $orderby, WP_Query $query ): string { 1435 if ( wp_is_random_content_query( $query ) ) { 1436 return 'RAND()'; 1437 } 1438 1439 return (string) $orderby; 1440 } 1441 1442 /** 1443 * Limits the random content query to a single post. 1444 * 1445 * Added to the {@see 'post_limits'} filter by {@see WP::handle_random()} while the random 1446 * content query runs. Enforcing the limit in SQL prevents {@see 'pre_get_posts'} 1447 * callbacks setting the number of posts, offset or page on archives from affecting 1448 * the selection. 1449 * 1450 * @since 7.2.0 1451 * 1452 * @param string $limits The LIMIT clause of the query. 1453 * @param WP_Query $query The WP_Query instance. 1454 * @return string The LIMIT clause of the query. 1455 */ 1456 function wp_random_content_post_limits( $limits, WP_Query $query ): string { 1457 if ( wp_is_random_content_query( $query ) ) { 1458 return 'LIMIT 0, 1'; 1459 } 1460 1461 return (string) $limits; 1462 } 1463 1464 /** 1465 * Prevents the random content query from being split into multiple database queries. 1466 * 1467 * Randomly ordered queries are not cached, so there is no benefit to querying the post 1468 * IDs separately to the post objects. Disabling the split allows the random post to be 1469 * retrieved in a single database query. 1470 * 1471 * @since 7.2.0 1472 * 1473 * @param bool $split_the_query Whether or not to split the query. 1474 * @param WP_Query $query The WP_Query instance. 1475 * @return bool Whether or not to split the query. 1476 */ 1477 function wp_random_content_split_the_query( $split_the_query, WP_Query $query ): bool { 1478 if ( wp_is_random_content_query( $query ) ) { 1479 return false; 1480 } 1481 1482 return (bool) $split_the_query; 1483 } 1484 1485 /** 1486 * Redirects random content requests to the randomly selected post. 1487 * 1488 * Called by {@see WP::handle_random()} immediately after the main query selects the 1489 * random post, to avoid unnecessary processing before the redirect. As the redirect 1490 * is sent before {@see WP::send_headers()}, the headers preventing the redirect being 1491 * cached or indexed are sent with it. 1492 * 1493 * If no post is selected, for example on an empty archive, {@see WP::handle_random()} 1494 * handles the request as a 404. If the post's permalink is on another site, the 1495 * request is handled as a regular archive request. 1496 * 1497 * @since 7.2.0 1498 * 1499 * @global WP_Query $wp_query WordPress Query object. 1500 */ 1501 function wp_random_content_redirect(): void { 1502 global $wp_query; 1503 1504 if ( ! wp_is_random_content_query( $wp_query ) || ! $wp_query->post instanceof WP_Post ) { 1505 return; 1506 } 1507 1508 $location = get_permalink( $wp_query->post ); 1509 1510 // Only consider redirecting permitted locations so wp_safe_redirect doesn't redirect to the fallback. 1511 if ( ! $location || ! wp_validate_redirect( $location, '' ) ) { 1512 return; 1513 } 1514 1515 nocache_headers(); 1516 1517 if ( ! headers_sent() ) { 1518 header( 'X-Robots-Tag: noindex, follow' ); 1519 } 1520 1521 if ( wp_safe_redirect( $location, 302 ) ) { 1522 exit; 1523 } 1524 } 1525 1526 /** 1527 * Flushes the rewrite rules when a page or post at the random content base changes. 1528 * 1529 * A published page at the random base, for example `example.com/random/`, takes 1530 * precedence over random content redirects. So does a published post when the 1531 * permalink structure places posts at the same URL, for example `/%postname%/`, 1532 * see {@see WP_Rewrite::post_permalinks_share_random_base()}. As the rewrite rules account 1533 * for the page or post, they need to be regenerated when it is published, 1534 * unpublished, renamed, moved or deleted. 1535 * 1536 * Runs on the {@see 'wp_after_insert_post'} and {@see 'after_delete_post'} actions. 1537 * 1538 * @since 7.2.0 1539 * 1540 * @see WP_Rewrite::random_rewrite_rules() 1541 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1542 * 1543 * @param int $post_id Post ID. 1544 * @param WP_Post $post Post object. 1545 * @param bool $update Optional. Whether this is an existing post being updated. Default false. 1546 * @param WP_Post|null $post_before Optional. Post object before the update, null for new posts. Default null. 1547 */ 1548 function wp_random_content_flush_rewrite_rules_for_post( int $post_id, WP_Post $post, bool $update = false, ?WP_Post $post_before = null ): void { 1549 global $wp_rewrite; 1550 1551 if ( ! $wp_rewrite->using_permalinks() || ! wp_is_random_content_redirect_enabled() ) { 1552 return; 1553 } 1554 1555 foreach ( array( $post, $post_before ) as $page ) { 1556 if ( 1557 $page instanceof WP_Post 1558 && $wp_rewrite->get_random_base() === $page->post_name 1559 && ( 1560 ( 'page' === $page->post_type && 0 === (int) $page->post_parent ) 1561 || ( 'post' === $page->post_type && $wp_rewrite->post_permalinks_share_random_base() ) 1562 ) 1563 ) { 1564 flush_rewrite_rules( false ); 1565 return; 1566 } 1567 } 1568 } 1569 1570 /** 1243 1571 * Set up global post data. 1244 1572 * -
trunk/src/wp-includes/rewrite.php
r64235 r64402 284 284 $wp_rewrite->flush_rules( $hard ); 285 285 } 286 } 287 288 /** 289 * Clears the rewrite rules when the site language changes. 290 * 291 * Rewrite rules can contain translated slugs, such as the default random content 292 * base, see {@see WP_Rewrite::get_random_base()}. Clearing the rules causes them to be 293 * regenerated in the new site language. 294 * 295 * The rules are regenerated on the next request rather than immediately, because 296 * the new language is not loaded until after the option is saved. Clearing the 297 * rules also avoids rewriting the .htaccess file. 298 * 299 * Runs on the {@see 'add_option_WPLANG'} and {@see 'update_option_WPLANG'} actions. 300 * 301 * @since 7.2.0 302 */ 303 function wp_flush_rewrite_rules_on_site_language_change(): void { 304 update_option( 'rewrite_rules', '' ); 286 305 } 287 306 -
trunk/src/wp-includes/version.php
r64337 r64402 24 24 * @global int $wp_db_version 25 25 */ 26 $wp_db_version = 643 37;26 $wp_db_version = 64342; 27 27 28 28 /** -
trunk/tests/phpstan/baselines/variable.undefined.neon
r64294 r64402 385 385 path: ../../../src/wp-admin/options-permalink.php 386 386 - 387 message: '#^Variable \$wp_rewrite might not be defined\.$#'388 identifier: variable.undefined389 count: 8390 path: ../../../src/wp-admin/options-permalink.php391 -392 387 message: '#^Variable \$options_to_update might not be defined\.$#' 393 388 identifier: variable.undefined -
trunk/tests/phpunit/tests/query/vars.php
r64073 r64402 68 68 'post_type', 69 69 'embed', 70 'random', 70 71 71 72 // Dynamically added public query vars:
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)