PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 3.0.6
WP STAGING – WordPress Backups, Restore, Migration & Clone v3.0.6
4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Backend / Optimizer / wp-staging-optimizer.php
wp-staging / Backend / Optimizer Last commit date
blank-theme 5 years ago Optimizer.php 2 years ago wp-staging-optimizer.php 2 years ago
wp-staging-optimizer.php
353 lines
1 <?php
2
3 /*
4 * Plugin Name: WP STAGING Optimizer
5 * Plugin URI: https://wp-staging.com
6 * Description: Prevents 3rd party plugins from being loaded during WP STAGING specific operations.
7 *
8 * This is a must-use standalone plugin -
9 * Do not use any of these methods in WP STAGING code base as this mu-plugin can be missing!
10 *
11 * Author: René Hermenau
12 * Version: 1.5.3
13 * Author URI: https://wp-staging.com
14 * Text Domain: wp-staging
15 */
16
17 // Version number of this mu-plugin. Important for automatic updates
18 // Important: Update WPSTG_OPTIMIZER_MUVERSION in /bootstrap.php to the same version!
19
20 if (!defined('WPSTG_OPTIMIZER_VERSION')) {
21 define('WPSTG_OPTIMIZER_VERSION', '1.5.3');
22 }
23 if (!function_exists('wpstgGetPluginsDir')) {
24 /** @return string */
25 function wpstgGetPluginsDir(): string
26 {
27 $pluginsDir = '';
28 if (defined('WP_PLUGIN_DIR')) {
29 $pluginsDir = trailingslashit(WP_PLUGIN_DIR);
30 } elseif (defined('WP_CONTENT_DIR')) {
31 $pluginsDir = trailingslashit(WP_CONTENT_DIR) . 'plugins/';
32 }
33 return $pluginsDir;
34 }
35 }
36
37 if (!function_exists('wpstgIsEnabledOptimizer')) {
38 /**
39 * Check if optimizer is enabled
40 * @return bool
41 */
42 function wpstgIsEnabledOptimizer(): bool
43 {
44 $status = (object)get_option('wpstg_settings');
45
46 if ($status && isset($status->optimizer) && $status->optimizer == 1) {
47 return true;
48 }
49
50 return false;
51 }
52 }
53
54 if (!function_exists('wpstgIsExcludedPlugin')) {
55 /**
56 * Check if a plugin should be excluded from the optimizer and still running during wp staging requests
57 * @param string $plugin
58 * @return bool
59 */
60 function wpstgIsExcludedPlugin(string $plugin): bool
61 {
62 $excludedPlugins = get_option('wpstg_optimizer_excluded', []);
63
64 // As a workaround for now we exclude all-in-one-wp-security-and-firewall AIOWPS plugin due
65 // to their salt postfix function which breaks WP STAGING completely.
66 //$excludedPlugins[] = 'all-in-one-wp-security-and-firewall';
67
68 // Check for custom excluded plugins
69 foreach ($excludedPlugins as $excludedPlugin) {
70 if (strpos($plugin, $excludedPlugin) !== false) {
71 return true;
72 }
73 }
74
75 return false;
76 }
77 }
78
79 if (!function_exists('wpstgExcludePlugins')) {
80 /**
81 * Remove all plugins except wp-staging and wp-staging-pro from blog-active plugins
82 *
83 * @param array $plugins numerically keyed array of plugin names (index=>name)
84 *
85 * @return array
86 */
87 function wpstgExcludePlugins(array $plugins): array
88 {
89 if (!is_array($plugins) || empty($plugins)) {
90 return $plugins;
91 }
92
93 if (!wpstgIsOptimizerRequest()) {
94 return $plugins;
95 }
96
97 foreach ($plugins as $key => $plugin) {
98 // Default filter. Must be at the beginning or wp staging plugin will be filtered and killed
99 if (strpos($plugin, 'wp-staging') !== false || wpstgIsExcludedPlugin($plugin)) {
100 continue;
101 }
102 unset($plugins[$key]);
103 }
104
105
106 return $plugins;
107 }
108
109 add_filter('option_active_plugins', 'wpstgExcludePlugins');
110 }
111
112 if (!function_exists('wpstgExcludeSitePlugins')) {
113 /**
114 * Remove all plugins except wp-staging and wp-staging-pro from network-active plugins
115 *
116 * @param array $plugins array of plugins keyed by name (name=>timestamp pairs)
117 *
118 * @return array
119 */
120 function wpstgExcludeSitePlugins(array $plugins): array
121 {
122 if (!is_array($plugins) || empty($plugins)) {
123 return $plugins;
124 }
125
126 if (!wpstgIsOptimizerRequest()) {
127 return $plugins;
128 }
129
130 foreach ($plugins as $plugin => $timestamp) {
131 // Default filter. Must be at the beginning or wp staging plugin will be filtered and killed
132 if (strpos($plugin, 'wp-staging') !== false || wpstgIsExcludedPlugin($plugin)) {
133 continue;
134 }
135
136 unset($plugins[$plugin]);
137 }
138
139 return $plugins;
140 }
141
142 if (is_multisite()) {
143 add_filter('site_option_active_sitewide_plugins', 'wpstgExcludeSitePlugins');
144 }
145 }
146
147 if (!function_exists('wpstgDisableTheme')) {
148 /**
149 *
150 * Disables the active theme during WP Staging AJAX requests
151 *
152 *
153 * @param $dir
154 *
155 * @return string
156 */
157 function wpstgDisableTheme($dir): string
158 {
159 $enableTheme = apply_filters('wpstg_optimizer_enable_theme', false);
160
161 if (wpstgIsOptimizerRequest() && $enableTheme === false) {
162 $wpstgRootPro = wpstgGetPluginsDir() . 'wp-staging-pro';
163 $wpstgRoot = wpstgGetPluginsDir() . 'wp-staging';
164
165 $file = DIRECTORY_SEPARATOR . 'Backend' . DIRECTORY_SEPARATOR . 'Optimizer' . DIRECTORY_SEPARATOR . 'blank-theme' . DIRECTORY_SEPARATOR . 'functions.php';
166 $theme = DIRECTORY_SEPARATOR . 'Backend' . DIRECTORY_SEPARATOR . 'Optimizer' . DIRECTORY_SEPARATOR . 'blank-theme';
167
168
169 if (file_exists($wpstgRoot . $file)) {
170 return $wpstgRoot . $theme;
171 } elseif (file_exists($wpstgRootPro . $file)) {
172 return $wpstgRootPro . $theme;
173 } else {
174 return '';
175 }
176 }
177
178 return $dir;
179 }
180
181 add_filter('stylesheet_directory', 'wpstgDisableTheme');
182 add_filter('template_directory', 'wpstgDisableTheme');
183 }
184
185 if (!function_exists('wpstgIsOptimizerRequest')) {
186 /**
187 * Should the current request be processed by optimizer?
188 *
189 * For WP STAGING requests that require other plugins to be active, use raw_wpstg_{actionName}
190 *
191 * @return bool
192 */
193 function wpstgIsOptimizerRequest(): bool
194 {
195 if (!wpstgIsEnabledOptimizer()) {
196 return false;
197 }
198
199 if (
200 defined('DOING_AJAX') &&
201 DOING_AJAX &&
202 isset($_REQUEST['action']) &&
203 strpos(sanitize_text_field($_REQUEST['action']), 'wpstg_send_report') === false &&
204 strpos(sanitize_text_field($_REQUEST['action']), 'wpstg') === 0
205 ) {
206 return true;
207 }
208
209 return false;
210 }
211 }
212
213 if (!function_exists('wpstgTgmpaCompatibility')) {
214 /**
215 * Remove TGM Plugin Activation 'force_activation' admin_init action hook if present.
216 *
217 * This is to stop excluded plugins being deactivated after a migration, when a theme uses TGMPA to require a plugin to be always active.
218 */
219 function wpstgTgmpaCompatibility()
220 {
221 $remove_function = false;
222
223 // run on wpstg page
224 if (isset($_GET['page']) && $_GET['page'] == 'wpstg_clone') {
225 $remove_function = true;
226 }
227 // run on wpstg ajax requests
228 if (defined('DOING_AJAX') && DOING_AJAX && isset($_POST['action']) && strpos(sanitize_text_field($_POST['action']), 'wpstg') !== false) {
229 $remove_function = true;
230 }
231
232 if ($remove_function) {
233 global $wp_filter;
234 $admin_init_functions = $wp_filter['admin_init'];
235 foreach ($admin_init_functions as $priority => $functions) {
236 foreach ($functions as $key => $function) {
237 // searching for function this way as can't rely on the calling class being named TGM_Plugin_Activation
238 if (strpos($key, 'force_activation') !== false) {
239 unset($wp_filter['admin_init'][$priority][$key]);
240
241 return;
242 }
243 }
244 }
245 }
246 }
247
248 add_action('admin_init', 'wpstgTgmpaCompatibility', 1);
249 }
250
251 if (!function_exists('wpstgIsStaging')) {
252 /**
253 * @return bool True if it is staging site. False otherwise.
254 */
255 function wpstgIsStaging(): bool
256 {
257 if (defined('WPSTAGING_DEV_SITE') && WPSTAGING_DEV_SITE === true) {
258 return true;
259 }
260
261 if (get_option("wpstg_is_staging_site") === "true") {
262 return true;
263 }
264
265 if (file_exists(ABSPATH . '.wp-staging')) {
266 return true;
267 }
268
269 return false;
270 }
271 }
272
273 if (!function_exists('wpstgGetCloneSettings')) {
274 /**
275 * Get the value of the given option in clone settings,
276 * If no option given return all clone settings
277 *
278 * @param string|null $option
279 * @return mixed
280 */
281 function wpstgGetCloneSettings(string $option = null)
282 {
283 $settings = get_option('wpstg_clone_settings', null);
284
285 // Return settings if no options given
286 if ($option === null) {
287 return $settings;
288 }
289
290 // Early Bail: if settings is null or if settings isn't object
291 if ($settings === null || !is_object($settings)) {
292 return null;
293 }
294
295 // Early bail if given option not exists
296 if (!property_exists($settings, $option)) {
297 return null;
298 }
299
300 return $settings->{$option};
301 }
302 }
303
304 /**
305 * Disable all outgoing e-mails on Staging site
306 * Will check against both the old and new logic of storing emails disabled option
307 */
308 if (wpstgIsStaging() && (((bool)get_option("wpstg_emails_disabled") === true) || (wpstgGetCloneSettings('wpstg_emails_disabled')))) {
309 if (!function_exists('wp_mail')) {
310 /**
311 * @param string $to
312 * @param string $subject
313 * @param string $message
314 * @param string $headers
315 * @param array $attachments
316 * @return bool
317 */
318 function wp_mail(string $to, string $subject, string $message, string $headers = '', array $attachments = []): bool
319 {
320 if (defined('WPSTG_DEBUG') && WPSTG_DEBUG) {
321 // Safely cast everything to string
322 $to = is_string($to) ? $to : wp_json_encode($to);
323 $subject = is_string($subject) ? $subject : wp_json_encode($subject);
324 $message = is_string($message) ? $message : wp_json_encode($message);
325 $headers = is_string($headers) ? $headers : wp_json_encode($headers);
326 $attachments = is_string($attachments) ? $attachments : wp_json_encode($attachments);
327
328 $log_entry = <<<LOG_ENTRY
329 /***
330 * WPSTAGING - Mails are disabled for this Staging site. This e-mail was going to be sent, but was prevented:
331 ***
332 * To: $to
333 ***
334 * Subject: $subject
335 ***
336 * Message: $message
337 ***
338 * Headers: $headers
339 ***
340 * Attachments: $attachments
341 ***/
342 LOG_ENTRY;
343
344 error_log($log_entry);
345 }
346
347 return false;
348 }
349 } elseif (defined('WPSTG_DEBUG') && WPSTG_DEBUG) {
350 error_log("WP STAGING: Could not override the wp_mail() function to disable e-mails on staging site, as it was already defined before the optimizer mu-plugin was loaded.");
351 }
352 }
353