PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.7.31.4
Pods – Custom Content Types and Fields v2.7.31.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 3 days ago boolean.php 3 days ago code.php 3 days ago color.php 3 days ago comment.php 3 days ago currency.php 3 days ago date.php 3 days ago datetime.php 3 days ago email.php 3 days ago file.php 3 days ago html.php 3 days ago link.php 3 days ago number.php 3 days ago oembed.php 3 days ago paragraph.php 3 days ago password.php 3 days ago phone.php 3 days ago pick.php 3 days ago slug.php 3 days ago taxonomy.php 3 days ago text.php 3 days ago time.php 3 days ago website.php 3 days ago wysiwyg.php 3 days ago
link.php
319 lines
1 <?php
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 display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
118
119 // Validate for an array because display is also used for the get_post_meta filters along the function chain
120 if ( ! is_array( $value ) ) {
121 return $value;
122 }
123
124 // Ensure proper format
125 $value = $this->pre_save( $value, $id, $name, $options, null, $pod );
126
127 if ( ! empty( $value['text'] ) ) {
128 $value['text'] = $this->strip_html( $value['text'], $options );
129 }
130
131 if ( ! empty( $value['url'] ) ) {
132
133 $link = '<a href="%s"%s>%s</a>';
134
135 // Build the URL
136 $url = $this->build_url( wp_parse_url( $value['url'] ) );
137
138 // Display URL as text by default. If text provided, use the text input
139 $text = $url;
140
141 if ( ! empty( $value['text'] ) ) {
142 $text = $value['text'];
143 }
144
145 $atts = '';
146
147 if ( ! empty( $value['target'] ) || ( ! isset( $value['target'] ) && 1 === (int) pods_v( static::$type . '_new_window', $options ) ) ) {
148 // Possible support for other targets in future
149 $atts .= ' target="' . esc_attr( $value['target'] ) . '" rel="noopener noreferrer"';
150 }
151
152 // Do shortcodes if this is enabled
153 if ( 1 === (int) pods_v( static::$type . '_allow_shortcode', $options ) ) {
154 $text = do_shortcode( $text );
155 }
156
157 // Return the value
158 $value = sprintf( $link, esc_url( $url ), $atts, $text );
159
160 } elseif ( ! empty( $value['text'] ) ) {
161 // No URL data found (probably database error), return text is this is available
162 $value = $value['text'];
163 }//end if
164
165 // Return database value or display value if above conditions are met
166 return $value;
167
168 }
169
170 /**
171 * Change the way the a list of values of the field are displayed with Pods::field
172 *
173 * @param mixed|null $value Field value.
174 * @param string|null $name Field name.
175 * @param array|null $options Field options.
176 * @param array|null $pod Pod options.
177 * @param int|null $id Item ID.
178 *
179 * @return mixed|null|string
180 *
181 * @since 2.7.0
182 */
183 public function display_list( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
184
185 return call_user_func_array( array( $this, 'display' ), func_get_args() );
186
187 }
188
189 /**
190 * {@inheritdoc}
191 */
192 public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
193
194 $options = (array) $options;
195 $form_field_type = PodsForm::$field_type;
196 $field_type = 'link';
197
198 // Ensure proper format
199 $value = $this->pre_save( $value, $id, $name, $options, null, $pod );
200
201 pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
202
203 }
204
205 /**
206 * {@inheritdoc}
207 */
208 public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
209 $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params );
210
211 $errors = array();
212 if ( is_array( $validate ) ) {
213 $errors = $validate;
214 }
215
216 $label = strip_tags( pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
217
218 $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
219
220 $check = $check['url'];
221
222 if ( is_array( $check ) ) {
223 $errors = $check;
224 } else {
225 if ( ! empty( $value['url'] ) && 0 < strlen( $value['url'] ) && '' === $check ) {
226 if ( $this->is_required( $options ) ) {
227 $errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
228 } else {
229 $errors[] = sprintf( __( 'Invalid link provided for the field %s.', 'pods' ), $label );
230 }
231 }
232 }
233
234 if ( ! empty( $errors ) ) {
235 return $errors;
236 }
237
238 return $validate;
239
240 }
241
242 /**
243 * {@inheritdoc}
244 */
245 public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
246
247 $options = (array) $options;
248
249 // Update from a single (non array) input field (like website) if the field updates
250 if ( is_string( $value ) ) {
251 $value = array( 'url' => $value );
252 }
253
254 $value = array_merge(
255 array(
256 'url' => '',
257 'text' => '',
258 'target' => '',
259 ), (array) $value
260 );
261
262 // Start URL format
263 if ( ! empty( $value['url'] ) ) {
264 $value['url'] = $this->validate_url( $value['url'], $options );
265 }
266
267 // Start Title format
268 if ( ! empty( $value['text'] ) ) {
269 $value['text'] = $this->strip_html( $value['text'], $options );
270 }
271
272 // Start Target format
273 if ( ! empty( $value['target'] ) ) {
274 $value['target'] = $this->validate_target( $value['target'] );
275 } elseif ( ! isset( $value['target'] ) && 1 === (int) pods_v( static::$type . '_new_window', $options, 0 ) ) {
276 $value['target'] = '_blank';
277 }
278
279 return $value;
280
281 }
282
283 /**
284 * Init the editor needed for WP Link modal to work
285 */
286 public function validate_link_modal() {
287
288 static $init;
289
290 if ( empty( $init ) ) {
291 if ( ! did_action( 'wp_enqueue_editor' ) ) {
292 add_action( 'shutdown', array( $this, 'add_link_modal' ) );
293 }
294 }
295
296 $init = true;
297
298 }
299
300 /**
301 * Echo the link modal code
302 */
303 public function add_link_modal() {
304
305 if ( ! class_exists( '_WP_Editors', false ) && file_exists( ABSPATH . WPINC . '/class-wp-editor.php' ) ) {
306 require_once ABSPATH . WPINC . '/class-wp-editor.php';
307 }
308
309 if ( class_exists( '_WP_Editors' ) && method_exists( '_WP_Editors', 'wp_link_dialog' ) ) {
310 _WP_Editors::wp_link_dialog();
311 } else {
312 echo '<div style="display:none;">';
313 wp_editor( '', 'pods-link-editor-hidden' );
314 echo '</div>';
315 }
316
317 }
318 }
319