Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Franz Josef Kaiser committed Oct 8, 2015
2 parents 8fa54e9 + 51fe410 commit e27417a
Show file tree
Hide file tree
Showing 17 changed files with 238 additions and 157 deletions.
24 changes: 16 additions & 8 deletions Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
// Handles the drag & drop uploader logic for the user avatar
// Use this filter to adjust the meta key with which a theme author
// can fetch the attachment ID to load the avatar
$key = apply_filters( 'wcm_user_avatar_meta_key', 'user_avatar' );
$key = apply_filters( 'wcm.avatar.meta_key', 'user_avatar' );

// When switching the plugin, this can be reused to allow
// access to the data in other plugins
register_meta( 'user', $key, null, null );

// Register the 'user_id' and 'screen_id'
// as additional data for attachment uploading
Expand All @@ -45,7 +49,7 @@
add_filter( 'wp_generate_attachment_metadata', [
new Services\AvatarAddMetaService( $key ),
'setup'
] );
], 10, 2 );

// Remove the user meta data when the attachment gets deleted
add_filter( 'delete_attachment', [
Expand All @@ -57,7 +61,7 @@
add_filter( 'delete_user', [
new Services\AvatarDeleteService( $key ),
'setup'
] );
], 10, 2 );

// Save files when using the "Browser Uploader"
foreach ( [ 'load-profile.php', 'load-user-edit.php', ] as $filter )
Expand All @@ -72,7 +76,7 @@
add_filter( 'script_loader_tag', [
new Services\UnderscoreTemplateScripts,
'setup'
] );
], 20, 3 );

// Ajax Model
$ajax = new Models\AjaxAware(
Expand Down Expand Up @@ -108,10 +112,14 @@
'setup'
] );

// Target for the Avatar Backbone template
// Target for the Avatar Backbone filled template
add_action( 'all_admin_notices', function() use ( $key )
{
echo '<div id="tmpl-main--container"></div>';
if ( in_array(
get_current_screen()->base,
[ 'profile', 'user-edit', ]
) )
echo '<div id="tmpl-main--container"></div>';
}, 20 );


Expand All @@ -123,7 +131,7 @@
[ 'profile', 'user-edit', 'media', 'upload', ]
),
'setup'
] );
], 10, 3 );


// Limit allowed MIME types for image uploads
Expand All @@ -134,7 +142,7 @@
[ 'profile', 'user-edit', 'media', 'upload', ]
),
'setup'
] );
], 10, 2 );


# Sizes
Expand Down
41 changes: 21 additions & 20 deletions assets/src/logo/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@
task : 'fetch'
} )
)
.then(
// success
function( response ) {
//console.log( 'RESPONSE', self.attributes );
},
// error
function( reason ) {
//console.log( 'Model:fetch Error',reason );
}
);
.done( function( response ) {
self.set( {
thumb : response.data.thumb,
name : response.data.name,
width : response.data.width,
height : response.data.height,
link : response.data.link
} );
} )
.fail( function( reason ) {
//console.log( 'Model:fetch Error',reason );
} );

/*wp.ajax.post( plugin.action, {
_ajax_nonce : plugin._ajax_nonce,
Expand All @@ -69,7 +71,9 @@

// Triggered by: views.Delete
destroy : function( att_id ) {

var self = this;

$.when(
$.post( this.urlRoot, {
action : plugin.action,
Expand All @@ -78,16 +82,13 @@
task : 'destroy'
} )
)
.then(
// success
function( response ) {
//console.log( 'Model:destroy Success', response );
},
// error
function( reason ) {
//console.log( 'Model:destroy Error', reason );
}
);
.done( function( response ) {
// console.log( 'Model:destroy Success', response );
} )
.fail( function( reason ) {
// console.log( 'Model:destroy Error', reason );
} );

return {};
}
} );
Expand Down
5 changes: 0 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,5 @@
},
"config" : {
"optimize-autoloader" : true
},
"extra" : {
"branch-alias" : {
"dev-dev" : "0.1.x-dev"
}
}
}
15 changes: 10 additions & 5 deletions src/Services/AddUserMetaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ public function __construct( $key, Callable $sanitizer, $error = NULL )


/**
* Adds/ Deletes user meta data
* Sanitizes the provided data
*
* Important: The $result values are completely useless for
* custom error messages as the return value is mostly 'false'
*
* @param int $user_id
*/
public function setup( $user_id = 0 )
{
if ( ! isset( $_POST[$this->key] ) )
if ( ! isset( $_POST[ $this->key ] ) )
return;

$data = $_POST[$this->key];
$data = $_POST[ $this->key ];

// Delete meta if deliberately emptied (deleted)
if ( empty( $data ) )
Expand All @@ -60,10 +63,12 @@ public function setup( $user_id = 0 )
// Sanitize data with a user defined & provided callback
add_filter( "sanitize_user_meta_{$this->key}", $this->sanitizer, 10, 3 );

$result = update_user_meta( $user_id, $this->key, $data );
$result = ! metadata_exists( 'user', $user_id, $this->key )
? add_user_meta( $user_id, $this->key, $data, TRUE )
: update_user_meta( $user_id, $this->key, $data );

#if ( ! is_int( $result ) )
# add_action( 'user_profile_update_errors', [ $this, 'addErrors' ], 10, 3 );
if ( ! is_int( $result ) )
add_action( 'user_profile_update_errors', [ $this, 'addErrors' ], 10, 3 );
}


Expand Down
23 changes: 18 additions & 5 deletions src/Services/AvatarAddMetaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,28 @@ public function setup( Array $meta = [ ], $att_id = 0 )
and isset( $_REQUEST['user_id'] )
)
) {
$user_id = absint( filter_var( $_REQUEST['user_id'], FILTER_VALIDATE_INT ) );
$user_id = absint( filter_var(
$_REQUEST['user_id'],
FILTER_VALIDATE_INT
) );

// Attach attachment ID to user meta as single entry (querying allowed)
update_user_meta( $user_id, $this->key, $att_id );
update_user_meta(
$user_id,
$this->key,
$att_id
);

// Attach user to attachment (single meta entry to allow querying)
add_post_meta( $att_id, 'user_id', $user_id, TRUE );
// Or: Attach user to attachment meta data array
$meta['user_id'] = $user_id;
add_post_meta(
$att_id,
'user_id',
$user_id,
TRUE
);

// Or: Attach user to the attachment meta data array
# $meta['user_id'] = $user_id;
}

return $meta;
Expand Down
20 changes: 10 additions & 10 deletions src/Services/AvatarBrowserUploaderSaveService.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ public function setup( $user_id = 0 )
if ( ! is_array( $file ) )
return;

$result = media_handle_upload( 'async-upload', $post_id = - 1 );
if ( is_wp_error( $result ) )
{
$this->error[] = $result;
add_filter( "{$this->key}_upload_errors", [
$this,
'addError',
], 10, 1 );
}

/*$location = admin_url( sprintf(
'%s.php',
get_current_screen()->base
Expand All @@ -72,16 +82,6 @@ public function setup( $user_id = 0 )
$location
);*/

$result = media_handle_upload( 'async-upload', $post_id = - 1 );
if ( is_wp_error( $result ) )
{
$this->error[] = $result;
add_filter( "{$this->key}_upload_errors", [
$this,
'addError',
], 10, 1 );
}

# Custom Errors: {$action}_prefilter > $file > return $file['error'] = ''; > returns [ 'error' => $message, ]
#if ( is_array( $result ) )
# var_dump( $result['file'], $result['url'], $result['type'] );
Expand Down
38 changes: 34 additions & 4 deletions src/Services/AvatarDimensionLimitService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class AvatarDimensionLimitService implements ServiceInterface
*/
public function setup( Array $file = [ ] )
{
// do not affect other screens
$screen = filter_input( INPUT_POST, 'screen_id', FILTER_SANITIZE_STRING );
if ( ! in_array( $screen, [ 'profile', 'user-edit' ], TRUE ) )
{
return $file;
}

$data = getimagesize( $file['tmp_name'] );
// "Handle" cases where we can't get any info:
if ( ! $data )
Expand All @@ -42,18 +49,41 @@ public function setup( Array $file = [ ] )
}*/

$limit = absint( filter_var( apply_filters(
'wcm.avatar.size_max',
// Maximum
$max = absint( filter_var( apply_filters(
'wcm.avatar.size.max',
1024
), FILTER_SANITIZE_NUMBER_INT ) );

if ( $limit < max( $width, $height ) )
// allow no limit by passing 0 as limit
if ( $max < 1 )
{
return $file;
}

if ( $max < max( $width, $height ) )
{
$file['error'] = sprintf( _x(
'Image exceeds maximum size of %spx × %spx',
'%s are the maximum pixels',
'clients_domain'
), number_format_i18n( $limit ), number_format_i18n( $limit ) );
), number_format_i18n( $max ), number_format_i18n( $max ) );
}

// Minimum
$min = absint( filter_var( apply_filters(
'wcm.avatar.size.min',
32
), FILTER_SANITIZE_NUMBER_INT ) );

// The larger size should not be below the requested min size
if ( $min > max( $width, $height ) )
{
$file['error'] = sprintf( _x(
'Image does not reach the needed minimum size of %spx × %spx',
'%s are the minimum pixels',
'clients_domain'
), number_format_i18n( $min ), number_format_i18n( $min ) );
}

/*$magick = new \Imagick( $file['tmp_name'] );
Expand Down
1 change: 1 addition & 0 deletions src/Services/AvatarFetchDeleteAjaxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function setup()
'link' => esc_url( get_edit_post_link( $att_id ) ),
] );
break;

case 'destroy' :
/** @var bool|\WP_Post $result */
$result = wp_delete_attachment( absint( $att_id ), TRUE );
Expand Down
3 changes: 2 additions & 1 deletion src/Services/AvatarMIMELimitService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public function setup( Array $mimes = [ ] )

if (
empty( $this->where )
or ! in_array( get_current_screen()->base, $this->where )
or current_user_can( $this->cap )
or is_admin()
&& ! in_array( get_current_screen()->base, $this->where )
)
return $mimes;

Expand Down
18 changes: 13 additions & 5 deletions src/Services/AvatarRegisterMetaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ public function setup( Array $params = [ ] )
# and 'add' === get_current_screen()->action
# )
# return $params;
if ( ! is_admin() )
return [
'screen_id' => '',
'user_id' => get_current_user_id(),
];

$user_id = get_current_user_id();
$screen = get_current_screen()->id;

$params['screen_id'] = get_current_screen()->id;
$params['user_id'] = isset( $user_id )
? $user_id
: $GLOBALS['user_id'];
if ( in_array( $screen, [ 'profile', 'user-edit', ], true ) ) {
$user_id = $screen === 'profile'
? get_current_user_id()
: $GLOBALS['user_id'];
$params['screen_id'] = $screen;
$params['user_id'] = $user_id;
}

return $params;
}
Expand Down
Loading

0 comments on commit e27417a

Please sign in to comment.