<!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][12871] trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress-textbox.php: Introduce the `wp-textbox` xProfile field type's class.</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/12871">12871</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/12871","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:49:18 +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 `wp-textbox` xProfile field type's class.
`BP_XProfile_Field_Type_WordPress_Textbox` deals with the following WordPress usermeta keys, as a text input is used to fill their values:
- `first_name`,
- `last_name`,
- `user_url`,
- and potential WP contact methods.
It contains a specific metabox callback to let Admins select the WordPress usermeta key the field will specifically deal with.
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="#trunksrcbpxprofileclassesclassbpxprofilefieldtypewordpresstextboxphp">trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress-textbox.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpxprofileclassesclassbpxprofilefieldtypewordpresstextboxphp"></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-textbox.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-textbox.php (rev 0)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress-textbox.php 2021-03-22 19:49:18 UTC (rev 12871)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,332 @@
</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 xProfile regular field type.
+ *
+ * @since 8.0.0
+ */
+class BP_XProfile_Field_Type_WordPress_Textbox extends BP_XProfile_Field_Type_WordPress {
+
+ /**
+ * Constructor for the WordPress regular field type.
+ *
+ * @since 8.0.0
+ */
+ public function __construct() {
+ parent::__construct();
+
+ $this->category = _x( 'WordPress Fields', 'xprofile field type category', 'buddypress' );
+ $this->name = _x( 'Text field', 'xprofile field type', 'buddypress' );
+ $this->accepts_null_value = true;
+ $this->do_settings_section = true;
+ $this->wp_user_key = '';
+
+ $this->set_format( '/^.*$/', 'replace' );
+
+ /**
+ * Fires inside __construct() method for BP_XProfile_Field_Type_WordPress_Textbox class.
+ *
+ * @since 8.0.0
+ *
+ * @param BP_XProfile_Field_Type_WordPress_Textbox $this Instance of the field type object.
+ */
+ do_action( 'bp_xprofile_field_type_wordpress_textbox', $this );
+
+ /*
+ * As we are using an xProfile field meta to store the WordPress field meta key we need
+ * to make sure $this->wp_user_key is set before once the field has been populated.
+ */
+ add_action( 'bp_xprofile_field', array( $this, 'set_wp_user_key' ), 10 );
+ }
+
+ /**
+ * Sets the WordPress field wp_user_key property before saving the xProfile field.
+ *
+ * @since 8.0.0
+ *
+ * @param BP_XProfile_Field $field Field object.
+ */
+ public function set_wp_user_key( $field ) {
+ if ( ! $this->wp_user_key && 'wp-textbox' === $field->type ) {
+ $this->wp_user_key = self::get_field_settings( $field->id );
+ }
+ }
+
+ /**
+ * 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 ) {
+ if ( ! $this->wp_user_key ) {
+ $this->wp_user_key = self::get_field_settings( $field_id );
+ }
+
+ return parent::get_field_value( $user_id, $field_id );
+ }
+
+ /**
+ * 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 ) {
+ if ( 'user_url' === $this->wp_user_key ) {
+ return esc_url_raw( $value );
+ }
+
+ return sanitize_text_field( $value );
+ }
+
+ /**
+ * Sanitize the user field before displaying it as an attribute.
+ *
+ * @since 8.0.0
+ *
+ * @param string $value The user field value.
+ * @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'] );
+ }
+
+ if ( ! $this->wp_user_key ) {
+ $this->wp_user_key = self::get_field_settings( bp_get_the_profile_field_id() );
+ }
+
+ if ( 'user_url' === $this->wp_user_key ) {
+ if ( bp_displayed_user_id() ) {
+ $field_value = bp_get_displayed_user()->userdata->{$this->wp_user_key};
+ } elseif ( $user_id ) {
+ $user = get_user_by( 'id', $user_id );
+ $field_value = $user->{$this->wp_user_key};
+ }
+ } else {
+ $field_value = bp_get_user_meta( $user_id, $this->wp_user_key, true );
+ }
+
+ $r = wp_parse_args( $raw_properties, array(
+ 'type' => 'text',
+ 'value' => $this->sanitize_for_output( $field_value, $user_id ),
+ ) );
+ ?>
+
+ <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 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 = wp_parse_args( $raw_properties, array(
+ 'type' => 'text'
+ ) ); ?>
+
+ <label for="<?php bp_the_profile_field_input_name(); ?>" class="screen-reader-text"><?php
+ /* translators: accessibility text */
+ esc_html_e( 'WordPress field', 'buddypress' );
+ ?></label>
+ <input <?php echo $this->get_edit_field_html_elements( $r ); ?>>
+
+ <?php
+ }
+
+ /**
+ * Get settings for a given WordPress field.
+ *
+ * @since 8.0.0
+ *
+ * @param int $field_id ID of the field.
+ * @return string The meta_key used for this field.
+ */
+ public static function get_field_settings( $field_id ) {
+ $wp_user_key = bp_xprofile_get_meta( $field_id, 'field', 'wp_user_key', true );
+
+ return sanitize_key( $wp_user_key );
+ }
+
+ /**
+ * Save settings from the field edit screen in the Dashboard.
+ *
+ * @since 8.0.0
+ *
+ * @param int $field_id ID of the field.
+ * @param array $settings Array of settings.
+ * @return bool True on success.
+ */
+ public function admin_save_settings( $field_id, $settings ) {
+ $existing_setting = self::get_field_settings( $field_id );
+ $setting = '';
+
+ if ( isset( $settings['wp_user_key'] ) ) {
+ $setting = sanitize_key( $settings['wp_user_key'] );
+ }
+
+ if ( $setting && $setting !== $existing_setting ) {
+ bp_xprofile_update_meta( $field_id, 'field', 'wp_user_key', $setting );
+ }
+
+ return true;
+ }
+
+ /**
+ * 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 = '' ) {
+ $type = array_search( get_class( $this ), bp_xprofile_get_field_types() );
+
+ if ( false === $type ) {
+ return;
+ }
+
+ $style = 'margin-top: 15px;';
+ if ( $current_field->type !== $type ) {
+ $style .= ' display: none;';
+ };
+
+ $setting = self::get_field_settings( $current_field->id );
+
+ $wp_labels = array_merge(
+ array(
+ 'first_name' => _x( 'First Name', 'xpofile wp-textbox field type label', 'buddypress' ),
+ 'last_name' => _x( 'Last Name', 'xpofile wp-textbox field type label', 'buddypress' ),
+ 'user_url' => _x( 'Website', 'xpofile wp-textbox field type label', 'buddypress' ),
+ ),
+ wp_get_user_contact_methods()
+ );
+ ?>
+ <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $style ); ?>">
+ <h3><?php esc_html_e( 'Select the information you want to use for your WordPress field.', 'buddypress' ); ?></h3>
+
+ <div class="inside" aria-live="polite" aria-atomic="true" aria-relevant="all">
+ <div class="bp-option">
+ <ul>
+ <?php
+ foreach ( $this->supported_keys as $key ) {
+ if ( 'description' === $key || ! isset( $wp_labels[ $key ] ) ) {
+ continue;
+ }
+
+ printf(
+ '<li>
+ <label for="wp-textbox-wp_user_key-%1$s">
+ <input type="radio" id="wp-textbox-wp_user_key-%1$s" name="field-settings[wp_user_key]" value="%1$s" %2$s/>
+ %3$s
+ </label>
+ </li>',
+ esc_attr( $key ),
+ checked( $key, $setting, false ),
+ esc_html( $wp_labels[ $key ] )
+ );
+ }
+ ?>
+ </ul>
+ </div>
+ </div>
+ </div>
+ <?php
+ }
+
+ /**
+ * Format WordPress field values 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 = '' ) {
+ $wp_user_key = self::get_field_settings( $field_id );
+
+ if ( ! $wp_user_key ) {
+ return '';
+ }
+
+ if ( 'user_url' === $wp_user_key ) {
+ $sanitized_website = sanitize_user_field( $wp_user_key, $field_value, bp_displayed_user_id(), 'attribute' );
+ return sprintf( '<a href="%1$s" rel="nofollow">%1$s</a>', $sanitized_website );
+ }
+
+ return sanitize_user_field( $wp_user_key, $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-textbox.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>