• Resolved xellagm

    (@xellagm)


    Hello,

    I was working on a custom plugin to clean medias of a website, and I use the trash media.

    I noticed that I have a conflict with W3TC when I put a media into the trash, and I find what is the problem, maybe you could add code correction to fix it (I use a custom filter in my plugin for now to bypass the problem).

    In “Util_AttachToActions.php” file, in “flush_posts_on_actions” function, you have this :

    add_filter( ‘pre_trash_post’, array( $o, ‘on_post_change’ ), 0, 2 );

    And in the ‘on_post_change’ function, I noticed this error :

    On line 108, where there is this code, you configure $post_id as the parent page of the media, to clear cache of this post, but the problem is that there is some medias that don’t have parent page, so the $post_id value become 0, and so the wp_trash_post is not working because the returned id is null. Also, It configure the trash post_id as the parent page instead of the media, so It trash the page and not the media if the parent is not empty.

    You should change the code to clear cache of the parent if it’s not empty, but keep the media initial id in $post_id, so maybe the code could be something like this :

    if ( isset( $post->post_type ) && 'attachment' === $post->post_type ) {
    $post_parent_id = $post->post_parent;
    if($post_parent_id !== 0) {
    // Clear cache parent only here
    $cacheflush->flush_post( $post_parent_id );
    }
    }

    Like this the page parent cache is clear, but we keep the action on the media wich is trashed.

    Hope you could change this part of the code and correct the error !

    Thank you !

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @xellagm

    Thank you for reaching out and I am happy to help!
    We have to check this and get back to you. This is your custom code so it may only impact the bahavior of your plugin and use case.
    I’ll get back to you once I have more info

    Thanks!

    Thread Starter xellagm

    (@xellagm)

    Hello Marko,

    I also find another thing, in the core function “wp_trash_post”, the filter “pre_trash_post” return null, and it has to be null for the function to continue and trash a post, like you can see here in “wp-includes/post.php” file, line 3744 :

    function wp_trash_post( $post_id = 0 ) {
    [...]
    $check = apply_filters( 'pre_trash_post', null, $post, $previous_status );

    if( null !== $check ) {
    return $check;
    }
    }

    So the function in W3TC “Util_AttachToActions.php” file should also return null on the “pre_trash_post” filter for medias to be trashed. I use a custom filter “pre_trash_post” called after your function, and I did this :

    function pre_trash_post_w3tc_conflict($trash, $post = null) {
    if ( is_null( $post ) ) {
    $post = get_post( $trash );
    }

    if($post->post_type === 'attachment') $trash = null;
    return $trash;
    }

    and it fix the conflict problem I have with W3TC, it put my media in trash, and trash is also working for others type of content.

    Hope it will help you to fix the problem in the plugin.

    Plugin Author Joe Cartonia

    (@joemoto)

    Hello @xellagm

    Thank you for reporting the issue. Media is only trashed when “MEDIA_TRASH” defined “true”. We were able to replicate the issue resulting in an error message “Error in moving the item to Trash.” A fix has been created — https://github.com/BoldGrid/w3-total-cache/pull/925 and will be included in the next release. Feel free to test the updated code and let us know if you still have any issues.

    Thread Starter xellagm

    (@xellagm)

    Hello @joemoto

    Thank you for your answer, and for the fix ! I’ll test the correction when the fix will be merged to the plugin, and I keep you inform if it’s good for me after this ! Thank you so much ! ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.