Completed
Push — master ( 2cd109...e24d60 )
by Jamie
02:45
created

FrmEntryShortcodeFormatter::content()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 4
nop 0
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
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' );
19
20
	/**
21
	 * @var array
22
	 * @since 2.04
23
	 */
24
	protected $fields = array();
25
26
	/**
27
	 * @var bool
28
	 * @since 2.04.02
29
	 */
30
	protected $is_plain_text = false;
31
32
	/**
33
	 * @var string
34
	 * @since 2.04
35
	 */
36
	protected $format = 'text';
37
38
	/**
39
	 * @var FrmTableHTMLGenerator
40
	 * @since 2.04
41
	 */
42
	protected $table_generator = null;
43
44
	/**
45
	 * @var array
46
	 * @since 2.04
47
	 */
48
	protected $array_content = array();
49
50
	/**
51
	 * FrmEntryShortcodeFormatter constructor
52
	 *
53
	 * @param int|string $form_id
54
	 * @param array $atts
55
	 */
56
	public function __construct( $form_id, $atts ) {
57
		if ( ! $form_id ) {
58
			return;
59
		}
60
61
		$this->init_form_id( $form_id );
62
		$this->init_fields();
63
64
		if ( empty( $this->fields ) ) {
65
			return;
66
		}
67
68
		$this->init_plain_text( $atts );
69
		$this->init_format( $atts );
70
71
		if ( $this->is_table_format() ) {
72
			$this->init_table_generator();
73
		}
74
	}
75
76
	/**
77
	 * Initialize the form_id property
78
	 *
79
	 * @since 2.04
80
	 * @param $form_id
81
	 */
82
	protected function init_form_id( $form_id ) {
83
		$this->form_id = (int) $form_id;
84
	}
85
86
	/**
87
	 * Initialize the fields property
88
	 *
89
	 * @since 2.04
90
	 */
91
	protected function init_fields() {
92
		$this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' );
93
	}
94
95
	/**
96
	 * Initialize the is_plain_text property
97
	 *
98
	 * @since 2.04.02
99
	 *
100
	 * @param array $atts
101
	 */
102
	protected function init_plain_text( $atts ) {
103
		if ( isset( $atts['plain_text'] ) && $atts['plain_text'] ) {
104
			$this->is_plain_text = true;
105
		}
106
	}
107
108
	/**
109
	 * Initialize the format property
110
	 *
111
	 * @since 2.04
112
	 *
113
	 * @param array $atts
114
	 */
115
	protected function init_format( $atts ) {
116
		if ( ! isset( $atts['format'] ) ) {
117
			$this->format = 'table';
118 View Code Duplication
		} else if ( $atts['format'] == 'array' ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
			$this->format = 'array';
120
		} else if ( $atts['format'] == 'text' ) {
121
122
			if ( $this->is_plain_text === true ) {
123
				$this->format = 'plain_text_block';
124
			} else {
125
				$this->format = 'table';
126
			}
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
		} else if ( $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.04.02
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
	 * @return array
289
	 */
290
	protected function add_single_field_array( $field, $value ) {
291
		$array = array(
292
			'label' => '[' . $field->id . ' show=field_label]',
293
			'val'   => '[' . $value . ']',
294
			'type'  => $field->type,
295
		);
296
297
		$this->array_content[ $field->id ] = apply_filters( 'frm_field_shortcodes_for_default_html_email', $array, $field );
298
299
		return $array;
300
	}
301
302
	/**
303
	 * Check if the format is default plain text
304
	 *
305
	 * @since 2.04.02
306
	 *
307
	 * @return bool
308
	 */
309
	protected function is_plain_text_format() {
310
		return $this->format === 'plain_text_block';
311
	}
312
313
	/**
314
	 * Check if the format is default HTML
315
	 *
316
	 * @since 2.04.02
317
	 *
318
	 * @return bool
319
	 */
320
	protected function is_table_format() {
321
		return $this->format === 'table';
322
	}
323
324
}