`) * and post type archives (`type:`). * * WordPress 7.2 picks random posts from "randomable" post types: the * `randomable` argument of `register_post_type()`, which defaults to * true for publicly queryable post types with an archive (posts yes, * pages and attachments no). Taxonomies count when they are publicly * queryable and attached to such a post type. On older WordPress the * argument doesn't exist, so the same default is worked out here. */ final class Archives { /** * Choice key => label. * * @return array */ public static function choices(): array { $choices = array(); $post_types = self::post_types(); foreach ( get_taxonomies( array( 'publicly_queryable' => true ), 'objects' ) as $taxonomy ) { // Post formats are a theme detail, not an archive people browse. if ( 'post_format' === $taxonomy->name || ! array_intersect( (array) $taxonomy->object_type, $post_types ) ) { continue; } /* translators: %s: singular taxonomy name, e.g. "Category". */ $label = sprintf( __( '%s archives', 'all-in-one-utilities' ), $taxonomy->labels->singular_name ); // WooCommerce's product categories are also called "Category": name the post type when it isn't posts. $owner = in_array( 'post', (array) $taxonomy->object_type, true ) ? null : get_post_type_object( (string) current( (array) $taxonomy->object_type ) ); if ( $owner ) { /* translators: 1: e.g. "Category archives", 2: post type name, e.g. "Products". */ $label = sprintf( __( '%1$s (%2$s)', 'all-in-one-utilities' ), $label, $owner->labels->name ); } $choices[ 'tax:' . $taxonomy->name ] = $label; } foreach ( $post_types as $post_type ) { if ( 'post' === $post_type ) { continue; // Its archive is the blog itself: the site-wide /random/, which stays off. } $object = get_post_type_object( $post_type ); if ( $object ) { /* translators: %s: post type name, e.g. "Products". */ $choices[ 'type:' . $post_type ] = sprintf( __( '%s archive', 'all-in-one-utilities' ), $object->labels->name ); } } return $choices; } /** * Post types random content can pick from. * * @return string[] */ public static function post_types(): array { $names = array(); foreach ( get_post_types( array(), 'objects' ) as $object ) { if ( property_exists( $object, 'randomable' ) && null !== $object->randomable ) { $eligible = (bool) $object->randomable; } else { $eligible = 'post' === $object->name || ( $object->publicly_queryable && $object->has_archive ); } if ( $eligible ) { $names[] = $object->name; } } return $names; } }