| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Rules\Condition; |
| 4 |
|
| 5 |
use Averta\Core\Utility\Arr; |
| 6 |
use http\Exception; |
| 7 |
|
| 8 |
class Conditions { |
| 9 |
|
| 10 |
/** |
| 11 |
* Collection of all Condition instances |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $items = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* List of slug of all defined conditions |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
protected $registeredIDs = [ |
| 23 |
'WordPress_Post', |
| 24 |
'WordPress_Page', |
| 25 |
'WordPress_IsArchive', |
| 26 |
'WordPress_StaticPage', |
| 27 |
'WordPress_IsCategory', |
| 28 |
'WordPress_IsTag', |
| 29 |
'WordPress_InCategory', |
| 30 |
'WordPress_HasTag', |
| 31 |
'WordPress_IsAuthor', |
| 32 |
'WordPress_IsLanguage', |
| 33 |
'CPT_IsSingle', |
| 34 |
'CPT_IsArchive', |
| 35 |
'CPT_IsTax', |
| 36 |
'WooCommerce_IsShop', |
| 37 |
'WooCommerce_IsArchive', |
| 38 |
'WooCommerce_Product', |
| 39 |
'WooCommerce_InProductCategory', |
| 40 |
'WooCommerce_HasProductTag', |
| 41 |
'WooCommerce_ByAuthor', |
| 42 |
'WooCommerce_InChildProductCategory', |
| 43 |
'WooCommerce_StaticPage', |
| 44 |
'WooCommerce_Cart', |
| 45 |
'Audience_Device', |
| 46 |
'Audience_Browser', |
| 47 |
'Audience_Country', |
| 48 |
'Audience_AuthenticatedStatus', |
| 49 |
'Advanced_Cookie', |
| 50 |
'Advanced_Referrer', |
| 51 |
'Advanced_URL' |
| 52 |
]; |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* List of all defined Condition groups |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public function getGroups(){ |
| 61 |
return [ |
| 62 |
'WordPress' => [ |
| 63 |
'label' => __('WordPress', 'depicter' ), |
| 64 |
'items' => [] |
| 65 |
], |
| 66 |
'CPT' => [ |
| 67 |
'label' => __('Custom Post Types', 'depicter' ), |
| 68 |
'items' => [] |
| 69 |
], |
| 70 |
'WooCommerce'=> [ |
| 71 |
'label' => __('WooCommerce', 'depicter' ), |
| 72 |
'items' => [] |
| 73 |
], |
| 74 |
'Audience' => [ |
| 75 |
'label' => __('Audience', 'depicter' ), |
| 76 |
'items' => [] |
| 77 |
], |
| 78 |
'Advanced' => [ |
| 79 |
'label' => __('Advanced', 'depicter' ), |
| 80 |
'items' => [] |
| 81 |
] |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get dynamic CPT IDs |
| 87 |
* |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
public function getDynamicCptIDs() { |
| 91 |
$IDs = []; |
| 92 |
|
| 93 |
$postTypes = get_post_types([ |
| 94 |
'public' => true, |
| 95 |
'_builtin' => false |
| 96 |
]); |
| 97 |
if ( !empty( $postTypes['product'] ) ) { |
| 98 |
unset( $postTypes['product'] ); |
| 99 |
} |
| 100 |
|
| 101 |
foreach( $postTypes as $postType ) { |
| 102 |
$IDs[] = 'CPT_SingleType|' . $postType; |
| 103 |
|
| 104 |
$taxonomies = get_taxonomies([ |
| 105 |
'object_type' => [ $postType ], |
| 106 |
'public' => true, |
| 107 |
'show_ui' => true, |
| 108 |
], 'object'); |
| 109 |
|
| 110 |
if ( ! empty( $taxonomies ) ) { |
| 111 |
foreach( $taxonomies as $tax ) { |
| 112 |
$IDs[] = 'CPT_HasTerm|' . $tax->name; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return $IDs; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* List of slug of all defined conditions |
| 122 |
* |
| 123 |
* @return array |
| 124 |
*/ |
| 125 |
public function getIDs(){ |
| 126 |
return Arr::merge( $this->registeredIDs, $this->getDynamicCptIDs() ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get all Condition instances |
| 131 |
* |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
public function all(){ |
| 135 |
return array_values( $this->collect() ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get all properties of conditions in an array |
| 140 |
* |
| 141 |
* @param bool $inGroups Whether to return items in plain array or in groups |
| 142 |
* |
| 143 |
* @return array |
| 144 |
*/ |
| 145 |
public function toArray( $inGroups = false ){ |
| 146 |
|
| 147 |
if( $inGroups ){ |
| 148 |
$groups = $this->getGroups(); |
| 149 |
|
| 150 |
foreach( $this->collect() as $conditionSlug => $conditionInstance ){ |
| 151 |
[ $groupID, $className ] = explode('_', $conditionSlug ); |
| 152 |
|
| 153 |
if( isset( $groups[ $groupID ]['items'] ) && is_array( $groups[ $groupID ]['items'] ) ){ |
| 154 |
$groups[ $groupID ]['items'][] = $conditionInstance->getProperties(); |
| 155 |
} |
| 156 |
} |
| 157 |
return $groups; |
| 158 |
|
| 159 |
} else { |
| 160 |
return array_map( function( Base $item ){ |
| 161 |
return $item->getProperties(); |
| 162 |
}, $this->all() ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get the condition instance by slug |
| 168 |
* |
| 169 |
* @param $conditionSlug |
| 170 |
* |
| 171 |
* @return Base |
| 172 |
*/ |
| 173 |
public function find( $conditionSlug ){ |
| 174 |
$param = ''; |
| 175 |
[ $groupID, $className ] = explode('_', $conditionSlug ); |
| 176 |
if ( strpos( $className, '|' ) ) { |
| 177 |
$param = explode( '|', $conditionSlug )[1]; |
| 178 |
$className = explode( '|', $className )[0]; |
| 179 |
} |
| 180 |
|
| 181 |
$fullyQualifiedClassName = "\\Depicter\\Rules\\Condition\\{$groupID}\\{$className}"; |
| 182 |
|
| 183 |
if( class_exists( $fullyQualifiedClassName ) ){ |
| 184 |
return ! empty( $param ) ? new $fullyQualifiedClassName( $param ) : new $fullyQualifiedClassName(); |
| 185 |
} |
| 186 |
return null; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
* Collect defined Condition classes in a list |
| 192 |
* |
| 193 |
* @return array |
| 194 |
*/ |
| 195 |
protected function collect(){ |
| 196 |
if( ! empty( $this->items ) ){ |
| 197 |
return $this->items; |
| 198 |
} |
| 199 |
|
| 200 |
foreach( $this->getIDs() as $conditionSlug ){ |
| 201 |
if( $conditionInstance = $this->find( $conditionSlug ) ){ |
| 202 |
if ( $conditionInstance->isVisible() ) { |
| 203 |
$this->items[ $conditionSlug ] = $conditionInstance; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return $this->items; |
| 209 |
} |
| 210 |
} |
| 211 |
|