Skip to content

RocketCDN free tier - Frontend#8288

Open
wordpressfan wants to merge 20 commits intofeature/rocket-cdn-free-tierfrom
feature/8263-frontend
Open

RocketCDN free tier - Frontend#8288
wordpressfan wants to merge 20 commits intofeature/rocket-cdn-free-tierfrom
feature/8263-frontend

Conversation

@wordpressfan
Copy link
Copy Markdown
Contributor

@wordpressfan wordpressfan commented May 5, 2026

Description

Fixes #8263

Apply CDN on frontend assets:

  • For RocketCDN free: apply it only for the selected pages (I added them manually into the database for testing purposes)
  • For RocketCDN paid: apply CDN in all pages except for excluded pages.
  • For Custom CDN: apply CDN in all pages, here we don't have the same exclusions mechanism.

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

The three cases mentioned above.

How to test

To simulate cdn type u need to use one of the following snippets:

RocketCDN paid:

add_filter( 'pre_get_rocket_option_cdn_type', function( $cdn_type ) {
	return 'rocketcdn';
} );

RocketCDN free:

add_filter( 'pre_get_rocket_option_cdn_type', function( $cdn_type ) {
	return 'rocketcdn_free';
} );

Custom CDN:

add_filter( 'pre_get_rocket_option_cdn_type', function( $cdn_type ) {
	return 'byocdn';
} );

For pages exclusions use the following snippet for RocketCDN paid and Custom CDN to apply exclusions (make sure to clear cache after enabling the snippet or changing on it):

add_filter( 'pre_get_rocket_option_cdn_reject_pages', function( $pages ) {
	return [
		'sample-page'
	];
} );

Affected Features & Quality Assurance Scope

RocketCDN free tier.

Technical description

Documentation

  • Now we depend directly on the option name cdn_type to decide which one is enabled instead of depending on the API until we have the API ready.
  • We added the exclusions option with the same name as in UI PR #/8280

New dependencies

N/A

Risks

N/A

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.)

@wordpressfan wordpressfan self-assigned this May 5, 2026
@wordpressfan wordpressfan linked an issue May 5, 2026 that may be closed by this pull request
@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented May 5, 2026

Up to standards ✅

🟢 Issues 1 minor

Results:
1 new issue

Category Results
Documentation 1 minor

View in Codacy

🟢 Metrics 9 complexity · 0 duplication

Metric Results
Complexity 9
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@wordpressfan wordpressfan changed the title Feature/8263 frontend RocketCDN free tier - Frontend May 5, 2026
@wordpressfan wordpressfan marked this pull request as ready for review May 5, 2026 18:06
@wordpressfan wordpressfan requested a review from Copilot May 5, 2026 18:10
Copy link
Copy Markdown
Contributor

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 implements frontend RocketCDN free-tier behavior by introducing a driver/strategy layer that decides whether CDN URL rewriting should happen for the current page (free: only whitelisted pages; paid/custom: rewrite broadly with page exclusions as applicable). It also simplifies CDN driver resolution to rely directly on the cdn_type option instead of the RocketCDN API.

Changes:

  • Added CDN “drivers” (free/paid/custom) and a factory to decide rewrite eligibility per request.
  • Updated CDN Subscriber to consult the resolved driver before rewriting URLs.
  • Simplified CDN\Context::get_driver() to return the cdn_type option value; updated unit fixtures/tests accordingly.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
inc/Engine/CDN/ServiceProvider.php Registers driver services, driver factory, and injects resolved driver into the CDN subscriber.
inc/Engine/CDN/Subscriber.php Adds driver gating (should_rewrite_url) to allow/deny rewriting on a per-page basis.
inc/Engine/CDN/Context.php Simplifies driver resolution to depend on cdn_type.
inc/Engine/CDN/Drivers/DriverInterface.php Defines the driver contract (should_rewrite_url).
inc/Engine/CDN/Drivers/DriverFactory.php Creates the active driver based on CDN\Context.
inc/Engine/CDN/Drivers/RocketCDNFree.php Implements free-tier behavior: only rewrite on pages found in RocketCDN DB table.
inc/Engine/CDN/Drivers/RocketCDNPaid.php Implements paid-tier behavior: rewrite unless URL matches excluded pages patterns.
inc/Engine/CDN/Drivers/Custom.php Custom CDN driver (currently unconditional rewrite).
inc/Engine/CDN/RocketCDN/Database/Queries/RocketCDN.php Adds is_url_found() helper for free-tier page lookup.
inc/Engine/Optimization/UrlTrait.php Adds get_current_url() helper used by the CDN subscriber to evaluate the current page URL.
tests/Unit/inc/Engine/CDN/Context/getDriver.php Updates unit test for the new Context constructor/signature and behavior.
tests/Fixtures/inc/Engine/CDN/Context/getDriver.php Updates fixtures to reflect cdn_type-based driver resolution.
phpstan-baseline.neon Updates baseline ignores (currently appears out of sync with the actual code in a couple places).

Comment thread inc/Engine/CDN/ServiceProvider.php Outdated
Comment thread inc/Engine/CDN/Drivers/Custom.php
Comment thread inc/Engine/CDN/Drivers/DriverFactory.php
Comment thread inc/Engine/CDN/Drivers/DriverFactory.php
Comment thread inc/Engine/Optimization/UrlTrait.php Outdated
Comment thread inc/Engine/CDN/RocketCDN/Database/Queries/RocketCDN.php
Comment thread phpstan-baseline.neon Outdated
Comment thread phpstan-baseline.neon
Comment thread inc/Engine/CDN/Drivers/RocketCDNFree.php
Comment thread inc/Engine/CDN/Context.php
Copilot AI and others added 2 commits May 5, 2026 18:37
… methods

Agent-Logs-Url: https://github.com/wp-media/wp-rocket/sessions/99ca5e7d-64e9-4539-9480-163a8475a550

Co-authored-by: wordpressfan <146129302+wordpressfan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented May 5, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • wpackagist.org
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/aLQcqX /usr/bin/composer install --no-interaction (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

…ure/8263-frontend

# Conflicts:
#	tests/Unit/inc/Engine/CDN/Subscriber/maybeReplaceUrl.php
#	tests/Unit/inc/Engine/CDN/Subscriber/rewrite.php
#	tests/Unit/inc/Engine/CDN/Subscriber/rewriteSrcset.php
… files

Agent-Logs-Url: https://github.com/wp-media/wp-rocket/sessions/4a2c199b-dd4f-45ea-8045-dd0e658140c9

Co-authored-by: wordpressfan <146129302+wordpressfan@users.noreply.github.com>
… DriverFactory

Agent-Logs-Url: https://github.com/wp-media/wp-rocket/sessions/78df7889-23cc-4d9b-a03d-93ef640286c8

Co-authored-by: wordpressfan <146129302+wordpressfan@users.noreply.github.com>
Comment thread inc/Engine/CDN/Drivers/DriverFactory.php Outdated
Comment thread inc/Engine/CDN/Drivers/DriverFactory.php Outdated
Copy link
Copy Markdown
Contributor

@jeawhanlee jeawhanlee left a comment

Choose a reason for hiding this comment

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

Great Job! I love the implementation

Comment thread inc/Engine/CDN/Drivers/DriverFactory.php Outdated
Comment thread inc/Engine/CDN/Drivers/DriverFactory.php Outdated
* @return bool
*/
public function should_rewrite_url( string $url ): bool {
return $this->query->is_url_found( $url );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe here we need to also factor that the homepage could be added but subscription creation would not have resolved on the website.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

we will consider having this in API, let me find a common way to handle this not to handle everything in the API GH issue.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Need to update the file name to our naming convention

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Need to update the file name to our naming convention

Comment thread phpstan-baseline.neon
-
message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#"
count: 1
count: 2
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a good opportunity to update

$hosts = (array) apply_filters( 'rocket_cdn_hosts', [], [ 'all' ] );
to use wpm_apply_filters_typed instead of increasing the count. WDYT?

Comment on lines -64 to -85
$cdn_type = (string) $this->options->get( 'cdn_type', self::ROCKETCDN_TYPE );

if ( self::ROCKETCDN_TYPE !== $cdn_type ) {
return self::BYOCDN_TYPE;
}

return $this->rocketcdn_resolver();
}

/**
* Resolves RocketCDN to either free or paid type.
*
* @return string
*/
private function rocketcdn_resolver(): string {
$subscription = $this->api_client->get_subscription_data();

if ( empty( $subscription['is_active'] ) || 'running' !== $subscription['subscription_status'] ) {
return self::ROCKETCDN_FREE_TYPE;
}

return self::ROCKETCDN_PAID_TYPE;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On a second thought, we should preserve this logic, the cdn_type should really hold 2 types of driver (byocdn or rocketcdn) we don't care if rocketcdn is free or paid, it's still rocketcdn.

The rocketcdn resolve would resolve if it's free or pro, we will not be sending request to the API for this, we can access the data from the transient.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I removed this part because we still don't know the final implementation for this in API side because we will mainly have the plan inside subscription status endpoint which is a new endpoint not the website search one that we use right now so we will save those data also in transient, I didn't want to have a code that is not needed for this PR and don't know if it's needed for next PRs or not.

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.

RocketCDN Free Tier: Business Logic (Frontend)

5 participants