PageRenderTime 67ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/ms-functions.php

https://bitbucket.org/aukhanev/xdn-wordpress31
PHP | 2076 lines | 1023 code | 276 blank | 777 comment | 298 complexity | 439e1b98a9ee98b75d43446f177025fb MD5 | raw file

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

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

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