arguments
2 years ago
arrayable
2 years ago
filters
2 years ago
http
2 years ago
macro-constants
2 years ago
post
2 years ago
resources
2 years ago
theme
2 years ago
attributes-trait.php
2 years ago
base-attributes-trait.php
2 years ago
builder-helper.php
2 years ago
compatibility.php
2 years ago
date-tools.php
2 years ago
gallery.php
2 years ago
get-icon-trait.php
2 years ago
get-template-trait.php
2 years ago
html-attributes-trait.php
2 years ago
instance-trait.php
2 years ago
macros-parser.php
2 years ago
regexp-tools.php
2 years ago
tools.php
2 years ago
macros-parser.php
177 lines
| 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 | // If this file is called directly, abort. |
| 10 | if ( ! defined( 'WPINC' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | class Macros_Parser { |
| 15 | |
| 16 | private $content = ''; |
| 17 | private $replacements = array(); |
| 18 | private $current_macro = ''; |
| 19 | |
| 20 | public function set_content( $content ): Macros_Parser { |
| 21 | if ( is_array( $content ) ) { |
| 22 | $content = $content[0] ?? ''; |
| 23 | } |
| 24 | |
| 25 | if ( ! $content || ! is_string( $content ) ) { |
| 26 | return $this; |
| 27 | } |
| 28 | $this->content = $content; |
| 29 | |
| 30 | return $this; |
| 31 | } |
| 32 | |
| 33 | public function set_replacements( $replacements ): Macros_Parser { |
| 34 | if ( ! $replacements ) { |
| 35 | return $this; |
| 36 | } |
| 37 | $this->replacements = $replacements; |
| 38 | |
| 39 | return $this; |
| 40 | } |
| 41 | |
| 42 | public function parse_macros( $content = false, $replacements = false ): string { |
| 43 | $this->set_content( $content )->set_replacements( $replacements ); |
| 44 | |
| 45 | if ( ! $this->content || ( ! $this->replacements && ! jet_fb_context()->has_request() ) ) { |
| 46 | return $this->content ?: ''; |
| 47 | } |
| 48 | |
| 49 | return $this->macros_replace(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Parse macros in content |
| 54 | * |
| 55 | * @return string |
| 56 | */ |
| 57 | public function macros_replace(): string { |
| 58 | Constants_Manager::instance(); |
| 59 | |
| 60 | $content = preg_replace_callback( |
| 61 | '/%([\w\-].*?\S?)%/', |
| 62 | function ( $replace_match ) { |
| 63 | $filters = explode( '|', $replace_match[1] ); |
| 64 | $name = $filters[0]; |
| 65 | array_shift( $filters ); |
| 66 | |
| 67 | if ( false !== strpos( $name, '::' ) ) { |
| 68 | $value = apply_filters( |
| 69 | 'jet-form-builder/custom-macro', |
| 70 | null, |
| 71 | $name, |
| 72 | $this |
| 73 | ); |
| 74 | |
| 75 | if ( is_null( $value ) ) { |
| 76 | return $replace_match[0]; |
| 77 | } |
| 78 | |
| 79 | return Filters_Manager::instance()->apply( $value, $filters ); |
| 80 | } |
| 81 | |
| 82 | if ( ! $this->has_replace( $name ) ) { |
| 83 | return $replace_match[0]; |
| 84 | } |
| 85 | |
| 86 | $this->current_macro = $name; |
| 87 | |
| 88 | $value = $this->get_replace( $name ); |
| 89 | |
| 90 | if ( ! empty( $filters ) ) { |
| 91 | return Filters_Manager::instance()->apply( $value, $filters ); |
| 92 | } |
| 93 | |
| 94 | if ( ! is_array( $value ) ) { |
| 95 | return $value; |
| 96 | } |
| 97 | |
| 98 | if ( 'repeater-field' === jet_fb_context()->get_field_type( $name ) ) { |
| 99 | return $this->verbose_repeater( $value, $name ); |
| 100 | } else { |
| 101 | return implode( ', ', $value ); |
| 102 | } |
| 103 | }, |
| 104 | $this->content |
| 105 | ); |
| 106 | |
| 107 | return is_string( $content ) ? $content : ''; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Verbose repeater items array |
| 112 | * |
| 113 | * @param array $items |
| 114 | * @param $name |
| 115 | * |
| 116 | * @return string |
| 117 | */ |
| 118 | protected function verbose_repeater( array $items, $name ) { |
| 119 | |
| 120 | $result = apply_filters( 'jet-form-builder/send-email/template-repeater', '', $items, $this ); |
| 121 | $counter = 1; |
| 122 | |
| 123 | if ( $result ) { |
| 124 | return $result; |
| 125 | } |
| 126 | |
| 127 | foreach ( $items as $index => $item ) { |
| 128 | $item_data = array(); |
| 129 | |
| 130 | foreach ( $item as $key => $value ) { |
| 131 | $label = jet_fb_context()->get_setting( 'label', array( $name, $index, $key ) ); |
| 132 | |
| 133 | $item_data[] = sprintf( |
| 134 | '%1$s: %2$s', |
| 135 | $label ?: $key, |
| 136 | $this->maybe_parse_if_array( $value ) |
| 137 | ); |
| 138 | } |
| 139 | $result .= ( $counter++ ) . ') ' . implode( ', ', $item_data ) . ';<br>'; |
| 140 | } |
| 141 | |
| 142 | return $result; |
| 143 | } |
| 144 | |
| 145 | private function maybe_parse_if_array( $value ) { |
| 146 | if ( is_array( $value ) ) { |
| 147 | return implode( ', ', $value ); |
| 148 | } |
| 149 | |
| 150 | return $value; |
| 151 | } |
| 152 | |
| 153 | protected function has_replace( string $name ): bool { |
| 154 | if ( ! empty( $this->replacements ) ) { |
| 155 | return isset( $this->replacements[ $name ] ); |
| 156 | } |
| 157 | |
| 158 | return jet_fb_context()->has_field( $name ); |
| 159 | } |
| 160 | |
| 161 | protected function get_replace( string $name ) { |
| 162 | if ( ! empty( $this->replacements ) ) { |
| 163 | return $this->replacements[ $name ] ?? ''; |
| 164 | } |
| 165 | |
| 166 | return jet_fb_context()->get_value( $name ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @return string |
| 171 | */ |
| 172 | public function get_current_macro(): string { |
| 173 | return $this->current_macro; |
| 174 | } |
| 175 | |
| 176 | } |
| 177 |