| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /model/e2pdf-action.php |
| 5 |
* |
| 6 |
* @package E2Pdf |
| 7 |
* @license GPLv3 |
| 8 |
* @link https://e2pdf.com |
| 9 |
*/ |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
die('Access denied.'); |
| 12 |
} |
| 13 |
|
| 14 |
class Model_E2pdf_Action extends Model_E2pdf_Model { |
| 15 |
|
| 16 |
private $extension; |
| 17 |
|
| 18 |
public function load($extension) { |
| 19 |
$this->extension = $extension; |
| 20 |
} |
| 21 |
|
| 22 |
public function process_actions($page) { |
| 23 |
if (!$page || !$this->extension) { |
| 24 |
return $page; |
| 25 |
} |
| 26 |
if (isset($page['actions']) && !empty($page['actions'])) { |
| 27 |
foreach ($page['actions'] as $action_key => $action) { |
| 28 |
$page = $this->process_action($page, $action, $action_key); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
if (isset($page['hidden']) && $page['hidden']) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf |
| 33 |
} else { |
| 34 |
if (!empty($page['elements'])) { |
| 35 |
foreach ($page['elements'] as $el_key => $el_value) { |
| 36 |
if (isset($el_value['actions']) && !empty($el_value['actions'])) { |
| 37 |
foreach ($el_value['actions'] as $action_key => $action) { |
| 38 |
$el_value = $this->process_action($el_value, $action, $action_key); |
| 39 |
} |
| 40 |
$page['elements'][$el_key] = $el_value; |
| 41 |
if ((isset($el_value['hidden']) && $el_value['hidden']) || ($el_value['page_id'] != $page['page_id'])) { |
| 42 |
unset($page['elements'][$el_key]); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
return $page; |
| 49 |
} |
| 50 |
|
| 51 |
public function process_global_actions($actions = array()) { |
| 52 |
if (!empty($actions) && $this->extension) { |
| 53 |
foreach ($actions as $action_key => $action) { |
| 54 |
if (isset($action['action'])) { |
| 55 |
if ($action['action'] !== 'redirect_access_by_url') { |
| 56 |
// Backward Compatibility |
| 57 |
if (!isset($action['property'])) { |
| 58 |
if (false !== strpos($action['action'], 'restrict_')) { |
| 59 |
$action['property'] = 'disable'; |
| 60 |
} else { |
| 61 |
$action['property'] = 'enable'; |
| 62 |
} |
| 63 |
$action['action'] = str_replace( |
| 64 |
[ |
| 65 |
'restrict_process_shortcode_', |
| 66 |
'process_shortcode_', |
| 67 |
'restrict_process_', |
| 68 |
'process_', |
| 69 |
'restrict_', |
| 70 |
], '', $action['action'] |
| 71 |
); |
| 72 |
} |
| 73 |
$action['action'] = $action['property'] . '_' . $action['action']; |
| 74 |
} |
| 75 |
} |
| 76 |
$actions[$action_key] = $this->process_action($action, $action, $action_key); |
| 77 |
} |
| 78 |
} |
| 79 |
return $actions; |
| 80 |
} |
| 81 |
|
| 82 |
public function process_page_id($page) { |
| 83 |
$elements = array(); |
| 84 |
if ($page && $this->extension) { |
| 85 |
if (!empty($page['elements'])) { |
| 86 |
foreach ($page['elements'] as $el_key => $el_value) { |
| 87 |
if (isset($el_value['actions']) && !empty($el_value['actions'])) { |
| 88 |
foreach ($el_value['actions'] as $action_key => $action) { |
| 89 |
if ($action['action'] == 'change' && isset($action['property']) && $action['property'] == 'page_id') { |
| 90 |
$el_value = $this->process_action($el_value, $action, $action_key); |
| 91 |
} |
| 92 |
} |
| 93 |
if ($el_value['page_id'] != $page['page_id']) { |
| 94 |
$elements[] = $el_value; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
return $elements; |
| 101 |
} |
| 102 |
|
| 103 |
private function process_action($element, $action, $action_key) { |
| 104 |
$apply = false; |
| 105 |
if (isset($action['conditions']) && !empty($action['conditions'])) { |
| 106 |
foreach ($action['conditions'] as $condition) { |
| 107 |
$apply = $this->process_condition($condition); |
| 108 |
if ($action['apply'] == 'any' && $apply) { |
| 109 |
break; |
| 110 |
} elseif ($action['apply'] == 'all' && !$apply) { |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
if ($apply) { |
| 116 |
$element = $this->apply_action($element, $action, $action_key); |
| 117 |
} else { |
| 118 |
$element = $this->apply_else_action($element, $action); |
| 119 |
} |
| 120 |
return $element; |
| 121 |
} |
| 122 |
|
| 123 |
private function process_condition($condition) { |
| 124 |
$result = false; |
| 125 |
$if = $this->extension->render($condition['if']); |
| 126 |
$value = $this->extension->render($condition['value']); |
| 127 |
switch ($condition['condition']) { |
| 128 |
case '=': |
| 129 |
$result = $if == $value ? true : false; |
| 130 |
break; |
| 131 |
case '!=': |
| 132 |
$result = $if != $value ? true : false; |
| 133 |
break; |
| 134 |
case '>': |
| 135 |
$result = $if > $value ? true : false; |
| 136 |
break; |
| 137 |
case '>=': |
| 138 |
$result = $if >= $value ? true : false; |
| 139 |
break; |
| 140 |
case '<': |
| 141 |
$result = $if < $value ? true : false; |
| 142 |
break; |
| 143 |
case '<=': |
| 144 |
$result = $if <= $value ? true : false; |
| 145 |
break; |
| 146 |
case 'like': |
| 147 |
if (empty($value) && empty($if)) { |
| 148 |
$result = true; |
| 149 |
} else { |
| 150 |
$result = !empty($value) && strpos($if, $value) !== false ? true : false; |
| 151 |
} |
| 152 |
break; |
| 153 |
case 'not_like': |
| 154 |
if (empty($value) && empty($if)) { |
| 155 |
$result = false; |
| 156 |
} elseif (empty($value) && !empty($if)) { |
| 157 |
$result = true; |
| 158 |
} else { |
| 159 |
$result = !empty($value) && strpos($if, $value) === false ? true : false; |
| 160 |
} |
| 161 |
break; |
| 162 |
case 'in_array': |
| 163 |
$unserialized = false; |
| 164 |
if (is_serialized($value)) { |
| 165 |
$unserialized = $this->helper->load('convert')->unserialize($value); |
| 166 |
} |
| 167 |
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
| 168 |
$result = is_array($unserialized) && in_array($if, $unserialized) ? true : false; |
| 169 |
break; |
| 170 |
case 'not_in_array': |
| 171 |
$unserialized = false; |
| 172 |
if (is_serialized($value)) { |
| 173 |
$unserialized = $this->helper->load('convert')->unserialize($value); |
| 174 |
} |
| 175 |
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
| 176 |
$result = is_array($unserialized) && in_array($if, $unserialized) ? false : true; |
| 177 |
break; |
| 178 |
case 'in_list': |
| 179 |
$list_if = array_map('trim', explode(',', $if)); |
| 180 |
$list_value = array_map('trim', explode(',', $value)); |
| 181 |
$result = !array_diff($list_if, $list_value) ? true : false; |
| 182 |
break; |
| 183 |
case 'not_in_list': |
| 184 |
$list_if = array_map('trim', explode(',', $if)); |
| 185 |
$list_value = array_map('trim', explode(',', $value)); |
| 186 |
$result = !array_diff($list_if, $list_value) ? false : true; |
| 187 |
break; |
| 188 |
case 'array_key_exists': |
| 189 |
$unserialized = false; |
| 190 |
if (is_serialized($value)) { |
| 191 |
$unserialized = $this->helper->load('convert')->unserialize($value); |
| 192 |
} |
| 193 |
$result = is_array($unserialized) && array_key_exists($if, $unserialized) ? true : false; |
| 194 |
break; |
| 195 |
case 'array_key_not_exists': |
| 196 |
$unserialized = false; |
| 197 |
if (is_serialized($value)) { |
| 198 |
$unserialized = $this->helper->load('convert')->unserialize($value); |
| 199 |
} |
| 200 |
$result = is_array($unserialized) && array_key_exists($if, $unserialized) ? false : true; |
| 201 |
break; |
| 202 |
} |
| 203 |
return $result; |
| 204 |
} |
| 205 |
|
| 206 |
private function apply_action($element, $action, $action_key) { |
| 207 |
if ($action['action'] == 'hide') { |
| 208 |
$element['hidden'] = true; |
| 209 |
} elseif ($action['action'] == 'show') { |
| 210 |
$element['hidden'] = false; |
| 211 |
} elseif ($action['action'] == 'merge' && $action['property']) { |
| 212 |
if ($action['property'] == 'value' && isset($element['element_id'])) { |
| 213 |
$value = isset($element[$action['property']]) ? $element[$action['property']] : ''; |
| 214 |
$change = $value . $action['change']; |
| 215 |
$element[$action['property']] = $change; |
| 216 |
} |
| 217 |
} elseif ($action['action'] == 'change' && $action['property']) { |
| 218 |
$number_properties = array( |
| 219 |
'width', 'height', 'left', 'top', |
| 220 |
); |
| 221 |
$not_properties = array( |
| 222 |
'top', 'left', 'width', 'height', 'value', 'page_id', |
| 223 |
); |
| 224 |
if ($action['property'] == 'value') { |
| 225 |
$action['change'] = $this->helper->load('translator')->pre_translate($action['change'], $element['template_id'], $element['element_id'], 'value_action_' . $action_key . '_change'); |
| 226 |
if (isset($action['format']) && $action['format'] != 'replace') { |
| 227 |
if ($action['format'] == 'insert_before') { |
| 228 |
$value = isset($element[$action['property']]) ? $element[$action['property']] : ''; |
| 229 |
$change = $action['change'] . $value; |
| 230 |
} elseif ($action['format'] == 'insert_after') { |
| 231 |
$value = isset($element[$action['property']]) ? $element[$action['property']] : ''; |
| 232 |
$change = $value . $action['change']; |
| 233 |
} elseif ($action['format'] == 'search') { |
| 234 |
$search = isset($action['search']) ? $action['search'] : ''; |
| 235 |
$replace = isset($action['change']) ? $action['change'] : ''; |
| 236 |
if ($search) { |
| 237 |
$value = isset($element[$action['property']]) ? $element[$action['property']] : ''; |
| 238 |
$pre_replace = isset($action['pre_replace']) ? $action['pre_replace'] : ''; |
| 239 |
if ($pre_replace == 'render') { |
| 240 |
$type = isset($element['type']) ? $element['type'] : ''; |
| 241 |
if (!isset($element['rendered'])) { |
| 242 |
if (in_array($type, array('e2pdf-html', 'e2pdf-page-number'), true)) { |
| 243 |
$value = $this->helper->load('filter')->filter_html_tags($this->extension->render($value, $element)); |
| 244 |
} else { |
| 245 |
$value = $this->extension->render($value, $element); |
| 246 |
} |
| 247 |
} |
| 248 |
if (in_array($type, array('e2pdf-html', 'e2pdf-page-number'), true)) { |
| 249 |
$replace = $this->helper->load('filter')->filter_html_tags($this->extension->render($replace, $element)); |
| 250 |
} else { |
| 251 |
$replace = $this->extension->render($replace, $element); |
| 252 |
} |
| 253 |
$element['rendered'] = true; |
| 254 |
} |
| 255 |
if ($value) { |
| 256 |
$change = str_replace($search, $replace, $value); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
} else { |
| 261 |
$change = $action['change']; |
| 262 |
} |
| 263 |
} else { |
| 264 |
$change = $this->extension->render($action['change']); |
| 265 |
} |
| 266 |
if ((substr($change, 0, 1) === '+' || substr($change, 0, 1) === '-') && in_array($action['property'], $number_properties, true)) { |
| 267 |
if (isset($element['element_id'])) { |
| 268 |
$value = isset($element[$action['property']]) ? $element[$action['property']] : 0; |
| 269 |
} else { |
| 270 |
$value = isset($element['properties'][$action['property']]) ? $element['properties'][$action['property']] : 0; |
| 271 |
} |
| 272 |
$change = $value + floatval($change); |
| 273 |
} |
| 274 |
if (in_array($action['property'], $not_properties, true) && isset($element['element_id'])) { |
| 275 |
$element[$action['property']] = $change; |
| 276 |
} else { |
| 277 |
$element['properties'][$action['property']] = $change; |
| 278 |
} |
| 279 |
} elseif (isset($element['action'])) { |
| 280 |
$element['success'] = true; |
| 281 |
} |
| 282 |
return $element; |
| 283 |
} |
| 284 |
|
| 285 |
private function apply_else_action($element, $action) { |
| 286 |
if ($action['action'] == 'show') { |
| 287 |
if (isset($action['else']) && $action['else'] == 'hide') { |
| 288 |
$element['hidden'] = true; |
| 289 |
} |
| 290 |
} |
| 291 |
return $element; |
| 292 |
} |
| 293 |
} |
| 294 |
|