| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /helper/e2pdf-shortcode.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 Helper_E2pdf_Shortcode { |
| 15 |
|
| 16 |
private $helper; |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 20 |
} |
| 21 |
|
| 22 |
public function get_shortcode_regex($tagnames = null) { |
| 23 |
if (version_compare(get_bloginfo('version'), '4.4.0', '<')) { |
| 24 |
global $shortcode_tags; |
| 25 |
|
| 26 |
if (empty($tagnames)) { |
| 27 |
$tagnames = array_keys($shortcode_tags); |
| 28 |
} |
| 29 |
$tagregexp = join('|', array_map('preg_quote', $tagnames)); |
| 30 |
|
| 31 |
return '\\[' |
| 32 |
. '(\\[?)' |
| 33 |
. "($tagregexp)" |
| 34 |
. '(?![\\w-])' |
| 35 |
. '(' |
| 36 |
. '[^\\]\\/]*' |
| 37 |
. '(?:' |
| 38 |
. '\\/(?!\\])' |
| 39 |
. '[^\\]\\/]*' |
| 40 |
. ')*?' |
| 41 |
. ')' |
| 42 |
. '(?:' |
| 43 |
. '(\\/)' |
| 44 |
. '\\]' |
| 45 |
. '|' |
| 46 |
. '\\]' |
| 47 |
. '(?:' |
| 48 |
. '(' |
| 49 |
. '[^\\[]*+' |
| 50 |
. '(?:' |
| 51 |
. '\\[(?!\\/\\2\\])' |
| 52 |
. '[^\\[]*+' |
| 53 |
. ')*+' |
| 54 |
. ')' |
| 55 |
. '\\[\\/\\2\\]' |
| 56 |
. ')?' |
| 57 |
. ')' |
| 58 |
. '(\\]?)'; |
| 59 |
} else { |
| 60 |
return get_shortcode_regex($tagnames); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public function get_shortcode($shortcodes = array(), $key = '') { |
| 65 |
$shortcode = array(); |
| 66 |
$shortcode[1] = $shortcodes[1][$key]; |
| 67 |
$shortcode[2] = $shortcodes[2][$key]; |
| 68 |
$shortcode[3] = $shortcodes[3][$key]; |
| 69 |
$shortcode[4] = $shortcodes[4][$key]; |
| 70 |
$shortcode[5] = $shortcodes[5][$key]; |
| 71 |
$shortcode[6] = $shortcodes[6][$key]; |
| 72 |
return $shortcode; |
| 73 |
} |
| 74 |
|
| 75 |
public function get_shortcode_content($shortcode_tag = '', $value = '') { |
| 76 |
$response = ''; |
| 77 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches); |
| 78 |
$tagnames = array_intersect(array($shortcode_tag), $matches[1]); |
| 79 |
if (!empty($tagnames)) { |
| 80 |
preg_match('/' . $this->get_shortcode_regex($tagnames) . '/', $value, $shortcode); |
| 81 |
if (isset($shortcode[5])) { |
| 82 |
$response = $shortcode[5]; |
| 83 |
} |
| 84 |
} |
| 85 |
return $response; |
| 86 |
} |
| 87 |
|
| 88 |
public function apply_path_attribute($value, $path = false, $delimiter = '.') { |
| 89 |
if ($path !== false) { |
| 90 |
if (!is_array($value) && !is_object($value)) { |
| 91 |
$value = $this->helper->load('convert')->is_unserialized_array($value); |
| 92 |
} |
| 93 |
$keys = explode($delimiter, $path); |
| 94 |
$obj = &$value; |
| 95 |
$found = true; |
| 96 |
foreach ($keys as $key) { |
| 97 |
if (is_array($obj) && isset($obj[$key])) { |
| 98 |
$obj = &$obj[$key]; |
| 99 |
} elseif (is_object($obj) && isset($obj->$key)) { |
| 100 |
$obj = &$obj->$key; |
| 101 |
} else { |
| 102 |
$found = false; |
| 103 |
break; |
| 104 |
} |
| 105 |
} |
| 106 |
return $found ? $obj : ''; |
| 107 |
} |
| 108 |
return ''; |
| 109 |
} |
| 110 |
|
| 111 |
public function apply_attachment_attribute($value, $function = 'attachment_url', $size = 'thumbnail') { |
| 112 |
if (is_array($value)) { |
| 113 |
$attachments = array(); |
| 114 |
foreach ($value as $post_meta_part) { |
| 115 |
if (!is_array($post_meta_part)) { |
| 116 |
if ($function == 'attachment_url') { |
| 117 |
$image = wp_get_attachment_url($post_meta_part); |
| 118 |
} else { |
| 119 |
$image = wp_get_attachment_image_url($post_meta_part, $size); |
| 120 |
} |
| 121 |
if ($image) { |
| 122 |
$attachments[] = $image; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
return $attachments; |
| 127 |
} else { |
| 128 |
if ($function == 'attachment_url') { |
| 129 |
$image = wp_get_attachment_url($value); |
| 130 |
} else { |
| 131 |
$image = wp_get_attachment_image_url($value, $size); |
| 132 |
} |
| 133 |
return $image ? $image : ''; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
public function apply_convert_attribute($convert, $post_meta, $implode = ',') { |
| 138 |
$type = false; |
| 139 |
switch (true) { |
| 140 |
case 0 === strpos($convert, 'term_id_to_'): |
| 141 |
$type = 'term'; |
| 142 |
$convert = str_replace('term_id_to_', '', $convert); |
| 143 |
break; |
| 144 |
case 0 === strpos($convert, 'user_id_to_'): |
| 145 |
$type = 'user'; |
| 146 |
$convert = str_replace('user_id_to_', '', $convert); |
| 147 |
break; |
| 148 |
case 0 === strpos($convert, 'post_id_to_'): |
| 149 |
$type = 'post'; |
| 150 |
$convert = str_replace('post_id_to_', '', $convert); |
| 151 |
break; |
| 152 |
} |
| 153 |
if ($type && $post_meta) { |
| 154 |
$implode = $implode === false ? ',' : $implode; |
| 155 |
if (!is_array($post_meta)) { |
| 156 |
if (strpos($post_meta, ',') !== false) { |
| 157 |
$post_meta = explode(',', $post_meta); |
| 158 |
} |
| 159 |
} |
| 160 |
if (is_array($post_meta)) { |
| 161 |
$sub_metas = array(); |
| 162 |
foreach ($post_meta as $post_meta_part) { |
| 163 |
if (!is_array($post_meta_part)) { |
| 164 |
switch ($type) { |
| 165 |
case 'term': |
| 166 |
$sub_meta = get_term($post_meta_part); |
| 167 |
break; |
| 168 |
case 'user': |
| 169 |
$sub_meta = get_userdata($post_meta_part); |
| 170 |
break; |
| 171 |
case 'post': |
| 172 |
$sub_meta = get_post($post_meta_part); |
| 173 |
break; |
| 174 |
} |
| 175 |
if ($sub_meta && !is_wp_error($sub_meta)) { |
| 176 |
if ($convert == $type) { |
| 177 |
$sub_metas[] = $sub_meta; |
| 178 |
} else { |
| 179 |
if (isset($sub_meta->$convert)) { |
| 180 |
$sub_metas[] = $sub_meta->$convert; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
$post_meta = $sub_metas; |
| 187 |
} else { |
| 188 |
switch ($type) { |
| 189 |
case 'term': |
| 190 |
$sub_meta = get_term($post_meta); |
| 191 |
break; |
| 192 |
case 'user': |
| 193 |
$sub_meta = get_userdata($post_meta); |
| 194 |
break; |
| 195 |
case 'post': |
| 196 |
$sub_meta = get_post($post_meta); |
| 197 |
break; |
| 198 |
} |
| 199 |
if ($sub_meta && !is_wp_error($sub_meta)) { |
| 200 |
if ($convert == $type) { |
| 201 |
$post_meta = $sub_meta; |
| 202 |
} else { |
| 203 |
if (isset($sub_meta->$convert)) { |
| 204 |
$post_meta = $sub_meta->$convert; |
| 205 |
} |
| 206 |
} |
| 207 |
} else { |
| 208 |
$post_meta = ''; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
return $post_meta; |
| 213 |
} |
| 214 |
|
| 215 |
public function apply_inline_shortcodes($value, $shortcode_tags, $extension) { |
| 216 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches); |
| 217 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 218 |
if (!empty($tagnames)) { |
| 219 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $value, $shortcodes); |
| 220 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 221 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 222 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 223 |
switch (true) { |
| 224 |
case (0 === strpos($shortcode[2], 'e2pdf-for')): |
| 225 |
$for_index = (0 === strpos($shortcode[2], 'e2pdf-for-')) ? str_replace('e2pdf-for-', '', $shortcode[2]) : 0; |
| 226 |
$sub_value = $this->helper->load('for')->do_shortcode($atts, $shortcode[5], $for_index, $extension); |
| 227 |
|
| 228 |
$sub_shortcode_tags = array_values(array_diff($shortcode_tags, [$shortcode[2]])); |
| 229 |
$sub_shortcode_tags[] = 'e2pdf-for-' . $for_index + 1; |
| 230 |
foreach ($sub_shortcode_tags as $sub_shortcode_key => $sub_shortcode_tag) { |
| 231 |
if (false === strpos($sub_value, '[' . $sub_shortcode_tag)) { |
| 232 |
unset($sub_shortcode_tags[$sub_shortcode_key]); |
| 233 |
} |
| 234 |
} |
| 235 |
if (!empty($sub_shortcode_tags)) { |
| 236 |
$sub_value = $this->apply_inline_shortcodes($sub_value, $sub_shortcode_tags, $extension); |
| 237 |
} |
| 238 |
$value = str_replace($shortcode_value, $sub_value, $value); |
| 239 |
break; |
| 240 |
case (0 === strpos($shortcode[2], 'e2pdf-acf-repeater')): |
| 241 |
if ($extension instanceof Extension_E2pdf_Wordpress) { |
| 242 |
if (!isset($atts['post_id']) && isset($extension->get('cached_post')->ID) && $extension->get('cached_post')->ID) { |
| 243 |
if ($extension->get('item') == '-3') { |
| 244 |
$atts['post_id'] = 'user_' . $extension->get('cached_post')->ID; |
| 245 |
} else { |
| 246 |
$atts['post_id'] = $extension->get('cached_post')->ID; |
| 247 |
} |
| 248 |
} |
| 249 |
} elseif ($extension instanceof Extension_E2pdf_Woocommerce) { |
| 250 |
if (!isset($atts['post_id']) && isset($extension->get('cached_post')->ID) && $extension->get('cached_post')->ID) { |
| 251 |
if ($extension->get('item') == 'product_variation' && isset($extension->get('cached_post')->post_parent)) { |
| 252 |
$atts['post_id'] = $extension->get('cached_post')->post_parent; |
| 253 |
} else { |
| 254 |
$atts['post_id'] = $extension->get('cached_post')->ID; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
$acf_repeater_index = (0 === strpos($shortcode[2], 'e2pdf-acf-repeater-')) ? str_replace('e2pdf-acf-repeater-', '', $shortcode[2]) : 0; |
| 260 |
$sub_value = $this->helper->load('acfrepeater')->do_shortcode($atts, $shortcode[5], $acf_repeater_index); |
| 261 |
|
| 262 |
$sub_shortcode_tags = array_values(array_diff($shortcode_tags, [$shortcode[2]])); |
| 263 |
$sub_shortcode_tags[] = 'e2pdf-acf-repeater-' . $acf_repeater_index + 1; |
| 264 |
foreach ($sub_shortcode_tags as $sub_shortcode_key => $sub_shortcode_tag) { |
| 265 |
if (false === strpos($sub_value, '[' . $sub_shortcode_tag)) { |
| 266 |
unset($sub_shortcode_tags[$sub_shortcode_key]); |
| 267 |
} |
| 268 |
} |
| 269 |
if (!empty($sub_shortcode_tags)) { |
| 270 |
$sub_value = $this->apply_inline_shortcodes($sub_value, $sub_shortcode_tags, $extension); |
| 271 |
} |
| 272 |
$value = str_replace($shortcode_value, $sub_value, $value); |
| 273 |
break; |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
return $value; |
| 278 |
} |
| 279 |
|
| 280 |
public function is_attachment($shortcode, $atts) { |
| 281 |
$true = [ |
| 282 |
'true', '1', |
| 283 |
]; |
| 284 |
if (($shortcode[2] === 'e2pdf-save' && isset($atts['attachment']) && in_array(strtolower((string) $atts['attachment']), $true, true)) || $shortcode[2] === 'e2pdf-attachment') { |
| 285 |
return true; |
| 286 |
} |
| 287 |
return false; |
| 288 |
} |
| 289 |
} |
| 290 |
|