<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][11849] trunk: xprofile: add telephone number field type.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/11849">11849</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/11849","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>djpaul</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-02-07 15:29:25 +0000 (Wed, 07 Feb 2018)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>xprofile: add telephone number field type.

Fixes <a href="http://buddypress.trac.wordpress.org/ticket/6549">#6549</a>

Props danbp, boonebgorges</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpxprofilebpxprofilefunctionsphp">trunk/src/bp-xprofile/bp-xprofile-functions.php</a></li>
<li><a href="#trunktestsphpunittestcasesxprofileclassbpxprofilefieldtypephp">trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofilefieldtypetelephonephp">trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpxprofilebpxprofilefunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/bp-xprofile-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/bp-xprofile-functions.php   2018-02-07 14:51:20 UTC (rev 11848)
+++ trunk/src/bp-xprofile/bp-xprofile-functions.php     2018-02-07 15:29:25 UTC (rev 11849)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -158,6 +158,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'selectbox'      => 'BP_XProfile_Field_Type_Selectbox',
</span><span class="cx" style="display: block; padding: 0 10px">                'textarea'       => 'BP_XProfile_Field_Type_Textarea',
</span><span class="cx" style="display: block; padding: 0 10px">                'textbox'        => 'BP_XProfile_Field_Type_Textbox',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                'telephone'      => 'BP_XProfile_Field_Type_Telephone',
</ins><span class="cx" style="display: block; padding: 0 10px">         );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -868,7 +869,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @param BP_XProfile_ProfileData $data Current instance of the profile data being saved.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function xprofile_sync_wp_profile_on_single_field_set( $data ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( bp_xprofile_fullname_field_id() !== $data->field_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre></div>
<a id="trunksrcbpxprofileclassesclassbpxprofilefieldtypetelephonephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php                          (rev 0)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-telephone.php    2018-02-07 15:29:25 UTC (rev 11849)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,156 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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'] ) ) {
+                       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;
+               }
+
+               return sprintf(
+                       '<a href="%1$s" rel="nofollow">%2$s</a>',
+                       esc_url( $url, array( 'tel' ) ),
+                       esc_html( $url_text )
+               );
+       }
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestcasesxprofileclassbpxprofilefieldtypephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php   2018-02-07 14:51:20 UTC (rev 11848)
+++ trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php     2018-02-07 15:29:25 UTC (rev 11849)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -75,7 +75,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_number_do_not_validate_string() {
</span><span class="cx" style="display: block; padding: 0 10px">                $field = bp_xprofile_create_field_type( 'number' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertFalse( $field->is_valid( 'telephone fields only accept integers' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertFalse( $field->is_valid( 'number fields only accept integers' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertFalse( $field->is_valid( '' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -176,4 +176,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( $field->is_valid( 'bar' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( $field->is_valid( array( 'bar' ) ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       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' ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>