<!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][10156] trunk/src/bp-core: Introduce the `BP_Attachment_Cover_Image` 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 { 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/10156">10156</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/10156","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-09-29 22:52:13 +0000 (Tue, 29 Sep 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'>Introduce the `BP_Attachment_Cover_Image` class

This class extends the BP_Attachment one to manage the uploaded BuddyPress Cover Images.

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

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcorebpcoreclassesphp">trunk/src/bp-core/bp-core-classes.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcbpcoreclassesclassbpattachmentcoverimagephp">trunk/src/bp-core/classes/class-bp-attachment-cover-image.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcorebpcoreclassesphp"></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-classes.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-classes.php     2015-09-29 22:42:56 UTC (rev 10155)
+++ trunk/src/bp-core/bp-core-classes.php       2015-09-29 22:52:13 UTC (rev 10156)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -23,3 +23,4 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/classes/class-bp-media-extractor.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/classes/class-bp-attachment.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/classes/class-bp-attachment-avatar.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require dirname( __FILE__ ) . '/classes/class-bp-attachment-cover-image.php';
</ins></span></pre></div>
<a id="trunksrcbpcoreclassesclassbpattachmentcoverimagephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/bp-core/classes/class-bp-attachment-cover-image.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-cover-image.php                             (rev 0)
+++ trunk/src/bp-core/classes/class-bp-attachment-cover-image.php       2015-09-29 22:52:13 UTC (rev 10156)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,262 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Core Cover Image attachment class.
+ *
+ * @package BuddyPress
+ * @subpackage Core
+ */
+
+// Exit if accessed directly
+defined( 'ABSPATH' ) || exit;
+
+/**
+ * BP Attachment Cover Image class.
+ *
+ * Extends BP Attachment to manage the cover images uploads.
+ *
+ * @since 2.4.0
+ */
+class BP_Attachment_Cover_Image extends BP_Attachment {
+       /**
+        * The constuctor
+        *
+        * @since 2.4.0
+        */
+       public function __construct() {
+               // Allowed cover image types & upload size
+               $allowed_types        = bp_attachments_get_allowed_types( 'cover_image' );
+               $max_upload_file_size = bp_attachments_get_max_upload_file_size( 'cover_image' );
+
+               parent::__construct( array(
+                       'action'                => 'bp_cover_image_upload',
+                       'file_input'            => 'file',
+                       'original_max_filesize' => $max_upload_file_size,
+                       'base_dir'              => bp_attachments_uploads_dir_get( 'dir' ),
+                       'required_wp_files'     => array( 'file', 'image' ),
+
+                       // Specific errors for cover images
+                       'upload_error_strings'  => array(
+                               11  => sprintf( __( 'That image is too big. Please upload one smaller than %s', 'buddypress' ), size_format( $max_upload_file_size ) ),
+                               12  => sprintf( _n( 'Please upload only this file type: %s.', 'Please upload only these file types: %s.', count( $allowed_types ), 'buddypress' ), self::get_cover_image_types( $allowed_types ) ),
+                       ),
+               ) );
+       }
+
+       /**
+        * Gets the available cover image types.
+        *
+        * @since 2.4.0
+        *
+        * @param array $allowed_types Array of allowed cover image types.
+        *
+        * @return string comma separated list of allowed cover image types.
+        */
+       public static function get_cover_image_types( $allowed_types = array() ) {
+               $types = array_map( 'strtoupper', $allowed_types );
+               $comma = _x( ',', 'cover image types separator', 'buddypress' );
+               return join( $comma . ' ', $types );
+       }
+
+       /**
+        * Cover image specific rules.
+        *
+        * Adds an error if the cover image size or type don't match BuddyPress needs.
+        * The error code is the index of $upload_error_strings.
+        *
+        * @since 2.4.0
+        *
+        * @param  array $file the temporary file attributes (before it has been moved).
+        *
+        * @return array the file with extra errors if needed.
+        */
+       public function validate_upload( $file = array() ) {
+               // Bail if already an error
+               if ( ! empty( $file['error'] ) ) {
+                       return $file;
+               }
+
+               // File size is too big
+               if ( $file['size'] > $this->original_max_filesize ) {
+                       $file['error'] = 11;
+
+               // File is of invalid type
+               } elseif ( ! bp_attachments_check_filetype( $file['tmp_name'], $file['name'], bp_attachments_get_allowed_mimes( 'cover_image' ) ) ) {
+                       $file['error'] = 12;
+               }
+
+               // Return with error code attached
+               return $file;
+       }
+
+       /**
+        * Set the directory when uploading a file
+        *
+        * @since 2.4.0
+        *
+        * @return array upload data (path, url, basedir...)
+        */
+       public function upload_dir_filter() {
+               // Default values are for profiles
+               $object_id = bp_displayed_user_id();
+
+               if ( empty( $object_id ) ) {
+                       $object_id = bp_loggedin_user_id();
+               }
+
+               $object_directory = 'members';
+
+               // We're in a group, edit default values
+               if ( bp_is_group() || bp_is_group_create() ) {
+                       $object_id        = bp_get_current_group_id();
+                       $object_directory = 'groups';
+               }
+
+               // Set the subdir
+               $subdir  = '/' . $object_directory . '/' . $object_id . '/cover-image';
+
+               return apply_filters( 'bp_attachments_cover_image_upload_datas', array(
+                       'path'    => $this->upload_path . $subdir,
+                       'url'     => $this->url . $subdir,
+                       'subdir'  => $subdir,
+                       'basedir' => $this->upload_path,
+                       'baseurl' => $this->url,
+                       'error'   => false
+               ) );
+       }
+
+       /**
+        * Adjust the cover image to fit with advised width & height.
+        *
+        * @since 2.4.0
+        *
+        * @param string $file the absolute path to the file.
+        * @return mixed
+        */
+       public function fit( $file = '', $dimensions = array() ) {
+               if ( empty( $dimensions['width'] ) || empty( $dimensions['height'] ) ) {
+                       return false;
+               }
+
+               // Get image size
+               $size   = @getimagesize( $file );
+               $retval = false;
+
+               // Check image size and shrink if too large
+               if ( $size[0] > $dimensions['width'] || $size[1] > $dimensions['height'] ) {
+                       $editor = wp_get_image_editor( $file );
+
+                       if ( ! is_wp_error( $editor ) ) {
+                               $editor->set_quality( 100 );
+
+                               $resized = $editor->resize( $dimensions['width'], $dimensions['height'], true );
+                               if ( ! is_wp_error( $resized ) ) {
+                                       $cover   = $editor->save( $this->generate_filename( $file ) );
+                               } else {
+                                       $retval = $resized;
+                               }
+
+                               // Check for cover creation errors
+                               if ( ( false === $retval ) && is_wp_error( $cover ) ) {
+                                       $retval = $cover;
+                               }
+
+                               // Cover is good so proceed
+                               if ( false === $retval ) {
+                                       $retval = $cover;
+                               }
+
+                       } else {
+                               $retval = $editor;
+                       }
+               }
+
+               return $retval;
+       }
+
+       /**
+        * Generate a filename for the cover image
+        *
+        * @since 2.4.0
+        *
+        * @param  string $file the absolute path to the file.
+        * @return string       the absolute path to the new file name
+        */
+       public function generate_filename( $file = '' ) {
+               if ( empty( $file ) || ! file_exists( $file ) ) {
+                       return false;
+               }
+
+               $info    = pathinfo( $file );
+               $dir     = $info['dirname'];
+               $ext     = strtolower( $info['extension'] );
+               $name    = wp_hash( $file . time() ) . '-bp-cover-image';
+
+               return trailingslashit( $dir ) . "{$name}.{$ext}";
+       }
+
+       /**
+        * Build script datas for the Uploader UI
+        *
+        * @since 2.4.0
+        *
+        * @return array the javascript localization data
+        */
+       public function script_data() {
+               // Get default script data
+               $script_data = parent::script_data();
+
+               if ( bp_is_user() ) {
+                       $item_id = bp_displayed_user_id();
+
+                       $script_data['bp_params'] = array(
+                               'object'          => 'user',
+                               'item_id'         => $item_id,
+                               'has_cover_image' => bp_attachments_get_user_has_cover_image( $item_id ),
+                               'nonces'  => array(
+                                       'remove' => wp_create_nonce( 'bp_delete_cover_image' ),
+                               ),
+                       );
+
+                       // Set feedback messages
+                       $script_data['feedback_messages'] = array(
+                               1 => __( 'Your new cover image was uploaded successfully.', 'buddypress' ),
+                               2 => __( 'There was a problem deleting your cover image. Please try again.', 'buddypress' ),
+                               3 => __( 'Your cover image was deleted successfully!', 'buddypress' ),
+                       );
+               } elseif ( bp_is_group() ) {
+                       $item_id = bp_get_current_group_id();
+
+                       $script_data['bp_params'] = array(
+                               'object'          => 'group',
+                               'item_id'         => bp_get_current_group_id(),
+                               'has_cover_image' => bp_attachments_get_group_has_cover_image( $item_id ),
+                               'nonces'  => array(
+                                       'remove' => wp_create_nonce( 'bp_delete_cover_image' ),
+                               ),
+                       );
+
+                       // Set feedback messages
+                       $script_data['feedback_messages'] = array(
+                               1 => __( 'The group cover image was uploaded successfully.', 'buddypress' ),
+                               2 => __( 'There was a problem deleting the group cover image. Please try again.', 'buddypress' ),
+                               3 => __( 'The group cover image was deleted successfully!', 'buddypress' ),
+                       );
+               } else {
+                       /**
+                        * Use this filter to include specific BuddyPress params for your object.
+                        * e.g. Cover image for blogs single item.
+                        *
+                        * @since 2.4.0
+                        *
+                        * @param array $value The cover image specific BuddyPress parameters.
+                        */
+                       $script_data['bp_params'] = apply_filters( 'bp_attachment_cover_image_params', array() );
+               }
+
+               // Include our specific js & css
+               $script_data['extra_js']  = array( 'bp-cover-image' );
+               $script_data['extra_css'] = array( 'bp-avatar' );
+
+               return apply_filters( 'bp_attachments_cover_image_script_data', $script_data );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/bp-core/classes/class-bp-attachment-cover-image.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>