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