function largo_get_avatar_src($id_or_email, $size) { $avatar_id = largo_get_user_avatar_id($id_or_email); if (empty($avatar_id)) { return false; } global $_wp_additional_image_sizes; $copy = $_wp_additional_image_sizes; usort($copy, function ($a, $b) { return $a['width'] - $b['width']; }); $square_image_sizes = array_filter($copy, function ($arg) { return $arg['width'] / $arg['height'] == 1; }); $requested_size = array($size, $size); foreach ($square_image_sizes as $key => $val) { if (round((double) ($val['width'] / $val['height']), 1, PHP_ROUND_HALF_DOWN) == round((double) ($requested_size[0] / $requested_size[1]), 1, PHP_ROUND_HALF_DOWN)) { // Try to find an image size equal to or just slightly larger than what was requested if ($val['width'] >= $requested_size[0]) { $requested_size = array($val['width'], $val['height']); break; } // If we can't find an image size, set the requested size to the largest of the // square sizes available if (end($square_image_sizes) == $square_image_sizes[$key]) { $requested_size = array($val['width'], $val['height']); } } } return wp_get_attachment_image_src($avatar_id, $requested_size); }
/** * Determine whether or not a user has an avatar. Fallback checks if user has a gravatar. * * @param $email string an author's email address * @return bool true if an avatar is available for this user * @since 0.4 */ function largo_has_avatar($email) { $user = get_user_by('email', $email); $result = largo_get_user_avatar_id($user->ID); if (!empty($result)) { return true; } else { if (largo_has_gravatar($email)) { return true; } } return false; }
function largo_add_avatar_field($user) { $image_src = largo_get_avatar_src($user->ID, '128'); largo_print_avatar_admin_css(); ?> <h3>User avatar</h3> <table class="form-table"> <tbody> <tr> <th><label for="<?php echo LARGO_AVATAR_INPUT_NAME; ?> "><?php _e('Current avatar', 'largo'); ?> </label></th> <td> <p id="largo-avatar-display"> <?php if (!empty($image_src)) { ?> <img src="<?php echo $image_src[0]; ?> " width="<?php echo $image_src[1]; ?> " height="<?php echo $image_src[2]; ?> " /><br /> <a href="<?php echo get_edit_post_link(largo_get_user_avatar_id($user->ID)); ?> "><?php _e('Edit', 'largo'); ?> </a> | <a id="largo-remove-avatar" href="#"><?php _e('Remove', 'largo'); ?> </a> <?php } if (empty($image_src) && largo_has_gravatar($user->user_email)) { echo get_avatar($user->ID); echo '<br />'; _e('Currently using Gravatar. Change at <a href="http://gravatar.com/">gravatar.com</a> or choose a different image below.', 'largo'); } ?> </p> <p id="largo-avatar-input" <?php if (!empty($image_src)) { ?> style="display:none;"<?php } ?> > <input type="file" name="<?php echo LARGO_AVATAR_INPUT_NAME; ?> " id="<?php echo LARGO_AVATAR_INPUT_NAME; ?> " /> </p> </td> </tr> </tbody> </table> <?php }
function test_largo_remove_avatar() { // Should also work if we're editing another user's profile largo_associate_avatar_with_user($this->user_id, $this->avatar_id); // We can simulate editing another user's profile by setting the $_POST['user_id'] key. $_POST['user_id'] = $this->user_id; try { $this->_handleAjax("largo_remove_avatar"); } catch (WPAjaxDieContinueException $e) { // Retrieve the $avatar_id. $retrieved = largo_get_user_avatar_id($this->user_id); // $retrieved should be empty at this point $this->assertEmpty($retrieved); } }