| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Main network class |
| | 5 | * |
| | 6 | * This class contains the values and functional methods for interacting with |
| | 7 | * a network of WordPress sites. Traditionally used for multisite, this class |
| | 8 | * is used by the $current_site global to setup the current network |
| | 9 | * |
| | 10 | * This class is most useful in WordPress multi-network installations where the |
| | 11 | * ability to interact with any network of sites is required. |
| | 12 | * |
| | 13 | * @since 4.3.0 |
| | 14 | */ |
| | 15 | class WP_Network { |
| | 16 | |
| | 17 | /** |
| | 18 | * Unique numerical ID. |
| | 19 | * |
| | 20 | * @since 4.3.0 |
| | 21 | * |
| | 22 | * @var int |
| | 23 | */ |
| | 24 | public $id = 0; |
| | 25 | |
| | 26 | /** |
| | 27 | * Domain of this network. |
| | 28 | * |
| | 29 | * @since 4.3.0 |
| | 30 | * |
| | 31 | * @var string |
| | 32 | */ |
| | 33 | public $domain = ''; |
| | 34 | |
| | 35 | /** |
| | 36 | * Path of this network. |
| | 37 | * |
| | 38 | * @since 4.3.0 |
| | 39 | * |
| | 40 | * @var string |
| | 41 | */ |
| | 42 | public $path = ''; |
| | 43 | |
| | 44 | /** |
| | 45 | * The ID of the main site mapped to this network. |
| | 46 | * |
| | 47 | * Named "blog" vs. "site" for legacy reasons. |
| | 48 | * |
| | 49 | * @since 4.3.0 |
| | 50 | * |
| | 51 | * @var int |
| | 52 | */ |
| | 53 | public $blog_id = 0; |
| | 54 | |
| | 55 | /** |
| | 56 | * Domain used for cookies for this network. |
| | 57 | * |
| | 58 | * @since 4.3.0 |
| | 59 | * |
| | 60 | * @var int |
| | 61 | */ |
| | 62 | public $cookie_domain = ''; |
| | 63 | |
| | 64 | /** |
| | 65 | * Create a new WP_Network object |
| | 66 | * |
| | 67 | * Will populate all relevant data if a network ID or object is passed |
| | 68 | * |
| | 69 | * @since 4.3.0 |
| | 70 | * |
| | 71 | * @param mixed $network stdClass object or network ID |
| | 72 | */ |
| | 73 | public function __construct( $network = false ) { |
| | 74 | |
| | 75 | // Bail if not populating from an existing network. |
| | 76 | if ( empty( $network ) ) { |
| | 77 | return false; |
| | 78 | } |
| | 79 | |
| | 80 | // Get the network ID. |
| | 81 | if ( is_object( $network ) && isset( $network->id ) ) { |
| | 82 | $network_id = $network->id; |
| | 83 | } elseif ( is_numeric( $network ) ) { |
| | 84 | $network_id = (int) $network; |
| | 85 | } else { |
| | 86 | return false; |
| | 87 | } |
| | 88 | |
| | 89 | $this->populate( $network_id ); |
| | 90 | |
| | 91 | return $this; |
| | 92 | } |
| | 93 | |
| | 94 | /** |
| | 95 | * @since 4.3.0 |
| | 96 | * |
| | 97 | * @param int $network_id |
| | 98 | * @return bool |
| | 99 | */ |
| | 100 | public function populate( $network_id = 0 ) { |
| | 101 | // Query for network |
| | 102 | $network = self::get_by_id( $network_id ); |
| | 103 | |
| | 104 | // Bail if no network to update |
| | 105 | if ( false === $network ) { |
| | 106 | return false; |
| | 107 | } |
| | 108 | |
| | 109 | // Set the main data |
| | 110 | $this->set_main_data( $network ); |
| | 111 | $this->set_cookie_domain(); |
| | 112 | } |
| | 113 | |
| | 114 | /** |
| | 115 | * Set the network ID, domain, and path |
| | 116 | * |
| | 117 | * @since 4.3.0 |
| | 118 | */ |
| | 119 | private function set_main_data( $network ) { |
| | 120 | $this->id = $network->id; |
| | 121 | $this->domain = $network->domain; |
| | 122 | $this->path = $network->path; |
| | 123 | } |
| | 124 | |
| | 125 | /** |
| | 126 | * Set the cookie domain, based on the network domain |
| | 127 | * |
| | 128 | * @todo What if the domain of the network doesn't match the current site? |
| | 129 | * |
| | 130 | * @since WordPress (4.3.0) |
| | 131 | */ |
| | 132 | private function set_cookie_domain() { |
| | 133 | $this->cookie_domain = $this->domain; |
| | 134 | if ( 'www.' === substr( $this->cookie_domain, 0, 4 ) ) { |
| | 135 | $this->cookie_domain = substr( $this->cookie_domain, 4 ); |
| | 136 | } |
| | 137 | } |
| | 138 | |
| | 139 | /** |
| | 140 | * Retreive a network from the database by its ID. |
| | 141 | * |
| | 142 | * @since 4.3.0 |
| | 143 | * |
| | 144 | * @param int $network_id |
| | 145 | * |
| | 146 | * @return bool|mixed |
| | 147 | */ |
| | 148 | public static function get_by_id( $network_id = 0 ) { |
| | 149 | global $wpdb; |
| | 150 | |
| | 151 | // Check cache |
| | 152 | $network = wp_cache_get( $network_id, 'networks' ); |
| | 153 | |
| | 154 | // Cache miss |
| | 155 | if ( false === $network ) { |
| | 156 | |
| | 157 | // Query for network data |
| | 158 | $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d", $network_id ) ); |
| | 159 | |
| | 160 | // Bail if network cannot be found |
| | 161 | if ( empty( $network ) || is_wp_error( $network ) ) { |
| | 162 | $network = false; |
| | 163 | } |
| | 164 | |
| | 165 | // Add network to cache |
| | 166 | wp_cache_add( $network_id, $network, 'networks' ); |
| | 167 | } |
| | 168 | |
| | 169 | // Return the network |
| | 170 | return $network; |
| | 171 | } |
| | 172 | } |
| | 173 | No newline at end of file |