PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.16.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.16.1
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmEntryShortcodeFormatter.php

FrmEntryShortcodeFormatter.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.16.1, at classes/models/FrmEntryShortcodeFormatter.php

343 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @since 2.04
8 */
9 class FrmEntryShortcodeFormatter {
10
11 /**
12 * @var int
13 * @since 2.04
14 */
15 protected $form_id = 0;
16
17 /**
18 * @var array
19 * @since 2.04
20 */
21 protected $skip_fields = array( 'captcha', 'html' );
22
23 /**
24 * @var array
25 * @since 3.0
26 */
27 protected $single_cell_fields = array( 'html' );
28
29 /**
30 * @var array
31 * @since 2.04
32 */
33 protected $fields = array();
34
35 /**
36 * @var bool
37 * @since 2.05
38 */
39 protected $is_plain_text = false;
40
41 /**
42 * @var string
43 * @since 2.04
44 */
45 protected $format = 'text';
46
47 /**
48 * @var FrmTableHTMLGenerator|null
49 * @since 2.04
50 */
51 protected $table_generator;
52
53 /**
54 * @var array
55 * @since 2.04
56 */
57 protected $array_content = array();
58
59 /**
60 * FrmEntryShortcodeFormatter constructor
61 *
62 * @param int|string $form_id
63 * @param array $atts
64 */
65 public function __construct( $form_id, $atts ) {
66 if ( ! $form_id ) {
67 return;
68 }
69
70 $this->init_form_id( $form_id );
71 $this->init_fields();
72
73 if ( empty( $this->fields ) ) {
74 return;
75 }
76
77 $this->init_plain_text( $atts );
78 $this->init_format( $atts );
79
80 if ( $this->is_table_format() ) {
81 $this->init_table_generator();
82 }
83 }
84
85 /**
86 * Initialize the form_id property
87 *
88 * @since 2.04
89 *
90 * @param int|string $form_id
91 *
92 * @return void
93 */
94 protected function init_form_id( $form_id ) {
95 $this->form_id = (int) $form_id;
96 }
97
98 /**
99 * Initialize the fields property
100 *
101 * @since 2.04
102 *
103 * @return void
104 */
105 protected function init_fields() {
106 $this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' );
107 }
108
109 /**
110 * Initialize the is_plain_text property
111 *
112 * @since 2.05
113 *
114 * @param array $atts
115 *
116 * @return void
117 */
118 protected function init_plain_text( $atts ) {
119 if ( isset( $atts['plain_text'] ) && $atts['plain_text'] ) {
120 $this->is_plain_text = true;
121 }
122 }
123
124 /**
125 * Initialize the format property
126 *
127 * @since 2.04
128 *
129 * @param array $atts
130 *
131 * @return void
132 */
133 protected function init_format( $atts ) {
134 if ( isset( $atts['format'] ) && is_string( $atts['format'] ) && $atts['format'] !== '' ) {
135 $this->format = $atts['format'];
136 } else {
137 $this->format = 'text';
138 }
139 }
140
141 /**
142 * Initialize the table_generator property
143 *
144 * @since 2.04
145 *
146 * @return void
147 */
148 protected function init_table_generator() {
149 $this->table_generator = new FrmTableHTMLGenerator( 'shortcode' );
150 }
151
152 /**
153 * Return the default HTML for an entry
154 *
155 * @since 2.04
156 */
157 public function content() {
158 if ( ! $this->form_id || empty( $this->fields ) ) {
159 return '';
160 }
161
162 if ( $this->format === 'array' ) {
163 $content = $this->get_array();
164 } elseif ( $this->is_plain_text_format() ) {
165 $content = $this->get_plain_text();
166 } else {
167 $content = $this->get_table();
168 }
169
170 return $content;
171 }
172
173 /**
174 * Return the default HTML array
175 *
176 * @since 2.04
177 *
178 * @return array
179 */
180 protected function get_array() {
181 foreach ( $this->fields as $field ) {
182 $this->add_field_array( $field );
183 }
184
185 return $this->array_content;
186 }
187
188 /**
189 * Return the default plain text for an email message
190 *
191 * @since 2.04
192 *
193 * @return string
194 */
195 protected function get_plain_text() {
196 return $this->generate_content_for_all_fields();
197 }
198
199 /**
200 * Return the default HTML for an email message
201 *
202 * @since 2.04
203 *
204 * @return string
205 */
206 protected function get_table() {
207 $content = $this->table_generator->generate_table_header();
208 $content .= $this->generate_content_for_all_fields();
209 $content .= $this->table_generator->generate_table_footer();
210
211 return $content;
212 }
213
214 /**
215 * Generate the content for all fields
216 *
217 * @since 2.05
218 *
219 * @return string
220 */
221 protected function generate_content_for_all_fields() {
222 $content = '';
223
224 foreach ( $this->fields as $field ) {
225 $content .= $this->generate_field_content( $field );
226 }
227
228 return $content;
229 }
230
231 /**
232 * Generate a field's HTML or plain text shortcodes
233 *
234 * @since 2.04
235 *
236 * @param stdClass $field
237 *
238 * @return string
239 */
240 protected function generate_field_content( $field ) {
241 if ( in_array( $field->type, $this->skip_fields ) ) {
242 return '';
243 }
244
245 $row = $this->generate_two_cell_shortcode_row( $field );
246
247 return $row;
248 }
249
250 /**
251 * Generate a two cell row of shortcodes for an HTML or plain text table
252 *
253 * @since 2.04
254 *
255 * @param stdClass $field
256 * @param mixed $value
257 *
258 * @return string
259 */
260 protected function generate_two_cell_shortcode_row( $field, $value = null ) {
261 $row = '[if ' . $field->id . ']';
262
263 $label = '[' . $field->id . ' show=field_label]';
264
265 if ( $value === null ) {
266 $value = '[' . $field->id . ']';
267 }
268
269 if ( $this->is_plain_text_format() ) {
270 $row .= $label . ': ' . $value . "\r\n";
271 } else {
272 $row .= $this->table_generator->generate_two_cell_table_row( $label, $value );
273 }
274
275 $row .= '[/if ' . $field->id . ']';
276
277 if ( $this->is_table_format() ) {
278 $row .= "\r\n";
279 }
280
281 return $row;
282 }
283
284 /**
285 * Generate a field's array for the default HTML array
286 *
287 * @since 2.04
288 *
289 * @param stdClass $field
290 *
291 * @return void
292 */
293 protected function add_field_array( $field ) {
294 if ( in_array( $field->type, $this->skip_fields ) ) {
295 return;
296 }
297
298 $this->add_single_field_array( $field, $field->id );
299 }
300
301 /**
302 * Generate a single field's array
303 *
304 * @since 2.04
305 *
306 * @param stdClass $field
307 * @param string $value
308 *
309 * @return void
310 */
311 protected function add_single_field_array( $field, $value ) {
312 $array = array(
313 'label' => '[' . $field->id . ' show=field_label]',
314 'val' => '[' . $value . ']',
315 'type' => $field->type,
316 );
317
318 $this->array_content[ $field->id ] = apply_filters( 'frm_field_shortcodes_for_default_html_email', $array, $field );
319 }
320
321 /**
322 * Check if the format is default plain text
323 *
324 * @since 2.05
325 *
326 * @return bool
327 */
328 protected function is_plain_text_format() {
329 return ( $this->format === 'text' && $this->is_plain_text === true );
330 }
331
332 /**
333 * Check if the format is default HTML
334 *
335 * @since 2.05
336 *
337 * @return bool
338 */
339 protected function is_table_format() {
340 return ( $this->format === 'text' && $this->is_plain_text === false );
341 }
342 }
343