PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 1.2.1
TinyPNG – JPEG, PNG & WebP image compression v1.2.1
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
187 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 (!isset($metadata['sizes']) || !is_array($metadata['sizes'])) {
93 $tiny_metadata->update();
94 return $metadata;
95 }
96
97 foreach ($metadata['sizes'] as $size => $info) {
98 if (isset($settings[$size]) && $settings[$size]['tinify'] && !$tiny_metadata->is_compressed($size)) {
99 try {
100 $response = $this->settings->get_compressor()->compress_file("$prefix${info['file']}");
101 $tiny_metadata->add_response($response, $size);
102 } catch (Tiny_Exception $e) {
103 $tiny_metadata->add_exception($e, $size);
104 }
105 }
106 }
107
108 $tiny_metadata->update();
109 return $metadata;
110 }
111
112 public function compress_image() {
113 $id = $_POST['id'];
114 if (!current_user_can('upload_files')) {
115 echo self::translate("You don't have permission to work with uploaded files") . '.';
116 exit();
117 }
118 if (!$id) {
119 echo self::translate("Not a valid media file") . '.';
120 exit();
121 }
122 $metadata = wp_get_attachment_metadata($id);
123 if (!$metadata) {
124 echo self::translate("Could not find metadata of media file") . '.';
125 }
126
127 $this->compress_attachment($metadata, $id);
128 $this->render_media_column(self::MEDIA_COLUMN, $id);
129
130 exit();
131 }
132
133 public function bulk_compress() {
134 check_admin_referer('bulk-media');
135
136 if (empty($_REQUEST['media']) || !is_array( $_REQUEST['media'])) {
137 return;
138 }
139
140 foreach ($_REQUEST['media'] as $id) {
141 $metadata = wp_get_attachment_metadata($id);
142 if ($metadata) {
143 $this->compress_attachment($metadata, $id);
144 }
145 }
146 }
147
148 public function add_media_columns($columns) {
149 $columns[self::MEDIA_COLUMN] = self::translate(self::MEDIA_COLUMN_HEADER);
150 return $columns;
151 }
152
153 public function render_media_column($column, $id) {
154 if ($column === self::MEDIA_COLUMN) {
155 $wp_metadata = wp_get_attachment_metadata($id);
156 $wp_sizes = isset($wp_metadata['sizes']) ? array_keys($wp_metadata['sizes']) : array();
157 $wp_sizes[] = Tiny_Metadata::ORIGINAL;
158
159 $sizes = array_intersect($wp_sizes, $this->settings->get_tinify_sizes());
160
161 $tiny_metadata = new Tiny_Metadata($id);
162 $missing = $tiny_metadata->get_missing_sizes($sizes);
163 $total = count($sizes);
164 $success = $total - count($missing);
165
166 if (count($missing) > 0) {
167 printf(self::translate_escape('Compressed %d out of %d sizes'), $success, $total);
168 echo '<br/>';
169 if (($error = $tiny_metadata->get_latest_error())) {
170 echo '<span class="error">' . self::translate_escape('Latest error') . ': '. self::translate_escape($error) .'<br/>';
171 }
172 echo '<button type="button" class="tiny-compress" data-id="' . $id . '">' .
173 self::translate_escape('Compress') . '</button>';
174 echo '<div class="spinner"></div>';
175 } else {
176 printf(self::translate_escape('Compressed %d out of %d sizes'), $success, $total);
177 $savings = $tiny_metadata->get_savings();
178 if ($savings['count'] > 0) {
179 echo '<br/>';
180 echo self::translate_escape('Total size') . ': ' . size_format($savings['input']) . '<br/>';
181 echo self::translate_escape('Compressed size') . ': ' . size_format($savings['output']);
182 }
183 }
184 }
185 }
186 }
187