| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Rules\Condition\CPT; |
| 4 |
|
| 5 |
use Averta\Core\Utility\Arr; |
| 6 |
use Depicter\Rules\Condition\CPT\Base as CPTConditionBase; |
| 7 |
class IsSingle extends CPTConditionBase |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @inheritdoc |
| 11 |
*/ |
| 12 |
public $slug = 'CPT_IsSingle'; |
| 13 |
|
| 14 |
/** |
| 15 |
* @inheritdoc |
| 16 |
*/ |
| 17 |
public $control = 'multiSelect'; |
| 18 |
|
| 19 |
/** |
| 20 |
* @inheritdoc |
| 21 |
*/ |
| 22 |
protected $belongsTo = 'CPT'; |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* @inheritdoc |
| 27 |
*/ |
| 28 |
public function getLabel(): ?string{ |
| 29 |
return __( "Single Page", 'depicter'); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @inheritDoc |
| 34 |
*/ |
| 35 |
public function getControlOptions(){ |
| 36 |
$options = parent::getControlOptions(); |
| 37 |
|
| 38 |
$singleOptions = array_map( function( $postType ){ |
| 39 |
return [ |
| 40 |
/* translators: singular name of post type */ |
| 41 |
'label' => sprintf( __( '%s Single Page', 'depicter' ), $postType->labels->singular_name ), |
| 42 |
'value' => $postType->name |
| 43 |
]; |
| 44 |
}, array_values( $this->availablePostTypes ) ); |
| 45 |
|
| 46 |
return Arr::merge( $options, [ 'options' => $singleOptions ]); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @inheritdoc |
| 51 |
*/ |
| 52 |
public function check( $value = null ): bool{ |
| 53 |
global $post; |
| 54 |
|
| 55 |
if ( is_null( $post ) ) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
$value = $value ?? $this->value; |
| 60 |
|
| 61 |
$isIncluded = ! empty ( $value ) ? is_singular( $value ) : is_singular(); |
| 62 |
|
| 63 |
return $this->selectionMode === 'include' ? $isIncluded : ! $isIncluded; |
| 64 |
} |
| 65 |
} |
| 66 |
|