Skip to content

Commit

Permalink
Add method to update lists from JSON files and clear cache
Browse files Browse the repository at this point in the history
* **DynamicLists.php**
  - Add `update_lists_from_files` method to clear transient cache and update lists from JSON files
  - Call `remove_lists_cache` and `get_lists` methods from each provider's data manager in `update_lists_from_files`

* **AbstractDataManager.php**
  - Add `remove_lists_cache` method to delete transient cache

* **Subscriber.php**
  - Modify `wp_rocket_upgrade` callback to call `update_lists_from_files` method
  - Add `update_lists_from_files` method to call `$this->dynamic_lists->update_lists_from_files()`
  • Loading branch information
Miraeld committed Oct 23, 2024
1 parent 815e639 commit 3493608
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions inc/Engine/Optimization/DynamicLists/AbstractDataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,13 @@ private function put_lists_to_file( string $content ): bool {
private function set_lists_cache( $content ) {
set_transient( $this->get_cache_transient_name(), $content, $this->cache_duration );
}

/**
* Removes the lists cache
*
* @return void
*/
public function remove_lists_cache() {
delete_transient( $this->get_cache_transient_name() );
}
}
26 changes: 26 additions & 0 deletions inc/Engine/Optimization/DynamicLists/DynamicLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,30 @@ public function get_lrc_exclusions(): array {

return $lists->lazy_rendering_exclusions ?? [];
}

/**
* Updates the lists from JSON files
*
* @return array
*/
public function update_lists_from_files() {
if ( $this->user->is_license_expired() ) {
return [
'success' => false,
'data' => '',
'message' => __( 'You need an active license to get the latest version of the lists from our server.', 'rocket' ),
];
}

foreach ( $this->providers as $provider ) {
$provider->data_manager->remove_lists_cache();
$provider->data_manager->get_lists();
}

return [
'success' => true,
'data' => '',
'message' => __( 'Lists are successfully updated from JSON files.', 'rocket' ),
];
}
}
10 changes: 10 additions & 0 deletions inc/Engine/Optimization/DynamicLists/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static function get_subscribed_events() {
'rocket_plugins_to_deactivate' => 'add_incompatible_plugins_to_deactivate',
'rocket_staging_list' => 'add_staging_exclusions',
'rocket_lrc_exclusions' => 'add_lrc_exclusions',
'wp_rocket_upgrade' => 'update_lists_from_files',
];
}

Expand Down Expand Up @@ -202,4 +203,13 @@ public function add_staging_exclusions( $stagings = [] ): array {
public function add_lrc_exclusions( $exclusions ): array {
return array_merge( (array) $exclusions, $this->dynamic_lists->get_lrc_exclusions() );
}

/**
* Update dynamic lists from JSON files.
*
* @return void
*/
public function update_lists_from_files() {
$this->dynamic_lists->update_lists_from_files();
}
}

0 comments on commit 3493608

Please sign in to comment.