Completed
Pull Request — 2.x (#4187)
by Scott Kingsley
06:00
created

PodsField_Link::pre_save()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 31
Code Lines 14

Duplication

Lines 3
Ratio 9.68 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 24
nop 7
dl 3
loc 31
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
			self::$type . '_new_window'      => array(
81
				'label'      => __( 'Open link in new window by default?', 'pods' ),
82
				'default'    => apply_filters( 'pods_form_ui_field_link_new_window', 0, self::$type ),
83
				'type'       => 'boolean',
84
				'dependency' => false
85
			),
86
			'output_options'                   => array(
87
				'label' => __( 'Link Text Output Options', 'pods' ),
88
				'group' => array(
89
					self::$type . '_allow_shortcode' => array(
90
						'label'      => __( 'Allow Shortcodes?', 'pods' ),
91
						'default'    => 0,
92
						'type'       => 'boolean',
93
						'dependency' => true
94
					),
95
					self::$type . '_allow_html'      => array(
96
						'label'      => __( 'Allow HTML?', 'pods' ),
97
						'default'    => 0,
98
						'type'       => 'boolean',
99
						'dependency' => true
100
					)
101
				)
102
			),
103
			self::$type . '_allowed_html_tags' => array(
104
				'label'      => __( 'Allowed HTML Tags', 'pods' ),
105
				'depends-on' => array( self::$type . '_allow_html' => true ),
106
				'default'    => 'strong em a ul ol li b i',
107
				'type'       => 'text'
108
			),
109
			/*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...
110
				'label' => __( 'Maximum Length', 'pods' ),
111
				'default' => 255,
112
				'type' => 'number',
113
				'help' => __( 'Set to -1 for no limit', 'pods' )
114
			),*/
115
			self::$type . '_html5'             => array(
116
				'label'   => __( 'Enable HTML5 Input Field?', 'pods' ),
117
				'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
118
				'type'    => 'boolean'
119
			)/*,
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...
120
			self::$type . '_size' => array(
121
				'label' => __( 'Field Size', 'pods' ),
122
				'default' => 'medium',
123
				'type' => 'pick',
124
				'data' => array(
125
					'small' => __( 'Small', 'pods' ),
126
					'medium' => __( 'Medium', 'pods' ),
127
					'large' => __( 'Large', 'pods' )
128
				)
129
			)*/
130
		);
131
132
		return $options;
133
134
	}
135
136
	/**
137
	 * Define the current field's schema for DB table storage
138
	 *
139
	 * @param array $options
140
	 *
141
	 * @return string
142
	 * @since 2.0
143
	 */
144
	public function schema( $options = null ) {
145
146
		$schema = 'LONGTEXT';
147
148
		return $schema;
149
150
	}
151
152
	/**
153
	 * Change the value of the field
154
	 *
155
	 * @param mixed  $value
156
	 * @param string $name
157
	 * @param array  $options
158
	 * @param array  $pod
159
	 * @param int    $id
160
	 *
161
	 * @return mixed|null|string
162
	 * @since 2.3
163
	 */
164
	public function value( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
165
166
		return $value;
167
168
	}
169
170
	/**
171
	 * Change the way the value of the field is displayed with Pods::get
172
	 *
173
	 * @param mixed  $value
174
	 * @param string $name
175
	 * @param array  $options
176
	 * @param array  $pod
177
	 * @param int    $id
178
	 *
179
	 * @return mixed|null|string
180
	 * @since 2.0
181
	 */
182
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
183
184
		// Validate for an array because display is also used for the get_post_meta filters along the function chain
185
		if ( ! is_array( $value ) ) {
186
			return $value;
187
		}
188
189
		// Ensure proper format
190
		$value = $this->pre_save( $value, $id, $name, $options, null, $pod );
191
192 View Code Duplication
		if ( ! empty( $value['text'] ) ) {
193
			$value['text'] = $this->strip_html( $value['text'], $options );
194
		}
195
196
		if ( ! empty( $value['url'] ) ) {
197
198
			$link = '<a href="%s"%s>%s</a>';
199
200
			// Build the URL
201
			$url = $this->build_url( parse_url( $value['url'] ) );
202
203
			// Display URL as text by default. If text provided, use the text input
204
			$text = $url;
205
206
			if ( ! empty( $value['text'] ) ) {
207
				$text = $value['text'];
208
			}
209
210
			$atts = '';
211
212
			if ( ! empty( $value['target'] ) ||
213
			     ( ! isset( $value['target'] ) && 1 == pods_var( self::$type . '_new_window', $options ) )
214
			) {
215
				// Possible support for other targets in future
216
				$atts .= ' target="' . esc_attr( $value['target'] ) . '"';
217
			}
218
219
			// Do shortcodes if this is enabled
220
			if ( 1 == pods_var( self::$type . '_allow_shortcode', $options ) ) {
221
				$text = do_shortcode( $text );
222
			}
223
224
			// Return the value
225
			$value = sprintf( $link, esc_url( $url ), $atts, $text );
226
227
		} elseif ( ! empty( $value['text'] ) ) {
228
			// No URL data found (probably database error), return text is this is available
229
			$value = $value['text'];
230
		}
231
232
		// Return database value or display value if above conditions are met
233
		return $value;
234
235
	}
236
237
	/**
238
	 * Customize output of the form field
239
	 *
240
	 * @param string $name
241
	 * @param mixed  $value
242
	 * @param array  $options
243
	 * @param array  $pod
244
	 * @param int    $id
245
	 *
246
	 * @since 2.0
247
	 */
248
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
249
250
		$options         = (array) $options;
251
		$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...
252
		$field_type      = 'link';
253
254
		// Ensure proper format
255
		$value = $this->pre_save( $value, $id, $name, $options, null, $pod );
256
257
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
258
259
	}
260
261
	/**
262
	 * Validate a value before it's saved
263
	 *
264
	 * @param mixed  $value
265
	 * @param string $name
266
	 * @param array  $options
267
	 * @param array  $fields
268
	 * @param array  $pod
269
	 * @param int    $id
270
	 *
271
	 * @return bool|array
272
	 *
273
	 * @since 2.0
274
	 */
275
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
276
277
		$errors = array();
278
279
		$label = strip_tags( pods_var_raw( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
280
281
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
282
283
		$check = $check['url'];
284
285
		if ( is_array( $check ) ) {
286
			$errors = $check;
287
		} else {
288
			if ( ! empty( $value['url'] ) && 0 < strlen( $value['url'] ) && strlen( $check ) < 1 ) {
289
				if ( 1 == pods_var( 'required', $options ) ) {
290
					$errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
291
				} else {
292
					$errors[] = sprintf( __( 'Invalid link provided for the field %s.', 'pods' ), $label );
293
				}
294
			}
295
		}
296
297
		if ( ! empty( $errors ) ) {
298
			return $errors;
299
		}
300
301
		return true;
302
303
	}
304
305
	/**
306
	 * Change the value or perform actions after validation but before saving to the DB
307
	 *
308
	 * @param mixed  $value
309
	 * @param int    $id
310
	 * @param string $name
311
	 * @param array  $options
312
	 * @param array  $fields
313
	 * @param array  $pod
314
	 * @param object $params
315
	 *
316
	 * @return array
317
	 *
318
	 * @since 2.0
319
	 */
320
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
321
322
		$options = (array) $options;
323
324
		// Update from a single (non array) input field (like website) if the field updates
325
		if ( is_string( $value ) ) {
326
			$value = array( 'url' => $value );
327
		}
328
329
		$value = array_merge( array( 'url' => '', 'text' => '', 'target' => '' ), (array) $value );
330
331
		// Start URL format
332
		if ( ! empty( $value['url'] ) ) {
333
			$value['url'] = $this->validate_url( $value['url'], $options );
334
		}
335
336
		// Start Title format
337 View Code Duplication
		if ( ! empty( $value['text'] ) ) {
338
			$value['text'] = $this->strip_html( $value['text'], $options );
339
		}
340
341
		// Start Target format
342
		if ( ! empty( $value['target'] ) ) {
343
			$value['target'] = $this->validate_target( $value['target'] );
344
		} elseif ( ! isset( $value['target'] ) && 1 == pods_v( self::$type . '_new_window', $options, 0 ) ) {
345
			$value['target'] = '_blank';
346
		}
347
348
		return $value;
349
350
	}
351
352
	/**
353
	 * Init the editor needed for WP Link modal to work
354
	 */
355
	public function validate_link_modal() {
356
357
		static $init;
358
359
		if ( empty( $init ) ) {
360
			if ( ! did_action( 'wp_enqueue_editor' ) ) {
361
				add_action( 'shutdown', array( $this, 'add_link_modal' ) );
362
			}
363
		}
364
365
		$init = true;
366
367
	}
368
369
	/**
370
	 * Echo the link modal code
371
	 */
372
	public function add_link_modal() {
373
374
		if ( ! class_exists( '_WP_Editors', false ) && file_exists( ABSPATH . WPINC . '/class-wp-editor.php' ) ) {
375
			require_once( ABSPATH . WPINC . '/class-wp-editor.php' );
376
		}
377
378
		if ( class_exists( '_WP_Editors' ) && method_exists( '_WP_Editors', 'wp_link_dialog' ) ) {
379
			_WP_Editors::wp_link_dialog();
380
		} else {
381
			echo '<div style="display:none;">';
382
			wp_editor( '', 'pods-link-editor-hidden' );
383
			echo '</div>';
384
		}
385
386
	}
387
}
388