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

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