PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.4.7
JetFormBuilder — Dynamic Blocks Form Builder v3.4.7
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / rich-content / macros-parser.php
jetformbuilder / modules / rich-content Last commit date
macros-parser.php 2 years ago module.php 2 years ago
macros-parser.php
175 lines
1 <?php
2
3
4 namespace JFB_Modules\Rich_Content;
5
6 use Jet_Form_Builder\Classes\Filters\Filters_Manager;
7 use Jet_Form_Builder\Classes\Macro_Constants\Constants_Manager;
8 use Jet_Form_Builder\Classes\Tools;
9
10 // If this file is called directly, abort.
11 if ( ! defined( 'WPINC' ) ) {
12 die;
13 }
14
15 class Macros_Parser {
16
17 private $content = '';
18 private $replacements = array();
19 private $current_macro = '';
20
21 public function set_content( $content ): Macros_Parser {
22 if ( is_array( $content ) ) {
23 $content = $content[0] ?? '';
24 }
25
26 $this->content = Tools::to_string( $content );
27
28 return $this;
29 }
30
31 public function set_replacements( $replacements ): Macros_Parser {
32 if ( ! $replacements ) {
33 return $this;
34 }
35 $this->replacements = $replacements;
36
37 return $this;
38 }
39
40 public function parse_macros( $content = false, $replacements = false ): string {
41 $this->set_content( $content )->set_replacements( $replacements );
42
43 if ( ! $this->content || ( ! $this->replacements && ! jet_fb_context()->has_request() ) ) {
44 return $this->content ?: '';
45 }
46
47 return $this->macros_replace();
48 }
49
50 /**
51 * Parse macros in content
52 *
53 * @return string
54 */
55 public function macros_replace(): string {
56 Constants_Manager::instance();
57
58 $content = preg_replace_callback(
59 '/%([\w\-].*?\S?)%/',
60 function ( $replace_match ) {
61 $filters = explode( '|', $replace_match[1] );
62 $name = $filters[0];
63 array_shift( $filters );
64
65 if ( false !== strpos( $name, '::' ) ) {
66 $value = apply_filters(
67 'jet-form-builder/custom-macro',
68 null,
69 $name,
70 $this
71 );
72
73 if ( is_null( $value ) ) {
74 return $replace_match[0];
75 }
76
77 return Filters_Manager::instance()->apply( $value, $filters );
78 }
79
80 if ( ! $this->has_replace( $name ) ) {
81 return $replace_match[0];
82 }
83
84 $this->current_macro = $name;
85
86 $value = $this->get_replace( $name );
87
88 if ( ! empty( $filters ) ) {
89 return Filters_Manager::instance()->apply( $value, $filters );
90 }
91
92 if ( ! is_array( $value ) ) {
93 return $value;
94 }
95
96 if ( 'repeater-field' === jet_fb_context()->get_field_type( $name ) ) {
97 return $this->verbose_repeater( $value, $name );
98 } else {
99 return implode( ', ', $value );
100 }
101 },
102 $this->content
103 );
104
105 return is_string( $content ) ? $content : '';
106 }
107
108 /**
109 * Verbose repeater items array
110 *
111 * @param array $items
112 * @param $name
113 *
114 * @return string
115 */
116 protected function verbose_repeater( array $items, $name ) {
117
118 $result = apply_filters( 'jet-form-builder/send-email/template-repeater', '', $items, $this );
119 $counter = 1;
120
121 if ( $result ) {
122 return $result;
123 }
124
125 foreach ( $items as $index => $item ) {
126 $item_data = array();
127
128 foreach ( $item as $key => $value ) {
129 $label = jet_fb_context()->get_setting( 'label', array( $name, $index, $key ) );
130
131 $item_data[] = sprintf(
132 '%1$s: %2$s',
133 $label ?: $key,
134 $this->maybe_parse_if_array( $value )
135 );
136 }
137 $result .= ( $counter++ ) . ') ' . implode( ', ', $item_data ) . ';<br>';
138 }
139
140 return $result;
141 }
142
143 private function maybe_parse_if_array( $value ) {
144 if ( is_array( $value ) ) {
145 return implode( ', ', $value );
146 }
147
148 return $value;
149 }
150
151 protected function has_replace( string $name ): bool {
152 if ( ! empty( $this->replacements ) ) {
153 return isset( $this->replacements[ $name ] );
154 }
155
156 return jet_fb_context()->has_field( $name );
157 }
158
159 protected function get_replace( string $name ) {
160 if ( ! empty( $this->replacements ) ) {
161 return $this->replacements[ $name ] ?? '';
162 }
163
164 return jet_fb_context()->get_value( $name );
165 }
166
167 /**
168 * @return string
169 */
170 public function get_current_macro(): string {
171 return $this->current_macro;
172 }
173
174 }
175