bool, // is_front_page() * 'singular' => bool, // is_singular() * 'object_id' => int, // queried object id on singular views, else 0 * 'post_type' => string, // queried object's post type on singular views, else '' * 'view' => string, // see view(): singular, blog_index, category, search, 404... * 'archive_of' => string, // post type whose archive this view is (ARCHIVE_SCOPE), else '' * 'legacy_post_id' => ?int, // global $post->ID -- today's lookup, fallback only * 'legacy_post_type' => string|false, // get_post_type() -- today's lookup, fallback only * ] * * Archives are mapped in exactly one place, ARCHIVE_SCOPE below. * * The blog index and the shop are archives, so they carry no object id and * a specific-item rule never matches them, exactly as today's footer lookup. */ class Request_Context { /** In ARCHIVE_SCOPE: the archive belongs to the post type being queried. */ const QUERIED_POST_TYPE = '*queried*'; /** * ARCHIVE SCOPE (KTD10, session-settled): which non-singular views count * as a post type's archive, so that "All posts", "All products" or a * custom post type rule also shows there. view => post type. * * Not listed, so covered by Entire site only: 'search', 'author', * 'taxonomy' (non-product custom taxonomies), '404', 'other'. Today's * 1.1.x lookup reached some of these through the loop's first post; the * owner has not yet decided whether to cover them. Covering one is a * single line here, e.g. 'search' => 'post'. */ const ARCHIVE_SCOPE = [ 'blog_index' => 'post', 'category' => 'post', 'tag' => 'post', 'date' => 'post', 'shop' => 'product', 'product_taxonomy' => 'product', 'post_type_archive' => self::QUERIED_POST_TYPE, ]; /** @var array|null */ private static $current = null; /** * The request's context, built once after `wp` and reused for the rest of * the request. Before `wp` it is built fresh on every call and not kept. */ public static function current(): array { if (null !== self::$current) { return self::$current; } $context = self::build(); if (did_action('wp')) { self::$current = $context; } return $context; } public static function build(): array { $singular = (bool) is_singular(); $view = self::view($singular); $object_id = $singular ? (int) get_queried_object_id() : 0; $post_type = ''; if ($singular && $object_id > 0) { $type = get_post_type($object_id); $post_type = is_string($type) ? $type : ''; } global $post; return [ 'front_page' => (bool) is_front_page(), 'singular' => $singular, 'object_id' => $object_id, 'post_type' => $post_type, 'view' => $view, 'archive_of' => self::archive_of($view), 'legacy_post_id' => isset($post->ID) ? (int) $post->ID : null, 'legacy_post_type' => get_post_type(), ]; } /** Forget the cached context. For tests, and for code that switches the main query. */ public static function reset(): void { self::$current = null; } /** * The post type whose archive a view is, per ARCHIVE_SCOPE, or ''. Pure, * so the migration's checks read the same scope as live requests. * * @param string $view From view(). * @param string $queried_post_type The queried post type, for 'post_type_archive'. */ public static function archive_for_view(string $view, string $queried_post_type = ''): string { if (!array_key_exists($view, self::ARCHIVE_SCOPE)) { return ''; } $post_type = self::ARCHIVE_SCOPE[$view]; return self::QUERIED_POST_TYPE === $post_type ? $queried_post_type : $post_type; } private static function archive_of(string $view): string { $queried = ''; if (self::QUERIED_POST_TYPE === (isset(self::ARCHIVE_SCOPE[$view]) ? self::ARCHIVE_SCOPE[$view] : null)) { $post_type = get_query_var('post_type'); if (is_array($post_type)) { $post_type = reset($post_type); } $queried = is_string($post_type) ? $post_type : ''; } return self::archive_for_view($view, $queried); } /** * Names the kind of view this request is. The first match wins. */ private static function view(bool $singular): string { // is_shop() is also true when WooCommerce leaves the shop a singular page. if (function_exists('is_shop') && is_shop()) { return 'shop'; } if ($singular) { return 'singular'; } if (is_home()) { return 'blog_index'; } if (is_category()) { return 'category'; } if (is_tag()) { return 'tag'; } if (is_date()) { return 'date'; } if (is_tax(['product_cat', 'product_tag'])) { return 'product_taxonomy'; } if (is_post_type_archive()) { return 'post_type_archive'; } if (is_search()) { return 'search'; } if (is_author()) { return 'author'; } if (is_tax()) { return 'taxonomy'; } if (is_404()) { return '404'; } return 'other'; } }