PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.34
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.34
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 6.5.4 All 140 releases
formidable / classes / models / FrmEntryShortcodeFormatter.php

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

349 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 *
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 ( ! $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 ( ! empty( $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 || ! $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
220 return $content . $this->table_generator->generate_table_footer();
221 }
222
223 /**
224 * Generate the content for all fields
225 *
226 * @since 2.05
227 *
228 * @return string
229 */
230 protected function generate_content_for_all_fields() {
231 $content = '';
232
233 foreach ( $this->fields as $field ) {
234 $content .= $this->generate_field_content( $field );
235 }
236
237 return $content;
238 }
239
240 /**
241 * Generate a field's HTML or plain text shortcodes
242 *
243 * @since 2.04
244 *
245 * @param stdClass $field
246 *
247 * @return string
248 */
249 protected function generate_field_content( $field ) {
250 if ( in_array( $field->type, $this->skip_fields, true ) ) {
251 return '';
252 }
253
254 return $this->generate_two_cell_shortcode_row( $field );
255 }
256
257 /**
258 * Generate a two cell row of shortcodes for an HTML or plain text table
259 *
260 * @since 2.04
261 *
262 * @param stdClass $field
263 * @param mixed $value
264 *
265 * @return string
266 */
267 protected function generate_two_cell_shortcode_row( $field, $value = null ) {
268 $row = '[if ' . $field->id . ']';
269 $label = '[' . $field->id . ' show=field_label]';
270
271 if ( $value === null ) {
272 $value = '[' . $field->id . ']';
273 }
274
275 if ( $this->is_plain_text_format() ) {
276 $row .= $label . ': ' . $value . "\r\n";
277 } else {
278 $row .= $this->table_generator->generate_two_cell_table_row( $label, $value );
279 }
280
281 $row .= '[/if ' . $field->id . ']';
282
283 if ( $this->is_table_format() ) {
284 $row .= "\r\n";
285 }
286
287 return $row;
288 }
289
290 /**
291 * Generate a field's array for the default HTML array
292 *
293 * @since 2.04
294 *
295 * @param stdClass $field
296 *
297 * @return void
298 */
299 protected function add_field_array( $field ) {
300 if ( in_array( $field->type, $this->skip_fields, true ) ) {
301 return;
302 }
303
304 $this->add_single_field_array( $field, $field->id );
305 }
306
307 /**
308 * Generate a single field's array
309 *
310 * @since 2.04
311 *
312 * @param stdClass $field
313 * @param string $value
314 *
315 * @return void
316 */
317 protected function add_single_field_array( $field, $value ) {
318 $array = array(
319 'label' => '[' . $field->id . ' show=field_label]',
320 'val' => '[' . $value . ']',
321 'type' => $field->type,
322 );
323
324 $this->array_content[ $field->id ] = apply_filters( 'frm_field_shortcodes_for_default_html_email', $array, $field );
325 }
326
327 /**
328 * Check if the format is default plain text
329 *
330 * @since 2.05
331 *
332 * @return bool
333 */
334 protected function is_plain_text_format() {
335 return $this->format === 'text' && $this->is_plain_text === true;
336 }
337
338 /**
339 * Check if the format is default HTML
340 *
341 * @since 2.05
342 *
343 * @return bool
344 */
345 protected function is_table_format() {
346 return $this->format === 'text' && $this->is_plain_text === false;
347 }
348 }
349