* * @copyright (c) 2018, Incsub (http://incsub.com) */ if ( ! defined( 'WPINC' ) ) { die; } /** * Class WP_Smush_Auto_Resize */ class WP_Smush_Auto_Resize extends WP_Smush_Content { /** * Is auto detection enabled. * * @var bool */ private $can_auto_detect = false; /** * WP_Smush_Auto_Resize constructor. */ public function init() { // Set auto resize flag. add_action( 'wp', array( $this, 'init_flags' ) ); // Load js file that is required in public facing pages. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_resize_assets' ) ); // Set a flag to media library images. add_action( 'smush_cdn_image_tag', array( $this, 'skip_image_resize_detection' ) ); // Generate markup for the template engine. add_action( 'wp_footer', array( $this, 'generate_markup' ) ); } /** * Check if auto resize can be performed. * * Allow only if current user is admin and auto resize * detection is enabled in settings. */ public function init_flags() { // Only required for admin users. if ( $this->settings->get( 'detection' ) && current_user_can( 'manage_options' ) ) { $this->can_auto_detect = true; } } /** * Enqueque JS files required in public pages. * * Enque resize detection js and css files to public * facing side of the site. Load only if auto detect * is enabled. * * @return void */ public function enqueue_resize_assets() { // Required only if auto detection is required. if ( ! $this->can_auto_detect ) { return; } // Required scripts for front end. wp_enqueue_script( 'smush-resize-detection', WP_SMUSH_URL . 'app/assets/js/smush-rd.min.js', array( 'jquery' ), WP_SMUSH_VERSION, true ); // Required styles for front end. wp_enqueue_style( 'smush-resize-detection', WP_SMUSH_URL . 'app/assets/css/smush-rd.min.css', array(), WP_SMUSH_VERSION ); // Define ajaxurl var. wp_localize_script( 'smush-resize-detection', 'wp_smush_resize_vars', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'ajax_nonce' => wp_create_nonce( 'smush_resize_nonce' ), // translators: %s - width, %s - height. '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' ), // translators: %s - width, %s - height. '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' ), ) ); } /** * Generate markup for the template engine. * * @since 2.9 */ public function generate_markup() { // Required only if auto detection is required. if ( ! $this->can_auto_detect ) { return; } ?>