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