private function compress($metadata, $attachment_id)
 {
     $mime_type = get_post_mime_type($attachment_id);
     $tiny_metadata = new Tiny_Metadata($attachment_id, $metadata);
     if ($this->settings->get_compressor() === null || strpos($mime_type, 'image/') !== 0) {
         return $metadata;
     }
     $success = 0;
     $failed = 0;
     $compressor = $this->settings->get_compressor();
     $sizes = $this->settings->get_tinify_sizes();
     $missing = $tiny_metadata->get_missing_sizes($sizes);
     foreach ($missing as $size) {
         try {
             $tiny_metadata->add_request($size);
             $tiny_metadata->update();
             $response = $compressor->compress_file($tiny_metadata->get_filename($size));
             $tiny_metadata->add_response($response, $size);
             $success++;
         } catch (Tiny_Exception $e) {
             $tiny_metadata->add_exception($e, $size);
             $failed++;
         }
     }
     $tiny_metadata->update();
     return array($tiny_metadata, array('success' => $success, 'failed' => $failed));
 }
示例#2
0
 public function testCompressShouldNotCompressTwice()
 {
     $this->wp->stub('get_post_mime_type', create_function('$i', 'return "image/png";'));
     $meta = new Tiny_Metadata(1);
     $meta->add_response(compressTestFile('test.png'));
     $meta->add_response(compressTestFile('test-large.png'), 'large');
     $meta->update();
     $this->compressor->expects($this->once())->method('compress_file')->withConsecutive(array($this->equalTo('/root/wp-content/upload/14/01/test-post-thumbnail.png')))->will($this->returnCallback('compressTestFile'));
     $this->subject->compress_attachment(getTestMetadata(), 1);
 }
示例#3
0
 public function testCompressWhenFileChanged()
 {
     $this->wp->stub('get_post_mime_type', create_function('$i', 'return "image/png";'));
     $testmeta = $this->wp->getTestMetadata();
     $meta = new Tiny_Metadata(1, $testmeta);
     $meta->add_request();
     $meta->add_response(self::successCompress('vfs://root/wp-content/uploads/14/01/test.png'));
     $meta->add_request('large');
     $meta->add_response(self::successCompress('vfs://root/wp-content/uploads/14/01/test-large.png'), 'large');
     $meta->add_request('post-thumbnail');
     $meta->add_response(self::successCompress('vfs://root/wp-content/uploads/14/01/test-post-thumbnail.png'), 'post-thumbnail');
     $meta->update();
     $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, 'successCompress')));
     $this->subject->compress_attachment($testmeta, 1);
 }
示例#4
0
 public function render_total_savings()
 {
     global $wpdb;
     $total_savings = 0;
     $result = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID DESC", ARRAY_A);
     for ($i = 0; $i < sizeof($result); $i++) {
         $tiny_metadata = new Tiny_Metadata($result[$i]["ID"]);
         $savings = $tiny_metadata->get_savings();
         $total_savings += $savings['input'] - $savings['output'];
     }
     echo '<p>';
     if ($total_savings > 0) {
         printf(self::translate_escape("You have saved a total of %s on images") . '!', '<strong>' . size_format($total_savings) . '</strong>');
     } else {
         $link = '<a href="upload.php?page=tiny-bulk-compress">' . self::translate_escape('Compress All Images') . '</a>';
         printf(self::translate_escape('No images compressed yet. Use %s to compress existing images') . '.', $link);
     }
     echo '</p>';
 }
示例#5
0
 public function testGetInProgressSizesShouldReturnSizeBeingCompressed()
 {
     $meta = array(Tiny_Metadata::META_KEY => array("thumbnail" => array("start" => 1447925134, "input" => array("size" => 46480))));
     $this->wp->setMetadata(5, $meta);
     $tiny_meta = new Tiny_Metadata(5, $this->json("wp_meta_default_sizes"));
     $this->assertEquals(array("thumbnail"), $tiny_meta->get_in_progress_sizes());
 }
示例#6
0
 public function render_media_column($column, $id)
 {
     if ($column === self::MEDIA_COLUMN) {
         $wp_metadata = wp_get_attachment_metadata($id);
         $wp_sizes = isset($wp_metadata['sizes']) ? array_keys($wp_metadata['sizes']) : array();
         $sizes = array_intersect($wp_sizes, $this->settings->get_tinify_sizes());
         $tiny_metadata = new Tiny_Metadata($id);
         $missing = $tiny_metadata->get_missing_sizes($sizes);
         $total = count($sizes);
         $success = $total - count($missing);
         if (count($missing) > 0) {
             printf(self::translate_escape('Compressed %d out of %d sizes'), $success, $total);
             echo '<br/>';
             if ($error = $tiny_metadata->get_latest_error()) {
                 echo '<span class="error">' . self::translate_escape('Latest error') . ': ' . self::translate_escape($error) . '<br/>';
             }
             echo '<button type="button" class="tinypng-compress" data-id="' . $id . '">' . self::translate_escape('Compress') . '</button>';
             echo '<div class="spinner"></div>';
         } else {
             $details = $tiny_metadata->get_value();
             printf(self::translate_escape('Compressed %d out of %d sizes'), $success, $total);
             echo '<br/>';
             echo self::translate_escape('Original size') . ': ' . size_format($details['input']['size']) . '<br/>';
             echo self::translate_escape('Compressed size') . ': ' . size_format($details['output']['size']);
         }
     }
 }