page = self::resolvePage(); // Order matters. is_home() is true on a blog-posts front page and // is_front_page() is true on a static one, so the front page has to be // settled before anything else claims the request. if (is_404()) { $context->type = self::TYPE_NOT_FOUND; $context->settingsKey = 'notFound'; return $context; } if (is_search()) { $context->type = self::TYPE_SEARCH; $context->settingsKey = 'search'; return $context; } if (is_front_page() || is_home()) { $context->type = self::TYPE_HOME; $context->settingsKey = 'home'; // A static front page is still a real post, and its per-post overrides // have to win over the home template. $frontId = (int) get_option('page_on_front'); if (is_front_page() && 'page' === get_option('show_on_front') && $frontId) { $context->object = get_post($frontId); $context->objectId = $frontId; $context->postType = 'page'; } return $context; } if (is_singular()) { $post = get_queried_object(); $context->type = self::TYPE_SINGULAR; $context->object = $post instanceof \WP_Post ? $post : null; $context->objectId = $context->object ? (int) $context->object->ID : 0; $context->postType = $context->object ? $context->object->post_type : ''; $context->settingsKey = $context->postType; return $context; } if (is_post_type_archive()) { $postType = get_query_var('post_type'); if (is_array($postType)) { $postType = reset($postType); } $context->type = self::TYPE_POST_TYPE_ARCHIVE; $context->postType = (string) $postType; $context->settingsKey = 'pta_' . $context->postType; return $context; } if (is_category() || is_tag() || is_tax()) { $term = get_queried_object(); $context->type = self::TYPE_TAXONOMY; $context->object = $term instanceof \WP_Term ? $term : null; $context->objectId = $context->object ? (int) $context->object->term_id : 0; $context->taxonomy = $context->object ? $context->object->taxonomy : ''; $context->settingsKey = 'tax_' . $context->taxonomy; return $context; } if (is_author()) { $user = get_queried_object(); $context->type = self::TYPE_AUTHOR; $context->object = $user instanceof \WP_User ? $user : null; $context->objectId = $context->object ? (int) $context->object->ID : 0; $context->settingsKey = 'author'; return $context; } if (is_date()) { $context->type = self::TYPE_DATE; $context->settingsKey = 'date'; return $context; } // Anything left is an archive shape we do not template separately. $context->type = self::TYPE_POST_TYPE_ARCHIVE; $context->settingsKey = 'pta_post'; return $context; } /** * Both `paged` (archive pagination) and `page` (a multi-page single post) * matter, and only one is ever set, so taking the larger gets the right * number without caring which kind of view this is. */ protected static function resolvePage() { $paged = (int) get_query_var('paged'); $page = (int) get_query_var('page'); return max(1, $paged, $page); } public function isPaged() { return $this->page > 1; } /** * True when this view is about a post that actually exists. Guards every * get_post_meta() call, since objectId is also set for terms and users. */ public function hasPost() { return $this->object instanceof \WP_Post; } public function hasTerm() { return $this->object instanceof \WP_Term; } public function hasUser() { return $this->object instanceof \WP_User; } }