| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Builder\Conditions; |
| 4 |
|
| 5 |
class Post extends Condition { |
| 6 |
private $post_type; |
| 7 |
|
| 8 |
public function __construct( $args = [] ) { |
| 9 |
$this->post_type = get_post_type_object( $args['post_type'] ); |
| 10 |
|
| 11 |
parent::__construct( $args ); |
| 12 |
} |
| 13 |
|
| 14 |
public function get_priority(): int { |
| 15 |
return 40; |
| 16 |
} |
| 17 |
|
| 18 |
public function get_type(): string { |
| 19 |
return 'singular'; |
| 20 |
} |
| 21 |
|
| 22 |
public function get_label(): string { |
| 23 |
return $this->post_type->labels->singular_name; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_all_label(): string { |
| 27 |
return $this->post_type->label; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_name(): string { |
| 31 |
return $this->post_type->name; |
| 32 |
} |
| 33 |
|
| 34 |
public function check( $args = [] ): bool { |
| 35 |
if ( isset( $args['id'] ) ) { |
| 36 |
$id = (int) $args['id']; |
| 37 |
if ( $id ) { |
| 38 |
return is_singular() && get_queried_object_id() === $id; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return is_singular( $this->post_type->name ); |
| 43 |
} |
| 44 |
|
| 45 |
public function register_sub_conditions() { |
| 46 |
// foreach ( $this->post_taxonomies as $slug => $object ) { |
| 47 |
// $in_taxonomy = new In_Taxonomy( [ |
| 48 |
// 'object' => $object, |
| 49 |
// ] ); |
| 50 |
// $this->register_sub_condition( $in_taxonomy ); |
| 51 |
// |
| 52 |
// if ( $object->hierarchical ) { |
| 53 |
// $in_sub_term = new In_Sub_Term( [ |
| 54 |
// 'object' => $object, |
| 55 |
// ] ); |
| 56 |
// $this->register_sub_condition( $in_sub_term ); |
| 57 |
// } |
| 58 |
// } |
| 59 |
|
| 60 |
$by_author = new PostByAuthor( $this->post_type ); |
| 61 |
$this->register_sub_condition( $by_author ); |
| 62 |
} |
| 63 |
|
| 64 |
protected function register_controls() { |
| 65 |
$this->add_control( "posts_query_" . $this->get_name(), [ |
| 66 |
'field' => 'ID', |
| 67 |
'query_type' => 'posts', |
| 68 |
'options' => [ '' => 'All' ], |
| 69 |
'query' => [ 'post_type' => $this->get_name() ] |
| 70 |
] ); |
| 71 |
} |
| 72 |
} |