@@ -22,148 +22,148 @@ |
||
| 22 | 22 | class ContextChecker |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * A unique string used to identify where this ContextChecker is being employed |
|
| 27 | - * Is currently only used within the hook name for the filterable return value of isAllowed(). |
|
| 28 | - * |
|
| 29 | - * @var string $identifier |
|
| 30 | - */ |
|
| 31 | - private $identifier; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * A list of values to be compared against the slug of the Context class passed to isAllowed() |
|
| 35 | - * |
|
| 36 | - * @var array $acceptable_values |
|
| 37 | - */ |
|
| 38 | - private $acceptable_values; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Closure that will be called to perform the evaluation within isAllowed(). |
|
| 42 | - * If none is provided, then a simple type sensitive in_array() check will be used |
|
| 43 | - * and return true if the incoming Context::slug() is found within the array of $acceptable_values. |
|
| 44 | - * |
|
| 45 | - * @var Closure $evaluation_callback |
|
| 46 | - */ |
|
| 47 | - private $evaluation_callback; |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * ContextChecker constructor. |
|
| 52 | - * |
|
| 53 | - * @param string $identifier |
|
| 54 | - * @param array $acceptable_values |
|
| 55 | - * @param Closure|null $evaluation_callback [optional] |
|
| 56 | - */ |
|
| 57 | - public function __construct($identifier, array $acceptable_values, Closure $evaluation_callback = null) |
|
| 58 | - { |
|
| 59 | - $this->setIdentifier($identifier); |
|
| 60 | - $this->setAcceptableValues($acceptable_values); |
|
| 61 | - $this->setEvaluationCallback($evaluation_callback); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param string $identifier |
|
| 67 | - */ |
|
| 68 | - private function setIdentifier($identifier) |
|
| 69 | - { |
|
| 70 | - $this->identifier = sanitize_key($identifier); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param array $acceptable_values |
|
| 76 | - */ |
|
| 77 | - private function setAcceptableValues(array $acceptable_values) |
|
| 78 | - { |
|
| 79 | - $this->acceptable_values = $acceptable_values; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @param Closure $evaluation_callback |
|
| 85 | - */ |
|
| 86 | - private function setEvaluationCallback(Closure $evaluation_callback = null) |
|
| 87 | - { |
|
| 88 | - $this->evaluation_callback = $evaluation_callback instanceof Closure |
|
| 89 | - ? $evaluation_callback |
|
| 90 | - : function (Context $context, $acceptable_values) { |
|
| 91 | - return in_array($context->slug(), $acceptable_values, true); |
|
| 92 | - }; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @return string |
|
| 98 | - */ |
|
| 99 | - protected function identifier() |
|
| 100 | - { |
|
| 101 | - return $this->identifier; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @return array |
|
| 107 | - */ |
|
| 108 | - protected function acceptableValues() |
|
| 109 | - { |
|
| 110 | - return apply_filters( |
|
| 111 | - "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__{$this->identifier}__acceptableValues", |
|
| 112 | - $this->acceptable_values |
|
| 113 | - ); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @return Closure |
|
| 119 | - */ |
|
| 120 | - protected function evaluationCallback() |
|
| 121 | - { |
|
| 122 | - return $this->evaluation_callback; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Returns true if the incoming Context class slug matches one of the preset acceptable values. |
|
| 129 | - * The result is filterable using the identifier for this ContextChecker. |
|
| 130 | - * example: |
|
| 131 | - * If this ContextChecker's $identifier was set to "registration-checkout-type", |
|
| 132 | - * then the filter here would be named: |
|
| 133 | - * "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed". |
|
| 134 | - * Other code could hook into the filter in isAllowed() using the above name |
|
| 135 | - * and test for additional acceptable values. |
|
| 136 | - * So if the set of $acceptable_values was: [ "initial-visit", "revisit" ] |
|
| 137 | - * then adding a filter to |
|
| 138 | - * "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed", |
|
| 139 | - * would allow you to perform your own conditional and allow "wait-list-checkout" as an acceptable value. |
|
| 140 | - * example: |
|
| 141 | - * add_filter( |
|
| 142 | - * 'FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed', |
|
| 143 | - * function ($is_allowed, Context $context) { |
|
| 144 | - * return $context->slug() === 'wait-list-checkout' |
|
| 145 | - * ? true |
|
| 146 | - * : $is_allowed; |
|
| 147 | - * }, |
|
| 148 | - * 10, |
|
| 149 | - * 2 |
|
| 150 | - * ); |
|
| 151 | - * |
|
| 152 | - * @param Context $context |
|
| 153 | - * @return boolean |
|
| 154 | - */ |
|
| 155 | - public function isAllowed(Context $context) |
|
| 156 | - { |
|
| 157 | - $evaluation_callback = $this->evaluationCallback(); |
|
| 158 | - return filter_var( |
|
| 159 | - apply_filters( |
|
| 160 | - "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__{$this->identifier}__isAllowed", |
|
| 161 | - $evaluation_callback($context, $this->acceptableValues()), |
|
| 162 | - $context, |
|
| 163 | - $this |
|
| 164 | - ), |
|
| 165 | - FILTER_VALIDATE_BOOLEAN |
|
| 166 | - ); |
|
| 167 | - } |
|
| 25 | + /** |
|
| 26 | + * A unique string used to identify where this ContextChecker is being employed |
|
| 27 | + * Is currently only used within the hook name for the filterable return value of isAllowed(). |
|
| 28 | + * |
|
| 29 | + * @var string $identifier |
|
| 30 | + */ |
|
| 31 | + private $identifier; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * A list of values to be compared against the slug of the Context class passed to isAllowed() |
|
| 35 | + * |
|
| 36 | + * @var array $acceptable_values |
|
| 37 | + */ |
|
| 38 | + private $acceptable_values; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Closure that will be called to perform the evaluation within isAllowed(). |
|
| 42 | + * If none is provided, then a simple type sensitive in_array() check will be used |
|
| 43 | + * and return true if the incoming Context::slug() is found within the array of $acceptable_values. |
|
| 44 | + * |
|
| 45 | + * @var Closure $evaluation_callback |
|
| 46 | + */ |
|
| 47 | + private $evaluation_callback; |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * ContextChecker constructor. |
|
| 52 | + * |
|
| 53 | + * @param string $identifier |
|
| 54 | + * @param array $acceptable_values |
|
| 55 | + * @param Closure|null $evaluation_callback [optional] |
|
| 56 | + */ |
|
| 57 | + public function __construct($identifier, array $acceptable_values, Closure $evaluation_callback = null) |
|
| 58 | + { |
|
| 59 | + $this->setIdentifier($identifier); |
|
| 60 | + $this->setAcceptableValues($acceptable_values); |
|
| 61 | + $this->setEvaluationCallback($evaluation_callback); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param string $identifier |
|
| 67 | + */ |
|
| 68 | + private function setIdentifier($identifier) |
|
| 69 | + { |
|
| 70 | + $this->identifier = sanitize_key($identifier); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param array $acceptable_values |
|
| 76 | + */ |
|
| 77 | + private function setAcceptableValues(array $acceptable_values) |
|
| 78 | + { |
|
| 79 | + $this->acceptable_values = $acceptable_values; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @param Closure $evaluation_callback |
|
| 85 | + */ |
|
| 86 | + private function setEvaluationCallback(Closure $evaluation_callback = null) |
|
| 87 | + { |
|
| 88 | + $this->evaluation_callback = $evaluation_callback instanceof Closure |
|
| 89 | + ? $evaluation_callback |
|
| 90 | + : function (Context $context, $acceptable_values) { |
|
| 91 | + return in_array($context->slug(), $acceptable_values, true); |
|
| 92 | + }; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @return string |
|
| 98 | + */ |
|
| 99 | + protected function identifier() |
|
| 100 | + { |
|
| 101 | + return $this->identifier; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @return array |
|
| 107 | + */ |
|
| 108 | + protected function acceptableValues() |
|
| 109 | + { |
|
| 110 | + return apply_filters( |
|
| 111 | + "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__{$this->identifier}__acceptableValues", |
|
| 112 | + $this->acceptable_values |
|
| 113 | + ); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @return Closure |
|
| 119 | + */ |
|
| 120 | + protected function evaluationCallback() |
|
| 121 | + { |
|
| 122 | + return $this->evaluation_callback; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Returns true if the incoming Context class slug matches one of the preset acceptable values. |
|
| 129 | + * The result is filterable using the identifier for this ContextChecker. |
|
| 130 | + * example: |
|
| 131 | + * If this ContextChecker's $identifier was set to "registration-checkout-type", |
|
| 132 | + * then the filter here would be named: |
|
| 133 | + * "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed". |
|
| 134 | + * Other code could hook into the filter in isAllowed() using the above name |
|
| 135 | + * and test for additional acceptable values. |
|
| 136 | + * So if the set of $acceptable_values was: [ "initial-visit", "revisit" ] |
|
| 137 | + * then adding a filter to |
|
| 138 | + * "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed", |
|
| 139 | + * would allow you to perform your own conditional and allow "wait-list-checkout" as an acceptable value. |
|
| 140 | + * example: |
|
| 141 | + * add_filter( |
|
| 142 | + * 'FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed', |
|
| 143 | + * function ($is_allowed, Context $context) { |
|
| 144 | + * return $context->slug() === 'wait-list-checkout' |
|
| 145 | + * ? true |
|
| 146 | + * : $is_allowed; |
|
| 147 | + * }, |
|
| 148 | + * 10, |
|
| 149 | + * 2 |
|
| 150 | + * ); |
|
| 151 | + * |
|
| 152 | + * @param Context $context |
|
| 153 | + * @return boolean |
|
| 154 | + */ |
|
| 155 | + public function isAllowed(Context $context) |
|
| 156 | + { |
|
| 157 | + $evaluation_callback = $this->evaluationCallback(); |
|
| 158 | + return filter_var( |
|
| 159 | + apply_filters( |
|
| 160 | + "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__{$this->identifier}__isAllowed", |
|
| 161 | + $evaluation_callback($context, $this->acceptableValues()), |
|
| 162 | + $context, |
|
| 163 | + $this |
|
| 164 | + ), |
|
| 165 | + FILTER_VALIDATE_BOOLEAN |
|
| 166 | + ); |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | 169 | } |