Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

2489 lignes
77 KiB

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