PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/ms-functions.php

https://gitlab.com/Gashler/dp
PHP | 1507 lines | 727 code | 218 blank | 562 comment | 197 complexity | f78069d6cae32fbe952278f62588515a MD5 | raw file
  1. <?php
  2. /**
  3. * Multisite WordPress API
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 3.0.0
  8. */
  9. /**
  10. * Gets the network's site and user counts.
  11. *
  12. * @since MU 1.0
  13. * @uses get_blog_count()
  14. * @uses get_user_count()
  15. *
  16. * @return array Site and user count for the network.
  17. */
  18. function get_sitestats() {
  19. global $wpdb;
  20. $stats = array(
  21. 'blogs' => get_blog_count(),
  22. 'users' => get_user_count(),
  23. );
  24. return $stats;
  25. }
  26. /**
  27. * Get the admin for a domain/path combination.
  28. *
  29. * @since MU 1.0
  30. *
  31. * @param string $sitedomain Optional. Site domain.
  32. * @param string $path Optional. Site path.
  33. * @return array The network admins
  34. */
  35. function get_admin_users_for_domain( $sitedomain = '', $path = '' ) {
  36. global $wpdb;
  37. if ( ! $sitedomain )
  38. $site_id = $wpdb->siteid;
  39. else
  40. $site_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path ) );
  41. if ( $site_id )
  42. return $wpdb->get_results( $wpdb->prepare( "SELECT u.ID, u.user_login, u.user_pass FROM $wpdb->users AS u, $wpdb->sitemeta AS sm WHERE sm.meta_key = 'admin_user_id' AND u.ID = sm.meta_value AND sm.site_id = %d", $site_id ), ARRAY_A );
  43. return false;
  44. }
  45. /**
  46. * Get one of a user's active blogs
  47. *
  48. * Returns the user's primary blog, if she has one and
  49. * it is active. If it's inactive, function returns another
  50. * active blog of the user. If none are found, the user
  51. * is added as a Subscriber to the Dashboard Blog and that blog
  52. * is returned.
  53. *
  54. * @since MU 1.0
  55. * @uses get_blogs_of_user()
  56. * @uses add_user_to_blog()
  57. * @uses get_blog_details()
  58. *
  59. * @param int $user_id The unique ID of the user
  60. * @return object The blog object
  61. */
  62. function get_active_blog_for_user( $user_id ) {
  63. global $wpdb;
  64. $blogs = get_blogs_of_user( $user_id );
  65. if ( empty( $blogs ) )
  66. return null;
  67. if ( !is_multisite() )
  68. return $blogs[$wpdb->blogid];
  69. $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
  70. $first_blog = current($blogs);
  71. if ( false !== $primary_blog ) {
  72. if ( ! isset( $blogs[ $primary_blog ] ) ) {
  73. update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  74. $primary = get_blog_details( $first_blog->userblog_id );
  75. } else {
  76. $primary = get_blog_details( $primary_blog );
  77. }
  78. } else {
  79. //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
  80. add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );
  81. update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  82. $primary = $first_blog;
  83. }
  84. if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
  85. $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
  86. $ret = false;
  87. if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
  88. foreach ( (array) $blogs as $blog_id => $blog ) {
  89. if ( $blog->site_id != $wpdb->siteid )
  90. continue;
  91. $details = get_blog_details( $blog_id );
  92. if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
  93. $ret = $blog;
  94. if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id )
  95. update_user_meta( $user_id, 'primary_blog', $blog_id );
  96. if ( !get_user_meta($user_id , 'source_domain', true) )
  97. update_user_meta( $user_id, 'source_domain', $blog->domain );
  98. break;
  99. }
  100. }
  101. } else {
  102. return null;
  103. }
  104. return $ret;
  105. } else {
  106. return $primary;
  107. }
  108. }
  109. /**
  110. * The number of active users in your installation.
  111. *
  112. * The count is cached and updated twice daily. This is not a live count.
  113. *
  114. * @since MU 2.7
  115. *
  116. * @return int
  117. */
  118. function get_user_count() {
  119. return get_site_option( 'user_count' );
  120. }
  121. /**
  122. * The number of active sites on your installation.
  123. *
  124. * The count is cached and updated twice daily. This is not a live count.
  125. *
  126. * @since MU 1.0
  127. *
  128. * @param int $id Optional. A site_id.
  129. * @return int
  130. */
  131. function get_blog_count( $id = 0 ) {
  132. return get_site_option( 'blog_count' );
  133. }
  134. /**
  135. * Get a blog post from any site on the network.
  136. *
  137. * @since MU 1.0
  138. *
  139. * @param int $blog_id ID of the blog.
  140. * @param int $post_id ID of the post you're looking for.
  141. * @return WP_Post|null WP_Post on success or null on failure
  142. */
  143. function get_blog_post( $blog_id, $post_id ) {
  144. switch_to_blog( $blog_id );
  145. $post = get_post( $post_id );
  146. restore_current_blog();
  147. return $post;
  148. }
  149. /**
  150. * Add a user to a blog.
  151. *
  152. * Use the 'add_user_to_blog' action to fire an event when
  153. * users are added to a blog.
  154. *
  155. * @since MU 1.0
  156. *
  157. * @param int $blog_id ID of the blog you're adding the user to.
  158. * @param int $user_id ID of the user you're adding.
  159. * @param string $role The role you want the user to have
  160. * @return bool
  161. */
  162. function add_user_to_blog( $blog_id, $user_id, $role ) {
  163. switch_to_blog($blog_id);
  164. $user = get_userdata( $user_id );
  165. if ( ! $user ) {
  166. restore_current_blog();
  167. return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
  168. }
  169. if ( !get_user_meta($user_id, 'primary_blog', true) ) {
  170. update_user_meta($user_id, 'primary_blog', $blog_id);
  171. $details = get_blog_details($blog_id);
  172. update_user_meta($user_id, 'source_domain', $details->domain);
  173. }
  174. $user->set_role($role);
  175. do_action('add_user_to_blog', $user_id, $role, $blog_id);
  176. wp_cache_delete( $user_id, 'users' );
  177. restore_current_blog();
  178. return true;
  179. }
  180. /**
  181. * Remove a user from a blog.
  182. *
  183. * Use the 'remove_user_from_blog' action to fire an event when
  184. * users are removed from a blog.
  185. *
  186. * Accepts an optional $reassign parameter, if you want to
  187. * reassign the user's blog posts to another user upon removal.
  188. *
  189. * @since MU 1.0
  190. *
  191. * @param int $user_id ID of the user you're removing.
  192. * @param int $blog_id ID of the blog you're removing the user from.
  193. * @param string $reassign Optional. A user to whom to reassign posts.
  194. * @return bool
  195. */
  196. function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
  197. global $wpdb;
  198. switch_to_blog($blog_id);
  199. $user_id = (int) $user_id;
  200. do_action('remove_user_from_blog', $user_id, $blog_id);
  201. // If being removed from the primary blog, set a new primary if the user is assigned
  202. // to multiple blogs.
  203. $primary_blog = get_user_meta($user_id, 'primary_blog', true);
  204. if ( $primary_blog == $blog_id ) {
  205. $new_id = '';
  206. $new_domain = '';
  207. $blogs = get_blogs_of_user($user_id);
  208. foreach ( (array) $blogs as $blog ) {
  209. if ( $blog->userblog_id == $blog_id )
  210. continue;
  211. $new_id = $blog->userblog_id;
  212. $new_domain = $blog->domain;
  213. break;
  214. }
  215. update_user_meta($user_id, 'primary_blog', $new_id);
  216. update_user_meta($user_id, 'source_domain', $new_domain);
  217. }
  218. // wp_revoke_user($user_id);
  219. $user = get_userdata( $user_id );
  220. if ( ! $user ) {
  221. restore_current_blog();
  222. return new WP_Error('user_does_not_exist', __('That user does not exist.'));
  223. }
  224. $user->remove_all_caps();
  225. $blogs = get_blogs_of_user($user_id);
  226. if ( count($blogs) == 0 ) {
  227. update_user_meta($user_id, 'primary_blog', '');
  228. update_user_meta($user_id, 'source_domain', '');
  229. }
  230. if ( $reassign != '' ) {
  231. $reassign = (int) $reassign;
  232. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id) );
  233. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id) );
  234. }
  235. restore_current_blog();
  236. return true;
  237. }
  238. /**
  239. * Create an empty blog.
  240. *
  241. * @since MU 1.0
  242. * @uses install_blog()
  243. *
  244. * @param string $domain The new blog's domain.
  245. * @param string $path The new blog's path.
  246. * @param string $weblog_title The new blog's title.
  247. * @param int $site_id Optional. Defaults to 1.
  248. * @return int The ID of the newly created blog
  249. */
  250. function create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 ) {
  251. if ( empty($path) )
  252. $path = '/';
  253. // Check if the domain has been used already. We should return an error message.
  254. if ( domain_exists($domain, $path, $site_id) )
  255. return __( '<strong>ERROR</strong>: Site URL already taken.' );
  256. // Need to back up wpdb table names, and create a new wp_blogs entry for new blog.
  257. // Need to get blog_id from wp_blogs, and create new table names.
  258. // Must restore table names at the end of function.
  259. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  260. return __( '<strong>ERROR</strong>: problem creating site entry.' );
  261. switch_to_blog($blog_id);
  262. install_blog($blog_id);
  263. restore_current_blog();
  264. return $blog_id;
  265. }
  266. /**
  267. * Get the permalink for a post on another blog.
  268. *
  269. * @since MU 1.0
  270. *
  271. * @param int $blog_id ID of the source blog.
  272. * @param int $post_id ID of the desired post.
  273. * @return string The post's permalink
  274. */
  275. function get_blog_permalink( $blog_id, $post_id ) {
  276. switch_to_blog( $blog_id );
  277. $link = get_permalink( $post_id );
  278. restore_current_blog();
  279. return $link;
  280. }
  281. /**
  282. * Get a blog's numeric ID from its URL.
  283. *
  284. * On a subdirectory installation like example.com/blog1/,
  285. * $domain will be the root 'example.com' and $path the
  286. * subdirectory '/blog1/'. With subdomains like blog1.example.com,
  287. * $domain is 'blog1.example.com' and $path is '/'.
  288. *
  289. * @since MU 2.6.5
  290. *
  291. * @param string $domain
  292. * @param string $path Optional. Not required for subdomain installations.
  293. * @return int 0 if no blog found, otherwise the ID of the matching blog
  294. */
  295. function get_blog_id_from_url( $domain, $path = '/' ) {
  296. global $wpdb;
  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. $id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s and path = %s /* get_blog_id_from_url */", $domain, $path ) );
  305. if ( ! $id ) {
  306. wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
  307. return 0;
  308. }
  309. wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
  310. return $id;
  311. }
  312. // Admin functions
  313. /**
  314. * Checks an email address against a list of banned domains.
  315. *
  316. * This function checks against the Banned Email Domains list
  317. * at wp-admin/network/settings.php. The check is only run on
  318. * self-registrations; user creation at wp-admin/network/users.php
  319. * bypasses this check.
  320. *
  321. * @since MU
  322. *
  323. * @param string $user_email The email provided by the user at registration.
  324. * @return bool Returns true when the email address is banned.
  325. */
  326. function is_email_address_unsafe( $user_email ) {
  327. $banned_names = get_site_option( 'banned_email_domains' );
  328. if ( $banned_names && ! is_array( $banned_names ) )
  329. $banned_names = explode( "\n", $banned_names );
  330. $is_email_address_unsafe = false;
  331. if ( $banned_names && is_array( $banned_names ) ) {
  332. $banned_names = array_map( 'strtolower', $banned_names );
  333. $normalized_email = strtolower( $user_email );
  334. list( $email_local_part, $email_domain ) = explode( '@', $normalized_email );
  335. foreach ( $banned_names as $banned_domain ) {
  336. if ( ! $banned_domain )
  337. continue;
  338. if ( $email_domain == $banned_domain ) {
  339. $is_email_address_unsafe = true;
  340. break;
  341. }
  342. $dotted_domain = ".$banned_domain";
  343. if ( $dotted_domain === substr( $normalized_email, -strlen( $dotted_domain ) ) ) {
  344. $is_email_address_unsafe = true;
  345. break;
  346. }
  347. }
  348. }
  349. return apply_filters( 'is_email_address_unsafe', $is_email_address_unsafe, $user_email );
  350. }
  351. /**
  352. * Processes new user registrations.
  353. *
  354. * Checks the data provided by the user during signup. Verifies
  355. * the validity and uniqueness of user names and user email addresses,
  356. * and checks email addresses against admin-provided domain
  357. * whitelists and blacklists.
  358. *
  359. * The hook 'wpmu_validate_user_signup' provides an easy way
  360. * to modify the signup process. The value $result, which is passed
  361. * to the hook, contains both the user-provided info and the error
  362. * messages created by the function. 'wpmu_validate_user_signup' allows
  363. * you to process the data in any way you'd like, and unset the
  364. * relevant errors if necessary.
  365. *
  366. * @since MU
  367. * @uses is_email_address_unsafe()
  368. * @uses username_exists()
  369. * @uses email_exists()
  370. *
  371. * @param string $user_name The login name provided by the user.
  372. * @param string $user_email The email provided by the user.
  373. * @return array Contains username, email, and error messages.
  374. */
  375. function wpmu_validate_user_signup($user_name, $user_email) {
  376. global $wpdb;
  377. $errors = new WP_Error();
  378. $orig_username = $user_name;
  379. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  380. if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) {
  381. $errors->add( 'user_name', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
  382. $user_name = $orig_username;
  383. }
  384. $user_email = sanitize_email( $user_email );
  385. if ( empty( $user_name ) )
  386. $errors->add('user_name', __( 'Please enter a username.' ) );
  387. $illegal_names = get_site_option( 'illegal_names' );
  388. if ( is_array( $illegal_names ) == false ) {
  389. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  390. add_site_option( 'illegal_names', $illegal_names );
  391. }
  392. if ( in_array( $user_name, $illegal_names ) == true )
  393. $errors->add('user_name', __( 'That username is not allowed.' ) );
  394. if ( is_email_address_unsafe( $user_email ) )
  395. $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.'));
  396. if ( strlen( $user_name ) < 4 )
  397. $errors->add('user_name', __( 'Username must be at least 4 characters.' ) );
  398. if ( strpos( ' ' . $user_name, '_' ) != false )
  399. $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character &#8220;_&#8221;!' ) );
  400. // all numeric?
  401. if ( preg_match( '/^[0-9]*$/', $user_name ) )
  402. $errors->add('user_name', __('Sorry, usernames must have letters too!'));
  403. if ( !is_email( $user_email ) )
  404. $errors->add('user_email', __( 'Please enter a valid email address.' ) );
  405. $limited_email_domains = get_site_option( 'limited_email_domains' );
  406. if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
  407. $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
  408. if ( in_array( $emaildomain, $limited_email_domains ) == false )
  409. $errors->add('user_email', __('Sorry, that email address is not allowed!'));
  410. }
  411. // Check if the username has been used already.
  412. if ( username_exists($user_name) )
  413. $errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
  414. // Check if the email address has been used already.
  415. if ( email_exists($user_email) )
  416. $errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );
  417. // Has someone already signed up for this username?
  418. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  419. if ( $signup != null ) {
  420. $registered_at = mysql2date('U', $signup->registered);
  421. $now = current_time( 'timestamp', true );
  422. $diff = $now - $registered_at;
  423. // If registered more than two days ago, cancel registration and let this signup go through.
  424. if ( $diff > 2 * DAY_IN_SECONDS )
  425. $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
  426. else
  427. $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
  428. }
  429. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  430. if ( $signup != null ) {
  431. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  432. // If registered more than two days ago, cancel registration and let this signup go through.
  433. if ( $diff > 2 * DAY_IN_SECONDS )
  434. $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
  435. else
  436. $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.'));
  437. }
  438. $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
  439. return apply_filters('wpmu_validate_user_signup', $result);
  440. }
  441. /**
  442. * Processes new site registrations.
  443. *
  444. * Checks the data provided by the user during blog signup. Verifies
  445. * the validity and uniqueness of blog paths and domains.
  446. *
  447. * This function prevents the current user from registering a new site
  448. * with a blogname equivalent to another user's login name. Passing the
  449. * $user parameter to the function, where $user is the other user, is
  450. * effectively an override of this limitation.
  451. *
  452. * Filter 'wpmu_validate_blog_signup' if you want to modify
  453. * the way that WordPress validates new site signups.
  454. *
  455. * @since MU
  456. * @uses domain_exists()
  457. * @uses username_exists()
  458. *
  459. * @param string $blogname The blog name provided by the user. Must be unique.
  460. * @param string $blog_title The blog title provided by the user.
  461. * @return array Contains the new site data and error messages.
  462. */
  463. function wpmu_validate_blog_signup($blogname, $blog_title, $user = '') {
  464. global $wpdb, $domain, $current_site;
  465. $base = $current_site->path;
  466. $blog_title = strip_tags( $blog_title );
  467. $blog_title = substr( $blog_title, 0, 50 );
  468. $errors = new WP_Error();
  469. $illegal_names = get_site_option( 'illegal_names' );
  470. if ( $illegal_names == false ) {
  471. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  472. add_site_option( 'illegal_names', $illegal_names );
  473. }
  474. // On sub dir installs, Some names are so illegal, only a filter can spring them from jail
  475. if (! is_subdomain_install() )
  476. $illegal_names = array_merge($illegal_names, apply_filters( 'subdirectory_reserved_names', array( 'page', 'comments', 'blog', 'files', 'feed' ) ) );
  477. if ( empty( $blogname ) )
  478. $errors->add('blogname', __( 'Please enter a site name.' ) );
  479. if ( preg_match( '/[^a-z0-9]+/', $blogname ) )
  480. $errors->add('blogname', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
  481. if ( in_array( $blogname, $illegal_names ) == true )
  482. $errors->add('blogname', __( 'That name is not allowed.' ) );
  483. if ( strlen( $blogname ) < 4 && !is_super_admin() )
  484. $errors->add('blogname', __( 'Site name must be at least 4 characters.' ) );
  485. if ( strpos( $blogname, '_' ) !== false )
  486. $errors->add( 'blogname', __( 'Sorry, site names may not contain the character &#8220;_&#8221;!' ) );
  487. // do not allow users to create a blog that conflicts with a page on the main blog.
  488. if ( !is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_site->blog_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) )
  489. $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
  490. // all numeric?
  491. if ( preg_match( '/^[0-9]*$/', $blogname ) )
  492. $errors->add('blogname', __('Sorry, site names must have letters too!'));
  493. $blogname = apply_filters( 'newblogname', $blogname );
  494. $blog_title = wp_unslash( $blog_title );
  495. if ( empty( $blog_title ) )
  496. $errors->add('blog_title', __( 'Please enter a site title.' ) );
  497. // Check if the domain/path has been used already.
  498. if ( is_subdomain_install() ) {
  499. $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
  500. $path = $base;
  501. } else {
  502. $mydomain = "$domain";
  503. $path = $base.$blogname.'/';
  504. }
  505. if ( domain_exists($mydomain, $path, $current_site->id) )
  506. $errors->add( 'blogname', __( 'Sorry, that site already exists!' ) );
  507. if ( username_exists( $blogname ) ) {
  508. if ( is_object( $user ) == false || ( is_object($user) && ( $user->user_login != $blogname ) ) )
  509. $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
  510. }
  511. // Has someone already signed up for this domain?
  512. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
  513. if ( ! empty($signup) ) {
  514. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  515. // If registered more than two days ago, cancel registration and let this signup go through.
  516. if ( $diff > 2 * DAY_IN_SECONDS )
  517. $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) );
  518. else
  519. $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
  520. }
  521. $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'user' => $user, 'errors' => $errors);
  522. return apply_filters('wpmu_validate_blog_signup', $result);
  523. }
  524. /**
  525. * Record site signup information for future activation.
  526. *
  527. * @since MU
  528. * @uses wpmu_signup_blog_notification()
  529. *
  530. * @param string $domain The requested domain.
  531. * @param string $path The requested path.
  532. * @param string $title The requested site title.
  533. * @param string $user The user's requested login name.
  534. * @param string $user_email The user's email address.
  535. * @param array $meta By default, contains the requested privacy setting and lang_id.
  536. */
  537. function wpmu_signup_blog($domain, $path, $title, $user, $user_email, $meta = '') {
  538. global $wpdb;
  539. $key = substr( md5( time() . rand() . $domain ), 0, 16 );
  540. $meta = serialize($meta);
  541. $wpdb->insert( $wpdb->signups, array(
  542. 'domain' => $domain,
  543. 'path' => $path,
  544. 'title' => $title,
  545. 'user_login' => $user,
  546. 'user_email' => $user_email,
  547. 'registered' => current_time('mysql', true),
  548. 'activation_key' => $key,
  549. 'meta' => $meta
  550. ) );
  551. wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta);
  552. }
  553. /**
  554. * Record user signup information for future activation.
  555. *
  556. * This function is used when user registration is open but
  557. * new site registration is not.
  558. *
  559. * @since MU
  560. * @uses wpmu_signup_user_notification()
  561. *
  562. * @param string $user The user's requested login name.
  563. * @param string $user_email The user's email address.
  564. * @param array $meta By default, this is an empty array.
  565. */
  566. function wpmu_signup_user($user, $user_email, $meta = '') {
  567. global $wpdb;
  568. // Format data
  569. $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
  570. $user_email = sanitize_email( $user_email );
  571. $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
  572. $meta = serialize($meta);
  573. $wpdb->insert( $wpdb->signups, array(
  574. 'domain' => '',
  575. 'path' => '',
  576. 'title' => '',
  577. 'user_login' => $user,
  578. 'user_email' => $user_email,
  579. 'registered' => current_time('mysql', true),
  580. 'activation_key' => $key,
  581. 'meta' => $meta
  582. ) );
  583. wpmu_signup_user_notification($user, $user_email, $key, $meta);
  584. }
  585. /**
  586. * Notify user of signup success.
  587. *
  588. * This is the notification function used when site registration
  589. * is enabled.
  590. *
  591. * Filter 'wpmu_signup_blog_notification' to bypass this function or
  592. * replace it with your own notification behavior.
  593. *
  594. * Filter 'wpmu_signup_blog_notification_email' and
  595. * 'wpmu_signup_blog_notification_subject' to change the content
  596. * and subject line of the email sent to newly registered users.
  597. *
  598. * @since MU
  599. *
  600. * @param string $domain The new blog domain.
  601. * @param string $path The new blog path.
  602. * @param string $title The site title.
  603. * @param string $user The user's login name.
  604. * @param string $user_email The user's email address.
  605. * @param array $meta By default, contains the requested privacy setting and lang_id.
  606. * @param string $key The activation key created in wpmu_signup_blog()
  607. * @return bool
  608. */
  609. function wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta = '') {
  610. global $current_site;
  611. if ( !apply_filters('wpmu_signup_blog_notification', $domain, $path, $title, $user, $user_email, $key, $meta) )
  612. return false;
  613. // Send email with activation link.
  614. if ( !is_subdomain_install() || $current_site->id != 1 )
  615. $activate_url = network_site_url("wp-activate.php?key=$key");
  616. else
  617. $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
  618. $activate_url = esc_url($activate_url);
  619. $admin_email = get_site_option( 'admin_email' );
  620. if ( $admin_email == '' )
  621. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  622. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  623. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  624. $message = sprintf(
  625. apply_filters( 'wpmu_signup_blog_notification_email',
  626. __( "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" ),
  627. $domain, $path, $title, $user, $user_email, $key, $meta
  628. ),
  629. $activate_url,
  630. esc_url( "http://{$domain}{$path}" ),
  631. $key
  632. );
  633. // TODO: Don't hard code activation link.
  634. $subject = sprintf(
  635. apply_filters( 'wpmu_signup_blog_notification_subject',
  636. __( '[%1$s] Activate %2$s' ),
  637. $domain, $path, $title, $user, $user_email, $key, $meta
  638. ),
  639. $from_name,
  640. esc_url( 'http://' . $domain . $path )
  641. );
  642. wp_mail($user_email, $subject, $message, $message_headers);
  643. return true;
  644. }
  645. /**
  646. * Notify user of signup success.
  647. *
  648. * This is the notification function used when no new site has
  649. * been requested.
  650. *
  651. * Filter 'wpmu_signup_user_notification' to bypass this function or
  652. * replace it with your own notification behavior.
  653. *
  654. * Filter 'wpmu_signup_user_notification_email' and
  655. * 'wpmu_signup_user_notification_subject' to change the content
  656. * and subject line of the email sent to newly registered users.
  657. *
  658. * @since MU
  659. *
  660. * @param string $user The user's login name.
  661. * @param string $user_email The user's email address.
  662. * @param array $meta By default, an empty array.
  663. * @param string $key The activation key created in wpmu_signup_user()
  664. * @return bool
  665. */
  666. function wpmu_signup_user_notification($user, $user_email, $key, $meta = '') {
  667. if ( !apply_filters('wpmu_signup_user_notification', $user, $user_email, $key, $meta) )
  668. return false;
  669. // Send email with activation link.
  670. $admin_email = get_site_option( 'admin_email' );
  671. if ( $admin_email == '' )
  672. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  673. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  674. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  675. $message = sprintf(
  676. apply_filters( 'wpmu_signup_user_notification_email',
  677. __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ),
  678. $user, $user_email, $key, $meta
  679. ),
  680. site_url( "wp-activate.php?key=$key" )
  681. );
  682. // TODO: Don't hard code activation link.
  683. $subject = sprintf(
  684. apply_filters( 'wpmu_signup_user_notification_subject',
  685. __( '[%1$s] Activate %2$s' ),
  686. $user, $user_email, $key, $meta
  687. ),
  688. $from_name,
  689. $user
  690. );
  691. wp_mail($user_email, $subject, $message, $message_headers);
  692. return true;
  693. }
  694. /**
  695. * Activate a signup.
  696. *
  697. * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events
  698. * that should happen only when users or sites are self-created (since
  699. * those actions are not called when users and sites are created
  700. * by a Super Admin).
  701. *
  702. * @since MU
  703. * @uses wp_generate_password()
  704. * @uses wpmu_welcome_user_notification()
  705. * @uses add_user_to_blog()
  706. * @uses wpmu_create_user()
  707. * @uses wpmu_create_blog()
  708. * @uses wpmu_welcome_notification()
  709. *
  710. * @param string $key The activation key provided to the user.
  711. * @return array An array containing information about the activated user and/or blog
  712. */
  713. function wpmu_activate_signup($key) {
  714. global $wpdb, $current_site;
  715. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
  716. if ( empty( $signup ) )
  717. return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
  718. if ( $signup->active ) {
  719. if ( empty( $signup->domain ) )
  720. return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
  721. else
  722. return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
  723. }
  724. $meta = maybe_unserialize($signup->meta);
  725. $password = wp_generate_password( 12, false );
  726. $user_id = username_exists($signup->user_login);
  727. if ( ! $user_id )
  728. $user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email);
  729. else
  730. $user_already_exists = true;
  731. if ( ! $user_id )
  732. return new WP_Error('create_user', __('Could not create user'), $signup);
  733. $now = current_time('mysql', true);
  734. if ( empty($signup->domain) ) {
  735. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  736. if ( isset( $user_already_exists ) )
  737. return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
  738. wpmu_welcome_user_notification( $user_id, $password, $meta );
  739. do_action( 'wpmu_activate_user', $user_id, $password, $meta );
  740. return array( 'user_id' => $user_id, 'password' => $password, 'meta' => $meta );
  741. }
  742. $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
  743. // TODO: What to do if we create a user but cannot create a blog?
  744. if ( is_wp_error($blog_id) ) {
  745. // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  746. // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  747. if ( 'blog_taken' == $blog_id->get_error_code() ) {
  748. $blog_id->add_data( $signup );
  749. $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
  750. }
  751. return $blog_id;
  752. }
  753. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  754. wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
  755. do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta);
  756. return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
  757. }
  758. /**
  759. * Create a user.
  760. *
  761. * This function runs when a user self-registers as well as when
  762. * a Super Admin creates a new user. Hook to 'wpmu_new_user' for events
  763. * that should affect all new users, but only on Multisite (otherwise
  764. * use 'user_register').
  765. *
  766. * @since MU
  767. * @uses wp_create_user()
  768. *
  769. * @param string $user_name The new user's login name.
  770. * @param string $password The new user's password.
  771. * @param string $email The new user's email address.
  772. * @return mixed Returns false on failure, or int $user_id on success
  773. */
  774. function wpmu_create_user( $user_name, $password, $email ) {
  775. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  776. $user_id = wp_create_user( $user_name, $password, $email );
  777. if ( is_wp_error( $user_id ) )
  778. return false;
  779. // Newly created users have no roles or caps until they are added to a blog.
  780. delete_user_option( $user_id, 'capabilities' );
  781. delete_user_option( $user_id, 'user_level' );
  782. do_action( 'wpmu_new_user', $user_id );
  783. return $user_id;
  784. }
  785. /**
  786. * Create a site.
  787. *
  788. * This function runs when a user self-registers a new site as well
  789. * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
  790. * for events that should affect all new sites.
  791. *
  792. * On subdirectory installs, $domain is the same as the main site's
  793. * domain, and the path is the subdirectory name (eg 'example.com'
  794. * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
  795. * root domain (eg 'blog1.example.com'), and $path is '/'.
  796. *
  797. * @since MU
  798. * @uses domain_exists()
  799. * @uses insert_blog()
  800. * @uses wp_install_defaults()
  801. * @uses add_user_to_blog()
  802. *
  803. * @param string $domain The new site's domain.
  804. * @param string $path The new site's path.
  805. * @param string $title The new site's title.
  806. * @param int $user_id The user ID of the new site's admin.
  807. * @param array $meta Optional. Used to set initial site options.
  808. * @param int $site_id Optional. Only relevant on multi-network installs.
  809. * @return mixed Returns WP_Error object on failure, int $blog_id on success
  810. */
  811. function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) {
  812. $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
  813. if ( is_subdomain_install() )
  814. $domain = str_replace( '@', '', $domain );
  815. $title = strip_tags( $title );
  816. $user_id = (int) $user_id;
  817. if ( empty($path) )
  818. $path = '/';
  819. // Check if the domain has been used already. We should return an error message.
  820. if ( domain_exists($domain, $path, $site_id) )
  821. return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
  822. if ( !defined('WP_INSTALLING') )
  823. define( 'WP_INSTALLING', true );
  824. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  825. return new WP_Error('insert_blog', __('Could not create site.'));
  826. switch_to_blog($blog_id);
  827. install_blog($blog_id, $title);
  828. wp_install_defaults($user_id);
  829. add_user_to_blog($blog_id, $user_id, 'administrator');
  830. if ( is_array($meta) ) foreach ($meta as $key => $value) {
  831. if ( $key == 'public' || $key == 'archived' || $key == 'mature' || $key == 'spam' || $key == 'deleted' || $key == 'lang_id' )
  832. update_blog_status( $blog_id, $key, $value );
  833. else
  834. update_option( $key, $value );
  835. }
  836. add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
  837. update_option( 'blog_public', (int)$meta['public'] );
  838. if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) )
  839. update_user_meta( $user_id, 'primary_blog', $blog_id );
  840. restore_current_blog();
  841. do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
  842. return $blog_id;
  843. }
  844. /**
  845. * Notifies the network admin that a new site has been activated.
  846. *
  847. * Filter 'newblog_notify_siteadmin' to change the content of
  848. * the notification email.
  849. *
  850. * @since MU
  851. *
  852. * @param int $blog_id The new site's ID.
  853. * @return bool
  854. */
  855. function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  856. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  857. return false;
  858. $email = get_site_option( 'admin_email' );
  859. if ( is_email($email) == false )
  860. return false;
  861. $options_site_url = esc_url(network_admin_url('settings.php'));
  862. switch_to_blog( $blog_id );
  863. $blogname = get_option( 'blogname' );
  864. $siteurl = site_url();
  865. restore_current_blog();
  866. $msg = sprintf( __( 'New Site: %1$s
  867. URL: %2$s
  868. Remote IP: %3$s
  869. Disable these notifications: %4$s' ), $blogname, $siteurl, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
  870. $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
  871. wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
  872. return true;
  873. }
  874. /**
  875. * Notifies the network admin that a new user has been activated.
  876. *
  877. * Filter 'newuser_notify_siteadmin' to change the content of
  878. * the notification email.
  879. *
  880. * @since MU
  881. * @uses apply_filters() Filter newuser_notify_siteadmin to change the content of the email message
  882. *
  883. * @param int $user_id The new user's ID.
  884. * @return bool
  885. */
  886. function newuser_notify_siteadmin( $user_id ) {
  887. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  888. return false;
  889. $email = get_site_option( 'admin_email' );
  890. if ( is_email($email) == false )
  891. return false;
  892. $user = get_userdata( $user_id );
  893. $options_site_url = esc_url(network_admin_url('settings.php'));
  894. $msg = sprintf(__('New User: %1$s
  895. Remote IP: %2$s
  896. Disable these notifications: %3$s'), $user->user_login, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
  897. $msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
  898. wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
  899. return true;
  900. }
  901. /**
  902. * Check whether a blogname is already taken.
  903. *
  904. * Used during the new site registration process to ensure
  905. * that each blogname is unique.
  906. *
  907. * @since MU
  908. *
  909. * @param string $domain The domain to be checked.
  910. * @param string $path The path to be checked.
  911. * @param int $site_id Optional. Relevant only on multi-network installs.
  912. * @return int
  913. */
  914. function domain_exists($domain, $path, $site_id = 1) {
  915. global $wpdb;
  916. $result = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s AND site_id = %d", $domain, $path, $site_id) );
  917. return apply_filters( 'domain_exists', $result, $domain, $path, $site_id );
  918. }
  919. /**
  920. * Store basic site info in the blogs table.
  921. *
  922. * This function creates a row in the wp_blogs table and returns
  923. * the new blog's ID. It is the first step in creating a new blog.
  924. *
  925. * @since MU
  926. *
  927. * @param string $domain The domain of the new site.
  928. * @param string $path The path of the new site.
  929. * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1.
  930. * @return int The ID of the new row
  931. */
  932. function insert_blog($domain, $path, $site_id) {
  933. global $wpdb;
  934. $path = trailingslashit($path);
  935. $site_id = (int) $site_id;
  936. $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
  937. if ( ! $result )
  938. return false;
  939. $blog_id = $wpdb->insert_id;
  940. refresh_blog_details( $blog_id );
  941. return $blog_id;
  942. }
  943. /**
  944. * Install an empty blog.
  945. *
  946. * Creates the new blog tables and options. If calling this function
  947. * directly, be sure to use switch_to_blog() first, so that $wpdb
  948. * points to the new blog.
  949. *
  950. * @since MU
  951. * @uses make_db_current_silent()
  952. * @uses populate_roles()
  953. *
  954. * @param int $blog_id The value returned by insert_blog().
  955. * @param string $blog_title The title of the new site.
  956. */
  957. function install_blog($blog_id, $blog_title = '') {
  958. global $wpdb, $wp_roles, $current_site;
  959. // Cast for security
  960. $blog_id = (int) $blog_id;
  961. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  962. $wpdb->suppress_errors();
  963. if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) )
  964. 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>' );
  965. $wpdb->suppress_errors( false );
  966. $url = get_blogaddress_by_id( $blog_id );
  967. // Set everything up
  968. make_db_current_silent( 'blog' );
  969. populate_options();
  970. populate_roles();
  971. $wp_roles->_init();
  972. $url = untrailingslashit( $url );
  973. update_option( 'siteurl', $url );
  974. update_option( 'home', $url );
  975. if ( get_site_option( 'ms_files_rewriting' ) )
  976. update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
  977. else
  978. update_option( 'upload_path', get_blog_option( $current_site->blog_id, 'upload_path' ) );
  979. update_option( 'blogname', wp_unslash( $blog_title ) );
  980. update_option( 'admin_email', '' );
  981. // remove all perms
  982. $table_prefix = $wpdb->get_blog_prefix();
  983. delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
  984. delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
  985. }
  986. /**
  987. * Set blog defaults.
  988. *
  989. * This function creates a row in the wp_blogs table.
  990. *
  991. * @since MU
  992. * @deprecated MU
  993. * @deprecated Use wp_install_defaults()
  994. * @uses wp_install_defaults()
  995. *
  996. * @param int $blog_id Ignored in this function.
  997. * @param int $user_id
  998. */
  999. function install_blog_defaults($blog_id, $user_id) {
  1000. global $wpdb;
  1001. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1002. $wpdb->suppress_errors();
  1003. wp_install_defaults($user_id);
  1004. $wpdb->suppress_errors( false );
  1005. }
  1006. /**
  1007. * Notify a user that her blog activation has been successful.
  1008. *
  1009. * Filter 'wpmu_welcome_notification' to disable or bypass.
  1010. *
  1011. * Filter 'update_welcome_email' and 'update_welcome_subject' to
  1012. * modify the content and subject line of the notification email.
  1013. *
  1014. * @since MU
  1015. *
  1016. * @param int $blog_id
  1017. * @param int $user_id
  1018. * @param string $password
  1019. * @param string $title The new blog's title
  1020. * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
  1021. * @return bool
  1022. */
  1023. function wpmu_welcome_notification($blog_id, $user_id, $password, $title, $meta = '') {
  1024. global $current_site;
  1025. if ( !apply_filters('wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta) )
  1026. return false;
  1027. $welcome_email = get_site_option( 'welcome_email' );
  1028. if ( $welcome_email == false )
  1029. $welcome_email = __( 'Dear User,
  1030. Your new SITE_NAME site has been successfully set up at:
  1031. BLOG_URL
  1032. You can log in to the administrator account with the following information:
  1033. Username: USERNAME
  1034. Password: PASSWORD
  1035. Log in here: BLOG_URLwp-login.php
  1036. We hope you enjoy your new site. Thanks!
  1037. --The Team @ SITE_NAME' );
  1038. $url = get_blogaddress_by_id($blog_id);
  1039. $user = get_userdata( $user_id );
  1040. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  1041. $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
  1042. $welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
  1043. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1044. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1045. $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta);
  1046. $admin_email = get_site_option( 'admin_email' );
  1047. if ( $admin_email == '' )
  1048. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1049. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1050. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1051. $message = $welcome_email;
  1052. if ( empty( $current_site->site_name ) )
  1053. $current_site->site_name = 'WordPress';
  1054. $subject = apply_filters( 'update_welcome_subject', sprintf(__('New %1$s Site: %2$s'), $current_site->site_name, wp_unslash( $title ) ) );
  1055. wp_mail($user->user_email, $subject, $message, $message_headers);
  1056. return true;
  1057. }
  1058. /**
  1059. * Notify a user that her account activation has been successful.
  1060. *
  1061. * Filter 'wpmu_welcome_user_notification' to disable or bypass.
  1062. *
  1063. * Filter 'update_welcome_user_email' and 'update_welcome_user_subject' to
  1064. * modify the content and subject line of the notification email.
  1065. *
  1066. * @since MU
  1067. *
  1068. * @param int $user_id
  1069. * @param string $password
  1070. * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
  1071. * @return bool
  1072. */
  1073. function wpmu_welcome_user_notification($user_id, $password, $meta = '') {
  1074. global $current_site;
  1075. if ( !apply_filters('wpmu_welcome_user_notification', $user_id, $password, $meta) )
  1076. return false;
  1077. $welcome_email = get_site_option( 'welcome_user_email' );
  1078. $user = get_userdata( $user_id );
  1079. $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta);
  1080. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  1081. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1082. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1083. $welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
  1084. $admin_email = get_site_option( 'admin_email' );
  1085. if ( $admin_email == '' )
  1086. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1087. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1088. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1089. $message = $welcome_email;
  1090. if ( empty( $current_site->site_name ) )
  1091. $current_site->site_name = 'WordPress';
  1092. $subject = apply_filters( 'update_welcome_user_subject', sprintf(__('New %1$s User: %2$s'), $current_site->site_name, $user->user_login) );
  1093. wp_mail($user->user_email, $subject, $message, $message_headers);
  1094. return true;
  1095. }
  1096. /**
  1097. * Get the current site info.
  1098. *
  1099. * Returns an object containing the ID, domain, path, and site_name
  1100. * of the site being viewed.
  1101. *
  1102. * @since MU
  1103. *
  1104. * @return object
  1105. */
  1106. function get_current_site() {
  1107. global $current_site;
  1108. return $current_site;
  1109. }
  1110. /**
  1111. * Get a user's most recent post.
  1112. *
  1113. * Walks through each of a user's blogs to find the post with
  1114. * the most recent post_date_gmt.
  1115. *
  1116. * @since MU
  1117. * @uses get_blogs_of_user()
  1118. *
  1119. * @param int $user_id
  1120. * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
  1121. */
  1122. function get_most_recent_post_of_user( $user_id ) {
  1123. global $wpdb;
  1124. $user_blogs = get_blogs_of_user( (int) $user_id );
  1125. $most_recent_post = array();
  1126. // Walk through each blog and get the most recent post
  1127. // published by $user_id
  1128. foreach ( (array) $user_blogs as $blog ) {
  1129. $prefix = $wpdb->get_blog_prefix( $blog->userblog_id );
  1130. $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);
  1131. // Make sure we found a post
  1132. if ( isset($recent_post['ID']) ) {
  1133. $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
  1134. // If this is the first post checked or if this post is
  1135. // newer than the current recent post, make it the new
  1136. // most recent post.
  1137. if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
  1138. $most_recent_post = array(
  1139. 'blog_id' => $blog->userblog_id,
  1140. 'post_id' => $recent_post['ID'],
  1141. 'post_date_gmt' => $recent_post['post_date_gmt'],
  1142. 'post_gmt_ts' => $post_gmt_ts
  1143. );
  1144. }
  1145. }
  1146. }
  1147. return $most_recent_post;
  1148. }
  1149. // Misc functions
  1150. /**
  1151. * Get the size of a directory.
  1152. *
  1153. * A helper function that is used primarily to check whether
  1154. * a blog has exceeded its allowed upload space.
  1155. *
  1156. * @since MU
  1157. * @uses recurse_dirsize()
  1158. *
  1159. * @param string $directory
  1160. * @return int
  1161. */
  1162. function get_dirsize( $directory ) {
  1163. $dirsize = get_transient( 'dirsize_cache' );
  1164. if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
  1165. return $dirsize[ $directory ][ 'size' ];
  1166. if ( false == is_array( $dirsize ) )
  1167. $dirsize = array();
  1168. $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
  1169. set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
  1170. return $dirsize[ $directory ][ 'size' ];
  1171. }
  1172. /**
  1173. * Get the size of a directory recursively.
  1174. *
  1175. * Used by get_dirsize() to get a directory's size when it contains
  1176. * other directories.
  1177. *
  1178. * @since MU
  1179. *
  1180. * @param string $directory
  1181. * @return int
  1182. */
  1183. function recurse_dirsize( $directory ) {
  1184. $size = 0;
  1185. $directory = untrailingslashit( $directory );
  1186. if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
  1187. return false;
  1188. if ($handle = opendir($directory)) {
  1189. while(($file = readdir($handle)) !== false) {
  1190. $path = $directory.'/'.$file;
  1191. if ($file != '.' && $file != '..') {
  1192. if (is_file($path)) {
  1193. $size += filesize($path);
  1194. } elseif (is_dir($path)) {
  1195. $handlesize = recurse_dirsize($path);
  1196. if ($handlesize > 0)
  1197. $size += $handlesize;
  1198. }
  1199. }
  1200. }
  1201. closedir($handle);
  1202. }
  1203. return $size;
  1204. }
  1205. /**
  1206. * Check an array of MIME types against a whitelist.
  1207. *
  1208. * WordPress ships with a set of allowed upload filetypes,
  1209. * which is defined in wp-includes/functions.php in
  1210. * get_allowed_mime_types(). This function is used to filter
  1211. * that list against the filetype whitelist provided by Multisite
  1212. * Super Admins at wp-admin/network/settings.php.
  1213. *
  1214. * @since MU
  1215. *
  1216. * @param array $mimes
  1217. * @return array
  1218. */
  1219. function check_upload_mimes( $mimes ) {
  1220. $site_exts = explode( ' ', get_site_option( 'upload_filetypes' ) );
  1221. foreach ( $site_exts as $ext ) {
  1222. foreach ( $mimes as $ext_pattern => $mime ) {
  1223. if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
  1224. $site_mimes[$ext_pattern] = $mime;
  1225. }
  1226. }
  1227. return $site_mimes;
  1228. }
  1229. /**
  1230. * Update a blog's post count.
  1231. *
  1232. * WordPress MS stores a blog's post count as an option so as
  1233. * to avoid extraneous COUNTs when a blog's details are fetched
  1234. * with get_blog_details(). This function is called when posts
  1235. * are published to make sure the count stays current.
  1236. *
  1237. * @since MU
  1238. */
  1239. function update_posts_count( $deprecated = '' ) {
  1240. global $wpdb;
  1241. update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
  1242. }
  1243. /**
  1244. * Logs user registrations.
  1245. *
  1246. * @since MU
  1247. *
  1248. * @param int $blog_id
  1249. * @param int $user_id
  1250. */
  1251. function wpmu_log_new_registrations( $blog_id, $user_id ) {
  1252. global $wpdb;
  1253. $user = get_userdata( (int) $user_id );
  1254. $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')) );
  1255. }
  1256. /**
  1257. * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
  1258. *
  1259. * @since 3.0.0
  1260. *
  1261. * @see term_id_filter
  1262. *
  1263. * @param int $term_id An ID for a term on the current blog.
  1264. * @return int An ID from the global terms table mapped from $term_id.
  1265. */
  1266. function global_terms( $term_id, $deprecated = '' ) {
  1267. global $wpdb;
  1268. static $global_terms_recurse = null;
  1269. if ( !global_terms_enabled() )
  1270. return $term_id;
  1271. // prevent a race condition
  1272. $recurse_start = false;
  1273. if ( $global_terms_recurse === null ) {
  1274. $recurse_start = true;
  1275. $global_terms_recurse = 1;
  1276. } elseif ( 10 < $global_terms_recurse++ ) {
  1277. return $term_id;
  1278. }
  1279. $term_id = intval( $term_id );
  1280. $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
  1281. $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
  1282. if ( $global_id == null ) {
  1283. $use