| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Rules\Condition\WordPress; |
| 4 |
|
| 5 |
use Averta\Core\Utility\Arr; |
| 6 |
use Depicter\Rules\Condition\Base as ConditionBase; |
| 7 |
|
| 8 |
class StaticPage extends ConditionBase |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @inheritdoc |
| 12 |
*/ |
| 13 |
public $slug = 'WordPress_StaticPage'; |
| 14 |
|
| 15 |
/** |
| 16 |
* @inheritdoc |
| 17 |
*/ |
| 18 |
public $control = 'multiSelect'; |
| 19 |
|
| 20 |
/** |
| 21 |
* @inheritdoc |
| 22 |
*/ |
| 23 |
protected $belongsTo = 'WordPress'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Tier of this condition |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $tier = 'free-user'; |
| 31 |
|
| 32 |
/** |
| 33 |
* @inheritdoc |
| 34 |
*/ |
| 35 |
public function getLabel(): ?string{ |
| 36 |
return __('WP Static Pages', 'depicter' ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @inheritDoc |
| 41 |
*/ |
| 42 |
public function getControlOptions(){ |
| 43 |
$options = parent::getControlOptions(); |
| 44 |
|
| 45 |
return Arr::merge( $options, [ 'options' => [ |
| 46 |
[ |
| 47 |
'label' => __( 'Home Page', 'depicter' ), |
| 48 |
'value' => 'is_home' |
| 49 |
], |
| 50 |
[ |
| 51 |
'label' => __( '404 Page', 'depicter' ), |
| 52 |
'value' => 'is_404' |
| 53 |
], |
| 54 |
[ |
| 55 |
'label' => __( 'Search Page', 'depicter' ), |
| 56 |
'value' => 'is_search' |
| 57 |
], |
| 58 |
[ |
| 59 |
'label' => __( 'Blog Page', 'depicter' ), |
| 60 |
'value' => 'is_blog' |
| 61 |
], |
| 62 |
[ |
| 63 |
'label' => __( 'Privacy Policy page', 'depicter' ), |
| 64 |
'value' => 'is_privacy_policy' |
| 65 |
] |
| 66 |
]]); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @inheritdoc |
| 71 |
*/ |
| 72 |
public function check( $value = null ): bool{ |
| 73 |
$isIncluded = false; |
| 74 |
|
| 75 |
$value = $value ?? $this->value ?? $this->defaultValue; |
| 76 |
|
| 77 |
if( !empty( $value ) && is_array( $value ) ){ |
| 78 |
foreach( $value as $page ){ |
| 79 |
if( $page == 'all' ){ |
| 80 |
$isIncluded = is_home() || is_404() || is_search() || $this->is_blog() || is_privacy_policy(); |
| 81 |
} elseif( $page == 'is_home' ) { |
| 82 |
$isIncluded = is_front_page(); |
| 83 |
} elseif( $page == 'is_404' ) { |
| 84 |
$isIncluded = is_404(); |
| 85 |
} elseif( $page == 'is_search' ) { |
| 86 |
$isIncluded = is_search(); |
| 87 |
} elseif( $page == 'is_blog' ) { |
| 88 |
$isIncluded = $this->is_blog(); |
| 89 |
} elseif( $page == 'is_privacy_policy' ) { |
| 90 |
$isIncluded = is_privacy_policy(); |
| 91 |
} |
| 92 |
|
| 93 |
if( $isIncluded ){ |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
return $this->selectionMode === 'include' ? $isIncluded : ! $isIncluded; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Check if page is blog or not |
| 105 |
* |
| 106 |
* @return bool |
| 107 |
*/ |
| 108 |
public function is_blog(): bool{ |
| 109 |
return ( is_archive() || is_author() || is_category() || is_tag() || is_home() ) && 'post' == get_post_type(); |
| 110 |
} |
| 111 |
} |
| 112 |
|