: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
$smush = WP_Smush::get_instance()->core()->mod->smush;
* @param int $attachment_id Attachment ID.
* @param array $meta Image metadata (passed by reference).
* @param WP_Error $errors WP_Error (passed by reference).
$smush->smushit( $attachment_id, $meta, $errors );
$smush_data = get_post_meta( $attachment_id, Smush::$smushed_meta_key, true );
$resize_savings = get_post_meta( $attachment_id, 'wp-smush-resize_savings', true );
$conversion_savings = Helper::get_pngjpg_savings( $attachment_id );
'count' => ! empty( $smush_data['sizes'] ) ? count( $smush_data['sizes'] ) : 0,
'size_before' => ! empty( $smush_data['stats'] ) ? $smush_data['stats']['size_before'] : 0,
'size_after' => ! empty( $smush_data['stats'] ) ? $smush_data['stats']['size_after'] : 0,
'savings_resize' => max( $resize_savings, 0 ),
'savings_conversion' => $conversion_savings['bytes'] > 0 ? $conversion_savings : 0,
'is_lossy' => ! empty( $smush_data ['stats'] ) ? $smush_data['stats']['lossy'] : false,
if ( $errors && is_wp_error( $errors ) && $errors->has_errors() ) {
$error = Error_Handler::get_error( $errors, Media_Item_Cache::get_instance()->get( $attachment_id ) );
'show_warning' => (int) $smush->show_warning(),
wp_send_json_error( $response );
// Runs after a image is successfully smushed.
do_action( 'image_smushed', $attachment_id, $stats );
// Update the bulk Limit count.
Core::update_smush_count();
'show_warning' => (int) $smush->show_warning(),
/***************************************
* Returns Directory Smush stats and Cumulative stats
public function get_dir_smush_stats() {
check_ajax_referer( 'wp-smush-ajax' );
$capability = is_multisite() ? 'manage_network' : 'manage_options';
if ( ! Helper::is_user_allowed( $capability ) ) {
wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 );
// Store the Total/Smushed count.
$stats = WP_Smush::get_instance()->core()->mod->dir->total_stats();
$result['dir_smush'] = $stats;
//$result['combined_stats'] = WP_Smush::get_instance()->core()->mod->dir->combine_stats( $stats );
// Store the stats in options table.
update_option( 'dir_smush_stats', $result, false );
wp_send_json_success( $result );
/***************************************
/***************************************
* Toggle lazy loading module.
* Handles "Activate" button press on the disabled lazy loading meta box.
* Handles "Deactivate" button press on the lazy loading meta box.
* Refreshes page on success.
public function smush_toggle_lazy_load() {
check_ajax_referer( 'save_wp_smush_options' );
if ( ! Helper::is_user_allowed( 'manage_options' ) ) {
'message' => __( 'User can not modify options', 'wp-smushit' ),
$param = isset( $_POST['param'] ) ? sanitize_text_field( wp_unslash( $_POST['param'] ) ) : false;
if ( 'true' === $param ) {
$settings = $this->settings->get_setting( 'wp-smush-lazy_load' );
// No settings, during init - set defaults.
$this->settings->init_lazy_load_defaults();
$this->settings->set( 'lazy_load', 'true' === $param );
* Remove spinner/placeholder icon from lazy-loading.
public function remove_icon() {
check_ajax_referer( 'save_wp_smush_options' );
if ( ! Helper::is_user_allowed( 'manage_options' ) ) {
wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 );
$id = filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT );
$type = filter_input( INPUT_POST, 'type', FILTER_SANITIZE_SPECIAL_CHARS );
$settings = $this->settings->get_setting( 'wp-smush-lazy_load' );
if ( false !== ( $key = array_search( $id, $settings['animation'][ $type ]['custom'] ) ) ) {
unset( $settings['animation'][ $type ]['custom'][ $key ] );
$this->settings->set_setting( 'wp-smush-lazy_load', $settings );
/***************************************
* Handles the upload of a config file.
public function upload_config() {
check_ajax_referer( 'smush_handle_config' );
$capability = is_multisite() ? 'manage_network' : 'manage_options';
if ( ! Helper::is_user_allowed( $capability ) ) {
wp_send_json_error( null, 403 );
* Data escaped and sanitized via \Smush\Core\Configs::save_uploaded_config()
* @see \Smush\Core\Configs::decode_and_validate_config_file()
$file = isset( $_FILES['file'] ) ? wp_unslash( $_FILES['file'] ) : false; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$configs_handler = new Configs();
$new_config = $configs_handler->save_uploaded_config( $file );
if ( ! is_wp_error( $new_config ) ) {
wp_send_json_success( $new_config );
array( 'error_msg' => $new_config->get_error_message() )
* Handles the upload of a config file.
public function save_config() {
check_ajax_referer( 'smush_handle_config' );
$capability = is_multisite() ? 'manage_network' : 'manage_options';
if ( ! Helper::is_user_allowed( $capability ) ) {
wp_send_json_error( null, 403 );
$configs_handler = new Configs();
wp_send_json_success( $configs_handler->get_config_from_current() );
* Applies the given config.
public function apply_config() {
check_ajax_referer( 'smush_handle_config' );
$capability = is_multisite() ? 'manage_network' : 'manage_options';
if ( ! Helper::is_user_allowed( $capability ) ) {
wp_send_json_error( null, 403 );
$id = filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT );
// Abort if no config ID was given.
array( 'error_msg' => esc_html__( 'Missing config ID', 'wp-smushit' ) )
$configs_handler = new Configs();
$response = $configs_handler->apply_config_by_id( $id );
if ( ! is_wp_error( $response ) ) {
array( 'error_msg' => esc_html( $response->get_error_message() ) )
/***************************************
public function recheck_api_status() {
if ( ! Helper::is_user_allowed( 'manage_options' ) ) {
wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 );
WP_Smush::get_instance()->validate_install( true );
/***************************************
* Hide the new features modal
public function hide_new_features_modal() {
check_ajax_referer( 'wp-smush-ajax' );
if ( ! Helper::is_user_allowed( 'manage_options' ) ) {
wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 );
delete_site_option( 'wp-smush-show_upgrade_modal' );