Database
2 years ago
Database.php
2 years ago
DateTimeAdapter.php
4 years ago
Directory.php
2 years ago
Maintenance.php
4 years ago
PhpAdapter.php
2 years ago
SourceDatabase.php
2 years ago
WpAdapter.php
3 years ago
WpAdapter.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Adapter; |
| 4 | |
| 5 | /** |
| 6 | * Class WP |
| 7 | * Adapter to maintain wordpress core function for WP backward compatibility support and deprecated functions |
| 8 | * |
| 9 | * @package WPStaging\Framework\Adapter |
| 10 | */ |
| 11 | class WpAdapter |
| 12 | { |
| 13 | /** |
| 14 | * Is the current request doing some ajax |
| 15 | * Alternative to wp_doing_ajax() as it is not available for WP < 4.7 |
| 16 | * This implementation is without hooks |
| 17 | * |
| 18 | * @return bool |
| 19 | */ |
| 20 | public function doingAjax() |
| 21 | { |
| 22 | return defined('DOING_AJAX') && DOING_AJAX; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @return bool |
| 27 | */ |
| 28 | public function isWpCliRequest() |
| 29 | { |
| 30 | return defined('WP_CLI') && WP_CLI; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Alternative to is_plugin_active. |
| 35 | * WordPress is_plugin_active is not available until admin_init hook, |
| 36 | * We needs its functionality before that. |
| 37 | * |
| 38 | * @param string $plugin |
| 39 | * @return bool |
| 40 | */ |
| 41 | public function isPluginActive($plugin) |
| 42 | { |
| 43 | // Prevent filters tampering with the active plugins list, such as wpstg-optimizer.php itself. |
| 44 | remove_all_filters('option_active_plugins'); |
| 45 | return in_array($plugin, (array) get_option('active_plugins', [])) || $this->isPluginNetworkActive($plugin); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Alternative to is_plugin_active_for_network. |
| 50 | * WordPress is_plugin_active_for_network is not available until admin_init hook, |
| 51 | * We needs its functionality before that. |
| 52 | * |
| 53 | * @param string $plugin |
| 54 | * @return bool |
| 55 | */ |
| 56 | public function isPluginNetworkActive($plugin) |
| 57 | { |
| 58 | if (!is_multisite()) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | // Prevent filters tampering with the active plugins list, such as wpstg-optimizer.php itself. |
| 63 | remove_all_filters('site_option_active_sitewide_plugins'); |
| 64 | $plugins = get_site_option('active_sitewide_plugins'); |
| 65 | if (isset($plugins[$plugin])) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Return the current network id |
| 74 | * Use get_current_network_id in WP >= 4.6 |
| 75 | * Use get_current_site()->id in WP < 4.6, >= 3.7 |
| 76 | * Use get_current_site()->ID in WP < 3.7 |
| 77 | * |
| 78 | * @return int |
| 79 | */ |
| 80 | public function getCurrentNetworkId() |
| 81 | { |
| 82 | // Early bail if not multisite |
| 83 | if (!is_multisite()) { |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | // For WP >= 4.6 |
| 88 | if (is_callable('get_current_network_id')) { |
| 89 | return get_current_network_id(); |
| 90 | } |
| 91 | |
| 92 | // If get_current_site is not available return 1 |
| 93 | if (!is_callable('get_current_site')) { |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | $currentSite = get_current_site(); |
| 98 | |
| 99 | // For WP >= 3.7 and < 4.6 |
| 100 | if (property_exists($currentSite, 'id')) { |
| 101 | $currentSite->id; |
| 102 | } |
| 103 | |
| 104 | // For WP < 3.7 |
| 105 | if (property_exists($currentSite, 'ID')) { |
| 106 | $currentSite->ID; |
| 107 | } |
| 108 | |
| 109 | return 1; |
| 110 | } |
| 111 | } |
| 112 |