;
$site_settings = $this->get_site_settings();
foreach ( $new_settings as $setting => $value ) {
if ( isset( $site_settings[ $setting ] ) ) {
$site_settings[ $setting ] = $value;
}
}
$this->update_site_option( self::SETTINGS_KEY, $site_settings );
$this->reset_cache_site_settings();
}
public function get_site_settings() {
if ( empty( $this->settings ) ) {
$this->settings = $this->prepare_site_settings();
}
return $this->settings;
}
private function prepare_site_settings() {
$is_multisite = is_multisite();
if ( ! $is_multisite ) {
// Make sure the new default settings are included into the old configs.
return wp_parse_args( get_option( self::SETTINGS_KEY, array() ), $this->defaults );
}
$network_settings = get_site_option( self::SETTINGS_KEY, array() );
$network_settings = wp_parse_args( $network_settings, $this->defaults );
if ( $this->is_network_enabled() ) {
return $network_settings;
}
$subsite_modules = $this->get_activated_subsite_pages();
$network_modules = array_diff( $this->modules, $subsite_modules );
$subsite_settings = get_option( self::SETTINGS_KEY, array() );
foreach ( $network_modules as $key ) {
// Remove values that are network wide from subsite settings.
$subsite_settings = array_diff_key( $subsite_settings, array_flip( $this->{$key . '_fields'} ) );
}
// And append subsite settings to the site settings.
$network_settings = array_merge( $network_settings, $subsite_settings );
return $network_settings;
}
/**
* Getter method for $settings.
*
* @since 3.0
*
* @param string $setting Setting to get. Default: get all settings.
*
* @return array|bool Return either a setting value or array of settings.
*/
public function get( $setting = '' ) {
$settings = $this->get_site_settings();
if ( ! empty( $setting ) ) {
return isset( $settings[ $setting ] ) ? $settings[ $setting ] : false;
}
return $settings;
}
/**
* Setter method for $settings.
*
* @since 3.0
*
* @param string $setting Setting to update.
* @param bool $value Value to set. Default: false.
*/
public function set( $setting = '', $value = false ) {
if ( empty( $setting ) ) {
return;
}
$this->update_site_settings( array( $setting => $value ) );
}
/**
* Get all Smush settings, based on if network settings are enabled or not.
*
* @param string $name Setting to fetch.
* @param mixed $default Default value.
*
* @return bool|mixed
*/
public function get_setting( $name = '', $default = false ) {
if ( empty( $name ) ) {
return false;
}
if ( ! is_multisite() ) {
return get_option( $name, $default );
}
$global = $this->is_network_setting( $name );
$global_settings = get_site_option( $name, $default );
if ( $global ) {
return $global_settings;
}
$subsite_settings = get_option( $name, $default );
$subsite_settings = false !== $subsite_settings ? $subsite_settings : $global_settings;
return $subsite_settings;
}
/**
* Update value for given setting key
*
* @param string $name Key.
* @param mixed $value Value.
*
* @return bool If the setting was updated or not
*/
public function set_setting( $name = '', $value = '' ) {
if ( empty( $name ) ) {
return false;
}
if ( self::SETTINGS_KEY === $name ) {
return $this->update_site_settings( $value );
}
return $this->update_site_option( $name, $value );
}
private function update_site_option( $name, $value ) {
$global = $this->is_network_setting( $name );
return $global ? update_site_option( $name, $value ) : update_option( $name, $value );
}
/**
* Delete the given key name.
*
* @param string $name Key.
*
* @return bool If the setting was updated or not
*/
public function delete_setting( $name = '' ) {
if ( empty( $name ) ) {
return false;
}
$global = $this->is_network_setting( $name );
return $global ? delete_site_option( $name ) : delete_option( $name );
}
/**
* Reset settings to defaults.
*
* @since 3.2.0
*/
public function reset() {
check_ajax_referer( 'wp_smush_reset' );
// Check capability.
if ( ! Helper::is_user_allowed( 'manage_options' ) ) {
wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 );
}
delete_site_option( self::SUBSITE_CONTROLS_OPTION_KEY );
delete_site_option( 'wp-smush-webp_hide_wizard' );
delete_site_option( 'wp-smush-preset_configs' );
$this->delete_setting( 'wp-smush-image_sizes' );
$this->delete_setting( 'wp-smush-resize_sizes' );
$this->delete_setting( 'wp-smush-cdn_status' );
$this->delete_setting( 'wp-smush-lazy_load' );
$this->delete_setting( 'wp-smush-hide-tutorials' );
delete_option( 'wp-smush-png2jpg-rewrite-rules-flushed' );
delete_option( 'wp_smush_scan_slice_size' );
// We used update_option for skip-smush-setup,
// so let's reset it with delete_option instead of delete_site_option for MU site.
delete_option( 'skip-smush-setup' );
// Reset site settings.
$this->reset_site_settings();
wp_send_json_success();
}
private function reset_site_settings() {
$this->delete_setting( self::SETTINGS_KEY );
$this->reset_cache_site_settings();
// The action wp_smush_settings_updated only triggers after option is updated, does not trigger on add_(site_)option.
// So to support this, we need to add the default option first.
$this->add_default_site_settings();
}
private function add_default_site_settings() {
$this->update_site_settings( $this->defaults );
}
public function initial_default_site_settings() {
if ( false === $this->get_setting( self::SETTINGS_KEY, false ) ) {
$this->add_default_site_settings();
}
}
/**
* Save settings.
*
* @since 3.8.6
*/
public function save_settings() {
check_ajax_referer( 'wp-smush-ajax' );
if ( ! Helper::is_user_allowed( 'manage_options' ) ) {
wp_send_json_error(
array(
'message' => esc_html__( "You don't have permission to do this.", 'wp-smushit' ),
)
);
}
// Delete S3 alert flag, if S3 option is disabled again.
if ( ! isset( $_POST['wp-smush-s3'] ) && isset( $settings['integration']['s3'] ) && $settings['integration']['s3'] ) {
delete_site_option( 'wp-smush-hide_s3support_alert' );
}
$page = filter_input( INPUT_POST, 'page', FILTER_SANITIZE_SPECIAL_CHARS );
if ( ! isset( $page ) ) {
wp_send_json_error(
array( 'message' => __( 'The page these settings belong to is missing.', 'wp-smushit' ) )
);
}
$new_settings = array();
$status = array(
'is_outdated_stats' => false,
);
if ( 'bulk' === $page ) {
foreach ( $this->get_bulk_fields() as $field ) {
// Skip the module enable/disable option.
if ( 'bulk' === $field ) {
continue;
}
if ( 'lossy' == $field ) {
$new_settings['lossy'] = filter_input( INPUT_POST, $field, FILTER_SANITIZE_NUMBER_INT );
continue;
}
$new_settings[ $field ] = filter_input( INPUT_POST, $field, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
}
$this->parse_bulk_settings();
}
if ( 'lazy-load' === $page ) {
$this->parse_lazy_load_settings();
}
if ( 'cdn' === $page ) {
foreach ( $this->get_cdn_fields() as $field ) {
// Skip the module enable/disable option.
if ( 'cdn' === $field ) {
continue;
}
$new_settings[ $field ] = filter_input( INPUT_POST, $field, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
}
$this->parse_cdn_settings();
}
if ( 'webp' === $page ) {
$this->parse_webp_settings();
}
if ( 'integrations' === $page ) {
foreach ( $this->get_integrations_fields() as $field ) {
$new_settings[ $field ] = filter_input( INPUT_POST, $field, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
}
}
if ( 'settings' === $page ) {
$tab = filter_input( INPUT_POST, 'tab', FILTER_SANITIZE_SPECIAL_CHARS );
if ( ! isset( $tab ) ) {
wp_send_json_error(
array( 'message' => __( 'The tab these settings belong to is missing.', 'wp-smushit' ) )
);
}
if ( 'general' === $tab ) {
$new_settings['usage'] = filter_input( INPUT_POST, 'usage', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
$new_settings['detection'] = filter_input( INPUT_POST, 'detection', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
}
if ( 'permissions' === $tab ) {
$new_settings['networkwide'] = $this->parse_access_settings();
}
if ( 'data' === $tab ) {
$new_settings['keep_data'] = filter_input( INPUT_POST, 'keep_data', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
}
if ( 'accessibility' === $tab ) {
$new_settings['accessible_colors'] = filter_input( INPUT_POST, 'accessible_colors', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
}
}
$this->update_site_settings( $new_settings );
$status['is_outdated_stats'] = Global_Stats::get()->is_outdated();
wp_send_json_success( $status );
}
/**
* Parse bulk Smush specific settings.
*
* Nonce processed in parent method.
*
* @since 3.2.0 Moved from save method.
*/
private function parse_bulk_settings() {
// Save the selected image sizes.
if ( isset( $_POST['wp-smush-auto-image-sizes'] ) && 'all' === $_POST['wp-smush-auto-image-sizes'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$this->delete_setting( 'wp-smush-image_sizes' );
} else {
if ( ! isset( $_POST['wp-smush-image_sizes'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$image_sizes = array();
} else {
$image_sizes = array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['wp-smush-image_sizes'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
$this->set_setting( 'wp-smush-image_sizes', $image_sizes );
}
// Update Resize width and height settings if set.
$resize_sizes['width'] = isset( $_POST['wp-smush-resize_width'] ) ? (int) $_POST['wp-smush-resize_width'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$resize_sizes['height'] = isset( $_POST['wp-smush-resize_height'] ) ? (int) $_POST['wp-smush-resize_height'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$this->set_setting( 'wp-smush-resize_sizes', $resize_sizes );
}
/**
* Parse CDN specific settings.
*
* @since 3.2.0 Moved from save method.
*/
private function parse_cdn_settings() {
// $status = connect to CDN.
if ( ! CDN_Helper::get_instance()->is_cdn_active() ) {
$response = WP_Smush::get_instance()->api()->enable();
// Probably an exponential back-off.
if ( is_wp_error( $response ) ) {
sleep( 1 ); // This is needed so we don't trigger the 597 API response.
$response = WP_Smush::get_instance()->api()->enable( true );
}
// Logged error inside API.
if ( ! is_wp_error( $response ) ) {
$response = json_decode( $response['body'] );
$this->set_setting( 'wp-smush-cdn_status', $response->data );
}
}
}
/**
* Parse lazy loading specific settings.
*
* @since 3.2.0
*/
private function parse_lazy_load_settings() {
$previous_settings = $this->get_setting( 'wp-smush-lazy_load' );
$args = array(
'format' => array(
'filter' => FILTER_VALIDATE_BOOLEAN,
'flags' => FILTER_REQUIRE_ARRAY,
),
'output' => array(
'filter' => FILTER_VALIDATE_BOOLEAN,
'flags' => FILTER_REQUIRE_ARRAY,
),
'include' => array(
'filter' => FILTER_VALIDATE_BOOLEAN,
'flags' => FILTER_REQUIRE_ARRAY,
),
'exclude-pages' => array(
'filter' => FILTER_CALLBACK,
'options' => 'sanitize_text_field',
),
'exclude-classes' => array(
'filter' => FILTER_CALLBACK,
'options' => 'sanitize_text_field',
),
'footer' => FILTER_VALIDATE_BOOLEAN,
'native' => FILTER_VALIDATE_BOOLEAN,
'noscript' => FILTER_VALIDATE_BOOLEAN,
);
$settings = filter_input_array( INPUT_POST, $args );
// Verify lazyload.
if ( ! empty( $_POST['animation'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$settings['animation'] = map_deep( wp_unslash( $_POST['animation'] ), 'sanitize_text_field' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
// Fade-in settings.
$settings['animation']['fadein']['duration'] = 0;
if ( isset( $settings['animation']['duration'] ) ) {
$settings['animation']['fadein']['duration'] = absint( $settings['animation']['duration'] );
unset( $settings['animation']['duration'] );
}
$settings['animation']['fadein']['delay'] = 0;
if ( isset( $settings['animation']['delay'] ) ) {
$settings['animation']['fadein']['delay'] = absint( $settings['animation']['delay'] );
unset( $settings['animation']['delay'] );
}
/**
* Spinner and placeholder settings.
*/
$items = array( 'spinner', 'placeholder' );
foreach ( $items as $item ) {
$settings['animation'][ $item ]['selected'] = isset( $settings['animation'][ "$item-icon" ] ) ? $settings['animation'][ "$item-icon" ] : 1;
unset( $settings['animation'][ "$item-icon" ] );
// Custom spinners.
if ( ! isset( $previous_settings['animation'][ $item ]['custom'] ) || ! is_array( $previous_settings['animation'][ $item ]['custom'] ) ) {
$settings['animation'][ $item ]['custom'] = array();
} else {
// Remove empty values.
$settings['animation'][ $item ]['custom'] = array_filter( $previous_settings['animation'][ $item ]['custom'] );
}
// Add uploaded custom spinner.
if ( isset( $settings['animation'][ "custom-$item" ] ) ) {
if ( ! empty( $settings['animation'][ "custom-$item" ] ) && ! in_array( $settings['animation'][ "custom-$item" ], $settings['animation'][ $item ]['custom'], true ) ) {
$settings['animation'][ $item ]['custom'][] = $settings['animation'][ "custom-$item" ];
$settings['animation'][ $item ]['selected'] = $settings['animation'][ "custom-$item" ];
}
unset( $settings['animation'][ "custom-$item" ] );
}
}
// Custom color for placeholder.
if ( ! isset( $settings['animation']['color'] ) ) {
$settings['animation']['placeholder']['color'] = $previous_settings['animation']['placeholder']['color'];
} else {
$settings['animation']['placeholder']['color'] = $settings['animation']['color'];
unset( $settings['animation']['color'] );
}
/**
* Exclusion rules.
*/
// Convert to array.
if ( ! empty( $settings['exclude-pages'] ) ) {
$settings['exclude-pages'] = preg_split( '/[\r\n\t ]+/', $settings['exclude-pages'] );
} else {
$settings['exclude-pages'] = array();
}
if ( ! empty( $settings['exclude-classes'] ) ) {
$settings['exclude-classes'] = preg_split( '/[\r\n\t ]+/', $settings['exclude-classes'] );
} else {
$settings['exclude-classes'] = array();
}
$this->set_setting( 'wp-smush-lazy_load', $settings );
}
private function parse_webp_settings() {
$webp_fallback_active = filter_input( INPUT_POST, 'webp-fallback', FILTER_VALIDATE_BOOLEAN );
$this->set( 'webp_fallback', ! empty( $webp_fallback_active ) );
}
/**
* Parse access control settings on multisite.
*
* @since 3.2.2
*
* @return mixed
*/
private function parse_access_settings() {
$current_value = get_site_option( self::SUBSITE_CONTROLS_OPTION_KEY );
$new_value = filter_input( INPUT_POST, 'wp-smush-subsite-access', FILTER_SANITIZE_SPECIAL_CHARS );
$access = filter_input( INPUT_POST, 'wp-smush-access', FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY );
if ( 'custom' === $new_value ) {
$new_value = $access;
}
if ( $current_value !== $new_value ) {
update_site_option( self::SUBSITE_CONTROLS_OPTION_KEY, $new_value );
}
return $new_value;
}
/**
* Apply a default configuration to lazy loading on first activation.
*
* @since 3.2.0
*/
public function init_lazy_load_defaults() {
$defaults = array(
'format' => array(
'jpeg' => true,
'png' => true,
'webp' => true,
'gif' => true,
'svg' => true,
'iframe' => true,
),
'output' => array(
'content' => true,
'widgets' => true,
'thumbnails' => true,
'gravatars' => true,
),
'animation' => array(
'selected' => 'fadein', // Accepts: fadein, spinner, placeholder, false.
'fadein' => array(
'duration' => 400,
'delay' => 0,
),
'spinner' => array(
'selected' => 1,
'custom' => array(),
),
'placeholder' => array(
'selected' => 1,
'custom' => array(),
'color' => '#F3F3F3',
),
),
'include' => array(
'frontpage' => true,
'home' => true,
'page' => true,
'single' => true,
'archive' => true,
'category' => true,
'tag' => true,
),
'exclude-pages' => array(),
'exclude-classes' => array(),
'footer' => true,
'native' => false,
'noscript' => false,
);
$this->set_setting( 'wp-smush-lazy_load', $defaults );
}
/**
* Check if in network admin.
*
* The is_network_admin() check does not work in ajax calls.
*
* @since 3.10.3
*
* @return bool
*/
public static function is_ajax_network_admin() {
return defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_SERVER['HTTP_REFERER'] ) && preg_match( '#^' . network_admin_url() . '#i', wp_unslash( $_SERVER['HTTP_REFERER'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
}
public function is_optimize_original_images_active() {
return ! empty( self::get_instance()->get( 'original' ) );
}
public function is_png2jpg_module_active() {
return $this->is_module_active( 'png_to_jpg' );
}
public function is_webp_module_active() {
return $this->is_module_active( 'webp_mod' );
}
public function is_resize_module_active() {
return $this->is_module_active( 'resize' );
}
public function is_backup_active() {
return $this->is_module_active( 'backup' );
}
public function is_s3_active() {
return $this->is_module_active( 's3' );
}
public function is_cdn_webp_conversion_active() {
return $this->is_cdn_active()
&& ! empty( self::get_instance()->get( 'webp' ) );
}
public function is_webp_direct_conversion_active() {
return $this->is_webp_module_active()
&& ! empty( self::get_instance()->get( 'webp_direct_conversion' ) );
}
public function is_automatic_compression_active() {
return self::get_instance()->get( 'auto' );
}
public function is_cdn_active() {
return $this->is_module_active( 'cdn' );
}
public function is_webp_fallback_active() {
return $this->is_webp_module_active()
&& ! empty( self::get_instance()->get( 'webp_fallback' ) );
}
public function is_lazyload_active() {
return self::get_instance()->get( 'lazy_load' );
}
public function is_module_active( $module ) {
$pro_modules = array(
'cdn',
'png_to_jpg',
'webp_mod',
's3',
'ultra',
);
$module_active = self::get_instance()->get( $module );
if ( in_array( $module, $pro_modules, true ) ) {
$module_active = $module_active && WP_Smush::is_pro();
}
return $module_active;
}
public function get_lossy_level_setting() {
$current_level = self::get_instance()->get( 'lossy' );
return $this->sanitize_lossy_level( $current_level );
}
public function sanitize_lossy_level( $lossy_level ) {
$highest_level = $this->get_highest_lossy_level();
if ( $lossy_level > $highest_level ) {
return $highest_level;
}
if ( $lossy_level > self::LEVEL_LOSSLESS ) {
return (int) $lossy_level;
}
return self::LEVEL_LOSSLESS;
}
public function get_highest_lossy_level() {
if( WP_Smush::is_pro() ) {
return self::LEVEL_ULTRA_LOSSY;
}
return self::LEVEL_SUPER_LOSSY;
}
public function get_current_lossy_level_label() {
$current_level = $this->get_lossy_level_setting();
return $this->get_lossy_level_label( $current_level );
}
public function get_lossy_level_label( $lossy_level ) {
$smush_modes = array(
self::LEVEL_LOSSLESS => __( 'Basic', 'wp-smushit' ),
self::LEVEL_SUPER_LOSSY => __( 'Super', 'wp-smushit' ),
self::LEVEL_ULTRA_LOSSY => __( 'Ultra', 'wp-smushit' ),
);
if ( ! isset( $smush_modes[ $lossy_level ] ) ) {
$lossy_level = self::LEVEL_LOSSLESS;
}
return $smush_modes[ $lossy_level ];
}
public function get_large_file_cutoff() {
return apply_filters( 'wp_smush_large_file_cut_off', 32 * 1024 * 1024 );
}
public function has_bulk_smush_page() {
return $this->is_page_active( 'bulk' );
}
public function has_cdn_page() {
return $this->is_page_active( 'cdn' );
}
public function has_webp_page() {
return $this->is_page_active( 'webp' );
}
private function is_page_active( $page_slug ) {
if ( ! is_multisite() ) {
return true;
}
$is_page_active_on_subsite = in_array( $page_slug, $this->get_activated_subsite_pages(), true );
if ( is_network_admin() ) {
return ! $is_page_active_on_subsite;
}
return $is_page_active_on_subsite;
}
/**
* @return array
*/
private function get_activated_subsite_pages() {
if ( is_array( $this->activated_subsite_pages ) ) {
return $this->activated_subsite_pages;
}
$this->activated_subsite_pages = array();
$subsite_controls = get_site_option( self::SUBSITE_CONTROLS_OPTION_KEY );
if ( empty( $subsite_controls ) ) {
return $this->activated_subsite_pages;
}
$this->activated_subsite_pages = array_keys( $this->get_subsite_page_modules() );
if ( is_array( $subsite_controls ) ) {
$this->activated_subsite_pages = $subsite_controls;
}
return $this->activated_subsite_pages;
}
private function get_subsite_page_modules() {
return array(
'bulk' => __( 'Bulk Smush', 'wp-smushit' ),
'integrations' => __( 'Integrations', 'wp-smushit' ),
'lazy_load' => __( 'Lazy Load', 'wp-smushit' ),
'cdn' => __( 'CDN', 'wp-smushit' ),
'tutorials' => __( 'Tutorials', 'wp-smushit' ),
);
}
}
Fatal error: Uncaught Error: Class 'Smush\Core\Settings' not found in /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/core/modules/class-abstract-module.php:53
Stack trace:
#0 /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/core/class-modules.php(124): Smush\Core\Modules\Abstract_Module->__construct()
#1 /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/core/class-core.php(157): Smush\Core\Modules->__construct()
#2 /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/core/class-stats.php(104): Smush\Core\Core->init()
#3 /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/wp-smush.php(359): Smush\Core\Stats->__construct()
#4 /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/wp-smush.php(279): WP_Smush->init()
#5 /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/wp-smush.php(257): WP_Smush->__construct()
#6 /home/deltakeprico/public_html/wp-includes/class-wp-hook.php(324): WP_Smush::get_instance('')
#7 /home/deltakeprico/public_html/wp-includes/clas in /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/core/modules/class-abstract-module.php on line 53