<!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][9755] trunk/src/bp-core: Avatar UI: Add Conditional functions and new BP Attachment methods</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/9755">9755</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/9755","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>2015-04-16 22:11:05 +0000 (Thu, 16 Apr 2015)</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'>Avatar UI: Add Conditional functions and new BP Attachment methods

- `bp_avatar_is_front_edit()` checks if the user is editing his or a group avatar on front-end
- `bp_avatar_use_webcam()` checks if the Camera feature should be enabled. By default, it is not the case for touch devices as it can introduce some confustions with the possibility of using the camera of the device to take a selfie, save it as an image an upload it using the Browse button of the Avatar UI. Chrome and Firefox are the only browsers supporting getUserMedia.

NB: these two functions can be filtered to disallow the Camera feature or to completely disallow the new Avatar UI if you prefer to carry on using the legacy UI.

`BP_Attachment::script_data()` and `BP_Attachment_Avatar::script_data()` are two new methods to build the javascript localization data. The `bp_attachment_avatar_params` filter is fired if the current object is not a group or a user and can be used to build the params of another object such as a blog. You can also use the filter `bp_attachment_avatar_script_data` if you need to override data for any object.

Props boonebgorges, DJPaul.

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

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcorebpcoreavatarsphp">trunk/src/bp-core/bp-core-avatars.php</a></li>
<li><a href="#trunksrcbpcoreclassesclassbpattachmentavatarphp">trunk/src/bp-core/classes/class-bp-attachment-avatar.php</a></li>
<li><a href="#trunksrcbpcoreclassesclassbpattachmentphp">trunk/src/bp-core/classes/class-bp-attachment.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcorebpcoreavatarsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/bp-core-avatars.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-avatars.php     2015-04-16 21:58:26 UTC (rev 9754)
+++ trunk/src/bp-core/bp-core-avatars.php       2015-04-16 22:11:05 UTC (rev 9755)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1373,3 +1373,67 @@
</span><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"> add_action( 'bp_parse_query', 'bp_core_avatar_reset_query', 10, 1 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Checks whether Avatar UI should be loaded
+ *
+ * @since  BuddyPress (2.3.0)
+ *
+ * @return bool True if Avatar UI should load, false otherwise
+ */
+function bp_avatar_is_front_edit() {
+       $retval = false;
+
+       if ( bp_is_user_change_avatar() && 'crop-image' !== bp_get_avatar_admin_step() ) {
+               $retval = ! bp_core_get_root_option( 'bp-disable-avatar-uploads' );
+       }
+
+       if ( bp_is_active( 'groups' ) ) {
+               // Group creation
+               if ( bp_is_group_create() && bp_is_group_creation_step( 'group-avatar' ) && 'crop-image' !== bp_get_avatar_admin_step() ) {
+                       $retval = ! bp_core_get_root_option( 'bp-disable-avatar-uploads' );
+
+               // Group Manage
+               } elseif ( bp_is_group_admin_page() && bp_is_group_admin_screen( 'group-avatar' ) && 'crop-image' !== bp_get_avatar_admin_step() ) {
+                       $retval = ! bp_core_get_root_option( 'bp-disable-avatar-uploads' );
+               }
+       }
+
+       /**
+        * Use this filter if you need to :
+        * - Load the avatar UI for a component that is !groups or !user (return true regarding your conditions)
+        * - Completely disable the avatar UI introduced in 2.3 (eg: __return_false())
+        *
+        * @since  BuddyPress (2.3.0)
+        *
+        * @var  bool whether to load the Avatar UI
+        */
+       return apply_filters( 'bp_avatar_is_front_edit', $retval );
+}
+
+/**
+ * Checks whether the Webcam Avatar UI part should be loaded
+ *
+ * @since  BuddyPress (2.3.0)
+ *
+ * @return bool True to load the Webcam Avatar UI part. False otherwise.
+ */
+function bp_avatar_use_webcam() {
+       /**
+        * Do not use the webcam feature for mobile devices
+        * to avoid possible confusions.
+        */
+       if ( wp_is_mobile() ) {
+               return false;
+       }
+
+       /**
+        * Use this filter if you need to disable the webcam capture feature
+        * by returning false.
+        *
+        * @since  BuddyPress (2.3.0)
+        *
+        * @var  bool whether to load Webcam Avatar UI part
+        */
+       return apply_filters( 'bp_avatar_use_webcam', true );
+}
</ins></span></pre></div>
<a id="trunksrcbpcoreclassesclassbpattachmentavatarphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/classes/class-bp-attachment-avatar.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/classes/class-bp-attachment-avatar.php  2015-04-16 21:58:26 UTC (rev 9754)
+++ trunk/src/bp-core/classes/class-bp-attachment-avatar.php    2015-04-16 22:11:05 UTC (rev 9755)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -256,4 +256,139 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Return the full and thumb cropped avatars
</span><span class="cx" style="display: block; padding: 0 10px">                return $avatar_types;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Get the user id to set its avatar
+        *
+        * @since BuddyPress (2.3.0)
+        *
+        * @return integer the user ID
+        */
+       private function get_user_id() {
+               $bp = buddypress();
+               $user_id = 0;
+
+               if ( bp_is_user() ) {
+                       $user_id = bp_displayed_user_id();
+               }
+
+               if ( ! empty( $bp->members->admin->user_id ) ) {
+                       $user_id = $bp->members->admin->user_id;
+               }
+
+               return $user_id;
+       }
+
+       /**
+        * Get the group id to set its avatar
+        *
+        * @since BuddyPress (2.3.0)
+        *
+        * @return integer the group id
+        */
+       private function get_group_id() {
+               $group_id = 0;
+
+               if ( bp_is_group() ) {
+                       $group_id = bp_get_current_group_id();
+               }
+
+               return $group_id;
+       }
+
+       /**
+        * Build script datas for the Uploader UI
+        *
+        * @since BuddyPress (2.3.0)
+        *
+        * @return array the javascript localization data
+        */
+       public function script_data() {
+               // Get default script data
+               $script_data = parent::script_data();
+
+               // Defaults to Avatar Backbone script
+               $js_scripts = array( 'bp-avatar' );
+
+               // Default object
+               $object = '';
+
+               // Get the possible item ids
+               $user_id  = $this->get_user_id();
+               $group_id = $this->get_group_id();
+
+               if ( ! empty( $user_id ) ) {
+                       // Should we load the the Webcam Avatar javascript file
+                       if ( bp_avatar_use_webcam() ) {
+                               $js_scripts = array( 'bp-webcam' );
+                       }
+
+                       $script_data['bp_params'] = array(
+                               'object'     => 'user',
+                               'item_id'    => $user_id,
+                               'has_avatar' => bp_get_user_has_avatar( $user_id ),
+                               'nonces'  => array(
+                                       'set'    => wp_create_nonce( 'bp_avatar_cropstore' ),
+                                       'remove' => wp_create_nonce( 'bp_delete_avatar_link' ),
+                               ),
+                       );
+
+                       // Set feedback messages
+                       $script_data['feedback_messages'] = array(
+                               1 => __( 'There was a problem cropping your profile photo.', 'buddypress' ),
+                               2 => __( 'Your new profile photo was uploaded successfully.', 'buddypress' ),
+                               3 => __( 'There was a problem deleting your profile photo. Please try again.', 'buddypress' ),
+                               4 => __( 'Your profile photo was deleted successfully!', 'buddypress' ),
+                       );
+               } elseif ( ! empty( $group_id ) ) {
+                       $script_data['bp_params'] = array(
+                               'object'     => 'group',
+                               'item_id'    => $group_id,
+                               'has_avatar' => bp_get_group_has_avatar( $group_id ),
+                               'nonces'     => array(
+                                       'set'    => wp_create_nonce( 'bp_avatar_cropstore' ),
+                                       'remove' => wp_create_nonce( 'bp_group_avatar_delete' ),
+                               ),
+                       );
+
+                       // Set feedback messages
+                       $script_data['feedback_messages'] = array(
+                               1 => __( 'There was a problem cropping the group profile photo.', 'buddypress' ),
+                               2 => __( 'The group profile photo was uploaded successfully.', 'buddypress' ),
+                               3 => __( 'There was a problem deleting the group profile photo. Please try again.', 'buddypress' ),
+                               4 => __( 'The group profile photo was deleted successfully!', 'buddypress' ),
+                       );
+               } else {
+                       /**
+                        * Use this filter to include specific BuddyPress params for your object
+                        * e.g. Blavatar
+                        *
+                        * @since BuddyPress (2.3.0)
+                        *
+                        * @param array the avatar specific BuddyPress parameters
+                        */
+                       $script_data['bp_params'] = apply_filters( 'bp_attachment_avatar_params', array() );
+               }
+
+               // Include the specific css
+               $script_data['extra_css'] = array( 'bp-avatar' );
+
+               // Include the specific css
+               $script_data['extra_js']  = $js_scripts;
+
+               // Set the object to contextualize the filter
+               if ( isset( $script_data['bp_params']['object'] ) ) {
+                       $object = $script_data['bp_params']['object'];
+               }
+
+               /**
+                * Use this filter to override/extend the avatar script data
+                *
+                * @since BuddyPress (2.3.0)
+                *
+                * @param array  $script_data the avatar script data
+                * @param string $object      the object the avatar belongs to (eg: user or group)
+                */
+               return apply_filters( 'bp_attachment_avatar_script_data', $script_data, $object );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcbpcoreclassesclassbpattachmentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/classes/class-bp-attachment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/classes/class-bp-attachment.php 2015-04-16 21:58:26 UTC (rev 9754)
+++ trunk/src/bp-core/classes/class-bp-attachment.php   2015-04-16 22:11:05 UTC (rev 9755)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -477,4 +477,27 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Finally crop the image
</span><span class="cx" style="display: block; padding: 0 10px">                return wp_crop_image( $r['original_file'], (int) $r['crop_x'], (int) $r['crop_y'], (int) $r['crop_w'], (int) $r['crop_h'], (int) $r['dst_w'], (int) $r['dst_h'], $r['src_abs'], $r['dst_file'] );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Build script datas for the Uploader UI
+        *
+        * Override this method from your child class to build the script datas
+        *
+        * @since BuddyPress (2.3.0)
+        *
+        * @return array the javascript localization data
+        */
+       public function script_data() {
+               $script_data = array(
+                       'action'            => $this->action,
+                       'file_data_name'    => $this->file_input,
+                       'max_file_size'     => $this->original_max_filesize,
+                       'feedback_messages' => array(
+                               1 => __( 'Sorry, uploading the file failed.', 'buddypress' ),
+                               2 => __( 'File successfully uploaded.', 'buddypress' ),
+                       ),
+               );
+
+               return $script_data;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>