wp-media / wp-rocket

Performance optimization plugin for WordPress
https://wp-rocket.me
GNU General Public License v2.0
682 stars 213 forks source link

Clear WooCommerce WPML product translations cache in sync #6020

Closed alfonso100 closed 1 week ago

alfonso100 commented 1 year ago

Before submitting an issue please check that you’ve completed the following steps: Yes - Made sure you’re on the latest version Yes - Used the search feature to ensure that the bug hasn’t been reported before

Describe the bug When a WooCommerce product is translated using WPML, clearing the product cache is not clearing the translations cache.

To Reproduce Steps to reproduce the behavior:

  1. On a translated product
  2. Update the product on one of the languages
  3. the translations cache is not cleared

Expected behavior We could clear the caches of the product translations. We have a helper plugin for this, but I think it should be included in WPR Core: https://github.com/wp-media/wp-rocket-helpers/blob/e7afd29e5df5d95709f24ad8d95cd9268a461379/cache/wp-rocket-clean-product-related-translations/wp-rocket-clean-product-related-translations.php

Additional context Slack threads: https://wp-media.slack.com/archives/C08N8J6VC/p1688147711560569 https://wp-media.slack.com/archives/C08N8J6VC/p1642191163159800

Tickets: https://secure.helpscout.net/conversation/2286777680/427506?folderId=2675957 https://secure.helpscout.net/conversation/1757191015/319329?folderId=3963184 https://secure.helpscout.net/conversation/1292443606/197570/ https://secure.helpscout.net/conversation/1181876964/169638/#thread-3384735033

Backlog Grooming (for WP Media dev team use only)

DahmaniAdame commented 1 year ago

Ideally, the cache clearing on translated pages should be limited to items impacting translations, like changing the price example. The cache shouldn't be cleared if the product description is changed in a specific language, for example.

joejoe04 commented 9 months ago

Updated the helper @alfonso100 created and linked to above. It's now available here: https://github.com/wp-media/wp-rocket-helpers/tree/master/cache/wp-rocket-wpml-clean-related-translations

It can now be applied for more than just products (pages, posts, etc), based on preferences set in the helper.

alfonso100 commented 6 months ago

I had a WPML case where when Slugs where translated, rocket_clean_post() cannot clear the translated entry, even if you pass the correct ID. The URL sent to rocket_clean_files is not the translated one. I managed to fix this, by looping the languages, and switching to the different language before getting the post permalink. Adding these URLs into rocket_post_purge_urls:

     // get the current lang, and save it into a variable.
$the_current_lang = apply_filters( 'wpml_current_language', NULL );

if ( empty( $urls_to_purge ) || ! is_array( $urls_to_purge ) )
    return $urls_to_purge;   

    $post_id = $post->ID;   
    $type = apply_filters( 'wpml_element_type', get_post_type( $post_id ) );
    $trid = apply_filters( 'wpml_element_trid', false, $post_id, $type );
    $translations = apply_filters( 'wpml_get_element_translations', array(), $trid, $type );

              // loop languages
    foreach ( $translations as $lang => $translation ) {
             // switch to each lang
        do_action( 'wpml_switch_language', $translation->language_code );

                     // get the permalink
        $permalink = get_permalink($translation->element_id);   

                     // add the permalink to the urls_to_purge array
                    $urls_to_purge[] = $permalink;              
    }

             // set the original language back
    do_action( 'wpml_switch_language', $the_current_lang );

    return $urls_to_purge; 

Ticket: https://secure.helpscout.net/conversation/2372003264/445028?folderId=273766 Possibly related to: https://github.com/wp-media/wp-rocket/issues/6246

piotrbak commented 1 week ago

This is not something that can be implemented soon. We'll reopen it when working on WPML