TaxonomyFactory.php
1.28 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
<?php
declare(strict_types=1);
namespace ACP\ListScreenFactory;
use AC;
use AC\ListScreen;
use ACP\ListScreen\Taxonomy;
use LogicException;
use WP_Screen;
class TaxonomyFactory extends AC\ListScreenFactory\BaseFactory
{
public function can_create(string $key): bool
{
return null !== $this->get_taxonomy($key);
}
private function get_taxonomy(string $key): ?string
{
if ( ! ac_helper()->string->starts_with($key, 'wp-taxonomy_')) {
return null;
}
$taxonomy = substr($key, 12);
return taxonomy_exists($taxonomy)
? $taxonomy
: null;
}
public function can_create_from_wp_screen(WP_Screen $screen): bool
{
return 'edit-tags' === $screen->base && $screen->taxonomy && $screen->taxonomy === filter_input(
INPUT_GET,
'taxonomy'
);
}
protected function create_list_screen(string $key): ListScreen
{
$taxonomy = $this->get_taxonomy($key);
if ( ! $taxonomy) {
throw new LogicException('Invalid taxonomy');
}
return new Taxonomy($taxonomy);
}
protected function create_list_screen_from_wp_screen(WP_Screen $screen): ListScreen
{
return new Taxonomy($screen->taxonomy);
}
}