scottsweb / wp-instagram-widget

❌ A WordPress widget for showing your latest Instagram photos.
115 stars 62 forks source link

Hashtags and usernames are stored under the same transient (Fix included) #86

Closed iamzenigma closed 6 years ago

iamzenigma commented 6 years ago

Hi,

I'm building a website for my sister and noticed something odd. Whenever I tried to call the feed for the hashtags ('#') I was given the same list as when I searched for the username ('@').

After delving into your code I noticed that, whenever the username or hashtag have the same names, they get saved under the same transient. For example: @test and #test result in the same 'instagram-a9-test', because the function sanitize_title_with_dashes() removes these @/# signs. In your code that means the result will always point to either one of them and therefore one that uses your plugin can't get results for hashtags and usernames with the same search value.

FIX: I'm not sure if transients are allowed to have @ or # values, so I introduced a simple variable to fix this issue. As far as I'm concerned this works, but I'm not sure if this would affect users who are currently using your plugin!

The start of my scrape_instagram() function has this code included:

    $transient_prefix = '';
    switch ( substr( $username, 0, 1 ) ) {
        case '#':
            $transient_prefix = 'hashtag';
            break;

        default:
            $transient_prefix = 'username';
            break;
    }

and the if statement looks like this:

if ( false === ( $instagram = get_transient( 'instagram-a9-' . $transient_prefix . '-' . sanitize_title_with_dashes( $username ) ) ) ) {
}

Thank you btw. Really like the simplicity of your coding! I LIKE WHAT YOU GOT! 💯

Zeff

scottsweb commented 6 years ago

Good catch and the fix looks sane too. I might shorten the prefix as I think transients names are limited, will aim to push an update in the next week.

iamzenigma commented 6 years ago

Awesome! Thanks! @scottsweb

iamzenigma commented 6 years ago

Oh btw, you'll probably do this, but silly me forgot to add the prefix to the set_transient() call hehe...the most important one.

set_transient( 'instagram-a9-' . $transient_prefix . '-' . sanitize_title_with_dashes( $username )...

Cheers.

scottsweb commented 6 years ago

Fixed in 2.0.3