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

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