Skip to content

Commit

Permalink
fix check for user_id on profile page, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kraftner committed Oct 24, 2016
1 parent ad4ed4d commit 6816934
Showing 1 changed file with 52 additions and 25 deletions.
77 changes: 52 additions & 25 deletions src/Services/AvatarAddMetaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,9 @@ public function __construct( $key )
*/
public function setup( Array $meta = [ ], $att_id = 0 )
{
if (
// "Multi-File Uploader"
(
isset( $_REQUEST['user_id'] )
and isset( $_REQUEST['screen_id'] )
and in_array( $_REQUEST['screen_id'], [
'profile',
'user-edit',
] )
)
// "Browser Uploader"
or (
isset( $_REQUEST['html-upload'] )
and 'Upload' === $_POST['html-upload']
and in_array( get_current_screen()->base, [
'profile',
'user-edit',
] )
and isset( $_REQUEST['user_id'] )
)
) {
$user_id = absint( filter_var(
$_REQUEST['user_id'],
FILTER_VALIDATE_INT
) );
if ( $this->isValidMultiUpload() or $this->isValidBrowserUpload() ) {

$user_id = $this->getUserID();

// Attach attachment ID to user meta as single entry (querying allowed)
update_user_meta(
Expand All @@ -87,4 +65,53 @@ public function setup( Array $meta = [ ], $att_id = 0 )

return $meta;
}

/**
* Returns the User ID, either from the $_REQUEST (on other peoples Profile) or from logged in user (on own profile)
* @return int
*/
protected function getUserID(){

if( isset( $_REQUEST['user_id'] ) ){
$user_id = absint( filter_var(
$_REQUEST['user_id'],
FILTER_VALIDATE_INT
) );
}else{
$user_id = get_current_user_id();
}

return $user_id;

}

/**
* @return bool
*/
protected function isValidMultiUpload(){
return isset( $_REQUEST['user_id'] )
and isset( $_REQUEST['screen_id'] )
and in_array( $_REQUEST['screen_id'], [
'profile',
'user-edit',
]);
}

/**
* @return bool
*/
protected function isValidBrowserUpload(){
return isset( $_REQUEST['html-upload'] )
and 'Upload' === $_POST['html-upload']
and (
(
'user-edit' === get_current_screen()->base
&&
isset( $_REQUEST['user_id'] )
)
or
'profile' === get_current_screen()->base
);
}

}

0 comments on commit 6816934

Please sign in to comment.