Plugin Directory

Changeset 3686562


Ignore:
Timestamp:
09/08/2026 12:12:32 PM (7 weeks ago)
Author:
megamenu
Message:

Guard nav_menu_locations cache invalidation to genuine save contexts (admin, REST, AJAX), preventing Polylang/WPML front-end language switches from wiping and

regenerating the entire CSS cache on nearly every request.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • megamenu/trunk/classes/style-manager.class.php

    r3542351 r3686562  
    103103                 * Defer to `shutdown` so `nav_menu_locations` is saved before regeneration (see `set_theme_mod`).
    104104                 *
     105                 * Only acts on `set_theme_mod()` calls made from a genuine admin/API save context (Customizer,
     106                 * nav-menus.php, REST). Multilingual plugins such as Polylang/WPML can end up calling
     107                 * `set_theme_mod( 'nav_menu_locations', ... )` on plain front-end requests to swap in the
     108                 * current language's menu; treating that as a real reassignment would wipe and regenerate the
     109                 * entire CSS cache (all languages) on almost every request. See `Mega_Menu_Integration_Polylang`.
     110                 *
    105111                 * @param array|false $new_value New location => menu ID map.
    106112                 * @param array|false $old_value Previous map or false.
    … …  
    108114                 */
    109115                public function schedule_delete_cache_on_nav_menu_locations_change( $new_value, $old_value ) {
     116                        if ( ! $this->is_genuine_nav_menu_locations_save() ) {
     117                                return is_array( $new_value ) ? $new_value : [];
     118                        }
     119
    110120                        if ( ! is_array( $new_value ) ) {
    111121                                $new_value = [];
    … …  
    141151
    142152                /**
     153                 * Whether the current request is a context where a user (or the REST API on their behalf)
     154                 * could genuinely be saving menu-location assignments, as opposed to a plain front-end
     155                 * request where a multilingual plugin may be swapping the theme mod value transiently.
     156                 *
     157                 * @since 3.11.0
     158                 * @return bool
     159                 */
     160                private function is_genuine_nav_menu_locations_save() {
     161                        if ( is_admin() ) {
     162                                return true;
     163                        }
     164
     165                        if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     166                                return true;
     167                        }
     168
     169                        if ( wp_doing_ajax() ) {
     170                                return true;
     171                        }
     172
     173                        return apply_filters( 'megamenu_is_genuine_nav_menu_locations_save', false );
     174                }
     175
     176
     177                /**
    143178                 * Run after theme mods are persisted so `get_nav_menu_locations()` matches the new assignments.
    144179                 *
Note: See TracChangeset for help on using the changeset viewer.