Completed
Pull Request — 2.x (#4370)
by
unknown
05:02
created

PodsField_Link::add_link_modal()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 0
dl 0
loc 15
rs 8.8571
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
	 * Change the way the a list of values of the field are displayed with Pods::field
239
	 *
240
	 * @param mixed|null  $value
241
	 * @param string|null $name
242
	 * @param array|null  $options
243
	 * @param array|null  $pod
244
	 * @param int|null    $id
245
	 *
246
	 * @return mixed|null|string
247
	 *
248
	 * @since 2.7
249
	 */
250
	public function display_list( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
251
252
		return call_user_func_array( array( $this, 'display' ), func_get_args() );
253
254
	}
255
256
	/**
257
	 * Customize output of the form field
258
	 *
259
	 * @param string $name
260
	 * @param mixed  $value
261
	 * @param array  $options
262
	 * @param array  $pod
263
	 * @param int    $id
264
	 *
265
	 * @since 2.0
266
	 */
267
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
268
269
		$options         = (array) $options;
270
		$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...
271
		$field_type      = 'link';
272
273
		// Ensure proper format
274
		$value = $this->pre_save( $value, $id, $name, $options, null, $pod );
275
276
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
277
278
	}
279
280
	/**
281
	 * Validate a value before it's saved
282
	 *
283
	 * @param mixed  $value
284
	 * @param string $name
285
	 * @param array  $options
286
	 * @param array  $fields
287
	 * @param array  $pod
288
	 * @param int    $id
289
	 *
290
	 * @return bool|array
291
	 *
292
	 * @since 2.0
293
	 */
294
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
295
296
		$errors = array();
297
298
		$label = strip_tags( pods_var_raw( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
299
300
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
301
302
		$check = $check['url'];
303
304
		if ( is_array( $check ) ) {
305
			$errors = $check;
306
		} else {
307
			if ( ! empty( $value['url'] ) && 0 < strlen( $value['url'] ) && strlen( $check ) < 1 ) {
308
				if ( 1 == pods_var( 'required', $options ) ) {
309
					$errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
310
				} else {
311
					$errors[] = sprintf( __( 'Invalid link provided for the field %s.', 'pods' ), $label );
312
				}
313
			}
314
		}
315
316
		if ( ! empty( $errors ) ) {
317
			return $errors;
318
		}
319
320
		return true;
321
322
	}
323
324
	/**
325
	 * Change the value or perform actions after validation but before saving to the DB
326
	 *
327
	 * @param mixed  $value
328
	 * @param int    $id
329
	 * @param string $name
330
	 * @param array  $options
331
	 * @param array  $fields
332
	 * @param array  $pod
333
	 * @param object $params
334
	 *
335
	 * @return array
336
	 *
337
	 * @since 2.0
338
	 */
339
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
340
341
		$options = (array) $options;
342
343
		// Update from a single (non array) input field (like website) if the field updates
344
		if ( is_string( $value ) ) {
345
			$value = array( 'url' => $value );
346
		}
347
348
		$value = array_merge( array( 'url' => '', 'text' => '', 'target' => '' ), (array) $value );
349
350
		// Start URL format
351
		if ( ! empty( $value['url'] ) ) {
352
			$value['url'] = $this->validate_url( $value['url'], $options );
353
		}
354
355
		// Start Title format
356 View Code Duplication
		if ( ! empty( $value['text'] ) ) {
357
			$value['text'] = $this->strip_html( $value['text'], $options );
358
		}
359
360
		// Start Target format
361
		if ( ! empty( $value['target'] ) ) {
362
			$value['target'] = $this->validate_target( $value['target'] );
363
		} elseif ( ! isset( $value['target'] ) && 1 == pods_v( self::$type . '_new_window', $options, 0 ) ) {
364
			$value['target'] = '_blank';
365
		}
366
367
		return $value;
368
369
	}
370
371
	/**
372
	 * Init the editor needed for WP Link modal to work
373
	 */
374
	public function validate_link_modal() {
375
376
		static $init;
377
378
		if ( empty( $init ) ) {
379
			if ( ! did_action( 'wp_enqueue_editor' ) ) {
380
				add_action( 'shutdown', array( $this, 'add_link_modal' ) );
381
			}
382
		}
383
384
		$init = true;
385
386
	}
387
388
	/**
389
	 * Echo the link modal code
390
	 */
391
	public function add_link_modal() {
392
393
		if ( ! class_exists( '_WP_Editors', false ) && file_exists( ABSPATH . WPINC . '/class-wp-editor.php' ) ) {
394
			require_once( ABSPATH . WPINC . '/class-wp-editor.php' );
395
		}
396
397
		if ( class_exists( '_WP_Editors' ) && method_exists( '_WP_Editors', 'wp_link_dialog' ) ) {
398
			_WP_Editors::wp_link_dialog();
399
		} else {
400
			echo '<div style="display:none;">';
401
			wp_editor( '', 'pods-link-editor-hidden' );
402
			echo '</div>';
403
		}
404
405
	}
406
}
407