$value) { if (strpos($key, $pluginFileName) !== false) { return $key; } } return false; } } if (!function_exists('wpstgGetPluginData')) { /** * @param string $pluginFileName * * @return array */ function wpstgGetPluginData(string $pluginFileName): array { $allPlugins = get_plugins(); foreach ($allPlugins as $key => $value) { if (strpos($key, $pluginFileName) !== false) { return $value; } } return []; } } if (!function_exists('wpstgGetFreeVersionNumberIfInstalled')) { /** * @return string returns empty string if free is not installed. */ function wpstgGetFreeVersionNumberIfInstalled(): string { $freeData = wpstgGetPluginData(WPSTG_FREE_VERSION_PLUGIN_FILE); $installedFreeVersionNumber = isset($freeData['Version']) ? $freeData['Version'] : ''; return $installedFreeVersionNumber; } } if (!function_exists('wpstgGetProVersionNumberIfInstalled')) { /** * @return string returns empty string if pro is not installed. */ function wpstgGetProVersionNumberIfInstalled(): string { $freeData = wpstgGetPluginData(WPSTG_PRO_VERSION_PLUGIN_FILE); $installedFreeVersionNumber = isset($freeData['Version']) ? $freeData['Version'] : ''; return $installedFreeVersionNumber; } } if (!function_exists('wpstgIsFreeVersionCompatible')) { /** * @return bool */ function wpstgIsFreeVersionCompatible(): bool { return defined('WPSTGPRO_MINIMUM_FREE_VERSION') && version_compare(wpstgGetFreeVersionNumberIfInstalled(), WPSTGPRO_MINIMUM_FREE_VERSION, '>='); } } if (!function_exists('wpstgIsFreeActiveButOutdated')) { /** * @return bool */ function wpstgIsFreeActiveButOutdated(): bool { if (wpstgIsFreeActiveInNetworkOrCurrentSite() && !wpstgIsFreeVersionCompatible()) { return true; } return false; } } if (!function_exists('wpstgDeactivatePlugin')) { /** * @param mixed $pluginFilePath * @return void */ function wpstgDeactivatePlugin($pluginFilePath) { if (is_network_admin()) { deactivate_plugins($pluginFilePath, false, true); } else { deactivate_plugins($pluginFilePath); } } } if (!function_exists('wpstgCanShowAnotherInstanceRunningNotice')) { /** * @param string $pluginFilePath * @return bool */ function wpstgCanShowAnotherInstanceRunningNotice(string $pluginFilePath): bool { if (!current_user_can('activate_plugins')) { return false; } if (strpos($pluginFilePath, 'wp-staging-pro.php') !== false && wpstgIsProActiveInNetworkOrInCurrentSite() && !wpstgIsFreeActiveInNetworkOrCurrentSite()) { return true; } if (strpos($pluginFilePath, 'wp-staging.php') !== false && !wpstgIsProActiveInNetworkOrInCurrentSite() && wpstgIsFreeActiveInNetworkOrCurrentSite()) { return true; } return false; } } if (!function_exists('wpstgCanThrowAnotherInstanceLoadedException')) { /** * @param string $pluginFilePath * @return bool */ function wpstgCanThrowAnotherInstanceLoadedException(string $pluginFilePath = ''): bool { if (defined('WPSTG_VERSION') && version_compare(WPSTG_VERSION, WPSTGPRO_MINIMUM_FREE_VERSION, '<')) { return true; } if (defined('WPSTGPRO_VERSION') && version_compare(WPSTGPRO_VERSION, '5.1.0', '<')) { return true; } if (!wpstgIsProActiveInNetworkOrInCurrentSite() && strpos($pluginFilePath, 'wp-staging-pro.php') === false) { return true; } return false; } } if (!function_exists('wpstgIsPluginActivated')) { /** * This function checks if a plugin is activated on single site. * * @param string $pluginFileName * * @return bool */ function wpstgIsPluginActivated(string $pluginFileName): bool { $activePlugins = wp_get_active_and_valid_plugins(); foreach ($activePlugins as $sitewidePlugin) { if (strpos($sitewidePlugin, $pluginFileName) !== false) { return true; } } return false; } } if (!function_exists('wpstgIsPluginActiveInNetwork')) { /** * @param string $pluginFileName * * @return bool */ function wpstgIsPluginActiveInNetwork(string $pluginFileName): bool { if (!is_multisite()) { return false; } $activePlugins = wp_get_active_network_plugins(); foreach ($activePlugins as $sitewidePlugin) { if (strpos($sitewidePlugin, $pluginFileName) !== false) { return true; } } return false; } } if (!function_exists('wpstgDoLoadPluginAutoLoad')) { /** * @param string $pluginFilePath * @return void */ function wpstgDoLoadPluginAutoLoad(string $pluginFilePath): bool { if (class_exists('\WPStaging\Core\WPStaging')) { return false; } if (strpos($pluginFilePath, 'wp-staging.php') === false) { return true; } if (strpos($pluginFilePath, 'wp-staging.php') !== false && (!is_network_admin() && !wpstgIsProPluginActive())) { return true; } if (strpos($pluginFilePath, 'wp-staging.php') !== false && (is_network_admin() && !wpstgIsProPluginActiveInNetwork())) { return true; } return false; } } /** * Early bail: Deactivate outdated free version. */ if (strpos($pluginFilePath, 'wp-staging-pro.php') !== false && wpstgIsFreeActiveButOutdated()) { // Deactivate free plugin. $pluginSlug = wpstgGetPluginSlug(WPSTG_FREE_VERSION_PLUGIN_FILE); wpstgDeactivatePlugin($pluginSlug); } /** * Early bail: Activating another WPSTAGING Plugin. * This is the only scenario where the plugin would be included after "plugins_loaded", * therefore we need to detect earlier, from the context of the request, whether this is going to happen, * to disable this plugin early and bail the bootstrap process to not conflict with the one being activated. * * Covers both clicking on the "Activate" button and selecting the "Activate" bulk-action. */ if (isset($_REQUEST['action'])) { switch ($_REQUEST['action']) : case 'activate': case 'error_scrape': if (isset($_REQUEST['plugin'])) { $plugin = (string)wp_unslash(sanitize_text_field($_REQUEST['plugin'])); $isActivatingWpStaging = strpos($plugin, 'wp-staging.php') || strpos($plugin, 'wp-staging-pro.php'); $isActivatingAnotherWpStaging = plugin_basename($plugin) !== plugin_basename($pluginFilePath); if ($isActivatingWpStaging && $isActivatingAnotherWpStaging && wpstgCanThrowAnotherInstanceLoadedException($plugin) && current_user_can('deactivate_plugin', plugin_basename($pluginFilePath))) { throw new Exception("Activating another WPSTAGING Plugin. Plugin that bailed bootstrapping: $pluginFilePath"); } } break; case 'activate-selected': case 'activate-multi': if (isset($_REQUEST['checked'])) { $plugins = array_map('sanitize_text_field', (array)wp_unslash($_REQUEST['checked'])); foreach ($plugins as $i => $plugin) { $isActivatingWpStaging = strpos($plugin, 'wp-staging.php') || strpos($plugin, 'wp-staging-pro.php'); $isActivatingAnotherWpStaging = plugin_basename($plugin) !== plugin_basename($pluginFilePath); if ($isActivatingWpStaging && $isActivatingAnotherWpStaging && wpstgCanThrowAnotherInstanceLoadedException($plugin) && current_user_can('deactivate_plugin', plugin_basename($pluginFilePath))) { throw new Exception("Activating another WPSTAGING Plugin. Plugin that bailed bootstrapping: $pluginFilePath"); } } } break; endswitch; } /** * Early bail: Another instance of WPSTAGING active. */ if ( // WPSTAGING <= 2.7.5 class_exists('\WPStaging\WPStaging') || // WPSTAGING >= 2.7.6 class_exists('\WPStaging\Core\WPStaging') ) { if (wpstgCanShowAnotherInstanceRunningNotice($pluginFilePath)) { add_action(is_network_admin() ? 'wpstg.network_admin_notices' : 'wpstg.admin_notices', function () { // phpcs:ignore WPStaging.Security.FirstArgNotAString, WPStaging.Security.AuthorizationChecked echo '
' . esc_html__('WP STAGING Already Active', 'wp-staging') . '
'; echo '' . esc_html__('Another WP STAGING is already activated, please leave only one instance of the WP STAGING plugin active at the same time.', 'wp-staging') . '
'; echo '' . esc_html__('WP STAGING', 'wp-staging') . '
'; echo '' . sprintf(esc_html__('WP STAGING requires at least WordPress %s to run. You have WordPress %s.', 'wp-staging'), esc_html($minimumWordPressVersion), esc_html($currentWordPressVersion)) . '
'; echo '