PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26
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.26, at classes/models/FrmEntryShortcodeFormatter.php

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