' ) );
}
}
/**
* Scans all the smushed attachments to check if they need to be resmushed as per the
* current settings, as user might have changed one of the configurations "Lossy", "Keep Original", "Preserve Exif"
*/
function scan_images() {
global $WpSmush, $wpsmushnextgenadmin, $wpsmush_db, $wpsmush_settings;
check_ajax_referer( 'save_wp_smush_options', 'wp_smush_options_nonce' );
$resmush_list = array();
//Save settings only if networkwide settings are disabled
if ( ( ! is_multisite() || ! $wpsmush_settings->is_network_enabled() ) && ( ! isset( $_REQUEST['process_settings'] ) || 'false' != $_REQUEST['process_settings'] ) ) {
//Save Settings
$wpsmush_settings->process_options();
}
//If there aren't any images in the library, return the notice
if ( 0 == $wpsmush_db->get_media_attachments( true ) ) {
$notice = esc_html__( "We haven’t found any images in your media library yet so there’s no smushing to be done!", "wp-smushit" );
$resp = '
' . $notice . '
';
//Save serialized settings
$this->save_serialized_settings();
wp_send_json_success( array(
'notice' => $resp,
'super_smush' => $WpSmush->lossy_enabled
) );
}
//Default Notice, to be displayed at the top of page
//Show a message, at the top
$message = esc_html__( 'Yay! All images are optimised as per your current settings.', 'wp-smushit' );
$resp = '
' . $message . '
';
//Scanning for NextGen or Media Library
$type = isset( $_REQUEST['type'] ) ? sanitize_text_field( $_REQUEST['type'] ) : '';
//If a user manually runs smush check
$return_ui = isset( $_REQUEST['get_ui'] ) && 'true' == $_REQUEST['get_ui'] ? true : false;
//Update the variables
$WpSmush->initialise();
//Logic: If none of the required settings is on, don't need to resmush any of the images
//We need at least one of these settings to be on, to check if any of the image needs resmush
//Allow to smush Upfront images as well
$upfront_active = class_exists( 'Upfront' );
//Initialize Media Library Stats
if ( 'nextgen' != $type && empty( $this->remaining_count ) ) {
$this->setup_global_stats();
}
//Intialize NextGen Stats
if ( 'nextgen' == $type && is_object( $wpsmushnextgenadmin ) && empty( $wpsmushnextgenadmin->remaining_count ) ) {
$wpsmushnextgenadmin->setup_stats();
}
$key = 'nextgen' == $type ? 'wp-smush-nextgen-resmush-list' : 'wp-smush-resmush-list';
$remaining_count = 'nextgen' == $type ? $wpsmushnextgenadmin->remaining_count : $this->remaining_count;
if ( 0 == $remaining_count && ! $WpSmush->lossy_enabled && ! $WpSmush->smush_original && $WpSmush->keep_exif && ! $upfront_active ) {
delete_option( $key );
//Save serialized settings
$this->save_serialized_settings();
wp_send_json_success( array( 'notice' => $resp ) );
}
//Set to empty by default
$ajax_response = '';
//Get Smushed Attachments
if ( 'nextgen' != $type ) {
//Get list of Smushed images
$attachments = ! empty( $this->smushed_attachments ) ? $this->smushed_attachments : $wpsmush_db->smushed_count( true );
} else {
global $wpsmushnextgenstats;
//Get smushed attachments list from nextgen class, We get the meta as well
$attachments = $wpsmushnextgenstats->get_ngg_images();
}
//Check if any of the smushed image needs to be resmushed
if ( ! empty( $attachments ) && is_array( $attachments ) ) {
foreach ( $attachments as $attachment_k => $attachment ) {
$should_resmush = false;
//For NextGen we get the metadata in the attachment data itself
if ( is_array( $attachment ) && ! empty( $attachment['wp_smush'] ) ) {
$smush_data = $attachment['wp_smush'];
} else {
//Check the current settings, and smush data for the image
$smush_data = get_post_meta( $attachment, $this->smushed_meta_key, true );
}
//If the image is already smushed
if ( is_array( $smush_data ) && ! empty( $smush_data['stats'] ) ) {
//If we need to optmise losslessly, add to resmush list
$smush_lossy = $WpSmush->lossy_enabled && ! $smush_data['stats']['lossy'];
//If we need to strip exif, put it in resmush list
$strip_exif = ! $WpSmush->keep_exif && isset( $smush_data['stats']['keep_exif'] ) && ( 1 == $smush_data['stats']['keep_exif'] );
//If Original image needs to be smushed
$smush_original = $WpSmush->smush_original && empty( $smush_data['sizes']['full'] );
if ( $smush_lossy || $strip_exif || $smush_original ) {
$should_resmush = true;
}
//If Image needs to be resized
if ( ! $should_resmush ) {
/**
* Get the resize settings, get the image dimensions, and check if it needs to be resized
*
*/
global $wpsmush_resize;
// Initialize resize class.
$wpsmush_resize->initialize();
$should_resmush = $wpsmush_resize->should_resize( $attachment );
}
//If image can be converted
if ( ! $should_resmush ) {
global $wpsmush_pngjpg;
$should_resmush = $wpsmush_pngjpg->can_be_converted( $attachment );
}
//If the image needs to be resmushed add it to the list
if ( $should_resmush ) {
$resmush_list[] = 'nextgen' == $type ? $attachment_k : $attachment;
continue;
}
}
}
//Check for Upfront images that needs to be smushed
if ( $upfront_active && 'nextgen' != $type ) {
$upfront_attachments = $wpsmush_db->get_upfront_images( $resmush_list );
if ( ! empty( $upfront_attachments ) && is_array( $upfront_attachments ) ) {
foreach ( $upfront_attachments as $u_attachment_id ) {
if ( ! in_array( $u_attachment_id, $resmush_list ) ) {
//Check if not smushed
$upfront_images = get_post_meta( $u_attachment_id, 'upfront_used_image_sizes', true );
if ( ! empty( $upfront_images ) && is_array( $upfront_images ) ) {
//Iterate over all the images
foreach ( $upfront_images as $image ) {
//If any of the element image is not smushed, add the id to resmush list
//and skip to next image
if ( empty( $image['is_smushed'] ) || 1 != $image['is_smushed'] ) {
$resmush_list[] = $u_attachment_id;
break;
}
}
}
}
}
}
}//End Of Upfront loop
//Store the resmush list in Options table
update_option( $key, $resmush_list );
}
//Delete resmush list if empty
if ( empty( $resmush_list ) ) {
//Delete the resmush list
delete_option( $key );
}
//Return the Remsmush list and UI to be appended to Bulk Smush UI
if ( $return_ui ) {
if ( 'nextgen' != $type ) {
//Set the variables
$this->resmush_ids = $resmush_list;
$resmush_count = count( $this->resmush_ids );
$smushed = ! empty( $this->resmush_ids ) ? $this->smushed_count - $resmush_count : $this->smushed_count;
} else {
//To avoid the php warning
$wpsmushnextgenadmin->resmush_ids = $resmush_list;
}
if ( ( $count = count( $resmush_list ) ) > 0 || $this->remaining_count > 0 ) {
if ( $count ) {
$show = true;
$count += 'nextgen' == $type ? $wpsmushnextgenadmin->remaining_count : $this->remaining_count;
$ajax_response = $this->bulk_ui->bulk_resmush_content( $count, $show );
}
}
}
if ( ! empty( $resmush_list ) || $remaining_count > 0 ) {
$message = sprintf( esc_html__( "You have images that need smushing. %sBulk smush now!%s", "wp-smushit" ), '', '' );
$resp = '
' . $message . '
';
}
//If there is a Ajax response return it, else return null
$return = ! empty( $ajax_response ) ? array(
"resmush_ids" => $resmush_list,
"content" => $ajax_response
) : array();
//Include the count
if ( ! empty( $count ) && $count ) {
$return['count'] = $count;
}
$return['notice'] = $resp;
$return['super_smush'] = $WpSmush->lossy_enabled;
if ( $WpSmush->lossy_enabled && 'nextgen' == $type ) {
$ss_count = $wpsmush_db->super_smushed_count( 'nextgen', $wpsmushnextgenstats->get_ngg_images( 'smushed' ) );
$return['super_smush_stats'] = sprintf( '%d/%d', $ss_count, $wpsmushnextgenadmin->total_count );
}
//Save serialized settings
$this->save_serialized_settings();
wp_send_json_success( $return );
}
/**
* Remove the given attachment id from resmush list and updates it to db
*
* @param $attachment_id
* @param string $mkey
*
*/
function update_resmush_list( $attachment_id, $mkey = 'wp-smush-resmush-list' ) {
$resmush_list = get_option( $mkey );
//If there are any items in the resmush list, Unset the Key
if ( ! empty( $resmush_list ) && count( $resmush_list ) > 0 ) {
$key = array_search( $attachment_id, $resmush_list );
if ( $resmush_list ) {
unset( $resmush_list[ $key ] );
}
$resmush_list = array_values( $resmush_list );
}
//If Resmush List is empty
if ( empty( $resmush_list ) || 0 == count( $resmush_list ) ) {
//Delete resmush list
delete_option( $mkey );
} else {
update_option( $mkey, $resmush_list );
}
}
/**
* Returns current user name to be displayed
* @return string
*/
function get_user_name() {
//Get username
$current_user = wp_get_current_user();
$name = ! empty( $current_user->first_name ) ? $current_user->first_name : $current_user->display_name;
return $name;
}
/**
* Format Numbers to short form 1000 -> 1k
*
* @param $number
*
* @return string
*/
function format_number( $number ) {
if ( $number >= 1000 ) {
return $number / 1000 . "k"; // NB: you will want to round this
} else {
return $number;
}
}
/**
* Add/Remove image id from Super Smushed images count
*
* @param int $id Image id
*
* @param string $op_type Add/remove, whether to add the image id or remove it from the list
*
* @return bool Whether the Super Smushed option was update or not
*
*/
function update_super_smush_count( $id, $op_type = 'add', $key = 'wp-smush-super_smushed' ) {
//Get the existing count
$super_smushed = get_option( $key, false );
//Initialize if it doesn't exists
if ( ! $super_smushed || empty( $super_smushed['ids'] ) ) {
$super_smushed = array(
'ids' => array()
);
}
//Insert the id, if not in there already
if ( 'add' == $op_type && ! in_array( $id, $super_smushed['ids'] ) ) {
$super_smushed['ids'][] = $id;
} elseif ( 'remove' == $op_type && false !== ( $k = array_search( $id, $super_smushed['ids'] ) ) ) {
//Else remove the id from the list
unset( $super_smushed['ids'][ $k ] );
//Reset all the indexes
$super_smushed['ids'] = array_values( $super_smushed['ids'] );
}
//Add the timestamp
$super_smushed['timestamp'] = current_time( 'timestamp' );
update_option( $key, $super_smushed );
//Update to database
return true;
}
/**
* Checks if the image compression is lossy, stores the image id in options table
*
* @param int $id Image Id
*
* @param array $stats Compression Stats
*
* @param string $key Meta Key for storing the Super Smushed ids (Optional for Media Library)
* Need To be specified for NextGen
*
* @return bool
*/
function update_lists( $id, $stats, $key = '' ) {
//If Stats are empty or the image id is not provided, return
if ( empty( $stats ) || empty( $id ) || empty( $stats['stats'] ) ) {
return false;
}
//Update Super Smush count
if ( isset( $stats['stats']['lossy'] ) && 1 == $stats['stats']['lossy'] ) {
if ( empty( $key ) ) {
update_post_meta( $id, 'wp-smush-lossy', 1 );
} else {
$this->update_super_smush_count( $id, 'add', $key );
}
}
//Check and update re-smush list for media gallery
if ( ! empty( $this->resmush_ids ) && in_array( $id, $this->resmush_ids ) ) {
$this->update_resmush_list( $id );
}
}
/**
* Delete the resmush list for Nextgen or the Media Library
*/
function delete_resmush_list() {
$key = ! empty( $_POST['type'] ) && 'nextgen' == $_POST['type'] ? 'wp-smush-nextgen-resmush-list' : 'wp-smush-resmush-list';
//Delete the resmush list
delete_option( $key );
wp_send_json_success();
}
/**
* Allows to bulk restore the images, if there is any backup for them
*/
function bulk_restore() {
global $wpsmush_db, $wpsmush_backup;
$smushed_attachments = ! empty( $this->smushed_attachments ) ? $this->smushed_attachments : $wpsmush_db->smushed_count( true );
foreach ( $smushed_attachments as $attachment ) {
$wpsmush_backup->restore_image( $attachment->attachment_id, false );
}
}
/**
* Loads the Shared UI to on all admin pages
*
* @param $current_page
*/
function load_shared_ui( $current_page ) {
//If class method exists, load shared UI
if ( class_exists( 'WDEV_Plugin_Ui' ) ) {
if ( method_exists( 'WDEV_Plugin_Ui', 'load' ) && in_array( $current_page, $this->plugin_pages ) ) {
//Load Shared UI
WDEV_Plugin_Ui::load( WP_SMUSH_URL . 'assets/shared-ui/', false );
}
}
}
/** Get the Maximum Width and Height settings for WrodPress
*
* @return array, Array of Max. Width and Height for image
*
*/
function get_max_image_dimensions() {
global $_wp_additional_image_sizes;
$width = $height = 0;
$limit = 9999; //Post-thumbnail
$image_sizes = get_intermediate_image_sizes();
// Create the full array with sizes and crop info
foreach ( $image_sizes as $size ) {
if ( in_array( $size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
$size_width = get_option( "{$size}_size_w" );
$size_height = get_option( "{$size}_size_h" );
} elseif ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
$size_width = $_wp_additional_image_sizes[ $size ]['width'];
$size_height = $_wp_additional_image_sizes[ $size ]['height'];
}
//Skip if no width and height
if ( ! isset( $size_width, $size_height ) ) {
continue;
}
//If within te limit, check for a max value
if ( $size_width <= $limit ) {
$width = max( $width, $size_width );
}
if ( $size_height <= $limit ) {
$height = max( $height, $size_height );
}
}
return array(
'width' => $width,
'height' => $height
);
}
/**
* Perform the resize operation for the image
*
* @param $attachment_id
*
* @param $meta
*
* @return mixed
*/
function resize_image( $attachment_id, $meta ) {
if ( empty( $attachment_id ) || empty( $meta ) ) {
return $meta;
}
global $wpsmush_resize;
return $wpsmush_resize->auto_resize( $attachment_id, $meta );
}
/**
* Limit for all the queries
*
* @return int|mixed|void
*
*/
function query_limit() {
$limit = apply_filters( 'wp_smush_query_limit', 2000 );
$limit = !empty( $this->total_count ) && $limit > $this->total_count ? $this->total_count : $limit;
$limit = intval( $limit );
return $limit;
}
/**
* Filter the number of results fetched at once for NextGen queries
*
* @return int|mixed|void
*
*/
function nextgen_query_limit() {
$limit = apply_filters( 'wp_smush_nextgen_query_limit', 1000 );
$limit = intval( $limit );
return $limit;
}
/**
* Show Update info in admin Notice
*
*/
function smush_updated() {
//@todo: Update Smush Update Notice for next release
//Make sure to not display this message for next release
$plugin_data = get_plugin_data( WP_SMUSH_DIR . 'wp-smush.php', false, false );
$version = ! empty( $plugin_data['Version'] ) ? $plugin_data['Version'] : '';
//If Versions Do not match
if ( empty( $version ) || $version != WP_SMUSH_VERSION ) {
return true;
}
//Do not display it for other users
if ( ! is_super_admin() || ! current_user_can( 'manage_options' ) ) {
return true;
}
//If dismissed, Delete the option on Plugin Activation, For alter releases
if ( 1 == get_site_option( 'wp-smush-hide_update_info' ) ) {
return true;
}
//Get Plugin dir, Return if it's WP Smush Pro installation
$dir_path = get_plugin_dir();
if ( ! empty( $dir_path ) && strpos( $dir_path, 'wp-smush-pro' ) !== false ) {
return true;
}
//Do not display the notice on Bulk Smush Screen
global $current_screen;
if ( ! empty( $current_screen->base ) && ( 'media_page_wp-smush-bulk' == $current_screen->base || 'gallery_page_wp-smush-nextgen-bulk' == $current_screen->base || 'settings_page_wp-smush-network' == $current_screen->base ) ) {
return true;
}
$upgrade_url = add_query_arg(
array(
'utm_source' => 'Smush-Free',
'utm_medium' => 'Banner',
'utm_campaign' => 'now-with-async'
),
$this->upgrade_url
);
$settings_link = is_multisite() ? network_admin_url( 'settings.php?page=wp-smush' ) : admin_url( 'upload.php?page=wp-smush-bulk' );
$settings_link = '';
$upgrade_link = '';
$message_s = sprintf( esc_html__( "Welcome to the newest version of WP Smush! In this update we've added the ability to bulk smush images in directories outside your uploads folder.", 'wp-smushit' ), WP_SMUSH_VERSION, '', '' );
//Message for network admin
$message_s .= is_multisite() ? sprintf( esc_html__( " And as a multisite user, you can manage %sSmush settings%s globally across all sites!", 'wp-smushit' ), $settings_link, '' ) : '';
//Upgrade link for free users
$message_s .= ! $this->validate_install() ? sprintf( esc_html__( " %sFind out more here >>%s", "wp-smushit" ), $upgrade_link, '' ) : '';
?>
get_setting( WP_SMUSH_PREFIX.'image_sizes' );
//If Images sizes aren't set, don't skip any of the image size
if( false === $image_sizes ) {
return false;
}
//Check if the size is in the smush list
if( is_array( $image_sizes ) && !in_array( $size, $image_sizes ) ) {
return true;
}
}
/**
* Get registered image sizes with dimension
*
*/
function image_dimensions() {
global $_wp_additional_image_sizes;
$additional_sizes = get_intermediate_image_sizes();
$sizes = array();
// Create the full array with sizes and crop info
foreach( $additional_sizes as $_size ) {
if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {
$sizes[ $_size ]['width'] = get_option( $_size . '_size_w' );
$sizes[ $_size ]['height'] = get_option( $_size . '_size_h' );
$sizes[ $_size ]['crop'] = (bool) get_option( $_size . '_crop' );
} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
$sizes[ $_size ] = array(
'width' => $_wp_additional_image_sizes[ $_size ]['width'],
'height' => $_wp_additional_image_sizes[ $_size ]['height'],
'crop' => $_wp_additional_image_sizes[ $_size ]['crop']
);
}
}
//Medium Large
if ( !isset( $sizes['medium_large'] ) || empty( $sizes['medium_large'] ) ) {
$width = intval( get_option( 'medium_large_size_w' ) );
$height = intval( get_option( 'medium_large_size_h' ) );
$sizes['medium_large'] = array(
'width' => $width,
'height' => $height
);
}
return $sizes;
}
/**
* Prints the Membership Validation issue notice
*
*/
function media_library_membership_notice() {
//No need to print it for free version
if( !$this->validate_install() ) {
return;
}
//Show it on Media Library page only
$screen = get_current_screen();
$screen_id = !empty( $screen ) ? $screen->id : '';
//Do not show notice anywhere else
if( empty( $screen ) || 'upload' != $screen_id ) {
return;
}
echo $this->bulk_ui->get_user_validation_message( $notice = true );
}
/**
* Save settings, Used for networkwide option
*/
function save_settings() {
//Validate Ajax request
check_ajax_referer( 'save_wp_smush_options', 'nonce' );
global $wpsmush_settings;
//Save Settings
$wpsmush_settings->process_options();
wp_send_json_success();
}
/**
* Returns a serialised string of current settings
*
* @return Serialised string of settings
*
*/
function get_serialised_settings() {
global $wpsmush_settings;
$settings = array();
foreach ( $this->settings as $key => $val ) {
$settings[ $key ] = $wpsmush_settings->get_setting( WP_SMUSH_PREFIX . $key );
}
$settings = maybe_serialize( $settings );
return $settings;
}
/**
* Stores the latest settings in serialised form in DB For the current settings
*
* No need to store the serialised settings, if network wide settings is disabled
* because the site would run the scan when settings are saved
*
*/
function save_serialized_settings() {
//Return -> Single Site | If network settings page | Networkwide Settings Disabled
if ( ! is_multisite() || is_network_admin() || ! get_site_option( WP_SMUSH_PREFIX . 'networkwide' ) ) {
return;
}
global $wpsmush_settings;
$c_settings = $this->get_serialised_settings();
$wpsmush_settings->update_setting( WP_SMUSH_PREFIX . 'last_settings', $c_settings );
}
/**
* Allows to filter the error message sent to the user
*
* @param string $error
* @param string $attachment_id
*
* @return mixed|null|string|void
*/
function filter_error( $error = '', $attachment_id = '' ) {
if ( empty( $error ) ) {
return null;
}
/**
* Used internally to modify the error message
*
*/
$error = apply_filters( 'wp_smush_error', $error, $attachment_id );
return $error;
}
}
global $wpsmushit_admin;
$wpsmushit_admin = new WpSmushitAdmin();
}