Skip to content

Conversation

Jeroen-1978
Copy link

Description

This pull request is ment to solve issue #2698

This pull request fixes a critical bug where purging a parent page's cache would unintentionally clear the cache for all of its descendant pages. It also ensures the _wpr_rocket_cache database table is correctly and fully updated, especially when page slugs are modified within a hierarchy.

Type of change

  • New feature (non-breaking change which adds functionality).
  • Bug fix (non-breaking change which fixes an issue).
  • Enhancement (non-breaking change which improves an existing functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as before).
  • Sub-task of #(issue number)
  • Chore
  • Release

Detailed scenario

What was tested

Describe the scenarios that you tested, and specify if it is automated or manual. For manual scenarios, provide a screenshot of the results.

How to test

Describe how the PR can be tested so that the validator can be autonomous: environment, dependencies, specific setup, steps to perform, API requests, etc.

Technical description

Documentation

Problem
The previous implementation purged parent pages when a post was updated. This led to several issues:

  1. Unintentional Cache Deletion: Purging a parent URL (e.g., /parent/) caused rocket_clean_files to delete the entire directory, which unintentionally wiped out the cache for all child pages (e.g., /parent/child/).
  2. Database Inconsistency on Update: Because child pages were deleted indirectly via directory removal, they were not explicitly listed in the purge process. This meant their entries in the _wpr_rocket_cache table were not updated, leading to a mismatch between the filesystem cache and the database.
  3. Database Inconsistency on Slug Change: When a slug was changed for a page in the middle of a page tree, only the cache for that single post was purged. The URLs of all its child pages also change as a result, but their old cached versions were not being purged, and their entries in the _wpr_rocket_cache table were not updated to reflect the new URL structure.

Solution
The logic has been inverted: instead of purging parent pages, the process now explicitly targets all affected child pages.

  1. Added get_all_descendant() function: A new helper function has been added to recursively retrieve all descendant page IDs (children, grandchildren, etc.) for a given post.
  2. Modified rocket_get_purge_urls() and rocket_clean_post_cache_on_slug_change(): Both functions have been updated to use the new get_all_descendant() helper. Instead of adding parent URLs to the purge list, they now build a precise list of all individual child pages that need to be purged.

This change ensures that only the intended cache files are removed and that each purged URL is explicitly processed, guaranteeing that the _wpr_rocket_cache table is always kept in sync with the actual state of the cache.

New dependencies

List any new dependencies that are required for this change.

Risks

List possible performance & security issues or risks, and explain how they have been mitigated.

Mandatory Checklist

Code validation

  • I validated all the Acceptance Criteria. If possible, provide screenshots or videos.
  • I triggered all changed lines of code at least once without new errors/warnings/notices.
  • I implemented built-in tests to cover the new/changed code.

Code style

  • I wrote a self-explanatory code about what it does.
  • I protected entry points against unexpected inputs.
  • I did not introduce unnecessary complexity.
  • Output messages (errors, notices, logs) are explicit enough for users to understand the issue and are actionnable.

Unticked items justification

If some mandatory items are not relevant, explain why in this section.

Additional Checks

  • In the case of complex code, I wrote comments to explain it.
  • When possible, I prepared ways to observe the implemented system (logs, data, etc.).
  • I added error handling logic when using functions that could throw errors (HTTP/API request, filesystem, etc.)

This pull request is ment to solve issue wp-media#2698 

This pull request fixes a critical bug where purging a parent page's cache would unintentionally clear the cache for all of its descendant pages. It also ensures the _wpr_rocket_cache database table is correctly and fully updated, especially when page slugs are modified within a hierarchy.

Problem
The previous implementation purged parent pages when a post was updated. This led to several issues:

1. Unintentional Cache Deletion: Purging a parent URL (e.g., /parent/) caused rocket_clean_files to delete the entire directory, which unintentionally wiped out the cache for all child pages (e.g., /parent/child/).
2. Database Inconsistency on Update: Because child pages were deleted indirectly via directory removal, they were not explicitly listed in the purge process. This meant their entries in the _wpr_rocket_cache table were not updated, leading to a mismatch between the filesystem cache and the database.
3. Database Inconsistency on Slug Change: When a slug was changed for a page in the middle of a page tree, only the cache for that single post was purged. The URLs of all its child pages also change as a result, but their old cached versions were not being purged, and their entries in the _wpr_rocket_cache table were not updated to reflect the new URL structure.

Solution
The logic has been inverted: instead of purging parent pages, the process now explicitly targets all affected child pages.

1. Added get_all_descendant() function: A new helper function has been added to recursively retrieve all descendant page IDs (children, grandchildren, etc.) for a given post.
2. Modified rocket_get_purge_urls() and rocket_clean_post_cache_on_slug_change(): Both functions have been updated to use the new get_all_descendant() helper. Instead of adding parent URLs to the purge list, they now build a precise list of all individual child pages that need to be purged.

This change ensures that only the intended cache files are removed and that each purged URL is explicitly processed, guaranteeing that the _wpr_rocket_cache table is always kept in sync with the actual state of the cache.
@Copilot Copilot AI review requested due to automatic review settings September 7, 2025 07:04
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a critical caching bug by replacing parent page cache purging with child page cache purging. The change prevents unintentional deletion of child page caches and ensures proper database synchronization when page hierarchies are modified.

  • Replaces parent page purging with explicit child page purging to prevent unintended cache deletion
  • Adds a new helper function to recursively find all descendant pages
  • Updates slug change handling to purge all affected child pages and maintain database consistency

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Jeroen-1978 Jeroen-1978 changed the title Replace parent post purge with child post purge Fix #2698: Replace parent post purge with child post purge Sep 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant