| 1 |
<?php |
| 2 |
/** |
| 3 |
* Theme Builder request context utilities. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons\Theme_Builder; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Builds normalized context information for template resolution. |
| 16 |
*/ |
| 17 |
class Context |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Build context data for the current request. |
| 21 |
* |
| 22 |
* @return array<string,mixed> |
| 23 |
*/ |
| 24 |
public static function from_request(): array |
| 25 |
{ |
| 26 |
$post_id = get_queried_object_id() ? (int) get_queried_object_id() : null; |
| 27 |
$post_type = $post_id ? get_post_type($post_id) : null; |
| 28 |
$author_id = is_author() ? (int) get_query_var('author') : null; |
| 29 |
$is_front_page = is_front_page(); |
| 30 |
// is_home() returns true for the blog posts page (whether it's also the front or not) |
| 31 |
$is_blog_page = is_home(); |
| 32 |
$term_id = null; |
| 33 |
$taxonomy = null; |
| 34 |
$location = 'unknown'; |
| 35 |
$type = 'single'; |
| 36 |
|
| 37 |
if (is_category() || is_tag() || is_tax()) { |
| 38 |
$term = get_queried_object(); |
| 39 |
if ($term && isset($term->term_id, $term->taxonomy)) { |
| 40 |
$term_id = (int) $term->term_id; |
| 41 |
$taxonomy = $term->taxonomy; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if (is_404()) { |
| 46 |
$type = 'not_found'; |
| 47 |
$location = 'not_found'; |
| 48 |
} elseif (is_search()) { |
| 49 |
$type = 'search'; |
| 50 |
$location = 'search_results'; |
| 51 |
} elseif (is_author()) { |
| 52 |
$type = 'author'; |
| 53 |
$location = 'author_all'; |
| 54 |
} elseif ($is_blog_page && !is_singular()) { |
| 55 |
// Blog posts page (archive of posts) - must check before is_singular |
| 56 |
// because is_home() can be true on static front page setups |
| 57 |
$type = 'archive'; |
| 58 |
$location = 'archive_blog'; |
| 59 |
} elseif (is_singular()) { |
| 60 |
$type = 'single'; |
| 61 |
if ('post' === $post_type) { |
| 62 |
$location = 'single_post'; |
| 63 |
} elseif ('page' === $post_type) { |
| 64 |
$location = 'single_page'; |
| 65 |
} elseif (!empty($post_type)) { |
| 66 |
$location = 'single_' . $post_type; |
| 67 |
} else { |
| 68 |
$location = 'single'; |
| 69 |
} |
| 70 |
} elseif (is_archive()) { |
| 71 |
$type = 'archive'; |
| 72 |
if (is_category()) { |
| 73 |
$location = 'archive_category'; |
| 74 |
} elseif (is_tag()) { |
| 75 |
$location = 'archive_tag'; |
| 76 |
} elseif (is_tax() && $taxonomy) { |
| 77 |
$location = 'archive_' . $taxonomy; |
| 78 |
} else { |
| 79 |
$location = 'archive'; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return [ |
| 84 |
'type' => $type, |
| 85 |
'location' => $location, |
| 86 |
'post_id' => $post_id, |
| 87 |
'post_type' => $post_type, |
| 88 |
'author_id' => $author_id, |
| 89 |
'term_id' => $term_id, |
| 90 |
'taxonomy' => $taxonomy, |
| 91 |
'is_front_page' => $is_front_page, |
| 92 |
'is_blog_page' => $is_blog_page, |
| 93 |
]; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|