Commit inicial powerservice

This commit is contained in:
Gabriel
2026-10-19 12:45:45 -06:00
commit ad85486fe2
10158 changed files with 2736804 additions and 0 deletions
@@ -0,0 +1,286 @@
<?php
namespace WP_Rocket\Engine\CDN\RocketCDN;
use WP_Error;
/**
* Class to Interact with the RocketCDN API
*/
class APIClient {
const ROCKETCDN_API = 'https://rocketcdn.me/api/';
/**
* Gets current RocketCDN subscription data from cache if it exists
*
* Else do a request to the API to get fresh data
*
* @since 3.5
*
* @return array
*/
public function get_subscription_data() {
$status = get_transient( 'rocketcdn_status' );
if ( false !== $status ) {
return $status;
}
return $this->get_remote_subscription_data();
}
/**
* Gets fresh RocketCDN subscription data from the API
*
* @since 3.5
*
* @return array
*/
private function get_remote_subscription_data() {
$default = [
'id' => 0,
'is_active' => false,
'cdn_url' => '',
'subscription_next_date_update' => 0,
'subscription_status' => 'cancelled',
];
$token = get_option( 'rocketcdn_user_token' );
if ( empty( $token ) ) {
return $default;
}
$args = [
'headers' => [
'Authorization' => 'Token ' . $token,
],
];
$response = wp_remote_get(
self::ROCKETCDN_API . 'website/search/?url=' . home_url(),
$args
);
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
$this->set_status_transient( $default, 3 * MINUTE_IN_SECONDS );
return $default;
}
$data = wp_remote_retrieve_body( $response );
if ( empty( $data ) ) {
$this->set_status_transient( $default, 3 * MINUTE_IN_SECONDS );
return $default;
}
$data = json_decode( $data, true );
$data = array_intersect_key( (array) $data, $default );
$data = array_merge( $default, $data );
$this->set_status_transient( $data, WEEK_IN_SECONDS );
return $data;
}
/**
* Sets the RocketCDN status transient with the provided value
*
* @since 3.5
*
* @param array $value Transient value.
* @param int $duration Transient duration.
* @return void
*/
private function set_status_transient( $value, $duration ) {
set_transient( 'rocketcdn_status', $value, $duration );
}
/**
* Gets pricing & promotion data for RocketCDN from cache if it exists
*
* Else do a request to the API to get fresh data
*
* @since 3.5
*
* @return array|WP_Error
*/
public function get_pricing_data() {
$pricing = get_transient( 'rocketcdn_pricing' );
if ( false !== $pricing ) {
return $pricing;
}
return $this->get_remote_pricing_data();
}
/**
* Gets fresh pricing & promotion data for RocketCDN
*
* @since 3.5
*
* @return array|WP_Error
*/
private function get_remote_pricing_data() {
$response = wp_remote_get( self::ROCKETCDN_API . 'pricing' );
if ( is_wp_error( $response ) ) {
return $response;
}
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return $this->get_wp_error( __( 'We could not fetch the current price because RocketCDN API returned an unexpected error code.', 'rocket' ) );
}
$data = wp_remote_retrieve_body( $response );
if ( empty( $data ) ) {
return $this->get_wp_error( __( 'RocketCDN is not available at the moment. Please retry later.', 'rocket' ) );
}
$data = json_decode( $data, true );
set_transient( 'rocketcdn_pricing', $data, 6 * HOUR_IN_SECONDS );
return $data;
}
/**
* Gets a new WP_Error instance
*
* @since 3.5
*
* @param string $message Error message.
*
* @return WP_Error
*/
private function get_wp_error( string $message ) {
return new WP_Error( 'rocketcdn_error', $message );
}
/**
* Sends a request to the API to purge the CDN cache
*
* @since 3.5
*
* @return array
*/
public function purge_cache_request() {
$subscription = $this->get_subscription_data();
$status = 'error';
if ( ! isset( $subscription['id'] ) || 0 === $subscription['id'] ) {
return [
'status' => $status,
'message' => __( 'RocketCDN cache purge failed: Missing identifier parameter.', 'rocket' ),
];
}
$token = get_option( 'rocketcdn_user_token' );
if ( empty( $token ) ) {
return [
'status' => $status,
'message' => __( 'RocketCDN cache purge failed: Missing user token.', 'rocket' ),
];
}
$args = [
'method' => 'DELETE',
'headers' => [
'Authorization' => 'Token ' . $token,
],
];
$response = wp_remote_request(
self::ROCKETCDN_API . 'website/' . $subscription['id'] . '/purge/',
$args
);
if ( is_wp_error( $response ) ) {
return [
'status' => $status,
'message' => $response->get_error_message(),
];
}
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return [
'status' => $status,
'message' => __( 'RocketCDN cache purge failed: The API returned an unexpected response code.', 'rocket' ),
];
}
$data = wp_remote_retrieve_body( $response );
if ( empty( $data ) ) {
return [
'status' => $status,
'message' => __( 'RocketCDN cache purge failed: The API returned an empty response.', 'rocket' ),
];
}
$data = json_decode( $data );
if ( ! isset( $data->success ) ) {
return [
'status' => $status,
'message' => __( 'RocketCDN cache purge failed: The API returned an unexpected response.', 'rocket' ),
];
}
if ( ! $data->success ) {
return [
'status' => $status,
'message' => sprintf(
// translators: %s = message returned by the API.
__( 'RocketCDN cache purge failed: %s.', 'rocket' ),
isset( $data->message ) ? $data->message : ''
),
];
}
return [
'status' => 'success',
'message' => __( 'RocketCDN cache purge successful.', 'rocket' ),
];
}
/**
* Filter the arguments used in an HTTP request, to make sure our user token has not been overwritten
* by some other plugin.
*
* @since 3.5
*
* @param array $args An array of HTTP request arguments.
* @param string $url The request URL.
* @return array
*/
public function preserve_authorization_token( $args, $url ) {
if ( strpos( $url, self::ROCKETCDN_API ) === false ) {
return $args;
}
if ( empty( $args['headers']['Authorization'] ) && self::ROCKETCDN_API . 'pricing' === $url ) {
return $args;
}
$token = get_option( 'rocketcdn_user_token' );
if ( empty( $token ) ) {
return $args;
}
$value = 'token ' . $token;
if ( isset( $args['headers']['Authorization'] ) && $value === $args['headers']['Authorization'] ) {
return $args;
}
$args['headers']['Authorization'] = $value;
return $args;
}
}
@@ -0,0 +1,256 @@
<?php
namespace WP_Rocket\Engine\CDN\RocketCDN;
use WP_Rocket\Abstract_Render;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Admin\Beacon\Beacon;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Subscriber for the RocketCDN integration in WP Rocket settings page
*
* @since 3.5
*/
class AdminPageSubscriber extends Abstract_Render implements Subscriber_Interface {
/**
* RocketCDN API Client instance.
*
* @var APIClient
*/
private $api_client;
/**
* WP Rocket options instance
*
* @var Options_Data
*/
private $options;
/**
* Beacon instance
*
* @var Beacon
*/
private $beacon;
/**
* Constructor
*
* @param APIClient $api_client RocketCDN API Client instance.
* @param Options_Data $options WP Rocket options instance.
* @param Beacon $beacon Beacon instance.
* @param string $template_path Path to the templates.
*/
public function __construct( APIClient $api_client, Options_Data $options, Beacon $beacon, $template_path ) {
parent::__construct( $template_path );
$this->api_client = $api_client;
$this->options = $options;
$this->beacon = $beacon;
}
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'rocket_dashboard_after_account_data' => 'display_rocketcdn_status',
'rocket_cdn_settings_fields' => 'rocketcdn_field',
'admin_post_rocket_purge_rocketcdn' => 'purge_cdn_cache',
'rocket_settings_page_footer' => 'add_subscription_modal',
'http_request_args' => [ 'preserve_authorization_token', PHP_INT_MAX, 2 ],
];
}
/**
* Displays the RocketCDN section on the dashboard tab
*
* @since 3.5
*
* @return void
*/
public function display_rocketcdn_status() {
/**
* Filters the display of the RocketCDN status.
*
* @param bool $display_rocketcdn_status; true to display, false otherwise.
*/
if ( ! apply_filters( 'rocket_display_rocketcdn_status', true ) ) {
return;
}
if ( $this->is_white_label_account() ) {
return;
}
$subscription_data = $this->api_client->get_subscription_data();
if ( 'running' === $subscription_data['subscription_status'] ) {
$label = __( 'Next Billing Date', 'rocket' );
$status_class = ' wpr-isValid';
$container_class = '';
$status_text = date_i18n( get_option( 'date_format' ), strtotime( $subscription_data['subscription_next_date_update'] ) );
$is_active = true;
} elseif ( 'cancelled' === $subscription_data['subscription_status'] ) {
$label = '';
$status_class = ' wpr-isInvalid';
$container_class = ' wpr-flex--egal';
$status_text = __( 'No Subscription', 'rocket' );
$is_active = false;
}
$data = [
'is_live_site' => rocket_is_live_site(),
'container_class' => $container_class,
'label' => $label,
'status_class' => $status_class,
'status_text' => $status_text,
'is_active' => $is_active,
];
echo $this->generate( 'dashboard-status', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Adds the RocketCDN fields to the CDN section
*
* @since 3.5
*
* @param array $fields CDN settings fields.
*
* @return array
*/
public function rocketcdn_field( $fields ) {
if ( $this->is_white_label_account() ) {
return $fields;
}
$subscription_data = $this->api_client->get_subscription_data();
if ( 'running' !== $subscription_data['subscription_status'] ) {
return $fields;
}
$helper_text = __( 'Your RocketCDN subscription is currently active.', 'rocket' );
$cdn_cnames = $this->options->get( 'cdn_cnames', [] );
if ( empty( $cdn_cnames ) || $cdn_cnames[0] !== $subscription_data['cdn_url'] ) {
$helper_text = sprintf(
// translators: %1$s = opening <code> tag, %2$s = CDN URL, %3$s = closing </code> tag.
__( 'To use RocketCDN, replace your CNAME with %1$s%2$s%3$s.', 'rocket' ),
'<code>',
$subscription_data['cdn_url'],
'</code>'
);
}
$beacon = $this->beacon->get_suggest( 'rocketcdn' );
$more_info = sprintf(
// translators: %1$is = opening link tag, %2$s = closing link tag.
__( '%1$sMore Info%2$s', 'rocket' ),
'<a href="' . esc_url( $beacon['url'] ) . '" data-beacon-article="' . esc_attr( $beacon['id'] ) . '" rel="noopener noreferrer" target="_blank">',
'</a>'
);
$fields['cdn_cnames'] = [
'type' => 'rocket_cdn',
'label' => __( 'CDN CNAME(s)', 'rocket' ),
'description' => __( 'Specify the CNAME(s) below', 'rocket' ),
'helper' => $helper_text . ' ' . $more_info,
'default' => '',
'section' => 'cnames_section',
'page' => 'page_cdn',
'beacon' => [
'url' => $beacon['url'],
'id' => $beacon['id'],
],
];
return $fields;
}
/**
* Purges the CDN cache and store the response in a transient.
*
* @since 3.5
*
* @return void
*/
public function purge_cdn_cache() {
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'rocket_purge_rocketcdn' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
wp_nonce_ays( '' );
}
if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_die();
}
set_transient( 'rocketcdn_purge_cache_response', $this->api_client->purge_cache_request(), HOUR_IN_SECONDS );
wp_safe_redirect( esc_url_raw( wp_get_referer() ) );
rocket_get_constant( 'WP_ROCKET_IS_TESTING', false ) ? wp_die() : exit;
}
/**
* Adds the subscription modal on the WP Rocket settings page
*
* @since 3.5
*
* @return void
*/
public function add_subscription_modal() {
if ( $this->is_white_label_account() ) {
return;
}
if ( ! rocket_is_live_site() ) {
return;
}
$iframe_src = add_query_arg(
[
'website' => home_url(),
'callback' => rest_url( 'wp-rocket/v1/rocketcdn/' ),
],
rocket_get_constant( 'WP_ROCKET_WEB_MAIN' ) . 'cdn/iframe'
);
?>
<div class="wpr-rocketcdn-modal" id="wpr-rocketcdn-modal" aria-hidden="true">
<div class="wpr-rocketcdn-modal__overlay" tabindex="-1">
<div class="wpr-rocketcdn-modal__container" role="dialog" aria-modal="true" aria-labelledby="wpr-rocketcdn-modal-title">
<div id="wpr-rocketcdn-modal-content">
<iframe id="rocketcdn-iframe" src="<?php echo esc_url( $iframe_src ); ?>" width="674" height="425"></iframe>
</div>
</div>
</div>
</div>
<?php
}
/**
* Filter the arguments used in an HTTP request, to make sure our user token has not been overwritten
* by some other plugin.
*
* @since 3.5
*
* @param array $args An array of HTTP request arguments.
* @param string $url The request URL.
* @return array
*/
public function preserve_authorization_token( $args, $url ) {
return $this->api_client->preserve_authorization_token( $args, $url );
}
/**
* Checks if white label is enabled
*
* @since 3.6
*
* @return bool
*/
private function is_white_label_account() {
return (bool) rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' );
}
}
@@ -0,0 +1,84 @@
<?php
namespace WP_Rocket\Engine\CDN\RocketCDN;
use WP_Rocket\Admin\Options;
use WP_Rocket\Admin\Options_Data;
/**
* Manager for WP Rocket CDN options
*
* @since 3.5
*/
class CDNOptionsManager {
/**
* WP Options API instance
*
* @var Options
*/
private $options_api;
/**
* WP Rocket Options instance
*
* @var Options_Data
*/
private $options;
/**
* Constructor
*
* @param Options $options_api WP Options API instance.
* @param Options_Data $options WP Rocket Options instance.
*/
public function __construct( Options $options_api, Options_Data $options ) {
$this->options_api = $options_api;
$this->options = $options;
}
/**
* Enable CDN option, save CDN URL & delete RocketCDN status transient
*
* @since 3.5
*
* @param string $cdn_url CDN URL.
* @return void
*/
public function enable( $cdn_url ) {
$this->options->set( 'cdn', 1 );
$this->options->set( 'cdn_cnames', [ $cdn_url ] );
$this->options->set( 'cdn_zone', [ 'all' ] );
$this->options_api->set( 'settings', $this->options->get_options() );
delete_transient( 'rocketcdn_status' );
rocket_clean_domain();
}
/**
* Disable CDN option, remove CDN URL & user token, delete RocketCDN status transient
*
* @since 3.5
*
* @return void
*/
public function disable() {
$this->options->set( 'cdn', 0 );
$this->options->set( 'cdn_cnames', [] );
$this->options->set( 'cdn_zone', [] );
$this->options_api->set( 'settings', $this->options->get_options() );
delete_option( 'rocketcdn_user_token' );
delete_transient( 'rocketcdn_status' );
rocket_clean_domain();
}
/**
* Get current CDN cnames.
*
* @return array
*/
public function get_cdn_cnames() {
return $this->options->get( 'cdn_cnames', [] );
}
}
@@ -0,0 +1,343 @@
<?php
namespace WP_Rocket\Engine\CDN\RocketCDN;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Subscriber for the RocketCDN integration in WP Rocket settings page
*
* @since 3.5
*/
class DataManagerSubscriber implements Subscriber_Interface {
const CRON_EVENT = 'rocketcdn_check_subscription_status_event';
/**
* RocketCDN API Client instance.
*
* @var APIClient
*/
private $api_client;
/**
* CDNOptionsManager instance.
*
* @var CDNOptionsManager
*/
private $cdn_options;
/**
* Constructor
*
* @param APIClient $api_client RocketCDN API Client instance.
* @param CDNOptionsManager $cdn_options CDNOptionsManager instance.
*/
public function __construct( APIClient $api_client, CDNOptionsManager $cdn_options ) {
$this->api_client = $api_client;
$this->cdn_options = $cdn_options;
}
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'wp_ajax_save_rocketcdn_token' => 'update_user_token',
'wp_ajax_rocketcdn_enable' => 'enable',
'wp_ajax_rocketcdn_disable' => 'disable',
'wp_ajax_rocketcdn_process_set' => 'set_process_status',
'wp_ajax_rocketcdn_process_status' => 'get_process_status',
'wp_ajax_rocketcdn_validate_token_cname' => 'validate_token_cname',
self::CRON_EVENT => 'maybe_disable_cdn',
];
}
/**
* Updates the RocketCDN user token value
*
* @since 3.5
*
* @return void
*/
public function update_user_token() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_send_json_error( 'unauthorized_user' );
return;
}
if ( empty( $_POST['value'] ) ) {
delete_option( 'rocketcdn_user_token' );
wp_send_json_success( 'user_token_deleted' );
return;
}
if ( ! is_string( $_POST['value'] ) ) {
wp_send_json_error( 'invalid_token' );
return;
}
$token = sanitize_key( $_POST['value'] );
if ( 40 !== strlen( $token ) ) {
wp_send_json_error( 'invalid_token_length' );
return;
}
update_option( 'rocketcdn_user_token', $token );
wp_send_json_success( 'user_token_saved' );
}
/**
* Ajax callback to enable RocketCDN
*
* @since 3.5
*
* @return void
*/
public function enable() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
$data = [
'process' => 'subscribe',
];
if ( ! current_user_can( 'rocket_manage_options' ) ) {
$data['message'] = 'unauthorized_user';
wp_send_json_error( $data );
return;
}
if ( empty( $_POST['cdn_url'] ) ) {
$data['message'] = 'cdn_url_empty';
wp_send_json_error( $data );
return;
}
$cdn_url = filter_var( wp_unslash( $_POST['cdn_url'] ), FILTER_VALIDATE_URL );
if ( ! $cdn_url ) {
$data['message'] = 'cdn_url_invalid_format';
wp_send_json_error( $data );
return;
}
$this->cdn_options->enable( esc_url_raw( $cdn_url ) );
$subscription = $this->api_client->get_subscription_data();
$this->schedule_subscription_check( $subscription );
$this->delete_process();
$data['message'] = 'rocketcdn_enabled';
wp_send_json_success( $data );
}
/**
* AJAX callback to disable RocketCDN
*
* @since 3.5
*
* @return void
*/
public function disable() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
$data = [
'process' => 'unsubscribe',
];
if ( ! current_user_can( 'rocket_manage_options' ) ) {
$data['message'] = 'unauthorized_user';
wp_send_json_error( $data );
return;
}
$this->cdn_options->disable();
$timestamp = wp_next_scheduled( self::CRON_EVENT );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, self::CRON_EVENT );
}
$this->delete_process();
$data['message'] = 'rocketcdn_disabled';
wp_send_json_success( $data );
}
/**
* Delete the option tracking the RocketCDN process state
*
* @since 3.5
*
* @return void
*/
private function delete_process() {
delete_option( 'rocketcdn_process' );
}
/**
* Set the RocketCDN subscription process status
*
* @since 3.5
*
* @return void
*/
public function set_process_status() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
if ( empty( $_POST['status'] ) ) {
return;
}
$status = filter_var( $_POST['status'], FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Used as a boolean.
if ( false === $status ) {
delete_option( 'rocketcdn_process' );
return;
}
update_option( 'rocketcdn_process', $status );
}
/**
* Check for RocketCDN subscription process status
*
* @since 3.5
*
* @return void
*/
public function get_process_status() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_send_json_error();
return;
}
if ( get_option( 'rocketcdn_process' ) ) {
wp_send_json_success();
return;
}
wp_send_json_error();
}
/**
* Cron job to disable CDN if the subscription expired
*
* @since 3.5
*
* @return void
*/
public function maybe_disable_cdn() {
delete_transient( 'rocketcdn_status' );
$subscription = $this->api_client->get_subscription_data();
if ( rocket_get_constant( 'WP_ROCKET_IS_TESTING', false ) ) {
$subscription = apply_filters( 'rocket_pre_get_subscription_data', $subscription );
}
if ( 'running' === $subscription['subscription_status'] ) {
$this->schedule_subscription_check( $subscription );
return;
}
$this->cdn_options->disable();
}
/**
* Validates and updates the token and cname from RocketCDN Iframe.
*
* @return void
*/
public function validate_token_cname() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
$data = [];
if ( ! current_user_can( 'rocket_manage_options' ) ) {
$data['message'] = 'unauthorized_user';
wp_send_json_error( $data );
return;
}
if ( empty( $_POST['cdn_url'] ) || empty( $_POST['cdn_token'] ) ) {
$data['message'] = 'cdn_values_empty';
wp_send_json_error( $data );
return;
}
$token = sanitize_key( $_POST['cdn_token'] );
$cdn_url = filter_var( wp_unslash( $_POST['cdn_url'] ), FILTER_VALIDATE_URL );
if ( ! $cdn_url ) {
$data['message'] = 'cdn_url_invalid_format';
wp_send_json_error( $data );
return;
}
if ( 40 !== strlen( $token ) ) {
$data['message'] = 'invalid_token_length';
wp_send_json_error( $data );
return;
}
$current_token = get_option( 'rocketcdn_user_token' );
$current_cname = $this->cdn_options->get_cdn_cnames();
if ( ! empty( $current_token ) ) {
$data['message'] = 'token_already_set';
wp_send_json_error( $data );
return;
}
update_option( 'rocketcdn_user_token', $token );
$this->cdn_options->enable( esc_url_raw( $cdn_url ) );
$data['message'] = 'token_updated_successfully';
wp_send_json_success( $data );
}
/**
* Schedule the next cron subscription check
*
* @since 3.5
*
* @param array $subscription Array containing the subscription data.
* @return void
*/
private function schedule_subscription_check( $subscription ) {
$timestamp = strtotime( $subscription['subscription_next_date_update'] ) + strtotime( '+2 days' );
if ( ! wp_next_scheduled( self::CRON_EVENT ) ) {
wp_schedule_single_event( $timestamp, self::CRON_EVENT );
}
}
}
@@ -0,0 +1,362 @@
<?php
namespace WP_Rocket\Engine\CDN\RocketCDN;
use WP_Rocket\Abstract_Render;
use WP_Rocket\Engine\Admin\Beacon\Beacon;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Subscriber for the RocketCDN notices on WP Rocket settings page
*
* @since 3.5
*/
class NoticesSubscriber extends Abstract_Render implements Subscriber_Interface {
/**
* RocketCDN API Client instance.
*
* @var APIClient
*/
private $api_client;
/**
* Beacon instance
*
* @var Beacon
*/
private $beacon;
/**
* Constructor
*
* @param APIClient $api_client RocketCDN API Client instance.
* @param Beacon $beacon Beacon instance.
* @param string $template_path Path to the templates.
*/
public function __construct( APIClient $api_client, Beacon $beacon, $template_path ) {
parent::__construct( $template_path );
$this->api_client = $api_client;
$this->beacon = $beacon;
}
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'admin_notices' => [
[ 'promote_rocketcdn_notice' ],
[ 'purge_cache_notice' ],
],
'rocket_before_cdn_sections' => 'display_rocketcdn_cta',
'wp_ajax_toggle_rocketcdn_cta' => 'toggle_cta',
'wp_ajax_rocketcdn_dismiss_notice' => 'dismiss_notice',
'admin_footer' => 'add_dismiss_script',
];
}
/**
* Adds notice to promote RocketCDN on settings page
*
* @since 3.5
*
* @return void
*/
public function promote_rocketcdn_notice() {
/**
* Filters RocketCDN promotion notice.
*
* @param bool $promotion_notice; true to display, false otherwise.
*/
if ( ! apply_filters( 'rocket_promote_rocketcdn_notice', true ) ) {
return;
}
if ( $this->is_white_label_account() ) {
return;
}
if ( ! rocket_is_live_site() ) {
return;
}
if ( ! $this->should_display_notice() ) {
return;
}
echo $this->generate( 'promote-notice' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Adds inline script to permanently dismissing the RocketCDN promotion notice
*
* @since 3.5
*
* @return void
*/
public function add_dismiss_script() {
if ( $this->is_white_label_account() ) {
return;
}
if ( ! rocket_is_live_site() ) {
return;
}
if ( ! $this->should_display_notice() ) {
return;
}
$nonce = wp_create_nonce( 'rocketcdn_dismiss_notice' );
?>
<script>
window.addEventListener( 'load', function() {
var dismissBtn = document.querySelectorAll( '#rocketcdn-promote-notice .notice-dismiss, #rocketcdn-promote-notice #rocketcdn-learn-more-dismiss' );
dismissBtn.forEach(function(element) {
element.addEventListener( 'click', function( event ) {
var httpRequest = new XMLHttpRequest(),
postData = '';
postData += 'action=rocketcdn_dismiss_notice';
postData += '&nonce=<?php echo esc_attr( $nonce ); ?>';
httpRequest.open( 'POST', '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>' );
httpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' )
httpRequest.send( postData );
});
});
});
</script>
<?php
}
/**
* Checks if the promotion notice should be displayed
*
* @since 3.5
*
* @return boolean
*/
private function should_display_notice() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return false;
}
if ( 'settings_page_wprocket' !== get_current_screen()->id ) {
return false;
}
if ( get_user_meta( get_current_user_id(), 'rocketcdn_dismiss_notice', true ) ) {
return false;
}
$subscription_data = $this->api_client->get_subscription_data();
return 'running' !== $subscription_data['subscription_status'];
}
/**
* Ajax callback to save the dismiss as a user meta
*
* @since 3.5
*
* @return void
*/
public function dismiss_notice() {
check_ajax_referer( 'rocketcdn_dismiss_notice', 'nonce', true );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_send_json_error( 'no permissions' );
return;
}
update_user_meta( get_current_user_id(), 'rocketcdn_dismiss_notice', true );
wp_send_json_success();
}
/**
* Displays the RocketCDN Call to Action on the CDN tab of WP Rocket settings page
*
* @since 3.5
*
* @return void
*/
public function display_rocketcdn_cta() {
/**
* Filters the display of the RocketCDN cta banner.
*
* @param bool $display_cta_banner; true to display, false otherwise.
*/
if ( ! apply_filters( 'rocket_display_rocketcdn_cta', true ) ) {
return;
}
if ( $this->is_white_label_account() ) {
return;
}
if ( ! rocket_is_live_site() ) {
return;
}
$subscription_data = $this->api_client->get_subscription_data();
if ( 'running' === $subscription_data['subscription_status'] ) {
return;
}
$pricing = $this->api_client->get_pricing_data();
$regular_price = '';
$nopromo_variant = '--no-promo';
$cta_small_class = 'wpr-isHidden';
$cta_big_class = '';
if ( get_user_meta( get_current_user_id(), 'rocket_rocketcdn_cta_hidden', true ) ) {
$cta_small_class = '';
$cta_big_class = 'wpr-isHidden';
}
$small_cta_data = [
'container_class' => $cta_small_class,
];
if ( is_wp_error( $pricing ) ) {
$beacon = $this->beacon->get_suggest( 'rocketcdn_error' );
$more_info = sprintf(
// translators: %1$is = opening link tag, %2$s = closing link tag.
__( '%1$sMore Info%2$s', 'rocket' ),
'<a href="' . esc_url( $beacon['url'] ) . '" data-beacon-article="' . esc_attr( $beacon['id'] ) . '" rel="noopener noreferrer" target="_blank">',
'</a>'
);
$message = $pricing->get_error_message() . ' ' . $more_info;
$big_cta_data = [
'container_class' => $cta_big_class,
'nopromo_variant' => $nopromo_variant,
'error' => true,
'message' => $message,
];
} else {
$current_price = number_format_i18n( $pricing['monthly_price'], 2 );
$promotion_campaign = '';
$end_date = strtotime( $pricing['end_date'] );
$promotion_end_date = '';
if (
$pricing['is_discount_active']
&&
$end_date > time()
) {
$promotion_campaign = $pricing['discount_campaign_name'];
$regular_price = $current_price;
$current_price = number_format_i18n( $pricing['discounted_price_monthly'], 2 ) . '*';
$nopromo_variant = '';
$promotion_end_date = date_i18n( get_option( 'date_format' ), $end_date );
}
$big_cta_data = [
'container_class' => $cta_big_class,
'promotion_campaign' => $promotion_campaign,
'promotion_end_date' => $promotion_end_date,
'nopromo_variant' => $nopromo_variant,
'regular_price' => $regular_price,
'current_price' => $current_price,
];
}
echo $this->generate( 'cta-small', $small_cta_data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
echo $this->generate( 'cta-big', $big_cta_data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Toggles display of the RocketCDN CTAs on the settings page
*
* @since 3.5
*
* @return void
*/
public function toggle_cta() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_send_json_error( 'no permissions' );
return;
}
if ( ! isset( $_POST['status'] ) ) {
wp_send_json_error( 'missing status' );
return;
}
if ( 'big' === $_POST['status'] ) {
delete_user_meta( get_current_user_id(), 'rocket_rocketcdn_cta_hidden' );
} elseif ( 'small' === $_POST['status'] ) {
update_user_meta( get_current_user_id(), 'rocket_rocketcdn_cta_hidden', true );
}
wp_send_json_success();
}
/**
* Displays a notice after purging the RocketCDN cache.
*
* @since 3.5
*
* @return void
*/
public function purge_cache_notice() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
if ( 'settings_page_wprocket' !== get_current_screen()->id ) {
return;
}
$purge_response = get_transient( 'rocketcdn_purge_cache_response' );
if ( false === $purge_response ) {
return;
}
$message = $purge_response['message'];
if ( 'error' === $purge_response['status'] ) {
$beacon = $this->beacon->get_suggest( 'rocketcdn_error' );
$more_info = sprintf(
// translators: %1$is = opening link tag, %2$s = closing link tag.
__( '%1$sMore Info%2$s', 'rocket' ),
'<a href="' . esc_url( $beacon['url'] ) . '" data-beacon-article="' . esc_attr( $beacon['id'] ) . '" rel="noopener noreferrer" target="_blank">',
'</a>'
);
$message .= ' ' . $more_info;
}
delete_transient( 'rocketcdn_purge_cache_response' );
rocket_notice_html(
[
'status' => $purge_response['status'],
'message' => $message,
]
);
}
/**
* Checks if white label is enabled
*
* @since 3.6
*
* @return bool
*/
private function is_white_label_account() {
return (bool) rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' );
}
}
@@ -0,0 +1,194 @@
<?php
namespace WP_Rocket\Engine\CDN\RocketCDN;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Admin\Options_Data;
/**
* Subscriber for RocketCDN REST API Integration
*
* @since 3.5
*/
class RESTSubscriber implements Subscriber_Interface {
const ROUTE_NAMESPACE = 'wp-rocket/v1';
/**
* CDNOptionsManager instance
*
* @var CDNOptionsManager
*/
private $cdn_options;
/**
* WP Rocket Options instance
*
* @var Options_Data
*/
private $options;
/**
* Constructor
*
* @param CDNOptionsManager $cdn_options CDNOptionsManager instance.
* @param Options_Data $options WP Rocket Options instance.
*/
public function __construct( CDNOptionsManager $cdn_options, Options_Data $options ) {
$this->cdn_options = $cdn_options;
$this->options = $options;
}
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'rest_api_init' => [
[ 'register_enable_route' ],
[ 'register_disable_route' ],
],
];
}
/**
* Register Enable route in the WP REST API
*
* @since 3.5
*
* @return void
*/
public function register_enable_route() {
register_rest_route(
self::ROUTE_NAMESPACE,
'rocketcdn/enable',
[
'methods' => 'PUT',
'callback' => [ $this, 'enable' ],
'args' => [
'email' => [
'required' => true,
'validate_callback' => [ $this, 'validate_email' ],
],
'key' => [
'required' => true,
'validate_callback' => [ $this, 'validate_key' ],
],
'url' => [
'required' => true,
'validate_callback' => function ( $param ) {
$url = esc_url_raw( $param );
return ! empty( $url );
},
'sanitize_callback' => function ( $param ) {
return esc_url_raw( $param );
},
],
],
'permission_callback' => '__return_true',
]
);
}
/**
* Register Disable route in the WP REST API
*
* @since 3.5
*
* @return void
*/
public function register_disable_route() {
register_rest_route(
self::ROUTE_NAMESPACE,
'rocketcdn/disable',
[
'methods' => 'PUT',
'callback' => [ $this, 'disable' ],
'args' => [
'email' => [
'required' => true,
'validate_callback' => [ $this, 'validate_email' ],
],
'key' => [
'required' => true,
'validate_callback' => [ $this, 'validate_key' ],
],
],
'permission_callback' => '__return_true',
]
);
}
/**
* Enable CDN and add RocketCDN URL to WP Rocket options
*
* @since 3.5
*
* @param \WP_REST_Request $request the WP REST Request object.
*
* @return \WP_REST_Response|\WP_Error
*/
public function enable( \WP_REST_Request $request ) {
$params = $request->get_body_params();
$this->cdn_options->enable( $params['url'] );
$response = [
'code' => 'success',
'message' => __( 'RocketCDN enabled', 'rocket' ),
'data' => [
'status' => 200,
],
];
return rest_ensure_response( $response );
}
/**
* Disable the CDN and remove the RocketCDN URL from WP Rocket options
*
* @since 3.5
*
* @param \WP_REST_Request $request the WP Rest Request object.
*
* @return \WP_REST_Response|\WP_Error
*/
public function disable( \WP_REST_Request $request ) {
$this->cdn_options->disable();
$response = [
'code' => 'success',
'message' => __( 'RocketCDN disabled', 'rocket' ),
'data' => [
'status' => 200,
],
];
return rest_ensure_response( $response );
}
/**
* Checks that the email sent along the request corresponds to the one saved in the DB
*
* @since 3.5
*
* @param string $param Parameter value to validate.
*
* @return bool
*/
public function validate_email( $param ) {
return ! empty( $param ) && $param === $this->options->get( 'consumer_email' );
}
/**
* Checks that the key sent along the request corresponds to the one saved in the DB
*
* @since 3.5
*
* @param string $param Parameter value to validate.
*
* @return bool
*/
public function validate_key( $param ) {
return ! empty( $param ) && $param === $this->options->get( 'consumer_key' );
}
}
@@ -0,0 +1,67 @@
<?php
namespace WP_Rocket\Engine\CDN\RocketCDN;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
/**
* Service provider for RocketCDN
*
* @since 3.5
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'rocketcdn_api_client',
'rocketcdn_options_manager',
'rocketcdn_data_manager_subscriber',
'rocketcdn_rest_subscriber',
'rocketcdn_admin_subscriber',
'rocketcdn_notices_subscriber',
];
/**
* Registers items with the container
*
* @return void
*/
public function register() {
$options = $this->getContainer()->get( 'options' );
// RocketCDN API Client.
$this->getContainer()->add( 'rocketcdn_api_client', APIClient::class );
// RocketCDN CDN options manager.
$this->getContainer()->add( 'rocketcdn_options_manager', CDNOptionsManager::class )
->addArgument( $this->getContainer()->get( 'options_api' ) )
->addArgument( $options );
// RocketCDN Data manager subscriber.
$this->getContainer()->share( 'rocketcdn_data_manager_subscriber', DataManagerSubscriber::class )
->addArgument( $this->getContainer()->get( 'rocketcdn_api_client' ) )
->addArgument( $this->getContainer()->get( 'rocketcdn_options_manager' ) )
->addTag( 'admin_subscriber' );
// RocketCDN REST API Subscriber.
$this->getContainer()->share( 'rocketcdn_rest_subscriber', RESTSubscriber::class )
->addArgument( $this->getContainer()->get( 'rocketcdn_options_manager' ) )
->addArgument( $options )
->addTag( 'common_subscriber' );
// RocketCDN Notices Subscriber.
$this->getContainer()->share( 'rocketcdn_notices_subscriber', NoticesSubscriber::class )
->addArgument( $this->getContainer()->get( 'rocketcdn_api_client' ) )
->addArgument( $this->getContainer()->get( 'beacon' ) )
->addArgument( __DIR__ . '/views' )
->addTag( 'admin_subscriber' );
// RocketCDN settings page subscriber.
$this->getContainer()->share( 'rocketcdn_admin_subscriber', AdminPageSubscriber::class )
->addArgument( $this->getContainer()->get( 'rocketcdn_api_client' ) )
->addArgument( $options )
->addArgument( $this->getContainer()->get( 'beacon' ) )
->addArgument( __DIR__ . '/views' )
->addTag( 'admin_subscriber' );
}
}
@@ -0,0 +1,93 @@
<?php
/**
* RocketCDN small CTA template.
*
* @since 3.5
*
* @param array $data {
* @type string $container_class container CSS class.
* @type string $promotion_campaign Promotion campaign title.
* @type string $promotion_end_date Promotion end date.
* @type string $nopromo_variant CSS modifier for the no promotion display.
* @type string $regular_price RocketCDN regular price.
* @type string $current_price RocketCDN current price.
* }
*/
defined( 'ABSPATH' ) || die( 'Cheatin&#8217; uh?' );
?>
<div class="wpr-rocketcdn-cta <?php echo esc_attr( $data['container_class'] ); ?>" id="wpr-rocketcdn-cta">
<?php if ( ! empty( $data['promotion_campaign'] ) ) : ?>
<div class="wpr-flex wpr-rocketcdn-promo">
<h3 class="wpr-rocketcdn-promo-title"><?php echo esc_html( $data['promotion_campaign'] ); ?></h3>
<p class="wpr-title2 wpr-rocketcdn-promo-date">
<?php
printf(
// Translators: %s = date formatted using date_i18n() and get_option( 'date_format' ).
esc_html__( 'Valid until %s only!', 'rocket' ),
esc_html( $data['promotion_end_date'] )
);
?>
</p>
</div>
<?php endif; ?>
<section class="wpr-rocketcdn-cta-content<?php echo esc_attr( $data['nopromo_variant'] ); ?>">
<h3 class="wpr-title2">RocketCDN</h3>
<p class="wpr-rocketcdn-cta-subtitle"><?php esc_html_e( 'Speed up your website thanks to:', 'rocket' ); ?></p>
<div class="wpr-flex">
<ul class="wpr-rocketcdn-features">
<li class="wpr-rocketcdn-feature wpr-rocketcdn-bandwidth">
<?php
// translators: %1$s = opening strong tag, %2$s = closing strong tag.
printf( esc_html__( 'High performance Content Delivery Network (CDN) with %1$sunlimited bandwidth%2$s', 'rocket' ), '<strong>', '</strong>' );
?>
</li>
<li class="wpr-rocketcdn-feature wpr-rocketcdn-configuration">
<?php
// translators: %1$s = opening strong tag, %2$s = closing strong tag.
printf( esc_html__( 'Easy configuration: the %1$sbest CDN settings%2$s are automatically applied', 'rocket' ), '<strong>', '</strong>' );
?>
</li>
<li class="wpr-rocketcdn-feature wpr-rocketcdn-automatic">
<?php
// translators: %1$s = opening strong tag, %2$s = closing strong tag.
printf( esc_html__( 'WP Rocket integration: the CDN option is %1$sautomatically configured%2$s in our plugin', 'rocket' ), '<strong>', '</strong>' );
?>
</li>
<li class="wpr-rocketcdn-cta-footer">
<a href="https://wp-rocket.me/rocketcdn/" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Learn more about RocketCDN', 'rocket' ); ?></a>
</li>
<?php if ( ! empty( $data['promotion_campaign'] ) ) : ?>
<li class="wpr-rocketcdn-cta-promo-footer">
<?php
printf(
// translators: %1$s = discounted price, %2$s = regular price.
esc_html__( '*$%1$s/month for 12 months then $%2$s/month. You can cancel your subscription at any time.', 'rocket' ),
esc_html( str_replace( '*', '', $data['current_price'] ) ),
esc_html( $data['regular_price'] )
);
?>
</li>
<?php endif; ?>
</ul>
<div class="wpr-rocketcdn-pricing">
<?php if ( ! empty( $data['error'] ) ) : ?>
<p><?php echo $data['message']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
<?php else : ?>
<?php if ( ! empty( $data['regular_price'] ) ) : ?>
<h4 class="wpr-title2 wpr-rocketcdn-pricing-regular"><del>$<?php echo esc_html( $data['regular_price'] ); ?></del></h4>
<?php endif; ?>
<h4 class="wpr-rocketcdn-pricing-current">
<span class="wpr-rocketcdn-cta-currency-minor">$</span>
<span class="wpr-rocketcdn-cta-currency-major"><?php echo esc_html( substr( $data['current_price'], 0, strpos( $data['current_price'], '.' ) ) ); ?></span>
<span class="wpr-rocketcdn-cta-currency-minor"><?php echo esc_html( substr( $data['current_price'], strpos( $data['current_price'], '.' ) ) ); ?>
</span>
</h4>
<p class="wpr-rocketcdn-cta-billing-detail"><?php esc_html_e( 'Billed monthly', 'rocket' ); ?></p>
<button class="wpr-button wpr-rocketcdn-open" data-micromodal-trigger="wpr-rocketcdn-modal"><?php esc_html_e( 'Get Started', 'rocket' ); ?></button>
<?php endif; ?>
</div>
</div>
</section>
<button class="wpr-rocketcdn-cta-close<?php echo esc_attr( $data['nopromo_variant'] ); ?>" id="wpr-rocketcdn-close-cta"><span class="screen-reader-text"><?php esc_html_e( 'Reduce this banner', 'rocket' ); ?></span></button>
</div>
@@ -0,0 +1,23 @@
<?php
/**
* RocketCDN small CTA template.
*
* @since 3.5
*
* @param array $data {
* @type string $container_class container CSS class.
* }
*/
defined( 'ABSPATH' ) || die( 'Cheatin&#8217; uh?' );
?>
<div class="wpr-rocketcdn-cta-small notice-alt notice-warning <?php echo esc_attr( $data['container_class'] ); ?>" id="wpr-rocketcdn-cta-small">
<div class="wpr-flex">
<section>
<h3 class="notice-title"><?php esc_html_e( 'Speed up your website with RocketCDN, WP Rocket’s Content Delivery Network.', 'rocket' ); ?></strong></h3>
</section>
<div>
<button class="wpr-button" id="wpr-rocketcdn-open-cta"><?php esc_html_e( 'Learn More', 'rocket' ); ?></button>
</div>
</div>
</div>
@@ -0,0 +1,37 @@
<?php
/**
* RocketCDN status on dashboard tab template.
*
* @since 3.5
*
* @param array $data {
* @type bool $is_live_site Identifies if the current website is a live or local/staging one
* @type string $container_class Flex container CSS class.
* @type string $label Content label.
* @type string $status_class CSS Class to display the status.
* @type string $status_text Text to display the subscription status.
* @type bool $is_active Boolean identifying the activation status.
* }
*/
?>
<div class="wpr-optionHeader">
<h3 class="wpr-title2">RocketCDN</h3>
</div>
<div class="wpr-field wpr-field-account">
<?php if ( ! $data['is_live_site'] ) : ?>
<span class="wpr-infoAccount wpr-isInvalid"><?php esc_html_e( 'RocketCDN is unavailable on local domains and staging sites.', 'rocket' ); ?></span>
<?php else : ?>
<div class="wpr-flex<?php echo esc_attr( $data['container_class'] ); ?>">
<div>
<span class="wpr-title3"><?php echo esc_html( $data['label'] ); ?></span>
<span class="wpr-infoAccount<?php echo esc_attr( $data['status_class'] ); ?>"><?php echo esc_html( $data['status_text'] ); ?></span>
</div>
<?php if ( ! $data['is_active'] ) : ?>
<div>
<a href="#page_cdn" class="wpr-button"><?php esc_html_e( 'Get RocketCDN', 'rocket' ); ?></a>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
@@ -0,0 +1,14 @@
<?php
/**
* Promote RocketCDN notice template.
*
* @since 3.5
*/
defined( 'ABSPATH' ) || die( 'Cheatin&#8217; uh?' );
?>
<div class="notice notice-alt notice-warning is-dismissible" id="rocketcdn-promote-notice">
<h2 class="notice-title"><?php esc_html_e( 'New!', 'rocket' ); ?></h2>
<p><?php esc_html_e( 'Speed up your website with RocketCDN, WP Rocket’s Content Delivery Network!', 'rocket' ); ?></p>
<p><a href="#page_cdn" class="wpr-button" id="rocketcdn-learn-more-dismiss"><?php esc_html_e( 'Learn More', 'rocket' ); ?></a></p>
</div>