add wp-rocket

This commit is contained in:
nguyen dung
2022-02-18 19:09:35 +07:00
parent 39b8cb3612
commit 3110d00ee7
927 changed files with 271703 additions and 2 deletions

View File

@@ -0,0 +1,324 @@
<?php
namespace WP_Rocket\Engine\License\API;
class Pricing {
/**
* The pricing data object
*
* @var object
*/
private $pricing;
/**
* Instantiate the class
*
* @param object $pricing The pricing object.
*/
public function __construct( $pricing ) {
$this->pricing = $pricing;
}
/**
* Gets the single license pricing data
*
* @return null|object
*/
public function get_single_pricing() {
if (
! isset( $this->pricing->licenses->single )
||
! is_object( $this->pricing->licenses->single )
) {
return null;
}
return $this->pricing->licenses->single;
}
/**
* Gets the plus license pricing data
*
* @return null|object
*/
public function get_plus_pricing() {
if (
! isset( $this->pricing->licenses->plus )
||
! is_object( $this->pricing->licenses->plus )
) {
return null;
}
return $this->pricing->licenses->plus;
}
/**
* Gets the infinite license pricing data
*
* @return null|object
*/
public function get_infinite_pricing() {
if (
! isset( $this->pricing->licenses->infinite )
||
! is_object( $this->pricing->licenses->infinite )
) {
return null;
}
return $this->pricing->licenses->infinite;
}
/**
* Gets the license renewal pricing data
*
* @return null|object
*/
public function get_renewals_data() {
if (
! isset( $this->pricing->renewals )
||
! is_object( $this->pricing->renewals )
) {
return null;
}
return $this->pricing->renewals;
}
/**
* Gets the promotion data
*
* @return null|object
*/
public function get_promo_data() {
if (
! isset( $this->pricing->promo )
||
! is_object( $this->pricing->promo )
) {
return null;
}
return $this->pricing->promo;
}
/**
* Checks if a promotion is currently active
*
* @return boolean
*/
public function is_promo_active() {
$promo_data = $this->get_promo_data();
if ( is_null( $promo_data ) ) {
return false;
}
if ( ! isset( $promo_data->start_date, $promo_data->end_date ) ) {
return false;
}
$current_time = time();
return (
absint( $promo_data->start_date ) < $current_time
&&
absint( $promo_data->end_date ) > $current_time
);
}
/**
* Gets promotion end date
*
* @return int
*/
public function get_promo_end() {
$promo = $this->get_promo_data();
if (
is_null( $promo )
||
! isset( $promo->end_date )
) {
return 0;
}
return absint( $promo->end_date );
}
/**
* Gets the regular upgrade price from single to plus
*
* @return int
*/
public function get_regular_single_to_plus_price() {
$plus_pricing = $this->get_plus_pricing();
if (
is_null( $plus_pricing )
||
! isset( $plus_pricing->prices->from_single->regular )
) {
return 0;
}
return $plus_pricing->prices->from_single->regular;
}
/**
* Gets the current upgrade price from single to plus
*
* @return int
*/
public function get_single_to_plus_price() {
$plus_pricing = $this->get_plus_pricing();
$regular = $this->get_regular_single_to_plus_price();
if (
is_null( $plus_pricing )
||
! isset( $plus_pricing->prices->from_single->sale )
) {
return $regular;
}
return $this->is_promo_active() ? $plus_pricing->prices->from_single->sale : $regular;
}
/**
* Gets the regular upgrade price from single to infinite
*
* @return int
*/
public function get_regular_single_to_infinite_price() {
$infinite_pricing = $this->get_infinite_pricing();
if (
is_null( $infinite_pricing )
||
! isset( $infinite_pricing->prices->from_single->regular )
) {
return 0;
}
return $infinite_pricing->prices->from_single->regular;
}
/**
* Gets the current upgrade price from single to plus
*
* @return int
*/
public function get_single_to_infinite_price() {
$infinite_pricing = $this->get_infinite_pricing();
$regular = $this->get_regular_single_to_infinite_price();
if (
is_null( $infinite_pricing )
||
! isset( $infinite_pricing->prices->from_single->sale )
) {
return $regular;
}
return $this->is_promo_active() ? $infinite_pricing->prices->from_single->sale : $regular;
}
/**
* Gets the regular upgrade price from plus to infinite
*
* @return int
*/
public function get_regular_plus_to_infinite_price() {
$infinite_pricing = $this->get_infinite_pricing();
if (
is_null( $infinite_pricing )
||
! isset( $infinite_pricing->prices->from_plus->regular )
) {
return 0;
}
return $infinite_pricing->prices->from_plus->regular;
}
/**
* Gets the current upgrade price from plus to infinite
*
* @return int
*/
public function get_plus_to_infinite_price() {
$infinite_pricing = $this->get_infinite_pricing();
$regular = $this->get_regular_plus_to_infinite_price();
if (
is_null( $infinite_pricing )
||
! isset( $infinite_pricing->prices->from_plus->sale )
) {
return $regular;
}
return $this->is_promo_active() ? $infinite_pricing->prices->from_plus->sale : $regular;
}
/**
* Gets the number of websites allowed for the single license
*
* @return int
*/
public function get_single_websites_count() {
$single_pricing = $this->get_single_pricing();
if (
is_null( $single_pricing )
||
! isset( $single_pricing->websites )
) {
return 0;
}
return (int) $single_pricing->websites;
}
/**
* Gets the number of websites allowed for the plus license
*
* @return int
*/
public function get_plus_websites_count() {
$plus_pricing = $this->get_plus_pricing();
if (
is_null( $plus_pricing )
||
! isset( $plus_pricing->websites )
) {
return 0;
}
return (int) $plus_pricing->websites;
}
/**
* Gets the number of websites allowed for the infinite license
*
* @return int
*/
public function get_infinite_websites_count() {
$infinite_pricing = $this->get_infinite_pricing();
if (
is_null( $infinite_pricing )
||
! isset( $infinite_pricing->websites )
) {
return 0;
}
return (int) $infinite_pricing->websites;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace WP_Rocket\Engine\License\API;
class PricingClient {
const PRICING_ENDPOINT = 'https://wp-rocket.me/stat/1.0/wp-rocket/pricing.php';
/**
* Gets pricing data from cache if it exists, else gets it from the pricing endpoint
*
* Cache the pricing data for 6 hours in a transient
*
* @since 3.7.3
*
* @return bool|object
*/
public function get_pricing_data() {
$cached_data = get_transient( 'wp_rocket_pricing' );
if ( false !== $cached_data ) {
return $cached_data;
}
$data = $this->get_raw_pricing_data();
if ( false === $data ) {
return false;
}
set_transient( 'wp_rocket_pricing', $data, 6 * HOUR_IN_SECONDS );
return $data;
}
/**
* Gets the pricing data from the pricing endpoint
*
* @since 3.7.3
*
* @return bool|object
*/
private function get_raw_pricing_data() {
$response = wp_safe_remote_get(
self::PRICING_ENDPOINT
);
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return false;
}
$body = wp_remote_retrieve_body( $response );
if ( empty( $body ) ) {
return false;
}
return json_decode( $body );
}
}

View File

@@ -0,0 +1,121 @@
<?php
namespace WP_Rocket\Engine\License\API;
class User {
/**
* The user object
*
* @var object
*/
private $user;
/**
* Instantiate the class
*
* @param object $user The user object.
*/
public function __construct( $user ) {
$this->user = is_object( $user ) ? $user : new \stdClass();
}
/**
* Gets the user license type
*
* @return int
*/
public function get_license_type() {
if ( ! isset( $this->user->licence_account ) ) {
return 0;
}
return (int) $this->user->licence_account;
}
/**
* Gets the user license expiration timestamp
*
* @return int
*/
public function get_license_expiration() {
if ( ! isset( $this->user->licence_expiration ) ) {
return 0;
}
return (int) $this->user->licence_expiration;
}
/**
* Checks if the user license is expired
*
* @return boolean
*/
public function is_license_expired() {
return time() > $this->get_license_expiration();
}
/**
* Gets the user license creation date
*
* @return int
*/
public function get_creation_date() {
if ( ! isset( $this->user->date_created ) ) {
return 0;
}
return (int) $this->user->date_created;
}
/**
* Checks if user has auto-renew enabled
*
* @return boolean
*/
public function is_auto_renew() {
if ( ! isset( $this->user->has_auto_renew ) ) {
return false;
}
return (bool) $this->user->has_auto_renew;
}
/**
* Gets the upgrade to plus URL
*
* @return string
*/
public function get_upgrade_plus_url() {
if ( ! isset( $this->user->upgrade_plus_url ) ) {
return '';
}
return $this->user->upgrade_plus_url;
}
/**
* Gets the upgrade to infinite url
*
* @return string
*/
public function get_upgrade_infinite_url() {
if ( ! isset( $this->user->upgrade_infinite_url ) ) {
return '';
}
return $this->user->upgrade_infinite_url;
}
/**
* Gets the renewal url
*
* @return string
*/
public function get_renewal_url() {
if ( ! isset( $this->user->renewal_url ) ) {
return '';
}
return $this->user->renewal_url;
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace WP_Rocket\Engine\License\API;
use WP_Rocket\Admin\Options_Data;
class UserClient {
const USER_ENDPOINT = 'https://wp-rocket.me/stat/1.0/wp-rocket/user.php';
/**
* WP Rocket options instance
*
* @var Options_Data
*/
private $options;
/**
* Instantiate the class
*
* @param Options_Data $options WP Rocket options instance.
*/
public function __construct( Options_Data $options ) {
$this->options = $options;
}
/**
* Gets user data from cache if it exists, else gets it from the user endpoint
*
* Cache the user data for 24 hours in a transient
*
* @since 3.7.3
*
* @return bool|object
*/
public function get_user_data() {
$cached_data = get_transient( 'wp_rocket_customer_data' );
if ( false !== $cached_data ) {
return $cached_data;
}
$data = $this->get_raw_user_data();
if ( false === $data ) {
return false;
}
set_transient( 'wp_rocket_customer_data', $data, DAY_IN_SECONDS );
return $data;
}
/**
* Gets the user data from the user endpoint
*
* @since 3.7.3
*
* @return bool|object
*/
private function get_raw_user_data() {
$customer_key = ! empty( $this->options->get( 'consumer_key', '' ) )
? $this->options->get( 'consumer_key', '' )
: rocket_get_constant( 'WP_ROCKET_KEY', '' );
$customer_email = ! empty( $this->options->get( 'consumer_email', '' ) )
? $this->options->get( 'consumer_email', '' )
: rocket_get_constant( 'WP_ROCKET_EMAIL', '' );
$response = wp_safe_remote_post(
self::USER_ENDPOINT,
[
'body' => 'user_id=' . rawurlencode( $customer_email ) . '&consumer_key=' . sanitize_key( $customer_key ),
]
);
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return false;
}
$body = wp_remote_retrieve_body( $response );
if ( empty( $body ) ) {
return false;
}
return json_decode( $body );
}
}

View File

@@ -0,0 +1,313 @@
<?php
namespace WP_Rocket\Engine\License;
use WP_Rocket\Abstract_Render;
use WP_Rocket\Engine\License\API\Pricing;
use WP_Rocket\Engine\License\API\User;
class Renewal extends Abstract_Render {
/**
* Pricing instance
*
* @var Pricing
*/
private $pricing;
/**
* User instance
*
* @var User
*/
private $user;
/**
* Instantiate the class
*
* @param Pricing $pricing Pricing instance.
* @param User $user User instance.
* @param string $template_path Path to the views.
*/
public function __construct( Pricing $pricing, User $user, $template_path ) {
parent::__construct( $template_path );
$this->pricing = $pricing;
$this->user = $user;
}
/**
* Displays the renewal banner for users expiring in less than 30 days
*
* @since 3.7.5
*
* @return void
*/
public function display_renewal_soon_banner() {
if ( rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' ) ) {
return;
}
if ( $this->user->is_license_expired() ) {
return;
}
if ( ! $this->is_expired_soon() ) {
return;
}
$data = $this->get_banner_data();
$data['countdown'] = $this->get_countdown_data();
echo $this->generate( 'renewal-soon-banner', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Displays the renewal banner for expired users
*
* @since 3.7.5
*
* @return void
*/
public function display_renewal_expired_banner() {
if ( rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' ) ) {
return;
}
if ( 0 === $this->user->get_license_expiration() ) {
return;
}
if ( ! $this->user->is_license_expired() ) {
return;
}
if ( false !== get_transient( 'rocket_renewal_banner_' . get_current_user_id() ) ) {
return;
}
echo $this->generate( 'renewal-expired-banner', $this->get_banner_data() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Get base data to display in the banners
*
* @since 3.7.5
*
* @return array
*/
private function get_banner_data() {
return [
'discount_percent' => $this->get_discount_percent(),
'discount_price' => number_format_i18n( $this->get_discount_price(), 2 ),
'renewal_url' => $this->user->get_renewal_url(),
];
}
/**
* AJAX callback to dismiss the renewal banner for expired users
*
* @since 3.7.5
*
* @return void
*/
public function dismiss_renewal_expired_banner() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
$transient = 'rocket_renewal_banner_' . get_current_user_id();
if ( false !== get_transient( $transient ) ) {
return;
}
set_transient( $transient, 1, MONTH_IN_SECONDS );
wp_send_json_success();
}
/**
* Adds the license expiration time to WP Rocket localize script data
*
* @since 3.7.5
*
* @param array $data Localize script data.
* @return array
*/
public function add_localize_script_data( $data ) {
if ( ! is_array( $data ) ) {
$data = (array) $data;
}
if ( $this->user->is_license_expired() ) {
return $data;
}
if ( ! $this->is_expired_soon() ) {
return $data;
}
$data['license_expiration'] = $this->user->get_license_expiration();
return $data;
}
/**
* Checks if the license expires in less than 30 days
*
* @since 3.7.5
*
* @return boolean
*/
private function is_expired_soon() {
if ( $this->user->is_auto_renew() ) {
return false;
}
$expiration_delay = $this->user->get_license_expiration() - time();
return 30 * DAY_IN_SECONDS > $expiration_delay;
}
/**
* Gets the discount percentage corresponding to the current user status
*
* @since 3.7.5
*
* @return int
*/
private function get_discount_percent() {
$renewals = $this->get_user_renewal_status();
if ( false === $renewals ) {
return 0;
}
if ( $renewals['is_expired'] ) {
return isset( $renewals['discount_percent']->is_expired ) ? $renewals['discount_percent']->is_expired : 0;
}
if ( $renewals['is_grandfather'] ) {
return isset( $renewals['discount_percent']->is_grandfather ) ? $renewals['discount_percent']->is_grandfather : 0;
}
return isset( $renewals['discount_percent']->not_grandfather ) ? $renewals['discount_percent']->not_grandfather : 0;
}
/**
* Gets the discount price corresponding to the current user status
*
* @since 3.7.5
*
* @return int
*/
private function get_discount_price() {
$renewals = $this->get_user_renewal_status();
if ( false === $renewals ) {
return 0;
}
$license = $this->get_license_pricing_data();
if ( $renewals['is_expired'] ) {
return isset( $license->prices->renewal->is_expired ) ? $license->prices->renewal->is_expired : 0;
}
if ( $renewals['is_grandfather'] ) {
return isset( $license->prices->renewal->is_grandfather ) ? $license->prices->renewal->is_grandfather : 0;
}
return isset( $license->prices->renewal->not_grandfather ) ? $license->prices->renewal->not_grandfather : 0;
}
/**
* Gets the user renewal status
*
* @since 3.7.5
*
* @return array
*/
private function get_user_renewal_status() {
$renewals = $this->pricing->get_renewals_data();
if ( ! isset( $renewals->extra_days, $renewals->grandfather_date, $renewals->discount_percent ) ) {
return false;
}
return [
'discount_percent' => $renewals->discount_percent,
'is_expired' => time() > ( $this->user->get_license_expiration() + ( $renewals->extra_days * DAY_IN_SECONDS ) ),
'is_grandfather' => $renewals->grandfather_date > $this->user->get_creation_date(),
];
}
/**
* Gets the license pricing data corresponding to the user license
*
* @since 3.7.5
*
* @return object|null
*/
private function get_license_pricing_data() {
$license = $this->user->get_license_type();
$plus_websites = $this->pricing->get_plus_websites_count();
if ( $license === $plus_websites ) {
return $this->pricing->get_plus_pricing();
} elseif (
$license >= $this->pricing->get_single_websites_count()
&&
$license < $plus_websites
) {
return $this->pricing->get_single_pricing();
}
return $this->pricing->get_infinite_pricing();
}
/**
* Gets the countdown data to display for the renewal soon banner
*
* @since 3.7.5
*
* @return array
*/
private function get_countdown_data() {
$data = [
'days' => 0,
'hours' => 0,
'minutes' => 0,
'seconds' => 0,
];
if ( rocket_get_constant( 'WP_ROCKET_IS_TESTING', false ) ) {
return $data;
}
$expiration = $this->user->get_license_expiration();
if ( 0 === $expiration ) {
return $data;
}
$now = date_create();
$end = date_timestamp_set( date_create(), $expiration );
if ( $now > $end ) {
return $data;
}
$remaining = date_diff( $now, $end );
$format = explode( ' ', $remaining->format( '%d %H %i %s' ) );
$data['days'] = $format[0];
$data['hours'] = $format[1];
$data['minutes'] = $format[2];
$data['seconds'] = $format[3];
return $data;
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace WP_Rocket\Engine\License;
use WP_Rocket\Engine\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\License\API\PricingClient;
use WP_Rocket\Engine\License\API\Pricing;
use WP_Rocket\Engine\License\API\UserClient;
use WP_Rocket\Engine\License\API\User;
use WP_Rocket\Engine\License\Renewal;
use WP_Rocket\Engine\License\Subscriber;
use WP_Rocket\Engine\License\Upgrade;
/**
* Service Provider for the License module
*
* @since 3.7.3
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* Aliases the service provider provides
*
* @var array
*/
protected $provides = [
'pricing_client',
'user_client',
'pricing',
'user',
'upgrade',
'renewal',
'license_subscriber',
];
/**
* Registers items with the container
*
* @return void
*/
public function register() {
$views = __DIR__ . '/views';
$this->getContainer()->add( 'pricing_client', PricingClient::class );
$this->getContainer()->add( 'user_client', UserClient::class )
->withArgument( $this->getContainer()->get( 'options' ) );
$this->getContainer()->share( 'pricing', Pricing::class )
->withArgument( $this->getContainer()->get( 'pricing_client' )->get_pricing_data() );
$this->getContainer()->share( 'user', User::class )
->withArgument( $this->getContainer()->get( 'user_client' )->get_user_data() );
$this->getContainer()->add( 'upgrade', Upgrade::class )
->withArgument( $this->getContainer()->get( 'pricing' ) )
->withArgument( $this->getContainer()->get( 'user' ) )
->withArgument( $views );
$this->getContainer()->add( 'renewal', Renewal::class )
->withArgument( $this->getContainer()->get( 'pricing' ) )
->withArgument( $this->getContainer()->get( 'user' ) )
->withArgument( $views );
$this->getContainer()->share( 'license_subscriber', Subscriber::class )
->withArgument( $this->getContainer()->get( 'upgrade' ) )
->withArgument( $this->getContainer()->get( 'renewal' ) );
}
}

View File

@@ -0,0 +1,187 @@
<?php
namespace WP_Rocket\Engine\License;
use WP_Rocket\Event_Management\Subscriber_Interface;
class Subscriber implements Subscriber_Interface {
/**
* Upgrade instance
*
* @var Upgrade
*/
private $upgrade;
/**
* Renewal instance
*
* @var Renewal
*/
private $renewal;
/**
* Instantiate the class
*
* @param Upgrade $upgrade Upgrade instance.
* @param Renewal $renewal Renewal instance.
*/
public function __construct( Upgrade $upgrade, Renewal $renewal ) {
$this->upgrade = $upgrade;
$this->renewal = $renewal;
}
/**
* Events this subscriber listens to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'rocket_dashboard_license_info' => 'display_upgrade_section',
'rocket_settings_page_footer' => 'display_upgrade_popin',
'rocket_menu_title' => 'add_notification_bubble',
'admin_footer-settings_page_wprocket' => 'dismiss_notification_bubble',
'rocket_before_dashboard_content' => [
[ 'display_promo_banner' ],
[ 'display_renewal_soon_banner', 11 ],
[ 'display_renewal_expired_banner', 12 ],
],
'wp_ajax_rocket_dismiss_promo' => 'dismiss_promo_banner',
'wp_ajax_rocket_dismiss_renewal' => 'dismiss_renewal_banner',
'rocket_localize_admin_script' => 'add_localize_script_data',
'wp_rocket_upgrade' => [ 'clean_user_transient', 15, 2 ],
];
}
/**
* Displays the upgrade section in the license info block
*
* @since 3.7.3
*
* @return void
*/
public function display_upgrade_section() {
$this->upgrade->display_upgrade_section();
}
/**
* Displays the upgrade popin
*
* @since 3.7.3
*
* @return void
*/
public function display_upgrade_popin() {
$this->upgrade->display_upgrade_popin();
}
/**
* Adds the notification bubble to the menu title if a promotion is active
*
* @since 3.7.4
*
* @param string $menu_title The text to be used for the menu.
* @return string
*/
public function add_notification_bubble( $menu_title ) {
return $this->upgrade->add_notification_bubble( $menu_title );
}
/**
* Prevents the notification bubble from showing once the user accessed the dashboard once
*
* @since 3.7.4
*
* @return void
*/
public function dismiss_notification_bubble() {
$this->upgrade->dismiss_notification_bubble();
}
/**
* Displays the promotions banner when a promotion is active
*
* @since 3.7.4
*
* @return void
*/
public function display_promo_banner() {
$this->upgrade->display_promo_banner();
}
/**
* AJAX callback to dismiss the promotion banner
*
* @since 3.7.4
*
* @return void
*/
public function dismiss_promo_banner() {
$this->upgrade->dismiss_promo_banner();
}
/**
* Adds the current time and promotion end time to WP Rocket localize script data
*
* @since 3.7.5 Add the renewal localize data
* @since 3.7.4
*
* @param array $data Localize script data.
* @return array
*/
public function add_localize_script_data( $data ) {
$data = $this->upgrade->add_localize_script_data( $data );
return $this->renewal->add_localize_script_data( $data );
}
/**
* Deletes the user data transient on 3.7.4 update
*
* @since 3.7.4
*
* @param string $new_version New version of the plugin.
* @param string $old_version Installed version of the plugin.
* @return void
*/
public function clean_user_transient( $new_version, $old_version ) {
if ( version_compare( $old_version, '3.7.4', '>' ) ) {
return;
}
delete_transient( 'wp_rocket_customer_data' );
}
/**
* Displays the renewal banner for users expiring in less than 30 days
*
* @since 3.7.5
*
* @return void
*/
public function display_renewal_soon_banner() {
$this->renewal->display_renewal_soon_banner();
}
/**
* Displays the renewal banner for expired users
*
* @since 3.7.5
*
* @return void
*/
public function display_renewal_expired_banner() {
$this->renewal->display_renewal_expired_banner();
}
/**
* AJAX callback to dismiss the renewal banner
*
* @since 3.7.5
*
* @return void
*/
public function dismiss_renewal_banner() {
$this->renewal->dismiss_renewal_expired_banner();
}
}

View File

@@ -0,0 +1,404 @@
<?php
namespace WP_Rocket\Engine\License;
use WP_Rocket\Abstract_Render;
use WP_Rocket\Engine\License\API\Pricing;
use WP_Rocket\Engine\License\API\User;
class Upgrade extends Abstract_Render {
/**
* Pricing instance
*
* @var Pricing
*/
private $pricing;
/**
* User instance
*
* @var User
*/
private $user;
/**
* Instantiate the class
*
* @param Pricing $pricing Pricing instance.
* @param User $user User instance.
* @param string $template_path Path to the views.
*/
public function __construct( Pricing $pricing, User $user, $template_path ) {
parent::__construct( $template_path );
$this->pricing = $pricing;
$this->user = $user;
}
/**
* Displays the upgrade section in the license block on the dashboard
*
* @return void
*/
public function display_upgrade_section() {
if ( ! $this->can_upgrade() ) {
return;
}
echo $this->generate( 'upgrade-section' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Displays the upgrade pop on the dashboard
*
* @return void
*/
public function display_upgrade_popin() {
if ( rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' ) ) {
return;
}
if ( ! $this->can_upgrade() ) {
return;
}
$data = [
'is_promo_active' => $this->pricing->is_promo_active(),
'upgrades' => $this->get_upgrade_choices(),
];
echo $this->generate( 'upgrade-popin', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Adds the notification bubble to WP Rocket menu item when a promo is active
*
* @param string $menu_title Menu title.
* @return string
*/
public function add_notification_bubble( $menu_title ) {
if ( ! $this->can_use_promo() ) {
return $menu_title;
}
if ( false !== get_transient( 'rocket_promo_seen_' . get_current_user_id() ) ) {
return $menu_title;
}
return $menu_title . ' <span class="rocket-promo-bubble">!</span>';
}
/**
* Prevents the notification bubble from showing once the user accessed the dashboard once
*
* @return void
*/
public function dismiss_notification_bubble() {
if ( ! $this->can_use_promo() ) {
return;
}
$user_id = get_current_user_id();
if ( false !== get_transient( "rocket_promo_seen_{$user_id}" ) ) {
return;
}
set_transient( "rocket_promo_seen_{$user_id}", 1, 2 * WEEK_IN_SECONDS );
}
/**
* Displays the promotion banner
*
* @return void
*/
public function display_promo_banner() {
if ( ! $this->can_use_promo() ) {
return;
}
if ( false !== get_transient( 'rocket_promo_banner_' . get_current_user_id() ) ) {
return;
}
$promo = $this->pricing->get_promo_data();
$promo_name = isset( $promo->name ) ? $promo->name : '';
$promo_discount = isset( $promo->discount_percent ) ? $promo->discount_percent : 0;
$data = [
'name' => $promo_name,
'discount_percent' => $promo_discount,
'countdown' => $this->get_countdown_data(),
'message' => $this->get_promo_message( $promo_name, $promo_discount ),
];
echo $this->generate( 'promo-banner', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* AJAX callback to dismiss the promotion banner
*
* @return void
*/
public function dismiss_promo_banner() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
$user = get_current_user_id();
if ( false !== get_transient( "rocket_promo_banner_{$user}" ) ) {
return;
}
set_transient( "rocket_promo_banner_{$user}", 1, 2 * WEEK_IN_SECONDS );
wp_send_json_success();
}
/**
* Adds the promotion end time to WP Rocket localize script data
*
* @since 3.7.4
*
* @param array $data Localize script data.
* @return array
*/
public function add_localize_script_data( $data ) {
if ( ! is_array( $data ) ) {
$data = (array) $data;
}
if ( ! $this->can_use_promo() ) {
return $data;
}
$data['promo_end'] = $this->pricing->get_promo_end();
return $data;
}
/**
* Returns an array containing the remaining days, hours, minutes & seconds for the promotion
*
* @since 3.7.4
*
* @return array
*/
private function get_countdown_data() {
$data = [
'days' => 0,
'hours' => 0,
'minutes' => 0,
'seconds' => 0,
];
if ( rocket_get_constant( 'WP_ROCKET_IS_TESTING', false ) ) {
return $data;
}
$promo_end = $this->pricing->get_promo_end();
if ( 0 === $promo_end ) {
return $data;
}
$now = date_create();
$end = date_timestamp_set( date_create(), $promo_end );
if ( $now > $end ) {
return $data;
}
$remaining = date_diff( $now, $end );
$format = explode( ' ', $remaining->format( '%d %H %i %s' ) );
$data['days'] = $format[0];
$data['hours'] = $format[1];
$data['minutes'] = $format[2];
$data['seconds'] = $format[3];
return $data;
}
/**
* Returns the promotion message to display in the banner
*
* @param string $promo_name Name of the promotion.
* @param int $promo_discount Discount percentage.
*
* @return string
*/
private function get_promo_message( $promo_name = '', $promo_discount = 0 ) {
$choices = 0;
$license = $this->user->get_license_type();
$plus_websites = $this->pricing->get_plus_websites_count();
if ( $license === $plus_websites ) {
$choices = 2;
} elseif (
$license >= $this->pricing->get_single_websites_count()
&&
$license < $plus_websites
) {
$choices = 1;
}
return sprintf(
// translators: %1$s = promotion name, %2$s = <br>, %3$s = <strong>, %4$s = promotion discount percentage, %5$s = </strong>.
_n(
'Take advantage of %1$s to speed up more websites:%2$s get a %3$s%4$s off%5$s for %3$supgrading your license to Plus or Infinite!%5$s',
'Take advantage of %1$s to speed up more websites:%2$s get a %3$s%4$s off%5$s for %3$supgrading your license to Infinite!%5$s',
$choices,
'rocket'
),
$promo_name,
'<br>',
'<strong>',
$promo_discount . '%',
'</strong>'
);
}
/**
* Checks if current user can use the promotion
*
* @since 3.7.4
*
* @return boolean
*/
private function can_use_promo() {
if ( rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' ) ) {
return false;
}
if ( ! $this->can_upgrade() ) {
return false;
}
if ( $this->is_expired_soon() ) {
return false;
}
return $this->pricing->is_promo_active();
}
/**
* Checks if the license expires in less than 30 days
*
* @return boolean
*/
private function is_expired_soon() {
$expiration_delay = $this->user->get_license_expiration() - time();
return 30 * DAY_IN_SECONDS > $expiration_delay;
}
/**
* Checks if the current license can upgrade
*
* @return boolean
*/
private function can_upgrade() {
return (
-1 !== $this->user->get_license_type()
&&
! $this->user->is_license_expired()
);
}
/**
* Gets the upgrade choices depending on the current license level
*
* @return array
*/
private function get_upgrade_choices() {
$choices = [];
$license = $this->user->get_license_type();
$plus_websites = $this->pricing->get_plus_websites_count();
if ( $license === $plus_websites ) {
$choices['infinite'] = $this->get_upgrade_from_plus_to_infinite_data();
} elseif (
$license >= $this->pricing->get_single_websites_count()
&&
$license < $plus_websites
) {
$choices['plus'] = $this->get_upgrade_from_single_to_plus_data();
$choices['infinite'] = $this->get_upgrade_from_single_to_infinite_data();
}
return $choices;
}
/**
* Gets the data to upgrade from single to plus
*
* @return array
*/
private function get_upgrade_from_single_to_plus_data() {
$price = $this->pricing->get_single_to_plus_price();
$data = [
'name' => 'Plus',
'price' => $price,
'websites' => $this->pricing->get_plus_websites_count(),
'upgrade_url' => $this->user->get_upgrade_plus_url(),
];
if ( $this->pricing->is_promo_active() ) {
$regular_price = $this->pricing->get_regular_single_to_plus_price();
$data['saving'] = $regular_price - $price;
$data['regular_price'] = $regular_price;
}
return $data;
}
/**
* Gets the data to upgrade from single to infinite
*
* @return array
*/
private function get_upgrade_from_single_to_infinite_data() {
$price = $this->pricing->get_single_to_infinite_price();
$data = [
'name' => 'Infinite',
'price' => $price,
'websites' => __( 'Unlimited', 'rocket' ),
'upgrade_url' => $this->user->get_upgrade_infinite_url(),
];
if ( $this->pricing->is_promo_active() ) {
$regular_price = $this->pricing->get_regular_single_to_infinite_price();
$data['saving'] = $regular_price - $price;
$data['regular_price'] = $regular_price;
}
return $data;
}
/**
* Gets the data to upgrade from plus to infinite
*
* @return array
*/
private function get_upgrade_from_plus_to_infinite_data() {
$price = $this->pricing->get_plus_to_infinite_price();
$data = [
'name' => 'Infinite',
'price' => $price,
'websites' => __( 'Unlimited', 'rocket' ),
'upgrade_url' => $this->user->get_upgrade_infinite_url(),
];
if ( $this->pricing->is_promo_active() ) {
$regular_price = $this->pricing->get_regular_plus_to_infinite_price();
$data['saving'] = $regular_price - $price;
$data['regular_price'] = $regular_price;
}
return $data;
}
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* Promo banner.
*
* @since 3.7.4
*/
defined( 'ABSPATH' ) || exit;
?>
<div class="rocket-promo-banner" id="rocket-promo-banner">
<div>
<h3 class="rocket-promo-title">
<span class="rocket-promo-discount">
<?php
// translators: %s = promotion discount percentage.
printf( esc_html__( '%s off', 'rocket' ), esc_html( $data['discount_percent'] . '%' ) );
?>
</span>
<?php
// translators: %s = promotion name.
printf( esc_html__( '%s promotion is live!', 'rocket' ), esc_html( $data['name'] ) );
?>
</h3>
<p class="rocket-promo-message"><?php echo wp_kses_post( $data['message'] ); ?></p>
</div>
<div class="rocket-promo-cta-block">
<p class="rocket-promo-deal"><?php esc_html_e( 'Hurry Up! Deal ends in:', 'rocket' ); ?></p>
<ul class="rocket-promo-countdown" id="rocket-promo-countdown">
<li class="rocket-countdown-item"><span class="rocket-countdown-value rocket-countdown-days"><?php echo esc_html( $data['countdown']['days'] ); ?></span> <?php esc_html_e( 'Days', 'rocket' ); ?></li>
<li class="rocket-countdown-item"><span class="rocket-countdown-value rocket-countdown-hours"><?php echo esc_html( $data['countdown']['hours'] ); ?></span> <?php esc_html_e( 'Hours', 'rocket' ); ?></li>
<li class="rocket-countdown-item"><span class="rocket-countdown-value rocket-countdown-minutes"><?php echo esc_html( $data['countdown']['minutes'] ); ?></span> <?php esc_html_e( 'Minutes', 'rocket' ); ?></li>
<li class="rocket-countdown-item"><span class="rocket-countdown-value rocket-countdown-seconds"><?php echo esc_html( $data['countdown']['seconds'] ); ?></span> <?php esc_html_e( 'Seconds', 'rocket' ); ?></li>
</ul>
<button class="rocket-promo-cta wpr-popin-upgrade-toggle"><?php esc_html_e( 'Upgrade now', 'rocket' ); ?></button>
</div>
<button class="wpr-notice-close wpr-icon-close" id="rocket-dismiss-promotion"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'rocket' ); ?></span></button>
</div>

View File

@@ -0,0 +1,40 @@
<?php
/**
* Renewal soon banner.
*
* @since 3.7.5
*/
defined( 'ABSPATH' ) || exit;
?>
<div class="rocket-promo-banner" id="rocket-renewal-banner">
<div class="rocket-expired-message">
<h3 class="rocket-expired-title"><?php esc_html_e( 'Your WP Rocket license is expired!', 'rocket' ); ?></h3>
<p>
<?php
printf(
// translators: %1$s = <strong>, %2$s = </strong>.
esc_html__( 'Your website could be much faster if it could take advantage of our %1$snew features and enhancements.%2$s', 'rocket' ),
'<strong>',
'</strong>'
);
?>
</p>
<p>
<?php
printf(
// translators: %1$s = <strong>, %2$s = discount percentage, %3$s = </strong>, %4$s = discount price.
esc_html__( 'Renew your license for 1 year and get an immediate %1$s%2$s off%3$s on your renewal rate: you will only pay %1$s%4$s%3$s!', 'rocket' ),
'<strong>',
esc_html( $data['discount_percent'] . '%' ),
'</strong>',
esc_html( '$' . $data['discount_price'] )
);
?>
</p>
</div>
<div class="rocket-expired-cta-container">
<a href="<?php echo esc_url( $data['renewal_url'] ); ?>" class="rocket-renew-cta" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Renew now', 'rocket' ); ?></a>
</div>
<button class="wpr-notice-close wpr-icon-close" id="rocket-dismiss-renewal"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'rocket' ); ?></span></button>
</div>

View File

@@ -0,0 +1,44 @@
<?php
/**
* Renewal soon banner.
*
* @since 3.7.5
*/
defined( 'ABSPATH' ) || exit;
?>
<div class="rocket-renewal-banner">
<ul class="rocket-promo-countdown" id="rocket-renew-countdown">
<li class="rocket-countdown-item"><span class="rocket-countdown-value rocket-countdown-days"><?php echo esc_html( $data['countdown']['days'] ); ?></span> <?php esc_html_e( 'Days', 'rocket' ); ?></li>
<li class="rocket-countdown-item"><span class="rocket-countdown-value rocket-countdown-hours"><?php echo esc_html( $data['countdown']['hours'] ); ?></span> <?php esc_html_e( 'Hours', 'rocket' ); ?></li>
<li class="rocket-countdown-item"><span class="rocket-countdown-value rocket-countdown-minutes"><?php echo esc_html( $data['countdown']['minutes'] ); ?></span> <?php esc_html_e( 'Minutes', 'rocket' ); ?></li>
<li class="rocket-countdown-item"><span class="rocket-countdown-value rocket-countdown-seconds"><?php echo esc_html( $data['countdown']['seconds'] ); ?></span> <?php esc_html_e( 'Seconds', 'rocket' ); ?></li>
</ul>
<div class="rocket-renew-message">
<p>
<?php
printf(
// translators: %1$s = <strong>, %2$s = </strong>.
esc_html__( 'Your %1$sWP Rocket license is about to expire.%2$s', 'rocket' ),
'<strong>',
'</strong>'
);
?>
</p>
<p>
<?php
printf(
// translators: %1$s = <strong>, %2$s = discount percentage, %3$s = </strong>, %4$s = discount price.
esc_html__( 'Renew with a %1$s%2$s discount%3$s before it is too late, you will only pay %1$s%4$s%3$s!', 'rocket' ),
'<strong>',
esc_html( $data['discount_percent'] . '%' ),
'</strong>',
esc_html( '$' . $data['discount_price'] )
);
?>
</p>
</div>
<div class="rocket-renew-cta-container">
<a href="<?php echo esc_url( $data['renewal_url'] ); ?>" class="rocket-renew-cta" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Renew now', 'rocket' ); ?></a>
</div>
</div>

View File

@@ -0,0 +1,61 @@
<?php
/**
* Upgrade section template.
*
* @since 3.7.3
*/
defined( 'ABSPATH' ) || exit;
?>
<div class="wpr-Popin wpr-Popin-Upgrade">
<div class="wpr-Popin-header">
<h2 class="wpr-title1"><?php esc_html_e( 'Speed Up More Websites', 'rocket' ); ?></h2>
<button class="wpr-Popin-close wpr-Popin-Upgrade-close wpr-icon-close"></button>
</div>
<div class="wpr-Popin-content">
<p>
<?php
// translators: %1$s = opening strong tag, %2$s = closing strong tag.
printf( esc_html__( 'You can use WP Rocket on more websites by upgrading your license. To upgrade, simply pay the %1$sprice difference%2$s between your current and new licenses, as shown below.', 'rocket' ), '<strong>', '</strong>' );
?>
</p>
<p>
<?php
// translators: %1$s = opening strong tag, %2$s = closing strong tag.
printf( esc_html__( '%1$sN.B.%2$s: Upgrading your license does not change your expiration date', 'rocket' ), '<strong>', '</strong>' );
?>
</p>
<div class="wpr-Popin-flex">
<?php foreach ( $data['upgrades'] as $rocket_upgrade ) : ?>
<div class="wpr-Upgrade-<?php echo esc_attr( $rocket_upgrade['name'] ); ?>">
<?php if ( true === $data['is_promo_active'] ) : ?>
<div class="wpr-upgrade-saving">
<?php
// translators: %s = price.
printf( esc_html__( 'Save $%s', 'rocket' ), esc_html( $rocket_upgrade['saving'] ) );
?>
</div>
<?php endif; ?>
<h3 class="wpr-upgrade-title"><?php echo esc_html( $rocket_upgrade['name'] ); ?></h3>
<div class="wpr-upgrade-prices"><span class="wpr-upgrade-price-symbol">$</span> <?php echo esc_html( $rocket_upgrade['price'] ); ?>
<?php if ( true === $data['is_promo_active'] ) : ?>
<del class="wpr-upgrade-price-regular">$ <?php echo esc_html( $rocket_upgrade['regular_price'] ); ?></del>
<?php endif; ?>
</div>
<div class="wpr-upgrade-websites">
<?php
// translators: %s = number of websites.
printf( esc_html__( '%s websites', 'rocket' ), esc_html( $rocket_upgrade['websites'] ) );
?>
</div>
<a href="<?php echo esc_url( $rocket_upgrade['upgrade_url'] ); ?>" class="wpr-upgrade-link" target="_blank" rel="noopener noreferrer">
<?php
// translators: %s = license name.
printf( esc_html__( 'Upgrade to %s', 'rocket' ), esc_html( $rocket_upgrade['name'] ) );
?>
</a>
</div>
<?php endforeach; ?>
</div>
</div>
</div>

View File

@@ -0,0 +1,12 @@
<?php
/**
* Upgrade section template.
*
* @since 3.7.3
*/
defined( 'ABSPATH' ) || exit;
?>
<p>
<?php esc_html_e( 'You can use WP Rocket on more websites by upgrading your license (you will only pay the price difference between your current and new licenses).', 'rocket' ); ?> <button class="wpr-license-upgrade-button wpr-popin-upgrade-toggle"><?php esc_html_e( 'Upgrade now', 'rocket' ); ?></button>
</p>