return true; } $bg_optimization = WP_Smush::get_instance()->core()->mod->bg_optimization; return 'background_email' === $field && $bg_optimization->can_use_background(); } /** * Getter method for settings fields. * * @since 3.2.2 * @return array */ public function get_settings_fields() { return $this->settings_fields; } /** * Getter method for lazy loading fields. * * @since 3.3.0 * @return array */ public function get_lazy_load_fields() { return $this->lazy_load_fields; } /** * Init settings. * * If there are no settings in the database, populate it with the defaults, if settings are present */ public function init() { $site_settings = array(); $global = $this->is_network_enabled(); // Always get global settings if global settings enabled or is in network admin. if ( true === $global || ( is_array( $global ) && is_network_admin() ) ) { $site_settings = get_site_option( 'wp-smush-settings', array() ); } if ( false === $global ) { $site_settings = get_option( 'wp-smush-settings', array() ); if ( ! is_multisite() ) { $this->settings = $site_settings; } // Make sure we're not missing any settings. $global_settings = get_site_option( 'wp-smush-settings', array() ); $site_settings = array_merge( $global_settings, $site_settings ); // Settings are taken from global settings. if ( ! empty( $global_settings ) ) { $site_settings['accessible_colors'] = isset( $global_settings['accessible_colors'] ) ? $global_settings['accessible_colors'] : $this->defaults['accessible_colors']; $site_settings['usage'] = isset( $global_settings['usage'] ) ? $global_settings['usage'] : $this->defaults['usage']; $site_settings['keep_data'] = isset( $global_settings['keep_data'] ) ? $global_settings['keep_data'] : $this->defaults['keep_data']; $site_settings['webp_mod'] = isset( $global_settings['webp_mod'] ) ? $global_settings['webp_mod'] : $this->defaults['webp_mod']; } } // Custom access enabled - combine settings from network with site settings. if ( is_array( $global ) ) { $network_settings = array_diff( $this->modules, $global ); $global_settings = get_site_option( 'wp-smush-settings', array() ); $site_settings = get_option( 'wp-smush-settings', array() ); foreach ( $network_settings as $key ) { // Remove values that are network wide from site settings. $site_settings = array_diff_key( $site_settings, array_flip( $this->{$key . '_fields'} ) ); // Take the values from network settings. $network_part = array_intersect_key( $global_settings, array_flip( $this->{$key . '_fields'} ) ); // And append them to the site settings. $site_settings = array_merge( $site_settings, $network_part ); } } if ( empty( $site_settings ) ) { $this->settings = $this->defaults; $this->set_setting( 'wp-smush-settings', $this->settings ); } else { $this->settings = wp_parse_args( $site_settings, $this->defaults ); } } /** * Checks whether the settings are applicable for the whole network/site or sitewise (multisite). */ public function is_network_enabled() { // If single site return true. if ( ! is_multisite() ) { return false; } if ( self::is_ajax_network_admin() ) { return true; } // Get directly from db. $network_enabled = get_site_option( self::SUBSITE_CONTROLS_OPTION_KEY ); if ( ! isset( $network_enabled ) || false === (bool) $network_enabled ) { return true; } if ( '1' === $network_enabled || true === $network_enabled ) { return false; } // Partial enabled. return $network_enabled; } /** * Check if user is able to access the page. * * @since 3.2.2 * * @param string|bool $module Check if a specific module is allowed. * @param bool $top_menu Is this a top level menu point? Defaults to a Smush sub page. * * @return bool|array Can access page or not. If custom access rules defined - return custom rules array. */ public static function can_access( $module = false, $top_menu = false ) { // Allow all access on single site installs. if ( ! is_multisite() ) { return true; } $access = get_site_option( self::SUBSITE_CONTROLS_OPTION_KEY ); // Check to if the settings update is network-wide or not ( only if in network admin ). $action = filter_input( INPUT_POST, 'action', FILTER_SANITIZE_SPECIAL_CHARS ); $is_network_admin = is_network_admin() || 'save_settings' === $action; if ( self::is_ajax_network_admin() ) { $is_network_admin = true; } if ( $is_network_admin && ! $access && $top_menu ) { return true; } if ( current_user_can( 'manage_options' ) && ( '1' === $access || 'custom' === $access && $top_menu ) ) { return true; } if ( is_array( $access ) && current_user_can( 'manage_options' ) ) { if ( ! $module ) { return $access; } if ( $is_network_admin && ! in_array( $module, $access, true ) ) { return true; } elseif ( ! $is_network_admin && in_array( $module, $access, true ) ) { return true; } return false; } return false; } /** * 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->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->settings[ $setting ] = $value; $this->set_setting( 'wp-smush-settings', $this->settings ); } /** * 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; } $global = $this->is_network_enabled(); if ( $global && ! is_array( $global ) ) { return get_site_option( $name, $default ); } // Fallback to network settings. $settings = get_option( $name, $default ); // TODO: this fallback is dangerous! Make sure that a proper false option is not replaced. return $settings ? $settings : get_site_option( $name, $default ); } /** * 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; } $global = $this->is_network_enabled(); return $global && ! is_array( $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_enabled(); return $global && ! is_array( $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-settings' ); $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( 'skip-smush-setup' ); $this->delete_setting( 'wp-smush-hide-tutorials' ); delete_option( 'wp-smush-png2jpg-rewrite-rules-flushed' ); delete_option( 'wp_smush_scan_slice_size' ); wp_send_json_success(); } /** * 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 ( '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 ); } } $settings = $this->get(); foreach ( $new_settings as $setting => $value ) { if ( ! isset( $settings[ $setting ] ) ) { continue; } $settings[ $setting ] = $value; } $this->set_setting( 'wp-smush-settings', $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 ( ! WP_Smush::get_instance()->core()->mod->cdn->get_status() ) { $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 ); } /** * 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_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_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 = $this->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' ); } 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' ), ); } }