async
2 years ago
background
2 years ago
bulk
2 years ago
helpers
2 years ago
class-abstract-module.php
2 years ago
class-backup.php
2 years ago
class-cdn.php
2 years ago
class-dir.php
2 years ago
class-lazy.php
2 years ago
class-png2jpg.php
2 years ago
class-product-analytics.php
2 years ago
class-resize-detection.php
2 years ago
class-resize.php
2 years ago
class-smush.php
2 years ago
class-webp.php
2 years ago
class-resize-detection.php
171 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Auto resize functionality: Resize_Detection class |
| 4 | * |
| 5 | * @package Smush\Core\Modules |
| 6 | * @version 2.8.0 |
| 7 | * |
| 8 | * @author Joel James <joel@incsub.com> |
| 9 | * |
| 10 | * @copyright (c) 2018, Incsub (http://incsub.com) |
| 11 | */ |
| 12 | |
| 13 | namespace Smush\Core\Modules; |
| 14 | |
| 15 | if ( ! defined( 'WPINC' ) ) { |
| 16 | die; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Class Resize_Detection |
| 21 | */ |
| 22 | class Resize_Detection extends Abstract_Module { |
| 23 | |
| 24 | /** |
| 25 | * Is auto detection enabled. |
| 26 | * |
| 27 | * @var bool |
| 28 | */ |
| 29 | private $can_auto_detect = false; |
| 30 | |
| 31 | /** |
| 32 | * Resize_Detection constructor. |
| 33 | */ |
| 34 | public function init() { |
| 35 | // Set auto resize flag. |
| 36 | add_action( 'wp', array( $this, 'init_flags' ) ); |
| 37 | |
| 38 | // Load js file that is required in public facing pages. |
| 39 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_resize_assets' ) ); |
| 40 | |
| 41 | // Set a flag to media library images. |
| 42 | add_filter( 'smush_cdn_image_tag', array( $this, 'skip_image_resize_detection' ) ); |
| 43 | |
| 44 | // Generate markup for the template engine. |
| 45 | add_action( 'wp_footer', array( $this, 'generate_markup' ) ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Check if auto resize can be performed. |
| 50 | * |
| 51 | * Allow only if current user is admin and auto resize |
| 52 | * detection is enabled in settings. |
| 53 | */ |
| 54 | public function init_flags() { |
| 55 | // Only required for admin users. |
| 56 | if ( $this->settings->get( 'detection' ) && current_user_can( 'manage_options' ) ) { |
| 57 | $this->can_auto_detect = true; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Enqueue JS files required in public pages. |
| 63 | * |
| 64 | * Enqueue resize detection js and css files to public |
| 65 | * facing side of the site. Load only if auto detect |
| 66 | * is enabled. |
| 67 | * |
| 68 | * @return void |
| 69 | */ |
| 70 | public function enqueue_resize_assets() { |
| 71 | // Required only if auto detection is required. |
| 72 | if ( ! $this->can_auto_detect ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // Required scripts for front end. |
| 77 | wp_enqueue_script( |
| 78 | 'smush-resize-detection', |
| 79 | WP_SMUSH_URL . 'app/assets/js/smush-rd.min.js', |
| 80 | array( 'jquery' ), |
| 81 | WP_SMUSH_VERSION, |
| 82 | true |
| 83 | ); |
| 84 | |
| 85 | // Required styles for front end. |
| 86 | wp_enqueue_style( |
| 87 | 'smush-resize-detection', |
| 88 | WP_SMUSH_URL . 'app/assets/css/smush-rd.min.css', |
| 89 | array(), |
| 90 | WP_SMUSH_VERSION |
| 91 | ); |
| 92 | |
| 93 | // Define ajaxurl var. |
| 94 | wp_localize_script( |
| 95 | 'smush-resize-detection', |
| 96 | 'wp_smush_resize_vars', |
| 97 | array( |
| 98 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 99 | 'ajax_nonce' => wp_create_nonce( 'smush_resize_nonce' ), |
| 100 | // translators: %s - width, %s - height. |
| 101 | 'large_image' => sprintf( __( 'This image is too large for its container. Adjust the image dimensions to %1$s x %2$spx for optimal results.', 'wp-smushit' ), 'width', 'height' ), |
| 102 | // translators: %s - width, %s - height. |
| 103 | 'small_image' => sprintf( __( 'This image is too small for its container. Adjust the image dimensions to %1$s x %2$spx for optimal results.', 'wp-smushit' ), 'width', 'height' ), |
| 104 | ) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Generate markup for the template engine. |
| 110 | * |
| 111 | * @since 2.9 |
| 112 | */ |
| 113 | public function generate_markup() { |
| 114 | // Required only if auto detection is required. |
| 115 | if ( ! $this->can_auto_detect ) { |
| 116 | return; |
| 117 | } |
| 118 | ?> |
| 119 | <div id="smush-image-bar-toggle" class="closed"> |
| 120 | <i class="sui-icon-loader" aria-hidden="true"> </i> |
| 121 | </div> |
| 122 | <div id="smush-image-bar" class="closed"> |
| 123 | <h3><?php esc_html_e( 'Image Issues', 'wp-smushit' ); ?></h3> |
| 124 | <p> |
| 125 | <?php esc_html_e( 'The images listed below are being resized to fit a container. To avoid serving oversized or blurry images, try to match the images to their container sizes.', 'wp-smushit' ); ?> |
| 126 | </p> |
| 127 | |
| 128 | <div id="smush-image-bar-items-bigger"> |
| 129 | <strong><?php esc_html_e( 'Oversized', 'wp-smushit' ); ?></strong> |
| 130 | </div> |
| 131 | <div id="smush-image-bar-items-smaller"> |
| 132 | <strong><?php esc_html_e( 'Undersized', 'wp-smushit' ); ?></strong> |
| 133 | </div> |
| 134 | |
| 135 | <div id="smush-image-bar-notice"> |
| 136 | <p><?php esc_html_e( 'All images are properly sized', 'wp-smushit' ); ?></p> |
| 137 | </div> |
| 138 | <p id="smush-image-bar-notice-desc"> |
| 139 | <?php esc_html_e( 'Note: It’s not always easy to make this happen, fix up what you can.', 'wp-smushit' ); ?> |
| 140 | </p> |
| 141 | </div> |
| 142 | <?php |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Exclude images that are hosted on CDN and have auto resize enabled. |
| 147 | * |
| 148 | * @since 3.2.0 |
| 149 | * |
| 150 | * @param string $image Image tag. |
| 151 | * |
| 152 | * @return string |
| 153 | */ |
| 154 | public function skip_image_resize_detection( $image ) { |
| 155 | // No need to add attachment id if auto detection is not enabled. |
| 156 | if ( ! $this->can_auto_detect ) { |
| 157 | return $image; |
| 158 | } |
| 159 | |
| 160 | // CDN with auto resize need to be enabled. |
| 161 | if ( ! $this->settings->get( 'cdn' ) || ! $this->settings->get( 'auto_resize' ) ) { |
| 162 | return $image; |
| 163 | } |
| 164 | |
| 165 | Helpers\Parser::add_attribute( $image, 'no-resize-detection' ); |
| 166 | |
| 167 | return $image; |
| 168 | } |
| 169 | |
| 170 | } |
| 171 |