|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\General; |
|
6
|
|
|
|
|
7
|
|
|
class EditorHelpers |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Get the post type currently being created/edited in the editor |
|
11
|
|
|
* |
|
12
|
|
|
* @return string |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function getEditingPostType(): ?string |
|
15
|
|
|
{ |
|
16
|
|
|
// When in the editor, there is no direct way to obtain the post type in hook "init", |
|
17
|
|
|
// since $typenow has not been initialized yet |
|
18
|
|
|
// Hence, recreate the logic to get post type from URL if we are on post-new.php, or |
|
19
|
|
|
// from edited post in post.php |
|
20
|
|
|
if (!\is_admin()) { |
|
21
|
|
|
return null; |
|
22
|
|
|
} |
|
23
|
|
|
global $pagenow; |
|
24
|
|
|
if (!in_array($pagenow, ['post-new.php', 'post.php'])) { |
|
25
|
|
|
return null; |
|
26
|
|
|
} |
|
27
|
|
|
$typenow = null; |
|
28
|
|
|
if ('post-new.php' === $pagenow) { |
|
29
|
|
|
if (isset($_REQUEST['post_type']) && \post_type_exists($_REQUEST['post_type'])) { |
|
30
|
|
|
$typenow = $_REQUEST['post_type']; |
|
31
|
|
|
}; |
|
32
|
|
|
} elseif ('post.php' === $pagenow) { |
|
33
|
|
|
$post_id = null; |
|
34
|
|
|
if (isset($_GET['post']) && isset($_POST['post_ID']) && (int) $_GET['post'] !== (int) $_POST['post_ID']) { |
|
35
|
|
|
// Do nothing |
|
36
|
|
|
} elseif (isset($_GET['post'])) { |
|
37
|
|
|
$post_id = (int) $_GET['post']; |
|
38
|
|
|
} elseif (isset($_POST['post_ID'])) { |
|
39
|
|
|
$post_id = (int) $_POST['post_ID']; |
|
40
|
|
|
} |
|
41
|
|
|
if (!is_null($post_id)) { |
|
42
|
|
|
$post = \get_post($post_id); |
|
43
|
|
|
if (!is_null($post)) { |
|
44
|
|
|
$typenow = $post->post_type; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
return $typenow; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the post ID currently being edited in the editor |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function getEditingPostID(): ?int |
|
55
|
|
|
{ |
|
56
|
|
|
if (!\is_admin()) { |
|
57
|
|
|
return null; |
|
58
|
|
|
} |
|
59
|
|
|
global $pagenow; |
|
60
|
|
|
if ($pagenow != 'post.php') { |
|
61
|
|
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
$post_id = null; |
|
64
|
|
|
if (isset($_GET['post']) && isset($_POST['post_ID']) && (int) $_GET['post'] !== (int) $_POST['post_ID']) { |
|
65
|
|
|
// Do nothing |
|
66
|
|
|
} elseif (isset($_GET['post'])) { |
|
67
|
|
|
$post_id = (int) $_GET['post']; |
|
68
|
|
|
} elseif (isset($_POST['post_ID'])) { |
|
69
|
|
|
$post_id = (int) $_POST['post_ID']; |
|
70
|
|
|
} |
|
71
|
|
|
return $post_id; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|