@param string $width_height Width/Height attributes in ready state like [height="100" width="100"].
*
* @return string IMG tag after adding attributes otherwise return the input img when error.
*/
private function assign_width_height( string $image, string $width_height ): string {
// Remove old width and height attributes if found.
$changed_image = preg_replace( '/(height|width)=[\'"](?:\S+)*[\'"]\s?/i', '', $image );
$changed_image = preg_replace( '/<\s*img/i', 'filesystem->exists( $image );
}
$file_headers = get_headers( $image );
if ( ! $file_headers ) {
return false;
}
return false !== strstr( $file_headers[0], '200' );
}
/**
* Check if we can specify image dimensions for all images.
*
* @return bool Can we or not.
*/
private function can_specify_dimensions_images(): bool {
/**
* Filter images dimensions attributes process.
*
* @since 2.2
*
* @param bool Do the job or not.
*/
return apply_filters( 'rocket_specify_image_dimensions', false )
||
$this->options->get( 'image_dimensions', false );
}
/**
* Check if we can specify image dimensions for one image.
*
* @param string $image Full img tag.
*
* @return false|string false if we can't specify for this image otherwise get img src attribute.
*/
private function can_specify_dimensions_one_image( string $image ) {
// Don't touch lazy-load file (no conflict with Photon (Jetpack)).
if (
false !== strpos( $image, 'data-lazy-original' )
||
false !== strpos( $image, 'data-no-image-dimensions' )
||
! preg_match( '/\s+src\s*=\s*[\'"](?[^\'"]+)/i', $image, $src_match )
) {
return false;
}
return $src_match['url'];
}
/**
* Get Image sizes.
*
* @param string $image_url Image url to get sizes for.
*
* @return array|false Get image sizes otherwise false.
*/
private function get_image_sizes( string $image_url ) {
if ( $this->is_external_file( $image_url ) ) {
$image_url = $this->normalize_url( $image_url );
if ( ! $this->can_specify_dimensions_external_images() ) {
Logger::debug(
'Specify Image Dimensions failed because you/server disabled specifying dimensions for external images.',
[ 'image_url' => $image_url ]
);
return false;
}
if ( ! $this->image_exists( $image_url, true ) ) {
Logger::debug(
'Specify Image Dimensions failed because external image not found.',
[ 'image_url' => $image_url ]
);
return false;
}
$sizes = $this->getimagesize( $image_url );
if ( ! $sizes ) {
Logger::debug(
'Specify Image Dimensions failed because image is not valid.',
[ 'image_url' => $image_url ]
);
return false;
}
return $sizes;
}
$local_path = $this->get_local_path( $image_url );
if ( ! $this->image_exists( $local_path, false ) ) {
Logger::debug(
'Specify Image Dimensions failed because internal image is not found.',
[ 'image_url' => $image_url ]
);
return false;
}
$sizes = $this->getimagesize( $local_path );
if ( ! $sizes ) {
Logger::debug(
'Specify Image Dimensions failed because image is not valid.',
[ 'image_url' => $image_url ]
);
return false;
}
return $sizes;
}
/**
* Gets image sizes for the given file
*
* @param string $filename File we want to retrieve information about.
*
* @return array|false
*/
private function getimagesize( string $filename ) {
$file = new SplFileInfo( strtok( $filename, '?' ) );
if ( 'svg' === $file->getExtension() ) {
return $this->svg_getimagesize( $filename );
}
return getimagesize( $filename );
}
/**
* Gets image sizes for the given SVG file
*
* Uses the width/height attributes if present, or fallback to viewBox attribute
*
* @param string $filename File we want to retrieve information about.
*
* @return array|false
*/
private function svg_getimagesize( string $filename ) {
$svgfile = simplexml_load_file( rawurlencode( $filename ), 'SimpleXMLElement', rocket_get_constant( 'LIBXML_NOERROR', 32 ) | rocket_get_constant( 'LIBXML_NOWARNING', 64 ) );
if ( ! $svgfile ) {
return false;
}
$width = $this->format_svg_value( (string) $svgfile->attributes()->width );
$height = $this->format_svg_value( (string) $svgfile->attributes()->height );
$size = [];
if (
! empty( $width )
&&
! empty( $height )
) {
$size[0] = $width;
$size[1] = $height;
$size[2] = 0;
$size[3] = 'width="' . absint( $width ) . '" height="' . absint( $height ) . '"';
return $size;
}
$view_box = preg_split( '/[\s,]+/', (string) $svgfile->attributes()->viewBox );
if ( ! empty( $view_box ) ) {
if (
! empty( $view_box[2] )
&&
! empty( $view_box[3] )
) {
$size[0] = $view_box[2];
$size[1] = $view_box[3];
$size[2] = 0;
$size[3] = 'width="' . absint( $size[0] ) . '" height="' . absint( $size[1] ) . '"';
return $size;
}
return false;
}
return false;
}
/**
* Formats the SVG width/height value in case of unusual units
*
* @since 3.10.8
*
* @param string $value The value of the SVG width/height attribute.
*
* @return string
*/
private function format_svg_value( string $value ): string {
// No unit, we can use the value directly.
if ( is_numeric( $value ) ) {
return $value;
}
if ( empty( $value ) ) {
return $value;
}
$px_pattern = '/([0-9]+)\s*px/i';
// If pixel unit, remove the unit and return the numeric value.
if ( preg_match( $px_pattern, $value ) ) {
return preg_replace( $px_pattern, '$1', $value );
}
// Return an empty string for other units.
return '';
}
/**
* Normalize relative url to full url.
*
* @param string $url Url to be normalized.
*
* @return string Normalized url.
*/
private function normalize_url( string $url ): string {
$url_host = wp_parse_url( $url, PHP_URL_HOST );
if ( empty( $url_host ) ) {
$relative_url = ltrim( wp_make_link_relative( $url ), '/' );
$site_url_components = wp_parse_url( site_url( '/' ) );
return $site_url_components['scheme'] . '://' . $site_url_components['host'] . '/' . $relative_url;
}
return $url;
}
}
rn $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 =
, %3$s = , %4$s = promotion discount percentage, %5$s = .
_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,
'
',
'',
$promo_discount . '%',
''
);
}
/**
* 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;
}
if ( $this->is_new_user() ) {
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 User license bought less than 14 days
*
* @return boolean
*/
private function is_new_user() {
return ( 14 * DAY_IN_SECONDS ) > time() - $this->user->get_creation_date();
}
/**
* 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;
}
}