| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Builder\Conditions; |
| 4 |
|
| 5 |
class Taxonomy extends Condition { |
| 6 |
protected $taxonomy; |
| 7 |
|
| 8 |
public function __construct( $args = [] ) { |
| 9 |
$this->taxonomy = $args['instance']; |
| 10 |
|
| 11 |
parent::__construct( $args ); |
| 12 |
} |
| 13 |
|
| 14 |
public function get_priority(): int { |
| 15 |
return 70; |
| 16 |
} |
| 17 |
|
| 18 |
public function get_type(): string { |
| 19 |
return 'archive'; |
| 20 |
} |
| 21 |
|
| 22 |
public function get_label(): string { |
| 23 |
return ucwords( $this->taxonomy->label ); |
| 24 |
} |
| 25 |
|
| 26 |
public function check( $args = [] ): bool { |
| 27 |
$taxonomy = $this->get_name(); |
| 28 |
$id = (int) $args['id']; |
| 29 |
|
| 30 |
if ( 'category' === $taxonomy ) { |
| 31 |
return is_category( $id ); |
| 32 |
} |
| 33 |
|
| 34 |
if ( 'post_tag' === $taxonomy ) { |
| 35 |
return is_tag( $id ); |
| 36 |
} |
| 37 |
|
| 38 |
return is_tax( $taxonomy, $id ); |
| 39 |
} |
| 40 |
|
| 41 |
public function get_name(): string { |
| 42 |
return $this->taxonomy->name; |
| 43 |
} |
| 44 |
|
| 45 |
protected function register_controls() { |
| 46 |
$this->add_control( 'taxonomy', [ |
| 47 |
'field' => 'term_id', |
| 48 |
'query_type' => 'taxonomy', |
| 49 |
'options' => [ '' => 'All' ], |
| 50 |
'query' => [ 'taxonomy' => $this->get_name() ] |
| 51 |
] ); |
| 52 |
} |
| 53 |
} |