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