ListKeysFactory.php
1.42 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
<?php
declare( strict_types=1 );
namespace AC\Table;
use AC\Type\ListKey;
use LogicException;
class ListKeysFactory implements ListKeysFactoryInterface {
public function create(): ListKeyCollection {
if ( ! did_action( 'init' ) ) {
throw new LogicException( "Call after the `init` hook." );
}
$keys = new ListKeyCollection();
foreach ( $this->get_post_types() as $post_type ) {
$post_type_object = get_post_type_object( $post_type );
if ( $post_type_object ) {
$keys->add( new ListKey( $post_type ) );
}
}
$keys->add( new ListKey( 'wp-comments' ) );
$keys->add( new ListKey( 'wp-users' ) );
$keys->add( new ListKey( 'wp-media' ) );
do_action( 'ac/list_keys', $keys );
return $keys;
}
protected function get_post_types(): array {
$post_types = get_post_types( [
'_builtin' => false,
'show_ui' => true,
] );
foreach ( [ 'post', 'page' ] as $builtin ) {
if ( post_type_exists( $builtin ) ) {
$post_types[ $builtin ] = $builtin;
}
}
// Reusable content blocks for Gutenberg
$wp_block = 'wp_block';
if ( post_type_exists( $wp_block ) && $this->has_post( $wp_block ) ) {
$post_types[ $wp_block ] = $wp_block;
}
return apply_filters( 'ac/post_types', $post_types );
}
private function has_post( string $post_type ): bool {
global $wpdb;
return (bool) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s LIMIT 1", $post_type ) );
}
}