PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.8.23.4
Pods – Custom Content Types and Fields v2.8.23.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / fields / link.php
pods / classes / fields Last commit date
avatar.php 2 weeks ago boolean.php 2 weeks ago code.php 2 weeks ago color.php 2 weeks ago comment.php 2 weeks ago currency.php 2 weeks ago date.php 2 weeks ago datetime.php 2 weeks ago email.php 2 weeks ago file.php 2 weeks ago heading.php 2 weeks ago html.php 2 weeks ago link.php 2 weeks ago number.php 2 weeks ago oembed.php 2 weeks ago paragraph.php 2 weeks ago password.php 2 weeks ago phone.php 2 weeks ago pick.php 2 weeks ago slug.php 2 weeks ago taxonomy.php 2 weeks ago text.php 2 weeks ago time.php 2 weeks ago website.php 2 weeks ago wysiwyg.php 2 weeks ago
link.php
324 lines
1 <?php
2
3 use Pods\Static_Cache;
4
5 /**
6 * @package Pods\Fields
7 */
8 class PodsField_Link extends PodsField_Website {
9
10 /**
11 * {@inheritdoc}
12 */
13 public static $group = 'Text';
14
15 /**
16 * {@inheritdoc}
17 */
18 public static $type = 'link';
19
20 /**
21 * {@inheritdoc}
22 */
23 public static $label = 'Link';
24
25 /**
26 * {@inheritdoc}
27 */
28 public static $prepare = '%s';
29
30 /**
31 * {@inheritdoc}
32 */
33 public function setup() {
34
35 static::$group = __( 'Text', 'pods' );
36 static::$label = __( 'Link', 'pods' );
37 }
38
39 /**
40 * {@inheritdoc}
41 */
42 public function options() {
43
44 $options = array(
45 static::$type . '_format' => array(
46 'label' => __( 'Format', 'pods' ),
47 'default' => 'normal',
48 'type' => 'pick',
49 'data' => array(
50 'none' => __( 'No URL format restrictions', 'pods' ),
51 'normal' => __( 'http://example.com/', 'pods' ),
52 'no-www' => __( 'http://example.com/ (remove www)', 'pods' ),
53 'force-www' => __( 'http://www.example.com/ (force www if no sub-domain provided)', 'pods' ),
54 'no-http' => __( 'example.com', 'pods' ),
55 'no-http-no-www' => __( 'example.com (force removal of www)', 'pods' ),
56 'no-http-force-www' => __( 'www.example.com (force www if no sub-domain provided)', 'pods' ),
57 ),
58 'pick_show_select_text' => 0,
59 ),
60 static::$type . '_select_existing' => array(
61 'label' => __( 'Enable Selecting from Existing Links', 'pods' ),
62 'default' => 1,
63 'type' => 'boolean',
64 'dependency' => true,
65 ),
66 static::$type . '_new_window' => array(
67 'label' => __( 'Open link in new window by default', 'pods' ),
68 'default' => apply_filters( 'pods_form_ui_field_link_new_window', 0, static::$type ),
69 'type' => 'boolean',
70 'dependency' => false,
71 ),
72 'output_options' => array(
73 'label' => __( 'Link Text Output Options', 'pods' ),
74 'type' => 'boolean_group',
75 'boolean_group' => array(
76 static::$type . '_allow_shortcode' => array(
77 'label' => __( 'Allow Shortcodes', 'pods' ),
78 'default' => 0,
79 'type' => 'boolean',
80 'dependency' => true,
81 ),
82 static::$type . '_allow_html' => array(
83 'label' => __( 'Allow HTML', 'pods' ),
84 'default' => 0,
85 'type' => 'boolean',
86 'dependency' => true,
87 ),
88 ),
89 ),
90 static::$type . '_allowed_html_tags' => array(
91 'label' => __( 'Allowed HTML Tags', 'pods' ),
92 'depends-on' => array( static::$type . '_allow_html' => true ),
93 'default' => 'strong em a ul ol li b i',
94 'type' => 'text',
95 ),
96 static::$type . '_html5' => array(
97 'label' => __( 'Enable HTML5 Input Field', 'pods' ),
98 'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ),
99 'type' => 'boolean',
100 ),
101 );
102
103 return $options;
104
105 }
106
107 /**
108 * {@inheritdoc}
109 */
110 public function schema( $options = null ) {
111
112 $schema = 'LONGTEXT';
113
114 return $schema;
115
116 }
117
118 /**
119 * {@inheritdoc}
120 */
121 public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
122
123 // Validate for an array because display is also used for the get_post_meta filters along the function chain
124 if ( ! is_array( $value ) ) {
125 return $value;
126 }
127
128 // Ensure proper format
129 $value = $this->pre_save( $value, $id, $name, $options, null, $pod );
130
131 if ( ! empty( $value['text'] ) ) {
132 $value['text'] = $this->strip_html( $value['text'], $options );
133 $value['text'] = $this->strip_shortcodes( $value['text'], $options );
134 $value['text'] = $this->trim_whitespace( $value['text'], $options );
135 }
136
137 if ( ! empty( $value['url'] ) ) {
138
139 $link = '<a href="%s"%s>%s</a>';
140
141 // Build the URL
142 $url = $this->build_url( wp_parse_url( $value['url'] ) );
143
144 // Display URL as text by default. If text provided, use the text input
145 $text = $url;
146
147 if ( ! empty( $value['text'] ) ) {
148 $text = $value['text'];
149 }
150
151 $atts = '';
152
153 if ( ! empty( $value['target'] ) || ( ! isset( $value['target'] ) && 1 === (int) pods_v( static::$type . '_new_window', $options ) ) ) {
154 // Possible support for other targets in future
155 $atts .= ' target="' . esc_attr( $value['target'] ) . '" rel="noopener noreferrer"';
156 }
157
158 // Do shortcodes if this is enabled
159 if ( 1 === (int) pods_v( static::$type . '_allow_shortcode', $options ) ) {
160 $text = do_shortcode( $text );
161 }
162
163 // Return the value
164 $value = sprintf( $link, esc_url( $url ), $atts, $text );
165
166 } elseif ( ! empty( $value['text'] ) ) {
167 // No URL data found (probably database error), return text is this is available
168 $value = $value['text'];
169 }//end if
170
171 // Return database value or display value if above conditions are met
172 return $value;
173
174 }
175
176 /**
177 * Change the way the a list of values of the field are displayed with Pods::field
178 *
179 * @param mixed|null $value Field value.
180 * @param string|null $name Field name.
181 * @param array|null $options Field options.
182 * @param array|null $pod Pod options.
183 * @param int|null $id Item ID.
184 *
185 * @return mixed|null|string
186 *
187 * @since 2.7.0
188 */
189 public function display_list( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
190
191 return call_user_func_array( array( $this, 'display' ), func_get_args() );
192
193 }
194
195 /**
196 * {@inheritdoc}
197 */
198 public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
199
200 $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options;
201 $form_field_type = PodsForm::$field_type;
202 $field_type = 'link';
203
204 // Ensure proper format
205 $value = $this->pre_save( $value, $id, $name, $options, null, $pod );
206
207 pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
208 }
209
210 /**
211 * {@inheritdoc}
212 */
213 public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
214 $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params );
215 $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
216
217 // Remove valid field keys from returning in website field.
218 if ( is_array( $value ) ) {
219 $validate = array_diff_key( $validate, $check );
220 }
221
222 $errors = array();
223 if ( is_array( $validate ) ) {
224 $errors = $validate;
225 }
226
227 if ( ! empty( $value['url'] ) && 0 < strlen( $value['url'] ) && '' === $check['url'] ) {
228 $label = strip_tags( pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
229
230 if ( $this->is_required( $options ) ) {
231 $errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
232 } else {
233 $errors[] = sprintf( __( 'Invalid link provided for the field %s.', 'pods' ), $label );
234 }
235 }
236
237 if ( ! empty( $errors ) ) {
238 return $errors;
239 }
240
241 return $validate;
242
243 }
244
245 /**
246 * {@inheritdoc}
247 */
248 public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
249
250 $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options;
251
252 // Update from a single (non array) input field (like website) if the field updates
253 if ( is_string( $value ) ) {
254 $value = array( 'url' => $value );
255 }
256
257 $value = array_merge(
258 array(
259 'url' => '',
260 'text' => '',
261 'target' => '',
262 ), (array) $value
263 );
264
265 // Start URL format
266 if ( ! empty( $value['url'] ) ) {
267 $value['url'] = $this->validate_url( $value['url'], $options );
268 }
269
270 // Start Title format
271 if ( ! empty( $value['text'] ) ) {
272 $value['text'] = $this->strip_html( $value['text'], $options );
273 $value['text'] = $this->strip_shortcodes( $value['text'], $options );
274 $value['text'] = $this->trim_whitespace( $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 $init = (boolean) pods_static_cache_get( 'init', __METHOD__ );
293
294 if ( $init ) {
295 return;
296 }
297
298 if ( ! did_action( 'wp_enqueue_editor' ) && ! has_action( 'shutdown', [ $this, 'add_link_modal' ] ) ) {
299 add_action( 'shutdown', [ $this, 'add_link_modal' ] );
300 }
301
302 pods_static_cache_set( 'init', 1, __METHOD__ );
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