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