Git Product home page Git Product logo

Comments (6)

Ipstenu avatar Ipstenu commented on July 28, 2024

Oh .. wow. That makes a lot of sense why it happens. Yeah, make a PR for that. It looks right. The only other thing I can think of is hooking into the post_status change functions themselves, but even so, it would require a large retrofit either way.

from varnish-http-purge.

RichardNeveri avatar RichardNeveri commented on July 28, 2024

@carlalexander Hi, I think you pinpointed my issue too. Noticed that posts reverted to draft do not trigger a purge request since the front page remains unchanged. Where did you implement this function? In the plugin itself of functions.php?

Thanks anyways!

update - Works for me in functions.php, hope this will be merged in the main branch. :)

from varnish-http-purge.

carlalexander avatar carlalexander commented on July 28, 2024

Yeah, it's just custom code we created to work around the issue. Will probably work on a patch tomorrow. πŸ˜„

from varnish-http-purge.

jerclarke avatar jerclarke commented on July 28, 2024

UPDATE: Code above stopped working!

Fatal error from running VarnishPurger->purgePost( )

Our editor discovered that we were getting fatal errors when posts were reverted to draft:

Fatal error: Uncaught Error: Call to undefined function purge_post() in /.../wp-content/plugins/varnish-http-purge/varnish-http-purge.php on lineΒ 867
..
8 | 1.0819 | 59413400 | gv_varnish_purge_non_accessible_post( ) | .../class-wp-hook.php:286
9 | 1.0819 | 59413424 | VarnishPurger->purgePost( ) | .../gv-varnish.php:133

From looking at the code, it seems obvious that purgePost was quite logically replaced with purge_post but that despite the efforts to add purgePost as an alias, it didn't work, and our code caused fatal errors as a result.

@Ipstenu I figured it out, though I missed it at first (have been editing this comment):

	public function purgePost( $post_id ) {
		purge_post( $post_id );
	}

Maybe it was due to bad automation, but clearly, we shouldn't expect that alias of the old method to work. I imagine you meant $this-purge_post().

It seems to apply to all of the aliases in that section, so you could fix them pretty easily if you want.

New version of working code to purge when post is moved from publish->draft

@RichardNeveri PLZ take note, if you're still running this code, you should update it.

Following is our current working code (also includes more verbose explanation of the issue and workaround):

/**
 * Purges a post from Varnish when their status changes from a publicly accessible link to one without one.
 *
 * Affects behavior of Varnish HTTP Purge plugin, which already handles most cases where the status changes, 
 * but neglects publish->draft and friends for seemingly technical reasons. 
 * 
 * For us having de-published posts (publish->draft) get removed from Varnish cash is obviously a priority, 
 * so this ensures that happens while we wait for a fix in the main plugin. 
 * 
 * @see github issue: https://github.com/Ipstenu/varnish-http-purge/issues/39
 * 
 * Runs VarnishPurger->purgePost when the old status is 
 * - publish
 * - private
 * - trash
 * And the new status is
 * - pending 
 * - draft
 * 
 * This covers the blind spots of the plugin, which already handles e.g. publish->trash and publish->private
 * 
 * This method must be called before WordPress updates the database because VarnishPurger uses functions
 * that use current database values. This is why we use pre_post_update
 * 
 * @uses get_post_status($post_id) which is old status 
 * @uses $post_data['post_status'] which is the new status that hasn't been saved yet
 * @global VarnishPurger $purger object instantiated at the bottom of varnish-http-purge.php
 * @param int   $post_id
 * @param array $post_data
 */
function gv_varnish_purge_non_accessible_post($post_id, $post_data) {
	global $purger;

	/**
	 * Run the purger if
	 * - The old status IS publish, private or trash
	 * - AND new status IS pending or draft
	 */
	if (!$purger instanceof VarnishPurger
		|| empty($post_data['post_status'])
		|| !in_array(get_post_status($post_id), array('publish', 'private', 'trash'))
		|| !in_array($post_data['post_status'], array('pending', 'draft'))
	) {
		return;
	}
	
	$purger->purge_post($post_id);
}
add_action('pre_post_update', 'gv_varnish_purge_non_accessible_post', 10, 2);

from varnish-http-purge.

Ipstenu avatar Ipstenu commented on July 28, 2024

God it would help if I PUSHED the code, wouldn't it? Lemme fix the deprecation!

from varnish-http-purge.

Ipstenu avatar Ipstenu commented on July 28, 2024

So for whatever reason, the promised patch never came in. I'm planning to add this in 5.0, so please check that out when that pops up in a bit.

from varnish-http-purge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.