| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Classes; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Classes\Filters\Filters_Manager; |
| 7 |
use Jet_Form_Builder\Classes\Macro_Constants\Constants_Manager; |
| 8 |
|
| 9 |
class Macros_Parser { |
| 10 |
|
| 11 |
private $content = ''; |
| 12 |
private $replacements = array(); |
| 13 |
|
| 14 |
public function set_content( $content ): Macros_Parser { |
| 15 |
if ( is_array( $content ) ) { |
| 16 |
$content = $content[0] ?? ''; |
| 17 |
} |
| 18 |
|
| 19 |
if ( ! $content || ! is_string( $content ) ) { |
| 20 |
return $this; |
| 21 |
} |
| 22 |
$this->content = $content; |
| 23 |
|
| 24 |
return $this; |
| 25 |
} |
| 26 |
|
| 27 |
public function set_replacements( $replacements ): Macros_Parser { |
| 28 |
if ( ! $replacements ) { |
| 29 |
return $this; |
| 30 |
} |
| 31 |
$this->replacements = $replacements; |
| 32 |
|
| 33 |
return $this; |
| 34 |
} |
| 35 |
|
| 36 |
public function parse_macros( $content = false, $replacements = false ): string { |
| 37 |
$this->set_content( $content )->set_replacements( $replacements ); |
| 38 |
|
| 39 |
if ( ! $this->content || ! $this->replacements ) { |
| 40 |
return $this->content ?: ''; |
| 41 |
} |
| 42 |
|
| 43 |
return $this->macros_replace(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Parse macros in content |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function macros_replace(): string { |
| 52 |
Constants_Manager::instance(); |
| 53 |
|
| 54 |
$content = preg_replace_callback( |
| 55 |
'/%(.+?)%/', |
| 56 |
function ( $match ) { |
| 57 |
$filters = explode( '|', $match[1] ); |
| 58 |
$name = $filters[0]; |
| 59 |
array_shift( $filters ); |
| 60 |
|
| 61 |
if ( false !== strpos( $name, '::' ) ) { |
| 62 |
$value = apply_filters( |
| 63 |
'jet-form-builder/custom-macro', |
| 64 |
null, |
| 65 |
$name, |
| 66 |
$this |
| 67 |
); |
| 68 |
|
| 69 |
if ( is_null( $value ) ) { |
| 70 |
return $match[0]; |
| 71 |
} |
| 72 |
|
| 73 |
return Filters_Manager::instance()->apply( $value, $filters ); |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! isset( $this->replacements[ $name ] ) ) { |
| 77 |
return $match[0]; |
| 78 |
} |
| 79 |
|
| 80 |
$value = $this->replacements[ $name ]; |
| 81 |
|
| 82 |
if ( ! empty( $filters ) ) { |
| 83 |
return Filters_Manager::instance()->apply( $value, $filters ); |
| 84 |
} |
| 85 |
|
| 86 |
if ( ! is_array( $value ) ) { |
| 87 |
return $value; |
| 88 |
} |
| 89 |
|
| 90 |
if ( jet_fb_request_handler()->is_type( $name, 'repeater-field' ) ) { |
| 91 |
return $this->verbose_repeater( $value ); |
| 92 |
} else { |
| 93 |
return implode( ', ', $value ); |
| 94 |
} |
| 95 |
}, |
| 96 |
$this->content |
| 97 |
); |
| 98 |
|
| 99 |
return is_string( $content ) ? $content : ''; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Verbose repeater items array |
| 104 |
* |
| 105 |
* @param array $items |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public function verbose_repeater( $items = array() ) { |
| 110 |
|
| 111 |
$result = apply_filters( 'jet-form-builder/send-email/template-repeater', '', $items, $this ); |
| 112 |
$index = 1; |
| 113 |
|
| 114 |
if ( $result ) { |
| 115 |
return $result; |
| 116 |
} |
| 117 |
|
| 118 |
foreach ( $items as $item ) { |
| 119 |
$item_data = array(); |
| 120 |
|
| 121 |
foreach ( $item as $key => $value ) { |
| 122 |
$item_data[] = sprintf( '%1$s: %2$s', $key, $this->maybe_parse_if_array( $value ) ); |
| 123 |
} |
| 124 |
$result .= $index ++ . ') ' . implode( ', ', $item_data ) . ';<br>'; |
| 125 |
} |
| 126 |
|
| 127 |
return $result; |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
private function maybe_parse_if_array( $value ) { |
| 132 |
if ( is_array( $value ) ) { |
| 133 |
return implode( ', ', $value ); |
| 134 |
} |
| 135 |
|
| 136 |
return $value; |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|