<!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][12870] trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress.php: Introduce the base class for WordPress xProfile fields.</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/12870">12870</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/12870","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:47:31 +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'>Introduce the base class for WordPress xProfile fields.

`BP_XProfile_Field_Type_WordPress` uses <a href="http://buddypress.trac.wordpress.org/changeset/12868">[12868]</a> improvements to specify the features & visibility the `wp-biography` and `wp-textbox` fields support. It hooks to `bp_xprofile_set_field_data_pre_save` introduced in <a href="http://buddypress.trac.wordpress.org/changeset/12869">[12869]</a> to handle field values persistency. It also handles fetching field values during a profile loop and caching these field values for a latter use. 

Props DJPaul, Offereins, needle, netweb, vapvarun

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

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

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpxprofileclassesclassbpxprofilefieldtypewordpressphp"></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.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.php                          (rev 0)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress.php    2021-03-22 19:47:31 UTC (rev 12870)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,241 @@
</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;
+
+/**
+ * Base class for xprofile field types that set/get WordPress profile data from usermeta.
+ *
+ * @since 8.0.0
+ */
+abstract class BP_XProfile_Field_Type_WordPress extends BP_XProfile_Field_Type {
+
+       /**
+        * The usermeta key for the WordPress field.
+        *
+        * @since 8.0.0
+        * @var string The meta key name of this WordPress field.
+        */
+       public $wp_user_key = '';
+
+       /**
+        * The WordPress supported user keys.
+        *
+        * @since 8.0.0
+        * @var string[] The WordPress supported user keys.
+        */
+       public $supported_keys = array();
+
+       /**
+        * WordPress field's visibility setting.
+        *
+        * Defaults to 'public'. This property enforces Field's default visibility.
+        *
+        * @since 8.0.0
+        *
+        * @return string The WordPress field's visibility setting.
+        */
+       public $visibility = 'public';
+
+       /**
+        * Supported features for the WordPress field type.
+        *
+        * @since 8.0.0
+        * @var bool[] The WordPress field supported features.
+        */
+       public static $supported_features = array(
+               'switch_fieldtype'        => false,
+               'required'                => false,
+               'do_autolink'             => false,
+               'allow_custom_visibility' => false,
+               'member_types'            => false,
+       );
+
+       /**
+        * Constructor for the WordPress field type.
+        *
+        * @since 8.0.0
+        */
+       public function __construct() {
+               parent::__construct();
+
+               /**
+                * Fires inside __construct() method for BP_XProfile_Field_Type_WordPress class.
+                *
+                * @since 8.0.0
+                *
+                * @param BP_XProfile_Field_Type_WordPress $this Instance of the field type object.
+                */
+               do_action( 'bp_xprofile_field_type_wordpress', $this );
+
+               // Use the `$wpdb->usermeta` table instead of the $bp->profile->table_name_data one.
+               add_filter( 'bp_xprofile_set_field_data_pre_save', array( $this, 'set_field_value' ), 10, 2 );
+
+               // Set the supported keys.
+               $this->supported_keys = bp_xprofile_get_wp_user_keys();
+       }
+
+       /**
+        * Sanitize the user field before inserting it into db.
+        *
+        * @since 8.0.0
+        *
+        * @param string $value The user field value.
+        */
+       abstract public function sanitize_for_db( $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.
+        */
+       abstract public function sanitize_for_output( $value, $user_id = 0 );
+
+       /**
+        * Sets the WordPress field value.
+        *
+        * @since 8.0.0
+        *
+        * @param boolean $retval Whether to shortcircuit the $bp->profile->table_name_data table.
+        *                        Default `false`.
+        * @param array $field_args {
+        *     An array of arguments.
+        *
+        *     @type object            $field_type_obj Field type object.
+        *     @type BP_XProfile_Field $field          Field object.
+        *     @type integer           $user_id        The user ID.
+        *     @type mixed             $value          Value passed to xprofile_set_field_data().
+        *     @type boolean           $is_required    Whether or not the field is required.
+        * }
+        * @return boolean Whether to shortcircuit the $bp->profile->table_name_data table.
+        */
+       public function set_field_value( $retval = false, $field_args = array() ) {
+               // Check the wp_user_key is valid and supported.
+               if ( ! isset( $field_args['field']->type_obj->wp_user_key ) || $this->wp_user_key !== $field_args['field']->type_obj->wp_user_key || ! in_array( $field_args['field']->type_obj->wp_user_key, $this->supported_keys, true ) ) {
+                       return false;
+               }
+
+               $wp_user_field_value = $this->sanitize_for_db( $field_args['value'] );
+               $bp_displayed_user   = bp_get_displayed_user();
+
+               if ( isset( $bp_displayed_user->updated_keys ) ) {
+                       $bp_displayed_user->updated_keys[ $this->wp_user_key ] = $wp_user_field_value;
+                       $retval = true;
+               } else {
+                       $retval = wp_update_user(
+                               array(
+                                       'ID'               => (int) $field_args['user_id'],
+                                       $this->wp_user_key => $wp_user_field_value,
+                               )
+                       );
+               }
+
+               if ( ! is_wp_error( $retval ) ) {
+                       $retval = true;
+               }
+
+               return $retval;
+       }
+
+       /**
+        * Gets the WordPress field value during an xProfile fields loop.
+        *
+        * This function is used inside `BP_XProfile_ProfileData::get_data_for_user()`
+        * to include the WordPress field value into the xProfile fields loop.
+        *
+        * @since 8.0.0
+        *
+        * @param integer $user_id The user ID.
+        * @param integer $field_id The xProfile field ID.
+        * @return array An array containing the metadata `id`, `value` and `table_name`.
+        */
+       public function get_field_value( $user_id, $field_id = 0 ) {
+               global $wpdb;
+               $wp_field = array(
+                       'id'         => 0,
+                       'value'      => '',
+                       'table_name' => $wpdb->usermeta,
+               );
+
+               if ( 'user_url' === $this->wp_user_key ) {
+                       if ( bp_displayed_user_id() ) {
+                               $wp_field['value'] = bp_get_displayed_user()->userdata->{$this->wp_user_key};
+                       } elseif ( $user_id ) {
+                               $user = get_user_by( 'id', $user_id );
+                               $wp_field['value'] = $user->{$this->wp_user_key};
+                       }
+
+                       $wp_field['id']         = $user_id;
+                       $wp_field['table_name'] = $wpdb->users;
+               } else {
+                       $umeta_key = $this->wp_user_key;
+                       $user_mid  = wp_cache_get( $user_id, 'bp_user_mid' );
+                       if ( ! $user_mid ) {
+                               $user_mid = array();
+                       }
+
+                       if ( ! $user_mid ) {
+                               $list_values = bp_get_user_meta( $user_id, $umeta_key );
+
+                               if ( is_array( $list_values ) ) {
+                                       $wp_field['value'] = reset( $list_values );
+                                       $wp_field['id']    = key( $list_values );
+
+                                       if ( 0 === $wp_field['id'] ) {
+                                               /*
+                                               * We can't just update the WP User Meta cache to key again meta values with meta_ids because of
+                                               * `return maybe_unserialize( $meta_cache[ $meta_key ][0] );` in `get_metadata_raw()`.
+                                               */
+                                               $user_meta_cache = wp_cache_get( $user_id, 'user_meta' );
+
+                                               if ( $user_meta_cache ) {
+                                                       $metas = $wpdb->get_results( $wpdb->prepare( "SELECT umeta_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE user_id = %d ORDER BY umeta_id ASC", $user_id ) );
+
+                                                       if ( $metas ) {
+                                                               foreach ( $user_meta_cache as $meta_key => $meta_values ) {
+                                                                       if ( ! in_array( $meta_key, $this->supported_keys, true ) ) {
+                                                                               continue;
+                                                                       }
+
+                                                                       foreach ( $meta_values as $meta_value ) {
+                                                                               $meta_object = wp_list_filter( $metas, array( 'meta_key' => $meta_key, 'meta_value' => $meta_value ) );
+
+                                                                               if ( 1 === count( $meta_object ) ) {
+                                                                                       $meta_object = reset( $meta_object );
+                                                                                       $user_mid[ $meta_key ][ $meta_object->umeta_id ] = $meta_value;
+
+                                                                                       // Set the meta_id for the requested field.
+                                                                                       if ( $umeta_key === $meta_key ) {
+                                                                                               $wp_field['id'] = $meta_object->umeta_id;
+                                                                                       }
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+
+                                                       // Set the User mid cache.
+                                                       wp_cache_set( $user_id, $user_mid, 'bp_user_mid' );
+                                               }
+                                       }
+                               }
+                       }
+
+                       if ( isset( $user_mid[ $umeta_key ] ) ) {
+                               $wp_field['value'] = reset( $user_mid[ $umeta_key ] );
+                               $wp_field['id']    = key( $user_mid[ $umeta_key ] );
+                       }
+               }
+
+               return $wp_field;
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/bp-xprofile/classes/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>