/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

Large files are truncated click here to view the full 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, $bl