<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[BuddyPress] [1760] trunk: Added avatar delete to user and group screens.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd>1760</dd>
<dt>Author</dt> <dd>apeatling</dd>
<dt>Date</dt> <dd>2009-09-02 19:54:19 +0000 (Wed, 02 Sep 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Added avatar delete to user and group screens. Fixes #411</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpcorebpcoreavatarsphp">trunk/bp-core/bp-core-avatars.php</a></li>
<li><a href="#trunkbpgroupsbpgroupstemplatetagsphp">trunk/bp-groups/bp-groups-templatetags.php</a></li>
<li><a href="#trunkbpgroupsphp">trunk/bp-groups.php</a></li>
<li><a href="#trunkbpxprofilebpxprofiletemplatetagsphp">trunk/bp-xprofile/bp-xprofile-templatetags.php</a></li>
<li><a href="#trunkbpxprofilephp">trunk/bp-xprofile.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpcorebpcoreavatarsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-avatars.php (1759 => 1760)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-avatars.php        2009-09-02 17:32:54 UTC (rev 1759)
+++ trunk/bp-core/bp-core-avatars.php        2009-09-02 19:54:19 UTC (rev 1760)
</span><span class="lines">@@ -139,14 +139,37 @@
</span><span class="cx">         $defaults = array(
</span><span class="cx">                 'item_id' =&gt; false,
</span><span class="cx">                 'object' =&gt; 'user', // user OR group OR blog OR custom type (if you use filters)
</span><del>-                'avatar_path' =&gt; false
</del><ins>+                'avatar_dir' =&gt; false
</ins><span class="cx">         );
</span><span class="cx"> 
</span><del>-        $r = wp_parse_args( $args, $defaults );
-        extract( $r, EXTR_SKIP );        
</del><ins>+        $args = wp_parse_args( $args, $defaults );
+        extract( $args, EXTR_SKIP );        
</ins><span class="cx">         
</span><del>-        if ( !$avatar_path )
-                return false;
</del><ins>+        if ( !$item_id ) {
+                if ( 'user' == $object )
+                        $item_id = $bp-&gt;displayed_user-&gt;id;
+                else if ( 'group' == $object )
+                        $item_id = $bp-&gt;groups-&gt;current_group-&gt;id;
+                else if ( 'blog' == $object )
+                        $item_id = $current_blog-&gt;id;
+                        
+                $item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object );
+        
+                if ( !$item_id ) return false;
+        }
+                
+        if ( !$avatar_dir ) {
+                if ( 'user' == $object )
+                        $avatar_dir = 'avatars';
+                else if ( 'group' == $object )
+                        $avatar_dir = 'group-avatars';
+                else if ( 'blog' == $object )
+                        $avatar_dir = 'blog-avatars';
+                        
+                $avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object );
+                
+                if ( !$avatar_dir ) return false;                
+        }
</ins><span class="cx"> 
</span><span class="cx">         if ( 'user' == $object ) {
</span><span class="cx">                 /* Delete any legacy meta entries if this is a user avatar */
</span><span class="lines">@@ -156,13 +179,24 @@
</span><span class="cx">                 delete_usermeta( $item_id, 'bp_core_avatar_v2' );
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if ( $av_dir = opendir( $avatar_path ) ) {
</del><ins>+        $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', WP_CONTENT_DIR . '/blogs.dir/' . BP_ROOT_BLOG . '/files/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );        
+
+        if ( !file_exists( $avatar_folder_dir ) )
+                return false;
+
+        if ( $av_dir = opendir( $avatar_folder_dir ) ) {
</ins><span class="cx">             while ( false !== ( $avatar_file = readdir($av_dir) ) ) {
</span><span class="cx">                         if ( ( preg_match( &quot;/-bpfull/&quot;, $avatar_file ) || preg_match( &quot;/-bpthumb/&quot;, $avatar_file ) ) &amp;&amp; '.' != $avatar_file &amp;&amp; '..' != $avatar_file )
</span><del>-                                @unlink( $avatar_path . '/' . $avatar_file );
</del><ins>+                                @unlink( $avatar_folder_dir . '/' . $avatar_file );                                
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">     closedir($av_dir);
</span><ins>+
+        @rmdir( $avatar_folder_dir );
+
+        do_action( 'bp_core_delete_existing_avatar', $args );
+
+        return true;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> function bp_core_avatar_handle_upload( $file, $upload_dir_filter ) {
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupstemplatetagsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-templatetags.php (1759 => 1760)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-templatetags.php        2009-09-02 17:32:54 UTC (rev 1759)
+++ trunk/bp-groups/bp-groups-templatetags.php        2009-09-02 19:54:19 UTC (rev 1760)
</span><span class="lines">@@ -63,6 +63,25 @@
</span><span class="cx">         &lt;?php }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+
+function bp_get_group_has_avatar() {
+        global $bp;
+
+        if ( !empty( $_FILES ) || !bp_core_fetch_avatar( array( 'item_id' =&gt; $bp-&gt;groups-&gt;current_group-&gt;id, 'object' =&gt; 'group', 'no_grav' =&gt; true ) ) )
+                return false;
+        
+        return true;
+}
+
+function bp_group_avatar_delete_link() {
+        echo bp_get_group_avatar_delete_link();
+}
+        function bp_get_group_avatar_delete_link() {
+                global $bp;
+                
+                return apply_filters( 'bp_get_group_avatar_delete_link', wp_nonce_url( bp_get_group_permalink( $bp-&gt;groups-&gt;current_group ) . '/admin/group-avatar/delete', 'bp_group_avatar_delete' ) );
+        }
+
</ins><span class="cx"> function bp_group_avatar_edit_form() {
</span><span class="cx">         groups_avatar_upload();
</span><span class="cx"> }
</span><span class="lines">@@ -398,7 +417,7 @@
</span><span class="cx">                         'width' =&gt; false,
</span><span class="cx">                         'height' =&gt; false,
</span><span class="cx">                         'class' =&gt; 'avatar',
</span><del>-                        'id' =&gt; 'avatar-crop-preview',
</del><ins>+                        'id' =&gt; false,
</ins><span class="cx">                         'alt' =&gt; __( 'Group avatar', 'buddypress' ) 
</span><span class="cx">                 );
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpgroupsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups.php (1759 => 1760)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups.php        2009-09-02 17:32:54 UTC (rev 1759)
+++ trunk/bp-groups.php        2009-09-02 19:54:19 UTC (rev 1760)
</span><span class="lines">@@ -1017,11 +1017,24 @@
</span><span class="cx">                 
</span><span class="cx">                 if ( !$bp-&gt;is_item_admin )
</span><span class="cx">                         return false;
</span><ins>+                        
+                /* If the group admin has deleted the admin avatar */
+                if ( 'delete' == $bp-&gt;action_variables[1] ) {
+                        
+                        /* Check the nonce */
+                        check_admin_referer( 'bp_group_avatar_delete' );
+                        
+                        if ( bp_core_delete_existing_avatar( array( 'item_id' =&gt; $bp-&gt;groups-&gt;current_group-&gt;id, 'object' =&gt; 'group' ) ) )
+                                bp_core_add_message( __( 'Your avatar was deleted successfully!', 'buddypress' ) );
+                        else
+                                bp_core_add_message( __( 'There was a problem deleting that avatar, please try again.', 'buddypress' ), 'error' );
</ins><span class="cx">                 
</span><ins>+                }        
+                
</ins><span class="cx">                 $bp-&gt;avatar_admin-&gt;step = 'upload-image';
</span><span class="cx"> 
</span><span class="cx">                 if ( !empty( $_FILES ) ) {
</span><del>-        
</del><ins>+
</ins><span class="cx">                         /* Check the nonce */
</span><span class="cx">                         check_admin_referer( 'bp_avatar_upload' );
</span><span class="cx"> 
</span><span class="lines">@@ -1032,12 +1045,12 @@
</span><span class="cx">                                 /* Make sure we include the jQuery jCrop file for image cropping */
</span><span class="cx">                                 add_action( 'wp', 'bp_core_add_jquery_cropper' );
</span><span class="cx">                         }
</span><del>-        
</del><ins>+
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* If the image cropping is done, crop the image and save a full/thumb version */
</span><span class="cx">                 if ( isset( $_POST['avatar-crop-submit'] ) ) {
</span><del>-        
</del><ins>+
</ins><span class="cx">                         /* Check the nonce */
</span><span class="cx">                         check_admin_referer( 'bp_avatar_cropstore' );
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpxprofilebpxprofiletemplatetagsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-xprofile/bp-xprofile-templatetags.php (1759 => 1760)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-xprofile/bp-xprofile-templatetags.php        2009-09-02 17:32:54 UTC (rev 1759)
+++ trunk/bp-xprofile/bp-xprofile-templatetags.php        2009-09-02 19:54:19 UTC (rev 1760)
</span><span class="lines">@@ -639,7 +639,22 @@
</span><span class="cx">         
</span><span class="cx">                 return apply_filters( 'bp_get_current_profile_group_id', $profile_group_id ); // admin/profile/edit/[group-id]
</span><span class="cx">         }
</span><ins>+
+function bp_avatar_delete_link() {
+        echo bp_get_avatar_delete_link();
+}
+        function bp_get_avatar_delete_link() {
+                global $bp;
+                
+                return apply_filters( 'bp_get_avatar_delete_link', wp_nonce_url( $bp-&gt;loggedin_user-&gt;domain . $bp-&gt;profile-&gt;slug . '/change-avatar/delete-avatar/', 'bp_delete_avatar_link' ) );
+        }
+
+function bp_get_user_has_avatar() {
+        if ( !bp_core_fetch_avatar( array( 'item_id' =&gt; $bp-&gt;displayed_user-&gt;id, 'no_grav' =&gt; true ) ) )
+                return false;
</ins><span class="cx">         
</span><ins>+        return true;
+}
</ins><span class="cx"> 
</span><span class="cx"> function bp_edit_profile_button() {
</span><span class="cx">         global $bp;
</span></span></pre></div>
<a id="trunkbpxprofilephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-xprofile.php (1759 => 1760)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-xprofile.php        2009-09-02 17:32:54 UTC (rev 1759)
+++ trunk/bp-xprofile.php        2009-09-02 19:54:19 UTC (rev 1760)
</span><span class="lines">@@ -444,18 +444,21 @@
</span><span class="cx"> function xprofile_action_delete_avatar() {
</span><span class="cx">         global $bp;
</span><span class="cx"> 
</span><del>-        if ( 'delete-avatar' != $bp-&gt;current_action )
</del><ins>+        if ( $bp-&gt;profile-&gt;slug != $bp-&gt;current_component || 'change-avatar' != $bp-&gt;current_action || 'delete-avatar' != $bp-&gt;action_variables[0] )
</ins><span class="cx">                 return false;
</span><del>-
-        if ( !check_admin_referer( 'bp_delete_avatar_link' ) )
</del><ins>+        
+        /* Check the nonce */
+        check_admin_referer( 'bp_delete_avatar_link' );
+        
+        if ( !bp_is_home() &amp;&amp; !is_site_admin() )
</ins><span class="cx">                 return false;
</span><span class="cx">         
</span><del>-        if ( !bp_is_home() )
-                return false;
</del><ins>+        if ( bp_core_delete_existing_avatar( array( 'item_id' =&gt; $bp-&gt;displayed_user-&gt;id ) ) )
+                bp_core_add_message( __( 'Your avatar was deleted successfully!', 'buddypress' ) );
+        else
+                bp_core_add_message( __( 'There was a problem deleting that avatar, please try again.', 'buddypress' ), 'error' );
</ins><span class="cx"> 
</span><del>-        bp_core_delete_avatar();
-        add_action( 'wp_head', 'bp_core_add_cropper_js' );
-        bp_core_load_template( apply_filters( 'xprofile_template_delete_avatar', 'profile/change-avatar' ) );
</del><ins>+        bp_core_redirect( $_SERVER['HTTP_REFERER'] ); 
</ins><span class="cx"> }
</span><span class="cx"> add_action( 'wp', 'xprofile_action_delete_avatar', 3 );
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>