| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Rules\Condition\CPT; |
| 4 |
|
| 5 |
use Depicter\Rules\Condition\Base as ConditionBase; |
| 6 |
|
| 7 |
class HasTerm extends ConditionBase { |
| 8 |
|
| 9 |
/** |
| 10 |
* @inheritdoc |
| 11 |
*/ |
| 12 |
public $slug = 'CPT_HasTerm'; |
| 13 |
|
| 14 |
/** |
| 15 |
* @inheritdoc |
| 16 |
*/ |
| 17 |
public $control = 'remoteMultiSelect'; |
| 18 |
|
| 19 |
/** |
| 20 |
* @inheritdoc |
| 21 |
*/ |
| 22 |
protected $queryable = true; |
| 23 |
|
| 24 |
/** |
| 25 |
* @inheritdoc |
| 26 |
*/ |
| 27 |
protected $belongsTo = 'CPT'; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var \WP_Taxonomy |
| 31 |
*/ |
| 32 |
protected $tax; |
| 33 |
|
| 34 |
public function __construct( string $tax = '' ){ |
| 35 |
if ( ! empty( $tax ) ) { |
| 36 |
$this->slug = $this->slug . '|' . $tax; |
| 37 |
$this->setTaxonomy(); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Set taxonomy object |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function setTaxonomy(){ |
| 47 |
if ( ! strpos( $this->slug, '|' ) ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$tax = explode( '|', $this->slug )[1]; |
| 52 |
$this->tax = get_taxonomy( $tax ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @inheritdoc |
| 57 |
*/ |
| 58 |
public function getLabel(): ?string{ |
| 59 |
return $this->tax->label; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @inheritdoc |
| 64 |
*/ |
| 65 |
public function getQueryResults(){ |
| 66 |
$this->setTaxonomy(); |
| 67 |
|
| 68 |
$terms = get_terms([ |
| 69 |
'taxonomy' => $this->tax->name, |
| 70 |
'hide_empty' => false, |
| 71 |
'orderby' => 'name', |
| 72 |
'order' => 'ASC', |
| 73 |
]); |
| 74 |
|
| 75 |
return array_map( function( $term ){ |
| 76 |
return [ |
| 77 |
'label' => $term->name, |
| 78 |
'value' => $term->term_id |
| 79 |
]; |
| 80 |
}, $terms ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @inheritdoc |
| 85 |
*/ |
| 86 |
public function check( $value = null ): bool{ |
| 87 |
global $post; |
| 88 |
|
| 89 |
if ( is_null( $post ) ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
$this->setTaxonomy(); |
| 94 |
|
| 95 |
$value = $value ?? $this->value; |
| 96 |
|
| 97 |
$isIncluded = empty( $value ) || $this->hasTerm( $post, $value ); |
| 98 |
|
| 99 |
return $this->selectionMode === 'include' ? $isIncluded : !$isIncluded; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @param \WP_Post $post |
| 104 |
* @param string $tax |
| 105 |
* @param array $value |
| 106 |
* |
| 107 |
* @return bool |
| 108 |
*/ |
| 109 |
public function hasTerm( \WP_Post $post, array $value ): bool{ |
| 110 |
|
| 111 |
if ( ! $this->tax || $post->post_type != $this->tax->object_type[0] ) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
if ( empty( $value ) ) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
foreach( $value as $termID ){ |
| 120 |
$result = $termID == 'all' || has_term( $termID, $this->tax->name ); |
| 121 |
|
| 122 |
if( $result ){ |
| 123 |
return true; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|