Geen omschrijving

general-template.php 156KB

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