eOptions Inherit' . PHP_EOL;
}
$content .= PHP_EOL;
$content .= ' ' . apply_filters( 'webpc_htaccess_original_cond', 'RewriteCond %{QUERY_STRING} original$' ) . PHP_EOL;
$content .= ' RewriteCond %{REQUEST_FILENAME} -f' . PHP_EOL;
$content .= ' RewriteRule . - [L]' . PHP_EOL;
foreach ( $this->format_factory->get_mime_types() as $format => $mime_type ) {
$content .= PHP_EOL;
foreach ( $settings[ SupportedExtensionsOption::OPTION_NAME ] as $ext ) {
if ( $format === $ext ) {
continue;
}
$content .= " RewriteCond %{HTTP_ACCEPT} {$mime_type}" . PHP_EOL;
if ( in_array( ExtraFeaturesOption::OPTION_VALUE_ONLY_SMALLER, $settings[ ExtraFeaturesOption::OPTION_NAME ] ) ) {
$content .= " RewriteCond %{REQUEST_FILENAME} -f" . PHP_EOL;
}
if ( $document_root === '%{DOCUMENT_ROOT}/' ) {
$content .= " RewriteCond %{DOCUMENT_ROOT}/{$output_path}/$1.{$ext}.{$format} -f" . PHP_EOL;
} elseif ( strpos( $document_root, '%{DOCUMENT_ROOT}' ) !== false ) {
$content .= " RewriteCond {$document_root}{$output_path}/$1.{$ext}.{$format} -f [OR]" . PHP_EOL;
$content .= " RewriteCond %{DOCUMENT_ROOT}/{$output_path}/$1.{$ext}.{$format} -f" . PHP_EOL;
} else {
$content .= " RewriteCond {$document_root}{$output_path}/$1.{$ext}.{$format} -f [OR]" . PHP_EOL;
$content .= " RewriteCond %{DOCUMENT_ROOT}{$root_suffix}{$output_path}/$1.{$ext}.{$format} -f" . PHP_EOL;
}
if ( apply_filters( 'webpc_htaccess_mod_rewrite_referer', false ) === true ) {
$content .= " RewriteCond %{HTTP_HOST}@@%{HTTP_REFERER} ^([^@]*)@@https?://\\1/.*" . PHP_EOL;
}
$content .= " RewriteRule (.+)\.{$ext}$ {$root_suffix_output}{$output_path}/$1.{$ext}.{$format} [NC,T={$mime_type},L]" . PHP_EOL;
}
}
$content .= '' . PHP_EOL;
return apply_filters( 'webpc_htaccess_mod_rewrite', trim( $content ), $output_path );
}
/**
* Generates rules for mod_headers.
*
* @param mixed[] $settings Plugin settings.
*
* @return string Rules for .htaccess file.
*/
protected function get_mod_headers_rules( array $settings ): string {
$content = '';
$extensions = implode( '|', $settings[ SupportedExtensionsOption::OPTION_NAME ] );
$cache_control = true;
if ( $settings[ CloudflareZoneIdOption::OPTION_NAME ] && $settings[ CloudflareApiTokenOption::OPTION_NAME ]
&& OptionsAccessManager::get_option( CloudflareConfigurator::REQUEST_CACHE_CONFIG_OPTION ) === 'yes' ) {
$cache_control = false;
} elseif ( EnvDetector::is_cdn_bunny() ) {
$cache_control = false;
}
$content .= '' . PHP_EOL;
if ( $extensions ) {
$content .= ' ' . PHP_EOL;
}
if ( apply_filters( 'webpc_htaccess_cache_control_private', $cache_control ) ) {
$content .= ' Header always set Cache-Control "private"' . PHP_EOL;
}
$content .= ' Header append Vary "Accept"' . PHP_EOL;
if ( $extensions ) {
$content .= ' ' . PHP_EOL;
}
$content .= '';
return apply_filters( 'webpc_htaccess_mod_headers', $content );
}
/**
* Generates rules for mod_expires.
*
* @return string Rules for .htaccess file.
*/
private function get_mod_expires_rules(): string {
$content = '';
$content .= '' . PHP_EOL;
$content .= ' ExpiresActive On' . PHP_EOL;
foreach ( $this->format_factory->get_mime_types() as $format => $mime_type ) {
$content .= " ExpiresByType {$mime_type} \"access plus 1 year\"" . PHP_EOL;
}
$content .= '';
return apply_filters( 'webpc_htaccess_mod_expires', $content );
}
/**
* Generates rules that add support for output formats.
*
* @param mixed[] $settings Plugin settings.
*
* @return string Rules for .htaccess file.
*/
private function get_mod_mime_rules( array $settings ): string {
$content = '';
if ( ! $settings[ SupportedExtensionsOption::OPTION_NAME ] ) {
return $content;
}
$content .= '' . PHP_EOL;
foreach ( $this->format_factory->get_mime_types() as $format => $mime_type ) {
$content .= " AddType {$mime_type} .{$format}" . PHP_EOL;
}
$content .= '';
return apply_filters( 'webpc_htaccess_mod_mime', $content );
}
/**
* Adds comments before and after rules for .htaccess file.
*
* @param string[] $rules Rules for .htaccess file.
*
* @return string Rules for .htaccess file.
*/
private function add_comments_to_rules( array $rules ): string {
if ( ! $rules ) {
return '';
}
$rows = [];
$rows[] = '';
$rows[] = '# BEGIN Converter for Media';
$rows[] = '# ! --- DO NOT EDIT PREVIOUS LINE --- !';
$rows = array_merge( $rows, array_filter( $rules ) );
$rows[] = '# ! --- DO NOT EDIT NEXT LINE --- !';
$rows[] = '# END Converter for Media';
$rows[] = '';
return implode( PHP_EOL, $rows );
}
/**
* Saves rules to .htaccess file in selected location.
*
* @param string $path_dir Location of .htaccess file.
* @param string $rules Rules for .htaccess file.
*
* @return void
*/
private function save_rewrites_in_htaccesss( string $path_dir, string $rules = '' ) {
$path_file = $path_dir . '/.htaccess';
$code = ( is_readable( $path_file ) ) ? file_get_contents( $path_file ) ?: '' : '';
$code = preg_replace(
'/((:?[\r\n|\r|\n]?)# BEGIN (Converter for Media|WebP Converter)(.*?)# END (Converter for Media|WebP Converter)(:?(:?[\r\n|\r|\n]+)?))/s',
'',
$code
);
if ( $rules && $code ) {
$code = PHP_EOL . $code;
}
$code = $rules . $code;
if ( is_writable( $path_dir ) ) {
file_put_contents( $path_file, $code );
}
}
}