public function test_compress_when_file_changed()
 {
     $this->wp->stub('get_post_mime_type', create_function('$i', 'return "image/png";'));
     $testmeta = $this->wp->getTestMetadata();
     $tiny_image = new Tiny_Image(new Tiny_Settings(), 1, $testmeta);
     $tiny_image->get_image_size()->add_tiny_meta_start();
     $tiny_image->get_image_size()->add_tiny_meta(self::success_compress('vfs://root/wp-content/uploads/14/01/test.png'));
     $tiny_image->get_image_size('large')->add_tiny_meta_start();
     $tiny_image->get_image_size('large')->add_tiny_meta(self::success_compress('vfs://root/wp-content/uploads/14/01/test-large.png'));
     $tiny_image->get_image_size('post-thumbnail')->add_tiny_meta_start();
     $tiny_image->get_image_size('post-thumbnail')->add_tiny_meta(self::success_compress('vfs://root/wp-content/uploads/14/01/test-post-thumbnail.png'));
     $tiny_image->update_tiny_post_meta();
     $this->vfs->getChild('wp-content/uploads/14/01/test-large.png')->truncate(100000);
     $this->compressor->expects($this->once())->method('compress_file')->withConsecutive(array($this->equalTo('vfs://root/wp-content/uploads/14/01/test-large.png')))->will($this->returnCallback(array($this, 'success_compress')));
     $this->subject->compress_on_upload($testmeta, 1);
 }
 public function set_up()
 {
     parent::set_up();
     $this->wp->createImagesFromJSON($this->json('image_filesystem_data'));
     $this->wp->setTinyMetadata(1, $this->json('image_database_metadata'));
     $tiny_image = new Tiny_Image(new Tiny_Settings(), 1, $this->json('_wp_attachment_metadata'));
     $this->original = $tiny_image->get_image_size();
     $this->thumbnail = $tiny_image->get_image_size('thumbnail');
     $this->small = $tiny_image->get_image_size('small');
     $this->medium = $tiny_image->get_image_size('medium');
     $this->large = $tiny_image->get_image_size('large');
 }
 public function render_bulk_optimization_page()
 {
     $stats = Tiny_Image::get_optimization_statistics($this->settings);
     $estimated_costs = Tiny_Compress::estimate_cost($stats['available-unoptimised-sizes'], $this->settings->get_compression_count());
     $admin_colors = self::retrieve_admin_colors();
     $active_tinify_sizes = $this->settings->get_active_tinify_sizes();
     $auto_start_bulk = isset($_REQUEST['ids']);
     include dirname(__FILE__) . '/views/bulk-optimization.php';
 }
 private function render_size_checkbox($size, $option)
 {
     $id = self::get_prefixed_name("sizes_{$size}");
     $name = self::get_prefixed_name('sizes[' . $size . ']');
     $checked = $option['tinify'] ? ' checked="checked"' : '';
     if (Tiny_Image::is_original($size)) {
         $label = esc_html__('Original image', 'tiny-compress-images') . ' (' . esc_html__('overwritten by compressed image', 'tiny-compress-images') . ')';
     } else {
         if (Tiny_Image::is_retina($size)) {
             $label = esc_html__('WP Retina 2x sizes', 'tiny-compress-images');
         } else {
             $label = esc_html__(ucfirst($size)) . ' - ' . $option['width'] . 'x' . $option['height'];
         }
     }
     echo '<p>';
     echo '<input type="checkbox" id="' . $id . '" name="' . $name . '" value="on" ' . $checked . '/>';
     echo '<label for="' . $id . '">' . $label . '</label>';
     echo '</p>';
 }
 }
 echo '</td>';
 if ($size->is_duplicate()) {
     echo '<td>-</td>';
     printf('<td colspan=2><em>' . esc_html__('Same file as "%s"', 'tiny-compress-images') . '</em></td>', esc_html__(ucfirst($size->duplicate_of_size())));
 } else {
     if ($size->has_been_compressed()) {
         echo '<td>' . size_format($size->meta['input']['size'], 1) . '</td>';
         echo '<td>' . size_format($size->meta['output']['size'], 1) . '</td>';
         echo '<td>' . sprintf(esc_html__('%s ago'), human_time_diff($size->end_time($size_name))) . '</td>';
     } else {
         if (!$size->exists()) {
             echo '<td>-</td>';
             echo '<td colspan=2><em>' . esc_html__('Not present', 'tiny-compress-images') . '</em></td>';
         } else {
             if (isset($size_active[$size_name]) || Tiny_Image::is_retina($size_name)) {
                 echo '<td>' . size_format($size->filesize(), 1) . '</td>';
                 echo '<td colspan=2><em>' . esc_html__('Not compressed', 'tiny-compress-images') . '</em></td>';
             } else {
                 if (isset($size_exists[$size_name])) {
                     echo '<td>' . size_format($size->filesize(), 1) . '</td>';
                     echo '<td colspan=2><em>' . esc_html__('Not configured to be compressed', 'tiny-compress-images') . '</em></td>';
                 } else {
                     if (!array_key_exists($size_name, $active_sizes)) {
                         echo '<td>' . size_format($size->filesize(), 1) . '</td>';
                         echo '<td colspan=2><em>' . esc_html__('Size is not in use', 'tiny-compress-images') . '</em></td>';
                     } else {
                         echo '<td>' . size_format($size->filesize(), 1) . '</td>';
                         echo '<td>-</td>';
                     }
                 }
 public function test_is_retina_for_non_retina_size_with_short_name()
 {
     $this->assertEquals(false, Tiny_Image::is_retina('file'));
 }
 public static function get_optimization_statistics($settings, $result = null)
 {
     global $wpdb;
     if (is_null($result)) {
         // Select posts that have "_wp_attachment_metadata" image metadata
         // and optionally contain "tiny_compress_images" metadata.
         $query = "SELECT\n\t\t\t\t\t{$wpdb->posts}.ID,\n\t\t\t\t\t{$wpdb->posts}.post_title,\n\t\t\t\t\t{$wpdb->postmeta}.meta_value,\n\t\t\t\t\twp_postmeta_file.meta_value AS unique_attachment_name,\n\t\t\t\t\twp_postmeta_tiny.meta_value AS tiny_meta_value\n\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\tLEFT JOIN {$wpdb->postmeta}\n\t\t\t\t\tON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id\n\t\t\t\tLEFT JOIN {$wpdb->postmeta} AS wp_postmeta_file\n\t\t\t\t\tON {$wpdb->posts}.ID = wp_postmeta_file.post_id\n\t\t\t\t\t\tAND wp_postmeta_file.meta_key = '_wp_attached_file'\n\t\t\t\tLEFT JOIN {$wpdb->postmeta} AS wp_postmeta_tiny\n\t\t\t\t\tON {$wpdb->posts}.ID = wp_postmeta_tiny.post_id\n\t\t\t\t\t\tAND wp_postmeta_tiny.meta_key = '" . self::META_KEY . "'\n\t\t\t\tWHERE {$wpdb->posts}.post_type = 'attachment'\n\t\t\t\t\tAND (\n\t\t\t\t\t\t{$wpdb->posts}.post_mime_type = 'image/jpeg' OR\n\t\t\t\t\t\t{$wpdb->posts}.post_mime_type = 'image/png'\n\t\t\t\t\t)\n\t\t\t\t\tAND {$wpdb->postmeta}.meta_key = '_wp_attachment_metadata'\n\t\t\t\tGROUP BY unique_attachment_name\n\t\t\t\tORDER BY ID DESC";
         $result = $wpdb->get_results($query, ARRAY_A);
     }
     $stats = array();
     $stats['uploaded-images'] = 0;
     $stats['optimized-image-sizes'] = 0;
     $stats['available-unoptimised-sizes'] = 0;
     $stats['optimized-library-size'] = 0;
     $stats['unoptimized-library-size'] = 0;
     $stats['available-for-optimization'] = array();
     for ($i = 0; $i < sizeof($result); $i++) {
         $wp_metadata = unserialize($result[$i]['meta_value']);
         $tiny_metadata = unserialize($result[$i]['tiny_meta_value']);
         if (!is_array($tiny_metadata)) {
             $tiny_metadata = array();
         }
         $tiny_image = new Tiny_Image($settings, $result[$i]['ID'], $wp_metadata, $tiny_metadata);
         $image_stats = $tiny_image->get_statistics();
         $stats['uploaded-images']++;
         $stats['available-unoptimised-sizes'] += $image_stats['available_unoptimized_sizes'];
         $stats['optimized-image-sizes'] += $image_stats['image_sizes_optimized'];
         $stats['optimized-library-size'] += $image_stats['optimized_total_size'];
         $stats['unoptimized-library-size'] += $image_stats['initial_total_size'];
         if ($image_stats['available_unoptimized_sizes'] > 0) {
             $stats['available-for-optimization'][] = array('ID' => $result[$i]['ID'], 'post_title' => $result[$i]['post_title']);
         }
     }
     return $stats;
 }