Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/admin/options.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
3
+
defined( 'ABSPATH' ) || exit;
4
+
5
+
/**
6
+
* When our settings are saved: purge, flush, preload!
7
+
*
8
+
* @since 1.0
9
+
*
10
+
* When the settings menu is hidden, redirect on the main settings page to avoid the same thing
11
+
* (Only when a form is sent from our options page )
12
+
*
13
+
* @since 2.1
14
+
*
15
+
* @param array $oldvalue An array of previous values for the settings.
16
+
* @param array $value An array of submitted values for the settings.
17
+
*/
18
+
function rocket_after_save_options( $oldvalue, $value ) {
19
+
if ( ! is_array( $oldvalue ) || ! is_array( $value ) ) {
20
+
return;
21
+
}
22
+
23
+
// phpcs:ignore WordPress.Security.NonceVerification.Missing
24
+
if ( ( array_key_exists( 'minify_js', $oldvalue ) && array_key_exists( 'minify_js', $value ) && $oldvalue['minify_js'] !== $value['minify_js'] )
25
+
||
26
+
( array_key_exists( 'exclude_js', $oldvalue ) && array_key_exists( 'exclude_js', $value ) && $oldvalue['exclude_js'] !== $value['exclude_js'] )
27
+
||
28
+
( array_key_exists( 'cdn', $oldvalue ) && array_key_exists( 'cdn', $value ) && $oldvalue['cdn'] !== $value['cdn'] )
29
+
||
30
+
( array_key_exists( 'cdn_cnames', $oldvalue ) && array_key_exists( 'cdn_cnames', $value ) && $oldvalue['cdn_cnames'] !== $value['cdn_cnames'] )
31
+
) {
32
+
rocket_clean_minify( 'js' );
33
+
}
34
+
35
+
// Regenerate advanced-cache.php file.
36
+
if (
37
+
! empty( $_POST ) // phpcs:ignore WordPress.Security.NonceVerification.Missing
38
+
&&
39
+
(
40
+
(
41
+
isset( $oldvalue['do_caching_mobile_files'] ) && ! isset( $value['do_caching_mobile_files'] )
42
+
)
43
+
||
44
+
(
45
+
! isset( $oldvalue['do_caching_mobile_files'] ) && isset( $value['do_caching_mobile_files'] )
46
+
)
47
+
||
48
+
(
49
+
isset( $oldvalue['do_caching_mobile_files'], $value['do_caching_mobile_files'] )
50
+
&&
51
+
$oldvalue['do_caching_mobile_files'] !== $value['do_caching_mobile_files']
52
+
)
53
+
||
54
+
$oldvalue['cache_mobile'] !== $value['cache_mobile']
55
+
) ) {
56
+
rocket_generate_advanced_cache_file();
57
+
}
58
+
59
+
// Update .htaccess file rules.
60
+
flush_rocket_htaccess( ! rocket_valid_key() );
61
+
62
+
// Update config file.
63
+
rocket_generate_config_file();
64
+
65
+
if ( isset( $oldvalue['analytics_enabled'], $value['analytics_enabled'] ) && $oldvalue['analytics_enabled'] !== $value['analytics_enabled'] && 1 === (int) $value['analytics_enabled'] ) {
66
+
set_transient( 'rocket_analytics_optin', 1 );
67
+
}
68
+
// If it's different, clean the domain.
69
+
if ( rocket_create_options_hash( $value ) !== rocket_create_options_hash( $oldvalue ) ) {
70
+
// Purge all cache files.
71
+
rocket_clean_domain();
72
+
73
+
/**
74
+
* Fires after WP Rocket options that require a cache purge have changed
75
+
*
76
+
* @since 3.11
77
+
*
78
+
* @param array $value An array of submitted values for the settings.
79
+
*/
80
+
do_action( 'rocket_options_changed', $value );
81
+
}
82
+
}
83
+
add_action( 'update_option_' . rocket_get_constant( 'WP_ROCKET_SLUG' ), 'rocket_after_save_options', 10, 2 );
84
+
85
+
/**
86
+
* Perform actions when settings are saved.
87
+
*
88
+
* @since 1.0
89
+
*
90
+
* @param array $newvalue An array of submitted options values.
91
+
* @param array $oldvalue An array of previous options values.
92
+
* @return array Updated submitted options values.
93
+
*/
94
+
function rocket_pre_main_option( $newvalue, $oldvalue ) {
95
+
$rocket_settings_errors = [];
96
+
97
+
// Make sure that fields that allow users to enter patterns are well formatted.
98
+
$is_form_submit = isset( $_POST['option_page'] ) ? sanitize_key( $_POST['option_page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
99
+
$is_form_submit = WP_ROCKET_PLUGIN_SLUG === $is_form_submit;
100
+
$errors = [];
101
+
$pattern_labels = [
102
+
'exclude_css' => __( 'Excluded CSS Files', 'rocket' ),
103
+
'exclude_inline_js' => __( 'Excluded Inline JavaScript', 'rocket' ),
104
+
'exclude_js' => __( 'Excluded JavaScript Files', 'rocket' ),
105
+
'exclude_defer_js' => __( 'Defer JavaScript Files', 'rocket' ),
106
+
'delay_js_exclusions' => __( 'Excluded Delay JavaScript Files', 'rocket' ),
107
+
'cache_reject_uri' => __( 'Never Cache URL(s)', 'rocket' ),
108
+
'cache_reject_ua' => __( 'Never Cache User Agent(s)', 'rocket' ),
109
+
'cache_purge_pages' => __( 'Always Purge URL(s)', 'rocket' ),
110
+
'cdn_reject_files' => __( 'Exclude files from CDN', 'rocket' ),
111
+
];
112
+
113
+
// unset the *_mask value as we don't need to save them.
114
+
unset( $newvalue['cloudflare_api_key_mask'], $newvalue['cloudflare_zone_id_mask'] );
115
+
116
+
foreach ( $pattern_labels as $pattern_field => $label ) {
117
+
if ( empty( $newvalue[ $pattern_field ] ) ) {
118
+
// The field is empty.
119
+
continue;
120
+
}
121
+
122
+
// Sanitize.
123
+
$newvalue[ $pattern_field ] = rocket_sanitize_textarea_field( $pattern_field, $newvalue[ $pattern_field ] );
124
+
125
+
// Validate.
126
+
$newvalue[ $pattern_field ] = array_filter(
127
+
$newvalue[ $pattern_field ],
128
+
function ( $excluded ) use ( $pattern_field, $label, $is_form_submit, &$errors ) {
129
+
if ( false === @preg_match( '#' . str_replace( '#', '\#', $excluded ) . '#', 'dummy-sample' ) && $is_form_submit ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
130
+
/* translators: 1 and 2 can be anything. */
131
+
$errors[ $pattern_field ][] = sprintf( __( '%1$s: <em>%2$s</em>', 'rocket' ), $label, esc_html( $excluded ) );
132
+
return false;
133
+
}
134
+
135
+
return true;
136
+
}
137
+
);
138
+
}
139
+
140
+
if ( $errors ) {
141
+
$error_message = _n( 'The following pattern is invalid and has been removed:', 'The following patterns are invalid and have been removed:', array_sum( array_map( 'count', $errors ) ), 'rocket' );
142
+
143
+
$error_message .= '</strong></p>'; // close out default opening tags from WP's settings_errors().
144
+
145
+
foreach ( $errors as $error ) {
146
+
$error_message .= '<ul><li>' . implode( '</li><li>', $error ) . '</li></ul>';
147
+
}
148
+
149
+
$error_message .= '<p><strong>'; // Re-open tags that WP's settings_errors() will close at end of notice box.
150
+
151
+
$container = apply_filters( 'rocket_container', [] );
152
+
$invalid_exclusions_beacon = $container->get( 'beacon' )->get_suggest( 'invalid_exclusions' );
153
+
$error_message .= sprintf(
154
+
'<a href="%1$s" data-beacon-article="%2$s" rel="noopener noreferrer" target="_blank">%3$s</a>',
155
+
$invalid_exclusions_beacon['url'],
156
+
$invalid_exclusions_beacon['id'],
157
+
__( 'More info', 'rocket' )
158
+
);
159
+
160
+
$errors = [];
161
+
162
+
$rocket_settings_errors[] = [
163
+
'setting' => 'general',
164
+
'code' => 'invalid_patterns',
165
+
'message' => __( 'WP Rocket: ', 'rocket' ) . $error_message,
166
+
'type' => 'error',
167
+
];
168
+
}
169
+
170
+
// Clear WP Rocket database optimize cron if the setting has been modified.
171
+
if (
172
+
( ( isset( $newvalue['schedule_automatic_cleanup'], $oldvalue['schedule_automatic_cleanup'] ) && $newvalue['schedule_automatic_cleanup'] !== $oldvalue['schedule_automatic_cleanup'] ) )
173
+
|| ( ( isset( $newvalue['automatic_cleanup_frequency'], $oldvalue['automatic_cleanup_frequency'] ) && $newvalue['automatic_cleanup_frequency'] !== $oldvalue['automatic_cleanup_frequency'] ) )
174
+
) {
175
+
if ( wp_next_scheduled( 'rocket_database_optimization_time_event' ) ) {
176
+
wp_clear_scheduled_hook( 'rocket_database_optimization_time_event' );
177
+
}
178
+
}
179
+
180
+
// Regenerate the minify key if JS files have been modified.
181
+
// phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
182
+
if (
183
+
( isset( $newvalue['minify_js'], $oldvalue['minify_js'] ) && $newvalue['minify_js'] != $oldvalue['minify_js'] ) // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
184
+
||
185
+
( isset( $newvalue['exclude_js'], $oldvalue['exclude_js'] ) && $newvalue['exclude_js'] !== $oldvalue['exclude_js'] )
186
+
||
187
+
(
188
+
( isset( $oldvalue['cdn'] ) && ! isset( $newvalue['cdn'] ) )
189
+
||
190
+
( ! isset( $oldvalue['cdn'] ) && isset( $newvalue['cdn'] ) )
191
+
)
192
+
) {
193
+
$newvalue['minify_js_key'] = create_rocket_uniqid();
194
+
}
195
+
196
+
// Checked the SSL option if the whole website is on SSL.
197
+
if ( rocket_is_ssl_website() ) {
198
+
$newvalue['cache_ssl'] = 1;
199
+
}
200
+
201
+
if ( 'local' === wp_get_environment_type() ) {
202
+
$newvalue['optimize_css_delivery'] = 0;
203
+
$newvalue['remove_unused_css'] = 0;
204
+
$newvalue['async_css'] = 0;
205
+
}
206
+
207
+
if ( ! rocket_get_constant( 'WP_ROCKET_ADVANCED_CACHE' ) ) {
208
+
rocket_generate_advanced_cache_file();
209
+
}
210
+
211
+
$keys = get_transient( WP_ROCKET_SLUG );
212
+
213
+
if ( $keys ) {
214
+
delete_transient( WP_ROCKET_SLUG );
215
+
$newvalue = array_merge( $newvalue, $keys );
216
+
}
217
+
218
+
if ( ! $rocket_settings_errors ) {
219
+
return $newvalue;
220
+
}
221
+
222
+
$transient_errors = get_transient( 'settings_errors' );
223
+
if ( ! $transient_errors || ! is_array( $transient_errors ) ) {
224
+
$transient_errors = [];
225
+
}
226
+
227
+
$settings_errors = array_merge( $transient_errors, $rocket_settings_errors );
228
+
229
+
foreach ( $settings_errors as $error ) {
230
+
add_settings_error( $error['setting'], $error['code'], $error['message'], $error['type'] );
231
+
}
232
+
233
+
return $newvalue;
234
+
}
235
+
236
+
add_filter( 'pre_update_option_' . rocket_get_constant( 'WP_ROCKET_SLUG' ), 'rocket_pre_main_option', 10, 2 );
237
+
238
+
/**
239
+
* Auto-activate the SSL cache if the website URL is updated with https protocol
240
+
*
241
+
* @since 2.7
242
+
*
243
+
* @param array $old_value An array of previous options values.
244
+
* @param array $value An array of submitted options values.
245
+
*/
246
+
function rocket_update_ssl_option_after_save_home_url( $old_value, $value ) {
247
+
if ( $old_value === $value || get_rocket_option( 'cache_ssl' ) ) {
248
+
return;
249
+
}
250
+
251
+
$scheme = rocket_extract_url_component( $value, PHP_URL_SCHEME );
252
+
253
+
update_rocket_option( 'cache_ssl', 'https' === $scheme ? 1 : 0 );
254
+
}
255
+
add_action( 'update_option_home', 'rocket_update_ssl_option_after_save_home_url', 10, 2 );
256
+