Completed
Branch 3.0 (991649)
by Stephanie
02:44
created

FrmFieldFormHtml::set_html()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @since 3.0
4
 */
5
6
class FrmFieldFormHtml {
7
8
	private $html;
9
10
	private $html_id;
11
12
	/**
13
	 *   @var FrmFieldType
14
	 */
15
	private $field_obj;
16
17
	private $field_id;
18
19
	private $form = array();
20
21
	private $pass_args = array();
22
23
	/**
24
	 * @since 3.0
25
	 *
26
	 * @param array $atts
27
	 */
28
	public function __construct( $atts ) {
29
		$this->_set( 'field_obj', $atts );
30
		$this->set_field_id( $atts );
31
		$this->_set( 'form', $atts );
32
		$this->_set( 'html_id', $atts );
33
		$this->set_html( $atts );
34
		$this->set_pass_args( $atts );
35
	}
36
37
	/**
38
	 * @since 3.0
39
	 *
40
	 * @param string $param
41
	 * @param array $atts
42
	 */
43
	private function _set( $param, $atts ) {
44
		if ( isset( $atts[ $param ] ) ) {
45
			$this->{$param} = $atts[ $param ];
46
		}
47
	}
48
49
	/**
50
	 * @since 3.0
51
	 *
52
	 * @param array $atts
53
	 */
54
	private function set_html( $atts ) {
55
		$this->set_from_field( $atts, array( 'param' => 'html', 'default' => 'custom_html' ) );
56
	}
57
58
	/**
59
	 * @since 3.0
60
	 *
61
	 * @param array $atts
62
	 */
63
	private function set_field_id( $atts ) {
64
		$this->set_from_field( $atts, array( 'param' => 'field_id', 'default' => 'id' ) );
65
	}
66
67
	/**
68
	 * @since 3.0
69
	 *
70
	 * @param array $atts
71
	 */
72
	private function set_pass_args( $atts ) {
73
		$this->pass_args = $atts;
74
		$exclude = array( 'field_obj', 'html' );
75
		
76
		foreach ( $exclude as $ex ) {
77
			if ( isset( $atts[ $ex ] ) ) {
78
				unset( $this->pass_args[ $ex ] );
79
			}
80
		}
81
	}
82
83
	/**
84
	 * @since 3.0
85
	 *
86
	 * @param array $atts
87
	 * @param array $set
88
	 */
89
	private function set_from_field( $atts, $set ) {
90
		if ( isset( $atts[ $set['param'] ] ) ) {
91
			$this->{$set['param']} = $atts[ $set['param'] ];
92
		} else {
93
			$this->{$set['param']} = $this->field_obj->get_field_column( $set['default'] );
94
		}
95
	}
96
97
	public function get_html() {
98
		$this->replace_shortcodes_before_input();
99
		$this->replace_shortcodes_with_atts();
100
		$this->replace_shortcodes_after_input();
101
102
		return $this->html;
103
	}
104
105
	/**
106
	 * @since 3.0
107
	 */
108
	private function replace_shortcodes_before_input() {
109
110
		// Remove the for attribute for captcha
111
		if ( $this->field_obj->get_field_column('type') == 'captcha' ) {
112
			$this->html = str_replace( ' for="field_[key]"', '', $this->html );
113
		}
114
115
		$this->replace_field_values();
116
		$this->add_class_to_divider();
117
118
		$this->replace_required_label_shortcode();
119
		$this->replace_required_class();
120
		$this->replace_description_shortcode();
121
		$this->replace_error_shortcode();
122
		$this->add_class_to_label();
123
		$this->add_field_div_classes();
124
125
		$this->replace_entry_key();
126
		$this->replace_form_shortcodes();
127
		$this->process_wp_shortcodes();
128
	}
129
130
	/**
131
	 * @since 3.0
132
	 */
133
	private function replace_field_values() {
134
		//replace [id]
135
		$this->html = str_replace( '[id]', $this->field_id, $this->html );
136
137
		// set the label for
138
		$this->html = str_replace( 'field_[key]', $this->html_id, $this->html );
139
140
		//replace [key]
141
		$this->html = str_replace( '[key]', $this->field_obj->get_field_column('field_key'), $this->html );
142
143
		//replace [field_name]
144
		$this->html = str_replace('[field_name]', $this->field_obj->get_field_column('name'), $this->html );
145
	}
146
147
	/**
148
	 * If field type is section heading, add class so a bottom margin
149
	 * can be added to either the h3 or description
150
	 *
151
	 * @since 3.0
152
	 */
153
	private function add_class_to_divider() {
154
		if ( $this->field_obj->get_field_column('type') == 'divider' ) {
155
			if ( FrmField::is_option_true( $this->field_obj->get_field(), 'description' ) ) {
156
				$this->html = str_replace( 'frm_description', 'frm_description frm_section_spacing', $this->html );
157
			} else {
158
				$this->html = str_replace( '[label_position]', '[label_position] frm_section_spacing', $this->html );
159
			}
160
		}
161
	}
162
163
	/**
164
	 * @since 3.0
165
	 */
166
	private function replace_required_label_shortcode() {
167
		$required = FrmField::is_required( $this->field_obj->get_field() ) ? $this->field_obj->get_field_column('required_indicator') : '';
168
		FrmShortcodeHelper::remove_inline_conditions( ! empty( $required ), 'required_label', $required, $this->html );
169
	}
170
171
	/**
172
	 * @since 3.0
173
	 */
174
	private function replace_description_shortcode() {
175
		$description = $this->field_obj->get_field_column('description');
176
		FrmShortcodeHelper::remove_inline_conditions( ( $description && $description != '' ), 'description', $description, $this->html );
177
	}
178
179
	/**
180
	 * @since 3.0
181
	 */
182
	private function replace_error_shortcode() {
183
		$error = isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ? $this->pass_args['errors'][ 'field' . $this->field_id ] : false;
184
		FrmShortcodeHelper::remove_inline_conditions( ! empty( $error ), 'error', $error, $this->html );
185
	}
186
187
	/**
188
	 * replace [required_class]
189
	 *
190
	 * @since 3.0
191
	 */
192
	private function replace_required_class() {
193
		$required_class = FrmField::is_required( $this->field_obj->get_field() ) ? ' frm_required_field' : '';
194
		$this->html = str_replace( '[required_class]', $required_class, $this->html );
195
	}
196
197
	/**
198
	 * @since 3.0
199
	 */
200
	private function replace_form_shortcodes() {
201
		if ( $this->form ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->form of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
202
			$form = (array) $this->form;
203
204
			//replace [form_key]
205
			$this->html = str_replace( '[form_key]', $form['form_key'], $this->html );
206
207
			//replace [form_name]
208
			$this->html = str_replace( '[form_name]', $form['name'], $this->html );
209
		}
210
	}
211
212
	/**
213
	 * @since 3.0
214
	 */
215
	public function replace_shortcodes_after_input() {
216
		$this->html .= "\n";
217
218
		//Return html if conf_field to prevent loop
219
		if ( $this->field_obj->get_field_column('conf_field') == 'stop' ) {
220
			return;
221
		}
222
223
		$this->filter_for_more_shortcodes();
224
		$this->filter_html_field_shortcodes();
225
		$this->remove_collapse_shortcode();
226
	}
227
228
	/**
229
	 * @since 3.0
230
	 */
231
	private function filter_for_more_shortcodes() {
232
		$atts = $this->pass_args;
233
234
		//If field is not in repeating section
235
		if ( empty( $atts['section_id'] ) ) {
236
			$atts = array( 'errors' => $this->pass_args['errors'], 'form' => $this->form );
237
		}
238
		$this->html = apply_filters( 'frm_replace_shortcodes', $this->html, $this->field_obj->get_field(), $atts );
239
	}
240
241
	/**
242
	 * @since 3.0
243
	 */
244
	private function filter_html_field_shortcodes() {
245
		if ( $this->field_obj->get_field_column('type') == 'html' ) {
246
			FrmFieldsHelper::run_wpautop( array( 'wpautop' => true ), $this->html );
247
248
			$this->html = apply_filters( 'frm_get_default_value', $this->html, (object) $this->field_obj->get_field(), false );
249
			$this->html = do_shortcode( $this->html );
250
		}
251
	}
252
253
	/**
254
	 * Remove [collapse_this] if it's still included after all processing
255
	 * @since 3.0
256
	 */
257
	private function remove_collapse_shortcode() {
258
		if ( preg_match( '/\[(collapse_this)\]/s', $this->html ) ) {
259
			$this->html = str_replace( '[collapse_this]', '', $this->html );
260
		}
261
	}
262
263
	/**
264
	 * @since 3.0
265
	 */
266
	private function replace_shortcodes_with_atts() {
267
		preg_match_all("/\[(input|deletelink)\b(.*?)(?:(\/))?\]/s", $this->html, $shortcodes, PREG_PATTERN_ORDER);
268
269
		foreach ( $shortcodes[0] as $short_key => $tag ) {
270
			$shortcode_atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[2][ $short_key ] );
271
			$tag = FrmShortcodeHelper::get_shortcode_tag( $shortcodes, $short_key, array( 'conditional' => false, 'conditional_check' => false ) );
272
273
			$replace_with = '';
274
275
			if ( $tag == 'deletelink' && FrmAppHelper::pro_is_installed() ) {
276
				$replace_with = FrmProEntriesController::entry_delete_link( $shortcode_atts );
277
			} elseif ( $tag == 'input' ) {
278
				$replace_with = $this->replace_input_shortcode( $shortcode_atts );
279
			}
280
281
			$this->html = str_replace( $shortcodes[0][ $short_key ], $replace_with, $this->html );
282
		}
283
	}
284
285
	/**
286
	 * @param array $shortcode_atts
287
	 *
288
	 * @return string
289
	 */
290
	private function replace_input_shortcode( $shortcode_atts ) {
291
		$shortcode_atts = $this->prepare_input_shortcode_atts( $shortcode_atts );
292
		return $this->field_obj->include_front_field_input( $this->pass_args, $shortcode_atts );
293
	}
294
295
	/**
296
	 * @param array $shortcode_atts
297
	 *
298
	 * @return array
299
	 */
300
	private function prepare_input_shortcode_atts( $shortcode_atts ) {
301
		if ( isset( $shortcode_atts['opt'] ) ) {
302
			$shortcode_atts['opt']--;
303
		}
304
305
		$field_class = isset( $shortcode_atts['class'] ) ? $shortcode_atts['class'] : '';
306
		$this->field_obj->set_field_column( 'input_class', $field_class );
307
308
		if ( isset( $shortcode_atts['class'] ) ) {
309
			unset( $shortcode_atts['class'] );
310
		}
311
312
		$this->field_obj->set_field_column( 'shortcodes', $shortcode_atts );
313
314
		return $shortcode_atts;
315
	}
316
317
	/**
318
	 * Add the label position class into the HTML
319
	 * If the label position is inside, add a class to show the label if the field has a value.
320
	 *
321
	 * @since 3.0
322
	 */
323
	private function add_class_to_label() {
324
		$label_class = in_array( $this->field_obj->get_field_column('type'), array( 'divider', 'end_divider', 'break' ) ) ? $this->field_obj->get_field_column('label') : ' frm_primary_label';
325
		$this->html = str_replace( '[label_position]', $label_class, $this->html );
326
		if ( $this->field_obj->get_field_column('label') == 'inside' && $this->field_obj->get_field_column('value') != '' ) {
327
			$this->html = str_replace( 'frm_primary_label', 'frm_primary_label frm_visible', $this->html );
328
		}
329
	}
330
331
	/**
332
	 * replace [entry_key]
333
	 *
334
	 * @since 3.0
335
	 */
336
	private function replace_entry_key() {
337
		$entry_key = FrmAppHelper::simple_get( 'entry', 'sanitize_title' );
338
		$this->html = str_replace( '[entry_key]', $entry_key, $this->html );
339
	}
340
341
	/**
342
	 * Add classes to a field div
343
	 *
344
	 * @since 3.0
345
	 */
346
	private function add_field_div_classes() {
347
		$classes = $this->get_field_div_classes();
348
349
		if ( $this->field_obj->get_field_column('type') == 'html' && strpos( $this->html, '[error_class]' ) === false ) {
350
			// there is no error_class shortcode for HTML fields
351
			$this->html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . $classes, $this->html );
352
		}
353
		$this->html = str_replace( '[error_class]', $classes, $this->html );
354
	}
355
356
357
	/**
358
	 * Get the classes for a field div
359
	 *
360
	 * @since 3.0
361
	 *
362
	 * @return string $classes
363
	 */
364
	private function get_field_div_classes() {
365
		// Add error class
366
		$classes = isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ? ' frm_blank_field' : '';
367
368
		// Add label position class
369
		$classes .= ' frm_' . $this->field_obj->get_field_column('label') . '_container';
370
371
		// Add CSS layout classes
372
		if ( ! empty( $this->field_obj->get_field_column('classes') ) ) {
373
			if ( ! strpos( $this->html, 'frm_form_field ') ) {
374
				$classes .= ' frm_form_field';
375
			}
376
			$classes .= ' ' . $this->field_obj->get_field_column('classes');
377
		}
378
379
		// Add class to HTML field
380
		if ( $this->field_obj->get_field_column('type') == 'html' ) {
381
			$classes .= ' frm_html_container';
382
		}
383
384
		// Get additional classes
385
		return apply_filters( 'frm_field_div_classes', $classes, $this->field_obj->get_field(), array( 'field_id' => $this->field_id ) );
386
	}
387
388
	/**
389
	 * This filters shortcodes in the field HTML
390
	 *
391
	 * @since 3.0
392
	 */
393
	private function process_wp_shortcodes() {
394
		if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
395
			$this->html = do_shortcode( $this->html );
396
		}
397
	}
398
}
399