-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-smush.php
295 lines (254 loc) · 8.96 KB
/
wp-smush.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/*
Plugin Name: WP Smush Pro
Plugin URI: http://premium.wpmudev.org/projects/wp-smush-pro/
Description: Reduce image file sizes, improve performance and boost your SEO using the <a href="https://premium.wpmudev.org/">WPMU DEV</a> WordPress Smush API.
Author: WPMU DEV
Version: 2.7.8
Author URI: https://premium.wpmudev.org/
Text Domain: wp-smushit
WDP ID: 912164
*/
/*
Copyright 2009-2017 Incsub (http://incsub.com)
Author - Aaron Edwards, Sam Najian, Umesh Kumar
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Constants
*/
$prefix = 'WP_SMUSH_';
$version = '2.7.8';
//Deactivate the .org version, if pro version is active
add_action( 'admin_init', 'deactivate_smush_org' );
if ( ! function_exists( 'deactivate_smush_org' ) ) {
function deactivate_smush_org() {
if ( is_plugin_active( 'wp-smush-pro/wp-smush.php' ) && is_plugin_active( 'wp-smushit/wp-smush.php' ) ) {
deactivate_plugins( 'wp-smushit/wp-smush.php' );
//Store in database, in order to show a notice on page load
update_site_option( 'smush_deactivated', 1 );
}
}
}
/**
* Set the default timeout for API request and AJAX timeout
*/
$timeout = apply_filters( 'WP_SMUSH_API_TIMEOUT', 90 );
// To support smushing on staging sites like SiteGround staging where
// staging site urls are different but redirects to main site url.
// Remove the protocols and www, and get the domain name.
$site_url = str_replace( array( 'http://', 'https://', 'www.' ), '', site_url() );
// If current site's url is different from site_url, disable Async.
if ( ! empty( $_SERVER['SERVER_NAME'] ) && ( 0 !== strpos( $site_url, $_SERVER['SERVER_NAME'] ) ) && ! defined( $prefix . 'ASYNC' ) ) {
define( $prefix . 'ASYNC', false );
}
$smush_constants = array(
'VERSION' => $version,
'BASENAME' => plugin_basename( __FILE__ ),
'API' => 'https://smushpro.wpmudev.org/1.0/',
'UA' => 'WP Smush/' . $version . '; ' . network_home_url(),
'DIR' => plugin_dir_path( __FILE__ ),
'URL' => plugin_dir_url( __FILE__ ),
'MAX_BYTES' => 1000000,
'PREMIUM_MAX_BYTES' => 32000000,
'PREFIX' => 'wp-smush-',
'TIMEOUT' => $timeout,
//If Set to false, WP Smush switch backs to the Old Sync Optimisation
'ASYNC' => true
);
foreach ( $smush_constants as $const_name => $constant_val ) {
if ( ! defined( $prefix . $const_name ) ) {
define( $prefix . $const_name, $constant_val );
}
}
//Include main class
require_once WP_SMUSH_DIR . 'lib/class-wp-smush.php';
/**
* Filters the rating message, include stats if greater than 1Mb
*
* @param $message
*
* @return string
*/
if ( ! function_exists( 'wp_smush_rating_message' ) ) {
function wp_smush_rating_message( $message ) {
global $wpsmushit_admin, $wpsmush_db;
if ( empty( $wpsmushit_admin->stats ) ) {
$wpsmushit_admin->setup_global_stats();
}
$savings = $wpsmushit_admin->stats;
$show_stats = false;
//If there is any saving, greater than 1Mb, show stats
if ( ! empty( $savings ) && ! empty( $savings['bytes'] ) && $savings['bytes'] > 1048576 ) {
$show_stats = true;
}
$message = "Hey %s, you've been using %s for a while now, and we hope you're happy with it.";
//Conditionally Show stats in rating message
if ( $show_stats ) {
$message .= sprintf( " You've smushed <strong>%s</strong> from %d images already, improving the speed and SEO ranking of this site!", $savings['human'], $savings['total_images'] );
}
$message .= " We've spent countless hours developing this free plugin for you, and we would really appreciate it if you dropped us a quick rating!";
return $message;
}
}
/**
* NewsLetter
*
* @param $message
*
* @return string
*/
if ( ! function_exists( 'wp_smush_email_message' ) ) {
function wp_smush_email_message( $message ) {
$message = "You're awesome for installing %s! Site speed isn't all image optimization though, so we've collected all the best speed resources we know in a single email - just for users of WP Smush!";
return $message;
}
}
if ( ! function_exists( 'get_plugin_dir' ) ) {
/**
* Returns the dir path for the plugin
*
* @return string
*/
function get_plugin_dir() {
$dir_path = plugin_dir_path( __FILE__ );
return $dir_path;
}
}
if ( is_admin() ) {
$dir_path = get_plugin_dir();
//Only for wordpress.org members
if ( strpos( $dir_path, 'wp-smushit' ) !== false ) {
require_once( WP_SMUSH_DIR . 'extras/free-dashboard/module.php' );
// Register the current plugin.
do_action(
'wdev-register-plugin',
/* 1 Plugin ID */
plugin_basename( __FILE__ ), /* Plugin ID */
/* 2 Plugin Title */
'WP Smush',
/* 3 https://wordpress.org */
'/plugins/wp-smushit/',
/* 4 Email Button CTA */
__( 'Get Fast', 'wp-smushit' ),
/* 5 getdrip Plugin param */
'Smush'
);
// The rating message contains 2 variables: user-name, plugin-name
add_filter(
'wdev-rating-message-' . plugin_basename( __FILE__ ),
'wp_smush_rating_message'
);
// The email message contains 1 variable: plugin-name
add_filter(
'wdev-email-message-' . plugin_basename( __FILE__ ),
'wp_smush_email_message'
);
} elseif ( strpos( $dir_path, 'wp-smush-pro' ) !== false && file_exists( WP_SMUSH_DIR . 'extras/dash-notice/wpmudev-dash-notification.php' ) ) {
//Only for WPMU DEV Members
require_once( WP_SMUSH_DIR . 'extras/dash-notice/wpmudev-dash-notification.php' );
//register items for the dashboard plugin
global $wpmudev_notices;
$wpmudev_notices[] = array(
'id' => 912164,
'name' => 'WP Smush Pro',
'screens' => array(
'upload',
'media_page_wp-smush-bulk'
)
);
}
}
//Show the required notice
add_action( 'network_admin_notices', 'smush_deactivated' );
add_action( 'admin_notices', 'smush_deactivated' );
//Display a admin Notice about plugin deactivation
if ( ! function_exists( 'smush_deactivated' ) ) {
function smush_deactivated() {
if ( get_site_option( 'smush_deactivated' ) && is_super_admin() ) { ?>
<div class="updated">
<p><?php esc_html_e( 'WP Smush Free was deactivated. You have WP Smush Pro active!', 'wp-smushit' ); ?></p>
</div> <?php
delete_site_option( 'smush_deactivated' );
}
}
}
if ( ! function_exists( 'smush_activated' ) ) {
/**
* Check if a existing install or new
*/
function smush_activated() {
global $wpsmush_settings;
$version = get_site_option( WP_SMUSH_PREFIX . 'version' );
$settings = ! empty( $wpsmush_settings->settings ) ? $wpsmush_settings->settings : $wpsmush_settings->init_settings();
//If the version is not saved or if the version is not same as the current version,
if ( ! $version || WP_SMUSH_VERSION != $version ) {
global $wpdb;
//Check if there are any existing smush stats
$query = "SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key=%s LIMIT 1";
$results = $wpdb->get_var( $wpdb->prepare( $query, 'wp-smpro-smush-data' ) );
if ( $results ) {
update_site_option( 'wp-smush-install-type', 'existing' );
} else {
//Check for existing settings
if ( false !== $settings['auto'] ) {
update_site_option( 'wp-smush-install-type', 'existing' );
}
}
//Store the plugin version in db
update_site_option( WP_SMUSH_PREFIX . 'version', WP_SMUSH_VERSION );
}
}
}
if ( ! function_exists( 'smush_sanitize_hex_color' ) ) {
/**
* Sanitizes a hex color.
*
* @param $color
*
* @return string Returns either '', a 3 or 6 digit hex color (with #), or nothing
*/
function smush_sanitize_hex_color( $color ) {
if ( '' === $color ) {
return '';
}
// 3 or 6 hex digits, or the empty string.
if ( preg_match( '|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
return $color;
}
}
}
if ( ! function_exists( 'smush_sanitize_hex_color_no_hash' ) ) {
/**
* Sanitizes a hex color without hash
*
* @param $color
*
* @return string Returns either '', a 3 or 6 digit hex color (with #), or nothing
*/
function smush_sanitize_hex_color_no_hash( $color ) {
$color = ltrim( $color, '#' );
if ( '' === $color ) {
return '';
}
return smush_sanitize_hex_color( '#' . $color ) ? $color : null;
}
}
//Load Translation files
add_action( 'plugins_loaded', 'smush_i18n' );
if ( ! function_exists( 'smush_i18n' ) ) {
function smush_i18n() {
$path = path_join( dirname( plugin_basename( __FILE__ ) ), 'languages/' );
load_plugin_textdomain( 'wp-smushit', false, $path );
}
}
register_activation_hook( __FILE__, 'smush_activated' );