No Description

pluggable.php 101KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887
  1. <?php
  2. /**
  3. * These functions can be replaced via plugins. If plugins do not redefine these
  4. * functions, then these will be used instead.
  5. *
  6. * @package WordPress
  7. */
  8. if ( ! function_exists( 'wp_set_current_user' ) ) :
  9. /**
  10. * Changes the current user by ID or name.
  11. *
  12. * Set $id to null and specify a name if you do not know a user's ID.
  13. *
  14. * Some WordPress functionality is based on the current user and not based on
  15. * the signed in user. Therefore, it opens the ability to edit and perform
  16. * actions on users who aren't signed in.
  17. *
  18. * @since 2.0.3
  19. *
  20. * @global WP_User $current_user The current user object which holds the user data.
  21. *
  22. * @param int $id User ID
  23. * @param string $name User's username
  24. * @return WP_User Current user User object
  25. */
  26. function wp_set_current_user( $id, $name = '' ) {
  27. global $current_user;
  28. // If `$id` matches the current user, there is nothing to do.
  29. if ( isset( $current_user )
  30. && ( $current_user instanceof WP_User )
  31. && ( $id == $current_user->ID )
  32. && ( null !== $id )
  33. ) {
  34. return $current_user;
  35. }
  36. $current_user = new WP_User( $id, $name );
  37. setup_userdata( $current_user->ID );
  38. /**
  39. * Fires after the current user is set.
  40. *
  41. * @since 2.0.1
  42. */
  43. do_action( 'set_current_user' );
  44. return $current_user;
  45. }
  46. endif;
  47. if ( ! function_exists( 'wp_get_current_user' ) ) :
  48. /**
  49. * Retrieve the current user object.
  50. *
  51. * Will set the current user, if the current user is not set. The current user
  52. * will be set to the logged-in person. If no user is logged-in, then it will
  53. * set the current user to 0, which is invalid and won't have any permissions.
  54. *
  55. * @since 2.0.3
  56. *
  57. * @see _wp_get_current_user()
  58. * @global WP_User $current_user Checks if the current user is set.
  59. *
  60. * @return WP_User Current WP_User instance.
  61. */
  62. function wp_get_current_user() {
  63. return _wp_get_current_user();
  64. }
  65. endif;
  66. if ( ! function_exists( 'get_userdata' ) ) :
  67. /**
  68. * Retrieve user info by user ID.
  69. *
  70. * @since 0.71
  71. *
  72. * @param int $user_id User ID
  73. * @return WP_User|false WP_User object on success, false on failure.
  74. */
  75. function get_userdata( $user_id ) {
  76. return get_user_by( 'id', $user_id );
  77. }
  78. endif;
  79. if ( ! function_exists( 'get_user_by' ) ) :
  80. /**
  81. * Retrieve user info by a given field
  82. *
  83. * @since 2.8.0
  84. * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
  85. * @since 5.8.0 Returns the global `$current_user` if it's the user being fetched.
  86. *
  87. * @global WP_User $current_user The current user object which holds the user data.
  88. *
  89. * @param string $field The field to retrieve the user with. id | ID | slug | email | login.
  90. * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
  91. * @return WP_User|false WP_User object on success, false on failure.
  92. */
  93. function get_user_by( $field, $value ) {
  94. global $current_user;
  95. $userdata = WP_User::get_data_by( $field, $value );
  96. if ( ! $userdata ) {
  97. return false;
  98. }
  99. if ( $current_user instanceof WP_User && $current_user->ID === (int) $userdata->ID ) {
  100. return $current_user;
  101. }
  102. $user = new WP_User;
  103. $user->init( $userdata );
  104. return $user;
  105. }
  106. endif;
  107. if ( ! function_exists( 'cache_users' ) ) :
  108. /**
  109. * Retrieve info for user lists to prevent multiple queries by get_userdata()
  110. *
  111. * @since 3.0.0
  112. *
  113. * @global wpdb $wpdb WordPress database abstraction object.
  114. *
  115. * @param array $user_ids User ID numbers list
  116. */
  117. function cache_users( $user_ids ) {
  118. global $wpdb;
  119. $clean = _get_non_cached_ids( $user_ids, 'users' );
  120. if ( empty( $clean ) ) {
  121. return;
  122. }
  123. $list = implode( ',', $clean );
  124. $users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" );
  125. $ids = array();
  126. foreach ( $users as $user ) {
  127. update_user_caches( $user );
  128. $ids[] = $user->ID;
  129. }
  130. update_meta_cache( 'user', $ids );
  131. }
  132. endif;
  133. if ( ! function_exists( 'wp_mail' ) ) :
  134. /**
  135. * Sends an email, similar to PHP's mail function.
  136. *
  137. * A true return value does not automatically mean that the user received the
  138. * email successfully. It just only means that the method used was able to
  139. * process the request without any errors.
  140. *
  141. * The default content type is `text/plain` which does not allow using HTML.
  142. * However, you can set the content type of the email by using the
  143. * {@see 'wp_mail_content_type'} filter.
  144. *
  145. * The default charset is based on the charset used on the blog. The charset can
  146. * be set using the {@see 'wp_mail_charset'} filter.
  147. *
  148. * @since 1.2.1
  149. * @since 5.5.0 is_email() is used for email validation,
  150. * instead of PHPMailer's default validator.
  151. *
  152. * @global PHPMailer\PHPMailer\PHPMailer $phpmailer
  153. *
  154. * @param string|string[] $to Array or comma-separated list of email addresses to send message.
  155. * @param string $subject Email subject.
  156. * @param string $message Message contents.
  157. * @param string|string[] $headers Optional. Additional headers.
  158. * @param string|string[] $attachments Optional. Paths to files to attach.
  159. * @return bool Whether the email was sent successfully.
  160. */
  161. function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
  162. // Compact the input, apply the filters, and extract them back out.
  163. /**
  164. * Filters the wp_mail() arguments.
  165. *
  166. * @since 2.2.0
  167. *
  168. * @param array $args {
  169. * Array of the `wp_mail()` arguments.
  170. *
  171. * @type string|string[] $to Array or comma-separated list of email addresses to send message.
  172. * @type string $subject Email subject.
  173. * @type string $message Message contents.
  174. * @type string|string[] $headers Additional headers.
  175. * @type string|string[] $attachments Paths to files to attach.
  176. * }
  177. */
  178. $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
  179. /**
  180. * Filters whether to preempt sending an email.
  181. *
  182. * Returning a non-null value will short-circuit {@see wp_mail()}, returning
  183. * that value instead. A boolean return value should be used to indicate whether
  184. * the email was successfully sent.
  185. *
  186. * @since 5.7.0
  187. *
  188. * @param null|bool $return Short-circuit return value.
  189. * @param array $atts {
  190. * Array of the `wp_mail()` arguments.
  191. *
  192. * @type string|string[] $to Array or comma-separated list of email addresses to send message.
  193. * @type string $subject Email subject.
  194. * @type string $message Message contents.
  195. * @type string|string[] $headers Additional headers.
  196. * @type string|string[] $attachments Paths to files to attach.
  197. * }
  198. */
  199. $pre_wp_mail = apply_filters( 'pre_wp_mail', null, $atts );
  200. if ( null !== $pre_wp_mail ) {
  201. return $pre_wp_mail;
  202. }
  203. if ( isset( $atts['to'] ) ) {
  204. $to = $atts['to'];
  205. }
  206. if ( ! is_array( $to ) ) {
  207. $to = explode( ',', $to );
  208. }
  209. if ( isset( $atts['subject'] ) ) {
  210. $subject = $atts['subject'];
  211. }
  212. if ( isset( $atts['message'] ) ) {
  213. $message = $atts['message'];
  214. }
  215. if ( isset( $atts['headers'] ) ) {
  216. $headers = $atts['headers'];
  217. }
  218. if ( isset( $atts['attachments'] ) ) {
  219. $attachments = $atts['attachments'];
  220. }
  221. if ( ! is_array( $attachments ) ) {
  222. $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
  223. }
  224. global $phpmailer;
  225. // (Re)create it, if it's gone missing.
  226. if ( ! ( $phpmailer instanceof PHPMailer\PHPMailer\PHPMailer ) ) {
  227. require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
  228. require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
  229. require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
  230. $phpmailer = new PHPMailer\PHPMailer\PHPMailer( true );
  231. $phpmailer::$validator = static function ( $email ) {
  232. return (bool) is_email( $email );
  233. };
  234. }
  235. // Headers.
  236. $cc = array();
  237. $bcc = array();
  238. $reply_to = array();
  239. if ( empty( $headers ) ) {
  240. $headers = array();
  241. } else {
  242. if ( ! is_array( $headers ) ) {
  243. // Explode the headers out, so this function can take
  244. // both string headers and an array of headers.
  245. $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
  246. } else {
  247. $tempheaders = $headers;
  248. }
  249. $headers = array();
  250. // If it's actually got contents.
  251. if ( ! empty( $tempheaders ) ) {
  252. // Iterate through the raw headers.
  253. foreach ( (array) $tempheaders as $header ) {
  254. if ( strpos( $header, ':' ) === false ) {
  255. if ( false !== stripos( $header, 'boundary=' ) ) {
  256. $parts = preg_split( '/boundary=/i', trim( $header ) );
  257. $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
  258. }
  259. continue;
  260. }
  261. // Explode them out.
  262. list( $name, $content ) = explode( ':', trim( $header ), 2 );
  263. // Cleanup crew.
  264. $name = trim( $name );
  265. $content = trim( $content );
  266. switch ( strtolower( $name ) ) {
  267. // Mainly for legacy -- process a "From:" header if it's there.
  268. case 'from':
  269. $bracket_pos = strpos( $content, '<' );
  270. if ( false !== $bracket_pos ) {
  271. // Text before the bracketed email is the "From" name.
  272. if ( $bracket_pos > 0 ) {
  273. $from_name = substr( $content, 0, $bracket_pos - 1 );
  274. $from_name = str_replace( '"', '', $from_name );
  275. $from_name = trim( $from_name );
  276. }
  277. $from_email = substr( $content, $bracket_pos + 1 );
  278. $from_email = str_replace( '>', '', $from_email );
  279. $from_email = trim( $from_email );
  280. // Avoid setting an empty $from_email.
  281. } elseif ( '' !== trim( $content ) ) {
  282. $from_email = trim( $content );
  283. }
  284. break;
  285. case 'content-type':
  286. if ( strpos( $content, ';' ) !== false ) {
  287. list( $type, $charset_content ) = explode( ';', $content );
  288. $content_type = trim( $type );
  289. if ( false !== stripos( $charset_content, 'charset=' ) ) {
  290. $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
  291. } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
  292. $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
  293. $charset = '';
  294. }
  295. // Avoid setting an empty $content_type.
  296. } elseif ( '' !== trim( $content ) ) {
  297. $content_type = trim( $content );
  298. }
  299. break;
  300. case 'cc':
  301. $cc = array_merge( (array) $cc, explode( ',', $content ) );
  302. break;
  303. case 'bcc':
  304. $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
  305. break;
  306. case 'reply-to':
  307. $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) );
  308. break;
  309. default:
  310. // Add it to our grand headers array.
  311. $headers[ trim( $name ) ] = trim( $content );
  312. break;
  313. }
  314. }
  315. }
  316. }
  317. // Empty out the values that may be set.
  318. $phpmailer->clearAllRecipients();
  319. $phpmailer->clearAttachments();
  320. $phpmailer->clearCustomHeaders();
  321. $phpmailer->clearReplyTos();
  322. // Set "From" name and email.
  323. // If we don't have a name from the input headers.
  324. if ( ! isset( $from_name ) ) {
  325. $from_name = 'WordPress';
  326. }
  327. /*
  328. * If we don't have an email from the input headers, default to wordpress@$sitename
  329. * Some hosts will block outgoing mail from this address if it doesn't exist,
  330. * but there's no easy alternative. Defaulting to admin_email might appear to be
  331. * another option, but some hosts may refuse to relay mail from an unknown domain.
  332. * See https://core.trac.wordpress.org/ticket/5007.
  333. */
  334. if ( ! isset( $from_email ) ) {
  335. // Get the site domain and get rid of www.
  336. $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST );
  337. if ( 'www.' === substr( $sitename, 0, 4 ) ) {
  338. $sitename = substr( $sitename, 4 );
  339. }
  340. $from_email = 'wordpress@' . $sitename;
  341. }
  342. /**
  343. * Filters the email address to send from.
  344. *
  345. * @since 2.2.0
  346. *
  347. * @param string $from_email Email address to send from.
  348. */
  349. $from_email = apply_filters( 'wp_mail_from', $from_email );
  350. /**
  351. * Filters the name to associate with the "from" email address.
  352. *
  353. * @since 2.3.0
  354. *
  355. * @param string $from_name Name associated with the "from" email address.
  356. */
  357. $from_name = apply_filters( 'wp_mail_from_name', $from_name );
  358. try {
  359. $phpmailer->setFrom( $from_email, $from_name, false );
  360. } catch ( PHPMailer\PHPMailer\Exception $e ) {
  361. $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
  362. $mail_error_data['phpmailer_exception_code'] = $e->getCode();
  363. /** This filter is documented in wp-includes/pluggable.php */
  364. do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );
  365. return false;
  366. }
  367. // Set mail's subject and body.
  368. $phpmailer->Subject = $subject;
  369. $phpmailer->Body = $message;
  370. // Set destination addresses, using appropriate methods for handling addresses.
  371. $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' );
  372. foreach ( $address_headers as $address_header => $addresses ) {
  373. if ( empty( $addresses ) ) {
  374. continue;
  375. }
  376. foreach ( (array) $addresses as $address ) {
  377. try {
  378. // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>".
  379. $recipient_name = '';
  380. if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) {
  381. if ( count( $matches ) == 3 ) {
  382. $recipient_name = $matches[1];
  383. $address = $matches[2];
  384. }
  385. }
  386. switch ( $address_header ) {
  387. case 'to':
  388. $phpmailer->addAddress( $address, $recipient_name );
  389. break;
  390. case 'cc':
  391. $phpmailer->addCc( $address, $recipient_name );
  392. break;
  393. case 'bcc':
  394. $phpmailer->addBcc( $address, $recipient_name );
  395. break;
  396. case 'reply_to':
  397. $phpmailer->addReplyTo( $address, $recipient_name );
  398. break;
  399. }
  400. } catch ( PHPMailer\PHPMailer\Exception $e ) {
  401. continue;
  402. }
  403. }
  404. }
  405. // Set to use PHP's mail().
  406. $phpmailer->isMail();
  407. // Set Content-Type and charset.
  408. // If we don't have a content-type from the input headers.
  409. if ( ! isset( $content_type ) ) {
  410. $content_type = 'text/plain';
  411. }
  412. /**
  413. * Filters the wp_mail() content type.
  414. *
  415. * @since 2.3.0
  416. *
  417. * @param string $content_type Default wp_mail() content type.
  418. */
  419. $content_type = apply_filters( 'wp_mail_content_type', $content_type );
  420. $phpmailer->ContentType = $content_type;
  421. // Set whether it's plaintext, depending on $content_type.
  422. if ( 'text/html' === $content_type ) {
  423. $phpmailer->isHTML( true );
  424. }
  425. // If we don't have a charset from the input headers.
  426. if ( ! isset( $charset ) ) {
  427. $charset = get_bloginfo( 'charset' );
  428. }
  429. /**
  430. * Filters the default wp_mail() charset.
  431. *
  432. * @since 2.3.0
  433. *
  434. * @param string $charset Default email charset.
  435. */
  436. $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
  437. // Set custom headers.
  438. if ( ! empty( $headers ) ) {
  439. foreach ( (array) $headers as $name => $content ) {
  440. // Only add custom headers not added automatically by PHPMailer.
  441. if ( ! in_array( $name, array( 'MIME-Version', 'X-Mailer' ), true ) ) {
  442. try {
  443. $phpmailer->addCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
  444. } catch ( PHPMailer\PHPMailer\Exception $e ) {
  445. continue;
  446. }
  447. }
  448. }
  449. if ( false !== stripos( $content_type, 'multipart' ) && ! empty( $boundary ) ) {
  450. $phpmailer->addCustomHeader( sprintf( 'Content-Type: %s; boundary="%s"', $content_type, $boundary ) );
  451. }
  452. }
  453. if ( ! empty( $attachments ) ) {
  454. foreach ( $attachments as $attachment ) {
  455. try {
  456. $phpmailer->addAttachment( $attachment );
  457. } catch ( PHPMailer\PHPMailer\Exception $e ) {
  458. continue;
  459. }
  460. }
  461. }
  462. /**
  463. * Fires after PHPMailer is initialized.
  464. *
  465. * @since 2.2.0
  466. *
  467. * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
  468. */
  469. do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
  470. // Send!
  471. try {
  472. return $phpmailer->send();
  473. } catch ( PHPMailer\PHPMailer\Exception $e ) {
  474. $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
  475. $mail_error_data['phpmailer_exception_code'] = $e->getCode();
  476. /**
  477. * Fires after a PHPMailer\PHPMailer\Exception is caught.
  478. *
  479. * @since 4.4.0
  480. *
  481. * @param WP_Error $error A WP_Error object with the PHPMailer\PHPMailer\Exception message, and an array
  482. * containing the mail recipient, subject, message, headers, and attachments.
  483. */
  484. do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );
  485. return false;
  486. }
  487. }
  488. endif;
  489. if ( ! function_exists( 'wp_authenticate' ) ) :
  490. /**
  491. * Authenticate a user, confirming the login credentials are valid.
  492. *
  493. * @since 2.5.0
  494. * @since 4.5.0 `$username` now accepts an email address.
  495. *
  496. * @param string $username User's username or email address.
  497. * @param string $password User's password.
  498. * @return WP_User|WP_Error WP_User object if the credentials are valid,
  499. * otherwise WP_Error.
  500. */
  501. function wp_authenticate( $username, $password ) {
  502. $username = sanitize_user( $username );
  503. $password = trim( $password );
  504. /**
  505. * Filters whether a set of user login credentials are valid.
  506. *
  507. * A WP_User object is returned if the credentials authenticate a user.
  508. * WP_Error or null otherwise.
  509. *
  510. * @since 2.8.0
  511. * @since 4.5.0 `$username` now accepts an email address.
  512. *
  513. * @param null|WP_User|WP_Error $user WP_User if the user is authenticated.
  514. * WP_Error or null otherwise.
  515. * @param string $username Username or email address.
  516. * @param string $password User password
  517. */
  518. $user = apply_filters( 'authenticate', null, $username, $password );
  519. if ( null == $user ) {
  520. // TODO: What should the error message be? (Or would these even happen?)
  521. // Only needed if all authentication handlers fail to return anything.
  522. $user = new WP_Error( 'authentication_failed', __( '<strong>Error</strong>: Invalid username, email address or incorrect password.' ) );
  523. }
  524. $ignore_codes = array( 'empty_username', 'empty_password' );
  525. if ( is_wp_error( $user ) && ! in_array( $user->get_error_code(), $ignore_codes, true ) ) {
  526. $error = $user;
  527. /**
  528. * Fires after a user login has failed.
  529. *
  530. * @since 2.5.0
  531. * @since 4.5.0 The value of `$username` can now be an email address.
  532. * @since 5.4.0 The `$error` parameter was added.
  533. *
  534. * @param string $username Username or email address.
  535. * @param WP_Error $error A WP_Error object with the authentication failure details.
  536. */
  537. do_action( 'wp_login_failed', $username, $error );
  538. }
  539. return $user;
  540. }
  541. endif;
  542. if ( ! function_exists( 'wp_logout' ) ) :
  543. /**
  544. * Log the current user out.
  545. *
  546. * @since 2.5.0
  547. */
  548. function wp_logout() {
  549. $user_id = get_current_user_id();
  550. wp_destroy_current_session();
  551. wp_clear_auth_cookie();
  552. wp_set_current_user( 0 );
  553. /**
  554. * Fires after a user is logged out.
  555. *
  556. * @since 1.5.0
  557. * @since 5.5.0 Added the `$user_id` parameter.
  558. *
  559. * @param int $user_id ID of the user that was logged out.
  560. */
  561. do_action( 'wp_logout', $user_id );
  562. }
  563. endif;
  564. if ( ! function_exists( 'wp_validate_auth_cookie' ) ) :
  565. /**
  566. * Validates authentication cookie.
  567. *
  568. * The checks include making sure that the authentication cookie is set and
  569. * pulling in the contents (if $cookie is not used).
  570. *
  571. * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
  572. * should be and compares the two.
  573. *
  574. * @since 2.5.0
  575. *
  576. * @global int $login_grace_period
  577. *
  578. * @param string $cookie Optional. If used, will validate contents instead of cookie's.
  579. * @param string $scheme Optional. The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'.
  580. * @return int|false User ID if valid cookie, false if invalid.
  581. */
  582. function wp_validate_auth_cookie( $cookie = '', $scheme = '' ) {
  583. $cookie_elements = wp_parse_auth_cookie( $cookie, $scheme );
  584. if ( ! $cookie_elements ) {
  585. /**
  586. * Fires if an authentication cookie is malformed.
  587. *
  588. * @since 2.7.0
  589. *
  590. * @param string $cookie Malformed auth cookie.
  591. * @param string $scheme Authentication scheme. Values include 'auth', 'secure_auth',
  592. * or 'logged_in'.
  593. */
  594. do_action( 'auth_cookie_malformed', $cookie, $scheme );
  595. return false;
  596. }
  597. $scheme = $cookie_elements['scheme'];
  598. $username = $cookie_elements['username'];
  599. $hmac = $cookie_elements['hmac'];
  600. $token = $cookie_elements['token'];
  601. $expired = $cookie_elements['expiration'];
  602. $expiration = $cookie_elements['expiration'];
  603. // Allow a grace period for POST and Ajax requests.
  604. if ( wp_doing_ajax() || 'POST' === $_SERVER['REQUEST_METHOD'] ) {
  605. $expired += HOUR_IN_SECONDS;
  606. }
  607. // Quick check to see if an honest cookie has expired.
  608. if ( $expired < time() ) {
  609. /**
  610. * Fires once an authentication cookie has expired.
  611. *
  612. * @since 2.7.0
  613. *
  614. * @param string[] $cookie_elements An array of data for the authentication cookie.
  615. */
  616. do_action( 'auth_cookie_expired', $cookie_elements );
  617. return false;
  618. }
  619. $user = get_user_by( 'login', $username );
  620. if ( ! $user ) {
  621. /**
  622. * Fires if a bad username is entered in the user authentication process.
  623. *
  624. * @since 2.7.0
  625. *
  626. * @param string[] $cookie_elements An array of data for the authentication cookie.
  627. */
  628. do_action( 'auth_cookie_bad_username', $cookie_elements );
  629. return false;
  630. }
  631. $pass_frag = substr( $user->user_pass, 8, 4 );
  632. $key = wp_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );
  633. // If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
  634. $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
  635. $hash = hash_hmac( $algo, $username . '|' . $expiration . '|' . $token, $key );
  636. if ( ! hash_equals( $hash, $hmac ) ) {
  637. /**
  638. * Fires if a bad authentication cookie hash is encountered.
  639. *
  640. * @since 2.7.0
  641. *
  642. * @param string[] $cookie_elements An array of data for the authentication cookie.
  643. */
  644. do_action( 'auth_cookie_bad_hash', $cookie_elements );
  645. return false;
  646. }
  647. $manager = WP_Session_Tokens::get_instance( $user->ID );
  648. if ( ! $manager->verify( $token ) ) {
  649. /**
  650. * Fires if a bad session token is encountered.
  651. *
  652. * @since 4.0.0
  653. *
  654. * @param string[] $cookie_elements An array of data for the authentication cookie.
  655. */
  656. do_action( 'auth_cookie_bad_session_token', $cookie_elements );
  657. return false;
  658. }
  659. // Ajax/POST grace period set above.
  660. if ( $expiration < time() ) {
  661. $GLOBALS['login_grace_period'] = 1;
  662. }
  663. /**
  664. * Fires once an authentication cookie has been validated.
  665. *
  666. * @since 2.7.0
  667. *
  668. * @param string[] $cookie_elements An array of data for the authentication cookie.
  669. * @param WP_User $user User object.
  670. */
  671. do_action( 'auth_cookie_valid', $cookie_elements, $user );
  672. return $user->ID;
  673. }
  674. endif;
  675. if ( ! function_exists( 'wp_generate_auth_cookie' ) ) :
  676. /**
  677. * Generates authentication cookie contents.
  678. *
  679. * @since 2.5.0
  680. * @since 4.0.0 The `$token` parameter was added.
  681. *
  682. * @param int $user_id User ID.
  683. * @param int $expiration The time the cookie expires as a UNIX timestamp.
  684. * @param string $scheme Optional. The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'.
  685. * Default 'auth'.
  686. * @param string $token User's session token to use for this cookie.
  687. * @return string Authentication cookie contents. Empty string if user does not exist.
  688. */
  689. function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) {
  690. $user = get_userdata( $user_id );
  691. if ( ! $user ) {
  692. return '';
  693. }
  694. if ( ! $token ) {
  695. $manager = WP_Session_Tokens::get_instance( $user_id );
  696. $token = $manager->create( $expiration );
  697. }
  698. $pass_frag = substr( $user->user_pass, 8, 4 );
  699. $key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );
  700. // If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
  701. $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
  702. $hash = hash_hmac( $algo, $user->user_login . '|' . $expiration . '|' . $token, $key );
  703. $cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash;
  704. /**
  705. * Filters the authentication cookie.
  706. *
  707. * @since 2.5.0
  708. * @since 4.0.0 The `$token` parameter was added.
  709. *
  710. * @param string $cookie Authentication cookie.
  711. * @param int $user_id User ID.
  712. * @param int $expiration The time the cookie expires as a UNIX timestamp.
  713. * @param string $scheme Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'.
  714. * @param string $token User's session token used.
  715. */
  716. return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token );
  717. }
  718. endif;
  719. if ( ! function_exists( 'wp_parse_auth_cookie' ) ) :
  720. /**
  721. * Parses a cookie into its components.
  722. *
  723. * @since 2.7.0
  724. *
  725. * @param string $cookie Authentication cookie.
  726. * @param string $scheme Optional. The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'.
  727. * @return string[]|false Authentication cookie components.
  728. */
  729. function wp_parse_auth_cookie( $cookie = '', $scheme = '' ) {
  730. if ( empty( $cookie ) ) {
  731. switch ( $scheme ) {
  732. case 'auth':
  733. $cookie_name = AUTH_COOKIE;
  734. break;
  735. case 'secure_auth':
  736. $cookie_name = SECURE_AUTH_COOKIE;
  737. break;
  738. case 'logged_in':
  739. $cookie_name = LOGGED_IN_COOKIE;
  740. break;
  741. default:
  742. if ( is_ssl() ) {
  743. $cookie_name = SECURE_AUTH_COOKIE;
  744. $scheme = 'secure_auth';
  745. } else {
  746. $cookie_name = AUTH_COOKIE;
  747. $scheme = 'auth';
  748. }
  749. }
  750. if ( empty( $_COOKIE[ $cookie_name ] ) ) {
  751. return false;
  752. }
  753. $cookie = $_COOKIE[ $cookie_name ];
  754. }
  755. $cookie_elements = explode( '|', $cookie );
  756. if ( count( $cookie_elements ) !== 4 ) {
  757. return false;
  758. }
  759. list( $username, $expiration, $token, $hmac ) = $cookie_elements;
  760. return compact( 'username', 'expiration', 'token', 'hmac', 'scheme' );
  761. }
  762. endif;
  763. if ( ! function_exists( 'wp_set_auth_cookie' ) ) :
  764. /**
  765. * Sets the authentication cookies based on user ID.
  766. *
  767. * The $remember parameter increases the time that the cookie will be kept. The
  768. * default the cookie is kept without remembering is two days. When $remember is
  769. * set, the cookies will be kept for 14 days or two weeks.
  770. *
  771. * @since 2.5.0
  772. * @since 4.3.0 Added the `$token` parameter.
  773. *
  774. * @param int $user_id User ID.
  775. * @param bool $remember Whether to remember the user.
  776. * @param bool|string $secure Whether the auth cookie should only be sent over HTTPS. Default is an empty
  777. * string which means the value of `is_ssl()` will be used.
  778. * @param string $token Optional. User's session token to use for this cookie.
  779. */
  780. function wp_set_auth_cookie( $user_id, $remember = false, $secure = '', $token = '' ) {
  781. if ( $remember ) {
  782. /**
  783. * Filters the duration of the authentication cookie expiration period.
  784. *
  785. * @since 2.8.0
  786. *
  787. * @param int $length Duration of the expiration period in seconds.
  788. * @param int $user_id User ID.
  789. * @param bool $remember Whether to remember the user login. Default false.
  790. */
  791. $expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember );
  792. /*
  793. * Ensure the browser will continue to send the cookie after the expiration time is reached.
  794. * Needed for the login grace period in wp_validate_auth_cookie().
  795. */
  796. $expire = $expiration + ( 12 * HOUR_IN_SECONDS );
  797. } else {
  798. /** This filter is documented in wp-includes/pluggable.php */
  799. $expiration = time() + apply_filters( 'auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user_id, $remember );
  800. $expire = 0;
  801. }
  802. if ( '' === $secure ) {
  803. $secure = is_ssl();
  804. }
  805. // Front-end cookie is secure when the auth cookie is secure and the site's home URL uses HTTPS.
  806. $secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME );
  807. /**
  808. * Filters whether the auth cookie should only be sent over HTTPS.
  809. *
  810. * @since 3.1.0
  811. *
  812. * @param bool $secure Whether the cookie should only be sent over HTTPS.
  813. * @param int $user_id User ID.
  814. */
  815. $secure = apply_filters( 'secure_auth_cookie', $secure, $user_id );
  816. /**
  817. * Filters whether the logged in cookie should only be sent over HTTPS.
  818. *
  819. * @since 3.1.0
  820. *
  821. * @param bool $secure_logged_in_cookie Whether the logged in cookie should only be sent over HTTPS.
  822. * @param int $user_id User ID.
  823. * @param bool $secure Whether the auth cookie should only be sent over HTTPS.
  824. */
  825. $secure_logged_in_cookie = apply_filters( 'secure_logged_in_cookie', $secure_logged_in_cookie, $user_id, $secure );
  826. if ( $secure ) {
  827. $auth_cookie_name = SECURE_AUTH_COOKIE;
  828. $scheme = 'secure_auth';
  829. } else {
  830. $auth_cookie_name = AUTH_COOKIE;
  831. $scheme = 'auth';
  832. }
  833. if ( '' === $token ) {
  834. $manager = WP_Session_Tokens::get_instance( $user_id );
  835. $token = $manager->create( $expiration );
  836. }
  837. $auth_cookie = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token );
  838. $logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token );
  839. /**
  840. * Fires immediately before the authentication cookie is set.
  841. *
  842. * @since 2.5.0
  843. * @since 4.9.0 The `$token` parameter was added.
  844. *
  845. * @param string $auth_cookie Authentication cookie value.
  846. * @param int $expire The time the login grace period expires as a UNIX timestamp.
  847. * Default is 12 hours past the cookie's expiration time.
  848. * @param int $expiration The time when the authentication cookie expires as a UNIX timestamp.
  849. * Default is 14 days from now.
  850. * @param int $user_id User ID.
  851. * @param string $scheme Authentication scheme. Values include 'auth' or 'secure_auth'.
  852. * @param string $token User's session token to use for this cookie.
  853. */
  854. do_action( 'set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme, $token );
  855. /**
  856. * Fires immediately before the logged-in authentication cookie is set.
  857. *
  858. * @since 2.6.0
  859. * @since 4.9.0 The `$token` parameter was added.
  860. *
  861. * @param string $logged_in_cookie The logged-in cookie value.
  862. * @param int $expire The time the login grace period expires as a UNIX timestamp.
  863. * Default is 12 hours past the cookie's expiration time.
  864. * @param int $expiration The time when the logged-in authentication cookie expires as a UNIX timestamp.
  865. * Default is 14 days from now.
  866. * @param int $user_id User ID.
  867. * @param string $scheme Authentication scheme. Default 'logged_in'.
  868. * @param string $token User's session token to use for this cookie.
  869. */
  870. do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in', $token );
  871. /**
  872. * Allows preventing auth cookies from actually being sent to the client.
  873. *
  874. * @since 4.7.4
  875. *
  876. * @param bool $send Whether to send auth cookies to the client.
  877. */
  878. if ( ! apply_filters( 'send_auth_cookies', true ) ) {
  879. return;
  880. }
  881. setcookie( $auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true );
  882. setcookie( $auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true );
  883. setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true );
  884. if ( COOKIEPATH != SITECOOKIEPATH ) {
  885. setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true );
  886. }
  887. }
  888. endif;
  889. if ( ! function_exists( 'wp_clear_auth_cookie' ) ) :
  890. /**
  891. * Removes all of the cookies associated with authentication.
  892. *
  893. * @since 2.5.0
  894. */
  895. function wp_clear_auth_cookie() {
  896. /**
  897. * Fires just before the authentication cookies are cleared.
  898. *
  899. * @since 2.7.0
  900. */
  901. do_action( 'clear_auth_cookie' );
  902. /** This filter is documented in wp-includes/pluggable.php */
  903. if ( ! apply_filters( 'send_auth_cookies', true ) ) {
  904. return;
  905. }
  906. // Auth cookies.
  907. setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN );
  908. setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN );
  909. setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
  910. setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
  911. setcookie( LOGGED_IN_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
  912. setcookie( LOGGED_IN_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
  913. // Settings cookies.
  914. setcookie( 'wp-settings-' . get_current_user_id(), ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
  915. setcookie( 'wp-settings-time-' . get_current_user_id(), ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
  916. // Old cookies.
  917. setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
  918. setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
  919. setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
  920. setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
  921. // Even older cookies.
  922. setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
  923. setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
  924. setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
  925. setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
  926. // Post password cookie.
  927. setcookie( 'wp-postpass_' . COOKIEHASH, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
  928. }
  929. endif;
  930. if ( ! function_exists( 'is_user_logged_in' ) ) :
  931. /**
  932. * Determines whether the current visitor is a logged in user.
  933. *
  934. * For more information on this and similar theme functions, check out
  935. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  936. * Conditional Tags} article in the Theme Developer Handbook.
  937. *
  938. * @since 2.0.0
  939. *
  940. * @return bool True if user is logged in, false if not logged in.
  941. */
  942. function is_user_logged_in() {
  943. $user = wp_get_current_user();
  944. return $user->exists();
  945. }
  946. endif;
  947. if ( ! function_exists( 'auth_redirect' ) ) :
  948. /**
  949. * Checks if a user is logged in, if not it redirects them to the login page.
  950. *
  951. * When this code is called from a page, it checks to see if the user viewing the page is logged in.
  952. * If the user is not logged in, they are redirected to the login page. The user is redirected
  953. * in such a way that, upon logging in, they will be sent directly to the page they were originally
  954. * trying to access.
  955. *
  956. * @since 1.5.0
  957. */
  958. function auth_redirect() {
  959. $secure = ( is_ssl() || force_ssl_admin() );
  960. /**
  961. * Filters whether to use a secure authentication redirect.
  962. *
  963. * @since 3.1.0
  964. *
  965. * @param bool $secure Whether to use a secure authentication redirect. Default false.
  966. */
  967. $secure = apply_filters( 'secure_auth_redirect', $secure );
  968. // If https is required and request is http, redirect.
  969. if ( $secure && ! is_ssl() && false !== strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
  970. if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
  971. wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
  972. exit;
  973. } else {
  974. wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  975. exit;
  976. }
  977. }
  978. /**
  979. * Filters the authentication redirect scheme.
  980. *
  981. * @since 2.9.0
  982. *
  983. * @param string $scheme Authentication redirect scheme. Default empty.
  984. */
  985. $scheme = apply_filters( 'auth_redirect_scheme', '' );
  986. $user_id = wp_validate_auth_cookie( '', $scheme );
  987. if ( $user_id ) {
  988. /**
  989. * Fires before the authentication redirect.
  990. *
  991. * @since 2.8.0
  992. *
  993. * @param int $user_id User ID.
  994. */
  995. do_action( 'auth_redirect', $user_id );
  996. // If the user wants ssl but the session is not ssl, redirect.
  997. if ( ! $secure && get_user_option( 'use_ssl', $user_id ) && false !== strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
  998. if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
  999. wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
  1000. exit;
  1001. } else {
  1002. wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  1003. exit;
  1004. }
  1005. }
  1006. return; // The cookie is good, so we're done.
  1007. }
  1008. // The cookie is no good, so force login.
  1009. nocache_headers();
  1010. $redirect = ( strpos( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) ? wp_get_referer() : set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  1011. $login_url = wp_login_url( $redirect, true );
  1012. wp_redirect( $login_url );
  1013. exit;
  1014. }
  1015. endif;
  1016. if ( ! function_exists( 'check_admin_referer' ) ) :
  1017. /**
  1018. * Ensures intent by verifying that a user was referred from another admin page with the correct security nonce.
  1019. *
  1020. * This function ensures the user intends to perform a given action, which helps protect against clickjacking style
  1021. * attacks. It verifies intent, not authorisation, therefore it does not verify the user's capabilities. This should
  1022. * be performed with `current_user_can()` or similar.
  1023. *
  1024. * If the nonce value is invalid, the function will exit with an "Are You Sure?" style message.
  1025. *
  1026. * @since 1.2.0
  1027. * @since 2.5.0 The `$query_arg` parameter was added.
  1028. *
  1029. * @param int|string $action The nonce action.
  1030. * @param string $query_arg Optional. Key to check for nonce in `$_REQUEST`. Default '_wpnonce'.
  1031. * @return int|false 1 if the nonce is valid and generated between 0-12 hours ago,
  1032. * 2 if the nonce is valid and generated between 12-24 hours ago.
  1033. * False if the nonce is invalid.
  1034. */
  1035. function check_admin_referer( $action = -1, $query_arg = '_wpnonce' ) {
  1036. if ( -1 === $action ) {
  1037. _doing_it_wrong( __FUNCTION__, __( 'You should specify an action to be verified by using the first parameter.' ), '3.2.0' );
  1038. }
  1039. $adminurl = strtolower( admin_url() );
  1040. $referer = strtolower( wp_get_referer() );
  1041. $result = isset( $_REQUEST[ $query_arg ] ) ? wp_verify_nonce( $_REQUEST[ $query_arg ], $action ) : false;
  1042. /**
  1043. * Fires once the admin request has been validated or not.
  1044. *
  1045. * @since 1.5.1
  1046. *
  1047. * @param string $action The nonce action.
  1048. * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between
  1049. * 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
  1050. */
  1051. do_action( 'check_admin_referer', $action, $result );
  1052. if ( ! $result && ! ( -1 === $action && strpos( $referer, $adminurl ) === 0 ) ) {
  1053. wp_nonce_ays( $action );
  1054. die();
  1055. }
  1056. return $result;
  1057. }
  1058. endif;
  1059. if ( ! function_exists( 'check_ajax_referer' ) ) :
  1060. /**
  1061. * Verifies the Ajax request to prevent processing requests external of the blog.
  1062. *
  1063. * @since 2.0.3
  1064. *
  1065. * @param int|string $action Action nonce.
  1066. * @param false|string $query_arg Optional. Key to check for the nonce in `$_REQUEST` (since 2.5). If false,
  1067. * `$_REQUEST` values will be evaluated for '_ajax_nonce', and '_wpnonce'
  1068. * (in that order). Default false.
  1069. * @param bool $die Optional. Whether to die early when the nonce cannot be verified.
  1070. * Default true.
  1071. * @return int|false 1 if the nonce is valid and generated between 0-12 hours ago,
  1072. * 2 if the nonce is valid and generated between 12-24 hours ago.
  1073. * False if the nonce is invalid.
  1074. */
  1075. function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
  1076. if ( -1 == $action ) {
  1077. _doing_it_wrong( __FUNCTION__, __( 'You should specify an action to be verified by using the first parameter.' ), '4.7.0' );
  1078. }
  1079. $nonce = '';
  1080. if ( $query_arg && isset( $_REQUEST[ $query_arg ] ) ) {
  1081. $nonce = $_REQUEST[ $query_arg ];
  1082. } elseif ( isset( $_REQUEST['_ajax_nonce'] ) ) {
  1083. $nonce = $_REQUEST['_ajax_nonce'];
  1084. } elseif ( isset( $_REQUEST['_wpnonce'] ) ) {
  1085. $nonce = $_REQUEST['_wpnonce'];
  1086. }
  1087. $result = wp_verify_nonce( $nonce, $action );
  1088. /**
  1089. * Fires once the Ajax request has been validated or not.
  1090. *
  1091. * @since 2.1.0
  1092. *
  1093. * @param string $action The Ajax nonce action.
  1094. * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between
  1095. * 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
  1096. */
  1097. do_action( 'check_ajax_referer', $action, $result );
  1098. if ( $die && false === $result ) {
  1099. if ( wp_doing_ajax() ) {
  1100. wp_die( -1, 403 );
  1101. } else {
  1102. die( '-1' );
  1103. }
  1104. }
  1105. return $result;
  1106. }
  1107. endif;
  1108. if ( ! function_exists( 'wp_redirect' ) ) :
  1109. /**
  1110. * Redirects to another page.
  1111. *
  1112. * Note: wp_redirect() does not exit automatically, and should almost always be
  1113. * followed by a call to `exit;`:
  1114. *
  1115. * wp_redirect( $url );
  1116. * exit;
  1117. *
  1118. * Exiting can also be selectively manipulated by using wp_redirect() as a conditional
  1119. * in conjunction with the {@see 'wp_redirect'} and {@see 'wp_redirect_location'} filters:
  1120. *
  1121. * if ( wp_redirect( $url ) ) {
  1122. * exit;
  1123. * }
  1124. *
  1125. * @since 1.5.1
  1126. * @since 5.1.0 The `$x_redirect_by` parameter was added.
  1127. * @since 5.4.0 On invalid status codes, wp_die() is called.
  1128. *
  1129. * @global bool $is_IIS
  1130. *
  1131. * @param string $location The path or URL to redirect to.
  1132. * @param int $status Optional. HTTP response status code to use. Default '302' (Moved Temporarily).
  1133. * @param string $x_redirect_by Optional. The application doing the redirect. Default 'WordPress'.
  1134. * @return bool False if the redirect was cancelled, true otherwise.
  1135. */
  1136. function wp_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) {
  1137. global $is_IIS;
  1138. /**
  1139. * Filters the redirect location.
  1140. *
  1141. * @since 2.1.0
  1142. *
  1143. * @param string $location The path or URL to redirect to.
  1144. * @param int $status The HTTP response status code to use.
  1145. */
  1146. $location = apply_filters( 'wp_redirect', $location, $status );
  1147. /**
  1148. * Filters the redirect HTTP response status code to use.
  1149. *
  1150. * @since 2.3.0
  1151. *
  1152. * @param int $status The HTTP response status code to use.
  1153. * @param string $location The path or URL to redirect to.
  1154. */
  1155. $status = apply_filters( 'wp_redirect_status', $status, $location );
  1156. if ( ! $location ) {
  1157. return false;
  1158. }
  1159. if ( $status < 300 || 399 < $status ) {
  1160. wp_die( __( 'HTTP redirect status code must be a redirection code, 3xx.' ) );
  1161. }
  1162. $location = wp_sanitize_redirect( $location );
  1163. if ( ! $is_IIS && 'cgi-fcgi' !== PHP_SAPI ) {
  1164. status_header( $status ); // This causes problems on IIS and some FastCGI setups.
  1165. }
  1166. /**
  1167. * Filters the X-Redirect-By header.
  1168. *
  1169. * Allows applications to identify themselves when they're doing a redirect.
  1170. *
  1171. * @since 5.1.0
  1172. *
  1173. * @param string $x_redirect_by The application doing the redirect.
  1174. * @param int $status Status code to use.
  1175. * @param string $location The path to redirect to.
  1176. */
  1177. $x_redirect_by = apply_filters( 'x_redirect_by', $x_redirect_by, $status, $location );
  1178. if ( is_string( $x_redirect_by ) ) {
  1179. header( "X-Redirect-By: $x_redirect_by" );
  1180. }
  1181. header( "Location: $location", true, $status );
  1182. return true;
  1183. }
  1184. endif;
  1185. if ( ! function_exists( 'wp_sanitize_redirect' ) ) :
  1186. /**
  1187. * Sanitizes a URL for use in a redirect.
  1188. *
  1189. * @since 2.3.0
  1190. *
  1191. * @param string $location The path to redirect to.
  1192. * @return string Redirect-sanitized URL.
  1193. */
  1194. function wp_sanitize_redirect( $location ) {
  1195. // Encode spaces.
  1196. $location = str_replace( ' ', '%20', $location );
  1197. $regex = '/
  1198. (
  1199. (?: [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
  1200. | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
  1201. | [\xE1-\xEC][\x80-\xBF]{2}
  1202. | \xED[\x80-\x9F][\x80-\xBF]
  1203. | [\xEE-\xEF][\x80-\xBF]{2}
  1204. | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
  1205. | [\xF1-\xF3][\x80-\xBF]{3}
  1206. | \xF4[\x80-\x8F][\x80-\xBF]{2}
  1207. ){1,40} # ...one or more times
  1208. )/x';
  1209. $location = preg_replace_callback( $regex, '_wp_sanitize_utf8_in_redirect', $location );
  1210. $location = preg_replace( '|[^a-z0-9-~+_.?#=&;,/:%!*\[\]()@]|i', '', $location );
  1211. $location = wp_kses_no_null( $location );
  1212. // Remove %0D and %0A from location.
  1213. $strip = array( '%0d', '%0a', '%0D', '%0A' );
  1214. return _deep_replace( $strip, $location );
  1215. }
  1216. /**
  1217. * URL encode UTF-8 characters in a URL.
  1218. *
  1219. * @ignore
  1220. * @since 4.2.0
  1221. * @access private
  1222. *
  1223. * @see wp_sanitize_redirect()
  1224. *
  1225. * @param array $matches RegEx matches against the redirect location.
  1226. * @return string URL-encoded version of the first RegEx match.
  1227. */
  1228. function _wp_sanitize_utf8_in_redirect( $matches ) {
  1229. return urlencode( $matches[0] );
  1230. }
  1231. endif;
  1232. if ( ! function_exists( 'wp_safe_redirect' ) ) :
  1233. /**
  1234. * Performs a safe (local) redirect, using wp_redirect().
  1235. *
  1236. * Checks whether the $location is using an allowed host, if it has an absolute
  1237. * path. A plugin can therefore set or remove allowed host(s) to or from the
  1238. * list.
  1239. *
  1240. * If the host is not allowed, then the redirect defaults to wp-admin on the siteurl
  1241. * instead. This prevents malicious redirects which redirect to another host,
  1242. * but only used in a few places.
  1243. *
  1244. * Note: wp_safe_redirect() does not exit automatically, and should almost always be
  1245. * followed by a call to `exit;`:
  1246. *
  1247. * wp_safe_redirect( $url );
  1248. * exit;
  1249. *
  1250. * Exiting can also be selectively manipulated by using wp_safe_redirect() as a conditional
  1251. * in conjunction with the {@see 'wp_redirect'} and {@see 'wp_redirect_location'} filters:
  1252. *
  1253. * if ( wp_safe_redirect( $url ) ) {
  1254. * exit;
  1255. * }
  1256. *
  1257. * @since 2.3.0
  1258. * @since 5.1.0 The return value from wp_redirect() is now passed on, and the `$x_redirect_by` parameter was added.
  1259. *
  1260. * @param string $location The path or URL to redirect to.
  1261. * @param int $status Optional. HTTP response status code to use. Default '302' (Moved Temporarily).
  1262. * @param string $x_redirect_by Optional. The application doing the redirect. Default 'WordPress'.
  1263. * @return bool False if the redirect was cancelled, true otherwise.
  1264. */
  1265. function wp_safe_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) {
  1266. // Need to look at the URL the way it will end up in wp_redirect().
  1267. $location = wp_sanitize_redirect( $location );
  1268. /**
  1269. * Filters the redirect fallback URL for when the provided redirect is not safe (local).
  1270. *
  1271. * @since 4.3.0
  1272. *
  1273. * @param string $fallback_url The fallback URL to use by default.
  1274. * @param int $status The HTTP response status code to use.
  1275. */
  1276. $location = wp_validate_redirect( $location, apply_filters( 'wp_safe_redirect_fallback', admin_url(), $status ) );
  1277. return wp_redirect( $location, $status, $x_redirect_by );
  1278. }
  1279. endif;
  1280. if ( ! function_exists( 'wp_validate_redirect' ) ) :
  1281. /**
  1282. * Validates a URL for use in a redirect.
  1283. *
  1284. * Checks whether the $location is using an allowed host, if it has an absolute
  1285. * path. A plugin can therefore set or remove allowed host(s) to or from the
  1286. * list.
  1287. *
  1288. * If the host is not allowed, then the redirect is to $default supplied
  1289. *
  1290. * @since 2.8.1
  1291. *
  1292. * @param string $location The redirect to validate
  1293. * @param string $default The value to return if $location is not allowed
  1294. * @return string redirect-sanitized URL
  1295. */
  1296. function wp_validate_redirect( $location, $default = '' ) {
  1297. $location = wp_sanitize_redirect( trim( $location, " \t\n\r\0\x08\x0B" ) );
  1298. // Browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'.
  1299. if ( '//' === substr( $location, 0, 2 ) ) {
  1300. $location = 'http:' . $location;
  1301. }
  1302. // In PHP 5 parse_url() may fail if the URL query part contains 'http://'.
  1303. // See https://bugs.php.net/bug.php?id=38143
  1304. $cut = strpos( $location, '?' );
  1305. $test = $cut ? substr( $location, 0, $cut ) : $location;
  1306. $lp = parse_url( $test );
  1307. // Give up if malformed URL.
  1308. if ( false === $lp ) {
  1309. return $default;
  1310. }
  1311. // Allow only 'http' and 'https' schemes. No 'data:', etc.
  1312. if ( isset( $lp['scheme'] ) && ! ( 'http' === $lp['scheme'] || 'https' === $lp['scheme'] ) ) {
  1313. return $default;
  1314. }
  1315. if ( ! isset( $lp['host'] ) && ! empty( $lp['path'] ) && '/' !== $lp['path'][0] ) {
  1316. $path = '';
  1317. if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
  1318. $path = dirname( parse_url( 'http://placeholder' . $_SERVER['REQUEST_URI'], PHP_URL_PATH ) . '?' );
  1319. $path = wp_normalize_path( $path );
  1320. }
  1321. $location = '/' . ltrim( $path . '/', '/' ) . $location;
  1322. }
  1323. // Reject if certain components are set but host is not.
  1324. // This catches URLs like https:host.com for which parse_url() does not set the host field.
  1325. if ( ! isset( $lp['host'] ) && ( isset( $lp['scheme'] ) || isset( $lp['user'] ) || isset( $lp['pass'] ) || isset( $lp['port'] ) ) ) {
  1326. return $default;
  1327. }
  1328. // Reject malformed components parse_url() can return on odd inputs.
  1329. foreach ( array( 'user', 'pass', 'host' ) as $component ) {
  1330. if ( isset( $lp[ $component ] ) && strpbrk( $lp[ $component ], ':/?#@' ) ) {
  1331. return $default;
  1332. }
  1333. }
  1334. $wpp = parse_url( home_url() );
  1335. /**
  1336. * Filters the list of allowed hosts to redirect to.
  1337. *
  1338. * @since 2.3.0
  1339. *
  1340. * @param string[] $hosts An array of allowed host names.
  1341. * @param string $host The host name of the redirect destination; empty string if not set.
  1342. */
  1343. $allowed_hosts = (array) apply_filters( 'allowed_redirect_hosts', array( $wpp['host'] ), isset( $lp['host'] ) ? $lp['host'] : '' );
  1344. if ( isset( $lp['host'] ) && ( ! in_array( $lp['host'], $allowed_hosts, true ) && strtolower( $wpp['host'] ) !== $lp['host'] ) ) {
  1345. $location = $default;
  1346. }
  1347. return $location;
  1348. }
  1349. endif;
  1350. if ( ! function_exists( 'wp_notify_postauthor' ) ) :
  1351. /**
  1352. * Notify an author (and/or others) of a comment/trackback/pingback on a post.
  1353. *
  1354. * @since 1.0.0
  1355. *
  1356. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1357. * @param string $deprecated Not used
  1358. * @return bool True on completion. False if no email addresses were specified.
  1359. */
  1360. function wp_notify_postauthor( $comment_id, $deprecated = null ) {
  1361. if ( null !== $deprecated ) {
  1362. _deprecated_argument( __FUNCTION__, '3.8.0' );
  1363. }
  1364. $comment = get_comment( $comment_id );
  1365. if ( empty( $comment ) || empty( $comment->comment_post_ID ) ) {
  1366. return false;
  1367. }
  1368. $post = get_post( $comment->comment_post_ID );
  1369. $author = get_userdata( $post->post_author );
  1370. // Who to notify? By default, just the post author, but others can be added.
  1371. $emails = array();
  1372. if ( $author ) {
  1373. $emails[] = $author->user_email;
  1374. }
  1375. /**
  1376. * Filters the list of email addresses to receive a comment notification.
  1377. *
  1378. * By default, only post authors are notified of comments. This filter allows
  1379. * others to be added.
  1380. *
  1381. * @since 3.7.0
  1382. *
  1383. * @param string[] $emails An array of email addresses to receive a comment notification.
  1384. * @param int $comment_id The comment ID.
  1385. */
  1386. $emails = apply_filters( 'comment_notification_recipients', $emails, $comment->comment_ID );
  1387. $emails = array_filter( $emails );
  1388. // If there are no addresses to send the comment to, bail.
  1389. if ( ! count( $emails ) ) {
  1390. return false;
  1391. }
  1392. // Facilitate unsetting below without knowing the keys.
  1393. $emails = array_flip( $emails );
  1394. /**
  1395. * Filters whether to notify comment authors of their comments on their own posts.
  1396. *
  1397. * By default, comment authors aren't notified of their comments on their own
  1398. * posts. This filter allows you to override that.
  1399. *
  1400. * @since 3.8.0
  1401. *
  1402. * @param bool $notify Whether to notify the post author of their own comment.
  1403. * Default false.
  1404. * @param int $comment_id The comment ID.
  1405. */
  1406. $notify_author = apply_filters( 'comment_notification_notify_author', false, $comment->comment_ID );
  1407. // The comment was left by the author.
  1408. if ( $author && ! $notify_author && $comment->user_id == $post->post_author ) {
  1409. unset( $emails[ $author->user_email ] );
  1410. }
  1411. // The author moderated a comment on their own post.
  1412. if ( $author && ! $notify_author && get_current_user_id() == $post->post_author ) {
  1413. unset( $emails[ $author->user_email ] );
  1414. }
  1415. // The post author is no longer a member of the blog.
  1416. if ( $author && ! $notify_author && ! user_can( $post->post_author, 'read_post', $post->ID ) ) {
  1417. unset( $emails[ $author->user_email ] );
  1418. }
  1419. // If there's no email to send the comment to, bail, otherwise flip array back around for use below.
  1420. if ( ! count( $emails ) ) {
  1421. return false;
  1422. } else {
  1423. $emails = array_flip( $emails );
  1424. }
  1425. $switched_locale = switch_to_locale( get_locale() );
  1426. $comment_author_domain = '';
  1427. if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) {
  1428. $comment_author_domain = gethostbyaddr( $comment->comment_author_IP );
  1429. }
  1430. // The blogname option is escaped with esc_html() on the way into the database in sanitize_option().
  1431. // We want to reverse this for the plain text arena of emails.
  1432. $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  1433. $comment_content = wp_specialchars_decode( $comment->comment_content );
  1434. switch ( $comment->comment_type ) {
  1435. case 'trackback':
  1436. /* translators: %s: Post title. */
  1437. $notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
  1438. /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
  1439. $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1440. /* translators: %s: Trackback/pingback/comment author URL. */
  1441. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1442. /* translators: %s: Comment text. */
  1443. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  1444. $notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n";
  1445. /* translators: Trackback notification email subject. 1: Site title, 2: Post title. */
  1446. $subject = sprintf( __( '[%1$s] Trackback: "%2$s"' ), $blogname, $post->post_title );
  1447. break;
  1448. case 'pingback':
  1449. /* translators: %s: Post title. */
  1450. $notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n";
  1451. /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
  1452. $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1453. /* translators: %s: Trackback/pingback/comment author URL. */
  1454. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1455. /* translators: %s: Comment text. */
  1456. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  1457. $notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n";
  1458. /* translators: Pingback notification email subject. 1: Site title, 2: Post title. */
  1459. $subject = sprintf( __( '[%1$s] Pingback: "%2$s"' ), $blogname, $post->post_title );
  1460. break;
  1461. default: // Comments.
  1462. /* translators: %s: Post title. */
  1463. $notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
  1464. /* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */
  1465. $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1466. /* translators: %s: Comment author email. */
  1467. $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
  1468. /* translators: %s: Trackback/pingback/comment author URL. */
  1469. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1470. if ( $comment->comment_parent && user_can( $post->post_author, 'edit_comment', $comment->comment_parent ) ) {
  1471. /* translators: Comment moderation. %s: Parent comment edit URL. */
  1472. $notify_message .= sprintf( __( 'In reply to: %s' ), admin_url( "comment.php?action=editcomment&c={$comment->comment_parent}#wpbody-content" ) ) . "\r\n";
  1473. }
  1474. /* translators: %s: Comment text. */
  1475. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  1476. $notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n";
  1477. /* translators: Comment notification email subject. 1: Site title, 2: Post title. */
  1478. $subject = sprintf( __( '[%1$s] Comment: "%2$s"' ), $blogname, $post->post_title );
  1479. break;
  1480. }
  1481. $notify_message .= get_permalink( $comment->comment_post_ID ) . "#comments\r\n\r\n";
  1482. /* translators: %s: Comment URL. */
  1483. $notify_message .= sprintf( __( 'Permalink: %s' ), get_comment_link( $comment ) ) . "\r\n";
  1484. if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) {
  1485. if ( EMPTY_TRASH_DAYS ) {
  1486. /* translators: Comment moderation. %s: Comment action URL. */
  1487. $notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
  1488. } else {
  1489. /* translators: Comment moderation. %s: Comment action URL. */
  1490. $notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
  1491. }
  1492. /* translators: Comment moderation. %s: Comment action URL. */
  1493. $notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
  1494. }
  1495. $wp_email = 'wordpress@' . preg_replace( '#^www\.#', '', wp_parse_url( network_home_url(), PHP_URL_HOST ) );
  1496. if ( '' === $comment->comment_author ) {
  1497. $from = "From: \"$blogname\" <$wp_email>";
  1498. if ( '' !== $comment->comment_author_email ) {
  1499. $reply_to = "Reply-To: $comment->comment_author_email";
  1500. }
  1501. } else {
  1502. $from = "From: \"$comment->comment_author\" <$wp_email>";
  1503. if ( '' !== $comment->comment_author_email ) {
  1504. $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
  1505. }
  1506. }
  1507. $message_headers = "$from\n"
  1508. . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n";
  1509. if ( isset( $reply_to ) ) {
  1510. $message_headers .= $reply_to . "\n";
  1511. }
  1512. /**
  1513. * Filters the comment notification email text.
  1514. *
  1515. * @since 1.5.2
  1516. *
  1517. * @param string $notify_message The comment notification email text.
  1518. * @param int $comment_id Comment ID.
  1519. */
  1520. $notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID );
  1521. /**
  1522. * Filters the comment notification email subject.
  1523. *
  1524. * @since 1.5.2
  1525. *
  1526. * @param string $subject The comment notification email subject.
  1527. * @param int $comment_id Comment ID.
  1528. */
  1529. $subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID );
  1530. /**
  1531. * Filters the comment notification email headers.
  1532. *
  1533. * @since 1.5.2
  1534. *
  1535. * @param string $message_headers Headers for the comment notification email.
  1536. * @param int $comment_id Comment ID.
  1537. */
  1538. $message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID );
  1539. foreach ( $emails as $email ) {
  1540. wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
  1541. }
  1542. if ( $switched_locale ) {
  1543. restore_previous_locale();
  1544. }
  1545. return true;
  1546. }
  1547. endif;
  1548. if ( ! function_exists( 'wp_notify_moderator' ) ) :
  1549. /**
  1550. * Notifies the moderator of the site about a new comment that is awaiting approval.
  1551. *
  1552. * @since 1.0.0
  1553. *
  1554. * @global wpdb $wpdb WordPress database abstraction object.
  1555. *
  1556. * Uses the {@see 'notify_moderator'} filter to determine whether the site moderator
  1557. * should be notified, overriding the site setting.
  1558. *
  1559. * @param int $comment_id Comment ID.
  1560. * @return true Always returns true.
  1561. */
  1562. function wp_notify_moderator( $comment_id ) {
  1563. global $wpdb;
  1564. $maybe_notify = get_option( 'moderation_notify' );
  1565. /**
  1566. * Filters whether to send the site moderator email notifications, overriding the site setting.
  1567. *
  1568. * @since 4.4.0
  1569. *
  1570. * @param bool $maybe_notify Whether to notify blog moderator.
  1571. * @param int $comment_ID The id of the comment for the notification.
  1572. */
  1573. $maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_id );
  1574. if ( ! $maybe_notify ) {
  1575. return true;
  1576. }
  1577. $comment = get_comment( $comment_id );
  1578. $post = get_post( $comment->comment_post_ID );
  1579. $user = get_userdata( $post->post_author );
  1580. // Send to the administration and to the post author if the author can modify the comment.
  1581. $emails = array( get_option( 'admin_email' ) );
  1582. if ( $user && user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) {
  1583. if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) {
  1584. $emails[] = $user->user_email;
  1585. }
  1586. }
  1587. $switched_locale = switch_to_locale( get_locale() );
  1588. $comment_author_domain = '';
  1589. if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) {
  1590. $comment_author_domain = gethostbyaddr( $comment->comment_author_IP );
  1591. }
  1592. $comments_waiting = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '0'" );
  1593. // The blogname option is escaped with esc_html() on the way into the database in sanitize_option().
  1594. // We want to reverse this for the plain text arena of emails.
  1595. $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  1596. $comment_content = wp_specialchars_decode( $comment->comment_content );
  1597. switch ( $comment->comment_type ) {
  1598. case 'trackback':
  1599. /* translators: %s: Post title. */
  1600. $notify_message = sprintf( __( 'A new trackback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
  1601. $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
  1602. /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
  1603. $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1604. /* translators: %s: Trackback/pingback/comment author URL. */
  1605. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1606. $notify_message .= __( 'Trackback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n";
  1607. break;
  1608. case 'pingback':
  1609. /* translators: %s: Post title. */
  1610. $notify_message = sprintf( __( 'A new pingback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
  1611. $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
  1612. /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
  1613. $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1614. /* translators: %s: Trackback/pingback/comment author URL. */
  1615. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1616. $notify_message .= __( 'Pingback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n";
  1617. break;
  1618. default: // Comments.
  1619. /* translators: %s: Post title. */
  1620. $notify_message = sprintf( __( 'A new comment on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
  1621. $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
  1622. /* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */
  1623. $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1624. /* translators: %s: Comment author email. */
  1625. $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
  1626. /* translators: %s: Trackback/pingback/comment author URL. */
  1627. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1628. if ( $comment->comment_parent ) {
  1629. /* translators: Comment moderation. %s: Parent comment edit URL. */
  1630. $notify_message .= sprintf( __( 'In reply to: %s' ), admin_url( "comment.php?action=editcomment&c={$comment->comment_parent}#wpbody-content" ) ) . "\r\n";
  1631. }
  1632. /* translators: %s: Comment text. */
  1633. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  1634. break;
  1635. }
  1636. /* translators: Comment moderation. %s: Comment action URL. */
  1637. $notify_message .= sprintf( __( 'Approve it: %s' ), admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) ) . "\r\n";
  1638. if ( EMPTY_TRASH_DAYS ) {
  1639. /* translators: Comment moderation. %s: Comment action URL. */
  1640. $notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) ) . "\r\n";
  1641. } else {
  1642. /* translators: Comment moderation. %s: Comment action URL. */
  1643. $notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) ) . "\r\n";
  1644. }
  1645. /* translators: Comment moderation. %s: Comment action URL. */
  1646. $notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) ) . "\r\n";
  1647. $notify_message .= sprintf(
  1648. /* translators: Comment moderation. %s: Number of comments awaiting approval. */
  1649. _n(
  1650. 'Currently %s comment is waiting for approval. Please visit the moderation panel:',
  1651. 'Currently %s comments are waiting for approval. Please visit the moderation panel:',
  1652. $comments_waiting
  1653. ),
  1654. number_format_i18n( $comments_waiting )
  1655. ) . "\r\n";
  1656. $notify_message .= admin_url( 'edit-comments.php?comment_status=moderated#wpbody-content' ) . "\r\n";
  1657. /* translators: Comment moderation notification email subject. 1: Site title, 2: Post title. */
  1658. $subject = sprintf( __( '[%1$s] Please moderate: "%2$s"' ), $blogname, $post->post_title );
  1659. $message_headers = '';
  1660. /**
  1661. * Filters the list of recipients for comment moderation emails.
  1662. *
  1663. * @since 3.7.0
  1664. *
  1665. * @param string[] $emails List of email addresses to notify for comment moderation.
  1666. * @param int $comment_id Comment ID.
  1667. */
  1668. $emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id );
  1669. /**
  1670. * Filters the comment moderation email text.
  1671. *
  1672. * @since 1.5.2
  1673. *
  1674. * @param string $notify_message Text of the comment moderation email.
  1675. * @param int $comment_id Comment ID.
  1676. */
  1677. $notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id );
  1678. /**
  1679. * Filters the comment moderation email subject.
  1680. *
  1681. * @since 1.5.2
  1682. *
  1683. * @param string $subject Subject of the comment moderation email.
  1684. * @param int $comment_id Comment ID.
  1685. */
  1686. $subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id );
  1687. /**
  1688. * Filters the comment moderation email headers.
  1689. *
  1690. * @since 2.8.0
  1691. *
  1692. * @param string $message_headers Headers for the comment moderation email.
  1693. * @param int $comment_id Comment ID.
  1694. */
  1695. $message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id );
  1696. foreach ( $emails as $email ) {
  1697. wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
  1698. }
  1699. if ( $switched_locale ) {
  1700. restore_previous_locale();
  1701. }
  1702. return true;
  1703. }
  1704. endif;
  1705. if ( ! function_exists( 'wp_password_change_notification' ) ) :
  1706. /**
  1707. * Notify the blog admin of a user changing password, normally via email.
  1708. *
  1709. * @since 2.7.0
  1710. *
  1711. * @param WP_User $user User object.
  1712. */
  1713. function wp_password_change_notification( $user ) {
  1714. // Send a copy of password change notification to the admin,
  1715. // but check to see if it's the admin whose password we're changing, and skip this.
  1716. if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) {
  1717. /* translators: %s: User name. */
  1718. $message = sprintf( __( 'Password changed for user: %s' ), $user->user_login ) . "\r\n";
  1719. // The blogname option is escaped with esc_html() on the way into the database in sanitize_option().
  1720. // We want to reverse this for the plain text arena of emails.
  1721. $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  1722. $wp_password_change_notification_email = array(
  1723. 'to' => get_option( 'admin_email' ),
  1724. /* translators: Password change notification email subject. %s: Site title. */
  1725. 'subject' => __( '[%s] Password Changed' ),
  1726. 'message' => $message,
  1727. 'headers' => '',
  1728. );
  1729. /**
  1730. * Filters the contents of the password change notification email sent to the site admin.
  1731. *
  1732. * @since 4.9.0
  1733. *
  1734. * @param array $wp_password_change_notification_email {
  1735. * Used to build wp_mail().
  1736. *
  1737. * @type string $to The intended recipient - site admin email address.
  1738. * @type string $subject The subject of the email.
  1739. * @type string $message The body of the email.
  1740. * @type string $headers The headers of the email.
  1741. * }
  1742. * @param WP_User $user User object for user whose password was changed.
  1743. * @param string $blogname The site title.
  1744. */
  1745. $wp_password_change_notification_email = apply_filters( 'wp_password_change_notification_email', $wp_password_change_notification_email, $user, $blogname );
  1746. wp_mail(
  1747. $wp_password_change_notification_email['to'],
  1748. wp_specialchars_decode( sprintf( $wp_password_change_notification_email['subject'], $blogname ) ),
  1749. $wp_password_change_notification_email['message'],
  1750. $wp_password_change_notification_email['headers']
  1751. );
  1752. }
  1753. }
  1754. endif;
  1755. if ( ! function_exists( 'wp_new_user_notification' ) ) :
  1756. /**
  1757. * Email login credentials to a newly-registered user.
  1758. *
  1759. * A new user registration notification is also sent to admin email.
  1760. *
  1761. * @since 2.0.0
  1762. * @since 4.3.0 The `$plaintext_pass` parameter was changed to `$notify`.
  1763. * @since 4.3.1 The `$plaintext_pass` parameter was deprecated. `$notify` added as a third parameter.
  1764. * @since 4.6.0 The `$notify` parameter accepts 'user' for sending notification only to the user created.
  1765. *
  1766. * @param int $user_id User ID.
  1767. * @param null $deprecated Not used (argument deprecated).
  1768. * @param string $notify Optional. Type of notification that should happen. Accepts 'admin' or an empty
  1769. * string (admin only), 'user', or 'both' (admin and user). Default empty.
  1770. */
  1771. function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) {
  1772. if ( null !== $deprecated ) {
  1773. _deprecated_argument( __FUNCTION__, '4.3.1' );
  1774. }
  1775. // Accepts only 'user', 'admin' , 'both' or default '' as $notify.
  1776. if ( ! in_array( $notify, array( 'user', 'admin', 'both', '' ), true ) ) {
  1777. return;
  1778. }
  1779. $user = get_userdata( $user_id );
  1780. // The blogname option is escaped with esc_html() on the way into the database in sanitize_option().
  1781. // We want to reverse this for the plain text arena of emails.
  1782. $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  1783. if ( 'user' !== $notify ) {
  1784. $switched_locale = switch_to_locale( get_locale() );
  1785. /* translators: %s: Site title. */
  1786. $message = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n";
  1787. /* translators: %s: User login. */
  1788. $message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n";
  1789. /* translators: %s: User email address. */
  1790. $message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "\r\n";
  1791. $wp_new_user_notification_email_admin = array(
  1792. 'to' => get_option( 'admin_email' ),
  1793. /* translators: New user registration notification email subject. %s: Site title. */
  1794. 'subject' => __( '[%s] New User Registration' ),
  1795. 'message' => $message,
  1796. 'headers' => '',
  1797. );
  1798. /**
  1799. * Filters the contents of the new user notification email sent to the site admin.
  1800. *
  1801. * @since 4.9.0
  1802. *
  1803. * @param array $wp_new_user_notification_email_admin {
  1804. * Used to build wp_mail().
  1805. *
  1806. * @type string $to The intended recipient - site admin email address.
  1807. * @type string $subject The subject of the email.
  1808. * @type string $message The body of the email.
  1809. * @type string $headers The headers of the email.
  1810. * }
  1811. * @param WP_User $user User object for new user.
  1812. * @param string $blogname The site title.
  1813. */
  1814. $wp_new_user_notification_email_admin = apply_filters( 'wp_new_user_notification_email_admin', $wp_new_user_notification_email_admin, $user, $blogname );
  1815. wp_mail(
  1816. $wp_new_user_notification_email_admin['to'],
  1817. wp_specialchars_decode( sprintf( $wp_new_user_notification_email_admin['subject'], $blogname ) ),
  1818. $wp_new_user_notification_email_admin['message'],
  1819. $wp_new_user_notification_email_admin['headers']
  1820. );
  1821. if ( $switched_locale ) {
  1822. restore_previous_locale();
  1823. }
  1824. }
  1825. // `$deprecated` was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification.
  1826. if ( 'admin' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) {
  1827. return;
  1828. }
  1829. $key = get_password_reset_key( $user );
  1830. if ( is_wp_error( $key ) ) {
  1831. return;
  1832. }
  1833. $switched_locale = switch_to_locale( get_user_locale( $user ) );
  1834. /* translators: %s: User login. */
  1835. $message = sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n";
  1836. $message .= __( 'To set your password, visit the following address:' ) . "\r\n\r\n";
  1837. $message .= network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user->user_login ), 'login' ) . "\r\n\r\n";
  1838. $message .= wp_login_url() . "\r\n";
  1839. $wp_new_user_notification_email = array(
  1840. 'to' => $user->user_email,
  1841. /* translators: Login details notification email subject. %s: Site title. */
  1842. 'subject' => __( '[%s] Login Details' ),
  1843. 'message' => $message,
  1844. 'headers' => '',
  1845. );
  1846. /**
  1847. * Filters the contents of the new user notification email sent to the new user.
  1848. *
  1849. * @since 4.9.0
  1850. *
  1851. * @param array $wp_new_user_notification_email {
  1852. * Used to build wp_mail().
  1853. *
  1854. * @type string $to The intended recipient - New user email address.
  1855. * @type string $subject The subject of the email.
  1856. * @type string $message The body of the email.
  1857. * @type string $headers The headers of the email.
  1858. * }
  1859. * @param WP_User $user User object for new user.
  1860. * @param string $blogname The site title.
  1861. */
  1862. $wp_new_user_notification_email = apply_filters( 'wp_new_user_notification_email', $wp_new_user_notification_email, $user, $blogname );
  1863. wp_mail(
  1864. $wp_new_user_notification_email['to'],
  1865. wp_specialchars_decode( sprintf( $wp_new_user_notification_email['subject'], $blogname ) ),
  1866. $wp_new_user_notification_email['message'],
  1867. $wp_new_user_notification_email['headers']
  1868. );
  1869. if ( $switched_locale ) {
  1870. restore_previous_locale();
  1871. }
  1872. }
  1873. endif;
  1874. if ( ! function_exists( 'wp_nonce_tick' ) ) :
  1875. /**
  1876. * Returns the time-dependent variable for nonce creation.
  1877. *
  1878. * A nonce has a lifespan of two ticks. Nonces in their second tick may be
  1879. * updated, e.g. by autosave.
  1880. *
  1881. * @since 2.5.0
  1882. *
  1883. * @return float Float value rounded up to the next highest integer.
  1884. */
  1885. function wp_nonce_tick() {
  1886. /**
  1887. * Filters the lifespan of nonces in seconds.
  1888. *
  1889. * @since 2.5.0
  1890. *
  1891. * @param int $lifespan Lifespan of nonces in seconds. Default 86,400 seconds, or one day.
  1892. */
  1893. $nonce_life = apply_filters( 'nonce_life', DAY_IN_SECONDS );
  1894. return ceil( time() / ( $nonce_life / 2 ) );
  1895. }
  1896. endif;
  1897. if ( ! function_exists( 'wp_verify_nonce' ) ) :
  1898. /**
  1899. * Verifies that a correct security nonce was used with time limit.
  1900. *
  1901. * A nonce is valid for 24 hours (by default).
  1902. *
  1903. * @since 2.0.3
  1904. *
  1905. * @param string $nonce Nonce value that was used for verification, usually via a form field.
  1906. * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
  1907. * @return int|false 1 if the nonce is valid and generated between 0-12 hours ago,
  1908. * 2 if the nonce is valid and generated between 12-24 hours ago.
  1909. * False if the nonce is invalid.
  1910. */
  1911. function wp_verify_nonce( $nonce, $action = -1 ) {
  1912. $nonce = (string) $nonce;
  1913. $user = wp_get_current_user();
  1914. $uid = (int) $user->ID;
  1915. if ( ! $uid ) {
  1916. /**
  1917. * Filters whether the user who generated the nonce is logged out.
  1918. *
  1919. * @since 3.5.0
  1920. *
  1921. * @param int $uid ID of the nonce-owning user.
  1922. * @param string $action The nonce action.
  1923. */
  1924. $uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
  1925. }
  1926. if ( empty( $nonce ) ) {
  1927. return false;
  1928. }
  1929. $token = wp_get_session_token();
  1930. $i = wp_nonce_tick();
  1931. // Nonce generated 0-12 hours ago.
  1932. $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
  1933. if ( hash_equals( $expected, $nonce ) ) {
  1934. return 1;
  1935. }
  1936. // Nonce generated 12-24 hours ago.
  1937. $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
  1938. if ( hash_equals( $expected, $nonce ) ) {
  1939. return 2;
  1940. }
  1941. /**
  1942. * Fires when nonce verification fails.
  1943. *
  1944. * @since 4.4.0
  1945. *
  1946. * @param string $nonce The invalid nonce.
  1947. * @param string|int $action The nonce action.
  1948. * @param WP_User $user The current user object.
  1949. * @param string $token The user's session token.
  1950. */
  1951. do_action( 'wp_verify_nonce_failed', $nonce, $action, $user, $token );
  1952. // Invalid nonce.
  1953. return false;
  1954. }
  1955. endif;
  1956. if ( ! function_exists( 'wp_create_nonce' ) ) :
  1957. /**
  1958. * Creates a cryptographic token tied to a specific action, user, user session,
  1959. * and window of time.
  1960. *
  1961. * @since 2.0.3
  1962. * @since 4.0.0 Session tokens were integrated with nonce creation
  1963. *
  1964. * @param string|int $action Scalar value to add context to the nonce.
  1965. * @return string The token.
  1966. */
  1967. function wp_create_nonce( $action = -1 ) {
  1968. $user = wp_get_current_user();
  1969. $uid = (int) $user->ID;
  1970. if ( ! $uid ) {
  1971. /** This filter is documented in wp-includes/pluggable.php */
  1972. $uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
  1973. }
  1974. $token = wp_get_session_token();
  1975. $i = wp_nonce_tick();
  1976. return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
  1977. }
  1978. endif;
  1979. if ( ! function_exists( 'wp_salt' ) ) :
  1980. /**
  1981. * Returns a salt to add to hashes.
  1982. *
  1983. * Salts are created using secret keys. Secret keys are located in two places:
  1984. * in the database and in the wp-config.php file. The secret key in the database
  1985. * is randomly generated and will be appended to the secret keys in wp-config.php.
  1986. *
  1987. * The secret keys in wp-config.php should be updated to strong, random keys to maximize
  1988. * security. Below is an example of how the secret key constants are defined.
  1989. * Do not paste this example directly into wp-config.php. Instead, have a
  1990. * {@link https://api.wordpress.org/secret-key/1.1/salt/ secret key created} just
  1991. * for you.
  1992. *
  1993. * define('AUTH_KEY', ' Xakm<o xQy rw4EMsLKM-?!T+,PFF})H4lzcW57AF0U@N@< >M%G4Yt>f`z]MON');
  1994. * define('SECURE_AUTH_KEY', 'LzJ}op]mr|6+![P}Ak:uNdJCJZd>(Hx.-Mh#Tz)pCIU#uGEnfFz|f ;;eU%/U^O~');
  1995. * define('LOGGED_IN_KEY', '|i|Ux`9<p-h$aFf(qnT:sDO:D1P^wZ$$/Ra@miTJi9G;ddp_<q}6H1)o|a +&JCM');
  1996. * define('NONCE_KEY', '%:R{[P|,s.KuMltH5}cI;/k<Gx~j!f0I)m_sIyu+&NJZ)-iO>z7X>QYR0Z_XnZ@|');
  1997. * define('AUTH_SALT', 'eZyT)-Naw]F8CwA*VaW#q*|.)g@o}||wf~@C-YSt}(dh_r6EbI#A,y|nU2{B#JBW');
  1998. * define('SECURE_AUTH_SALT', '!=oLUTXh,QW=H `}`L|9/^4-3 STz},T(w}W<I`.JjPi)<Bmf1v,HpGe}T1:Xt7n');
  1999. * define('LOGGED_IN_SALT', '+XSqHc;@Q*K_b|Z?NC[3H!!EONbh.n<+=uKR:>*c(u`g~EJBf#8u#R{mUEZrozmm');
  2000. * define('NONCE_SALT', 'h`GXHhD>SLWVfg1(1(N{;.V!MoE(SfbA_ksP@&`+AycHcAV$+?@3q+rxV{%^VyKT');
  2001. *
  2002. * Salting passwords helps against tools which has stored hashed values of
  2003. * common dictionary strings. The added values makes it harder to crack.
  2004. *
  2005. * @since 2.5.0
  2006. *
  2007. * @link https://api.wordpress.org/secret-key/1.1/salt/ Create secrets for wp-config.php
  2008. *
  2009. * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce)
  2010. * @return string Salt value
  2011. */
  2012. function wp_salt( $scheme = 'auth' ) {
  2013. static $cached_salts = array();
  2014. if ( isset( $cached_salts[ $scheme ] ) ) {
  2015. /**
  2016. * Filters the WordPress salt.
  2017. *
  2018. * @since 2.5.0
  2019. *
  2020. * @param string $cached_salt Cached salt for the given scheme.
  2021. * @param string $scheme Authentication scheme. Values include 'auth',
  2022. * 'secure_auth', 'logged_in', and 'nonce'.
  2023. */
  2024. return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme );
  2025. }
  2026. static $duplicated_keys;
  2027. if ( null === $duplicated_keys ) {
  2028. $duplicated_keys = array( 'put your unique phrase here' => true );
  2029. foreach ( array( 'AUTH', 'SECURE_AUTH', 'LOGGED_IN', 'NONCE', 'SECRET' ) as $first ) {
  2030. foreach ( array( 'KEY', 'SALT' ) as $second ) {
  2031. if ( ! defined( "{$first}_{$second}" ) ) {
  2032. continue;
  2033. }
  2034. $value = constant( "{$first}_{$second}" );
  2035. $duplicated_keys[ $value ] = isset( $duplicated_keys[ $value ] );
  2036. }
  2037. }
  2038. }
  2039. $values = array(
  2040. 'key' => '',
  2041. 'salt' => '',
  2042. );
  2043. if ( defined( 'SECRET_KEY' ) && SECRET_KEY && empty( $duplicated_keys[ SECRET_KEY ] ) ) {
  2044. $values['key'] = SECRET_KEY;
  2045. }
  2046. if ( 'auth' === $scheme && defined( 'SECRET_SALT' ) && SECRET_SALT && empty( $duplicated_keys[ SECRET_SALT ] ) ) {
  2047. $values['salt'] = SECRET_SALT;
  2048. }
  2049. if ( in_array( $scheme, array( 'auth', 'secure_auth', 'logged_in', 'nonce' ), true ) ) {
  2050. foreach ( array( 'key', 'salt' ) as $type ) {
  2051. $const = strtoupper( "{$scheme}_{$type}" );
  2052. if ( defined( $const ) && constant( $const ) && empty( $duplicated_keys[ constant( $const ) ] ) ) {
  2053. $values[ $type ] = constant( $const );
  2054. } elseif ( ! $values[ $type ] ) {
  2055. $values[ $type ] = get_site_option( "{$scheme}_{$type}" );
  2056. if ( ! $values[ $type ] ) {
  2057. $values[ $type ] = wp_generate_password( 64, true, true );
  2058. update_site_option( "{$scheme}_{$type}", $values[ $type ] );
  2059. }
  2060. }
  2061. }
  2062. } else {
  2063. if ( ! $values['key'] ) {
  2064. $values['key'] = get_site_option( 'secret_key' );
  2065. if ( ! $values['key'] ) {
  2066. $values['key'] = wp_generate_password( 64, true, true );
  2067. update_site_option( 'secret_key', $values['key'] );
  2068. }
  2069. }
  2070. $values['salt'] = hash_hmac( 'md5', $scheme, $values['key'] );
  2071. }
  2072. $cached_salts[ $scheme ] = $values['key'] . $values['salt'];
  2073. /** This filter is documented in wp-includes/pluggable.php */
  2074. return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme );
  2075. }
  2076. endif;
  2077. if ( ! function_exists( 'wp_hash' ) ) :
  2078. /**
  2079. * Get hash of given string.
  2080. *
  2081. * @since 2.0.3
  2082. *
  2083. * @param string $data Plain text to hash
  2084. * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce)
  2085. * @return string Hash of $data
  2086. */
  2087. function wp_hash( $data, $scheme = 'auth' ) {
  2088. $salt = wp_salt( $scheme );
  2089. return hash_hmac( 'md5', $data, $salt );
  2090. }
  2091. endif;
  2092. if ( ! function_exists( 'wp_hash_password' ) ) :
  2093. /**
  2094. * Create a hash (encrypt) of a plain text password.
  2095. *
  2096. * For integration with other applications, this function can be overwritten to
  2097. * instead use the other package password checking algorithm.
  2098. *
  2099. * @since 2.5.0
  2100. *
  2101. * @global PasswordHash $wp_hasher PHPass object
  2102. *
  2103. * @param string $password Plain text user password to hash
  2104. * @return string The hash string of the password
  2105. */
  2106. function wp_hash_password( $password ) {
  2107. global $wp_hasher;
  2108. if ( empty( $wp_hasher ) ) {
  2109. require_once ABSPATH . WPINC . '/class-phpass.php';
  2110. // By default, use the portable hash from phpass.
  2111. $wp_hasher = new PasswordHash( 8, true );
  2112. }
  2113. return $wp_hasher->HashPassword( trim( $password ) );
  2114. }
  2115. endif;
  2116. if ( ! function_exists( 'wp_check_password' ) ) :
  2117. /**
  2118. * Checks the plaintext password against the encrypted Password.
  2119. *
  2120. * Maintains compatibility between old version and the new cookie authentication
  2121. * protocol using PHPass library. The $hash parameter is the encrypted password
  2122. * and the function compares the plain text password when encrypted similarly
  2123. * against the already encrypted password to see if they match.
  2124. *
  2125. * For integration with other applications, this function can be overwritten to
  2126. * instead use the other package password checking algorithm.
  2127. *
  2128. * @since 2.5.0
  2129. *
  2130. * @global PasswordHash $wp_hasher PHPass object used for checking the password
  2131. * against the $hash + $password
  2132. * @uses PasswordHash::CheckPassword
  2133. *
  2134. * @param string $password Plaintext user's password
  2135. * @param string $hash Hash of the user's password to check against.
  2136. * @param string|int $user_id Optional. User ID.
  2137. * @return bool False, if the $password does not match the hashed password
  2138. */
  2139. function wp_check_password( $password, $hash, $user_id = '' ) {
  2140. global $wp_hasher;
  2141. // If the hash is still md5...
  2142. if ( strlen( $hash ) <= 32 ) {
  2143. $check = hash_equals( $hash, md5( $password ) );
  2144. if ( $check && $user_id ) {
  2145. // Rehash using new hash.
  2146. wp_set_password( $password, $user_id );
  2147. $hash = wp_hash_password( $password );
  2148. }
  2149. /**
  2150. * Filters whether the plaintext password matches the encrypted password.
  2151. *
  2152. * @since 2.5.0
  2153. *
  2154. * @param bool $check Whether the passwords match.
  2155. * @param string $password The plaintext password.
  2156. * @param string $hash The hashed password.
  2157. * @param string|int $user_id User ID. Can be empty.
  2158. */
  2159. return apply_filters( 'check_password', $check, $password, $hash, $user_id );
  2160. }
  2161. // If the stored hash is longer than an MD5,
  2162. // presume the new style phpass portable hash.
  2163. if ( empty( $wp_hasher ) ) {
  2164. require_once ABSPATH . WPINC . '/class-phpass.php';
  2165. // By default, use the portable hash from phpass.
  2166. $wp_hasher = new PasswordHash( 8, true );
  2167. }
  2168. $check = $wp_hasher->CheckPassword( $password, $hash );
  2169. /** This filter is documented in wp-includes/pluggable.php */
  2170. return apply_filters( 'check_password', $check, $password, $hash, $user_id );
  2171. }
  2172. endif;
  2173. if ( ! function_exists( 'wp_generate_password' ) ) :
  2174. /**
  2175. * Generates a random password drawn from the defined set of characters.
  2176. *
  2177. * Uses wp_rand() is used to create passwords with far less predictability
  2178. * than similar native PHP functions like `rand()` or `mt_rand()`.
  2179. *
  2180. * @since 2.5.0
  2181. *
  2182. * @param int $length Optional. The length of password to generate. Default 12.
  2183. * @param bool $special_chars Optional. Whether to include standard special characters.
  2184. * Default true.
  2185. * @param bool $extra_special_chars Optional. Whether to include other special characters.
  2186. * Used when generating secret keys and salts. Default false.
  2187. * @return string The random password.
  2188. */
  2189. function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
  2190. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  2191. if ( $special_chars ) {
  2192. $chars .= '!@#$%^&*()';
  2193. }
  2194. if ( $extra_special_chars ) {
  2195. $chars .= '-_ []{}<>~`+=,.;:/?|';
  2196. }
  2197. $password = '';
  2198. for ( $i = 0; $i < $length; $i++ ) {
  2199. $password .= substr( $chars, wp_rand( 0, strlen( $chars ) - 1 ), 1 );
  2200. }
  2201. /**
  2202. * Filters the randomly-generated password.
  2203. *
  2204. * @since 3.0.0
  2205. * @since 5.3.0 Added the `$length`, `$special_chars`, and `$extra_special_chars` parameters.
  2206. *
  2207. * @param string $password The generated password.
  2208. * @param int $length The length of password to generate.
  2209. * @param bool $special_chars Whether to include standard special characters.
  2210. * @param bool $extra_special_chars Whether to include other special characters.
  2211. */
  2212. return apply_filters( 'random_password', $password, $length, $special_chars, $extra_special_chars );
  2213. }
  2214. endif;
  2215. if ( ! function_exists( 'wp_rand' ) ) :
  2216. /**
  2217. * Generates a random number.
  2218. *
  2219. * @since 2.6.2
  2220. * @since 4.4.0 Uses PHP7 random_int() or the random_compat library if available.
  2221. *
  2222. * @global string $rnd_value
  2223. *
  2224. * @param int $min Lower limit for the generated number
  2225. * @param int $max Upper limit for the generated number
  2226. * @return int A random number between min and max
  2227. */
  2228. function wp_rand( $min = 0, $max = 0 ) {
  2229. global $rnd_value;
  2230. // Some misconfigured 32-bit environments (Entropy PHP, for example)
  2231. // truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats.
  2232. $max_random_number = 3000000000 === 2147483647 ? (float) '4294967295' : 4294967295; // 4294967295 = 0xffffffff
  2233. // We only handle ints, floats are truncated to their integer value.
  2234. $min = (int) $min;
  2235. $max = (int) $max;
  2236. // Use PHP's CSPRNG, or a compatible method.
  2237. static $use_random_int_functionality = true;
  2238. if ( $use_random_int_functionality ) {
  2239. try {
  2240. $_max = ( 0 != $max ) ? $max : $max_random_number;
  2241. // wp_rand() can accept arguments in either order, PHP cannot.
  2242. $_max = max( $min, $_max );
  2243. $_min = min( $min, $_max );
  2244. $val = random_int( $_min, $_max );
  2245. if ( false !== $val ) {
  2246. return absint( $val );
  2247. } else {
  2248. $use_random_int_functionality = false;
  2249. }
  2250. } catch ( Error $e ) {
  2251. $use_random_int_functionality = false;
  2252. } catch ( Exception $e ) {
  2253. $use_random_int_functionality = false;
  2254. }
  2255. }
  2256. // Reset $rnd_value after 14 uses.
  2257. // 32 (md5) + 40 (sha1) + 40 (sha1) / 8 = 14 random numbers from $rnd_value.
  2258. if ( strlen( $rnd_value ) < 8 ) {
  2259. if ( defined( 'WP_SETUP_CONFIG' ) ) {
  2260. static $seed = '';
  2261. } else {
  2262. $seed = get_transient( 'random_seed' );
  2263. }
  2264. $rnd_value = md5( uniqid( microtime() . mt_rand(), true ) . $seed );
  2265. $rnd_value .= sha1( $rnd_value );
  2266. $rnd_value .= sha1( $rnd_value . $seed );
  2267. $seed = md5( $seed . $rnd_value );
  2268. if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) {
  2269. set_transient( 'random_seed', $seed );
  2270. }
  2271. }
  2272. // Take the first 8 digits for our value.
  2273. $value = substr( $rnd_value, 0, 8 );
  2274. // Strip the first eight, leaving the remainder for the next call to wp_rand().
  2275. $rnd_value = substr( $rnd_value, 8 );
  2276. $value = abs( hexdec( $value ) );
  2277. // Reduce the value to be within the min - max range.
  2278. if ( 0 != $max ) {
  2279. $value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 );
  2280. }
  2281. return abs( (int) $value );
  2282. }
  2283. endif;
  2284. if ( ! function_exists( 'wp_set_password' ) ) :
  2285. /**
  2286. * Updates the user's password with a new encrypted one.
  2287. *
  2288. * For integration with other applications, this function can be overwritten to
  2289. * instead use the other package password checking algorithm.
  2290. *
  2291. * Please note: This function should be used sparingly and is really only meant for single-time
  2292. * application. Leveraging this improperly in a plugin or theme could result in an endless loop
  2293. * of password resets if precautions are not taken to ensure it does not execute on every page load.
  2294. *
  2295. * @since 2.5.0
  2296. *
  2297. * @global wpdb $wpdb WordPress database abstraction object.
  2298. *
  2299. * @param string $password The plaintext new user password
  2300. * @param int $user_id User ID
  2301. */
  2302. function wp_set_password( $password, $user_id ) {
  2303. global $wpdb;
  2304. $hash = wp_hash_password( $password );
  2305. $wpdb->update(
  2306. $wpdb->users,
  2307. array(
  2308. 'user_pass' => $hash,
  2309. 'user_activation_key' => '',
  2310. ),
  2311. array( 'ID' => $user_id )
  2312. );
  2313. clean_user_cache( $user_id );
  2314. }
  2315. endif;
  2316. if ( ! function_exists( 'get_avatar' ) ) :
  2317. /**
  2318. * Retrieve the avatar `<img>` tag for a user, email address, MD5 hash, comment, or post.
  2319. *
  2320. * @since 2.5.0
  2321. * @since 4.2.0 Optional `$args` parameter added.
  2322. *
  2323. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
  2324. * user email, WP_User object, WP_Post object, or WP_Comment object.
  2325. * @param int $size Optional. Height and width of the avatar image file in pixels. Default 96.
  2326. * @param string $default Optional. URL for the default image or a default type. Accepts '404'
  2327. * (return a 404 instead of a default image), 'retro' (8bit), 'monsterid'
  2328. * (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"),
  2329. * 'mystery', 'mm', or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF),
  2330. * or 'gravatar_default' (the Gravatar logo). Default is the value of the
  2331. * 'avatar_default' option, with a fallback of 'mystery'.
  2332. * @param string $alt Optional. Alternative text to use in img tag. Default empty.
  2333. * @param array $args {
  2334. * Optional. Extra arguments to retrieve the avatar.
  2335. *
  2336. * @type int $height Display height of the avatar in pixels. Defaults to $size.
  2337. * @type int $width Display width of the avatar in pixels. Defaults to $size.
  2338. * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
  2339. * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
  2340. * judged in that order. Default is the value of the 'avatar_rating' option.
  2341. * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
  2342. * Default null.
  2343. * @type array|string $class Array or string of additional classes to add to the img element.
  2344. * Default null.
  2345. * @type bool $force_display Whether to always show the avatar - ignores the show_avatars option.
  2346. * Default false.
  2347. * @type string $loading Value for the `loading` attribute.
  2348. * Default null.
  2349. * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
  2350. * }
  2351. * @return string|false `<img>` tag for the user's avatar. False on failure.
  2352. */
  2353. function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) {
  2354. $defaults = array(
  2355. // get_avatar_data() args.
  2356. 'size' => 96,
  2357. 'height' => null,
  2358. 'width' => null,
  2359. 'default' => get_option( 'avatar_default', 'mystery' ),
  2360. 'force_default' => false,
  2361. 'rating' => get_option( 'avatar_rating' ),
  2362. 'scheme' => null,
  2363. 'alt' => '',
  2364. 'class' => null,
  2365. 'force_display' => false,
  2366. 'loading' => null,
  2367. 'extra_attr' => '',
  2368. );
  2369. if ( wp_lazy_loading_enabled( 'img', 'get_avatar' ) ) {
  2370. $defaults['loading'] = 'lazy';
  2371. }
  2372. if ( empty( $args ) ) {
  2373. $args = array();
  2374. }
  2375. $args['size'] = (int) $size;
  2376. $args['default'] = $default;
  2377. $args['alt'] = $alt;
  2378. $args = wp_parse_args( $args, $defaults );
  2379. if ( empty( $args['height'] ) ) {
  2380. $args['height'] = $args['size'];
  2381. }
  2382. if ( empty( $args['width'] ) ) {
  2383. $args['width'] = $args['size'];
  2384. }
  2385. if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
  2386. $id_or_email = get_comment( $id_or_email );
  2387. }
  2388. /**
  2389. * Allows the HTML for a user's avatar to be returned early.
  2390. *
  2391. * Passing a non-null value will effectively short-circuit get_avatar(), passing
  2392. * the value through the {@see 'get_avatar'} filter and returning early.
  2393. *
  2394. * @since 4.2.0
  2395. *
  2396. * @param string|null $avatar HTML for the user's avatar. Default null.
  2397. * @param mixed $id_or_email The avatar to retrieve. Accepts a user_id, Gravatar MD5 hash,
  2398. * user email, WP_User object, WP_Post object, or WP_Comment object.
  2399. * @param array $args Arguments passed to get_avatar_url(), after processing.
  2400. */
  2401. $avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args );
  2402. if ( ! is_null( $avatar ) ) {
  2403. /** This filter is documented in wp-includes/pluggable.php */
  2404. return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args );
  2405. }
  2406. if ( ! $args['force_display'] && ! get_option( 'show_avatars' ) ) {
  2407. return false;
  2408. }
  2409. $url2x = get_avatar_url( $id_or_email, array_merge( $args, array( 'size' => $args['size'] * 2 ) ) );
  2410. $args = get_avatar_data( $id_or_email, $args );
  2411. $url = $args['url'];
  2412. if ( ! $url || is_wp_error( $url ) ) {
  2413. return false;
  2414. }
  2415. $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
  2416. if ( ! $args['found_avatar'] || $args['force_default'] ) {
  2417. $class[] = 'avatar-default';
  2418. }
  2419. if ( $args['class'] ) {
  2420. if ( is_array( $args['class'] ) ) {
  2421. $class = array_merge( $class, $args['class'] );
  2422. } else {
  2423. $class[] = $args['class'];
  2424. }
  2425. }
  2426. // Add `loading` attribute.
  2427. $extra_attr = $args['extra_attr'];
  2428. $loading = $args['loading'];
  2429. if ( in_array( $loading, array( 'lazy', 'eager' ), true ) && ! preg_match( '/\bloading\s*=/', $extra_attr ) ) {
  2430. if ( ! empty( $extra_attr ) ) {
  2431. $extra_attr .= ' ';
  2432. }
  2433. $extra_attr .= "loading='{$loading}'";
  2434. }
  2435. $avatar = sprintf(
  2436. "<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>",
  2437. esc_attr( $args['alt'] ),
  2438. esc_url( $url ),
  2439. esc_url( $url2x ) . ' 2x',
  2440. esc_attr( implode( ' ', $class ) ),
  2441. (int) $args['height'],
  2442. (int) $args['width'],
  2443. $extra_attr
  2444. );
  2445. /**
  2446. * Filters the HTML for a user's avatar.
  2447. *
  2448. * @since 2.5.0
  2449. * @since 4.2.0 The `$args` parameter was added.
  2450. *
  2451. * @param string $avatar HTML for the user's avatar.
  2452. * @param mixed $id_or_email The avatar to retrieve. Accepts a user_id, Gravatar MD5 hash,
  2453. * user email, WP_User object, WP_Post object, or WP_Comment object.
  2454. * @param int $size Square avatar width and height in pixels to retrieve.
  2455. * @param string $default URL for the default image or a default type. Accepts '404', 'retro', 'monsterid',
  2456. * 'wavatar', 'indenticon', 'mystery', 'mm', 'mysteryman', 'blank', or 'gravatar_default'.
  2457. * Default is the value of the 'avatar_default' option, with a fallback of 'mystery'.
  2458. * @param string $alt Alternative text to use in the avatar image tag. Default empty.
  2459. * @param array $args Arguments passed to get_avatar_data(), after processing.
  2460. */
  2461. return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args );
  2462. }
  2463. endif;
  2464. if ( ! function_exists( 'wp_text_diff' ) ) :
  2465. /**
  2466. * Displays a human readable HTML representation of the difference between two strings.
  2467. *
  2468. * The Diff is available for getting the changes between versions. The output is
  2469. * HTML, so the primary use is for displaying the changes. If the two strings
  2470. * are equivalent, then an empty string will be returned.
  2471. *
  2472. * @since 2.6.0
  2473. *
  2474. * @see wp_parse_args() Used to change defaults to user defined settings.
  2475. * @uses Text_Diff
  2476. * @uses WP_Text_Diff_Renderer_Table
  2477. *
  2478. * @param string $left_string "old" (left) version of string
  2479. * @param string $right_string "new" (right) version of string
  2480. * @param string|array $args {
  2481. * Associative array of options to pass to WP_Text_Diff_Renderer_Table().
  2482. *
  2483. * @type string $title Titles the diff in a manner compatible
  2484. * with the output. Default empty.
  2485. * @type string $title_left Change the HTML to the left of the title.
  2486. * Default empty.
  2487. * @type string $title_right Change the HTML to the right of the title.
  2488. * Default empty.
  2489. * @type bool $show_split_view True for split view (two columns), false for
  2490. * un-split view (single column). Default true.
  2491. * }
  2492. * @return string Empty string if strings are equivalent or HTML with differences.
  2493. */
  2494. function wp_text_diff( $left_string, $right_string, $args = null ) {
  2495. $defaults = array(
  2496. 'title' => '',
  2497. 'title_left' => '',
  2498. 'title_right' => '',
  2499. 'show_split_view' => true,
  2500. );
  2501. $args = wp_parse_args( $args, $defaults );
  2502. if ( ! class_exists( 'WP_Text_Diff_Renderer_Table', false ) ) {
  2503. require ABSPATH . WPINC . '/wp-diff.php';
  2504. }
  2505. $left_string = normalize_whitespace( $left_string );
  2506. $right_string = normalize_whitespace( $right_string );
  2507. $left_lines = explode( "\n", $left_string );
  2508. $right_lines = explode( "\n", $right_string );
  2509. $text_diff = new Text_Diff( $left_lines, $right_lines );
  2510. $renderer = new WP_Text_Diff_Renderer_Table( $args );
  2511. $diff = $renderer->render( $text_diff );
  2512. if ( ! $diff ) {
  2513. return '';
  2514. }
  2515. $is_split_view = ! empty( $args['show_split_view'] );
  2516. $is_split_view_class = $is_split_view ? ' is-split-view' : '';
  2517. $r = "<table class='diff$is_split_view_class'>\n";
  2518. if ( $args['title'] ) {
  2519. $r .= "<caption class='diff-title'>$args[title]</caption>\n";
  2520. }
  2521. if ( $args['title_left'] || $args['title_right'] ) {
  2522. $r .= '<thead>';
  2523. }
  2524. if ( $args['title_left'] || $args['title_right'] ) {
  2525. $th_or_td_left = empty( $args['title_left'] ) ? 'td' : 'th';
  2526. $th_or_td_right = empty( $args['title_right'] ) ? 'td' : 'th';
  2527. $r .= "<tr class='diff-sub-title'>\n";
  2528. $r .= "\t<$th_or_td_left>$args[title_left]</$th_or_td_left>\n";
  2529. if ( $is_split_view ) {
  2530. $r .= "\t<$th_or_td_right>$args[title_right]</$th_or_td_right>\n";
  2531. }
  2532. $r .= "</tr>\n";
  2533. }
  2534. if ( $args['title_left'] || $args['title_right'] ) {
  2535. $r .= "</thead>\n";
  2536. }
  2537. $r .= "<tbody>\n$diff\n</tbody>\n";
  2538. $r .= '</table>';
  2539. return $r;
  2540. }
  2541. endif;