<!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][12872] trunk: Add the `wp-biography` xProfile field type's class & unit tests</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 { white-space: pre-line; 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/12872">12872</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/12872","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-03-22 19:51:14 +0000 (Mon, 22 Mar 2021)</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'>Add the `wp-biography` xProfile field type's class & unit tests

- `BP_XProfile_Field_Type_WordPress_Biography` deals with users description (Biographical information) as a textarea is used to fill its value.
- The PHP Unit tests suite now includes 5 more tests about updating/getting WordPress xProfile fields.

Props DJPaul, Offereins, needle, netweb, vapvarun

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

<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofilefieldtypewordpressbiographyphp">trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress-biography.php</a></li>
<li><a href="#trunktestsphpunittestcasesxprofileclassbpxprofilefieldtypewordpressphp">trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type-wordpress.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpxprofileclassesclassbpxprofilefieldtypewordpressbiographyphp"></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-wordpress-biography.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-wordpress-biography.php                                (rev 0)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress-biography.php  2021-03-22 19:51:14 UTC (rev 12872)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,167 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * BuddyPress XProfile Classes.
+ *
+ * @package BuddyPress
+ * @subpackage XProfileClasses
+ * @since 8.0.0
+ */
+
+// Exit if accessed directly.
+defined( 'ABSPATH' ) || exit;
+
+/**
+ * WordPress Biography xProfile field type.
+ *
+ * @since 8.0.0
+ */
+class BP_XProfile_Field_Type_WordPress_Biography extends BP_XProfile_Field_Type_WordPress {
+
+       /**
+        * Constructor for the WordPress biography field type.
+        *
+        * @since 8.0.0
+        */
+       public function __construct() {
+               parent::__construct();
+
+               $this->category           = _x( 'WordPress Fields', 'xprofile field type category', 'buddypress' );
+               $this->name               = _x( 'Biography', 'xprofile field type', 'buddypress' );
+               $this->accepts_null_value = true;
+               $this->wp_user_key        = 'description';
+
+               $this->set_format( '/^.*$/m', 'replace' );
+
+               /**
+                * Fires inside __construct() method for BP_XProfile_Field_Type_WordPress_Biography class.
+                *
+                * @since 8.0.0
+                *
+                * @param BP_XProfile_Field_Type_WordPress_Biography $this Instance of the field type object.
+                */
+               do_action( 'bp_xprofile_field_type_wordpress_biography', $this );
+       }
+
+       /**
+        * Sanitize the user field before saving it to db.
+        *
+        * @since 8.0.0
+        *
+        * @param string $value The user field value.
+        * @return string The sanitized field value.
+        */
+       public function sanitize_for_db( $value ) {
+               return trim( $value );
+       }
+
+       /**
+        * Sanitize the user field before displaying it as an attribute.
+        *
+        * @since 8.0.0
+        *
+        * @param string $value The user field value.
+        * @param integer $user_id The user ID.
+        * @return string The sanitized field value.
+        */
+       public function sanitize_for_output( $value, $user_id = 0 ) {
+               if ( ! $user_id ) {
+                       $user_id = bp_displayed_user_id();
+               }
+
+               return sanitize_user_field( $this->wp_user_key, $value, $user_id, 'attribute' );
+       }
+
+       /**
+        * Output the edit field HTML for this field type.
+        *
+        * Must be used inside the {@link bp_profile_fields()} template loop.
+        *
+        * @since 8.0.0
+        *
+        * @param array $raw_properties Optional key/value array of
+        *                              {@link http://dev.w3.org/html5/markup/textarea.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 ( ! is_admin() && isset( $raw_properties['user_id'] ) ) {
+                       unset( $raw_properties['user_id'] );
+               }
+
+               $user_id = bp_displayed_user_id();
+               if ( isset( $raw_properties['user_id'] ) && $raw_properties['user_id'] ) {
+                       $user_id = (int) $raw_properties['user_id'];
+                       unset( $raw_properties['user_id'] );
+               }
+               ?>
+
+               <label for="<?php bp_the_profile_field_input_name(); ?>">
+                       <?php bp_the_profile_field_name(); ?>
+                       <?php bp_the_profile_field_required_label(); ?>
+               </label>
+
+               <?php
+               /** This action is documented in bp-xprofile/bp-xprofile-classes */
+               do_action( bp_get_the_profile_field_errors_action() );
+
+               $r = wp_parse_args( $raw_properties, array(
+                       'cols' => 40,
+                       'rows' => 5,
+               ) );
+               ?>
+
+               <textarea <?php echo $this->get_edit_field_html_elements( $r ); ?>><?php
+                       echo $this->sanitize_for_output( bp_get_user_meta( $user_id, $this->wp_user_key, true ), $user_id );
+               ?></textarea>
+
+               <?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 8.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(
+                       'cols' => 40,
+                       'rows' => 5,
+               ) ); ?>
+
+               <textarea <?php echo $this->get_edit_field_html_elements( $r ); ?>></textarea>
+
+               <?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 8.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 WordPress Biography for display.
+        *
+        * @since 8.0.0
+        *
+        * @param string     $field_value The field value, as saved in the database.
+        * @param string|int $field_id    Optional. ID of the field.
+        * @return string The sanitized WordPress field.
+        */
+       public static function display_filter( $field_value, $field_id = '' ) {
+               return sanitize_user_field( 'description', $field_value, bp_displayed_user_id(), 'display' );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress-biography.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestcasesxprofileclassbpxprofilefieldtypewordpressphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type-wordpress.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-wordpress.php                         (rev 0)
+++ trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type-wordpress.php   2021-03-22 19:51:14 UTC (rev 12872)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,142 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * @group xprofile
+ * @group BP_Tests_XProfile_Field_Type_WordPress
+ * @ticket BP7162
+ */
+class BP_Tests_XProfile_Field_Type_WordPress extends BP_UnitTestCase {
+
+       public function test_wp_textbox_validate_string() {
+               $field = bp_xprofile_create_field_type( 'wp-textbox' );
+               $this->assertTrue( $field->is_valid( 'Hello WordPress Fields!' ) );
+       }
+
+       /**
+        * @group xprofile_set_field_data
+        */
+       public function test_wp_textbox_set_last_name_field_data() {
+               $user_id  = self::factory()->user->create();
+               $group_id = self::factory()->xprofile_group->create();
+               $field_id = self::factory()->xprofile_field->create(
+                       array(
+                               'field_group_id' => $group_id,
+                               'type'           => 'wp-textbox',
+                               'name'           => 'WP Last Name',
+                       )
+               );
+
+               // Set the WP User Key.
+               bp_xprofile_update_meta( $field_id, 'field', 'wp_user_key', 'last_name' );
+
+               $field_data = xprofile_set_field_data( $field_id, $user_id, 'bar' );
+
+               $user = get_user_by( 'id', $user_id );
+
+               $this->assertEquals( 'bar', $user->last_name );
+       }
+
+       /**
+        * @group bp_xprofile_get_groups
+        */
+       public function test_wp_textbox_get_user_url_field_data() {
+               $user_id  = self::factory()->user->create(
+                       array(
+                               'user_url' => 'https://buddypress.org',
+                       )
+               );
+               $group_id = self::factory()->xprofile_group->create();
+               $field_id = self::factory()->xprofile_field->create(
+                       array(
+                               'field_group_id' => $group_id,
+                               'type'           => 'wp-textbox',
+                               'name'           => 'WP User URL',
+                       )
+               );
+
+               // Set the WP User Key.
+               bp_xprofile_update_meta( $field_id, 'field', 'wp_user_key', 'user_url' );
+
+               $groups = bp_xprofile_get_groups(
+                       array(
+                               'profile_group_id'       => $group_id,
+                               'user_id'                => $user_id,
+                               'fetch_fields'           => true,
+                               'fetch_field_data'       => true,
+                       )
+               );
+               $group  = reset( $groups );
+               $field  = reset( $group->fields );
+
+               $this->assertEquals( 'https://buddypress.org', $field->data->value );
+       }
+
+       /**
+        * @group xprofile_get_field_data
+        */
+       public function test_wp_textbox_get_first_name_field_value() {
+               $user_id  = self::factory()->user->create(
+                       array(
+                               'first_name' => 'foo',
+                       )
+               );
+               $group_id = self::factory()->xprofile_group->create();
+               $field_id = self::factory()->xprofile_field->create(
+                       array(
+                               'field_group_id' => $group_id,
+                               'type'           => 'wp-textbox',
+                               'name'           => 'WP First Name',
+                       )
+               );
+
+               // Set the WP User Key.
+               bp_xprofile_update_meta( $field_id, 'field', 'wp_user_key', 'first_name' );
+
+               $field_data = xprofile_get_field_data( $field_id, $user_id );
+
+               $this->assertEquals( 'foo', $field_data );
+       }
+
+       /**
+        * @group bp_member_profile_data
+        * @group bp_get_member_profile_data
+        */
+       public function test_wp_biography_get_field_value() {
+               global $members_template;
+               $reset_members_template = $members_template;
+
+               $user_id  = self::factory()->user->create(
+                       array(
+                               'description' => 'The BuddyPress community is awesome!',
+                       )
+               );
+               $group_id = self::factory()->xprofile_group->create();
+               $field_id = self::factory()->xprofile_field->create(
+                       array(
+                               'field_group_id' => $group_id,
+                               'type'           => 'wp-biography',
+                               'name'           => 'About Me',
+                       )
+               );
+
+               $members_template = new BP_Core_Members_Template(
+                       array(
+                               'include'  => $user_id,
+                               'type'     => 'alphabetical',
+                               'page'     => 1,
+                               'per_page' => 1,
+                       )
+               );
+
+               bp_the_member();
+
+               $profile_data = bp_get_member_profile_data(
+                       array(
+                               'field' => 'About Me'
+                       )
+               );
+
+               $members_template = $reset_members_template;
+
+               $this->assertEquals( 'The BuddyPress community is awesome!', $profile_data );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type-wordpress.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>

</body>
</html>