| @@ -9,18 +9,14 @@ | ||
| 9 | 9 | class FrmFieldUrl extends FrmFieldType { |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * @var string |
| 13 | + * | |
| 13 | 14 | * @since 3.0 |
| 14 | 15 | */ |
| 15 | 16 | protected $type = 'url'; |
| 16 | 17 | |
| 17 | 18 | /** |
| 18 | - * @var string | |
| 19 | - */ | |
| 20 | - protected $display_type = 'text'; | |
| 21 | - | |
| 22 | - /** | |
| 23 | 19 | * @var bool |
| 24 | 20 | */ |
| 25 | 21 | protected $array_allowed = false; |
| 26 | 22 | |
| @@ -52,8 +48,10 @@ | ||
| 52 | 48 | return __( 'Website', 'formidable' ); |
| 53 | 49 | } |
| 54 | 50 | |
| 55 | 51 | /** |
| 52 | + * @param array $atts | |
| 53 | + * | |
| 56 | 54 | * @return void |
| 57 | 55 | */ |
| 58 | 56 | protected function fill_default_atts( &$atts ) { |
| 59 | 57 | $defaults = array( |
| @@ -66,15 +64,19 @@ | ||
| 66 | 64 | $atts['sep'] = ' '; |
| 67 | 65 | } |
| 68 | 66 | } |
| 69 | 67 | |
| 68 | + /** | |
| 69 | + * @param array $args | |
| 70 | + */ | |
| 70 | 71 | public function validate( $args ) { |
| 71 | 72 | $value = $args['value']; |
| 72 | - if ( trim( $value ) == 'http://' || empty( $value ) ) { | |
| 73 | + | |
| 74 | + if ( trim( $value ) === 'http://' || ! $value ) { | |
| 73 | 75 | $value = ''; |
| 74 | 76 | } else { |
| 75 | 77 | $value = esc_url_raw( $value ); |
| 76 | - $value = preg_match( '/^(https?|ftps?|mailto|news|feed|telnet):/is', $value ) ? $value : 'http://' . $value; | |
| 78 | + $value = preg_match( '/^(https?|ftps?|mailto|news|feed|telnet):/is', $value ) ? $value : 'https://' . $value; | |
| 77 | 79 | } |
| 78 | 80 | |
| 79 | 81 | FrmEntriesHelper::set_posted_value( $this->field, $value, $args ); |
| 80 | 82 | |
| @@ -79,12 +81,13 @@ | ||
| 79 | 81 | FrmEntriesHelper::set_posted_value( $this->field, $value, $args ); |
| 80 | 82 | |
| 81 | 83 | $errors = array(); |
| 82 | 84 | |
| 83 | - // validate the url format | |
| 84 | - if ( ! empty( $value ) && ! preg_match( '/^http(s)?:\/\/(?:localhost|(?:[\da-z\.-]+\.[\da-z\.-]+))/i', $value ) ) { | |
| 85 | + // Validate the url format | |
| 86 | + if ( $value && ! preg_match( '/^http(s)?:\/\/(?:localhost|(?:[\da-z\.-]+\.[\da-z\.-]+))/i', $value ) ) { | |
| 85 | 87 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' ); |
| 86 | - } elseif ( $this->field->required == '1' && empty( $value ) ) { | |
| 88 | + // skipcq: PHP-W1067 -- $this->field is always a field object by the time validate() runs; FrmFieldType's constructor just accepts array|int|object for lazy construction elsewhere. | |
| 89 | + } elseif ( $this->field->required == '1' && ! $value ) { // phpcs:ignore Universal.Operators.StrictComparisons | |
| 87 | 90 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'blank' ); |
| 88 | 91 | } |
| 89 | 92 | |
| 90 | 93 | return $errors; |
| @@ -90,27 +93,32 @@ | ||
| 90 | 93 | return $errors; |
| 91 | 94 | } |
| 92 | 95 | |
| 93 | 96 | protected function prepare_display_value( $value, $atts ) { |
| 94 | - if ( $atts['html'] ) { | |
| 95 | - $images = ''; | |
| 96 | - foreach ( (array) $value as $url ) { | |
| 97 | - $image_regex = '/(\.(?i)(jpg|jpeg|png|gif))$/'; | |
| 98 | - $is_image = preg_match( $image_regex, $url ); | |
| 99 | - if ( $is_image ) { | |
| 100 | - $images .= '<img src="' . esc_attr( $url ) . '" class="frm_image_from_url" alt="" /> '; | |
| 101 | - } else { | |
| 102 | - $images .= strip_tags( $url ); | |
| 103 | - } | |
| 97 | + if ( ! $atts['html'] ) { | |
| 98 | + return $value; | |
| 99 | + } | |
| 100 | + | |
| 101 | + $images = ''; | |
| 102 | + | |
| 103 | + foreach ( (array) $value as $url ) { | |
| 104 | + $image_regex = '/(\.(?i)(jpg|jpeg|png|gif))$/'; | |
| 105 | + $is_image = preg_match( $image_regex, $url ); | |
| 106 | + | |
| 107 | + if ( $is_image ) { | |
| 108 | + $images .= '<img src="' . esc_url( $url ) . '" class="frm_image_from_url" alt="" /> '; | |
| 109 | + } else { | |
| 110 | + $images .= strip_tags( $url ); | |
| 104 | 111 | } |
| 105 | - $value = $images; | |
| 106 | 112 | } |
| 107 | 113 | |
| 108 | - return $value; | |
| 114 | + return $images; | |
| 109 | 115 | } |
| 110 | 116 | |
| 111 | 117 | /** |
| 112 | 118 | * @since 4.0.04 |
| 119 | + * | |
| 120 | + * @param array|string $value | |
| 113 | 121 | * |
| 114 | 122 | * @return void |
| 115 | 123 | */ |
| 116 | 124 | public function sanitize_value( &$value ) { |