ms-functions.php 89 KB

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