PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.0.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.0.2
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 3 years ago wp-staging-optimizer.php 3 years ago
wp-staging-optimizer.php
344 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.1
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 if (!defined('WPSTG_OPTIMIZER_VERSION')) {
19 define('WPSTG_OPTIMIZER_VERSION', '1.5.1');
20 }
21 if (!function_exists('wpstgGetPluginsDir')) {
22 /** @return string */
23 function wpstgGetPluginsDir()
24 {
25 $pluginsDir = '';
26 if (defined('WP_PLUGIN_DIR')) {
27 $pluginsDir = trailingslashit(WP_PLUGIN_DIR);
28 } elseif (defined('WP_CONTENT_DIR')) {
29 $pluginsDir = trailingslashit(WP_CONTENT_DIR) . 'plugins/';
30 }
31 return $pluginsDir;
32 }
33 }
34
35 if (!function_exists('wpstgIsEnabledOptimizer')) {
36 /*
37 * Check if optimizer is enabled
38 * @return bool false if it's disabled
39 *
40 */
41 function wpstgIsEnabledOptimizer()
42 {
43 $status = (object)get_option('wpstg_settings');
44
45 if ($status && isset($status->optimizer) && $status->optimizer == 1) {
46 return true;
47 }
48
49 return false;
50 }
51 }
52
53 if (!function_exists('wpstgIsExcludedPlugin')) {
54 /**
55 * Check if certain plugins are excluded and still runing during wp staging requests
56 * @return boolean
57 */
58 function wpstgIsExcludedPlugin($plugin)
59 {
60 $excludedPlugins = get_option('wpstg_optimizer_excluded', []);
61
62 if (empty($excludedPlugins)) {
63 return false;
64 }
65
66 // Check for custom excluded plugins
67 foreach ($excludedPlugins as $excludedPlugin) {
68 if (strpos($plugin, $excludedPlugin) !== false) {
69 return true;
70 } else {
71 continue;
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($plugins)
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($plugins)
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 add_filter('site_option_active_sitewide_plugins', 'wpstgExcludeSitePlugins');
143 }
144
145
146 if (!function_exists('wpstgDisableTheme')) {
147 /**
148 *
149 * Disables the theme during WP Staging AJAX requests
150 *
151 *
152 * @param $dir
153 *
154 * @return string
155 */
156 function wpstgDisableTheme($dir)
157 {
158 $enableTheme = apply_filters('wpstg_optimizer_enable_theme', false);
159
160 if (wpstgIsOptimizerRequest() && $enableTheme === false) {
161 $wpstgRootPro = wpstgGetPluginsDir() . 'wp-staging-pro';
162 $wpstgRoot = wpstgGetPluginsDir() . 'wp-staging';
163
164 $file = DIRECTORY_SEPARATOR . 'Backend' . DIRECTORY_SEPARATOR . 'Optimizer' . DIRECTORY_SEPARATOR . 'blank-theme' . DIRECTORY_SEPARATOR . 'functions.php';
165 $theme = DIRECTORY_SEPARATOR . 'Backend' . DIRECTORY_SEPARATOR . 'Optimizer' . DIRECTORY_SEPARATOR . 'blank-theme';
166
167
168 if (file_exists($wpstgRoot . $file)) {
169 return $wpstgRoot . $theme;
170 } elseif (file_exists($wpstgRootPro . $file)) {
171 return $wpstgRootPro . $theme;
172 } else {
173 return '';
174 }
175 }
176
177 return $dir;
178 }
179
180 add_filter('stylesheet_directory', 'wpstgDisableTheme');
181 add_filter('template_directory', 'wpstgDisableTheme');
182 }
183
184 if (!function_exists('wpstgIsOptimizerRequest')) {
185 /**
186 * Should the current request be processed by optimizer?
187 *
188 * For WP STAGING requests that require other plugins to be active, use raw_wpstg_{actionName}
189 *
190 * @return bool
191 */
192 function wpstgIsOptimizerRequest()
193 {
194 if (!wpstgIsEnabledOptimizer()) {
195 return false;
196 }
197
198 if (
199 defined('DOING_AJAX') &&
200 DOING_AJAX &&
201 isset($_REQUEST['action']) &&
202 strpos(sanitize_text_field($_REQUEST['action']), 'wpstg_send_report') === false &&
203 strpos(sanitize_text_field($_REQUEST['action']), 'wpstg') === 0
204 ) {
205 return true;
206 }
207
208 return false;
209 }
210 }
211
212 if (!function_exists('wpstgTgmpaCompatibility')) {
213 /**
214 * Remove TGM Plugin Activation 'force_activation' admin_init action hook if present.
215 *
216 * This is to stop excluded plugins being deactivated after a migration, when a theme uses TGMPA to require a plugin to be always active.
217 */
218 function wpstgTgmpaCompatibility()
219 {
220 $remove_function = false;
221
222 // run on wpstg page
223 if (isset($_GET['page']) && $_GET['page'] == 'wpstg_clone') {
224 $remove_function = true;
225 }
226 // run on wpstg ajax requests
227 if (defined('DOING_AJAX') && DOING_AJAX && isset($_POST['action']) && strpos(sanitize_text_field($_POST['action']), 'wpstg') !== false) {
228 $remove_function = true;
229 }
230
231 if ($remove_function) {
232 global $wp_filter;
233 $admin_init_functions = $wp_filter['admin_init'];
234 foreach ($admin_init_functions as $priority => $functions) {
235 foreach ($functions as $key => $function) {
236 // searching for function this way as can't rely on the calling class being named TGM_Plugin_Activation
237 if (strpos($key, 'force_activation') !== false) {
238 unset($wp_filter['admin_init'][$priority][$key]);
239
240 return;
241 }
242 }
243 }
244 }
245 }
246
247 add_action('admin_init', 'wpstgTgmpaCompatibility', 1);
248 }
249
250 if (!function_exists('wpstgIsStaging')) {
251 /**
252 * @return bool True if is staging site. False otherwise.
253 */
254 function wpstgIsStaging()
255 {
256 if (file_exists(ABSPATH . '.wp-staging-cloneable')) {
257 return false;
258 }
259
260 if (get_option("wpstg_is_staging_site") === "true") {
261 return true;
262 }
263
264 if (file_exists(ABSPATH . '.wp-staging')) {
265 return true;
266 }
267
268 return false;
269 }
270 }
271
272 if (!function_exists('wpstgGetCloneSettings')) {
273 /**
274 * Get the value of the given option in clone settings,
275 * If no option given return all clone settings
276 *
277 * @param string $option
278 * @return mixed
279 */
280 function wpstgGetCloneSettings($option = null)
281 {
282 $settings = get_option('wpstg_clone_settings', null);
283
284 // Return settings if no options given
285 if ($option === null) {
286 return $settings;
287 }
288
289 // Early Bail: if settings is null or if settings isn't object
290 if ($settings === null || !is_object($settings)) {
291 return null;
292 }
293
294 // Early bail if given option not exists
295 if (!property_exists($settings, $option)) {
296 return null;
297 }
298
299 return $settings->{$option};
300 }
301 }
302
303 /**
304 * Disable all outgoing e-mails on Staging site
305 * Will check against both the old and new logic of storing emails disabled option
306 */
307 if (wpstgIsStaging() && (((bool)get_option("wpstg_emails_disabled") === true) || ((bool)wpstgGetCloneSettings('wpstg_emails_disabled')))) {
308 if (!function_exists('wp_mail')) {
309 function wp_mail($to, $subject, $message, $headers = '', $attachments = [])
310 {
311 if (defined('WPSTG_DEBUG') && WPSTG_DEBUG) {
312 // Safely cast everything to string
313 $to = is_string($to) ? $to : wp_json_encode($to);
314 $subject = is_string($subject) ? $subject : wp_json_encode($subject);
315 $message = is_string($message) ? $message : wp_json_encode($message);
316 $headers = is_string($headers) ? $headers : wp_json_encode($headers);
317 $attachments = is_string($attachments) ? $attachments : wp_json_encode($attachments);
318
319 $log_entry = <<<LOG_ENTRY
320 /***
321 * WPSTAGING - Mails are disabled for this Staging site. This e-mail was going to be sent, but was prevented:
322 ***
323 * To: $to
324 ***
325 * Subject: $subject
326 ***
327 * Message: $message
328 ***
329 * Headers: $headers
330 ***
331 * Attachments: $attachments
332 ***/
333 LOG_ENTRY;
334
335 error_log($log_entry);
336 }
337 }
338 } else {
339 if (defined('WPSTG_DEBUG') && WPSTG_DEBUG) {
340 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.");
341 }
342 }
343 }
344