webtions / i-recommend-this

This plugin allows your visitors to simply like/recommend your posts instead of comment on it.
22 stars 10 forks source link

Add do_action("after_recommend",$post_id) hook for customization and cache refresh #40

Open opicron opened 2 months ago

opicron commented 2 months ago

When using cache systems the browser is not updated after casting a vote/recommendation.

For now I hook update_post_meta and added_post_meta to check if the field _recommended is set.

It would be nice to use add_action to hook the plugins do_action hook instead.

add_action( 'added_post_meta', 'my_after_post_meta', 10, 4 );
add_action( 'updated_post_meta', 'my_after_post_meta', 10, 4 );
function my_after_post_meta( $meta_id, $post_id, $meta_key, $meta_value )
{
    if ( '_recommended' == $meta_key ) {
       //clear cache of $post_id
    }
}
hchouhan commented 2 months ago

@opicron Can you please share which caching plugin you use?

opicron commented 2 months ago

I am using Cloudways as a host, they auto install the Breeze cache plugin. I know how to clear the cache. I think that clearing cache is something devs should hook into themselves.

It is not feasible to add all caching plugin clearing codes to your plugin. But adding a hook to do it might be a good idea. We can hook into it. The above code hooking into the post_meta update/add is a bit obscure and maybe not the best for all devs.

This is the code used for clearing cache on Cloudways Breeze plugin:

            global $wp_filesystem;

        if ( empty( $wp_filesystem ) ) {
            require_once ABSPATH . 'wp-admin/includes/file.php';
            WP_Filesystem();
        }

        $url_path = get_permalink($post_id);
        $cache_base_path = breeze_get_cache_base_path( false, 0 );

        if ( $wp_filesystem->exists( $cache_base_path . hash( 'sha512', $url_path ) ) ) 
        {
            $wp_filesystem->rmdir( $cache_base_path . hash( 'sha512', $url_path ), true );
        }

        // Clear the varnish cache.
        do_action( 'breeze_clear_varnish' );
opicron commented 2 months ago

I hope you agree to not add 'bloat' to the plugin by catering for cache refresh. After trying many Like Button plugins yours is the most lean and intresting to continue using.

There was one plugin that had 20 cache plugin refresh options implemented. While a FAQ on the wordpress page showing snippets to clear cache specific for a single provider would be fine.

If you can add do_action('after_vote',$post_id); after updating the metadata anybody can hook anything they want :).