/bp-xprofile/classes/class-bp-xprofile-field-type-url.php

  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. * Constructor for the URL field type 
  22. * 
  23. * @since 2.1.0 
  24. */ 
  25. public function __construct() { 
  26. parent::__construct(); 
  27.  
  28. $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' ); 
  29. $this->name = _x( 'URL', 'xprofile field type', 'buddypress' ); 
  30.  
  31. $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' ); 
  32.  
  33. /** 
  34. * Fires inside __construct() method for BP_XProfile_Field_Type_URL class. 
  35. * 
  36. * @since 2.0.0 
  37. * 
  38. * @param BP_XProfile_Field_Type_URL $this Current instance of 
  39. * the field type URL. 
  40. */ 
  41. do_action( 'bp_xprofile_field_type_url', $this ); 
  42.  
  43. /** 
  44. * Output the edit field HTML for this field type. 
  45. * 
  46. * Must be used inside the {@link bp_profile_fields()} template loop. 
  47. * 
  48. * @since 2.1.0 
  49. * 
  50. * @param array $raw_properties Optional key/value array of 
  51. * {@link http://dev.w3.org/html5/markup/input.number.html permitted attributes} 
  52. * that you want to add. 
  53. */ 
  54. public function edit_field_html( array $raw_properties = array() ) { 
  55.  
  56. // `user_id` is a special optional parameter that certain other 
  57. // fields types pass to {@link bp_the_profile_field_options()}. 
  58. if ( isset( $raw_properties['user_id'] ) ) { 
  59. unset( $raw_properties['user_id'] ); 
  60.  
  61. $r = bp_parse_args( $raw_properties, array( 
  62. 'type' => 'text',  
  63. 'inputmode' => 'url',  
  64. 'value' => esc_url( bp_get_the_profile_field_edit_value() ),  
  65. ) ); ?> 
  66.  
  67. <label for="<?php bp_the_profile_field_input_name(); ?>"> 
  68. <?php bp_the_profile_field_name(); ?> 
  69. <?php bp_the_profile_field_required_label(); ?> 
  70. </label> 
  71.  
  72. <?php 
  73.  
  74. /** This action is documented in bp-xprofile/bp-xprofile-classes */ 
  75. do_action( bp_get_the_profile_field_errors_action() ); ?> 
  76.  
  77. <input <?php echo $this->get_edit_field_html_elements( $r ); ?>> 
  78.  
  79. <?php 
  80.  
  81. /** 
  82. * Output HTML for this field type on the wp-admin Profile Fields screen. 
  83. * 
  84. * Must be used inside the {@link bp_profile_fields()} template loop. 
  85. * 
  86. * @since 2.1.0 
  87. * 
  88. * @param array $raw_properties Optional key/value array of permitted 
  89. * attributes that you want to add. 
  90. */ 
  91. public function admin_field_html( array $raw_properties = array() ) { 
  92.  
  93. $r = bp_parse_args( $raw_properties, array( 
  94. 'type' => 'url' 
  95. ) ); ?> 
  96.  
  97. <label for="<?php bp_the_profile_field_input_name(); ?>" class="screen-reader-text"><?php 
  98. /** translators: accessibility text */ 
  99. esc_html_e( 'URL', 'buddypress' ); 
  100. ?></label> 
  101. <input <?php echo $this->get_edit_field_html_elements( $r ); ?>> 
  102.  
  103. <?php 
  104.  
  105. /** 
  106. * This method usually outputs HTML for this field type's children options 
  107. * on the wp-admin Profile Fields "Add Field" and "Edit Field" screens, but 
  108. * for this field type, we don't want it, so it's stubbed out. 
  109. * 
  110. * @since 2.1.0 
  111. * 
  112. * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen. 
  113. * @param string $control_type Optional. HTML input type used to render the current 
  114. * field's child options. 
  115. */ 
  116. public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {} 
  117.  
  118. /** 
  119. * Modify submitted URL values before validation. 
  120. * 
  121. * The URL validation regex requires a http(s) protocol, so that all 
  122. * values saved in the database are fully-formed URLs. However, we 
  123. * still want to allow users to enter URLs without a protocol, for a 
  124. * better user experience. So we catch submitted URL values, and if 
  125. * the protocol is missing, we prepend 'http://' before passing to 
  126. * is_valid(). 
  127. * 
  128. * @since 2.1.0 
  129. * @since 2.4.0 Added the `$field_id` parameter. 
  130. * 
  131. * @param string $submitted_value Raw value submitted by the user. 
  132. * @param string|int $field_id Optional. ID of the field. 
  133. * @return string 
  134. */ 
  135. public static function pre_validate_filter( $submitted_value = '', $field_id = '' ) { 
  136.  
  137. // Allow empty URL values. 
  138. if ( empty( $submitted_value ) ) { 
  139. return ''; 
  140.  
  141. // Run some checks on the submitted value. 
  142. if ( false === strpos( $submitted_value, ':' ) && 
  143. substr( $submitted_value, 0, 1 ) !== '/' && 
  144. substr( $submitted_value, 0, 1 ) !== '#' && 
  145. ! preg_match( '/^[a-z0-9-]+?\.php/i', $submitted_value ) 
  146. ) { 
  147. $submitted_value = 'http://' . $submitted_value; 
  148.  
  149. return $submitted_value; 
  150.  
  151. /** 
  152. * Format URL values for display. 
  153. * 
  154. * @since 2.1.0 
  155. * @since 2.4.0 Added the `$field_id` parameter. 
  156. * 
  157. * @param string $field_value The URL value, as saved in the database. 
  158. * @param string|int $field_id Optional. ID of the field. 
  159. * @return string URL converted to a link. 
  160. */ 
  161. public static function display_filter( $field_value, $field_id = '' ) { 
  162. $link = strip_tags( $field_value ); 
  163. $no_scheme = preg_replace( '#^https?://#', '', rtrim( $link, '/' ) ); 
  164. $url_text = str_replace( $link, $no_scheme, $field_value ); 
  165. return '<a href="' . esc_url( $field_value ) . '" rel="nofollow">' . esc_html( $url_text ) . '</a>'; 
.