get_boolean( 'minify.enabled' ); $engine = $config->get_string( 'minify.engine' ); $rewrite = $config->get_boolean( 'minify.rewrite' ); $this->fix_folders( $config, $exs ); if ( $config->get_boolean( 'config.check' ) || $force_all_checks ) { if ( $minify_enabled && Util_Rule::can_check_rules() && $rewrite ) { $this->rules_core_add( $config, $exs ); } else { $this->rules_core_remove( $exs ); } if ( $minify_enabled && 'file' === $engine ) { $this->rules_cache_add( $config, $exs ); } else { $this->rules_cache_remove( $exs ); } } // if no errors so far - check if rewrite actually works. if ( count( $exs->exceptions() ) <= 0 ) { try { if ( $minify_enabled && $rewrite && $config->get_boolean( 'minify.debug' ) ) { $this->verify_rewrite_working(); } } catch ( \Exception $ex ) { $exs->push( $ex ); } if ( $minify_enabled ) { $this->verify_engine_working( $config, $exs ); } } if ( count( $exs->exceptions() ) > 0 ) { throw $exs; } } /** * Handles configuration adjustments triggered by events. * * @param Config $config W3TC Config containing Minify settings. * @param string $event The event that triggered this function. * @param Config $old_config Optional previous W3TC Config for comparison. * * @return void */ public function fix_on_event( $config, $event, $old_config = null ) { $minify_enabled = $config->get_boolean( 'minify.enabled' ); $engine = $config->get_string( 'minify.engine' ); // Schedules events. if ( $minify_enabled && ( 'file' === $engine || 'file_generic' === $engine ) ) { $new_interval = $config->get_integer( 'minify.file.gc' ); $old_interval = $old_config ? $old_config->get_integer( 'minify.file.gc' ) : -1; if ( null !== $old_config && $new_interval !== $old_interval ) { $this->unschedule_gc(); } if ( ! wp_next_scheduled( 'w3_minify_cleanup' ) ) { wp_schedule_event( time(), 'w3_minify_cleanup', 'w3_minify_cleanup' ); } } else { $this->unschedule_gc(); } } /** * Executes necessary clean-up actions after the plugin is deactivated. * * @return void * * @throws \Util_Environment_Exceptions If removal of rules or unscheduling fails. */ public function fix_after_deactivation() { $exs = new Util_Environment_Exceptions(); $this->rules_core_remove( $exs ); $this->rules_cache_remove( $exs ); $this->unschedule_gc(); $this->unschedule_purge_wpcron(); if ( count( $exs->exceptions() ) > 0 ) { throw $exs; } } /** * Retrieves the required rewrite rules based on the current configuration. * * @param Config $config W3TC Config containing Minify settings. * * @return array|null Array of rewrite rules or null if Minify is disabled. */ public function get_required_rules( $config ) { if ( ! $config->get_boolean( 'minify.enabled' ) ) { return null; } $rewrite_rules = array(); if ( 'file' === $config->get_string( 'minify.engine' ) ) { $minify_rules_cache_path = Util_Rule::get_minify_rules_cache_path(); $rewrite_rules[] = array( 'filename' => $minify_rules_cache_path, 'content' => $this->rules_cache_generate( $config ), ); } $minify_rules_core_path = Util_Rule::get_minify_rules_core_path(); $rewrite_rules[] = array( 'filename' => $minify_rules_core_path, 'content' => $this->rules_core_generate( $config ), 'priority' => 1000, ); return $rewrite_rules; } /** * Fixes folder-related issues for the Minify engine. * * @param Config $config W3TC Config containing Minify settings. * @param object $exs Collection of exceptions encountered during the process. * * @return void */ private function fix_folders( $config, $exs ) { // folder that we delete if exists and not writeable. if ( $config->get_boolean( 'minify.enabled' ) && 'file' === $config->get_string( 'minify.engine' ) ) { $dir = W3TC_CACHE_MINIFY_DIR; try { if ( file_exists( $dir ) && ! is_writeable( $dir ) ) { Util_WpFile::delete_folder( $dir, '', isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '' ); } } catch ( Util_WpFile_FilesystemRmdirException $ex ) { $exs->push( $ex ); } } } /** * Verifies that the Minify engine is working correctly. * * @param Config $config W3TC Config containing Minify settings. * @param object $exs Collection of exceptions encountered during the process. * * @return void */ private function verify_engine_working( $config, $exs ) { $minifiers_errors = array(); if ( 'yuijs' === $config->get_string( 'minify.js.engine' ) ) { $path_java = $config->get_string( 'minify.yuijs.path.java' ); $path_jar = $config->get_string( 'minify.yuijs.path.jar' ); if ( ! file_exists( $path_java ) ) { $minifiers_errors[] = sprintf( 'YUI Compressor (JS): JAVA executable path was not found. The default minifier JSMin will be used instead.' ); } elseif ( ! file_exists( $path_jar ) ) { $minifiers_errors[] = sprintf( 'YUI Compressor (JS): JAR file path was not found. The default minifier JSMin will be used instead.' ); } } if ( 'yuicss' === $config->get_string( 'minify.css.engine' ) ) { $path_java = $config->get_string( 'minify.yuicss.path.java' ); $path_jar = $config->get_string( 'minify.yuicss.path.jar' ); if ( ! file_exists( $path_java ) ) { $minifiers_errors[] = sprintf( 'YUI Compressor (CSS): JAVA executable path was not found. The default CSS minifier will be used instead.' ); } elseif ( ! file_exists( $path_jar ) ) { $minifiers_errors[] = sprintf( 'YUI Compressor (CSS): JAR file path was not found. The default CSS minifier will be used instead.' ); } } if ( 'ccjs' === $config->get_string( 'minify.js.engine' ) ) { $path_java = $config->get_string( 'minify.ccjs.path.java' ); $path_jar = $config->get_string( 'minify.ccjs.path.jar' ); if ( ! file_exists( $path_java ) ) { $minifiers_errors[] = sprintf( 'Closure Compiler: JAVA executable path was not found. The default minifier JSMin will be used instead.' ); } elseif ( ! file_exists( $path_jar ) ) { $minifiers_errors[] = sprintf( 'Closure Compiler: JAR file path was not found. The default minifier JSMin will be used instead.' ); } } if ( count( $minifiers_errors ) ) { $minify_error = 'The following minifiers cannot be found or are no longer working:
This message will automatically disappear once the issue is resolved.';
$exs->push( new Util_Environment_Exception( $minify_error ) );
}
}
/**
* Verifies that URL rewriting functionality for Minify is working.
*
* @return void
*
* @throws \Util_Environment_Exception If URL rewriting fails.
*/
private function verify_rewrite_working() {
$url = Minify_Core::minified_url( wp_rand() . 'w3tc_rewrite_test.css' );
$result = $this->test_rewrite( $url );
if ( 'ok' !== $result ) {
$home_url = get_home_url();
$tech_message =
( Util_Environment::is_nginx() ? 'nginx configuration file' : '.htaccess file' ) .
' contains rules to rewrite url ' . $url . '. If handled by plugin, it returns "Minify OK"' .
' message.
The plugin made a request to ' . $url . ' but received:
' . $result .
'
instead of "Minify OK" response.
';
$error = 'W3 Total Cache error:It appears Minify ' .
'URL rewriting is not working. ';
if ( Util_Environment::is_nginx() ) {
$error .= 'Please verify that all configuration files are included in the configuration file ' .
'(and that you have reloaded / restarted nginx).';
} else {
$error .= 'Please verify that the server configuration allows .htaccess';
}
$error .= '
Unfortunately minification will not function without custom rewrite rules. ' .
'Please ask your server administrator for assistance. Also refer to the install page for the rules for your server.';
throw new Util_Environment_Exception(
esc_html( $error ),
esc_html( $tech_message )
);
}
}
/**
* Tests the rewrite rules by sending a request to a test Minify URL.
*
* @param string $url The URL to test for rewriting functionality.
*
* @return string 'ok' if successful, or an error message otherwise.
*/
private function test_rewrite( $url ) {
$key = sprintf( 'w3tc_rewrite_test_%s', substr( md5( $url ), 0, 16 ) );
$result = get_site_transient( $key );
if ( 'ok' !== $result ) {
$response = Util_Http::get( $url );
$is_ok = (
! is_wp_error( $response ) &&
200 === $response['response']['code'] &&
'Minify OK' === trim( $response['body'] )
);
if ( $is_ok ) {
$result = 'ok';
} elseif ( is_wp_error( $response ) ) {
$result = $response->get_error_message();
} else {
$result = '
' . print_r( $response['response'], true ) . ''; // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r } set_site_transient( $key, $result, 30 ); } return $result; } /** * Unschedules the garbage collection event for Minify files. * * @return void */ private function unschedule_gc() { if ( wp_next_scheduled( 'w3_minify_cleanup' ) ) { wp_clear_scheduled_hook( 'w3_minify_cleanup' ); } } /** * Unschedules the WordPress cron event for purging Minify cache. * * @since 2.8.0 * * @return void */ private function unschedule_purge_wpcron() { if ( wp_next_scheduled( 'w3tc_minifycache_purge_wpcron' ) ) { wp_clear_scheduled_hook( 'w3tc_minifycache_purge_wpcron' ); } } /** * Adds core rewrite rules for Minify. * * @param Config $config W3TC Config containing Minify settings. * @param object $exs Collection of exceptions encountered during the process. * * @return void */ private function rules_core_add( $config, $exs ) { Util_Rule::add_rules( $exs, Util_Rule::get_minify_rules_core_path(), $this->rules_core_generate( $config ), W3TC_MARKER_BEGIN_MINIFY_CORE, W3TC_MARKER_END_MINIFY_CORE, array( W3TC_MARKER_BEGIN_PGCACHE_CORE => 0, W3TC_MARKER_BEGIN_WORDPRESS => 0, W3TC_MARKER_END_BROWSERCACHE_CACHE => strlen( W3TC_MARKER_END_BROWSERCACHE_CACHE ) + 1, W3TC_MARKER_END_PGCACHE_CACHE => strlen( W3TC_MARKER_END_PGCACHE_CACHE ) + 1, W3TC_MARKER_END_MINIFY_CACHE => strlen( W3TC_MARKER_END_MINIFY_CACHE ) + 1, ) ); } /** * Removes core rewrite rules for Minify. * * @param object $exs Collection of exceptions encountered during the process. * * @return void */ private function rules_core_remove( $exs ) { // no need to remove rules for apache - its in cache .htaccess file. if ( ! Util_Environment::is_nginx() ) { return; } Util_Rule::remove_rules( $exs, Util_Rule::get_minify_rules_core_path(), W3TC_MARKER_BEGIN_MINIFY_CORE, W3TC_MARKER_END_MINIFY_CORE ); } /** * Generates the core rewrite rules for Minify based on the server environment. * * @param Config $config W3TC Config containing Minify settings. * * @return string The generated rules. */ public function rules_core_generate( $config ) { switch ( true ) { case Util_Environment::is_apache(): case Util_Environment::is_litespeed(): return $this->rules_core_generate_apache( $config ); case Util_Environment::is_nginx(): return $this->rules_core_generate_nginx( $config ); } return ''; } /** * Retrieves the site URI for the current WordPress installation. * * @return string The relative site URI. */ private function site_uri() { $site_uri = rtrim( network_site_url( '', 'relative' ), '/' ) . '/'; // There is a bug in WP where network_home_url can return a non-relative URI even though scheme is set to relative. if ( Util_Environment::is_url( $site_uri ) ) { $site_uri = wp_parse_url( $site_uri, PHP_URL_PATH ); } return $site_uri; } /** * Generates Apache rewrite rules for core minification functionality. * * @param Config $config W3TC Config containing relevant settings. * * @return string Generated Apache rewrite rules. */ public function rules_core_generate_apache( $config ) { $cache_uri = Util_Environment::url_to_uri( Util_Environment::filename_to_url( W3TC_CACHE_MINIFY_DIR ) ) . '/'; $site_uri = $this->site_uri(); // There is a bug in WP where network_home_url can return a non-relative URI even though scheme is set to relative. if ( Util_Environment::is_url( $site_uri ) ) { $site_uri = wp_parse_url( $site_uri, PHP_URL_PATH ); } $engine = $config->get_string( 'minify.engine' ); $browsercache = $config->get_boolean( 'browsercache.enabled' ); $brotli = ( $browsercache && $config->get_boolean( 'browsercache.cssjs.brotli' ) && ! defined( 'W3TC_PAGECACHE_OUTPUT_COMPRESSION_OFF' ) ); $compression = ( $browsercache && $config->get_boolean( 'browsercache.cssjs.compression' ) && ! defined( 'W3TC_PAGECACHE_OUTPUT_COMPRESSION_OFF' ) ); $rules = ''; $rules .= W3TC_MARKER_BEGIN_MINIFY_CORE . "\n"; $rules .= "