| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Rules\Condition\WordPress; |
| 4 |
|
| 5 |
use Depicter\Rules\Condition\Base as ConditionBase; |
| 6 |
|
| 7 |
class Post extends ConditionBase { |
| 8 |
|
| 9 |
/** |
| 10 |
* @inheritdoc |
| 11 |
*/ |
| 12 |
public $slug = 'WordPress_Post'; |
| 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 = 'WordPress'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Tier of this condition |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $tier = 'free-user'; |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* @inheritdoc |
| 39 |
*/ |
| 40 |
public function getLabel(){ |
| 41 |
return __('A WP Post', 'depicter' ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @inheritdoc |
| 46 |
*/ |
| 47 |
public function getQueryResults(){ |
| 48 |
$posts = get_posts([ |
| 49 |
'post_type' => $this->postType, |
| 50 |
'numberposts' => $this->maxOptionsNumber, |
| 51 |
'fields' => [ 'ids', 'post_title'] |
| 52 |
]); |
| 53 |
|
| 54 |
return array_map( function( $post ){ |
| 55 |
return [ |
| 56 |
'label' => $post->post_title, |
| 57 |
'value' => $post->ID |
| 58 |
]; |
| 59 |
}, $posts ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @inheritdoc |
| 64 |
*/ |
| 65 |
public function check( $value = null ){ |
| 66 |
global $post; |
| 67 |
|
| 68 |
$value = $value ?? $this->value; |
| 69 |
|
| 70 |
// if it was not a singular WP page |
| 71 |
if ( is_null( $post ) || ! is_singular() ) { |
| 72 |
$isMet = false; |
| 73 |
} else { |
| 74 |
$isMet = ! empty( $value ) ? in_array( $post->ID, $value ) : is_singular( $this->postType ); |
| 75 |
} |
| 76 |
|
| 77 |
return $this->selectionMode === 'include' ? $isMet : !$isMet; |
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
|