ms-functions.php 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556
  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. *
  14. * @return array Site and user count for the network.
  15. */
  16. function get_sitestats() {
  17. $stats = array(
  18. 'blogs' => get_blog_count(),
  19. 'users' => get_user_count(),
  20. );
  21. return $stats;
  22. }
  23. /**
  24. * Get one of a user's active blogs
  25. *
  26. * Returns the user's primary blog, if they have one and
  27. * it is active. If it's inactive, function returns another
  28. * active blog of the user. If none are found, the user
  29. * is added as a Subscriber to the Dashboard Blog and that blog
  30. * is returned.
  31. *
  32. * @since MU 1.0
  33. *
  34. * @global wpdb $wpdb WordPress database abstraction object.
  35. *
  36. * @param int $user_id The unique ID of the user
  37. * @return WP_Site|void The blog object
  38. */
  39. function get_active_blog_for_user( $user_id ) {
  40. global $wpdb;
  41. $blogs = get_blogs_of_user( $user_id );
  42. if ( empty( $blogs ) )
  43. return;
  44. if ( !is_multisite() )
  45. return $blogs[$wpdb->blogid];
  46. $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
  47. $first_blog = current($blogs);
  48. if ( false !== $primary_blog ) {
  49. if ( ! isset( $blogs[ $primary_blog ] ) ) {
  50. update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  51. $primary = get_site( $first_blog->userblog_id );
  52. } else {
  53. $primary = get_site( $primary_blog );
  54. }
  55. } else {
  56. //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
  57. add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );
  58. update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  59. $primary = $first_blog;
  60. }
  61. if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
  62. $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
  63. $ret = false;
  64. if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
  65. foreach ( (array) $blogs as $blog_id => $blog ) {
  66. if ( $blog->site_id != $wpdb->siteid )
  67. continue;
  68. $details = get_site( $blog_id );
  69. if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
  70. $ret = $blog;
  71. if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id )
  72. update_user_meta( $user_id, 'primary_blog', $blog_id );
  73. if ( !get_user_meta($user_id , 'source_domain', true) )
  74. update_user_meta( $user_id, 'source_domain', $blog->domain );
  75. break;
  76. }
  77. }
  78. } else {
  79. return;
  80. }
  81. return $ret;
  82. } else {
  83. return $primary;
  84. }
  85. }
  86. /**
  87. * The number of active users in your installation.
  88. *
  89. * The count is cached and updated twice daily. This is not a live count.
  90. *
  91. * @since MU 2.7
  92. * @since 4.8.0 The $network_id parameter has been added.
  93. *
  94. * @param int|null $network_id ID of the network. Default is the current network.
  95. * @return int Number of active users on the network.
  96. */
  97. function get_user_count( $network_id = null ) {
  98. return get_network_option( $network_id, 'user_count' );
  99. }
  100. /**
  101. * The number of active sites on your installation.
  102. *
  103. * The count is cached and updated twice daily. This is not a live count.
  104. *
  105. * @since MU 1.0
  106. * @since 3.7.0 The $network_id parameter has been deprecated.
  107. * @since 4.8.0 The $network_id parameter is now being used.
  108. *
  109. * @param int|null $network_id ID of the network. Default is the current network.
  110. * @return int Number of active sites on the network.
  111. */
  112. function get_blog_count( $network_id = null ) {
  113. return get_network_option( $network_id, 'blog_count' );
  114. }
  115. /**
  116. * Get a blog post from any site on the network.
  117. *
  118. * @since MU 1.0
  119. *
  120. * @param int $blog_id ID of the blog.
  121. * @param int $post_id ID of the post you're looking for.
  122. * @return WP_Post|null WP_Post on success or null on failure
  123. */
  124. function get_blog_post( $blog_id, $post_id ) {
  125. switch_to_blog( $blog_id );
  126. $post = get_post( $post_id );
  127. restore_current_blog();
  128. return $post;
  129. }
  130. /**
  131. * Adds a user to a blog.
  132. *
  133. * Use the {@see 'add_user_to_blog'} action to fire an event when users are added to a blog.
  134. *
  135. * @since MU 1.0
  136. *
  137. * @param int $blog_id ID of the blog you're adding the user to.
  138. * @param int $user_id ID of the user you're adding.
  139. * @param string $role The role you want the user to have
  140. * @return true|WP_Error
  141. */
  142. function add_user_to_blog( $blog_id, $user_id, $role ) {
  143. switch_to_blog($blog_id);
  144. $user = get_userdata( $user_id );
  145. if ( ! $user ) {
  146. restore_current_blog();
  147. return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
  148. }
  149. if ( !get_user_meta($user_id, 'primary_blog', true) ) {
  150. update_user_meta($user_id, 'primary_blog', $blog_id);
  151. $site = get_site( $blog_id );
  152. update_user_meta( $user_id, 'source_domain', $site->domain );
  153. }
  154. $user->set_role($role);
  155. /**
  156. * Fires immediately after a user is added to a site.
  157. *
  158. * @since MU
  159. *
  160. * @param int $user_id User ID.
  161. * @param string $role User role.
  162. * @param int $blog_id Blog ID.
  163. */
  164. do_action( 'add_user_to_blog', $user_id, $role, $blog_id );
  165. wp_cache_delete( $user_id, 'users' );
  166. wp_cache_delete( $blog_id . '_user_count', 'blog-details' );
  167. restore_current_blog();
  168. return true;
  169. }
  170. /**
  171. * Remove a user from a blog.
  172. *
  173. * Use the {@see 'remove_user_from_blog'} action to fire an event when
  174. * users are removed from a blog.
  175. *
  176. * Accepts an optional `$reassign` parameter, if you want to
  177. * reassign the user's blog posts to another user upon removal.
  178. *
  179. * @since MU 1.0
  180. *
  181. * @global wpdb $wpdb WordPress database abstraction object.
  182. *
  183. * @param int $user_id ID of the user you're removing.
  184. * @param int $blog_id ID of the blog you're removing the user from.
  185. * @param string $reassign Optional. A user to whom to reassign posts.
  186. * @return true|WP_Error
  187. */
  188. function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
  189. global $wpdb;
  190. switch_to_blog($blog_id);
  191. $user_id = (int) $user_id;
  192. /**
  193. * Fires before a user is removed from a site.
  194. *
  195. * @since MU
  196. *
  197. * @param int $user_id User ID.
  198. * @param int $blog_id Blog ID.
  199. */
  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. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $user_id ) );
  233. $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $user_id ) );
  234. if ( ! empty( $post_ids ) ) {
  235. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id ) );
  236. array_walk( $post_ids, 'clean_post_cache' );
  237. }
  238. if ( ! empty( $link_ids ) ) {
  239. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id ) );
  240. array_walk( $link_ids, 'clean_bookmark_cache' );
  241. }
  242. }
  243. restore_current_blog();
  244. return true;
  245. }
  246. /**
  247. * Get the permalink for a post on another blog.
  248. *
  249. * @since MU 1.0
  250. *
  251. * @param int $blog_id ID of the source blog.
  252. * @param int $post_id ID of the desired post.
  253. * @return string The post's permalink
  254. */
  255. function get_blog_permalink( $blog_id, $post_id ) {
  256. switch_to_blog( $blog_id );
  257. $link = get_permalink( $post_id );
  258. restore_current_blog();
  259. return $link;
  260. }
  261. /**
  262. * Get a blog's numeric ID from its URL.
  263. *
  264. * On a subdirectory installation like example.com/blog1/,
  265. * $domain will be the root 'example.com' and $path the
  266. * subdirectory '/blog1/'. With subdomains like blog1.example.com,
  267. * $domain is 'blog1.example.com' and $path is '/'.
  268. *
  269. * @since MU 2.6.5
  270. *
  271. * @global wpdb $wpdb WordPress database abstraction object.
  272. *
  273. * @param string $domain
  274. * @param string $path Optional. Not required for subdomain installations.
  275. * @return int 0 if no blog found, otherwise the ID of the matching blog
  276. */
  277. function get_blog_id_from_url( $domain, $path = '/' ) {
  278. $domain = strtolower( $domain );
  279. $path = strtolower( $path );
  280. $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
  281. if ( $id == -1 ) // blog does not exist
  282. return 0;
  283. elseif ( $id )
  284. return (int) $id;
  285. $args = array(
  286. 'domain' => $domain,
  287. 'path' => $path,
  288. 'fields' => 'ids',
  289. );
  290. $result = get_sites( $args );
  291. $id = array_shift( $result );
  292. if ( ! $id ) {
  293. wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
  294. return 0;
  295. }
  296. wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
  297. return $id;
  298. }
  299. // Admin functions
  300. /**
  301. * Checks an email address against a list of banned domains.
  302. *
  303. * This function checks against the Banned Email Domains list
  304. * at wp-admin/network/settings.php. The check is only run on
  305. * self-registrations; user creation at wp-admin/network/users.php
  306. * bypasses this check.
  307. *
  308. * @since MU
  309. *
  310. * @param string $user_email The email provided by the user at registration.
  311. * @return bool Returns true when the email address is banned.
  312. */
  313. function is_email_address_unsafe( $user_email ) {
  314. $banned_names = get_site_option( 'banned_email_domains' );
  315. if ( $banned_names && ! is_array( $banned_names ) )
  316. $banned_names = explode( "\n", $banned_names );
  317. $is_email_address_unsafe = false;
  318. if ( $banned_names && is_array( $banned_names ) && false !== strpos( $user_email, '@', 1 ) ) {
  319. $banned_names = array_map( 'strtolower', $banned_names );
  320. $normalized_email = strtolower( $user_email );
  321. list( $email_local_part, $email_domain ) = explode( '@', $normalized_email );
  322. foreach ( $banned_names as $banned_domain ) {
  323. if ( ! $banned_domain )
  324. continue;
  325. if ( $email_domain == $banned_domain ) {
  326. $is_email_address_unsafe = true;
  327. break;
  328. }
  329. $dotted_domain = ".$banned_domain";
  330. if ( $dotted_domain === substr( $normalized_email, -strlen( $dotted_domain ) ) ) {
  331. $is_email_address_unsafe = true;
  332. break;
  333. }
  334. }
  335. }
  336. /**
  337. * Filters whether an email address is unsafe.
  338. *
  339. * @since 3.5.0
  340. *
  341. * @param bool $is_email_address_unsafe Whether the email address is "unsafe". Default false.
  342. * @param string $user_email User email address.
  343. */
  344. return apply_filters( 'is_email_address_unsafe', $is_email_address_unsafe, $user_email );
  345. }
  346. /**
  347. * Sanitize and validate data required for a user sign-up.
  348. *
  349. * Verifies the validity and uniqueness of user names and user email addresses,
  350. * and checks email addresses against admin-provided domain whitelists and blacklists.
  351. *
  352. * The {@see 'wpmu_validate_user_signup'} hook provides an easy way to modify the sign-up
  353. * process. The value $result, which is passed to the hook, contains both the user-provided
  354. * info and the error messages created by the function. {@see 'wpmu_validate_user_signup'}
  355. * allows you to process the data in any way you'd like, and unset the relevant errors if
  356. * necessary.
  357. *
  358. * @since MU
  359. *
  360. * @global wpdb $wpdb WordPress database abstraction object.
  361. *
  362. * @param string $user_name The login name provided by the user.
  363. * @param string $user_email The email provided by the user.
  364. * @return array Contains username, email, and error messages.
  365. */
  366. function wpmu_validate_user_signup($user_name, $user_email) {
  367. global $wpdb;
  368. $errors = new WP_Error();
  369. $orig_username = $user_name;
  370. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  371. if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) {
  372. $errors->add( 'user_name', __( 'Usernames can only contain lowercase letters (a-z) and numbers.' ) );
  373. $user_name = $orig_username;
  374. }
  375. $user_email = sanitize_email( $user_email );
  376. if ( empty( $user_name ) )
  377. $errors->add('user_name', __( 'Please enter a username.' ) );
  378. $illegal_names = get_site_option( 'illegal_names' );
  379. if ( ! is_array( $illegal_names ) ) {
  380. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  381. add_site_option( 'illegal_names', $illegal_names );
  382. }
  383. if ( in_array( $user_name, $illegal_names ) ) {
  384. $errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) );
  385. }
  386. /** This filter is documented in wp-includes/user.php */
  387. $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
  388. if ( in_array( strtolower( $user_name ), array_map( 'strtolower', $illegal_logins ) ) ) {
  389. $errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) );
  390. }
  391. if ( ! is_email( $user_email ) ) {
  392. $errors->add( 'user_email', __( 'Please enter a valid email address.' ) );
  393. } elseif ( is_email_address_unsafe( $user_email ) ) {
  394. $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.' ) );
  395. }
  396. if ( strlen( $user_name ) < 4 )
  397. $errors->add('user_name', __( 'Username must be at least 4 characters.' ) );
  398. if ( strlen( $user_name ) > 60 ) {
  399. $errors->add( 'user_name', __( 'Username may not be longer than 60 characters.' ) );
  400. }
  401. // all numeric?
  402. if ( preg_match( '/^[0-9]*$/', $user_name ) )
  403. $errors->add('user_name', __('Sorry, usernames must have letters too!'));
  404. $limited_email_domains = get_site_option( 'limited_email_domains' );
  405. if ( is_array( $limited_email_domains ) && ! empty( $limited_email_domains ) ) {
  406. $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
  407. if ( ! in_array( $emaildomain, $limited_email_domains ) ) {
  408. $errors->add('user_email', __('Sorry, that email address is not allowed!'));
  409. }
  410. }
  411. // Check if the username has been used already.
  412. if ( username_exists($user_name) )
  413. $errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
  414. // Check if the email address has been used already.
  415. if ( email_exists($user_email) )
  416. $errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );
  417. // Has someone already signed up for this username?
  418. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  419. if ( $signup != null ) {
  420. $registered_at = mysql2date('U', $signup->registered);
  421. $now = current_time( 'timestamp', true );
  422. $diff = $now - $registered_at;
  423. // If registered more than two days ago, cancel registration and let this signup go through.
  424. if ( $diff > 2 * DAY_IN_SECONDS )
  425. $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
  426. else
  427. $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
  428. }
  429. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  430. if ( $signup != null ) {
  431. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  432. // If registered more than two days ago, cancel registration and let this signup go through.
  433. if ( $diff > 2 * DAY_IN_SECONDS )
  434. $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
  435. else
  436. $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.'));
  437. }
  438. $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
  439. /**
  440. * Filters the validated user registration details.
  441. *
  442. * This does not allow you to override the username or email of the user during
  443. * registration. The values are solely used for validation and error handling.
  444. *
  445. * @since MU
  446. *
  447. * @param array $result {
  448. * The array of user name, email and the error messages.
  449. *
  450. * @type string $user_name Sanitized and unique username.
  451. * @type string $orig_username Original username.
  452. * @type string $user_email User email address.
  453. * @type WP_Error $errors WP_Error object containing any errors found.
  454. * }
  455. */
  456. return apply_filters( 'wpmu_validate_user_signup', $result );
  457. }
  458. /**
  459. * Processes new site registrations.
  460. *
  461. * Checks the data provided by the user during blog signup. Verifies
  462. * the validity and uniqueness of blog paths and domains.
  463. *
  464. * This function prevents the current user from registering a new site
  465. * with a blogname equivalent to another user's login name. Passing the
  466. * $user parameter to the function, where $user is the other user, is
  467. * effectively an override of this limitation.
  468. *
  469. * Filter {@see 'wpmu_validate_blog_signup'} if you want to modify
  470. * the way that WordPress validates new site signups.
  471. *
  472. * @since MU
  473. *
  474. * @global wpdb $wpdb
  475. * @global string $domain
  476. *
  477. * @param string $blogname The blog name provided by the user. Must be unique.
  478. * @param string $blog_title The blog title provided by the user.
  479. * @param WP_User|string $user Optional. The user object to check against the new site name.
  480. * @return array Contains the new site data and error messages.
  481. */
  482. function wpmu_validate_blog_signup( $blogname, $blog_title, $user = '' ) {
  483. global $wpdb, $domain;
  484. $current_network = get_network();
  485. $base = $current_network->path;
  486. $blog_title = strip_tags( $blog_title );
  487. $errors = new WP_Error();
  488. $illegal_names = get_site_option( 'illegal_names' );
  489. if ( $illegal_names == false ) {
  490. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  491. add_site_option( 'illegal_names', $illegal_names );
  492. }
  493. /*
  494. * On sub dir installs, some names are so illegal, only a filter can
  495. * spring them from jail.
  496. */
  497. if ( ! is_subdomain_install() ) {
  498. $illegal_names = array_merge( $illegal_names, get_subdirectory_reserved_names() );
  499. }
  500. if ( empty( $blogname ) )
  501. $errors->add('blogname', __( 'Please enter a site name.' ) );
  502. if ( preg_match( '/[^a-z0-9]+/', $blogname ) ) {
  503. $errors->add( 'blogname', __( 'Site names can only contain lowercase letters (a-z) and numbers.' ) );
  504. }
  505. if ( in_array( $blogname, $illegal_names ) )
  506. $errors->add('blogname', __( 'That name is not allowed.' ) );
  507. /**
  508. * Filters the minimum site name length required when validating a site signup.
  509. *
  510. * @since 4.8.0
  511. *
  512. * @param int $length The minimum site name length. Default 4.
  513. */
  514. $minimum_site_name_length = apply_filters( 'minimum_site_name_length', 4 );
  515. if ( strlen( $blogname ) < $minimum_site_name_length ) {
  516. /* translators: %s: minimum site name length */
  517. $errors->add( 'blogname', sprintf( _n( 'Site name must be at least %s character.', 'Site name must be at least %s characters.', $minimum_site_name_length ), number_format_i18n( $minimum_site_name_length ) ) );
  518. }
  519. // do not allow users to create a blog that conflicts with a page on the main blog.
  520. if ( !is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_network->site_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) )
  521. $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
  522. // all numeric?
  523. if ( preg_match( '/^[0-9]*$/', $blogname ) )
  524. $errors->add('blogname', __('Sorry, site names must have letters too!'));
  525. /**
  526. * Filters the new site name during registration.
  527. *
  528. * The name is the site's subdomain or the site's subdirectory
  529. * path depending on the network settings.
  530. *
  531. * @since MU
  532. *
  533. * @param string $blogname Site name.
  534. */
  535. $blogname = apply_filters( 'newblogname', $blogname );
  536. $blog_title = wp_unslash( $blog_title );
  537. if ( empty( $blog_title ) )
  538. $errors->add('blog_title', __( 'Please enter a site title.' ) );
  539. // Check if the domain/path has been used already.
  540. if ( is_subdomain_install() ) {
  541. $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
  542. $path = $base;
  543. } else {
  544. $mydomain = "$domain";
  545. $path = $base.$blogname.'/';
  546. }
  547. if ( domain_exists($mydomain, $path, $current_network->id) )
  548. $errors->add( 'blogname', __( 'Sorry, that site already exists!' ) );
  549. if ( username_exists( $blogname ) ) {
  550. if ( ! is_object( $user ) || ( is_object($user) && ( $user->user_login != $blogname ) ) )
  551. $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
  552. }
  553. // Has someone already signed up for this domain?
  554. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
  555. if ( ! empty($signup) ) {
  556. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  557. // If registered more than two days ago, cancel registration and let this signup go through.
  558. if ( $diff > 2 * DAY_IN_SECONDS )
  559. $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) );
  560. else
  561. $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
  562. }
  563. $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'user' => $user, 'errors' => $errors);
  564. /**
  565. * Filters site details and error messages following registration.
  566. *
  567. * @since MU
  568. *
  569. * @param array $result {
  570. * Array of domain, path, blog name, blog title, user and error messages.
  571. *
  572. * @type string $domain Domain for the site.
  573. * @type string $path Path for the site. Used in subdirectory installs.
  574. * @type string $blogname The unique site name (slug).
  575. * @type string $blog_title Blog title.
  576. * @type string|WP_User $user By default, an empty string. A user object if provided.
  577. * @type WP_Error $errors WP_Error containing any errors found.
  578. * }
  579. */
  580. return apply_filters( 'wpmu_validate_blog_signup', $result );
  581. }
  582. /**
  583. * Record site signup information for future activation.
  584. *
  585. * @since MU
  586. *
  587. * @global wpdb $wpdb WordPress database abstraction object.
  588. *
  589. * @param string $domain The requested domain.
  590. * @param string $path The requested path.
  591. * @param string $title The requested site title.
  592. * @param string $user The user's requested login name.
  593. * @param string $user_email The user's email address.
  594. * @param array $meta Optional. Signup meta data. By default, contains the requested privacy setting and lang_id.
  595. */
  596. function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() ) {
  597. global $wpdb;
  598. $key = substr( md5( time() . wp_rand() . $domain ), 0, 16 );
  599. /**
  600. * Filters the metadata for a site signup.
  601. *
  602. * The metadata will be serialized prior to storing it in the database.
  603. *
  604. * @since 4.8.0
  605. *
  606. * @param array $meta Signup meta data. Default empty array.
  607. * @param string $domain The requested domain.
  608. * @param string $path The requested path.
  609. * @param string $title The requested site title.
  610. * @param string $user The user's requested login name.
  611. * @param string $user_email The user's email address.
  612. * @param string $key The user's activation key.
  613. */
  614. $meta = apply_filters( 'signup_site_meta', $meta, $domain, $path, $title, $user, $user_email, $key );
  615. $wpdb->insert( $wpdb->signups, array(
  616. 'domain' => $domain,
  617. 'path' => $path,
  618. 'title' => $title,
  619. 'user_login' => $user,
  620. 'user_email' => $user_email,
  621. 'registered' => current_time('mysql', true),
  622. 'activation_key' => $key,
  623. 'meta' => serialize( $meta )
  624. ) );
  625. /**
  626. * Fires after site signup information has been written to the database.
  627. *
  628. * @since 4.4.0
  629. *
  630. * @param string $domain The requested domain.
  631. * @param string $path The requested path.
  632. * @param string $title The requested site title.
  633. * @param string $user The user's requested login name.
  634. * @param string $user_email The user's email address.
  635. * @param string $key The user's activation key.
  636. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  637. */
  638. do_action( 'after_signup_site', $domain, $path, $title, $user, $user_email, $key, $meta );
  639. }
  640. /**
  641. * Record user signup information for future activation.
  642. *
  643. * This function is used when user registration is open but
  644. * new site registration is not.
  645. *
  646. * @since MU
  647. *
  648. * @global wpdb $wpdb WordPress database abstraction object.
  649. *
  650. * @param string $user The user's requested login name.
  651. * @param string $user_email The user's email address.
  652. * @param array $meta Optional. Signup meta data. Default empty array.
  653. */
  654. function wpmu_signup_user( $user, $user_email, $meta = array() ) {
  655. global $wpdb;
  656. // Format data
  657. $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
  658. $user_email = sanitize_email( $user_email );
  659. $key = substr( md5( time() . wp_rand() . $user_email ), 0, 16 );
  660. /**
  661. * Filters the metadata for a user signup.
  662. *
  663. * The metadata will be serialized prior to storing it in the database.
  664. *
  665. * @since 4.8.0
  666. *
  667. * @param array $meta Signup meta data. Default empty array.
  668. * @param string $user The user's requested login name.
  669. * @param string $user_email The user's email address.
  670. * @param string $key The user's activation key.
  671. */
  672. $meta = apply_filters( 'signup_user_meta', $meta, $user, $user_email, $key );
  673. $wpdb->insert( $wpdb->signups, array(
  674. 'domain' => '',
  675. 'path' => '',
  676. 'title' => '',
  677. 'user_login' => $user,
  678. 'user_email' => $user_email,
  679. 'registered' => current_time('mysql', true),
  680. 'activation_key' => $key,
  681. 'meta' => serialize( $meta )
  682. ) );
  683. /**
  684. * Fires after a user's signup information has been written to the database.
  685. *
  686. * @since 4.4.0
  687. *
  688. * @param string $user The user's requested login name.
  689. * @param string $user_email The user's email address.
  690. * @param string $key The user's activation key.
  691. * @param array $meta Signup meta data. Default empty array.
  692. */
  693. do_action( 'after_signup_user', $user, $user_email, $key, $meta );
  694. }
  695. /**
  696. * Notify user of signup success.
  697. *
  698. * This is the notification function used when site registration
  699. * is enabled.
  700. *
  701. * Filter {@see 'wpmu_signup_blog_notification'} to bypass this function or
  702. * replace it with your own notification behavior.
  703. *
  704. * Filter {@see 'wpmu_signup_blog_notification_email'} and
  705. * {@see 'wpmu_signup_blog_notification_subject'} to change the content
  706. * and subject line of the email sent to newly registered users.
  707. *
  708. * @since MU
  709. *
  710. * @param string $domain The new blog domain.
  711. * @param string $path The new blog path.
  712. * @param string $title The site title.
  713. * @param string $user_login The user's login name.
  714. * @param string $user_email The user's email address.
  715. * @param string $key The activation key created in wpmu_signup_blog()
  716. * @param array $meta Optional. Signup meta data. By default, contains the requested privacy setting and lang_id.
  717. * @return bool
  718. */
  719. function wpmu_signup_blog_notification( $domain, $path, $title, $user_login, $user_email, $key, $meta = array() ) {
  720. /**
  721. * Filters whether to bypass the new site email notification.
  722. *
  723. * @since MU
  724. *
  725. * @param string|bool $domain Site domain.
  726. * @param string $path Site path.
  727. * @param string $title Site title.
  728. * @param string $user_login User login name.
  729. * @param string $user_email User email address.
  730. * @param string $key Activation key created in wpmu_signup_blog().
  731. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  732. */
  733. if ( ! apply_filters( 'wpmu_signup_blog_notification', $domain, $path, $title, $user_login, $user_email, $key, $meta ) ) {
  734. return false;
  735. }
  736. // Send email with activation link.
  737. if ( !is_subdomain_install() || get_current_network_id() != 1 )
  738. $activate_url = network_site_url("wp-activate.php?key=$key");
  739. else
  740. $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
  741. $activate_url = esc_url($activate_url);
  742. $admin_email = get_site_option( 'admin_email' );
  743. if ( $admin_email == '' )
  744. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  745. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  746. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  747. $user = get_user_by( 'login', $user_login );
  748. $switched_locale = switch_to_locale( get_user_locale( $user ) );
  749. $message = sprintf(
  750. /**
  751. * Filters the message content of the new blog notification email.
  752. *
  753. * Content should be formatted for transmission via wp_mail().
  754. *
  755. * @since MU
  756. *
  757. * @param string $content Content of the notification email.
  758. * @param string $domain Site domain.
  759. * @param string $path Site path.
  760. * @param string $title Site title.
  761. * @param string $user_login User login name.
  762. * @param string $user_email User email address.
  763. * @param string $key Activation key created in wpmu_signup_blog().
  764. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  765. */
  766. apply_filters( 'wpmu_signup_blog_notification_email',
  767. __( "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" ),
  768. $domain, $path, $title, $user_login, $user_email, $key, $meta
  769. ),
  770. $activate_url,
  771. esc_url( "http://{$domain}{$path}" ),
  772. $key
  773. );
  774. // TODO: Don't hard code activation link.
  775. $subject = sprintf(
  776. /**
  777. * Filters the subject of the new blog notification email.
  778. *
  779. * @since MU
  780. *
  781. * @param string $subject Subject of the notification email.
  782. * @param string $domain Site domain.
  783. * @param string $path Site path.
  784. * @param string $title Site title.
  785. * @param string $user_login User login name.
  786. * @param string $user_email User email address.
  787. * @param string $key Activation key created in wpmu_signup_blog().
  788. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  789. */
  790. apply_filters( 'wpmu_signup_blog_notification_subject',
  791. /* translators: New site notification email subject. 1: Network name, 2: New site URL */
  792. _x( '[%1$s] Activate %2$s', 'New site notification email subject' ),
  793. $domain, $path, $title, $user_login, $user_email, $key, $meta
  794. ),
  795. $from_name,
  796. esc_url( 'http://' . $domain . $path )
  797. );
  798. wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  799. if ( $switched_locale ) {
  800. restore_previous_locale();
  801. }
  802. return true;
  803. }
  804. /**
  805. * Notify user of signup success.
  806. *
  807. * This is the notification function used when no new site has
  808. * been requested.
  809. *
  810. * Filter {@see 'wpmu_signup_user_notification'} to bypass this function or
  811. * replace it with your own notification behavior.
  812. *
  813. * Filter {@see 'wpmu_signup_user_notification_email'} and
  814. * {@see 'wpmu_signup_user_notification_subject'} to change the content
  815. * and subject line of the email sent to newly registered users.
  816. *
  817. * @since MU
  818. *
  819. * @param string $user_login The user's login name.
  820. * @param string $user_email The user's email address.
  821. * @param string $key The activation key created in wpmu_signup_user()
  822. * @param array $meta Optional. Signup meta data. Default empty array.
  823. * @return bool
  824. */
  825. function wpmu_signup_user_notification( $user_login, $user_email, $key, $meta = array() ) {
  826. /**
  827. * Filters whether to bypass the email notification for new user sign-up.
  828. *
  829. * @since MU
  830. *
  831. * @param string $user_login User login name.
  832. * @param string $user_email User email address.
  833. * @param string $key Activation key created in wpmu_signup_user().
  834. * @param array $meta Signup meta data. Default empty array.
  835. */
  836. if ( ! apply_filters( 'wpmu_signup_user_notification', $user_login, $user_email, $key, $meta ) )
  837. return false;
  838. $user = get_user_by( 'login', $user_login );
  839. $switched_locale = switch_to_locale( get_user_locale( $user ) );
  840. // Send email with activation link.
  841. $admin_email = get_site_option( 'admin_email' );
  842. if ( $admin_email == '' )
  843. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  844. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  845. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  846. $message = sprintf(
  847. /**
  848. * Filters the content of the notification email for new user sign-up.
  849. *
  850. * Content should be formatted for transmission via wp_mail().
  851. *
  852. * @since MU
  853. *
  854. * @param string $content Content of the notification email.
  855. * @param string $user_login User login name.
  856. * @param string $user_email User email address.
  857. * @param string $key Activation key created in wpmu_signup_user().
  858. * @param array $meta Signup meta data. Default empty array.
  859. */
  860. apply_filters( 'wpmu_signup_user_notification_email',
  861. __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ),
  862. $user_login, $user_email, $key, $meta
  863. ),
  864. site_url( "wp-activate.php?key=$key" )
  865. );
  866. // TODO: Don't hard code activation link.
  867. $subject = sprintf(
  868. /**
  869. * Filters the subject of the notification email of new user signup.
  870. *
  871. * @since MU
  872. *
  873. * @param string $subject Subject of the notification email.
  874. * @param string $user_login User login name.
  875. * @param string $user_email User email address.
  876. * @param string $key Activation key created in wpmu_signup_user().
  877. * @param array $meta Signup meta data. Default empty array.
  878. */
  879. apply_filters( 'wpmu_signup_user_notification_subject',
  880. /* translators: New user notification email subject. 1: Network name, 2: New user login */
  881. _x( '[%1$s] Activate %2$s', 'New user notification email subject' ),
  882. $user_login, $user_email, $key, $meta
  883. ),
  884. $from_name,
  885. $user_login
  886. );
  887. wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  888. if ( $switched_locale ) {
  889. restore_previous_locale();
  890. }
  891. return true;
  892. }
  893. /**
  894. * Activate a signup.
  895. *
  896. * Hook to {@see 'wpmu_activate_user'} or {@see 'wpmu_activate_blog'} for events
  897. * that should happen only when users or sites are self-created (since
  898. * those actions are not called when users and sites are created
  899. * by a Super Admin).
  900. *
  901. * @since MU
  902. *
  903. * @global wpdb $wpdb WordPress database abstraction object.
  904. *
  905. * @param string $key The activation key provided to the user.
  906. * @return array|WP_Error An array containing information about the activated user and/or blog
  907. */
  908. function wpmu_activate_signup($key) {
  909. global $wpdb;
  910. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
  911. if ( empty( $signup ) )
  912. return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
  913. if ( $signup->active ) {
  914. if ( empty( $signup->domain ) )
  915. return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
  916. else
  917. return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
  918. }
  919. $meta = maybe_unserialize($signup->meta);
  920. $password = wp_generate_password( 12, false );
  921. $user_id = username_exists($signup->user_login);
  922. if ( ! $user_id )
  923. $user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email);
  924. else
  925. $user_already_exists = true;
  926. if ( ! $user_id )
  927. return new WP_Error('create_user', __('Could not create user'), $signup);
  928. $now = current_time('mysql', true);
  929. if ( empty($signup->domain) ) {
  930. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  931. if ( isset( $user_already_exists ) )
  932. return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
  933. /**
  934. * Fires immediately after a new user is activated.
  935. *
  936. * @since MU
  937. *
  938. * @param int $user_id User ID.
  939. * @param int $password User password.
  940. * @param array $meta Signup meta data.
  941. */
  942. do_action( 'wpmu_activate_user', $user_id, $password, $meta );
  943. return array( 'user_id' => $user_id, 'password' => $password, 'meta' => $meta );
  944. }
  945. $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
  946. // TODO: What to do if we create a user but cannot create a blog?
  947. if ( is_wp_error($blog_id) ) {
  948. // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  949. // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  950. if ( 'blog_taken' == $blog_id->get_error_code() ) {
  951. $blog_id->add_data( $signup );
  952. $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
  953. }
  954. return $blog_id;
  955. }
  956. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  957. /**
  958. * Fires immediately after a site is activated.
  959. *
  960. * @since MU
  961. *
  962. * @param int $blog_id Blog ID.
  963. * @param int $user_id User ID.
  964. * @param int $password User password.
  965. * @param string $signup_title Site title.
  966. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  967. */
  968. do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta );
  969. return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
  970. }
  971. /**
  972. * Create a user.
  973. *
  974. * This function runs when a user self-registers as well as when
  975. * a Super Admin creates a new user. Hook to {@see 'wpmu_new_user'} for events
  976. * that should affect all new users, but only on Multisite (otherwise
  977. * use {@see'user_register'}).
  978. *
  979. * @since MU
  980. *
  981. * @param string $user_name The new user's login name.
  982. * @param string $password The new user's password.
  983. * @param string $email The new user's email address.
  984. * @return int|false Returns false on failure, or int $user_id on success
  985. */
  986. function wpmu_create_user( $user_name, $password, $email ) {
  987. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  988. $user_id = wp_create_user( $user_name, $password, $email );
  989. if ( is_wp_error( $user_id ) )
  990. return false;
  991. // Newly created users have no roles or caps until they are added to a blog.
  992. delete_user_option( $user_id, 'capabilities' );
  993. delete_user_option( $user_id, 'user_level' );
  994. /**
  995. * Fires immediately after a new user is created.
  996. *
  997. * @since MU
  998. *
  999. * @param int $user_id User ID.
  1000. */
  1001. do_action( 'wpmu_new_user', $user_id );
  1002. return $user_id;
  1003. }
  1004. /**
  1005. * Create a site.
  1006. *
  1007. * This function runs when a user self-registers a new site as well
  1008. * as when a Super Admin creates a new site. Hook to {@see 'wpmu_new_blog'}
  1009. * for events that should affect all new sites.
  1010. *
  1011. * On subdirectory installs, $domain is the same as the main site's
  1012. * domain, and the path is the subdirectory name (eg 'example.com'
  1013. * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
  1014. * root domain (eg 'blog1.example.com'), and $path is '/'.
  1015. *
  1016. * @since MU
  1017. *
  1018. * @param string $domain The new site's domain.
  1019. * @param string $path The new site's path.
  1020. * @param string $title The new site's title.
  1021. * @param int $user_id The user ID of the new site's admin.
  1022. * @param array $meta Optional. Used to set initial site options.
  1023. * @param int $site_id Optional. Only relevant on multi-network installs.
  1024. * @return int|WP_Error Returns WP_Error object on failure, int $blog_id on success
  1025. */
  1026. function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $site_id = 1 ) {
  1027. $defaults = array(
  1028. 'public' => 0,
  1029. 'WPLANG' => get_site_option( 'WPLANG' ),
  1030. );
  1031. $meta = wp_parse_args( $meta, $defaults );
  1032. $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
  1033. if ( is_subdomain_install() )
  1034. $domain = str_replace( '@', '', $domain );
  1035. $title = strip_tags( $title );
  1036. $user_id = (int) $user_id;
  1037. if ( empty($path) )
  1038. $path = '/';
  1039. // Check if the domain has been used already. We should return an error message.
  1040. if ( domain_exists($domain, $path, $site_id) )
  1041. return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
  1042. if ( ! wp_installing() ) {
  1043. wp_installing( true );
  1044. }
  1045. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  1046. return new WP_Error('insert_blog', __('Could not create site.'));
  1047. switch_to_blog($blog_id);
  1048. install_blog($blog_id, $title);
  1049. wp_install_defaults($user_id);
  1050. add_user_to_blog($blog_id, $user_id, 'administrator');
  1051. foreach ( $meta as $key => $value ) {
  1052. if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) )
  1053. update_blog_status( $blog_id, $key, $value );
  1054. else
  1055. update_option( $key, $value );
  1056. }
  1057. update_option( 'blog_public', (int) $meta['public'] );
  1058. if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) )
  1059. update_user_meta( $user_id, 'primary_blog', $blog_id );
  1060. restore_current_blog();
  1061. /**
  1062. * Fires immediately after a new site is created.
  1063. *
  1064. * @since MU
  1065. *
  1066. * @param int $blog_id Blog ID.
  1067. * @param int $user_id User ID.
  1068. * @param string $domain Site domain.
  1069. * @param string $path Site path.
  1070. * @param int $site_id Site ID. Only relevant on multi-network installs.
  1071. * @param array $meta Meta data. Used to set initial site options.
  1072. */
  1073. do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
  1074. wp_cache_set( 'last_changed', microtime(), 'sites' );
  1075. return $blog_id;
  1076. }
  1077. /**
  1078. * Notifies the network admin that a new site has been activated.
  1079. *
  1080. * Filter {@see 'newblog_notify_siteadmin'} to change the content of
  1081. * the notification email.
  1082. *
  1083. * @since MU
  1084. *
  1085. * @param int $blog_id The new site's ID.
  1086. * @param string $deprecated Not used.
  1087. * @return bool
  1088. */
  1089. function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  1090. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  1091. return false;
  1092. $email = get_site_option( 'admin_email' );
  1093. if ( is_email($email) == false )
  1094. return false;
  1095. $options_site_url = esc_url(network_admin_url('settings.php'));
  1096. switch_to_blog( $blog_id );
  1097. $blogname = get_option( 'blogname' );
  1098. $siteurl = site_url();
  1099. restore_current_blog();
  1100. /* translators: New site notification email. 1: Site URL, 2: User IP address, 3: Settings screen URL */
  1101. $msg = sprintf( __( 'New Site: %1$s
  1102. URL: %2$s
  1103. Remote IP: %3$s
  1104. Disable these notifications: %4$s' ), $blogname, $siteurl, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
  1105. /**
  1106. * Filters the message body of the new site activation email sent
  1107. * to the network administrator.
  1108. *
  1109. * @since MU
  1110. *
  1111. * @param string $msg Email body.
  1112. */
  1113. $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
  1114. wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
  1115. return true;
  1116. }
  1117. /**
  1118. * Notifies the network admin that a new user has been activated.
  1119. *
  1120. * Filter {@see 'newuser_notify_siteadmin'} to change the content of
  1121. * the notification email.
  1122. *
  1123. * @since MU
  1124. *
  1125. * @param int $user_id The new user's ID.
  1126. * @return bool
  1127. */
  1128. function newuser_notify_siteadmin( $user_id ) {
  1129. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  1130. return false;
  1131. $email = get_site_option( 'admin_email' );
  1132. if ( is_email($email) == false )
  1133. return false;
  1134. $user = get_userdata( $user_id );
  1135. $options_site_url = esc_url(network_admin_url('settings.php'));
  1136. /* translators: New user notification email. 1: User login, 2: User IP address, 3: Settings screen URL */
  1137. $msg = sprintf(__('New User: %1$s
  1138. Remote IP: %2$s
  1139. Disable these notifications: %3$s'), $user->user_login, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
  1140. /**
  1141. * Filters the message body of the new user activation email sent
  1142. * to the network administrator.
  1143. *
  1144. * @since MU
  1145. *
  1146. * @param string $msg Email body.
  1147. * @param WP_User $user WP_User instance of the new user.
  1148. */
  1149. $msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
  1150. wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
  1151. return true;
  1152. }
  1153. /**
  1154. * Check whether a blogname is already taken.
  1155. *
  1156. * Used during the new site registration process to ensure
  1157. * that each blogname is unique.
  1158. *
  1159. * @since MU
  1160. *
  1161. * @global wpdb $wpdb WordPress database abstraction object.
  1162. *
  1163. * @param string $domain The domain to be checked.
  1164. * @param string $path The path to be checked.
  1165. * @param int $site_id Optional. Relevant only on multi-network installs.
  1166. * @return int
  1167. */
  1168. function domain_exists($domain, $path, $site_id = 1) {
  1169. $path = trailingslashit( $path );
  1170. $args = array(
  1171. 'network_id' => $site_id,
  1172. 'domain' => $domain,
  1173. 'path' => $path,
  1174. 'fields' => 'ids',
  1175. );
  1176. $result = get_sites( $args );
  1177. $result = array_shift( $result );
  1178. /**
  1179. * Filters whether a blogname is taken.
  1180. *
  1181. * @since 3.5.0
  1182. *
  1183. * @param int|null $result The blog_id if the blogname exists, null otherwise.
  1184. * @param string $domain Domain to be checked.
  1185. * @param string $path Path to be checked.
  1186. * @param int $site_id Site ID. Relevant only on multi-network installs.
  1187. */
  1188. return apply_filters( 'domain_exists', $result, $domain, $path, $site_id );
  1189. }
  1190. /**
  1191. * Store basic site info in the blogs table.
  1192. *
  1193. * This function creates a row in the wp_blogs table and returns
  1194. * the new blog's ID. It is the first step in creating a new blog.
  1195. *
  1196. * @since MU
  1197. *
  1198. * @global wpdb $wpdb WordPress database abstraction object.
  1199. *
  1200. * @param string $domain The domain of the new site.
  1201. * @param string $path The path of the new site.
  1202. * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1.
  1203. * @return int|false The ID of the new row
  1204. */
  1205. function insert_blog($domain, $path, $site_id) {
  1206. global $wpdb;
  1207. $path = trailingslashit($path);
  1208. $site_id = (int) $site_id;
  1209. $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
  1210. if ( ! $result )
  1211. return false;
  1212. $blog_id = $wpdb->insert_id;
  1213. refresh_blog_details( $blog_id );
  1214. wp_maybe_update_network_site_counts( $site_id );
  1215. return $blog_id;
  1216. }
  1217. /**
  1218. * Install an empty blog.
  1219. *
  1220. * Creates the new blog tables and options. If calling this function
  1221. * directly, be sure to use switch_to_blog() first, so that $wpdb
  1222. * points to the new blog.
  1223. *
  1224. * @since MU
  1225. *
  1226. * @global wpdb $wpdb
  1227. * @global WP_Roles $wp_roles
  1228. *
  1229. * @param int $blog_id The value returned by insert_blog().
  1230. * @param string $blog_title The title of the new site.
  1231. */
  1232. function install_blog( $blog_id, $blog_title = '' ) {
  1233. global $wpdb, $wp_roles;
  1234. // Cast for security
  1235. $blog_id = (int) $blog_id;
  1236. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1237. $suppress = $wpdb->suppress_errors();
  1238. if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) )
  1239. 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>' );
  1240. $wpdb->suppress_errors( $suppress );
  1241. $url = get_blogaddress_by_id( $blog_id );
  1242. // Set everything up
  1243. make_db_current_silent( 'blog' );
  1244. populate_options();
  1245. populate_roles();
  1246. // populate_roles() clears previous role definitions so we start over.
  1247. $wp_roles = new WP_Roles();
  1248. $siteurl = $home = untrailingslashit( $url );
  1249. if ( ! is_subdomain_install() ) {
  1250. if ( 'https' === parse_url( get_site_option( 'siteurl' ), PHP_URL_SCHEME ) ) {
  1251. $siteurl = set_url_scheme( $siteurl, 'https' );
  1252. }
  1253. if ( 'https' === parse_url( get_home_url( get_network()->site_id ), PHP_URL_SCHEME ) ) {
  1254. $home = set_url_scheme( $home, 'https' );
  1255. }
  1256. }
  1257. update_option( 'siteurl', $siteurl );
  1258. update_option( 'home', $home );
  1259. if ( get_site_option( 'ms_files_rewriting' ) )
  1260. update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
  1261. else
  1262. update_option( 'upload_path', get_blog_option( get_network()->site_id, 'upload_path' ) );
  1263. update_option( 'blogname', wp_unslash( $blog_title ) );
  1264. update_option( 'admin_email', '' );
  1265. // remove all perms
  1266. $table_prefix = $wpdb->get_blog_prefix();
  1267. delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
  1268. delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
  1269. }
  1270. /**
  1271. * Set blog defaults.
  1272. *
  1273. * This function creates a row in the wp_blogs table.
  1274. *
  1275. * @since MU
  1276. * @deprecated MU
  1277. * @deprecated Use wp_install_defaults()
  1278. *
  1279. * @global wpdb $wpdb WordPress database abstraction object.
  1280. *
  1281. * @param int $blog_id Ignored in this function.
  1282. * @param int $user_id
  1283. */
  1284. function install_blog_defaults($blog_id, $user_id) {
  1285. global $wpdb;
  1286. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1287. $suppress = $wpdb->suppress_errors();
  1288. wp_install_defaults($user_id);
  1289. $wpdb->suppress_errors( $suppress );
  1290. }
  1291. /**
  1292. * Notify a user that their blog activation has been successful.
  1293. *
  1294. * Filter {@see 'wpmu_welcome_notification'} to disable or bypass.
  1295. *
  1296. * Filter {@see 'update_welcome_email'} and {@see 'update_welcome_subject'} to
  1297. * modify the content and subject line of the notification email.
  1298. *
  1299. * @since MU
  1300. *
  1301. * @param int $blog_id Blog ID.
  1302. * @param int $user_id User ID.
  1303. * @param string $password User password.
  1304. * @param string $title Site title.
  1305. * @param array $meta Optional. Signup meta data. By default, contains the requested privacy setting and lang_id.
  1306. * @return bool
  1307. */
  1308. function wpmu_welcome_notification( $blog_id, $user_id, $password, $title, $meta = array() ) {
  1309. $current_network = get_network();
  1310. /**
  1311. * Filters whether to bypass the welcome email after site activation.
  1312. *
  1313. * Returning false disables the welcome email.
  1314. *
  1315. * @since MU
  1316. *
  1317. * @param int|bool $blog_id Blog ID.
  1318. * @param int $user_id User ID.
  1319. * @param string $password User password.
  1320. * @param string $title Site title.
  1321. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  1322. */
  1323. if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) )
  1324. return false;
  1325. $user = get_userdata( $user_id );
  1326. $switched_locale = switch_to_locale( get_user_locale( $user ) );
  1327. $welcome_email = get_site_option( 'welcome_email' );
  1328. if ( $welcome_email == false ) {
  1329. /* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
  1330. $welcome_email = __( 'Howdy USERNAME,
  1331. Your new SITE_NAME site has been successfully set up at:
  1332. BLOG_URL
  1333. You can log in to the administrator account with the following information:
  1334. Username: USERNAME
  1335. Password: PASSWORD
  1336. Log in here: BLOG_URLwp-login.php
  1337. We hope you enjoy your new site. Thanks!
  1338. --The Team @ SITE_NAME' );
  1339. }
  1340. $url = get_blogaddress_by_id($blog_id);
  1341. $welcome_email = str_replace( 'SITE_NAME', $current_network->site_name, $welcome_email );
  1342. $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
  1343. $welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
  1344. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1345. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1346. /**
  1347. * Filters the content of the welcome email after site activation.
  1348. *
  1349. * Content should be formatted for transmission via wp_mail().
  1350. *
  1351. * @since MU
  1352. *
  1353. * @param string $welcome_email Message body of the email.
  1354. * @param int $blog_id Blog ID.
  1355. * @param int $user_id User ID.
  1356. * @param string $password User password.
  1357. * @param string $title Site title.
  1358. * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
  1359. */
  1360. $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta );
  1361. $admin_email = get_site_option( 'admin_email' );
  1362. if ( $admin_email == '' )
  1363. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1364. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1365. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1366. $message = $welcome_email;
  1367. if ( empty( $current_network->site_name ) )
  1368. $current_network->site_name = 'WordPress';
  1369. /* translators: New site notification email subject. 1: Network name, 2: New site name */
  1370. $subject = __( 'New %1$s Site: %2$s' );
  1371. /**
  1372. * Filters the subject of the welcome email after site activation.
  1373. *
  1374. * @since MU
  1375. *
  1376. * @param string $subject Subject of the email.
  1377. */
  1378. $subject = apply_filters( 'update_welcome_subject', sprintf( $subject, $current_network->site_name, wp_unslash( $title ) ) );
  1379. wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  1380. if ( $switched_locale ) {
  1381. restore_previous_locale();
  1382. }
  1383. return true;
  1384. }
  1385. /**
  1386. * Notify a user that their account activation has been successful.
  1387. *
  1388. * Filter {@see 'wpmu_welcome_user_notification'} to disable or bypass.
  1389. *
  1390. * Filter {@see 'update_welcome_user_email'} and {@see 'update_welcome_user_subject'} to
  1391. * modify the content and subject line of the notification email.
  1392. *
  1393. * @since MU
  1394. *
  1395. * @param int $user_id User ID.
  1396. * @param string $password User password.
  1397. * @param array $meta Optional. Signup meta data. Default empty array.
  1398. * @return bool
  1399. */
  1400. function wpmu_welcome_user_notification( $user_id, $password, $meta = array() ) {
  1401. $current_network = get_network();
  1402. /**
  1403. * Filters whether to bypass the welcome email after user activation.
  1404. *
  1405. * Returning false disables the welcome email.
  1406. *
  1407. * @since MU
  1408. *
  1409. * @param int $user_id User ID.
  1410. * @param string $password User password.
  1411. * @param array $meta Signup meta data. Default empty array.
  1412. */
  1413. if ( ! apply_filters( 'wpmu_welcome_user_notification', $user_id, $password, $meta ) )
  1414. return false;
  1415. $welcome_email = get_site_option( 'welcome_user_email' );
  1416. $user = get_userdata( $user_id );
  1417. $switched_locale = switch_to_locale( get_user_locale( $user ) );
  1418. /**
  1419. * Filters the content of the welcome email after user activation.
  1420. *
  1421. * Content should be formatted for transmission via wp_mail().
  1422. *
  1423. * @since MU
  1424. *
  1425. * @param string $welcome_email The message body of the account activation success email.
  1426. * @param int $user_id User ID.
  1427. * @param string $password User password.
  1428. * @param array $meta Signup meta data. Default empty array.
  1429. */
  1430. $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta );
  1431. $welcome_email = str_replace( 'SITE_NAME', $current_network->site_name, $welcome_email );
  1432. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1433. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1434. $welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
  1435. $admin_email = get_site_option( 'admin_email' );
  1436. if ( $admin_email == '' )
  1437. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1438. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1439. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1440. $message = $welcome_email;
  1441. if ( empty( $current_network->site_name ) )
  1442. $current_network->site_name = 'WordPress';
  1443. /* translators: New user notification email subject. 1: Network name, 2: New user login */
  1444. $subject = __( 'New %1$s User: %2$s' );
  1445. /**
  1446. * Filters the subject of the welcome email after user activation.
  1447. *
  1448. * @since MU
  1449. *
  1450. * @param string $subject Subject of the email.
  1451. */
  1452. $subject = apply_filters( 'update_welcome_user_subject', sprintf( $subject, $current_network->site_name, $user->user_login) );
  1453. wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  1454. if ( $switched_locale ) {
  1455. restore_previous_locale();
  1456. }
  1457. return true;
  1458. }
  1459. /**
  1460. * Get the current network.
  1461. *
  1462. * Returns an object containing the 'id', 'domain', 'path', and 'site_name'
  1463. * properties of the network being viewed.
  1464. *
  1465. * @see wpmu_current_site()
  1466. *
  1467. * @since MU
  1468. *
  1469. * @global WP_Network $current_site
  1470. *
  1471. * @return WP_Network
  1472. */
  1473. function get_current_site() {
  1474. global $current_site;
  1475. return $current_site;
  1476. }
  1477. /**
  1478. * Get a user's most recent post.
  1479. *
  1480. * Walks through each of a user's blogs to find the post with
  1481. * the most recent post_date_gmt.
  1482. *
  1483. * @since MU
  1484. *
  1485. * @global wpdb $wpdb WordPress database abstraction object.
  1486. *
  1487. * @param int $user_id
  1488. * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
  1489. */
  1490. function get_most_recent_post_of_user( $user_id ) {
  1491. global $wpdb;
  1492. $user_blogs = get_blogs_of_user( (int) $user_id );
  1493. $most_recent_post = array();
  1494. // Walk through each blog and get the most recent post
  1495. // published by $user_id
  1496. foreach ( (array) $user_blogs as $blog ) {
  1497. $prefix = $wpdb->get_blog_prefix( $blog->userblog_id );
  1498. $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);
  1499. // Make sure we found a post
  1500. if ( isset($recent_post['ID']) ) {
  1501. $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
  1502. // If this is the first post checked or if this post is
  1503. // newer than the current recent post, make it the new
  1504. // most recent post.
  1505. if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
  1506. $most_recent_post = array(
  1507. 'blog_id' => $blog->userblog_id,
  1508. 'post_id' => $recent_post['ID'],
  1509. 'post_date_gmt' => $recent_post['post_date_gmt'],
  1510. 'post_gmt_ts' => $post_gmt_ts
  1511. );
  1512. }
  1513. }
  1514. }
  1515. return $most_recent_post;
  1516. }
  1517. // Misc functions
  1518. /**
  1519. * Get the size of a directory.
  1520. *
  1521. * A helper function that is used primarily to check whether
  1522. * a blog has exceeded its allowed upload space.
  1523. *
  1524. * @since MU
  1525. *
  1526. * @param string $directory Full path of a directory.
  1527. * @return int Size of the directory in MB.
  1528. */
  1529. function get_dirsize( $directory ) {
  1530. $dirsize = get_transient( 'dirsize_cache' );
  1531. if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
  1532. return $dirsize[ $directory ][ 'size' ];
  1533. if ( ! is_array( $dirsize ) )
  1534. $dirsize = array();
  1535. // Exclude individual site directories from the total when checking the main site,
  1536. // as they are subdirectories and should not be counted.
  1537. if ( is_main_site() ) {
  1538. $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory, $directory . '/sites' );
  1539. } else {
  1540. $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
  1541. }
  1542. set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
  1543. return $dirsize[ $directory ][ 'size' ];
  1544. }
  1545. /**
  1546. * Get the size of a directory recursively.
  1547. *
  1548. * Used by get_dirsize() to get a directory's size when it contains
  1549. * other directories.
  1550. *
  1551. * @since MU
  1552. * @since 4.3.0 $exclude parameter added.
  1553. *
  1554. * @param string $directory Full path of a directory.
  1555. * @param string $exclude Optional. Full path of a subdirectory to exclude from the total.
  1556. * @return int|false Size in MB if a valid directory. False if not.
  1557. */
  1558. function recurse_dirsize( $directory, $exclude = null ) {
  1559. $size = 0;
  1560. $directory = untrailingslashit( $directory );
  1561. if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) {
  1562. return false;
  1563. }
  1564. if ($handle = opendir($directory)) {
  1565. while(($file = readdir($handle)) !== false) {
  1566. $path = $directory.'/'.$file;
  1567. if ($file != '.' && $file != '..') {
  1568. if (is_file($path)) {
  1569. $size += filesize($path);
  1570. } elseif (is_dir($path)) {
  1571. $handlesize = recurse_dirsize( $path, $exclude );
  1572. if ($handlesize > 0)
  1573. $size += $handlesize;
  1574. }
  1575. }
  1576. }
  1577. closedir($handle);
  1578. }
  1579. return $size;
  1580. }
  1581. /**
  1582. * Check an array of MIME types against a whitelist.
  1583. *
  1584. * WordPress ships with a set of allowed upload filetypes,
  1585. * which is defined in wp-includes/functions.php in
  1586. * get_allowed_mime_types(). This function is used to filter
  1587. * that list against the filetype whitelist provided by Multisite
  1588. * Super Admins at wp-admin/network/settings.php.
  1589. *
  1590. * @since MU
  1591. *
  1592. * @param array $mimes
  1593. * @return array
  1594. */
  1595. function check_upload_mimes( $mimes ) {
  1596. $site_exts = explode( ' ', get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ) );
  1597. $site_mimes = array();
  1598. foreach ( $site_exts as $ext ) {
  1599. foreach ( $mimes as $ext_pattern => $mime ) {
  1600. if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
  1601. $site_mimes[$ext_pattern] = $mime;
  1602. }
  1603. }
  1604. return $site_mimes;
  1605. }
  1606. /**
  1607. * Update a blog's post count.
  1608. *
  1609. * WordPress MS stores a blog's post count as an option so as
  1610. * to avoid extraneous COUNTs when a blog's details are fetched
  1611. * with get_site(). This function is called when posts are published
  1612. * or unpublished to make sure the count stays current.
  1613. *
  1614. * @since MU
  1615. *
  1616. * @global wpdb $wpdb WordPress database abstraction object.
  1617. *
  1618. * @param string $deprecated Not used.
  1619. */
  1620. function update_posts_count( $deprecated = '' ) {
  1621. global $wpdb;
  1622. update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
  1623. }
  1624. /**
  1625. * Logs user registrations.
  1626. *
  1627. * @since MU
  1628. *
  1629. * @global wpdb $wpdb WordPress database abstraction object.
  1630. *
  1631. * @param int $blog_id
  1632. * @param int $user_id
  1633. */
  1634. function wpmu_log_new_registrations( $blog_id, $user_id ) {
  1635. global $wpdb;
  1636. $user = get_userdata( (int) $user_id );
  1637. if ( $user )
  1638. $wpdb->insert( $wpdb->registration_log, array('email' => $user->user_email, 'IP' => preg_replace( '/[^0-9., ]/', '', wp_unslash( $_SERVER['REMOTE_ADDR'] ) ), 'blog_id' => $blog_id, 'date_registered' => current_time('mysql')) );
  1639. }
  1640. /**
  1641. * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
  1642. *
  1643. * @since 3.0.0
  1644. *
  1645. * @see term_id_filter
  1646. *
  1647. * @global wpdb $wpdb WordPress database abstraction object.
  1648. * @staticvar int $global_terms_recurse
  1649. *
  1650. * @param int $term_id An ID for a term on the current blog.
  1651. * @param string $deprecated Not used.
  1652. * @return int An ID from the global terms table mapped from $term_id.
  1653. */
  1654. function global_terms( $term_id, $deprecated = '' ) {
  1655. global $wpdb;
  1656. static $global_terms_recurse = null;
  1657. if ( !global_terms_enabled() )
  1658. return $term_id;
  1659. // prevent a race condition
  1660. $recurse_start = false;
  1661. if ( $global_terms_recurse === null ) {
  1662. $recurse_start = true;
  1663. $global_terms_recurse = 1;
  1664. } elseif ( 10 < $global_terms_recurse++ ) {
  1665. return $term_id;
  1666. }
  1667. $term_id = intval( $term_id );
  1668. $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
  1669. $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
  1670. if ( $global_id == null ) {
  1671. $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
  1672. if ( null == $used_global_id ) {
  1673. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1674. $global_id = $wpdb->insert_id;
  1675. if ( empty( $global_id ) )
  1676. return $term_id;
  1677. } else {
  1678. $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
  1679. $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
  1680. $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
  1681. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1682. $global_id = $wpdb->insert_id;
  1683. }
  1684. } elseif ( $global_id != $term_id ) {
  1685. $local_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
  1686. if ( null != $local_id ) {
  1687. global_terms( $local_id );
  1688. if ( 10 < $global_terms_recurse ) {
  1689. $global_id = $term_id;
  1690. }
  1691. }
  1692. }
  1693. if ( $global_id != $term_id ) {
  1694. if ( get_option( 'default_category' ) == $term_id )
  1695. update_option( 'default_category', $global_id );
  1696. $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
  1697. $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
  1698. $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
  1699. clean_term_cache($term_id);
  1700. }
  1701. if ( $recurse_start )
  1702. $global_terms_recurse = null;
  1703. return $global_id;
  1704. }
  1705. /**
  1706. * Ensure that the current site's domain is listed in the allowed redirect host list.
  1707. *
  1708. * @see wp_validate_redirect()
  1709. * @since MU
  1710. *
  1711. * @param array|string $deprecated Not used.
  1712. * @return array The current site's domain
  1713. */
  1714. function redirect_this_site( $deprecated = '' ) {
  1715. return array( get_network()->domain );
  1716. }
  1717. /**
  1718. * Check whether an upload is too big.
  1719. *
  1720. * @since MU
  1721. *
  1722. * @blessed
  1723. *
  1724. * @param array $upload
  1725. * @return string|array If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
  1726. */
  1727. function upload_is_file_too_big( $upload ) {
  1728. if ( ! is_array( $upload ) || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) )
  1729. return $upload;
  1730. if ( strlen( $upload['bits'] ) > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
  1731. return sprintf( __( 'This file is too big. Files must be less than %d KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ) );
  1732. }
  1733. return $upload;
  1734. }
  1735. /**
  1736. * Add a nonce field to the signup page.
  1737. *
  1738. * @since MU
  1739. */
  1740. function signup_nonce_fields() {
  1741. $id = mt_rand();
  1742. echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
  1743. wp_nonce_field('signup_form_' . $id, '_signup_form', false);
  1744. }
  1745. /**
  1746. * Process the signup nonce created in signup_nonce_fields().
  1747. *
  1748. * @since MU
  1749. *
  1750. * @param array $result
  1751. * @return array
  1752. */
  1753. function signup_nonce_check( $result ) {
  1754. if ( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
  1755. return $result;
  1756. if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
  1757. wp_die( __( 'Please try again.' ) );
  1758. return $result;
  1759. }
  1760. /**
  1761. * Correct 404 redirects when NOBLOGREDIRECT is defined.
  1762. *
  1763. * @since MU
  1764. */
  1765. function maybe_redirect_404() {
  1766. /**
  1767. * Filters the redirect URL for 404s on the main site.
  1768. *
  1769. * The filter is only evaluated if the NOBLOGREDIRECT constant is defined.
  1770. *
  1771. * @since 3.0.0
  1772. *
  1773. * @param string $no_blog_redirect The redirect URL defined in NOBLOGREDIRECT.
  1774. */
  1775. if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
  1776. if ( $destination == '%siteurl%' )
  1777. $destination = network_home_url();
  1778. wp_redirect( $destination );
  1779. exit();
  1780. }
  1781. }
  1782. /**
  1783. * Add a new user to a blog by visiting /newbloguser/username/.
  1784. *
  1785. * This will only work when the user's details are saved as an option
  1786. * keyed as 'new_user_x', where 'x' is the username of the user to be
  1787. * added, as when a user is invited through the regular WP Add User interface.
  1788. *
  1789. * @since MU
  1790. */
  1791. function maybe_add_existing_user_to_blog() {
  1792. if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) )
  1793. return;
  1794. $parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
  1795. $key = array_pop( $parts );
  1796. if ( $key == '' )
  1797. $key = array_pop( $parts );
  1798. $details = get_option( 'new_user_' . $key );
  1799. if ( !empty( $details ) )
  1800. delete_option( 'new_user_' . $key );
  1801. if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) )
  1802. wp_die( sprintf(__('An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.'), home_url() ) );
  1803. 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' ), array( 'response' => 200 ) );
  1804. }
  1805. /**
  1806. * Add a user to a blog based on details from maybe_add_existing_user_to_blog().
  1807. *
  1808. * @since MU
  1809. *
  1810. * @param array $details
  1811. * @return true|WP_Error|void
  1812. */
  1813. function add_existing_user_to_blog( $details = false ) {
  1814. if ( is_array( $details ) ) {
  1815. $blog_id = get_current_blog_id();
  1816. $result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] );
  1817. /**
  1818. * Fires immediately after an existing user is added to a site.
  1819. *
  1820. * @since MU
  1821. *
  1822. * @param int $user_id User ID.
  1823. * @param mixed $result True on success or a WP_Error object if the user doesn't exist.
  1824. */
  1825. do_action( 'added_existing_user', $details['user_id'], $result );
  1826. return $result;
  1827. }
  1828. }
  1829. /**
  1830. * Adds a newly created user to the appropriate blog
  1831. *
  1832. * To add a user in general, use add_user_to_blog(). This function
  1833. * is specifically hooked into the {@see 'wpmu_activate_user'} action.
  1834. *
  1835. * @since MU
  1836. * @see add_user_to_blog()
  1837. *
  1838. * @param int $user_id
  1839. * @param mixed $password Ignored.
  1840. * @param array $meta
  1841. */
  1842. function add_new_user_to_blog( $user_id, $password, $meta ) {
  1843. if ( !empty( $meta[ 'add_to_blog' ] ) ) {
  1844. $blog_id = $meta[ 'add_to_blog' ];
  1845. $role = $meta[ 'new_role' ];
  1846. remove_user_from_blog($user_id, get_network()->site_id); // remove user from main blog.
  1847. add_user_to_blog( $blog_id, $user_id, $role );
  1848. update_user_meta( $user_id, 'primary_blog', $blog_id );
  1849. }
  1850. }
  1851. /**
  1852. * Correct From host on outgoing mail to match the site domain
  1853. *
  1854. * @since MU
  1855. *
  1856. * @param PHPMailer $phpmailer The PHPMailer instance, passed by reference.
  1857. */
  1858. function fix_phpmailer_messageid( $phpmailer ) {
  1859. $phpmailer->Hostname = get_network()->domain;
  1860. }
  1861. /**
  1862. * Check to see whether a user is marked as a spammer, based on user login.
  1863. *
  1864. * @since MU
  1865. *
  1866. * @param string|WP_User $user Optional. Defaults to current user. WP_User object,
  1867. * or user login name as a string.
  1868. * @return bool
  1869. */
  1870. function is_user_spammy( $user = null ) {
  1871. if ( ! ( $user instanceof WP_User ) ) {
  1872. if ( $user ) {
  1873. $user = get_user_by( 'login', $user );
  1874. } else {
  1875. $user = wp_get_current_user();
  1876. }
  1877. }
  1878. return $user && isset( $user->spam ) && 1 == $user->spam;
  1879. }
  1880. /**
  1881. * Update this blog's 'public' setting in the global blogs table.
  1882. *
  1883. * Public blogs have a setting of 1, private blogs are 0.
  1884. *
  1885. * @since MU
  1886. *
  1887. * @param int $old_value
  1888. * @param int $value The new public value
  1889. */
  1890. function update_blog_public( $old_value, $value ) {
  1891. update_blog_status( get_current_blog_id(), 'public', (int) $value );
  1892. }
  1893. /**
  1894. * Check whether a usermeta key has to do with the current blog.
  1895. *
  1896. * @since MU
  1897. *
  1898. * @global wpdb $wpdb WordPress database abstraction object.
  1899. *
  1900. * @param string $key
  1901. * @param int $user_id Optional. Defaults to current user.
  1902. * @param int $blog_id Optional. Defaults to current blog.
  1903. * @return bool
  1904. */
  1905. function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {
  1906. global $wpdb;
  1907. $current_user = wp_get_current_user();
  1908. if ( $blog_id == 0 ) {
  1909. $blog_id = $wpdb->blogid;
  1910. }
  1911. $local_key = $wpdb->get_blog_prefix( $blog_id ) . $key;
  1912. return isset( $current_user->$local_key );
  1913. }
  1914. /**
  1915. * Check whether users can self-register, based on Network settings.
  1916. *
  1917. * @since MU
  1918. *
  1919. * @return bool
  1920. */
  1921. function users_can_register_signup_filter() {
  1922. $registration = get_site_option('registration');
  1923. return ( $registration == 'all' || $registration == 'user' );
  1924. }
  1925. /**
  1926. * Ensure that the welcome message is not empty. Currently unused.
  1927. *
  1928. * @since MU
  1929. *
  1930. * @param string $text
  1931. * @return string
  1932. */
  1933. function welcome_user_msg_filter( $text ) {
  1934. if ( !$text ) {
  1935. remove_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
  1936. /* translators: Do not translate USERNAME, PASSWORD, LOGINLINK, SITE_NAME: those are placeholders. */
  1937. $text = __( 'Howdy USERNAME,
  1938. Your new account is set up.
  1939. You can log in with the following information:
  1940. Username: USERNAME
  1941. Password: PASSWORD
  1942. LOGINLINK
  1943. Thanks!
  1944. --The Team @ SITE_NAME' );
  1945. update_site_option( 'welcome_user_email', $text );
  1946. }
  1947. return $text;
  1948. }
  1949. /**
  1950. * Whether to force SSL on content.
  1951. *
  1952. * @since 2.8.5
  1953. *
  1954. * @staticvar bool $forced_content
  1955. *
  1956. * @param bool $force
  1957. * @return bool True if forced, false if not forced.
  1958. */
  1959. function force_ssl_content( $force = '' ) {
  1960. static $forced_content = false;
  1961. if ( '' != $force ) {
  1962. $old_forced = $forced_content;
  1963. $forced_content = $force;
  1964. return $old_forced;
  1965. }
  1966. return $forced_content;
  1967. }
  1968. /**
  1969. * Formats a URL to use https.
  1970. *
  1971. * Useful as a filter.
  1972. *
  1973. * @since 2.8.5
  1974. *
  1975. * @param string $url URL
  1976. * @return string URL with https as the scheme
  1977. */
  1978. function filter_SSL( $url ) {
  1979. if ( ! is_string( $url ) )
  1980. return get_bloginfo( 'url' ); // Return home blog url with proper scheme
  1981. if ( force_ssl_content() && is_ssl() )
  1982. $url = set_url_scheme( $url, 'https' );
  1983. return $url;
  1984. }
  1985. /**
  1986. * Schedule update of the network-wide counts for the current network.
  1987. *
  1988. * @since 3.1.0
  1989. */
  1990. function wp_schedule_update_network_counts() {
  1991. if ( !is_main_site() )
  1992. return;
  1993. if ( ! wp_next_scheduled('update_network_counts') && ! wp_installing() )
  1994. wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
  1995. }
  1996. /**
  1997. * Update the network-wide counts for the current network.
  1998. *
  1999. * @since 3.1.0
  2000. * @since 4.8.0 The $network_id parameter has been added.
  2001. *
  2002. * @param int|null $network_id ID of the network. Default is the current network.
  2003. */
  2004. function wp_update_network_counts( $network_id = null ) {
  2005. wp_update_network_user_counts( $network_id );
  2006. wp_update_network_site_counts( $network_id );
  2007. }
  2008. /**
  2009. * Update the count of sites for the current network.
  2010. *
  2011. * If enabled through the {@see 'enable_live_network_counts'} filter, update the sites count
  2012. * on a network when a site is created or its status is updated.
  2013. *
  2014. * @since 3.7.0
  2015. * @since 4.8.0 The $network_id parameter has been added.
  2016. *
  2017. * @param int|null $network_id ID of the network. Default is the current network.
  2018. */
  2019. function wp_maybe_update_network_site_counts( $network_id = null ) {
  2020. $is_small_network = ! wp_is_large_network( 'sites', $network_id );
  2021. /**
  2022. * Filters whether to update network site or user counts when a new site is created.
  2023. *
  2024. * @since 3.7.0
  2025. *
  2026. * @see wp_is_large_network()
  2027. *
  2028. * @param bool $small_network Whether the network is considered small.
  2029. * @param string $context Context. Either 'users' or 'sites'.
  2030. */
  2031. if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) )
  2032. return;
  2033. wp_update_network_site_counts( $network_id );
  2034. }
  2035. /**
  2036. * Update the network-wide users count.
  2037. *
  2038. * If enabled through the {@see 'enable_live_network_counts'} filter, update the users count
  2039. * on a network when a user is created or its status is updated.
  2040. *
  2041. * @since 3.7.0
  2042. * @since 4.8.0 The $network_id parameter has been added.
  2043. *
  2044. * @param int|null $network_id ID of the network. Default is the current network.
  2045. */
  2046. function wp_maybe_update_network_user_counts( $network_id = null ) {
  2047. $is_small_network = ! wp_is_large_network( 'users', $network_id );
  2048. /** This filter is documented in wp-includes/ms-functions.php */
  2049. if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) )
  2050. return;
  2051. wp_update_network_user_counts( $network_id );
  2052. }
  2053. /**
  2054. * Update the network-wide site count.
  2055. *
  2056. * @since 3.7.0
  2057. * @since 4.8.0 The $network_id parameter has been added.
  2058. *
  2059. * @param int|null $network_id ID of the network. Default is the current network.
  2060. */
  2061. function wp_update_network_site_counts( $network_id = null ) {
  2062. $network_id = (int) $network_id;
  2063. if ( ! $network_id ) {
  2064. $network_id = get_current_network_id();
  2065. }
  2066. $count = get_sites( array(
  2067. 'network_id' => $network_id,
  2068. 'spam' => 0,
  2069. 'deleted' => 0,
  2070. 'archived' => 0,
  2071. 'count' => true,
  2072. ) );
  2073. update_network_option( $network_id, 'blog_count', $count );
  2074. }
  2075. /**
  2076. * Update the network-wide user count.
  2077. *
  2078. * @since 3.7.0
  2079. * @since 4.8.0 The $network_id parameter has been added.
  2080. *
  2081. * @global wpdb $wpdb WordPress database abstraction object.
  2082. *
  2083. * @param int|null $network_id ID of the network. Default is the current network.
  2084. */
  2085. function wp_update_network_user_counts( $network_id = null ) {
  2086. global $wpdb;
  2087. $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
  2088. update_network_option( $network_id, 'user_count', $count );
  2089. }
  2090. /**
  2091. * Returns the space used by the current blog.
  2092. *
  2093. * @since 3.5.0
  2094. *
  2095. * @return int Used space in megabytes
  2096. */
  2097. function get_space_used() {
  2098. /**
  2099. * Filters the amount of storage space used by the current site.
  2100. *
  2101. * @since 3.5.0
  2102. *
  2103. * @param int|bool $space_used The amount of used space, in megabytes. Default false.
  2104. */
  2105. $space_used = apply_filters( 'pre_get_space_used', false );
  2106. if ( false === $space_used ) {
  2107. $upload_dir = wp_upload_dir();
  2108. $space_used = get_dirsize( $upload_dir['basedir'] ) / MB_IN_BYTES;
  2109. }
  2110. return $space_used;
  2111. }
  2112. /**
  2113. * Returns the upload quota for the current blog.
  2114. *
  2115. * @since MU
  2116. *
  2117. * @return int Quota in megabytes
  2118. */
  2119. function get_space_allowed() {
  2120. $space_allowed = get_option( 'blog_upload_space' );
  2121. if ( ! is_numeric( $space_allowed ) )
  2122. $space_allowed = get_site_option( 'blog_upload_space' );
  2123. if ( ! is_numeric( $space_allowed ) )
  2124. $space_allowed = 100;
  2125. /**
  2126. * Filters the upload quota for the current site.
  2127. *
  2128. * @since 3.7.0
  2129. *
  2130. * @param int $space_allowed Upload quota in megabytes for the current blog.
  2131. */
  2132. return apply_filters( 'get_space_allowed', $space_allowed );
  2133. }
  2134. /**
  2135. * Determines if there is any upload space left in the current blog's quota.
  2136. *
  2137. * @since 3.0.0
  2138. *
  2139. * @return int of upload space available in bytes
  2140. */
  2141. function get_upload_space_available() {
  2142. $allowed = get_space_allowed();
  2143. if ( $allowed < 0 ) {
  2144. $allowed = 0;
  2145. }
  2146. $space_allowed = $allowed * MB_IN_BYTES;
  2147. if ( get_site_option( 'upload_space_check_disabled' ) )
  2148. return $space_allowed;
  2149. $space_used = get_space_used() * MB_IN_BYTES;
  2150. if ( ( $space_allowed - $space_used ) <= 0 )
  2151. return 0;
  2152. return $space_allowed - $space_used;
  2153. }
  2154. /**
  2155. * Determines if there is any upload space left in the current blog's quota.
  2156. *
  2157. * @since 3.0.0
  2158. * @return bool True if space is available, false otherwise.
  2159. */
  2160. function is_upload_space_available() {
  2161. if ( get_site_option( 'upload_space_check_disabled' ) )
  2162. return true;
  2163. return (bool) get_upload_space_available();
  2164. }
  2165. /**
  2166. * Filters the maximum upload file size allowed, in bytes.
  2167. *
  2168. * @since 3.0.0
  2169. *
  2170. * @param int $size Upload size limit in bytes.
  2171. * @return int Upload size limit in bytes.
  2172. */
  2173. function upload_size_limit_filter( $size ) {
  2174. $fileupload_maxk = KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 );
  2175. if ( get_site_option( 'upload_space_check_disabled' ) )
  2176. return min( $size, $fileupload_maxk );
  2177. return min( $size, $fileupload_maxk, get_upload_space_available() );
  2178. }
  2179. /**
  2180. * Whether or not we have a large network.
  2181. *
  2182. * The default criteria for a large network is either more than 10,000 users or more than 10,000 sites.
  2183. * Plugins can alter this criteria using the {@see 'wp_is_large_network'} filter.
  2184. *
  2185. * @since 3.3.0
  2186. * @since 4.8.0 The $network_id parameter has been added.
  2187. *
  2188. * @param string $using 'sites or 'users'. Default is 'sites'.
  2189. * @param int|null $network_id ID of the network. Default is the current network.
  2190. * @return bool True if the network meets the criteria for large. False otherwise.
  2191. */
  2192. function wp_is_large_network( $using = 'sites', $network_id = null ) {
  2193. $network_id = (int) $network_id;
  2194. if ( ! $network_id ) {
  2195. $network_id = get_current_network_id();
  2196. }
  2197. if ( 'users' == $using ) {
  2198. $count = get_user_count( $network_id );
  2199. /**
  2200. * Filters whether the network is considered large.
  2201. *
  2202. * @since 3.3.0
  2203. * @since 4.8.0 The $network_id parameter has been added.
  2204. *
  2205. * @param bool $is_large_network Whether the network has more than 10000 users or sites.
  2206. * @param string $component The component to count. Accepts 'users', or 'sites'.
  2207. * @param int $count The count of items for the component.
  2208. * @param int $network_id The ID of the network being checked.
  2209. */
  2210. return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count, $network_id );
  2211. }
  2212. $count = get_blog_count( $network_id );
  2213. /** This filter is documented in wp-includes/ms-functions.php */
  2214. return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count, $network_id );
  2215. }
  2216. /**
  2217. * Retrieves a list of reserved site on a sub-directory Multisite install.
  2218. *
  2219. * @since 4.4.0
  2220. *
  2221. * @return array $names Array of reserved subdirectory names.
  2222. */
  2223. function get_subdirectory_reserved_names() {
  2224. $names = array(
  2225. 'page', 'comments', 'blog', 'files', 'feed', 'wp-admin',
  2226. 'wp-content', 'wp-includes', 'wp-json', 'embed'
  2227. );
  2228. /**
  2229. * Filters reserved site names on a sub-directory Multisite install.
  2230. *
  2231. * @since 3.0.0
  2232. * @since 4.4.0 'wp-admin', 'wp-content', 'wp-includes', 'wp-json', and 'embed' were added
  2233. * to the reserved names list.
  2234. *
  2235. * @param array $subdirectory_reserved_names Array of reserved names.
  2236. */
  2237. return apply_filters( 'subdirectory_reserved_names', $names );
  2238. }