class-adbc-common-model.php
7 months ago
class-adbc-cron-jobs.php
2 months ago
class-adbc-database.php
5 months ago
class-adbc-options.php
4 months ago
class-adbc-post-types.php
3 months ago
class-adbc-posts-meta.php
4 months ago
class-adbc-sites.php
7 months ago
class-adbc-tables.php
3 months ago
class-adbc-transients.php
4 months ago
class-adbc-users-meta.php
4 months ago
class-adbc-sites.php
183 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | /** |
| 8 | * ADBC sites class. |
| 9 | * |
| 10 | * This class provides the sites functions. Used to get the sites list and count... |
| 11 | */ |
| 12 | class ADBC_Sites extends ADBC_Singleton { |
| 13 | |
| 14 | private static $prefix_list = []; // Holds the list of prefixes for all tables with the prefix as the key and the site ID as the value. |
| 15 | private static $switched = null; // Holds the blog ID we are currently switched to. Or null if not switched. |
| 16 | |
| 17 | /** |
| 18 | * Override the parent constructor to prepare all tables prefixes. |
| 19 | */ |
| 20 | protected function __construct() { |
| 21 | parent::__construct(); |
| 22 | $this->prepare_all_prefixes(); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Prepare all tables prefixes either for single site or multisite. |
| 27 | * |
| 28 | * @return void |
| 29 | */ |
| 30 | private function prepare_all_prefixes() { |
| 31 | |
| 32 | global $wpdb; |
| 33 | |
| 34 | if ( is_multisite() ) { |
| 35 | |
| 36 | $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs ORDER BY blog_id ASC" ); |
| 37 | |
| 38 | foreach ( $blog_ids as $blog_id ) { |
| 39 | self::$prefix_list[ $wpdb->get_blog_prefix( $blog_id ) ] = (int) $blog_id; |
| 40 | } |
| 41 | |
| 42 | } else { |
| 43 | self::$prefix_list[ $wpdb->prefix ] = get_current_blog_id(); |
| 44 | } |
| 45 | |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get all tables prefixes. |
| 50 | * |
| 51 | * @return array The list of all tables prefixes. |
| 52 | */ |
| 53 | public function get_all_prefixes() { |
| 54 | return self::$prefix_list; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Switch to the given blog ID. |
| 59 | * |
| 60 | * @param int $id The blog ID to switch to. |
| 61 | */ |
| 62 | public function switch_to_blog_id( $id ) { |
| 63 | |
| 64 | $id = (int) $id; |
| 65 | |
| 66 | if ( $id !== get_current_blog_id() ) { |
| 67 | switch_to_blog( $id ); |
| 68 | self::$switched = $id; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Restore the current blog to the original one. |
| 74 | * |
| 75 | * @return void |
| 76 | */ |
| 77 | public function restore_blog() { |
| 78 | if ( self::$switched !== null ) { |
| 79 | restore_current_blog(); |
| 80 | self::$switched = null; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the table prefix from the site ID. |
| 86 | * |
| 87 | * @param int $site_id The site ID. |
| 88 | * @return string|null The table prefix for the given site ID or null if not found. |
| 89 | */ |
| 90 | public function get_prefix_from_site_id( $site_id ) { |
| 91 | |
| 92 | $site_id = (int) $site_id; |
| 93 | |
| 94 | foreach ( self::$prefix_list as $prefix => $id ) { |
| 95 | |
| 96 | if ( $id === $site_id ) |
| 97 | return $prefix; |
| 98 | } |
| 99 | |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get the site ID from the table prefix. |
| 105 | * |
| 106 | * @param string $prefix The table prefix. |
| 107 | * @return int|null The site ID for the given prefix or null if not found. |
| 108 | */ |
| 109 | public function get_site_id_from_prefix( $prefix ) { |
| 110 | return isset( self::$prefix_list[ $prefix ] ) ? self::$prefix_list[ $prefix ] : null; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Count sites in multisite. |
| 115 | * |
| 116 | * @return string|int Formatted site count. |
| 117 | */ |
| 118 | public function count_sites_in_multisite( $return_type = 'int' ) { |
| 119 | |
| 120 | if ( ! is_multisite() ) |
| 121 | return $return_type == 'string' ? __( '1 site', 'advanced-database-cleaner' ) : 1; |
| 122 | |
| 123 | $site_count = get_sites( [ 'count' => true ] ); |
| 124 | |
| 125 | if ( $return_type === 'string' ) |
| 126 | return sprintf( |
| 127 | /* translators: 1: Number of sites */ |
| 128 | _n( '%s site', '%s sites', $site_count, 'advanced-database-cleaner' ), $site_count ); |
| 129 | |
| 130 | return $site_count; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Return a list of sites (network-wide or a single site). |
| 135 | * |
| 136 | * @param string|int $site_id_filter Either "all" or a numeric site-ID (as string/int). |
| 137 | * @return array[] Array of [ id, name, prefix ] rows. |
| 138 | */ |
| 139 | public function get_sites_list( $site_id_filter = 'all' ) { |
| 140 | global $wpdb; |
| 141 | |
| 142 | // Single site |
| 143 | if ( ! is_multisite() ) { |
| 144 | return [ |
| 145 | [ |
| 146 | 'id' => get_current_blog_id(), |
| 147 | 'name' => get_bloginfo( 'name' ), |
| 148 | 'prefix' => $wpdb->prefix, |
| 149 | ], |
| 150 | ]; |
| 151 | } |
| 152 | |
| 153 | // Forced site ID in multisite |
| 154 | if ( $site_id_filter !== 'all' ) { |
| 155 | |
| 156 | $site = get_site( $site_id_filter ); |
| 157 | if ( $site instanceof WP_Site ) { |
| 158 | return [ |
| 159 | [ |
| 160 | 'id' => $site->blog_id, |
| 161 | 'name' => get_blog_option( $site->blog_id, 'blogname' ), |
| 162 | 'prefix' => $wpdb->get_blog_prefix( $site->blog_id ), |
| 163 | ], |
| 164 | ]; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Get all sites in multisite |
| 169 | $sites = get_sites( [ 'number' => 0 ] ); // load all |
| 170 | $results = []; |
| 171 | |
| 172 | foreach ( $sites as $site ) { |
| 173 | $results[] = [ |
| 174 | 'id' => $site->blog_id, |
| 175 | 'name' => get_blog_option( $site->blog_id, 'blogname' ), |
| 176 | 'prefix' => $wpdb->get_blog_prefix( $site->blog_id ), |
| 177 | ]; |
| 178 | } |
| 179 | |
| 180 | return $results; |
| 181 | } |
| 182 | |
| 183 | } |