| 1 |
<?php |
| 2 |
|
| 3 |
class SGPMCondition |
| 4 |
{ |
| 5 |
public $conditions; |
| 6 |
|
| 7 |
public function __construct($targetData) |
| 8 |
{ |
| 9 |
if (!empty($targetData)) { |
| 10 |
$this->setConditions($targetData); |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
public function setConditions($conditions) |
| 15 |
{ |
| 16 |
$this->conditions = $conditions; |
| 17 |
} |
| 18 |
|
| 19 |
public function getConditions() |
| 20 |
{ |
| 21 |
return $this->conditions; |
| 22 |
} |
| 23 |
|
| 24 |
public function render() |
| 25 |
{ |
| 26 |
$conditions = $this->getConditions(); |
| 27 |
$view = ''; |
| 28 |
end($conditions); |
| 29 |
$lastRuleId = key($conditions); |
| 30 |
foreach ($conditions as $ruleId => $condition) { |
| 31 |
if (isset($condition['condition_type'])) continue; |
| 32 |
|
| 33 |
$separator = ''; |
| 34 |
$view .= self::createConditionRuleRow($condition, $ruleId, $lastRuleId); |
| 35 |
} |
| 36 |
|
| 37 |
return $view; |
| 38 |
} |
| 39 |
|
| 40 |
public static function createConditionRuleRow($condition, $ruleId, $lastRuleId = 0) |
| 41 |
{ |
| 42 |
ob_start(); |
| 43 |
?> |
| 44 |
<div class="sgpm-target-rule sgpm-target-rule-<?php echo esc_attr($ruleId)?>" data-rule-id="<?php echo esc_attr($ruleId)?>"> |
| 45 |
<?php foreach($condition as $conditionName => $conditionValue): ?> |
| 46 |
<?php |
| 47 |
$idHiddenDiv = $conditionName; |
| 48 |
$showRowStatusClass = ''; |
| 49 |
$hideStatus = self::getParamRowHideStatus($condition, $conditionName); |
| 50 |
$showRowStatusClass = ($hideStatus) ? 'sgpm-hide-condition-row' : $showRowStatusClass; |
| 51 |
?> |
| 52 |
<div data-condition-name="<?php echo esc_attr($conditionName);?>" class="<?php echo 'sgpm-condition-'.esc_attr($conditionName).'-wrapper'.' '.esc_attr($showRowStatusClass); ?>"> |
| 53 |
<div class="sgpm-condition-<?php echo esc_attr($conditionName)?>-content"> |
| 54 |
<?php echo wp_kses(self::createConditionElement($condition, esc_attr($conditionName), esc_attr($ruleId)), array( |
| 55 |
'select' => array('class' => [], 'name' => [], 'data-select-type'=>[], 'autocomplete'=>[], 'multiple' => [], 'isnotposttype' => [], 'data-value-param'=>[]), |
| 56 |
'optgroup' => array('label' => [], 'value' => [], 'selected' => []), // Corrected the handling of optgroup attributes |
| 57 |
'option' => array('label' => [], 'value' => [], 'selected' => []) // Corrected the handling of optgroup attributes |
| 58 |
));?> |
| 59 |
</div> |
| 60 |
</div> |
| 61 |
<?php endforeach; ?> |
| 62 |
<?php echo wp_kses(self::createConditionOperators($condition, esc_attr($idHiddenDiv), esc_attr($lastRuleId), esc_attr($ruleId)), array( |
| 63 |
'div' => array('class' => [], 'data-condition-name' => [], 'data-rule-id'=>[]), |
| 64 |
'i' => array('class' => []), |
| 65 |
'ul' => array('class' => []), |
| 66 |
'li' => array('class' => [], 'tilte' =>[]), |
| 67 |
'a' => array('class' => [], 'href' => [], 'data-id'=>[]), |
| 68 |
'span' => array('class' => [], 'role' => [], 'aria-autocomplete'=>[], 'aria-haspopup'=>[], 'aria-expanded'=>[], 'tabindex'=>[]), |
| 69 |
)); ?> |
| 70 |
</div> |
| 71 |
<?php |
| 72 |
$targetOptionRow = ob_get_contents(); |
| 73 |
ob_end_clean(); |
| 74 |
return $targetOptionRow; |
| 75 |
} |
| 76 |
|
| 77 |
public static function createConditionOperators($condition, $idHiddenDiv, $lastRuleId, $ruleId) |
| 78 |
{ |
| 79 |
global $SGPM_DATA_CONFIG_ARRAY; |
| 80 |
$operatorsHtml = ''; |
| 81 |
$conditionData = $SGPM_DATA_CONFIG_ARRAY['displayTarget']; |
| 82 |
$operatorsData = $conditionData['operators']; |
| 83 |
|
| 84 |
if (empty($operatorsData)) { |
| 85 |
return $operatorsHtml; |
| 86 |
} |
| 87 |
|
| 88 |
foreach ($operatorsData as $operator) { |
| 89 |
$identificatorClass = ''; |
| 90 |
$style = ''; |
| 91 |
|
| 92 |
if ($operator['operator'] == 'edit') { |
| 93 |
$identificatorClass = $idHiddenDiv; |
| 94 |
} |
| 95 |
if ($operator['operator'] == 'add') { |
| 96 |
$style = ''; |
| 97 |
/*Don't show add button if it's not for last element*/ |
| 98 |
if ($ruleId < $lastRuleId) { |
| 99 |
$style = 'style="display: none;"'; |
| 100 |
} |
| 101 |
} |
| 102 |
$operatorsHtml .= '<div class="sgpm-rules-'.esc_attr($operator['operator']).'-button-wrapper" '.esc_attr($style).'> |
| 103 |
<div class="sgpm-display-inline-grid"> |
| 104 |
<span></span> |
| 105 |
<a href="#" class="sgpm-margin-top-7 sgpm-rules-action-button sgpm-rules-'.esc_attr($operator['operator']).'-rule" data-id="'.esc_attr($identificatorClass).'"> |
| 106 |
'.wp_kses_post($operator['name']).' |
| 107 |
</a> |
| 108 |
</div> |
| 109 |
</div>'; |
| 110 |
} |
| 111 |
|
| 112 |
return $operatorsHtml; |
| 113 |
} |
| 114 |
|
| 115 |
public static function createConditionElement($condition, $ruleName, $ruleId) |
| 116 |
{ |
| 117 |
global $SGPM_DATA_CONFIG_ARRAY; |
| 118 |
$ruleElementData = array(); |
| 119 |
$savedParam = ''; |
| 120 |
$conditionConfig = $SGPM_DATA_CONFIG_ARRAY['displayTarget']; |
| 121 |
$rulesType = $conditionConfig['columnTypes']; |
| 122 |
$paramsData = $conditionConfig['paramsData']; |
| 123 |
$attrs = $conditionConfig['attrs']; |
| 124 |
|
| 125 |
if (!empty($condition[$ruleName])) { |
| 126 |
$savedParam = $condition[$ruleName]; |
| 127 |
} |
| 128 |
else if (!empty($condition['hiddenOption'])) { |
| 129 |
$savedParam = @$condition['hiddenOption'][$ruleName]; |
| 130 |
} |
| 131 |
|
| 132 |
$ruleElementData['ruleName'] = $ruleName; |
| 133 |
if ($ruleName == 'value') { |
| 134 |
$ruleName = $condition['param']; |
| 135 |
} |
| 136 |
|
| 137 |
$type = isset($rulesType[$ruleName]) ? $rulesType[$ruleName] : ""; |
| 138 |
$data = isset($paramsData[$ruleName]) ? $paramsData[$ruleName] : ""; |
| 139 |
$attr = isset($attrs[$ruleName]) ? $attrs[$ruleName] : ""; |
| 140 |
|
| 141 |
$ruleElementData['ruleId'] = $ruleId; |
| 142 |
$ruleElementData['type'] = $type; |
| 143 |
$ruleElementData['data'] = $data; |
| 144 |
$ruleElementData['saved'] = $savedParam; |
| 145 |
$ruleElementData['attr'] = $attr; |
| 146 |
$ruleElementData['condition'] = $condition; |
| 147 |
|
| 148 |
return self::createRuleField($ruleElementData); |
| 149 |
} |
| 150 |
|
| 151 |
public static function createRuleField($ruleElementData) |
| 152 |
{ |
| 153 |
$attr = array(); |
| 154 |
$type = $ruleElementData['type']; |
| 155 |
$condition = $ruleElementData['condition']; |
| 156 |
echo wp_kses(self::createElementHeader($ruleElementData), array( |
| 157 |
'div' => array('class' => [], 'data-condition-name' => [], 'data-rule-id'=>[]), |
| 158 |
'i' => array('class' => []), |
| 159 |
'a' => array('class' => [], 'href' => [], 'data-id'=>[]), |
| 160 |
'span' => array('class' => [], 'role' => [], 'aria-autocomplete'=>[], 'aria-haspopup'=>[], 'aria-expanded'=>[], 'tabindex'=>[]), |
| 161 |
)); |
| 162 |
$name = 'sgpm-display-target['.$ruleElementData['ruleId'].']['.$ruleElementData['ruleName'].']'; |
| 163 |
$attr['name'] = $name; |
| 164 |
|
| 165 |
if (is_array($ruleElementData['attr'])) { |
| 166 |
$attr += $ruleElementData['attr']; |
| 167 |
} |
| 168 |
$rowField = ''; |
| 169 |
|
| 170 |
switch ($type) { |
| 171 |
case 'select': |
| 172 |
if (!empty($attr['multiple'])) { |
| 173 |
$attr['name'] .= '[]'; |
| 174 |
} |
| 175 |
$savedData = $ruleElementData['saved']; |
| 176 |
|
| 177 |
if (empty($ruleElementData['data'])) { |
| 178 |
$ruleElementData['data'] = $ruleElementData['saved']; |
| 179 |
$savedData = array(); |
| 180 |
|
| 181 |
if (!empty($ruleElementData['saved'])) { |
| 182 |
$savedData = array_keys($ruleElementData['saved']); |
| 183 |
} |
| 184 |
} |
| 185 |
$rowField .= SGPMHelper::createSelectBox($ruleElementData['data'], $savedData, $attr); |
| 186 |
break; |
| 187 |
case 'text': |
| 188 |
$attr['type'] = $type; |
| 189 |
$rowField .= SGPMHelper::createInput($ruleElementData['data'], $ruleElementData['saved'], $attr); |
| 190 |
break; |
| 191 |
case 'checkbox': |
| 192 |
$attr['type'] = $type; |
| 193 |
$rowField .= SGPMHelper::createCheckBox($ruleElementData['data'], $ruleElementData['saved'], $attr); |
| 194 |
break; |
| 195 |
} |
| 196 |
|
| 197 |
return $rowField; |
| 198 |
} |
| 199 |
|
| 200 |
public static function createElementHeader($ruleElementData) |
| 201 |
{ |
| 202 |
global $SGPM_DATA_CONFIG_ARRAY; |
| 203 |
$condition = $ruleElementData['condition']; |
| 204 |
$conditionName = 'displayTarget'; |
| 205 |
$ruleName = $ruleElementData['ruleName']; |
| 206 |
$elementHeader = $ruleName; |
| 207 |
/* |
| 208 |
* |
| 209 |
* Check here labels |
| 210 |
* |
| 211 |
* */ |
| 212 |
if ($ruleName == 'param') { |
| 213 |
$elementHeader = __('Select Target', 'sgpmPopupMaker'); |
| 214 |
} |
| 215 |
else if ($ruleName == 'value') { |
| 216 |
|
| 217 |
$paramName = explode("_", $condition['param']); |
| 218 |
$paramName = $paramName[0]; |
| 219 |
if (!isset($SGPM_DATA_CONFIG_ARRAY['displayTarget']['paramsData']['param'][$paramName][$condition['param']])) { |
| 220 |
$paramName = ucfirst($paramName); |
| 221 |
} |
| 222 |
$elementHeader = @$SGPM_DATA_CONFIG_ARRAY['displayTarget']['paramsData']['param'][$paramName][$condition['param']]; |
| 223 |
} |
| 224 |
else if ($ruleName == 'operator') { |
| 225 |
$elementHeader = ucfirst($ruleName); |
| 226 |
} |
| 227 |
|
| 228 |
return '<span class="sgpm-margin-bottom-5">'.esc_html($elementHeader).'</span>'; |
| 229 |
} |
| 230 |
|
| 231 |
private static function getParamRowHideStatus($condition, $ruleName) |
| 232 |
{ |
| 233 |
global $SGPM_DATA_CONFIG_ARRAY; |
| 234 |
|
| 235 |
if ($ruleName == 'hiddenOption') { |
| 236 |
return ''; |
| 237 |
} |
| 238 |
$status = false; |
| 239 |
$conditionName = $ruleName; |
| 240 |
$conditionConfig = $SGPM_DATA_CONFIG_ARRAY['displayTarget']; |
| 241 |
$paramsData = $conditionConfig['paramsData']; |
| 242 |
$ruleElementData['ruleName'] = $ruleName; |
| 243 |
|
| 244 |
if ($ruleName == 'value') { |
| 245 |
$ruleName = $condition['param']; |
| 246 |
} |
| 247 |
|
| 248 |
if (is_null($paramsData[$ruleName])) { |
| 249 |
$status = true; |
| 250 |
} |
| 251 |
|
| 252 |
return $status; |
| 253 |
} |
| 254 |
} |
| 255 |
|