,htaccess:bool}> */ public static function definitions() { return array( 'compression' => array( 'changes' => array( 'gzip_enabled' => true, 'brotli_enabled' => true, ), 'htaccess' => true, ), 'browser_cache' => array( 'changes' => array( 'caching_enabled' => true ), 'htaccess' => true, ), 'heartbeat' => array( 'changes' => array( 'heartbeat_admin_secs' => 120 ), 'htaccess' => false, ), 'emoji' => array( 'changes' => array( 'remove_emojis' => true ), 'htaccess' => false, ), 'query_strings' => array( 'changes' => array( 'remove_query_strings' => true ), 'htaccess' => false, ), ); } /** * @param mixed $fix * @return bool Ist das eine bekannte Massnahme? */ public static function is_known( $fix ) { return is_string( $fix ) && isset( self::definitions()[ $fix ] ); } /** * Einstellungen nach der Massnahme: der aktuelle Stand plus genau die Aenderungen. * * @param string $fix * @param array $current * @return array */ public static function apply_to_settings( $fix, array $current, array $context = array() ) { if ( ! self::is_known( $fix ) ) { return $current; } return array_replace( $current, self::definitions()[ $fix ]['changes'] ); } /** * Bedarf die Massnahme der .htaccess? */ public static function needs_htaccess( $fix ) { return self::is_known( $fix ) && self::definitions()[ $fix ]['htaccess']; } /** * Eine Zeile fuer die Bestaetigung: was wird geaendert? * * @param string $fix * @return string */ public static function describe( $fix ) { switch ( $fix ) { case 'compression': return __( 'This writes compression rules (gzip and, where available, Brotli) into your .htaccess file. A backup is made first, your site is tested afterwards, and the change is undone automatically if something goes wrong.', '360-orbit-speed-optimizer' ); case 'browser_cache': return __( 'This writes browser caching rules into your .htaccess file. A backup is made first, your site is tested afterwards, and the change is undone automatically if something goes wrong.', '360-orbit-speed-optimizer' ); case 'heartbeat': return __( 'This makes WordPress ask the server only every 120 seconds in the admin area. Nothing is written to files; you can change it back under Cleanup & Scripts.', '360-orbit-speed-optimizer' ); case 'emoji': return __( 'This switches off the emoji script. Nothing is written to files; you can change it back under Cleanup & Scripts.', '360-orbit-speed-optimizer' ); case 'query_strings': return __( 'This removes the version number from the addresses of style sheets and scripts. Nothing is written to files; you can change it back under Cleanup & Scripts.', '360-orbit-speed-optimizer' ); } return ''; } /** * Fuehrt die Massnahme aus. * * @param string $fix * @return array{ok:bool,kind:string,message:string} */ public static function apply( $fix ) { if ( ! self::is_known( $fix ) ) { return self::result( false, 'unknown', __( 'Unknown action.', '360-orbit-speed-optimizer' ) ); } if ( self::needs_htaccess( $fix ) ) { $environment = Wp360_Speed_Optimizer_Doctor_Environment::collect(); if ( empty( $environment['can_write_htaccess'] ) ) { return self::result( false, 'unsupported', __( 'This server does not allow the change from WordPress. Please ask your hosting provider (see the steps above).', '360-orbit-speed-optimizer' ) ); } } $context = array(); $before = Wp360_Speed_Optimizer_Settings::get(); $after = Wp360_Speed_Optimizer_Settings::save( self::apply_to_settings( $fix, $before, $context ) ); if ( ! self::needs_htaccess( $fix ) ) { return self::result( true, 'applied', __( 'Done. The setting was changed.', '360-orbit-speed-optimizer' ) ); } $written = Wp360_Speed_Optimizer_Htaccess::apply( $after ); $test = isset( $written['test'] ) && is_array( $written['test'] ) ? $written['test'] : array(); if ( empty( $written['written'] ) ) { Wp360_Speed_Optimizer_Settings::save( $before ); return self::result( false, 'not_writable', __( 'The .htaccess file could not be written, so nothing was changed.', '360-orbit-speed-optimizer' ) ); } if ( empty( $test['ok'] ) || ! empty( $test['rolled_back'] ) ) { Wp360_Speed_Optimizer_Settings::save( $before ); return self::result( false, 'rolled_back', __( 'The test of your site failed after the change, so it was undone automatically. Your site is as it was before.', '360-orbit-speed-optimizer' ) ); } return self::result( true, 'applied', __( 'Done. The change was written and your site passed the test.', '360-orbit-speed-optimizer' ) ); } /** * @return array{ok:bool,kind:string,message:string} */ private static function result( $ok, $kind, $message ) { return array( 'ok' => (bool) $ok, 'kind' => $kind, 'message' => $message, ); } }