Skip to content

Commit

Permalink
Use core API for generating email hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed Apr 29, 2018
1 parent 2d333a8 commit 64a2287
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
31 changes: 23 additions & 8 deletions includes/avatar-privacy/class-user-avatar-upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ class User_Avatar_Upload {
*/
private $base_url;

/**
* The core API.
*
* @var \Avatar_Privacy_Core
*/
private $core;

/**
* Creates a new instance.
*
Expand All @@ -110,6 +117,15 @@ public function __construct( $plugin_file, Filesystem_Cache $file_cache ) {
$this->file_cache = $file_cache;
}

/**
* Sets the core API instance to use.
*
* @param \Avatar_Privacy_Core $core The core API.
*/
public function set_core( \Avatar_Privacy_Core $core ) {
$this->core = $core;
}

/**
* Retrieves the markup for uploading user avatars.
*
Expand Down Expand Up @@ -264,7 +280,7 @@ public function get_unique_filename( $directory, $filename, $extension ) {
* @param int $user_id The user ID.
*/
public function delete_uploaded_avatar( $user_id ) {
$hash = \get_user_meta( $user_id, \Avatar_Privacy_Core::EMAIL_HASH_META_KEY, true );
$hash = $this->core->get_user_hash( $user_id );

$this->file_cache->invalidate( 'user', "#/{$hash}-[1-9][0-9]*\.[a-z]{3}\$#" );

Expand All @@ -277,21 +293,20 @@ public function delete_uploaded_avatar( $user_id ) {
/**
* Retrieves the URL for the given default icon type.
*
* @param string[] $avatar The full-size avatar image information.
* @param string $email The mail address used to generate the identity hash.
* @param int $size The requested size in pixels.
* @param \Avatar_Privacy_Core $core The core API.
* @param bool $force Optional. Whether to force the regeneration of the icon. Default false.
* @param string[] $avatar The full-size avatar image information.
* @param string $email The mail address used to generate the identity hash.
* @param int $size The requested size in pixels.
* @param bool $force Optional. Whether to force the regeneration of the icon. Default false.
*
* @return string
*/
public function get_icon_url( array $avatar, $email, $size, \Avatar_Privacy_Core $core, $force = false ) {
public function get_icon_url( array $avatar, $email, $size, $force = false ) {
if ( empty( $this->base_dir ) || empty( $this->base_url ) ) {
$this->base_url = $this->file_cache->get_base_url();
$this->base_dir = $this->file_cache->get_base_dir();
}

$hash = $core->get_hash( $email );
$hash = $this->core->get_hash( $email );
$extension = \Avatar_Privacy\Components\Images::FILE_EXTENSION[ $avatar['type'] ];
$filename = "user/{$this->get_sub_dir( $hash )}/{$hash}-{$size}.{$extension}";
$target = "{$this->base_dir}{$filename}";
Expand Down
2 changes: 1 addition & 1 deletion includes/avatar-privacy/components/class-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public function gravatar_icon_url( $url, $email, $size, $user_id = false, $ratin
* @return string
*/
public function user_avatar_icon_url( array $avatar, $email, $size, $force = false ) {
return $this->user_avatar->get_icon_url( $avatar, $email, $size, $this->core, $force );
return $this->user_avatar->get_icon_url( $avatar, $email, $size, $force );
}


Expand Down
2 changes: 2 additions & 0 deletions includes/avatar-privacy/components/class-user-profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public function __construct( $plugin_file, User_Avatar_Upload $upload ) {
* @return void
*/
public function run( \Avatar_Privacy_Core $core ) {
$this->upload->set_core( $core );

if ( \is_admin() ) {
\add_action( 'admin_init', [ $this, 'admin_init' ] );
}
Expand Down

0 comments on commit 64a2287

Please sign in to comment.