| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /helper/e2pdf-acfrepeater.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_Acfrepeater { |
| 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) { |
| 23 |
$result = []; |
| 24 |
$field = isset($atts['field']) ? $atts['field'] : null; |
| 25 |
$post_id = isset($atts['post_id']) ? $atts['post_id'] : null; |
| 26 |
$implode = isset($atts['implode']) ? $atts['implode'] : ''; |
| 27 |
$index = 0; |
| 28 |
if (function_exists('have_rows') && have_rows($field, $post_id)) { |
| 29 |
$total = count(get_field($field, $post_id)); |
| 30 |
while (have_rows($field, $post_id)) { |
| 31 |
the_row(); |
| 32 |
$result[] = $this->apply($value, $atts, $index, $for, $total); |
| 33 |
$index++; |
| 34 |
} |
| 35 |
} |
| 36 |
return implode($implode, $result); |
| 37 |
} |
| 38 |
|
| 39 |
public function apply($value, $atts, $index, $for = 0, $total = 0) { |
| 40 |
if ($value) { |
| 41 |
|
| 42 |
$field = isset($atts['field']) ? $atts['field'] : null; |
| 43 |
$post_id = isset($atts['post_id']) ? $atts['post_id'] : null; |
| 44 |
|
| 45 |
$for_index = $for ? '-' . $for : ''; |
| 46 |
$evenodd = $index % 2 == 0 ? '0' : '1'; |
| 47 |
$replace = array( |
| 48 |
'[e2pdf-acf-repeater-index' . $for_index . ']' => $index, |
| 49 |
'[e2pdf-acf-repeater-counter' . $for_index . ']' => $index + 1, |
| 50 |
'[e2pdf-acf-repeater-evenodd' . $for_index . ']' => $evenodd, |
| 51 |
'[e2pdf-acf-repeater-count' . $for_index . ']' => $total, |
| 52 |
); |
| 53 |
$value = str_replace(array_keys($replace), $replace, $value); |
| 54 |
$value = preg_replace('/\[(e2pdf-for-index|e2pdf-for-counter|e2pdf-for-key|e2pdf-for-evenodd|e2pdf-acf-repeater-index|e2pdf-acf-repeater-counter|e2pdf-acf-repeater-evenodd)(-\d+)?\]/', '#$1$2#', $value); |
| 55 |
$shortcode_tags = array( |
| 56 |
'acf', |
| 57 |
); |
| 58 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches); |
| 59 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 60 |
if (!empty($tagnames)) { |
| 61 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $value, $shortcodes); |
| 62 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 63 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 64 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 65 |
$repeater = isset($atts['repeater']) ? $atts['repeater'] : $field; |
| 66 |
if ($shortcode[2] === 'acf') { |
| 67 |
if (!isset($atts['post_id']) && $post_id) { |
| 68 |
$shortcode[3] .= ' post_id="' . $post_id . '"'; |
| 69 |
} |
| 70 |
if ($field && isset($atts['field'])) { |
| 71 |
if ($repeater !== $field) { |
| 72 |
$value = str_replace($shortcode_value, '[' . $shortcode[2] . $shortcode[3] . ']', $value); |
| 73 |
} else { |
| 74 |
if (isset($atts['repeater'])) { |
| 75 |
$shortcode[3] .= ' field="' . $field . '_' . $index . '_' . $atts['field'] . '"'; |
| 76 |
} else { |
| 77 |
$shortcode[3] .= ' field="' . $field . '_' . $index . '_' . $atts['field'] . '" repeater="' . $field . '"'; |
| 78 |
} |
| 79 |
$value = str_replace($shortcode_value, '[' . $shortcode[2] . $shortcode[3] . ']', $value); |
| 80 |
} |
| 81 |
} else { |
| 82 |
$value = str_replace($shortcode_value, '', $value); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
$value = preg_replace('/#(e2pdf-for-index|e2pdf-for-counter|e2pdf-for-key|e2pdf-for-evenodd|e2pdf-acf-repeater-index|e2pdf-acf-repeater-counter|e2pdf-acf-repeater-evenodd)(-\d+)?#/', '[$1$2]', $value); |
| 88 |
} |
| 89 |
return $value; |
| 90 |
} |
| 91 |
} |
| 92 |
|