: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
use Smush\Core\CDN\CDN_Controller;
use Smush\Core\Webp\Webp_Configuration;
private $pro_features = array( 'png_to_jpg', 's3', 'nextgen', 'cdn', 'webp', 'webp_mod' );
public function __construct() {
$this->settings = Settings::get_instance();
* Gets the local list of configs via Smush endpoint.
public function get_callback() {
$stored_configs = get_site_option( 'wp-smush-preset_configs', false );
if ( false === $stored_configs ) {
$stored_configs = array( $this->get_basic_config() );
update_site_option( 'wp-smush-preset_configs', $stored_configs );
* Updates the local list of configs via Smush endpoint.
* @param WP_REST_Request $request Class containing the request data.
public function post_callback( $request ) {
$data = json_decode( $request->get_body(), true );
if ( ! is_array( $data ) ) {
return new WP_Error( '400', esc_html__( 'Missing configs data', 'wp-smushit' ), array( 'status' => 400 ) );
$sanitized_data = $this->sanitize_configs_list( $data );
update_site_option( 'wp-smush-preset_configs', $sanitized_data );
* Checks whether the current user can perform requests to Smush's endpoint.
public function permission_callback() {
$capability = is_multisite() ? 'manage_network' : 'manage_options';
return current_user_can( $capability );
* Adds the default configuration to the local configs.
private function get_basic_config() {
'name' => __( 'Default config', 'wp-smushit' ),
'description' => __( 'Recommended performance config for every site.', 'wp-smushit' ),
'lossy' => Settings::LEVEL_SUPER_LOSSY,
'background_email' => false,
'accessible_colors' => false,
'background_images' => true,
'rest_api_support' => false,
$basic_config['config']['strings'] = $this->format_config_to_display( $basic_config['config']['configs'] );
* Sanitizes the full list of configs.
* @param array $configs_list Configs list to sanitize.
private function sanitize_configs_list( $configs_list ) {
$sanitized_list = array();
foreach ( $configs_list as $config_data ) {
if ( isset( $config_data['name'] ) ) {
$name = sanitize_text_field( $config_data['name'] );
if ( isset( $config_data['description'] ) ) {
$description = sanitize_text_field( $config_data['description'] );
$configs = isset( $config_data['config']['configs'] ) ? $config_data['config']['configs'] : array();
'id' => filter_var( $config_data['id'], FILTER_VALIDATE_INT ),
'name' => empty( $name ) ? __( 'Undefined', 'wp-smushit' ) : $name,
'description' => empty( $description ) ? '' : $description,
'config' => $this->sanitize_and_format_configs( $configs ),
if ( ! empty( $config_data['hub_id'] ) ) {
$sanitized_data['hub_id'] = filter_var( $config_data['hub_id'], FILTER_VALIDATE_INT );
if ( isset( $config_data['default'] ) ) {
$sanitized_data['default'] = filter_var( $config_data['default'], FILTER_VALIDATE_BOOLEAN );
$sanitized_list[] = $sanitized_data;
* Tries to save the uploaded config.
* @param array $file The uploaded file.
public function save_uploaded_config( $file ) {
return $this->decode_and_validate_config_file( $file );
} catch ( Exception $e ) {
return new WP_Error( 'error_saving', $e->getMessage() );
* Tries to decode and validate the uploaded config file.
* @param array $file The uploaded file.
* @throws Exception When there's an error with the uploaded file.
private function decode_and_validate_config_file( $file ) {
throw new Exception( __( 'The configs file is required', 'wp-smushit' ) );
} elseif ( ! empty( $file['error'] ) ) {
/* translators: error message */
throw new Exception( sprintf( __( 'Error: %s.', 'wp-smushit' ), $file['error'] ) );
} elseif ( 'application/json' !== $file['type'] ) {
throw new Exception( __( 'The file must be a JSON.', 'wp-smushit' ) );
$json_file = file_get_contents( $file['tmp_name'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
throw new Exception( __( 'There was an error getting the contents of the file.', 'wp-smushit' ) );
$configs = json_decode( $json_file, true );
if ( empty( $configs ) || ! is_array( $configs ) ) {
throw new Exception( __( 'There was an error decoding the file.', 'wp-smushit' ) );
// Make sure the config has a name and configs.
if ( empty( $configs['name'] ) || empty( $configs['config'] ) ) {
throw new Exception( __( 'The uploaded config must have a name and a set of settings. Please make sure the uploaded file is the correct one.', 'wp-smushit' ) );
$plugin = isset( $configs['plugin'] ) ? $configs['plugin'] : 0;
$configs = $this->sanitize_configs_list( array( $configs ) );
// Restore back plugin ID.
$configs['plugin'] = $plugin;
// Let's re-create this to avoid differences between imported settings coming from other versions.
$configs['config']['strings'] = $this->format_config_to_display( $configs['config']['configs'] );
if ( empty( $configs['config']['configs'] ) ) {
throw new Exception( __( 'The provided configs list isn’t correct. Please make sure the uploaded file is the correct one.', 'wp-smushit' ) );
// Don't keep these if they exist.
if ( isset( $configs['hub_id'] ) ) {
unset( $configs['hub_id'] );
if ( isset( $configs['default'] ) ) {
unset( $configs['default'] );
* Applies a config given its ID.
* @param string $id The ID of the config to apply.
public function apply_config_by_id( $id ) {
$stored_configs = get_site_option( 'wp-smush-preset_configs' );
foreach ( $stored_configs as $config_data ) {
if ( (int) $config_data['id'] === (int) $id ) {
// The config with the given ID doesn't exist.
return new WP_Error( '404', __( 'The given config ID does not exist', 'wp-smushit' ) );
$this->apply_config( $config['config']['configs'], $config['name'] );
* Applies the given config.
* @param array $config The config to apply.
public function apply_config( $config, $config_name = '' ) {
$sanitized_config = $this->sanitize_config( $config );
// Update 'networkwide' options in multisites.
if ( is_multisite() && isset( $sanitized_config['networkwide'] ) ) {
update_site_option( 'wp-smush-networkwide', $sanitized_config['networkwide'] );
$settings_handler = Settings::get_instance();
if ( isset( $sanitized_config['resize_sizes'] ) ) {
$settings_handler->set_setting( 'wp-smush-resize_sizes', $sanitized_config['resize_sizes'] );
// Update settings. We could reuse the `save` method from settings to handle this instead.
if ( ! empty( $sanitized_config['settings'] ) ) {
$stored_settings = $settings_handler->get_setting( 'wp-smush-settings' );
// Keep the keys that are in use in this version.
$new_settings = array_intersect_key( $sanitized_config['settings'], $stored_settings );
if ( ! WP_Smush::is_pro() ) {
// Disable the pro features before applying them.
foreach ( $this->pro_features as $name ) {
$new_settings[ $name ] = false;
// Update Local WebP status.
if ( isset( $new_settings['webp_mod'] ) ) {
$webp_configuration = Webp_Configuration::get_instance();
$enable_webp = ! empty( $new_settings['webp_mod'] );
$direct_conversion_enabled = ! empty( $new_settings['webp_direct_conversion'] );
$settings_handler->set( 'webp_direct_conversion', $direct_conversion_enabled );
$webp_configuration->toggle_module( $enable_webp );
// Update the CDN status for CDN changes.
if ( isset( $new_settings['cdn'] ) && $new_settings['cdn'] !== $stored_settings['cdn'] ) {
CDN_Controller::get_instance()->toggle_cdn( $new_settings['cdn'] );
// Keep the stored settings that aren't present in the incoming one.
$new_settings = array_merge( $stored_settings, $new_settings );
$settings_handler->set_setting( 'wp-smush-settings', $new_settings );
if ( ! empty( $sanitized_config['lazy_load'] ) ) {
$stored_lazy_load = $settings_handler->get_setting( 'wp-smush-lazy_load' );
// Save the defaults before applying the config if the current settings aren't set.
if ( empty( $stored_lazy_load ) ) {
$settings_handler->init_lazy_load_defaults();
$stored_lazy_load = $settings_handler->get_setting( 'wp-smush-lazy_load' );
// Keep the settings that are in use in this version.
foreach ( $sanitized_config['lazy_load'] as $key => $value ) {
if ( is_array( $value ) && is_array( $stored_lazy_load[ $key ] ) ) {
$sanitized_config['lazy_load'][ $key ] = array_intersect_key( $value, $stored_lazy_load[ $key ] );
// Keep the stored settings that aren't present in the incoming one.
$new_lazy_load = array_replace_recursive( $stored_lazy_load, $sanitized_config['lazy_load'] );
$settings_handler->set_setting( 'wp-smush-lazy_load', $new_lazy_load );
do_action( 'wp_smush_config_applied', $config_name );
// Skip onboarding if applying a config.
update_option( 'skip-smush-setup', true );
* Gets a new config array based on the current settings.
public function get_config_from_current() {
$settings = Settings::get_instance();
$stored_settings = $settings->get_setting( 'wp-smush-settings' );
$configs = array( 'settings' => $stored_settings );
if ( $stored_settings['resize'] ) {
$configs['resize_sizes'] = $settings->get_setting( 'wp-smush-resize_sizes' );
// Let's store this only for multisites.
$configs['networkwide'] = get_site_option( 'wp-smush-networkwide' );
// There's a site_option that handles this.
unset( $configs['settings']['networkwide'] );
unset( $configs['settings']['api_auth'] );
// These are unique per site. They shouldn't be used.
unset( $configs['settings']['bulk'] );
// Include the lazy load settings only when lazy load is enabled.
if ( ! empty( $configs['settings']['lazy_load'] ) ) {
$lazy_load_settings = $settings->get_setting( 'wp-smush-lazy_load' );
if ( ! empty( $lazy_load_settings ) ) {
// Exclude unique settings.
unset( $lazy_load_settings['animation']['placeholder'] );
unset( $lazy_load_settings['animation']['spinner'] );
unset( $lazy_load_settings['exclude-pages'] );
unset( $lazy_load_settings['exclude-classes'] );
if ( 'fadein' !== $lazy_load_settings['animation']['selected'] ) {
unset( $lazy_load_settings['animation']['fadein'] );
$configs['lazy_load'] = $lazy_load_settings;
// Exclude CDN fields if CDN is disabled.
if ( empty( $configs['settings']['cdn'] ) ) {
foreach ( $settings->get_cdn_fields() as $field ) {
if ( 'cdn' !== $field ) {
unset( $configs['settings'][ $field ] );
'strings' => $this->format_config_to_display( $configs ),
* Sanitizes the given config.
* @param array $config Config array to sanitize.
private function sanitize_config( $config ) {
if ( isset( $config['networkwide'] ) ) {
if ( ! is_array( $config['networkwide'] ) ) {
$sanitized['networkwide'] = sanitize_text_field( $config['networkwide'] );
$sanitized['networkwide'] = filter_var(
'options' => 'sanitize_text_field',
if ( ! empty( $config['settings'] ) ) {
$sanitized['settings'] = filter_var( $config['settings'], FILTER_VALIDATE_BOOLEAN, FILTER_REQUIRE_ARRAY );
if ( isset( $config['settings']['lossy'] ) ) {
$sanitized['settings']['lossy'] = $this->settings->sanitize_lossy_level( $config['settings']['lossy'] );
if ( isset( $config['resize_sizes'] ) ) {
if ( is_bool( $config['resize_sizes'] ) ) {
$sanitized['resize_sizes'] = $config['resize_sizes'];
$sanitized['resize_sizes'] = array(
'width' => (int) $config['resize_sizes']['width'],
'height' => (int) $config['resize_sizes']['height'],
if ( ! empty( $config['lazy_load'] ) ) {
'filter' => FILTER_VALIDATE_BOOLEAN,
'flags' => FILTER_REQUIRE_ARRAY + FILTER_NULL_ON_FAILURE,
'filter' => FILTER_VALIDATE_BOOLEAN,
'flags' => FILTER_REQUIRE_ARRAY,
'filter' => FILTER_CALLBACK,
'options' => 'sanitize_text_field',
'flags' => FILTER_REQUIRE_ARRAY,