<!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][10192] trunk/src/bp-core/bp-core-attachments.php: Cover Image: improve the modularity of the upload process</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/10192">10192</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/10192","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-10-05 23:56:42 +0000 (Mon, 05 Oct 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'>Cover Image: improve the modularity of the upload process
Create a specific function to generate the cover image file according to the settings of the feature, the upload process will use.
See <a href="http://buddypress.trac.wordpress.org/ticket/6570">#6570</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcorebpcoreattachmentsphp">trunk/src/bp-core/bp-core-attachments.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcorebpcoreattachmentsphp"></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-attachments.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-attachments.php 2015-10-05 22:55:15 UTC (rev 10191)
+++ trunk/src/bp-core/bp-core-attachments.php 2015-10-05 23:56:42 UTC (rev 10192)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -887,6 +887,87 @@
</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"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Generate the cover image file.
+ *
+ * @since 2.4.0
+ *
+ * @param array $args {
+ * @type string $file The absolute path to the image. Required.
+ * @type string $component The component for the object (eg: groups, xprofile). Required.
+ * @type string $cover_image_dir The Cover image dir to write the image into. Required.
+ * }
+ * @param BP_Attachment_Cover_Image $cover_image_class The class to use to fit the cover image.
+ * @return bool|array An array containing cover image data on success, false otherwise.
+ */
+function bp_attachments_cover_image_generate_file( $args = array(), $cover_image_class = null ) {
+ // Bail if an argument is missing
+ if ( empty( $args['file'] ) || empty( $args['component'] ) || empty( $args['cover_image_dir'] ) ) {
+ return false;
+ }
+
+ // Get advised dimensions for the cover image
+ $dimensions = bp_attachments_get_cover_image_dimensions( $args['component'] );
+
+ // No dimensions or the file does not match with the cover image dir, stop!
+ if ( false === $dimensions || $args['file'] !== $args['cover_image_dir'] . '/' . wp_basename( $args['file'] ) ) {
+ return false;
+ }
+
+ if ( ! is_a( $cover_image_class, 'BP_Attachment_Cover_Image' ) ) {
+ $cover_image_class = new BP_Attachment_Cover_Image();
+ }
+
+ // Make sure the file is inside the Cover Image Upload path.
+ if ( false === strpos( $args['file'], $cover_image_class->upload_path ) ) {
+ return false;
+ }
+
+ // Resize the image so that it fit with the cover image dimensions
+ $cover_image = $cover_image_class->fit( $args['file'], $dimensions );
+ $is_too_small = false;
+
+ // Image is too small in width and height
+ if ( empty( $cover_image ) ) {
+ $cover_file = $cover_image_class->generate_filename( $args['file'] );
+ @rename( $args['file'], $cover_file );
+
+ // It's too small!
+ $is_too_small = true;
+ } elseif ( ! empty( $cover_image['path'] ) ) {
+ $cover_file = $cover_image['path'];
+
+ // Image is too small in width or height
+ if ( $cover_image['width'] < $dimensions['width'] || $cover_image['height'] < $dimensions['height'] ) {
+ $is_too_small = true;
+ }
+ }
+
+ // We were not able to generate the cover image file.
+ if ( empty( $cover_file ) ) {
+ return false;
+ }
+
+ // Do some clean up with old cover image, now a new one is set.
+ $cover_basename = wp_basename( $cover_file );
+
+ if ( $att_dir = opendir( $args['cover_image_dir'] ) ) {
+ while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) {
+ // skip directories and the new cover image
+ if ( 2 < strlen( $attachment_file ) && 0 !== strpos( $attachment_file, '.' ) && $cover_basename !== $attachment_file ) {
+ @unlink( $args['cover_image_dir'] . '/' . $attachment_file );
+ }
+ }
+ }
+
+ // Finally return needed data.
+ return array(
+ 'cover_file' => $cover_file,
+ 'cover_basename' => $cover_basename,
+ 'is_too_small' => $is_too_small
+ );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Ajax Upload and set a cover image
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.4.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -989,42 +1070,9 @@
</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">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Get advised dimensions for the cover image
- $dimensions = bp_attachments_get_cover_image_dimensions( $object_data['component'] );
-
- // Resize the image so that it fit with the cover image dimensions
- $cover_image = $cover_image_attachment->fit( $uploaded['file'], $dimensions );
- $is_too_small = false;
-
- // Image is too small in width and height
- if ( empty( $cover_image ) ) {
- $cover_file = $cover_image_attachment->generate_filename( $uploaded['file'] );
- @rename( $uploaded['file'], $cover_file );
-
- // It's too small!
- $is_too_small = true;
- } elseif ( ! empty( $cover_image['path'] ) ) {
- $cover_file = $cover_image['path'];
-
- if ( $cover_image['width'] < $dimensions['width'] || $cover_image['height'] < $dimensions['height'] ) {
- $is_too_small = true;
- }
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Default error message
</span><span class="cx" style="display: block; padding: 0 10px"> $error_message = __( 'There was a problem uploading the cover image.', 'buddypress' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( empty( $cover_file ) ) {
- // Upload error response
- bp_attachments_json_response( false, $is_html4, array(
- 'type' => 'upload_error',
- 'message' => $error_message,
- ) );
- }
-
- // Set the basename for the cover file
- $cover_basename = wp_basename( $cover_file );
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Get BuddyPress Attachments Uploads Dir datas
</span><span class="cx" style="display: block; padding: 0 10px"> $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1039,24 +1087,35 @@
</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">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Clean up the cover dir to only keep the uploaded cover image
- if ( $att_dir = opendir( $cover_dir ) ) {
- while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) {
- // skip directories and the new cover image
- if ( 2 < strlen( $attachment_file ) && 0 !== strpos( $attachment_file, '.' ) && $cover_basename !== $attachment_file ) {
- @unlink( $cover_dir . '/' . $attachment_file );
- }
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Generate the cover image so that it fit to feature's dimensions
+ *
+ * Unlike the Avatar, Uploading and generating the cover image is happening during
+ * the same Ajax request, as we already instantiated the BP_Attachment_Cover_Image
+ * class, let's use it.
+ */
+ $cover = bp_attachments_cover_image_generate_file( array(
+ 'file' => $uploaded['file'],
+ 'component' => $object_data['component'],
+ 'cover_image_dir' => $cover_dir
+ ), $cover_image_attachment );
+
+ if ( ! $cover ) {
+ // Upload error response
+ bp_attachments_json_response( false, $is_html4, array(
+ 'type' => 'upload_error',
+ 'message' => $error_message,
+ ) );
</ins><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"> // Build the url to the file
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $cover_url = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $cover_subdir . '/' . $cover_basename;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cover_url = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $cover_subdir . '/' . $cover['cover_basename'];
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Init Feedback code, 1 is success
</span><span class="cx" style="display: block; padding: 0 10px"> $feedback_code = 1;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // 0 is the size warning
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $is_too_small ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $cover['is_too_small'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $feedback_code = 0;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre>
</div>
</div>
</body>
</html>