| 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_Foreach { |
| 16 |
|
| 17 |
private $helper; |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* [e2pdf-foreach-x] shortcode |
| 25 |
* @param array $atts - Attributes |
| 26 |
* @param string $value - Content |
| 27 |
*/ |
| 28 |
public function add($attributes = array(), $value = '', $foreach = 1) { |
| 29 |
if (false !== strpos($value, '[')) { |
| 30 |
$shortcode_tags = array( |
| 31 |
'e2pdf-foreach-' . $foreach, |
| 32 |
); |
| 33 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches); |
| 34 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 35 |
foreach ($matches[1] as $key => $shortcode) { |
| 36 |
if (strpos($shortcode, ':') !== false) { |
| 37 |
$shortcode_tags[] = $shortcode; |
| 38 |
} |
| 39 |
} |
| 40 |
if (!empty($tagnames)) { |
| 41 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $value, $shortcodes); |
| 42 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 43 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 44 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 45 |
if (isset($atts['shortcode'])) { |
| 46 |
$response = array(); |
| 47 |
$implode = isset($atts['implode']) ? $atts['implode'] : ''; |
| 48 |
$foreach_shortcode = str_replace('-', '_', $atts['shortcode']); |
| 49 |
if ($attributes['shortcode'] == $atts['shortcode']) { |
| 50 |
if (!isset($atts['id']) && isset($attributes['id'])) { |
| 51 |
$atts['id'] = $attributes['id']; |
| 52 |
} |
| 53 |
if (!isset($atts['wc_order_id']) && isset($attributes['wc_order_id'])) { |
| 54 |
$atts['wc_order_id'] = $attributes['wc_order_id']; |
| 55 |
} |
| 56 |
} else { |
| 57 |
if ($attributes['shortcode'] == 'e2pdf-wc-order') { |
| 58 |
if ($atts['shortcode'] == 'e2pdf-wc-product') { |
| 59 |
if (!isset($atts['wc_order_id']) && isset($attributes['id'])) { |
| 60 |
$atts['wc_order_id'] = $attributes['id']; |
| 61 |
} |
| 62 |
} |
| 63 |
} elseif ($attributes['shortcode'] == 'e2pdf-wc-cart') { |
| 64 |
if ($atts['shortcode'] == 'e2pdf-wc-product') { |
| 65 |
if (!isset($atts['wc_order_id']) && isset($attributes['id'])) { |
| 66 |
$atts['wc_order_id'] = 'cart'; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
$model_e2pdf_shortcode = new Model_E2pdf_Shortcode(); |
| 72 |
if (method_exists($model_e2pdf_shortcode, $foreach_shortcode)) { |
| 73 |
add_filter('e2pdf_raw_output', array($this->helper, '__return_true'), 999); |
| 74 |
$data = $model_e2pdf_shortcode->$foreach_shortcode($atts, ''); |
| 75 |
remove_filter('e2pdf_raw_output', array($this->helper, '__return_true'), 999); |
| 76 |
if (is_array($data) && count($data) > 0) { |
| 77 |
$index = 0; |
| 78 |
foreach ($data as $data_key => $data_value) { |
| 79 |
$sub_value = $this->do_shortcode($shortcode[5], $data_key, $data_value, $index, $foreach); |
| 80 |
$response[] = $this->add($attributes, $sub_value, $foreach + 1); |
| 81 |
$index++; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
$value = str_replace($shortcode_value, implode($implode, $response), $value); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
return $value; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* [e2pdf-foreach] inner shortcodes support |
| 95 |
*/ |
| 96 |
public function do_shortcode($value, $data_key, $data_value, $index, $foreach = 0) { |
| 97 |
if ($value) { |
| 98 |
$foreach_index = $foreach ? '-' . $foreach : ''; |
| 99 |
$evenodd = $index % 2 == 0 ? '0' : '1'; |
| 100 |
$replace = array( |
| 101 |
'[e2pdf-foreach-index' . $foreach_index . ']' => $index, |
| 102 |
'[e2pdf-foreach-counter' . $foreach_index . ']' => $index + 1, |
| 103 |
'[e2pdf-foreach-key' . $foreach_index . ']' => $data_key, |
| 104 |
'[e2pdf-foreach-value' . $foreach_index . ']' => is_array($data_value) || is_object($data_value) ? serialize($data_value) : $data_value, |
| 105 |
'[e2pdf-foreach-evenodd' . $foreach_index . ']' => $evenodd, |
| 106 |
); |
| 107 |
$value = str_replace(array_keys($replace), $replace, $value); |
| 108 |
$shortcode_tags = array( |
| 109 |
'e2pdf-foreach-value' . $foreach_index . '', |
| 110 |
); |
| 111 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches); |
| 112 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 113 |
if (!empty($tagnames)) { |
| 114 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $value, $shortcodes); |
| 115 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 116 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 117 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 118 |
if (isset($atts['path'])) { |
| 119 |
$path = $atts['path']; |
| 120 |
$post_meta = $data_value; |
| 121 |
if ((is_array($post_meta) || is_object($post_meta)) && $path !== false) { |
| 122 |
$path_parts = explode('.', $path); |
| 123 |
$path_value = &$post_meta; |
| 124 |
$found = true; |
| 125 |
foreach ($path_parts as $path_part) { |
| 126 |
if (is_array($path_value) && isset($path_value[$path_part])) { |
| 127 |
$path_value = &$path_value[$path_part]; |
| 128 |
} elseif (is_object($path_value) && isset($path_value->$path_part)) { |
| 129 |
$path_value = &$path_value->$path_part; |
| 130 |
} else { |
| 131 |
$found = false; |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
if ($found) { |
| 136 |
$post_meta = $path_value; |
| 137 |
} else { |
| 138 |
$post_meta = ''; |
| 139 |
} |
| 140 |
} |
| 141 |
if (is_array($post_meta) || is_object($post_meta)) { |
| 142 |
$post_meta = serialize($post_meta); |
| 143 |
} |
| 144 |
$value = str_replace($shortcode_value, $post_meta, $value); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
return $value; |
| 150 |
} |
| 151 |
} |
| 152 |
|