PageRenderTime 70ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/ms-functions.php

http://officialwp.codeplex.com
PHP | 2016 lines | 976 code | 283 blank | 757 comment | 275 complexity | 4dc74655275eebcc56bafa4287701cf4 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Multisite WordPress API
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 3.0.0
  8. */
  9. /**
  10. * Gets the network's site and user counts.
  11. *
  12. * @since MU 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. $domain = addslashes( $domain );
  252. $weblog_title = addslashes( $weblog_title );
  253. if ( empty($path) )
  254. $path = '/';
  255. // Check if the domain has been used already. We should return an error message.
  256. if ( domain_exists($domain, $path, $site_id) )
  257. return __( '<strong>ERROR</strong>: Site URL already taken.' );
  258. // Need to back up wpdb table names, and create a new wp_blogs entry for new blog.
  259. // Need to get blog_id from wp_blogs, and create new table names.
  260. // Must restore table names at the end of function.
  261. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  262. return __( '<strong>ERROR</strong>: problem creating site entry.' );
  263. switch_to_blog($blog_id);
  264. install_blog($blog_id);
  265. restore_current_blog();
  266. return $blog_id;
  267. }
  268. /**
  269. * Get the permalink for a post on another blog.
  270. *
  271. * @since MU 1.0
  272. *
  273. * @param int $blog_id ID of the source blog.
  274. * @param int $post_id ID of the desired post.
  275. * @return string The post's permalink
  276. */
  277. function get_blog_permalink( $blog_id, $post_id ) {
  278. switch_to_blog( $blog_id );
  279. $link = get_permalink( $post_id );
  280. restore_current_blog();
  281. return $link;
  282. }
  283. /**
  284. * Get a blog's numeric ID from its URL.
  285. *
  286. * On a subdirectory installation like example.com/blog1/,
  287. * $domain will be the root 'example.com' and $path the
  288. * subdirectory '/blog1/'. With subdomains like blog1.example.com,
  289. * $domain is 'blog1.example.com' and $path is '/'.
  290. *
  291. * @since MU 2.6.5
  292. *
  293. * @param string $domain
  294. * @param string $path Optional. Not required for subdomain installations.
  295. * @return int 0 if no blog found, otherwise the ID of the matching blog
  296. */
  297. function get_blog_id_from_url( $domain, $path = '/' ) {
  298. global $wpdb;
  299. $domain = strtolower( $domain );
  300. $path = strtolower( $path );
  301. $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
  302. if ( $id == -1 ) // blog does not exist
  303. return 0;
  304. elseif ( $id )
  305. return (int) $id;
  306. $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 ) );
  307. if ( ! $id ) {
  308. wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
  309. return 0;
  310. }
  311. wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
  312. return $id;
  313. }
  314. // Admin functions
  315. /**
  316. * Checks an email address against a list of banned domains.
  317. *
  318. * This function checks against the Banned Email Domains list
  319. * at wp-admin/network/settings.php. The check is only run on
  320. * self-registrations; user creation at wp-admin/network/users.php
  321. * bypasses this check.
  322. *
  323. * @since MU
  324. *
  325. * @param string $user_email The email provided by the user at registration.
  326. * @return bool Returns true when the email address is banned.
  327. */
  328. function is_email_address_unsafe( $user_email ) {
  329. $banned_names = get_site_option( 'banned_email_domains' );
  330. if ( $banned_names && ! is_array( $banned_names ) )
  331. $banned_names = explode( "\n", $banned_names );
  332. $is_email_address_unsafe = false;
  333. if ( $banned_names && is_array( $banned_names ) ) {
  334. list( $email_local_part, $email_domain ) = explode( '@', $user_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( $user_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. $match = array();
  402. preg_match( '/[0-9]*/', $user_name, $match );
  403. if ( $match[0] == $user_name )
  404. $errors->add('user_name', __('Sorry, usernames must have letters too!'));
  405. if ( !is_email( $user_email ) )
  406. $errors->add('user_email', __( 'Please enter a valid email address.' ) );
  407. $limited_email_domains = get_site_option( 'limited_email_domains' );
  408. if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
  409. $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
  410. if ( in_array( $emaildomain, $limited_email_domains ) == false )
  411. $errors->add('user_email', __('Sorry, that email address is not allowed!'));
  412. }
  413. // Check if the username has been used already.
  414. if ( username_exists($user_name) )
  415. $errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
  416. // Check if the email address has been used already.
  417. if ( email_exists($user_email) )
  418. $errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );
  419. // Has someone already signed up for this username?
  420. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  421. if ( $signup != null ) {
  422. $registered_at = mysql2date('U', $signup->registered);
  423. $now = current_time( 'timestamp', true );
  424. $diff = $now - $registered_at;
  425. // If registered more than two days ago, cancel registration and let this signup go through.
  426. if ( $diff > 2 * DAY_IN_SECONDS )
  427. $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
  428. else
  429. $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
  430. if ( $signup->active == 0 && $signup->user_email == $user_email )
  431. $errors->add('user_email_used', __('username and email used'));
  432. }
  433. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  434. if ( $signup != null ) {
  435. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  436. // If registered more than two days ago, cancel registration and let this signup go through.
  437. if ( $diff > 2 * DAY_IN_SECONDS )
  438. $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
  439. else
  440. $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.'));
  441. }
  442. $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
  443. return apply_filters('wpmu_validate_user_signup', $result);
  444. }
  445. /**
  446. * Processes new site registrations.
  447. *
  448. * Checks the data provided by the user during blog signup. Verifies
  449. * the validity and uniqueness of blog paths and domains.
  450. *
  451. * This function prevents the current user from registering a new site
  452. * with a blogname equivalent to another user's login name. Passing the
  453. * $user parameter to the function, where $user is the other user, is
  454. * effectively an override of this limitation.
  455. *
  456. * Filter 'wpmu_validate_blog_signup' if you want to modify
  457. * the way that WordPress validates new site signups.
  458. *
  459. * @since MU
  460. * @uses domain_exists()
  461. * @uses username_exists()
  462. *
  463. * @param string $blogname The blog name provided by the user. Must be unique.
  464. * @param string $blog_title The blog title provided by the user.
  465. * @return array Contains the new site data and error messages.
  466. */
  467. function wpmu_validate_blog_signup($blogname, $blog_title, $user = '') {
  468. global $wpdb, $domain, $current_site;
  469. $base = $current_site->path;
  470. $blog_title = strip_tags( $blog_title );
  471. $blog_title = substr( $blog_title, 0, 50 );
  472. $errors = new WP_Error();
  473. $illegal_names = get_site_option( 'illegal_names' );
  474. if ( $illegal_names == false ) {
  475. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  476. add_site_option( 'illegal_names', $illegal_names );
  477. }
  478. // On sub dir installs, Some names are so illegal, only a filter can spring them from jail
  479. if (! is_subdomain_install() )
  480. $illegal_names = array_merge($illegal_names, apply_filters( 'subdirectory_reserved_names', array( 'page', 'comments', 'blog', 'files', 'feed' ) ) );
  481. if ( empty( $blogname ) )
  482. $errors->add('blogname', __( 'Please enter a site name.' ) );
  483. if ( preg_match( '/[^a-z0-9]+/', $blogname ) )
  484. $errors->add('blogname', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
  485. if ( in_array( $blogname, $illegal_names ) == true )
  486. $errors->add('blogname', __( 'That name is not allowed.' ) );
  487. if ( strlen( $blogname ) < 4 && !is_super_admin() )
  488. $errors->add('blogname', __( 'Site name must be at least 4 characters.' ) );
  489. if ( strpos( ' ' . $blogname, '_' ) != false )
  490. $errors->add( 'blogname', __( 'Sorry, site names may not contain the character &#8220;_&#8221;!' ) );
  491. // do not allow users to create a blog that conflicts with a page on the main blog.
  492. 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 ) ) )
  493. $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
  494. // all numeric?
  495. $match = array();
  496. preg_match( '/[0-9]*/', $blogname, $match );
  497. if ( $match[0] == $blogname )
  498. $errors->add('blogname', __('Sorry, site names must have letters too!'));
  499. $blogname = apply_filters( 'newblogname', $blogname );
  500. $blog_title = stripslashes( $blog_title );
  501. if ( empty( $blog_title ) )
  502. $errors->add('blog_title', __( 'Please enter a site title.' ) );
  503. // Check if the domain/path has been used already.
  504. if ( is_subdomain_install() ) {
  505. $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
  506. $path = $base;
  507. } else {
  508. $mydomain = "$domain";
  509. $path = $base.$blogname.'/';
  510. }
  511. if ( domain_exists($mydomain, $path, $current_site->id) )
  512. $errors->add( 'blogname', __( 'Sorry, that site already exists!' ) );
  513. if ( username_exists( $blogname ) ) {
  514. if ( is_object( $user ) == false || ( is_object($user) && ( $user->user_login != $blogname ) ) )
  515. $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
  516. }
  517. // Has someone already signed up for this domain?
  518. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
  519. if ( ! empty($signup) ) {
  520. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  521. // If registered more than two days ago, cancel registration and let this signup go through.
  522. if ( $diff > 2 * DAY_IN_SECONDS )
  523. $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) );
  524. else
  525. $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
  526. }
  527. $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'user' => $user, 'errors' => $errors);
  528. return apply_filters('wpmu_validate_blog_signup', $result);
  529. }
  530. /**
  531. * Record site signup information for future activation.
  532. *
  533. * @since MU
  534. * @uses wpmu_signup_blog_notification()
  535. *
  536. * @param string $domain The requested domain.
  537. * @param string $path The requested path.
  538. * @param string $title The requested site title.
  539. * @param string $user The user's requested login name.
  540. * @param string $user_email The user's email address.
  541. * @param array $meta By default, contains the requested privacy setting and lang_id.
  542. */
  543. function wpmu_signup_blog($domain, $path, $title, $user, $user_email, $meta = '') {
  544. global $wpdb;
  545. $key = substr( md5( time() . rand() . $domain ), 0, 16 );
  546. $meta = serialize($meta);
  547. $domain = $wpdb->escape($domain);
  548. $path = $wpdb->escape($path);
  549. $title = $wpdb->escape($title);
  550. $wpdb->insert( $wpdb->signups, array(
  551. 'domain' => $domain,
  552. 'path' => $path,
  553. 'title' => $title,
  554. 'user_login' => $user,
  555. 'user_email' => $user_email,
  556. 'registered' => current_time('mysql', true),
  557. 'activation_key' => $key,
  558. 'meta' => $meta
  559. ) );
  560. wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta);
  561. }
  562. /**
  563. * Record user signup information for future activation.
  564. *
  565. * This function is used when user registration is open but
  566. * new site registration is not.
  567. *
  568. * @since MU
  569. * @uses wpmu_signup_user_notification()
  570. *
  571. * @param string $user The user's requested login name.
  572. * @param string $user_email The user's email address.
  573. * @param array $meta By default, this is an empty array.
  574. */
  575. function wpmu_signup_user($user, $user_email, $meta = '') {
  576. global $wpdb;
  577. // Format data
  578. $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
  579. $user_email = sanitize_email( $user_email );
  580. $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
  581. $meta = serialize($meta);
  582. $wpdb->insert( $wpdb->signups, array(
  583. 'domain' => '',
  584. 'path' => '',
  585. 'title' => '',
  586. 'user_login' => $user,
  587. 'user_email' => $user_email,
  588. 'registered' => current_time('mysql', true),
  589. 'activation_key' => $key,
  590. 'meta' => $meta
  591. ) );
  592. wpmu_signup_user_notification($user, $user_email, $key, $meta);
  593. }
  594. /**
  595. * Notify user of signup success.
  596. *
  597. * This is the notification function used when site registration
  598. * is enabled.
  599. *
  600. * Filter 'wpmu_signup_blog_notification' to bypass this function or
  601. * replace it with your own notification behavior.
  602. *
  603. * Filter 'wpmu_signup_blog_notification_email' and
  604. * 'wpmu_signup_blog_notification_subject' to change the content
  605. * and subject line of the email sent to newly registered users.
  606. *
  607. * @since MU
  608. *
  609. * @param string $domain The new blog domain.
  610. * @param string $path The new blog path.
  611. * @param string $title The site title.
  612. * @param string $user The user's login name.
  613. * @param string $user_email The user's email address.
  614. * @param array $meta By default, contains the requested privacy setting and lang_id.
  615. * @param string $key The activation key created in wpmu_signup_blog()
  616. * @return bool
  617. */
  618. function wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta = '') {
  619. global $current_site;
  620. if ( !apply_filters('wpmu_signup_blog_notification', $domain, $path, $title, $user, $user_email, $key, $meta) )
  621. return false;
  622. // Send email with activation link.
  623. if ( !is_subdomain_install() || $current_site->id != 1 )
  624. $activate_url = network_site_url("wp-activate.php?key=$key");
  625. else
  626. $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
  627. $activate_url = esc_url($activate_url);
  628. $admin_email = get_site_option( 'admin_email' );
  629. if ( $admin_email == '' )
  630. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  631. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  632. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  633. $message = sprintf(
  634. apply_filters( 'wpmu_signup_blog_notification_email',
  635. __( "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" ),
  636. $domain, $path, $title, $user, $user_email, $key, $meta
  637. ),
  638. $activate_url,
  639. esc_url( "http://{$domain}{$path}" ),
  640. $key
  641. );
  642. // TODO: Don't hard code activation link.
  643. $subject = sprintf(
  644. apply_filters( 'wpmu_signup_blog_notification_subject',
  645. __( '[%1$s] Activate %2$s' ),
  646. $domain, $path, $title, $user, $user_email, $key, $meta
  647. ),
  648. $from_name,
  649. esc_url( 'http://' . $domain . $path )
  650. );
  651. wp_mail($user_email, $subject, $message, $message_headers);
  652. return true;
  653. }
  654. /**
  655. * Notify user of signup success.
  656. *
  657. * This is the notification function used when no new site has
  658. * been requested.
  659. *
  660. * Filter 'wpmu_signup_user_notification' to bypass this function or
  661. * replace it with your own notification behavior.
  662. *
  663. * Filter 'wpmu_signup_user_notification_email' and
  664. * 'wpmu_signup_user_notification_subject' to change the content
  665. * and subject line of the email sent to newly registered users.
  666. *
  667. * @since MU
  668. *
  669. * @param string $user The user's login name.
  670. * @param string $user_email The user's email address.
  671. * @param array $meta By default, an empty array.
  672. * @param string $key The activation key created in wpmu_signup_user()
  673. * @return bool
  674. */
  675. function wpmu_signup_user_notification($user, $user_email, $key, $meta = '') {
  676. if ( !apply_filters('wpmu_signup_user_notification', $user, $user_email, $key, $meta) )
  677. return false;
  678. // Send email with activation link.
  679. $admin_email = get_site_option( 'admin_email' );
  680. if ( $admin_email == '' )
  681. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  682. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  683. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  684. $message = sprintf(
  685. apply_filters( 'wpmu_signup_user_notification_email',
  686. __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ),
  687. $user, $user_email, $key, $meta
  688. ),
  689. site_url( "wp-activate.php?key=$key" )
  690. );
  691. // TODO: Don't hard code activation link.
  692. $subject = sprintf(
  693. apply_filters( 'wpmu_signup_user_notification_subject',
  694. __( '[%1$s] Activate %2$s' ),
  695. $user, $user_email, $key, $meta
  696. ),
  697. $from_name,
  698. $user
  699. );
  700. wp_mail($user_email, $subject, $message, $message_headers);
  701. return true;
  702. }
  703. /**
  704. * Activate a signup.
  705. *
  706. * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events
  707. * that should happen only when users or sites are self-created (since
  708. * those actions are not called when users and sites are created
  709. * by a Super Admin).
  710. *
  711. * @since MU
  712. * @uses wp_generate_password()
  713. * @uses wpmu_welcome_user_notification()
  714. * @uses add_user_to_blog()
  715. * @uses add_new_user_to_blog()
  716. * @uses wpmu_create_user()
  717. * @uses wpmu_create_blog()
  718. * @uses wpmu_welcome_notification()
  719. *
  720. * @param string $key The activation key provided to the user.
  721. * @return array An array containing information about the activated user and/or blog
  722. */
  723. function wpmu_activate_signup($key) {
  724. global $wpdb, $current_site;
  725. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
  726. if ( empty( $signup ) )
  727. return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
  728. if ( $signup->active ) {
  729. if ( empty( $signup->domain ) )
  730. return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
  731. else
  732. return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
  733. }
  734. $meta = maybe_unserialize($signup->meta);
  735. $user_login = $wpdb->escape($signup->user_login);
  736. $user_email = $wpdb->escape($signup->user_email);
  737. $password = wp_generate_password( 12, false );
  738. $user_id = username_exists($user_login);
  739. if ( ! $user_id )
  740. $user_id = wpmu_create_user($user_login, $password, $user_email);
  741. else
  742. $user_already_exists = true;
  743. if ( ! $user_id )
  744. return new WP_Error('create_user', __('Could not create user'), $signup);
  745. $now = current_time('mysql', true);
  746. if ( empty($signup->domain) ) {
  747. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  748. if ( isset( $user_already_exists ) )
  749. return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
  750. wpmu_welcome_user_notification($user_id, $password, $meta);
  751. add_new_user_to_blog( $user_id, $user_email, $meta );
  752. do_action('wpmu_activate_user', $user_id, $password, $meta);
  753. return array('user_id' => $user_id, 'password' => $password, 'meta' => $meta);
  754. }
  755. $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
  756. // TODO: What to do if we create a user but cannot create a blog?
  757. if ( is_wp_error($blog_id) ) {
  758. // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  759. // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  760. if ( 'blog_taken' == $blog_id->get_error_code() ) {
  761. $blog_id->add_data( $signup );
  762. $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
  763. }
  764. return $blog_id;
  765. }
  766. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  767. wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
  768. do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta);
  769. return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
  770. }
  771. /**
  772. * Create a user.
  773. *
  774. * This function runs when a user self-registers as well as when
  775. * a Super Admin creates a new user. Hook to 'wpmu_new_user' for events
  776. * that should affect all new users, but only on Multisite (otherwise
  777. * use 'user_register').
  778. *
  779. * @since MU
  780. * @uses wp_create_user()
  781. *
  782. * @param string $user_name The new user's login name.
  783. * @param string $password The new user's password.
  784. * @param string $email The new user's email address.
  785. * @return mixed Returns false on failure, or int $user_id on success
  786. */
  787. function wpmu_create_user( $user_name, $password, $email) {
  788. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  789. $user_id = wp_create_user( $user_name, $password, $email );
  790. if ( is_wp_error($user_id) )
  791. return false;
  792. // Newly created users have no roles or caps until they are added to a blog.
  793. delete_user_option( $user_id, 'capabilities' );
  794. delete_user_option( $user_id, 'user_level' );
  795. do_action( 'wpmu_new_user', $user_id );
  796. return $user_id;
  797. }
  798. /**
  799. * Create a site.
  800. *
  801. * This function runs when a user self-registers a new site as well
  802. * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
  803. * for events that should affect all new sites.
  804. *
  805. * On subdirectory installs, $domain is the same as the main site's
  806. * domain, and the path is the subdirectory name (eg 'example.com'
  807. * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
  808. * root domain (eg 'blog1.example.com'), and $path is '/'.
  809. *
  810. * @since MU
  811. * @uses domain_exists()
  812. * @uses insert_blog()
  813. * @uses wp_install_defaults()
  814. * @uses add_user_to_blog()
  815. *
  816. * @param string $domain The new site's domain.
  817. * @param string $path The new site's path.
  818. * @param string $title The new site's title.
  819. * @param int $user_id The user ID of the new site's admin.
  820. * @param array $meta Optional. Used to set initial site options.
  821. * @param int $site_id Optional. Only relevant on multi-network installs.
  822. * @return mixed Returns WP_Error object on failure, int $blog_id on success
  823. */
  824. function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) {
  825. $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
  826. if ( is_subdomain_install() )
  827. $domain = str_replace( '@', '', $domain );
  828. $title = strip_tags( $title );
  829. $user_id = (int) $user_id;
  830. if ( empty($path) )
  831. $path = '/';
  832. // Check if the domain has been used already. We should return an error message.
  833. if ( domain_exists($domain, $path, $site_id) )
  834. return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
  835. if ( !defined('WP_INSTALLING') )
  836. define( 'WP_INSTALLING', true );
  837. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  838. return new WP_Error('insert_blog', __('Could not create site.'));
  839. switch_to_blog($blog_id);
  840. install_blog($blog_id, $title);
  841. wp_install_defaults($user_id);
  842. add_user_to_blog($blog_id, $user_id, 'administrator');
  843. if ( is_array($meta) ) foreach ($meta as $key => $value) {
  844. if ( $key == 'public' || $key == 'archived' || $key == 'mature' || $key == 'spam' || $key == 'deleted' || $key == 'lang_id' )
  845. update_blog_status( $blog_id, $key, $value );
  846. else
  847. update_option( $key, $value );
  848. }
  849. add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
  850. update_option( 'blog_public', (int)$meta['public'] );
  851. if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) )
  852. update_user_meta( $user_id, 'primary_blog', $blog_id );
  853. restore_current_blog();
  854. do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
  855. return $blog_id;
  856. }
  857. /**
  858. * Notifies the network admin that a new site has been activated.
  859. *
  860. * Filter 'newblog_notify_siteadmin' to change the content of
  861. * the notification email.
  862. *
  863. * @since MU
  864. *
  865. * @param int $blog_id The new site's ID.
  866. * @return bool
  867. */
  868. function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  869. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  870. return false;
  871. $email = get_site_option( 'admin_email' );
  872. if ( is_email($email) == false )
  873. return false;
  874. $options_site_url = esc_url(network_admin_url('settings.php'));
  875. switch_to_blog( $blog_id );
  876. $blogname = get_option( 'blogname' );
  877. $siteurl = site_url();
  878. restore_current_blog();
  879. $msg = sprintf( __( 'New Site: %1$s
  880. URL: %2$s
  881. Remote IP: %3$s
  882. Disable these notifications: %4$s' ), $blogname, $siteurl, $_SERVER['REMOTE_ADDR'], $options_site_url);
  883. $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
  884. wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
  885. return true;
  886. }
  887. /**
  888. * Notifies the network admin that a new user has been activated.
  889. *
  890. * Filter 'newuser_notify_siteadmin' to change the content of
  891. * the notification email.
  892. *
  893. * @since MU
  894. * @uses apply_filters() Filter newuser_notify_siteadmin to change the content of the email message
  895. *
  896. * @param int $user_id The new user's ID.
  897. * @return bool
  898. */
  899. function newuser_notify_siteadmin( $user_id ) {
  900. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  901. return false;
  902. $email = get_site_option( 'admin_email' );
  903. if ( is_email($email) == false )
  904. return false;
  905. $user = get_userdata( $user_id );
  906. $options_site_url = esc_url(network_admin_url('settings.php'));
  907. $msg = sprintf(__('New User: %1$s
  908. Remote IP: %2$s
  909. Disable these notifications: %3$s'), $user->user_login, $_SERVER['REMOTE_ADDR'], $options_site_url);
  910. $msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
  911. wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
  912. return true;
  913. }
  914. /**
  915. * Check whether a blogname is already taken.
  916. *
  917. * Used during the new site registration process to ensure
  918. * that each blogname is unique.
  919. *
  920. * @since MU
  921. *
  922. * @param string $domain The domain to be checked.
  923. * @param string $path The path to be checked.
  924. * @param int $site_id Optional. Relevant only on multi-network installs.
  925. * @return int
  926. */
  927. function domain_exists($domain, $path, $site_id = 1) {
  928. global $wpdb;
  929. $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) );
  930. return apply_filters( 'domain_exists', $result, $domain, $path, $site_id );
  931. }
  932. /**
  933. * Store basic site info in the blogs table.
  934. *
  935. * This function creates a row in the wp_blogs table and returns
  936. * the new blog's ID. It is the first step in creating a new blog.
  937. *
  938. * @since MU
  939. *
  940. * @param string $domain The domain of the new site.
  941. * @param string $path The path of the new site.
  942. * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1.
  943. * @return int The ID of the new row
  944. */
  945. function insert_blog($domain, $path, $site_id) {
  946. global $wpdb;
  947. $path = trailingslashit($path);
  948. $site_id = (int) $site_id;
  949. $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
  950. if ( ! $result )
  951. return false;
  952. refresh_blog_details($wpdb->insert_id);
  953. return $wpdb->insert_id;
  954. }
  955. /**
  956. * Install an empty blog.
  957. *
  958. * Creates the new blog tables and options. If calling this function
  959. * directly, be sure to use switch_to_blog() first, so that $wpdb
  960. * points to the new blog.
  961. *
  962. * @since MU
  963. * @uses make_db_current_silent()
  964. * @uses populate_roles()
  965. *
  966. * @param int $blog_id The value returned by insert_blog().
  967. * @param string $blog_title The title of the new site.
  968. */
  969. function install_blog($blog_id, $blog_title = '') {
  970. global $wpdb, $wp_roles, $current_site;
  971. // Cast for security
  972. $blog_id = (int) $blog_id;
  973. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  974. $wpdb->suppress_errors();
  975. if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) )
  976. 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>' );
  977. $wpdb->suppress_errors( false );
  978. $url = get_blogaddress_by_id( $blog_id );
  979. // Set everything up
  980. make_db_current_silent( 'blog' );
  981. populate_options();
  982. populate_roles();
  983. $wp_roles->_init();
  984. $url = untrailingslashit( $url );
  985. update_option( 'siteurl', $url );
  986. update_option( 'home', $url );
  987. if ( get_site_option( 'ms_files_rewriting' ) )
  988. update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
  989. else
  990. update_option( 'upload_path', get_blog_option( $current_site->blog_id, 'upload_path' ) );
  991. update_option( 'blogname', stripslashes( $blog_title ) );
  992. update_option( 'admin_email', '' );
  993. // remove all perms
  994. $table_prefix = $wpdb->get_blog_prefix();
  995. delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
  996. delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
  997. }
  998. /**
  999. * Set blog defaults.
  1000. *
  1001. * This function creates a row in the wp_blogs table.
  1002. *
  1003. * @since MU
  1004. * @deprecated MU
  1005. * @deprecated Use wp_install_defaults()
  1006. * @uses wp_install_defaults()
  1007. *
  1008. * @param int $blog_id Ignored in this function.
  1009. * @param int $user_id
  1010. */
  1011. function install_blog_defaults($blog_id, $user_id) {
  1012. global $wpdb;
  1013. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1014. $wpdb->suppress_errors();
  1015. wp_install_defaults($user_id);
  1016. $wpdb->suppress_errors( false );
  1017. }
  1018. /**
  1019. * Notify a user that her blog activation has been successful.
  1020. *
  1021. * Filter 'wpmu_welcome_notification' to disable or bypass.
  1022. *
  1023. * Filter 'update_welcome_email' and 'update_welcome_subject' to
  1024. * modify the content and subject line of the notification email.
  1025. *
  1026. * @since MU
  1027. *
  1028. * @param int $blog_id
  1029. * @param int $user_id
  1030. * @param string $password
  1031. * @param string $title The new blog's title
  1032. * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
  1033. * @return bool
  1034. */
  1035. function wpmu_welcome_notification($blog_id, $user_id, $password, $title, $meta = '') {
  1036. global $current_site;
  1037. if ( !apply_filters('wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta) )
  1038. return false;
  1039. $welcome_email = stripslashes( get_site_option( 'welcome_email' ) );
  1040. if ( $welcome_email == false )
  1041. $welcome_email = stripslashes( __( 'Dear User,
  1042. Your new SITE_NAME site has been successfully set up at:
  1043. BLOG_URL
  1044. You can log in to the administrator account with the following information:
  1045. Username: USERNAME
  1046. Password: PASSWORD
  1047. Log in here: BLOG_URLwp-login.php
  1048. We hope you enjoy your new site. Thanks!
  1049. --The Team @ SITE_NAME' ) );
  1050. $url = get_blogaddress_by_id($blog_id);
  1051. $user = get_userdata( $user_id );
  1052. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  1053. $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
  1054. $welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
  1055. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1056. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1057. $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta);
  1058. $admin_email = get_site_option( 'admin_email' );
  1059. if ( $admin_email == '' )
  1060. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1061. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1062. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1063. $message = $welcome_email;
  1064. if ( empty( $current_site->site_name ) )
  1065. $current_site->site_name = 'WordPress';
  1066. $subject = apply_filters( 'update_welcome_subject', sprintf(__('New %1$s Site: %2$s'), $current_site->site_name, stripslashes( $title ) ) );
  1067. wp_mail($user->user_email, $subject, $message, $message_headers);
  1068. return true;
  1069. }
  1070. /**
  1071. * Notify a user that her account activation has been successful.
  1072. *
  1073. * Filter 'wpmu_welcome_user_notification' to disable or bypass.
  1074. *
  1075. * Filter 'update_welcome_user_email' and 'update_welcome_user_subject' to
  1076. * modify the content and subject line of the notification email.
  1077. *
  1078. * @since MU
  1079. *
  1080. * @param int $user_id
  1081. * @param string $password
  1082. * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
  1083. * @return bool
  1084. */
  1085. function wpmu_welcome_user_notification($user_id, $password, $meta = '') {
  1086. global $current_site;
  1087. if ( !apply_filters('wpmu_welcome_user_notification', $user_id, $password, $meta) )
  1088. return false;
  1089. $welcome_email = get_site_option( 'welcome_user_email' );
  1090. $user = get_userdata( $user_id );
  1091. $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta);
  1092. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  1093. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1094. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1095. $welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
  1096. $admin_email = get_site_option( 'admin_email' );
  1097. if ( $admin_email == '' )
  1098. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1099. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1100. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1101. $message = $welcome_email;
  1102. if ( empty( $current_site->site_name ) )
  1103. $current_site->site_name = 'WordPress';
  1104. $subject = apply_filters( 'update_welcome_user_subject', sprintf(__('New %1$s User: %2$s'), $current_site->site_name, $user->user_login) );
  1105. wp_mail($user->user_email, $subject, $message, $message_headers);
  1106. return true;
  1107. }
  1108. /**
  1109. * Get the current site info.
  1110. *
  1111. * Returns an object containing the ID, domain, path, and site_name
  1112. * of the site being viewed.
  1113. *
  1114. * @since MU
  1115. *
  1116. * @return object
  1117. */
  1118. function get_current_site() {
  1119. global $current_site;
  1120. return $current_site;
  1121. }
  1122. /**
  1123. * Get a numeric user ID from either an email address or a login.
  1124. *
  1125. * @since MU
  1126. * @uses is_email()
  1127. *
  1128. * @param string $string
  1129. * @return int
  1130. */
  1131. function get_user_id_from_string( $string ) {
  1132. $user_id = 0;
  1133. if ( is_email( $string ) ) {
  1134. $user = get_user_by('email', $string);
  1135. if ( $user )
  1136. $user_id = $user->ID;
  1137. } elseif ( is_numeric( $string ) ) {
  1138. $user_id = $string;
  1139. } else {
  1140. $user = get_user_by('login', $string);
  1141. if ( $user )
  1142. $user_id = $user->ID;
  1143. }
  1144. return $user_id;
  1145. }
  1146. /**
  1147. * Get a user's most recent post.
  1148. *
  1149. * Walks through each of a user's blogs to find the post with
  1150. * the most recent post_date_gmt.
  1151. *
  1152. * @since MU
  1153. * @uses get_blogs_of_user()
  1154. *
  1155. * @param int $user_id
  1156. * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
  1157. */
  1158. function get_most_recent_post_of_user( $user_id ) {
  1159. global $wpdb;
  1160. $user_blogs = get_blogs_of_user( (int) $user_id );
  1161. $most_recent_post = array();
  1162. // Walk through each blog and get the most recent post
  1163. // published by $user_id
  1164. foreach ( (array) $user_blogs as $blog ) {
  1165. $prefix = $wpdb->get_blog_prefix( $blog->userblog_id );
  1166. $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);
  1167. // Make sure we found a post
  1168. if ( isset($recent_post['ID']) ) {
  1169. $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
  1170. // If this is the first post checked or if this post is
  1171. // newer than the current recent post, make it the new
  1172. // most recent post.
  1173. if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
  1174. $most_recent_post = array(
  1175. 'blog_id' => $blog->userblog_id,
  1176. 'post_id' => $recent_post['ID'],
  1177. 'post_date_gmt' => $recent_post['post_date_gmt'],
  1178. 'post_gmt_ts' => $post_gmt_ts
  1179. );
  1180. }
  1181. }
  1182. }
  1183. return $most_recent_post;
  1184. }
  1185. // Misc functions
  1186. /**
  1187. * Get the size of a directory.
  1188. *
  1189. * A helper function that is used primarily to check whether
  1190. * a blog has exceeded its allowed upload space.
  1191. *
  1192. * @since MU
  1193. * @uses recurse_dirsize()
  1194. *
  1195. * @param string $directory
  1196. * @return int
  1197. */
  1198. function get_dirsize( $directory ) {
  1199. $dirsize = get_transient( 'dirsize_cache' );
  1200. if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
  1201. return $dirsize[ $directory ][ 'size' ];
  1202. if ( false == is_array( $dirsize ) )
  1203. $dirsize = array();
  1204. $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
  1205. set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
  1206. return $dirsize[ $directory ][ 'size' ];
  1207. }
  1208. /**
  1209. * Get the size of a directory recursively.
  1210. *
  1211. * Used by get_dirsize() to get a directory's size when it contains
  1212. * other directories.
  1213. *
  1214. * @since MU
  1215. *
  1216. * @param string $directory
  1217. * @return int
  1218. */
  1219. function recurse_dirsize( $directory ) {
  1220. $size = 0;
  1221. $directory = untrailingslashit( $directory );
  1222. if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
  1223. return false;
  1224. if ($handle = opendir($directory)) {
  1225. while(($file = readdir($handle)) !== false) {
  1226. $path = $directory.'/'.$file;
  1227. if ($file != '.' && $file != '..') {
  1228. if (is_file($path)) {
  1229. $size += filesize($path);
  1230. } elseif (is_dir($path)) {
  1231. $handlesize = recurse_dirsize($path);
  1232. if ($handlesize > 0)
  1233. $size += $handlesize;
  1234. }
  1235. }
  1236. }
  1237. closedir($handle);
  1238. }
  1239. return $size;
  1240. }
  1241. /**
  1242. * Check an array of MIME types against a whitelist.
  1243. *
  1244. * WordPress ships with a set of allowed upload filetypes,
  1245. * which is defined in wp-includes/functions.php in
  1246. * get_allowed_mime_types(). This function is used to filter
  1247. * that list against the filetype whitelist provided by Multisite
  1248. * Super Admins at wp-admin/network/settings.php.
  1249. *
  1250. * @since MU
  1251. *
  1252. * @param array $mimes
  1253. * @return array
  1254. */
  1255. function check_upload_mimes( $mimes ) {
  1256. $site_exts = explode( ' ', get_site_option( 'upload_filetypes' ) );
  1257. foreach ( $site_exts as $ext ) {
  1258. foreach ( $mimes as $ext_pattern => $mime ) {
  1259. if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
  1260. $site_mimes[$ext_pattern] = $mime;
  1261. }
  1262. }
  1263. return $site_mimes;
  1264. }
  1265. /**
  1266. * Update a blog's post count.
  1267. *
  1268. * WordPress MS stores a blog's post count as an option so as
  1269. * to avoid extraneous COUNTs when a blog's details are fetched
  1270. * with get_blog_details(). This function is called when posts
  1271. * are published to make sure the count stays current.
  1272. *
  1273. * @since MU
  1274. */
  1275. function update_posts_count( $deprecated = '' ) {
  1276. global $wpdb;
  1277. update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
  1278. }
  1279. /**
  1280. * Logs user registrations.
  1281. *
  1282. * @since MU
  1283. *
  1284. * @param int $blog_id
  1285. * @param int $user_id
  1286. */
  1287. function wpmu_log_new_registrations( $blog_id, $user_id ) {
  1288. global $wpdb;
  1289. $user = get_userdata( (int) $user_id );
  1290. $wpdb->insert( $wpdb->registration_log, array('email' => $user->user_email, 'IP' => preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] ), 'blog_id' => $blog_id, 'date_registered' => current_time('mysql')) );
  1291. }
  1292. /**
  1293. * Maintains a canonical list …

Large files files are truncated, but you can click here to view the full file