Completed
Pull Request — 2.x (#4569)
by Scott Kingsley
08:49
created

PodsField_WYSIWYG::strip_html()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package Pods\Fields
5
 */
6
class PodsField_WYSIWYG extends PodsField {
7
8
	/**
9
	 * {@inheritdoc}
10
	 */
11
	public static $group = 'Paragraph';
12
13
	/**
14
	 * {@inheritdoc}
15
	 */
16
	public static $type = 'wysiwyg';
17
18
	/**
19
	 * {@inheritdoc}
20
	 */
21
	public static $label = 'WYSIWYG (Visual Editor)';
22
23
	/**
24
	 * {@inheritdoc}
25
	 */
26
	public static $prepare = '%s';
27
28
	/**
29
	 * {@inheritdoc}
30
	 */
31
	public function setup() {
32
33
		self::$label = __( 'WYSIWYG (Visual Editor)', 'pods' );
34
	}
35
36
	/**
37
	 * {@inheritdoc}
38
	 */
39
	public function options() {
40
41
		$options = array(
42
			static::$type . '_repeatable'        => array(
43
				'label'             => __( 'Repeatable Field', 'pods' ),
44
				'default'           => 0,
45
				'type'              => 'boolean',
46
				'help'              => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),
47
				'boolean_yes_label' => '',
48
				'dependency'        => true,
49
				'developer_mode'    => true,
50
			),
51
			static::$type . '_editor'            => array(
52
				'label'      => __( 'Editor', 'pods' ),
53
				'default'    => 'tinymce',
54
				'type'       => 'pick',
55
				'data'       => apply_filters(
56
					'pods_form_ui_field_wysiwyg_editors', array(
57
						'tinymce'  => __( 'TinyMCE (WP Default)', 'pods' ),
58
						'cleditor' => __( 'CLEditor', 'pods' ),
59
					)
60
				),
61
				'dependency' => true,
62
			),
63
			'editor_options'                     => array(
64
				'label'      => __( 'Editor Options', 'pods' ),
65
				'depends-on' => array( static::$type . '_editor' => 'tinymce' ),
66
				'group'      => array(
67
					static::$type . '_media_buttons' => array(
68
						'label'   => __( 'Enable Media Buttons?', 'pods' ),
69
						'default' => 1,
70
						'type'    => 'boolean',
71
					),
72
				),
73
			),
74
			'output_options'                     => array(
75
				'label' => __( 'Output Options', 'pods' ),
76
				'group' => array(
77
					static::$type . '_oembed'          => array(
78
						'label'   => __( 'Enable oEmbed?', 'pods' ),
79
						'default' => 0,
80
						'type'    => 'boolean',
81
						'help'    => array(
82
							__( 'Embed videos, images, tweets, and other content.', 'pods' ),
83
							'http://codex.wordpress.org/Embeds',
84
						),
85
					),
86
					static::$type . '_wptexturize'     => array(
87
						'label'   => __( 'Enable wptexturize?', 'pods' ),
88
						'default' => 1,
89
						'type'    => 'boolean',
90
						'help'    => array(
91
							__( 'Transforms less-beautfiul text characters into stylized equivalents.', 'pods' ),
92
							'http://codex.wordpress.org/Function_Reference/wptexturize',
93
						),
94
					),
95
					static::$type . '_convert_chars'   => array(
96
						'label'   => __( 'Enable convert_chars?', 'pods' ),
97
						'default' => 1,
98
						'type'    => 'boolean',
99
						'help'    => array(
100
							__( 'Converts text into valid XHTML and Unicode', 'pods' ),
101
							'http://codex.wordpress.org/Function_Reference/convert_chars',
102
						),
103
					),
104
					static::$type . '_wpautop'         => array(
105
						'label'   => __( 'Enable wpautop?', 'pods' ),
106
						'default' => 1,
107
						'type'    => 'boolean',
108
						'help'    => array(
109
							__( 'Changes double line-breaks in the text into HTML paragraphs', 'pods' ),
110
							'http://codex.wordpress.org/Function_Reference/wpautop',
111
						),
112
					),
113
					static::$type . '_allow_shortcode' => array(
114
						'label'      => __( 'Allow Shortcodes?', 'pods' ),
115
						'default'    => 0,
116
						'type'       => 'boolean',
117
						'dependency' => true,
118
						'help'       => array(
119
							__( 'Embed [shortcodes] that help transform your static content into dynamic content.', 'pods' ),
120
							'http://codex.wordpress.org/Shortcode_API',
121
						),
122
					),
123
				),
124
			),
125
			static::$type . '_allowed_html_tags' => array(
126
				'label'   => __( 'Allowed HTML Tags', 'pods' ),
127
				'default' => '',
128
				'type'    => 'text',
129
				'help'    => __( 'Format: strong em a ul ol li b i', 'pods' ),
130
			),
131
		);
132
133
		if ( function_exists( 'Markdown' ) ) {
134
			$options['output_options']['group'][ static::$type . '_allow_markdown' ] = array(
135
				'label'   => __( 'Allow Markdown Syntax?', 'pods' ),
136
				'default' => 0,
137
				'type'    => 'boolean',
138
			);
139
		}
140
141
		return $options;
142
	}
143
144
	/**
145
	 * {@inheritdoc}
146
	 */
147
	public function schema( $options = null ) {
148
149
		$schema = 'LONGTEXT';
150
151
		return $schema;
152
	}
153
154
	/**
155
	 * {@inheritdoc}
156
	 */
157
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
158
159
		$value = $this->strip_html( $value, $options );
160
161
		if ( 1 === (int) pods_v( static::$type . '_oembed', $options, 0 ) ) {
162
			$post_temp = false;
163
164
			// Workaround for WP_Embed since it needs a $post to work from
165
			if ( 'post_type' === pods_v( 'type', $pod ) && 0 < $id && ( ! isset( $GLOBALS['post'] ) || empty( $GLOBALS['post'] ) ) ) {
166
				$post_temp = true;
167
168
				// @codingStandardsIgnoreLine
169
				$GLOBALS['post'] = get_post( $id );
170
			}
171
172
			/**
173
			 * @var $embed WP_Embed
174
			 */
175
			$embed = $GLOBALS['wp_embed'];
176
			$value = $embed->run_shortcode( $value );
177
			$value = $embed->autoembed( $value );
178
179
			// Cleanup after ourselves
180
			if ( $post_temp ) {
181
				// @codingStandardsIgnoreLine
182
				$GLOBALS['post'] = null;
183
			}
184
		}//end if
185
186
		if ( 1 === (int) pods_v( static::$type . '_wptexturize', $options, 1 ) ) {
187
			$value = wptexturize( $value );
188
		}
189
190
		if ( 1 === (int) pods_v( static::$type . '_convert_chars', $options, 1 ) ) {
191
			$value = convert_chars( $value );
192
		}
193
194
		if ( 1 === (int) pods_v( static::$type . '_wpautop', $options, 1 ) ) {
195
			$value = wpautop( $value );
196
		}
197
198
		if ( 1 === (int) pods_v( static::$type . '_allow_shortcode', $options, 0 ) ) {
199
			if ( 1 === (int) pods_v( static::$type . '_wpautop', $options, 1 ) ) {
200
				$value = shortcode_unautop( $value );
201
			}
202
203
			$value = do_shortcode( $value );
204
		}
205
206
		if ( function_exists( 'Markdown' ) && 1 === (int) pods_v( static::$type . '_allow_markdown', $options ) ) {
207
			$value = Markdown( $value );
208
		}
209
210
		return $value;
211
	}
212
213
	/**
214
	 * {@inheritdoc}
215
	 */
216
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
217
218
		$options         = (array) $options;
219
		$form_field_type = PodsForm::$field_type;
220
221
		if ( is_array( $value ) ) {
222
			$value = implode( "\n", $value );
223
		}
224
225
		if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) {
226
			if ( pods_v( 'read_only', $options, false ) ) {
227
				$options['readonly'] = true;
228
229
				$field_type = 'textarea';
230
			} else {
231
				return;
232
			}
233
		} elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
234
			$options['readonly'] = true;
235
236
			$field_type = 'textarea';
237
		} elseif ( 'tinymce' === pods_v( static::$type . '_editor', $options ) ) {
238
			$field_type = 'tinymce';
239
		} elseif ( 'cleditor' === pods_v( static::$type . '_editor', $options ) ) {
240
			$field_type = 'cleditor';
241
		} else {
242
			// Support custom WYSIWYG integration
243
			do_action( 'pods_form_ui_field_wysiwyg_' . pods_v( static::$type . '_editor', $options ), $name, $value, $options, $pod, $id );
244
			do_action( 'pods_form_ui_field_wysiwyg', pods_v( static::$type . '_editor', $options ), $name, $value, $options, $pod, $id );
245
246
			return;
247
		}//end if
248
249
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
250
	}
251
252
	/**
253
	 * {@inheritdoc}
254
	 */
255
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
256
257
		$value = $this->strip_html( $value, $options );
258
259
		$length = (int) pods_v( static::$type . '_max_length', $options, 0 );
260
261
		if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
262
			$value = pods_mb_substr( $value, 0, $length );
263
		}
264
265
		return $value;
266
	}
267
268
	/**
269
	 * {@inheritdoc}
270
	 */
271
	public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
272
273
		$value = $this->strip_html( $value, $options );
274
275
		$value = wp_trim_words( $value );
276
277
		return $value;
278
	}
279
280
	/**
281
	 * {@inheritdoc}
282
	 */
283
	public function strip_html( $value, $options = null ) {
284
285
		$options = (array) $options;
286
287
		// Allow HTML tags.
288
		$options[ static::$type . '_allow_html' ] = 1;
289
290
		return parent::strip_html( $value, $options );
291
	}
292
}
293