Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Adapt to More Current Avatar Filters #40

Merged
merged 11 commits into from
Aug 19, 2020
Merged

Fix: Adapt to More Current Avatar Filters #40

merged 11 commits into from
Aug 19, 2020

Conversation

dinhtungdu
Copy link
Contributor

@dinhtungdu dinhtungdu commented Jan 20, 2020

Description of the Change

  • Switch to pre_get_avatar_data filter.
  • Remove get_avatar function that overrides the core one.
  • Add $args parameter to get_simple_local_avatar function.
  • Remove Simple_Local_Avatars::get_avatar method. ~> added back for backward compat.
  • Add Simple_Local_Avatars::get_avatar_data() method.
  • Add Simple_Local_Avatars::get_simple_local_avatar_url() method.
  • Add Simple_Local_Avatars::get_default_avatar_url() method.
  • Fix coding standard issues.

Benefits

In fact, we only need to filter the url of the avatar, not the avatar
img element. So instead of hooking to get_avatar filter and overriding
the get_avatar function, we hook to pre_get_avatar_data filter to
manipulate the avatar url and let the core handle the rest. This reduces
duplication code and minimizes effort to follow core changes.

Possible Drawbacks

We need to care about/check for backward compatibility issues.

Verification Process

Check if avatars display correctly when Local Avatars Only is enabled/disabled.

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Applicable Issues

Closes #39
Relates #19

In fact, we only need to filter the url of the avatar, not the avatar
img element. So instead of hook to `get_avatar` filter and override the
`get_avatar` function, we hook to `pre_get_avatar_data` filter to
manipulate the avatar url and let the core handle the reset. This reduce
duplication code and effort to follow core changes.
@dinhtungdu dinhtungdu requested a review from jeffpaul January 20, 2020 01:44
@dinhtungdu dinhtungdu self-assigned this Jan 20, 2020
@jeffpaul jeffpaul added this to the 2.2 milestone Jan 20, 2020
@jeffpaul jeffpaul added the type:enhancement New feature or request. label Jan 20, 2020
@jeffpaul jeffpaul requested review from dkotter and ryanwelcher and removed request for jeffpaul January 20, 2020 14:59
@jeffpaul
Copy link
Member

Pinging @dkotter and @ryanwelcher on this to see if they can review this PR.

@jeffpaul
Copy link
Member

@heyjones - I wanted to check and see if you'd mind giving this PR a review in relation to your PR to see if this approach might also work for your use?

@heyjones
Copy link

Sorry for the delay... it maintains the class args I'm passing through the theme!

@dinhtungdu I'd recommend possibly running $this->options through wp_parse_args() as I had a PHP notice locally since I didn't have the only option set:

Notice: Undefined index: only ... on line 70

Aside from that, @jeffpaul as long as you don't see this causing any issues with backwards capability I think it's a really clever solution!

@dinhtungdu
Copy link
Contributor Author

@heyjones, thanks for the suggestion, I fixed it in 00a8362

@MikeNGarrett
Copy link

I really like this method better than continuing support via a pluggable function.

@lincolnlemos
Copy link

This is what I was looking for! Thanks 👍

@MikeNGarrett
Copy link

Is there an expected timeline for when this PR will be released?

@jeffpaul
Copy link
Member

jeffpaul commented May 7, 2020

@MikeNGarrett one item I'd like to see is some test coverage, be that unit tests and/or WP Acceptance tests, before pushing forward with another release. If someone is able to add a PR to increase (heck, add) test coverage then that'd be wonderful.

public function get_default_avatar_url( $size ) {
if ( empty( $default ) ) {
$avatar_default = get_option( 'avatar_default' );
if ( empty( $avatar_default ) )
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this was just copied from the current codebase but we should make sure any if statements here have brackets { } at the end of the lines.


$host = is_ssl() ? 'https://secure.gravatar.com' : 'http://0.gravatar.com';

if ( 'mystery' == $default )
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

@dinhtungdu
Copy link
Contributor Author

dinhtungdu commented May 21, 2020

@dkotter Currently we have a lot of coding style issues, I think it's better to address them in a separated PR after this is merged, does it work for you?

@Log1x
Copy link

Log1x commented Jun 10, 2020

any update on this?

@jeffpaul
Copy link
Member

@Log1x we have a couple other plugins scheduled for releases ahead of Simple Local Avatars. Getting this PR through review and into a release is on our todo list, just not in the immediate future. If you have an urgent or important need for this specific change, then please reach out to me directly and we discuss... thanks!

@jeffpaul jeffpaul requested a review from dkotter June 15, 2020 14:57
@joshuaabenazer
Copy link

joshuaabenazer commented Jun 25, 2020

So while using Simple Local Avatars on a project recently, I just needed the image url rather than the whole image tag. I assumed that since get_avatar() works out of the box, the other functions would also be intercepted similarly. The case in question here is the get_avatar_url() function.
We can use the pre_get_avatar_data filter in get_avatar_data() to short-circuit the function and updated the URL or maybe just let it go through the whole function and update the args accordingly in the get_avatar_data filter or maybe just get_avatar_url filter

I feel that if we update the URL at that stage, we may not be required to override the pluggable get_avatar() function. I haven't spent much time in this and I could be wrong, but I feel this is the right ticket to look into this and see if we can take over all the avatar related functions in core, just so that we are able to use them as is while the plugin does the rest behind the scenes.

@jeffpaul jeffpaul mentioned this pull request Jun 30, 2020
18 tasks
helen
helen previously approved these changes Aug 11, 2020
@ammon-lockwood
Copy link

@dinhtungdu Thank you, I'm seeing the right URL as well if I use the get_avatar_url and the get_avatar functions. It appears the issue is between this plugin and the wpgraphql plugin which expects to have the found_avatar set. In the code in #40 when we find the local avatar and set it in the $args on line 81 we should also set the found_avatar to true right after on line 82 so that anything looking for that argument can key off it. In our case wpgraphql doesn't return anything from the avatar data unless found_avatar is true. WordPress sets it to true when the default Gravatar is set and so we should set this argument as well when we find the local avatar. I propose the get_avatar_data function look like this:

        /**
	 * Filter avatar data early to add avatar url if needed. This filter hooks
	 * before Gravatar setup to prevent wasted requests.
	 *
	 * @since 2.2.0
	 *
	 * @param array $args        Arguments passed to get_avatar_data(), after processing.
	 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
	 *                           user email, WP_User object, WP_Post object, or WP_Comment object.
	 */
	public function get_avatar_data( $args, $id_or_email ) {
		$simple_local_avatar_url = $this->get_simple_local_avatar_url( $id_or_email, $args['size'] );
		if( $simple_local_avatar_url ) {
			$args['url'] = $simple_local_avatar_url;
			$args['found_avatar'] = true;
		}

		// Local only mode
		if( ! $simple_local_avatar_url && ! empty( $this->options['only'] ) ) {
			$args['url'] = $this->get_default_avatar_url( $args['size'] );
		}

		return $args;
	}

@dinhtungdu dinhtungdu linked an issue Aug 13, 2020 that may be closed by this pull request
@dinhtungdu dinhtungdu linked an issue Aug 13, 2020 that may be closed by this pull request
@dinhtungdu
Copy link
Contributor Author

dinhtungdu commented Aug 18, 2020

@sumnercreations Can you please test the latest commit and see if your issue is resolved? https://github.com/10up/simple-local-avatars/tree/fix/39

@jeffpaul
Copy link
Member

After internal discussion and review, it was agreed this was ready for merge so I'm going to get this in and start looking towards what else remains in a 2.2.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement New feature or request.
Projects
None yet
10 participants