PageRenderTime 113ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/wordpress3.4.2/wp-includes/ms-functions.php

https://bitbucket.org/ch3tag/mothers
PHP | 1982 lines | 972 code | 269 blank | 741 comment | 279 complexity | dcbb9a44903b2113aa953d299985daec MD5 | raw file

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

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