=> '', 'count_total' => '', 'count_images' => '', 'unsmushed' => '', 'resmush' => '', 'savings_bytes' => '', 'savings_resize' => '', 'savings_conversion' => '', 'savings_supersmush' => '', 'pro_savings' => '', ); } // Check if scanner class is available. $scanner_ready = isset( $this->mod->dir->scanner ); $data['dir_smush'] = array( 'currentScanStep' => $scanner_ready ? $this->mod->dir->scanner->get_current_scan_step() : 0, 'totalSteps' => $scanner_ready ? $this->mod->dir->scanner->get_scan_steps() : 0, ); $data['resize_sizes'] = $this->get_max_image_dimensions(); // Convert it into ms. $data['timeout'] = WP_SMUSH_TIMEOUT * 1000; wp_localize_script( $handle, 'wp_smushit_data', $data ); } /** * Check bulk sent count, whether to allow further smushing or not * * @param bool $reset To hard reset the transient. * @param string $key Transient Key - bulk_sent_count/dir_sent_count. * * @return bool */ public static function check_bulk_limit( $reset = false, $key = 'bulk_sent_count' ) { $transient_name = WP_SMUSH_PREFIX . $key; // If we JUST need to reset the transient. if ( $reset ) { set_transient( $transient_name, 0, 60 ); return; } $bulk_sent_count = (int) get_transient( $transient_name ); // Check if bulk smush limit is less than limit. if ( ! $bulk_sent_count || $bulk_sent_count < self::$max_free_bulk ) { $continue = true; } elseif ( $bulk_sent_count === self::$max_free_bulk ) { // If user has reached the limit, reset the transient. $continue = false; $reset = true; } else { $continue = false; } // If we need to reset the transient. if ( $reset ) { set_transient( $transient_name, 0, 60 ); } return $continue; } /** * Get registered image sizes with dimension * * @return array */ public function image_dimensions() { // Get from cache if available to avoid duplicate looping. $sizes = wp_cache_get( 'get_image_sizes', 'smush_image_sizes' ); if ( $sizes ) { return $sizes; } global $_wp_additional_image_sizes; $additional_sizes = get_intermediate_image_sizes(); $sizes = array(); if ( empty( $additional_sizes ) ) { return $sizes; } // Create the full array with sizes and crop info. foreach ( $additional_sizes as $_size ) { if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ), true ) ) { $sizes[ $_size ]['width'] = get_option( $_size . '_size_w' ); $sizes[ $_size ]['height'] = get_option( $_size . '_size_h' ); $sizes[ $_size ]['crop'] = (bool) get_option( $_size . '_crop' ); } elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) { $sizes[ $_size ] = array( 'width' => $_wp_additional_image_sizes[ $_size ]['width'], 'height' => $_wp_additional_image_sizes[ $_size ]['height'], 'crop' => $_wp_additional_image_sizes[ $_size ]['crop'], ); } } // Medium Large. if ( ! isset( $sizes['medium_large'] ) || empty( $sizes['medium_large'] ) ) { $width = (int) get_option( 'medium_large_size_w' ); $height = (int) get_option( 'medium_large_size_h' ); $sizes['medium_large'] = array( 'width' => $width, 'height' => $height, ); } // Set cache to avoid this loop next time. wp_cache_set( 'get_image_sizes', $sizes, 'smush_image_sizes' ); return $sizes; } /** * Get the Maximum Width and Height settings for WrodPress * * @return array, Array of Max. Width and Height for image. */ public function get_max_image_dimensions() { global $_wp_additional_image_sizes; $width = 0; $height = 0; $limit = 9999; // Post-thumbnail. $image_sizes = get_intermediate_image_sizes(); // If image sizes are filtered and no image size list is returned. if ( empty( $image_sizes ) ) { return array( 'width' => $width, 'height' => $height, ); } // Create the full array with sizes and crop info. foreach ( $image_sizes as $size ) { if ( in_array( $size, array( 'thumbnail', 'medium', 'medium_large', 'large' ), true ) ) { $size_width = get_option( "{$size}_size_w" ); $size_height = get_option( "{$size}_size_h" ); } elseif ( isset( $_wp_additional_image_sizes[ $size ] ) ) { $size_width = $_wp_additional_image_sizes[ $size ]['width']; $size_height = $_wp_additional_image_sizes[ $size ]['height']; } // Skip if no width and height. if ( ! isset( $size_width, $size_height ) ) { continue; } // If within te limit, check for a max value. if ( $size_width <= $limit ) { $width = max( $width, $size_width ); } if ( $size_height <= $limit ) { $height = max( $height, $size_height ); } } return array( 'width' => $width, 'height' => $height, ); } /** * Update the image smushed count in transient * * @param string $key Database key. */ public static function update_smush_count( $key = 'bulk_sent_count' ) { $transient_name = WP_SMUSH_PREFIX . $key; $bulk_sent_count = get_transient( $transient_name ); // If bulk sent count is not set. if ( false === $bulk_sent_count ) { // Start transient at 0. set_transient( $transient_name, 1, 200 ); } elseif ( $bulk_sent_count < self::$max_free_bulk ) { // If lte $this->max_free_bulk images are sent, increment. set_transient( $transient_name, $bulk_sent_count + 1, 200 ); } } /** * Set the big image threshold. * * @since 3.3.2 * * @param int $threshold The threshold value in pixels. Default 2560. * @param array $imagesize Indexed array of the image width and height (in that order). * @param string $file Full path to the uploaded image file. * @param int $attachment_id Attachment post ID. * * @return int New threshold. */ public function big_image_size_threshold( $threshold, $imagesize, $file, $attachment_id ) { if ( ! Settings::get_instance()->get( 'resize' ) ) { return $threshold; } $resize_sizes = Settings::get_instance()->get_setting( WP_SMUSH_PREFIX . 'resize_sizes' ); if ( ! $resize_sizes || ! is_array( $resize_sizes ) ) { return $threshold; } return $resize_sizes['width'] > $resize_sizes['height'] ? $resize_sizes['width'] : $resize_sizes['height']; } }