PageRenderTime 58ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/ms-functions.php

https://bitbucket.org/skyarch-iijima/wordpress
PHP | 2744 lines | 1117 code | 329 blank | 1298 comment | 260 complexity | 347598ec1d6512d32f1916404c32653a MD5 | raw file
  1. <?php
  2. /**
  3. * Multisite WordPress API
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 3.0.0
  8. */
  9. /**
  10. * Gets the network's site and user counts.
  11. *
  12. * @since MU (3.0.0)
  13. *
  14. * @return array Site and user count for the network.
  15. */
  16. function get_sitestats() {
  17. $stats = array(
  18. 'blogs' => get_blog_count(),
  19. 'users' => get_user_count(),
  20. );
  21. return $stats;
  22. }
  23. /**
  24. * Get one of a user's active blogs
  25. *
  26. * Returns the user's primary blog, if they have one and
  27. * it is active. If it's inactive, function returns another
  28. * active blog of the user. If none are found, the user
  29. * is added as a Subscriber to the Dashboard Blog and that blog
  30. * is returned.
  31. *
  32. * @since MU (3.0.0)
  33. *
  34. * @param int $user_id The unique ID of the user
  35. * @return WP_Site|void The blog object
  36. */
  37. function get_active_blog_for_user( $user_id ) {
  38. $blogs = get_blogs_of_user( $user_id );
  39. if ( empty( $blogs ) )
  40. return;
  41. if ( ! is_multisite() ) {
  42. return $blogs[ get_current_blog_id() ];
  43. }
  44. $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
  45. $first_blog = current($blogs);
  46. if ( false !== $primary_blog ) {
  47. if ( ! isset( $blogs[ $primary_blog ] ) ) {
  48. update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  49. $primary = get_site( $first_blog->userblog_id );
  50. } else {
  51. $primary = get_site( $primary_blog );
  52. }
  53. } else {
  54. //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
  55. $result = add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );
  56. if ( ! is_wp_error( $result ) ) {
  57. update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  58. $primary = $first_blog;
  59. }
  60. }
  61. if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
  62. $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
  63. $ret = false;
  64. if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
  65. foreach ( (array) $blogs as $blog_id => $blog ) {
  66. if ( $blog->site_id != get_current_network_id() )
  67. continue;
  68. $details = get_site( $blog_id );
  69. if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
  70. $ret = $blog;
  71. if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id )
  72. update_user_meta( $user_id, 'primary_blog', $blog_id );
  73. if ( !get_user_meta($user_id , 'source_domain', true) )
  74. update_user_meta( $user_id, 'source_domain', $blog->domain );
  75. break;
  76. }
  77. }
  78. } else {
  79. return;
  80. }
  81. return $ret;
  82. } else {
  83. return $primary;
  84. }
  85. }
  86. /**
  87. * The number of active users in your installation.
  88. *
  89. * The count is cached and updated twice daily. This is not a live count.
  90. *
  91. * @since MU (3.0.0)
  92. * @since 4.8.0 The $network_id parameter has been added.
  93. *
  94. * @param int|null $network_id ID of the network. Default is the current network.
  95. * @return int Number of active users on the network.
  96. */
  97. function get_user_count( $network_id = null ) {
  98. return get_network_option( $network_id, 'user_count' );
  99. }
  100. /**
  101. * The number of active sites on your installation.
  102. *
  103. * The count is cached and updated twice daily. This is not a live count.
  104. *
  105. * @since MU (3.0.0)
  106. * @since 3.7.0 The $network_id parameter has been deprecated.
  107. * @since 4.8.0 The $network_id parameter is now being used.
  108. *
  109. * @param int|null $network_id ID of the network. Default is the current network.
  110. * @return int Number of active sites on the network.
  111. */
  112. function get_blog_count( $network_id = null ) {
  113. return get_network_option( $network_id, 'blog_count' );
  114. }
  115. /**
  116. * Get a blog post from any site on the network.
  117. *
  118. * @since MU (3.0.0)
  119. *
  120. * @param int $blog_id ID of the blog.
  121. * @param int $post_id ID of the post you're looking for.
  122. * @return WP_Post|null WP_Post on success or null on failure
  123. */
  124. function get_blog_post( $blog_id, $post_id ) {
  125. switch_to_blog( $blog_id );
  126. $post = get_post( $post_id );
  127. restore_current_blog();
  128. return $post;
  129. }
  130. /**
  131. * Adds a user to a blog.
  132. *
  133. * Use the {@see 'add_user_to_blog'} action to fire an event when users are added to a blog.
  134. *
  135. * @since MU (3.0.0)
  136. *
  137. * @param int $blog_id ID of the blog you're adding the user to.
  138. * @param int $user_id ID of the user you're adding.
  139. * @param string $role The role you want the user to have
  140. * @return true|WP_Error
  141. */
  142. function add_user_to_blog( $blog_id, $user_id, $role ) {
  143. switch_to_blog($blog_id);
  144. $user = get_userdata( $user_id );
  145. if ( ! $user ) {
  146. restore_current_blog();
  147. return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
  148. }
  149. /**
  150. * Filters whether a user should be added to a site.
  151. *
  152. * @since 4.9.0
  153. *
  154. * @param bool|WP_Error $retval True if the user should be added to the site, false
  155. * or error object otherwise.
  156. * @param int $user_id User ID.
  157. * @param string $role User role.
  158. * @param int $blog_id Site ID.
  159. */
  160. $can_add_user = apply_filters( 'can_add_user_to_blog', true, $user_id, $role, $blog_id );
  161. if ( true !== $can_add_user ) {
  162. restore_current_blog();
  163. if ( is_wp_error( $can_add_user ) ) {
  164. return $can_add_user;
  165. }
  166. return new WP_Error( 'user_cannot_be_added', __( 'User cannot be added to this site.' ) );
  167. }
  168. if ( !get_user_meta($user_id, 'primary_blog', true) ) {
  169. update_user_meta($user_id, 'primary_blog', $blog_id);
  170. $site = get_site( $blog_id );
  171. update_user_meta( $user_id, 'source_domain', $site->domain );
  172. }
  173. $user->set_role($role);
  174. /**
  175. * Fires immediately after a user is added to a site.
  176. *
  177. * @since MU (3.0.0)
  178. *
  179. * @param int $user_id User ID.
  180. * @param string $role User role.
  181. * @param int $blog_id Blog ID.
  182. */
  183. do_action( 'add_user_to_blog', $user_id, $role, $blog_id );
  184. wp_cache_delete( $user_id, 'users' );
  185. wp_cache_delete( $blog_id . '_user_count', 'blog-details' );
  186. restore_current_blog();
  187. return true;
  188. }
  189. /**
  190. * Remove a user from a blog.
  191. *
  192. * Use the {@see 'remove_user_from_blog'} action to fire an event when
  193. * users are removed from a blog.
  194. *
  195. * Accepts an optional `$reassign` parameter, if you want to
  196. * reassign the user's blog posts to another user upon removal.
  197. *
  198. * @since MU (3.0.0)
  199. *
  200. * @global wpdb $wpdb WordPress database abstraction object.
  201. *
  202. * @param int $user_id ID of the user you're removing.
  203. * @param int $blog_id ID of the blog you're removing the user from.
  204. * @param string $reassign Optional. A user to whom to reassign posts.
  205. * @return true|WP_Error
  206. */
  207. function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
  208. global $wpdb;
  209. switch_to_blog($blog_id);
  210. $user_id = (int) $user_id;
  211. /**
  212. * Fires before a user is removed from a site.
  213. *
  214. * @since MU (3.0.0)
  215. *
  216. * @param int $user_id User ID.
  217. * @param int $blog_id Blog ID.
  218. */
  219. do_action( 'remove_user_from_blog', $user_id, $blog_id );
  220. // If being removed from the primary blog, set a new primary if the user is assigned
  221. // to multiple blogs.
  222. $primary_blog = get_user_meta($user_id, 'primary_blog', true);
  223. if ( $primary_blog == $blog_id ) {
  224. $new_id = '';
  225. $new_domain = '';
  226. $blogs = get_blogs_of_user($user_id);
  227. foreach ( (array) $blogs as $blog ) {
  228. if ( $blog->userblog_id == $blog_id )
  229. continue;
  230. $new_id = $blog->userblog_id;
  231. $new_domain = $blog->domain;
  232. break;
  233. }
  234. update_user_meta($user_id, 'primary_blog', $new_id);
  235. update_user_meta($user_id, 'source_domain', $new_domain);
  236. }
  237. // wp_revoke_user($user_id);
  238. $user = get_userdata( $user_id );
  239. if ( ! $user ) {
  240. restore_current_blog();
  241. return new WP_Error('user_does_not_exist', __('That user does not exist.'));
  242. }
  243. $user->remove_all_caps();
  244. $blogs = get_blogs_of_user($user_id);
  245. if ( count($blogs) == 0 ) {
  246. update_user_meta($user_id, 'primary_blog', '');
  247. update_user_meta($user_id, 'source_domain', '');
  248. }
  249. if ( $reassign != '' ) {
  250. $reassign = (int) $reassign;
  251. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $user_id ) );
  252. $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $user_id ) );
  253. if ( ! empty( $post_ids ) ) {
  254. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id ) );
  255. array_walk( $post_ids, 'clean_post_cache' );
  256. }
  257. if ( ! empty( $link_ids ) ) {
  258. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id ) );
  259. array_walk( $link_ids, 'clean_bookmark_cache' );
  260. }
  261. }
  262. restore_current_blog();
  263. return true;
  264. }
  265. /**
  266. * Get the permalink for a post on another blog.
  267. *
  268. * @since MU (3.0.0) 1.0
  269. *
  270. * @param int $blog_id ID of the source blog.
  271. * @param int $post_id ID of the desired post.
  272. * @return string The post's permalink
  273. */
  274. function get_blog_permalink( $blog_id, $post_id ) {
  275. switch_to_blog( $blog_id );
  276. $link = get_permalink( $post_id );
  277. restore_current_blog();
  278. return $link;
  279. }
  280. /**
  281. * Get a blog's numeric ID from its URL.
  282. *
  283. * On a subdirectory installation like example.com/blog1/,
  284. * $domain will be the root 'example.com' and $path the
  285. * subdirectory '/blog1/'. With subdomains like blog1.example.com,
  286. * $domain is 'blog1.example.com' and $path is '/'.
  287. *
  288. * @since MU (3.0.0)
  289. *
  290. * @global wpdb $wpdb WordPress database abstraction object.
  291. *
  292. * @param string $domain
  293. * @param string $path Optional. Not required for subdomain installations.
  294. * @return int 0 if no blog found, otherwise the ID of the matching blog
  295. */
  296. function get_blog_id_from_url( $domain, $path = '/' ) {
  297. $domain = strtolower( $domain );
  298. $path = strtolower( $path );
  299. $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
  300. if ( $id == -1 ) // blog does not exist
  301. return 0;
  302. elseif ( $id )
  303. return (int) $id;
  304. $args = array(
  305. 'domain' => $domain,
  306. 'path' => $path,
  307. 'fields' => 'ids',
  308. 'number' => 1,
  309. );
  310. $result = get_sites( $args );
  311. $id = array_shift( $result );
  312. if ( ! $id ) {
  313. wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
  314. return 0;
  315. }
  316. wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
  317. return $id;
  318. }
  319. // Admin functions
  320. /**
  321. * Checks an email address against a list of banned domains.
  322. *
  323. * This function checks against the Banned Email Domains list
  324. * at wp-admin/network/settings.php. The check is only run on
  325. * self-registrations; user creation at wp-admin/network/users.php
  326. * bypasses this check.
  327. *
  328. * @since MU (3.0.0)
  329. *
  330. * @param string $user_email The email provided by the user at registration.
  331. * @return bool Returns true when the email address is banned.
  332. */
  333. function is_email_address_unsafe( $user_email ) {
  334. $banned_names = get_site_option( 'banned_email_domains' );
  335. if ( $banned_names && ! is_array( $banned_names ) )
  336. $banned_names = explode( "\n", $banned_names );
  337. $is_email_address_unsafe = false;
  338. if ( $banned_names && is_array( $banned_names ) && false !== strpos( $user_email, '@', 1 ) ) {
  339. $banned_names = array_map( 'strtolower', $banned_names );
  340. $normalized_email = strtolower( $user_email );
  341. list( $email_local_part, $email_domain ) = explode( '@', $normalized_email );
  342. foreach ( $banned_names as $banned_domain ) {
  343. if ( ! $banned_domain )
  344. continue;
  345. if ( $email_domain == $banned_domain ) {
  346. $is_email_address_unsafe = true;
  347. break;
  348. }
  349. $dotted_domain = ".$banned_domain";
  350. if ( $dotted_domain === substr( $normalized_email, -strlen( $dotted_domain ) ) ) {
  351. $is_email_address_unsafe = true;
  352. break;
  353. }
  354. }
  355. }
  356. /**
  357. * Filters whether an email address is unsafe.
  358. *
  359. * @since 3.5.0
  360. *
  361. * @param bool $is_email_address_unsafe Whether the email address is "unsafe". Default false.
  362. * @param string $user_email User email address.
  363. */
  364. return apply_filters( 'is_email_address_unsafe', $is_email_address_unsafe, $user_email );
  365. }
  366. /**
  367. * Sanitize and validate data required for a user sign-up.
  368. *
  369. * Verifies the validity and uniqueness of user names and user email addresses,
  370. * and checks email addresses against admin-provided domain whitelists and blacklists.
  371. *
  372. * The {@see 'wpmu_validate_user_signup'} hook provides an easy way to modify the sign-up
  373. * process. The value $result, which is passed to the hook, contains both the user-provided
  374. * info and the error messages created by the function. {@see 'wpmu_validate_user_signup'}
  375. * allows you to process the data in any way you'd like, and unset the relevant errors if
  376. * necessary.
  377. *
  378. * @since MU (3.0.0)
  379. *
  380. * @global wpdb $wpdb WordPress database abstraction object.
  381. *
  382. * @param string $user_name The login name provided by the user.
  383. * @param string $user_email The email provided by the user.
  384. * @return array Contains username, email, and error messages.
  385. */
  386. function wpmu_validate_user_signup($user_name, $user_email) {
  387. global $wpdb;
  388. $errors = new WP_Error();
  389. $orig_username = $user_name;
  390. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  391. if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) {
  392. $errors->add( 'user_name', __( 'Usernames can only contain lowercase letters (a-z) and numbers.' ) );
  393. $user_name = $orig_username;
  394. }
  395. $user_email = sanitize_email( $user_email );
  396. if ( empty( $user_name ) )
  397. $errors->add('user_name', __( 'Please enter a username.' ) );
  398. $illegal_names = get_site_option( 'illegal_names' );
  399. if ( ! is_array( $illegal_names ) ) {
  400. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  401. add_site_option( 'illegal_names', $illegal_names );
  402. }
  403. if ( in_array( $user_name, $illegal_names ) ) {
  404. $errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) );
  405. }
  406. /** This filter is documented in wp-includes/user.php */
  407. $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
  408. if ( in_array( strtolower( $user_name ), array_map( 'strtolower', $illegal_logins ) ) ) {
  409. $errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) );
  410. }
  411. if ( ! is_email( $user_email ) ) {
  412. $errors->add( 'user_email', __( 'Please enter a valid email address.' ) );
  413. } elseif ( is_email_address_unsafe( $user_email ) ) {
  414. $errors->add( 'user_email', __( 'You cannot use that email address to signup. We are having problems with them blocking some of our email. Please use another email provider.' ) );
  415. }
  416. if ( strlen( $user_name ) < 4 )
  417. $errors->add('user_name', __( 'Username must be at least 4 characters.' ) );
  418. if ( strlen( $user_name ) > 60 ) {
  419. $errors->add( 'user_name', __( 'Username may not be longer than 60 characters.' ) );
  420. }
  421. // all numeric?
  422. if ( preg_match( '/^[0-9]*$/', $user_name ) )
  423. $errors->add('user_name', __('Sorry, usernames must have letters too!'));
  424. $limited_email_domains = get_site_option( 'limited_email_domains' );
  425. if ( is_array( $limited_email_domains ) && ! empty( $limited_email_domains ) ) {
  426. $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
  427. if ( ! in_array( $emaildomain, $limited_email_domains ) ) {
  428. $errors->add('user_email', __('Sorry, that email address is not allowed!'));
  429. }
  430. }
  431. // Check if the username has been used already.
  432. if ( username_exists($user_name) )
  433. $errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
  434. // Check if the email address has been used already.
  435. if ( email_exists($user_email) )
  436. $errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );
  437. // Has someone already signed up for this username?
  438. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  439. if ( $signup != null ) {
  440. $registered_at = mysql2date('U', $signup->registered);
  441. $now = current_time( 'timestamp', true );
  442. $diff = $now - $registered_at;
  443. // If registered more than two days ago, cancel registration and let this signup go through.
  444. if ( $diff > 2 * DAY_IN_SECONDS )
  445. $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
  446. else
  447. $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
  448. }
  449. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  450. if ( $signup != null ) {
  451. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  452. // If registered more than two days ago, cancel registration and let this signup go through.
  453. if ( $diff > 2 * DAY_IN_SECONDS )
  454. $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
  455. else
  456. $errors->add('user_email', __('That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.'));
  457. }
  458. $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
  459. /**
  460. * Filters the validated user registration details.
  461. *
  462. * This does not allow you to override the username or email of the user during
  463. * registration. The values are solely used for validation and error handling.
  464. *
  465. * @since MU (3.0.0)
  466. *
  467. * @param array $result {
  468. * The array of user name, email and the error messages.
  469. *
  470. * @type string $user_name Sanitized and unique username.
  471. * @type string $orig_username Original username.
  472. * @type string $user_email User email address.
  473. * @type WP_Error $errors WP_Error object containing any errors found.
  474. * }
  475. */
  476. return apply_filters( 'wpmu_validate_user_signup', $result );
  477. }
  478. /**
  479. * Processes new site registrations.
  480. *
  481. * Checks the data provided by the user during blog signup. Verifies
  482. * the validity and uniqueness of blog paths and domains.
  483. *
  484. * This function prevents the current user from registering a new site
  485. * with a blogname equivalent to another user's login name. Passing the
  486. * $user parameter to the function, where $user is the other user, is
  487. * effectively an override of this limitation.
  488. *
  489. * Filter {@see 'wpmu_validate_blog_signup'} if you want to modify
  490. * the way that WordPress validates new site signups.
  491. *
  492. * @since MU (3.0.0)
  493. *
  494. * @global wpdb $wpdb
  495. * @global string $domain
  496. *
  497. * @param string $blogname The blog name provided by the user. Must be unique.
  498. * @param string $blog_title The blog title provided by the user.
  499. * @param WP_User|string $user Optional. The user object to check against the new site name.
  500. * @return array Contains the new site data and error messages.
  501. */
  502. function wpmu_validate_blog_signup( $blogname, $blog_title, $user = '' ) {
  503. global $wpdb, $domain;
  504. $current_network = get_network();
  505. $base = $current_network->path;
  506. $blog_title = strip_tags( $blog_title );
  507. $errors = new WP_Error();
  508. $illegal_names = get_site_option( 'illegal_names' );
  509. if ( $illegal_names == false ) {
  510. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  511. add_site_option( 'illegal_names', $illegal_names );
  512. }
  513. /*
  514. * On sub dir installations, some names are so illegal, only a filter can
  515. * spring them from jail.
  516. */
  517. if ( ! is_subdomain_install() ) {
  518. $illegal_names = array_merge( $illegal_names, get_subdirectory_reserved_names() );
  519. }
  520. if ( empty( $blogname ) )
  521. $errors->add('blogname', __( 'Please enter a site name.' ) );
  522. if ( preg_match( '/[^a-z0-9]+/', $blogname ) ) {
  523. $errors->add( 'blogname', __( 'Site names can only contain lowercase letters (a-z) and numbers.' ) );
  524. }
  525. if ( in_array( $blogname, $illegal_names ) )
  526. $errors->add('blogname', __( 'That name is not allowed.' ) );
  527. /**
  528. * Filters the minimum site name length required when validating a site signup.
  529. *
  530. * @since 4.8.0
  531. *
  532. * @param int $length The minimum site name length. Default 4.
  533. */
  534. $minimum_site_name_length = apply_filters( 'minimum_site_name_length', 4 );
  535. if ( strlen( $blogname ) < $minimum_site_name_length ) {
  536. /* translators: %s: minimum site name length */
  537. $errors->add( 'blogname', sprintf( _n( 'Site name must be at least %s character.', 'Site name must be at least %s characters.', $minimum_site_name_length ), number_format_i18n( $minimum_site_name_length ) ) );
  538. }
  539. // do not allow users to create a blog that conflicts with a page on the main blog.
  540. if ( !is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_network->site_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) )
  541. $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
  542. // all numeric?
  543. if ( preg_match( '/^[0-9]*$/', $blogname ) )
  544. $errors->add('blogname', __('Sorry, site names must have letters too!'));
  545. /**
  546. * Filters the new site name during registration.
  547. *
  548. * The name is the site's subdomain or the site's subdirectory
  549. * path depending on the network settings.
  550. *
  551. * @since MU (3.0.0)
  552. *
  553. * @param string $blogname Site name.
  554. */
  555. $blogname = apply_filters( 'newblogname', $blogname );
  556. $blog_title = wp_unslash( $blog_title );
  557. if ( empty( $blog_title ) )
  558. $errors->add('blog_title', __( 'Please enter a site title.' ) );
  559. // Check if the domain/path has been used already.
  560. if ( is_subdomain_install() ) {
  561. $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
  562. $path = $base;
  563. } else {
  564. $mydomain = "$domain";
  565. $path = $base.$blogname.'/';
  566. }
  567. if ( domain_exists($mydomain, $path, $current_network->id) )
  568. $errors->add( 'blogname', __( 'Sorry, that site already exists!' ) );
  569. if ( username_exists( $blogname ) ) {
  570. if ( ! is_object( $user ) || ( is_object($user) && ( $user->user_login != $blogname ) ) )
  571. $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
  572. }
  573. // Has someone already signed up for this domain?
  574. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
  575. if ( ! empty($signup) ) {
  576. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  577. // If registered more than two days ago, cancel registration and let this signup go through.
  578. if ( $diff > 2 * DAY_IN_SECONDS )
  579. $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) );
  580. else
  581. $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
  582. }
  583. $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'user' => $user, 'errors' => $errors);
  584. /**
  585. * Filters site details and error messages following registration.
  586. *
  587. * @since MU (3.0.0)
  588. *
  589. * @param array $result {
  590. * Array of domain, path, blog name, blog title, user and error messages.
  591. *
  592. * @type string $domain Domain for the site.
  593. * @type string $path Path for the site. Used in subdirectory installations.
  594. * @type string $blogname The unique site name (slug).
  595. * @type string $blog_title Blog title.
  596. * @type string|WP_User $user By default, an empty string. A user object if provided.
  597. * @type WP_Error $errors WP_Error containing any errors found.
  598. * }
  599. */
  600. return apply_filters( 'wpmu_validate_blog_signup', $result );
  601. }
  602. /**
  603. * Record site signup information for future activation.
  604. *
  605. * @since MU (3.0.0)
  606. *
  607. * @global wpdb $wpdb WordPress database abstraction object.
  608. *
  609. * @param string $domain The requested domain.
  610. * @param string $path The requested path.
  611. * @param string $title The requested site title.
  612. * @param string $user The user's requested login name.
  613. * @param string $user_email The user's email address.
  614. * @param array $meta Optional. Signup meta data. By default, contains the requested privacy setting and lang_id.
  615. */
  616. function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() ) {
  617. global $wpdb;
  618. $key = substr( md5( time() . wp_rand() . $domain ), 0, 16 );
  619. /**
  620. * Filters the metadata for a site signup.
  621. *
  622. * The metadata will be serialized prior to storing it in the database.
  623. *
  624. * @since 4.8.0
  625. *
  626. * @param array $meta Signup meta data. Default empty array.
  627. * @param string $domain The requested domain.
  628. * @param string $path The requested path.
  629. * @param string $title The requested site title.
  630. * @param string $user The user's requested login name.
  631. * @param string $user_email The user's email address.
  632. * @param string $key The user's activation key.
  633. */
  634. $meta = apply_filters( 'signup_site_meta', $meta, $domain, $path, $title, $user, $user_email, $key );
  635. $wpdb->insert( $wpdb->signups, array(
  636. 'domain' => $domain,
  637. 'path' => $path,
  638. 'title' => $title,
  639. 'user_login' => $user,
  640. 'user_email' => $user_email,
  641. 'registered' => current_time('mysql', true),
  642. 'activation_key' => $key,
  643. 'meta' => serialize( $meta )
  644. ) );
  645. /**
  646. * Fires after site signup information has been written to the database.
  647. *
  648. * @since 4.4.0
  649. *
  650. * @param string $domain The requested domain.
  651. * @param string $path The requested path.
  652. * @param string $title The requested site title.
  653. * @param string $user The user's requested login name.
  654. * @param string $user_email The user's email address.
  655. * @param string $key The user's activation key.
  656. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  657. */
  658. do_action( 'after_signup_site', $domain, $path, $title, $user, $user_email, $key, $meta );
  659. }
  660. /**
  661. * Record user signup information for future activation.
  662. *
  663. * This function is used when user registration is open but
  664. * new site registration is not.
  665. *
  666. * @since MU (3.0.0)
  667. *
  668. * @global wpdb $wpdb WordPress database abstraction object.
  669. *
  670. * @param string $user The user's requested login name.
  671. * @param string $user_email The user's email address.
  672. * @param array $meta Optional. Signup meta data. Default empty array.
  673. */
  674. function wpmu_signup_user( $user, $user_email, $meta = array() ) {
  675. global $wpdb;
  676. // Format data
  677. $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
  678. $user_email = sanitize_email( $user_email );
  679. $key = substr( md5( time() . wp_rand() . $user_email ), 0, 16 );
  680. /**
  681. * Filters the metadata for a user signup.
  682. *
  683. * The metadata will be serialized prior to storing it in the database.
  684. *
  685. * @since 4.8.0
  686. *
  687. * @param array $meta Signup meta data. Default empty array.
  688. * @param string $user The user's requested login name.
  689. * @param string $user_email The user's email address.
  690. * @param string $key The user's activation key.
  691. */
  692. $meta = apply_filters( 'signup_user_meta', $meta, $user, $user_email, $key );
  693. $wpdb->insert( $wpdb->signups, array(
  694. 'domain' => '',
  695. 'path' => '',
  696. 'title' => '',
  697. 'user_login' => $user,
  698. 'user_email' => $user_email,
  699. 'registered' => current_time('mysql', true),
  700. 'activation_key' => $key,
  701. 'meta' => serialize( $meta )
  702. ) );
  703. /**
  704. * Fires after a user's signup information has been written to the database.
  705. *
  706. * @since 4.4.0
  707. *
  708. * @param string $user The user's requested login name.
  709. * @param string $user_email The user's email address.
  710. * @param string $key The user's activation key.
  711. * @param array $meta Signup meta data. Default empty array.
  712. */
  713. do_action( 'after_signup_user', $user, $user_email, $key, $meta );
  714. }
  715. /**
  716. * Send a confirmation request email to a user when they sign up for a new site. The new site will not become active
  717. * until the confirmation link is clicked.
  718. *
  719. * This is the notification function used when site registration
  720. * is enabled.
  721. *
  722. * Filter {@see 'wpmu_signup_blog_notification'} to bypass this function or
  723. * replace it with your own notification behavior.
  724. *
  725. * Filter {@see 'wpmu_signup_blog_notification_email'} and
  726. * {@see 'wpmu_signup_blog_notification_subject'} to change the content
  727. * and subject line of the email sent to newly registered users.
  728. *
  729. * @since MU (3.0.0)
  730. *
  731. * @param string $domain The new blog domain.
  732. * @param string $path The new blog path.
  733. * @param string $title The site title.
  734. * @param string $user_login The user's login name.
  735. * @param string $user_email The user's email address.
  736. * @param string $key The activation key created in wpmu_signup_blog()
  737. * @param array $meta Optional. Signup meta data. By default, contains the requested privacy setting and lang_id.
  738. * @return bool
  739. */
  740. function wpmu_signup_blog_notification( $domain, $path, $title, $user_login, $user_email, $key, $meta = array() ) {
  741. /**
  742. * Filters whether to bypass the new site email notification.
  743. *
  744. * @since MU (3.0.0)
  745. *
  746. * @param string|bool $domain Site domain.
  747. * @param string $path Site path.
  748. * @param string $title Site title.
  749. * @param string $user_login User login name.
  750. * @param string $user_email User email address.
  751. * @param string $key Activation key created in wpmu_signup_blog().
  752. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  753. */
  754. if ( ! apply_filters( 'wpmu_signup_blog_notification', $domain, $path, $title, $user_login, $user_email, $key, $meta ) ) {
  755. return false;
  756. }
  757. // Send email with activation link.
  758. if ( !is_subdomain_install() || get_current_network_id() != 1 )
  759. $activate_url = network_site_url("wp-activate.php?key=$key");
  760. else
  761. $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
  762. $activate_url = esc_url($activate_url);
  763. $admin_email = get_site_option( 'admin_email' );
  764. if ( $admin_email == '' )
  765. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  766. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  767. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  768. $user = get_user_by( 'login', $user_login );
  769. $switched_locale = switch_to_locale( get_user_locale( $user ) );
  770. $message = sprintf(
  771. /**
  772. * Filters the message content of the new blog notification email.
  773. *
  774. * Content should be formatted for transmission via wp_mail().
  775. *
  776. * @since MU (3.0.0)
  777. *
  778. * @param string $content Content of the notification email.
  779. * @param string $domain Site domain.
  780. * @param string $path Site path.
  781. * @param string $title Site title.
  782. * @param string $user_login User login name.
  783. * @param string $user_email User email address.
  784. * @param string $key Activation key created in wpmu_signup_blog().
  785. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  786. */
  787. apply_filters( 'wpmu_signup_blog_notification_email',
  788. __( "To activate your blog, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login.\n\nAfter you activate, you can visit your site here:\n\n%s" ),
  789. $domain, $path, $title, $user_login, $user_email, $key, $meta
  790. ),
  791. $activate_url,
  792. esc_url( "http://{$domain}{$path}" ),
  793. $key
  794. );
  795. // TODO: Don't hard code activation link.
  796. $subject = sprintf(
  797. /**
  798. * Filters the subject of the new blog notification email.
  799. *
  800. * @since MU (3.0.0)
  801. *
  802. * @param string $subject Subject of the notification email.
  803. * @param string $domain Site domain.
  804. * @param string $path Site path.
  805. * @param string $title Site title.
  806. * @param string $user_login User login name.
  807. * @param string $user_email User email address.
  808. * @param string $key Activation key created in wpmu_signup_blog().
  809. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  810. */
  811. apply_filters( 'wpmu_signup_blog_notification_subject',
  812. /* translators: New site notification email subject. 1: Network name, 2: New site URL */
  813. _x( '[%1$s] Activate %2$s', 'New site notification email subject' ),
  814. $domain, $path, $title, $user_login, $user_email, $key, $meta
  815. ),
  816. $from_name,
  817. esc_url( 'http://' . $domain . $path )
  818. );
  819. wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  820. if ( $switched_locale ) {
  821. restore_previous_locale();
  822. }
  823. return true;
  824. }
  825. /**
  826. * Send a confirmation request email to a user when they sign up for a new user account (without signing up for a site
  827. * at the same time). The user account will not become active until the confirmation link is clicked.
  828. *
  829. * This is the notification function used when no new site has
  830. * been requested.
  831. *
  832. * Filter {@see 'wpmu_signup_user_notification'} to bypass this function or
  833. * replace it with your own notification behavior.
  834. *
  835. * Filter {@see 'wpmu_signup_user_notification_email'} and
  836. * {@see 'wpmu_signup_user_notification_subject'} to change the content
  837. * and subject line of the email sent to newly registered users.
  838. *
  839. * @since MU (3.0.0)
  840. *
  841. * @param string $user_login The user's login name.
  842. * @param string $user_email The user's email address.
  843. * @param string $key The activation key created in wpmu_signup_user()
  844. * @param array $meta Optional. Signup meta data. Default empty array.
  845. * @return bool
  846. */
  847. function wpmu_signup_user_notification( $user_login, $user_email, $key, $meta = array() ) {
  848. /**
  849. * Filters whether to bypass the email notification for new user sign-up.
  850. *
  851. * @since MU (3.0.0)
  852. *
  853. * @param string $user_login User login name.
  854. * @param string $user_email User email address.
  855. * @param string $key Activation key created in wpmu_signup_user().
  856. * @param array $meta Signup meta data. Default empty array.
  857. */
  858. if ( ! apply_filters( 'wpmu_signup_user_notification', $user_login, $user_email, $key, $meta ) )
  859. return false;
  860. $user = get_user_by( 'login', $user_login );
  861. $switched_locale = switch_to_locale( get_user_locale( $user ) );
  862. // Send email with activation link.
  863. $admin_email = get_site_option( 'admin_email' );
  864. if ( $admin_email == '' )
  865. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  866. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  867. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  868. $message = sprintf(
  869. /**
  870. * Filters the content of the notification email for new user sign-up.
  871. *
  872. * Content should be formatted for transmission via wp_mail().
  873. *
  874. * @since MU (3.0.0)
  875. *
  876. * @param string $content Content of the notification email.
  877. * @param string $user_login User login name.
  878. * @param string $user_email User email address.
  879. * @param string $key Activation key created in wpmu_signup_user().
  880. * @param array $meta Signup meta data. Default empty array.
  881. */
  882. apply_filters( 'wpmu_signup_user_notification_email',
  883. __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ),
  884. $user_login, $user_email, $key, $meta
  885. ),
  886. site_url( "wp-activate.php?key=$key" )
  887. );
  888. // TODO: Don't hard code activation link.
  889. $subject = sprintf(
  890. /**
  891. * Filters the subject of the notification email of new user signup.
  892. *
  893. * @since MU (3.0.0)
  894. *
  895. * @param string $subject Subject of the notification email.
  896. * @param string $user_login User login name.
  897. * @param string $user_email User email address.
  898. * @param string $key Activation key created in wpmu_signup_user().
  899. * @param array $meta Signup meta data. Default empty array.
  900. */
  901. apply_filters( 'wpmu_signup_user_notification_subject',
  902. /* translators: New user notification email subject. 1: Network name, 2: New user login */
  903. _x( '[%1$s] Activate %2$s', 'New user notification email subject' ),
  904. $user_login, $user_email, $key, $meta
  905. ),
  906. $from_name,
  907. $user_login
  908. );
  909. wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  910. if ( $switched_locale ) {
  911. restore_previous_locale();
  912. }
  913. return true;
  914. }
  915. /**
  916. * Activate a signup.
  917. *
  918. * Hook to {@see 'wpmu_activate_user'} or {@see 'wpmu_activate_blog'} for events
  919. * that should happen only when users or sites are self-created (since
  920. * those actions are not called when users and sites are created
  921. * by a Super Admin).
  922. *
  923. * @since MU (3.0.0)
  924. *
  925. * @global wpdb $wpdb WordPress database abstraction object.
  926. *
  927. * @param string $key The activation key provided to the user.
  928. * @return array|WP_Error An array containing information about the activated user and/or blog
  929. */
  930. function wpmu_activate_signup($key) {
  931. global $wpdb;
  932. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
  933. if ( empty( $signup ) )
  934. return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
  935. if ( $signup->active ) {
  936. if ( empty( $signup->domain ) )
  937. return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
  938. else
  939. return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
  940. }
  941. $meta = maybe_unserialize($signup->meta);
  942. $password = wp_generate_password( 12, false );
  943. $user_id = username_exists($signup->user_login);
  944. if ( ! $user_id )
  945. $user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email);
  946. else
  947. $user_already_exists = true;
  948. if ( ! $user_id )
  949. return new WP_Error('create_user', __('Could not create user'), $signup);
  950. $now = current_time('mysql', true);
  951. if ( empty($signup->domain) ) {
  952. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  953. if ( isset( $user_already_exists ) )
  954. return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
  955. /**
  956. * Fires immediately after a new user is activated.
  957. *
  958. * @since MU (3.0.0)
  959. *
  960. * @param int $user_id User ID.
  961. * @param int $password User password.
  962. * @param array $meta Signup meta data.
  963. */
  964. do_action( 'wpmu_activate_user', $user_id, $password, $meta );
  965. return array( 'user_id' => $user_id, 'password' => $password, 'meta' => $meta );
  966. }
  967. $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, get_current_network_id() );
  968. // TODO: What to do if we create a user but cannot create a blog?
  969. if ( is_wp_error($blog_id) ) {
  970. // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  971. // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  972. if ( 'blog_taken' == $blog_id->get_error_code() ) {
  973. $blog_id->add_data( $signup );
  974. $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
  975. }
  976. return $blog_id;
  977. }
  978. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  979. /**
  980. * Fires immediately after a site is activated.
  981. *
  982. * @since MU (3.0.0)
  983. *
  984. * @param int $blog_id Blog ID.
  985. * @param int $user_id User ID.
  986. * @param int $password User password.
  987. * @param string $signup_title Site title.
  988. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  989. */
  990. do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta );
  991. return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
  992. }
  993. /**
  994. * Create a user.
  995. *
  996. * This function runs when a user self-registers as well as when
  997. * a Super Admin creates a new user. Hook to {@see 'wpmu_new_user'} for events
  998. * that should affect all new users, but only on Multisite (otherwise
  999. * use {@see'user_register'}).
  1000. *
  1001. * @since MU (3.0.0)
  1002. *
  1003. * @param string $user_name The new user's login name.
  1004. * @param string $password The new user's password.
  1005. * @param string $email The new user's email address.
  1006. * @return int|false Returns false on failure, or int $user_id on success
  1007. */
  1008. function wpmu_create_user( $user_name, $password, $email ) {
  1009. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  1010. $user_id = wp_create_user( $user_name, $password, $email );
  1011. if ( is_wp_error( $user_id ) )
  1012. return false;
  1013. // Newly created users have no roles or caps until they are added to a blog.
  1014. delete_user_option( $user_id, 'capabilities' );
  1015. delete_user_option( $user_id, 'user_level' );
  1016. /**
  1017. * Fires immediately after a new user is created.
  1018. *
  1019. * @since MU (3.0.0)
  1020. *
  1021. * @param int $user_id User ID.
  1022. */
  1023. do_action( 'wpmu_new_user', $user_id );
  1024. return $user_id;
  1025. }
  1026. /**
  1027. * Create a site.
  1028. *
  1029. * This function runs when a user self-registers a new site as well
  1030. * as when a Super Admin creates a new site. Hook to {@see 'wpmu_new_blog'}
  1031. * for events that should affect all new sites.
  1032. *
  1033. * On subdirectory installations, $domain is the same as the main site's
  1034. * domain, and the path is the subdirectory name (eg 'example.com'
  1035. * and '/blog1/'). On subdomain installations, $domain is the new subdomain +
  1036. * root domain (eg 'blog1.example.com'), and $path is '/'.
  1037. *
  1038. * @since MU (3.0.0)
  1039. *
  1040. * @param string $domain The new site's domain.
  1041. * @param string $path The new site's path.
  1042. * @param string $title The new site's title.
  1043. * @param int $user_id The user ID of the new site's admin.
  1044. * @param array $meta Optional. Array of key=>value pairs used to set initial site options.
  1045. * If valid status keys are included ('public', 'archived', 'mature',
  1046. * 'spam', 'deleted', or 'lang_id') the given site status(es) will be
  1047. * updated. Otherwise, keys and values will be used to set options for
  1048. * the new site. Default empty array.
  1049. * @param int $network_id Optional. Network ID. Only relevant on multi-network installations.
  1050. * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
  1051. */
  1052. function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $network_id = 1 ) {
  1053. $defaults = array(
  1054. 'public' => 0,
  1055. 'WPLANG' => get_network_option( $network_id, 'WPLANG' ),
  1056. );
  1057. $meta = wp_parse_args( $meta, $defaults );
  1058. $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
  1059. if ( is_subdomain_install() )
  1060. $domain = str_replace( '@', '', $domain );
  1061. $title = strip_tags( $title );
  1062. $user_id = (int) $user_id;
  1063. if ( empty($path) )
  1064. $path = '/';
  1065. // Check if the domain has been used already. We should return an error message.
  1066. if ( domain_exists($domain, $path, $network_id) )
  1067. return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
  1068. if ( ! wp_installing() ) {
  1069. wp_installing( true );
  1070. }
  1071. if ( ! $blog_id = insert_blog($domain, $path, $network_id) )
  1072. return new WP_Error('insert_blog', __('Could not create site.'));
  1073. switch_to_blog($blog_id);
  1074. install_blog($blog_id, $title);
  1075. wp_install_defaults($user_id);
  1076. add_user_to_blog($blog_id, $user_id, 'administrator');
  1077. foreach ( $meta as $key => $value ) {
  1078. if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) )
  1079. update_blog_status( $blog_id, $key, $value );
  1080. else
  1081. update_option( $key, $value );
  1082. }
  1083. update_option( 'blog_public', (int) $meta['public'] );
  1084. if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) )
  1085. update_user_meta( $user_id, 'primary_blog', $blog_id );
  1086. restore_current_blog();
  1087. /**
  1088. * Fires immediately after a new site is created.
  1089. *
  1090. * @since MU (3.0.0)
  1091. *
  1092. * @param int $blog_id Site ID.
  1093. * @param int $user_id User ID.
  1094. * @param string $domain Site domain.
  1095. * @param string $path Site path.
  1096. * @param int $network_id Network ID. Only relevant on multi-network installations.
  1097. * @param array $meta Meta data. Used to set initial site options.
  1098. */
  1099. do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $network_id, $meta );
  1100. wp_cache_set( 'last_changed', microtime(), 'sites' );
  1101. return $blog_id;
  1102. }
  1103. /**
  1104. * Notifies the network admin that a new site has been activated.
  1105. *
  1106. * Filter {@see 'newblog_notify_siteadmin'} to change the content of
  1107. * the notification email.
  1108. *
  1109. * @since MU (3.0.0)
  1110. *
  1111. * @param int $blog_id The new site's ID.
  1112. * @param string $deprecated Not used.
  1113. * @return bool
  1114. */
  1115. function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  1116. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  1117. return false;
  1118. $email = get_site_option( 'admin_email' );
  1119. if ( is_email($email) == false )
  1120. return false;
  1121. $options_site_url = esc_url(network_admin_url('settings.php'));
  1122. switch_to_blog( $blog_id );
  1123. $blogname = get_option( 'blogname' );
  1124. $siteurl = site_url();
  1125. restore_current_blog();
  1126. /* translators: New site notification email. 1: Site URL, 2: User IP address, 3: Settings screen URL */
  1127. $msg = sprintf( __( 'New Site: %1$s
  1128. URL: %2$s
  1129. Remote IP address: %3$s
  1130. Disable these notifications: %4$s' ), $blogname, $siteurl, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
  1131. /**
  1132. * Filters the message body of the new site activation email sent
  1133. * to the network administrator.
  1134. *
  1135. * @since MU (3.0.0)
  1136. *
  1137. * @param string $msg Email body.
  1138. */
  1139. $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
  1140. wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
  1141. return true;
  1142. }
  1143. /**
  1144. * Notifies the network admin that a new user has been activated.
  1145. *
  1146. * Filter {@see 'newuser_notify_siteadmin'} to change the content of
  1147. * the notification email.
  1148. *
  1149. * @since MU (3.0.0)
  1150. *
  1151. * @param int $user_id The new user's ID.
  1152. * @return bool
  1153. */
  1154. function newuser_notify_siteadmin( $user_id ) {
  1155. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  1156. return false;
  1157. $email = get_site_option( 'admin_email' );
  1158. if ( is_email($email) == false )
  1159. return false;
  1160. $user = get_userdata( $user_id );
  1161. $options_site_url = esc_url(network_admin_url('settings.php'));
  1162. /* translators: New user notification email. 1: User login, 2: User IP address, 3: Settings screen URL */
  1163. $msg = sprintf(__('New User: %1$s
  1164. Remote IP address: %2$s
  1165. Disable these notifications: %3$s'), $user->user_login, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
  1166. /**
  1167. * Filters the message body of the new user activation email sent
  1168. * to the network administrator.
  1169. *
  1170. * @since MU (3.0.0)
  1171. *
  1172. * @param string $msg Email body.
  1173. * @param WP_User $user WP_User instance of the new user.
  1174. */
  1175. $msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
  1176. wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
  1177. return true;
  1178. }
  1179. /**
  1180. * Checks whether a site name is already taken.
  1181. *
  1182. * The name is the site's subdomain or the site's subdirectory
  1183. * path depending on the network settings.
  1184. *
  1185. * Used during the new site registration process to ensure
  1186. * that each site name is unique.
  1187. *
  1188. * @since MU (3.0.0)
  1189. *
  1190. * @param string $domain The domain to be checked.
  1191. * @param string $path The path to be checked.
  1192. * @param int $network_id Optional. Network ID. Relevant only on multi-network installations.
  1193. * @return int|null The site ID if the site name exists, null otherwise.
  1194. */
  1195. function domain_exists( $domain, $path, $network_id = 1 ) {
  1196. $path = trailingslashit( $path );
  1197. $args = array(
  1198. 'network_id' => $network_id,
  1199. 'domain' => $domain,
  1200. 'path' => $path,
  1201. 'fields' => 'ids',
  1202. 'number' => 1,
  1203. );
  1204. $result = get_sites( $args );
  1205. $result = array_shift( $result );
  1206. /**
  1207. * Filters whether a site name is taken.
  1208. *
  1209. * The name is the site's subdomain or the site's subdirectory
  1210. * path depending on the network settings.
  1211. *
  1212. * @since 3.5.0
  1213. *
  1214. * @param int|null $result The site ID if the site name exists, null otherwise.
  1215. * @param string $domain Domain to be checked.
  1216. * @param string $path Path to be checked.
  1217. * @param int $network_id Network ID. Relevant only on multi-network installations.
  1218. */
  1219. return apply_filters( 'domain_exists', $result, $domain, $path, $network_id );
  1220. }
  1221. /**
  1222. * Store basic site info in the blogs table.
  1223. *
  1224. * This function creates a row in the wp_blogs table and returns
  1225. * the new blog's ID. It is the first step in creating a new blog.
  1226. *
  1227. * @since MU (3.0.0)
  1228. *
  1229. * @global wpdb $wpdb WordPress database abstraction object.
  1230. *
  1231. * @param string $domain The domain of the new site.
  1232. * @param string $path The path of the new site.
  1233. * @param int $network_id Unless you're running a multi-network installation, be sure to set this value to 1.
  1234. * @return int|false The ID of the new row
  1235. */
  1236. function insert_blog($domain, $path, $network_id) {
  1237. global $wpdb;
  1238. $path = trailingslashit($path);
  1239. $network_id = (int) $network_id;
  1240. $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $network_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
  1241. if ( ! $result )
  1242. return false;
  1243. $blog_id = $wpdb->insert_id;
  1244. clean_blog_cache( $blog_id );
  1245. wp_maybe_update_network_site_counts( $network_id );
  1246. return $blog_id;
  1247. }
  1248. /**
  1249. * Install an empty blog.
  1250. *
  1251. * Creates the new blog tables and options. If calling this function
  1252. * directly, be sure to use switch_to_blog() first, so that $wpdb
  1253. * points to the new blog.
  1254. *
  1255. * @since MU (3.0.0)
  1256. *
  1257. * @global wpdb $wpdb
  1258. * @global WP_Roles $wp_roles
  1259. *
  1260. * @param int $blog_id The value returned by insert_blog().
  1261. * @param string $blog_title The title of the new site.
  1262. */
  1263. function install_blog( $blog_id, $blog_title = '' ) {
  1264. global $wpdb, $wp_roles;
  1265. // Cast for security
  1266. $blog_id = (int) $blog_id;
  1267. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1268. $suppress = $wpdb->suppress_errors();
  1269. if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) )
  1270. die( '<h1>' . __( 'Already Installed' ) . '</h1><p>' . __( 'You appear to have already installed WordPress. To reinstall please clear your old database tables first.' ) . '</p></body></html>' );
  1271. $wpdb->suppress_errors( $suppress );
  1272. $url = get_blogaddress_by_id( $blog_id );
  1273. // Set everything up
  1274. make_db_current_silent( 'blog' );
  1275. populate_options();
  1276. populate_roles();
  1277. // populate_roles() clears previous role definitions so we start over.
  1278. $wp_roles = new WP_Roles();
  1279. $siteurl = $home = untrailingslashit( $url );
  1280. if ( ! is_subdomain_install() ) {
  1281. if ( 'https' === parse_url( get_site_option( 'siteurl' ), PHP_URL_SCHEME ) ) {
  1282. $siteurl = set_url_scheme( $siteurl, 'https' );
  1283. }
  1284. if ( 'https' === parse_url( get_home_url( get_network()->site_id ), PHP_URL_SCHEME ) ) {
  1285. $home = set_url_scheme( $home, 'https' );
  1286. }
  1287. }
  1288. update_option( 'siteurl', $siteurl );
  1289. update_option( 'home', $home );
  1290. if ( get_site_option( 'ms_files_rewriting' ) )
  1291. update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
  1292. else
  1293. update_option( 'upload_path', get_blog_option( get_network()->site_id, 'upload_path' ) );
  1294. update_option( 'blogname', wp_unslash( $blog_title ) );
  1295. update_option( 'admin_email', '' );
  1296. // remove all perms
  1297. $table_prefix = $wpdb->get_blog_prefix();
  1298. delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
  1299. delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
  1300. }
  1301. /**
  1302. * Set blog defaults.
  1303. *
  1304. * This function creates a row in the wp_blogs table.
  1305. *
  1306. * @since MU (3.0.0)
  1307. * @deprecated MU
  1308. * @deprecated Use wp_install_defaults()
  1309. *
  1310. * @global wpdb $wpdb WordPress database abstraction object.
  1311. *
  1312. * @param int $blog_id Ignored in this function.
  1313. * @param int $user_id
  1314. */
  1315. function install_blog_defaults($blog_id, $user_id) {
  1316. global $wpdb;
  1317. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1318. $suppress = $wpdb->suppress_errors();
  1319. wp_install_defaults($user_id);
  1320. $wpdb->suppress_errors( $suppress );
  1321. }
  1322. /**
  1323. * Notify a user that their blog activation has been successful.
  1324. *
  1325. * Filter {@see 'wpmu_welcome_notification'} to disable or bypass.
  1326. *
  1327. * Filter {@see 'update_welcome_email'} and {@see 'update_welcome_subject'} to
  1328. * modify the content and subject line of the notification email.
  1329. *
  1330. * @since MU (3.0.0)
  1331. *
  1332. * @param int $blog_id Blog ID.
  1333. * @param int $user_id User ID.
  1334. * @param string $password User password.
  1335. * @param string $title Site title.
  1336. * @param array $meta Optional. Signup meta data. By default, contains the requested privacy setting and lang_id.
  1337. * @return bool
  1338. */
  1339. function wpmu_welcome_notification( $blog_id, $user_id, $password, $title, $meta = array() ) {
  1340. $current_network = get_network();
  1341. /**
  1342. * Filters whether to bypass the welcome email after site activation.
  1343. *
  1344. * Returning false disables the welcome email.
  1345. *
  1346. * @since MU (3.0.0)
  1347. *
  1348. * @param int|bool $blog_id Blog ID.
  1349. * @param int $user_id User ID.
  1350. * @param string $password User password.
  1351. * @param string $title Site title.
  1352. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  1353. */
  1354. if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) )
  1355. return false;
  1356. $user = get_userdata( $user_id );
  1357. $switched_locale = switch_to_locale( get_user_locale( $user ) );
  1358. $welcome_email = get_site_option( 'welcome_email' );
  1359. if ( $welcome_email == false ) {
  1360. /* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
  1361. $welcome_email = __( 'Howdy USERNAME,
  1362. Your new SITE_NAME site has been successfully set up at:
  1363. BLOG_URL
  1364. You can log in to the administrator account with the following information:
  1365. Username: USERNAME
  1366. Password: PASSWORD
  1367. Log in here: BLOG_URLwp-login.php
  1368. We hope you enjoy your new site. Thanks!
  1369. --The Team @ SITE_NAME' );
  1370. }
  1371. $url = get_blogaddress_by_id($blog_id);
  1372. $welcome_email = str_replace( 'SITE_NAME', $current_network->site_name, $welcome_email );
  1373. $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
  1374. $welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
  1375. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1376. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1377. /**
  1378. * Filters the content of the welcome email after site activation.
  1379. *
  1380. * Content should be formatted for transmission via wp_mail().
  1381. *
  1382. * @since MU (3.0.0)
  1383. *
  1384. * @param string $welcome_email Message body of the email.
  1385. * @param int $blog_id Blog ID.
  1386. * @param int $user_id User ID.
  1387. * @param string $password User password.
  1388. * @param string $title Site title.
  1389. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  1390. */
  1391. $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta );
  1392. $admin_email = get_site_option( 'admin_email' );
  1393. if ( $admin_email == '' )
  1394. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1395. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1396. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1397. $message = $welcome_email;
  1398. if ( empty( $current_network->site_name ) )
  1399. $current_network->site_name = 'WordPress';
  1400. /* translators: New site notification email subject. 1: Network name, 2: New site name */
  1401. $subject = __( 'New %1$s Site: %2$s' );
  1402. /**
  1403. * Filters the subject of the welcome email after site activation.
  1404. *
  1405. * @since MU (3.0.0)
  1406. *
  1407. * @param string $subject Subject of the email.
  1408. */
  1409. $subject = apply_filters( 'update_welcome_subject', sprintf( $subject, $current_network->site_name, wp_unslash( $title ) ) );
  1410. wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  1411. if ( $switched_locale ) {
  1412. restore_previous_locale();
  1413. }
  1414. return true;
  1415. }
  1416. /**
  1417. * Notify a user that their account activation has been successful.
  1418. *
  1419. * Filter {@see 'wpmu_welcome_user_notification'} to disable or bypass.
  1420. *
  1421. * Filter {@see 'update_welcome_user_email'} and {@see 'update_welcome_user_subject'} to
  1422. * modify the content and subject line of the notification email.
  1423. *
  1424. * @since MU (3.0.0)
  1425. *
  1426. * @param int $user_id User ID.
  1427. * @param string $password User password.
  1428. * @param array $meta Optional. Signup meta data. Default empty array.
  1429. * @return bool
  1430. */
  1431. function wpmu_welcome_user_notification( $user_id, $password, $meta = array() ) {
  1432. $current_network = get_network();
  1433. /**
  1434. * Filters whether to bypass the welcome email after user activation.
  1435. *
  1436. * Returning false disables the welcome email.
  1437. *
  1438. * @since MU (3.0.0)
  1439. *
  1440. * @param int $user_id User ID.
  1441. * @param string $password User password.
  1442. * @param array $meta Signup meta data. Default empty array.
  1443. */
  1444. if ( ! apply_filters( 'wpmu_welcome_user_notification', $user_id, $password, $meta ) )
  1445. return false;
  1446. $welcome_email = get_site_option( 'welcome_user_email' );
  1447. $user = get_userdata( $user_id );
  1448. $switched_locale = switch_to_locale( get_user_locale( $user ) );
  1449. /**
  1450. * Filters the content of the welcome email after user activation.
  1451. *
  1452. * Content should be formatted for transmission via wp_mail().
  1453. *
  1454. * @since MU (3.0.0)
  1455. *
  1456. * @param string $welcome_email The message body of the account activation success email.
  1457. * @param int $user_id User ID.
  1458. * @param string $password User password.
  1459. * @param array $meta Signup meta data. Default empty array.
  1460. */
  1461. $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta );
  1462. $welcome_email = str_replace( 'SITE_NAME', $current_network->site_name, $welcome_email );
  1463. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1464. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1465. $welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
  1466. $admin_email = get_site_option( 'admin_email' );
  1467. if ( $admin_email == '' )
  1468. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1469. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1470. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1471. $message = $welcome_email;
  1472. if ( empty( $current_network->site_name ) )
  1473. $current_network->site_name = 'WordPress';
  1474. /* translators: New user notification email subject. 1: Network name, 2: New user login */
  1475. $subject = __( 'New %1$s User: %2$s' );
  1476. /**
  1477. * Filters the subject of the welcome email after user activation.
  1478. *
  1479. * @since MU (3.0.0)
  1480. *
  1481. * @param string $subject Subject of the email.
  1482. */
  1483. $subject = apply_filters( 'update_welcome_user_subject', sprintf( $subject, $current_network->site_name, $user->user_login) );
  1484. wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  1485. if ( $switched_locale ) {
  1486. restore_previous_locale();
  1487. }
  1488. return true;
  1489. }
  1490. /**
  1491. * Get the current network.
  1492. *
  1493. * Returns an object containing the 'id', 'domain', 'path', and 'site_name'
  1494. * properties of the network being viewed.
  1495. *
  1496. * @see wpmu_current_site()
  1497. *
  1498. * @since MU (3.0.0)
  1499. *
  1500. * @global WP_Network $current_site
  1501. *
  1502. * @return WP_Network
  1503. */
  1504. function get_current_site() {
  1505. global $current_site;
  1506. return $current_site;
  1507. }
  1508. /**
  1509. * Get a user's most recent post.
  1510. *
  1511. * Walks through each of a user's blogs to find the post with
  1512. * the most recent post_date_gmt.
  1513. *
  1514. * @since MU (3.0.0)
  1515. *
  1516. * @global wpdb $wpdb WordPress database abstraction object.
  1517. *
  1518. * @param int $user_id
  1519. * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
  1520. */
  1521. function get_most_recent_post_of_user( $user_id ) {
  1522. global $wpdb;
  1523. $user_blogs = get_blogs_of_user( (int) $user_id );
  1524. $most_recent_post = array();
  1525. // Walk through each blog and get the most recent post
  1526. // published by $user_id
  1527. foreach ( (array) $user_blogs as $blog ) {
  1528. $prefix = $wpdb->get_blog_prefix( $blog->userblog_id );
  1529. $recent_post = $wpdb->get_row( $wpdb->prepare("SELECT ID, post_date_gmt FROM {$prefix}posts WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1", $user_id ), ARRAY_A);
  1530. // Make sure we found a post
  1531. if ( isset($recent_post['ID']) ) {
  1532. $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
  1533. // If this is the first post checked or if this post is
  1534. // newer than the current recent post, make it the new
  1535. // most recent post.
  1536. if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
  1537. $most_recent_post = array(
  1538. 'blog_id' => $blog->userblog_id,
  1539. 'post_id' => $recent_post['ID'],
  1540. 'post_date_gmt' => $recent_post['post_date_gmt'],
  1541. 'post_gmt_ts' => $post_gmt_ts
  1542. );
  1543. }
  1544. }
  1545. }
  1546. return $most_recent_post;
  1547. }
  1548. // Misc functions
  1549. /**
  1550. * Get the size of a directory.
  1551. *
  1552. * A helper function that is used primarily to check whether
  1553. * a blog has exceeded its allowed upload space.
  1554. *
  1555. * @since MU (3.0.0)
  1556. *
  1557. * @param string $directory Full path of a directory.
  1558. * @return int Size of the directory in MB.
  1559. */
  1560. function get_dirsize( $directory ) {
  1561. $dirsize = get_transient( 'dirsize_cache' );
  1562. if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
  1563. return $dirsize[ $directory ][ 'size' ];
  1564. if ( ! is_array( $dirsize ) )
  1565. $dirsize = array();
  1566. // Exclude individual site directories from the total when checking the main site,
  1567. // as they are subdirectories and should not be counted.
  1568. if ( is_main_site() ) {
  1569. $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory, $directory . '/sites' );
  1570. } else {
  1571. $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
  1572. }
  1573. set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
  1574. return $dirsize[ $directory ][ 'size' ];
  1575. }
  1576. /**
  1577. * Get the size of a directory recursively.
  1578. *
  1579. * Used by get_dirsize() to get a directory's size when it contains
  1580. * other directories.
  1581. *
  1582. * @since MU (3.0.0)
  1583. * @since 4.3.0 $exclude parameter added.
  1584. *
  1585. * @param string $directory Full path of a directory.
  1586. * @param string $exclude Optional. Full path of a subdirectory to exclude from the total.
  1587. * @return int|false Size in MB if a valid directory. False if not.
  1588. */
  1589. function recurse_dirsize( $directory, $exclude = null ) {
  1590. $size = 0;
  1591. $directory = untrailingslashit( $directory );
  1592. if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) {
  1593. return false;
  1594. }
  1595. if ($handle = opendir($directory)) {
  1596. while(($file = readdir($handle)) !== false) {
  1597. $path = $directory.'/'.$file;
  1598. if ($file != '.' && $file != '..') {
  1599. if (is_file($path)) {
  1600. $size += filesize($path);
  1601. } elseif (is_dir($path)) {
  1602. $handlesize = recurse_dirsize( $path, $exclude );
  1603. if ($handlesize > 0)
  1604. $size += $handlesize;
  1605. }
  1606. }
  1607. }
  1608. closedir($handle);
  1609. }
  1610. return $size;
  1611. }
  1612. /**
  1613. * Check an array of MIME types against a whitelist.
  1614. *
  1615. * WordPress ships with a set of allowed upload filetypes,
  1616. * which is defined in wp-includes/functions.php in
  1617. * get_allowed_mime_types(). This function is used to filter
  1618. * that list against the filetype whitelist provided by Multisite
  1619. * Super Admins at wp-admin/network/settings.php.
  1620. *
  1621. * @since MU (3.0.0)
  1622. *
  1623. * @param array $mimes
  1624. * @return array
  1625. */
  1626. function check_upload_mimes( $mimes ) {
  1627. $site_exts = explode( ' ', get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ) );
  1628. $site_mimes = array();
  1629. foreach ( $site_exts as $ext ) {
  1630. foreach ( $mimes as $ext_pattern => $mime ) {
  1631. if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
  1632. $site_mimes[$ext_pattern] = $mime;
  1633. }
  1634. }
  1635. return $site_mimes;
  1636. }
  1637. /**
  1638. * Update a blog's post count.
  1639. *
  1640. * WordPress MS stores a blog's post count as an option so as
  1641. * to avoid extraneous COUNTs when a blog's details are fetched
  1642. * with get_site(). This function is called when posts are published
  1643. * or unpublished to make sure the count stays current.
  1644. *
  1645. * @since MU (3.0.0)
  1646. *
  1647. * @global wpdb $wpdb WordPress database abstraction object.
  1648. *
  1649. * @param string $deprecated Not used.
  1650. */
  1651. function update_posts_count( $deprecated = '' ) {
  1652. global $wpdb;
  1653. update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
  1654. }
  1655. /**
  1656. * Logs the user email, IP, and registration date of a new site.
  1657. *
  1658. * @since MU (3.0.0)
  1659. *
  1660. * @global wpdb $wpdb WordPress database abstraction object.
  1661. *
  1662. * @param int $blog_id
  1663. * @param int $user_id
  1664. */
  1665. function wpmu_log_new_registrations( $blog_id, $user_id ) {
  1666. global $wpdb;
  1667. $user = get_userdata( (int) $user_id );
  1668. if ( $user )
  1669. $wpdb->insert( $wpdb->registration_log, array('email' => $user->user_email, 'IP' => preg_replace( '/[^0-9., ]/', '', wp_unslash( $_SERVER['REMOTE_ADDR'] ) ), 'blog_id' => $blog_id, 'date_registered' => current_time('mysql')) );
  1670. }
  1671. /**
  1672. * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
  1673. *
  1674. * @since 3.0.0
  1675. *
  1676. * @see term_id_filter
  1677. *
  1678. * @global wpdb $wpdb WordPress database abstraction object.
  1679. * @staticvar int $global_terms_recurse
  1680. *
  1681. * @param int $term_id An ID for a term on the current blog.
  1682. * @param string $deprecated Not used.
  1683. * @return int An ID from the global terms table mapped from $term_id.
  1684. */
  1685. function global_terms( $term_id, $deprecated = '' ) {
  1686. global $wpdb;
  1687. static $global_terms_recurse = null;
  1688. if ( !global_terms_enabled() )
  1689. return $term_id;
  1690. // prevent a race condition
  1691. $recurse_start = false;
  1692. if ( $global_terms_recurse === null ) {
  1693. $recurse_start = true;
  1694. $global_terms_recurse = 1;
  1695. } elseif ( 10 < $global_terms_recurse++ ) {
  1696. return $term_id;
  1697. }
  1698. $term_id = intval( $term_id );
  1699. $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
  1700. $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
  1701. if ( $global_id == null ) {
  1702. $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
  1703. if ( null == $used_global_id ) {
  1704. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1705. $global_id = $wpdb->insert_id;
  1706. if ( empty( $global_id ) )
  1707. return $term_id;
  1708. } else {
  1709. $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
  1710. $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
  1711. $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
  1712. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1713. $global_id = $wpdb->insert_id;
  1714. }
  1715. } elseif ( $global_id != $term_id ) {
  1716. $local_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
  1717. if ( null != $local_id ) {
  1718. global_terms( $local_id );
  1719. if ( 10 < $global_terms_recurse ) {
  1720. $global_id = $term_id;
  1721. }
  1722. }
  1723. }
  1724. if ( $global_id != $term_id ) {
  1725. if ( get_option( 'default_category' ) == $term_id )
  1726. update_option( 'default_category', $global_id );
  1727. $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
  1728. $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
  1729. $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
  1730. clean_term_cache($term_id);
  1731. }
  1732. if ( $recurse_start )
  1733. $global_terms_recurse = null;
  1734. return $global_id;
  1735. }
  1736. /**
  1737. * Ensure that the current site's domain is listed in the allowed redirect host list.
  1738. *
  1739. * @see wp_validate_redirect()
  1740. * @since MU (3.0.0)
  1741. *
  1742. * @param array|string $deprecated Not used.
  1743. * @return array The current site's domain
  1744. */
  1745. function redirect_this_site( $deprecated = '' ) {
  1746. return array( get_network()->domain );
  1747. }
  1748. /**
  1749. * Check whether an upload is too big.
  1750. *
  1751. * @since MU (3.0.0)
  1752. *
  1753. * @blessed
  1754. *
  1755. * @param array $upload
  1756. * @return string|array If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
  1757. */
  1758. function upload_is_file_too_big( $upload ) {
  1759. if ( ! is_array( $upload ) || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) )
  1760. return $upload;
  1761. if ( strlen( $upload['bits'] ) > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
  1762. return sprintf( __( 'This file is too big. Files must be less than %d KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ) );
  1763. }
  1764. return $upload;
  1765. }
  1766. /**
  1767. * Add a nonce field to the signup page.
  1768. *
  1769. * @since MU (3.0.0)
  1770. */
  1771. function signup_nonce_fields() {
  1772. $id = mt_rand();
  1773. echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
  1774. wp_nonce_field('signup_form_' . $id, '_signup_form', false);
  1775. }
  1776. /**
  1777. * Process the signup nonce created in signup_nonce_fields().
  1778. *
  1779. * @since MU (3.0.0)
  1780. *
  1781. * @param array $result
  1782. * @return array
  1783. */
  1784. function signup_nonce_check( $result ) {
  1785. if ( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
  1786. return $result;
  1787. if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
  1788. wp_die( __( 'Please try again.' ) );
  1789. return $result;
  1790. }
  1791. /**
  1792. * Correct 404 redirects when NOBLOGREDIRECT is defined.
  1793. *
  1794. * @since MU (3.0.0)
  1795. */
  1796. function maybe_redirect_404() {
  1797. /**
  1798. * Filters the redirect URL for 404s on the main site.
  1799. *
  1800. * The filter is only evaluated if the NOBLOGREDIRECT constant is defined.
  1801. *
  1802. * @since 3.0.0
  1803. *
  1804. * @param string $no_blog_redirect The redirect URL defined in NOBLOGREDIRECT.
  1805. */
  1806. if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
  1807. if ( $destination == '%siteurl%' )
  1808. $destination = network_home_url();
  1809. wp_redirect( $destination );
  1810. exit();
  1811. }
  1812. }
  1813. /**
  1814. * Add a new user to a blog by visiting /newbloguser/{key}/.
  1815. *
  1816. * This will only work when the user's details are saved as an option
  1817. * keyed as 'new_user_{key}', where '{key}' is a hash generated for the user to be
  1818. * added, as when a user is invited through the regular WP Add User interface.
  1819. *
  1820. * @since MU (3.0.0)
  1821. */
  1822. function maybe_add_existing_user_to_blog() {
  1823. if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) )
  1824. return;
  1825. $parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
  1826. $key = array_pop( $parts );
  1827. if ( $key == '' )
  1828. $key = array_pop( $parts );
  1829. $details = get_option( 'new_user_' . $key );
  1830. if ( !empty( $details ) )
  1831. delete_option( 'new_user_' . $key );
  1832. if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) )
  1833. wp_die( sprintf(__('An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.'), home_url() ) );
  1834. wp_die( sprintf( __( 'You have been added to this site. Please visit the <a href="%s">homepage</a> or <a href="%s">log in</a> using your username and password.' ), home_url(), admin_url() ), __( 'WordPress &rsaquo; Success' ), array( 'response' => 200 ) );
  1835. }
  1836. /**
  1837. * Add a user to a blog based on details from maybe_add_existing_user_to_blog().
  1838. *
  1839. * @since MU (3.0.0)
  1840. *
  1841. * @param array $details
  1842. * @return true|WP_Error|void
  1843. */
  1844. function add_existing_user_to_blog( $details = false ) {
  1845. if ( is_array( $details ) ) {
  1846. $blog_id = get_current_blog_id();
  1847. $result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] );
  1848. /**
  1849. * Fires immediately after an existing user is added to a site.
  1850. *
  1851. * @since MU (3.0.0)
  1852. *
  1853. * @param int $user_id User ID.
  1854. * @param mixed $result True on success or a WP_Error object if the user doesn't exist
  1855. * or could not be added.
  1856. */
  1857. do_action( 'added_existing_user', $details['user_id'], $result );
  1858. return $result;
  1859. }
  1860. }
  1861. /**
  1862. * Adds a newly created user to the appropriate blog
  1863. *
  1864. * To add a user in general, use add_user_to_blog(). This function
  1865. * is specifically hooked into the {@see 'wpmu_activate_user'} action.
  1866. *
  1867. * @since MU (3.0.0)
  1868. * @see add_user_to_blog()
  1869. *
  1870. * @param int $user_id
  1871. * @param mixed $password Ignored.
  1872. * @param array $meta
  1873. */
  1874. function add_new_user_to_blog( $user_id, $password, $meta ) {
  1875. if ( !empty( $meta[ 'add_to_blog' ] ) ) {
  1876. $blog_id = $meta[ 'add_to_blog' ];
  1877. $role = $meta[ 'new_role' ];
  1878. remove_user_from_blog( $user_id, get_network()->site_id ); // remove user from main blog.
  1879. $result = add_user_to_blog( $blog_id, $user_id, $role );
  1880. if ( ! is_wp_error( $result ) ) {
  1881. update_user_meta( $user_id, 'primary_blog', $blog_id );
  1882. }
  1883. }
  1884. }
  1885. /**
  1886. * Correct From host on outgoing mail to match the site domain
  1887. *
  1888. * @since MU (3.0.0)
  1889. *
  1890. * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
  1891. */
  1892. function fix_phpmailer_messageid( $phpmailer ) {
  1893. $phpmailer->Hostname = get_network()->domain;
  1894. }
  1895. /**
  1896. * Check to see whether a user is marked as a spammer, based on user login.
  1897. *
  1898. * @since MU (3.0.0)
  1899. *
  1900. * @param string|WP_User $user Optional. Defaults to current user. WP_User object,
  1901. * or user login name as a string.
  1902. * @return bool
  1903. */
  1904. function is_user_spammy( $user = null ) {
  1905. if ( ! ( $user instanceof WP_User ) ) {
  1906. if ( $user ) {
  1907. $user = get_user_by( 'login', $user );
  1908. } else {
  1909. $user = wp_get_current_user();
  1910. }
  1911. }
  1912. return $user && isset( $user->spam ) && 1 == $user->spam;
  1913. }
  1914. /**
  1915. * Update this blog's 'public' setting in the global blogs table.
  1916. *
  1917. * Public blogs have a setting of 1, private blogs are 0.
  1918. *
  1919. * @since MU (3.0.0)
  1920. *
  1921. * @param int $old_value
  1922. * @param int $value The new public value
  1923. */
  1924. function update_blog_public( $old_value, $value ) {
  1925. update_blog_status( get_current_blog_id(), 'public', (int) $value );
  1926. }
  1927. /**
  1928. * Check whether users can self-register, based on Network settings.
  1929. *
  1930. * @since MU (3.0.0)
  1931. *
  1932. * @return bool
  1933. */
  1934. function users_can_register_signup_filter() {
  1935. $registration = get_site_option('registration');
  1936. return ( $registration == 'all' || $registration == 'user' );
  1937. }
  1938. /**
  1939. * Ensure that the welcome message is not empty. Currently unused.
  1940. *
  1941. * @since MU (3.0.0)
  1942. *
  1943. * @param string $text
  1944. * @return string
  1945. */
  1946. function welcome_user_msg_filter( $text ) {
  1947. if ( !$text ) {
  1948. remove_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
  1949. /* translators: Do not translate USERNAME, PASSWORD, LOGINLINK, SITE_NAME: those are placeholders. */
  1950. $text = __( 'Howdy USERNAME,
  1951. Your new account is set up.
  1952. You can log in with the following information:
  1953. Username: USERNAME
  1954. Password: PASSWORD
  1955. LOGINLINK
  1956. Thanks!
  1957. --The Team @ SITE_NAME' );
  1958. update_site_option( 'welcome_user_email', $text );
  1959. }
  1960. return $text;
  1961. }
  1962. /**
  1963. * Whether to force SSL on content.
  1964. *
  1965. * @since 2.8.5
  1966. *
  1967. * @staticvar bool $forced_content
  1968. *
  1969. * @param bool $force
  1970. * @return bool True if forced, false if not forced.
  1971. */
  1972. function force_ssl_content( $force = '' ) {
  1973. static $forced_content = false;
  1974. if ( '' != $force ) {
  1975. $old_forced = $forced_content;
  1976. $forced_content = $force;
  1977. return $old_forced;
  1978. }
  1979. return $forced_content;
  1980. }
  1981. /**
  1982. * Formats a URL to use https.
  1983. *
  1984. * Useful as a filter.
  1985. *
  1986. * @since 2.8.5
  1987. *
  1988. * @param string $url URL
  1989. * @return string URL with https as the scheme
  1990. */
  1991. function filter_SSL( $url ) {
  1992. if ( ! is_string( $url ) )
  1993. return get_bloginfo( 'url' ); // Return home blog url with proper scheme
  1994. if ( force_ssl_content() && is_ssl() )
  1995. $url = set_url_scheme( $url, 'https' );
  1996. return $url;
  1997. }
  1998. /**
  1999. * Schedule update of the network-wide counts for the current network.
  2000. *
  2001. * @since 3.1.0
  2002. */
  2003. function wp_schedule_update_network_counts() {
  2004. if ( !is_main_site() )
  2005. return;
  2006. if ( ! wp_next_scheduled('update_network_counts') && ! wp_installing() )
  2007. wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
  2008. }
  2009. /**
  2010. * Update the network-wide counts for the current network.
  2011. *
  2012. * @since 3.1.0
  2013. * @since 4.8.0 The $network_id parameter has been added.
  2014. *
  2015. * @param int|null $network_id ID of the network. Default is the current network.
  2016. */
  2017. function wp_update_network_counts( $network_id = null ) {
  2018. wp_update_network_user_counts( $network_id );
  2019. wp_update_network_site_counts( $network_id );
  2020. }
  2021. /**
  2022. * Update the count of sites for the current network.
  2023. *
  2024. * If enabled through the {@see 'enable_live_network_counts'} filter, update the sites count
  2025. * on a network when a site is created or its status is updated.
  2026. *
  2027. * @since 3.7.0
  2028. * @since 4.8.0 The $network_id parameter has been added.
  2029. *
  2030. * @param int|null $network_id ID of the network. Default is the current network.
  2031. */
  2032. function wp_maybe_update_network_site_counts( $network_id = null ) {
  2033. $is_small_network = ! wp_is_large_network( 'sites', $network_id );
  2034. /**
  2035. * Filters whether to update network site or user counts when a new site is created.
  2036. *
  2037. * @since 3.7.0
  2038. *
  2039. * @see wp_is_large_network()
  2040. *
  2041. * @param bool $small_network Whether the network is considered small.
  2042. * @param string $context Context. Either 'users' or 'sites'.
  2043. */
  2044. if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) )
  2045. return;
  2046. wp_update_network_site_counts( $network_id );
  2047. }
  2048. /**
  2049. * Update the network-wide users count.
  2050. *
  2051. * If enabled through the {@see 'enable_live_network_counts'} filter, update the users count
  2052. * on a network when a user is created or its status is updated.
  2053. *
  2054. * @since 3.7.0
  2055. * @since 4.8.0 The $network_id parameter has been added.
  2056. *
  2057. * @param int|null $network_id ID of the network. Default is the current network.
  2058. */
  2059. function wp_maybe_update_network_user_counts( $network_id = null ) {
  2060. $is_small_network = ! wp_is_large_network( 'users', $network_id );
  2061. /** This filter is documented in wp-includes/ms-functions.php */
  2062. if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) )
  2063. return;
  2064. wp_update_network_user_counts( $network_id );
  2065. }
  2066. /**
  2067. * Update the network-wide site count.
  2068. *
  2069. * @since 3.7.0
  2070. * @since 4.8.0 The $network_id parameter has been added.
  2071. *
  2072. * @param int|null $network_id ID of the network. Default is the current network.
  2073. */
  2074. function wp_update_network_site_counts( $network_id = null ) {
  2075. $network_id = (int) $network_id;
  2076. if ( ! $network_id ) {
  2077. $network_id = get_current_network_id();
  2078. }
  2079. $count = get_sites( array(
  2080. 'network_id' => $network_id,
  2081. 'spam' => 0,
  2082. 'deleted' => 0,
  2083. 'archived' => 0,
  2084. 'count' => true,
  2085. ) );
  2086. update_network_option( $network_id, 'blog_count', $count );
  2087. }
  2088. /**
  2089. * Update the network-wide user count.
  2090. *
  2091. * @since 3.7.0
  2092. * @since 4.8.0 The $network_id parameter has been added.
  2093. *
  2094. * @global wpdb $wpdb WordPress database abstraction object.
  2095. *
  2096. * @param int|null $network_id ID of the network. Default is the current network.
  2097. */
  2098. function wp_update_network_user_counts( $network_id = null ) {
  2099. global $wpdb;
  2100. $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
  2101. update_network_option( $network_id, 'user_count', $count );
  2102. }
  2103. /**
  2104. * Returns the space used by the current blog.
  2105. *
  2106. * @since 3.5.0
  2107. *
  2108. * @return int Used space in megabytes
  2109. */
  2110. function get_space_used() {
  2111. /**
  2112. * Filters the amount of storage space used by the current site.
  2113. *
  2114. * @since 3.5.0
  2115. *
  2116. * @param int|bool $space_used The amount of used space, in megabytes. Default false.
  2117. */
  2118. $space_used = apply_filters( 'pre_get_space_used', false );
  2119. if ( false === $space_used ) {
  2120. $upload_dir = wp_upload_dir();
  2121. $space_used = get_dirsize( $upload_dir['basedir'] ) / MB_IN_BYTES;
  2122. }
  2123. return $space_used;
  2124. }
  2125. /**
  2126. * Returns the upload quota for the current blog.
  2127. *
  2128. * @since MU (3.0.0)
  2129. *
  2130. * @return int Quota in megabytes
  2131. */
  2132. function get_space_allowed() {
  2133. $space_allowed = get_option( 'blog_upload_space' );
  2134. if ( ! is_numeric( $space_allowed ) )
  2135. $space_allowed = get_site_option( 'blog_upload_space' );
  2136. if ( ! is_numeric( $space_allowed ) )
  2137. $space_allowed = 100;
  2138. /**
  2139. * Filters the upload quota for the current site.
  2140. *
  2141. * @since 3.7.0
  2142. *
  2143. * @param int $space_allowed Upload quota in megabytes for the current blog.
  2144. */
  2145. return apply_filters( 'get_space_allowed', $space_allowed );
  2146. }
  2147. /**
  2148. * Determines if there is any upload space left in the current blog's quota.
  2149. *
  2150. * @since 3.0.0
  2151. *
  2152. * @return int of upload space available in bytes
  2153. */
  2154. function get_upload_space_available() {
  2155. $allowed = get_space_allowed();
  2156. if ( $allowed < 0 ) {
  2157. $allowed = 0;
  2158. }
  2159. $space_allowed = $allowed * MB_IN_BYTES;
  2160. if ( get_site_option( 'upload_space_check_disabled' ) )
  2161. return $space_allowed;
  2162. $space_used = get_space_used() * MB_IN_BYTES;
  2163. if ( ( $space_allowed - $space_used ) <= 0 )
  2164. return 0;
  2165. return $space_allowed - $space_used;
  2166. }
  2167. /**
  2168. * Determines if there is any upload space left in the current blog's quota.
  2169. *
  2170. * @since 3.0.0
  2171. * @return bool True if space is available, false otherwise.
  2172. */
  2173. function is_upload_space_available() {
  2174. if ( get_site_option( 'upload_space_check_disabled' ) )
  2175. return true;
  2176. return (bool) get_upload_space_available();
  2177. }
  2178. /**
  2179. * Filters the maximum upload file size allowed, in bytes.
  2180. *
  2181. * @since 3.0.0
  2182. *
  2183. * @param int $size Upload size limit in bytes.
  2184. * @return int Upload size limit in bytes.
  2185. */
  2186. function upload_size_limit_filter( $size ) {
  2187. $fileupload_maxk = KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 );
  2188. if ( get_site_option( 'upload_space_check_disabled' ) )
  2189. return min( $size, $fileupload_maxk );
  2190. return min( $size, $fileupload_maxk, get_upload_space_available() );
  2191. }
  2192. /**
  2193. * Whether or not we have a large network.
  2194. *
  2195. * The default criteria for a large network is either more than 10,000 users or more than 10,000 sites.
  2196. * Plugins can alter this criteria using the {@see 'wp_is_large_network'} filter.
  2197. *
  2198. * @since 3.3.0
  2199. * @since 4.8.0 The $network_id parameter has been added.
  2200. *
  2201. * @param string $using 'sites or 'users'. Default is 'sites'.
  2202. * @param int|null $network_id ID of the network. Default is the current network.
  2203. * @return bool True if the network meets the criteria for large. False otherwise.
  2204. */
  2205. function wp_is_large_network( $using = 'sites', $network_id = null ) {
  2206. $network_id = (int) $network_id;
  2207. if ( ! $network_id ) {
  2208. $network_id = get_current_network_id();
  2209. }
  2210. if ( 'users' == $using ) {
  2211. $count = get_user_count( $network_id );
  2212. /**
  2213. * Filters whether the network is considered large.
  2214. *
  2215. * @since 3.3.0
  2216. * @since 4.8.0 The $network_id parameter has been added.
  2217. *
  2218. * @param bool $is_large_network Whether the network has more than 10000 users or sites.
  2219. * @param string $component The component to count. Accepts 'users', or 'sites'.
  2220. * @param int $count The count of items for the component.
  2221. * @param int $network_id The ID of the network being checked.
  2222. */
  2223. return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count, $network_id );
  2224. }
  2225. $count = get_blog_count( $network_id );
  2226. /** This filter is documented in wp-includes/ms-functions.php */
  2227. return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count, $network_id );
  2228. }
  2229. /**
  2230. * Retrieves a list of reserved site on a sub-directory Multisite installation.
  2231. *
  2232. * @since 4.4.0
  2233. *
  2234. * @return array $names Array of reserved subdirectory names.
  2235. */
  2236. function get_subdirectory_reserved_names() {
  2237. $names = array(
  2238. 'page', 'comments', 'blog', 'files', 'feed', 'wp-admin',
  2239. 'wp-content', 'wp-includes', 'wp-json', 'embed'
  2240. );
  2241. /**
  2242. * Filters reserved site names on a sub-directory Multisite installation.
  2243. *
  2244. * @since 3.0.0
  2245. * @since 4.4.0 'wp-admin', 'wp-content', 'wp-includes', 'wp-json', and 'embed' were added
  2246. * to the reserved names list.
  2247. *
  2248. * @param array $subdirectory_reserved_names Array of reserved names.
  2249. */
  2250. return apply_filters( 'subdirectory_reserved_names', $names );
  2251. }
  2252. /**
  2253. * Send a confirmation request email when a change of network admin email address is attempted.
  2254. *
  2255. * The new network admin address will not become active until confirmed.
  2256. *
  2257. * @since 4.9.0
  2258. *
  2259. * @param string $old_value The old network admin email address.
  2260. * @param string $value The proposed new network admin email address.
  2261. */
  2262. function update_network_option_new_admin_email( $old_value, $value ) {
  2263. if ( $value == get_site_option( 'admin_email' ) || ! is_email( $value ) ) {
  2264. return;
  2265. }
  2266. $hash = md5( $value . time() . mt_rand() );
  2267. $new_admin_email = array(
  2268. 'hash' => $hash,
  2269. 'newemail' => $value,
  2270. );
  2271. update_site_option( 'network_admin_hash', $new_admin_email );
  2272. $switched_locale = switch_to_locale( get_user_locale() );
  2273. /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
  2274. $email_text = __( 'Howdy ###USERNAME###,
  2275. You recently requested to have the network admin email address on
  2276. your network changed.
  2277. If this is correct, please click on the following link to change it:
  2278. ###ADMIN_URL###
  2279. You can safely ignore and delete this email if you do not want to
  2280. take this action.
  2281. This email has been sent to ###EMAIL###
  2282. Regards,
  2283. All at ###SITENAME###
  2284. ###SITEURL###' );
  2285. /**
  2286. * Filters the text of the email sent when a change of network admin email address is attempted.
  2287. *
  2288. * The following strings have a special meaning and will get replaced dynamically:
  2289. * ###USERNAME### The current user's username.
  2290. * ###ADMIN_URL### The link to click on to confirm the email change.
  2291. * ###EMAIL### The proposed new network admin email address.
  2292. * ###SITENAME### The name of the network.
  2293. * ###SITEURL### The URL to the network.
  2294. *
  2295. * @since 4.9.0
  2296. *
  2297. * @param string $email_text Text in the email.
  2298. * @param array $new_admin_email {
  2299. * Data relating to the new network admin email address.
  2300. *
  2301. * @type string $hash The secure hash used in the confirmation link URL.
  2302. * @type string $newemail The proposed new network admin email address.
  2303. * }
  2304. */
  2305. $content = apply_filters( 'new_network_admin_email_content', $email_text, $new_admin_email );
  2306. $current_user = wp_get_current_user();
  2307. $content = str_replace( '###USERNAME###', $current_user->user_login, $content );
  2308. $content = str_replace( '###ADMIN_URL###', esc_url( network_admin_url( 'settings.php?network_admin_hash=' . $hash ) ), $content );
  2309. $content = str_replace( '###EMAIL###', $value, $content );
  2310. $content = str_replace( '###SITENAME###', wp_specialchars_decode( get_site_option( 'site_name' ), ENT_QUOTES ), $content );
  2311. $content = str_replace( '###SITEURL###', network_home_url(), $content );
  2312. wp_mail( $value, sprintf( __( '[%s] New Network Admin Email Address' ), wp_specialchars_decode( get_site_option( 'site_name' ), ENT_QUOTES ) ), $content );
  2313. if ( $switched_locale ) {
  2314. restore_previous_locale();
  2315. }
  2316. }
  2317. /**
  2318. * Send an email to the old network admin email address when the network admin email address changes.
  2319. *
  2320. * @since 4.9.0
  2321. *
  2322. * @param string $option_name The relevant database option name.
  2323. * @param string $new_email The new network admin email address.
  2324. * @param string $old_email The old network admin email address.
  2325. * @param int $network_id ID of the network.
  2326. */
  2327. function wp_network_admin_email_change_notification( $option_name, $new_email, $old_email, $network_id ) {
  2328. $send = true;
  2329. // Don't send the notification to the default 'admin_email' value.
  2330. if ( 'you@example.com' === $old_email ) {
  2331. $send = false;
  2332. }
  2333. /**
  2334. * Filters whether to send the network admin email change notification email.
  2335. *
  2336. * @since 4.9.0
  2337. *
  2338. * @param bool $send Whether to send the email notification.
  2339. * @param string $old_email The old network admin email address.
  2340. * @param string $new_email The new network admin email address.
  2341. * @param int $network_id ID of the network.
  2342. */
  2343. $send = apply_filters( 'send_network_admin_email_change_email', $send, $old_email, $new_email, $network_id );
  2344. if ( ! $send ) {
  2345. return;
  2346. }
  2347. /* translators: Do not translate OLD_EMAIL, NEW_EMAIL, SITENAME, SITEURL: those are placeholders. */
  2348. $email_change_text = __( 'Hi,
  2349. This notice confirms that the network admin email address was changed on ###SITENAME###.
  2350. The new network admin email address is ###NEW_EMAIL###.
  2351. This email has been sent to ###OLD_EMAIL###
  2352. Regards,
  2353. All at ###SITENAME###
  2354. ###SITEURL###' );
  2355. $email_change_email = array(
  2356. 'to' => $old_email,
  2357. /* translators: Network admin email change notification email subject. %s: Network title */
  2358. 'subject' => __( '[%s] Notice of Network Admin Email Change' ),
  2359. 'message' => $email_change_text,
  2360. 'headers' => '',
  2361. );
  2362. // get network name
  2363. $network_name = wp_specialchars_decode( get_site_option( 'site_name' ), ENT_QUOTES );
  2364. /**
  2365. * Filters the contents of the email notification sent when the network admin email address is changed.
  2366. *
  2367. * @since 4.9.0
  2368. *
  2369. * @param array $email_change_email {
  2370. * Used to build wp_mail().
  2371. *
  2372. * @type string $to The intended recipient.
  2373. * @type string $subject The subject of the email.
  2374. * @type string $message The content of the email.
  2375. * The following strings have a special meaning and will get replaced dynamically:
  2376. * - ###OLD_EMAIL### The old network admin email address.
  2377. * - ###NEW_EMAIL### The new network admin email address.
  2378. * - ###SITENAME### The name of the network.
  2379. * - ###SITEURL### The URL to the site.
  2380. * @type string $headers Headers.
  2381. * }
  2382. * @param string $old_email The old network admin email address.
  2383. * @param string $new_email The new network admin email address.
  2384. * @param int $network_id ID of the network.
  2385. */
  2386. $email_change_email = apply_filters( 'network_admin_email_change_email', $email_change_email, $old_email, $new_email, $network_id );
  2387. $email_change_email['message'] = str_replace( '###OLD_EMAIL###', $old_email, $email_change_email['message'] );
  2388. $email_change_email['message'] = str_replace( '###NEW_EMAIL###', $new_email, $email_change_email['message'] );
  2389. $email_change_email['message'] = str_replace( '###SITENAME###', $network_name, $email_change_email['message'] );
  2390. $email_change_email['message'] = str_replace( '###SITEURL###', home_url(), $email_change_email['message'] );
  2391. wp_mail( $email_change_email['to'], sprintf(
  2392. $email_change_email['subject'],
  2393. $network_name
  2394. ), $email_change_email['message'], $email_change_email['headers'] );
  2395. }