sync();
}
/**
* Makes the uploads .htaccess match the delivery setting.
*
* @return bool Whether the file matches now.
*/
public function sync(): bool {
$this->settings->flush();
if ( 'rewrite' === $this->settings->get( 'delivery' ) && $this->server_reads_htaccess() ) {
return $this->write( $this->apache_lines() );
}
return $this->remove();
}
/**
* Removes the rules.
*
* @return bool
*/
public function remove(): bool {
$file = $this->htaccess_path();
if ( ! is_file( $file ) || ! str_contains( (string) Paths::filesystem()->get_contents( $file ), '# BEGIN ' . self::MARKER ) ) {
return true;
}
return $this->write( array() );
}
/**
* Whether the rules are in place.
*
* @return bool
*/
public function are_written(): bool {
$file = $this->htaccess_path();
return is_file( $file ) && str_contains( (string) Paths::filesystem()->get_contents( $file ), 'RewriteCond %{HTTP_ACCEPT} image/webp' );
}
/**
* Whether this server reads .htaccess files.
*
* @return bool
*/
public function server_reads_htaccess(): bool {
global $is_apache;
return (bool) $is_apache;
}
/**
* Returns the Apache and LiteSpeed rules.
*
* @return string[]
*/
public function apache_lines(): array {
return array(
'',
'AddType image/webp .webp',
'AddType image/avif .avif',
'',
'',
'RewriteEngine On',
'RewriteCond %{HTTP_ACCEPT} image/avif',
'RewriteCond %{REQUEST_FILENAME}.avif -f',
'RewriteRule ^(.+\.(?:jpe?g|png|gif|webp))$ $1.avif [T=image/avif,L]',
'RewriteCond %{HTTP_ACCEPT} image/webp',
'RewriteCond %{REQUEST_FILENAME}.webp -f',
'RewriteRule ^(.+\.(?:jpe?g|png|gif))$ $1.webp [T=image/webp,L]',
'',
'',
'',
'Header append Vary Accept',
'',
'',
);
}
/**
* Returns the Nginx configuration.
*
* @return string
*/
public function nginx_snippet(): string {
$path = (string) wp_parse_url( (string) wp_upload_dir( null, false )['baseurl'], PHP_URL_PATH );
return implode(
"\n",
array(
'# In the http block:',
'# The default suffix names no real file. An empty one would make try_files stop at the original.',
'map $http_accept $pixelkeep_avif {',
' default ".pixelkeep-none";',
' "~*image/avif" ".avif";',
'}',
'map $http_accept $pixelkeep_webp {',
' default ".pixelkeep-none";',
' "~*image/webp" ".webp";',
'}',
'',
'# In the server block, before other image locations:',
'location ~* ^' . $path . '/.+\.(?:jpe?g|png|gif)$ {',
' types {',
' image/jpeg jpg jpeg;',
' image/png png;',
' image/gif gif;',
' image/webp webp;',
' image/avif avif;',
' }',
' add_header Vary Accept;',
' try_files $uri$pixelkeep_avif $uri$pixelkeep_webp $uri =404;',
'}',
)
);
}
/**
* Requests a converted image as a modern browser would, to see what the server really does.
*
* @return array{tested: bool, type: string, vary: bool}
*/
public function test(): array {
global $wpdb;
$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s LIMIT 1", '_pixelkeep_webp' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
$url = 0 === $id ? false : wp_get_attachment_url( $id );
if ( false === $url || ! isset( $this->derivatives->records( $id )['webp']['full'] ) ) {
return array(
'tested' => false,
'type' => '',
'vary' => false,
);
}
$response = wp_remote_get(
$url,
array(
'timeout' => 10,
'sslverify' => false,
'headers' => array( 'Accept' => 'image/webp,image/*' ),
)
);
if ( is_wp_error( $response ) ) {
return array(
'tested' => false,
'type' => '',
'vary' => false,
);
}
return array(
'tested' => true,
'type' => (string) wp_remote_retrieve_header( $response, 'content-type' ),
'vary' => false !== stripos( (string) wp_remote_retrieve_header( $response, 'vary' ), 'accept' ),
);
}
/**
* Returns the uploads .htaccess path.
*
* @return string
*/
private function htaccess_path(): string {
return Paths::uploads_base() . '/.htaccess';
}
/**
* Writes the marked block.
*
* @param string[] $lines Lines between the markers.
* @return bool
*/
private function write( array $lines ): bool {
require_once ABSPATH . 'wp-admin/includes/misc.php';
return insert_with_markers( $this->htaccess_path(), self::MARKER, $lines );
}
}