暂无描述

ms-functions.php 92KB

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