TaxonomyFactory.php
1.46 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
<?php
declare( strict_types=1 );
namespace ACP\ListScreenFactory;
use AC;
use AC\ListScreen;
use AC\ListScreenFactory\ListSettingsTrait;
use ACP\ListScreen\Taxonomy;
use LogicException;
use WP_Screen;
class TaxonomyFactory implements AC\ListScreenFactoryInterface {
use ListSettingsTrait;
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_by_wp_screen( WP_Screen $screen ): bool {
return 'edit-tags' === $screen->base && $screen->taxonomy && $screen->taxonomy === filter_input( INPUT_GET, 'taxonomy' );
}
public function create( string $key, array $settings = [] ): ListScreen {
if ( ! $this->can_create( $key ) ) {
throw new LogicException( 'Invalid key' );
}
$taxonomy = $this->get_taxonomy( $key );
if ( ! $taxonomy ) {
throw new LogicException( 'Invalid taxonomy' );
}
return $this->add_settings( new Taxonomy( $taxonomy ), $settings );
}
public function create_by_wp_screen( WP_Screen $screen, array $settings = [] ): ListScreen {
if ( ! $this->can_create_by_wp_screen( $screen ) ) {
throw new LogicException( 'Invalid screen' );
}
return $this->add_settings( new Taxonomy( $screen->taxonomy ), $settings );
}
}