-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfunctions.php
More file actions
132 lines (110 loc) · 4.6 KB
/
Copy pathfunctions.php
File metadata and controls
132 lines (110 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
namespace n5s\PageForCustomPostType {
use n5s\PageForCustomPostType\Core\Api;
/**
* Check if the current page is a page for custom post type.
*
* @param string|null $postType Optional post type to check for specifically.
*/
function is_page_for_custom_post_type(?string $postType = null): bool
{
return Plugin::getInstance()->getApi()->isPageForCustomPostType($postType);
}
/**
* Get the custom post type associated with a page ID.
*
* @param int $pageId The page ID to check.
* @return string|null The post type name, or null if not found.
*/
function get_custom_post_type_for_page(int $pageId): ?string
{
return Plugin::getInstance()->getApi()->getPostTypeFromPageId($pageId);
}
/**
* Get the page ID assigned to a custom post type.
*
* @param string|null $postType The post type name. If null, uses current query's post type.
* @return int|null The page ID, or null if not found.
*/
function get_page_id_for_custom_post_type(?string $postType = null): ?int
{
if ($postType === null) {
$wpQuery = $GLOBALS['wp_query'] ?? null;
if (!$wpQuery instanceof \WP_Query) {
return null;
}
$postType = $wpQuery->{Api::QUERY_VAR_IS_PFCPT} ?? null;
if (!\is_string($postType)) {
return null;
}
}
return Plugin::getInstance()->getApi()->getPageIdFromPostType($postType);
}
/**
* Get the URL for a custom post type's archive page.
*
* @param string|null $postType The post type name. If null, uses current query's post type.
* @return string|null The page URL, or null if not found.
*/
function get_page_url_for_custom_post_type(?string $postType = null): ?string
{
$pageId = get_page_id_for_custom_post_type($postType);
if (!$pageId) {
return null;
}
$url = get_permalink($pageId);
return $url !== false ? $url : null;
}
}
namespace {
// Deprecated back-compat shims. The function_exists() guards run during
// Composer's files autoload, before coverage starts, so they cannot be
// covered; the function bodies are exercised by PublicApiTest.
// @codeCoverageIgnoreStart
if (!\function_exists('is_page_for_custom_post_type')) {
/**
* @deprecated 1.0.0 Use \n5s\PageForCustomPostType\is_page_for_custom_post_type() instead.
*/
// phpcs:ignore Syde.Functions.DisallowGlobalFunction.Found
function is_page_for_custom_post_type(?string $postType = null): bool
{
_deprecated_function(__FUNCTION__, '1.0.0', '\n5s\PageForCustomPostType\is_page_for_custom_post_type()');
return \n5s\PageForCustomPostType\is_page_for_custom_post_type($postType);
}
}
if (!\function_exists('get_custom_post_type_for_page')) {
/**
* @deprecated 1.0.0 Use \n5s\PageForCustomPostType\get_custom_post_type_for_page() instead.
*/
// phpcs:ignore Syde.Functions.DisallowGlobalFunction.Found
function get_custom_post_type_for_page(int $pageId): ?string
{
_deprecated_function(__FUNCTION__, '1.0.0', '\n5s\PageForCustomPostType\get_custom_post_type_for_page()');
return \n5s\PageForCustomPostType\get_custom_post_type_for_page($pageId);
}
}
if (!\function_exists('get_page_id_for_custom_post_type')) {
/**
* @deprecated 1.0.0 Use \n5s\PageForCustomPostType\get_page_id_for_custom_post_type() instead.
*/
// phpcs:ignore Syde.Functions.DisallowGlobalFunction.Found
function get_page_id_for_custom_post_type(?string $postType = null): ?int
{
_deprecated_function(__FUNCTION__, '1.0.0', '\n5s\PageForCustomPostType\get_page_id_for_custom_post_type()');
return \n5s\PageForCustomPostType\get_page_id_for_custom_post_type($postType);
}
}
if (!\function_exists('get_page_url_for_custom_post_type')) {
/**
* @deprecated 1.0.0 Use \n5s\PageForCustomPostType\get_page_url_for_custom_post_type() instead.
*/
// phpcs:ignore Syde.Functions.DisallowGlobalFunction.Found
function get_page_url_for_custom_post_type(?string $postType = null): ?string
{
_deprecated_function(__FUNCTION__, '1.0.0', '\n5s\PageForCustomPostType\get_page_url_for_custom_post_type()');
return \n5s\PageForCustomPostType\get_page_url_for_custom_post_type($postType);
}
}
// @codeCoverageIgnoreEnd
}