[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/classes/ -> class-bp-xprofile-field-type-url.php (source)

   1  <?php
   2  /**
   3   * BuddyPress XProfile Classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage XProfileClasses
   7   * @since 2.1.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * URL xprofile field type.
  15   *
  16   * @since 2.1.0
  17   */
  18  class BP_XProfile_Field_Type_URL extends BP_XProfile_Field_Type {
  19  
  20      /**
  21       * Supported features for the URL field type.
  22       *
  23       * @since 8.0.0
  24       * @var bool[] The URL field type supported features.
  25       */
  26      public static $supported_features = array(
  27          'do_autolink' => false,
  28      );
  29  
  30      /**
  31       * Constructor for the URL field type
  32       *
  33       * @since 2.1.0
  34       */
  35  	public function __construct() {
  36          parent::__construct();
  37  
  38          $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' );
  39          $this->name     = _x( 'URL', 'xprofile field type', 'buddypress' );
  40  
  41          $this->set_format( '_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS', 'replace' );
  42  
  43          /**
  44           * Fires inside __construct() method for BP_XProfile_Field_Type_URL class.
  45           *
  46           * @since 2.0.0
  47           *
  48           * @param BP_XProfile_Field_Type_URL $this Current instance of
  49           *                                         the field type URL.
  50           */
  51          do_action( 'bp_xprofile_field_type_url', $this );
  52      }
  53  
  54      /**
  55       * Output the edit field HTML for this field type.
  56       *
  57       * Must be used inside the {@link bp_profile_fields()} template loop.
  58       *
  59       * @since 2.1.0
  60       *
  61       * @param array $raw_properties Optional key/value array of
  62       *                              {@link http://dev.w3.org/html5/markup/input.number.html permitted attributes}
  63       *                              that you want to add.
  64       */
  65  	public function edit_field_html( array $raw_properties = array() ) {
  66  
  67          // `user_id` is a special optional parameter that certain other
  68          // fields types pass to {@link bp_the_profile_field_options()}.
  69          if ( isset( $raw_properties['user_id'] ) ) {
  70              unset( $raw_properties['user_id'] );
  71          }
  72  
  73          $r = bp_parse_args(
  74              $raw_properties,
  75              array(
  76                  'type'      => 'text',
  77                  'inputmode' => 'url',
  78                  'value'     => esc_url( bp_get_the_profile_field_edit_value() ),
  79              )
  80          );
  81          ?>
  82  
  83          <legend id="<?php bp_the_profile_field_input_name(); ?>-1">
  84              <?php bp_the_profile_field_name(); ?>
  85              <?php bp_the_profile_field_required_label(); ?>
  86          </legend>
  87  
  88          <?php
  89  
  90          /** This action is documented in bp-xprofile/bp-xprofile-classes */
  91          do_action( bp_get_the_profile_field_errors_action() ); ?>
  92  
  93          <input <?php echo $this->get_edit_field_html_elements( $r ); ?> aria-labelledby="<?php bp_the_profile_field_input_name(); ?>-1" aria-describedby="<?php bp_the_profile_field_input_name(); ?>-3">
  94  
  95          <?php if ( bp_get_the_profile_field_description() ) : ?>
  96              <p class="description" id="<?php bp_the_profile_field_input_name(); ?>-3"><?php bp_the_profile_field_description(); ?></p>
  97          <?php endif; ?>
  98  
  99          <?php
 100      }
 101  
 102      /**
 103       * Output HTML for this field type on the wp-admin Profile Fields screen.
 104       *
 105       * Must be used inside the {@link bp_profile_fields()} template loop.
 106       *
 107       * @since 2.1.0
 108       *
 109       * @param array $raw_properties Optional key/value array of permitted
 110       *                              attributes that you want to add.
 111       */
 112  	public function admin_field_html( array $raw_properties = array() ) {
 113  
 114          $r = bp_parse_args(
 115              $raw_properties,
 116              array(
 117                  'type' => 'url',
 118              )
 119          );
 120          ?>
 121  
 122          <label for="<?php bp_the_profile_field_input_name(); ?>" class="screen-reader-text"><?php
 123              /* translators: accessibility text */
 124              esc_html_e( 'URL', 'buddypress' );
 125          ?></label>
 126          <input <?php echo $this->get_edit_field_html_elements( $r ); ?>>
 127  
 128          <?php
 129      }
 130  
 131      /**
 132       * This method usually outputs HTML for this field type's children options
 133       * on the wp-admin Profile Fields "Add Field" and "Edit Field" screens, but
 134       * for this field type, we don't want it, so it's stubbed out.
 135       *
 136       * @since 2.1.0
 137       *
 138       * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
 139       * @param string            $control_type  Optional. HTML input type used to render the current
 140       *                                         field's child options.
 141       */
 142  	public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {}
 143  
 144      /**
 145       * Modify submitted URL values before validation.
 146       *
 147       * The URL validation regex requires a http(s) protocol, so that all
 148       * values saved in the database are fully-formed URLs. However, we
 149       * still want to allow users to enter URLs without a protocol, for a
 150       * better user experience. So we catch submitted URL values, and if
 151       * the protocol is missing, we prepend 'http://' before passing to
 152       * is_valid().
 153       *
 154       * @since 2.1.0
 155       * @since 2.4.0 Added the `$field_id` parameter.
 156       *
 157       * @param string     $submitted_value Raw value submitted by the user.
 158       * @param string|int $field_id        Optional. ID of the field.
 159       * @return string
 160       */
 161  	public static function pre_validate_filter( $submitted_value = '', $field_id = '' ) {
 162  
 163          // Allow empty URL values.
 164          if ( empty( $submitted_value ) ) {
 165              return '';
 166          }
 167  
 168          // Run some checks on the submitted value.
 169          if ( false === strpos( $submitted_value, ':'  ) &&
 170              substr( $submitted_value, 0, 1 ) !== '/' &&
 171              substr( $submitted_value, 0, 1 ) !== '#' &&
 172              ! preg_match( '/^[a-z0-9-]+?\.php/i', $submitted_value )
 173          ) {
 174              $submitted_value = 'http://' . $submitted_value;
 175          }
 176  
 177          return $submitted_value;
 178      }
 179  
 180      /**
 181       * Format URL values for display.
 182       *
 183       * @since 2.1.0
 184       * @since 2.4.0 Added the `$field_id` parameter.
 185       *
 186       * @param string     $field_value The URL value, as saved in the database.
 187       * @param string|int $field_id    Optional. ID of the field.
 188       * @return string URL converted to a link.
 189       */
 190  	public static function display_filter( $field_value, $field_id = '' ) {
 191          $link      = strip_tags( $field_value );
 192          $no_scheme = preg_replace( '#^https?://#', '', rtrim( $link, '/' ) );
 193          $url_text  = str_replace( $link, $no_scheme, $field_value );
 194          return '<a href="' . esc_url( $field_value ) . '" rel="nofollow">' . esc_html( $url_text ) . '</a>';
 195      }
 196  }


Generated: Tue Mar 19 01:01:09 2024 Cross-referenced by PHPXref 0.7.1