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

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