PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  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)) {
  1317. $size += filesize($path);
  1318. } elseif (is_dir($path)) {
  1319. $handlesize = recurse_dirsize($path);
  1320. if ($handlesize > 0)
  1321. $size += $handlesize;
  1322. }
  1323. }
  1324. }
  1325. closedir($handle);
  1326. }
  1327. return $size;
  1328. }
  1329. /**
  1330. * Check whether a blog has used its allotted upload space.
  1331. *
  1332. * Used by get_dirsize() to get a directory's size when it contains
  1333. * other directories.
  1334. *
  1335. * @since MU
  1336. * @uses get_dirsize()
  1337. *
  1338. * @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true.
  1339. * @return int
  1340. */
  1341. function upload_is_user_over_quota( $echo = true ) {
  1342. if ( get_site_option( 'upload_space_check_disabled' ) )
  1343. return false;
  1344. $spaceAllowed = get_space_allowed();
  1345. if ( empty( $spaceAllowed ) || !is_numeric( $spaceAllowed ) )
  1346. $spaceAllowed = 10; // Default space allowed is 10 MB
  1347. $dirName = BLOGUPLOADDIR;
  1348. $size = get_dirsize($dirName) / 1024 / 1024;
  1349. if ( ($spaceAllowed-$size) < 0 ) {
  1350. if ( $echo )
  1351. _e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' ); // No space left
  1352. return true;
  1353. } else {
  1354. return false;
  1355. }
  1356. }
  1357. /**
  1358. * Check an array of MIME types against a whitelist.
  1359. *
  1360. * WordPress ships with a set of allowed upload filetypes,
  1361. * which is defined in wp-includes/functions.php in
  1362. * get_allowed_mime_types(). This function is used to filter
  1363. * that list against the filetype whitelist provided by Multisite
  1364. * Super Admins at wp-admin/network/settings.php.
  1365. *
  1366. * @since MU
  1367. *
  1368. * @param array $mimes
  1369. * @return array
  1370. */
  1371. function check_upload_mimes( $mimes ) {
  1372. $site_exts = explode( ' ', get_site_option( 'upload_filetypes' ) );
  1373. foreach ( $site_exts as $ext ) {
  1374. foreach ( $mimes as $ext_pattern => $mime ) {
  1375. if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
  1376. $site_mimes[$ext_pattern] = $mime;
  1377. }
  1378. }
  1379. return $site_mimes;
  1380. }
  1381. /**
  1382. * Update a blog's post count.
  1383. *
  1384. * WordPress MS stores a blog's post count as an option so as
  1385. * to avoid extraneous COUNTs when a blog's details are fetched
  1386. * with get_blog_details(). This function is called when posts
  1387. * are published to make sure the count stays current.
  1388. *
  1389. * @since MU
  1390. */
  1391. function update_posts_count( $deprecated = '' ) {
  1392. global $wpdb;
  1393. update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
  1394. }
  1395. /**
  1396. * Logs user registrations.
  1397. *
  1398. * @since MU
  1399. *
  1400. * @param int $blog_id
  1401. * @param int $user_id
  1402. */
  1403. function wpmu_log_new_registrations( $blog_id, $user_id ) {
  1404. global $wpdb;
  1405. $user = new WP_User( (int) $user_id );
  1406. $wpdb->insert( $wpdb->registration_log, array('email' => $user->user_email, 'IP' => preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] ), 'blog_id' => $blog_id, 'date_registered' => current_time('mysql')) );
  1407. }
  1408. /**
  1409. * Get the remaining upload space for this blog.
  1410. *
  1411. * @since MU
  1412. * @uses upload_is_user_over_quota()
  1413. * @uses get_space_allowed()
  1414. * @uses get_dirsize()
  1415. *
  1416. * @param int $size
  1417. * @return int
  1418. */
  1419. function fix_import_form_size( $size ) {
  1420. if ( upload_is_user_over_quota( false ) == true )
  1421. return 0;
  1422. $spaceAllowed = 1024 * 1024 * get_space_allowed();
  1423. $dirName = BLOGUPLOADDIR;
  1424. $dirsize = get_dirsize($dirName) ;
  1425. if ( $size > $spaceAllowed - $dirsize )
  1426. return $spaceAllowed - $dirsize; // remaining space
  1427. else
  1428. return $size; // default
  1429. }
  1430. /**
  1431. * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
  1432. *
  1433. * @since 3.0.0
  1434. *
  1435. * @see term_id_filter
  1436. *
  1437. * @param int $term_id An ID for a term on the current blog.
  1438. * @return int An ID from the global terms table mapped from $term_id.
  1439. */
  1440. function global_terms( $term_id, $deprecated = '' ) {
  1441. global $wpdb;
  1442. static $global_terms_recurse = null;
  1443. if ( !global_terms_enabled() )
  1444. return $term_id;
  1445. // prevent a race condition
  1446. $recurse_start = false;
  1447. if ( $global_terms_recurse === null ) {
  1448. $recurse_start = true;
  1449. $global_terms_recurse = 1;
  1450. } elseif ( 10 < $global_terms_recurse++ ) {
  1451. return $term_id;
  1452. }
  1453. $term_id = intval( $term_id );
  1454. $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
  1455. $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
  1456. if ( $global_id == null ) {
  1457. $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
  1458. if ( null == $used_global_id ) {
  1459. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1460. $global_id = $wpdb->insert_id;
  1461. if ( empty( $global_id ) )
  1462. return $term_id;
  1463. } else {
  1464. $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
  1465. $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
  1466. $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
  1467. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1468. $global_id = $wpdb->insert_id;
  1469. }
  1470. } elseif ( $global_id != $term_id ) {
  1471. $local_id = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
  1472. if ( null != $local_id )
  1473. $local_id = global_terms( $local_id );
  1474. if ( 10 < $global_terms_recurse )
  1475. $global_id = $term_id;
  1476. }
  1477. if ( $global_id != $term_id ) {
  1478. if ( get_option( 'default_category' ) == $term_id )
  1479. update_option( 'default_category', $global_id );
  1480. $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
  1481. $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
  1482. $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
  1483. clean_term_cache($term_id);
  1484. }
  1485. if( $recurse_start )
  1486. $global_terms_recurse = null;
  1487. return $global_id;
  1488. }
  1489. /**
  1490. * Ensure that the current site's domain is listed in the allowed redirect host list.
  1491. *
  1492. * @see wp_validate_redirect()
  1493. * @since MU
  1494. *
  1495. * @return array The current site's domain
  1496. */
  1497. function redirect_this_site( $deprecated = '' ) {
  1498. global $current_site;
  1499. return array( $current_site->domain );
  1500. }
  1501. /**
  1502. * Check whether an upload is too big.
  1503. *
  1504. * @since MU
  1505. *
  1506. * @param array $upload
  1507. * @return mixed If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
  1508. */
  1509. function upload_is_file_too_big( $upload ) {
  1510. if ( is_array( $upload ) == false || defined( 'WP_IMPORTING' ) )
  1511. return $upload;
  1512. if ( strlen( $upload['bits'] ) > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
  1513. return sprintf( __( 'This file is too big. Files must be less than %d KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ));
  1514. return $upload;
  1515. }
  1516. /**
  1517. * Add a nonce field to the signup page.
  1518. *
  1519. * @since MU
  1520. * @uses wp_nonce_field()
  1521. */
  1522. function signup_nonce_fields() {
  1523. $id = mt_rand();
  1524. echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
  1525. wp_nonce_field('signup_form_' . $id, '_signup_form', false);
  1526. }
  1527. /**
  1528. * Process the signup nonce created in signup_nonce_fields().
  1529. *
  1530. * @since MU
  1531. * @uses wp_create_nonce()
  1532. *
  1533. * @param array $result
  1534. * @return array
  1535. */
  1536. function signup_nonce_check( $result ) {
  1537. if ( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
  1538. return $result;
  1539. if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
  1540. wp_die( __('Please try again!') );
  1541. return $result;
  1542. }
  1543. /**
  1544. * Correct 404 redirects when NOBLOGREDIRECT is defined.
  1545. *
  1546. * @since MU
  1547. */
  1548. function maybe_redirect_404() {
  1549. global $current_site;
  1550. if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
  1551. if ( $destination == '%siteurl%' )
  1552. $destination = network_home_url();
  1553. wp_redirect( $destination );
  1554. exit();
  1555. }
  1556. }
  1557. /**
  1558. * Add a new user to a blog by visiting /newbloguser/username/.
  1559. *
  1560. * This will only work when the user's details are saved as an option
  1561. * keyed as 'new_user_x', where 'x' is the username of the user to be
  1562. * added, as when a user is invited through the regular WP Add User interface.
  1563. *
  1564. * @since MU
  1565. * @uses add_existing_user_to_blog()
  1566. */
  1567. function maybe_add_existing_user_to_blog() {
  1568. if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) )
  1569. return false;
  1570. $parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
  1571. $key = array_pop( $parts );
  1572. if ( $key == '' )
  1573. $key = array_pop( $parts );
  1574. $details = get_option( 'new_user_' . $key );
  1575. if ( !empty( $details ) )
  1576. delete_option( 'new_user_' . $key );
  1577. if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) )
  1578. wp_die( sprintf(__('An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.'), site_url() ) );
  1579. wp_die( sprintf(__('You have been added to this site. Please visit the <a href="%s">homepage</a> or <a href="%s">login</a> using your username and password.'), site_url(), admin_url() ), __('Success') );
  1580. }
  1581. /**
  1582. * Add a user to a blog based on details from maybe_add_existing_user_to_blog().
  1583. *
  1584. * @since MU
  1585. * @uses add_user_to_blog()
  1586. *
  1587. * @param array $details
  1588. */
  1589. function add_existing_user_to_blog( $details = false ) {
  1590. global $blog_id;
  1591. if ( is_array( $details ) ) {
  1592. $result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] );
  1593. do_action( 'added_existing_user', $details[ 'user_id' ], $result );
  1594. }
  1595. return $result;
  1596. }
  1597. /**
  1598. * Add a newly created user to the appropriate blog
  1599. *
  1600. * @since MU
  1601. *
  1602. * @param int $user_id
  1603. * @param string $email
  1604. * @param array $meta
  1605. */
  1606. function add_new_user_to_blog( $user_id, $email, $meta ) {
  1607. global $current_site;
  1608. if ( $meta[ 'add_to_blog' ] ) {
  1609. $blog_id = $meta[ 'add_to_blog' ];
  1610. $role = $meta[ 'new_role' ];
  1611. remove_user_from_blog($user_id, $current_site->blog_id); // remove user from main blog.
  1612. add_user_to_blog( $blog_id, $user_id, $role );
  1613. update_user_meta( $user_id, 'primary_blog', $blog_id );
  1614. }
  1615. }
  1616. /**
  1617. * Correct From host on outgoing mail to match the site domain
  1618. *
  1619. * @since MU
  1620. */
  1621. function fix_phpmailer_messageid( $phpmailer ) {
  1622. global $current_site;
  1623. $phpmailer->Hostname = $current_site->domain;
  1624. }
  1625. /**
  1626. * Check to see whether a user is marked as a spammer, based on username
  1627. *
  1628. * @since MU
  1629. * @uses get_current_user_id()
  1630. * @uses get_user_id_from_string()
  1631. *
  1632. * @param string $username
  1633. * @return bool
  1634. */
  1635. function is_user_spammy( $username = 0 ) {
  1636. if ( $username == 0 ) {
  1637. $user_id = get_current_user_id();
  1638. } else {
  1639. $user_id = get_user_id_from_string( $username );
  1640. }
  1641. $u = new WP_User( $user_id );
  1642. return ( isset( $u->spam ) && $u->spam == 1 );
  1643. }
  1644. /**
  1645. * Update this blog's 'public' setting in the global blogs table.
  1646. *
  1647. * Public blogs have a setting of 1, private blogs are 0.
  1648. *
  1649. * @since MU
  1650. * @uses update_blog_status()
  1651. *
  1652. * @param int $old_value
  1653. * @param int $value The new public value
  1654. * @return bool
  1655. */
  1656. function update_blog_public( $old_value, $value ) {
  1657. global $wpdb;
  1658. do_action('update_blog_public');
  1659. update_blog_status( $wpdb->blogid, 'public', (int) $value );
  1660. }
  1661. add_action('update_option_blog_public', 'update_blog_public', 10, 2);
  1662. /**
  1663. * Get the "dashboard blog", the blog where users without a blog edit their profile data.
  1664. *
  1665. * @since MU
  1666. * @uses get_blog_details()
  1667. *
  1668. * @return int
  1669. */
  1670. function get_dashboard_blog() {
  1671. if ( $blog = get_site_option( 'dashboard_blog' ) )
  1672. return get_blog_details( $blog );
  1673. return get_blog_details( $GLOBALS['current_site']->blog_id );
  1674. }
  1675. /**
  1676. * Check whether a usermeta key has to do with the current blog.
  1677. *
  1678. * @since MU
  1679. * @uses wp_get_current_user()
  1680. *
  1681. * @param string $key
  1682. * @param int $user_id Optional. Defaults to current user.
  1683. * @param int $blog_id Optional. Defaults to current blog.
  1684. * @return bool
  1685. */
  1686. function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {
  1687. global $wpdb;
  1688. $current_user = wp_get_current_user();
  1689. if ( $user_id == 0 )
  1690. $user_id = $current_user->ID;
  1691. if ( $blog_id == 0 )
  1692. $blog_id = $wpdb->blogid;
  1693. $local_key = $wpdb->base_prefix . $blog_id . '_' . $key;
  1694. if ( isset( $current_user->$local_key ) )
  1695. return true;
  1696. return false;
  1697. }
  1698. /**
  1699. * Check whether users can self-register, based on Network settings.
  1700. *
  1701. * @since MU
  1702. *
  1703. * @return bool
  1704. */
  1705. function users_can_register_signup_filter() {
  1706. $registration = get_site_option('registration');
  1707. if ( $registration == 'all' || $registration == 'user' )
  1708. return true;
  1709. return false;
  1710. }
  1711. add_filter('option_users_can_register', 'users_can_register_signup_filter');
  1712. /**
  1713. * Ensure that the welcome message is not empty. Currently unused.
  1714. *
  1715. * @since MU
  1716. *
  1717. * @param string $text
  1718. * @return string
  1719. */
  1720. function welcome_user_msg_filter( $text ) {
  1721. if ( !$text ) {
  1722. return __( 'Dear User,
  1723. Your new account is set up.
  1724. You can log in with the following information:
  1725. Username: USERNAME
  1726. Password: PASSWORD
  1727. LOGINLINK
  1728. Thanks!
  1729. --The Team @ SITE_NAME' );
  1730. }
  1731. return $text;
  1732. }
  1733. add_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
  1734. /**
  1735. * Whether to force SSL on content.
  1736. *
  1737. * @since 2.8.5
  1738. *
  1739. * @param string|bool $force
  1740. * @return bool True if forced, false if not forced.
  1741. */
  1742. function force_ssl_content( $force = '' ) {
  1743. static $forced_content;
  1744. if ( '' != $force ) {
  1745. $old_forced = $forced_content;
  1746. $forced_content = $force;
  1747. return $old_forced;
  1748. }
  1749. return $forced_content;
  1750. }
  1751. /**
  1752. * Formats an String URL to use HTTPS if HTTP is found.
  1753. * Useful as a filter.
  1754. *
  1755. * @since 2.8.5
  1756. **/
  1757. function filter_SSL( $url ) {
  1758. if ( !is_string( $url ) )
  1759. return get_bloginfo( 'url' ); //return home blog url with proper scheme
  1760. $arrURL = parse_url( $url );
  1761. if ( force_ssl_content() && is_ssl() ) {
  1762. if ( 'http' === $arrURL['scheme'] && 'https' !== $arrURL['scheme'] )
  1763. $url = str_replace( $arrURL['scheme'], 'https', $url );
  1764. }
  1765. return $url;
  1766. }
  1767. /**
  1768. * Schedule update of the network-wide counts for the current network.
  1769. *
  1770. * @since 3.1.0
  1771. */
  1772. function wp_schedule_update_network_counts() {
  1773. if ( !is_main_site() )
  1774. return;
  1775. if ( !wp_next_scheduled('update_network_counts') && !defined('WP_INSTALLING') )
  1776. wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
  1777. }
  1778. /**
  1779. * Update the network-wide counts for the current network.
  1780. *
  1781. * @since 3.1.0
  1782. */
  1783. function wp_update_network_counts() {
  1784. global $wpdb;
  1785. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(blog_id) as c FROM $wpdb->blogs WHERE site_id = %d AND spam = '0' AND deleted = '0' and archived = '0'", $wpdb->siteid) );
  1786. update_site_option( 'blog_count', $count );
  1787. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'") );
  1788. update_site_option( 'user_count', $count );
  1789. }
  1790. ?>