Ei kuvausta

general-template.php 154KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876
  1. <?php
  2. /**
  3. * General template tags that can go anywhere in a template.
  4. *
  5. * @package WordPress
  6. * @subpackage Template
  7. */
  8. /**
  9. * Load header template.
  10. *
  11. * Includes the header template for a theme or if a name is specified then a
  12. * specialised header will be included.
  13. *
  14. * For the parameter, if the file is called "header-special.php" then specify
  15. * "special".
  16. *
  17. * @since 1.5.0
  18. * @since 5.5.0 A return value was added.
  19. * @since 5.5.0 The `$args` parameter was added.
  20. *
  21. * @param string $name The name of the specialised header.
  22. * @param array $args Optional. Additional arguments passed to the header template.
  23. * Default empty array.
  24. * @return void|false Void on success, false if the template does not exist.
  25. */
  26. function get_header( $name = null, $args = array() ) {
  27. /**
  28. * Fires before the header template file is loaded.
  29. *
  30. * @since 2.1.0
  31. * @since 2.8.0 The `$name` parameter was added.
  32. * @since 5.5.0 The `$args` parameter was added.
  33. *
  34. * @param string|null $name Name of the specific header file to use. Null for the default header.
  35. * @param array $args Additional arguments passed to the header template.
  36. */
  37. do_action( 'get_header', $name, $args );
  38. $templates = array();
  39. $name = (string) $name;
  40. if ( '' !== $name ) {
  41. $templates[] = "header-{$name}.php";
  42. }
  43. $templates[] = 'header.php';
  44. if ( ! locate_template( $templates, true, true, $args ) ) {
  45. return false;
  46. }
  47. }
  48. /**
  49. * Load footer template.
  50. *
  51. * Includes the footer template for a theme or if a name is specified then a
  52. * specialised footer will be included.
  53. *
  54. * For the parameter, if the file is called "footer-special.php" then specify
  55. * "special".
  56. *
  57. * @since 1.5.0
  58. * @since 5.5.0 A return value was added.
  59. * @since 5.5.0 The `$args` parameter was added.
  60. *
  61. * @param string $name The name of the specialised footer.
  62. * @param array $args Optional. Additional arguments passed to the footer template.
  63. * Default empty array.
  64. * @return void|false Void on success, false if the template does not exist.
  65. */
  66. function get_footer( $name = null, $args = array() ) {
  67. /**
  68. * Fires before the footer template file is loaded.
  69. *
  70. * @since 2.1.0
  71. * @since 2.8.0 The `$name` parameter was added.
  72. * @since 5.5.0 The `$args` parameter was added.
  73. *
  74. * @param string|null $name Name of the specific footer file to use. Null for the default footer.
  75. * @param array $args Additional arguments passed to the footer template.
  76. */
  77. do_action( 'get_footer', $name, $args );
  78. $templates = array();
  79. $name = (string) $name;
  80. if ( '' !== $name ) {
  81. $templates[] = "footer-{$name}.php";
  82. }
  83. $templates[] = 'footer.php';
  84. if ( ! locate_template( $templates, true, true, $args ) ) {
  85. return false;
  86. }
  87. }
  88. /**
  89. * Load sidebar template.
  90. *
  91. * Includes the sidebar template for a theme or if a name is specified then a
  92. * specialised sidebar will be included.
  93. *
  94. * For the parameter, if the file is called "sidebar-special.php" then specify
  95. * "special".
  96. *
  97. * @since 1.5.0
  98. * @since 5.5.0 A return value was added.
  99. * @since 5.5.0 The `$args` parameter was added.
  100. *
  101. * @param string $name The name of the specialised sidebar.
  102. * @param array $args Optional. Additional arguments passed to the sidebar template.
  103. * Default empty array.
  104. * @return void|false Void on success, false if the template does not exist.
  105. */
  106. function get_sidebar( $name = null, $args = array() ) {
  107. /**
  108. * Fires before the sidebar template file is loaded.
  109. *
  110. * @since 2.2.0
  111. * @since 2.8.0 The `$name` parameter was added.
  112. * @since 5.5.0 The `$args` parameter was added.
  113. *
  114. * @param string|null $name Name of the specific sidebar file to use. Null for the default sidebar.
  115. * @param array $args Additional arguments passed to the sidebar template.
  116. */
  117. do_action( 'get_sidebar', $name, $args );
  118. $templates = array();
  119. $name = (string) $name;
  120. if ( '' !== $name ) {
  121. $templates[] = "sidebar-{$name}.php";
  122. }
  123. $templates[] = 'sidebar.php';
  124. if ( ! locate_template( $templates, true, true, $args ) ) {
  125. return false;
  126. }
  127. }
  128. /**
  129. * Loads a template part into a template.
  130. *
  131. * Provides a simple mechanism for child themes to overload reusable sections of code
  132. * in the theme.
  133. *
  134. * Includes the named template part for a theme or if a name is specified then a
  135. * specialised part will be included. If the theme contains no {slug}.php file
  136. * then no template will be included.
  137. *
  138. * The template is included using require, not require_once, so you may include the
  139. * same template part multiple times.
  140. *
  141. * For the $name parameter, if the file is called "{slug}-special.php" then specify
  142. * "special".
  143. *
  144. * @since 3.0.0
  145. * @since 5.5.0 A return value was added.
  146. * @since 5.5.0 The `$args` parameter was added.
  147. *
  148. * @param string $slug The slug name for the generic template.
  149. * @param string $name The name of the specialised template.
  150. * @param array $args Optional. Additional arguments passed to the template.
  151. * Default empty array.
  152. * @return void|false Void on success, false if the template does not exist.
  153. */
  154. function get_template_part( $slug, $name = null, $args = array() ) {
  155. /**
  156. * Fires before the specified template part file is loaded.
  157. *
  158. * The dynamic portion of the hook name, `$slug`, refers to the slug name
  159. * for the generic template part.
  160. *
  161. * @since 3.0.0
  162. * @since 5.5.0 The `$args` parameter was added.
  163. *
  164. * @param string $slug The slug name for the generic template.
  165. * @param string|null $name The name of the specialized template.
  166. * @param array $args Additional arguments passed to the template.
  167. */
  168. do_action( "get_template_part_{$slug}", $slug, $name, $args );
  169. $templates = array();
  170. $name = (string) $name;
  171. if ( '' !== $name ) {
  172. $templates[] = "{$slug}-{$name}.php";
  173. }
  174. $templates[] = "{$slug}.php";
  175. /**
  176. * Fires before a template part is loaded.
  177. *
  178. * @since 5.2.0
  179. * @since 5.5.0 The `$args` parameter was added.
  180. *
  181. * @param string $slug The slug name for the generic template.
  182. * @param string $name The name of the specialized template.
  183. * @param string[] $templates Array of template files to search for, in order.
  184. * @param array $args Additional arguments passed to the template.
  185. */
  186. do_action( 'get_template_part', $slug, $name, $templates, $args );
  187. if ( ! locate_template( $templates, true, false, $args ) ) {
  188. return false;
  189. }
  190. }
  191. /**
  192. * Display search form.
  193. *
  194. * Will first attempt to locate the searchform.php file in either the child or
  195. * the parent, then load it. If it doesn't exist, then the default search form
  196. * will be displayed. The default search form is HTML, which will be displayed.
  197. * There is a filter applied to the search form HTML in order to edit or replace
  198. * it. The filter is {@see 'get_search_form'}.
  199. *
  200. * This function is primarily used by themes which want to hardcode the search
  201. * form into the sidebar and also by the search widget in WordPress.
  202. *
  203. * There is also an action that is called whenever the function is run called,
  204. * {@see 'pre_get_search_form'}. This can be useful for outputting JavaScript that the
  205. * search relies on or various formatting that applies to the beginning of the
  206. * search. To give a few examples of what it can be used for.
  207. *
  208. * @since 2.7.0
  209. * @since 5.2.0 The `$args` array parameter was added in place of an `$echo` boolean flag.
  210. *
  211. * @param array $args {
  212. * Optional. Array of display arguments.
  213. *
  214. * @type bool $echo Whether to echo or return the form. Default true.
  215. * @type string $aria_label ARIA label for the search form. Useful to distinguish
  216. * multiple search forms on the same page and improve
  217. * accessibility. Default empty.
  218. * }
  219. * @return void|string Void if 'echo' argument is true, search form HTML if 'echo' is false.
  220. */
  221. function get_search_form( $args = array() ) {
  222. /**
  223. * Fires before the search form is retrieved, at the start of get_search_form().
  224. *
  225. * @since 2.7.0 as 'get_search_form' action.
  226. * @since 3.6.0
  227. * @since 5.5.0 The `$args` parameter was added.
  228. *
  229. * @link https://core.trac.wordpress.org/ticket/19321
  230. *
  231. * @param array $args The array of arguments for building the search form.
  232. * See get_search_form() for information on accepted arguments.
  233. */
  234. do_action( 'pre_get_search_form', $args );
  235. $echo = true;
  236. if ( ! is_array( $args ) ) {
  237. /*
  238. * Back compat: to ensure previous uses of get_search_form() continue to
  239. * function as expected, we handle a value for the boolean $echo param removed
  240. * in 5.2.0. Then we deal with the $args array and cast its defaults.
  241. */
  242. $echo = (bool) $args;
  243. // Set an empty array and allow default arguments to take over.
  244. $args = array();
  245. }
  246. // Defaults are to echo and to output no custom label on the form.
  247. $defaults = array(
  248. 'echo' => $echo,
  249. 'aria_label' => '',
  250. );
  251. $args = wp_parse_args( $args, $defaults );
  252. /**
  253. * Filters the array of arguments used when generating the search form.
  254. *
  255. * @since 5.2.0
  256. *
  257. * @param array $args The array of arguments for building the search form.
  258. * See get_search_form() for information on accepted arguments.
  259. */
  260. $args = apply_filters( 'search_form_args', $args );
  261. // Ensure that the filtered arguments contain all required default values.
  262. $args = array_merge( $defaults, $args );
  263. $format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml';
  264. /**
  265. * Filters the HTML format of the search form.
  266. *
  267. * @since 3.6.0
  268. * @since 5.5.0 The `$args` parameter was added.
  269. *
  270. * @param string $format The type of markup to use in the search form.
  271. * Accepts 'html5', 'xhtml'.
  272. * @param array $args The array of arguments for building the search form.
  273. * See get_search_form() for information on accepted arguments.
  274. */
  275. $format = apply_filters( 'search_form_format', $format, $args );
  276. $search_form_template = locate_template( 'searchform.php' );
  277. if ( '' !== $search_form_template ) {
  278. ob_start();
  279. require $search_form_template;
  280. $form = ob_get_clean();
  281. } else {
  282. // Build a string containing an aria-label to use for the search form.
  283. if ( $args['aria_label'] ) {
  284. $aria_label = 'aria-label="' . esc_attr( $args['aria_label'] ) . '" ';
  285. } else {
  286. /*
  287. * If there's no custom aria-label, we can set a default here. At the
  288. * moment it's empty as there's uncertainty about what the default should be.
  289. */
  290. $aria_label = '';
  291. }
  292. if ( 'html5' === $format ) {
  293. $form = '<form role="search" ' . $aria_label . 'method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
  294. <label>
  295. <span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>
  296. <input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search &hellip;', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" />
  297. </label>
  298. <input type="submit" class="search-submit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
  299. </form>';
  300. } else {
  301. $form = '<form role="search" ' . $aria_label . 'method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
  302. <div>
  303. <label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
  304. <input type="text" value="' . get_search_query() . '" name="s" id="s" />
  305. <input type="submit" id="searchsubmit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
  306. </div>
  307. </form>';
  308. }
  309. }
  310. /**
  311. * Filters the HTML output of the search form.
  312. *
  313. * @since 2.7.0
  314. * @since 5.5.0 The `$args` parameter was added.
  315. *
  316. * @param string $form The search form HTML output.
  317. * @param array $args The array of arguments for building the search form.
  318. * See get_search_form() for information on accepted arguments.
  319. */
  320. $result = apply_filters( 'get_search_form', $form, $args );
  321. if ( null === $result ) {
  322. $result = $form;
  323. }
  324. if ( $args['echo'] ) {
  325. echo $result;
  326. } else {
  327. return $result;
  328. }
  329. }
  330. /**
  331. * Display the Log In/Out link.
  332. *
  333. * Displays a link, which allows users to navigate to the Log In page to log in
  334. * or log out depending on whether they are currently logged in.
  335. *
  336. * @since 1.5.0
  337. *
  338. * @param string $redirect Optional path to redirect to on login/logout.
  339. * @param bool $echo Default to echo and not return the link.
  340. * @return void|string Void if `$echo` argument is true, log in/out link if `$echo` is false.
  341. */
  342. function wp_loginout( $redirect = '', $echo = true ) {
  343. if ( ! is_user_logged_in() ) {
  344. $link = '<a href="' . esc_url( wp_login_url( $redirect ) ) . '">' . __( 'Log in' ) . '</a>';
  345. } else {
  346. $link = '<a href="' . esc_url( wp_logout_url( $redirect ) ) . '">' . __( 'Log out' ) . '</a>';
  347. }
  348. if ( $echo ) {
  349. /**
  350. * Filters the HTML output for the Log In/Log Out link.
  351. *
  352. * @since 1.5.0
  353. *
  354. * @param string $link The HTML link content.
  355. */
  356. echo apply_filters( 'loginout', $link );
  357. } else {
  358. /** This filter is documented in wp-includes/general-template.php */
  359. return apply_filters( 'loginout', $link );
  360. }
  361. }
  362. /**
  363. * Retrieves the logout URL.
  364. *
  365. * Returns the URL that allows the user to log out of the site.
  366. *
  367. * @since 2.7.0
  368. *
  369. * @param string $redirect Path to redirect to on logout.
  370. * @return string The logout URL. Note: HTML-encoded via esc_html() in wp_nonce_url().
  371. */
  372. function wp_logout_url( $redirect = '' ) {
  373. $args = array();
  374. if ( ! empty( $redirect ) ) {
  375. $args['redirect_to'] = urlencode( $redirect );
  376. }
  377. $logout_url = add_query_arg( $args, site_url( 'wp-login.php?action=logout', 'login' ) );
  378. $logout_url = wp_nonce_url( $logout_url, 'log-out' );
  379. /**
  380. * Filters the logout URL.
  381. *
  382. * @since 2.8.0
  383. *
  384. * @param string $logout_url The HTML-encoded logout URL.
  385. * @param string $redirect Path to redirect to on logout.
  386. */
  387. return apply_filters( 'logout_url', $logout_url, $redirect );
  388. }
  389. /**
  390. * Retrieves the login URL.
  391. *
  392. * @since 2.7.0
  393. *
  394. * @param string $redirect Path to redirect to on log in.
  395. * @param bool $force_reauth Whether to force reauthorization, even if a cookie is present.
  396. * Default false.
  397. * @return string The login URL. Not HTML-encoded.
  398. */
  399. function wp_login_url( $redirect = '', $force_reauth = false ) {
  400. $login_url = site_url( 'wp-login.php', 'login' );
  401. if ( ! empty( $redirect ) ) {
  402. $login_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $login_url );
  403. }
  404. if ( $force_reauth ) {
  405. $login_url = add_query_arg( 'reauth', '1', $login_url );
  406. }
  407. /**
  408. * Filters the login URL.
  409. *
  410. * @since 2.8.0
  411. * @since 4.2.0 The `$force_reauth` parameter was added.
  412. *
  413. * @param string $login_url The login URL. Not HTML-encoded.
  414. * @param string $redirect The path to redirect to on login, if supplied.
  415. * @param bool $force_reauth Whether to force reauthorization, even if a cookie is present.
  416. */
  417. return apply_filters( 'login_url', $login_url, $redirect, $force_reauth );
  418. }
  419. /**
  420. * Returns the URL that allows the user to register on the site.
  421. *
  422. * @since 3.6.0
  423. *
  424. * @return string User registration URL.
  425. */
  426. function wp_registration_url() {
  427. /**
  428. * Filters the user registration URL.
  429. *
  430. * @since 3.6.0
  431. *
  432. * @param string $register The user registration URL.
  433. */
  434. return apply_filters( 'register_url', site_url( 'wp-login.php?action=register', 'login' ) );
  435. }
  436. /**
  437. * Provides a simple login form for use anywhere within WordPress.
  438. *
  439. * The login form HTML is echoed by default. Pass a false value for `$echo` to return it instead.
  440. *
  441. * @since 3.0.0
  442. *
  443. * @param array $args {
  444. * Optional. Array of options to control the form output. Default empty array.
  445. *
  446. * @type bool $echo Whether to display the login form or return the form HTML code.
  447. * Default true (echo).
  448. * @type string $redirect URL to redirect to. Must be absolute, as in "https://example.com/mypage/".
  449. * Default is to redirect back to the request URI.
  450. * @type string $form_id ID attribute value for the form. Default 'loginform'.
  451. * @type string $label_username Label for the username or email address field. Default 'Username or Email Address'.
  452. * @type string $label_password Label for the password field. Default 'Password'.
  453. * @type string $label_remember Label for the remember field. Default 'Remember Me'.
  454. * @type string $label_log_in Label for the submit button. Default 'Log In'.
  455. * @type string $id_username ID attribute value for the username field. Default 'user_login'.
  456. * @type string $id_password ID attribute value for the password field. Default 'user_pass'.
  457. * @type string $id_remember ID attribute value for the remember field. Default 'rememberme'.
  458. * @type string $id_submit ID attribute value for the submit button. Default 'wp-submit'.
  459. * @type bool $remember Whether to display the "rememberme" checkbox in the form.
  460. * @type string $value_username Default value for the username field. Default empty.
  461. * @type bool $value_remember Whether the "Remember Me" checkbox should be checked by default.
  462. * Default false (unchecked).
  463. *
  464. * }
  465. * @return void|string Void if 'echo' argument is true, login form HTML if 'echo' is false.
  466. */
  467. function wp_login_form( $args = array() ) {
  468. $defaults = array(
  469. 'echo' => true,
  470. // Default 'redirect' value takes the user back to the request URI.
  471. 'redirect' => ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
  472. 'form_id' => 'loginform',
  473. 'label_username' => __( 'Username or Email Address' ),
  474. 'label_password' => __( 'Password' ),
  475. 'label_remember' => __( 'Remember Me' ),
  476. 'label_log_in' => __( 'Log In' ),
  477. 'id_username' => 'user_login',
  478. 'id_password' => 'user_pass',
  479. 'id_remember' => 'rememberme',
  480. 'id_submit' => 'wp-submit',
  481. 'remember' => true,
  482. 'value_username' => '',
  483. // Set 'value_remember' to true to default the "Remember me" checkbox to checked.
  484. 'value_remember' => false,
  485. );
  486. /**
  487. * Filters the default login form output arguments.
  488. *
  489. * @since 3.0.0
  490. *
  491. * @see wp_login_form()
  492. *
  493. * @param array $defaults An array of default login form arguments.
  494. */
  495. $args = wp_parse_args( $args, apply_filters( 'login_form_defaults', $defaults ) );
  496. /**
  497. * Filters content to display at the top of the login form.
  498. *
  499. * The filter evaluates just following the opening form tag element.
  500. *
  501. * @since 3.0.0
  502. *
  503. * @param string $content Content to display. Default empty.
  504. * @param array $args Array of login form arguments.
  505. */
  506. $login_form_top = apply_filters( 'login_form_top', '', $args );
  507. /**
  508. * Filters content to display in the middle of the login form.
  509. *
  510. * The filter evaluates just following the location where the 'login-password'
  511. * field is displayed.
  512. *
  513. * @since 3.0.0
  514. *
  515. * @param string $content Content to display. Default empty.
  516. * @param array $args Array of login form arguments.
  517. */
  518. $login_form_middle = apply_filters( 'login_form_middle', '', $args );
  519. /**
  520. * Filters content to display at the bottom of the login form.
  521. *
  522. * The filter evaluates just preceding the closing form tag element.
  523. *
  524. * @since 3.0.0
  525. *
  526. * @param string $content Content to display. Default empty.
  527. * @param array $args Array of login form arguments.
  528. */
  529. $login_form_bottom = apply_filters( 'login_form_bottom', '', $args );
  530. $form = '
  531. <form name="' . $args['form_id'] . '" id="' . $args['form_id'] . '" action="' . esc_url( site_url( 'wp-login.php', 'login_post' ) ) . '" method="post">
  532. ' . $login_form_top . '
  533. <p class="login-username">
  534. <label for="' . esc_attr( $args['id_username'] ) . '">' . esc_html( $args['label_username'] ) . '</label>
  535. <input type="text" name="log" id="' . esc_attr( $args['id_username'] ) . '" class="input" value="' . esc_attr( $args['value_username'] ) . '" size="20" />
  536. </p>
  537. <p class="login-password">
  538. <label for="' . esc_attr( $args['id_password'] ) . '">' . esc_html( $args['label_password'] ) . '</label>
  539. <input type="password" name="pwd" id="' . esc_attr( $args['id_password'] ) . '" class="input" value="" size="20" />
  540. </p>
  541. ' . $login_form_middle . '
  542. ' . ( $args['remember'] ? '<p class="login-remember"><label><input name="rememberme" type="checkbox" id="' . esc_attr( $args['id_remember'] ) . '" value="forever"' . ( $args['value_remember'] ? ' checked="checked"' : '' ) . ' /> ' . esc_html( $args['label_remember'] ) . '</label></p>' : '' ) . '
  543. <p class="login-submit">
  544. <input type="submit" name="wp-submit" id="' . esc_attr( $args['id_submit'] ) . '" class="button button-primary" value="' . esc_attr( $args['label_log_in'] ) . '" />
  545. <input type="hidden" name="redirect_to" value="' . esc_url( $args['redirect'] ) . '" />
  546. </p>
  547. ' . $login_form_bottom . '
  548. </form>';
  549. if ( $args['echo'] ) {
  550. echo $form;
  551. } else {
  552. return $form;
  553. }
  554. }
  555. /**
  556. * Returns the URL that allows the user to retrieve the lost password
  557. *
  558. * @since 2.8.0
  559. *
  560. * @param string $redirect Path to redirect to on login.
  561. * @return string Lost password URL.
  562. */
  563. function wp_lostpassword_url( $redirect = '' ) {
  564. $args = array(
  565. 'action' => 'lostpassword',
  566. );
  567. if ( ! empty( $redirect ) ) {
  568. $args['redirect_to'] = urlencode( $redirect );
  569. }
  570. if ( is_multisite() ) {
  571. $blog_details = get_blog_details();
  572. $wp_login_path = $blog_details->path . 'wp-login.php';
  573. } else {
  574. $wp_login_path = 'wp-login.php';
  575. }
  576. $lostpassword_url = add_query_arg( $args, network_site_url( $wp_login_path, 'login' ) );
  577. /**
  578. * Filters the Lost Password URL.
  579. *
  580. * @since 2.8.0
  581. *
  582. * @param string $lostpassword_url The lost password page URL.
  583. * @param string $redirect The path to redirect to on login.
  584. */
  585. return apply_filters( 'lostpassword_url', $lostpassword_url, $redirect );
  586. }
  587. /**
  588. * Display the Registration or Admin link.
  589. *
  590. * Display a link which allows the user to navigate to the registration page if
  591. * not logged in and registration is enabled or to the dashboard if logged in.
  592. *
  593. * @since 1.5.0
  594. *
  595. * @param string $before Text to output before the link. Default `<li>`.
  596. * @param string $after Text to output after the link. Default `</li>`.
  597. * @param bool $echo Default to echo and not return the link.
  598. * @return void|string Void if `$echo` argument is true, registration or admin link
  599. * if `$echo` is false.
  600. */
  601. function wp_register( $before = '<li>', $after = '</li>', $echo = true ) {
  602. if ( ! is_user_logged_in() ) {
  603. if ( get_option( 'users_can_register' ) ) {
  604. $link = $before . '<a href="' . esc_url( wp_registration_url() ) . '">' . __( 'Register' ) . '</a>' . $after;
  605. } else {
  606. $link = '';
  607. }
  608. } elseif ( current_user_can( 'read' ) ) {
  609. $link = $before . '<a href="' . admin_url() . '">' . __( 'Site Admin' ) . '</a>' . $after;
  610. } else {
  611. $link = '';
  612. }
  613. /**
  614. * Filters the HTML link to the Registration or Admin page.
  615. *
  616. * Users are sent to the admin page if logged-in, or the registration page
  617. * if enabled and logged-out.
  618. *
  619. * @since 1.5.0
  620. *
  621. * @param string $link The HTML code for the link to the Registration or Admin page.
  622. */
  623. $link = apply_filters( 'register', $link );
  624. if ( $echo ) {
  625. echo $link;
  626. } else {
  627. return $link;
  628. }
  629. }
  630. /**
  631. * Theme container function for the 'wp_meta' action.
  632. *
  633. * The {@see 'wp_meta'} action can have several purposes, depending on how you use it,
  634. * but one purpose might have been to allow for theme switching.
  635. *
  636. * @since 1.5.0
  637. *
  638. * @link https://core.trac.wordpress.org/ticket/1458 Explanation of 'wp_meta' action.
  639. */
  640. function wp_meta() {
  641. /**
  642. * Fires before displaying echoed content in the sidebar.
  643. *
  644. * @since 1.5.0
  645. */
  646. do_action( 'wp_meta' );
  647. }
  648. /**
  649. * Displays information about the current site.
  650. *
  651. * @since 0.71
  652. *
  653. * @see get_bloginfo() For possible `$show` values
  654. *
  655. * @param string $show Optional. Site information to display. Default empty.
  656. */
  657. function bloginfo( $show = '' ) {
  658. echo get_bloginfo( $show, 'display' );
  659. }
  660. /**
  661. * Retrieves information about the current site.
  662. *
  663. * Possible values for `$show` include:
  664. *
  665. * - 'name' - Site title (set in Settings > General)
  666. * - 'description' - Site tagline (set in Settings > General)
  667. * - 'wpurl' - The WordPress address (URL) (set in Settings > General)
  668. * - 'url' - The Site address (URL) (set in Settings > General)
  669. * - 'admin_email' - Admin email (set in Settings > General)
  670. * - 'charset' - The "Encoding for pages and feeds" (set in Settings > Reading)
  671. * - 'version' - The current WordPress version
  672. * - 'html_type' - The content-type (default: "text/html"). Themes and plugins
  673. * can override the default value using the {@see 'pre_option_html_type'} filter
  674. * - 'text_direction' - The text direction determined by the site's language. is_rtl()
  675. * should be used instead
  676. * - 'language' - Language code for the current site
  677. * - 'stylesheet_url' - URL to the stylesheet for the active theme. An active child theme
  678. * will take precedence over this value
  679. * - 'stylesheet_directory' - Directory path for the active theme. An active child theme
  680. * will take precedence over this value
  681. * - 'template_url' / 'template_directory' - URL of the active theme's directory. An active
  682. * child theme will NOT take precedence over this value
  683. * - 'pingback_url' - The pingback XML-RPC file URL (xmlrpc.php)
  684. * - 'atom_url' - The Atom feed URL (/feed/atom)
  685. * - 'rdf_url' - The RDF/RSS 1.0 feed URL (/feed/rdf)
  686. * - 'rss_url' - The RSS 0.92 feed URL (/feed/rss)
  687. * - 'rss2_url' - The RSS 2.0 feed URL (/feed)
  688. * - 'comments_atom_url' - The comments Atom feed URL (/comments/feed)
  689. * - 'comments_rss2_url' - The comments RSS 2.0 feed URL (/comments/feed)
  690. *
  691. * Some `$show` values are deprecated and will be removed in future versions.
  692. * These options will trigger the _deprecated_argument() function.
  693. *
  694. * Deprecated arguments include:
  695. *
  696. * - 'siteurl' - Use 'url' instead
  697. * - 'home' - Use 'url' instead
  698. *
  699. * @since 0.71
  700. *
  701. * @global string $wp_version The WordPress version string.
  702. *
  703. * @param string $show Optional. Site info to retrieve. Default empty (site name).
  704. * @param string $filter Optional. How to filter what is retrieved. Default 'raw'.
  705. * @return string Mostly string values, might be empty.
  706. */
  707. function get_bloginfo( $show = '', $filter = 'raw' ) {
  708. switch ( $show ) {
  709. case 'home': // Deprecated.
  710. case 'siteurl': // Deprecated.
  711. _deprecated_argument(
  712. __FUNCTION__,
  713. '2.2.0',
  714. sprintf(
  715. /* translators: 1: 'siteurl'/'home' argument, 2: bloginfo() function name, 3: 'url' argument. */
  716. __( 'The %1$s option is deprecated for the family of %2$s functions. Use the %3$s option instead.' ),
  717. '<code>' . $show . '</code>',
  718. '<code>bloginfo()</code>',
  719. '<code>url</code>'
  720. )
  721. );
  722. // Intentional fall-through to be handled by the 'url' case.
  723. case 'url':
  724. $output = home_url();
  725. break;
  726. case 'wpurl':
  727. $output = site_url();
  728. break;
  729. case 'description':
  730. $output = get_option( 'blogdescription' );
  731. break;
  732. case 'rdf_url':
  733. $output = get_feed_link( 'rdf' );
  734. break;
  735. case 'rss_url':
  736. $output = get_feed_link( 'rss' );
  737. break;
  738. case 'rss2_url':
  739. $output = get_feed_link( 'rss2' );
  740. break;
  741. case 'atom_url':
  742. $output = get_feed_link( 'atom' );
  743. break;
  744. case 'comments_atom_url':
  745. $output = get_feed_link( 'comments_atom' );
  746. break;
  747. case 'comments_rss2_url':
  748. $output = get_feed_link( 'comments_rss2' );
  749. break;
  750. case 'pingback_url':
  751. $output = site_url( 'xmlrpc.php' );
  752. break;
  753. case 'stylesheet_url':
  754. $output = get_stylesheet_uri();
  755. break;
  756. case 'stylesheet_directory':
  757. $output = get_stylesheet_directory_uri();
  758. break;
  759. case 'template_directory':
  760. case 'template_url':
  761. $output = get_template_directory_uri();
  762. break;
  763. case 'admin_email':
  764. $output = get_option( 'admin_email' );
  765. break;
  766. case 'charset':
  767. $output = get_option( 'blog_charset' );
  768. if ( '' === $output ) {
  769. $output = 'UTF-8';
  770. }
  771. break;
  772. case 'html_type':
  773. $output = get_option( 'html_type' );
  774. break;
  775. case 'version':
  776. global $wp_version;
  777. $output = $wp_version;
  778. break;
  779. case 'language':
  780. /*
  781. * translators: Translate this to the correct language tag for your locale,
  782. * see https://www.w3.org/International/articles/language-tags/ for reference.
  783. * Do not translate into your own language.
  784. */
  785. $output = __( 'html_lang_attribute' );
  786. if ( 'html_lang_attribute' === $output || preg_match( '/[^a-zA-Z0-9-]/', $output ) ) {
  787. $output = determine_locale();
  788. $output = str_replace( '_', '-', $output );
  789. }
  790. break;
  791. case 'text_direction':
  792. _deprecated_argument(
  793. __FUNCTION__,
  794. '2.2.0',
  795. sprintf(
  796. /* translators: 1: 'text_direction' argument, 2: bloginfo() function name, 3: is_rtl() function name. */
  797. __( 'The %1$s option is deprecated for the family of %2$s functions. Use the %3$s function instead.' ),
  798. '<code>' . $show . '</code>',
  799. '<code>bloginfo()</code>',
  800. '<code>is_rtl()</code>'
  801. )
  802. );
  803. if ( function_exists( 'is_rtl' ) ) {
  804. $output = is_rtl() ? 'rtl' : 'ltr';
  805. } else {
  806. $output = 'ltr';
  807. }
  808. break;
  809. case 'name':
  810. default:
  811. $output = get_option( 'blogname' );
  812. break;
  813. }
  814. $url = true;
  815. if ( strpos( $show, 'url' ) === false &&
  816. strpos( $show, 'directory' ) === false &&
  817. strpos( $show, 'home' ) === false ) {
  818. $url = false;
  819. }
  820. if ( 'display' === $filter ) {
  821. if ( $url ) {
  822. /**
  823. * Filters the URL returned by get_bloginfo().
  824. *
  825. * @since 2.0.5
  826. *
  827. * @param string $output The URL returned by bloginfo().
  828. * @param string $show Type of information requested.
  829. */
  830. $output = apply_filters( 'bloginfo_url', $output, $show );
  831. } else {
  832. /**
  833. * Filters the site information returned by get_bloginfo().
  834. *
  835. * @since 0.71
  836. *
  837. * @param mixed $output The requested non-URL site information.
  838. * @param string $show Type of information requested.
  839. */
  840. $output = apply_filters( 'bloginfo', $output, $show );
  841. }
  842. }
  843. return $output;
  844. }
  845. /**
  846. * Returns the Site Icon URL.
  847. *
  848. * @since 4.3.0
  849. *
  850. * @param int $size Optional. Size of the site icon. Default 512 (pixels).
  851. * @param string $url Optional. Fallback url if no site icon is found. Default empty.
  852. * @param int $blog_id Optional. ID of the blog to get the site icon for. Default current blog.
  853. * @return string Site Icon URL.
  854. */
  855. function get_site_icon_url( $size = 512, $url = '', $blog_id = 0 ) {
  856. $switched_blog = false;
  857. if ( is_multisite() && ! empty( $blog_id ) && get_current_blog_id() !== (int) $blog_id ) {
  858. switch_to_blog( $blog_id );
  859. $switched_blog = true;
  860. }
  861. $site_icon_id = get_option( 'site_icon' );
  862. if ( $site_icon_id ) {
  863. if ( $size >= 512 ) {
  864. $size_data = 'full';
  865. } else {
  866. $size_data = array( $size, $size );
  867. }
  868. $url = wp_get_attachment_image_url( $site_icon_id, $size_data );
  869. }
  870. if ( $switched_blog ) {
  871. restore_current_blog();
  872. }
  873. /**
  874. * Filters the site icon URL.
  875. *
  876. * @since 4.4.0
  877. *
  878. * @param string $url Site icon URL.
  879. * @param int $size Size of the site icon.
  880. * @param int $blog_id ID of the blog to get the site icon for.
  881. */
  882. return apply_filters( 'get_site_icon_url', $url, $size, $blog_id );
  883. }
  884. /**
  885. * Displays the Site Icon URL.
  886. *
  887. * @since 4.3.0
  888. *
  889. * @param int $size Optional. Size of the site icon. Default 512 (pixels).
  890. * @param string $url Optional. Fallback url if no site icon is found. Default empty.
  891. * @param int $blog_id Optional. ID of the blog to get the site icon for. Default current blog.
  892. */
  893. function site_icon_url( $size = 512, $url = '', $blog_id = 0 ) {
  894. echo esc_url( get_site_icon_url( $size, $url, $blog_id ) );
  895. }
  896. /**
  897. * Whether the site has a Site Icon.
  898. *
  899. * @since 4.3.0
  900. *
  901. * @param int $blog_id Optional. ID of the blog in question. Default current blog.
  902. * @return bool Whether the site has a site icon or not.
  903. */
  904. function has_site_icon( $blog_id = 0 ) {
  905. return (bool) get_site_icon_url( 512, '', $blog_id );
  906. }
  907. /**
  908. * Determines whether the site has a custom logo.
  909. *
  910. * @since 4.5.0
  911. *
  912. * @param int $blog_id Optional. ID of the blog in question. Default is the ID of the current blog.
  913. * @return bool Whether the site has a custom logo or not.
  914. */
  915. function has_custom_logo( $blog_id = 0 ) {
  916. $switched_blog = false;
  917. if ( is_multisite() && ! empty( $blog_id ) && get_current_blog_id() !== (int) $blog_id ) {
  918. switch_to_blog( $blog_id );
  919. $switched_blog = true;
  920. }
  921. $custom_logo_id = get_theme_mod( 'custom_logo' );
  922. if ( $switched_blog ) {
  923. restore_current_blog();
  924. }
  925. return (bool) $custom_logo_id;
  926. }
  927. /**
  928. * Returns a custom logo, linked to home unless the theme supports removing the link on the home page.
  929. *
  930. * @since 4.5.0
  931. * @since 5.5.0 Added option to remove the link on the home page with `unlink-homepage-logo` theme support
  932. * for the `custom-logo` theme feature.
  933. * @since 5.5.1 Disabled lazy-loading by default.
  934. *
  935. * @param int $blog_id Optional. ID of the blog in question. Default is the ID of the current blog.
  936. * @return string Custom logo markup.
  937. */
  938. function get_custom_logo( $blog_id = 0 ) {
  939. $html = '';
  940. $switched_blog = false;
  941. if ( is_multisite() && ! empty( $blog_id ) && get_current_blog_id() !== (int) $blog_id ) {
  942. switch_to_blog( $blog_id );
  943. $switched_blog = true;
  944. }
  945. $custom_logo_id = get_theme_mod( 'custom_logo' );
  946. // We have a logo. Logo is go.
  947. if ( $custom_logo_id ) {
  948. $custom_logo_attr = array(
  949. 'class' => 'custom-logo',
  950. 'loading' => false,
  951. );
  952. $unlink_homepage_logo = (bool) get_theme_support( 'custom-logo', 'unlink-homepage-logo' );
  953. if ( $unlink_homepage_logo && is_front_page() && ! is_paged() ) {
  954. /*
  955. * If on the home page, set the logo alt attribute to an empty string,
  956. * as the image is decorative and doesn't need its purpose to be described.
  957. */
  958. $custom_logo_attr['alt'] = '';
  959. } else {
  960. /*
  961. * If the logo alt attribute is empty, get the site title and explicitly pass it
  962. * to the attributes used by wp_get_attachment_image().
  963. */
  964. $image_alt = get_post_meta( $custom_logo_id, '_wp_attachment_image_alt', true );
  965. if ( empty( $image_alt ) ) {
  966. $custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' );
  967. }
  968. }
  969. /**
  970. * Filters the list of custom logo image attributes.
  971. *
  972. * @since 5.5.0
  973. *
  974. * @param array $custom_logo_attr Custom logo image attributes.
  975. * @param int $custom_logo_id Custom logo attachment ID.
  976. * @param int $blog_id ID of the blog to get the custom logo for.
  977. */
  978. $custom_logo_attr = apply_filters( 'get_custom_logo_image_attributes', $custom_logo_attr, $custom_logo_id, $blog_id );
  979. /*
  980. * If the alt attribute is not empty, there's no need to explicitly pass it
  981. * because wp_get_attachment_image() already adds the alt attribute.
  982. */
  983. $image = wp_get_attachment_image( $custom_logo_id, 'full', false, $custom_logo_attr );
  984. if ( $unlink_homepage_logo && is_front_page() && ! is_paged() ) {
  985. // If on the home page, don't link the logo to home.
  986. $html = sprintf(
  987. '<span class="custom-logo-link">%1$s</span>',
  988. $image
  989. );
  990. } else {
  991. $aria_current = is_front_page() && ! is_paged() ? ' aria-current="page"' : '';
  992. $html = sprintf(
  993. '<a href="%1$s" class="custom-logo-link" rel="home"%2$s>%3$s</a>',
  994. esc_url( home_url( '/' ) ),
  995. $aria_current,
  996. $image
  997. );
  998. }
  999. } elseif ( is_customize_preview() ) {
  1000. // If no logo is set but we're in the Customizer, leave a placeholder (needed for the live preview).
  1001. $html = sprintf(
  1002. '<a href="%1$s" class="custom-logo-link" style="display:none;"><img class="custom-logo" alt="" /></a>',
  1003. esc_url( home_url( '/' ) )
  1004. );
  1005. }
  1006. if ( $switched_blog ) {
  1007. restore_current_blog();
  1008. }
  1009. /**
  1010. * Filters the custom logo output.
  1011. *
  1012. * @since 4.5.0
  1013. * @since 4.6.0 Added the `$blog_id` parameter.
  1014. *
  1015. * @param string $html Custom logo HTML output.
  1016. * @param int $blog_id ID of the blog to get the custom logo for.
  1017. */
  1018. return apply_filters( 'get_custom_logo', $html, $blog_id );
  1019. }
  1020. /**
  1021. * Displays a custom logo, linked to home unless the theme supports removing the link on the home page.
  1022. *
  1023. * @since 4.5.0
  1024. *
  1025. * @param int $blog_id Optional. ID of the blog in question. Default is the ID of the current blog.
  1026. */
  1027. function the_custom_logo( $blog_id = 0 ) {
  1028. echo get_custom_logo( $blog_id );
  1029. }
  1030. /**
  1031. * Returns document title for the current page.
  1032. *
  1033. * @since 4.4.0
  1034. *
  1035. * @global int $page Page number of a single post.
  1036. * @global int $paged Page number of a list of posts.
  1037. *
  1038. * @return string Tag with the document title.
  1039. */
  1040. function wp_get_document_title() {
  1041. /**
  1042. * Filters the document title before it is generated.
  1043. *
  1044. * Passing a non-empty value will short-circuit wp_get_document_title(),
  1045. * returning that value instead.
  1046. *
  1047. * @since 4.4.0
  1048. *
  1049. * @param string $title The document title. Default empty string.
  1050. */
  1051. $title = apply_filters( 'pre_get_document_title', '' );
  1052. if ( ! empty( $title ) ) {
  1053. return $title;
  1054. }
  1055. global $page, $paged;
  1056. $title = array(
  1057. 'title' => '',
  1058. );
  1059. // If it's a 404 page, use a "Page not found" title.
  1060. if ( is_404() ) {
  1061. $title['title'] = __( 'Page not found' );
  1062. // If it's a search, use a dynamic search results title.
  1063. } elseif ( is_search() ) {
  1064. /* translators: %s: Search query. */
  1065. $title['title'] = sprintf( __( 'Search Results for &#8220;%s&#8221;' ), get_search_query() );
  1066. // If on the front page, use the site title.
  1067. } elseif ( is_front_page() ) {
  1068. $title['title'] = get_bloginfo( 'name', 'display' );
  1069. // If on a post type archive, use the post type archive title.
  1070. } elseif ( is_post_type_archive() ) {
  1071. $title['title'] = post_type_archive_title( '', false );
  1072. // If on a taxonomy archive, use the term title.
  1073. } elseif ( is_tax() ) {
  1074. $title['title'] = single_term_title( '', false );
  1075. /*
  1076. * If we're on the blog page that is not the homepage
  1077. * or a single post of any post type, use the post title.
  1078. */
  1079. } elseif ( is_home() || is_singular() ) {
  1080. $title['title'] = single_post_title( '', false );
  1081. // If on a category or tag archive, use the term title.
  1082. } elseif ( is_category() || is_tag() ) {
  1083. $title['title'] = single_term_title( '', false );
  1084. // If on an author archive, use the author's display name.
  1085. } elseif ( is_author() && get_queried_object() ) {
  1086. $author = get_queried_object();
  1087. $title['title'] = $author->display_name;
  1088. // If it's a date archive, use the date as the title.
  1089. } elseif ( is_year() ) {
  1090. $title['title'] = get_the_date( _x( 'Y', 'yearly archives date format' ) );
  1091. } elseif ( is_month() ) {
  1092. $title['title'] = get_the_date( _x( 'F Y', 'monthly archives date format' ) );
  1093. } elseif ( is_day() ) {
  1094. $title['title'] = get_the_date();
  1095. }
  1096. // Add a page number if necessary.
  1097. if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
  1098. /* translators: %s: Page number. */
  1099. $title['page'] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
  1100. }
  1101. // Append the description or site title to give context.
  1102. if ( is_front_page() ) {
  1103. $title['tagline'] = get_bloginfo( 'description', 'display' );
  1104. } else {
  1105. $title['site'] = get_bloginfo( 'name', 'display' );
  1106. }
  1107. /**
  1108. * Filters the separator for the document title.
  1109. *
  1110. * @since 4.4.0
  1111. *
  1112. * @param string $sep Document title separator. Default '-'.
  1113. */
  1114. $sep = apply_filters( 'document_title_separator', '-' );
  1115. /**
  1116. * Filters the parts of the document title.
  1117. *
  1118. * @since 4.4.0
  1119. *
  1120. * @param array $title {
  1121. * The document title parts.
  1122. *
  1123. * @type string $title Title of the viewed page.
  1124. * @type string $page Optional. Page number if paginated.
  1125. * @type string $tagline Optional. Site description when on home page.
  1126. * @type string $site Optional. Site title when not on home page.
  1127. * }
  1128. */
  1129. $title = apply_filters( 'document_title_parts', $title );
  1130. $title = implode( " $sep ", array_filter( $title ) );
  1131. /**
  1132. * Filters the document title.
  1133. *
  1134. * @since 5.8.0
  1135. *
  1136. * @param string $title Document title.
  1137. */
  1138. $title = apply_filters( 'document_title', $title );
  1139. return $title;
  1140. }
  1141. /**
  1142. * Displays title tag with content.
  1143. *
  1144. * @ignore
  1145. * @since 4.1.0
  1146. * @since 4.4.0 Improved title output replaced `wp_title()`.
  1147. * @access private
  1148. */
  1149. function _wp_render_title_tag() {
  1150. if ( ! current_theme_supports( 'title-tag' ) ) {
  1151. return;
  1152. }
  1153. echo '<title>' . wp_get_document_title() . '</title>' . "\n";
  1154. }
  1155. /**
  1156. * Display or retrieve page title for all areas of blog.
  1157. *
  1158. * By default, the page title will display the separator before the page title,
  1159. * so that the blog title will be before the page title. This is not good for
  1160. * title display, since the blog title shows up on most tabs and not what is
  1161. * important, which is the page that the user is looking at.
  1162. *
  1163. * There are also SEO benefits to having the blog title after or to the 'right'
  1164. * of the page title. However, it is mostly common sense to have the blog title
  1165. * to the right with most browsers supporting tabs. You can achieve this by
  1166. * using the seplocation parameter and setting the value to 'right'. This change
  1167. * was introduced around 2.5.0, in case backward compatibility of themes is
  1168. * important.
  1169. *
  1170. * @since 1.0.0
  1171. *
  1172. * @global WP_Locale $wp_locale WordPress date and time locale object.
  1173. *
  1174. * @param string $sep Optional. How to separate the various items within the page title.
  1175. * Default '&raquo;'.
  1176. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1177. * @param string $seplocation Optional. Location of the separator ('left' or 'right').
  1178. * @return string|null String on retrieve, null when displaying.
  1179. */
  1180. function wp_title( $sep = '&raquo;', $display = true, $seplocation = '' ) {
  1181. global $wp_locale;
  1182. $m = get_query_var( 'm' );
  1183. $year = get_query_var( 'year' );
  1184. $monthnum = get_query_var( 'monthnum' );
  1185. $day = get_query_var( 'day' );
  1186. $search = get_query_var( 's' );
  1187. $title = '';
  1188. $t_sep = '%WP_TITLE_SEP%'; // Temporary separator, for accurate flipping, if necessary.
  1189. // If there is a post.
  1190. if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
  1191. $title = single_post_title( '', false );
  1192. }
  1193. // If there's a post type archive.
  1194. if ( is_post_type_archive() ) {
  1195. $post_type = get_query_var( 'post_type' );
  1196. if ( is_array( $post_type ) ) {
  1197. $post_type = reset( $post_type );
  1198. }
  1199. $post_type_object = get_post_type_object( $post_type );
  1200. if ( ! $post_type_object->has_archive ) {
  1201. $title = post_type_archive_title( '', false );
  1202. }
  1203. }
  1204. // If there's a category or tag.
  1205. if ( is_category() || is_tag() ) {
  1206. $title = single_term_title( '', false );
  1207. }
  1208. // If there's a taxonomy.
  1209. if ( is_tax() ) {
  1210. $term = get_queried_object();
  1211. if ( $term ) {
  1212. $tax = get_taxonomy( $term->taxonomy );
  1213. $title = single_term_title( $tax->labels->name . $t_sep, false );
  1214. }
  1215. }
  1216. // If there's an author.
  1217. if ( is_author() && ! is_post_type_archive() ) {
  1218. $author = get_queried_object();
  1219. if ( $author ) {
  1220. $title = $author->display_name;
  1221. }
  1222. }
  1223. // Post type archives with has_archive should override terms.
  1224. if ( is_post_type_archive() && $post_type_object->has_archive ) {
  1225. $title = post_type_archive_title( '', false );
  1226. }
  1227. // If there's a month.
  1228. if ( is_archive() && ! empty( $m ) ) {
  1229. $my_year = substr( $m, 0, 4 );
  1230. $my_month = $wp_locale->get_month( substr( $m, 4, 2 ) );
  1231. $my_day = (int) substr( $m, 6, 2 );
  1232. $title = $my_year . ( $my_month ? $t_sep . $my_month : '' ) . ( $my_day ? $t_sep . $my_day : '' );
  1233. }
  1234. // If there's a year.
  1235. if ( is_archive() && ! empty( $year ) ) {
  1236. $title = $year;
  1237. if ( ! empty( $monthnum ) ) {
  1238. $title .= $t_sep . $wp_locale->get_month( $monthnum );
  1239. }
  1240. if ( ! empty( $day ) ) {
  1241. $title .= $t_sep . zeroise( $day, 2 );
  1242. }
  1243. }
  1244. // If it's a search.
  1245. if ( is_search() ) {
  1246. /* translators: 1: Separator, 2: Search query. */
  1247. $title = sprintf( __( 'Search Results %1$s %2$s' ), $t_sep, strip_tags( $search ) );
  1248. }
  1249. // If it's a 404 page.
  1250. if ( is_404() ) {
  1251. $title = __( 'Page not found' );
  1252. }
  1253. $prefix = '';
  1254. if ( ! empty( $title ) ) {
  1255. $prefix = " $sep ";
  1256. }
  1257. /**
  1258. * Filters the parts of the page title.
  1259. *
  1260. * @since 4.0.0
  1261. *
  1262. * @param string[] $title_array Array of parts of the page title.
  1263. */
  1264. $title_array = apply_filters( 'wp_title_parts', explode( $t_sep, $title ) );
  1265. // Determines position of the separator and direction of the breadcrumb.
  1266. if ( 'right' === $seplocation ) { // Separator on right, so reverse the order.
  1267. $title_array = array_reverse( $title_array );
  1268. $title = implode( " $sep ", $title_array ) . $prefix;
  1269. } else {
  1270. $title = $prefix . implode( " $sep ", $title_array );
  1271. }
  1272. /**
  1273. * Filters the text of the page title.
  1274. *
  1275. * @since 2.0.0
  1276. *
  1277. * @param string $title Page title.
  1278. * @param string $sep Title separator.
  1279. * @param string $seplocation Location of the separator ('left' or 'right').
  1280. */
  1281. $title = apply_filters( 'wp_title', $title, $sep, $seplocation );
  1282. // Send it out.
  1283. if ( $display ) {
  1284. echo $title;
  1285. } else {
  1286. return $title;
  1287. }
  1288. }
  1289. /**
  1290. * Display or retrieve page title for post.
  1291. *
  1292. * This is optimized for single.php template file for displaying the post title.
  1293. *
  1294. * It does not support placing the separator after the title, but by leaving the
  1295. * prefix parameter empty, you can set the title separator manually. The prefix
  1296. * does not automatically place a space between the prefix, so if there should
  1297. * be a space, the parameter value will need to have it at the end.
  1298. *
  1299. * @since 0.71
  1300. *
  1301. * @param string $prefix Optional. What to display before the title.
  1302. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1303. * @return string|void Title when retrieving.
  1304. */
  1305. function single_post_title( $prefix = '', $display = true ) {
  1306. $_post = get_queried_object();
  1307. if ( ! isset( $_post->post_title ) ) {
  1308. return;
  1309. }
  1310. /**
  1311. * Filters the page title for a single post.
  1312. *
  1313. * @since 0.71
  1314. *
  1315. * @param string $_post_title The single post page title.
  1316. * @param WP_Post $_post The current post.
  1317. */
  1318. $title = apply_filters( 'single_post_title', $_post->post_title, $_post );
  1319. if ( $display ) {
  1320. echo $prefix . $title;
  1321. } else {
  1322. return $prefix . $title;
  1323. }
  1324. }
  1325. /**
  1326. * Display or retrieve title for a post type archive.
  1327. *
  1328. * This is optimized for archive.php and archive-{$post_type}.php template files
  1329. * for displaying the title of the post type.
  1330. *
  1331. * @since 3.1.0
  1332. *
  1333. * @param string $prefix Optional. What to display before the title.
  1334. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1335. * @return string|void Title when retrieving, null when displaying or failure.
  1336. */
  1337. function post_type_archive_title( $prefix = '', $display = true ) {
  1338. if ( ! is_post_type_archive() ) {
  1339. return;
  1340. }
  1341. $post_type = get_query_var( 'post_type' );
  1342. if ( is_array( $post_type ) ) {
  1343. $post_type = reset( $post_type );
  1344. }
  1345. $post_type_obj = get_post_type_object( $post_type );
  1346. /**
  1347. * Filters the post type archive title.
  1348. *
  1349. * @since 3.1.0
  1350. *
  1351. * @param string $post_type_name Post type 'name' label.
  1352. * @param string $post_type Post type.
  1353. */
  1354. $title = apply_filters( 'post_type_archive_title', $post_type_obj->labels->name, $post_type );
  1355. if ( $display ) {
  1356. echo $prefix . $title;
  1357. } else {
  1358. return $prefix . $title;
  1359. }
  1360. }
  1361. /**
  1362. * Display or retrieve page title for category archive.
  1363. *
  1364. * Useful for category template files for displaying the category page title.
  1365. * The prefix does not automatically place a space between the prefix, so if
  1366. * there should be a space, the parameter value will need to have it at the end.
  1367. *
  1368. * @since 0.71
  1369. *
  1370. * @param string $prefix Optional. What to display before the title.
  1371. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1372. * @return string|void Title when retrieving.
  1373. */
  1374. function single_cat_title( $prefix = '', $display = true ) {
  1375. return single_term_title( $prefix, $display );
  1376. }
  1377. /**
  1378. * Display or retrieve page title for tag post archive.
  1379. *
  1380. * Useful for tag template files for displaying the tag page title. The prefix
  1381. * does not automatically place a space between the prefix, so if there should
  1382. * be a space, the parameter value will need to have it at the end.
  1383. *
  1384. * @since 2.3.0
  1385. *
  1386. * @param string $prefix Optional. What to display before the title.
  1387. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1388. * @return string|void Title when retrieving.
  1389. */
  1390. function single_tag_title( $prefix = '', $display = true ) {
  1391. return single_term_title( $prefix, $display );
  1392. }
  1393. /**
  1394. * Display or retrieve page title for taxonomy term archive.
  1395. *
  1396. * Useful for taxonomy term template files for displaying the taxonomy term page title.
  1397. * The prefix does not automatically place a space between the prefix, so if there should
  1398. * be a space, the parameter value will need to have it at the end.
  1399. *
  1400. * @since 3.1.0
  1401. *
  1402. * @param string $prefix Optional. What to display before the title.
  1403. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1404. * @return string|void Title when retrieving.
  1405. */
  1406. function single_term_title( $prefix = '', $display = true ) {
  1407. $term = get_queried_object();
  1408. if ( ! $term ) {
  1409. return;
  1410. }
  1411. if ( is_category() ) {
  1412. /**
  1413. * Filters the category archive page title.
  1414. *
  1415. * @since 2.0.10
  1416. *
  1417. * @param string $term_name Category name for archive being displayed.
  1418. */
  1419. $term_name = apply_filters( 'single_cat_title', $term->name );
  1420. } elseif ( is_tag() ) {
  1421. /**
  1422. * Filters the tag archive page title.
  1423. *
  1424. * @since 2.3.0
  1425. *
  1426. * @param string $term_name Tag name for archive being displayed.
  1427. */
  1428. $term_name = apply_filters( 'single_tag_title', $term->name );
  1429. } elseif ( is_tax() ) {
  1430. /**
  1431. * Filters the custom taxonomy archive page title.
  1432. *
  1433. * @since 3.1.0
  1434. *
  1435. * @param string $term_name Term name for archive being displayed.
  1436. */
  1437. $term_name = apply_filters( 'single_term_title', $term->name );
  1438. } else {
  1439. return;
  1440. }
  1441. if ( empty( $term_name ) ) {
  1442. return;
  1443. }
  1444. if ( $display ) {
  1445. echo $prefix . $term_name;
  1446. } else {
  1447. return $prefix . $term_name;
  1448. }
  1449. }
  1450. /**
  1451. * Display or retrieve page title for post archive based on date.
  1452. *
  1453. * Useful for when the template only needs to display the month and year,
  1454. * if either are available. The prefix does not automatically place a space
  1455. * between the prefix, so if there should be a space, the parameter value
  1456. * will need to have it at the end.
  1457. *
  1458. * @since 0.71
  1459. *
  1460. * @global WP_Locale $wp_locale WordPress date and time locale object.
  1461. *
  1462. * @param string $prefix Optional. What to display before the title.
  1463. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1464. * @return string|void Title when retrieving.
  1465. */
  1466. function single_month_title( $prefix = '', $display = true ) {
  1467. global $wp_locale;
  1468. $m = get_query_var( 'm' );
  1469. $year = get_query_var( 'year' );
  1470. $monthnum = get_query_var( 'monthnum' );
  1471. if ( ! empty( $monthnum ) && ! empty( $year ) ) {
  1472. $my_year = $year;
  1473. $my_month = $wp_locale->get_month( $monthnum );
  1474. } elseif ( ! empty( $m ) ) {
  1475. $my_year = substr( $m, 0, 4 );
  1476. $my_month = $wp_locale->get_month( substr( $m, 4, 2 ) );
  1477. }
  1478. if ( empty( $my_month ) ) {
  1479. return false;
  1480. }
  1481. $result = $prefix . $my_month . $prefix . $my_year;
  1482. if ( ! $display ) {
  1483. return $result;
  1484. }
  1485. echo $result;
  1486. }
  1487. /**
  1488. * Display the archive title based on the queried object.
  1489. *
  1490. * @since 4.1.0
  1491. *
  1492. * @see get_the_archive_title()
  1493. *
  1494. * @param string $before Optional. Content to prepend to the title. Default empty.
  1495. * @param string $after Optional. Content to append to the title. Default empty.
  1496. */
  1497. function the_archive_title( $before = '', $after = '' ) {
  1498. $title = get_the_archive_title();
  1499. if ( ! empty( $title ) ) {
  1500. echo $before . $title . $after;
  1501. }
  1502. }
  1503. /**
  1504. * Retrieve the archive title based on the queried object.
  1505. *
  1506. * @since 4.1.0
  1507. * @since 5.5.0 The title part is wrapped in a `<span>` element.
  1508. *
  1509. * @return string Archive title.
  1510. */
  1511. function get_the_archive_title() {
  1512. $title = __( 'Archives' );
  1513. $prefix = '';
  1514. if ( is_category() ) {
  1515. $title = single_cat_title( '', false );
  1516. $prefix = _x( 'Category:', 'category archive title prefix' );
  1517. } elseif ( is_tag() ) {
  1518. $title = single_tag_title( '', false );
  1519. $prefix = _x( 'Tag:', 'tag archive title prefix' );
  1520. } elseif ( is_author() ) {
  1521. $title = get_the_author();
  1522. $prefix = _x( 'Author:', 'author archive title prefix' );
  1523. } elseif ( is_year() ) {
  1524. $title = get_the_date( _x( 'Y', 'yearly archives date format' ) );
  1525. $prefix = _x( 'Year:', 'date archive title prefix' );
  1526. } elseif ( is_month() ) {
  1527. $title = get_the_date( _x( 'F Y', 'monthly archives date format' ) );
  1528. $prefix = _x( 'Month:', 'date archive title prefix' );
  1529. } elseif ( is_day() ) {
  1530. $title = get_the_date( _x( 'F j, Y', 'daily archives date format' ) );
  1531. $prefix = _x( 'Day:', 'date archive title prefix' );
  1532. } elseif ( is_tax( 'post_format' ) ) {
  1533. if ( is_tax( 'post_format', 'post-format-aside' ) ) {
  1534. $title = _x( 'Asides', 'post format archive title' );
  1535. } elseif ( is_tax( 'post_format', 'post-format-gallery' ) ) {
  1536. $title = _x( 'Galleries', 'post format archive title' );
  1537. } elseif ( is_tax( 'post_format', 'post-format-image' ) ) {
  1538. $title = _x( 'Images', 'post format archive title' );
  1539. } elseif ( is_tax( 'post_format', 'post-format-video' ) ) {
  1540. $title = _x( 'Videos', 'post format archive title' );
  1541. } elseif ( is_tax( 'post_format', 'post-format-quote' ) ) {
  1542. $title = _x( 'Quotes', 'post format archive title' );
  1543. } elseif ( is_tax( 'post_format', 'post-format-link' ) ) {
  1544. $title = _x( 'Links', 'post format archive title' );
  1545. } elseif ( is_tax( 'post_format', 'post-format-status' ) ) {
  1546. $title = _x( 'Statuses', 'post format archive title' );
  1547. } elseif ( is_tax( 'post_format', 'post-format-audio' ) ) {
  1548. $title = _x( 'Audio', 'post format archive title' );
  1549. } elseif ( is_tax( 'post_format', 'post-format-chat' ) ) {
  1550. $title = _x( 'Chats', 'post format archive title' );
  1551. }
  1552. } elseif ( is_post_type_archive() ) {
  1553. $title = post_type_archive_title( '', false );
  1554. $prefix = _x( 'Archives:', 'post type archive title prefix' );
  1555. } elseif ( is_tax() ) {
  1556. $queried_object = get_queried_object();
  1557. if ( $queried_object ) {
  1558. $tax = get_taxonomy( $queried_object->taxonomy );
  1559. $title = single_term_title( '', false );
  1560. $prefix = sprintf(
  1561. /* translators: %s: Taxonomy singular name. */
  1562. _x( '%s:', 'taxonomy term archive title prefix' ),
  1563. $tax->labels->singular_name
  1564. );
  1565. }
  1566. }
  1567. $original_title = $title;
  1568. /**
  1569. * Filters the archive title prefix.
  1570. *
  1571. * @since 5.5.0
  1572. *
  1573. * @param string $prefix Archive title prefix.
  1574. */
  1575. $prefix = apply_filters( 'get_the_archive_title_prefix', $prefix );
  1576. if ( $prefix ) {
  1577. $title = sprintf(
  1578. /* translators: 1: Title prefix. 2: Title. */
  1579. _x( '%1$s %2$s', 'archive title' ),
  1580. $prefix,
  1581. '<span>' . $title . '</span>'
  1582. );
  1583. }
  1584. /**
  1585. * Filters the archive title.
  1586. *
  1587. * @since 4.1.0
  1588. * @since 5.5.0 Added the `$prefix` and `$original_title` parameters.
  1589. *
  1590. * @param string $title Archive title to be displayed.
  1591. * @param string $original_title Archive title without prefix.
  1592. * @param string $prefix Archive title prefix.
  1593. */
  1594. return apply_filters( 'get_the_archive_title', $title, $original_title, $prefix );
  1595. }
  1596. /**
  1597. * Display category, tag, term, or author description.
  1598. *
  1599. * @since 4.1.0
  1600. *
  1601. * @see get_the_archive_description()
  1602. *
  1603. * @param string $before Optional. Content to prepend to the description. Default empty.
  1604. * @param string $after Optional. Content to append to the description. Default empty.
  1605. */
  1606. function the_archive_description( $before = '', $after = '' ) {
  1607. $description = get_the_archive_description();
  1608. if ( $description ) {
  1609. echo $before . $description . $after;
  1610. }
  1611. }
  1612. /**
  1613. * Retrieves the description for an author, post type, or term archive.
  1614. *
  1615. * @since 4.1.0
  1616. * @since 4.7.0 Added support for author archives.
  1617. * @since 4.9.0 Added support for post type archives.
  1618. *
  1619. * @see term_description()
  1620. *
  1621. * @return string Archive description.
  1622. */
  1623. function get_the_archive_description() {
  1624. if ( is_author() ) {
  1625. $description = get_the_author_meta( 'description' );
  1626. } elseif ( is_post_type_archive() ) {
  1627. $description = get_the_post_type_description();
  1628. } else {
  1629. $description = term_description();
  1630. }
  1631. /**
  1632. * Filters the archive description.
  1633. *
  1634. * @since 4.1.0
  1635. *
  1636. * @param string $description Archive description to be displayed.
  1637. */
  1638. return apply_filters( 'get_the_archive_description', $description );
  1639. }
  1640. /**
  1641. * Retrieves the description for a post type archive.
  1642. *
  1643. * @since 4.9.0
  1644. *
  1645. * @return string The post type description.
  1646. */
  1647. function get_the_post_type_description() {
  1648. $post_type = get_query_var( 'post_type' );
  1649. if ( is_array( $post_type ) ) {
  1650. $post_type = reset( $post_type );
  1651. }
  1652. $post_type_obj = get_post_type_object( $post_type );
  1653. // Check if a description is set.
  1654. if ( isset( $post_type_obj->description ) ) {
  1655. $description = $post_type_obj->description;
  1656. } else {
  1657. $description = '';
  1658. }
  1659. /**
  1660. * Filters the description for a post type archive.
  1661. *
  1662. * @since 4.9.0
  1663. *
  1664. * @param string $description The post type description.
  1665. * @param WP_Post_Type $post_type_obj The post type object.
  1666. */
  1667. return apply_filters( 'get_the_post_type_description', $description, $post_type_obj );
  1668. }
  1669. /**
  1670. * Retrieve archive link content based on predefined or custom code.
  1671. *
  1672. * The format can be one of four styles. The 'link' for head element, 'option'
  1673. * for use in the select element, 'html' for use in list (either ol or ul HTML
  1674. * elements). Custom content is also supported using the before and after
  1675. * parameters.
  1676. *
  1677. * The 'link' format uses the `<link>` HTML element with the **archives**
  1678. * relationship. The before and after parameters are not used. The text
  1679. * parameter is used to describe the link.
  1680. *
  1681. * The 'option' format uses the option HTML element for use in select element.
  1682. * The value is the url parameter and the before and after parameters are used
  1683. * between the text description.
  1684. *
  1685. * The 'html' format, which is the default, uses the li HTML element for use in
  1686. * the list HTML elements. The before parameter is before the link and the after
  1687. * parameter is after the closing link.
  1688. *
  1689. * The custom format uses the before parameter before the link ('a' HTML
  1690. * element) and the after parameter after the closing link tag. If the above
  1691. * three values for the format are not used, then custom format is assumed.
  1692. *
  1693. * @since 1.0.0
  1694. * @since 5.2.0 Added the `$selected` parameter.
  1695. *
  1696. * @param string $url URL to archive.
  1697. * @param string $text Archive text description.
  1698. * @param string $format Optional. Can be 'link', 'option', 'html', or custom. Default 'html'.
  1699. * @param string $before Optional. Content to prepend to the description. Default empty.
  1700. * @param string $after Optional. Content to append to the description. Default empty.
  1701. * @param bool $selected Optional. Set to true if the current page is the selected archive page.
  1702. * @return string HTML link content for archive.
  1703. */
  1704. function get_archives_link( $url, $text, $format = 'html', $before = '', $after = '', $selected = false ) {
  1705. $text = wptexturize( $text );
  1706. $url = esc_url( $url );
  1707. $aria_current = $selected ? ' aria-current="page"' : '';
  1708. if ( 'link' === $format ) {
  1709. $link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n";
  1710. } elseif ( 'option' === $format ) {
  1711. $selected_attr = $selected ? " selected='selected'" : '';
  1712. $link_html = "\t<option value='$url'$selected_attr>$before $text $after</option>\n";
  1713. } elseif ( 'html' === $format ) {
  1714. $link_html = "\t<li>$before<a href='$url'$aria_current>$text</a>$after</li>\n";
  1715. } else { // Custom.
  1716. $link_html = "\t$before<a href='$url'$aria_current>$text</a>$after\n";
  1717. }
  1718. /**
  1719. * Filters the archive link content.
  1720. *
  1721. * @since 2.6.0
  1722. * @since 4.5.0 Added the `$url`, `$text`, `$format`, `$before`, and `$after` parameters.
  1723. * @since 5.2.0 Added the `$selected` parameter.
  1724. *
  1725. * @param string $link_html The archive HTML link content.
  1726. * @param string $url URL to archive.
  1727. * @param string $text Archive text description.
  1728. * @param string $format Link format. Can be 'link', 'option', 'html', or custom.
  1729. * @param string $before Content to prepend to the description.
  1730. * @param string $after Content to append to the description.
  1731. * @param bool $selected True if the current page is the selected archive.
  1732. */
  1733. return apply_filters( 'get_archives_link', $link_html, $url, $text, $format, $before, $after, $selected );
  1734. }
  1735. /**
  1736. * Display archive links based on type and format.
  1737. *
  1738. * @since 1.2.0
  1739. * @since 4.4.0 The `$post_type` argument was added.
  1740. * @since 5.2.0 The `$year`, `$monthnum`, `$day`, and `$w` arguments were added.
  1741. *
  1742. * @see get_archives_link()
  1743. *
  1744. * @global wpdb $wpdb WordPress database abstraction object.
  1745. * @global WP_Locale $wp_locale WordPress date and time locale object.
  1746. *
  1747. * @param string|array $args {
  1748. * Default archive links arguments. Optional.
  1749. *
  1750. * @type string $type Type of archive to retrieve. Accepts 'daily', 'weekly', 'monthly',
  1751. * 'yearly', 'postbypost', or 'alpha'. Both 'postbypost' and 'alpha'
  1752. * display the same archive link list as well as post titles instead
  1753. * of displaying dates. The difference between the two is that 'alpha'
  1754. * will order by post title and 'postbypost' will order by post date.
  1755. * Default 'monthly'.
  1756. * @type string|int $limit Number of links to limit the query to. Default empty (no limit).
  1757. * @type string $format Format each link should take using the $before and $after args.
  1758. * Accepts 'link' (`<link>` tag), 'option' (`<option>` tag), 'html'
  1759. * (`<li>` tag), or a custom format, which generates a link anchor
  1760. * with $before preceding and $after succeeding. Default 'html'.
  1761. * @type string $before Markup to prepend to the beginning of each link. Default empty.
  1762. * @type string $after Markup to append to the end of each link. Default empty.
  1763. * @type bool $show_post_count Whether to display the post count alongside the link. Default false.
  1764. * @type bool|int $echo Whether to echo or return the links list. Default 1|true to echo.
  1765. * @type string $order Whether to use ascending or descending order. Accepts 'ASC', or 'DESC'.
  1766. * Default 'DESC'.
  1767. * @type string $post_type Post type. Default 'post'.
  1768. * @type string $year Year. Default current year.
  1769. * @type string $monthnum Month number. Default current month number.
  1770. * @type string $day Day. Default current day.
  1771. * @type string $w Week. Default current week.
  1772. * }
  1773. * @return void|string Void if 'echo' argument is true, archive links if 'echo' is false.
  1774. */
  1775. function wp_get_archives( $args = '' ) {
  1776. global $wpdb, $wp_locale;
  1777. $defaults = array(
  1778. 'type' => 'monthly',
  1779. 'limit' => '',
  1780. 'format' => 'html',
  1781. 'before' => '',
  1782. 'after' => '',
  1783. 'show_post_count' => false,
  1784. 'echo' => 1,
  1785. 'order' => 'DESC',
  1786. 'post_type' => 'post',
  1787. 'year' => get_query_var( 'year' ),
  1788. 'monthnum' => get_query_var( 'monthnum' ),
  1789. 'day' => get_query_var( 'day' ),
  1790. 'w' => get_query_var( 'w' ),
  1791. );
  1792. $parsed_args = wp_parse_args( $args, $defaults );
  1793. $post_type_object = get_post_type_object( $parsed_args['post_type'] );
  1794. if ( ! is_post_type_viewable( $post_type_object ) ) {
  1795. return;
  1796. }
  1797. $parsed_args['post_type'] = $post_type_object->name;
  1798. if ( '' === $parsed_args['type'] ) {
  1799. $parsed_args['type'] = 'monthly';
  1800. }
  1801. if ( ! empty( $parsed_args['limit'] ) ) {
  1802. $parsed_args['limit'] = absint( $parsed_args['limit'] );
  1803. $parsed_args['limit'] = ' LIMIT ' . $parsed_args['limit'];
  1804. }
  1805. $order = strtoupper( $parsed_args['order'] );
  1806. if ( 'ASC' !== $order ) {
  1807. $order = 'DESC';
  1808. }
  1809. // This is what will separate dates on weekly archive links.
  1810. $archive_week_separator = '&#8211;';
  1811. $sql_where = $wpdb->prepare( "WHERE post_type = %s AND post_status = 'publish'", $parsed_args['post_type'] );
  1812. /**
  1813. * Filters the SQL WHERE clause for retrieving archives.
  1814. *
  1815. * @since 2.2.0
  1816. *
  1817. * @param string $sql_where Portion of SQL query containing the WHERE clause.
  1818. * @param array $parsed_args An array of default arguments.
  1819. */
  1820. $where = apply_filters( 'getarchives_where', $sql_where, $parsed_args );
  1821. /**
  1822. * Filters the SQL JOIN clause for retrieving archives.
  1823. *
  1824. * @since 2.2.0
  1825. *
  1826. * @param string $sql_join Portion of SQL query containing JOIN clause.
  1827. * @param array $parsed_args An array of default arguments.
  1828. */
  1829. $join = apply_filters( 'getarchives_join', '', $parsed_args );
  1830. $output = '';
  1831. $last_changed = wp_cache_get_last_changed( 'posts' );
  1832. $limit = $parsed_args['limit'];
  1833. if ( 'monthly' === $parsed_args['type'] ) {
  1834. $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit";
  1835. $key = md5( $query );
  1836. $key = "wp_get_archives:$key:$last_changed";
  1837. $results = wp_cache_get( $key, 'posts' );
  1838. if ( ! $results ) {
  1839. $results = $wpdb->get_results( $query );
  1840. wp_cache_set( $key, $results, 'posts' );
  1841. }
  1842. if ( $results ) {
  1843. $after = $parsed_args['after'];
  1844. foreach ( (array) $results as $result ) {
  1845. $url = get_month_link( $result->year, $result->month );
  1846. if ( 'post' !== $parsed_args['post_type'] ) {
  1847. $url = add_query_arg( 'post_type', $parsed_args['post_type'], $url );
  1848. }
  1849. /* translators: 1: Month name, 2: 4-digit year. */
  1850. $text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year );
  1851. if ( $parsed_args['show_post_count'] ) {
  1852. $parsed_args['after'] = '&nbsp;(' . $result->posts . ')' . $after;
  1853. }
  1854. $selected = is_archive() && (string) $parsed_args['year'] === $result->year && (string) $parsed_args['monthnum'] === $result->month;
  1855. $output .= get_archives_link( $url, $text, $parsed_args['format'], $parsed_args['before'], $parsed_args['after'], $selected );
  1856. }
  1857. }
  1858. } elseif ( 'yearly' === $parsed_args['type'] ) {
  1859. $query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit";
  1860. $key = md5( $query );
  1861. $key = "wp_get_archives:$key:$last_changed";
  1862. $results = wp_cache_get( $key, 'posts' );
  1863. if ( ! $results ) {
  1864. $results = $wpdb->get_results( $query );
  1865. wp_cache_set( $key, $results, 'posts' );
  1866. }
  1867. if ( $results ) {
  1868. $after = $parsed_args['after'];
  1869. foreach ( (array) $results as $result ) {
  1870. $url = get_year_link( $result->year );
  1871. if ( 'post' !== $parsed_args['post_type'] ) {
  1872. $url = add_query_arg( 'post_type', $parsed_args['post_type'], $url );
  1873. }
  1874. $text = sprintf( '%d', $result->year );
  1875. if ( $parsed_args['show_post_count'] ) {
  1876. $parsed_args['after'] = '&nbsp;(' . $result->posts . ')' . $after;
  1877. }
  1878. $selected = is_archive() && (string) $parsed_args['year'] === $result->year;
  1879. $output .= get_archives_link( $url, $text, $parsed_args['format'], $parsed_args['before'], $parsed_args['after'], $selected );
  1880. }
  1881. }
  1882. } elseif ( 'daily' === $parsed_args['type'] ) {
  1883. $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date $order $limit";
  1884. $key = md5( $query );
  1885. $key = "wp_get_archives:$key:$last_changed";
  1886. $results = wp_cache_get( $key, 'posts' );
  1887. if ( ! $results ) {
  1888. $results = $wpdb->get_results( $query );
  1889. wp_cache_set( $key, $results, 'posts' );
  1890. }
  1891. if ( $results ) {
  1892. $after = $parsed_args['after'];
  1893. foreach ( (array) $results as $result ) {
  1894. $url = get_day_link( $result->year, $result->month, $result->dayofmonth );
  1895. if ( 'post' !== $parsed_args['post_type'] ) {
  1896. $url = add_query_arg( 'post_type', $parsed_args['post_type'], $url );
  1897. }
  1898. $date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth );
  1899. $text = mysql2date( get_option( 'date_format' ), $date );
  1900. if ( $parsed_args['show_post_count'] ) {
  1901. $parsed_args['after'] = '&nbsp;(' . $result->posts . ')' . $after;
  1902. }
  1903. $selected = is_archive() && (string) $parsed_args['year'] === $result->year && (string) $parsed_args['monthnum'] === $result->month && (string) $parsed_args['day'] === $result->dayofmonth;
  1904. $output .= get_archives_link( $url, $text, $parsed_args['format'], $parsed_args['before'], $parsed_args['after'], $selected );
  1905. }
  1906. }
  1907. } elseif ( 'weekly' === $parsed_args['type'] ) {
  1908. $week = _wp_mysql_week( '`post_date`' );
  1909. $query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit";
  1910. $key = md5( $query );
  1911. $key = "wp_get_archives:$key:$last_changed";
  1912. $results = wp_cache_get( $key, 'posts' );
  1913. if ( ! $results ) {
  1914. $results = $wpdb->get_results( $query );
  1915. wp_cache_set( $key, $results, 'posts' );
  1916. }
  1917. $arc_w_last = '';
  1918. if ( $results ) {
  1919. $after = $parsed_args['after'];
  1920. foreach ( (array) $results as $result ) {
  1921. if ( $result->week != $arc_w_last ) {
  1922. $arc_year = $result->yr;
  1923. $arc_w_last = $result->week;
  1924. $arc_week = get_weekstartend( $result->yyyymmdd, get_option( 'start_of_week' ) );
  1925. $arc_week_start = date_i18n( get_option( 'date_format' ), $arc_week['start'] );
  1926. $arc_week_end = date_i18n( get_option( 'date_format' ), $arc_week['end'] );
  1927. $url = add_query_arg(
  1928. array(
  1929. 'm' => $arc_year,
  1930. 'w' => $result->week,
  1931. ),
  1932. home_url( '/' )
  1933. );
  1934. if ( 'post' !== $parsed_args['post_type'] ) {
  1935. $url = add_query_arg( 'post_type', $parsed_args['post_type'], $url );
  1936. }
  1937. $text = $arc_week_start . $archive_week_separator . $arc_week_end;
  1938. if ( $parsed_args['show_post_count'] ) {
  1939. $parsed_args['after'] = '&nbsp;(' . $result->posts . ')' . $after;
  1940. }
  1941. $selected = is_archive() && (string) $parsed_args['year'] === $result->yr && (string) $parsed_args['w'] === $result->week;
  1942. $output .= get_archives_link( $url, $text, $parsed_args['format'], $parsed_args['before'], $parsed_args['after'], $selected );
  1943. }
  1944. }
  1945. }
  1946. } elseif ( ( 'postbypost' === $parsed_args['type'] ) || ( 'alpha' === $parsed_args['type'] ) ) {
  1947. $orderby = ( 'alpha' === $parsed_args['type'] ) ? 'post_title ASC ' : 'post_date DESC, ID DESC ';
  1948. $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
  1949. $key = md5( $query );
  1950. $key = "wp_get_archives:$key:$last_changed";
  1951. $results = wp_cache_get( $key, 'posts' );
  1952. if ( ! $results ) {
  1953. $results = $wpdb->get_results( $query );
  1954. wp_cache_set( $key, $results, 'posts' );
  1955. }
  1956. if ( $results ) {
  1957. foreach ( (array) $results as $result ) {
  1958. if ( '0000-00-00 00:00:00' !== $result->post_date ) {
  1959. $url = get_permalink( $result );
  1960. if ( $result->post_title ) {
  1961. /** This filter is documented in wp-includes/post-template.php */
  1962. $text = strip_tags( apply_filters( 'the_title', $result->post_title, $result->ID ) );
  1963. } else {
  1964. $text = $result->ID;
  1965. }
  1966. $selected = get_the_ID() === $result->ID;
  1967. $output .= get_archives_link( $url, $text, $parsed_args['format'], $parsed_args['before'], $parsed_args['after'], $selected );
  1968. }
  1969. }
  1970. }
  1971. }
  1972. if ( $parsed_args['echo'] ) {
  1973. echo $output;
  1974. } else {
  1975. return $output;
  1976. }
  1977. }
  1978. /**
  1979. * Get number of days since the start of the week.
  1980. *
  1981. * @since 1.5.0
  1982. *
  1983. * @param int $num Number of day.
  1984. * @return float Days since the start of the week.
  1985. */
  1986. function calendar_week_mod( $num ) {
  1987. $base = 7;
  1988. return ( $num - $base * floor( $num / $base ) );
  1989. }
  1990. /**
  1991. * Display calendar with days that have posts as links.
  1992. *
  1993. * The calendar is cached, which will be retrieved, if it exists. If there are
  1994. * no posts for the month, then it will not be displayed.
  1995. *
  1996. * @since 1.0.0
  1997. *
  1998. * @global wpdb $wpdb WordPress database abstraction object.
  1999. * @global int $m
  2000. * @global int $monthnum
  2001. * @global int $year
  2002. * @global WP_Locale $wp_locale WordPress date and time locale object.
  2003. * @global array $posts
  2004. *
  2005. * @param bool $initial Optional. Whether to use initial calendar names. Default true.
  2006. * @param bool $echo Optional. Whether to display the calendar output. Default true.
  2007. * @return void|string Void if `$echo` argument is true, calendar HTML if `$echo` is false.
  2008. */
  2009. function get_calendar( $initial = true, $echo = true ) {
  2010. global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
  2011. $key = md5( $m . $monthnum . $year );
  2012. $cache = wp_cache_get( 'get_calendar', 'calendar' );
  2013. if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {
  2014. /** This filter is documented in wp-includes/general-template.php */
  2015. $output = apply_filters( 'get_calendar', $cache[ $key ] );
  2016. if ( $echo ) {
  2017. echo $output;
  2018. return;
  2019. }
  2020. return $output;
  2021. }
  2022. if ( ! is_array( $cache ) ) {
  2023. $cache = array();
  2024. }
  2025. // Quick check. If we have no posts at all, abort!
  2026. if ( ! $posts ) {
  2027. $gotsome = $wpdb->get_var( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1" );
  2028. if ( ! $gotsome ) {
  2029. $cache[ $key ] = '';
  2030. wp_cache_set( 'get_calendar', $cache, 'calendar' );
  2031. return;
  2032. }
  2033. }
  2034. if ( isset( $_GET['w'] ) ) {
  2035. $w = (int) $_GET['w'];
  2036. }
  2037. // week_begins = 0 stands for Sunday.
  2038. $week_begins = (int) get_option( 'start_of_week' );
  2039. // Let's figure out when we are.
  2040. if ( ! empty( $monthnum ) && ! empty( $year ) ) {
  2041. $thismonth = zeroise( (int) $monthnum, 2 );
  2042. $thisyear = (int) $year;
  2043. } elseif ( ! empty( $w ) ) {
  2044. // We need to get the month from MySQL.
  2045. $thisyear = (int) substr( $m, 0, 4 );
  2046. // It seems MySQL's weeks disagree with PHP's.
  2047. $d = ( ( $w - 1 ) * 7 ) + 6;
  2048. $thismonth = $wpdb->get_var( "SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')" );
  2049. } elseif ( ! empty( $m ) ) {
  2050. $thisyear = (int) substr( $m, 0, 4 );
  2051. if ( strlen( $m ) < 6 ) {
  2052. $thismonth = '01';
  2053. } else {
  2054. $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 );
  2055. }
  2056. } else {
  2057. $thisyear = current_time( 'Y' );
  2058. $thismonth = current_time( 'm' );
  2059. }
  2060. $unixmonth = mktime( 0, 0, 0, $thismonth, 1, $thisyear );
  2061. $last_day = gmdate( 't', $unixmonth );
  2062. // Get the next and previous month and year with at least one post.
  2063. $previous = $wpdb->get_row(
  2064. "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
  2065. FROM $wpdb->posts
  2066. WHERE post_date < '$thisyear-$thismonth-01'
  2067. AND post_type = 'post' AND post_status = 'publish'
  2068. ORDER BY post_date DESC
  2069. LIMIT 1"
  2070. );
  2071. $next = $wpdb->get_row(
  2072. "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
  2073. FROM $wpdb->posts
  2074. WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
  2075. AND post_type = 'post' AND post_status = 'publish'
  2076. ORDER BY post_date ASC
  2077. LIMIT 1"
  2078. );
  2079. /* translators: Calendar caption: 1: Month name, 2: 4-digit year. */
  2080. $calendar_caption = _x( '%1$s %2$s', 'calendar caption' );
  2081. $calendar_output = '<table id="wp-calendar" class="wp-calendar-table">
  2082. <caption>' . sprintf(
  2083. $calendar_caption,
  2084. $wp_locale->get_month( $thismonth ),
  2085. gmdate( 'Y', $unixmonth )
  2086. ) . '</caption>
  2087. <thead>
  2088. <tr>';
  2089. $myweek = array();
  2090. for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) {
  2091. $myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
  2092. }
  2093. foreach ( $myweek as $wd ) {
  2094. $day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
  2095. $wd = esc_attr( $wd );
  2096. $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
  2097. }
  2098. $calendar_output .= '
  2099. </tr>
  2100. </thead>
  2101. <tbody>
  2102. <tr>';
  2103. $daywithpost = array();
  2104. // Get days with posts.
  2105. $dayswithposts = $wpdb->get_results(
  2106. "SELECT DISTINCT DAYOFMONTH(post_date)
  2107. FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
  2108. AND post_type = 'post' AND post_status = 'publish'
  2109. AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'",
  2110. ARRAY_N
  2111. );
  2112. if ( $dayswithposts ) {
  2113. foreach ( (array) $dayswithposts as $daywith ) {
  2114. $daywithpost[] = (int) $daywith[0];
  2115. }
  2116. }
  2117. // See how much we should pad in the beginning.
  2118. $pad = calendar_week_mod( gmdate( 'w', $unixmonth ) - $week_begins );
  2119. if ( 0 != $pad ) {
  2120. $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr( $pad ) . '" class="pad">&nbsp;</td>';
  2121. }
  2122. $newrow = false;
  2123. $daysinmonth = (int) gmdate( 't', $unixmonth );
  2124. for ( $day = 1; $day <= $daysinmonth; ++$day ) {
  2125. if ( isset( $newrow ) && $newrow ) {
  2126. $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
  2127. }
  2128. $newrow = false;
  2129. if ( current_time( 'j' ) == $day &&
  2130. current_time( 'm' ) == $thismonth &&
  2131. current_time( 'Y' ) == $thisyear ) {
  2132. $calendar_output .= '<td id="today">';
  2133. } else {
  2134. $calendar_output .= '<td>';
  2135. }
  2136. if ( in_array( $day, $daywithpost, true ) ) {
  2137. // Any posts today?
  2138. $date_format = gmdate( _x( 'F j, Y', 'daily archives date format' ), strtotime( "{$thisyear}-{$thismonth}-{$day}" ) );
  2139. /* translators: Post calendar label. %s: Date. */
  2140. $label = sprintf( __( 'Posts published on %s' ), $date_format );
  2141. $calendar_output .= sprintf(
  2142. '<a href="%s" aria-label="%s">%s</a>',
  2143. get_day_link( $thisyear, $thismonth, $day ),
  2144. esc_attr( $label ),
  2145. $day
  2146. );
  2147. } else {
  2148. $calendar_output .= $day;
  2149. }
  2150. $calendar_output .= '</td>';
  2151. if ( 6 == calendar_week_mod( gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
  2152. $newrow = true;
  2153. }
  2154. }
  2155. $pad = 7 - calendar_week_mod( gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins );
  2156. if ( 0 != $pad && 7 != $pad ) {
  2157. $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr( $pad ) . '">&nbsp;</td>';
  2158. }
  2159. $calendar_output .= "\n\t</tr>\n\t</tbody>";
  2160. $calendar_output .= "\n\t</table>";
  2161. $calendar_output .= '<nav aria-label="' . __( 'Previous and next months' ) . '" class="wp-calendar-nav">';
  2162. if ( $previous ) {
  2163. $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-prev"><a href="' . get_month_link( $previous->year, $previous->month ) . '">&laquo; ' .
  2164. $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) ) .
  2165. '</a></span>';
  2166. } else {
  2167. $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-prev">&nbsp;</span>';
  2168. }
  2169. $calendar_output .= "\n\t\t" . '<span class="pad">&nbsp;</span>';
  2170. if ( $next ) {
  2171. $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-next"><a href="' . get_month_link( $next->year, $next->month ) . '">' .
  2172. $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) ) .
  2173. ' &raquo;</a></span>';
  2174. } else {
  2175. $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-next">&nbsp;</span>';
  2176. }
  2177. $calendar_output .= '
  2178. </nav>';
  2179. $cache[ $key ] = $calendar_output;
  2180. wp_cache_set( 'get_calendar', $cache, 'calendar' );
  2181. if ( $echo ) {
  2182. /**
  2183. * Filters the HTML calendar output.
  2184. *
  2185. * @since 3.0.0
  2186. *
  2187. * @param string $calendar_output HTML output of the calendar.
  2188. */
  2189. echo apply_filters( 'get_calendar', $calendar_output );
  2190. return;
  2191. }
  2192. /** This filter is documented in wp-includes/general-template.php */
  2193. return apply_filters( 'get_calendar', $calendar_output );
  2194. }
  2195. /**
  2196. * Purge the cached results of get_calendar.
  2197. *
  2198. * @see get_calendar()
  2199. * @since 2.1.0
  2200. */
  2201. function delete_get_calendar_cache() {
  2202. wp_cache_delete( 'get_calendar', 'calendar' );
  2203. }
  2204. /**
  2205. * Display all of the allowed tags in HTML format with attributes.
  2206. *
  2207. * This is useful for displaying in the comment area, which elements and
  2208. * attributes are supported. As well as any plugins which want to display it.
  2209. *
  2210. * @since 1.0.1
  2211. *
  2212. * @global array $allowedtags
  2213. *
  2214. * @return string HTML allowed tags entity encoded.
  2215. */
  2216. function allowed_tags() {
  2217. global $allowedtags;
  2218. $allowed = '';
  2219. foreach ( (array) $allowedtags as $tag => $attributes ) {
  2220. $allowed .= '<' . $tag;
  2221. if ( 0 < count( $attributes ) ) {
  2222. foreach ( $attributes as $attribute => $limits ) {
  2223. $allowed .= ' ' . $attribute . '=""';
  2224. }
  2225. }
  2226. $allowed .= '> ';
  2227. }
  2228. return htmlentities( $allowed );
  2229. }
  2230. /***** Date/Time tags */
  2231. /**
  2232. * Outputs the date in iso8601 format for xml files.
  2233. *
  2234. * @since 1.0.0
  2235. */
  2236. function the_date_xml() {
  2237. echo mysql2date( 'Y-m-d', get_post()->post_date, false );
  2238. }
  2239. /**
  2240. * Display or Retrieve the date the current post was written (once per date)
  2241. *
  2242. * Will only output the date if the current post's date is different from the
  2243. * previous one output.
  2244. *
  2245. * i.e. Only one date listing will show per day worth of posts shown in the loop, even if the
  2246. * function is called several times for each post.
  2247. *
  2248. * HTML output can be filtered with 'the_date'.
  2249. * Date string output can be filtered with 'get_the_date'.
  2250. *
  2251. * @since 0.71
  2252. *
  2253. * @global string $currentday The day of the current post in the loop.
  2254. * @global string $previousday The day of the previous post in the loop.
  2255. *
  2256. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  2257. * @param string $before Optional. Output before the date. Default empty.
  2258. * @param string $after Optional. Output after the date. Default empty.
  2259. * @param bool $echo Optional. Whether to echo the date or return it. Default true.
  2260. * @return string|void String if retrieving.
  2261. */
  2262. function the_date( $format = '', $before = '', $after = '', $echo = true ) {
  2263. global $currentday, $previousday;
  2264. $the_date = '';
  2265. if ( is_new_day() ) {
  2266. $the_date = $before . get_the_date( $format ) . $after;
  2267. $previousday = $currentday;
  2268. }
  2269. /**
  2270. * Filters the date a post was published for display.
  2271. *
  2272. * @since 0.71
  2273. *
  2274. * @param string $the_date The formatted date string.
  2275. * @param string $format PHP date format.
  2276. * @param string $before HTML output before the date.
  2277. * @param string $after HTML output after the date.
  2278. */
  2279. $the_date = apply_filters( 'the_date', $the_date, $format, $before, $after );
  2280. if ( $echo ) {
  2281. echo $the_date;
  2282. } else {
  2283. return $the_date;
  2284. }
  2285. }
  2286. /**
  2287. * Retrieve the date on which the post was written.
  2288. *
  2289. * Unlike the_date() this function will always return the date.
  2290. * Modify output with the {@see 'get_the_date'} filter.
  2291. *
  2292. * @since 3.0.0
  2293. *
  2294. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  2295. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post.
  2296. * @return string|false Date the current post was written. False on failure.
  2297. */
  2298. function get_the_date( $format = '', $post = null ) {
  2299. $post = get_post( $post );
  2300. if ( ! $post ) {
  2301. return false;
  2302. }
  2303. $_format = ! empty( $format ) ? $format : get_option( 'date_format' );
  2304. $the_date = get_post_time( $_format, false, $post, true );
  2305. /**
  2306. * Filters the date a post was published.
  2307. *
  2308. * @since 3.0.0
  2309. *
  2310. * @param string $the_date The formatted date.
  2311. * @param string $format PHP date format.
  2312. * @param int|WP_Post $post The post object or ID.
  2313. */
  2314. return apply_filters( 'get_the_date', $the_date, $format, $post );
  2315. }
  2316. /**
  2317. * Display the date on which the post was last modified.
  2318. *
  2319. * @since 2.1.0
  2320. *
  2321. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  2322. * @param string $before Optional. Output before the date. Default empty.
  2323. * @param string $after Optional. Output after the date. Default empty.
  2324. * @param bool $echo Optional. Whether to echo the date or return it. Default true.
  2325. * @return string|void String if retrieving.
  2326. */
  2327. function the_modified_date( $format = '', $before = '', $after = '', $echo = true ) {
  2328. $the_modified_date = $before . get_the_modified_date( $format ) . $after;
  2329. /**
  2330. * Filters the date a post was last modified for display.
  2331. *
  2332. * @since 2.1.0
  2333. *
  2334. * @param string|false $the_modified_date The last modified date or false if no post is found.
  2335. * @param string $format PHP date format.
  2336. * @param string $before HTML output before the date.
  2337. * @param string $after HTML output after the date.
  2338. */
  2339. $the_modified_date = apply_filters( 'the_modified_date', $the_modified_date, $format, $before, $after );
  2340. if ( $echo ) {
  2341. echo $the_modified_date;
  2342. } else {
  2343. return $the_modified_date;
  2344. }
  2345. }
  2346. /**
  2347. * Retrieve the date on which the post was last modified.
  2348. *
  2349. * @since 2.1.0
  2350. * @since 4.6.0 Added the `$post` parameter.
  2351. *
  2352. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  2353. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post.
  2354. * @return string|int|false Date the current post was modified. False on failure.
  2355. */
  2356. function get_the_modified_date( $format = '', $post = null ) {
  2357. $post = get_post( $post );
  2358. if ( ! $post ) {
  2359. // For backward compatibility, failures go through the filter below.
  2360. $the_time = false;
  2361. } else {
  2362. $_format = ! empty( $format ) ? $format : get_option( 'date_format' );
  2363. $the_time = get_post_modified_time( $_format, false, $post, true );
  2364. }
  2365. /**
  2366. * Filters the date a post was last modified.
  2367. *
  2368. * @since 2.1.0
  2369. * @since 4.6.0 Added the `$post` parameter.
  2370. *
  2371. * @param string|int|false $the_time The formatted date or false if no post is found.
  2372. * @param string $format PHP date format.
  2373. * @param WP_Post|null $post WP_Post object or null if no post is found.
  2374. */
  2375. return apply_filters( 'get_the_modified_date', $the_time, $format, $post );
  2376. }
  2377. /**
  2378. * Display the time at which the post was written.
  2379. *
  2380. * @since 0.71
  2381. *
  2382. * @param string $format Optional. Format to use for retrieving the time the post
  2383. * was written. Accepts 'G', 'U', or PHP date format.
  2384. * Defaults to the 'time_format' option.
  2385. */
  2386. function the_time( $format = '' ) {
  2387. /**
  2388. * Filters the time a post was written for display.
  2389. *
  2390. * @since 0.71
  2391. *
  2392. * @param string $get_the_time The formatted time.
  2393. * @param string $format Format to use for retrieving the time the post
  2394. * was written. Accepts 'G', 'U', or PHP date format.
  2395. */
  2396. echo apply_filters( 'the_time', get_the_time( $format ), $format );
  2397. }
  2398. /**
  2399. * Retrieve the time at which the post was written.
  2400. *
  2401. * @since 1.5.0
  2402. *
  2403. * @param string $format Optional. Format to use for retrieving the time the post
  2404. * was written. Accepts 'G', 'U', or PHP date format.
  2405. * Defaults to the 'time_format' option.
  2406. * @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object.
  2407. * @return string|int|false Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
  2408. * False on failure.
  2409. */
  2410. function get_the_time( $format = '', $post = null ) {
  2411. $post = get_post( $post );
  2412. if ( ! $post ) {
  2413. return false;
  2414. }
  2415. $_format = ! empty( $format ) ? $format : get_option( 'time_format' );
  2416. $the_time = get_post_time( $_format, false, $post, true );
  2417. /**
  2418. * Filters the time a post was written.
  2419. *
  2420. * @since 1.5.0
  2421. *
  2422. * @param string $the_time The formatted time.
  2423. * @param string $format Format to use for retrieving the time the post
  2424. * was written. Accepts 'G', 'U', or PHP date format.
  2425. * @param int|WP_Post $post WP_Post object or ID.
  2426. */
  2427. return apply_filters( 'get_the_time', $the_time, $format, $post );
  2428. }
  2429. /**
  2430. * Retrieve the time at which the post was written.
  2431. *
  2432. * @since 2.0.0
  2433. *
  2434. * @param string $format Optional. Format to use for retrieving the time the post
  2435. * was written. Accepts 'G', 'U', or PHP date format. Default 'U'.
  2436. * @param bool $gmt Optional. Whether to retrieve the GMT time. Default false.
  2437. * @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object.
  2438. * @param bool $translate Whether to translate the time string. Default false.
  2439. * @return string|int|false Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
  2440. * False on failure.
  2441. */
  2442. function get_post_time( $format = 'U', $gmt = false, $post = null, $translate = false ) {
  2443. $post = get_post( $post );
  2444. if ( ! $post ) {
  2445. return false;
  2446. }
  2447. $source = ( $gmt ) ? 'gmt' : 'local';
  2448. $datetime = get_post_datetime( $post, 'date', $source );
  2449. if ( false === $datetime ) {
  2450. return false;
  2451. }
  2452. if ( 'U' === $format || 'G' === $format ) {
  2453. $time = $datetime->getTimestamp();
  2454. // Returns a sum of timestamp with timezone offset. Ideally should never be used.
  2455. if ( ! $gmt ) {
  2456. $time += $datetime->getOffset();
  2457. }
  2458. } elseif ( $translate ) {
  2459. $time = wp_date( $format, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
  2460. } else {
  2461. if ( $gmt ) {
  2462. $datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
  2463. }
  2464. $time = $datetime->format( $format );
  2465. }
  2466. /**
  2467. * Filters the localized time a post was written.
  2468. *
  2469. * @since 2.6.0
  2470. *
  2471. * @param string $time The formatted time.
  2472. * @param string $format Format to use for retrieving the time the post was written.
  2473. * Accepts 'G', 'U', or PHP date format. Default 'U'.
  2474. * @param bool $gmt Whether to retrieve the GMT time. Default false.
  2475. */
  2476. return apply_filters( 'get_post_time', $time, $format, $gmt );
  2477. }
  2478. /**
  2479. * Retrieve post published or modified time as a `DateTimeImmutable` object instance.
  2480. *
  2481. * The object will be set to the timezone from WordPress settings.
  2482. *
  2483. * For legacy reasons, this function allows to choose to instantiate from local or UTC time in database.
  2484. * Normally this should make no difference to the result. However, the values might get out of sync in database,
  2485. * typically because of timezone setting changes. The parameter ensures the ability to reproduce backwards
  2486. * compatible behaviors in such cases.
  2487. *
  2488. * @since 5.3.0
  2489. *
  2490. * @param int|WP_Post $post Optional. WP_Post object or ID. Default is global `$post` object.
  2491. * @param string $field Optional. Published or modified time to use from database. Accepts 'date' or 'modified'.
  2492. * Default 'date'.
  2493. * @param string $source Optional. Local or UTC time to use from database. Accepts 'local' or 'gmt'.
  2494. * Default 'local'.
  2495. * @return DateTimeImmutable|false Time object on success, false on failure.
  2496. */
  2497. function get_post_datetime( $post = null, $field = 'date', $source = 'local' ) {
  2498. $post = get_post( $post );
  2499. if ( ! $post ) {
  2500. return false;
  2501. }
  2502. $wp_timezone = wp_timezone();
  2503. if ( 'gmt' === $source ) {
  2504. $time = ( 'modified' === $field ) ? $post->post_modified_gmt : $post->post_date_gmt;
  2505. $timezone = new DateTimeZone( 'UTC' );
  2506. } else {
  2507. $time = ( 'modified' === $field ) ? $post->post_modified : $post->post_date;
  2508. $timezone = $wp_timezone;
  2509. }
  2510. if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) {
  2511. return false;
  2512. }
  2513. $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', $time, $timezone );
  2514. if ( false === $datetime ) {
  2515. return false;
  2516. }
  2517. return $datetime->setTimezone( $wp_timezone );
  2518. }
  2519. /**
  2520. * Retrieve post published or modified time as a Unix timestamp.
  2521. *
  2522. * Note that this function returns a true Unix timestamp, not summed with timezone offset
  2523. * like older WP functions.
  2524. *
  2525. * @since 5.3.0
  2526. *
  2527. * @param int|WP_Post $post Optional. WP_Post object or ID. Default is global `$post` object.
  2528. * @param string $field Optional. Published or modified time to use from database. Accepts 'date' or 'modified'.
  2529. * Default 'date'.
  2530. * @return int|false Unix timestamp on success, false on failure.
  2531. */
  2532. function get_post_timestamp( $post = null, $field = 'date' ) {
  2533. $datetime = get_post_datetime( $post, $field );
  2534. if ( false === $datetime ) {
  2535. return false;
  2536. }
  2537. return $datetime->getTimestamp();
  2538. }
  2539. /**
  2540. * Display the time at which the post was last modified.
  2541. *
  2542. * @since 2.0.0
  2543. *
  2544. * @param string $format Optional. Format to use for retrieving the time the post
  2545. * was modified. Accepts 'G', 'U', or PHP date format.
  2546. * Defaults to the 'time_format' option.
  2547. */
  2548. function the_modified_time( $format = '' ) {
  2549. /**
  2550. * Filters the localized time a post was last modified, for display.
  2551. *
  2552. * @since 2.0.0
  2553. *
  2554. * @param string|false $get_the_modified_time The formatted time or false if no post is found.
  2555. * @param string $format Format to use for retrieving the time the post
  2556. * was modified. Accepts 'G', 'U', or PHP date format.
  2557. */
  2558. echo apply_filters( 'the_modified_time', get_the_modified_time( $format ), $format );
  2559. }
  2560. /**
  2561. * Retrieve the time at which the post was last modified.
  2562. *
  2563. * @since 2.0.0
  2564. * @since 4.6.0 Added the `$post` parameter.
  2565. *
  2566. * @param string $format Optional. Format to use for retrieving the time the post
  2567. * was modified. Accepts 'G', 'U', or PHP date format.
  2568. * Defaults to the 'time_format' option.
  2569. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post.
  2570. * @return string|int|false Formatted date string or Unix timestamp. False on failure.
  2571. */
  2572. function get_the_modified_time( $format = '', $post = null ) {
  2573. $post = get_post( $post );
  2574. if ( ! $post ) {
  2575. // For backward compatibility, failures go through the filter below.
  2576. $the_time = false;
  2577. } else {
  2578. $_format = ! empty( $format ) ? $format : get_option( 'time_format' );
  2579. $the_time = get_post_modified_time( $_format, false, $post, true );
  2580. }
  2581. /**
  2582. * Filters the localized time a post was last modified.
  2583. *
  2584. * @since 2.0.0
  2585. * @since 4.6.0 Added the `$post` parameter.
  2586. *
  2587. * @param string|int|false $the_time The formatted time or false if no post is found.
  2588. * @param string $format Format to use for retrieving the time the post
  2589. * was modified. Accepts 'G', 'U', or PHP date format.
  2590. * @param WP_Post|null $post WP_Post object or null if no post is found.
  2591. */
  2592. return apply_filters( 'get_the_modified_time', $the_time, $format, $post );
  2593. }
  2594. /**
  2595. * Retrieve the time at which the post was last modified.
  2596. *
  2597. * @since 2.0.0
  2598. *
  2599. * @param string $format Optional. Format to use for retrieving the time the post
  2600. * was modified. Accepts 'G', 'U', or PHP date format. Default 'U'.
  2601. * @param bool $gmt Optional. Whether to retrieve the GMT time. Default false.
  2602. * @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object.
  2603. * @param bool $translate Whether to translate the time string. Default false.
  2604. * @return string|int|false Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
  2605. * False on failure.
  2606. */
  2607. function get_post_modified_time( $format = 'U', $gmt = false, $post = null, $translate = false ) {
  2608. $post = get_post( $post );
  2609. if ( ! $post ) {
  2610. return false;
  2611. }
  2612. $source = ( $gmt ) ? 'gmt' : 'local';
  2613. $datetime = get_post_datetime( $post, 'modified', $source );
  2614. if ( false === $datetime ) {
  2615. return false;
  2616. }
  2617. if ( 'U' === $format || 'G' === $format ) {
  2618. $time = $datetime->getTimestamp();
  2619. // Returns a sum of timestamp with timezone offset. Ideally should never be used.
  2620. if ( ! $gmt ) {
  2621. $time += $datetime->getOffset();
  2622. }
  2623. } elseif ( $translate ) {
  2624. $time = wp_date( $format, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
  2625. } else {
  2626. if ( $gmt ) {
  2627. $datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
  2628. }
  2629. $time = $datetime->format( $format );
  2630. }
  2631. /**
  2632. * Filters the localized time a post was last modified.
  2633. *
  2634. * @since 2.8.0
  2635. *
  2636. * @param string|int $time Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
  2637. * @param string $format Format to use for retrieving the time the post was modified.
  2638. * Accepts 'G', 'U', or PHP date format. Default 'U'.
  2639. * @param bool $gmt Whether to retrieve the GMT time. Default false.
  2640. */
  2641. return apply_filters( 'get_post_modified_time', $time, $format, $gmt );
  2642. }
  2643. /**
  2644. * Display the weekday on which the post was written.
  2645. *
  2646. * @since 0.71
  2647. *
  2648. * @global WP_Locale $wp_locale WordPress date and time locale object.
  2649. */
  2650. function the_weekday() {
  2651. global $wp_locale;
  2652. $post = get_post();
  2653. if ( ! $post ) {
  2654. return;
  2655. }
  2656. $the_weekday = $wp_locale->get_weekday( get_post_time( 'w', false, $post ) );
  2657. /**
  2658. * Filters the weekday on which the post was written, for display.
  2659. *
  2660. * @since 0.71
  2661. *
  2662. * @param string $the_weekday
  2663. */
  2664. echo apply_filters( 'the_weekday', $the_weekday );
  2665. }
  2666. /**
  2667. * Display the weekday on which the post was written.
  2668. *
  2669. * Will only output the weekday if the current post's weekday is different from
  2670. * the previous one output.
  2671. *
  2672. * @since 0.71
  2673. *
  2674. * @global WP_Locale $wp_locale WordPress date and time locale object.
  2675. * @global string $currentday The day of the current post in the loop.
  2676. * @global string $previousweekday The day of the previous post in the loop.
  2677. *
  2678. * @param string $before Optional. Output before the date. Default empty.
  2679. * @param string $after Optional. Output after the date. Default empty.
  2680. */
  2681. function the_weekday_date( $before = '', $after = '' ) {
  2682. global $wp_locale, $currentday, $previousweekday;
  2683. $post = get_post();
  2684. if ( ! $post ) {
  2685. return;
  2686. }
  2687. $the_weekday_date = '';
  2688. if ( $currentday !== $previousweekday ) {
  2689. $the_weekday_date .= $before;
  2690. $the_weekday_date .= $wp_locale->get_weekday( get_post_time( 'w', false, $post ) );
  2691. $the_weekday_date .= $after;
  2692. $previousweekday = $currentday;
  2693. }
  2694. /**
  2695. * Filters the localized date on which the post was written, for display.
  2696. *
  2697. * @since 0.71
  2698. *
  2699. * @param string $the_weekday_date The weekday on which the post was written.
  2700. * @param string $before The HTML to output before the date.
  2701. * @param string $after The HTML to output after the date.
  2702. */
  2703. echo apply_filters( 'the_weekday_date', $the_weekday_date, $before, $after );
  2704. }
  2705. /**
  2706. * Fire the wp_head action.
  2707. *
  2708. * See {@see 'wp_head'}.
  2709. *
  2710. * @since 1.2.0
  2711. */
  2712. function wp_head() {
  2713. /**
  2714. * Prints scripts or data in the head tag on the front end.
  2715. *
  2716. * @since 1.5.0
  2717. */
  2718. do_action( 'wp_head' );
  2719. }
  2720. /**
  2721. * Fire the wp_footer action.
  2722. *
  2723. * See {@see 'wp_footer'}.
  2724. *
  2725. * @since 1.5.1
  2726. */
  2727. function wp_footer() {
  2728. /**
  2729. * Prints scripts or data before the closing body tag on the front end.
  2730. *
  2731. * @since 1.5.1
  2732. */
  2733. do_action( 'wp_footer' );
  2734. }
  2735. /**
  2736. * Fire the wp_body_open action.
  2737. *
  2738. * See {@see 'wp_body_open'}.
  2739. *
  2740. * @since 5.2.0
  2741. */
  2742. function wp_body_open() {
  2743. /**
  2744. * Triggered after the opening body tag.
  2745. *
  2746. * @since 5.2.0
  2747. */
  2748. do_action( 'wp_body_open' );
  2749. }
  2750. /**
  2751. * Display the links to the general feeds.
  2752. *
  2753. * @since 2.8.0
  2754. *
  2755. * @param array $args Optional arguments.
  2756. */
  2757. function feed_links( $args = array() ) {
  2758. if ( ! current_theme_supports( 'automatic-feed-links' ) ) {
  2759. return;
  2760. }
  2761. $defaults = array(
  2762. /* translators: Separator between blog name and feed type in feed links. */
  2763. 'separator' => _x( '&raquo;', 'feed link' ),
  2764. /* translators: 1: Blog title, 2: Separator (raquo). */
  2765. 'feedtitle' => __( '%1$s %2$s Feed' ),
  2766. /* translators: 1: Blog title, 2: Separator (raquo). */
  2767. 'comstitle' => __( '%1$s %2$s Comments Feed' ),
  2768. );
  2769. $args = wp_parse_args( $args, $defaults );
  2770. /**
  2771. * Filters whether to display the posts feed link.
  2772. *
  2773. * @since 4.4.0
  2774. *
  2775. * @param bool $show Whether to display the posts feed link. Default true.
  2776. */
  2777. if ( apply_filters( 'feed_links_show_posts_feed', true ) ) {
  2778. echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( sprintf( $args['feedtitle'], get_bloginfo( 'name' ), $args['separator'] ) ) . '" href="' . esc_url( get_feed_link() ) . "\" />\n";
  2779. }
  2780. /**
  2781. * Filters whether to display the comments feed link.
  2782. *
  2783. * @since 4.4.0
  2784. *
  2785. * @param bool $show Whether to display the comments feed link. Default true.
  2786. */
  2787. if ( apply_filters( 'feed_links_show_comments_feed', true ) ) {
  2788. echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( sprintf( $args['comstitle'], get_bloginfo( 'name' ), $args['separator'] ) ) . '" href="' . esc_url( get_feed_link( 'comments_' . get_default_feed() ) ) . "\" />\n";
  2789. }
  2790. }
  2791. /**
  2792. * Display the links to the extra feeds such as category feeds.
  2793. *
  2794. * @since 2.8.0
  2795. *
  2796. * @param array $args Optional arguments.
  2797. */
  2798. function feed_links_extra( $args = array() ) {
  2799. $defaults = array(
  2800. /* translators: Separator between blog name and feed type in feed links. */
  2801. 'separator' => _x( '&raquo;', 'feed link' ),
  2802. /* translators: 1: Blog name, 2: Separator (raquo), 3: Post title. */
  2803. 'singletitle' => __( '%1$s %2$s %3$s Comments Feed' ),
  2804. /* translators: 1: Blog name, 2: Separator (raquo), 3: Category name. */
  2805. 'cattitle' => __( '%1$s %2$s %3$s Category Feed' ),
  2806. /* translators: 1: Blog name, 2: Separator (raquo), 3: Tag name. */
  2807. 'tagtitle' => __( '%1$s %2$s %3$s Tag Feed' ),
  2808. /* translators: 1: Blog name, 2: Separator (raquo), 3: Term name, 4: Taxonomy singular name. */
  2809. 'taxtitle' => __( '%1$s %2$s %3$s %4$s Feed' ),
  2810. /* translators: 1: Blog name, 2: Separator (raquo), 3: Author name. */
  2811. 'authortitle' => __( '%1$s %2$s Posts by %3$s Feed' ),
  2812. /* translators: 1: Blog name, 2: Separator (raquo), 3: Search query. */
  2813. 'searchtitle' => __( '%1$s %2$s Search Results for &#8220;%3$s&#8221; Feed' ),
  2814. /* translators: 1: Blog name, 2: Separator (raquo), 3: Post type name. */
  2815. 'posttypetitle' => __( '%1$s %2$s %3$s Feed' ),
  2816. );
  2817. $args = wp_parse_args( $args, $defaults );
  2818. if ( is_singular() ) {
  2819. $id = 0;
  2820. $post = get_post( $id );
  2821. if ( comments_open() || pings_open() || $post->comment_count > 0 ) {
  2822. $title = sprintf( $args['singletitle'], get_bloginfo( 'name' ), $args['separator'], the_title_attribute( array( 'echo' => false ) ) );
  2823. $href = get_post_comments_feed_link( $post->ID );
  2824. }
  2825. } elseif ( is_post_type_archive() ) {
  2826. $post_type = get_query_var( 'post_type' );
  2827. if ( is_array( $post_type ) ) {
  2828. $post_type = reset( $post_type );
  2829. }
  2830. $post_type_obj = get_post_type_object( $post_type );
  2831. $title = sprintf( $args['posttypetitle'], get_bloginfo( 'name' ), $args['separator'], $post_type_obj->labels->name );
  2832. $href = get_post_type_archive_feed_link( $post_type_obj->name );
  2833. } elseif ( is_category() ) {
  2834. $term = get_queried_object();
  2835. if ( $term ) {
  2836. $title = sprintf( $args['cattitle'], get_bloginfo( 'name' ), $args['separator'], $term->name );
  2837. $href = get_category_feed_link( $term->term_id );
  2838. }
  2839. } elseif ( is_tag() ) {
  2840. $term = get_queried_object();
  2841. if ( $term ) {
  2842. $title = sprintf( $args['tagtitle'], get_bloginfo( 'name' ), $args['separator'], $term->name );
  2843. $href = get_tag_feed_link( $term->term_id );
  2844. }
  2845. } elseif ( is_tax() ) {
  2846. $term = get_queried_object();
  2847. if ( $term ) {
  2848. $tax = get_taxonomy( $term->taxonomy );
  2849. $title = sprintf( $args['taxtitle'], get_bloginfo( 'name' ), $args['separator'], $term->name, $tax->labels->singular_name );
  2850. $href = get_term_feed_link( $term->term_id, $term->taxonomy );
  2851. }
  2852. } elseif ( is_author() ) {
  2853. $author_id = (int) get_query_var( 'author' );
  2854. $title = sprintf( $args['authortitle'], get_bloginfo( 'name' ), $args['separator'], get_the_author_meta( 'display_name', $author_id ) );
  2855. $href = get_author_feed_link( $author_id );
  2856. } elseif ( is_search() ) {
  2857. $title = sprintf( $args['searchtitle'], get_bloginfo( 'name' ), $args['separator'], get_search_query( false ) );
  2858. $href = get_search_feed_link();
  2859. }
  2860. if ( isset( $title ) && isset( $href ) ) {
  2861. echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( $title ) . '" href="' . esc_url( $href ) . '" />' . "\n";
  2862. }
  2863. }
  2864. /**
  2865. * Display the link to the Really Simple Discovery service endpoint.
  2866. *
  2867. * @link http://archipelago.phrasewise.com/rsd
  2868. * @since 2.0.0
  2869. */
  2870. function rsd_link() {
  2871. echo '<link rel="EditURI" type="application/rsd+xml" title="RSD" href="' . esc_url( site_url( 'xmlrpc.php?rsd', 'rpc' ) ) . '" />' . "\n";
  2872. }
  2873. /**
  2874. * Display the link to the Windows Live Writer manifest file.
  2875. *
  2876. * @link https://msdn.microsoft.com/en-us/library/bb463265.aspx
  2877. * @since 2.3.1
  2878. */
  2879. function wlwmanifest_link() {
  2880. echo '<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="' . includes_url( 'wlwmanifest.xml' ) . '" /> ' . "\n";
  2881. }
  2882. /**
  2883. * Displays a referrer strict-origin-when-cross-origin meta tag.
  2884. *
  2885. * Outputs a referrer origin-when-cross-origin meta tag that tells the browser not to send the full
  2886. * url as a referrer to other sites when cross-origin assets are loaded.
  2887. *
  2888. * Typical usage is as a wp_head callback. add_action( 'wp_head', 'wp_strict_cross_origin_referrer' );
  2889. *
  2890. * @since 5.7.0
  2891. */
  2892. function wp_strict_cross_origin_referrer() {
  2893. ?>
  2894. <meta name='referrer' content='strict-origin-when-cross-origin' />
  2895. <?php
  2896. }
  2897. /**
  2898. * Display site icon meta tags.
  2899. *
  2900. * @since 4.3.0
  2901. *
  2902. * @link https://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#rel-icon HTML5 specification link icon.
  2903. */
  2904. function wp_site_icon() {
  2905. if ( ! has_site_icon() && ! is_customize_preview() ) {
  2906. return;
  2907. }
  2908. $meta_tags = array();
  2909. $icon_32 = get_site_icon_url( 32 );
  2910. if ( empty( $icon_32 ) && is_customize_preview() ) {
  2911. $icon_32 = '/favicon.ico'; // Serve default favicon URL in customizer so element can be updated for preview.
  2912. }
  2913. if ( $icon_32 ) {
  2914. $meta_tags[] = sprintf( '<link rel="icon" href="%s" sizes="32x32" />', esc_url( $icon_32 ) );
  2915. }
  2916. $icon_192 = get_site_icon_url( 192 );
  2917. if ( $icon_192 ) {
  2918. $meta_tags[] = sprintf( '<link rel="icon" href="%s" sizes="192x192" />', esc_url( $icon_192 ) );
  2919. }
  2920. $icon_180 = get_site_icon_url( 180 );
  2921. if ( $icon_180 ) {
  2922. $meta_tags[] = sprintf( '<link rel="apple-touch-icon" href="%s" />', esc_url( $icon_180 ) );
  2923. }
  2924. $icon_270 = get_site_icon_url( 270 );
  2925. if ( $icon_270 ) {
  2926. $meta_tags[] = sprintf( '<meta name="msapplication-TileImage" content="%s" />', esc_url( $icon_270 ) );
  2927. }
  2928. /**
  2929. * Filters the site icon meta tags, so plugins can add their own.
  2930. *
  2931. * @since 4.3.0
  2932. *
  2933. * @param string[] $meta_tags Array of Site Icon meta tags.
  2934. */
  2935. $meta_tags = apply_filters( 'site_icon_meta_tags', $meta_tags );
  2936. $meta_tags = array_filter( $meta_tags );
  2937. foreach ( $meta_tags as $meta_tag ) {
  2938. echo "$meta_tag\n";
  2939. }
  2940. }
  2941. /**
  2942. * Prints resource hints to browsers for pre-fetching, pre-rendering
  2943. * and pre-connecting to web sites.
  2944. *
  2945. * Gives hints to browsers to prefetch specific pages or render them
  2946. * in the background, to perform DNS lookups or to begin the connection
  2947. * handshake (DNS, TCP, TLS) in the background.
  2948. *
  2949. * These performance improving indicators work by using `<link rel"…">`.
  2950. *
  2951. * @since 4.6.0
  2952. */
  2953. function wp_resource_hints() {
  2954. $hints = array(
  2955. 'dns-prefetch' => wp_dependencies_unique_hosts(),
  2956. 'preconnect' => array(),
  2957. 'prefetch' => array(),
  2958. 'prerender' => array(),
  2959. );
  2960. /*
  2961. * Add DNS prefetch for the Emoji CDN.
  2962. * The path is removed in the foreach loop below.
  2963. */
  2964. /** This filter is documented in wp-includes/formatting.php */
  2965. $hints['dns-prefetch'][] = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/13.0.0/svg/' );
  2966. foreach ( $hints as $relation_type => $urls ) {
  2967. $unique_urls = array();
  2968. /**
  2969. * Filters domains and URLs for resource hints of relation type.
  2970. *
  2971. * @since 4.6.0
  2972. * @since 4.7.0 The `$urls` parameter accepts arrays of specific HTML attributes
  2973. * as its child elements.
  2974. *
  2975. * @param array $urls {
  2976. * Array of resources and their attributes, or URLs to print for resource hints.
  2977. *
  2978. * @type array|string ...$0 {
  2979. * Array of resource attributes, or a URL string.
  2980. *
  2981. * @type string $href URL to include in resource hints. Required.
  2982. * @type string $as How the browser should treat the resource
  2983. * (`script`, `style`, `image`, `document`, etc).
  2984. * @type string $crossorigin Indicates the CORS policy of the specified resource.
  2985. * @type float $pr Expected probability that the resource hint will be used.
  2986. * @type string $type Type of the resource (`text/html`, `text/css`, etc).
  2987. * }
  2988. * }
  2989. * @param string $relation_type The relation type the URLs are printed for,
  2990. * e.g. 'preconnect' or 'prerender'.
  2991. */
  2992. $urls = apply_filters( 'wp_resource_hints', $urls, $relation_type );
  2993. foreach ( $urls as $key => $url ) {
  2994. $atts = array();
  2995. if ( is_array( $url ) ) {
  2996. if ( isset( $url['href'] ) ) {
  2997. $atts = $url;
  2998. $url = $url['href'];
  2999. } else {
  3000. continue;
  3001. }
  3002. }
  3003. $url = esc_url( $url, array( 'http', 'https' ) );
  3004. if ( ! $url ) {
  3005. continue;
  3006. }
  3007. if ( isset( $unique_urls[ $url ] ) ) {
  3008. continue;
  3009. }
  3010. if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ), true ) ) {
  3011. $parsed = wp_parse_url( $url );
  3012. if ( empty( $parsed['host'] ) ) {
  3013. continue;
  3014. }
  3015. if ( 'preconnect' === $relation_type && ! empty( $parsed['scheme'] ) ) {
  3016. $url = $parsed['scheme'] . '://' . $parsed['host'];
  3017. } else {
  3018. // Use protocol-relative URLs for dns-prefetch or if scheme is missing.
  3019. $url = '//' . $parsed['host'];
  3020. }
  3021. }
  3022. $atts['rel'] = $relation_type;
  3023. $atts['href'] = $url;
  3024. $unique_urls[ $url ] = $atts;
  3025. }
  3026. foreach ( $unique_urls as $atts ) {
  3027. $html = '';
  3028. foreach ( $atts as $attr => $value ) {
  3029. if ( ! is_scalar( $value )
  3030. || ( ! in_array( $attr, array( 'as', 'crossorigin', 'href', 'pr', 'rel', 'type' ), true ) && ! is_numeric( $attr ) )
  3031. ) {
  3032. continue;
  3033. }
  3034. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  3035. if ( ! is_string( $attr ) ) {
  3036. $html .= " $value";
  3037. } else {
  3038. $html .= " $attr='$value'";
  3039. }
  3040. }
  3041. $html = trim( $html );
  3042. echo "<link $html />\n";
  3043. }
  3044. }
  3045. }
  3046. /**
  3047. * Retrieves a list of unique hosts of all enqueued scripts and styles.
  3048. *
  3049. * @since 4.6.0
  3050. *
  3051. * @return string[] A list of unique hosts of enqueued scripts and styles.
  3052. */
  3053. function wp_dependencies_unique_hosts() {
  3054. global $wp_scripts, $wp_styles;
  3055. $unique_hosts = array();
  3056. foreach ( array( $wp_scripts, $wp_styles ) as $dependencies ) {
  3057. if ( $dependencies instanceof WP_Dependencies && ! empty( $dependencies->queue ) ) {
  3058. foreach ( $dependencies->queue as $handle ) {
  3059. if ( ! isset( $dependencies->registered[ $handle ] ) ) {
  3060. continue;
  3061. }
  3062. /* @var _WP_Dependency $dependency */
  3063. $dependency = $dependencies->registered[ $handle ];
  3064. $parsed = wp_parse_url( $dependency->src );
  3065. if ( ! empty( $parsed['host'] )
  3066. && ! in_array( $parsed['host'], $unique_hosts, true ) && $parsed['host'] !== $_SERVER['SERVER_NAME']
  3067. ) {
  3068. $unique_hosts[] = $parsed['host'];
  3069. }
  3070. }
  3071. }
  3072. }
  3073. return $unique_hosts;
  3074. }
  3075. /**
  3076. * Whether the user can access the visual editor.
  3077. *
  3078. * Checks if the user can access the visual editor and that it's supported by the user's browser.
  3079. *
  3080. * @since 2.0.0
  3081. *
  3082. * @global bool $wp_rich_edit Whether the user can access the visual editor.
  3083. * @global bool $is_gecko Whether the browser is Gecko-based.
  3084. * @global bool $is_opera Whether the browser is Opera.
  3085. * @global bool $is_safari Whether the browser is Safari.
  3086. * @global bool $is_chrome Whether the browser is Chrome.
  3087. * @global bool $is_IE Whether the browser is Internet Explorer.
  3088. * @global bool $is_edge Whether the browser is Microsoft Edge.
  3089. *
  3090. * @return bool True if the user can access the visual editor, false otherwise.
  3091. */
  3092. function user_can_richedit() {
  3093. global $wp_rich_edit, $is_gecko, $is_opera, $is_safari, $is_chrome, $is_IE, $is_edge;
  3094. if ( ! isset( $wp_rich_edit ) ) {
  3095. $wp_rich_edit = false;
  3096. if ( 'true' === get_user_option( 'rich_editing' ) || ! is_user_logged_in() ) { // Default to 'true' for logged out users.
  3097. if ( $is_safari ) {
  3098. $wp_rich_edit = ! wp_is_mobile() || ( preg_match( '!AppleWebKit/(\d+)!', $_SERVER['HTTP_USER_AGENT'], $match ) && (int) $match[1] >= 534 );
  3099. } elseif ( $is_IE ) {
  3100. $wp_rich_edit = ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Trident/7.0;' ) !== false );
  3101. } elseif ( $is_gecko || $is_chrome || $is_edge || ( $is_opera && ! wp_is_mobile() ) ) {
  3102. $wp_rich_edit = true;
  3103. }
  3104. }
  3105. }
  3106. /**
  3107. * Filters whether the user can access the visual editor.
  3108. *
  3109. * @since 2.1.0
  3110. *
  3111. * @param bool $wp_rich_edit Whether the user can access the visual editor.
  3112. */
  3113. return apply_filters( 'user_can_richedit', $wp_rich_edit );
  3114. }
  3115. /**
  3116. * Find out which editor should be displayed by default.
  3117. *
  3118. * Works out which of the two editors to display as the current editor for a
  3119. * user. The 'html' setting is for the "Text" editor tab.
  3120. *
  3121. * @since 2.5.0
  3122. *
  3123. * @return string Either 'tinymce', or 'html', or 'test'
  3124. */
  3125. function wp_default_editor() {
  3126. $r = user_can_richedit() ? 'tinymce' : 'html'; // Defaults.
  3127. if ( wp_get_current_user() ) { // Look for cookie.
  3128. $ed = get_user_setting( 'editor', 'tinymce' );
  3129. $r = ( in_array( $ed, array( 'tinymce', 'html', 'test' ), true ) ) ? $ed : $r;
  3130. }
  3131. /**
  3132. * Filters which editor should be displayed by default.
  3133. *
  3134. * @since 2.5.0
  3135. *
  3136. * @param string $r Which editor should be displayed by default. Either 'tinymce', 'html', or 'test'.
  3137. */
  3138. return apply_filters( 'wp_default_editor', $r );
  3139. }
  3140. /**
  3141. * Renders an editor.
  3142. *
  3143. * Using this function is the proper way to output all needed components for both TinyMCE and Quicktags.
  3144. * _WP_Editors should not be used directly. See https://core.trac.wordpress.org/ticket/17144.
  3145. *
  3146. * NOTE: Once initialized the TinyMCE editor cannot be safely moved in the DOM. For that reason
  3147. * running wp_editor() inside of a meta box is not a good idea unless only Quicktags is used.
  3148. * On the post edit screen several actions can be used to include additional editors
  3149. * containing TinyMCE: 'edit_page_form', 'edit_form_advanced' and 'dbx_post_sidebar'.
  3150. * See https://core.trac.wordpress.org/ticket/19173 for more information.
  3151. *
  3152. * @see _WP_Editors::editor()
  3153. * @see _WP_Editors::parse_settings()
  3154. * @since 3.3.0
  3155. *
  3156. * @param string $content Initial content for the editor.
  3157. * @param string $editor_id HTML ID attribute value for the textarea and TinyMCE.
  3158. * Should not contain square brackets.
  3159. * @param array $settings See _WP_Editors::parse_settings() for description.
  3160. */
  3161. function wp_editor( $content, $editor_id, $settings = array() ) {
  3162. if ( ! class_exists( '_WP_Editors', false ) ) {
  3163. require ABSPATH . WPINC . '/class-wp-editor.php';
  3164. }
  3165. _WP_Editors::editor( $content, $editor_id, $settings );
  3166. }
  3167. /**
  3168. * Outputs the editor scripts, stylesheets, and default settings.
  3169. *
  3170. * The editor can be initialized when needed after page load.
  3171. * See wp.editor.initialize() in wp-admin/js/editor.js for initialization options.
  3172. *
  3173. * @uses _WP_Editors
  3174. * @since 4.8.0
  3175. */
  3176. function wp_enqueue_editor() {
  3177. if ( ! class_exists( '_WP_Editors', false ) ) {
  3178. require ABSPATH . WPINC . '/class-wp-editor.php';
  3179. }
  3180. _WP_Editors::enqueue_default_editor();
  3181. }
  3182. /**
  3183. * Enqueue assets needed by the code editor for the given settings.
  3184. *
  3185. * @since 4.9.0
  3186. *
  3187. * @see wp_enqueue_editor()
  3188. * @see wp_get_code_editor_settings();
  3189. * @see _WP_Editors::parse_settings()
  3190. *
  3191. * @param array $args {
  3192. * Args.
  3193. *
  3194. * @type string $type The MIME type of the file to be edited.
  3195. * @type string $file Filename to be edited. Extension is used to sniff the type. Can be supplied as alternative to `$type` param.
  3196. * @type WP_Theme $theme Theme being edited when on theme editor.
  3197. * @type string $plugin Plugin being edited when on plugin editor.
  3198. * @type array $codemirror Additional CodeMirror setting overrides.
  3199. * @type array $csslint CSSLint rule overrides.
  3200. * @type array $jshint JSHint rule overrides.
  3201. * @type array $htmlhint HTMLHint rule overrides.
  3202. * }
  3203. * @return array|false Settings for the enqueued code editor, or false if the editor was not enqueued.
  3204. */
  3205. function wp_enqueue_code_editor( $args ) {
  3206. if ( is_user_logged_in() && 'false' === wp_get_current_user()->syntax_highlighting ) {
  3207. return false;
  3208. }
  3209. $settings = wp_get_code_editor_settings( $args );
  3210. if ( empty( $settings ) || empty( $settings['codemirror'] ) ) {
  3211. return false;
  3212. }
  3213. wp_enqueue_script( 'code-editor' );
  3214. wp_enqueue_style( 'code-editor' );
  3215. if ( isset( $settings['codemirror']['mode'] ) ) {
  3216. $mode = $settings['codemirror']['mode'];
  3217. if ( is_string( $mode ) ) {
  3218. $mode = array(
  3219. 'name' => $mode,
  3220. );
  3221. }
  3222. if ( ! empty( $settings['codemirror']['lint'] ) ) {
  3223. switch ( $mode['name'] ) {
  3224. case 'css':
  3225. case 'text/css':
  3226. case 'text/x-scss':
  3227. case 'text/x-less':
  3228. wp_enqueue_script( 'csslint' );
  3229. break;
  3230. case 'htmlmixed':
  3231. case 'text/html':
  3232. case 'php':
  3233. case 'application/x-httpd-php':
  3234. case 'text/x-php':
  3235. wp_enqueue_script( 'htmlhint' );
  3236. wp_enqueue_script( 'csslint' );
  3237. wp_enqueue_script( 'jshint' );
  3238. if ( ! current_user_can( 'unfiltered_html' ) ) {
  3239. wp_enqueue_script( 'htmlhint-kses' );
  3240. }
  3241. break;
  3242. case 'javascript':
  3243. case 'application/ecmascript':
  3244. case 'application/json':
  3245. case 'application/javascript':
  3246. case 'application/ld+json':
  3247. case 'text/typescript':
  3248. case 'application/typescript':
  3249. wp_enqueue_script( 'jshint' );
  3250. wp_enqueue_script( 'jsonlint' );
  3251. break;
  3252. }
  3253. }
  3254. }
  3255. wp_add_inline_script( 'code-editor', sprintf( 'jQuery.extend( wp.codeEditor.defaultSettings, %s );', wp_json_encode( $settings ) ) );
  3256. /**
  3257. * Fires when scripts and styles are enqueued for the code editor.
  3258. *
  3259. * @since 4.9.0
  3260. *
  3261. * @param array $settings Settings for the enqueued code editor.
  3262. */
  3263. do_action( 'wp_enqueue_code_editor', $settings );
  3264. return $settings;
  3265. }
  3266. /**
  3267. * Generate and return code editor settings.
  3268. *
  3269. * @since 5.0.0
  3270. *
  3271. * @see wp_enqueue_code_editor()
  3272. *
  3273. * @param array $args {
  3274. * Args.
  3275. *
  3276. * @type string $type The MIME type of the file to be edited.
  3277. * @type string $file Filename to be edited. Extension is used to sniff the type. Can be supplied as alternative to `$type` param.
  3278. * @type WP_Theme $theme Theme being edited when on theme editor.
  3279. * @type string $plugin Plugin being edited when on plugin editor.
  3280. * @type array $codemirror Additional CodeMirror setting overrides.
  3281. * @type array $csslint CSSLint rule overrides.
  3282. * @type array $jshint JSHint rule overrides.
  3283. * @type array $htmlhint HTMLHint rule overrides.
  3284. * }
  3285. * @return array|false Settings for the code editor.
  3286. */
  3287. function wp_get_code_editor_settings( $args ) {
  3288. $settings = array(
  3289. 'codemirror' => array(
  3290. 'indentUnit' => 4,
  3291. 'indentWithTabs' => true,
  3292. 'inputStyle' => 'contenteditable',
  3293. 'lineNumbers' => true,
  3294. 'lineWrapping' => true,
  3295. 'styleActiveLine' => true,
  3296. 'continueComments' => true,
  3297. 'extraKeys' => array(
  3298. 'Ctrl-Space' => 'autocomplete',
  3299. 'Ctrl-/' => 'toggleComment',
  3300. 'Cmd-/' => 'toggleComment',
  3301. 'Alt-F' => 'findPersistent',
  3302. 'Ctrl-F' => 'findPersistent',
  3303. 'Cmd-F' => 'findPersistent',
  3304. ),
  3305. 'direction' => 'ltr', // Code is shown in LTR even in RTL languages.
  3306. 'gutters' => array(),
  3307. ),
  3308. 'csslint' => array(
  3309. 'errors' => true, // Parsing errors.
  3310. 'box-model' => true,
  3311. 'display-property-grouping' => true,
  3312. 'duplicate-properties' => true,
  3313. 'known-properties' => true,
  3314. 'outline-none' => true,
  3315. ),
  3316. 'jshint' => array(
  3317. // The following are copied from <https://github.com/WordPress/wordpress-develop/blob/4.8.1/.jshintrc>.
  3318. 'boss' => true,
  3319. 'curly' => true,
  3320. 'eqeqeq' => true,
  3321. 'eqnull' => true,
  3322. 'es3' => true,
  3323. 'expr' => true,
  3324. 'immed' => true,
  3325. 'noarg' => true,
  3326. 'nonbsp' => true,
  3327. 'onevar' => true,
  3328. 'quotmark' => 'single',
  3329. 'trailing' => true,
  3330. 'undef' => true,
  3331. 'unused' => true,
  3332. 'browser' => true,
  3333. 'globals' => array(
  3334. '_' => false,
  3335. 'Backbone' => false,
  3336. 'jQuery' => false,
  3337. 'JSON' => false,
  3338. 'wp' => false,
  3339. ),
  3340. ),
  3341. 'htmlhint' => array(
  3342. 'tagname-lowercase' => true,
  3343. 'attr-lowercase' => true,
  3344. 'attr-value-double-quotes' => false,
  3345. 'doctype-first' => false,
  3346. 'tag-pair' => true,
  3347. 'spec-char-escape' => true,
  3348. 'id-unique' => true,
  3349. 'src-not-empty' => true,
  3350. 'attr-no-duplication' => true,
  3351. 'alt-require' => true,
  3352. 'space-tab-mixed-disabled' => 'tab',
  3353. 'attr-unsafe-chars' => true,
  3354. ),
  3355. );
  3356. $type = '';
  3357. if ( isset( $args['type'] ) ) {
  3358. $type = $args['type'];
  3359. // Remap MIME types to ones that CodeMirror modes will recognize.
  3360. if ( 'application/x-patch' === $type || 'text/x-patch' === $type ) {
  3361. $type = 'text/x-diff';
  3362. }
  3363. } elseif ( isset( $args['file'] ) && false !== strpos( basename( $args['file'] ), '.' ) ) {
  3364. $extension = strtolower( pathinfo( $args['file'], PATHINFO_EXTENSION ) );
  3365. foreach ( wp_get_mime_types() as $exts => $mime ) {
  3366. if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
  3367. $type = $mime;
  3368. break;
  3369. }
  3370. }
  3371. // Supply any types that are not matched by wp_get_mime_types().
  3372. if ( empty( $type ) ) {
  3373. switch ( $extension ) {
  3374. case 'conf':
  3375. $type = 'text/nginx';
  3376. break;
  3377. case 'css':
  3378. $type = 'text/css';
  3379. break;
  3380. case 'diff':
  3381. case 'patch':
  3382. $type = 'text/x-diff';
  3383. break;
  3384. case 'html':
  3385. case 'htm':
  3386. $type = 'text/html';
  3387. break;
  3388. case 'http':
  3389. $type = 'message/http';
  3390. break;
  3391. case 'js':
  3392. $type = 'text/javascript';
  3393. break;
  3394. case 'json':
  3395. $type = 'application/json';
  3396. break;
  3397. case 'jsx':
  3398. $type = 'text/jsx';
  3399. break;
  3400. case 'less':
  3401. $type = 'text/x-less';
  3402. break;
  3403. case 'md':
  3404. $type = 'text/x-gfm';
  3405. break;
  3406. case 'php':
  3407. case 'phtml':
  3408. case 'php3':
  3409. case 'php4':
  3410. case 'php5':
  3411. case 'php7':
  3412. case 'phps':
  3413. $type = 'application/x-httpd-php';
  3414. break;
  3415. case 'scss':
  3416. $type = 'text/x-scss';
  3417. break;
  3418. case 'sass':
  3419. $type = 'text/x-sass';
  3420. break;
  3421. case 'sh':
  3422. case 'bash':
  3423. $type = 'text/x-sh';
  3424. break;
  3425. case 'sql':
  3426. $type = 'text/x-sql';
  3427. break;
  3428. case 'svg':
  3429. $type = 'application/svg+xml';
  3430. break;
  3431. case 'xml':
  3432. $type = 'text/xml';
  3433. break;
  3434. case 'yml':
  3435. case 'yaml':
  3436. $type = 'text/x-yaml';
  3437. break;
  3438. case 'txt':
  3439. default:
  3440. $type = 'text/plain';
  3441. break;
  3442. }
  3443. }
  3444. }
  3445. if ( in_array( $type, array( 'text/css', 'text/x-scss', 'text/x-less', 'text/x-sass' ), true ) ) {
  3446. $settings['codemirror'] = array_merge(
  3447. $settings['codemirror'],
  3448. array(
  3449. 'mode' => $type,
  3450. 'lint' => false,
  3451. 'autoCloseBrackets' => true,
  3452. 'matchBrackets' => true,
  3453. )
  3454. );
  3455. } elseif ( 'text/x-diff' === $type ) {
  3456. $settings['codemirror'] = array_merge(
  3457. $settings['codemirror'],
  3458. array(
  3459. 'mode' => 'diff',
  3460. )
  3461. );
  3462. } elseif ( 'text/html' === $type ) {
  3463. $settings['codemirror'] = array_merge(
  3464. $settings['codemirror'],
  3465. array(
  3466. 'mode' => 'htmlmixed',
  3467. 'lint' => true,
  3468. 'autoCloseBrackets' => true,
  3469. 'autoCloseTags' => true,
  3470. 'matchTags' => array(
  3471. 'bothTags' => true,
  3472. ),
  3473. )
  3474. );
  3475. if ( ! current_user_can( 'unfiltered_html' ) ) {
  3476. $settings['htmlhint']['kses'] = wp_kses_allowed_html( 'post' );
  3477. }
  3478. } elseif ( 'text/x-gfm' === $type ) {
  3479. $settings['codemirror'] = array_merge(
  3480. $settings['codemirror'],
  3481. array(
  3482. 'mode' => 'gfm',
  3483. 'highlightFormatting' => true,
  3484. )
  3485. );
  3486. } elseif ( 'application/javascript' === $type || 'text/javascript' === $type ) {
  3487. $settings['codemirror'] = array_merge(
  3488. $settings['codemirror'],
  3489. array(
  3490. 'mode' => 'javascript',
  3491. 'lint' => true,
  3492. 'autoCloseBrackets' => true,
  3493. 'matchBrackets' => true,
  3494. )
  3495. );
  3496. } elseif ( false !== strpos( $type, 'json' ) ) {
  3497. $settings['codemirror'] = array_merge(
  3498. $settings['codemirror'],
  3499. array(
  3500. 'mode' => array(
  3501. 'name' => 'javascript',
  3502. ),
  3503. 'lint' => true,
  3504. 'autoCloseBrackets' => true,
  3505. 'matchBrackets' => true,
  3506. )
  3507. );
  3508. if ( 'application/ld+json' === $type ) {
  3509. $settings['codemirror']['mode']['jsonld'] = true;
  3510. } else {
  3511. $settings['codemirror']['mode']['json'] = true;
  3512. }
  3513. } elseif ( false !== strpos( $type, 'jsx' ) ) {
  3514. $settings['codemirror'] = array_merge(
  3515. $settings['codemirror'],
  3516. array(
  3517. 'mode' => 'jsx',
  3518. 'autoCloseBrackets' => true,
  3519. 'matchBrackets' => true,
  3520. )
  3521. );
  3522. } elseif ( 'text/x-markdown' === $type ) {
  3523. $settings['codemirror'] = array_merge(
  3524. $settings['codemirror'],
  3525. array(
  3526. 'mode' => 'markdown',
  3527. 'highlightFormatting' => true,
  3528. )
  3529. );
  3530. } elseif ( 'text/nginx' === $type ) {
  3531. $settings['codemirror'] = array_merge(
  3532. $settings['codemirror'],
  3533. array(
  3534. 'mode' => 'nginx',
  3535. )
  3536. );
  3537. } elseif ( 'application/x-httpd-php' === $type ) {
  3538. $settings['codemirror'] = array_merge(
  3539. $settings['codemirror'],
  3540. array(
  3541. 'mode' => 'php',
  3542. 'autoCloseBrackets' => true,
  3543. 'autoCloseTags' => true,
  3544. 'matchBrackets' => true,
  3545. 'matchTags' => array(
  3546. 'bothTags' => true,
  3547. ),
  3548. )
  3549. );
  3550. } elseif ( 'text/x-sql' === $type || 'text/x-mysql' === $type ) {
  3551. $settings['codemirror'] = array_merge(
  3552. $settings['codemirror'],
  3553. array(
  3554. 'mode' => 'sql',
  3555. 'autoCloseBrackets' => true,
  3556. 'matchBrackets' => true,
  3557. )
  3558. );
  3559. } elseif ( false !== strpos( $type, 'xml' ) ) {
  3560. $settings['codemirror'] = array_merge(
  3561. $settings['codemirror'],
  3562. array(
  3563. 'mode' => 'xml',
  3564. 'autoCloseBrackets' => true,
  3565. 'autoCloseTags' => true,
  3566. 'matchTags' => array(
  3567. 'bothTags' => true,
  3568. ),
  3569. )
  3570. );
  3571. } elseif ( 'text/x-yaml' === $type ) {
  3572. $settings['codemirror'] = array_merge(
  3573. $settings['codemirror'],
  3574. array(
  3575. 'mode' => 'yaml',
  3576. )
  3577. );
  3578. } else {
  3579. $settings['codemirror']['mode'] = $type;
  3580. }
  3581. if ( ! empty( $settings['codemirror']['lint'] ) ) {
  3582. $settings['codemirror']['gutters'][] = 'CodeMirror-lint-markers';
  3583. }
  3584. // Let settings supplied via args override any defaults.
  3585. foreach ( wp_array_slice_assoc( $args, array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ) ) as $key => $value ) {
  3586. $settings[ $key ] = array_merge(
  3587. $settings[ $key ],
  3588. $value
  3589. );
  3590. }
  3591. /**
  3592. * Filters settings that are passed into the code editor.
  3593. *
  3594. * Returning a falsey value will disable the syntax-highlighting code editor.
  3595. *
  3596. * @since 4.9.0
  3597. *
  3598. * @param array $settings The array of settings passed to the code editor.
  3599. * A falsey value disables the editor.
  3600. * @param array $args {
  3601. * Args passed when calling `get_code_editor_settings()`.
  3602. *
  3603. * @type string $type The MIME type of the file to be edited.
  3604. * @type string $file Filename being edited.
  3605. * @type WP_Theme $theme Theme being edited when on theme editor.
  3606. * @type string $plugin Plugin being edited when on plugin editor.
  3607. * @type array $codemirror Additional CodeMirror setting overrides.
  3608. * @type array $csslint CSSLint rule overrides.
  3609. * @type array $jshint JSHint rule overrides.
  3610. * @type array $htmlhint HTMLHint rule overrides.
  3611. * }
  3612. */
  3613. return apply_filters( 'wp_code_editor_settings', $settings, $args );
  3614. }
  3615. /**
  3616. * Retrieves the contents of the search WordPress query variable.
  3617. *
  3618. * The search query string is passed through esc_attr() to ensure that it is safe
  3619. * for placing in an HTML attribute.
  3620. *
  3621. * @since 2.3.0
  3622. *
  3623. * @param bool $escaped Whether the result is escaped. Default true.
  3624. * Only use when you are later escaping it. Do not use unescaped.
  3625. * @return string
  3626. */
  3627. function get_search_query( $escaped = true ) {
  3628. /**
  3629. * Filters the contents of the search query variable.
  3630. *
  3631. * @since 2.3.0
  3632. *
  3633. * @param mixed $search Contents of the search query variable.
  3634. */
  3635. $query = apply_filters( 'get_search_query', get_query_var( 's' ) );
  3636. if ( $escaped ) {
  3637. $query = esc_attr( $query );
  3638. }
  3639. return $query;
  3640. }
  3641. /**
  3642. * Displays the contents of the search query variable.
  3643. *
  3644. * The search query string is passed through esc_attr() to ensure that it is safe
  3645. * for placing in an HTML attribute.
  3646. *
  3647. * @since 2.1.0
  3648. */
  3649. function the_search_query() {
  3650. /**
  3651. * Filters the contents of the search query variable for display.
  3652. *
  3653. * @since 2.3.0
  3654. *
  3655. * @param mixed $search Contents of the search query variable.
  3656. */
  3657. echo esc_attr( apply_filters( 'the_search_query', get_search_query( false ) ) );
  3658. }
  3659. /**
  3660. * Gets the language attributes for the 'html' tag.
  3661. *
  3662. * Builds up a set of HTML attributes containing the text direction and language
  3663. * information for the page.
  3664. *
  3665. * @since 4.3.0
  3666. *
  3667. * @param string $doctype Optional. The type of HTML document. Accepts 'xhtml' or 'html'. Default 'html'.
  3668. */
  3669. function get_language_attributes( $doctype = 'html' ) {
  3670. $attributes = array();
  3671. if ( function_exists( 'is_rtl' ) && is_rtl() ) {
  3672. $attributes[] = 'dir="rtl"';
  3673. }
  3674. $lang = get_bloginfo( 'language' );
  3675. if ( $lang ) {
  3676. if ( 'text/html' === get_option( 'html_type' ) || 'html' === $doctype ) {
  3677. $attributes[] = 'lang="' . esc_attr( $lang ) . '"';
  3678. }
  3679. if ( 'text/html' !== get_option( 'html_type' ) || 'xhtml' === $doctype ) {
  3680. $attributes[] = 'xml:lang="' . esc_attr( $lang ) . '"';
  3681. }
  3682. }
  3683. $output = implode( ' ', $attributes );
  3684. /**
  3685. * Filters the language attributes for display in the 'html' tag.
  3686. *
  3687. * @since 2.5.0
  3688. * @since 4.3.0 Added the `$doctype` parameter.
  3689. *
  3690. * @param string $output A space-separated list of language attributes.
  3691. * @param string $doctype The type of HTML document (xhtml|html).
  3692. */
  3693. return apply_filters( 'language_attributes', $output, $doctype );
  3694. }
  3695. /**
  3696. * Displays the language attributes for the 'html' tag.
  3697. *
  3698. * Builds up a set of HTML attributes containing the text direction and language
  3699. * information for the page.
  3700. *
  3701. * @since 2.1.0
  3702. * @since 4.3.0 Converted into a wrapper for get_language_attributes().
  3703. *
  3704. * @param string $doctype Optional. The type of HTML document. Accepts 'xhtml' or 'html'. Default 'html'.
  3705. */
  3706. function language_attributes( $doctype = 'html' ) {
  3707. echo get_language_attributes( $doctype );
  3708. }
  3709. /**
  3710. * Retrieves paginated links for archive post pages.
  3711. *
  3712. * Technically, the function can be used to create paginated link list for any
  3713. * area. The 'base' argument is used to reference the url, which will be used to
  3714. * create the paginated links. The 'format' argument is then used for replacing
  3715. * the page number. It is however, most likely and by default, to be used on the
  3716. * archive post pages.
  3717. *
  3718. * The 'type' argument controls format of the returned value. The default is
  3719. * 'plain', which is just a string with the links separated by a newline
  3720. * character. The other possible values are either 'array' or 'list'. The
  3721. * 'array' value will return an array of the paginated link list to offer full
  3722. * control of display. The 'list' value will place all of the paginated links in
  3723. * an unordered HTML list.
  3724. *
  3725. * The 'total' argument is the total amount of pages and is an integer. The
  3726. * 'current' argument is the current page number and is also an integer.
  3727. *
  3728. * An example of the 'base' argument is "http://example.com/all_posts.php%_%"
  3729. * and the '%_%' is required. The '%_%' will be replaced by the contents of in
  3730. * the 'format' argument. An example for the 'format' argument is "?page=%#%"
  3731. * and the '%#%' is also required. The '%#%' will be replaced with the page
  3732. * number.
  3733. *
  3734. * You can include the previous and next links in the list by setting the
  3735. * 'prev_next' argument to true, which it is by default. You can set the
  3736. * previous text, by using the 'prev_text' argument. You can set the next text
  3737. * by setting the 'next_text' argument.
  3738. *
  3739. * If the 'show_all' argument is set to true, then it will show all of the pages
  3740. * instead of a short list of the pages near the current page. By default, the
  3741. * 'show_all' is set to false and controlled by the 'end_size' and 'mid_size'
  3742. * arguments. The 'end_size' argument is how many numbers on either the start
  3743. * and the end list edges, by default is 1. The 'mid_size' argument is how many
  3744. * numbers to either side of current page, but not including current page.
  3745. *
  3746. * It is possible to add query vars to the link by using the 'add_args' argument
  3747. * and see add_query_arg() for more information.
  3748. *
  3749. * The 'before_page_number' and 'after_page_number' arguments allow users to
  3750. * augment the links themselves. Typically this might be to add context to the
  3751. * numbered links so that screen reader users understand what the links are for.
  3752. * The text strings are added before and after the page number - within the
  3753. * anchor tag.
  3754. *
  3755. * @since 2.1.0
  3756. * @since 4.9.0 Added the `aria_current` argument.
  3757. *
  3758. * @global WP_Query $wp_query WordPress Query object.
  3759. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  3760. *
  3761. * @param string|array $args {
  3762. * Optional. Array or string of arguments for generating paginated links for archives.
  3763. *
  3764. * @type string $base Base of the paginated url. Default empty.
  3765. * @type string $format Format for the pagination structure. Default empty.
  3766. * @type int $total The total amount of pages. Default is the value WP_Query's
  3767. * `max_num_pages` or 1.
  3768. * @type int $current The current page number. Default is 'paged' query var or 1.
  3769. * @type string $aria_current The value for the aria-current attribute. Possible values are 'page',
  3770. * 'step', 'location', 'date', 'time', 'true', 'false'. Default is 'page'.
  3771. * @type bool $show_all Whether to show all pages. Default false.
  3772. * @type int $end_size How many numbers on either the start and the end list edges.
  3773. * Default 1.
  3774. * @type int $mid_size How many numbers to either side of the current pages. Default 2.
  3775. * @type bool $prev_next Whether to include the previous and next links in the list. Default true.
  3776. * @type bool $prev_text The previous page text. Default '&laquo; Previous'.
  3777. * @type bool $next_text The next page text. Default 'Next &raquo;'.
  3778. * @type string $type Controls format of the returned value. Possible values are 'plain',
  3779. * 'array' and 'list'. Default is 'plain'.
  3780. * @type array $add_args An array of query args to add. Default false.
  3781. * @type string $add_fragment A string to append to each link. Default empty.
  3782. * @type string $before_page_number A string to appear before the page number. Default empty.
  3783. * @type string $after_page_number A string to append after the page number. Default empty.
  3784. * }
  3785. * @return string|array|void String of page links or array of page links, depending on 'type' argument.
  3786. * Void if total number of pages is less than 2.
  3787. */
  3788. function paginate_links( $args = '' ) {
  3789. global $wp_query, $wp_rewrite;
  3790. // Setting up default values based on the current URL.
  3791. $pagenum_link = html_entity_decode( get_pagenum_link() );
  3792. $url_parts = explode( '?', $pagenum_link );
  3793. // Get max pages and current page out of the current query, if available.
  3794. $total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
  3795. $current = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1;
  3796. // Append the format placeholder to the base URL.
  3797. $pagenum_link = trailingslashit( $url_parts[0] ) . '%_%';
  3798. // URL base depends on permalink settings.
  3799. $format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
  3800. $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
  3801. $defaults = array(
  3802. 'base' => $pagenum_link, // http://example.com/all_posts.php%_% : %_% is replaced by format (below).
  3803. 'format' => $format, // ?page=%#% : %#% is replaced by the page number.
  3804. 'total' => $total,
  3805. 'current' => $current,
  3806. 'aria_current' => 'page',
  3807. 'show_all' => false,
  3808. 'prev_next' => true,
  3809. 'prev_text' => __( '&laquo; Previous' ),
  3810. 'next_text' => __( 'Next &raquo;' ),
  3811. 'end_size' => 1,
  3812. 'mid_size' => 2,
  3813. 'type' => 'plain',
  3814. 'add_args' => array(), // Array of query args to add.
  3815. 'add_fragment' => '',
  3816. 'before_page_number' => '',
  3817. 'after_page_number' => '',
  3818. );
  3819. $args = wp_parse_args( $args, $defaults );
  3820. if ( ! is_array( $args['add_args'] ) ) {
  3821. $args['add_args'] = array();
  3822. }
  3823. // Merge additional query vars found in the original URL into 'add_args' array.
  3824. if ( isset( $url_parts[1] ) ) {
  3825. // Find the format argument.
  3826. $format = explode( '?', str_replace( '%_%', $args['format'], $args['base'] ) );
  3827. $format_query = isset( $format[1] ) ? $format[1] : '';
  3828. wp_parse_str( $format_query, $format_args );
  3829. // Find the query args of the requested URL.
  3830. wp_parse_str( $url_parts[1], $url_query_args );
  3831. // Remove the format argument from the array of query arguments, to avoid overwriting custom format.
  3832. foreach ( $format_args as $format_arg => $format_arg_value ) {
  3833. unset( $url_query_args[ $format_arg ] );
  3834. }
  3835. $args['add_args'] = array_merge( $args['add_args'], urlencode_deep( $url_query_args ) );
  3836. }
  3837. // Who knows what else people pass in $args.
  3838. $total = (int) $args['total'];
  3839. if ( $total < 2 ) {
  3840. return;
  3841. }
  3842. $current = (int) $args['current'];
  3843. $end_size = (int) $args['end_size']; // Out of bounds? Make it the default.
  3844. if ( $end_size < 1 ) {
  3845. $end_size = 1;
  3846. }
  3847. $mid_size = (int) $args['mid_size'];
  3848. if ( $mid_size < 0 ) {
  3849. $mid_size = 2;
  3850. }
  3851. $add_args = $args['add_args'];
  3852. $r = '';
  3853. $page_links = array();
  3854. $dots = false;
  3855. if ( $args['prev_next'] && $current && 1 < $current ) :
  3856. $link = str_replace( '%_%', 2 == $current ? '' : $args['format'], $args['base'] );
  3857. $link = str_replace( '%#%', $current - 1, $link );
  3858. if ( $add_args ) {
  3859. $link = add_query_arg( $add_args, $link );
  3860. }
  3861. $link .= $args['add_fragment'];
  3862. $page_links[] = sprintf(
  3863. '<a class="prev page-numbers" href="%s">%s</a>',
  3864. /**
  3865. * Filters the paginated links for the given archive pages.
  3866. *
  3867. * @since 3.0.0
  3868. *
  3869. * @param string $link The paginated link URL.
  3870. */
  3871. esc_url( apply_filters( 'paginate_links', $link ) ),
  3872. $args['prev_text']
  3873. );
  3874. endif;
  3875. for ( $n = 1; $n <= $total; $n++ ) :
  3876. if ( $n == $current ) :
  3877. $page_links[] = sprintf(
  3878. '<span aria-current="%s" class="page-numbers current">%s</span>',
  3879. esc_attr( $args['aria_current'] ),
  3880. $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number']
  3881. );
  3882. $dots = true;
  3883. else :
  3884. if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
  3885. $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] );
  3886. $link = str_replace( '%#%', $n, $link );
  3887. if ( $add_args ) {
  3888. $link = add_query_arg( $add_args, $link );
  3889. }
  3890. $link .= $args['add_fragment'];
  3891. $page_links[] = sprintf(
  3892. '<a class="page-numbers" href="%s">%s</a>',
  3893. /** This filter is documented in wp-includes/general-template.php */
  3894. esc_url( apply_filters( 'paginate_links', $link ) ),
  3895. $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number']
  3896. );
  3897. $dots = true;
  3898. elseif ( $dots && ! $args['show_all'] ) :
  3899. $page_links[] = '<span class="page-numbers dots">' . __( '&hellip;' ) . '</span>';
  3900. $dots = false;
  3901. endif;
  3902. endif;
  3903. endfor;
  3904. if ( $args['prev_next'] && $current && $current < $total ) :
  3905. $link = str_replace( '%_%', $args['format'], $args['base'] );
  3906. $link = str_replace( '%#%', $current + 1, $link );
  3907. if ( $add_args ) {
  3908. $link = add_query_arg( $add_args, $link );
  3909. }
  3910. $link .= $args['add_fragment'];
  3911. $page_links[] = sprintf(
  3912. '<a class="next page-numbers" href="%s">%s</a>',
  3913. /** This filter is documented in wp-includes/general-template.php */
  3914. esc_url( apply_filters( 'paginate_links', $link ) ),
  3915. $args['next_text']
  3916. );
  3917. endif;
  3918. switch ( $args['type'] ) {
  3919. case 'array':
  3920. return $page_links;
  3921. case 'list':
  3922. $r .= "<ul class='page-numbers'>\n\t<li>";
  3923. $r .= implode( "</li>\n\t<li>", $page_links );
  3924. $r .= "</li>\n</ul>\n";
  3925. break;
  3926. default:
  3927. $r = implode( "\n", $page_links );
  3928. break;
  3929. }
  3930. /**
  3931. * Filters the HTML output of paginated links for archives.
  3932. *
  3933. * @since 5.7.0
  3934. *
  3935. * @param string $r HTML output.
  3936. * @param array $args An array of arguments. See paginate_links()
  3937. * for information on accepted arguments.
  3938. */
  3939. $r = apply_filters( 'paginate_links_output', $r, $args );
  3940. return $r;
  3941. }
  3942. /**
  3943. * Registers an admin color scheme css file.
  3944. *
  3945. * Allows a plugin to register a new admin color scheme. For example:
  3946. *
  3947. * wp_admin_css_color( 'classic', __( 'Classic' ), admin_url( "css/colors-classic.css" ), array(
  3948. * '#07273E', '#14568A', '#D54E21', '#2683AE'
  3949. * ) );
  3950. *
  3951. * @since 2.5.0
  3952. *
  3953. * @global array $_wp_admin_css_colors
  3954. *
  3955. * @param string $key The unique key for this theme.
  3956. * @param string $name The name of the theme.
  3957. * @param string $url The URL of the CSS file containing the color scheme.
  3958. * @param array $colors Optional. An array of CSS color definition strings which are used
  3959. * to give the user a feel for the theme.
  3960. * @param array $icons {
  3961. * Optional. CSS color definitions used to color any SVG icons.
  3962. *
  3963. * @type string $base SVG icon base color.
  3964. * @type string $focus SVG icon color on focus.
  3965. * @type string $current SVG icon color of current admin menu link.
  3966. * }
  3967. */
  3968. function wp_admin_css_color( $key, $name, $url, $colors = array(), $icons = array() ) {
  3969. global $_wp_admin_css_colors;
  3970. if ( ! isset( $_wp_admin_css_colors ) ) {
  3971. $_wp_admin_css_colors = array();
  3972. }
  3973. $_wp_admin_css_colors[ $key ] = (object) array(
  3974. 'name' => $name,
  3975. 'url' => $url,
  3976. 'colors' => $colors,
  3977. 'icon_colors' => $icons,
  3978. );
  3979. }
  3980. /**
  3981. * Registers the default admin color schemes.
  3982. *
  3983. * Registers the initial set of eight color schemes in the Profile section
  3984. * of the dashboard which allows for styling the admin menu and toolbar.
  3985. *
  3986. * @see wp_admin_css_color()
  3987. *
  3988. * @since 3.0.0
  3989. */
  3990. function register_admin_color_schemes() {
  3991. $suffix = is_rtl() ? '-rtl' : '';
  3992. $suffix .= SCRIPT_DEBUG ? '' : '.min';
  3993. wp_admin_css_color(
  3994. 'fresh',
  3995. _x( 'Default', 'admin color scheme' ),
  3996. false,
  3997. array( '#1d2327', '#2c3338', '#2271b1', '#72aee6' ),
  3998. array(
  3999. 'base' => '#a7aaad',
  4000. 'focus' => '#72aee6',
  4001. 'current' => '#fff',
  4002. )
  4003. );
  4004. wp_admin_css_color(
  4005. 'light',
  4006. _x( 'Light', 'admin color scheme' ),
  4007. admin_url( "css/colors/light/colors$suffix.css" ),
  4008. array( '#e5e5e5', '#999', '#d64e07', '#04a4cc' ),
  4009. array(
  4010. 'base' => '#999',
  4011. 'focus' => '#ccc',
  4012. 'current' => '#ccc',
  4013. )
  4014. );
  4015. wp_admin_css_color(
  4016. 'modern',
  4017. _x( 'Modern', 'admin color scheme' ),
  4018. admin_url( "css/colors/modern/colors$suffix.css" ),
  4019. array( '#1e1e1e', '#3858e9', '#33f078' ),
  4020. array(
  4021. 'base' => '#f3f1f1',
  4022. 'focus' => '#fff',
  4023. 'current' => '#fff',
  4024. )
  4025. );
  4026. wp_admin_css_color(
  4027. 'blue',
  4028. _x( 'Blue', 'admin color scheme' ),
  4029. admin_url( "css/colors/blue/colors$suffix.css" ),
  4030. array( '#096484', '#4796b3', '#52accc', '#74B6CE' ),
  4031. array(
  4032. 'base' => '#e5f8ff',
  4033. 'focus' => '#fff',
  4034. 'current' => '#fff',
  4035. )
  4036. );
  4037. wp_admin_css_color(
  4038. 'midnight',
  4039. _x( 'Midnight', 'admin color scheme' ),
  4040. admin_url( "css/colors/midnight/colors$suffix.css" ),
  4041. array( '#25282b', '#363b3f', '#69a8bb', '#e14d43' ),
  4042. array(
  4043. 'base' => '#f1f2f3',
  4044. 'focus' => '#fff',
  4045. 'current' => '#fff',
  4046. )
  4047. );
  4048. wp_admin_css_color(
  4049. 'sunrise',
  4050. _x( 'Sunrise', 'admin color scheme' ),
  4051. admin_url( "css/colors/sunrise/colors$suffix.css" ),
  4052. array( '#b43c38', '#cf4944', '#dd823b', '#ccaf0b' ),
  4053. array(
  4054. 'base' => '#f3f1f1',
  4055. 'focus' => '#fff',
  4056. 'current' => '#fff',
  4057. )
  4058. );
  4059. wp_admin_css_color(
  4060. 'ectoplasm',
  4061. _x( 'Ectoplasm', 'admin color scheme' ),
  4062. admin_url( "css/colors/ectoplasm/colors$suffix.css" ),
  4063. array( '#413256', '#523f6d', '#a3b745', '#d46f15' ),
  4064. array(
  4065. 'base' => '#ece6f6',
  4066. 'focus' => '#fff',
  4067. 'current' => '#fff',
  4068. )
  4069. );
  4070. wp_admin_css_color(
  4071. 'ocean',
  4072. _x( 'Ocean', 'admin color scheme' ),
  4073. admin_url( "css/colors/ocean/colors$suffix.css" ),
  4074. array( '#627c83', '#738e96', '#9ebaa0', '#aa9d88' ),
  4075. array(
  4076. 'base' => '#f2fcff',
  4077. 'focus' => '#fff',
  4078. 'current' => '#fff',
  4079. )
  4080. );
  4081. wp_admin_css_color(
  4082. 'coffee',
  4083. _x( 'Coffee', 'admin color scheme' ),
  4084. admin_url( "css/colors/coffee/colors$suffix.css" ),
  4085. array( '#46403c', '#59524c', '#c7a589', '#9ea476' ),
  4086. array(
  4087. 'base' => '#f3f2f1',
  4088. 'focus' => '#fff',
  4089. 'current' => '#fff',
  4090. )
  4091. );
  4092. }
  4093. /**
  4094. * Displays the URL of a WordPress admin CSS file.
  4095. *
  4096. * @see WP_Styles::_css_href and its {@see 'style_loader_src'} filter.
  4097. *
  4098. * @since 2.3.0
  4099. *
  4100. * @param string $file file relative to wp-admin/ without its ".css" extension.
  4101. * @return string
  4102. */
  4103. function wp_admin_css_uri( $file = 'wp-admin' ) {
  4104. if ( defined( 'WP_INSTALLING' ) ) {
  4105. $_file = "./$file.css";
  4106. } else {
  4107. $_file = admin_url( "$file.css" );
  4108. }
  4109. $_file = add_query_arg( 'version', get_bloginfo( 'version' ), $_file );
  4110. /**
  4111. * Filters the URI of a WordPress admin CSS file.
  4112. *
  4113. * @since 2.3.0
  4114. *
  4115. * @param string $_file Relative path to the file with query arguments attached.
  4116. * @param string $file Relative path to the file, minus its ".css" extension.
  4117. */
  4118. return apply_filters( 'wp_admin_css_uri', $_file, $file );
  4119. }
  4120. /**
  4121. * Enqueues or directly prints a stylesheet link to the specified CSS file.
  4122. *
  4123. * "Intelligently" decides to enqueue or to print the CSS file. If the
  4124. * {@see 'wp_print_styles'} action has *not* yet been called, the CSS file will be
  4125. * enqueued. If the {@see 'wp_print_styles'} action has been called, the CSS link will
  4126. * be printed. Printing may be forced by passing true as the $force_echo
  4127. * (second) parameter.
  4128. *
  4129. * For backward compatibility with WordPress 2.3 calling method: If the $file
  4130. * (first) parameter does not correspond to a registered CSS file, we assume
  4131. * $file is a file relative to wp-admin/ without its ".css" extension. A
  4132. * stylesheet link to that generated URL is printed.
  4133. *
  4134. * @since 2.3.0
  4135. *
  4136. * @param string $file Optional. Style handle name or file name (without ".css" extension) relative
  4137. * to wp-admin/. Defaults to 'wp-admin'.
  4138. * @param bool $force_echo Optional. Force the stylesheet link to be printed rather than enqueued.
  4139. */
  4140. function wp_admin_css( $file = 'wp-admin', $force_echo = false ) {
  4141. // For backward compatibility.
  4142. $handle = 0 === strpos( $file, 'css/' ) ? substr( $file, 4 ) : $file;
  4143. if ( wp_styles()->query( $handle ) ) {
  4144. if ( $force_echo || did_action( 'wp_print_styles' ) ) {
  4145. // We already printed the style queue. Print this one immediately.
  4146. wp_print_styles( $handle );
  4147. } else {
  4148. // Add to style queue.
  4149. wp_enqueue_style( $handle );
  4150. }
  4151. return;
  4152. }
  4153. $stylesheet_link = sprintf(
  4154. "<link rel='stylesheet' href='%s' type='text/css' />\n",
  4155. esc_url( wp_admin_css_uri( $file ) )
  4156. );
  4157. /**
  4158. * Filters the stylesheet link to the specified CSS file.
  4159. *
  4160. * If the site is set to display right-to-left, the RTL stylesheet link
  4161. * will be used instead.
  4162. *
  4163. * @since 2.3.0
  4164. * @param string $stylesheet_link HTML link element for the stylesheet.
  4165. * @param string $file Style handle name or filename (without ".css" extension)
  4166. * relative to wp-admin/. Defaults to 'wp-admin'.
  4167. */
  4168. echo apply_filters( 'wp_admin_css', $stylesheet_link, $file );
  4169. if ( function_exists( 'is_rtl' ) && is_rtl() ) {
  4170. $rtl_stylesheet_link = sprintf(
  4171. "<link rel='stylesheet' href='%s' type='text/css' />\n",
  4172. esc_url( wp_admin_css_uri( "$file-rtl" ) )
  4173. );
  4174. /** This filter is documented in wp-includes/general-template.php */
  4175. echo apply_filters( 'wp_admin_css', $rtl_stylesheet_link, "$file-rtl" );
  4176. }
  4177. }
  4178. /**
  4179. * Enqueues the default ThickBox js and css.
  4180. *
  4181. * If any of the settings need to be changed, this can be done with another js
  4182. * file similar to media-upload.js. That file should
  4183. * require array('thickbox') to ensure it is loaded after.
  4184. *
  4185. * @since 2.5.0
  4186. */
  4187. function add_thickbox() {
  4188. wp_enqueue_script( 'thickbox' );
  4189. wp_enqueue_style( 'thickbox' );
  4190. if ( is_network_admin() ) {
  4191. add_action( 'admin_head', '_thickbox_path_admin_subfolder' );
  4192. }
  4193. }
  4194. /**
  4195. * Displays the XHTML generator that is generated on the wp_head hook.
  4196. *
  4197. * See {@see 'wp_head'}.
  4198. *
  4199. * @since 2.5.0
  4200. */
  4201. function wp_generator() {
  4202. /**
  4203. * Filters the output of the XHTML generator tag.
  4204. *
  4205. * @since 2.5.0
  4206. *
  4207. * @param string $generator_type The XHTML generator.
  4208. */
  4209. the_generator( apply_filters( 'wp_generator_type', 'xhtml' ) );
  4210. }
  4211. /**
  4212. * Display the generator XML or Comment for RSS, ATOM, etc.
  4213. *
  4214. * Returns the correct generator type for the requested output format. Allows
  4215. * for a plugin to filter generators overall the {@see 'the_generator'} filter.
  4216. *
  4217. * @since 2.5.0
  4218. *
  4219. * @param string $type The type of generator to output - (html|xhtml|atom|rss2|rdf|comment|export).
  4220. */
  4221. function the_generator( $type ) {
  4222. /**
  4223. * Filters the output of the XHTML generator tag for display.
  4224. *
  4225. * @since 2.5.0
  4226. *
  4227. * @param string $generator_type The generator output.
  4228. * @param string $type The type of generator to output. Accepts 'html',
  4229. * 'xhtml', 'atom', 'rss2', 'rdf', 'comment', 'export'.
  4230. */
  4231. echo apply_filters( 'the_generator', get_the_generator( $type ), $type ) . "\n";
  4232. }
  4233. /**
  4234. * Creates the generator XML or Comment for RSS, ATOM, etc.
  4235. *
  4236. * Returns the correct generator type for the requested output format. Allows
  4237. * for a plugin to filter generators on an individual basis using the
  4238. * {@see 'get_the_generator_$type'} filter.
  4239. *
  4240. * @since 2.5.0
  4241. *
  4242. * @param string $type The type of generator to return - (html|xhtml|atom|rss2|rdf|comment|export).
  4243. * @return string|void The HTML content for the generator.
  4244. */
  4245. function get_the_generator( $type = '' ) {
  4246. if ( empty( $type ) ) {
  4247. $current_filter = current_filter();
  4248. if ( empty( $current_filter ) ) {
  4249. return;
  4250. }
  4251. switch ( $current_filter ) {
  4252. case 'rss2_head':
  4253. case 'commentsrss2_head':
  4254. $type = 'rss2';
  4255. break;
  4256. case 'rss_head':
  4257. case 'opml_head':
  4258. $type = 'comment';
  4259. break;
  4260. case 'rdf_header':
  4261. $type = 'rdf';
  4262. break;
  4263. case 'atom_head':
  4264. case 'comments_atom_head':
  4265. case 'app_head':
  4266. $type = 'atom';
  4267. break;
  4268. }
  4269. }
  4270. switch ( $type ) {
  4271. case 'html':
  4272. $gen = '<meta name="generator" content="WordPress ' . esc_attr( get_bloginfo( 'version' ) ) . '">';
  4273. break;
  4274. case 'xhtml':
  4275. $gen = '<meta name="generator" content="WordPress ' . esc_attr( get_bloginfo( 'version' ) ) . '" />';
  4276. break;
  4277. case 'atom':
  4278. $gen = '<generator uri="https://wordpress.org/" version="' . esc_attr( get_bloginfo_rss( 'version' ) ) . '">WordPress</generator>';
  4279. break;
  4280. case 'rss2':
  4281. $gen = '<generator>' . esc_url_raw( 'https://wordpress.org/?v=' . get_bloginfo_rss( 'version' ) ) . '</generator>';
  4282. break;
  4283. case 'rdf':
  4284. $gen = '<admin:generatorAgent rdf:resource="' . esc_url_raw( 'https://wordpress.org/?v=' . get_bloginfo_rss( 'version' ) ) . '" />';
  4285. break;
  4286. case 'comment':
  4287. $gen = '<!-- generator="WordPress/' . esc_attr( get_bloginfo( 'version' ) ) . '" -->';
  4288. break;
  4289. case 'export':
  4290. $gen = '<!-- generator="WordPress/' . esc_attr( get_bloginfo_rss( 'version' ) ) . '" created="' . gmdate( 'Y-m-d H:i' ) . '" -->';
  4291. break;
  4292. }
  4293. /**
  4294. * Filters the HTML for the retrieved generator type.
  4295. *
  4296. * The dynamic portion of the hook name, `$type`, refers to the generator type.
  4297. *
  4298. * Possible hook names include:
  4299. *
  4300. * - `get_the_generator_atom`
  4301. * - `get_the_generator_comment`
  4302. * - `get_the_generator_export`
  4303. * - `get_the_generator_html`
  4304. * - `get_the_generator_rdf`
  4305. * - `get_the_generator_rss2`
  4306. * - `get_the_generator_xhtml`
  4307. *
  4308. * @since 2.5.0
  4309. *
  4310. * @param string $gen The HTML markup output to wp_head().
  4311. * @param string $type The type of generator. Accepts 'html', 'xhtml', 'atom',
  4312. * 'rss2', 'rdf', 'comment', 'export'.
  4313. */
  4314. return apply_filters( "get_the_generator_{$type}", $gen, $type );
  4315. }
  4316. /**
  4317. * Outputs the HTML checked attribute.
  4318. *
  4319. * Compares the first two arguments and if identical marks as checked
  4320. *
  4321. * @since 1.0.0
  4322. *
  4323. * @param mixed $checked One of the values to compare
  4324. * @param mixed $current (true) The other value to compare if not just true
  4325. * @param bool $echo Whether to echo or just return the string
  4326. * @return string HTML attribute or empty string
  4327. */
  4328. function checked( $checked, $current = true, $echo = true ) {
  4329. return __checked_selected_helper( $checked, $current, $echo, 'checked' );
  4330. }
  4331. /**
  4332. * Outputs the HTML selected attribute.
  4333. *
  4334. * Compares the first two arguments and if identical marks as selected
  4335. *
  4336. * @since 1.0.0
  4337. *
  4338. * @param mixed $selected One of the values to compare
  4339. * @param mixed $current (true) The other value to compare if not just true
  4340. * @param bool $echo Whether to echo or just return the string
  4341. * @return string HTML attribute or empty string
  4342. */
  4343. function selected( $selected, $current = true, $echo = true ) {
  4344. return __checked_selected_helper( $selected, $current, $echo, 'selected' );
  4345. }
  4346. /**
  4347. * Outputs the HTML disabled attribute.
  4348. *
  4349. * Compares the first two arguments and if identical marks as disabled
  4350. *
  4351. * @since 3.0.0
  4352. *
  4353. * @param mixed $disabled One of the values to compare
  4354. * @param mixed $current (true) The other value to compare if not just true
  4355. * @param bool $echo Whether to echo or just return the string
  4356. * @return string HTML attribute or empty string
  4357. */
  4358. function disabled( $disabled, $current = true, $echo = true ) {
  4359. return __checked_selected_helper( $disabled, $current, $echo, 'disabled' );
  4360. }
  4361. /**
  4362. * Outputs the HTML readonly attribute.
  4363. *
  4364. * Compares the first two arguments and if identical marks as readonly
  4365. *
  4366. * @since 4.9.0
  4367. *
  4368. * @param mixed $readonly One of the values to compare
  4369. * @param mixed $current (true) The other value to compare if not just true
  4370. * @param bool $echo Whether to echo or just return the string
  4371. * @return string HTML attribute or empty string
  4372. */
  4373. function readonly( $readonly, $current = true, $echo = true ) {
  4374. return __checked_selected_helper( $readonly, $current, $echo, 'readonly' );
  4375. }
  4376. /**
  4377. * Private helper function for checked, selected, disabled and readonly.
  4378. *
  4379. * Compares the first two arguments and if identical marks as $type
  4380. *
  4381. * @since 2.8.0
  4382. * @access private
  4383. *
  4384. * @param mixed $helper One of the values to compare
  4385. * @param mixed $current (true) The other value to compare if not just true
  4386. * @param bool $echo Whether to echo or just return the string
  4387. * @param string $type The type of checked|selected|disabled|readonly we are doing
  4388. * @return string HTML attribute or empty string
  4389. */
  4390. function __checked_selected_helper( $helper, $current, $echo, $type ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
  4391. if ( (string) $helper === (string) $current ) {
  4392. $result = " $type='$type'";
  4393. } else {
  4394. $result = '';
  4395. }
  4396. if ( $echo ) {
  4397. echo $result;
  4398. }
  4399. return $result;
  4400. }
  4401. /**
  4402. * Default settings for heartbeat
  4403. *
  4404. * Outputs the nonce used in the heartbeat XHR
  4405. *
  4406. * @since 3.6.0
  4407. *
  4408. * @param array $settings
  4409. * @return array Heartbeat settings.
  4410. */
  4411. function wp_heartbeat_settings( $settings ) {
  4412. if ( ! is_admin() ) {
  4413. $settings['ajaxurl'] = admin_url( 'admin-ajax.php', 'relative' );
  4414. }
  4415. if ( is_user_logged_in() ) {
  4416. $settings['nonce'] = wp_create_nonce( 'heartbeat-nonce' );
  4417. }
  4418. return $settings;
  4419. }