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