array $args Arguments. * @param string $src Image src. * @param string $image Image tag. */ $args = apply_filters( 'smush_image_cdn_args', array(), $image ); } /** * Filter hook to alter image src before going through cdn. * * @param string $src Image src. * @param string $image Image tag. */ $src = apply_filters( 'smush_image_src_before_cdn', $src, $image ); // Generate cdn url from local url. $src = $this->generate_cdn_url( $src, $args ); /** * Filter hook to alter image src after replacing with CDN base. * * @param string $src Image src. * @param string $image Image tag. */ return apply_filters( 'smush_image_src_after_cdn', $src, $image ); } /** * Filters an array of image srcset values, replacing each URL with resized CDN urls. * * Keep the existing srcset sizes if already added by WP, then calculate extra sizes * if required. * * @since 3.0 * * @param array $sources One or more arrays of source data to include in the 'srcset'. * @param array $size_array Array of width and height values in pixels. * @param string $image_src The 'src' of the image. * @param array $image_meta The image metadata as returned by 'wp_get_attachment_metadata()'. * @param int $attachment_id Image attachment ID or 0. * * @return array $sources */ public function update_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id = 0 ) { if ( ! is_array( $sources ) || ! $this->is_supported_path( $image_src ) ) { return $sources; } $main_image_url = false; // Try to get image URL from attachment ID. if ( empty( $attachment_id ) ) { $url = wp_get_attachment_url( $attachment_id ); $main_image_url = $url; } foreach ( $sources as $i => $source ) { if ( ! $this->is_valid_url( $source['url'] ) ) { continue; } if ( apply_filters( 'smush_cdn_skip_image', false, $source['url'], $source ) ) { continue; } list( $width, $height ) = $this->get_size_from_file_name( $source['url'] ); // The file already has a resized version as a thumbnail. if ( 'w' === $source['descriptor'] && $width === (int) $source['value'] ) { $sources[ $i ]['url'] = $this->generate_cdn_url( $source['url'] ); continue; } // If don't have attachment id, get original image by removing dimensions from url. if ( empty( $url ) ) { $url = $this->get_url_without_dimensions( $source['url'] ); } $args = array(); // If we got size from url, add them. if ( ! empty( $width ) && ! empty( $height ) ) { // Set size arg. $args = array( 'size' => "{$width}x{$height}", ); } // Replace with CDN url. $sources[ $i ]['url'] = $this->generate_cdn_url( $url, $args ); } // Set additional sizes if required. if ( $this->settings->get( 'auto_resize' ) ) { $sources = $this->set_additional_srcset( $sources, $size_array, $main_image_url, $image_meta, $image_src ); // Make it look good. ksort( $sources ); } return $sources; } /** * Update image sizes for responsive size. * * @since 3.0 * * @param string $sizes A source size value for use in a 'sizes' attribute. * @param array $size Requested size. * * @return string */ public function update_image_sizes( $sizes, $size ) { if ( ! doing_filter( 'the_content' ) ) { return $sizes; } // Get maximum content width. $content_width = $this->max_content_width(); if ( is_array( $size ) && $size[0] < $content_width ) { return $sizes; } return sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $content_width ); } /** * Add resize arguments to content image src. * * @since 3.0 * * @param array $args Current arguments. * @param object $image Image tag object from DOM. * * @return array $args */ public function update_cdn_image_src_args( $args, $image ) { // Get registered image sizes. $image_sizes = WP_Smush::get_instance()->core()->image_dimensions(); // Find the width and height attributes. $width = false; $height = false; // Try to get the width and height from img tag. if ( preg_match( '/width=["|\']?(\b[[:digit:]]+(?!%)\b)["|\']?/i', $image, $width_string ) ) { $width = $width_string[1]; } if ( preg_match( '/height=["|\']?(\b[[:digit:]]+(?!%)\b)["|\']?/i', $image, $height_string ) ) { $height = $height_string[1]; } $size = array(); // Detect WP registered image size from HTML class. if ( preg_match( '/size-([^"\'\s]+)[^"\']*["|\']?/i', $image, $size ) ) { $size = array_pop( $size ); if ( ! array_key_exists( $size, $image_sizes ) ) { return $args; } // This is probably a correctly sized thumbnail - no need to resize. if ( (int) $width === $image_sizes[ $size ]['width'] || (int) $height === $image_sizes[ $size ]['height'] ) { return $args; } // If this size exists in registered sizes, add argument. if ( 'full' !== $size ) { $args['size'] = (int) $image_sizes[ $size ]['width'] . 'x' . (int) $image_sizes[ $size ]['height']; } } else { // It's not a registered thumbnail size. if ( $width && $height ) { $args['size'] = (int) $width . 'x' . (int) $height; } } return $args; } /** * Process CDN status. * * @since 3.0 * @since 3.1 Moved from Ajax class. * * @param array|WP_Error $status Status in JSON format. * * @return stdClass|WP_Error */ public function process_cdn_status( $status ) { if ( is_wp_error( $status ) ) { return $status; } $status = json_decode( $status['body'] ); // Too many requests. if ( is_null( $status ) ) { return new WP_Error( 'too_many_requests', __( 'Too many requests, please try again in a moment.', 'wp-smushit' ) ); } // Some other error from API. if ( ! $status->success ) { return new WP_Error( $status->data->error_code, $status->data->message ); } return $status->data; } /** * Update CDN stats (daily) cron task or via the get_cdn_stats ajax request. * * @since 3.1.0 */ public function update_stats() { $status = $this->settings->get_setting( 'wp-smush-cdn_status' ); $smush = WP_Smush::get_instance(); if ( isset( $status->cdn_enabling ) && $status->cdn_enabling ) { $new_status = $this->process_cdn_status( $smush->api()->enable() ); } elseif ( wp_doing_cron() ) { // Verify CDN status via Cron. $new_status = $this->process_cdn_status( WP_Smush::get_instance()->api()->check() ); } // For ajax. if ( ! isset( $new_status ) ) { // At this point we already know that $status->data is valid. return wp_send_json_success( $status ); } // Return in case of error. if ( is_wp_error( $new_status ) ) { $code = is_numeric( $new_status->get_error_code() ) ? $new_status->get_error_code() : null; wp_send_json_error( array( 'message' => $new_status->get_error_message(), ), $code ); } $this->settings->set_setting( 'wp-smush-cdn_status', $new_status ); // For ajax. if ( ! wp_doing_cron() ) { // At this point we already know that $status->data is valid. wp_send_json_success( $new_status ); } } /** * Disable CDN stats update cron task. * * @since 3.1.0 */ public static function unschedule_cron() { $timestamp = wp_next_scheduled( 'smush_update_cdn_stats' ); wp_unschedule_event( $timestamp, 'smush_update_cdn_stats' ); } /** * Set cron task to update CDN stats daily. * * @since 3.1.0 */ public function schedule_cron() { if ( ! wp_next_scheduled( 'smush_update_cdn_stats' ) ) { // Schedule first run for next day, as we've already checked just now. wp_schedule_event( time() + DAY_IN_SECONDS, 'daily', 'smush_update_cdn_stats' ); } } /** * Filters the API response. * * Allows modification of the response data after inserting * embedded data (if any) and before echoing the response data. * * @since 3.6.0 * * @param array $response Response data to send to the client. * * @return array */ public function filter_rest_api_response( $response ) { if ( ! $this->settings->get( 'rest_api_support' ) ) { return $response; } if ( ! is_array( $response ) || ! isset( $response['content']['rendered'] ) ) { return $response; } $images = Helpers\Parser::get_links_from_content( $response['content']['rendered'] ); if ( ! isset( $images[0] ) || empty( $images[0] ) ) { return $response; } foreach ( $images[0] as $key => $image ) { $image = $this->is_supported_path( $image ); if ( ! $image ) { continue; } // Replace the data-envira-srcset of the image with CDN link. $image = $this->generate_cdn_url( $image ); if ( $image ) { // Replace the src of the image with CDN link. $response['content']['rendered'] = str_replace( $images[0][ $key ], $image, $response['content']['rendered'] ); } } return $response; } /************************************** * * PRIVATE METHODS * * Functions that are used by the public methods of this CDN class. * * @since 3.0.0: * * @see is_valid_url() * @see get_size_from_file_name() * @see get_url_without_dimensions() * @see max_content_width() * @see set_additional_srcset() * @see generate_srcset() * @see maybe_generate_srcset() * @see is_supported_path() */ /** * Check if we can use the image URL in CDN. * * @since 3.0 * * @param string $url Image URL. * * @return bool */ private function is_valid_url( $url ) { $parsed_url = wp_parse_url( $url ); if ( ! $parsed_url ) { return false; } // No host or path found. if ( ! isset( $parsed_url['host'] ) || ! isset( $parsed_url['path'] ) ) { return false; } // If not supported extension - return false. if ( ! in_array( strtolower( pathinfo( $parsed_url['path'], PATHINFO_EXTENSION ) ), $this->supported_extensions, true ) ) { return false; } return true; } /** * Try to determine height and width from strings WP appends to resized image filenames. * * @since 3.0 * * @param string $src The image URL. * * @return array An array consisting of width and height. */ private function get_size_from_file_name( $src ) { $size = array(); if ( preg_match( '/-(\d+)x(\d+)\.(?:' . implode( '|', $this->supported_extensions ) . ')$/i', $src, $size ) ) { // Get size and width. $width = (int) $size[1]; $height = (int) $size[2]; // Handle retina images. if ( strpos( $src, '@2x' ) ) { $width = 2 * $width; $height = 2 * $height; } // Return width and height as array. if ( $width && $height ) { return array( $width, $height ); } } return array( false, false ); } /** * Get full size image url from resized one. * * @since 3.0 * * @param string $src Image URL. * * @return string */ private function get_url_without_dimensions( $src ) { if ( ! preg_match( '/(-\d+x\d+)\.(' . implode( '|', $this->supported_extensions ) . ')(?:\?.+)?$/i', $src, $src_parts ) ) { return $src; } // Remove WP's resize string to get the original image. $original_src = str_replace( $src_parts[1], '', $src ); // Upload directory. $upload_dir = wp_get_upload_dir(); // Extracts the file path to the image minus the base url. $file_path = substr( $original_src, strlen( $upload_dir['baseurl'] ) ); // Continue only if the file exists. if ( file_exists( $upload_dir['basedir'] . $file_path ) ) { return $original_src; } // Revert to source if file does not exist. return $src; } /** * Get $content_width global var value. * * @since 3.0 * * @return bool|string */ private function max_content_width() { // Get global content width (if content width is empty, set 1900). $content_width = isset( $GLOBALS['content_width'] ) ? (int) $GLOBALS['content_width'] : 1920; // Avoid situations, when themes misuse the global. if ( 0 === $content_width ) { $content_width = 1920; } // Check to see if we are resizing the images (can not go over that value). $resize_sizes = $this->settings->get_setting( 'wp-smush-resize_sizes' ); if ( isset( $resize_sizes['width'] ) && $resize_sizes['width'] < $content_width ) { return $resize_sizes['width']; } return $content_width; } /** * Filters an array of image srcset values, and add additional values. * * @since 3.0 * * @param array $sources An array of image urls and widths. * @param array $size_array Array of width and height values in pixels. * @param string $url Image URL. * @param array $image_meta The image metadata. * @param string $image_src The src of the image. * * @return array $sources */ private function set_additional_srcset( $sources, $size_array, $url, $image_meta, $image_src = '' ) { $content_width = $this->max_content_width(); // If url is empty, try to get from src. if ( empty( $url ) ) { $url = $this->get_url_without_dimensions( $image_src ); } // We need to add additional dimensions. $full_width = $image_meta['width']; $full_height = $image_meta['height']; $current_width = $size_array[0]; $current_height = $size_array[1]; // Get width and height calculated by WP. list( $constrained_width, $constrained_height ) = wp_constrain_dimensions( $full_width, $full_height, $current_width, $current_height ); // Calculate base width. // If $constrained_width sizes are smaller than current size, set maximum content width. if ( abs( $constrained_width - $current_width ) <= 1 && abs( $constrained_height - $current_height ) <= 1 ) { $base_width = $content_width; } else { $base_width = $current_width; } $current_widths = array_keys( $sources ); $new_sources = array(); /** * Filter to add/update/bypass additional srcsets. * * If empty value or false is retured, additional srcset * will not be generated. * * @param array|bool $additional_multipliers Additional multipliers. */ $additional_multipliers = apply_filters( 'smush_srcset_additional_multipliers', array( 0.2, 0.4, 0.6, 0.8, 1, 2, 3, ) ); // Continue only if additional multipliers found or not skipped. // Filter already documented in class-cdn.php. if ( apply_filters( 'smush_skip_image_from_cdn', false, $url, false ) || empty( $additional_multipliers ) ) { return $sources; } // Loop through each multipliers and generate image. foreach ( $additional_multipliers as $multiplier ) { // New width by multiplying with original size. $new_width = (int) ( $base_width * $multiplier ); // In most cases - going over the current width is not recommended and probably not what the user is expecting. if ( $new_width > $current_width ) { continue; } // If a nearly sized image already exist, skip. foreach ( $current_widths as $_width ) { // If +- 50 pixel difference - skip. if ( abs( $_width - $new_width ) < 50 || ( $new_width > $full_width ) ) { continue 2; } } // We need the width as well... $dimensions = wp_constrain_dimensions( $current_width, $current_height, $new_width ); // Arguments for cdn url. $args = array( 'size' => "{$new_width}x{$dimensions[1]}", ); // Add new srcset item. $new_sources[ $new_width ] = array( 'url' => $this->generate_cdn_url( $url, $args ), 'descriptor' => 'w', 'value' => $new_width, ); } // Assign new srcset items to existing ones. if ( ! empty( $new_sources ) ) { // Loop through each items and replace/add. foreach ( $new_sources as $_width_key => $_width_values ) { $sources[ $_width_key ] = $_width_values; } } return $sources; } /** * Try to generate the srcset for the image. * * @since 3.0 * * @param string $src Image source. * * @return array|bool */ private function generate_srcset( $src ) { /** * Try to get the attachment URL. * * TODO: attachment_url_to_postid() can be resource intensive and cause 100% CPU spikes. * * @see https://core.trac.wordpress.org/ticket/41281 */ $attachment_id = attachment_url_to_postid( $src ); $image_meta = array(); $width = 0; $height = 0; // Try to get width and height from image. if ( $attachment_id ) { list( $src, $width, $height ) = wp_get_attachment_image_src( $attachment_id, 'full' ); $image_meta = wp_get_attachment_metadata( $attachment_id ); } // Revolution slider fix: images will always return 0 height and 0 width. if ( $src && ( empty( $width ) || empty( $height ) ) ) { // Try to get the dimensions directly from the file. list( $width, $height ) = $this->get_image_size( $src ); } if ( empty( $width ) || empty( $height ) ) { return false; } // This is an image placeholder - do not generate srcset. if ( $width === $height && 1 === $width ) { return false; } if ( empty( $image_meta ) ) { $image_meta = array( 'width' => $width, 'height' => $height, ); } $size_array = array( absint( $width ), absint( $height ) ); $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ); /** * In some rare cases, the wp_calculate_image_srcset() will not generate any srcset, because there are * not image sizes defined. If that is the case, try to revert to our custom maybe_generate_srcset() to * generate the srcset string. * * Also srcset will not be generated for images that are not part of the media library (no $attachment_id). */ if ( ! $srcset ) { $srcset = $this->maybe_generate_srcset( $width, $height, $src, $image_meta ); } $sizes = $srcset ? wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id ) : false; return array( $srcset, $sizes ); } /** * Try to generate srcset. * * @since 3.0 * * @param int $width Attachment width. * @param int $height Attachment height. * @param string $src Image source. * @param array $meta Image meta. * * @return bool|string */ private function maybe_generate_srcset( $width, $height, $src, $meta ) { $sources[ $width ] = array( 'url' => $this->generate_cdn_url( $src ), 'descriptor' => 'w', 'value' => $width, ); $sources = $this->set_additional_srcset( $sources, array( absint( $width ), absint( $height ) ), $src, $meta ); $srcsets = array(); if ( 1 < count( $sources ) ) { foreach ( $sources as $source ) { $srcsets[] = str_replace( ' ', '%20', $source['url'] ) . ' ' . $source['value'] . $source['descriptor']; } return implode( ',', $srcsets ); } return false; } /** * Check if the image path is supported by the CDN. * * @since 3.0 * @since 3.3.0 Changed access to public. * * @param string $src Image path. * * @return bool|string */ public function is_supported_path( $src ) { // Remove whitespaces. $src = trim( $src ); // No image? Return. if ( empty( $src ) ) { return false; } // Allow only these extensions in CDN. $path = wp_parse_url( $src, PHP_URL_PATH ); // Make sure $path is not null, because passing null to parameter is deprecated in PHP 8.1. if ( empty( $path ) ) { return false; } $ext = strtolower( pathinfo( $path, PATHINFO_EXTENSION ) ); if ( ! in_array( $ext, $this->supported_extensions, true ) ) { return false; } $url_parts = wp_parse_url( $src ); // Unsupported scheme. if ( isset( $url_parts['scheme'] ) && 'http' !== $url_parts['scheme'] && 'https' !== $url_parts['scheme'] ) { return false; } if ( ! isset( $url_parts['scheme'] ) && 0 === strpos( $src, '//' ) ) { $src = is_ssl() ? 'https:' . $src : 'http:' . $src; } // This is a relative path, try to get the URL. if ( ! isset( $url_parts['host'] ) && ! isset( $url_parts['scheme'] ) ) { $src = site_url( $src ); } $mapped_domain = $this->check_mapped_domain(); /** * There are chances for a custom uploads directory using UPLOADS constant. * * But some security plugins (for example, WP Hide & Security Enhance) will allow replacing paths via Nginx/Apache * rules. So for this reason, we don't want the path to be replaced everywhere with the custom UPLOADS constant, * we just want to let the user redefine it here, in the CDN. * * @since 3.4.0 * * @param array $uploads { * Array of information about the upload directory. * * @type string $path Base directory and subdirectory or full path to upload directory. * @type string $url Base URL and subdirectory or absolute URL to upload directory. * @type string $subdir Subdirectory if uploads use year/month folders option is on. * @type string $basedir Path without subdir. * @type string $baseurl URL path without subdir. * @type string|false $error False or error message. * } * * Usage (replace /wp-content/uploads/ with /media/ directory): * * add_filter( * 'smush_cdn_custom_uploads_dir', * function( $uploads ) { * $uploads['baseurl'] = 'https://example.com/media'; * return $uploads; * } * ); */ $uploads = apply_filters( 'smush_cdn_custom_uploads_dir', wp_get_upload_dir() ); // Check if the src is within custom uploads directory. $uploads = ! isset( $uploads['baseurl'] ) || false !== strpos( $src, $uploads['baseurl'] ); if ( ( false === strpos( $src, content_url() ) && ! $uploads ) || ( is_multisite() && $mapped_domain && false === strpos( $src, $mapped_domain ) ) ) { return false; } return $src; } /** * Support for domain mapping plugin. * * @since 3.1.1 */ private function check_mapped_domain() { if ( ! is_multisite() ) { return false; } if ( ! defined( 'DOMAINMAP_BASEFILE' ) ) { return false; } $domain = wp_cache_get( 'smush_mapped_site_domain', 'smush' ); if ( ! $domain ) { global $wpdb; $domain = $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM {$wpdb->base_prefix}domain_mapping WHERE blog_id = %d ORDER BY id LIMIT 1", get_current_blog_id() ) ); // Db call ok. if ( null !== $domain ) { wp_cache_add( 'smush_mapped_site_domain', $domain, 'smush' ); } } return $domain; } /** * Init the page parser. */ private function init_parser() { $background_images = $this->settings->get( 'background_images' ); if ( $background_images ) { $this->parser->enable( 'background_images' ); } $this->parser->enable( $this->slug ); } /** * Try to get the image dimensions from a local file. * * @since 3.4.0 * @param string $url Image URL. * * @return array|false */ private function get_image_size( $url ) { if ( $this->site_url !== $this->home_url ) { $url = str_replace( $this->site_url, $this->home_url, $url ); } $path = wp_make_link_relative( $url ); $path = wp_normalize_path( ABSPATH . $path ); if ( ! file_exists( $path ) ) { return false; } return wp_getimagesize( $path ); } }