| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Builder\Conditions; |
| 4 |
|
| 5 |
|
| 6 |
class PostTypeArchive extends Condition { |
| 7 |
protected $post_type; |
| 8 |
protected $post_taxonomies = []; |
| 9 |
|
| 10 |
public function __construct( $args = [] ) { |
| 11 |
$this->post_type = get_post_type_object( $args['post_type'] ); |
| 12 |
$taxonomies = get_object_taxonomies( $args['post_type'], 'objects' ); |
| 13 |
|
| 14 |
$this->post_taxonomies = wp_filter_object_list( $taxonomies, [ |
| 15 |
'public' => true, |
| 16 |
'show_in_nav_menus' => true, |
| 17 |
] ); |
| 18 |
|
| 19 |
parent::__construct( $args ); |
| 20 |
} |
| 21 |
|
| 22 |
public function get_priority(): int { |
| 23 |
return 70; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_name(): string { |
| 27 |
return $this->post_type->name . '_archive'; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_all_label(): string { |
| 31 |
return sprintf( __( '%s Archive', 'templately' ), $this->post_type->label ); |
| 32 |
} |
| 33 |
|
| 34 |
public function get_label(): string { |
| 35 |
return sprintf( __( '%s Archive', 'templately' ), $this->post_type->labels->singular_name ); |
| 36 |
} |
| 37 |
|
| 38 |
public function get_type(): string { |
| 39 |
return 'archive'; |
| 40 |
} |
| 41 |
|
| 42 |
public function check( $args = [] ): bool { |
| 43 |
return is_post_type_archive( $this->post_type->name ) || ( 'post' === $this->post_type->name && is_home() ); |
| 44 |
} |
| 45 |
|
| 46 |
protected function register_sub_conditions() { |
| 47 |
if ( ! empty( $this->post_taxonomies ) ) { |
| 48 |
foreach ( $this->post_taxonomies as $taxonomy => $taxonomy_object ) { |
| 49 |
$condition = new Taxonomy( [ |
| 50 |
'taxonomy' => $taxonomy, |
| 51 |
'instance' => $taxonomy_object |
| 52 |
] ); |
| 53 |
|
| 54 |
$this->register_sub_condition( $condition ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
if ( $this->post_type->name === 'product' ) { |
| 59 |
$condition = new ProductSearch(); |
| 60 |
$this->register_sub_condition( $condition ); |
| 61 |
} |
| 62 |
} |
| 63 |
} |