CondeNast / purgely

A WordPress plugin to manage Fastly caching behavior and purging.
24 stars 9 forks source link

Purge the URL for the post if the service ID isn't set #32

Open simonwistow opened 8 years ago

simonwistow commented 8 years ago

This is for users who mix and match two different services (e.g static and dynamic content).

It does not purge related content. It probably should.

One problem is that get_permalink($post_id) can optionally return the url with or without the post name being included.

I'm not sure whether we:

coveralls commented 8 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 7cdcc446caa55624bb2f2eda5177b1b79db07df7 on fastly:purge-url-when-no-service-set into 0f48431f8dc47a69cb8e701a710b972807f27e48 on CondeNast:master.

tollmanz commented 8 years ago

This is for users who mix and match two different services (e.g static and dynamic content).

Can you explain the problem more? Why would users not set the service ID?

schuob commented 8 years ago

@tollmanz

the problem here is that the user has 2 difference service with different origin (dynamic/static) not that best setup. But because of the fact that Purgely only supports a single service id and api key this won't purge both of their service that would share the same URL.

@simonwistow I did something like this and it seems to work

// Checking if a key is set, if not purge based on URL instead of surrogate-keys
        $temp_service_id = Purgely_Settings::get_setting( 'fastly_service_id' );
        $datetime = date( "Y/m/d (D) H:i:s", time() );
        error_log("Purgely Purge Log : ".$datetime." ".$temp_service_id."\n", 3, '/var/tmp/app.log');
        if ( $temp_service_id != '' ) {
            // original loop
                    foreach ( $ids as $id ) {
                            purgely_purge_surrogate_key( $id );
                    }
        } else {
            // if there is no key set - look for the post's permalink and home-url() to purged by URL.
            foreach ( $ids as $id ) {
                if ( $id == 'template-home' ) {
                    $temp_url = home_url();
                    purgely_purge_url( $temp_url );
                } else {
                    $arr = explode('-',$id);
                    $temp_url = get_permalink($arr[1]);
                    purgely_purge_url( $temp_url );
                }
            }
        }

I also realize that $home_key = 'template-home'; set earlier in the code is the wrong assumption as in this scenario the homepage surrogate-key was template-page.

tollmanz commented 8 years ago

@schuob In your opinion, is a problem that is likely to occur or is it something fairly custom?

My preference is to design for the majority rather than add features for the 1%. FWIW, it's fairly straight forward to do this outside of the plugin with something like:

add_action( 'save_post', function( $post_id ) {
    purgely_purge_url( $post_id );
} );

I'd also happily add a new action to Purgely_Purges->purge() to support additional purge routines when a purge is executed. For instance:

public function purge( $post_id ) {
    if ( ! in_array( get_post_status( $post_id ), array( 'publish', 'trash' ) ) ) {
        return;
    }
    purgely_purge_surrogate_key( 'post-' . absint( $post_id ) );

    do_action( 'purgely_after_purge', $post_id );
}

This would allow for:

add_action( 'purgely_after_purge', function( $post_id ) {
    purgely_purge_url( $post_id );
} );

By using the purgely_after_purge, you can precisely target the purge event and add any needed custom functionality.

schuob commented 8 years ago

@tollmanz I feel this scenario is not fairly common and agree with you. Is there a way for me to expose the surrogate-key for the homepage on the wp-purge.php? Right now I have it set to purge my homepage "statically" rather then dynamically.

tollmanz commented 8 years ago

@schuob It is definitely not obvious, but I intend to do it as part of the fix I outlined in https://github.com/CondeNast/purgely/pull/29#issuecomment-228614181. Right now, the home page is not properly surrogate keyed.

For now, this code will do it for you. You can add it as an mu-plugin or pop it in your functions.php:

add_action( 'wp', function() {
    if ( ! function_exists( 'purgely_add_surrogate_key') ) {
        return;
    }

    if ( is_home() ) {
        purgely_add_surrogate_key( 'template-home' );
    }
}, 100 );

To purge the homepage for all updates, you can do:

add_action( 'save_post', function( $post_id ) {
    purgely_purge_surrogate_key( 'template-home' );
} );

This will be baked in when I get some time to address it the right way.

schuob commented 8 years ago

@tollmanz thanks! I will tweak my purgely for that.

I simplify it a bit by doing this:

$temp_service_id = Purgely_Settings::get_setting( 'fastly_service_id' );
if ( $temp_service_id != '' ) {
    purgely_purge_surrogate_key( 'post-' . absint( $post_id ) );
    purgely_purge_surrogate_key( 'template-home' );
} else {
    // if there is no key set - look for the post's permalink and home-url() to purged by URL.
   $hp_url = home_url();
   $post_url = get_permalink( absint( $post_id ) );
   purgely_purge_url( $post_url );
   purgely_purge_url( $hp_url );
}
schuob commented 8 years ago

@tollmanz I tweaked this a bit as I found that blog-style vs static homepage treat is_home() differently. is_front_page() seem to work will.

add_action( 'wp', function() {
    if ( ! function_exists( 'purgely_add_surrogate_key') ) {
        return;
    }

    if ( is_front_page() ) {
        purgely_add_surrogate_key( 'template-home' );
    }
}, 100 );```
tollmanz commented 8 years ago

@schuob This all looks good! I think this highlights how tricky the issue is. We need to work this out so it works well for the average use case, but allows for easy customization.

From the Fastly perspective, if these scenarios happen with some frequency, but don't make sense as the default behavior, you could maintain a small, highly focused plugins (or alternatively, a "snippet" library). It could make support much easier for you.

schuob commented 8 years ago

@tollmanz I agree, i've already put together an internal doc for edge-cases and how to address them. So far we've only ran into one but pretty sure we should see more in the future. I look forward in seeing your approach to the above with WordPress 4.6