| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /helper/e2pdf-for.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_For { |
| 15 |
|
| 16 |
private $helper; |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 20 |
} |
| 21 |
|
| 22 |
public function do_shortcode($atts = array(), $value = '', $for = 0, $extension = null) { |
| 23 |
$result = array(); |
| 24 |
$implode = isset($atts['implode']) ? $atts['implode'] : ''; |
| 25 |
$for_index = $for ? '-' . $for : ''; |
| 26 |
$tags = array( |
| 27 |
'e2pdf-for-data' => 'e2pdf-for-data' . $for_index, |
| 28 |
'e2pdf-for-do' => 'e2pdf-for-do' . $for_index, |
| 29 |
'e2pdf-for-else' => 'e2pdf-for-else' . $for_index, |
| 30 |
); |
| 31 |
/* Backward compatibility */ |
| 32 |
if (false === strpos($value, '[e2pdf-for-data')) { |
| 33 |
$tags = array( |
| 34 |
'e2pdf-for-data' => 'e2pdf-data' . $for_index, |
| 35 |
'e2pdf-for-do' => 'e2pdf-do' . $for_index, |
| 36 |
'e2pdf-for-else' => 'e2pdf-else' . $for_index, |
| 37 |
); |
| 38 |
} |
| 39 |
add_filter('e2pdf_for_do_shortcode_data_process', array($this, 'filter_do_shortcode_data_process')); |
| 40 |
if ($extension && method_exists($extension, 'render')) { |
| 41 |
$data = $this->helper->load('convert')->is_unserialized_array($extension->render($this->helper->load('shortcode')->get_shortcode_content($tags['e2pdf-for-data'], $value), array(), false, true)); |
| 42 |
} else { |
| 43 |
$data = $this->helper->load('convert')->is_unserialized_array($this->helper->load('shortcode')->get_shortcode_content($tags['e2pdf-for-data'], $value)); |
| 44 |
} |
| 45 |
remove_filter('e2pdf_for_do_shortcode_data_process', array($this, 'filter_do_shortcode_data_process')); |
| 46 |
if (!empty($data)) { |
| 47 |
$index = 0; |
| 48 |
$total = count($data); |
| 49 |
foreach ($data as $data_key => $data_value) { |
| 50 |
$do = $this->helper->load('shortcode')->get_shortcode_content($tags['e2pdf-for-do'], $value); |
| 51 |
$result[] = $this->apply($do, $data_key, $data_value, $index, $for, $total); |
| 52 |
$index++; |
| 53 |
} |
| 54 |
$response = implode($implode, $result); |
| 55 |
} else { |
| 56 |
$response = $this->helper->load('shortcode')->get_shortcode_content($tags['e2pdf-for-else'], $value); |
| 57 |
} |
| 58 |
return $response; |
| 59 |
} |
| 60 |
|
| 61 |
public function apply($value, $data_key, $data_value, $index, $for = 0, $total = 0) { |
| 62 |
if ($value) { |
| 63 |
$for_index = $for ? '-' . $for : ''; |
| 64 |
$evenodd = $index % 2 == 0 ? '0' : '1'; |
| 65 |
$replace = array( |
| 66 |
'[e2pdf-for-index' . $for_index . ']' => $index, |
| 67 |
'[e2pdf-for-counter' . $for_index . ']' => $index + 1, |
| 68 |
'[e2pdf-for-key' . $for_index . ']' => $data_key, |
| 69 |
'[e2pdf-for-evenodd' . $for_index . ']' => $evenodd, |
| 70 |
'[e2pdf-for-count' . $for_index . ']' => $total, |
| 71 |
); |
| 72 |
$value = str_replace(array_keys($replace), $replace, $value); |
| 73 |
$shortcode_tags = array( |
| 74 |
'e2pdf-for-value' . $for_index . '', |
| 75 |
); |
| 76 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches); |
| 77 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 78 |
if (!empty($tagnames)) { |
| 79 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $value, $shortcodes); |
| 80 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 81 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 82 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 83 |
$response = ''; |
| 84 |
$path = isset($atts['path']) ? $atts['path'] : false; |
| 85 |
$implode = isset($atts['implode']) ? $atts['implode'] : false; |
| 86 |
|
| 87 |
$post_meta = $data_value; |
| 88 |
if ((is_array($post_meta) || is_object($post_meta)) && $path !== false) { |
| 89 |
$path_parts = explode('.', $path); |
| 90 |
$path_value = &$post_meta; |
| 91 |
$found = true; |
| 92 |
foreach ($path_parts as $path_part) { |
| 93 |
if (is_array($path_value) && isset($path_value[$path_part])) { |
| 94 |
$path_value = &$path_value[$path_part]; |
| 95 |
} elseif (is_object($path_value) && isset($path_value->$path_part)) { |
| 96 |
$path_value = &$path_value->$path_part; |
| 97 |
} else { |
| 98 |
$found = false; |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
if ($found) { |
| 103 |
$post_meta = $path_value; |
| 104 |
} else { |
| 105 |
$post_meta = ''; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if (is_array($post_meta)) { |
| 110 |
if ($implode !== false) { |
| 111 |
if (!$this->helper->is_multidimensional($post_meta)) { |
| 112 |
foreach ($post_meta as $post_meta_key => $post_meta_value) { |
| 113 |
$post_meta[$post_meta_key] = $this->helper->load('translator')->translate($post_meta_value); |
| 114 |
} |
| 115 |
$response = implode($implode, $post_meta); |
| 116 |
} else { |
| 117 |
$response = serialize($post_meta); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 118 |
} |
| 119 |
} else { |
| 120 |
$response = serialize($post_meta); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 121 |
} |
| 122 |
} elseif (is_object($post_meta)) { |
| 123 |
$response = serialize($post_meta); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 124 |
} else { |
| 125 |
$response = $post_meta; |
| 126 |
} |
| 127 |
$value = str_replace($shortcode_value, $response, $value); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
return $value; |
| 132 |
} |
| 133 |
|
| 134 |
public function filter_do_shortcode_data_process($status) { |
| 135 |
return true; |
| 136 |
} |
| 137 |
} |
| 138 |
|