Initial commit xmarticaims

This commit is contained in:
2026-11-02 10:26:32 -06:00
commit e74236f5c1
9764 changed files with 2880647 additions and 0 deletions
@@ -0,0 +1,258 @@
<?php
namespace WP_Rocket\Engine\Plugin;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Manages the plugin information.
*/
class InformationSubscriber implements Subscriber_Interface {
use UpdaterApiTools;
/**
* Plugin slug.
*
* @var string
*/
private $plugin_slug;
/**
* URL to contact to get plugin info.
*
* @var string
*/
private $api_url;
/**
* An ID to use when a API request fails.
*
* @var string
*/
protected $request_error_id = 'plugins_api_failed';
/**
* Constructor
*
* @param array $args {
* Required arguments to populate the class properties.
*
* @type string $plugin_file Full path to the plugin.
* @type string $api_url URL to contact to get update info.
* }
*/
public function __construct( $args ) {
if ( isset( $args['plugin_file'] ) ) {
$this->plugin_slug = $this->get_plugin_slug( $args['plugin_file'] );
}
if ( isset( $args['api_url'] ) ) {
$this->api_url = $args['api_url'];
}
}
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'plugins_api' => [ 'exclude_rocket_from_wp_info', 10, 3 ],
'plugins_api_result' => [
[ 'add_rocket_info', 10, 3 ],
[ 'add_plugins_to_result', 11, 3 ],
],
'rocket_wp_tested_version' => 'add_wp_tested_version',
];
}
/**
* Don’t ask for plugin info to the repository.
*
* @param false|object|array $bool The result object or array. Default false.
* @param string $action The type of information being requested from the Plugin Install API.
* @param object $args Plugin API arguments.
* @return false|object|array Empty object if slug is WP Rocket, default value otherwise.
*/
public function exclude_rocket_from_wp_info( $bool, $action, $args ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.boolFound
if ( ! $this->is_requesting_rocket_info( $action, $args ) ) {
return $bool;
}
return new \stdClass();
}
/**
* Insert WP Rocket plugin info.
*
* @param object|\WP_Error $res Response object or WP_Error.
* @param string $action The type of information being requested from the Plugin Install API.
* @param object $args Plugin API arguments.
* @return object|\WP_Error Updated response object or WP_Error.
*/
public function add_rocket_info( $res, $action, $args ) {
if ( ! $this->is_requesting_rocket_info( $action, $args ) || empty( $res->external ) ) {
return $res;
}
return $this->get_plugin_information();
}
/**
* Adds the WP tested version value from our API
*
* @param string $wp_tested_version WP tested version.
*
* @return string
*/
public function add_wp_tested_version( $wp_tested_version ): string {
$info = $this->get_plugin_information();
if ( empty( $info->tested ) ) {
return $wp_tested_version;
}
return $info->tested;
}
/**
* Tell if requesting WP Rocket plugin info.
*
* @param string $action The type of information being requested from the Plugin Install API.
* @param object $args Plugin API arguments.
* @return bool
*/
private function is_requesting_rocket_info( $action, $args ) {
return ( 'query_plugins' === $action || 'plugin_information' === $action ) && isset( $args->slug ) && $args->slug === $this->plugin_slug;
}
/**
* Gets the plugin information data
*
* @return object|\WP_Error
*/
private function get_plugin_information() {
$response = wp_remote_get( $this->api_url );
if ( is_wp_error( $response ) ) {
return $this->get_request_error( $response->get_error_message() );
}
$res = maybe_unserialize( wp_remote_retrieve_body( $response ) );
$code = wp_remote_retrieve_response_code( $response );
if (
200 !== $code
||
! ( is_object( $res ) || is_array( $res ) )
) {
return $this->get_request_error( wp_remote_retrieve_body( $response ) );
}
return $res;
}
/**
* Filter plugin fetching API results to inject Imagify
*
* @param object|WP_Error $result Response object or WP_Error.
* @param string $action The type of information being requested from the Plugin Install API.
* @param object $args Plugin API arguments.
*
* @return object|WP_Error
*/
public function add_plugins_to_result( $result, $action, $args ) {
if ( ! $this->can_add_plugins( $result, $args ) ) {
return $result;
}
$plugins = [
'seo-by-rank-math' => 'seo-by-rank-math/rank-math.php',
'imagify' => 'imagify/imagify.php',
];
// grab all slugs from the api results.
$result_slugs = wp_list_pluck( $result->plugins, 'slug' );
foreach ( $plugins as $slug => $path ) {
if ( is_plugin_active( $path ) || is_plugin_active_for_network( $path ) ) {
continue;
}
if ( in_array( $slug, $result_slugs, true ) ) {
foreach ( $result->plugins as $index => $plugin ) {
if ( is_object( $plugin ) ) {
$plugin = (array) $plugin;
}
if ( $slug === $plugin['slug'] ) {
$move = $plugin;
unset( $result->plugins[ $index ] );
array_unshift( $result->plugins, $move );
}
}
continue;
}
$plugin_data = $this->get_plugin_data( $slug );
if ( empty( $plugin_data ) ) {
continue;
}
array_unshift( $result->plugins, $plugin_data );
}
return $result;
}
/**
* Checks if we can add plugins to the results
*
* @param object|WP_error $result Response object or WP_Error.
* @param object $args Plugin API arguments.
*
* @return bool
*/
private function can_add_plugins( $result, $args ) {
if ( is_wp_error( $result ) ) {
return false;
}
if ( empty( $args->browse ) ) {
return false;
}
if ( 'featured' !== $args->browse && 'recommended' !== $args->browse && 'popular' !== $args->browse ) {
return false;
}
if ( ! isset( $result->info['page'] ) || 1 < $result->info['page'] ) {
return false;
}
return true;
}
/**
* Returns plugin data
*
* @param string $slug Plugin slug.
*
* @return array|object
*/
private function get_plugin_data( string $slug ) {
$query_args = [
'slug' => $slug,
'fields' => [
'icons' => true,
'active_installs' => true,
'short_description' => true,
'group' => true,
],
];
$plugin_data = plugins_api( 'plugin_information', $query_args );
if ( is_wp_error( $plugin_data ) ) {
return [];
}
return $plugin_data;
}
}
@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Plugin;
use WP_Rocket\Abstract_Render;
use WP_Rocket\Engine\License\API\User;
class RenewalNotice extends Abstract_Render {
/**
* User instance
*
* @var User
*/
private $user;
/**
* Constructor
*
* @param User $user User instance.
* @param string $template_path Template path.
*/
public function __construct( User $user, string $template_path ) {
parent::__construct( $template_path );
$this->user = $user;
}
/**
* Display the renewal notice on plugins page
*
* @param string $version Latest version number.
*
* @return void
*/
public function renewal_notice( $version ) {
if ( ! $this->user->is_license_expired() ) {
return;
}
if ( ! $this->is_major_version_available( $version ) ) {
return;
}
$major = $this->extract_major( $version );
$data = [
'version' => $major,
'release_url' => 'https://wp-rocket.me/blog/wp-rocket-' . str_replace( '.', '-', $major ) . '/',
'renew_url' => $this->user->get_renewal_url(),
];
echo $this->generate( 'update-renewal-expired-notice', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Adds styles for expired banner
*
* @param string $version Latest version number.
*
* @return void
*/
public function add_expired_styles( $version ) {
if ( ! $this->user->is_license_expired() ) {
return;
}
if ( ! $this->is_major_version_available( $version ) ) {
return;
}
echo '<style>.plugins tr[data-slug=wp-rocket] th, .plugins tr[data-slug=wp-rocket] td {box-shadow: none !important;}.notice.wp-rocket-update{border-color:#d63638;background-color:#fbf9e8;}.plugin-update .notice.wp-rocket-update a{color:#2782ad;}.wp-rocket-update p::before{display: inline-block;font: normal 20px/1 dashicons;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;vertical-align: top;color: #d63638;content: "\f534";}@media screen and (max-width: 782px){.plugins tr[data-slug=wp-rocket].active + .plugin-update-tr::before {background-color: #f0f6fc;border-left: 4px solid #72aee6;}}</style>';
}
/**
* Checks if a new major version is available
*
* @param string $version Version available from the API.
*
* @return bool
*/
private function is_major_version_available( $version ): bool {
$current_version = rocket_get_constant( 'WP_ROCKET_VERSION', '' );
$current_major = $this->extract_major( $current_version );
$version_major = $this->extract_major( $version );
return version_compare( $current_major, $version_major, '<' );
}
/**
* Extracts the major version number from the provided version
*
* @param string $version Version number.
*
* @return string
*/
private function extract_major( $version ): string {
$parts = explode( '.', $version );
return $parts[0] . '.' . $parts[1];
}
}
@@ -0,0 +1,76 @@
<?php
namespace WP_Rocket\Engine\Plugin;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
/**
* Service provider for the WP Rocket updates.
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provided 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 = [
'plugin_renewal_notice',
'plugin_updater_common_subscriber',
'plugin_information_subscriber',
'plugin_updater_subscriber',
];
/**
* Registers items with the container
*
* @return void
*/
public function register() {
$api_url = wp_parse_url( WP_ROCKET_WEB_INFO );
$this->getContainer()->add( 'plugin_renewal_notice', RenewalNotice::class )
->addArgument( $this->getContainer()->get( 'user' ) )
->addArgument( $this->getContainer()->get( 'template_path' ) . '/plugins/' )
->addTag( 'common_subscriber' );
$this->getContainer()->share( 'plugin_updater_common_subscriber', UpdaterApiCommonSubscriber::class )
->addArgument(
[
'api_host' => $api_url['host'],
'site_url' => home_url(),
'plugin_version' => WP_ROCKET_VERSION,
'settings_slug' => WP_ROCKET_SLUG,
'settings_nonce_key' => WP_ROCKET_PLUGIN_SLUG,
'plugin_options' => $this->getContainer()->get( 'options' ),
]
)
->addTag( 'common_subscriber' );
$this->getContainer()->share( 'plugin_information_subscriber', InformationSubscriber::class )
->addArgument(
[
'plugin_file' => WP_ROCKET_FILE,
'api_url' => WP_ROCKET_WEB_INFO,
]
)
->addTag( 'common_subscriber' );
$this->getContainer()->share( 'plugin_updater_subscriber', UpdaterSubscriber::class )
->addArgument( $this->getContainer()->get( 'plugin_renewal_notice' ) )
->addArgument(
[
'plugin_file' => WP_ROCKET_FILE,
'plugin_version' => WP_ROCKET_VERSION,
'vendor_url' => WP_ROCKET_WEB_MAIN,
'api_url' => WP_ROCKET_WEB_CHECK,
'icons' => [
'2x' => WP_ROCKET_ASSETS_IMG_URL . 'icon-256x256.png',
'1x' => WP_ROCKET_ASSETS_IMG_URL . 'icon-128x128.png',
],
]
)
->addTag( 'common_subscriber' );
}
}
@@ -0,0 +1,144 @@
<?php
namespace WP_Rocket\Engine\Plugin;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Manages common hooks for the plugin updater.
*/
class UpdaterApiCommonSubscriber implements Subscriber_Interface {
/**
* API’s URL domain.
*
* @var string
*/
private $api_host;
/**
* URL to the site’s home.
*
* @var string
*/
private $site_url;
/**
* Current version of the plugin.
*
* @var string
*/
private $plugin_version;
/**
* Key slug used when submitting new settings (POST).
*
* @var string
*/
private $settings_slug;
/**
* The key (1st part of the action) used for the nonce field used on the settings page. It is also used in the page URL.
*
* @var string
*/
private $settings_nonce_key;
/**
* Options instance.
*
* @var \WP_Rocket\Admin\Options
*/
private $plugin_options;
/**
* Constructor
*
* @param array $args {
* Required arguments to populate the class properties.
*
* @type string $api_host API’s URL domain.
* @type string $site_url URL to the site’s home.
* @type string $plugin_version Current version of the plugin.
* @type string $settings_slug Key slug used when submitting new settings (POST).
* @type string $settings_nonce_key The key (1st part of the action) used for the nonce field used on the settings page. It is also used in the page URL.
* @type Options $plugin_options Options instance.
* }
*/
public function __construct( $args ) {
foreach ( [ 'api_host', 'site_url', 'plugin_version', 'settings_slug', 'settings_nonce_key', 'plugin_options' ] as $setting ) {
if ( isset( $args[ $setting ] ) ) {
$this->$setting = $args[ $setting ];
}
}
}
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'http_request_args' => [ 'maybe_set_rocket_user_agent', 10, 2 ],
];
}
/**
* Force our user agent header when we hit our URLs.
*
* @param array $request An array of request arguments.
* @param string $url Requested URL.
* @return array An array of requested arguments
*/
public function maybe_set_rocket_user_agent( $request, $url ) {
if ( ! is_string( $url ) ) {
return $request;
}
if ( $this->api_host && strpos( $url, $this->api_host ) !== false ) {
$request['user-agent'] = sprintf( '%s;%s', $request['user-agent'], $this->get_rocket_user_agent() );
}
return $request;
}
/**
* Get the user agent to use when requesting the API.
*
* @return string WP Rocket user agent
*/
public function get_rocket_user_agent() {
$consumer_key = $this->get_current_option( 'consumer_key' );
$consumer_email = $this->get_current_option( 'consumer_email' );
$php_version = preg_replace( '@^(\d+\.\d+).*@', '\1', phpversion() );
return sprintf( 'WP-Rocket|%s|%s|%s|%s|%s;', $this->plugin_version, $consumer_key, $consumer_email, esc_url( $this->site_url ), $php_version );
}
/**
* Get a plugin option. If the value is currently being posted through the settings page, it is returned instead of the one stored in the database.
*
* @param string $field_name Name of a plugin option.
* @return string
*/
protected function get_current_option( $field_name ) {
if ( current_user_can( 'rocket_manage_options' ) && wp_verify_nonce( filter_input( INPUT_POST, '_wpnonce' ), $this->settings_nonce_key . '-options' ) ) {
$posted = filter_input( INPUT_POST, $this->settings_slug, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
if ( ! empty( $posted[ $field_name ] ) && is_string( $posted[ $field_name ] ) ) {
// The value has been posted through the settings page.
return sanitize_text_field( $posted[ $field_name ] );
}
}
if ( ! $this->plugin_options ) {
return '';
}
$option_value = $this->plugin_options->get( $field_name );
if ( $option_value && is_string( $option_value ) ) {
return $option_value;
}
return '';
}
}
@@ -0,0 +1,68 @@
<?php
namespace WP_Rocket\Engine\Plugin;
use WP_Rocket\Logger\Logger;
/**
* Trait for the plugin updater.
*/
trait UpdaterApiTools {
/**
* Get a \WP_Error object to use when the request to WP Rocket’s server fails.
*
* @param mixed $data Error data to pass along the \WP_Error object.
* @return \WP_Error
*/
protected function get_request_error( $data = [] ) {
if ( ! is_array( $data ) ) {
$data = [
'response' => $data,
];
}
Logger::debug(
'Error when contacting the API.',
array_merge( [ 'Plugin Information' ], $data )
);
return new \WP_Error(
$this->request_error_id,
sprintf(
// translators: %s is an URL.
__( 'An unexpected error occurred. Something may be wrong with WP-Rocket.me or this server&#8217;s configuration. If you continue to have problems, <a href="%s">contact support</a>.', 'rocket' ),
$this->get_support_url()
),
$data
);
}
/**
* Get support URL.
*
* @return string
*/
protected function get_support_url() {
return rocket_get_external_url(
'support',
[
'utm_source' => 'wp_plugin',
'utm_medium' => 'wp_rocket',
]
);
}
/**
* Get a plugin slug, given its full path.
*
* @param string $plugin_file Full path to the plugin.
* @return string
*/
protected function get_plugin_slug( $plugin_file ) {
$plugin_file = trim( $plugin_file, '/' );
$plugin_slug = explode( '/', $plugin_file );
$plugin_slug = end( $plugin_slug );
$plugin_slug = str_replace( '.php', '', $plugin_slug );
return $plugin_slug;
}
}
@@ -0,0 +1,614 @@
<?php
namespace WP_Rocket\Engine\Plugin;
use Plugin_Upgrader;
use Plugin_Upgrader_Skin;
use WP_Error;
use WP_Rocket\Event_Management\{Event_Manager, Event_Manager_Aware_Subscriber_Interface};
/**
* Manages the plugin updates.
*/
class UpdaterSubscriber implements Event_Manager_Aware_Subscriber_Interface {
use UpdaterApiTools;
/**
* Full path to the plugin.
*
* @var string
*/
private $plugin_file;
/**
* Current version of the plugin.
*
* @var string
*/
private $plugin_version;
/**
* URL to the plugin provider.
*
* @var string
*/
private $vendor_url;
/**
* URL to contact to get update info.
*
* @var string
*/
private $api_url;
/**
* A list of plugin’s icon URLs.
*
* @var array {
* @type string $2x URL to the High-DPI size (png or jpg). Optional.
* @type string $1x URL to the normal icon size (png or jpg). Mandatory.
* @type string $svg URL to the svg version of the icon. Optional.
* }
* @see https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons
*/
private $icons;
/**
* An ID to use when a API request fails.
*
* @var string
*/
protected $request_error_id = 'rocket_update_failed';
/**
* Name of the transient that caches the update data.
*
* @var string
*/
protected $cache_transient_name = 'wp_rocket_update_data';
/**
* The WordPress Event Manager
*
* @var Event_Manager
*/
protected $event_manager;
/**
* RenewalNotice instance
*
* @var RenewalNotice
*/
private $renewal_notice;
/**
* Constructor
*
* @param RenewalNotice $renewal_notice RenewalNotice instance.
* @param array $args {
* Required arguments to populate the class properties.
*
* @type string $plugin_file Full path to the plugin.
* @type string $plugin_version Current version of the plugin.
* @type string $vendor_url URL to the plugin provider.
* @type string $api_url URL to contact to get update info.
* }
*/
public function __construct( RenewalNotice $renewal_notice, $args ) {
foreach ( [ 'plugin_file', 'plugin_version', 'vendor_url', 'api_url', 'icons' ] as $setting ) {
if ( isset( $args[ $setting ] ) ) {
$this->$setting = $args[ $setting ];
}
}
$this->renewal_notice = $renewal_notice;
}
/**
* {@inheritdoc}
*
* @param Event_Manager $event_manager The WordPress Event Manager.
*/
public function set_event_manager( Event_Manager $event_manager ) {
$this->event_manager = $event_manager;
}
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'http_request_args' => [ 'exclude_rocket_from_wp_updates', 5, 2 ],
'pre_set_site_transient_update_plugins' => 'maybe_add_rocket_update_data',
'deleted_site_transient' => 'maybe_delete_rocket_update_data_cache',
'wp_rocket_loaded' => 'maybe_force_check',
'auto_update_plugin' => [ 'disable_auto_updates', 10, 2 ],
'admin_post_rocket_rollback' => 'rollback',
'upgrader_pre_install' => [ 'upgrade_pre_install_option', 10, 2 ],
'upgrader_post_install' => [ 'upgrade_post_install_option', 10, 2 ],
'after_plugin_row_wp-rocket/wp-rocket.php' => 'display_renewal_notice',
'admin_print_styles-plugins.php' => 'add_expired_styles',
];
}
/**
* When WP checks plugin versions against the latest versions hosted on WordPress.org, remove WPR from the list.
*
* @see wp_update_plugins()
*
* @param array $request An array of HTTP request arguments.
* @param string $url The request URL.
* @return array Updated array of HTTP request arguments.
*/
public function exclude_rocket_from_wp_updates( $request, $url ) {
if ( ! is_string( $url ) ) {
return $request;
}
if ( ! preg_match( '@^https?://api.wordpress.org/plugins/update-check(/|\?|$)@', $url ) || empty( $request['body']['plugins'] ) ) {
// Not a plugin update request. Stop immediately.
return $request;
}
/**
* Depending on the API version, the data can have several forms:
* - Can be serialized or JSON encoded,
* - Can be an object of arrays or an object of objects.
*/
$is_serialized = is_serialized( $request['body']['plugins'] );
$basename = plugin_basename( $this->plugin_file );
$edited = false;
if ( $is_serialized ) {
$plugins = maybe_unserialize( $request['body']['plugins'] );
} else {
$plugins = json_decode( $request['body']['plugins'] );
}
if ( ! empty( $plugins->plugins ) ) {
if ( is_object( $plugins->plugins ) ) {
if ( isset( $plugins->plugins->$basename ) ) {
unset( $plugins->plugins->$basename );
$edited = true;
}
} elseif ( is_array( $plugins->plugins ) ) {
if ( isset( $plugins->plugins[ $basename ] ) ) {
unset( $plugins->plugins[ $basename ] );
$edited = true;
}
}
}
if ( ! empty( $plugins->active ) ) {
$active_is_object = is_object( $plugins->active );
if ( $active_is_object || is_array( $plugins->active ) ) {
foreach ( $plugins->active as $key => $plugin_basename ) {
if ( $plugin_basename !== $basename ) {
continue;
}
if ( $active_is_object ) {
unset( $plugins->active->$key );
} else {
unset( $plugins->active[ $key ] );
}
$edited = true;
break;
}
}
}
if ( $edited ) {
if ( $is_serialized ) {
$request['body']['plugins'] = maybe_serialize( $plugins );
} else {
$request['body']['plugins'] = wp_json_encode( $plugins );
}
}
return $request;
}
/**
* Add WPR update data to the "WP update" transient.
*
* @param \stdClass $transient_value New value of site transient.
* @return \stdClass
*/
public function maybe_add_rocket_update_data( $transient_value ) {
if ( defined( 'WP_INSTALLING' ) ) {
return $transient_value;
}
// Get the remote version data.
$remote_data = $this->get_cached_latest_version_data();
if ( is_wp_error( $remote_data ) ) {
return $transient_value;
}
// Make sure the transient value is well formed.
if ( ! is_object( $transient_value ) ) {
$transient_value = new \stdClass();
}
if ( empty( $transient_value->response ) ) {
$transient_value->response = [];
}
if ( empty( $transient_value->checked ) ) {
$transient_value->checked = [];
}
// If a newer version is available, add the update.
if ( version_compare( $this->plugin_version, $remote_data->new_version, '<' ) ) {
$transient_value->response[ $remote_data->plugin ] = $remote_data;
}
$transient_value->checked[ $remote_data->plugin ] = $this->plugin_version;
return $transient_value;
}
/**
* Delete WPR update data cache when the "WP update" transient is deleted.
*
* @param string $transient_name Deleted transient name.
*/
public function maybe_delete_rocket_update_data_cache( $transient_name ) {
if ( 'update_plugins' === $transient_name ) {
$this->delete_rocket_update_data_cache();
}
}
/**
* If the `rocket_force_update` query arg is set, force WP to refresh the list of plugins to update.
*/
public function maybe_force_check() {
if ( is_string( filter_input( INPUT_GET, 'rocket_force_update' ) ) ) {
delete_site_transient( 'update_plugins' );
}
}
/**
* Disable auto-updates for WP Rocket
*
* @param bool|null $update Whether to update. The value of null is internally used to detect whether nothing has hooked into this filter.
* @param object $item The update offer.
* @return bool|null
*/
public function disable_auto_updates( $update, $item ) {
if ( isset( $item->plugin ) && ( 'wp-rocket/wp-rocket.php' === $item->plugin ) ) {
return false;
}
return $update;
}
/**
* Get the latest WPR update data from our server.
*
* @return \stdClass|\WP_Error {
* A \WP_Error object on failure. An object on success:
*
* @type string $slug The plugin slug.
* @type string $plugin The plugin base name.
* @type string $new_version The plugin new version.
* @type string $url URL to the plugin provider.
* @type string $package URL to the zip file of the new version.
* @type array $icons {
* A list of plugin’s icon URLs.
*
* @type string $2x URL to the High-DPI size (png or jpg). Optional.
* @type string $1x URL to the normal icon size (png or jpg). Mandatory.
* @type string $svg URL to the svg version of the icon. Optional.
* }
* }
*/
public function get_latest_version_data() {
$request = wp_remote_get(
$this->api_url,
[
'timeout' => 30,
]
);
if ( is_wp_error( $request ) ) {
return $this->get_request_error(
[
'error_code' => $request->get_error_code(),
'response' => $request->get_error_message(),
]
);
}
$res = trim( wp_remote_retrieve_body( $request ) );
$code = wp_remote_retrieve_response_code( $request );
if ( 200 !== $code ) {
/**
* If the response doesn’t have a status 200: it is an error, or there is no new update.
*/
return $this->get_request_error(
[
'http_code' => $code,
'response' => $res,
]
);
}
/**
* This will match:
* - `2.3.4.5-beta1||1.2.3.4-beta2||||||||||||||||||||||||||||||||`: expired license.
* - `2.3.4.5-beta1|https://wp-rocket.me/i-should-write-a-funny-thing-here/wp-rocket_1.2.3.4-beta2.zip|1.2.3.4-beta2`: valid license.
*/
if ( ! preg_match( '@^(?<stable_version>\d+(?:\.\d+){1,3}[^|]*)\|(?<package>(?:http.+\.zip)?)\|(?<user_version>\d+(?:\.\d+){1,3}[^|]*)(?:\|+)?$@', $res, $match ) ) {
/**
* If the response doesn’t have the right format, it is an error.
*/
return $this->get_request_error( $res );
}
$obj = new \stdClass();
$obj->slug = $this->get_plugin_slug( $this->plugin_file );
$obj->plugin = plugin_basename( $this->plugin_file );
$obj->new_version = $match['user_version'];
$obj->url = $this->vendor_url;
$obj->package = $match['package'];
$obj->stable_version = $match['stable_version'];
/**
* Filters the WP tested version value
*
* @since 3.10.7
*
* @param string $wp_tested_version WP tested version value.
*/
$obj->tested = apply_filters( 'rocket_wp_tested_version', WP_ROCKET_WP_VERSION_TESTED );
if ( $this->icons && ! empty( $this->icons['1x'] ) ) {
$obj->icons = $this->icons;
}
return $obj;
}
/**
* Get the cached version of the latest WPR update data.
*
* @return \stdClass|\WP_Error {
* A \WP_Error object on failure. An object on success:
*
* @type string $slug The plugin slug.
* @type string $plugin The plugin base name.
* @type string $new_version The plugin new version.
* @type string $url URL to the plugin provider.
* @type string $package URL to the zip file of the new version.
* @type array $icons {
* A list of plugin’s icon URLs.
*
* @type string $2x URL to the High-DPI size (png or jpg). Optional.
* @type string $1x URL to the normal icon size (png or jpg). Mandatory.
* @type string $svg URL to the svg version of the icon. Optional.
* }
* }
*/
public function get_cached_latest_version_data() {
static $response;
if ( isset( $response ) ) {
// "force update" won’t bypass the static cache: only one http request by page load.
return $response;
}
$force_update = is_string( filter_input( INPUT_GET, 'rocket_force_update' ) );
if ( ! $force_update ) {
// No "force update": try to get the result from a transient.
$response = get_site_transient( $this->cache_transient_name );
if ( $response && is_object( $response ) ) {
// Got something in cache.
return $response;
}
}
// Get fresh data.
$response = $this->get_latest_version_data();
$cache_duration = 12 * HOUR_IN_SECONDS;
if ( is_wp_error( $response ) ) {
$error_data = $response->get_error_data();
if ( ! empty( $error_data['error_code'] ) ) {
// `wp_remote_get()` returned an internal error ('error_code' contains a WP_Error code ).
$cache_duration = HOUR_IN_SECONDS;
} elseif ( ! empty( $error_data['http_code'] ) && $error_data['http_code'] >= 400 ) {
// We got a 4xx or 5xx HTTP error.
$cache_duration = 2 * HOUR_IN_SECONDS;
}
}
set_site_transient( $this->cache_transient_name, $response, $cache_duration );
return $response;
}
/**
* Delete WP Rocket update data cache.
*/
public function delete_rocket_update_data_cache() {
delete_site_transient( $this->cache_transient_name );
}
/**
* Do the rollback
*
* @since 2.4
*/
public function rollback() {
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'rocket_rollback' ) ) {
wp_nonce_ays( '' );
}
if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_nonce_ays( '' );
}
/**
* Fires before doing the rollback
*/
do_action( 'rocket_before_rollback' );
$plugin_transient = get_site_transient( 'update_plugins' );
$plugin_folder = plugin_basename( dirname( WP_ROCKET_FILE ) );
$plugin = $plugin_folder . '/' . basename( WP_ROCKET_FILE );
$plugin_transient->response[ $plugin ] = (object) [
'slug' => $plugin_folder,
'new_version' => WP_ROCKET_LASTVERSION,
'url' => 'https://wp-rocket.me',
'package' => sprintf( 'https://wp-rocket.me/%s/wp-rocket_%s.zip', get_rocket_option( 'consumer_key' ), WP_ROCKET_LASTVERSION ),
];
$this->event_manager->remove_callback( 'pre_set_site_transient_update_plugins', [ $this, 'maybe_add_rocket_update_data' ] );
set_site_transient( 'update_plugins', $plugin_transient );
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
// translators: %s is the plugin name.
$title = sprintf( __( '%s Update Rollback', 'rocket' ), WP_ROCKET_PLUGIN_NAME );
$nonce = 'upgrade-plugin_' . $plugin;
$url = 'update.php?action=upgrade-plugin&plugin=' . rawurlencode( $plugin );
$upgrader_skin = new Plugin_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'plugin' ) );
$upgrader = new Plugin_Upgrader( $upgrader_skin );
add_filter( 'update_plugin_complete_actions', [ $this, 'rollback_add_return_link' ] );
rocket_put_content( WP_CONTENT_DIR . '/advanced-cache.php', '' );
$upgrader->upgrade( $plugin );
wp_die(
'',
// translators: %s is the plugin name.
esc_html( sprintf( __( '%s Update Rollback', 'rocket' ), WP_ROCKET_PLUGIN_NAME ) ),
[
'response' => 200,
]
);
}
/**
* After a rollback has been done, replace the "return to" link by a link pointing to WP Rocket's tools page.
* A link to the plugins page is kept in case the plugin is not reactivated correctly.
*
* @since 3.2.4
*
* @param array $update_actions Array of plugin action links.
* @return array The array of links where the "return to" link has been replaced.
*/
public function rollback_add_return_link( $update_actions ) {
if ( ! isset( $update_actions['plugins_page'] ) ) {
return $update_actions;
}
$update_actions['plugins_page'] = sprintf(
/* translators: 1 and 3 are link openings, 2 is a link closing. */
__( '%1$sReturn to WP Rocket%2$s or %3$sgo to Plugins page%2$s', 'rocket' ),
'<a href="' . esc_url( admin_url( 'options-general.php?page=' . WP_ROCKET_PLUGIN_SLUG ) . '#tools' ) . '" target="_parent">',
'</a>',
'<a href="' . esc_url( admin_url( 'plugins.php' ) ) . '" target="_parent">'
);
return $update_actions;
}
/**
* Set plugin option before upgrade.
*
* @param mixed $return The result of the upgrade process.
* @param array $plugin The plugin data.
*
* @return mixed|WP_Error
*/
public function upgrade_pre_install_option( $return, $plugin = [] ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.returnFound
if ( is_wp_error( $return ) || ! $plugin ) {
return $return;
}
$plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
if ( empty( $plugin ) || 'wp-rocket/wp-rocket.php' !== $plugin ) {
return $return;
}
set_transient( 'wp_rocket_updating', true, MINUTE_IN_SECONDS );
return $return;
}
/**
* Update plugin option after upgrade.
*
* @param mixed $return The result of the upgrade process.
* @param array $plugin The plugin data.
*
* @return mixed|string|WP_Error
*/
public function upgrade_post_install_option( $return, $plugin = [] ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.returnFound
if ( is_wp_error( $return ) || ! $plugin ) {
return $return;
}
$plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
if ( empty( $plugin ) || 'wp-rocket/wp-rocket.php' !== $plugin ) {
return $return;
}
delete_transient( 'wp_rocket_updating' );
return $return;
}
/**
* Displays Renewal notice on the plugins page
*
* @return void
*/
public function display_renewal_notice() {
$latest_version_data = $this->get_cached_latest_version_data();
if ( is_wp_error( $latest_version_data ) ) {
return;
}
if ( ! isset( $latest_version_data->stable_version ) ) {
return;
}
$this->renewal_notice->renewal_notice( $latest_version_data->stable_version );
}
/**
* Adds styles for expired banner
*
* @return void
*/
public function add_expired_styles() {
$latest_version_data = $this->get_cached_latest_version_data();
if ( is_wp_error( $latest_version_data ) ) {
return;
}
if ( ! isset( $latest_version_data->stable_version ) ) {
return;
}
$this->renewal_notice->add_expired_styles( $latest_version_data->stable_version );
}
}