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

/wp-includes/ms-functions.php

https://bitbucket.org/abnopanda/wordpress
PHP | 2001 lines | 970 code | 280 blank | 751 comment | 274 complexity | b06f7a24500205c5e5a3adbad54208f9 MD5 | raw file
  1. <?php
  2. /**
  3. * Multisite WordPress API
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 3.0.0
  8. */
  9. /**
  10. * Gets the network's site and user counts.
  11. *
  12. * @since MU 1.0
  13. * @uses get_blog_count()
  14. * @uses get_user_count()
  15. *
  16. * @return array Site and user count for the network.
  17. */
  18. function get_sitestats() {
  19. global $wpdb;
  20. $stats = array(
  21. 'blogs' => get_blog_count(),
  22. 'users' => get_user_count(),
  23. );
  24. return $stats;
  25. }
  26. /**
  27. * Get the admin for a domain/path combination.
  28. *
  29. * @since MU 1.0
  30. *
  31. * @param string $sitedomain Optional. Site domain.
  32. * @param string $path Optional. Site path.
  33. * @return array The network admins
  34. */
  35. function get_admin_users_for_domain( $sitedomain = '', $path = '' ) {
  36. global $wpdb;
  37. if ( ! $sitedomain )
  38. $site_id = $wpdb->siteid;
  39. else
  40. $site_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path ) );
  41. if ( $site_id )
  42. return $wpdb->get_results( $wpdb->prepare( "SELECT u.ID, u.user_login, u.user_pass FROM $wpdb->users AS u, $wpdb->sitemeta AS sm WHERE sm.meta_key = 'admin_user_id' AND u.ID = sm.meta_value AND sm.site_id = %d", $site_id ), ARRAY_A );
  43. return false;
  44. }
  45. /**
  46. * Get one of a user's active blogs
  47. *
  48. * Returns the user's primary blog, if she has one and
  49. * it is active. If it's inactive, function returns another
  50. * active blog of the user. If none are found, the user
  51. * is added as a Subscriber to the Dashboard Blog and that blog
  52. * is returned.
  53. *
  54. * @since MU 1.0
  55. * @uses get_blogs_of_user()
  56. * @uses add_user_to_blog()
  57. * @uses get_blog_details()
  58. *
  59. * @param int $user_id The unique ID of the user
  60. * @return object The blog object
  61. */
  62. function get_active_blog_for_user( $user_id ) {
  63. global $wpdb;
  64. $blogs = get_blogs_of_user( $user_id );
  65. if ( empty( $blogs ) )
  66. return null;
  67. if ( !is_multisite() )
  68. return $blogs[$wpdb->blogid];
  69. $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
  70. $first_blog = current($blogs);
  71. if ( false !== $primary_blog ) {
  72. if ( ! isset( $blogs[ $primary_blog ] ) ) {
  73. update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  74. $primary = get_blog_details( $first_blog->userblog_id );
  75. } else {
  76. $primary = get_blog_details( $primary_blog );
  77. }
  78. } else {
  79. //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
  80. add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );
  81. update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  82. $primary = $first_blog;
  83. }
  84. if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
  85. $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
  86. $ret = false;
  87. if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
  88. foreach ( (array) $blogs as $blog_id => $blog ) {
  89. if ( $blog->site_id != $wpdb->siteid )
  90. continue;
  91. $details = get_blog_details( $blog_id );
  92. if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
  93. $ret = $blog;
  94. if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id )
  95. update_user_meta( $user_id, 'primary_blog', $blog_id );
  96. if ( !get_user_meta($user_id , 'source_domain', true) )
  97. update_user_meta( $user_id, 'source_domain', $blog->domain );
  98. break;
  99. }
  100. }
  101. } else {
  102. return null;
  103. }
  104. return $ret;
  105. } else {
  106. return $primary;
  107. }
  108. }
  109. /**
  110. * The number of active users in your installation.
  111. *
  112. * The count is cached and updated twice daily. This is not a live count.
  113. *
  114. * @since MU 2.7
  115. *
  116. * @return int
  117. */
  118. function get_user_count() {
  119. return get_site_option( 'user_count' );
  120. }
  121. /**
  122. * The number of active sites on your installation.
  123. *
  124. * The count is cached and updated twice daily. This is not a live count.
  125. *
  126. * @since MU 1.0
  127. *
  128. * @param int $id Optional. A site_id.
  129. * @return int
  130. */
  131. function get_blog_count( $id = 0 ) {
  132. return get_site_option( 'blog_count' );
  133. }
  134. /**
  135. * Get a blog post from any site on the network.
  136. *
  137. * @since MU 1.0
  138. *
  139. * @param int $blog_id ID of the blog.
  140. * @param int $post_id ID of the post you're looking for.
  141. * @return WP_Post|null WP_Post on success or null on failure
  142. */
  143. function get_blog_post( $blog_id, $post_id ) {
  144. switch_to_blog( $blog_id );
  145. $post = get_post( $post_id );
  146. restore_current_blog();
  147. return $post;
  148. }
  149. /**
  150. * Add a user to a blog.
  151. *
  152. * Use the 'add_user_to_blog' action to fire an event when
  153. * users are added to a blog.
  154. *
  155. * @since MU 1.0
  156. *
  157. * @param int $blog_id ID of the blog you're adding the user to.
  158. * @param int $user_id ID of the user you're adding.
  159. * @param string $role The role you want the user to have
  160. * @return bool
  161. */
  162. function add_user_to_blog( $blog_id, $user_id, $role ) {
  163. switch_to_blog($blog_id);
  164. $user = get_userdata( $user_id );
  165. if ( ! $user ) {
  166. restore_current_blog();
  167. return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
  168. }
  169. if ( !get_user_meta($user_id, 'primary_blog', true) ) {
  170. update_user_meta($user_id, 'primary_blog', $blog_id);
  171. $details = get_blog_details($blog_id);
  172. update_user_meta($user_id, 'source_domain', $details->domain);
  173. }
  174. $user->set_role($role);
  175. do_action('add_user_to_blog', $user_id, $role, $blog_id);
  176. wp_cache_delete( $user_id, 'users' );
  177. restore_current_blog();
  178. return true;
  179. }
  180. /**
  181. * Remove a user from a blog.
  182. *
  183. * Use the 'remove_user_from_blog' action to fire an event when
  184. * users are removed from a blog.
  185. *
  186. * Accepts an optional $reassign parameter, if you want to
  187. * reassign the user's blog posts to another user upon removal.
  188. *
  189. * @since MU 1.0
  190. *
  191. * @param int $user_id ID of the user you're removing.
  192. * @param int $blog_id ID of the blog you're removing the user from.
  193. * @param string $reassign Optional. A user to whom to reassign posts.
  194. * @return bool
  195. */
  196. function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
  197. global $wpdb;
  198. switch_to_blog($blog_id);
  199. $user_id = (int) $user_id;
  200. do_action('remove_user_from_blog', $user_id, $blog_id);
  201. // If being removed from the primary blog, set a new primary if the user is assigned
  202. // to multiple blogs.
  203. $primary_blog = get_user_meta($user_id, 'primary_blog', true);
  204. if ( $primary_blog == $blog_id ) {
  205. $new_id = '';
  206. $new_domain = '';
  207. $blogs = get_blogs_of_user($user_id);
  208. foreach ( (array) $blogs as $blog ) {
  209. if ( $blog->userblog_id == $blog_id )
  210. continue;
  211. $new_id = $blog->userblog_id;
  212. $new_domain = $blog->domain;
  213. break;
  214. }
  215. update_user_meta($user_id, 'primary_blog', $new_id);
  216. update_user_meta($user_id, 'source_domain', $new_domain);
  217. }
  218. // wp_revoke_user($user_id);
  219. $user = get_userdata( $user_id );
  220. if ( ! $user ) {
  221. restore_current_blog();
  222. return new WP_Error('user_does_not_exist', __('That user does not exist.'));
  223. }
  224. $user->remove_all_caps();
  225. $blogs = get_blogs_of_user($user_id);
  226. if ( count($blogs) == 0 ) {
  227. update_user_meta($user_id, 'primary_blog', '');
  228. update_user_meta($user_id, 'source_domain', '');
  229. }
  230. if ( $reassign != '' ) {
  231. $reassign = (int) $reassign;
  232. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id) );
  233. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id) );
  234. }
  235. restore_current_blog();
  236. return true;
  237. }
  238. /**
  239. * Create an empty blog.
  240. *
  241. * @since MU 1.0
  242. * @uses install_blog()
  243. *
  244. * @param string $domain The new blog's domain.
  245. * @param string $path The new blog's path.
  246. * @param string $weblog_title The new blog's title.
  247. * @param int $site_id Optional. Defaults to 1.
  248. * @return int The ID of the newly created blog
  249. */
  250. function create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 ) {
  251. $domain = addslashes( $domain );
  252. $weblog_title = addslashes( $weblog_title );
  253. if ( empty($path) )
  254. $path = '/';
  255. // Check if the domain has been used already. We should return an error message.
  256. if ( domain_exists($domain, $path, $site_id) )
  257. return __( '<strong>ERROR</strong>: Site URL already taken.' );
  258. // Need to back up wpdb table names, and create a new wp_blogs entry for new blog.
  259. // Need to get blog_id from wp_blogs, and create new table names.
  260. // Must restore table names at the end of function.
  261. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  262. return __( '<strong>ERROR</strong>: problem creating site entry.' );
  263. switch_to_blog($blog_id);
  264. install_blog($blog_id);
  265. restore_current_blog();
  266. return $blog_id;
  267. }
  268. /**
  269. * Get the permalink for a post on another blog.
  270. *
  271. * @since MU 1.0
  272. *
  273. * @param int $blog_id ID of the source blog.
  274. * @param int $post_id ID of the desired post.
  275. * @return string The post's permalink
  276. */
  277. function get_blog_permalink( $blog_id, $post_id ) {
  278. switch_to_blog( $blog_id );
  279. $link = get_permalink( $post_id );
  280. restore_current_blog();
  281. return $link;
  282. }
  283. /**
  284. * Get a blog's numeric ID from its URL.
  285. *
  286. * On a subdirectory installation like example.com/blog1/,
  287. * $domain will be the root 'example.com' and $path the
  288. * subdirectory '/blog1/'. With subdomains like blog1.example.com,
  289. * $domain is 'blog1.example.com' and $path is '/'.
  290. *
  291. * @since MU 2.6.5
  292. *
  293. * @param string $domain
  294. * @param string $path Optional. Not required for subdomain installations.
  295. * @return int 0 if no blog found, otherwise the ID of the matching blog
  296. */
  297. function get_blog_id_from_url( $domain, $path = '/' ) {
  298. global $wpdb;
  299. $domain = strtolower( $domain );
  300. $path = strtolower( $path );
  301. $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
  302. if ( $id == -1 ) // blog does not exist
  303. return 0;
  304. elseif ( $id )
  305. return (int) $id;
  306. $id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s and path = %s /* get_blog_id_from_url */", $domain, $path ) );
  307. if ( ! $id ) {
  308. wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
  309. return 0;
  310. }
  311. wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
  312. return $id;
  313. }
  314. // Admin functions
  315. /**
  316. * Checks an email address against a list of banned domains.
  317. *
  318. * This function checks against the Banned Email Domains list
  319. * at wp-admin/network/settings.php. The check is only run on
  320. * self-registrations; user creation at wp-admin/network/users.php
  321. * bypasses this check.
  322. *
  323. * @since MU
  324. *
  325. * @param string $user_email The email provided by the user at registration.
  326. * @return bool Returns true when the email address is banned.
  327. */
  328. function is_email_address_unsafe( $user_email ) {
  329. $banned_names = get_site_option( 'banned_email_domains' );
  330. if ( $banned_names && ! is_array( $banned_names ) )
  331. $banned_names = explode( "\n", $banned_names );
  332. $is_email_address_unsafe = false;
  333. if ( $banned_names && is_array( $banned_names ) ) {
  334. list( $email_local_part, $email_domain ) = explode( '@', $user_email );
  335. foreach ( $banned_names as $banned_domain ) {
  336. if ( ! $banned_domain )
  337. continue;
  338. if ( $email_domain == $banned_domain ) {
  339. $is_email_address_unsafe = true;
  340. break;
  341. }
  342. $dotted_domain = ".$banned_domain";
  343. if ( $dotted_domain === substr( $user_email, -strlen( $dotted_domain ) ) ) {
  344. $is_email_address_unsafe = true;
  345. break;
  346. }
  347. }
  348. }
  349. return apply_filters( 'is_email_address_unsafe', $is_email_address_unsafe, $user_email );
  350. }
  351. /**
  352. * Processes new user registrations.
  353. *
  354. * Checks the data provided by the user during signup. Verifies
  355. * the validity and uniqueness of user names and user email addresses,
  356. * and checks email addresses against admin-provided domain
  357. * whitelists and blacklists.
  358. *
  359. * The hook 'wpmu_validate_user_signup' provides an easy way
  360. * to modify the signup process. The value $result, which is passed
  361. * to the hook, contains both the user-provided info and the error
  362. * messages created by the function. 'wpmu_validate_user_signup' allows
  363. * you to process the data in any way you'd like, and unset the
  364. * relevant errors if necessary.
  365. *
  366. * @since MU
  367. * @uses is_email_address_unsafe()
  368. * @uses username_exists()
  369. * @uses email_exists()
  370. *
  371. * @param string $user_name The login name provided by the user.
  372. * @param string $user_email The email provided by the user.
  373. * @return array Contains username, email, and error messages.
  374. */
  375. function wpmu_validate_user_signup($user_name, $user_email) {
  376. global $wpdb;
  377. $errors = new WP_Error();
  378. $orig_username = $user_name;
  379. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  380. if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) {
  381. $errors->add( 'user_name', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
  382. $user_name = $orig_username;
  383. }
  384. $user_email = sanitize_email( $user_email );
  385. if ( empty( $user_name ) )
  386. $errors->add('user_name', __( 'Please enter a username.' ) );
  387. $illegal_names = get_site_option( 'illegal_names' );
  388. if ( is_array( $illegal_names ) == false ) {
  389. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  390. add_site_option( 'illegal_names', $illegal_names );
  391. }
  392. if ( in_array( $user_name, $illegal_names ) == true )
  393. $errors->add('user_name', __( 'That username is not allowed.' ) );
  394. if ( is_email_address_unsafe( $user_email ) )
  395. $errors->add('user_email', __('You cannot use that email address to signup. We are having problems with them blocking some of our email. Please use another email provider.'));
  396. if ( strlen( $user_name ) < 4 )
  397. $errors->add('user_name', __( 'Username must be at least 4 characters.' ) );
  398. if ( strpos( ' ' . $user_name, '_' ) != false )
  399. $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character &#8220;_&#8221;!' ) );
  400. // all numeric?
  401. $match = array();
  402. preg_match( '/[0-9]*/', $user_name, $match );
  403. if ( $match[0] == $user_name )
  404. $errors->add('user_name', __('Sorry, usernames must have letters too!'));
  405. if ( !is_email( $user_email ) )
  406. $errors->add('user_email', __( 'Please enter a valid email address.' ) );
  407. $limited_email_domains = get_site_option( 'limited_email_domains' );
  408. if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
  409. $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
  410. if ( in_array( $emaildomain, $limited_email_domains ) == false )
  411. $errors->add('user_email', __('Sorry, that email address is not allowed!'));
  412. }
  413. // Check if the username has been used already.
  414. if ( username_exists($user_name) )
  415. $errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
  416. // Check if the email address has been used already.
  417. if ( email_exists($user_email) )
  418. $errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );
  419. // Has someone already signed up for this username?
  420. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  421. if ( $signup != null ) {
  422. $registered_at = mysql2date('U', $signup->registered);
  423. $now = current_time( 'timestamp', true );
  424. $diff = $now - $registered_at;
  425. // If registered more than two days ago, cancel registration and let this signup go through.
  426. if ( $diff > 2 * DAY_IN_SECONDS )
  427. $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
  428. else
  429. $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
  430. if ( $signup->active == 0 && $signup->user_email == $user_email )
  431. $errors->add('user_email_used', __('username and email used'));
  432. }
  433. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  434. if ( $signup != null ) {
  435. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  436. // If registered more than two days ago, cancel registration and let this signup go through.
  437. if ( $diff > 2 * DAY_IN_SECONDS )
  438. $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
  439. else
  440. $errors->add('user_email', __('That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.'));
  441. }
  442. $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
  443. return apply_filters('wpmu_validate_user_signup', $result);
  444. }
  445. /**
  446. * Processes new site registrations.
  447. *
  448. * Checks the data provided by the user during blog signup. Verifies
  449. * the validity and uniqueness of blog paths and domains.
  450. *
  451. * This function prevents the current user from registering a new site
  452. * with a blogname equivalent to another user's login name. Passing the
  453. * $user parameter to the function, where $user is the other user, is
  454. * effectively an override of this limitation.
  455. *
  456. * Filter 'wpmu_validate_blog_signup' if you want to modify
  457. * the way that WordPress validates new site signups.
  458. *
  459. * @since MU
  460. * @uses domain_exists()
  461. * @uses username_exists()
  462. *
  463. * @param string $blogname The blog name provided by the user. Must be unique.
  464. * @param string $blog_title The blog title provided by the user.
  465. * @return array Contains the new site data and error messages.
  466. */
  467. function wpmu_validate_blog_signup($blogname, $blog_title, $user = '') {
  468. global $wpdb, $domain, $base, $current_site;
  469. $blog_title = strip_tags( $blog_title );
  470. $blog_title = substr( $blog_title, 0, 50 );
  471. $errors = new WP_Error();
  472. $illegal_names = get_site_option( 'illegal_names' );
  473. if ( $illegal_names == false ) {
  474. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  475. add_site_option( 'illegal_names', $illegal_names );
  476. }
  477. // On sub dir installs, Some names are so illegal, only a filter can spring them from jail
  478. if (! is_subdomain_install() )
  479. $illegal_names = array_merge($illegal_names, apply_filters( 'subdirectory_reserved_names', array( 'page', 'comments', 'blog', 'files', 'feed' ) ) );
  480. if ( empty( $blogname ) )
  481. $errors->add('blogname', __( 'Please enter a site name.' ) );
  482. if ( preg_match( '/[^a-z0-9]+/', $blogname ) )
  483. $errors->add('blogname', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
  484. if ( in_array( $blogname, $illegal_names ) == true )
  485. $errors->add('blogname', __( 'That name is not allowed.' ) );
  486. if ( strlen( $blogname ) < 4 && !is_super_admin() )
  487. $errors->add('blogname', __( 'Site name must be at least 4 characters.' ) );
  488. if ( strpos( ' ' . $blogname, '_' ) != false )
  489. $errors->add( 'blogname', __( 'Sorry, site names may not contain the character &#8220;_&#8221;!' ) );
  490. // do not allow users to create a blog that conflicts with a page on the main blog.
  491. 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 ) ) )
  492. $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
  493. // all numeric?
  494. $match = array();
  495. preg_match( '/[0-9]*/', $blogname, $match );
  496. if ( $match[0] == $blogname )
  497. $errors->add('blogname', __('Sorry, site names must have letters too!'));
  498. $blogname = apply_filters( 'newblogname', $blogname );
  499. $blog_title = stripslashes( $blog_title );
  500. if ( empty( $blog_title ) )
  501. $errors->add('blog_title', __( 'Please enter a site title.' ) );
  502. // Check if the domain/path has been used already.
  503. if ( is_subdomain_install() ) {
  504. $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
  505. $path = $base;
  506. } else {
  507. $mydomain = "$domain";
  508. $path = $base.$blogname.'/';
  509. }
  510. if ( domain_exists($mydomain, $path, $current_site->id) )
  511. $errors->add( 'blogname', __( 'Sorry, that site already exists!' ) );
  512. if ( username_exists( $blogname ) ) {
  513. if ( is_object( $user ) == false || ( is_object($user) && ( $user->user_login != $blogname ) ) )
  514. $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
  515. }
  516. // Has someone already signed up for this domain?
  517. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
  518. if ( ! empty($signup) ) {
  519. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  520. // If registered more than two days ago, cancel registration and let this signup go through.
  521. if ( $diff > 2 * DAY_IN_SECONDS )
  522. $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) );
  523. else
  524. $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
  525. }
  526. $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'user' => $user, 'errors' => $errors);
  527. return apply_filters('wpmu_validate_blog_signup', $result);
  528. }
  529. /**
  530. * Record site signup information for future activation.
  531. *
  532. * @since MU
  533. * @uses wpmu_signup_blog_notification()
  534. *
  535. * @param string $domain The requested domain.
  536. * @param string $path The requested path.
  537. * @param string $title The requested site title.
  538. * @param string $user The user's requested login name.
  539. * @param string $user_email The user's email address.
  540. * @param array $meta By default, contains the requested privacy setting and lang_id.
  541. */
  542. function wpmu_signup_blog($domain, $path, $title, $user, $user_email, $meta = '') {
  543. global $wpdb;
  544. $key = substr( md5( time() . rand() . $domain ), 0, 16 );
  545. $meta = serialize($meta);
  546. $domain = $wpdb->escape($domain);
  547. $path = $wpdb->escape($path);
  548. $title = $wpdb->escape($title);
  549. $wpdb->insert( $wpdb->signups, array(
  550. 'domain' => $domain,
  551. 'path' => $path,
  552. 'title' => $title,
  553. 'user_login' => $user,
  554. 'user_email' => $user_email,
  555. 'registered' => current_time('mysql', true),
  556. 'activation_key' => $key,
  557. 'meta' => $meta
  558. ) );
  559. wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta);
  560. }
  561. /**
  562. * Record user signup information for future activation.
  563. *
  564. * This function is used when user registration is open but
  565. * new site registration is not.
  566. *
  567. * @since MU
  568. * @uses wpmu_signup_user_notification()
  569. *
  570. * @param string $user The user's requested login name.
  571. * @param string $user_email The user's email address.
  572. * @param array $meta By default, this is an empty array.
  573. */
  574. function wpmu_signup_user($user, $user_email, $meta = '') {
  575. global $wpdb;
  576. // Format data
  577. $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
  578. $user_email = sanitize_email( $user_email );
  579. $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
  580. $meta = serialize($meta);
  581. $wpdb->insert( $wpdb->signups, array(
  582. 'domain' => '',
  583. 'path' => '',
  584. 'title' => '',
  585. 'user_login' => $user,
  586. 'user_email' => $user_email,
  587. 'registered' => current_time('mysql', true),
  588. 'activation_key' => $key,
  589. 'meta' => $meta
  590. ) );
  591. wpmu_signup_user_notification($user, $user_email, $key, $meta);
  592. }
  593. /**
  594. * Notify user of signup success.
  595. *
  596. * This is the notification function used when site registration
  597. * is enabled.
  598. *
  599. * Filter 'wpmu_signup_blog_notification' to bypass this function or
  600. * replace it with your own notification behavior.
  601. *
  602. * Filter 'wpmu_signup_blog_notification_email' and
  603. * 'wpmu_signup_blog_notification_subject' to change the content
  604. * and subject line of the email sent to newly registered users.
  605. *
  606. * @since MU
  607. *
  608. * @param string $domain The new blog domain.
  609. * @param string $path The new blog path.
  610. * @param string $title The site title.
  611. * @param string $user The user's login name.
  612. * @param string $user_email The user's email address.
  613. * @param array $meta By default, contains the requested privacy setting and lang_id.
  614. * @param string $key The activation key created in wpmu_signup_blog()
  615. * @return bool
  616. */
  617. function wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta = '') {
  618. global $current_site;
  619. if ( !apply_filters('wpmu_signup_blog_notification', $domain, $path, $title, $user, $user_email, $key, $meta) )
  620. return false;
  621. // Send email with activation link.
  622. if ( !is_subdomain_install() || $current_site->id != 1 )
  623. $activate_url = network_site_url("wp-activate.php?key=$key");
  624. else
  625. $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
  626. $activate_url = esc_url($activate_url);
  627. $admin_email = get_site_option( 'admin_email' );
  628. if ( $admin_email == '' )
  629. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  630. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  631. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  632. $message = sprintf(
  633. apply_filters( 'wpmu_signup_blog_notification_email',
  634. __( "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" ),
  635. $domain, $path, $title, $user, $user_email, $key, $meta
  636. ),
  637. $activate_url,
  638. esc_url( "http://{$domain}{$path}" ),
  639. $key
  640. );
  641. // TODO: Don't hard code activation link.
  642. $subject = sprintf(
  643. apply_filters( 'wpmu_signup_blog_notification_subject',
  644. __( '[%1$s] Activate %2$s' ),
  645. $domain, $path, $title, $user, $user_email, $key, $meta
  646. ),
  647. $from_name,
  648. esc_url( 'http://' . $domain . $path )
  649. );
  650. wp_mail($user_email, $subject, $message, $message_headers);
  651. return true;
  652. }
  653. /**
  654. * Notify user of signup success.
  655. *
  656. * This is the notification function used when no new site has
  657. * been requested.
  658. *
  659. * Filter 'wpmu_signup_user_notification' to bypass this function or
  660. * replace it with your own notification behavior.
  661. *
  662. * Filter 'wpmu_signup_user_notification_email' and
  663. * 'wpmu_signup_user_notification_subject' to change the content
  664. * and subject line of the email sent to newly registered users.
  665. *
  666. * @since MU
  667. *
  668. * @param string $user The user's login name.
  669. * @param string $user_email The user's email address.
  670. * @param array $meta By default, an empty array.
  671. * @param string $key The activation key created in wpmu_signup_user()
  672. * @return bool
  673. */
  674. function wpmu_signup_user_notification($user, $user_email, $key, $meta = '') {
  675. if ( !apply_filters('wpmu_signup_user_notification', $user, $user_email, $key, $meta) )
  676. return false;
  677. // Send email with activation link.
  678. $admin_email = get_site_option( 'admin_email' );
  679. if ( $admin_email == '' )
  680. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  681. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  682. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  683. $message = sprintf(
  684. apply_filters( 'wpmu_signup_user_notification_email',
  685. __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ),
  686. $user, $user_email, $key, $meta
  687. ),
  688. site_url( "wp-activate.php?key=$key" )
  689. );
  690. // TODO: Don't hard code activation link.
  691. $subject = sprintf(
  692. apply_filters( 'wpmu_signup_user_notification_subject',
  693. __( '[%1$s] Activate %2$s' ),
  694. $user, $user_email, $key, $meta
  695. ),
  696. $from_name,
  697. $user
  698. );
  699. wp_mail($user_email, $subject, $message, $message_headers);
  700. return true;
  701. }
  702. /**
  703. * Activate a signup.
  704. *
  705. * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events
  706. * that should happen only when users or sites are self-created (since
  707. * those actions are not called when users and sites are created
  708. * by a Super Admin).
  709. *
  710. * @since MU
  711. * @uses wp_generate_password()
  712. * @uses wpmu_welcome_user_notification()
  713. * @uses add_user_to_blog()
  714. * @uses add_new_user_to_blog()
  715. * @uses wpmu_create_user()
  716. * @uses wpmu_create_blog()
  717. * @uses wpmu_welcome_notification()
  718. *
  719. * @param string $key The activation key provided to the user.
  720. * @return array An array containing information about the activated user and/or blog
  721. */
  722. function wpmu_activate_signup($key) {
  723. global $wpdb, $current_site;
  724. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
  725. if ( empty( $signup ) )
  726. return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
  727. if ( $signup->active ) {
  728. if ( empty( $signup->domain ) )
  729. return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
  730. else
  731. return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
  732. }
  733. $meta = maybe_unserialize($signup->meta);
  734. $user_login = $wpdb->escape($signup->user_login);
  735. $user_email = $wpdb->escape($signup->user_email);
  736. $password = wp_generate_password( 12, false );
  737. $user_id = username_exists($user_login);
  738. if ( ! $user_id )
  739. $user_id = wpmu_create_user($user_login, $password, $user_email);
  740. else
  741. $user_already_exists = true;
  742. if ( ! $user_id )
  743. return new WP_Error('create_user', __('Could not create user'), $signup);
  744. $now = current_time('mysql', true);
  745. if ( empty($signup->domain) ) {
  746. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  747. if ( isset( $user_already_exists ) )
  748. return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
  749. wpmu_welcome_user_notification($user_id, $password, $meta);
  750. add_new_user_to_blog( $user_id, $user_email, $meta );
  751. do_action('wpmu_activate_user', $user_id, $password, $meta);
  752. return array('user_id' => $user_id, 'password' => $password, 'meta' => $meta);
  753. }
  754. $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
  755. // TODO: What to do if we create a user but cannot create a blog?
  756. if ( is_wp_error($blog_id) ) {
  757. // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  758. // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  759. if ( 'blog_taken' == $blog_id->get_error_code() ) {
  760. $blog_id->add_data( $signup );
  761. $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
  762. }
  763. return $blog_id;
  764. }
  765. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  766. wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
  767. do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta);
  768. return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
  769. }
  770. /**
  771. * Create a user.
  772. *
  773. * This function runs when a user self-registers as well as when
  774. * a Super Admin creates a new user. Hook to 'wpmu_new_user' for events
  775. * that should affect all new users, but only on Multisite (otherwise
  776. * use 'user_register').
  777. *
  778. * @since MU
  779. * @uses wp_create_user()
  780. *
  781. * @param string $user_name The new user's login name.
  782. * @param string $password The new user's password.
  783. * @param string $email The new user's email address.
  784. * @return mixed Returns false on failure, or int $user_id on success
  785. */
  786. function wpmu_create_user( $user_name, $password, $email) {
  787. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  788. $user_id = wp_create_user( $user_name, $password, $email );
  789. if ( is_wp_error($user_id) )
  790. return false;
  791. // Newly created users have no roles or caps until they are added to a blog.
  792. delete_user_option( $user_id, 'capabilities' );
  793. delete_user_option( $user_id, 'user_level' );
  794. do_action( 'wpmu_new_user', $user_id );
  795. return $user_id;
  796. }
  797. /**
  798. * Create a site.
  799. *
  800. * This function runs when a user self-registers a new site as well
  801. * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
  802. * for events that should affect all new sites.
  803. *
  804. * On subdirectory installs, $domain is the same as the main site's
  805. * domain, and the path is the subdirectory name (eg 'example.com'
  806. * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
  807. * root domain (eg 'blog1.example.com'), and $path is '/'.
  808. *
  809. * @since MU
  810. * @uses domain_exists()
  811. * @uses insert_blog()
  812. * @uses wp_install_defaults()
  813. * @uses add_user_to_blog()
  814. *
  815. * @param string $domain The new site's domain.
  816. * @param string $path The new site's path.
  817. * @param string $title The new site's title.
  818. * @param int $user_id The user ID of the new site's admin.
  819. * @param array $meta Optional. Used to set initial site options.
  820. * @param int $site_id Optional. Only relevant on multi-network installs.
  821. * @return mixed Returns WP_Error object on failure, int $blog_id on success
  822. */
  823. function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) {
  824. $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
  825. if ( is_subdomain_install() )
  826. $domain = str_replace( '@', '', $domain );
  827. $title = strip_tags( $title );
  828. $user_id = (int) $user_id;
  829. if ( empty($path) )
  830. $path = '/';
  831. // Check if the domain has been used already. We should return an error message.
  832. if ( domain_exists($domain, $path, $site_id) )
  833. return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
  834. if ( !defined('WP_INSTALLING') )
  835. define( 'WP_INSTALLING', true );
  836. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  837. return new WP_Error('insert_blog', __('Could not create site.'));
  838. switch_to_blog($blog_id);
  839. install_blog($blog_id, $title);
  840. wp_install_defaults($user_id);
  841. add_user_to_blog($blog_id, $user_id, 'administrator');
  842. if ( is_array($meta) ) foreach ($meta as $key => $value) {
  843. if ( $key == 'public' || $key == 'archived' || $key == 'mature' || $key == 'spam' || $key == 'deleted' || $key == 'lang_id' )
  844. update_blog_status( $blog_id, $key, $value );
  845. else
  846. update_option( $key, $value );
  847. }
  848. add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
  849. update_option( 'blog_public', (int)$meta['public'] );
  850. if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) )
  851. update_user_meta( $user_id, 'primary_blog', $blog_id );
  852. restore_current_blog();
  853. do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
  854. return $blog_id;
  855. }
  856. /**
  857. * Notifies the network admin that a new site has been activated.
  858. *
  859. * Filter 'newblog_notify_siteadmin' to change the content of
  860. * the notification email.
  861. *
  862. * @since MU
  863. *
  864. * @param int $blog_id The new site's ID.
  865. * @return bool
  866. */
  867. function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  868. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  869. return false;
  870. $email = get_site_option( 'admin_email' );
  871. if ( is_email($email) == false )
  872. return false;
  873. $options_site_url = esc_url(network_admin_url('settings.php'));
  874. switch_to_blog( $blog_id );
  875. $blogname = get_option( 'blogname' );
  876. $siteurl = site_url();
  877. restore_current_blog();
  878. $msg = sprintf( __( 'New Site: %1$s
  879. URL: %2$s
  880. Remote IP: %3$s
  881. Disable these notifications: %4$s' ), $blogname, $siteurl, $_SERVER['REMOTE_ADDR'], $options_site_url);
  882. $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
  883. wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
  884. return true;
  885. }
  886. /**
  887. * Notifies the network admin that a new user has been activated.
  888. *
  889. * Filter 'newuser_notify_siteadmin' to change the content of
  890. * the notification email.
  891. *
  892. * @since MU
  893. * @uses apply_filters() Filter newuser_notify_siteadmin to change the content of the email message
  894. *
  895. * @param int $user_id The new user's ID.
  896. * @return bool
  897. */
  898. function newuser_notify_siteadmin( $user_id ) {
  899. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  900. return false;
  901. $email = get_site_option( 'admin_email' );
  902. if ( is_email($email) == false )
  903. return false;
  904. $user = get_userdata( $user_id );
  905. $options_site_url = esc_url(network_admin_url('settings.php'));
  906. $msg = sprintf(__('New User: %1$s
  907. Remote IP: %2$s
  908. Disable these notifications: %3$s'), $user->user_login, $_SERVER['REMOTE_ADDR'], $options_site_url);
  909. $msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
  910. wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
  911. return true;
  912. }
  913. /**
  914. * Check whether a blogname is already taken.
  915. *
  916. * Used during the new site registration process to ensure
  917. * that each blogname is unique.
  918. *
  919. * @since MU
  920. *
  921. * @param string $domain The domain to be checked.
  922. * @param string $path The path to be checked.
  923. * @param int $site_id Optional. Relevant only on multi-network installs.
  924. * @return int
  925. */
  926. function domain_exists($domain, $path, $site_id = 1) {
  927. global $wpdb;
  928. $result = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s AND site_id = %d", $domain, $path, $site_id) );
  929. return apply_filters( 'domain_exists', $result, $domain, $path, $site_id );
  930. }
  931. /**
  932. * Store basic site info in the blogs table.
  933. *
  934. * This function creates a row in the wp_blogs table and returns
  935. * the new blog's ID. It is the first step in creating a new blog.
  936. *
  937. * @since MU
  938. *
  939. * @param string $domain The domain of the new site.
  940. * @param string $path The path of the new site.
  941. * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1.
  942. * @return int The ID of the new row
  943. */
  944. function insert_blog($domain, $path, $site_id) {
  945. global $wpdb;
  946. $path = trailingslashit($path);
  947. $site_id = (int) $site_id;
  948. $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
  949. if ( ! $result )
  950. return false;
  951. refresh_blog_details($wpdb->insert_id);
  952. return $wpdb->insert_id;
  953. }
  954. /**
  955. * Install an empty blog.
  956. *
  957. * Creates the new blog tables and options. If calling this function
  958. * directly, be sure to use switch_to_blog() first, so that $wpdb
  959. * points to the new blog.
  960. *
  961. * @since MU
  962. * @uses make_db_current_silent()
  963. * @uses populate_roles()
  964. *
  965. * @param int $blog_id The value returned by insert_blog().
  966. * @param string $blog_title The title of the new site.
  967. */
  968. function install_blog($blog_id, $blog_title = '') {
  969. global $wpdb, $wp_roles, $current_site;
  970. // Cast for security
  971. $blog_id = (int) $blog_id;
  972. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  973. $wpdb->suppress_errors();
  974. if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) )
  975. 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>' );
  976. $wpdb->suppress_errors( false );
  977. $url = get_blogaddress_by_id( $blog_id );
  978. // Set everything up
  979. make_db_current_silent( 'blog' );
  980. populate_options();
  981. populate_roles();
  982. $wp_roles->_init();
  983. $url = untrailingslashit( $url );
  984. update_option( 'siteurl', $url );
  985. update_option( 'home', $url );
  986. if ( get_site_option( 'ms_files_rewriting' ) )
  987. update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
  988. else
  989. update_option( 'upload_path', get_blog_option( $current_site->blog_id, 'upload_path' ) );
  990. update_option( 'blogname', stripslashes( $blog_title ) );
  991. update_option( 'admin_email', '' );
  992. // remove all perms
  993. $table_prefix = $wpdb->get_blog_prefix();
  994. delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
  995. delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
  996. }
  997. /**
  998. * Set blog defaults.
  999. *
  1000. * This function creates a row in the wp_blogs table.
  1001. *
  1002. * @since MU
  1003. * @deprecated MU
  1004. * @deprecated Use wp_install_defaults()
  1005. * @uses wp_install_defaults()
  1006. *
  1007. * @param int $blog_id Ignored in this function.
  1008. * @param int $user_id
  1009. */
  1010. function install_blog_defaults($blog_id, $user_id) {
  1011. global $wpdb;
  1012. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1013. $wpdb->suppress_errors();
  1014. wp_install_defaults($user_id);
  1015. $wpdb->suppress_errors( false );
  1016. }
  1017. /**
  1018. * Notify a user that her blog activation has been successful.
  1019. *
  1020. * Filter 'wpmu_welcome_notification' to disable or bypass.
  1021. *
  1022. * Filter 'update_welcome_email' and 'update_welcome_subject' to
  1023. * modify the content and subject line of the notification email.
  1024. *
  1025. * @since MU
  1026. *
  1027. * @param int $blog_id
  1028. * @param int $user_id
  1029. * @param string $password
  1030. * @param string $title The new blog's title
  1031. * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
  1032. * @return bool
  1033. */
  1034. function wpmu_welcome_notification($blog_id, $user_id, $password, $title, $meta = '') {
  1035. global $current_site;
  1036. if ( !apply_filters('wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta) )
  1037. return false;
  1038. $welcome_email = stripslashes( get_site_option( 'welcome_email' ) );
  1039. if ( $welcome_email == false )
  1040. $welcome_email = stripslashes( __( 'Dear User,
  1041. Your new SITE_NAME site has been successfully set up at:
  1042. BLOG_URL
  1043. You can log in to the administrator account with the following information:
  1044. Username: USERNAME
  1045. Password: PASSWORD
  1046. Log in here: BLOG_URLwp-login.php
  1047. We hope you enjoy your new site. Thanks!
  1048. --The Team @ SITE_NAME' ) );
  1049. $url = get_blogaddress_by_id($blog_id);
  1050. $user = get_userdata( $user_id );
  1051. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  1052. $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
  1053. $welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
  1054. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1055. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1056. $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta);
  1057. $admin_email = get_site_option( 'admin_email' );
  1058. if ( $admin_email == '' )
  1059. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1060. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1061. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1062. $message = $welcome_email;
  1063. if ( empty( $current_site->site_name ) )
  1064. $current_site->site_name = 'WordPress';
  1065. $subject = apply_filters( 'update_welcome_subject', sprintf(__('New %1$s Site: %2$s'), $current_site->site_name, stripslashes( $title ) ) );
  1066. wp_mail($user->user_email, $subject, $message, $message_headers);
  1067. return true;
  1068. }
  1069. /**
  1070. * Notify a user that her account activation has been successful.
  1071. *
  1072. * Filter 'wpmu_welcome_user_notification' to disable or bypass.
  1073. *
  1074. * Filter 'update_welcome_user_email' and 'update_welcome_user_subject' to
  1075. * modify the content and subject line of the notification email.
  1076. *
  1077. * @since MU
  1078. *
  1079. * @param int $user_id
  1080. * @param string $password
  1081. * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
  1082. * @return bool
  1083. */
  1084. function wpmu_welcome_user_notification($user_id, $password, $meta = '') {
  1085. global $current_site;
  1086. if ( !apply_filters('wpmu_welcome_user_notification', $user_id, $password, $meta) )
  1087. return false;
  1088. $welcome_email = get_site_option( 'welcome_user_email' );
  1089. $user = get_userdata( $user_id );
  1090. $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta);
  1091. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  1092. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1093. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1094. $welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
  1095. $admin_email = get_site_option( 'admin_email' );
  1096. if ( $admin_email == '' )
  1097. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1098. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1099. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1100. $message = $welcome_email;
  1101. if ( empty( $current_site->site_name ) )
  1102. $current_site->site_name = 'WordPress';
  1103. $subject = apply_filters( 'update_welcome_user_subject', sprintf(__('New %1$s User: %2$s'), $current_site->site_name, $user->user_login) );
  1104. wp_mail($user->user_email, $subject, $message, $message_headers);
  1105. return true;
  1106. }
  1107. /**
  1108. * Get the current site info.
  1109. *
  1110. * Returns an object containing the ID, domain, path, and site_name
  1111. * of the site being viewed.
  1112. *
  1113. * @since MU
  1114. *
  1115. * @return object
  1116. */
  1117. function get_current_site() {
  1118. global $current_site;
  1119. return $current_site;
  1120. }
  1121. /**
  1122. * Get a numeric user ID from either an email address or a login.
  1123. *
  1124. * @since MU
  1125. * @uses is_email()
  1126. *
  1127. * @param string $string
  1128. * @return int
  1129. */
  1130. function get_user_id_from_string( $string ) {
  1131. $user_id = 0;
  1132. if ( is_email( $string ) ) {
  1133. $user = get_user_by('email', $string);
  1134. if ( $user )
  1135. $user_id = $user->ID;
  1136. } elseif ( is_numeric( $string ) ) {
  1137. $user_id = $string;
  1138. } else {
  1139. $user = get_user_by('login', $string);
  1140. if ( $user )
  1141. $user_id = $user->ID;
  1142. }
  1143. return $user_id;
  1144. }
  1145. /**
  1146. * Get a user's most recent post.
  1147. *
  1148. * Walks through each of a user's blogs to find the post with
  1149. * the most recent post_date_gmt.
  1150. *
  1151. * @since MU
  1152. * @uses get_blogs_of_user()
  1153. *
  1154. * @param int $user_id
  1155. * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
  1156. */
  1157. function get_most_recent_post_of_user( $user_id ) {
  1158. global $wpdb;
  1159. $user_blogs = get_blogs_of_user( (int) $user_id );
  1160. $most_recent_post = array();
  1161. // Walk through each blog and get the most recent post
  1162. // published by $user_id
  1163. foreach ( (array) $user_blogs as $blog ) {
  1164. $prefix = $wpdb->get_blog_prefix( $blog->userblog_id );
  1165. $recent_post = $wpdb->get_row( $wpdb->prepare("SELECT ID, post_date_gmt FROM {$prefix}posts WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1", $user_id ), ARRAY_A);
  1166. // Make sure we found a post
  1167. if ( isset($recent_post['ID']) ) {
  1168. $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
  1169. // If this is the first post checked or if this post is
  1170. // newer than the current recent post, make it the new
  1171. // most recent post.
  1172. if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
  1173. $most_recent_post = array(
  1174. 'blog_id' => $blog->userblog_id,
  1175. 'post_id' => $recent_post['ID'],
  1176. 'post_date_gmt' => $recent_post['post_date_gmt'],
  1177. 'post_gmt_ts' => $post_gmt_ts
  1178. );
  1179. }
  1180. }
  1181. }
  1182. return $most_recent_post;
  1183. }
  1184. // Misc functions
  1185. /**
  1186. * Get the size of a directory.
  1187. *
  1188. * A helper function that is used primarily to check whether
  1189. * a blog has exceeded its allowed upload space.
  1190. *
  1191. * @since MU
  1192. * @uses recurse_dirsize()
  1193. *
  1194. * @param string $directory
  1195. * @return int
  1196. */
  1197. function get_dirsize( $directory ) {
  1198. $dirsize = get_transient( 'dirsize_cache' );
  1199. if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
  1200. return $dirsize[ $directory ][ 'size' ];
  1201. if ( false == is_array( $dirsize ) )
  1202. $dirsize = array();
  1203. $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
  1204. set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
  1205. return $dirsize[ $directory ][ 'size' ];
  1206. }
  1207. /**
  1208. * Get the size of a directory recursively.
  1209. *
  1210. * Used by get_dirsize() to get a directory's size when it contains
  1211. * other directories.
  1212. *
  1213. * @since MU
  1214. *
  1215. * @param string $directory
  1216. * @return int
  1217. */
  1218. function recurse_dirsize( $directory ) {
  1219. $size = 0;
  1220. $directory = untrailingslashit( $directory );
  1221. if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
  1222. return false;
  1223. if ($handle = opendir($directory)) {
  1224. while(($file = readdir($handle)) !== false) {
  1225. $path = $directory.'/'.$file;
  1226. if ($file != '.' && $file != '..') {
  1227. if (is_file($path)) {
  1228. $size += filesize($path);
  1229. } elseif (is_dir($path)) {
  1230. $handlesize = recurse_dirsize($path);
  1231. if ($handlesize > 0)
  1232. $size += $handlesize;
  1233. }
  1234. }
  1235. }
  1236. closedir($handle);
  1237. }
  1238. return $size;
  1239. }
  1240. /**
  1241. * Check an array of MIME types against a whitelist.
  1242. *
  1243. * WordPress ships with a set of allowed upload filetypes,
  1244. * which is defined in wp-includes/functions.php in
  1245. * get_allowed_mime_types(). This function is used to filter
  1246. * that list against the filetype whitelist provided by Multisite
  1247. * Super Admins at wp-admin/network/settings.php.
  1248. *
  1249. * @since MU
  1250. *
  1251. * @param array $mimes
  1252. * @return array
  1253. */
  1254. function check_upload_mimes( $mimes ) {
  1255. $site_exts = explode( ' ', get_site_option( 'upload_filetypes' ) );
  1256. foreach ( $site_exts as $ext ) {
  1257. foreach ( $mimes as $ext_pattern => $mime ) {
  1258. if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
  1259. $site_mimes[$ext_pattern] = $mime;
  1260. }
  1261. }
  1262. return $site_mimes;
  1263. }
  1264. /**
  1265. * Update a blog's post count.
  1266. *
  1267. * WordPress MS stores a blog's post count as an option so as
  1268. * to avoid extraneous COUNTs when a blog's details are fetched
  1269. * with get_blog_details(). This function is called when posts
  1270. * are published to make sure the count stays current.
  1271. *
  1272. * @since MU
  1273. */
  1274. function update_posts_count( $deprecated = '' ) {
  1275. global $wpdb;
  1276. update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
  1277. }
  1278. /**
  1279. * Logs user registrations.
  1280. *
  1281. * @since MU
  1282. *
  1283. * @param int $blog_id
  1284. * @param int $user_id
  1285. */
  1286. function wpmu_log_new_registrations( $blog_id, $user_id ) {
  1287. global $wpdb;
  1288. $user = get_userdata( (int) $user_id );
  1289. $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')) );
  1290. }
  1291. /**
  1292. * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
  1293. *
  1294. * @since 3.0.0
  1295. *
  1296. * @see term_id_filter
  1297. *
  1298. * @param int $term_id An ID for a term on the current blog.
  1299. * @return int An ID from the global terms table mapped from $term_id.
  1300. */
  1301. function global_terms( $term_id, $deprecated = '' ) {
  1302. global $wpdb;
  1303. static $global_terms_recurse = null;
  1304. if ( !global_terms_enabled() )
  1305. return $term_id;
  1306. // prevent a race condition
  1307. $recurse_start = false;
  1308. if ( $global_terms_recurse === null ) {
  1309. $recurse_start = true;
  1310. $global_terms_recurse = 1;
  1311. } elseif ( 10 < $global_terms_recurse++ ) {
  1312. return $term_id;
  1313. }
  1314. $term_id = intval( $term_id );
  1315. $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
  1316. $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
  1317. if ( $global_id == null ) {
  1318. $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
  1319. if ( null == $used_global_id ) {
  1320. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1321. $global_id = $wpdb->insert_id;
  1322. if ( empty( $global_id ) )
  1323. return $term_id;
  1324. } else {
  1325. $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
  1326. $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
  1327. $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
  1328. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1329. $global_id = $wpdb->insert_id;
  1330. }
  1331. } elseif ( $global_id != $term_id ) {
  1332. $local_id = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
  1333. if ( null != $local_id )
  1334. $local_id = global_terms( $local_id );
  1335. if ( 10 < $global_terms_recurse )
  1336. $global_id = $term_id;
  1337. }
  1338. if ( $global_id != $term_id ) {
  1339. if ( get_option( 'default_category' ) == $term_id )
  1340. update_option( 'default_category', $global_id );
  1341. $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
  1342. $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
  1343. $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
  1344. clean_term_cache($term_id);
  1345. }
  1346. if( $recurse_start )
  1347. $global_terms_recurse = null;
  1348. return $global_id;
  1349. }
  1350. /**
  1351. * Ensure that the current site's domain is listed in the allowed redirect host list.
  1352. *
  1353. * @see wp_validate_redirect()
  1354. * @since MU
  1355. *
  1356. * @return array The current site's domain
  1357. */
  1358. function redirect_this_site( $deprecated = '' ) {
  1359. global $current_site;
  1360. return array( $current_site->domain );
  1361. }
  1362. /**
  1363. * Check whether an upload is too big.
  1364. *
  1365. * @since MU
  1366. *
  1367. * @param array $upload
  1368. * @return mixed If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
  1369. */
  1370. function upload_is_file_too_big( $upload ) {
  1371. if ( is_array( $upload ) == false || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) )
  1372. return $upload;
  1373. if ( strlen( $upload['bits'] ) > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
  1374. return sprintf( __( 'This file is too big. Files must be less than %d KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ));
  1375. return $upload;
  1376. }
  1377. /**
  1378. * Add a nonce field to the signup page.
  1379. *
  1380. * @since MU
  1381. * @uses wp_nonce_field()
  1382. */
  1383. function signup_nonce_fields() {
  1384. $id = mt_rand();
  1385. echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
  1386. wp_nonce_field('signup_form_' . $id, '_signup_form', false);
  1387. }
  1388. /**
  1389. * Process the signup nonce created in signup_nonce_fields().
  1390. *
  1391. * @since MU
  1392. * @uses wp_create_nonce()
  1393. *
  1394. * @param array $result
  1395. * @return array
  1396. */
  1397. function signup_nonce_check( $result ) {
  1398. if ( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
  1399. return $result;
  1400. if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
  1401. wp_die( __( 'Please try again.' ) );
  1402. return $result;
  1403. }
  1404. /**
  1405. * Correct 404 redirects when NOBLOGREDIRECT is defined.
  1406. *
  1407. * @since MU
  1408. */
  1409. function maybe_redirect_404() {
  1410. global $current_site;
  1411. if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
  1412. if ( $destination == '%siteurl%' )
  1413. $destination = network_home_url();
  1414. wp_redirect( $destination );
  1415. exit();
  1416. }
  1417. }
  1418. /**
  1419. * Add a new user to a blog by visiting /newbloguser/username/.
  1420. *
  1421. * This will only work when the user's details are saved as an option
  1422. * keyed as 'new_user_x', where 'x' is the username of the user to be
  1423. * added, as when a user is invited through the regular WP Add User interface.
  1424. *
  1425. * @since MU
  1426. * @uses add_existing_user_to_blog()
  1427. */
  1428. function maybe_add_existing_user_to_blog() {
  1429. if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) )
  1430. return false;
  1431. $parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
  1432. $key = array_pop( $parts );
  1433. if ( $key == '' )
  1434. $key = array_pop( $parts );
  1435. $details = get_option( 'new_user_' . $key );
  1436. if ( !empty( $details ) )
  1437. delete_option( 'new_user_' . $key );
  1438. if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) )
  1439. wp_die( sprintf(__('An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.'), home_url() ) );
  1440. wp_die( sprintf( __( 'You have been added to this site. Please visit the <a href="%s">homepage</a> or <a href="%s">log in</a> using your username and password.' ), home_url(), admin_url() ), __( 'WordPress &rsaquo; Success' ) );
  1441. }
  1442. /**
  1443. * Add a user to a blog based on details from maybe_add_existing_user_to_blog().
  1444. *
  1445. * @since MU
  1446. * @uses add_user_to_blog()
  1447. *
  1448. * @param array $details
  1449. */
  1450. function add_existing_user_to_blog( $details = false ) {
  1451. global $blog_id;
  1452. if ( is_array( $details ) ) {
  1453. $result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] );
  1454. do_action( 'added_existing_user', $details[ 'user_id' ], $result );
  1455. }
  1456. return $result;
  1457. }
  1458. /**
  1459. * Add a newly created user to the appropriate blog
  1460. *
  1461. * @since MU
  1462. *
  1463. * @param int $user_id
  1464. * @param string $email
  1465. * @param array $meta
  1466. */
  1467. function add_new_user_to_blog( $user_id, $email, $meta ) {
  1468. global $current_site;
  1469. if ( !empty( $meta[ 'add_to_blog' ] ) ) {
  1470. $blog_id = $meta[ 'add_to_blog' ];
  1471. $role = $meta[ 'new_role' ];
  1472. remove_user_from_blog($user_id, $current_site->blog_id); // remove user from main blog.
  1473. add_user_to_blog( $blog_id, $user_id, $role );
  1474. update_user_meta( $user_id, 'primary_blog', $blog_id );
  1475. }
  1476. }
  1477. /**
  1478. * Correct From host on outgoing mail to match the site domain
  1479. *
  1480. * @since MU
  1481. */
  1482. function fix_phpmailer_messageid( $phpmailer ) {
  1483. global $current_site;
  1484. $phpmailer->Hostname = $current_site->domain;
  1485. }
  1486. /**
  1487. * Check to see whether a user is marked as a spammer, based on username
  1488. *
  1489. * @since MU
  1490. * @uses get_current_user_id()
  1491. * @uses get_user_id_from_string()
  1492. *
  1493. * @param string $username
  1494. * @return bool
  1495. */
  1496. function is_user_spammy( $username = 0 ) {
  1497. if ( $username == 0 ) {
  1498. $user_id = get_current_user_id();
  1499. } else {
  1500. $user_id = get_user_id_from_string( $username );
  1501. }
  1502. $u = get_userdata( $user_id );
  1503. return ( isset( $u->spam ) && $u->spam == 1 );
  1504. }
  1505. /**
  1506. * Update this blog's 'public' setting in the global blogs table.
  1507. *
  1508. * Public blogs have a setting of 1, private blogs are 0.
  1509. *
  1510. * @since MU
  1511. * @uses update_blog_status()
  1512. *
  1513. * @param int $old_value
  1514. * @param int $value The new public value
  1515. * @return bool
  1516. */
  1517. function update_blog_public( $old_value, $value ) {
  1518. global $wpdb;
  1519. do_action('update_blog_public');
  1520. update_blog_status( $wpdb->blogid, 'public', (int) $value );
  1521. }
  1522. add_action('update_option_blog_public', 'update_blog_public', 10, 2);
  1523. /**
  1524. * Get the "dashboard blog", the blog where users without a blog edit their profile data.
  1525. *
  1526. * @since MU
  1527. * @uses get_blog_details()
  1528. *
  1529. * @return int
  1530. */
  1531. function get_dashboard_blog() {
  1532. if ( $blog = get_site_option( 'dashboard_blog' ) )
  1533. return get_blog_details( $blog );
  1534. return get_blog_details( $GLOBALS['current_site']->blog_id );
  1535. }
  1536. /**
  1537. * Check whether a usermeta key has to do with the current blog.
  1538. *
  1539. * @since MU
  1540. * @uses wp_get_current_user()
  1541. *
  1542. * @param string $key
  1543. * @param int $user_id Optional. Defaults to current user.
  1544. * @param int $blog_id Optional. Defaults to current blog.
  1545. * @return bool
  1546. */
  1547. function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {
  1548. global $wpdb;
  1549. $current_user = wp_get_current_user();
  1550. if ( $user_id == 0 )
  1551. $user_id = $current_user->ID;
  1552. if ( $blog_id == 0 )
  1553. $blog_id = $wpdb->blogid;
  1554. $local_key = $wpdb->get_blog_prefix( $blog_id ) . $key;
  1555. if ( isset( $current_user->$local_key ) )
  1556. return true;
  1557. return false;
  1558. }
  1559. /**
  1560. * Check whether users can self-register, based on Network settings.
  1561. *
  1562. * @since MU
  1563. *
  1564. * @return bool
  1565. */
  1566. function users_can_register_signup_filter() {
  1567. $registration = get_site_option('registration');
  1568. if ( $registration == 'all' || $registration == 'user' )
  1569. return true;
  1570. return false;
  1571. }
  1572. add_filter('option_users_can_register', 'users_can_register_signup_filter');
  1573. /**
  1574. * Ensure that the welcome message is not empty. Currently unused.
  1575. *
  1576. * @since MU
  1577. *
  1578. * @param string $text
  1579. * @return string
  1580. */
  1581. function welcome_user_msg_filter( $text ) {
  1582. if ( !$text ) {
  1583. remove_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
  1584. $text = __( 'Dear User,
  1585. Your new account is set up.
  1586. You can log in with the following information:
  1587. Username: USERNAME
  1588. Password: PASSWORD
  1589. LOGINLINK
  1590. Thanks!
  1591. --The Team @ SITE_NAME' );
  1592. update_site_option( 'welcome_user_email', $text );
  1593. }
  1594. return $text;
  1595. }
  1596. add_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
  1597. /**
  1598. * Whether to force SSL on content.
  1599. *
  1600. * @since 2.8.5
  1601. *
  1602. * @param string|bool $force
  1603. * @return bool True if forced, false if not forced.
  1604. */
  1605. function force_ssl_content( $force = '' ) {
  1606. static $forced_content;
  1607. if ( '' != $force ) {
  1608. $old_forced = $forced_content;
  1609. $forced_content = $force;
  1610. return $old_forced;
  1611. }
  1612. return $forced_content;
  1613. }
  1614. /**
  1615. * Formats a URL to use https.
  1616. *
  1617. * Useful as a filter.
  1618. *
  1619. * @since 2.8.5
  1620. *
  1621. * @param string URL
  1622. * @return string URL with https as the scheme
  1623. */
  1624. function filter_SSL( $url ) {
  1625. if ( ! is_string( $url ) )
  1626. return get_bloginfo( 'url' ); // Return home blog url with proper scheme
  1627. if ( force_ssl_content() && is_ssl() )
  1628. $url = set_url_scheme( $url, 'https' );
  1629. return $url;
  1630. }
  1631. /**
  1632. * Schedule update of the network-wide counts for the current network.
  1633. *
  1634. * @since 3.1.0
  1635. */
  1636. function wp_schedule_update_network_counts() {
  1637. if ( !is_main_site() )
  1638. return;
  1639. if ( !wp_next_scheduled('update_network_counts') && !defined('WP_INSTALLING') )
  1640. wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
  1641. }
  1642. /**
  1643. * Update the network-wide counts for the current network.
  1644. *
  1645. * @since 3.1.0
  1646. */
  1647. function wp_update_network_counts() {
  1648. global $wpdb;
  1649. $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) );
  1650. update_site_option( 'blog_count', $count );
  1651. $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
  1652. update_site_option( 'user_count', $count );
  1653. }
  1654. /**
  1655. * Returns the space used by the current blog.
  1656. *
  1657. * @since 3.5.0
  1658. *
  1659. * @return int Used space in megabytes
  1660. */
  1661. function get_space_used() {
  1662. // Allow for an alternative way of tracking storage space used
  1663. $space_used = apply_filters( 'pre_get_space_used', false );
  1664. if ( false === $space_used ) {
  1665. $upload_dir = wp_upload_dir();
  1666. $space_used = get_dirsize( $upload_dir['basedir'] ) / 1024 / 1024;
  1667. }
  1668. return $space_used;
  1669. }
  1670. /**
  1671. * Returns the upload quota for the current blog.
  1672. *
  1673. * @since MU
  1674. *
  1675. * @return int Quota in megabytes
  1676. */
  1677. function get_space_allowed() {
  1678. $space_allowed = get_option( 'blog_upload_space' );
  1679. if ( ! is_numeric( $space_allowed ) )
  1680. $space_allowed = get_site_option( 'blog_upload_space' );
  1681. if ( empty( $space_allowed ) || ! is_numeric( $space_allowed ) )
  1682. $space_allowed = 50;
  1683. return $space_allowed;
  1684. }
  1685. /**
  1686. * Determines if there is any upload space left in the current blog's quota.
  1687. *
  1688. * @since 3.0.0
  1689. *
  1690. * @return int of upload space available in bytes
  1691. */
  1692. function get_upload_space_available() {
  1693. $space_allowed = get_space_allowed() * 1024 * 1024;
  1694. if ( get_site_option( 'upload_space_check_disabled' ) )
  1695. return $space_allowed;
  1696. $space_used = get_space_used() * 1024 * 1024;
  1697. if ( ( $space_allowed - $space_used ) <= 0 )
  1698. return 0;
  1699. return $space_allowed - $space_used;
  1700. }
  1701. /**
  1702. * @since 3.0.0
  1703. *
  1704. * @return int of upload size limit in bytes
  1705. */
  1706. function upload_size_limit_filter( $size ) {
  1707. $fileupload_maxk = 1024 * get_site_option( 'fileupload_maxk', 1500 );
  1708. if ( get_site_option( 'upload_space_check_disabled' ) )
  1709. return min( $size, $fileupload_maxk );
  1710. return min( $size, $fileupload_maxk, get_upload_space_available() );
  1711. }