on get_instance() { if ( null == self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Disable certain modules if there are conflicting plugins installed. * * @since 5.0.0 */ public function disable_modules() { $excluded = $this->get_excluded( $this->blocking_plugins ); foreach ( $excluded['excluded_modules'] as $module ) { $options = $this->modules[ $module ]['options']; array_map( function( $option ) { Options::disable_option( $option ); }, $options ); } delete_option( 'disable_conflicting_modules' ); } /** * Display notice for blocking plugins. * * @since 5.0.0 */ public function blocking_plugins_notice() { if ( 0 === (int) get_site_option( 'siteground_optimizer_blocking_plugins_notice', 1 ) || ! current_user_can( 'administrator' ) || ( is_multisite() && ! is_network_admin() ) ) { return; } $excluded = $this->get_excluded( $this->blocking_plugins ); // Bail if we don't have conflicted plugins. if ( empty( $excluded['conflicting_plugins'] ) ) { return; } $message = sprintf( __( 'Important message from Speed Optimizer by SiteGround plugin: We have detected that there is duplicate functionality with other plugins installed on your site: %1$s and have deactivated the following functions from our plugin: %2$s. If you wish to enable them, please do that from the Speed Optimizer by SiteGround config page.', 'sg-cachepress' ), implode( ', ', $excluded['conflicting_plugins'] ), implode( ', ', $this->get_modules_pretty_names( $excluded['excluded_modules'] ) ) ); printf( '

%2$s

', esc_attr( 'notice notice-error sg-optimizer-notice' ), $message, admin_url( 'admin-ajax.php?action=dismiss_blocking_plugins_notice' ) ); } /** * Display notice for cache plugins. * * @since 5.0.0 */ public function cache_plugins_notice() { if ( 0 === (int) get_site_option( 'siteground_optimizer_cache_plugins_notice', 1 ) || ! current_user_can( 'administrator' ) || ( is_multisite() && ! is_network_admin() ) ) { return; } $excluded = $this->get_excluded( $this->cache_plugins ); // Bail if we don't have conflicted plugins. if ( empty( $excluded['conflicting_plugins'] ) ) { return; } $message = sprintf( __( 'Important warning from Speed Optimizer by SiteGround plugin: We have detected that there is duplicate functionality with other plugins installed on your site: %s. Please note that having two plugins with the same functionality may actually decrease your site\'s performance and hurt your pages loading times so we recommend you to leave only one of the plugins active.', 'sg-cachepress' ), implode( ', ', $excluded['conflicting_plugins'] ) ); printf( '

%2$s

', esc_attr( 'notice notice-error sg-optimizer-notice' ), $message, admin_url( 'admin-ajax.php?action=dismiss_cache_plugins_notice' ) ); } /** * Return modules pretty names. * * @since 5.0.0 * * @param array $modules Excluded modules. * * @return array Excluded module pretty names. */ private function get_modules_pretty_names( $modules ) { $excluded_modules = array_intersect_key( $this->modules, array_flip( $modules ) ); $module_names = array(); foreach ( $excluded_modules as $module ) { $module_names[] = $module['title']; } return $module_names; } /** * Return list of modules that should be excluded. * * @since 5.0.0 * * @param array $plugins Conflicting plugins. * * @return array List of all excluded modules. */ public function get_excluded( $plugins ) { $excluded_modules = array(); $conflicting_plugins = array(); // Get all active plugins. $active_plugins = get_option( 'active_plugins', array() ); // Add network plugins to active plugins list. if ( is_multisite() ) { $network_plugins = get_site_option( 'active_sitewide_plugins', array() ); $active_plugins = array_merge( $active_plugins, array_flip( $network_plugins ) ); } foreach ( $active_plugins as $plugin ) { // Continue if it's not conflicting plugin. if ( ! array_key_exists( $plugin, $plugins ) ) { continue; } // Update excluded modules. $excluded_modules = array_merge( $excluded_modules, $plugins[ $plugin ]['modules'] ); $conflicting_plugins[] = $plugins[ $plugin ]['title']; } return array( 'conflicting_plugins' => array_unique( $conflicting_plugins ), 'excluded_modules' => array_unique( $excluded_modules ), ); } /** * Return the modules that should be active. * * @since 5.0.0 * * @return array Array of all modules that should be activated. */ public function get_active_modules() { if ( ! is_multisite() ) { return array_keys( $this->modules ); } return $this->apply_mu_disabled_modules( array_keys( $this->modules ) ); } /** * Return the tabs that should be active. * * @since 5.0.0 * * @param array $active_modules List of all active modules. * * @return array Array of all tabs that should be activated. */ public function get_active_tabs( $active_modules = array() ) { // Get active modules if they are not defined. if ( empty( $active_modules ) ) { $active_modules = $this->get_active_modules(); } // Build tabs data. foreach ( $this->tabs as $tab_slug => $tab ) { $active_tabs[ $tab_slug ] = __( $tab['title'], 'sg-cachepress' ); } // Return the tabs. if ( ! is_multisite() ) { // Return the tabs. return $active_tabs; } // Network admins have their own tabs. if ( is_network_admin() ) { return $this->multisite_tabs; } // Return active tabs for multisite. return array_intersect_key( $active_tabs, // Active tabs. array_filter( Multisite::get_permissions() ) // Get multisite permissions. ); } /** * Modify active modules for multisite. * * @since 5.0.0 * * @param array $active_modules All active modules. * * @return array Modified active modules. */ public function apply_mu_disabled_modules( $active_modules ) { // Disabled modules for multisite. $disabled_modules = array( 'single_site' => array( 'memcached', 'phpchecker', 'gzip', 'browser_cache', ), 'network_admin' => array( 'ssl', ), ); // Disable single site modules. if ( ! is_network_admin() ) { return array_diff( $active_modules, $disabled_modules['single_site'] ); } // Disable network admin modules. return array_diff( $active_modules, $disabled_modules['network_admin'] ); } /** * Get modules for the slider on plugin page. * * @since 5.5 * * @return array Array of modules. */ public function get_slider_modules() { $modules = array(); $whats_new = array(); // Get the new modules. $new_modules = get_option( 'siteground_optimizer_whats_new', array() ); // Add the new modules to the response if the optimization is not enabled. if ( ! empty( $new_modules ) ) { foreach ( $new_modules as $index => $card ) { if ( Options::is_enabled( 'siteground_optimizer_' . $card['optimization'] ) ) { continue; } $modules[] = $card; $whats_new[] = $index; } } // Merge and remove the empty cards. $cards = array_filter( array_merge( $modules, // Add "What's new" modules. array_merge( array( $this->get_optimizations(), // Add optimizations that are not yet enabled to the response. ), // Add the default card. array( array( 'type' => 'default', 'title' => __( 'Welcome to Speed Optimizer by SiteGround', 'sg-cachepress' ), 'text' => __( 'Get the best performance for your WordPress website with our optimization plugin. It handles caching, system settings, and all the necessary configurations for a blazing-fast website. With the Speed Optimizer by SiteGround enabled, you’re getting the very best from your hosting environment!', 'sg-cachepress' ), 'icon' => 'presentational-speed-caching', 'icon_color' => 'salmon', ), ) ) ) ); // Finally return the response. return array( 'whats_new' => $whats_new, 'cards' => $cards, ); } /** * Get optimizations which are currently disabled. * * @since 5.5. * * @return array Array of possible optimizations. */ public function get_optimizations() { $optimizations = array(); $count = 3; $is_siteground = Helper_Service::is_siteground(); // Order the modules. $keys = array_column( $this->modules, 'weight' ); array_multisort( $keys, SORT_DESC, $this->modules ); foreach ( $this->modules as $module ) { // Bail if there are no optimizations. if ( empty( $module['options'][0] ) ) { continue; } // Bail if the optimization is alredy enabled. if ( Options::is_enabled( $module['options'][0] ) ) { continue; } // Bail if the optimization is not important. if ( 0 == $module['weight'] ) { continue; } // Or if the optimization is for avalon servers only. if ( ! $is_siteground && ! empty( $module['avalon'] ) ) { continue; } // Add the optimization to the array. $optimizations[] = array( 'title' => $module['title'], 'text' => $module['text'], 'optimization' => str_replace( 'siteground_optimizer_', '', $module['options'][0] ), 'link' => $module['tab'], ); // Return the optimizations if we've reached the required quantity. if ( count( $optimizations ) == $count ) { return array( 'type' => 'optimizations', 'boxes' => $optimizations, ); } } return array(); } }