Completed
Pull Request — 2.x (#3632)
by Scott Kingsley
05:01
created

PodsField_Link::validate()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 8
nop 7
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 7 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require_once( PODS_DIR . 'classes/fields/website.php' );
3
4
/**
5
 * @package Pods\Fields
6
 */
7
class PodsField_Link extends PodsField_Website {
8
9
	/**
10
	 * Field Type Group
11
	 *
12
	 * @var string
13
	 * @since 2.0
14
	 */
15
	public static $group = 'Text';
16
17
	/**
18
	 * Field Type Identifier
19
	 *
20
	 * @var string
21
	 * @since 2.0
22
	 */
23
	public static $type = 'link';
24
25
	/**
26
	 * Field Type Label
27
	 *
28
	 * @var string
29
	 * @since 2.0
30
	 */
31
	public static $label = 'Link';
32
33
	/**
34
	 * Field Type Preparation
35
	 *
36
	 * @var string
37
	 * @since 2.0
38
	 */
39
	public static $prepare = '%s';
40
41
	/**
42
	 * Do things like register/enqueue scripts and stylesheets
43
	 *
44
	 * @since 2.0
45
	 */
46
	public function __construct() {
47
		self::$label = __( 'Link', 'pods' );
48
	}
49
50
	/**
51
	 * Add options and set defaults to
52
	 *
53
	 * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
54
	 *
55
	 * @since 2.0
56
	 */
57
	public function options() {
58
59
		$options = array(
60
			self::$type . '_format'            => array(
61
				'label'   => __( 'Format', 'pods' ),
62
				'default' => 'normal',
63
				'type'    => 'pick',
64
				'data'    => array(
65
					'none'              => __( 'No URL format restrictions', 'pods' ),
66
					'normal'            => __( 'http://example.com/', 'pods' ),
67
					'no-www'            => __( 'http://example.com/ (remove www)', 'pods' ),
68
					'force-www'         => __( 'http://www.example.com/ (force www if no sub-domain provided)', 'pods' ),
69
					'no-http'           => __( 'example.com', 'pods' ),
70
					'no-http-no-www'    => __( 'example.com (force removal of www)', 'pods' ),
71
					'no-http-force-www' => __( 'www.example.com (force www if no sub-domain provided)', 'pods' )
72
				)
73
			),
74
			self::$type . '_select_existing'   => array(
75
				'label'      => __( 'Enable Selecting from Existing Links?', 'pods' ),
76
				'default'    => 1,
77
				'type'       => 'boolean',
78
				'dependency' => true
79
			),
80
			'output_options'                   => array(
81
				'label' => __( 'Link Text Output Options', 'pods' ),
82
				'group' => array(
83
					self::$type . '_allow_shortcode' => array(
84
						'label'      => __( 'Allow Shortcodes?', 'pods' ),
85
						'default'    => 0,
86
						'type'       => 'boolean',
87
						'dependency' => true
88
					),
89
					self::$type . '_allow_html'      => array(
90
						'label'      => __( 'Allow HTML?', 'pods' ),
91
						'default'    => 0,
92
						'type'       => 'boolean',
93
						'dependency' => true
94
					)
95
				)
96
			),
97
			self::$type . '_allowed_html_tags' => array(
98
				'label'      => __( 'Allowed HTML Tags', 'pods' ),
99
				'depends-on' => array( self::$type . '_allow_html' => true ),
100
				'default'    => 'strong em a ul ol li b i',
101
				'type'       => 'text'
102
			),
103
			/*self::$type . '_max_length' => array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
				'label' => __( 'Maximum Length', 'pods' ),
105
				'default' => 255,
106
				'type' => 'number',
107
				'help' => __( 'Set to -1 for no limit', 'pods' )
108
			),*/
109
			self::$type . '_html5'             => array(
110
				'label'   => __( 'Enable HTML5 Input Field?', 'pods' ),
111
				'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
112
				'type'    => 'boolean'
113
			)/*,
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
			self::$type . '_size' => array(
115
				'label' => __( 'Field Size', 'pods' ),
116
				'default' => 'medium',
117
				'type' => 'pick',
118
				'data' => array(
119
					'small' => __( 'Small', 'pods' ),
120
					'medium' => __( 'Medium', 'pods' ),
121
					'large' => __( 'Large', 'pods' )
122
				)
123
			)*/
124
		);
125
126
		return $options;
127
128
	}
129
130
	/**
131
	 * Define the current field's schema for DB table storage
132
	 *
133
	 * @param array $options
134
	 *
135
	 * @return array
136
	 * @since 2.0
137
	 */
138
	public function schema( $options = null ) {
139
140
		$schema = 'LONGTEXT';
141
142
		return $schema;
143
144
	}
145
146
	/**
147
	 * Change the value of the field
148
	 *
149
	 * @param mixed  $value
150
	 * @param string $name
151
	 * @param array  $options
152
	 * @param array  $pod
153
	 * @param int    $id
154
	 *
155
	 * @return mixed|null|string
156
	 * @since 2.3
157
	 */
158
	public function value( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
159
160
		return $value;
161
162
	}
163
164
	/**
165
	 * Change the way the value of the field is displayed with Pods::get
166
	 *
167
	 * @param mixed  $value
168
	 * @param string $name
169
	 * @param array  $options
170
	 * @param array  $pod
171
	 * @param int    $id
172
	 *
173
	 * @return mixed|null|string
174
	 * @since 2.0
175
	 */
176
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
177
178
		// Validate for an array because display is also used for the get_post_meta filters along the function chain
179
		if ( ! is_array( $value ) ) {
180
			return $value;
181
		}
182
183
		// Ensure proper format
184
		$value = $this->pre_save( $value, $id, $name, $options, null, $pod );
185
186 View Code Duplication
		if ( ! empty( $value['text'] ) ) {
187
			$value['text'] = $this->strip_html( $value['text'], $options );
188
		}
189
190
		if ( ! empty( $value['url'] ) ) {
191
192
			$link = '<a href="%s"%s>%s</a>';
193
194
			// Build the URL
195
			$url = $this->build_url( parse_url( $value['url'] ) );
196
197
			// Display URL as text by default. If text provided, use the text input
198
			$text = $url;
199
200
			if ( ! empty( $value['text'] ) ) {
201
				$text = $value['text'];
202
			}
203
204
			$atts = '';
205
206
			if ( ! empty( $value['target'] ) ) {
207
				// Possible support for other targets in future
208
				$atts .= ' target="' . esc_attr( $value['target'] ) . '"';
209
			}
210
211
			// Do shortcodes if this is enabled
212
			if ( 1 == pods_var( self::$type . '_allow_shortcode', $options ) ) {
213
				$text = do_shortcode( $text );
214
			}
215
216
			// Return the value
217
			$value = sprintf( $link, esc_url( $url ), $atts, $text );
218
219
		} elseif ( ! empty( $value['text'] ) ) {
220
			// No URL data found (probably database error), return text is this is available
221
			$value = $value['text'];
222
		}
223
224
		// Return database value or display value if above conditions are met
225
		return $value;
226
227
	}
228
229
	/**
230
	 * Customize output of the form field
231
	 *
232
	 * @param string $name
233
	 * @param mixed  $value
234
	 * @param array  $options
235
	 * @param array  $pod
236
	 * @param int    $id
237
	 *
238
	 * @since 2.0
239
	 */
240
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
241
242
		$options         = (array) $options;
243
		$form_field_type = PodsForm::$field_type;
0 ignored issues
show
Bug introduced by
The property field_type cannot be accessed from this context as it is declared private in class PodsForm.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
244
		$field_type      = 'link';
245
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
246
247
	}
248
249
	/**
250
	 * Validate a value before it's saved
251
	 *
252
	 * @param mixed  $value
253
	 * @param string $name
254
	 * @param array  $options
255
	 * @param array  $fields
256
	 * @param array  $pod
257
	 * @param int    $id
258
	 *
259
	 * @since 2.0
260
	 */
261
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
262
263
		$errors = array();
264
265
		$label = strip_tags( pods_var_raw( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
266
267
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
268
269
		$check = $check['url'];
270
271
		if ( is_array( $check ) ) {
272
			$errors = $check;
273
		} else {
274
			if ( ! empty( $value['url'] ) && 0 < strlen( $value['url'] ) && strlen( $check ) < 1 ) {
275
				if ( 1 == pods_var( 'required', $options ) ) {
276
					$errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
277
				} else {
278
					$errors[] = sprintf( __( 'Invalid link provided for the field %s.', 'pods' ), $label );
279
				}
280
			}
281
		}
282
283
		if ( ! empty( $errors ) ) {
284
			return $errors;
285
		}
286
287
		return true;
288
289
	}
290
291
	/**
292
	 * Change the value or perform actions after validation but before saving to the DB
293
	 *
294
	 * @param mixed  $value
295
	 * @param int    $id
296
	 * @param string $name
297
	 * @param array  $options
298
	 * @param array  $fields
299
	 * @param array  $pod
300
	 * @param object $params
301
	 *
302
	 * @since 2.0
303
	 */
304
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
305
306
		$options = (array) $options;
307
308
		$value = array_merge( array( 'url' => '', 'text' => '', 'target' => '' ), (array) $value );
309
310
		// Start URL format
311
		if ( ! empty( $value['url'] ) ) {
312
			$value['url'] = $this->validate_url( $value['url'], $options );
313
		}
314
315
		// Start Title format
316 View Code Duplication
		if ( ! empty( $value['text'] ) ) {
317
			$value['text'] = $this->strip_html( $value['text'], $options );
318
		}
319
320
		// Start Target format
321
		if ( ! empty( $value['target'] ) ) {
322
			$value['target'] = $this->validate_target( $value['target'], $options );
323
		}
324
325
		return $value;
326
327
	}
328
329
	/**
330
	 * Init the editor needed for WP Link modal to work
331
	 */
332
	public function validate_link_modal() {
333
334
		static $init;
335
336
		if ( empty( $init ) ) {
337
			if ( ! did_action( 'wp_enqueue_editor' ) ) {
338
				add_action( 'shutdown', array( $this, 'add_link_modal' ) );
339
			}
340
		}
341
342
		$init = true;
343
344
	}
345
346
	/**
347
	 * Echo the link modal code
348
	 */
349
	public function add_link_modal() {
350
351
		if ( ! class_exists( '_WP_Editors', false ) && file_exists( ABSPATH . WPINC . '/class-wp-editor.php' ) ) {
352
			require_once( ABSPATH . WPINC . '/class-wp-editor.php' );
353
		}
354
355
		if ( class_exists( '_WP_Editors' ) && method_exists( '_WP_Editors', 'wp_link_dialog' ) ) {
356
			_WP_Editors::wp_link_dialog();
357
		} else {
358
			echo '<div style="display:none;">';
359
			wp_editor( '', 'pods-link-editor-hidden' );
360
			echo '</div>';
361
		}
362
	
363
	}
364
}
365