From fcb0791a9b6576be3c29d5b46937d94d80455f71 Mon Sep 17 00:00:00 2001
From: Paul Gibbs <djpaul@gmail.com>
Date: Sun, 4 Feb 2018 16:35:20 +0000
Subject: [PATCH 1/2] Phone number profile field type

Fixes #6549
---
 src/bp-xprofile/bp-xprofile-functions.php          |   3 +-
 .../class-bp-xprofile-field-type-telephone.php     | 151 +++++++++++++++++++++
 .../xprofile/class-bp-xprofile-field-type.php      |   9 +-
 3 files changed, 161 insertions(+), 2 deletions(-)
 create mode 100644 src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php

diff --git a/src/bp-xprofile/bp-xprofile-functions.php b/src/bp-xprofile/bp-xprofile-functions.php
index e4c16d3d7..337d2cd79 100644
--- a/src/bp-xprofile/bp-xprofile-functions.php
+++ b/src/bp-xprofile/bp-xprofile-functions.php
@@ -158,6 +158,7 @@ function bp_xprofile_get_field_types() {
 		'selectbox'      => 'BP_XProfile_Field_Type_Selectbox',
 		'textarea'       => 'BP_XProfile_Field_Type_Textarea',
 		'textbox'        => 'BP_XProfile_Field_Type_Textbox',
+		'telephone'      => 'BP_XProfile_Field_Type_Telephone',
 	);
 
 	/**
@@ -868,7 +869,7 @@ function xprofile_sync_bp_profile( &$errors, $update, &$user ) {
  * @param BP_XProfile_ProfileData $data Current instance of the profile data being saved.
  */
 function xprofile_sync_wp_profile_on_single_field_set( $data ) {
-	
+
 	if ( bp_xprofile_fullname_field_id() !== $data->field_id ) {
 		return;
 	}
diff --git a/src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php b/src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php
new file mode 100644
index 000000000..4b00dd810
--- /dev/null
+++ b/src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php
@@ -0,0 +1,151 @@
+<?php
+/**
+ * BuddyPress XProfile Classes.
+ *
+ * @package BuddyPress
+ * @subpackage XProfileClasses
+ * @since 3.0.0
+ */
+
+// Exit if accessed directly.
+defined( 'ABSPATH' ) || exit;
+
+/**
+ * Telephone number xprofile field type.
+ *
+ * @since 3.0.0
+ */
+class BP_XProfile_Field_Type_Telephone extends BP_XProfile_Field_Type {
+
+	/**
+	 * Constructor for the telephone number field type.
+	 *
+	 * @since 3.0.0
+	 */
+	public function __construct() {
+		parent::__construct();
+
+		$this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' );
+		$this->name     = _x( 'Phone Number', 'xprofile field type', 'buddypress' );
+
+		$this->set_format( '/^.*$/', 'replace' );
+
+		/**
+		 * Fires inside __construct() method for BP_XProfile_Field_Type_Telephone class.
+		 *
+		 * @since 3.0.0
+		 *
+		 * @param BP_XProfile_Field_Type_Telephone $this Current instance of the field type.
+		 */
+		do_action( 'bp_xprofile_field_type_telephone', $this );
+	}
+
+	/**
+	 * Output the edit field HTML for this field type.
+	 *
+	 * Must be used inside the {@link bp_profile_fields()} template loop.
+	 *
+	 * @since 3.0.0
+	 *
+	 * @param array $raw_properties Optional key/value array of
+	 *                              {@link http://dev.w3.org/html5/markup/input.text.html permitted attributes}
+	 *                              that you want to add.
+	 */
+	public function edit_field_html( array $raw_properties = array() ) {
+		/*
+		 * User_id is a special optional parameter that certain other fields
+		 * types pass to {@link bp_the_profile_field_options()}.
+		 */
+		if ( isset( $raw_properties['user_id'] ) ) {
+			unset( $raw_properties['user_id'] );
+		}
+
+		$r = bp_parse_args( $raw_properties, array(
+			'type'  => 'tel',
+			'value' => bp_get_the_profile_field_edit_value(),
+		) ); ?>
+
+		<legend id="<?php bp_the_profile_field_input_name(); ?>-1">
+			<?php bp_the_profile_field_name(); ?>
+			<?php bp_the_profile_field_required_label(); ?>
+		</legend>
+
+		<?php
+
+		/** This action is documented in bp-xprofile/bp-xprofile-classes */
+		do_action( bp_get_the_profile_field_errors_action() ); ?>
+
+		<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">
+
+		<?php if ( bp_get_the_profile_field_description() ) : ?>
+			<p class="description" id="<?php bp_the_profile_field_input_name(); ?>-3"><?php bp_the_profile_field_description(); ?></p>
+		<?php endif; ?>
+
+		<?php
+	}
+
+	/**
+	 * Output HTML for this field type on the wp-admin Profile Fields screen.
+	 *
+	 * Must be used inside the {@link bp_profile_fields()} template loop.
+	 *
+	 * @since 3.0.0
+	 *
+	 * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
+	 */
+	public function admin_field_html( array $raw_properties = array() ) {
+		$r = bp_parse_args( $raw_properties, array(
+			'type' => 'tel',
+		) ); ?>
+
+		<label for="<?php bp_the_profile_field_input_name(); ?>" class="screen-reader-text"><?php
+			/* translators: accessibility text */
+			esc_html_e( 'Phone Number', 'buddypress' );
+		?></label>
+		<input <?php echo $this->get_edit_field_html_elements( $r ); ?>>
+
+		<?php
+	}
+
+	/**
+	 * This method usually outputs HTML for this field type's children options on the wp-admin Profile Fields
+	 * "Add Field" and "Edit Field" screens, but for this field type, we don't want it, so it's stubbed out.
+	 *
+	 * @since 3.0.0
+	 *
+	 * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
+	 * @param string            $control_type  Optional. HTML input type used to render the
+	 *                                         current field's child options.
+	 */
+	public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {}
+
+	/**
+	 * Format URL values for display.
+	 *
+	 * @since 3.0.0
+	 *
+	 * @param string     $field_value The URL value, as saved in the database.
+	 * @param string|int $field_id    Optional. ID of the field.
+	 *
+	 * @return string URL converted to a link.
+	 */
+	public static function display_filter( $field_value, $field_id = '' ) {
+		$url   = wp_strip_all_tags( $field_value );
+		$parts = parse_url( $url );
+
+		// Add the tel:// protocol to the field value.
+		if ( isset( $parts['scheme'] ) && strtolower( $parts['scheme'] ) !== 'tel' ) {
+			$url      = preg_replace( '#^' . $parts['scheme'] . '#i', 'tel', $url );
+			$url_text = preg_replace( '#^tel://#i', '', $url );
+		} else {
+			$url_text = $url;
+			$url      = 'tel://' . $url;
+		}
+
+		return sprintf(
+			'<a href="%1$s" rel="nofollow">%2$s</a>',
+			esc_url( $url, array( 'tel' ) ),
+			esc_html( $url_text )
+		);
+	}
+}
diff --git a/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php b/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php
index 6fa264464..6b07a1124 100644
--- a/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php
+++ b/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php
@@ -75,7 +75,7 @@ public function test_datebox_do_not_validate_date_with_timestamp() {
 
 	public function test_number_do_not_validate_string() {
 		$field = bp_xprofile_create_field_type( 'number' );
-		$this->assertFalse( $field->is_valid( 'telephone fields only accept integers' ) );
+		$this->assertFalse( $field->is_valid( 'number fields only accept integers' ) );
 		$this->assertFalse( $field->is_valid( '' ) );
 	}
 
@@ -176,4 +176,11 @@ public function test_placeholder_validate_any_value() {
 		$this->assertTrue( $field->is_valid( 'bar' ) );
 		$this->assertTrue( $field->is_valid( array( 'bar' ) ) );
 	}
+
+	public function test_telephone_validate_number_formats() {
+		$field = bp_xprofile_create_field_type( 'telephone' );
+		$this->assertTrue( $field->is_valid( '07700 900461' ) );
+		$this->assertTrue( $field->is_valid( '555-2368' ) );
+		$this->assertTrue( $field->is_valid( '(212) 664-7665' ) );
+	}
 }

From d9a6a0be7e80dc8e5c9dd92000558a891d109f23 Mon Sep 17 00:00:00 2001
From: Paul Gibbs <djpaul@gmail.com>
Date: Wed, 7 Feb 2018 15:25:12 +0000
Subject: [PATCH 2/2] Update telephone patch

---
 .../classes/class-bp-xprofile-field-type-telephone.php           | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php b/src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php
index 4b00dd810..275bb3abd 100644
--- a/src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php
+++ b/src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php
@@ -134,9 +134,14 @@ public static function display_filter( $field_value, $field_id = '' ) {
 		$parts = parse_url( $url );
 
 		// Add the tel:// protocol to the field value.
-		if ( isset( $parts['scheme'] ) && strtolower( $parts['scheme'] ) !== 'tel' ) {
-			$url      = preg_replace( '#^' . $parts['scheme'] . '#i', 'tel', $url );
+		if ( isset( $parts['scheme'] ) ) {
+			if ( strtolower( $parts['scheme'] ) !== 'tel' ) {
+				$scheme = preg_quote( $parts['scheme'], '#' );
+				$url    = preg_replace( '#^' . $scheme . '#i', 'tel', $url );
+			}
+
 			$url_text = preg_replace( '#^tel://#i', '', $url );
+
 		} else {
 			$url_text = $url;
 			$url      = 'tel://' . $url;
