WpMultisite.php
6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
namespace AIOSEO\Plugin\Common\Traits\Helpers;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Contains methods related to multisite.
*
* @since 4.2.5
*/
trait WpMultisite {
/**
* Returns the network ID.
*
* @since 4.2.5
*
* @return int The integer of the blog/site id.
*/
public function getNetworkId() {
if ( is_multisite() ) {
return get_network()->site_id;
}
return get_current_blog_id();
}
/**
* Get a site (with aliases) by it's blog ID.
*
* @since 4.2.5
*
* @param int $blogId The blog ID.
* @return WP_Site|null The site.
*/
public function getSiteByBlogId( $blogId ) {
$sites = $this->getSites();
foreach ( $sites['sites'] as $site ) {
if ( $site->blog_id === $blogId ) {
return $site;
}
}
return null;
}
/**
* Get the current site.
*
* @since 4.2.5
*
* @return \WP_Site|Object A WP_Site instance of the current site or an object representing the same.
*/
public function getSite() {
if ( is_multisite() ) {
return get_site();
}
return (object) [
'domain' => $this->getSiteDomain(),
'path' => $this->getHomePath()
];
}
/**
* Get all sites in the multisite network.
*
* @since 4.2.5
*
* @param int|string $limit The number of sites to get or 'all'.
* @param int $offset The offset to start at.
* @param null|string $searchTerm The search term to look for.
* @param null|string $filter A filter to look up sites by.
* @param null|string $orderBy The column to order results by. Defaults to null.
* @param string $orderDir The direction to order results by. Defaults to 'DESC'.
* @return array An array of sites.
*/
public function getSites( $limit = 'all', $offset = 0, $searchTerm = null, $filter = 'all', $orderBy = null, $orderDir = 'DESC' ) {
$countSites = wp_count_sites();
$sites = get_sites( [
'network_id' => get_current_network_id(),
'number' => $countSites['public'],
'public' => 1
] );
$allSites = [];
foreach ( $sites as $site ) {
$clonedSite = clone $site;
$clonedSite->adminUrl = get_admin_url( $site->blog_id );
$clonedSite->homeUrl = get_home_url( $site->blog_id );
if ( $this->includeSite( $clonedSite, $filter ) ) {
$allSites[] = $clonedSite;
}
// We need to look up aliases for Mercator, this checks to see if it's even enabled.
if ( ! class_exists( '\Mercator\Mapping' ) ) {
continue;
}
$aliases = $this->getSiteAliases( $site );
foreach ( $aliases as $alias ) {
$aliasSite = clone $clonedSite;
$aliasSite->domain = $alias['domain'];
$aliasSite->path = '/';
$aliasSite->alias = $alias;
$aliasSite->parentDomain = $site->domain;
$aliasSite->parentPath = $site->path;
if ( $this->includeSite( $aliasSite, $filter ) ) {
$allSites[] = $aliasSite;
}
}
}
// If we have a search term, let's filter down these results.
if ( ! empty( $searchTerm ) ) {
foreach ( $allSites as $key => $site ) {
$keep = false;
if (
false !== stripos( $site->domain, $searchTerm ) ||
false !== stripos( $site->path, $searchTerm ) ||
false !== stripos( $site->parentDomain, $searchTerm ) ||
false !== stripos( $site->parentPath, $searchTerm )
) {
$keep = true;
}
if ( ! $keep ) {
unset( $allSites[ $key ] );
}
}
}
// Ordering the sites.
if ( ! empty( $orderBy ) ) {
usort( $allSites, function( $site1, $site2 ) use ( $orderBy, $orderDir ) {
if ( empty( $site1->{ $orderBy } ) ) {
return 0;
}
return 'ASC' === strtoupper( $orderDir )
? ( $site1->{ $orderBy } > $site2->{ $orderBy } ? 1 : 0 )
: ( $site1->{ $orderBy } < $site2->{ $orderBy } ? 1 : 0 );
} );
}
return [
'total' => count( $allSites ),
'limit' => $limit,
'sites' => 'all' === $limit ? $allSites : array_slice( $allSites, $offset, $limit )
];
}
/**
* Filter sites based on a passed in filter. Options include 'all', 'activated' or 'deactivated'.
*
* @since 4.2.5
*
* @param Object $site The site object.
* @param string $filter The filter to use.
* @return bool The site if allowed or null if not.
*/
private function includeSite( $site, $filter ) {
if ( 'all' === $filter ) {
return true;
}
static $activeSites = null;
if ( null === $activeSites ) {
$activeSites = json_decode( aioseo()->internalNetworkOptions->internal->sites->active );
}
$siteIsActive = false;
foreach ( $activeSites as $as ) {
if ( $as->domain === $site->domain && $as->path === $site->path ) {
$siteIsActive = true;
}
}
if (
( 'deactivated' === $filter && ! $siteIsActive ) ||
( 'activated' === $filter && $siteIsActive )
) {
return true;
}
return false;
}
/**
* Get an array of aliases for a WP_Site.
*
* @since 4.2.5
*
* @param \WP_Site $site The Site.
* @return array An array of aliases.
*/
public function getSiteAliases( $site ) {
// We need to look up aliases for Mercator, this checks to see if it's even enabled.
if ( ! class_exists( '\Mercator\Mapping' ) ) {
return [];
}
$aliases = \Mercator\Mapping::get_by_site( $site->blog_id );
if ( empty( $aliases ) ) {
return [];
}
$aliasData = [];
foreach ( $aliases as $alias ) {
$aliasData[] = [
'alias_id' => $alias->get_id(),
'domain' => $alias->get_domain(),
'active' => $alias->is_active()
];
}
return $aliasData;
}
/**
* Wrapper for switch_to_blog especially for non-multisite setups.
*
* @since 4.2.5
*
* @param int $blogId The blog ID to switch to.
* @return bool Always returns true.
*/
public function switchToBlog( $blogId ) {
if ( ! is_multisite() ) {
return true;
}
return switch_to_blog( $blogId );
}
/**
* Wrapper for restore_current_blog especially for non-multisite setups.
*
* @since 4.2.5
*
* @return bool True on success, false if we're already on the current blog or not in a multisite environment.
*/
public function restoreCurrentBlog() {
if ( ! is_multisite() ) {
return false;
}
return restore_current_blog();
}
/**
* Checks if the current plugin is network activated.
*
* @since 4.2.8
*
* @param string|null $plugin The plugin to check for network activation.
* @return bool True if network activated, false if not.
*/
public function isPluginNetworkActivated( $plugin = null ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( ! is_multisite() ) {
return false;
}
$plugin = $plugin ? $plugin : plugin_basename( AIOSEO_FILE );
// If the plugin is not network activated, then no it's not network licensed.
if ( ! is_plugin_active_for_network( $plugin ) ) {
return false;
}
return true;
}
}