forked from tinify/wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-tiny-apache-rewrite.php
More file actions
165 lines (146 loc) · 4.79 KB
/
class-tiny-apache-rewrite.php
File metadata and controls
165 lines (146 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
* Tiny Compress Images - WordPress plugin.
* Copyright (C) 2015-2018 Tinify B.V.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n*/
/**
* Tiny_Apache_Rewrite
* class responsible for the apache rules for image delivery.
* - toggling the rules when saving settings
* - inserting/removing rules from htaccess
*
* We are only updating rules when:
* - updating the option convert_format
* - uninstalling the plug-in
*/
class Tiny_Apache_Rewrite extends Tiny_WP_Base {
/**
* seperator when rules are inserted
* @var string
*/
const MARKER = 'tiny-compress-images';
/**
* Installs or uninstalls the htaccess rules
* hooked into `update_option_tinypng_convert_format`
* https://developer.wordpress.org/reference/hooks/update_option_option/
*
*
* @param mixed $old_value
* @param mixed $value
* @param string $option
* @return void
*/
public static function toggle_rules( $old_value, $value, $option ) {
$old_delivery = isset( $old_value['delivery_method'] ) ?
$old_value['delivery_method'] : null;
$new_delivery = isset( $value['delivery_method'] ) ?
$value['delivery_method'] : null;
if ( $old_delivery === $new_delivery ) {
return;
}
if ( 'htaccess' === $new_delivery ) {
$installed = self::install_rules();
Tiny_Logger::debug( 'htaccess rules installed: ' . $installed );
return;
}
// We only uninstall if we were previously using htaccess
if ( 'htaccess' === $old_delivery ) {
$uninstalled = self::uninstall_rules();
Tiny_Logger::debug( 'htaccess rules uninstaled: ' . $uninstalled );
}
}
/**
* Generate .htaccess rewrite rules for serving WebP and AVIF images.
*
* @return string The .htaccess rules
*/
private static function get_rewrite_rules() {
$rules = array();
$rules[] = '<IfModule mod_rewrite.c>';
$rules[] = 'RewriteEngine On';
$rules[] = 'RewriteOptions Inherit';
$rules = array_merge( $rules, self::get_avif_rules() );
$rules = array_merge( $rules, self::get_webp_rules() );
$rules[] = '</IfModule>';
$rules[] = '<IfModule mod_headers.c>';
$rules[] = '<FilesMatch "\.(jpe?g|png)$">';
$rules[] = 'Header append Vary Accept';
$rules[] = '</FilesMatch>';
$rules[] = '</IfModule>';
$rules[] = '<IfModule mod_mime.c>';
$rules[] = 'AddType image/webp .webp';
$rules[] = 'AddType image/avif .avif';
$rules[] = '</IfModule>';
return implode( "\n", $rules );
}
/**
* Generate AVIF rewrite rules.
*
* @return array[] AVIF rewrite rules
*/
private static function get_avif_rules() {
$rules = array();
$rules[] = 'RewriteCond %{HTTP_ACCEPT} image/avif';
$rules[] = 'RewriteCond %{REQUEST_URI} ^(.+)\.(?:jpe?g|png)$';
$rules[] = 'RewriteCond %{DOCUMENT_ROOT}/%1.avif -f';
$rules[] = 'RewriteCond %{QUERY_STRING} !type=original';
$rules[] = 'RewriteRule (.+)\.(?:jpe?g|png)$ $1.avif [T=image/avif,L]';
return $rules;
}
/**
* Generate WebP rewrite rules.
*
* @return array[] WebP rewrite rules
*/
private static function get_webp_rules() {
$rules = array();
$rules[] = 'RewriteCond %{HTTP_ACCEPT} image/webp';
$rules[] = 'RewriteCond %{REQUEST_URI} ^(.+)\.(?:jpe?g|png)$';
$rules[] = 'RewriteCond %{DOCUMENT_ROOT}/%1.webp -f';
$rules[] = 'RewriteCond %{QUERY_STRING} !type=original';
$rules[] = 'RewriteRule (.+)\.(?:jpe?g|png)$ $1.webp [T=image/webp,L]';
return $rules;
}
/**
* Install rewrite rules to .htaccess files.
*
* @return bool True on success, false otherwise
*/
private static function install_rules() {
$rules = self::get_rewrite_rules();
$upload_dir = wp_upload_dir();
if ( isset( $upload_dir['basedir'] ) && is_writable( $upload_dir['basedir'] ) ) {
$htaccess_file = $upload_dir['basedir'] . '/.htaccess';
return insert_with_markers( $htaccess_file, self::MARKER, $rules );
}
return false;
}
/**
* Remove rewrite rules from .htaccess files.
*
* @return bool True on success, false otherwise
*/
public static function uninstall_rules() {
$upload_dir = wp_upload_dir();
if (
file_exists( $upload_dir['basedir'] . '/.htaccess' )
) {
$htaccess_file = $upload_dir['basedir'] . '/.htaccess';
return insert_with_markers( $htaccess_file, self::MARKER, '' );
}
return false;
}
}