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

PodsField_Link::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
	 * {@inheritdoc}
11
	 */
12
	public static $group = 'Text';
13
14
	/**
15
	 * {@inheritdoc}
16
	 */
17
	public static $type = 'link';
18
19
	/**
20
	 * {@inheritdoc}
21
	 */
22
	public static $label = 'Link';
23
24
	/**
25
	 * {@inheritdoc}
26
	 */
27
	public static $prepare = '%s';
28
29
	/**
30
	 * {@inheritdoc}
31
	 */
32
	public function setup() {
33
34
		self::$label = __( 'Link', 'pods' );
35
	}
36
37
	/**
38
	 * {@inheritdoc}
39
	 */
40
	public function options() {
41
42
		$options = array(
43
			static::$type . '_format'            => array(
44
				'label'   => __( 'Format', 'pods' ),
45
				'default' => 'normal',
46
				'type'    => 'pick',
47
				'data'    => array(
48
					'none'              => __( 'No URL format restrictions', 'pods' ),
49
					'normal'            => __( 'http://example.com/', 'pods' ),
50
					'no-www'            => __( 'http://example.com/ (remove www)', 'pods' ),
51
					'force-www'         => __( 'http://www.example.com/ (force www if no sub-domain provided)', 'pods' ),
52
					'no-http'           => __( 'example.com', 'pods' ),
53
					'no-http-no-www'    => __( 'example.com (force removal of www)', 'pods' ),
54
					'no-http-force-www' => __( 'www.example.com (force www if no sub-domain provided)', 'pods' ),
55
				),
56
			),
57
			static::$type . '_select_existing'   => array(
58
				'label'      => __( 'Enable Selecting from Existing Links?', 'pods' ),
59
				'default'    => 1,
60
				'type'       => 'boolean',
61
				'dependency' => true,
62
			),
63
			static::$type . '_new_window'        => array(
64
				'label'      => __( 'Open link in new window by default?', 'pods' ),
65
				'default'    => apply_filters( 'pods_form_ui_field_link_new_window', 0, static::$type ),
66
				'type'       => 'boolean',
67
				'dependency' => false,
68
			),
69
			'output_options'                     => array(
70
				'label' => __( 'Link Text Output Options', 'pods' ),
71
				'group' => array(
72
					static::$type . '_allow_shortcode' => array(
73
						'label'      => __( 'Allow Shortcodes?', 'pods' ),
74
						'default'    => 0,
75
						'type'       => 'boolean',
76
						'dependency' => true,
77
					),
78
					static::$type . '_allow_html'      => array(
79
						'label'      => __( 'Allow HTML?', 'pods' ),
80
						'default'    => 0,
81
						'type'       => 'boolean',
82
						'dependency' => true,
83
					),
84
				),
85
			),
86
			static::$type . '_allowed_html_tags' => array(
87
				'label'      => __( 'Allowed HTML Tags', 'pods' ),
88
				'depends-on' => array( static::$type . '_allow_html' => true ),
89
				'default'    => 'strong em a ul ol li b i',
90
				'type'       => 'text',
91
			),
92
			static::$type . '_html5'             => array(
93
				'label'   => __( 'Enable HTML5 Input Field?', 'pods' ),
94
				'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ),
95
				'type'    => 'boolean',
96
			),
97
		);
98
99
		return $options;
100
101
	}
102
103
	/**
104
	 * {@inheritdoc}
105
	 */
106
	public function schema( $options = null ) {
107
108
		$schema = 'LONGTEXT';
109
110
		return $schema;
111
112
	}
113
114
	/**
115
	 * {@inheritdoc}
116
	 */
117
	public function value( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
118
119
		return $value;
120
121
	}
122
123
	/**
124
	 * {@inheritdoc}
125
	 */
126
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
127
128
		// Validate for an array because display is also used for the get_post_meta filters along the function chain
129
		if ( ! is_array( $value ) ) {
130
			return $value;
131
		}
132
133
		// Ensure proper format
134
		$value = $this->pre_save( $value, $id, $name, $options, null, $pod );
135
136 View Code Duplication
		if ( ! empty( $value['text'] ) ) {
137
			$value['text'] = $this->strip_html( $value['text'], $options );
138
		}
139
140
		if ( ! empty( $value['url'] ) ) {
141
142
			$link = '<a href="%s"%s>%s</a>';
143
144
			// Build the URL
145
			$url = $this->build_url( wp_parse_url( $value['url'] ) );
146
147
			// Display URL as text by default. If text provided, use the text input
148
			$text = $url;
149
150
			if ( ! empty( $value['text'] ) ) {
151
				$text = $value['text'];
152
			}
153
154
			$atts = '';
155
156
			if ( ! empty( $value['target'] ) || ( ! isset( $value['target'] ) && 1 === (int) pods_v( static::$type . '_new_window', $options ) ) ) {
157
				// Possible support for other targets in future
158
				$atts .= ' target="' . esc_attr( $value['target'] ) . '"';
159
			}
160
161
			// Do shortcodes if this is enabled
162
			if ( 1 === (int) pods_v( static::$type . '_allow_shortcode', $options ) ) {
163
				$text = do_shortcode( $text );
164
			}
165
166
			// Return the value
167
			$value = sprintf( $link, esc_url( $url ), $atts, $text );
168
169
		} elseif ( ! empty( $value['text'] ) ) {
170
			// No URL data found (probably database error), return text is this is available
171
			$value = $value['text'];
172
		}//end if
173
174
		// Return database value or display value if above conditions are met
175
		return $value;
176
177
	}
178
179
	/**
180
	 * Change the way the a list of values of the field are displayed with Pods::field
181
	 *
182
	 * @param mixed|null  $value   Field value.
183
	 * @param string|null $name    Field name.
184
	 * @param array|null  $options Field options.
185
	 * @param array|null  $pod     Pod options.
186
	 * @param int|null    $id      Item ID.
187
	 *
188
	 * @return mixed|null|string
189
	 *
190
	 * @since 2.7
191
	 */
192
	public function display_list( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
193
194
		return call_user_func_array( array( $this, 'display' ), func_get_args() );
195
196
	}
197
198
	/**
199
	 * {@inheritdoc}
200
	 */
201
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
202
203
		$options         = (array) $options;
204
		$form_field_type = PodsForm::$field_type;
205
		$field_type      = 'link';
206
207
		// Ensure proper format
208
		$value = $this->pre_save( $value, $id, $name, $options, null, $pod );
209
210
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
211
212
	}
213
214
	/**
215
	 * {@inheritdoc}
216
	 */
217
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
218
219
		$errors = array();
220
221
		$label = strip_tags( pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
222
223
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
224
225
		$check = $check['url'];
226
227
		if ( is_array( $check ) ) {
228
			$errors = $check;
229
		} else {
230
			if ( ! empty( $value['url'] ) && 0 < strlen( $value['url'] ) && '' === $check ) {
231
				if ( 1 === (int) pods_v( 'required', $options ) ) {
232
					$errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
233
				} else {
234
					$errors[] = sprintf( __( 'Invalid link provided for the field %s.', 'pods' ), $label );
235
				}
236
			}
237
		}
238
239
		if ( ! empty( $errors ) ) {
240
			return $errors;
241
		}
242
243
		return true;
244
245
	}
246
247
	/**
248
	 * {@inheritdoc}
249
	 */
250
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
251
252
		$options = (array) $options;
253
254
		// Update from a single (non array) input field (like website) if the field updates
255
		if ( is_string( $value ) ) {
256
			$value = array( 'url' => $value );
257
		}
258
259
		$value = array_merge(
260
			array(
261
				'url'    => '',
262
				'text'   => '',
263
				'target' => '',
264
			), (array) $value
265
		);
266
267
		// Start URL format
268
		if ( ! empty( $value['url'] ) ) {
269
			$value['url'] = $this->validate_url( $value['url'], $options );
270
		}
271
272
		// Start Title format
273 View Code Duplication
		if ( ! empty( $value['text'] ) ) {
274
			$value['text'] = $this->strip_html( $value['text'], $options );
275
		}
276
277
		// Start Target format
278
		if ( ! empty( $value['target'] ) ) {
279
			$value['target'] = $this->validate_target( $value['target'] );
280
		} elseif ( ! isset( $value['target'] ) && 1 === (int) pods_v( static::$type . '_new_window', $options, 0 ) ) {
281
			$value['target'] = '_blank';
282
		}
283
284
		return $value;
285
286
	}
287
288
	/**
289
	 * Init the editor needed for WP Link modal to work
290
	 */
291
	public function validate_link_modal() {
292
293
		static $init;
294
295
		if ( empty( $init ) ) {
296
			if ( ! did_action( 'wp_enqueue_editor' ) ) {
297
				add_action( 'shutdown', array( $this, 'add_link_modal' ) );
298
			}
299
		}
300
301
		$init = true;
302
303
	}
304
305
	/**
306
	 * Echo the link modal code
307
	 */
308
	public function add_link_modal() {
309
310
		if ( ! class_exists( '_WP_Editors', false ) && file_exists( ABSPATH . WPINC . '/class-wp-editor.php' ) ) {
311
			require_once ABSPATH . WPINC . '/class-wp-editor.php';
312
		}
313
314
		if ( class_exists( '_WP_Editors' ) && method_exists( '_WP_Editors', 'wp_link_dialog' ) ) {
315
			_WP_Editors::wp_link_dialog();
316
		} else {
317
			echo '<div style="display:none;">';
318
			wp_editor( '', 'pods-link-editor-hidden' );
319
			echo '</div>';
320
		}
321
322
	}
323
}
324