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