| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Shortcode If Helper |
| 5 |
* @copyright Copyright 2017 https://e2pdf.com |
| 6 |
* @license GPLv3 |
| 7 |
* @version 1 |
| 8 |
* @link https://e2pdf.com |
| 9 |
* @since 1.22.00 |
| 10 |
*/ |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
die('Access denied.'); |
| 13 |
} |
| 14 |
|
| 15 |
class Helper_E2pdf_If { |
| 16 |
|
| 17 |
private $helper; |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 21 |
} |
| 22 |
|
| 23 |
public function get_delimiters() { |
| 24 |
return array( |
| 25 |
'[&&]', |
| 26 |
'[||]', |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
public function get_brackets() { |
| 31 |
return array( |
| 32 |
'[(]', |
| 33 |
'[)]', |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
public function get_comparators() { |
| 38 |
return array( |
| 39 |
'[==]', |
| 40 |
'[===]', |
| 41 |
'[!=]', |
| 42 |
'[!==]', |
| 43 |
'[>]', |
| 44 |
'[>=]', |
| 45 |
'[<]', |
| 46 |
'[<=]', |
| 47 |
'[contains]', |
| 48 |
'[not_contains]', |
| 49 |
'[is_array]', |
| 50 |
'[in_array]', |
| 51 |
'[not_in_array]', |
| 52 |
'[in_list]', |
| 53 |
'[not_in_list]', |
| 54 |
'[array_key_exists]', |
| 55 |
'[array_key_not_exists]', |
| 56 |
'[isset]', |
| 57 |
'[not_isset]', |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
public function get_shortcodes() { |
| 62 |
return array_merge($this->get_delimiters(), $this->get_brackets(), $this->get_comparators()); |
| 63 |
} |
| 64 |
|
| 65 |
public function do_shortcode($atts = array(), $value = '', $if = 0, $extension = null) { |
| 66 |
$if_index = $if ? '-' . $if : ''; |
| 67 |
$tags = array( |
| 68 |
'e2pdf-if-condition' => 'e2pdf-if-condition' . $if_index, |
| 69 |
'e2pdf-if-do' => 'e2pdf-if-do' . $if_index, |
| 70 |
'e2pdf-if-else' => 'e2pdf-if-else' . $if_index, |
| 71 |
); |
| 72 |
/* Backward compatibility */ |
| 73 |
if (false === strpos($value, '[e2pdf-if-condition')) { |
| 74 |
$tags = array( |
| 75 |
'e2pdf-if-condition' => 'e2pdf-condition' . $if_index, |
| 76 |
'e2pdf-if-do' => 'e2pdf-do' . $if_index, |
| 77 |
'e2pdf-if-else' => 'e2pdf-else' . $if_index, |
| 78 |
); |
| 79 |
} |
| 80 |
$comparators = $this->get_shortcodes(); |
| 81 |
$sub_values = preg_split('/(\\' . str_replace(array('||', '(', ')'), array('\|\|', '\(', '\)'), implode('|\\', $comparators)) . ')/', $this->helper->load('shortcode')->get_shortcode_content($tags['e2pdf-if-condition'], $value), -1, PREG_SPLIT_DELIM_CAPTURE); |
| 82 |
$condition = ''; |
| 83 |
foreach ($sub_values as $sub_value) { |
| 84 |
if ($sub_value && in_array($sub_value, $comparators)) { |
| 85 |
$condition .= $sub_value; |
| 86 |
} elseif ($extension && method_exists($extension, 'render')) { |
| 87 |
$condition .= $extension->render($sub_value, array(), false); |
| 88 |
} |
| 89 |
} |
| 90 |
$result = $this->apply($condition, false, $extension); |
| 91 |
if ($result) { |
| 92 |
$response = $this->helper->load('shortcode')->get_shortcode_content($tags['e2pdf-if-do'], $value); |
| 93 |
} else { |
| 94 |
$response = $this->helper->load('shortcode')->get_shortcode_content($tags['e2pdf-if-else'], $value); |
| 95 |
} |
| 96 |
if ($response && false !== strpos($response, '[e2pdf-if-' . $if + 1)) { |
| 97 |
$shortcode_tags = array( |
| 98 |
'e2pdf-if-' . $if + 1, |
| 99 |
); |
| 100 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $response, $matches); |
| 101 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 102 |
if (!empty($tagnames)) { |
| 103 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $response, $shortcodes); |
| 104 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 105 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 106 |
$response = str_replace($shortcode_value, $this->do_shortcode(shortcode_parse_atts($shortcode[3]), $shortcode[5], $if + 1, $extension), $response); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
return $response; |
| 111 |
} |
| 112 |
|
| 113 |
public function apply($condition, $nested = false, $extension = null) { |
| 114 |
while (strpos($condition, '[(]') !== false && strpos($condition, '[)]') !== false) { |
| 115 |
preg_match_all('/\[\(\](?:(?!\[\(\]|\[\)\]).)+\[\)\]/', $condition, $matches); |
| 116 |
if (!empty($matches[0])) { |
| 117 |
foreach ($matches[0] as $match) { |
| 118 |
$condition = str_replace($match, $this->apply(substr($match, 3, -3), true), $condition, $extension); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
$delimiters = $this->get_delimiters(); |
| 123 |
$sub_conditions = preg_split('/(\\' . str_replace('||', '\|\|', implode('|\\', $delimiters)) . ')/', $condition, -1, PREG_SPLIT_DELIM_CAPTURE); |
| 124 |
$final_result = false; |
| 125 |
$delimiter = false; |
| 126 |
foreach ($sub_conditions as $sub_condition) { |
| 127 |
$result = false; |
| 128 |
if ($sub_condition && in_array($sub_condition, $delimiters)) { |
| 129 |
$delimiter = $sub_condition; |
| 130 |
} else { |
| 131 |
$expression = false; |
| 132 |
foreach ($this->get_comparators() as $comparator) { |
| 133 |
if (false !== strpos($sub_condition, $comparator)) { |
| 134 |
$expression = $comparator; |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
$if = ''; |
| 139 |
$value = ''; |
| 140 |
if ($expression) { |
| 141 |
$comparision = explode($expression, $sub_condition); |
| 142 |
if (isset($comparision[0])) { |
| 143 |
$if = $comparision[0]; |
| 144 |
} |
| 145 |
if (isset($comparision[1])) { |
| 146 |
$value = $comparision[1]; |
| 147 |
} |
| 148 |
} else { |
| 149 |
$if = $sub_condition; |
| 150 |
} |
| 151 |
$result = false; |
| 152 |
switch ($expression) { |
| 153 |
case '[==]': |
| 154 |
$result = $if == $value ? true : false; |
| 155 |
break; |
| 156 |
case '[===]': |
| 157 |
$result = $if === $value ? true : false; |
| 158 |
break; |
| 159 |
case '[!=]': |
| 160 |
$result = $if != $value ? true : false; |
| 161 |
break; |
| 162 |
case '[!==]': |
| 163 |
$result = $if !== $value ? true : false; |
| 164 |
break; |
| 165 |
case '[>]': |
| 166 |
$result = $if > $value ? true : false; |
| 167 |
break; |
| 168 |
case '[>=]': |
| 169 |
$result = $if >= $value ? true : false; |
| 170 |
break; |
| 171 |
case '[<]': |
| 172 |
$result = $if < $value ? true : false; |
| 173 |
break; |
| 174 |
case '[<=]': |
| 175 |
$result = $if <= $value ? true : false; |
| 176 |
break; |
| 177 |
case '[contains]': |
| 178 |
if (empty($value) && empty($if)) { |
| 179 |
$result = true; |
| 180 |
} else { |
| 181 |
$result = !empty($value) && strpos($if, $value) !== false ? true : false; |
| 182 |
} |
| 183 |
break; |
| 184 |
case '[not_contains]': |
| 185 |
if (empty($value) && empty($if)) { |
| 186 |
$result = false; |
| 187 |
} elseif (empty($value) && !empty($if)) { |
| 188 |
$result = true; |
| 189 |
} else { |
| 190 |
$result = !empty($value) && strpos($if, $value) === false ? true : false; |
| 191 |
} |
| 192 |
break; |
| 193 |
case '[is_array]': |
| 194 |
$unserialized = false; |
| 195 |
if (is_serialized($value)) { |
| 196 |
$unserialized = $this->helper->load('convert')->unserialize($value); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 197 |
} |
| 198 |
$result = is_array($unserialized) ? true : false; |
| 199 |
break; |
| 200 |
case '[in_array]': |
| 201 |
$unserialized = false; |
| 202 |
if (is_serialized($value)) { |
| 203 |
$unserialized = $this->helper->load('convert')->unserialize($value); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 204 |
} |
| 205 |
$result = is_array($unserialized) && in_array($if, $unserialized) ? true : false; |
| 206 |
break; |
| 207 |
case '[not_in_array]': |
| 208 |
$unserialized = false; |
| 209 |
if (is_serialized($value)) { |
| 210 |
$unserialized = $this->helper->load('convert')->unserialize($value); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 211 |
} |
| 212 |
$result = !is_array($unserialized) || (is_array($unserialized) && !in_array($if, $unserialized)) ? true : false; |
| 213 |
break; |
| 214 |
case '[in_list]': |
| 215 |
$list_if = array_map('trim', explode(',', $if)); |
| 216 |
$list_value = array_map('trim', explode(',', $value)); |
| 217 |
$result = !array_diff($list_if, $list_value) ? true : false; |
| 218 |
break; |
| 219 |
case '[not_in_list]': |
| 220 |
$list_if = array_map('trim', explode(',', $if)); |
| 221 |
$list_value = array_map('trim', explode(',', $value)); |
| 222 |
$result = !array_diff($list_if, $list_value) ? false : true; |
| 223 |
break; |
| 224 |
case '[array_key_exists]': |
| 225 |
$unserialized = false; |
| 226 |
if (is_serialized($value)) { |
| 227 |
$unserialized = $this->helper->load('convert')->unserialize($value); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 228 |
} |
| 229 |
$result = is_array($unserialized) && array_key_exists($if, $unserialized) ? true : false; |
| 230 |
break; |
| 231 |
case '[array_key_not_exists]': |
| 232 |
$unserialized = false; |
| 233 |
if (is_serialized($value)) { |
| 234 |
$unserialized = $this->helper->load('convert')->unserialize($value); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 235 |
} |
| 236 |
$result = !is_array($unserialized) || (is_array($unserialized) && !array_key_exists($if, $unserialized)) ? true : false; |
| 237 |
break; |
| 238 |
case '[isset]': |
| 239 |
$unserialized = false; |
| 240 |
if (is_serialized($value)) { |
| 241 |
$unserialized = $this->helper->load('convert')->unserialize($value); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 242 |
} |
| 243 |
$result = is_array($unserialized) && isset($unserialized[$if]) ? true : false; |
| 244 |
break; |
| 245 |
case '[not_isset]': |
| 246 |
$unserialized = false; |
| 247 |
if (is_serialized($value)) { |
| 248 |
$unserialized = $this->helper->load('convert')->unserialize($value); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 249 |
} |
| 250 |
$result = !is_array($unserialized) || (is_array($unserialized) && !isset($unserialized[$if])) ? true : false; |
| 251 |
break; |
| 252 |
default: |
| 253 |
$result = $if ? true : false; |
| 254 |
break; |
| 255 |
} |
| 256 |
if ($delimiter == '[||]') { |
| 257 |
$final_result = $final_result || $result ? true : false; |
| 258 |
} elseif ($delimiter == '[&&]') { |
| 259 |
$final_result = $final_result && $result ? true : false; |
| 260 |
} else { |
| 261 |
$final_result = $result; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
if ($nested) { |
| 266 |
return $final_result ? '1[==]1' : '1[==]0'; |
| 267 |
} else { |
| 268 |
return $final_result; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|