PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 1.2.0
TinyPNG – JPEG, PNG & WebP image compression v1.2.0
3.7.0 3.6.14 trunk 1.0.0 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.4.0 1.5.0 1.6.0 1.7.0 1.7.1 1.7.2 2.0.0 2.0.1 2.0.2 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.0.0 3.0.1 3.1.0 3.2.0 3.2.1 3.3 3.4 3.4.1 3.4.2 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.6.0 3.6.1 3.6.10 3.6.11 3.6.12 3.6.13 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9
tiny-compress-images / src / class-tiny-plugin.php
tiny-compress-images / src Last commit date
config 11 years ago languages 11 years ago scripts 11 years ago styles 11 years ago cacert.pem 11 years ago class-tiny-compress-curl.php 11 years ago class-tiny-compress-fopen.php 11 years ago class-tiny-compress.php 11 years ago class-tiny-compressor-status.php 11 years ago class-tiny-exception.php 11 years ago class-tiny-metadata.php 11 years ago class-tiny-php.php 11 years ago class-tiny-plugin.php 11 years ago class-tiny-settings.php 11 years ago class-tiny-wp-base.php 11 years ago
class-tiny-plugin.php
186 lines
1 <?php
2 /*
3 * Tiny Compress Images - WordPress plugin.
4 * Copyright (C) 2015 Voormedia B.V.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 class Tiny_Plugin extends Tiny_WP_Base {
22 const MEDIA_COLUMN = self::NAME;
23 const MEDIA_COLUMN_HEADER = 'Compression';
24
25 private $settings;
26
27 public static function jpeg_quality() {
28 return 95;
29 }
30
31 public function __construct() {
32 parent::__construct();
33 $this->settings = new Tiny_Settings();
34 }
35
36 public function set_compressor($compressor) {
37 $this->settings->set_compressor($compressor);
38 }
39
40 public function init() {
41 add_filter('jpeg_quality', $this->get_static_method('jpeg_quality'));
42 add_filter('wp_editor_set_quality', $this->get_static_method('jpeg_quality'));
43 add_filter('wp_generate_attachment_metadata', $this->get_method('compress_attachment'), 10, 2);
44 load_plugin_textdomain(self::NAME, false, dirname(plugin_basename(__FILE__)) . '/languages');
45 }
46
47 public function admin_init() {
48 add_filter('manage_media_columns', $this->get_method('add_media_columns'));
49 add_action('manage_media_custom_column', $this->get_method('render_media_column'), 10, 2);
50 add_action('wp_ajax_tiny_compress_image', $this->get_method('compress_image'));
51 add_action('admin_action_tiny_bulk_compress', $this->get_method('bulk_compress'));
52 add_action('admin_enqueue_scripts', $this->get_method('enqueue_scripts'));
53 }
54
55 public function enqueue_scripts($hook) {
56 wp_enqueue_style(self::NAME .'_admin', plugins_url('/styles/admin.css', __FILE__),
57 array(), self::plugin_version());
58
59 $handle = self::NAME .'_admin';
60 wp_register_script($handle, plugins_url('/scripts/admin.js', __FILE__),
61 array(), self::plugin_version(), true);
62
63 wp_localize_script($handle, 'tinyCompressL10n', array(
64 'bulkAction' => self::translate('Compress all uncompressed sizes'),
65 ));
66 wp_enqueue_script($handle);
67 }
68
69 public function compress_attachment($metadata, $attachment_id) {
70 $mime_type = get_post_mime_type($attachment_id);
71 $tiny_metadata = new Tiny_Metadata($attachment_id);
72
73 if ($this->settings->get_compressor() === null || strpos($mime_type, 'image/') !== 0) {
74 return $metadata;
75 }
76
77 $path_info = pathinfo($metadata['file']);
78 $upload_dir = wp_upload_dir();
79 $prefix = $upload_dir['basedir'] . '/' . $path_info['dirname'] . '/';
80
81 $settings = $this->settings->get_sizes();
82
83 if ($settings[Tiny_Metadata::ORIGINAL]['tinify'] && !$tiny_metadata->is_compressed()) {
84 try {
85 $response = $this->settings->get_compressor()->compress_file("$prefix${path_info['basename']}");
86 $tiny_metadata->add_response($response);
87 } catch (Tiny_Exception $e) {
88 $tiny_metadata->add_exception($e);
89 }
90 }
91
92 if (!is_array($metadata['sizes'])) {
93 return $metadata;
94 }
95
96 foreach ($metadata['sizes'] as $size => $info) {
97 if (isset($settings[$size]) && $settings[$size]['tinify'] && !$tiny_metadata->is_compressed($size)) {
98 try {
99 $response = $this->settings->get_compressor()->compress_file("$prefix${info['file']}");
100 $tiny_metadata->add_response($response, $size);
101 } catch (Tiny_Exception $e) {
102 $tiny_metadata->add_exception($e, $size);
103 }
104 }
105 }
106 $tiny_metadata->update();
107
108 return $metadata;
109 }
110
111 public function compress_image() {
112 $id = $_POST['id'];
113 if (!current_user_can('upload_files')) {
114 echo self::translate("You don't have permission to work with uploaded files") . '.';
115 exit();
116 }
117 if (!$id) {
118 echo self::translate("Not a valid media file") . '.';
119 exit();
120 }
121 $metadata = wp_get_attachment_metadata($id);
122 if (!$metadata) {
123 echo self::translate("Could not find metadata of media file") . '.';
124 }
125
126 $this->compress_attachment($metadata, $id);
127 $this->render_media_column(self::MEDIA_COLUMN, $id);
128
129 exit();
130 }
131
132 public function bulk_compress() {
133 check_admin_referer('bulk-media');
134
135 if (empty($_REQUEST['media']) || !is_array( $_REQUEST['media'])) {
136 return;
137 }
138
139 foreach ($_REQUEST['media'] as $id) {
140 $metadata = wp_get_attachment_metadata($id);
141 if ($metadata) {
142 $this->compress_attachment($metadata, $id);
143 }
144 }
145 }
146
147 public function add_media_columns($columns) {
148 $columns[self::MEDIA_COLUMN] = self::translate(self::MEDIA_COLUMN_HEADER);
149 return $columns;
150 }
151
152 public function render_media_column($column, $id) {
153 if ($column === self::MEDIA_COLUMN) {
154 $wp_metadata = wp_get_attachment_metadata($id);
155 $wp_sizes = isset($wp_metadata['sizes']) ? array_keys($wp_metadata['sizes']) : array();
156 $wp_sizes[] = Tiny_Metadata::ORIGINAL;
157
158 $sizes = array_intersect($wp_sizes, $this->settings->get_tinify_sizes());
159
160 $tiny_metadata = new Tiny_Metadata($id);
161 $missing = $tiny_metadata->get_missing_sizes($sizes);
162 $total = count($sizes);
163 $success = $total - count($missing);
164
165 if (count($missing) > 0) {
166 printf(self::translate_escape('Compressed %d out of %d sizes'), $success, $total);
167 echo '<br/>';
168 if (($error = $tiny_metadata->get_latest_error())) {
169 echo '<span class="error">' . self::translate_escape('Latest error') . ': '. self::translate_escape($error) .'<br/>';
170 }
171 echo '<button type="button" class="tiny-compress" data-id="' . $id . '">' .
172 self::translate_escape('Compress') . '</button>';
173 echo '<div class="spinner"></div>';
174 } else {
175 printf(self::translate_escape('Compressed %d out of %d sizes'), $success, $total);
176 $savings = $tiny_metadata->get_savings();
177 if ($savings['count'] > 0) {
178 echo '<br/>';
179 echo self::translate_escape('Total size') . ': ' . size_format($savings['input']) . '<br/>';
180 echo self::translate_escape('Compressed size') . ': ' . size_format($savings['output']);
181 }
182 }
183 }
184 }
185 }
186