PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 1.1.0
TinyPNG – JPEG, PNG & WebP image compression v1.1.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-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
188 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 private $compressor;
27
28 public static function jpeg_quality() {
29 return 95;
30 }
31
32 public function __construct() {
33 parent::__construct();
34 $this->settings = new Tiny_Settings();
35 try {
36 $this->compressor = Tiny_Compress::get_compressor($this->settings->get_api_key());
37 } catch (Tiny_Exception $e) {
38 $this->add_admin_notice(self::translate_escape($e->getMessage()));
39 }
40 }
41
42 public function set_compressor($compressor) {
43 $this->compressor = $compressor;
44 }
45
46 public function init() {
47 add_filter('jpeg_quality', $this->get_static_method('jpeg_quality'));
48 add_filter('wp_editor_set_quality', $this->get_static_method('jpeg_quality'));
49 add_filter('wp_generate_attachment_metadata', $this->get_method('compress_attachment'), 10, 2);
50 load_plugin_textdomain(self::NAME, false, dirname(plugin_basename(__FILE__)) . '/languages');
51 }
52
53 public function admin_init() {
54 add_filter('manage_media_columns', $this->get_method('add_media_columns'));
55 add_action('manage_media_custom_column', $this->get_method('render_media_column'), 10, 2);
56 add_action('wp_ajax_tiny_compress_image', $this->get_method('compress_image'));
57 add_action('admin_action_tiny_bulk_compress', $this->get_method('bulk_compress'));
58 add_action('admin_enqueue_scripts', $this->get_method('enqueue_scripts'));
59 }
60
61 public function enqueue_scripts($hook) {
62 wp_enqueue_style(self::NAME .'_admin', plugins_url('/styles/admin.css', __FILE__),
63 array(), self::plugin_version());
64
65 $handle = self::NAME .'_admin';
66 wp_register_script($handle, plugins_url('/scripts/admin.js', __FILE__),
67 array(), self::plugin_version(), true);
68
69 wp_localize_script($handle, 'tinyCompressL10n', array(
70 'bulkAction' => self::translate('Compress all uncompressed sizes'),
71 ));
72 wp_enqueue_script($handle);
73 }
74
75 public function compress_attachment($metadata, $attachment_id) {
76 $mime_type = get_post_mime_type($attachment_id);
77 $tiny_metadata = new Tiny_Metadata($attachment_id);
78
79 if ($this->compressor === null || strpos($mime_type, 'image/') !== 0) {
80 return $metadata;
81 }
82
83 $path_info = pathinfo($metadata['file']);
84 $upload_dir = wp_upload_dir();
85 $prefix = $upload_dir['basedir'] . '/' . $path_info['dirname'] . '/';
86
87 $settings = $this->settings->get_sizes();
88
89 if ($settings[Tiny_Metadata::ORIGINAL]['tinify'] && !$tiny_metadata->is_compressed()) {
90 try {
91 $response = $this->compressor->compress_file("$prefix${path_info['basename']}");
92 $tiny_metadata->add_response($response);
93 } catch (Tiny_Exception $e) {
94 $tiny_metadata->add_exception($e);
95 }
96 }
97
98 foreach ($metadata['sizes'] as $size => $info) {
99 if (isset($settings[$size]) && $settings[$size]['tinify'] && !$tiny_metadata->is_compressed($size)) {
100 try {
101 $response = $this->compressor->compress_file("$prefix${info['file']}");
102 $tiny_metadata->add_response($response, $size);
103 } catch (Tiny_Exception $e) {
104 $tiny_metadata->add_exception($e, $size);
105 }
106 }
107 }
108 $tiny_metadata->update();
109
110 return $metadata;
111 }
112
113 public function compress_image() {
114 $id = $_POST['id'];
115 if (!current_user_can('upload_files')) {
116 echo self::translate("You don't have permission to work with uploaded files") . '.';
117 exit();
118 }
119 if (!$id) {
120 echo self::translate("Not a valid media file") . '.';
121 exit();
122 }
123 $metadata = wp_get_attachment_metadata($id);
124 if (!$metadata) {
125 echo self::translate("Could not find metadata of media file") . '.';
126 }
127
128 $this->compress_attachment($metadata, $id);
129 $this->render_media_column(self::MEDIA_COLUMN, $id);
130
131 exit();
132 }
133
134 public function bulk_compress() {
135 check_admin_referer('bulk-media');
136
137 if (empty($_REQUEST['media']) || !is_array( $_REQUEST['media'])) {
138 return;
139 }
140
141 foreach ($_REQUEST['media'] as $id) {
142 $metadata = wp_get_attachment_metadata($id);
143 if ($metadata) {
144 $this->compress_attachment($metadata, $id);
145 }
146 }
147 }
148
149 public function add_media_columns($columns) {
150 $columns[self::MEDIA_COLUMN] = self::translate(self::MEDIA_COLUMN_HEADER);
151 return $columns;
152 }
153
154 public function render_media_column($column, $id) {
155 if ($column === self::MEDIA_COLUMN) {
156 $wp_metadata = wp_get_attachment_metadata($id);
157 $wp_sizes = isset($wp_metadata['sizes']) ? array_keys($wp_metadata['sizes']) : array();
158 $wp_sizes[] = Tiny_Metadata::ORIGINAL;
159
160 $sizes = array_intersect($wp_sizes, $this->settings->get_tinify_sizes());
161
162 $tiny_metadata = new Tiny_Metadata($id);
163 $missing = $tiny_metadata->get_missing_sizes($sizes);
164 $total = count($sizes);
165 $success = $total - count($missing);
166
167 if (count($missing) > 0) {
168 printf(self::translate_escape('Compressed %d out of %d sizes'), $success, $total);
169 echo '<br/>';
170 if (($error = $tiny_metadata->get_latest_error())) {
171 echo '<span class="error">' . self::translate_escape('Latest error') . ': '. self::translate_escape($error) .'<br/>';
172 }
173 echo '<button type="button" class="tiny-compress" data-id="' . $id . '">' .
174 self::translate_escape('Compress') . '</button>';
175 echo '<div class="spinner"></div>';
176 } else {
177 printf(self::translate_escape('Compressed %d out of %d sizes'), $success, $total);
178 $savings = $tiny_metadata->get_savings();
179 if ($savings['count'] > 0) {
180 echo '<br/>';
181 echo self::translate_escape('Total size') . ': ' . size_format($savings['input']) . '<br/>';
182 echo self::translate_escape('Compressed size') . ': ' . size_format($savings['output']);
183 }
184 }
185 }
186 }
187 }
188