PageRenderTime 32ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/ms-functions.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 2074 lines | 1021 code | 276 blank | 777 comment | 298 complexity | 5dd1b85b09abb178428b373fe9951b47 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  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. );
  780. // TODO: Don't hard code activation link.
  781. $subject = sprintf(
  782. apply_filters( 'wpmu_signup_user_notification_subject',
  783. __( '[%1$s] Activate %2$s' ),
  784. $user, $user_email, $key, $meta
  785. ),
  786. $from_name,
  787. $user
  788. );
  789. wp_mail($user_email, $subject, $message, $message_headers);
  790. return true;
  791. }
  792. /**
  793. * Activate a signup.
  794. *
  795. * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events
  796. * that should happen only when users or sites are self-created (since
  797. * those actions are not called when users and sites are created
  798. * by a Super Admin).
  799. *
  800. * @since MU
  801. * @uses wp_generate_password()
  802. * @uses wpmu_welcome_user_notification()
  803. * @uses add_user_to_blog()
  804. * @uses add_new_user_to_blog()
  805. * @uses wpmu_create_user()
  806. * @uses wpmu_create_blog()
  807. * @uses wpmu_welcome_notification()
  808. *
  809. * @param string $key The activation key provided to the user.
  810. * @return array An array containing information about the activated user and/or blog
  811. */
  812. function wpmu_activate_signup($key) {
  813. global $wpdb, $current_site;
  814. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
  815. if ( empty( $signup ) )
  816. return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
  817. if ( $signup->active ) {
  818. if ( empty( $signup->domain ) )
  819. return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
  820. else
  821. return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
  822. }
  823. $meta = unserialize($signup->meta);
  824. $user_login = $wpdb->escape($signup->user_login);
  825. $user_email = $wpdb->escape($signup->user_email);
  826. $password = wp_generate_password( 12, false );
  827. $user_id = username_exists($user_login);
  828. if ( ! $user_id )
  829. $user_id = wpmu_create_user($user_login, $password, $user_email);
  830. else
  831. $user_already_exists = true;
  832. if ( ! $user_id )
  833. return new WP_Error('create_user', __('Could not create user'), $signup);
  834. $now = current_time('mysql', true);
  835. if ( empty($signup->domain) ) {
  836. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  837. if ( isset( $user_already_exists ) )
  838. return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
  839. wpmu_welcome_user_notification($user_id, $password, $meta);
  840. add_new_user_to_blog( $user_id, $user_email, $meta );
  841. do_action('wpmu_activate_user', $user_id, $password, $meta);
  842. return array('user_id' => $user_id, 'password' => $password, 'meta' => $meta);
  843. }
  844. $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
  845. // TODO: What to do if we create a user but cannot create a blog?
  846. if ( is_wp_error($blog_id) ) {
  847. // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  848. // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  849. if ( 'blog_taken' == $blog_id->get_error_code() ) {
  850. $blog_id->add_data( $signup );
  851. $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
  852. }
  853. return $blog_id;
  854. }
  855. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  856. wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
  857. do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta);
  858. return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
  859. }
  860. /**
  861. * Create a user.
  862. *
  863. * This function runs when a user self-registers as well as when
  864. * a Super Admin creates a new user. Hook to 'wpmu_new_user' for events
  865. * that should affect all new users, but only on Multisite (otherwise
  866. * use 'user_register').
  867. *
  868. * @since MU
  869. * @uses wp_create_user()
  870. *
  871. * @param string $user_name The new user's login name.
  872. * @param string $password The new user's password.
  873. * @param string $email The new user's email address.
  874. * @return mixed Returns false on failure, or int $user_id on success
  875. */
  876. function wpmu_create_user( $user_name, $password, $email) {
  877. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  878. $user_id = wp_create_user( $user_name, $password, $email );
  879. if ( is_wp_error($user_id) )
  880. return false;
  881. // Newly created users have no roles or caps until they are added to a blog.
  882. delete_user_option( $user_id, 'capabilities' );
  883. delete_user_option( $user_id, 'user_level' );
  884. do_action( 'wpmu_new_user', $user_id );
  885. return $user_id;
  886. }
  887. /**
  888. * Create a site.
  889. *
  890. * This function runs when a user self-registers a new site as well
  891. * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
  892. * for events that should affect all new sites.
  893. *
  894. * On subdirectory installs, $domain is the same as the main site's
  895. * domain, and the path is the subdirectory name (eg 'example.com'
  896. * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
  897. * root domain (eg 'blog1.example.com'), and $path is '/'.
  898. *
  899. * @since MU
  900. * @uses domain_exists()
  901. * @uses insert_blog()
  902. * @uses wp_install_defaults()
  903. * @uses add_user_to_blog()
  904. *
  905. * @param string $domain The new site's domain.
  906. * @param string $path The new site's path.
  907. * @param string $title The new site's title.
  908. * @param int $user_id The user ID of the new site's admin.
  909. * @param array $meta Optional. Used to set initial site options.
  910. * @param int $site_id Optional. Only relevant on multi-network installs.
  911. * @return mixed Returns WP_Error object on failure, int $blog_id on success
  912. */
  913. function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) {
  914. $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
  915. if ( is_subdomain_install() )
  916. $domain = str_replace( '@', '', $domain );
  917. $title = strip_tags( $title );
  918. $user_id = (int) $user_id;
  919. if ( empty($path) )
  920. $path = '/';
  921. // Check if the domain has been used already. We should return an error message.
  922. if ( domain_exists($domain, $path, $site_id) )
  923. return new WP_Error('blog_taken', __('Site already exists.'));
  924. if ( !defined('WP_INSTALLING') )
  925. define( 'WP_INSTALLING', true );
  926. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  927. return new WP_Error('insert_blog', __('Could not create site.'));
  928. switch_to_blog($blog_id);
  929. install_blog($blog_id, $title);
  930. wp_install_defaults($user_id);
  931. add_user_to_blog($blog_id, $user_id, 'administrator');
  932. if ( is_array($meta) ) foreach ($meta as $key => $value) {
  933. if ( $key == 'public' || $key == 'archived' || $key == 'mature' || $key == 'spam' || $key == 'deleted' || $key == 'lang_id' )
  934. update_blog_status( $blog_id, $key, $value );
  935. else
  936. update_option( $key, $value );
  937. }
  938. add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
  939. update_option( 'blog_public', (int)$meta['public'] );
  940. if ( !is_super_admin() && ! get_user_meta( $user_id, 'primary_blog', true ) )
  941. update_user_meta( $user_id, 'primary_blog', $blog_id );
  942. restore_current_blog();
  943. do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
  944. return $blog_id;
  945. }
  946. /**
  947. * Notifies the network admin that a new site has been activated.
  948. *
  949. * Filter 'newblog_notify_siteadmin' to change the content of
  950. * the notification email.
  951. *
  952. * @since MU
  953. *
  954. * @param int $blog_id The new site's ID.
  955. * @return bool
  956. */
  957. function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  958. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  959. return false;
  960. $email = get_site_option( 'admin_email' );
  961. if ( is_email($email) == false )
  962. return false;
  963. $options_site_url = esc_url(network_admin_url('settings.php'));
  964. switch_to_blog( $blog_id );
  965. $blogname = get_option( 'blogname' );
  966. $siteurl = site_url();
  967. restore_current_blog();
  968. $msg = sprintf( __( 'New Site: %1s
  969. URL: %2s
  970. Remote IP: %3s
  971. Disable these notifications: %4s' ), $blogname, $siteurl, $_SERVER['REMOTE_ADDR'], $options_site_url);
  972. $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
  973. wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
  974. return true;
  975. }
  976. /**
  977. * Notifies the network admin that a new user has been activated.
  978. *
  979. * Filter 'newuser_notify_siteadmin' to change the content of
  980. * the notification email.
  981. *
  982. * @since MU
  983. *
  984. * @param int $user_id The new user's ID.
  985. * @return bool
  986. */
  987. function newuser_notify_siteadmin( $user_id ) {
  988. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  989. return false;
  990. $email = get_site_option( 'admin_email' );
  991. if ( is_email($email) == false )
  992. return false;
  993. $user = new WP_User($user_id);
  994. $options_site_url = esc_url(network_admin_url('settings.php'));
  995. $msg = sprintf(__('New User: %1s
  996. Remote IP: %2s
  997. Disable these notifications: %3s'), $user->user_login, $_SERVER['REMOTE_ADDR'], $options_site_url);
  998. $msg = apply_filters( 'newuser_notify_siteadmin', $msg );
  999. wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
  1000. return true;
  1001. }
  1002. /**
  1003. * Check whether a blogname is already taken.
  1004. *
  1005. * Used during the new site registration process to ensure
  1006. * that each blogname is unique.
  1007. *
  1008. * @since MU
  1009. *
  1010. * @param string $domain The domain to be checked.
  1011. * @param string $path The path to be checked.
  1012. * @param int $site_id Optional. Relevant only on multi-network installs.
  1013. * @return int
  1014. */
  1015. function domain_exists($domain, $path, $site_id = 1) {
  1016. global $wpdb;
  1017. 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) );
  1018. }
  1019. /**
  1020. * Store basic site info in the blogs table.
  1021. *
  1022. * This function creates a row in the wp_blogs table and returns
  1023. * the new blog's ID. It is the first step in creating a new blog.
  1024. *
  1025. * @since MU
  1026. *
  1027. * @param string $domain The domain of the new site.
  1028. * @param string $path The path of the new site.
  1029. * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1.
  1030. * @return int The ID of the new row
  1031. */
  1032. function insert_blog($domain, $path, $site_id) {
  1033. global $wpdb;
  1034. $path = trailingslashit($path);
  1035. $site_id = (int) $site_id;
  1036. $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
  1037. if ( ! $result )
  1038. return false;
  1039. refresh_blog_details($wpdb->insert_id);
  1040. return $wpdb->insert_id;
  1041. }
  1042. /**
  1043. * Install an empty blog.
  1044. *
  1045. * Creates the new blog tables and options. If calling this function
  1046. * directly, be sure to use switch_to_blog() first, so that $wpdb
  1047. * points to the new blog.
  1048. *
  1049. * @since MU
  1050. * @uses make_db_current_silent()
  1051. * @uses populate_roles()
  1052. *
  1053. * @param int $blog_id The value returned by insert_blog().
  1054. * @param string $blog_title The title of the new site.
  1055. */
  1056. function install_blog($blog_id, $blog_title = '') {
  1057. global $wpdb, $table_prefix, $wp_roles;
  1058. $wpdb->suppress_errors();
  1059. // Cast for security
  1060. $blog_id = (int) $blog_id;
  1061. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1062. if ( $wpdb->get_results("SELECT ID FROM $wpdb->posts") )
  1063. 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>');
  1064. $wpdb->suppress_errors(false);
  1065. $url = get_blogaddress_by_id($blog_id);
  1066. // Set everything up
  1067. make_db_current_silent();
  1068. populate_options();
  1069. populate_roles();
  1070. $wp_roles->_init();
  1071. // fix url.
  1072. update_option('siteurl', $url);
  1073. update_option('home', $url);
  1074. update_option('fileupload_url', $url . "files" );
  1075. update_option('upload_path', UPLOADBLOGSDIR . "/$blog_id/files");
  1076. update_option('blogname', stripslashes( $blog_title ) );
  1077. update_option('admin_email', '');
  1078. $wpdb->update( $wpdb->options, array('option_value' => ''), array('option_name' => 'admin_email') );
  1079. // remove all perms
  1080. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'user_level') );
  1081. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'capabilities') );
  1082. $wpdb->suppress_errors( false );
  1083. }
  1084. /**
  1085. * Set blog defaults.
  1086. *
  1087. * This function creates a row in the wp_blogs table.
  1088. *
  1089. * @since MU
  1090. * @deprecated MU
  1091. * @deprecated Use wp_install_defaults()
  1092. * @uses wp_install_defaults()
  1093. *
  1094. * @param int $blog_id Ignored in this function.
  1095. * @param int $user_id
  1096. */
  1097. function install_blog_defaults($blog_id, $user_id) {
  1098. global $wpdb;
  1099. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1100. $wpdb->suppress_errors();
  1101. wp_install_defaults($user_id);
  1102. $wpdb->suppress_errors( false );
  1103. }
  1104. /**
  1105. * Notify a user that her blog activation has been successful.
  1106. *
  1107. * Filter 'wpmu_welcome_notification' to disable or bypass.
  1108. *
  1109. * Filter 'update_welcome_email' and 'update_welcome_subject' to
  1110. * modify the content and subject line of the notification email.
  1111. *
  1112. * @since MU
  1113. *
  1114. * @param int $blog_id
  1115. * @param int $user_id
  1116. * @param string $password
  1117. * @param string $title The new blog's title
  1118. * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
  1119. * @return bool
  1120. */
  1121. function wpmu_welcome_notification($blog_id, $user_id, $password, $title, $meta = '') {
  1122. global $current_site;
  1123. if ( !apply_filters('wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta) )
  1124. return false;
  1125. $welcome_email = stripslashes( get_site_option( 'welcome_email' ) );
  1126. if ( $welcome_email == false )
  1127. $welcome_email = stripslashes( __( 'Dear User,
  1128. Your new SITE_NAME site has been successfully set up at:
  1129. BLOG_URL
  1130. You can log in to the administrator account with the following information:
  1131. Username: USERNAME
  1132. Password: PASSWORD
  1133. Log in here: BLOG_URLwp-login.php
  1134. We hope you enjoy your new site. Thanks!
  1135. --The Team @ SITE_NAME' ) );
  1136. $url = get_blogaddress_by_id($blog_id);
  1137. $user = new WP_User($user_id);
  1138. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  1139. $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
  1140. $welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
  1141. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1142. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1143. $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta);
  1144. $admin_email = get_site_option( 'admin_email' );
  1145. if ( $admin_email == '' )
  1146. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1147. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1148. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1149. $message = $welcome_email;
  1150. if ( empty( $current_site->site_name ) )
  1151. $current_site->site_name = 'WordPress';
  1152. $subject = apply_filters( 'update_welcome_subject', sprintf(__('New %1$s Site: %2$s'), $current_site->site_name, stripslashes( $title ) ) );
  1153. wp_mail($user->user_email, $subject, $message, $message_headers);
  1154. return true;
  1155. }
  1156. /**
  1157. * Notify a user that her account activation has been successful.
  1158. *
  1159. * Filter 'wpmu_welcome_user_notification' to disable or bypass.
  1160. *
  1161. * Filter 'update_welcome_user_email' and 'update_welcome_user_subject' to
  1162. * modify the content and subject line of the notification email.
  1163. *
  1164. * @since MU
  1165. *
  1166. * @param int $user_id
  1167. * @param string $password
  1168. * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
  1169. * @return bool
  1170. */
  1171. function wpmu_welcome_user_notification($user_id, $password, $meta = '') {
  1172. global $current_site;
  1173. if ( !apply_filters('wpmu_welcome_user_notification', $user_id, $password, $meta) )
  1174. return false;
  1175. $welcome_email = get_site_option( 'welcome_user_email' );
  1176. $user = new WP_User($user_id);
  1177. $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta);
  1178. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  1179. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1180. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1181. $welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
  1182. $admin_email = get_site_option( 'admin_email' );
  1183. if ( $admin_email == '' )
  1184. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1185. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1186. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1187. $message = $welcome_email;
  1188. if ( empty( $current_site->site_name ) )
  1189. $current_site->site_name = 'WordPress';
  1190. $subject = apply_filters( 'update_welcome_user_subject', sprintf(__('New %1$s User: %2$s'), $current_site->site_name, $user->user_login) );
  1191. wp_mail($user->user_email, $subject, $message, $message_headers);
  1192. return true;
  1193. }
  1194. /**
  1195. * Get the current site info.
  1196. *
  1197. * Returns an object containing the ID, domain, path, and site_name
  1198. * of the site being viewed.
  1199. *
  1200. * @since MU
  1201. *
  1202. * @return object
  1203. */
  1204. function get_current_site() {
  1205. global $current_site;
  1206. return $current_site;
  1207. }
  1208. /**
  1209. * Get a numeric user ID from either an email address or a login.
  1210. *
  1211. * @since MU
  1212. * @uses is_email()
  1213. *
  1214. * @param string $string
  1215. * @return int
  1216. */
  1217. function get_user_id_from_string( $string ) {
  1218. $user_id = 0;
  1219. if ( is_email( $string ) ) {
  1220. $user = get_user_by('email', $string);
  1221. if ( $user )
  1222. $user_id = $user->ID;
  1223. } elseif ( is_numeric( $string ) ) {
  1224. $user_id = $string;
  1225. } else {
  1226. $user = get_user_by('login', $string);
  1227. if ( $user )
  1228. $user_id = $user->ID;
  1229. }
  1230. return $user_id;
  1231. }
  1232. /**
  1233. * Get a user's most recent post.
  1234. *
  1235. * Walks through each of a user's blogs to find the post with
  1236. * the most recent post_date_gmt.
  1237. *
  1238. * @since MU
  1239. * @uses get_blogs_of_user()
  1240. *
  1241. * @param int $user_id
  1242. * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
  1243. */
  1244. function get_most_recent_post_of_user( $user_id ) {
  1245. global $wpdb;
  1246. $user_blogs = get_blogs_of_user( (int) $user_id );
  1247. $most_recent_post = array();
  1248. // Walk through each blog and get the most recent post
  1249. // published by $user_id
  1250. foreach ( (array) $user_blogs as $blog ) {
  1251. $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);
  1252. // Make sure we found a post
  1253. if ( isset($recent_post['ID']) ) {
  1254. $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
  1255. // If this is the first post checked or if this post is
  1256. // newer than the current recent post, make it the new
  1257. // most recent post.
  1258. if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
  1259. $most_recent_post = array(
  1260. 'blog_id' => $blog->userblog_id,
  1261. 'post_id' => $recent_post['ID'],
  1262. 'post_date_gmt' => $recent_post['post_date_gmt'],
  1263. 'post_gmt_ts' => $post_gmt_ts
  1264. );
  1265. }
  1266. }
  1267. }
  1268. return $most_recent_post;
  1269. }
  1270. // Misc functions
  1271. /**
  1272. * Get the size of a directory.
  1273. *
  1274. * A helper function that is used primarily to check whether
  1275. * a blog has exceeded its allowed upload space.
  1276. *
  1277. * @since MU
  1278. * @uses recurse_dirsize()
  1279. *
  1280. * @param string $directory
  1281. * @return int
  1282. */
  1283. function get_dirsize( $directory ) {
  1284. $dirsize = get_transient( 'dirsize_cache' );
  1285. if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
  1286. return $dirsize[ $directory ][ 'size' ];
  1287. if ( false == is_array( $dirsize ) )
  1288. $dirsize = array();
  1289. $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
  1290. set_transient( 'dirsize_cache', $dirsize, 3600 );
  1291. return $dirsize[ $directory ][ 'size' ];
  1292. }
  1293. /**
  1294. * Get the size of a directory recursively.
  1295. *
  1296. * Used by get_dirsize() to get a directory's size when it contains
  1297. * other directories.
  1298. *
  1299. * @since MU
  1300. *
  1301. * @param string $directory
  1302. * @return int
  1303. */
  1304. function recurse_dirsize( $directory ) {
  1305. $size = 0;
  1306. if ( substr( $directory, -1 ) == '/' )
  1307. $directory = substr($directory,0,-1);
  1308. if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
  1309. return false;
  1310. if ($handle = opendir($directory)) {
  1311. while(($file = readdir($handle)) !== false) {
  1312. $path = $directory.'/'.$file;
  1313. if ($file != '.' && $file != '..') {
  1314. if (is_file($path)) {
  1315. $size += filesize($path);
  1316. } elseif (is_dir($path)) {
  1317. $handlesize = recurse_dirsize($path);
  1318. if ($handlesize > 0)
  1319. $size += $handlesize;
  1320. }
  1321. }
  1322. }
  1323. closedir($handle);
  1324. }
  1325. return $size;
  1326. }
  1327. /**
  1328. * Check whether a blog has used its allotted upload space.
  1329. *
  1330. * Used by get_dirsize() to get a directory's size when it contains
  1331. * other directories.
  1332. *
  1333. * @since MU
  1334. * @uses get_dirsize()
  1335. *
  1336. * @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true.
  1337. * @return int
  1338. */
  1339. function upload_is_user_over_quota( $echo = true ) {
  1340. if ( get_site_option( 'upload_space_check_disabled' ) )
  1341. return false;
  1342. $spaceAllowed = get_space_allowed();
  1343. if ( empty( $spaceAllowed ) || !is_numeric( $spaceAllowed ) )
  1344. $spaceAllowed = 10; // Default space allowed is 10 MB
  1345. $dirName = BLOGUPLOADDIR;
  1346. $size = get_dirsize($dirName) / 1024 / 1024;
  1347. if ( ($spaceAllowed-$size) < 0 ) {
  1348. if ( $echo )
  1349. _e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' ); // No space left
  1350. return true;
  1351. } else {
  1352. return false;
  1353. }
  1354. }
  1355. /**
  1356. * Check an array of MIME types against a whitelist.
  1357. *
  1358. * WordPress ships with a set of allowed upload filetypes,
  1359. * which is defined in wp-includes/functions.php in
  1360. * get_allowed_mime_types(). This function is used to filter
  1361. * that list against the filetype whitelist provided by Multisite
  1362. * Super Admins at wp-admin/network/settings.php.
  1363. *
  1364. * @since MU
  1365. *
  1366. * @param array $mimes
  1367. * @return array
  1368. */
  1369. function check_upload_mimes( $mimes ) {
  1370. $site_exts = explode( ' ', get_site_option( 'upload_filetypes' ) );
  1371. foreach ( $site_exts as $ext ) {
  1372. foreach ( $mimes as $ext_pattern => $mime ) {
  1373. if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
  1374. $site_mimes[$ext_pattern] = $mime;
  1375. }
  1376. }
  1377. return $site_mimes;
  1378. }
  1379. /**
  1380. * Update a blog's post count.
  1381. *
  1382. * WordPress MS stores a blog's post count as an option so as
  1383. * to avoid extraneous COUNTs when a blog's details are fetched
  1384. * with get_blog_details(). This function is called when posts
  1385. * are published to make sure the count stays current.
  1386. *
  1387. * @since MU
  1388. */
  1389. function update_posts_count( $deprecated = '' ) {
  1390. global $wpdb;
  1391. update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
  1392. }
  1393. /**
  1394. * Logs user registrations.
  1395. *
  1396. * @since MU
  1397. *
  1398. * @param int $blog_id
  1399. * @param int $user_id
  1400. */
  1401. function wpmu_log_new_registrations( $blog_id, $user_id ) {
  1402. global $wpdb;
  1403. $user = new WP_User( (int) $user_id );
  1404. $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')) );
  1405. }
  1406. /**
  1407. * Get the remaining upload space for this blog.
  1408. *
  1409. * @since MU
  1410. * @uses upload_is_user_over_quota()
  1411. * @uses get_space_allowed()
  1412. * @uses get_dirsize()
  1413. *
  1414. * @param int $size
  1415. * @return int
  1416. */
  1417. function fix_import_form_size( $size ) {
  1418. if ( upload_is_user_over_quota( false ) == true )
  1419. return 0;
  1420. $spaceAllowed = 1024 * 1024 * get_space_allowed();
  1421. $dirName = BLOGUPLOADDIR;
  1422. $dirsize = get_dirsize($dirName) ;
  1423. if ( $size > $spaceAllowed - $dirsize )
  1424. return $spaceAllowed - $dirsize; // remaining space
  1425. else
  1426. return $size; // default
  1427. }
  1428. /**
  1429. * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
  1430. *
  1431. * @since 3.0.0
  1432. *
  1433. * @see term_id_filter
  1434. *
  1435. * @param int $term_id An ID for a term on the current blog.
  1436. * @return int An ID from the global terms table mapped from $term_id.
  1437. */
  1438. function global_terms( $term_id, $deprecated = '' ) {
  1439. global $wpdb;
  1440. static $global_terms_recurse = null;
  1441. if ( !global_terms_enabled() )
  1442. return $term_id;
  1443. // prevent a race condition
  1444. $recurse_start = false;
  1445. if ( $global_terms_recurse === null ) {
  1446. $recurse_start = true;
  1447. $global_terms_recurse = 1;
  1448. } elseif ( 10 < $global_terms_recurse++ ) {
  1449. return $term_id;
  1450. }
  1451. $term_id = intval( $term_id );
  1452. $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
  1453. $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
  1454. if ( $global_id == null ) {
  1455. $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
  1456. if ( null == $used_global_id ) {
  1457. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1458. $global_id = $wpdb->insert_id;
  1459. if ( empty( $global_id ) )
  1460. return $term_id;
  1461. } else {
  1462. $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
  1463. $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
  1464. $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
  1465. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1466. $global_id = $wpdb->insert_id;
  1467. }
  1468. } elseif ( $global_id != $term_id ) {
  1469. $local_id = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
  1470. if ( null != $local_id )
  1471. $local_id = global_terms( $local_id );
  1472. if ( 10 < $global_terms_recurse )
  1473. $global_id = $term_id;
  1474. }
  1475. if ( $global_id != $term_id ) {
  1476. if ( get_option( 'default_category' ) == $term_id )
  1477. update_option( 'default_category', $global_id );
  1478. $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
  1479. $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
  1480. $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
  1481. clean_term_cache($term_id);
  1482. }
  1483. if( $recurse_start )
  1484. $global_terms_recurse = null;
  1485. return $global_id;
  1486. }
  1487. /**
  1488. * Ensure that the current site's domain is listed in the allowed redirect host list.
  1489. *
  1490. * @see wp_validate_redirect()
  1491. * @since MU
  1492. *
  1493. * @return array The current site's domain
  1494. */
  1495. function redirect_this_site( $deprecated = '' ) {
  1496. global $current_site;
  1497. return array( $current_site->domain );
  1498. }
  1499. /**
  1500. * Check whether an upload is too big.
  1501. *
  1502. * @since MU
  1503. *
  1504. * @param array $upload
  1505. * @return mixed If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
  1506. */
  1507. function upload_is_file_too_big( $upload ) {
  1508. if ( is_array( $upload ) == false || defined( 'WP_IMPORTING' ) )
  1509. return $upload;
  1510. if ( strlen( $upload['bits'] ) > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
  1511. return sprintf( __( 'This file is too big. Files must be less than %d KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ));
  1512. return $upload;
  1513. }
  1514. /**
  1515. * Add a nonce field to the signup page.
  1516. *
  1517. * @since MU
  1518. * @uses wp_nonce_field()
  1519. */
  1520. function signup_nonce_fields() {
  1521. $id = mt_rand();
  1522. echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
  1523. wp_nonce_field('signup_form_' . $id, '_signup_form', false);
  1524. }
  1525. /**
  1526. * Process the signup nonce created in signup_nonce_fields().
  1527. *
  1528. * @since MU
  1529. * @uses wp_create_nonce()
  1530. *
  1531. * @param array $result
  1532. * @return array
  1533. */
  1534. function signup_nonce_check( $result ) {
  1535. if ( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
  1536. return $result;
  1537. if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
  1538. wp_die( __('Please try again!') );
  1539. return $result;
  1540. }
  1541. /**
  1542. * Correct 404 redirects when NOBLOGREDIRECT is defined.
  1543. *
  1544. * @since MU
  1545. */
  1546. function maybe_redirect_404() {
  1547. global $current_site;
  1548. if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
  1549. if ( $destination == '%siteurl%' )
  1550. $destination = network_home_url();
  1551. wp_redirect( $destination );
  1552. exit();
  1553. }
  1554. }
  1555. /**
  1556. * Add a new user to a blog by visiting /newbloguser/username/.
  1557. *
  1558. * This will only work when the user's details are saved as an option
  1559. * keyed as 'new_user_x', where 'x' is the username of the user to be
  1560. * added, as when a user is invited through the regular WP Add User interface.
  1561. *
  1562. * @since MU
  1563. * @uses add_existing_user_to_blog()
  1564. */
  1565. function maybe_add_existing_user_to_blog() {
  1566. if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) )
  1567. return false;
  1568. $parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
  1569. $key = array_pop( $parts );
  1570. if ( $key == '' )
  1571. $key = array_pop( $parts );
  1572. $details = get_option( 'new_user_' . $key );
  1573. if ( !empty( $details ) )
  1574. delete_option( 'new_user_' . $key );
  1575. if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) )
  1576. wp_die( sprintf(__('An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.'), site_url() ) );
  1577. 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') );
  1578. }
  1579. /**
  1580. * Add a user to a blog based on details from maybe_add_existing_user_to_blog().
  1581. *
  1582. * @since MU
  1583. * @uses add_user_to_blog()
  1584. *
  1585. * @param array $details
  1586. */
  1587. function add_existing_user_to_blog( $details = false ) {
  1588. global $blog_id;
  1589. if ( is_array( $details ) ) {
  1590. $result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] );
  1591. do_action( 'added_existing_user', $details[ 'user_id' ], $result );
  1592. }
  1593. return $result;
  1594. }
  1595. /**
  1596. * Add a newly created user to the appropriate blog
  1597. *
  1598. * @since MU
  1599. *
  1600. * @param int $user_id
  1601. * @param string $email
  1602. * @param array $meta
  1603. */
  1604. function add_new_user_to_blog( $user_id, $email, $meta ) {
  1605. global $current_site;
  1606. if ( !empty( $meta[ 'add_to_blog' ] ) ) {
  1607. $blog_id = $meta[ 'add_to_blog' ];
  1608. $role = $meta[ 'new_role' ];
  1609. remove_user_from_blog($user_id, $current_site->blog_id); // remove user from main blog.
  1610. add_user_to_blog( $blog_id, $user_id, $role );
  1611. update_user_meta( $user_id, 'primary_blog', $blog_id );
  1612. }
  1613. }
  1614. /**
  1615. * Correct From host on outgoing mail to match the site domain
  1616. *
  1617. * @since MU
  1618. */
  1619. function fix_phpmailer_messageid( $phpmailer ) {
  1620. global $current_site;
  1621. $phpmailer->Hostname = $current_site->domain;
  1622. }
  1623. /**
  1624. * Check to see whether a user is marked as a spammer, based on username
  1625. *
  1626. * @since MU
  1627. * @uses get_current_user_id()
  1628. * @uses get_user_id_from_string()
  1629. *
  1630. * @param string $username
  1631. * @return bool
  1632. */
  1633. function is_user_spammy( $username = 0 ) {
  1634. if ( $username == 0 ) {
  1635. $user_id = get_current_user_id();
  1636. } else {
  1637. $user_id = get_user_id_from_string( $username );
  1638. }
  1639. $u = new WP_User( $user_id );
  1640. return ( isset( $u->spam ) && $u->spam == 1 );
  1641. }
  1642. /**
  1643. * Update this blog's 'public' setting in the global blogs table.
  1644. *
  1645. * Public blogs have a setting of 1, private blogs are 0.
  1646. *
  1647. * @since MU
  1648. * @uses update_blog_status()
  1649. *
  1650. * @param int $old_value
  1651. * @param int $value The new public value
  1652. * @return bool
  1653. */
  1654. function update_blog_public( $old_value, $value ) {
  1655. global $wpdb;
  1656. do_action('update_blog_public');
  1657. update_blog_status( $wpdb->blogid, 'public', (int) $value );
  1658. }
  1659. add_action('update_option_blog_public', 'update_blog_public', 10, 2);
  1660. /**
  1661. * Get the "dashboard blog", the blog where users without a blog edit their profile data.
  1662. *
  1663. * @since MU
  1664. * @uses get_blog_details()
  1665. *
  1666. * @return int
  1667. */
  1668. function get_dashboard_blog() {
  1669. if ( $blog = get_site_option( 'dashboard_blog' ) )
  1670. return get_blog_details( $blog );
  1671. return get_blog_details( $GLOBALS['current_site']->blog_id );
  1672. }
  1673. /**
  1674. * Check whether a usermeta key has to do with the current blog.
  1675. *
  1676. * @since MU
  1677. * @uses wp_get_current_user()
  1678. *
  1679. * @param string $key
  1680. * @param int $user_id Optional. Defaults to current user.
  1681. * @param int $blog_id Optional. Defaults to current blog.
  1682. * @return bool
  1683. */
  1684. function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {
  1685. global $wpdb;
  1686. $current_user = wp_get_current_user();
  1687. if ( $user_id == 0 )
  1688. $user_id = $current_user->ID;
  1689. if ( $blog_id == 0 )
  1690. $blog_id = $wpdb->blogid;
  1691. $local_key = $wpdb->base_prefix . $blog_id . '_' . $key;
  1692. if ( isset( $current_user->$local_key ) )
  1693. return true;
  1694. return false;
  1695. }
  1696. /**
  1697. * Check whether users can self-register, based on Network settings.
  1698. *
  1699. * @since MU
  1700. *
  1701. * @return bool
  1702. */
  1703. function users_can_register_signup_filter() {
  1704. $registration = get_site_option('registration');
  1705. if ( $registration == 'all' || $registration == 'user' )
  1706. return true;
  1707. return false;
  1708. }
  1709. add_filter('option_users_can_register', 'users_can_register_signup_filter');
  1710. /**
  1711. * Ensure that the welcome message is not empty. Currently unused.
  1712. *
  1713. * @since MU
  1714. *
  1715. * @param string $text
  1716. * @return string
  1717. */
  1718. function welcome_user_msg_filter( $text ) {
  1719. if ( !$text ) {
  1720. return __( 'Dear User,
  1721. Your new account is set up.
  1722. You can log in with the following information:
  1723. Username: USERNAME
  1724. Password: PASSWORD
  1725. LOGINLINK
  1726. Thanks!
  1727. --The Team @ SITE_NAME' );
  1728. }
  1729. return $text;
  1730. }
  1731. add_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
  1732. /**
  1733. * Whether to force SSL on content.
  1734. *
  1735. * @since 2.8.5
  1736. *
  1737. * @param string|bool $force
  1738. * @return bool True if forced, false if not forced.
  1739. */
  1740. function force_ssl_content( $force = '' ) {
  1741. static $forced_content;
  1742. if ( '' != $force ) {
  1743. $old_forced = $forced_content;
  1744. $forced_content = $force;
  1745. return $old_forced;
  1746. }
  1747. return $forced_content;
  1748. }
  1749. /**
  1750. * Formats an String URL to use HTTPS if HTTP is found.
  1751. * Useful as a filter.
  1752. *
  1753. * @since 2.8.5
  1754. **/
  1755. function filter_SSL( $url ) {
  1756. if ( !is_string( $url ) )
  1757. return get_bloginfo( 'url' ); //return home blog url with proper scheme
  1758. $arrURL = parse_url( $url );
  1759. if ( force_ssl_content() && is_ssl() ) {
  1760. if ( 'http' === $arrURL['scheme'] && 'https' !== $arrURL['scheme'] )
  1761. $url = str_replace( $arrURL['scheme'], 'https', $url );
  1762. }
  1763. return $url;
  1764. }
  1765. /**
  1766. * Schedule update of the network-wide counts for the current network.
  1767. *
  1768. * @since 3.1.0
  1769. */
  1770. function wp_schedule_update_network_counts() {
  1771. if ( !is_main_site() )
  1772. return;
  1773. if ( !wp_next_scheduled('update_network_counts') && !defined('WP_INSTALLING') )
  1774. wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
  1775. }
  1776. /**
  1777. * Update the network-wide counts for the current network.
  1778. *
  1779. * @since 3.1.0
  1780. */
  1781. function wp_update_network_counts() {
  1782. global $wpdb;
  1783. $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) );
  1784. update_site_option( 'blog_count', $count );
  1785. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'") );
  1786. update_site_option( 'user_count', $count );
  1787. }
  1788. ?>