Nessuna descrizione

link-template.php 145KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609
  1. <?php
  2. /**
  3. * WordPress Link Template Functions
  4. *
  5. * @package WordPress
  6. * @subpackage Template
  7. */
  8. /**
  9. * Displays the permalink for the current post.
  10. *
  11. * @since 1.2.0
  12. * @since 4.4.0 Added the `$post` parameter.
  13. *
  14. * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
  15. */
  16. function the_permalink( $post = 0 ) {
  17. /**
  18. * Filters the display of the permalink for the current post.
  19. *
  20. * @since 1.5.0
  21. * @since 4.4.0 Added the `$post` parameter.
  22. *
  23. * @param string $permalink The permalink for the current post.
  24. * @param int|WP_Post $post Post ID, WP_Post object, or 0. Default 0.
  25. */
  26. echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
  27. }
  28. /**
  29. * Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
  30. *
  31. * Conditionally adds a trailing slash if the permalink structure has a trailing
  32. * slash, strips the trailing slash if not. The string is passed through the
  33. * {@see 'user_trailingslashit'} filter. Will remove trailing slash from string, if
  34. * site is not set to have them.
  35. *
  36. * @since 2.2.0
  37. *
  38. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  39. *
  40. * @param string $string URL with or without a trailing slash.
  41. * @param string $type_of_url Optional. The type of URL being considered (e.g. single, category, etc)
  42. * for use in the filter. Default empty string.
  43. * @return string The URL with the trailing slash appended or stripped.
  44. */
  45. function user_trailingslashit( $string, $type_of_url = '' ) {
  46. global $wp_rewrite;
  47. if ( $wp_rewrite->use_trailing_slashes ) {
  48. $string = trailingslashit( $string );
  49. } else {
  50. $string = untrailingslashit( $string );
  51. }
  52. /**
  53. * Filters the trailing-slashed string, depending on whether the site is set to use trailing slashes.
  54. *
  55. * @since 2.2.0
  56. *
  57. * @param string $string URL with or without a trailing slash.
  58. * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
  59. * 'single_feed', 'single_paged', 'commentpaged', 'paged', 'home', 'feed',
  60. * 'category', 'page', 'year', 'month', 'day', 'post_type_archive'.
  61. */
  62. return apply_filters( 'user_trailingslashit', $string, $type_of_url );
  63. }
  64. /**
  65. * Displays the permalink anchor for the current post.
  66. *
  67. * The permalink mode title will use the post title for the 'a' element 'id'
  68. * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
  69. *
  70. * @since 0.71
  71. *
  72. * @param string $mode Optional. Permalink mode. Accepts 'title' or 'id'. Default 'id'.
  73. */
  74. function permalink_anchor( $mode = 'id' ) {
  75. $post = get_post();
  76. switch ( strtolower( $mode ) ) {
  77. case 'title':
  78. $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
  79. echo '<a id="' . $title . '"></a>';
  80. break;
  81. case 'id':
  82. default:
  83. echo '<a id="post-' . $post->ID . '"></a>';
  84. break;
  85. }
  86. }
  87. /**
  88. * Determine whether post should always use a plain permalink structure.
  89. *
  90. * @since 5.7.0
  91. *
  92. * @param WP_Post|int|null $post Optional. Post ID or post object. Defaults to global $post.
  93. * @param bool|null $sample Optional. Whether to force consideration based on sample links.
  94. * If omitted, a sample link is generated if a post object is passed
  95. * with the filter property set to 'sample'.
  96. * @return bool Whether to use a plain permalink structure.
  97. */
  98. function wp_force_plain_post_permalink( $post = null, $sample = null ) {
  99. if (
  100. null === $sample &&
  101. is_object( $post ) &&
  102. isset( $post->filter ) &&
  103. 'sample' === $post->filter
  104. ) {
  105. $sample = true;
  106. } else {
  107. $post = get_post( $post );
  108. $sample = null !== $sample ? $sample : false;
  109. }
  110. if ( ! $post ) {
  111. return true;
  112. }
  113. $post_status_obj = get_post_status_object( get_post_status( $post ) );
  114. $post_type_obj = get_post_type_object( get_post_type( $post ) );
  115. if ( ! $post_status_obj || ! $post_type_obj ) {
  116. return true;
  117. }
  118. if (
  119. // Publicly viewable links never have plain permalinks.
  120. is_post_status_viewable( $post_status_obj ) ||
  121. (
  122. // Private posts don't have plain permalinks if the user can read them.
  123. $post_status_obj->private &&
  124. current_user_can( 'read_post', $post->ID )
  125. ) ||
  126. // Protected posts don't have plain links if getting a sample URL.
  127. ( $post_status_obj->protected && $sample )
  128. ) {
  129. return false;
  130. }
  131. return true;
  132. }
  133. /**
  134. * Retrieves the full permalink for the current post or post ID.
  135. *
  136. * This function is an alias for get_permalink().
  137. *
  138. * @since 3.9.0
  139. *
  140. * @see get_permalink()
  141. *
  142. * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
  143. * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
  144. * @return string|false The permalink URL or false if post does not exist.
  145. */
  146. function get_the_permalink( $post = 0, $leavename = false ) {
  147. return get_permalink( $post, $leavename );
  148. }
  149. /**
  150. * Retrieves the full permalink for the current post or post ID.
  151. *
  152. * @since 1.0.0
  153. *
  154. * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
  155. * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
  156. * @return string|false The permalink URL or false if post does not exist.
  157. */
  158. function get_permalink( $post = 0, $leavename = false ) {
  159. $rewritecode = array(
  160. '%year%',
  161. '%monthnum%',
  162. '%day%',
  163. '%hour%',
  164. '%minute%',
  165. '%second%',
  166. $leavename ? '' : '%postname%',
  167. '%post_id%',
  168. '%category%',
  169. '%author%',
  170. $leavename ? '' : '%pagename%',
  171. );
  172. if ( is_object( $post ) && isset( $post->filter ) && 'sample' === $post->filter ) {
  173. $sample = true;
  174. } else {
  175. $post = get_post( $post );
  176. $sample = false;
  177. }
  178. if ( empty( $post->ID ) ) {
  179. return false;
  180. }
  181. if ( 'page' === $post->post_type ) {
  182. return get_page_link( $post, $leavename, $sample );
  183. } elseif ( 'attachment' === $post->post_type ) {
  184. return get_attachment_link( $post, $leavename );
  185. } elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ), true ) ) {
  186. return get_post_permalink( $post, $leavename, $sample );
  187. }
  188. $permalink = get_option( 'permalink_structure' );
  189. /**
  190. * Filters the permalink structure for a post before token replacement occurs.
  191. *
  192. * Only applies to posts with post_type of 'post'.
  193. *
  194. * @since 3.0.0
  195. *
  196. * @param string $permalink The site's permalink structure.
  197. * @param WP_Post $post The post in question.
  198. * @param bool $leavename Whether to keep the post name.
  199. */
  200. $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
  201. if (
  202. $permalink &&
  203. ! wp_force_plain_post_permalink( $post )
  204. ) {
  205. $category = '';
  206. if ( strpos( $permalink, '%category%' ) !== false ) {
  207. $cats = get_the_category( $post->ID );
  208. if ( $cats ) {
  209. $cats = wp_list_sort(
  210. $cats,
  211. array(
  212. 'term_id' => 'ASC',
  213. )
  214. );
  215. /**
  216. * Filters the category that gets used in the %category% permalink token.
  217. *
  218. * @since 3.5.0
  219. *
  220. * @param WP_Term $cat The category to use in the permalink.
  221. * @param array $cats Array of all categories (WP_Term objects) associated with the post.
  222. * @param WP_Post $post The post in question.
  223. */
  224. $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
  225. $category_object = get_term( $category_object, 'category' );
  226. $category = $category_object->slug;
  227. if ( $category_object->parent ) {
  228. $category = get_category_parents( $category_object->parent, false, '/', true ) . $category;
  229. }
  230. }
  231. // Show default category in permalinks,
  232. // without having to assign it explicitly.
  233. if ( empty( $category ) ) {
  234. $default_category = get_term( get_option( 'default_category' ), 'category' );
  235. if ( $default_category && ! is_wp_error( $default_category ) ) {
  236. $category = $default_category->slug;
  237. }
  238. }
  239. }
  240. $author = '';
  241. if ( strpos( $permalink, '%author%' ) !== false ) {
  242. $authordata = get_userdata( $post->post_author );
  243. $author = $authordata->user_nicename;
  244. }
  245. // This is not an API call because the permalink is based on the stored post_date value,
  246. // which should be parsed as local time regardless of the default PHP timezone.
  247. $date = explode( ' ', str_replace( array( '-', ':' ), ' ', $post->post_date ) );
  248. $rewritereplace = array(
  249. $date[0],
  250. $date[1],
  251. $date[2],
  252. $date[3],
  253. $date[4],
  254. $date[5],
  255. $post->post_name,
  256. $post->ID,
  257. $category,
  258. $author,
  259. $post->post_name,
  260. );
  261. $permalink = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) );
  262. $permalink = user_trailingslashit( $permalink, 'single' );
  263. } else { // If they're not using the fancy permalink option.
  264. $permalink = home_url( '?p=' . $post->ID );
  265. }
  266. /**
  267. * Filters the permalink for a post.
  268. *
  269. * Only applies to posts with post_type of 'post'.
  270. *
  271. * @since 1.5.0
  272. *
  273. * @param string $permalink The post's permalink.
  274. * @param WP_Post $post The post in question.
  275. * @param bool $leavename Whether to keep the post name.
  276. */
  277. return apply_filters( 'post_link', $permalink, $post, $leavename );
  278. }
  279. /**
  280. * Retrieves the permalink for a post of a custom post type.
  281. *
  282. * @since 3.0.0
  283. *
  284. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  285. *
  286. * @param int|WP_Post $id Optional. Post ID or post object. Default is the global `$post`.
  287. * @param bool $leavename Optional. Whether to keep post name. Default false.
  288. * @param bool $sample Optional. Is it a sample permalink. Default false.
  289. * @return string|WP_Error The post permalink.
  290. */
  291. function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
  292. global $wp_rewrite;
  293. $post = get_post( $id );
  294. if ( is_wp_error( $post ) ) {
  295. return $post;
  296. }
  297. $post_link = $wp_rewrite->get_extra_permastruct( $post->post_type );
  298. $slug = $post->post_name;
  299. $force_plain_link = wp_force_plain_post_permalink( $post );
  300. $post_type = get_post_type_object( $post->post_type );
  301. if ( $post_type->hierarchical ) {
  302. $slug = get_page_uri( $post );
  303. }
  304. if ( ! empty( $post_link ) && ( ! $force_plain_link || $sample ) ) {
  305. if ( ! $leavename ) {
  306. $post_link = str_replace( "%$post->post_type%", $slug, $post_link );
  307. }
  308. $post_link = home_url( user_trailingslashit( $post_link ) );
  309. } else {
  310. if ( $post_type->query_var && ( isset( $post->post_status ) && ! $force_plain_link ) ) {
  311. $post_link = add_query_arg( $post_type->query_var, $slug, '' );
  312. } else {
  313. $post_link = add_query_arg(
  314. array(
  315. 'post_type' => $post->post_type,
  316. 'p' => $post->ID,
  317. ),
  318. ''
  319. );
  320. }
  321. $post_link = home_url( $post_link );
  322. }
  323. /**
  324. * Filters the permalink for a post of a custom post type.
  325. *
  326. * @since 3.0.0
  327. *
  328. * @param string $post_link The post's permalink.
  329. * @param WP_Post $post The post in question.
  330. * @param bool $leavename Whether to keep the post name.
  331. * @param bool $sample Is it a sample permalink.
  332. */
  333. return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
  334. }
  335. /**
  336. * Retrieves the permalink for the current page or page ID.
  337. *
  338. * Respects page_on_front. Use this one.
  339. *
  340. * @since 1.5.0
  341. *
  342. * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`.
  343. * @param bool $leavename Optional. Whether to keep the page name. Default false.
  344. * @param bool $sample Optional. Whether it should be treated as a sample permalink.
  345. * Default false.
  346. * @return string The page permalink.
  347. */
  348. function get_page_link( $post = false, $leavename = false, $sample = false ) {
  349. $post = get_post( $post );
  350. if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID ) {
  351. $link = home_url( '/' );
  352. } else {
  353. $link = _get_page_link( $post, $leavename, $sample );
  354. }
  355. /**
  356. * Filters the permalink for a page.
  357. *
  358. * @since 1.5.0
  359. *
  360. * @param string $link The page's permalink.
  361. * @param int $post_id The ID of the page.
  362. * @param bool $sample Is it a sample permalink.
  363. */
  364. return apply_filters( 'page_link', $link, $post->ID, $sample );
  365. }
  366. /**
  367. * Retrieves the page permalink.
  368. *
  369. * Ignores page_on_front. Internal use only.
  370. *
  371. * @since 2.1.0
  372. * @access private
  373. *
  374. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  375. *
  376. * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`.
  377. * @param bool $leavename Optional. Whether to keep the page name. Default false.
  378. * @param bool $sample Optional. Whether it should be treated as a sample permalink.
  379. * Default false.
  380. * @return string The page permalink.
  381. */
  382. function _get_page_link( $post = false, $leavename = false, $sample = false ) {
  383. global $wp_rewrite;
  384. $post = get_post( $post );
  385. $force_plain_link = wp_force_plain_post_permalink( $post );
  386. $link = $wp_rewrite->get_page_permastruct();
  387. if ( ! empty( $link ) && ( ( isset( $post->post_status ) && ! $force_plain_link ) || $sample ) ) {
  388. if ( ! $leavename ) {
  389. $link = str_replace( '%pagename%', get_page_uri( $post ), $link );
  390. }
  391. $link = home_url( $link );
  392. $link = user_trailingslashit( $link, 'page' );
  393. } else {
  394. $link = home_url( '?page_id=' . $post->ID );
  395. }
  396. /**
  397. * Filters the permalink for a non-page_on_front page.
  398. *
  399. * @since 2.1.0
  400. *
  401. * @param string $link The page's permalink.
  402. * @param int $post_id The ID of the page.
  403. */
  404. return apply_filters( '_get_page_link', $link, $post->ID );
  405. }
  406. /**
  407. * Retrieves the permalink for an attachment.
  408. *
  409. * This can be used in the WordPress Loop or outside of it.
  410. *
  411. * @since 2.0.0
  412. *
  413. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  414. *
  415. * @param int|object $post Optional. Post ID or object. Default uses the global `$post`.
  416. * @param bool $leavename Optional. Whether to keep the page name. Default false.
  417. * @return string The attachment permalink.
  418. */
  419. function get_attachment_link( $post = null, $leavename = false ) {
  420. global $wp_rewrite;
  421. $link = false;
  422. $post = get_post( $post );
  423. $force_plain_link = wp_force_plain_post_permalink( $post );
  424. $parent_id = $post->post_parent;
  425. $parent = $parent_id ? get_post( $parent_id ) : false;
  426. $parent_valid = true; // Default for no parent.
  427. if (
  428. $parent_id &&
  429. (
  430. $post->post_parent === $post->ID ||
  431. ! $parent ||
  432. ! is_post_type_viewable( get_post_type( $parent ) )
  433. )
  434. ) {
  435. // Post is either its own parent or parent post unavailable.
  436. $parent_valid = false;
  437. }
  438. if ( $force_plain_link || ! $parent_valid ) {
  439. $link = false;
  440. } elseif ( $wp_rewrite->using_permalinks() && $parent ) {
  441. if ( 'page' === $parent->post_type ) {
  442. $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front.
  443. } else {
  444. $parentlink = get_permalink( $post->post_parent );
  445. }
  446. if ( is_numeric( $post->post_name ) || false !== strpos( get_option( 'permalink_structure' ), '%category%' ) ) {
  447. $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker.
  448. } else {
  449. $name = $post->post_name;
  450. }
  451. if ( strpos( $parentlink, '?' ) === false ) {
  452. $link = user_trailingslashit( trailingslashit( $parentlink ) . '%postname%' );
  453. }
  454. if ( ! $leavename ) {
  455. $link = str_replace( '%postname%', $name, $link );
  456. }
  457. } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) {
  458. $link = home_url( user_trailingslashit( $post->post_name ) );
  459. }
  460. if ( ! $link ) {
  461. $link = home_url( '/?attachment_id=' . $post->ID );
  462. }
  463. /**
  464. * Filters the permalink for an attachment.
  465. *
  466. * @since 2.0.0
  467. * @since 5.6.0 Providing an empty string will now disable
  468. * the view attachment page link on the media modal.
  469. *
  470. * @param string $link The attachment's permalink.
  471. * @param int $post_id Attachment ID.
  472. */
  473. return apply_filters( 'attachment_link', $link, $post->ID );
  474. }
  475. /**
  476. * Retrieves the permalink for the year archives.
  477. *
  478. * @since 1.5.0
  479. *
  480. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  481. *
  482. * @param int|false $year Integer of year. False for current year.
  483. * @return string The permalink for the specified year archive.
  484. */
  485. function get_year_link( $year ) {
  486. global $wp_rewrite;
  487. if ( ! $year ) {
  488. $year = current_time( 'Y' );
  489. }
  490. $yearlink = $wp_rewrite->get_year_permastruct();
  491. if ( ! empty( $yearlink ) ) {
  492. $yearlink = str_replace( '%year%', $year, $yearlink );
  493. $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
  494. } else {
  495. $yearlink = home_url( '?m=' . $year );
  496. }
  497. /**
  498. * Filters the year archive permalink.
  499. *
  500. * @since 1.5.0
  501. *
  502. * @param string $yearlink Permalink for the year archive.
  503. * @param int $year Year for the archive.
  504. */
  505. return apply_filters( 'year_link', $yearlink, $year );
  506. }
  507. /**
  508. * Retrieves the permalink for the month archives with year.
  509. *
  510. * @since 1.0.0
  511. *
  512. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  513. *
  514. * @param int|false $year Integer of year. False for current year.
  515. * @param int|false $month Integer of month. False for current month.
  516. * @return string The permalink for the specified month and year archive.
  517. */
  518. function get_month_link( $year, $month ) {
  519. global $wp_rewrite;
  520. if ( ! $year ) {
  521. $year = current_time( 'Y' );
  522. }
  523. if ( ! $month ) {
  524. $month = current_time( 'm' );
  525. }
  526. $monthlink = $wp_rewrite->get_month_permastruct();
  527. if ( ! empty( $monthlink ) ) {
  528. $monthlink = str_replace( '%year%', $year, $monthlink );
  529. $monthlink = str_replace( '%monthnum%', zeroise( (int) $month, 2 ), $monthlink );
  530. $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
  531. } else {
  532. $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
  533. }
  534. /**
  535. * Filters the month archive permalink.
  536. *
  537. * @since 1.5.0
  538. *
  539. * @param string $monthlink Permalink for the month archive.
  540. * @param int $year Year for the archive.
  541. * @param int $month The month for the archive.
  542. */
  543. return apply_filters( 'month_link', $monthlink, $year, $month );
  544. }
  545. /**
  546. * Retrieves the permalink for the day archives with year and month.
  547. *
  548. * @since 1.0.0
  549. *
  550. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  551. *
  552. * @param int|false $year Integer of year. False for current year.
  553. * @param int|false $month Integer of month. False for current month.
  554. * @param int|false $day Integer of day. False for current day.
  555. * @return string The permalink for the specified day, month, and year archive.
  556. */
  557. function get_day_link( $year, $month, $day ) {
  558. global $wp_rewrite;
  559. if ( ! $year ) {
  560. $year = current_time( 'Y' );
  561. }
  562. if ( ! $month ) {
  563. $month = current_time( 'm' );
  564. }
  565. if ( ! $day ) {
  566. $day = current_time( 'j' );
  567. }
  568. $daylink = $wp_rewrite->get_day_permastruct();
  569. if ( ! empty( $daylink ) ) {
  570. $daylink = str_replace( '%year%', $year, $daylink );
  571. $daylink = str_replace( '%monthnum%', zeroise( (int) $month, 2 ), $daylink );
  572. $daylink = str_replace( '%day%', zeroise( (int) $day, 2 ), $daylink );
  573. $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
  574. } else {
  575. $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
  576. }
  577. /**
  578. * Filters the day archive permalink.
  579. *
  580. * @since 1.5.0
  581. *
  582. * @param string $daylink Permalink for the day archive.
  583. * @param int $year Year for the archive.
  584. * @param int $month Month for the archive.
  585. * @param int $day The day for the archive.
  586. */
  587. return apply_filters( 'day_link', $daylink, $year, $month, $day );
  588. }
  589. /**
  590. * Displays the permalink for the feed type.
  591. *
  592. * @since 3.0.0
  593. *
  594. * @param string $anchor The link's anchor text.
  595. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  596. * Default is the value of get_default_feed().
  597. */
  598. function the_feed_link( $anchor, $feed = '' ) {
  599. $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
  600. /**
  601. * Filters the feed link anchor tag.
  602. *
  603. * @since 3.0.0
  604. *
  605. * @param string $link The complete anchor tag for a feed link.
  606. * @param string $feed The feed type. Possible values include 'rss2', 'atom',
  607. * or an empty string for the default feed type.
  608. */
  609. echo apply_filters( 'the_feed_link', $link, $feed );
  610. }
  611. /**
  612. * Retrieves the permalink for the feed type.
  613. *
  614. * @since 1.5.0
  615. *
  616. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  617. *
  618. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  619. * Default is the value of get_default_feed().
  620. * @return string The feed permalink.
  621. */
  622. function get_feed_link( $feed = '' ) {
  623. global $wp_rewrite;
  624. $permalink = $wp_rewrite->get_feed_permastruct();
  625. if ( $permalink ) {
  626. if ( false !== strpos( $feed, 'comments_' ) ) {
  627. $feed = str_replace( 'comments_', '', $feed );
  628. $permalink = $wp_rewrite->get_comment_feed_permastruct();
  629. }
  630. if ( get_default_feed() == $feed ) {
  631. $feed = '';
  632. }
  633. $permalink = str_replace( '%feed%', $feed, $permalink );
  634. $permalink = preg_replace( '#/+#', '/', "/$permalink" );
  635. $output = home_url( user_trailingslashit( $permalink, 'feed' ) );
  636. } else {
  637. if ( empty( $feed ) ) {
  638. $feed = get_default_feed();
  639. }
  640. if ( false !== strpos( $feed, 'comments_' ) ) {
  641. $feed = str_replace( 'comments_', 'comments-', $feed );
  642. }
  643. $output = home_url( "?feed={$feed}" );
  644. }
  645. /**
  646. * Filters the feed type permalink.
  647. *
  648. * @since 1.5.0
  649. *
  650. * @param string $output The feed permalink.
  651. * @param string $feed The feed type. Possible values include 'rss2', 'atom',
  652. * or an empty string for the default feed type.
  653. */
  654. return apply_filters( 'feed_link', $output, $feed );
  655. }
  656. /**
  657. * Retrieves the permalink for the post comments feed.
  658. *
  659. * @since 2.2.0
  660. *
  661. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
  662. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  663. * Default is the value of get_default_feed().
  664. * @return string The permalink for the comments feed for the given post on success, empty string on failure.
  665. */
  666. function get_post_comments_feed_link( $post_id = 0, $feed = '' ) {
  667. $post_id = absint( $post_id );
  668. if ( ! $post_id ) {
  669. $post_id = get_the_ID();
  670. }
  671. if ( empty( $feed ) ) {
  672. $feed = get_default_feed();
  673. }
  674. $post = get_post( $post_id );
  675. // Bail out if the post does not exist.
  676. if ( ! $post instanceof WP_Post ) {
  677. return '';
  678. }
  679. $unattached = 'attachment' === $post->post_type && 0 === (int) $post->post_parent;
  680. if ( get_option( 'permalink_structure' ) ) {
  681. if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post_id ) {
  682. $url = _get_page_link( $post_id );
  683. } else {
  684. $url = get_permalink( $post_id );
  685. }
  686. if ( $unattached ) {
  687. $url = home_url( '/feed/' );
  688. if ( get_default_feed() !== $feed ) {
  689. $url .= "$feed/";
  690. }
  691. $url = add_query_arg( 'attachment_id', $post_id, $url );
  692. } else {
  693. $url = trailingslashit( $url ) . 'feed';
  694. if ( get_default_feed() != $feed ) {
  695. $url .= "/$feed";
  696. }
  697. $url = user_trailingslashit( $url, 'single_feed' );
  698. }
  699. } else {
  700. if ( $unattached ) {
  701. $url = add_query_arg(
  702. array(
  703. 'feed' => $feed,
  704. 'attachment_id' => $post_id,
  705. ),
  706. home_url( '/' )
  707. );
  708. } elseif ( 'page' === $post->post_type ) {
  709. $url = add_query_arg(
  710. array(
  711. 'feed' => $feed,
  712. 'page_id' => $post_id,
  713. ),
  714. home_url( '/' )
  715. );
  716. } else {
  717. $url = add_query_arg(
  718. array(
  719. 'feed' => $feed,
  720. 'p' => $post_id,
  721. ),
  722. home_url( '/' )
  723. );
  724. }
  725. }
  726. /**
  727. * Filters the post comments feed permalink.
  728. *
  729. * @since 1.5.1
  730. *
  731. * @param string $url Post comments feed permalink.
  732. */
  733. return apply_filters( 'post_comments_feed_link', $url );
  734. }
  735. /**
  736. * Displays the comment feed link for a post.
  737. *
  738. * Prints out the comment feed link for a post. Link text is placed in the
  739. * anchor. If no link text is specified, default text is used. If no post ID is
  740. * specified, the current post is used.
  741. *
  742. * @since 2.5.0
  743. *
  744. * @param string $link_text Optional. Descriptive link text. Default 'Comments Feed'.
  745. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
  746. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  747. * Default is the value of get_default_feed().
  748. */
  749. function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
  750. $url = get_post_comments_feed_link( $post_id, $feed );
  751. if ( empty( $link_text ) ) {
  752. $link_text = __( 'Comments Feed' );
  753. }
  754. $link = '<a href="' . esc_url( $url ) . '">' . $link_text . '</a>';
  755. /**
  756. * Filters the post comment feed link anchor tag.
  757. *
  758. * @since 2.8.0
  759. *
  760. * @param string $link The complete anchor tag for the comment feed link.
  761. * @param int $post_id Post ID.
  762. * @param string $feed The feed type. Possible values include 'rss2', 'atom',
  763. * or an empty string for the default feed type.
  764. */
  765. echo apply_filters( 'post_comments_feed_link_html', $link, $post_id, $feed );
  766. }
  767. /**
  768. * Retrieves the feed link for a given author.
  769. *
  770. * Returns a link to the feed for all posts by a given author. A specific feed
  771. * can be requested or left blank to get the default feed.
  772. *
  773. * @since 2.5.0
  774. *
  775. * @param int $author_id Author ID.
  776. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  777. * Default is the value of get_default_feed().
  778. * @return string Link to the feed for the author specified by $author_id.
  779. */
  780. function get_author_feed_link( $author_id, $feed = '' ) {
  781. $author_id = (int) $author_id;
  782. $permalink_structure = get_option( 'permalink_structure' );
  783. if ( empty( $feed ) ) {
  784. $feed = get_default_feed();
  785. }
  786. if ( ! $permalink_structure ) {
  787. $link = home_url( "?feed=$feed&amp;author=" . $author_id );
  788. } else {
  789. $link = get_author_posts_url( $author_id );
  790. if ( get_default_feed() == $feed ) {
  791. $feed_link = 'feed';
  792. } else {
  793. $feed_link = "feed/$feed";
  794. }
  795. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
  796. }
  797. /**
  798. * Filters the feed link for a given author.
  799. *
  800. * @since 1.5.1
  801. *
  802. * @param string $link The author feed link.
  803. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  804. */
  805. $link = apply_filters( 'author_feed_link', $link, $feed );
  806. return $link;
  807. }
  808. /**
  809. * Retrieves the feed link for a category.
  810. *
  811. * Returns a link to the feed for all posts in a given category. A specific feed
  812. * can be requested or left blank to get the default feed.
  813. *
  814. * @since 2.5.0
  815. *
  816. * @param int $cat_id Category ID.
  817. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  818. * Default is the value of get_default_feed().
  819. * @return string Link to the feed for the category specified by $cat_id.
  820. */
  821. function get_category_feed_link( $cat_id, $feed = '' ) {
  822. return get_term_feed_link( $cat_id, 'category', $feed );
  823. }
  824. /**
  825. * Retrieves the feed link for a term.
  826. *
  827. * Returns a link to the feed for all posts in a given term. A specific feed
  828. * can be requested or left blank to get the default feed.
  829. *
  830. * @since 3.0.0
  831. *
  832. * @param int $term_id Term ID.
  833. * @param string $taxonomy Optional. Taxonomy of `$term_id`. Default 'category'.
  834. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  835. * Default is the value of get_default_feed().
  836. * @return string|false Link to the feed for the term specified by $term_id and $taxonomy.
  837. */
  838. function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
  839. $term_id = (int) $term_id;
  840. $term = get_term( $term_id, $taxonomy );
  841. if ( empty( $term ) || is_wp_error( $term ) ) {
  842. return false;
  843. }
  844. if ( empty( $feed ) ) {
  845. $feed = get_default_feed();
  846. }
  847. $permalink_structure = get_option( 'permalink_structure' );
  848. if ( ! $permalink_structure ) {
  849. if ( 'category' === $taxonomy ) {
  850. $link = home_url( "?feed=$feed&amp;cat=$term_id" );
  851. } elseif ( 'post_tag' === $taxonomy ) {
  852. $link = home_url( "?feed=$feed&amp;tag=$term->slug" );
  853. } else {
  854. $t = get_taxonomy( $taxonomy );
  855. $link = home_url( "?feed=$feed&amp;$t->query_var=$term->slug" );
  856. }
  857. } else {
  858. $link = get_term_link( $term_id, $term->taxonomy );
  859. if ( get_default_feed() == $feed ) {
  860. $feed_link = 'feed';
  861. } else {
  862. $feed_link = "feed/$feed";
  863. }
  864. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
  865. }
  866. if ( 'category' === $taxonomy ) {
  867. /**
  868. * Filters the category feed link.
  869. *
  870. * @since 1.5.1
  871. *
  872. * @param string $link The category feed link.
  873. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  874. */
  875. $link = apply_filters( 'category_feed_link', $link, $feed );
  876. } elseif ( 'post_tag' === $taxonomy ) {
  877. /**
  878. * Filters the post tag feed link.
  879. *
  880. * @since 2.3.0
  881. *
  882. * @param string $link The tag feed link.
  883. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  884. */
  885. $link = apply_filters( 'tag_feed_link', $link, $feed );
  886. } else {
  887. /**
  888. * Filters the feed link for a taxonomy other than 'category' or 'post_tag'.
  889. *
  890. * @since 3.0.0
  891. *
  892. * @param string $link The taxonomy feed link.
  893. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  894. * @param string $taxonomy The taxonomy name.
  895. */
  896. $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
  897. }
  898. return $link;
  899. }
  900. /**
  901. * Retrieves the permalink for a tag feed.
  902. *
  903. * @since 2.3.0
  904. *
  905. * @param int $tag_id Tag ID.
  906. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  907. * Default is the value of get_default_feed().
  908. * @return string The feed permalink for the given tag.
  909. */
  910. function get_tag_feed_link( $tag_id, $feed = '' ) {
  911. return get_term_feed_link( $tag_id, 'post_tag', $feed );
  912. }
  913. /**
  914. * Retrieves the edit link for a tag.
  915. *
  916. * @since 2.7.0
  917. *
  918. * @param int $tag_id Tag ID.
  919. * @param string $taxonomy Optional. Taxonomy slug. Default 'post_tag'.
  920. * @return string The edit tag link URL for the given tag.
  921. */
  922. function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
  923. /**
  924. * Filters the edit link for a tag (or term in another taxonomy).
  925. *
  926. * @since 2.7.0
  927. *
  928. * @param string $link The term edit link.
  929. */
  930. return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
  931. }
  932. /**
  933. * Displays or retrieves the edit link for a tag with formatting.
  934. *
  935. * @since 2.7.0
  936. *
  937. * @param string $link Optional. Anchor text. If empty, default is 'Edit This'. Default empty.
  938. * @param string $before Optional. Display before edit link. Default empty.
  939. * @param string $after Optional. Display after edit link. Default empty.
  940. * @param WP_Term $tag Optional. Term object. If null, the queried object will be inspected.
  941. * Default null.
  942. */
  943. function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
  944. $link = edit_term_link( $link, '', '', $tag, false );
  945. /**
  946. * Filters the anchor tag for the edit link for a tag (or term in another taxonomy).
  947. *
  948. * @since 2.7.0
  949. *
  950. * @param string $link The anchor tag for the edit link.
  951. */
  952. echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
  953. }
  954. /**
  955. * Retrieves the URL for editing a given term.
  956. *
  957. * @since 3.1.0
  958. * @since 4.5.0 The `$taxonomy` parameter was made optional.
  959. *
  960. * @param int $term_id Term ID.
  961. * @param string $taxonomy Optional. Taxonomy. Defaults to the taxonomy of the term identified
  962. * by `$term_id`.
  963. * @param string $object_type Optional. The object type. Used to highlight the proper post type
  964. * menu on the linked page. Defaults to the first object_type associated
  965. * with the taxonomy.
  966. * @return string|null The edit term link URL for the given term, or null on failure.
  967. */
  968. function get_edit_term_link( $term_id, $taxonomy = '', $object_type = '' ) {
  969. $term = get_term( $term_id, $taxonomy );
  970. if ( ! $term || is_wp_error( $term ) ) {
  971. return;
  972. }
  973. $tax = get_taxonomy( $term->taxonomy );
  974. if ( ! $tax || ! current_user_can( 'edit_term', $term->term_id ) ) {
  975. return;
  976. }
  977. $args = array(
  978. 'taxonomy' => $taxonomy,
  979. 'tag_ID' => $term->term_id,
  980. );
  981. if ( $object_type ) {
  982. $args['post_type'] = $object_type;
  983. } elseif ( ! empty( $tax->object_type ) ) {
  984. $args['post_type'] = reset( $tax->object_type );
  985. }
  986. if ( $tax->show_ui ) {
  987. $location = add_query_arg( $args, admin_url( 'term.php' ) );
  988. } else {
  989. $location = '';
  990. }
  991. /**
  992. * Filters the edit link for a term.
  993. *
  994. * @since 3.1.0
  995. *
  996. * @param string $location The edit link.
  997. * @param int $term_id Term ID.
  998. * @param string $taxonomy Taxonomy name.
  999. * @param string $object_type The object type (eg. the post type).
  1000. */
  1001. return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
  1002. }
  1003. /**
  1004. * Displays or retrieves the edit term link with formatting.
  1005. *
  1006. * @since 3.1.0
  1007. *
  1008. * @param string $link Optional. Anchor text. If empty, default is 'Edit This'. Default empty.
  1009. * @param string $before Optional. Display before edit link. Default empty.
  1010. * @param string $after Optional. Display after edit link. Default empty.
  1011. * @param WP_Term $term Optional. Term object. If null, the queried object will be inspected. Default null.
  1012. * @param bool $echo Optional. Whether or not to echo the return. Default true.
  1013. * @return string|void HTML content.
  1014. */
  1015. function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
  1016. if ( is_null( $term ) ) {
  1017. $term = get_queried_object();
  1018. }
  1019. if ( ! $term ) {
  1020. return;
  1021. }
  1022. $tax = get_taxonomy( $term->taxonomy );
  1023. if ( ! current_user_can( 'edit_term', $term->term_id ) ) {
  1024. return;
  1025. }
  1026. if ( empty( $link ) ) {
  1027. $link = __( 'Edit This' );
  1028. }
  1029. $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
  1030. /**
  1031. * Filters the anchor tag for the edit link of a term.
  1032. *
  1033. * @since 3.1.0
  1034. *
  1035. * @param string $link The anchor tag for the edit link.
  1036. * @param int $term_id Term ID.
  1037. */
  1038. $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
  1039. if ( $echo ) {
  1040. echo $link;
  1041. } else {
  1042. return $link;
  1043. }
  1044. }
  1045. /**
  1046. * Retrieves the permalink for a search.
  1047. *
  1048. * @since 3.0.0
  1049. *
  1050. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  1051. *
  1052. * @param string $query Optional. The query string to use. If empty the current query is used. Default empty.
  1053. * @return string The search permalink.
  1054. */
  1055. function get_search_link( $query = '' ) {
  1056. global $wp_rewrite;
  1057. if ( empty( $query ) ) {
  1058. $search = get_search_query( false );
  1059. } else {
  1060. $search = stripslashes( $query );
  1061. }
  1062. $permastruct = $wp_rewrite->get_search_permastruct();
  1063. if ( empty( $permastruct ) ) {
  1064. $link = home_url( '?s=' . urlencode( $search ) );
  1065. } else {
  1066. $search = urlencode( $search );
  1067. $search = str_replace( '%2F', '/', $search ); // %2F(/) is not valid within a URL, send it un-encoded.
  1068. $link = str_replace( '%search%', $search, $permastruct );
  1069. $link = home_url( user_trailingslashit( $link, 'search' ) );
  1070. }
  1071. /**
  1072. * Filters the search permalink.
  1073. *
  1074. * @since 3.0.0
  1075. *
  1076. * @param string $link Search permalink.
  1077. * @param string $search The URL-encoded search term.
  1078. */
  1079. return apply_filters( 'search_link', $link, $search );
  1080. }
  1081. /**
  1082. * Retrieves the permalink for the search results feed.
  1083. *
  1084. * @since 2.5.0
  1085. *
  1086. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  1087. *
  1088. * @param string $search_query Optional. Search query. Default empty.
  1089. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  1090. * Default is the value of get_default_feed().
  1091. * @return string The search results feed permalink.
  1092. */
  1093. function get_search_feed_link( $search_query = '', $feed = '' ) {
  1094. global $wp_rewrite;
  1095. $link = get_search_link( $search_query );
  1096. if ( empty( $feed ) ) {
  1097. $feed = get_default_feed();
  1098. }
  1099. $permastruct = $wp_rewrite->get_search_permastruct();
  1100. if ( empty( $permastruct ) ) {
  1101. $link = add_query_arg( 'feed', $feed, $link );
  1102. } else {
  1103. $link = trailingslashit( $link );
  1104. $link .= "feed/$feed/";
  1105. }
  1106. /**
  1107. * Filters the search feed link.
  1108. *
  1109. * @since 2.5.0
  1110. *
  1111. * @param string $link Search feed link.
  1112. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  1113. * @param string $type The search type. One of 'posts' or 'comments'.
  1114. */
  1115. return apply_filters( 'search_feed_link', $link, $feed, 'posts' );
  1116. }
  1117. /**
  1118. * Retrieves the permalink for the search results comments feed.
  1119. *
  1120. * @since 2.5.0
  1121. *
  1122. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  1123. *
  1124. * @param string $search_query Optional. Search query. Default empty.
  1125. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  1126. * Default is the value of get_default_feed().
  1127. * @return string The comments feed search results permalink.
  1128. */
  1129. function get_search_comments_feed_link( $search_query = '', $feed = '' ) {
  1130. global $wp_rewrite;
  1131. if ( empty( $feed ) ) {
  1132. $feed = get_default_feed();
  1133. }
  1134. $link = get_search_feed_link( $search_query, $feed );
  1135. $permastruct = $wp_rewrite->get_search_permastruct();
  1136. if ( empty( $permastruct ) ) {
  1137. $link = add_query_arg( 'feed', 'comments-' . $feed, $link );
  1138. } else {
  1139. $link = add_query_arg( 'withcomments', 1, $link );
  1140. }
  1141. /** This filter is documented in wp-includes/link-template.php */
  1142. return apply_filters( 'search_feed_link', $link, $feed, 'comments' );
  1143. }
  1144. /**
  1145. * Retrieves the permalink for a post type archive.
  1146. *
  1147. * @since 3.1.0
  1148. * @since 4.5.0 Support for posts was added.
  1149. *
  1150. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  1151. *
  1152. * @param string $post_type Post type.
  1153. * @return string|false The post type archive permalink. False if the post type
  1154. * does not exist or does not have an archive.
  1155. */
  1156. function get_post_type_archive_link( $post_type ) {
  1157. global $wp_rewrite;
  1158. $post_type_obj = get_post_type_object( $post_type );
  1159. if ( ! $post_type_obj ) {
  1160. return false;
  1161. }
  1162. if ( 'post' === $post_type ) {
  1163. $show_on_front = get_option( 'show_on_front' );
  1164. $page_for_posts = get_option( 'page_for_posts' );
  1165. if ( 'page' === $show_on_front && $page_for_posts ) {
  1166. $link = get_permalink( $page_for_posts );
  1167. } else {
  1168. $link = get_home_url();
  1169. }
  1170. /** This filter is documented in wp-includes/link-template.php */
  1171. return apply_filters( 'post_type_archive_link', $link, $post_type );
  1172. }
  1173. if ( ! $post_type_obj->has_archive ) {
  1174. return false;
  1175. }
  1176. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
  1177. $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
  1178. if ( $post_type_obj->rewrite['with_front'] ) {
  1179. $struct = $wp_rewrite->front . $struct;
  1180. } else {
  1181. $struct = $wp_rewrite->root . $struct;
  1182. }
  1183. $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
  1184. } else {
  1185. $link = home_url( '?post_type=' . $post_type );
  1186. }
  1187. /**
  1188. * Filters the post type archive permalink.
  1189. *
  1190. * @since 3.1.0
  1191. *
  1192. * @param string $link The post type archive permalink.
  1193. * @param string $post_type Post type name.
  1194. */
  1195. return apply_filters( 'post_type_archive_link', $link, $post_type );
  1196. }
  1197. /**
  1198. * Retrieves the permalink for a post type archive feed.
  1199. *
  1200. * @since 3.1.0
  1201. *
  1202. * @param string $post_type Post type.
  1203. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
  1204. * Default is the value of get_default_feed().
  1205. * @return string|false The post type feed permalink. False if the post type
  1206. * does not exist or does not have an archive.
  1207. */
  1208. function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
  1209. $default_feed = get_default_feed();
  1210. if ( empty( $feed ) ) {
  1211. $feed = $default_feed;
  1212. }
  1213. $link = get_post_type_archive_link( $post_type );
  1214. if ( ! $link ) {
  1215. return false;
  1216. }
  1217. $post_type_obj = get_post_type_object( $post_type );
  1218. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
  1219. $link = trailingslashit( $link );
  1220. $link .= 'feed/';
  1221. if ( $feed != $default_feed ) {
  1222. $link .= "$feed/";
  1223. }
  1224. } else {
  1225. $link = add_query_arg( 'feed', $feed, $link );
  1226. }
  1227. /**
  1228. * Filters the post type archive feed link.
  1229. *
  1230. * @since 3.1.0
  1231. *
  1232. * @param string $link The post type archive feed link.
  1233. * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
  1234. */
  1235. return apply_filters( 'post_type_archive_feed_link', $link, $feed );
  1236. }
  1237. /**
  1238. * Retrieves the URL used for the post preview.
  1239. *
  1240. * Allows additional query args to be appended.
  1241. *
  1242. * @since 4.4.0
  1243. *
  1244. * @param int|WP_Post $post Optional. Post ID or `WP_Post` object. Defaults to global `$post`.
  1245. * @param array $query_args Optional. Array of additional query args to be appended to the link.
  1246. * Default empty array.
  1247. * @param string $preview_link Optional. Base preview link to be used if it should differ from the
  1248. * post permalink. Default empty.
  1249. * @return string|null URL used for the post preview, or null if the post does not exist.
  1250. */
  1251. function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) {
  1252. $post = get_post( $post );
  1253. if ( ! $post ) {
  1254. return;
  1255. }
  1256. $post_type_object = get_post_type_object( $post->post_type );
  1257. if ( is_post_type_viewable( $post_type_object ) ) {
  1258. if ( ! $preview_link ) {
  1259. $preview_link = set_url_scheme( get_permalink( $post ) );
  1260. }
  1261. $query_args['preview'] = 'true';
  1262. $preview_link = add_query_arg( $query_args, $preview_link );
  1263. }
  1264. /**
  1265. * Filters the URL used for a post preview.
  1266. *
  1267. * @since 2.0.5
  1268. * @since 4.0.0 Added the `$post` parameter.
  1269. *
  1270. * @param string $preview_link URL used for the post preview.
  1271. * @param WP_Post $post Post object.
  1272. */
  1273. return apply_filters( 'preview_post_link', $preview_link, $post );
  1274. }
  1275. /**
  1276. * Retrieves the edit post link for post.
  1277. *
  1278. * Can be used within the WordPress loop or outside of it. Can be used with
  1279. * pages, posts, attachments, and revisions.
  1280. *
  1281. * @since 2.3.0
  1282. *
  1283. * @param int|WP_Post $id Optional. Post ID or post object. Default is the global `$post`.
  1284. * @param string $context Optional. How to output the '&' character. Default '&amp;'.
  1285. * @return string|null The edit post link for the given post. Null if the post type does not exist
  1286. * or does not allow an editing UI.
  1287. */
  1288. function get_edit_post_link( $id = 0, $context = 'display' ) {
  1289. $post = get_post( $id );
  1290. if ( ! $post ) {
  1291. return;
  1292. }
  1293. if ( 'revision' === $post->post_type ) {
  1294. $action = '';
  1295. } elseif ( 'display' === $context ) {
  1296. $action = '&amp;action=edit';
  1297. } else {
  1298. $action = '&action=edit';
  1299. }
  1300. $post_type_object = get_post_type_object( $post->post_type );
  1301. if ( ! $post_type_object ) {
  1302. return;
  1303. }
  1304. if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  1305. return;
  1306. }
  1307. if ( $post_type_object->_edit_link ) {
  1308. $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );
  1309. } else {
  1310. $link = '';
  1311. }
  1312. /**
  1313. * Filters the post edit link.
  1314. *
  1315. * @since 2.3.0
  1316. *
  1317. * @param string $link The edit link.
  1318. * @param int $post_id Post ID.
  1319. * @param string $context The link context. If set to 'display' then ampersands
  1320. * are encoded.
  1321. */
  1322. return apply_filters( 'get_edit_post_link', $link, $post->ID, $context );
  1323. }
  1324. /**
  1325. * Displays the edit post link for post.
  1326. *
  1327. * @since 1.0.0
  1328. * @since 4.4.0 The `$class` argument was added.
  1329. *
  1330. * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null.
  1331. * @param string $before Optional. Display before edit link. Default empty.
  1332. * @param string $after Optional. Display after edit link. Default empty.
  1333. * @param int|WP_Post $id Optional. Post ID or post object. Default is the global `$post`.
  1334. * @param string $class Optional. Add custom class to link. Default 'post-edit-link'.
  1335. */
  1336. function edit_post_link( $text = null, $before = '', $after = '', $id = 0, $class = 'post-edit-link' ) {
  1337. $post = get_post( $id );
  1338. if ( ! $post ) {
  1339. return;
  1340. }
  1341. $url = get_edit_post_link( $post->ID );
  1342. if ( ! $url ) {
  1343. return;
  1344. }
  1345. if ( null === $text ) {
  1346. $text = __( 'Edit This' );
  1347. }
  1348. $link = '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>';
  1349. /**
  1350. * Filters the post edit link anchor tag.
  1351. *
  1352. * @since 2.3.0
  1353. *
  1354. * @param string $link Anchor tag for the edit link.
  1355. * @param int $post_id Post ID.
  1356. * @param string $text Anchor text.
  1357. */
  1358. echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
  1359. }
  1360. /**
  1361. * Retrieves the delete posts link for post.
  1362. *
  1363. * Can be used within the WordPress loop or outside of it, with any post type.
  1364. *
  1365. * @since 2.9.0
  1366. *
  1367. * @param int|WP_Post $id Optional. Post ID or post object. Default is the global `$post`.
  1368. * @param string $deprecated Not used.
  1369. * @param bool $force_delete Optional. Whether to bypass Trash and force deletion. Default false.
  1370. * @return string|void The delete post link URL for the given post.
  1371. */
  1372. function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
  1373. if ( ! empty( $deprecated ) ) {
  1374. _deprecated_argument( __FUNCTION__, '3.0.0' );
  1375. }
  1376. $post = get_post( $id );
  1377. if ( ! $post ) {
  1378. return;
  1379. }
  1380. $post_type_object = get_post_type_object( $post->post_type );
  1381. if ( ! $post_type_object ) {
  1382. return;
  1383. }
  1384. if ( ! current_user_can( 'delete_post', $post->ID ) ) {
  1385. return;
  1386. }
  1387. $action = ( $force_delete || ! EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
  1388. $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
  1389. /**
  1390. * Filters the post delete link.
  1391. *
  1392. * @since 2.9.0
  1393. *
  1394. * @param string $link The delete link.
  1395. * @param int $post_id Post ID.
  1396. * @param bool $force_delete Whether to bypass the Trash and force deletion. Default false.
  1397. */
  1398. return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
  1399. }
  1400. /**
  1401. * Retrieves the edit comment link.
  1402. *
  1403. * @since 2.3.0
  1404. *
  1405. * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object.
  1406. * @return string|void The edit comment link URL for the given comment.
  1407. */
  1408. function get_edit_comment_link( $comment_id = 0 ) {
  1409. $comment = get_comment( $comment_id );
  1410. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  1411. return;
  1412. }
  1413. $location = admin_url( 'comment.php?action=editcomment&amp;c=' ) . $comment->comment_ID;
  1414. /**
  1415. * Filters the comment edit link.
  1416. *
  1417. * @since 2.3.0
  1418. *
  1419. * @param string $location The edit link.
  1420. */
  1421. return apply_filters( 'get_edit_comment_link', $location );
  1422. }
  1423. /**
  1424. * Displays the edit comment link with formatting.
  1425. *
  1426. * @since 1.0.0
  1427. *
  1428. * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null.
  1429. * @param string $before Optional. Display before edit link. Default empty.
  1430. * @param string $after Optional. Display after edit link. Default empty.
  1431. */
  1432. function edit_comment_link( $text = null, $before = '', $after = '' ) {
  1433. $comment = get_comment();
  1434. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  1435. return;
  1436. }
  1437. if ( null === $text ) {
  1438. $text = __( 'Edit This' );
  1439. }
  1440. $link = '<a class="comment-edit-link" href="' . esc_url( get_edit_comment_link( $comment ) ) . '">' . $text . '</a>';
  1441. /**
  1442. * Filters the comment edit link anchor tag.
  1443. *
  1444. * @since 2.3.0
  1445. *
  1446. * @param string $link Anchor tag for the edit link.
  1447. * @param int $comment_id Comment ID.
  1448. * @param string $text Anchor text.
  1449. */
  1450. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
  1451. }
  1452. /**
  1453. * Displays the edit bookmark link.
  1454. *
  1455. * @since 2.7.0
  1456. *
  1457. * @param int|stdClass $link Optional. Bookmark ID. Default is the ID of the current bookmark.
  1458. * @return string|void The edit bookmark link URL.
  1459. */
  1460. function get_edit_bookmark_link( $link = 0 ) {
  1461. $link = get_bookmark( $link );
  1462. if ( ! current_user_can( 'manage_links' ) ) {
  1463. return;
  1464. }
  1465. $location = admin_url( 'link.php?action=edit&amp;link_id=' ) . $link->link_id;
  1466. /**
  1467. * Filters the bookmark edit link.
  1468. *
  1469. * @since 2.7.0
  1470. *
  1471. * @param string $location The edit link.
  1472. * @param int $link_id Bookmark ID.
  1473. */
  1474. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
  1475. }
  1476. /**
  1477. * Displays the edit bookmark link anchor content.
  1478. *
  1479. * @since 2.7.0
  1480. *
  1481. * @param string $link Optional. Anchor text. If empty, default is 'Edit This'. Default empty.
  1482. * @param string $before Optional. Display before edit link. Default empty.
  1483. * @param string $after Optional. Display after edit link. Default empty.
  1484. * @param int $bookmark Optional. Bookmark ID. Default is the current bookmark.
  1485. */
  1486. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
  1487. $bookmark = get_bookmark( $bookmark );
  1488. if ( ! current_user_can( 'manage_links' ) ) {
  1489. return;
  1490. }
  1491. if ( empty( $link ) ) {
  1492. $link = __( 'Edit This' );
  1493. }
  1494. $link = '<a href="' . esc_url( get_edit_bookmark_link( $bookmark ) ) . '">' . $link . '</a>';
  1495. /**
  1496. * Filters the bookmark edit link anchor tag.
  1497. *
  1498. * @since 2.7.0
  1499. *
  1500. * @param string $link Anchor tag for the edit link.
  1501. * @param int $link_id Bookmark ID.
  1502. */
  1503. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
  1504. }
  1505. /**
  1506. * Retrieves the edit user link.
  1507. *
  1508. * @since 3.5.0
  1509. *
  1510. * @param int $user_id Optional. User ID. Defaults to the current user.
  1511. * @return string URL to edit user page or empty string.
  1512. */
  1513. function get_edit_user_link( $user_id = null ) {
  1514. if ( ! $user_id ) {
  1515. $user_id = get_current_user_id();
  1516. }
  1517. if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) ) {
  1518. return '';
  1519. }
  1520. $user = get_userdata( $user_id );
  1521. if ( ! $user ) {
  1522. return '';
  1523. }
  1524. if ( get_current_user_id() == $user->ID ) {
  1525. $link = get_edit_profile_url( $user->ID );
  1526. } else {
  1527. $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
  1528. }
  1529. /**
  1530. * Filters the user edit link.
  1531. *
  1532. * @since 3.5.0
  1533. *
  1534. * @param string $link The edit link.
  1535. * @param int $user_id User ID.
  1536. */
  1537. return apply_filters( 'get_edit_user_link', $link, $user->ID );
  1538. }
  1539. //
  1540. // Navigation links.
  1541. //
  1542. /**
  1543. * Retrieves the previous post that is adjacent to the current post.
  1544. *
  1545. * @since 1.5.0
  1546. *
  1547. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
  1548. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1549. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1550. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
  1551. * corresponding post exists.
  1552. */
  1553. function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1554. return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
  1555. }
  1556. /**
  1557. * Retrieves the next post that is adjacent to the current post.
  1558. *
  1559. * @since 1.5.0
  1560. *
  1561. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
  1562. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1563. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1564. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
  1565. * corresponding post exists.
  1566. */
  1567. function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1568. return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
  1569. }
  1570. /**
  1571. * Retrieves the adjacent post.
  1572. *
  1573. * Can either be next or previous post.
  1574. *
  1575. * @since 2.5.0
  1576. *
  1577. * @global wpdb $wpdb WordPress database abstraction object.
  1578. *
  1579. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
  1580. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty string.
  1581. * @param bool $previous Optional. Whether to retrieve previous post. Default true
  1582. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1583. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no
  1584. * corresponding post exists.
  1585. */
  1586. function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1587. global $wpdb;
  1588. $post = get_post();
  1589. if ( ! $post || ! taxonomy_exists( $taxonomy ) ) {
  1590. return null;
  1591. }
  1592. $current_post_date = $post->post_date;
  1593. $join = '';
  1594. $where = '';
  1595. $adjacent = $previous ? 'previous' : 'next';
  1596. if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
  1597. // Back-compat, $excluded_terms used to be $excluded_categories with IDs separated by " and ".
  1598. if ( false !== strpos( $excluded_terms, ' and ' ) ) {
  1599. _deprecated_argument(
  1600. __FUNCTION__,
  1601. '3.3.0',
  1602. sprintf(
  1603. /* translators: %s: The word 'and'. */
  1604. __( 'Use commas instead of %s to separate excluded terms.' ),
  1605. "'and'"
  1606. )
  1607. );
  1608. $excluded_terms = explode( ' and ', $excluded_terms );
  1609. } else {
  1610. $excluded_terms = explode( ',', $excluded_terms );
  1611. }
  1612. $excluded_terms = array_map( 'intval', $excluded_terms );
  1613. }
  1614. /**
  1615. * Filters the IDs of terms excluded from adjacent post queries.
  1616. *
  1617. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1618. * of adjacency, 'next' or 'previous'.
  1619. *
  1620. * Possible hook names include:
  1621. *
  1622. * - `get_next_post_excluded_terms`
  1623. * - `get_previous_post_excluded_terms`
  1624. *
  1625. * @since 4.4.0
  1626. *
  1627. * @param array|string $excluded_terms Array of excluded term IDs. Empty string if none were provided.
  1628. */
  1629. $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
  1630. if ( $in_same_term || ! empty( $excluded_terms ) ) {
  1631. if ( $in_same_term ) {
  1632. $join .= " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
  1633. $where .= $wpdb->prepare( 'AND tt.taxonomy = %s', $taxonomy );
  1634. if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
  1635. return '';
  1636. }
  1637. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  1638. // Remove any exclusions from the term array to include.
  1639. $term_array = array_diff( $term_array, (array) $excluded_terms );
  1640. $term_array = array_map( 'intval', $term_array );
  1641. if ( ! $term_array || is_wp_error( $term_array ) ) {
  1642. return '';
  1643. }
  1644. $where .= ' AND tt.term_id IN (' . implode( ',', $term_array ) . ')';
  1645. }
  1646. if ( ! empty( $excluded_terms ) ) {
  1647. $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( ',', array_map( 'intval', $excluded_terms ) ) . ') )';
  1648. }
  1649. }
  1650. // 'post_status' clause depends on the current user.
  1651. if ( is_user_logged_in() ) {
  1652. $user_id = get_current_user_id();
  1653. $post_type_object = get_post_type_object( $post->post_type );
  1654. if ( empty( $post_type_object ) ) {
  1655. $post_type_cap = $post->post_type;
  1656. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  1657. } else {
  1658. $read_private_cap = $post_type_object->cap->read_private_posts;
  1659. }
  1660. /*
  1661. * Results should include private posts belonging to the current user, or private posts where the
  1662. * current user has the 'read_private_posts' cap.
  1663. */
  1664. $private_states = get_post_stati( array( 'private' => true ) );
  1665. $where .= " AND ( p.post_status = 'publish'";
  1666. foreach ( (array) $private_states as $state ) {
  1667. if ( current_user_can( $read_private_cap ) ) {
  1668. $where .= $wpdb->prepare( ' OR p.post_status = %s', $state );
  1669. } else {
  1670. $where .= $wpdb->prepare( ' OR (p.post_author = %d AND p.post_status = %s)', $user_id, $state );
  1671. }
  1672. }
  1673. $where .= ' )';
  1674. } else {
  1675. $where .= " AND p.post_status = 'publish'";
  1676. }
  1677. $op = $previous ? '<' : '>';
  1678. $order = $previous ? 'DESC' : 'ASC';
  1679. /**
  1680. * Filters the JOIN clause in the SQL for an adjacent post query.
  1681. *
  1682. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1683. * of adjacency, 'next' or 'previous'.
  1684. *
  1685. * Possible hook names include:
  1686. *
  1687. * - `get_next_post_join`
  1688. * - `get_previous_post_join`
  1689. *
  1690. * @since 2.5.0
  1691. * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
  1692. *
  1693. * @param string $join The JOIN clause in the SQL.
  1694. * @param bool $in_same_term Whether post should be in a same taxonomy term.
  1695. * @param array $excluded_terms Array of excluded term IDs.
  1696. * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
  1697. * @param WP_Post $post WP_Post object.
  1698. */
  1699. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post );
  1700. /**
  1701. * Filters the WHERE clause in the SQL for an adjacent post query.
  1702. *
  1703. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1704. * of adjacency, 'next' or 'previous'.
  1705. *
  1706. * Possible hook names include:
  1707. *
  1708. * - `get_next_post_where`
  1709. * - `get_previous_post_where`
  1710. *
  1711. * @since 2.5.0
  1712. * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
  1713. *
  1714. * @param string $where The `WHERE` clause in the SQL.
  1715. * @param bool $in_same_term Whether post should be in a same taxonomy term.
  1716. * @param array $excluded_terms Array of excluded term IDs.
  1717. * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
  1718. * @param WP_Post $post WP_Post object.
  1719. */
  1720. $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms, $taxonomy, $post );
  1721. /**
  1722. * Filters the ORDER BY clause in the SQL for an adjacent post query.
  1723. *
  1724. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1725. * of adjacency, 'next' or 'previous'.
  1726. *
  1727. * Possible hook names include:
  1728. *
  1729. * - `get_next_post_sort`
  1730. * - `get_previous_post_sort`
  1731. *
  1732. * @since 2.5.0
  1733. * @since 4.4.0 Added the `$post` parameter.
  1734. * @since 4.9.0 Added the `$order` parameter.
  1735. *
  1736. * @param string $order_by The `ORDER BY` clause in the SQL.
  1737. * @param WP_Post $post WP_Post object.
  1738. * @param string $order Sort order. 'DESC' for previous post, 'ASC' for next.
  1739. */
  1740. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post, $order );
  1741. $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
  1742. $query_key = 'adjacent_post_' . md5( $query );
  1743. $result = wp_cache_get( $query_key, 'counts' );
  1744. if ( false !== $result ) {
  1745. if ( $result ) {
  1746. $result = get_post( $result );
  1747. }
  1748. return $result;
  1749. }
  1750. $result = $wpdb->get_var( $query );
  1751. if ( null === $result ) {
  1752. $result = '';
  1753. }
  1754. wp_cache_set( $query_key, $result, 'counts' );
  1755. if ( $result ) {
  1756. $result = get_post( $result );
  1757. }
  1758. return $result;
  1759. }
  1760. /**
  1761. * Retrieves the adjacent post relational link.
  1762. *
  1763. * Can either be next or previous post relational link.
  1764. *
  1765. * @since 2.8.0
  1766. *
  1767. * @param string $title Optional. Link title format. Default '%title'.
  1768. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1769. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1770. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1771. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1772. * @return string|void The adjacent post relational link URL.
  1773. */
  1774. function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1775. $post = get_post();
  1776. if ( $previous && is_attachment() && $post ) {
  1777. $post = get_post( $post->post_parent );
  1778. } else {
  1779. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
  1780. }
  1781. if ( empty( $post ) ) {
  1782. return;
  1783. }
  1784. $post_title = the_title_attribute(
  1785. array(
  1786. 'echo' => false,
  1787. 'post' => $post,
  1788. )
  1789. );
  1790. if ( empty( $post_title ) ) {
  1791. $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
  1792. }
  1793. $date = mysql2date( get_option( 'date_format' ), $post->post_date );
  1794. $title = str_replace( '%title', $post_title, $title );
  1795. $title = str_replace( '%date', $date, $title );
  1796. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
  1797. $link .= esc_attr( $title );
  1798. $link .= "' href='" . get_permalink( $post ) . "' />\n";
  1799. $adjacent = $previous ? 'previous' : 'next';
  1800. /**
  1801. * Filters the adjacent post relational link.
  1802. *
  1803. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1804. * of adjacency, 'next' or 'previous'.
  1805. *
  1806. * Possible hook names include:
  1807. *
  1808. * - `next_post_rel_link`
  1809. * - `previous_post_rel_link`
  1810. *
  1811. * @since 2.8.0
  1812. *
  1813. * @param string $link The relational link.
  1814. */
  1815. return apply_filters( "{$adjacent}_post_rel_link", $link );
  1816. }
  1817. /**
  1818. * Displays the relational links for the posts adjacent to the current post.
  1819. *
  1820. * @since 2.8.0
  1821. *
  1822. * @param string $title Optional. Link title format. Default '%title'.
  1823. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1824. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1825. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1826. */
  1827. function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1828. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
  1829. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
  1830. }
  1831. /**
  1832. * Displays relational links for the posts adjacent to the current post for single post pages.
  1833. *
  1834. * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins
  1835. * or theme templates.
  1836. *
  1837. * @since 3.0.0
  1838. * @since 5.6.0 No longer used in core.
  1839. *
  1840. * @see adjacent_posts_rel_link()
  1841. */
  1842. function adjacent_posts_rel_link_wp_head() {
  1843. if ( ! is_single() || is_attachment() ) {
  1844. return;
  1845. }
  1846. adjacent_posts_rel_link();
  1847. }
  1848. /**
  1849. * Displays the relational link for the next post adjacent to the current post.
  1850. *
  1851. * @since 2.8.0
  1852. *
  1853. * @see get_adjacent_post_rel_link()
  1854. *
  1855. * @param string $title Optional. Link title format. Default '%title'.
  1856. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1857. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1858. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1859. */
  1860. function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1861. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
  1862. }
  1863. /**
  1864. * Displays the relational link for the previous post adjacent to the current post.
  1865. *
  1866. * @since 2.8.0
  1867. *
  1868. * @see get_adjacent_post_rel_link()
  1869. *
  1870. * @param string $title Optional. Link title format. Default '%title'.
  1871. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1872. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
  1873. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1874. */
  1875. function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1876. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
  1877. }
  1878. /**
  1879. * Retrieves the boundary post.
  1880. *
  1881. * Boundary being either the first or last post by publish date within the constraints specified
  1882. * by $in_same_term or $excluded_terms.
  1883. *
  1884. * @since 2.8.0
  1885. *
  1886. * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
  1887. * Default false.
  1888. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1889. * Default empty.
  1890. * @param bool $start Optional. Whether to retrieve first or last post. Default true
  1891. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1892. * @return null|array Array containing the boundary post object if successful, null otherwise.
  1893. */
  1894. function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
  1895. $post = get_post();
  1896. if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) {
  1897. return null;
  1898. }
  1899. $query_args = array(
  1900. 'posts_per_page' => 1,
  1901. 'order' => $start ? 'ASC' : 'DESC',
  1902. 'update_post_term_cache' => false,
  1903. 'update_post_meta_cache' => false,
  1904. );
  1905. $term_array = array();
  1906. if ( ! is_array( $excluded_terms ) ) {
  1907. if ( ! empty( $excluded_terms ) ) {
  1908. $excluded_terms = explode( ',', $excluded_terms );
  1909. } else {
  1910. $excluded_terms = array();
  1911. }
  1912. }
  1913. if ( $in_same_term || ! empty( $excluded_terms ) ) {
  1914. if ( $in_same_term ) {
  1915. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  1916. }
  1917. if ( ! empty( $excluded_terms ) ) {
  1918. $excluded_terms = array_map( 'intval', $excluded_terms );
  1919. $excluded_terms = array_diff( $excluded_terms, $term_array );
  1920. $inverse_terms = array();
  1921. foreach ( $excluded_terms as $excluded_term ) {
  1922. $inverse_terms[] = $excluded_term * -1;
  1923. }
  1924. $excluded_terms = $inverse_terms;
  1925. }
  1926. $query_args['tax_query'] = array(
  1927. array(
  1928. 'taxonomy' => $taxonomy,
  1929. 'terms' => array_merge( $term_array, $excluded_terms ),
  1930. ),
  1931. );
  1932. }
  1933. return get_posts( $query_args );
  1934. }
  1935. /**
  1936. * Retrieves the previous post link that is adjacent to the current post.
  1937. *
  1938. * @since 3.7.0
  1939. *
  1940. * @param string $format Optional. Link anchor format. Default '&laquo; %link'.
  1941. * @param string $link Optional. Link permalink format. Default '%title'.
  1942. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1943. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1944. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1945. * @return string The link URL of the previous post in relation to the current post.
  1946. */
  1947. function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1948. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
  1949. }
  1950. /**
  1951. * Displays the previous post link that is adjacent to the current post.
  1952. *
  1953. * @since 1.5.0
  1954. *
  1955. * @see get_previous_post_link()
  1956. *
  1957. * @param string $format Optional. Link anchor format. Default '&laquo; %link'.
  1958. * @param string $link Optional. Link permalink format. Default '%title'.
  1959. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1960. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1961. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1962. */
  1963. function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1964. echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
  1965. }
  1966. /**
  1967. * Retrieves the next post link that is adjacent to the current post.
  1968. *
  1969. * @since 3.7.0
  1970. *
  1971. * @param string $format Optional. Link anchor format. Default '&laquo; %link'.
  1972. * @param string $link Optional. Link permalink format. Default '%title'.
  1973. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1974. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1975. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1976. * @return string The link URL of the next post in relation to the current post.
  1977. */
  1978. function get_next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1979. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
  1980. }
  1981. /**
  1982. * Displays the next post link that is adjacent to the current post.
  1983. *
  1984. * @since 1.5.0
  1985. *
  1986. * @see get_next_post_link()
  1987. *
  1988. * @param string $format Optional. Link anchor format. Default '&laquo; %link'.
  1989. * @param string $link Optional. Link permalink format. Default '%title'
  1990. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  1991. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  1992. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1993. */
  1994. function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1995. echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
  1996. }
  1997. /**
  1998. * Retrieves the adjacent post link.
  1999. *
  2000. * Can be either next post link or previous.
  2001. *
  2002. * @since 3.7.0
  2003. *
  2004. * @param string $format Link anchor format.
  2005. * @param string $link Link permalink format.
  2006. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  2007. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs. Default empty.
  2008. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  2009. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  2010. * @return string The link URL of the previous or next post in relation to the current post.
  2011. */
  2012. function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  2013. if ( $previous && is_attachment() ) {
  2014. $post = get_post( get_post()->post_parent );
  2015. } else {
  2016. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
  2017. }
  2018. if ( ! $post ) {
  2019. $output = '';
  2020. } else {
  2021. $title = $post->post_title;
  2022. if ( empty( $post->post_title ) ) {
  2023. $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
  2024. }
  2025. /** This filter is documented in wp-includes/post-template.php */
  2026. $title = apply_filters( 'the_title', $title, $post->ID );
  2027. $date = mysql2date( get_option( 'date_format' ), $post->post_date );
  2028. $rel = $previous ? 'prev' : 'next';
  2029. $string = '<a href="' . get_permalink( $post ) . '" rel="' . $rel . '">';
  2030. $inlink = str_replace( '%title', $title, $link );
  2031. $inlink = str_replace( '%date', $date, $inlink );
  2032. $inlink = $string . $inlink . '</a>';
  2033. $output = str_replace( '%link', $inlink, $format );
  2034. }
  2035. $adjacent = $previous ? 'previous' : 'next';
  2036. /**
  2037. * Filters the adjacent post link.
  2038. *
  2039. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  2040. * of adjacency, 'next' or 'previous'.
  2041. *
  2042. * Possible hook names include:
  2043. *
  2044. * - `next_post_link`
  2045. * - `previous_post_link`
  2046. *
  2047. * @since 2.6.0
  2048. * @since 4.2.0 Added the `$adjacent` parameter.
  2049. *
  2050. * @param string $output The adjacent post link.
  2051. * @param string $format Link anchor format.
  2052. * @param string $link Link permalink format.
  2053. * @param WP_Post $post The adjacent post.
  2054. * @param string $adjacent Whether the post is previous or next.
  2055. */
  2056. return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );
  2057. }
  2058. /**
  2059. * Displays the adjacent post link.
  2060. *
  2061. * Can be either next post link or previous.
  2062. *
  2063. * @since 2.5.0
  2064. *
  2065. * @param string $format Link anchor format.
  2066. * @param string $link Link permalink format.
  2067. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  2068. * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs. Default empty.
  2069. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  2070. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  2071. */
  2072. function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  2073. echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
  2074. }
  2075. /**
  2076. * Retrieves the link for a page number.
  2077. *
  2078. * @since 1.5.0
  2079. *
  2080. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  2081. *
  2082. * @param int $pagenum Optional. Page number. Default 1.
  2083. * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
  2084. * Otherwise, prepares the URL with esc_url_raw().
  2085. * @return string The link URL for the given page number.
  2086. */
  2087. function get_pagenum_link( $pagenum = 1, $escape = true ) {
  2088. global $wp_rewrite;
  2089. $pagenum = (int) $pagenum;
  2090. $request = remove_query_arg( 'paged' );
  2091. $home_root = parse_url( home_url() );
  2092. $home_root = ( isset( $home_root['path'] ) ) ? $home_root['path'] : '';
  2093. $home_root = preg_quote( $home_root, '|' );
  2094. $request = preg_replace( '|^' . $home_root . '|i', '', $request );
  2095. $request = preg_replace( '|^/+|', '', $request );
  2096. if ( ! $wp_rewrite->using_permalinks() || is_admin() ) {
  2097. $base = trailingslashit( get_bloginfo( 'url' ) );
  2098. if ( $pagenum > 1 ) {
  2099. $result = add_query_arg( 'paged', $pagenum, $base . $request );
  2100. } else {
  2101. $result = $base . $request;
  2102. }
  2103. } else {
  2104. $qs_regex = '|\?.*?$|';
  2105. preg_match( $qs_regex, $request, $qs_match );
  2106. if ( ! empty( $qs_match[0] ) ) {
  2107. $query_string = $qs_match[0];
  2108. $request = preg_replace( $qs_regex, '', $request );
  2109. } else {
  2110. $query_string = '';
  2111. }
  2112. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request );
  2113. $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request );
  2114. $request = ltrim( $request, '/' );
  2115. $base = trailingslashit( get_bloginfo( 'url' ) );
  2116. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' !== $request ) ) {
  2117. $base .= $wp_rewrite->index . '/';
  2118. }
  2119. if ( $pagenum > 1 ) {
  2120. $request = ( ( ! empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . '/' . $pagenum, 'paged' );
  2121. }
  2122. $result = $base . $request . $query_string;
  2123. }
  2124. /**
  2125. * Filters the page number link for the current request.
  2126. *
  2127. * @since 2.5.0
  2128. * @since 5.2.0 Added the `$pagenum` argument.
  2129. *
  2130. * @param string $result The page number link.
  2131. * @param int $pagenum The page number.
  2132. */
  2133. $result = apply_filters( 'get_pagenum_link', $result, $pagenum );
  2134. if ( $escape ) {
  2135. return esc_url( $result );
  2136. } else {
  2137. return esc_url_raw( $result );
  2138. }
  2139. }
  2140. /**
  2141. * Retrieves the next posts page link.
  2142. *
  2143. * Backported from 2.1.3 to 2.0.10.
  2144. *
  2145. * @since 2.0.10
  2146. *
  2147. * @global int $paged
  2148. *
  2149. * @param int $max_page Optional. Max pages. Default 0.
  2150. * @return string|void The link URL for next posts page.
  2151. */
  2152. function get_next_posts_page_link( $max_page = 0 ) {
  2153. global $paged;
  2154. if ( ! is_single() ) {
  2155. if ( ! $paged ) {
  2156. $paged = 1;
  2157. }
  2158. $nextpage = (int) $paged + 1;
  2159. if ( ! $max_page || $max_page >= $nextpage ) {
  2160. return get_pagenum_link( $nextpage );
  2161. }
  2162. }
  2163. }
  2164. /**
  2165. * Displays or retrieves the next posts page link.
  2166. *
  2167. * @since 0.71
  2168. *
  2169. * @param int $max_page Optional. Max pages. Default 0.
  2170. * @param bool $echo Optional. Whether to echo the link. Default true.
  2171. * @return string|void The link URL for next posts page if `$echo = false`.
  2172. */
  2173. function next_posts( $max_page = 0, $echo = true ) {
  2174. $output = esc_url( get_next_posts_page_link( $max_page ) );
  2175. if ( $echo ) {
  2176. echo $output;
  2177. } else {
  2178. return $output;
  2179. }
  2180. }
  2181. /**
  2182. * Retrieves the next posts page link.
  2183. *
  2184. * @since 2.7.0
  2185. *
  2186. * @global int $paged
  2187. * @global WP_Query $wp_query WordPress Query object.
  2188. *
  2189. * @param string $label Content for link text.
  2190. * @param int $max_page Optional. Max pages. Default 0.
  2191. * @return string|void HTML-formatted next posts page link.
  2192. */
  2193. function get_next_posts_link( $label = null, $max_page = 0 ) {
  2194. global $paged, $wp_query;
  2195. if ( ! $max_page ) {
  2196. $max_page = $wp_query->max_num_pages;
  2197. }
  2198. if ( ! $paged ) {
  2199. $paged = 1;
  2200. }
  2201. $nextpage = (int) $paged + 1;
  2202. if ( null === $label ) {
  2203. $label = __( 'Next Page &raquo;' );
  2204. }
  2205. if ( ! is_single() && ( $nextpage <= $max_page ) ) {
  2206. /**
  2207. * Filters the anchor tag attributes for the next posts page link.
  2208. *
  2209. * @since 2.7.0
  2210. *
  2211. * @param string $attributes Attributes for the anchor tag.
  2212. */
  2213. $attr = apply_filters( 'next_posts_link_attributes', '' );
  2214. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) . '</a>';
  2215. }
  2216. }
  2217. /**
  2218. * Displays the next posts page link.
  2219. *
  2220. * @since 0.71
  2221. *
  2222. * @param string $label Content for link text.
  2223. * @param int $max_page Optional. Max pages. Default 0.
  2224. */
  2225. function next_posts_link( $label = null, $max_page = 0 ) {
  2226. echo get_next_posts_link( $label, $max_page );
  2227. }
  2228. /**
  2229. * Retrieves the previous posts page link.
  2230. *
  2231. * Will only return string, if not on a single page or post.
  2232. *
  2233. * Backported to 2.0.10 from 2.1.3.
  2234. *
  2235. * @since 2.0.10
  2236. *
  2237. * @global int $paged
  2238. *
  2239. * @return string|void The link for the previous posts page.
  2240. */
  2241. function get_previous_posts_page_link() {
  2242. global $paged;
  2243. if ( ! is_single() ) {
  2244. $nextpage = (int) $paged - 1;
  2245. if ( $nextpage < 1 ) {
  2246. $nextpage = 1;
  2247. }
  2248. return get_pagenum_link( $nextpage );
  2249. }
  2250. }
  2251. /**
  2252. * Displays or retrieves the previous posts page link.
  2253. *
  2254. * @since 0.71
  2255. *
  2256. * @param bool $echo Optional. Whether to echo the link. Default true.
  2257. * @return string|void The previous posts page link if `$echo = false`.
  2258. */
  2259. function previous_posts( $echo = true ) {
  2260. $output = esc_url( get_previous_posts_page_link() );
  2261. if ( $echo ) {
  2262. echo $output;
  2263. } else {
  2264. return $output;
  2265. }
  2266. }
  2267. /**
  2268. * Retrieves the previous posts page link.
  2269. *
  2270. * @since 2.7.0
  2271. *
  2272. * @global int $paged
  2273. *
  2274. * @param string $label Optional. Previous page link text.
  2275. * @return string|void HTML-formatted previous page link.
  2276. */
  2277. function get_previous_posts_link( $label = null ) {
  2278. global $paged;
  2279. if ( null === $label ) {
  2280. $label = __( '&laquo; Previous Page' );
  2281. }
  2282. if ( ! is_single() && $paged > 1 ) {
  2283. /**
  2284. * Filters the anchor tag attributes for the previous posts page link.
  2285. *
  2286. * @since 2.7.0
  2287. *
  2288. * @param string $attributes Attributes for the anchor tag.
  2289. */
  2290. $attr = apply_filters( 'previous_posts_link_attributes', '' );
  2291. return '<a href="' . previous_posts( false ) . "\" $attr>" . preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) . '</a>';
  2292. }
  2293. }
  2294. /**
  2295. * Displays the previous posts page link.
  2296. *
  2297. * @since 0.71
  2298. *
  2299. * @param string $label Optional. Previous page link text.
  2300. */
  2301. function previous_posts_link( $label = null ) {
  2302. echo get_previous_posts_link( $label );
  2303. }
  2304. /**
  2305. * Retrieves the post pages link navigation for previous and next pages.
  2306. *
  2307. * @since 2.8.0
  2308. *
  2309. * @global WP_Query $wp_query WordPress Query object.
  2310. *
  2311. * @param string|array $args {
  2312. * Optional. Arguments to build the post pages link navigation.
  2313. *
  2314. * @type string $sep Separator character. Default '&#8212;'.
  2315. * @type string $prelabel Link text to display for the previous page link.
  2316. * Default '&laquo; Previous Page'.
  2317. * @type string $nxtlabel Link text to display for the next page link.
  2318. * Default 'Next Page &raquo;'.
  2319. * }
  2320. * @return string The posts link navigation.
  2321. */
  2322. function get_posts_nav_link( $args = array() ) {
  2323. global $wp_query;
  2324. $return = '';
  2325. if ( ! is_singular() ) {
  2326. $defaults = array(
  2327. 'sep' => ' &#8212; ',
  2328. 'prelabel' => __( '&laquo; Previous Page' ),
  2329. 'nxtlabel' => __( 'Next Page &raquo;' ),
  2330. );
  2331. $args = wp_parse_args( $args, $defaults );
  2332. $max_num_pages = $wp_query->max_num_pages;
  2333. $paged = get_query_var( 'paged' );
  2334. // Only have sep if there's both prev and next results.
  2335. if ( $paged < 2 || $paged >= $max_num_pages ) {
  2336. $args['sep'] = '';
  2337. }
  2338. if ( $max_num_pages > 1 ) {
  2339. $return = get_previous_posts_link( $args['prelabel'] );
  2340. $return .= preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $args['sep'] );
  2341. $return .= get_next_posts_link( $args['nxtlabel'] );
  2342. }
  2343. }
  2344. return $return;
  2345. }
  2346. /**
  2347. * Displays the post pages link navigation for previous and next pages.
  2348. *
  2349. * @since 0.71
  2350. *
  2351. * @param string $sep Optional. Separator for posts navigation links. Default empty.
  2352. * @param string $prelabel Optional. Label for previous pages. Default empty.
  2353. * @param string $nxtlabel Optional Label for next pages. Default empty.
  2354. */
  2355. function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
  2356. $args = array_filter( compact( 'sep', 'prelabel', 'nxtlabel' ) );
  2357. echo get_posts_nav_link( $args );
  2358. }
  2359. /**
  2360. * Retrieves the navigation to next/previous post, when applicable.
  2361. *
  2362. * @since 4.1.0
  2363. * @since 4.4.0 Introduced the `in_same_term`, `excluded_terms`, and `taxonomy` arguments.
  2364. * @since 5.3.0 Added the `aria_label` parameter.
  2365. * @since 5.5.0 Added the `class` parameter.
  2366. *
  2367. * @param array $args {
  2368. * Optional. Default post navigation arguments. Default empty array.
  2369. *
  2370. * @type string $prev_text Anchor text to display in the previous post link. Default '%title'.
  2371. * @type string $next_text Anchor text to display in the next post link. Default '%title'.
  2372. * @type bool $in_same_term Whether link should be in a same taxonomy term. Default false.
  2373. * @type int[]|string $excluded_terms Array or comma-separated list of excluded term IDs. Default empty.
  2374. * @type string $taxonomy Taxonomy, if `$in_same_term` is true. Default 'category'.
  2375. * @type string $screen_reader_text Screen reader text for the nav element. Default 'Post navigation'.
  2376. * @type string $aria_label ARIA label text for the nav element. Default 'Posts'.
  2377. * @type string $class Custom class for the nav element. Default 'post-navigation'.
  2378. * }
  2379. * @return string Markup for post links.
  2380. */
  2381. function get_the_post_navigation( $args = array() ) {
  2382. // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
  2383. if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
  2384. $args['aria_label'] = $args['screen_reader_text'];
  2385. }
  2386. $args = wp_parse_args(
  2387. $args,
  2388. array(
  2389. 'prev_text' => '%title',
  2390. 'next_text' => '%title',
  2391. 'in_same_term' => false,
  2392. 'excluded_terms' => '',
  2393. 'taxonomy' => 'category',
  2394. 'screen_reader_text' => __( 'Post navigation' ),
  2395. 'aria_label' => __( 'Posts' ),
  2396. 'class' => 'post-navigation',
  2397. )
  2398. );
  2399. $navigation = '';
  2400. $previous = get_previous_post_link(
  2401. '<div class="nav-previous">%link</div>',
  2402. $args['prev_text'],
  2403. $args['in_same_term'],
  2404. $args['excluded_terms'],
  2405. $args['taxonomy']
  2406. );
  2407. $next = get_next_post_link(
  2408. '<div class="nav-next">%link</div>',
  2409. $args['next_text'],
  2410. $args['in_same_term'],
  2411. $args['excluded_terms'],
  2412. $args['taxonomy']
  2413. );
  2414. // Only add markup if there's somewhere to navigate to.
  2415. if ( $previous || $next ) {
  2416. $navigation = _navigation_markup( $previous . $next, $args['class'], $args['screen_reader_text'], $args['aria_label'] );
  2417. }
  2418. return $navigation;
  2419. }
  2420. /**
  2421. * Displays the navigation to next/previous post, when applicable.
  2422. *
  2423. * @since 4.1.0
  2424. *
  2425. * @param array $args Optional. See get_the_post_navigation() for available arguments.
  2426. * Default empty array.
  2427. */
  2428. function the_post_navigation( $args = array() ) {
  2429. echo get_the_post_navigation( $args );
  2430. }
  2431. /**
  2432. * Returns the navigation to next/previous set of posts, when applicable.
  2433. *
  2434. * @since 4.1.0
  2435. * @since 5.3.0 Added the `aria_label` parameter.
  2436. * @since 5.5.0 Added the `class` parameter.
  2437. *
  2438. * @global WP_Query $wp_query WordPress Query object.
  2439. *
  2440. * @param array $args {
  2441. * Optional. Default posts navigation arguments. Default empty array.
  2442. *
  2443. * @type string $prev_text Anchor text to display in the previous posts link.
  2444. * Default 'Older posts'.
  2445. * @type string $next_text Anchor text to display in the next posts link.
  2446. * Default 'Newer posts'.
  2447. * @type string $screen_reader_text Screen reader text for the nav element.
  2448. * Default 'Posts navigation'.
  2449. * @type string $aria_label ARIA label text for the nav element. Default 'Posts'.
  2450. * @type string $class Custom class for the nav element. Default 'posts-navigation'.
  2451. * }
  2452. * @return string Markup for posts links.
  2453. */
  2454. function get_the_posts_navigation( $args = array() ) {
  2455. $navigation = '';
  2456. // Don't print empty markup if there's only one page.
  2457. if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
  2458. // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
  2459. if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
  2460. $args['aria_label'] = $args['screen_reader_text'];
  2461. }
  2462. $args = wp_parse_args(
  2463. $args,
  2464. array(
  2465. 'prev_text' => __( 'Older posts' ),
  2466. 'next_text' => __( 'Newer posts' ),
  2467. 'screen_reader_text' => __( 'Posts navigation' ),
  2468. 'aria_label' => __( 'Posts' ),
  2469. 'class' => 'posts-navigation',
  2470. )
  2471. );
  2472. $next_link = get_previous_posts_link( $args['next_text'] );
  2473. $prev_link = get_next_posts_link( $args['prev_text'] );
  2474. if ( $prev_link ) {
  2475. $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
  2476. }
  2477. if ( $next_link ) {
  2478. $navigation .= '<div class="nav-next">' . $next_link . '</div>';
  2479. }
  2480. $navigation = _navigation_markup( $navigation, $args['class'], $args['screen_reader_text'], $args['aria_label'] );
  2481. }
  2482. return $navigation;
  2483. }
  2484. /**
  2485. * Displays the navigation to next/previous set of posts, when applicable.
  2486. *
  2487. * @since 4.1.0
  2488. *
  2489. * @param array $args Optional. See get_the_posts_navigation() for available arguments.
  2490. * Default empty array.
  2491. */
  2492. function the_posts_navigation( $args = array() ) {
  2493. echo get_the_posts_navigation( $args );
  2494. }
  2495. /**
  2496. * Retrieves a paginated navigation to next/previous set of posts, when applicable.
  2497. *
  2498. * @since 4.1.0
  2499. * @since 5.3.0 Added the `aria_label` parameter.
  2500. * @since 5.5.0 Added the `class` parameter.
  2501. *
  2502. * @param array $args {
  2503. * Optional. Default pagination arguments, see paginate_links().
  2504. *
  2505. * @type string $screen_reader_text Screen reader text for navigation element.
  2506. * Default 'Posts navigation'.
  2507. * @type string $aria_label ARIA label text for the nav element. Default 'Posts'.
  2508. * @type string $class Custom class for the nav element. Default 'pagination'.
  2509. * }
  2510. * @return string Markup for pagination links.
  2511. */
  2512. function get_the_posts_pagination( $args = array() ) {
  2513. $navigation = '';
  2514. // Don't print empty markup if there's only one page.
  2515. if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
  2516. // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
  2517. if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
  2518. $args['aria_label'] = $args['screen_reader_text'];
  2519. }
  2520. $args = wp_parse_args(
  2521. $args,
  2522. array(
  2523. 'mid_size' => 1,
  2524. 'prev_text' => _x( 'Previous', 'previous set of posts' ),
  2525. 'next_text' => _x( 'Next', 'next set of posts' ),
  2526. 'screen_reader_text' => __( 'Posts navigation' ),
  2527. 'aria_label' => __( 'Posts' ),
  2528. 'class' => 'pagination',
  2529. )
  2530. );
  2531. // Make sure we get a string back. Plain is the next best thing.
  2532. if ( isset( $args['type'] ) && 'array' === $args['type'] ) {
  2533. $args['type'] = 'plain';
  2534. }
  2535. // Set up paginated links.
  2536. $links = paginate_links( $args );
  2537. if ( $links ) {
  2538. $navigation = _navigation_markup( $links, $args['class'], $args['screen_reader_text'], $args['aria_label'] );
  2539. }
  2540. }
  2541. return $navigation;
  2542. }
  2543. /**
  2544. * Displays a paginated navigation to next/previous set of posts, when applicable.
  2545. *
  2546. * @since 4.1.0
  2547. *
  2548. * @param array $args Optional. See get_the_posts_pagination() for available arguments.
  2549. * Default empty array.
  2550. */
  2551. function the_posts_pagination( $args = array() ) {
  2552. echo get_the_posts_pagination( $args );
  2553. }
  2554. /**
  2555. * Wraps passed links in navigational markup.
  2556. *
  2557. * @since 4.1.0
  2558. * @since 5.3.0 Added the `aria_label` parameter.
  2559. * @access private
  2560. *
  2561. * @param string $links Navigational links.
  2562. * @param string $class Optional. Custom class for the nav element.
  2563. * Default 'posts-navigation'.
  2564. * @param string $screen_reader_text Optional. Screen reader text for the nav element.
  2565. * Default 'Posts navigation'.
  2566. * @param string $aria_label Optional. ARIA label for the nav element.
  2567. * Defaults to the value of `$screen_reader_text`.
  2568. * @return string Navigation template tag.
  2569. */
  2570. function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '', $aria_label = '' ) {
  2571. if ( empty( $screen_reader_text ) ) {
  2572. $screen_reader_text = __( 'Posts navigation' );
  2573. }
  2574. if ( empty( $aria_label ) ) {
  2575. $aria_label = $screen_reader_text;
  2576. }
  2577. $template = '
  2578. <nav class="navigation %1$s" role="navigation" aria-label="%4$s">
  2579. <h2 class="screen-reader-text">%2$s</h2>
  2580. <div class="nav-links">%3$s</div>
  2581. </nav>';
  2582. /**
  2583. * Filters the navigation markup template.
  2584. *
  2585. * Note: The filtered template HTML must contain specifiers for the navigation
  2586. * class (%1$s), the screen-reader-text value (%2$s), placement of the navigation
  2587. * links (%3$s), and ARIA label text if screen-reader-text does not fit that (%4$s):
  2588. *
  2589. * <nav class="navigation %1$s" role="navigation" aria-label="%4$s">
  2590. * <h2 class="screen-reader-text">%2$s</h2>
  2591. * <div class="nav-links">%3$s</div>
  2592. * </nav>
  2593. *
  2594. * @since 4.4.0
  2595. *
  2596. * @param string $template The default template.
  2597. * @param string $class The class passed by the calling function.
  2598. * @return string Navigation template.
  2599. */
  2600. $template = apply_filters( 'navigation_markup_template', $template, $class );
  2601. return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links, esc_html( $aria_label ) );
  2602. }
  2603. /**
  2604. * Retrieves the comments page number link.
  2605. *
  2606. * @since 2.7.0
  2607. *
  2608. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  2609. *
  2610. * @param int $pagenum Optional. Page number. Default 1.
  2611. * @param int $max_page Optional. The maximum number of comment pages. Default 0.
  2612. * @return string The comments page number link URL.
  2613. */
  2614. function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
  2615. global $wp_rewrite;
  2616. $pagenum = (int) $pagenum;
  2617. $result = get_permalink();
  2618. if ( 'newest' === get_option( 'default_comments_page' ) ) {
  2619. if ( $pagenum != $max_page ) {
  2620. if ( $wp_rewrite->using_permalinks() ) {
  2621. $result = user_trailingslashit( trailingslashit( $result ) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged' );
  2622. } else {
  2623. $result = add_query_arg( 'cpage', $pagenum, $result );
  2624. }
  2625. }
  2626. } elseif ( $pagenum > 1 ) {
  2627. if ( $wp_rewrite->using_permalinks() ) {
  2628. $result = user_trailingslashit( trailingslashit( $result ) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged' );
  2629. } else {
  2630. $result = add_query_arg( 'cpage', $pagenum, $result );
  2631. }
  2632. }
  2633. $result .= '#comments';
  2634. /**
  2635. * Filters the comments page number link for the current request.
  2636. *
  2637. * @since 2.7.0
  2638. *
  2639. * @param string $result The comments page number link.
  2640. */
  2641. return apply_filters( 'get_comments_pagenum_link', $result );
  2642. }
  2643. /**
  2644. * Retrieves the link to the next comments page.
  2645. *
  2646. * @since 2.7.1
  2647. *
  2648. * @global WP_Query $wp_query WordPress Query object.
  2649. *
  2650. * @param string $label Optional. Label for link text. Default empty.
  2651. * @param int $max_page Optional. Max page. Default 0.
  2652. * @return string|void HTML-formatted link for the next page of comments.
  2653. */
  2654. function get_next_comments_link( $label = '', $max_page = 0 ) {
  2655. global $wp_query;
  2656. if ( ! is_singular() ) {
  2657. return;
  2658. }
  2659. $page = get_query_var( 'cpage' );
  2660. if ( ! $page ) {
  2661. $page = 1;
  2662. }
  2663. $nextpage = (int) $page + 1;
  2664. if ( empty( $max_page ) ) {
  2665. $max_page = $wp_query->max_num_comment_pages;
  2666. }
  2667. if ( empty( $max_page ) ) {
  2668. $max_page = get_comment_pages_count();
  2669. }
  2670. if ( $nextpage > $max_page ) {
  2671. return;
  2672. }
  2673. if ( empty( $label ) ) {
  2674. $label = __( 'Newer Comments &raquo;' );
  2675. }
  2676. /**
  2677. * Filters the anchor tag attributes for the next comments page link.
  2678. *
  2679. * @since 2.7.0
  2680. *
  2681. * @param string $attributes Attributes for the anchor tag.
  2682. */
  2683. return '<a href="' . esc_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>' . preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) . '</a>';
  2684. }
  2685. /**
  2686. * Displays the link to the next comments page.
  2687. *
  2688. * @since 2.7.0
  2689. *
  2690. * @param string $label Optional. Label for link text. Default empty.
  2691. * @param int $max_page Optional. Max page. Default 0.
  2692. */
  2693. function next_comments_link( $label = '', $max_page = 0 ) {
  2694. echo get_next_comments_link( $label, $max_page );
  2695. }
  2696. /**
  2697. * Retrieves the link to the previous comments page.
  2698. *
  2699. * @since 2.7.1
  2700. *
  2701. * @param string $label Optional. Label for comments link text. Default empty.
  2702. * @return string|void HTML-formatted link for the previous page of comments.
  2703. */
  2704. function get_previous_comments_link( $label = '' ) {
  2705. if ( ! is_singular() ) {
  2706. return;
  2707. }
  2708. $page = get_query_var( 'cpage' );
  2709. if ( (int) $page <= 1 ) {
  2710. return;
  2711. }
  2712. $prevpage = (int) $page - 1;
  2713. if ( empty( $label ) ) {
  2714. $label = __( '&laquo; Older Comments' );
  2715. }
  2716. /**
  2717. * Filters the anchor tag attributes for the previous comments page link.
  2718. *
  2719. * @since 2.7.0
  2720. *
  2721. * @param string $attributes Attributes for the anchor tag.
  2722. */
  2723. return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) . '</a>';
  2724. }
  2725. /**
  2726. * Displays the link to the previous comments page.
  2727. *
  2728. * @since 2.7.0
  2729. *
  2730. * @param string $label Optional. Label for comments link text. Default empty.
  2731. */
  2732. function previous_comments_link( $label = '' ) {
  2733. echo get_previous_comments_link( $label );
  2734. }
  2735. /**
  2736. * Displays or retrieves pagination links for the comments on the current post.
  2737. *
  2738. * @see paginate_links()
  2739. * @since 2.7.0
  2740. *
  2741. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  2742. *
  2743. * @param string|array $args Optional args. See paginate_links(). Default empty array.
  2744. * @return void|string|array Void if 'echo' argument is true and 'type' is not an array,
  2745. * or if the query is not for an existing single post of any post type.
  2746. * Otherwise, markup for comment page links or array of comment page links,
  2747. * depending on 'type' argument.
  2748. */
  2749. function paginate_comments_links( $args = array() ) {
  2750. global $wp_rewrite;
  2751. if ( ! is_singular() ) {
  2752. return;
  2753. }
  2754. $page = get_query_var( 'cpage' );
  2755. if ( ! $page ) {
  2756. $page = 1;
  2757. }
  2758. $max_page = get_comment_pages_count();
  2759. $defaults = array(
  2760. 'base' => add_query_arg( 'cpage', '%#%' ),
  2761. 'format' => '',
  2762. 'total' => $max_page,
  2763. 'current' => $page,
  2764. 'echo' => true,
  2765. 'type' => 'plain',
  2766. 'add_fragment' => '#comments',
  2767. );
  2768. if ( $wp_rewrite->using_permalinks() ) {
  2769. $defaults['base'] = user_trailingslashit( trailingslashit( get_permalink() ) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged' );
  2770. }
  2771. $args = wp_parse_args( $args, $defaults );
  2772. $page_links = paginate_links( $args );
  2773. if ( $args['echo'] && 'array' !== $args['type'] ) {
  2774. echo $page_links;
  2775. } else {
  2776. return $page_links;
  2777. }
  2778. }
  2779. /**
  2780. * Retrieves navigation to next/previous set of comments, when applicable.
  2781. *
  2782. * @since 4.4.0
  2783. * @since 5.3.0 Added the `aria_label` parameter.
  2784. * @since 5.5.0 Added the `class` parameter.
  2785. *
  2786. * @param array $args {
  2787. * Optional. Default comments navigation arguments.
  2788. *
  2789. * @type string $prev_text Anchor text to display in the previous comments link.
  2790. * Default 'Older comments'.
  2791. * @type string $next_text Anchor text to display in the next comments link.
  2792. * Default 'Newer comments'.
  2793. * @type string $screen_reader_text Screen reader text for the nav element. Default 'Comments navigation'.
  2794. * @type string $aria_label ARIA label text for the nav element. Default 'Comments'.
  2795. * @type string $class Custom class for the nav element. Default 'comment-navigation'.
  2796. * }
  2797. * @return string Markup for comments links.
  2798. */
  2799. function get_the_comments_navigation( $args = array() ) {
  2800. $navigation = '';
  2801. // Are there comments to navigate through?
  2802. if ( get_comment_pages_count() > 1 ) {
  2803. // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
  2804. if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
  2805. $args['aria_label'] = $args['screen_reader_text'];
  2806. }
  2807. $args = wp_parse_args(
  2808. $args,
  2809. array(
  2810. 'prev_text' => __( 'Older comments' ),
  2811. 'next_text' => __( 'Newer comments' ),
  2812. 'screen_reader_text' => __( 'Comments navigation' ),
  2813. 'aria_label' => __( 'Comments' ),
  2814. 'class' => 'comment-navigation',
  2815. )
  2816. );
  2817. $prev_link = get_previous_comments_link( $args['prev_text'] );
  2818. $next_link = get_next_comments_link( $args['next_text'] );
  2819. if ( $prev_link ) {
  2820. $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
  2821. }
  2822. if ( $next_link ) {
  2823. $navigation .= '<div class="nav-next">' . $next_link . '</div>';
  2824. }
  2825. $navigation = _navigation_markup( $navigation, $args['class'], $args['screen_reader_text'], $args['aria_label'] );
  2826. }
  2827. return $navigation;
  2828. }
  2829. /**
  2830. * Displays navigation to next/previous set of comments, when applicable.
  2831. *
  2832. * @since 4.4.0
  2833. *
  2834. * @param array $args See get_the_comments_navigation() for available arguments. Default empty array.
  2835. */
  2836. function the_comments_navigation( $args = array() ) {
  2837. echo get_the_comments_navigation( $args );
  2838. }
  2839. /**
  2840. * Retrieves a paginated navigation to next/previous set of comments, when applicable.
  2841. *
  2842. * @since 4.4.0
  2843. * @since 5.3.0 Added the `aria_label` parameter.
  2844. * @since 5.5.0 Added the `class` parameter.
  2845. *
  2846. * @see paginate_comments_links()
  2847. *
  2848. * @param array $args {
  2849. * Optional. Default pagination arguments.
  2850. *
  2851. * @type string $screen_reader_text Screen reader text for the nav element. Default 'Comments navigation'.
  2852. * @type string $aria_label ARIA label text for the nav element. Default 'Comments'.
  2853. * @type string $class Custom class for the nav element. Default 'comments-pagination'.
  2854. * }
  2855. * @return string Markup for pagination links.
  2856. */
  2857. function get_the_comments_pagination( $args = array() ) {
  2858. $navigation = '';
  2859. // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
  2860. if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
  2861. $args['aria_label'] = $args['screen_reader_text'];
  2862. }
  2863. $args = wp_parse_args(
  2864. $args,
  2865. array(
  2866. 'screen_reader_text' => __( 'Comments navigation' ),
  2867. 'aria_label' => __( 'Comments' ),
  2868. 'class' => 'comments-pagination',
  2869. )
  2870. );
  2871. $args['echo'] = false;
  2872. // Make sure we get a string back. Plain is the next best thing.
  2873. if ( isset( $args['type'] ) && 'array' === $args['type'] ) {
  2874. $args['type'] = 'plain';
  2875. }
  2876. $links = paginate_comments_links( $args );
  2877. if ( $links ) {
  2878. $navigation = _navigation_markup( $links, $args['class'], $args['screen_reader_text'], $args['aria_label'] );
  2879. }
  2880. return $navigation;
  2881. }
  2882. /**
  2883. * Displays a paginated navigation to next/previous set of comments, when applicable.
  2884. *
  2885. * @since 4.4.0
  2886. *
  2887. * @param array $args See get_the_comments_pagination() for available arguments. Default empty array.
  2888. */
  2889. function the_comments_pagination( $args = array() ) {
  2890. echo get_the_comments_pagination( $args );
  2891. }
  2892. /**
  2893. * Retrieves the URL for the current site where the front end is accessible.
  2894. *
  2895. * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
  2896. * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
  2897. * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
  2898. *
  2899. * @since 3.0.0
  2900. *
  2901. * @param string $path Optional. Path relative to the home URL. Default empty.
  2902. * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts
  2903. * 'http', 'https', 'relative', 'rest', or null. Default null.
  2904. * @return string Home URL link with optional path appended.
  2905. */
  2906. function home_url( $path = '', $scheme = null ) {
  2907. return get_home_url( null, $path, $scheme );
  2908. }
  2909. /**
  2910. * Retrieves the URL for a given site where the front end is accessible.
  2911. *
  2912. * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
  2913. * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
  2914. * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
  2915. *
  2916. * @since 3.0.0
  2917. *
  2918. * @param int|null $blog_id Optional. Site ID. Default null (current site).
  2919. * @param string $path Optional. Path relative to the home URL. Default empty.
  2920. * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts
  2921. * 'http', 'https', 'relative', 'rest', or null. Default null.
  2922. * @return string Home URL link with optional path appended.
  2923. */
  2924. function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
  2925. $orig_scheme = $scheme;
  2926. if ( empty( $blog_id ) || ! is_multisite() ) {
  2927. $url = get_option( 'home' );
  2928. } else {
  2929. switch_to_blog( $blog_id );
  2930. $url = get_option( 'home' );
  2931. restore_current_blog();
  2932. }
  2933. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ), true ) ) {
  2934. if ( is_ssl() ) {
  2935. $scheme = 'https';
  2936. } else {
  2937. $scheme = parse_url( $url, PHP_URL_SCHEME );
  2938. }
  2939. }
  2940. $url = set_url_scheme( $url, $scheme );
  2941. if ( $path && is_string( $path ) ) {
  2942. $url .= '/' . ltrim( $path, '/' );
  2943. }
  2944. /**
  2945. * Filters the home URL.
  2946. *
  2947. * @since 3.0.0
  2948. *
  2949. * @param string $url The complete home URL including scheme and path.
  2950. * @param string $path Path relative to the home URL. Blank string if no path is specified.
  2951. * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',
  2952. * 'relative', 'rest', or null.
  2953. * @param int|null $blog_id Site ID, or null for the current site.
  2954. */
  2955. return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
  2956. }
  2957. /**
  2958. * Retrieves the URL for the current site where WordPress application files
  2959. * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
  2960. *
  2961. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  2962. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  2963. * overridden.
  2964. *
  2965. * @since 3.0.0
  2966. *
  2967. * @param string $path Optional. Path relative to the site URL. Default empty.
  2968. * @param string|null $scheme Optional. Scheme to give the site URL context. See set_url_scheme().
  2969. * @return string Site URL link with optional path appended.
  2970. */
  2971. function site_url( $path = '', $scheme = null ) {
  2972. return get_site_url( null, $path, $scheme );
  2973. }
  2974. /**
  2975. * Retrieves the URL for a given site where WordPress application files
  2976. * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
  2977. *
  2978. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  2979. * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https',
  2980. * `is_ssl()` is overridden.
  2981. *
  2982. * @since 3.0.0
  2983. *
  2984. * @param int|null $blog_id Optional. Site ID. Default null (current site).
  2985. * @param string $path Optional. Path relative to the site URL. Default empty.
  2986. * @param string|null $scheme Optional. Scheme to give the site URL context. Accepts
  2987. * 'http', 'https', 'login', 'login_post', 'admin', or
  2988. * 'relative'. Default null.
  2989. * @return string Site URL link with optional path appended.
  2990. */
  2991. function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
  2992. if ( empty( $blog_id ) || ! is_multisite() ) {
  2993. $url = get_option( 'siteurl' );
  2994. } else {
  2995. switch_to_blog( $blog_id );
  2996. $url = get_option( 'siteurl' );
  2997. restore_current_blog();
  2998. }
  2999. $url = set_url_scheme( $url, $scheme );
  3000. if ( $path && is_string( $path ) ) {
  3001. $url .= '/' . ltrim( $path, '/' );
  3002. }
  3003. /**
  3004. * Filters the site URL.
  3005. *
  3006. * @since 2.7.0
  3007. *
  3008. * @param string $url The complete site URL including scheme and path.
  3009. * @param string $path Path relative to the site URL. Blank string if no path is specified.
  3010. * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
  3011. * 'login_post', 'admin', 'relative' or null.
  3012. * @param int|null $blog_id Site ID, or null for the current site.
  3013. */
  3014. return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
  3015. }
  3016. /**
  3017. * Retrieves the URL to the admin area for the current site.
  3018. *
  3019. * @since 2.6.0
  3020. *
  3021. * @param string $path Optional. Path relative to the admin URL. Default 'admin'.
  3022. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
  3023. * 'http' or 'https' can be passed to force those schemes.
  3024. * @return string Admin URL link with optional path appended.
  3025. */
  3026. function admin_url( $path = '', $scheme = 'admin' ) {
  3027. return get_admin_url( null, $path, $scheme );
  3028. }
  3029. /**
  3030. * Retrieves the URL to the admin area for a given site.
  3031. *
  3032. * @since 3.0.0
  3033. *
  3034. * @param int|null $blog_id Optional. Site ID. Default null (current site).
  3035. * @param string $path Optional. Path relative to the admin URL. Default empty.
  3036. * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https',
  3037. * to force those schemes. Default 'admin', which obeys
  3038. * force_ssl_admin() and is_ssl().
  3039. * @return string Admin URL link with optional path appended.
  3040. */
  3041. function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
  3042. $url = get_site_url( $blog_id, 'wp-admin/', $scheme );
  3043. if ( $path && is_string( $path ) ) {
  3044. $url .= ltrim( $path, '/' );
  3045. }
  3046. /**
  3047. * Filters the admin area URL.
  3048. *
  3049. * @since 2.8.0
  3050. * @since 5.8.0 The `$scheme` parameter was added.
  3051. *
  3052. * @param string $url The complete admin area URL including scheme and path.
  3053. * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
  3054. * @param int|null $blog_id Site ID, or null for the current site.
  3055. * @param string|null $scheme The scheme to use. Accepts 'http', 'https',
  3056. * 'admin', or null. Default 'admin', which obeys force_ssl_admin() and is_ssl().
  3057. */
  3058. return apply_filters( 'admin_url', $url, $path, $blog_id, $scheme );
  3059. }
  3060. /**
  3061. * Retrieves the URL to the includes directory.
  3062. *
  3063. * @since 2.6.0
  3064. *
  3065. * @param string $path Optional. Path relative to the includes URL. Default empty.
  3066. * @param string|null $scheme Optional. Scheme to give the includes URL context. Accepts
  3067. * 'http', 'https', or 'relative'. Default null.
  3068. * @return string Includes URL link with optional path appended.
  3069. */
  3070. function includes_url( $path = '', $scheme = null ) {
  3071. $url = site_url( '/' . WPINC . '/', $scheme );
  3072. if ( $path && is_string( $path ) ) {
  3073. $url .= ltrim( $path, '/' );
  3074. }
  3075. /**
  3076. * Filters the URL to the includes directory.
  3077. *
  3078. * @since 2.8.0
  3079. * @since 5.8.0 The `$scheme` parameter was added.
  3080. *
  3081. * @param string $url The complete URL to the includes directory including scheme and path.
  3082. * @param string $path Path relative to the URL to the wp-includes directory. Blank string
  3083. * if no path is specified.
  3084. * @param string|null $scheme Scheme to give the includes URL context. Accepts
  3085. * 'http', 'https', 'relative', or null. Default null.
  3086. */
  3087. return apply_filters( 'includes_url', $url, $path, $scheme );
  3088. }
  3089. /**
  3090. * Retrieves the URL to the content directory.
  3091. *
  3092. * @since 2.6.0
  3093. *
  3094. * @param string $path Optional. Path relative to the content URL. Default empty.
  3095. * @return string Content URL link with optional path appended.
  3096. */
  3097. function content_url( $path = '' ) {
  3098. $url = set_url_scheme( WP_CONTENT_URL );
  3099. if ( $path && is_string( $path ) ) {
  3100. $url .= '/' . ltrim( $path, '/' );
  3101. }
  3102. /**
  3103. * Filters the URL to the content directory.
  3104. *
  3105. * @since 2.8.0
  3106. *
  3107. * @param string $url The complete URL to the content directory including scheme and path.
  3108. * @param string $path Path relative to the URL to the content directory. Blank string
  3109. * if no path is specified.
  3110. */
  3111. return apply_filters( 'content_url', $url, $path );
  3112. }
  3113. /**
  3114. * Retrieves a URL within the plugins or mu-plugins directory.
  3115. *
  3116. * Defaults to the plugins directory URL if no arguments are supplied.
  3117. *
  3118. * @since 2.6.0
  3119. *
  3120. * @param string $path Optional. Extra path appended to the end of the URL, including
  3121. * the relative directory if $plugin is supplied. Default empty.
  3122. * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
  3123. * The URL will be relative to its directory. Default empty.
  3124. * Typically this is done by passing `__FILE__` as the argument.
  3125. * @return string Plugins URL link with optional paths appended.
  3126. */
  3127. function plugins_url( $path = '', $plugin = '' ) {
  3128. $path = wp_normalize_path( $path );
  3129. $plugin = wp_normalize_path( $plugin );
  3130. $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
  3131. if ( ! empty( $plugin ) && 0 === strpos( $plugin, $mu_plugin_dir ) ) {
  3132. $url = WPMU_PLUGIN_URL;
  3133. } else {
  3134. $url = WP_PLUGIN_URL;
  3135. }
  3136. $url = set_url_scheme( $url );
  3137. if ( ! empty( $plugin ) && is_string( $plugin ) ) {
  3138. $folder = dirname( plugin_basename( $plugin ) );
  3139. if ( '.' !== $folder ) {
  3140. $url .= '/' . ltrim( $folder, '/' );
  3141. }
  3142. }
  3143. if ( $path && is_string( $path ) ) {
  3144. $url .= '/' . ltrim( $path, '/' );
  3145. }
  3146. /**
  3147. * Filters the URL to the plugins directory.
  3148. *
  3149. * @since 2.8.0
  3150. *
  3151. * @param string $url The complete URL to the plugins directory including scheme and path.
  3152. * @param string $path Path relative to the URL to the plugins directory. Blank string
  3153. * if no path is specified.
  3154. * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
  3155. * is specified.
  3156. */
  3157. return apply_filters( 'plugins_url', $url, $path, $plugin );
  3158. }
  3159. /**
  3160. * Retrieves the site URL for the current network.
  3161. *
  3162. * Returns the site URL with the appropriate protocol, 'https' if
  3163. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  3164. * overridden.
  3165. *
  3166. * @since 3.0.0
  3167. *
  3168. * @see set_url_scheme()
  3169. *
  3170. * @param string $path Optional. Path relative to the site URL. Default empty.
  3171. * @param string|null $scheme Optional. Scheme to give the site URL context. Accepts
  3172. * 'http', 'https', or 'relative'. Default null.
  3173. * @return string Site URL link with optional path appended.
  3174. */
  3175. function network_site_url( $path = '', $scheme = null ) {
  3176. if ( ! is_multisite() ) {
  3177. return site_url( $path, $scheme );
  3178. }
  3179. $current_network = get_network();
  3180. if ( 'relative' === $scheme ) {
  3181. $url = $current_network->path;
  3182. } else {
  3183. $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme );
  3184. }
  3185. if ( $path && is_string( $path ) ) {
  3186. $url .= ltrim( $path, '/' );
  3187. }
  3188. /**
  3189. * Filters the network site URL.
  3190. *
  3191. * @since 3.0.0
  3192. *
  3193. * @param string $url The complete network site URL including scheme and path.
  3194. * @param string $path Path relative to the network site URL. Blank string if
  3195. * no path is specified.
  3196. * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
  3197. * 'relative' or null.
  3198. */
  3199. return apply_filters( 'network_site_url', $url, $path, $scheme );
  3200. }
  3201. /**
  3202. * Retrieves the home URL for the current network.
  3203. *
  3204. * Returns the home URL with the appropriate protocol, 'https' is_ssl()
  3205. * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
  3206. * overridden.
  3207. *
  3208. * @since 3.0.0
  3209. *
  3210. * @param string $path Optional. Path relative to the home URL. Default empty.
  3211. * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts
  3212. * 'http', 'https', or 'relative'. Default null.
  3213. * @return string Home URL link with optional path appended.
  3214. */
  3215. function network_home_url( $path = '', $scheme = null ) {
  3216. if ( ! is_multisite() ) {
  3217. return home_url( $path, $scheme );
  3218. }
  3219. $current_network = get_network();
  3220. $orig_scheme = $scheme;
  3221. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ), true ) ) {
  3222. $scheme = is_ssl() ? 'https' : 'http';
  3223. }
  3224. if ( 'relative' === $scheme ) {
  3225. $url = $current_network->path;
  3226. } else {
  3227. $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme );
  3228. }
  3229. if ( $path && is_string( $path ) ) {
  3230. $url .= ltrim( $path, '/' );
  3231. }
  3232. /**
  3233. * Filters the network home URL.
  3234. *
  3235. * @since 3.0.0
  3236. *
  3237. * @param string $url The complete network home URL including scheme and path.
  3238. * @param string $path Path relative to the network home URL. Blank string
  3239. * if no path is specified.
  3240. * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
  3241. * 'relative' or null.
  3242. */
  3243. return apply_filters( 'network_home_url', $url, $path, $orig_scheme );
  3244. }
  3245. /**
  3246. * Retrieves the URL to the admin area for the network.
  3247. *
  3248. * @since 3.0.0
  3249. *
  3250. * @param string $path Optional path relative to the admin URL. Default empty.
  3251. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
  3252. * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  3253. * @return string Admin URL link with optional path appended.
  3254. */
  3255. function network_admin_url( $path = '', $scheme = 'admin' ) {
  3256. if ( ! is_multisite() ) {
  3257. return admin_url( $path, $scheme );
  3258. }
  3259. $url = network_site_url( 'wp-admin/network/', $scheme );
  3260. if ( $path && is_string( $path ) ) {
  3261. $url .= ltrim( $path, '/' );
  3262. }
  3263. /**
  3264. * Filters the network admin URL.
  3265. *
  3266. * @since 3.0.0
  3267. * @since 5.8.0 The `$scheme` parameter was added.
  3268. *
  3269. * @param string $url The complete network admin URL including scheme and path.
  3270. * @param string $path Path relative to the network admin URL. Blank string if
  3271. * no path is specified.
  3272. * @param string|null $scheme The scheme to use. Accepts 'http', 'https',
  3273. * 'admin', or null. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
  3274. */
  3275. return apply_filters( 'network_admin_url', $url, $path, $scheme );
  3276. }
  3277. /**
  3278. * Retrieves the URL to the admin area for the current user.
  3279. *
  3280. * @since 3.0.0
  3281. *
  3282. * @param string $path Optional. Path relative to the admin URL. Default empty.
  3283. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
  3284. * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  3285. * @return string Admin URL link with optional path appended.
  3286. */
  3287. function user_admin_url( $path = '', $scheme = 'admin' ) {
  3288. $url = network_site_url( 'wp-admin/user/', $scheme );
  3289. if ( $path && is_string( $path ) ) {
  3290. $url .= ltrim( $path, '/' );
  3291. }
  3292. /**
  3293. * Filters the user admin URL for the current user.
  3294. *
  3295. * @since 3.1.0
  3296. * @since 5.8.0 The `$scheme` parameter was added.
  3297. *
  3298. * @param string $url The complete URL including scheme and path.
  3299. * @param string $path Path relative to the URL. Blank string if
  3300. * no path is specified.
  3301. * @param string|null $scheme The scheme to use. Accepts 'http', 'https',
  3302. * 'admin', or null. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
  3303. */
  3304. return apply_filters( 'user_admin_url', $url, $path, $scheme );
  3305. }
  3306. /**
  3307. * Retrieves the URL to the admin area for either the current site or the network depending on context.
  3308. *
  3309. * @since 3.1.0
  3310. *
  3311. * @param string $path Optional. Path relative to the admin URL. Default empty.
  3312. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
  3313. * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  3314. * @return string Admin URL link with optional path appended.
  3315. */
  3316. function self_admin_url( $path = '', $scheme = 'admin' ) {
  3317. if ( is_network_admin() ) {
  3318. $url = network_admin_url( $path, $scheme );
  3319. } elseif ( is_user_admin() ) {
  3320. $url = user_admin_url( $path, $scheme );
  3321. } else {
  3322. $url = admin_url( $path, $scheme );
  3323. }
  3324. /**
  3325. * Filters the admin URL for the current site or network depending on context.
  3326. *
  3327. * @since 4.9.0
  3328. *
  3329. * @param string $url The complete URL including scheme and path.
  3330. * @param string $path Path relative to the URL. Blank string if no path is specified.
  3331. * @param string $scheme The scheme to use.
  3332. */
  3333. return apply_filters( 'self_admin_url', $url, $path, $scheme );
  3334. }
  3335. /**
  3336. * Sets the scheme for a URL.
  3337. *
  3338. * @since 3.4.0
  3339. * @since 4.4.0 The 'rest' scheme was added.
  3340. *
  3341. * @param string $url Absolute URL that includes a scheme
  3342. * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login',
  3343. * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null.
  3344. * @return string URL with chosen scheme.
  3345. */
  3346. function set_url_scheme( $url, $scheme = null ) {
  3347. $orig_scheme = $scheme;
  3348. if ( ! $scheme ) {
  3349. $scheme = is_ssl() ? 'https' : 'http';
  3350. } elseif ( 'admin' === $scheme || 'login' === $scheme || 'login_post' === $scheme || 'rpc' === $scheme ) {
  3351. $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
  3352. } elseif ( 'http' !== $scheme && 'https' !== $scheme && 'relative' !== $scheme ) {
  3353. $scheme = is_ssl() ? 'https' : 'http';
  3354. }
  3355. $url = trim( $url );
  3356. if ( substr( $url, 0, 2 ) === '//' ) {
  3357. $url = 'http:' . $url;
  3358. }
  3359. if ( 'relative' === $scheme ) {
  3360. $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
  3361. if ( '' !== $url && '/' === $url[0] ) {
  3362. $url = '/' . ltrim( $url, "/ \t\n\r\0\x0B" );
  3363. }
  3364. } else {
  3365. $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
  3366. }
  3367. /**
  3368. * Filters the resulting URL after setting the scheme.
  3369. *
  3370. * @since 3.4.0
  3371. *
  3372. * @param string $url The complete URL including scheme and path.
  3373. * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
  3374. * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
  3375. * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
  3376. */
  3377. return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
  3378. }
  3379. /**
  3380. * Retrieves the URL to the user's dashboard.
  3381. *
  3382. * If a user does not belong to any site, the global user dashboard is used. If the user
  3383. * belongs to the current site, the dashboard for the current site is returned. If the user
  3384. * cannot edit the current site, the dashboard to the user's primary site is returned.
  3385. *
  3386. * @since 3.1.0
  3387. *
  3388. * @param int $user_id Optional. User ID. Defaults to current user.
  3389. * @param string $path Optional path relative to the dashboard. Use only paths known to
  3390. * both site and user admins. Default empty.
  3391. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin()
  3392. * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  3393. * @return string Dashboard URL link with optional path appended.
  3394. */
  3395. function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
  3396. $user_id = $user_id ? (int) $user_id : get_current_user_id();
  3397. $blogs = get_blogs_of_user( $user_id );
  3398. if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs ) ) {
  3399. $url = user_admin_url( $path, $scheme );
  3400. } elseif ( ! is_multisite() ) {
  3401. $url = admin_url( $path, $scheme );
  3402. } else {
  3403. $current_blog = get_current_blog_id();
  3404. if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ), true ) ) ) {
  3405. $url = admin_url( $path, $scheme );
  3406. } else {
  3407. $active = get_active_blog_for_user( $user_id );
  3408. if ( $active ) {
  3409. $url = get_admin_url( $active->blog_id, $path, $scheme );
  3410. } else {
  3411. $url = user_admin_url( $path, $scheme );
  3412. }
  3413. }
  3414. }
  3415. /**
  3416. * Filters the dashboard URL for a user.
  3417. *
  3418. * @since 3.1.0
  3419. *
  3420. * @param string $url The complete URL including scheme and path.
  3421. * @param int $user_id The user ID.
  3422. * @param string $path Path relative to the URL. Blank string if no path is specified.
  3423. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
  3424. * 'login_post', 'admin', 'relative' or null.
  3425. */
  3426. return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme );
  3427. }
  3428. /**
  3429. * Retrieves the URL to the user's profile editor.
  3430. *
  3431. * @since 3.1.0
  3432. *
  3433. * @param int $user_id Optional. User ID. Defaults to current user.
  3434. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
  3435. * and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  3436. * @return string Dashboard URL link with optional path appended.
  3437. */
  3438. function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
  3439. $user_id = $user_id ? (int) $user_id : get_current_user_id();
  3440. if ( is_user_admin() ) {
  3441. $url = user_admin_url( 'profile.php', $scheme );
  3442. } elseif ( is_network_admin() ) {
  3443. $url = network_admin_url( 'profile.php', $scheme );
  3444. } else {
  3445. $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
  3446. }
  3447. /**
  3448. * Filters the URL for a user's profile editor.
  3449. *
  3450. * @since 3.1.0
  3451. *
  3452. * @param string $url The complete URL including scheme and path.
  3453. * @param int $user_id The user ID.
  3454. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
  3455. * 'login_post', 'admin', 'relative' or null.
  3456. */
  3457. return apply_filters( 'edit_profile_url', $url, $user_id, $scheme );
  3458. }
  3459. /**
  3460. * Returns the canonical URL for a post.
  3461. *
  3462. * When the post is the same as the current requested page the function will handle the
  3463. * pagination arguments too.
  3464. *
  3465. * @since 4.6.0
  3466. *
  3467. * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`.
  3468. * @return string|false The canonical URL, or false if the post does not exist or has not
  3469. * been published yet.
  3470. */
  3471. function wp_get_canonical_url( $post = null ) {
  3472. $post = get_post( $post );
  3473. if ( ! $post ) {
  3474. return false;
  3475. }
  3476. if ( 'publish' !== $post->post_status ) {
  3477. return false;
  3478. }
  3479. $canonical_url = get_permalink( $post );
  3480. // If a canonical is being generated for the current page, make sure it has pagination if needed.
  3481. if ( get_queried_object_id() === $post->ID ) {
  3482. $page = get_query_var( 'page', 0 );
  3483. if ( $page >= 2 ) {
  3484. if ( ! get_option( 'permalink_structure' ) ) {
  3485. $canonical_url = add_query_arg( 'page', $page, $canonical_url );
  3486. } else {
  3487. $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' );
  3488. }
  3489. }
  3490. $cpage = get_query_var( 'cpage', 0 );
  3491. if ( $cpage ) {
  3492. $canonical_url = get_comments_pagenum_link( $cpage );
  3493. }
  3494. }
  3495. /**
  3496. * Filters the canonical URL for a post.
  3497. *
  3498. * @since 4.6.0
  3499. *
  3500. * @param string $canonical_url The post's canonical URL.
  3501. * @param WP_Post $post Post object.
  3502. */
  3503. return apply_filters( 'get_canonical_url', $canonical_url, $post );
  3504. }
  3505. /**
  3506. * Outputs rel=canonical for singular queries.
  3507. *
  3508. * @since 2.9.0
  3509. * @since 4.6.0 Adjusted to use `wp_get_canonical_url()`.
  3510. */
  3511. function rel_canonical() {
  3512. if ( ! is_singular() ) {
  3513. return;
  3514. }
  3515. $id = get_queried_object_id();
  3516. if ( 0 === $id ) {
  3517. return;
  3518. }
  3519. $url = wp_get_canonical_url( $id );
  3520. if ( ! empty( $url ) ) {
  3521. echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
  3522. }
  3523. }
  3524. /**
  3525. * Returns a shortlink for a post, page, attachment, or site.
  3526. *
  3527. * This function exists to provide a shortlink tag that all themes and plugins can target.
  3528. * A plugin must hook in to provide the actual shortlinks. Default shortlink support is
  3529. * limited to providing ?p= style links for posts. Plugins can short-circuit this function
  3530. * via the {@see 'pre_get_shortlink'} filter or filter the output via the {@see 'get_shortlink'}
  3531. * filter.
  3532. *
  3533. * @since 3.0.0
  3534. *
  3535. * @param int $id Optional. A post or site ID. Default is 0, which means the current post or site.
  3536. * @param string $context Optional. Whether the ID is a 'site' id, 'post' id, or 'media' id. If 'post',
  3537. * the post_type of the post is consulted. If 'query', the current query is consulted
  3538. * to determine the ID and context. Default 'post'.
  3539. * @param bool $allow_slugs Optional. Whether to allow post slugs in the shortlink. It is up to the plugin how
  3540. * and whether to honor this. Default true.
  3541. * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks
  3542. * are not enabled.
  3543. */
  3544. function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
  3545. /**
  3546. * Filters whether to preempt generating a shortlink for the given post.
  3547. *
  3548. * Returning a truthy value from the filter will effectively short-circuit
  3549. * the shortlink generation process, returning that value instead.
  3550. *
  3551. * @since 3.0.0
  3552. *
  3553. * @param false|string $return Short-circuit return value. Either false or a URL string.
  3554. * @param int $id Post ID, or 0 for the current post.
  3555. * @param string $context The context for the link. One of 'post' or 'query',
  3556. * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
  3557. */
  3558. $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
  3559. if ( false !== $shortlink ) {
  3560. return $shortlink;
  3561. }
  3562. $post_id = 0;
  3563. if ( 'query' === $context && is_singular() ) {
  3564. $post_id = get_queried_object_id();
  3565. $post = get_post( $post_id );
  3566. } elseif ( 'post' === $context ) {
  3567. $post = get_post( $id );
  3568. if ( ! empty( $post->ID ) ) {
  3569. $post_id = $post->ID;
  3570. }
  3571. }
  3572. $shortlink = '';
  3573. // Return `?p=` link for all public post types.
  3574. if ( ! empty( $post_id ) ) {
  3575. $post_type = get_post_type_object( $post->post_type );
  3576. if ( 'page' === $post->post_type && get_option( 'page_on_front' ) == $post->ID && 'page' === get_option( 'show_on_front' ) ) {
  3577. $shortlink = home_url( '/' );
  3578. } elseif ( $post_type && $post_type->public ) {
  3579. $shortlink = home_url( '?p=' . $post_id );
  3580. }
  3581. }
  3582. /**
  3583. * Filters the shortlink for a post.
  3584. *
  3585. * @since 3.0.0
  3586. *
  3587. * @param string $shortlink Shortlink URL.
  3588. * @param int $id Post ID, or 0 for the current post.
  3589. * @param string $context The context for the link. One of 'post' or 'query',
  3590. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
  3591. */
  3592. return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
  3593. }
  3594. /**
  3595. * Injects rel=shortlink into the head if a shortlink is defined for the current page.
  3596. *
  3597. * Attached to the {@see 'wp_head'} action.
  3598. *
  3599. * @since 3.0.0
  3600. */
  3601. function wp_shortlink_wp_head() {
  3602. $shortlink = wp_get_shortlink( 0, 'query' );
  3603. if ( empty( $shortlink ) ) {
  3604. return;
  3605. }
  3606. echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
  3607. }
  3608. /**
  3609. * Sends a Link: rel=shortlink header if a shortlink is defined for the current page.
  3610. *
  3611. * Attached to the {@see 'wp'} action.
  3612. *
  3613. * @since 3.0.0
  3614. */
  3615. function wp_shortlink_header() {
  3616. if ( headers_sent() ) {
  3617. return;
  3618. }
  3619. $shortlink = wp_get_shortlink( 0, 'query' );
  3620. if ( empty( $shortlink ) ) {
  3621. return;
  3622. }
  3623. header( 'Link: <' . $shortlink . '>; rel=shortlink', false );
  3624. }
  3625. /**
  3626. * Displays the shortlink for a post.
  3627. *
  3628. * Must be called from inside "The Loop"
  3629. *
  3630. * Call like the_shortlink( __( 'Shortlinkage FTW' ) )
  3631. *
  3632. * @since 3.0.0
  3633. *
  3634. * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
  3635. * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
  3636. * @param string $before Optional HTML to display before the link. Default empty.
  3637. * @param string $after Optional HTML to display after the link. Default empty.
  3638. */
  3639. function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
  3640. $post = get_post();
  3641. if ( empty( $text ) ) {
  3642. $text = __( 'This is the short link.' );
  3643. }
  3644. if ( empty( $title ) ) {
  3645. $title = the_title_attribute( array( 'echo' => false ) );
  3646. }
  3647. $shortlink = wp_get_shortlink( $post->ID );
  3648. if ( ! empty( $shortlink ) ) {
  3649. $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
  3650. /**
  3651. * Filters the short link anchor tag for a post.
  3652. *
  3653. * @since 3.0.0
  3654. *
  3655. * @param string $link Shortlink anchor tag.
  3656. * @param string $shortlink Shortlink URL.
  3657. * @param string $text Shortlink's text.
  3658. * @param string $title Shortlink's title attribute.
  3659. */
  3660. $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
  3661. echo $before, $link, $after;
  3662. }
  3663. }
  3664. /**
  3665. * Retrieves the avatar URL.
  3666. *
  3667. * @since 4.2.0
  3668. *
  3669. * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
  3670. * user email, WP_User object, WP_Post object, or WP_Comment object.
  3671. * @param array $args {
  3672. * Optional. Arguments to return instead of the default arguments.
  3673. *
  3674. * @type int $size Height and width of the avatar in pixels. Default 96.
  3675. * @type string $default URL for the default image or a default type. Accepts '404' (return
  3676. * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
  3677. * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
  3678. * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
  3679. * 'gravatar_default' (the Gravatar logo). Default is the value of the
  3680. * 'avatar_default' option, with a fallback of 'mystery'.
  3681. * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
  3682. * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
  3683. * judged in that order. Default is the value of the 'avatar_rating' option.
  3684. * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
  3685. * Default null.
  3686. * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
  3687. * plus a "found_avatar" guess. Pass as a reference. Default null.
  3688. * }
  3689. * @return string|false The URL of the avatar on success, false on failure.
  3690. */
  3691. function get_avatar_url( $id_or_email, $args = null ) {
  3692. $args = get_avatar_data( $id_or_email, $args );
  3693. return $args['url'];
  3694. }
  3695. /**
  3696. * Check if this comment type allows avatars to be retrieved.
  3697. *
  3698. * @since 5.1.0
  3699. *
  3700. * @param string $comment_type Comment type to check.
  3701. * @return bool Whether the comment type is allowed for retrieving avatars.
  3702. */
  3703. function is_avatar_comment_type( $comment_type ) {
  3704. /**
  3705. * Filters the list of allowed comment types for retrieving avatars.
  3706. *
  3707. * @since 3.0.0
  3708. *
  3709. * @param array $types An array of content types. Default only contains 'comment'.
  3710. */
  3711. $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
  3712. return in_array( $comment_type, (array) $allowed_comment_types, true );
  3713. }
  3714. /**
  3715. * Retrieves default data about the avatar.
  3716. *
  3717. * @since 4.2.0
  3718. *
  3719. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
  3720. * user email, WP_User object, WP_Post object, or WP_Comment object.
  3721. * @param array $args {
  3722. * Optional. Arguments to return instead of the default arguments.
  3723. *
  3724. * @type int $size Height and width of the avatar image file in pixels. Default 96.
  3725. * @type int $height Display height of the avatar in pixels. Defaults to $size.
  3726. * @type int $width Display width of the avatar in pixels. Defaults to $size.
  3727. * @type string $default URL for the default image or a default type. Accepts '404' (return
  3728. * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
  3729. * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
  3730. * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
  3731. * 'gravatar_default' (the Gravatar logo). Default is the value of the
  3732. * 'avatar_default' option, with a fallback of 'mystery'.
  3733. * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
  3734. * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
  3735. * judged in that order. Default is the value of the 'avatar_rating' option.
  3736. * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
  3737. * Default null.
  3738. * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
  3739. * plus a "found_avatar" guess. Pass as a reference. Default null.
  3740. * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
  3741. * }
  3742. * @return array {
  3743. * Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
  3744. *
  3745. * @type bool $found_avatar True if we were able to find an avatar for this user,
  3746. * false or not set if we couldn't.
  3747. * @type string $url The URL of the avatar we found.
  3748. * }
  3749. */
  3750. function get_avatar_data( $id_or_email, $args = null ) {
  3751. $args = wp_parse_args(
  3752. $args,
  3753. array(
  3754. 'size' => 96,
  3755. 'height' => null,
  3756. 'width' => null,
  3757. 'default' => get_option( 'avatar_default', 'mystery' ),
  3758. 'force_default' => false,
  3759. 'rating' => get_option( 'avatar_rating' ),
  3760. 'scheme' => null,
  3761. 'processed_args' => null, // If used, should be a reference.
  3762. 'extra_attr' => '',
  3763. )
  3764. );
  3765. if ( is_numeric( $args['size'] ) ) {
  3766. $args['size'] = absint( $args['size'] );
  3767. if ( ! $args['size'] ) {
  3768. $args['size'] = 96;
  3769. }
  3770. } else {
  3771. $args['size'] = 96;
  3772. }
  3773. if ( is_numeric( $args['height'] ) ) {
  3774. $args['height'] = absint( $args['height'] );
  3775. if ( ! $args['height'] ) {
  3776. $args['height'] = $args['size'];
  3777. }
  3778. } else {
  3779. $args['height'] = $args['size'];
  3780. }
  3781. if ( is_numeric( $args['width'] ) ) {
  3782. $args['width'] = absint( $args['width'] );
  3783. if ( ! $args['width'] ) {
  3784. $args['width'] = $args['size'];
  3785. }
  3786. } else {
  3787. $args['width'] = $args['size'];
  3788. }
  3789. if ( empty( $args['default'] ) ) {
  3790. $args['default'] = get_option( 'avatar_default', 'mystery' );
  3791. }
  3792. switch ( $args['default'] ) {
  3793. case 'mm':
  3794. case 'mystery':
  3795. case 'mysteryman':
  3796. $args['default'] = 'mm';
  3797. break;
  3798. case 'gravatar_default':
  3799. $args['default'] = false;
  3800. break;
  3801. }
  3802. $args['force_default'] = (bool) $args['force_default'];
  3803. $args['rating'] = strtolower( $args['rating'] );
  3804. $args['found_avatar'] = false;
  3805. /**
  3806. * Filters whether to retrieve the avatar URL early.
  3807. *
  3808. * Passing a non-null value in the 'url' member of the return array will
  3809. * effectively short circuit get_avatar_data(), passing the value through
  3810. * the {@see 'get_avatar_data'} filter and returning early.
  3811. *
  3812. * @since 4.2.0
  3813. *
  3814. * @param array $args Arguments passed to get_avatar_data(), after processing.
  3815. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
  3816. * user email, WP_User object, WP_Post object, or WP_Comment object.
  3817. */
  3818. $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
  3819. if ( isset( $args['url'] ) ) {
  3820. /** This filter is documented in wp-includes/link-template.php */
  3821. return apply_filters( 'get_avatar_data', $args, $id_or_email );
  3822. }
  3823. $email_hash = '';
  3824. $user = false;
  3825. $email = false;
  3826. if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
  3827. $id_or_email = get_comment( $id_or_email );
  3828. }
  3829. // Process the user identifier.
  3830. if ( is_numeric( $id_or_email ) ) {
  3831. $user = get_user_by( 'id', absint( $id_or_email ) );
  3832. } elseif ( is_string( $id_or_email ) ) {
  3833. if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
  3834. // MD5 hash.
  3835. list( $email_hash ) = explode( '@', $id_or_email );
  3836. } else {
  3837. // Email address.
  3838. $email = $id_or_email;
  3839. }
  3840. } elseif ( $id_or_email instanceof WP_User ) {
  3841. // User object.
  3842. $user = $id_or_email;
  3843. } elseif ( $id_or_email instanceof WP_Post ) {
  3844. // Post object.
  3845. $user = get_user_by( 'id', (int) $id_or_email->post_author );
  3846. } elseif ( $id_or_email instanceof WP_Comment ) {
  3847. if ( ! is_avatar_comment_type( get_comment_type( $id_or_email ) ) ) {
  3848. $args['url'] = false;
  3849. /** This filter is documented in wp-includes/link-template.php */
  3850. return apply_filters( 'get_avatar_data', $args, $id_or_email );
  3851. }
  3852. if ( ! empty( $id_or_email->user_id ) ) {
  3853. $user = get_user_by( 'id', (int) $id_or_email->user_id );
  3854. }
  3855. if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
  3856. $email = $id_or_email->comment_author_email;
  3857. }
  3858. }
  3859. if ( ! $email_hash ) {
  3860. if ( $user ) {
  3861. $email = $user->user_email;
  3862. }
  3863. if ( $email ) {
  3864. $email_hash = md5( strtolower( trim( $email ) ) );
  3865. }
  3866. }
  3867. if ( $email_hash ) {
  3868. $args['found_avatar'] = true;
  3869. $gravatar_server = hexdec( $email_hash[0] ) % 3;
  3870. } else {
  3871. $gravatar_server = rand( 0, 2 );
  3872. }
  3873. $url_args = array(
  3874. 's' => $args['size'],
  3875. 'd' => $args['default'],
  3876. 'f' => $args['force_default'] ? 'y' : false,
  3877. 'r' => $args['rating'],
  3878. );
  3879. if ( is_ssl() ) {
  3880. $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
  3881. } else {
  3882. $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
  3883. }
  3884. $url = add_query_arg(
  3885. rawurlencode_deep( array_filter( $url_args ) ),
  3886. set_url_scheme( $url, $args['scheme'] )
  3887. );
  3888. /**
  3889. * Filters the avatar URL.
  3890. *
  3891. * @since 4.2.0
  3892. *
  3893. * @param string $url The URL of the avatar.
  3894. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
  3895. * user email, WP_User object, WP_Post object, or WP_Comment object.
  3896. * @param array $args Arguments passed to get_avatar_data(), after processing.
  3897. */
  3898. $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
  3899. /**
  3900. * Filters the avatar data.
  3901. *
  3902. * @since 4.2.0
  3903. *
  3904. * @param array $args Arguments passed to get_avatar_data(), after processing.
  3905. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
  3906. * user email, WP_User object, WP_Post object, or WP_Comment object.
  3907. */
  3908. return apply_filters( 'get_avatar_data', $args, $id_or_email );
  3909. }
  3910. /**
  3911. * Retrieves the URL of a file in the theme.
  3912. *
  3913. * Searches in the stylesheet directory before the template directory so themes
  3914. * which inherit from a parent theme can just override one file.
  3915. *
  3916. * @since 4.7.0
  3917. *
  3918. * @param string $file Optional. File to search for in the stylesheet directory.
  3919. * @return string The URL of the file.
  3920. */
  3921. function get_theme_file_uri( $file = '' ) {
  3922. $file = ltrim( $file, '/' );
  3923. if ( empty( $file ) ) {
  3924. $url = get_stylesheet_directory_uri();
  3925. } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
  3926. $url = get_stylesheet_directory_uri() . '/' . $file;
  3927. } else {
  3928. $url = get_template_directory_uri() . '/' . $file;
  3929. }
  3930. /**
  3931. * Filters the URL to a file in the theme.
  3932. *
  3933. * @since 4.7.0
  3934. *
  3935. * @param string $url The file URL.
  3936. * @param string $file The requested file to search for.
  3937. */
  3938. return apply_filters( 'theme_file_uri', $url, $file );
  3939. }
  3940. /**
  3941. * Retrieves the URL of a file in the parent theme.
  3942. *
  3943. * @since 4.7.0
  3944. *
  3945. * @param string $file Optional. File to return the URL for in the template directory.
  3946. * @return string The URL of the file.
  3947. */
  3948. function get_parent_theme_file_uri( $file = '' ) {
  3949. $file = ltrim( $file, '/' );
  3950. if ( empty( $file ) ) {
  3951. $url = get_template_directory_uri();
  3952. } else {
  3953. $url = get_template_directory_uri() . '/' . $file;
  3954. }
  3955. /**
  3956. * Filters the URL to a file in the parent theme.
  3957. *
  3958. * @since 4.7.0
  3959. *
  3960. * @param string $url The file URL.
  3961. * @param string $file The requested file to search for.
  3962. */
  3963. return apply_filters( 'parent_theme_file_uri', $url, $file );
  3964. }
  3965. /**
  3966. * Retrieves the path of a file in the theme.
  3967. *
  3968. * Searches in the stylesheet directory before the template directory so themes
  3969. * which inherit from a parent theme can just override one file.
  3970. *
  3971. * @since 4.7.0
  3972. *
  3973. * @param string $file Optional. File to search for in the stylesheet directory.
  3974. * @return string The path of the file.
  3975. */
  3976. function get_theme_file_path( $file = '' ) {
  3977. $file = ltrim( $file, '/' );
  3978. if ( empty( $file ) ) {
  3979. $path = get_stylesheet_directory();
  3980. } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
  3981. $path = get_stylesheet_directory() . '/' . $file;
  3982. } else {
  3983. $path = get_template_directory() . '/' . $file;
  3984. }
  3985. /**
  3986. * Filters the path to a file in the theme.
  3987. *
  3988. * @since 4.7.0
  3989. *
  3990. * @param string $path The file path.
  3991. * @param string $file The requested file to search for.
  3992. */
  3993. return apply_filters( 'theme_file_path', $path, $file );
  3994. }
  3995. /**
  3996. * Retrieves the path of a file in the parent theme.
  3997. *
  3998. * @since 4.7.0
  3999. *
  4000. * @param string $file Optional. File to return the path for in the template directory.
  4001. * @return string The path of the file.
  4002. */
  4003. function get_parent_theme_file_path( $file = '' ) {
  4004. $file = ltrim( $file, '/' );
  4005. if ( empty( $file ) ) {
  4006. $path = get_template_directory();
  4007. } else {
  4008. $path = get_template_directory() . '/' . $file;
  4009. }
  4010. /**
  4011. * Filters the path to a file in the parent theme.
  4012. *
  4013. * @since 4.7.0
  4014. *
  4015. * @param string $path The file path.
  4016. * @param string $file The requested file to search for.
  4017. */
  4018. return apply_filters( 'parent_theme_file_path', $path, $file );
  4019. }
  4020. /**
  4021. * Retrieves the URL to the privacy policy page.
  4022. *
  4023. * @since 4.9.6
  4024. *
  4025. * @return string The URL to the privacy policy page. Empty string if it doesn't exist.
  4026. */
  4027. function get_privacy_policy_url() {
  4028. $url = '';
  4029. $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
  4030. if ( ! empty( $policy_page_id ) && get_post_status( $policy_page_id ) === 'publish' ) {
  4031. $url = (string) get_permalink( $policy_page_id );
  4032. }
  4033. /**
  4034. * Filters the URL of the privacy policy page.
  4035. *
  4036. * @since 4.9.6
  4037. *
  4038. * @param string $url The URL to the privacy policy page. Empty string
  4039. * if it doesn't exist.
  4040. * @param int $policy_page_id The ID of privacy policy page.
  4041. */
  4042. return apply_filters( 'privacy_policy_url', $url, $policy_page_id );
  4043. }
  4044. /**
  4045. * Displays the privacy policy link with formatting, when applicable.
  4046. *
  4047. * @since 4.9.6
  4048. *
  4049. * @param string $before Optional. Display before privacy policy link. Default empty.
  4050. * @param string $after Optional. Display after privacy policy link. Default empty.
  4051. */
  4052. function the_privacy_policy_link( $before = '', $after = '' ) {
  4053. echo get_the_privacy_policy_link( $before, $after );
  4054. }
  4055. /**
  4056. * Returns the privacy policy link with formatting, when applicable.
  4057. *
  4058. * @since 4.9.6
  4059. *
  4060. * @param string $before Optional. Display before privacy policy link. Default empty.
  4061. * @param string $after Optional. Display after privacy policy link. Default empty.
  4062. * @return string Markup for the link and surrounding elements. Empty string if it
  4063. * doesn't exist.
  4064. */
  4065. function get_the_privacy_policy_link( $before = '', $after = '' ) {
  4066. $link = '';
  4067. $privacy_policy_url = get_privacy_policy_url();
  4068. $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
  4069. $page_title = ( $policy_page_id ) ? get_the_title( $policy_page_id ) : '';
  4070. if ( $privacy_policy_url && $page_title ) {
  4071. $link = sprintf(
  4072. '<a class="privacy-policy-link" href="%s">%s</a>',
  4073. esc_url( $privacy_policy_url ),
  4074. esc_html( $page_title )
  4075. );
  4076. }
  4077. /**
  4078. * Filters the privacy policy link.
  4079. *
  4080. * @since 4.9.6
  4081. *
  4082. * @param string $link The privacy policy link. Empty string if it
  4083. * doesn't exist.
  4084. * @param string $privacy_policy_url The URL of the privacy policy. Empty string
  4085. * if it doesn't exist.
  4086. */
  4087. $link = apply_filters( 'the_privacy_policy_link', $link, $privacy_policy_url );
  4088. if ( $link ) {
  4089. return $before . $link . $after;
  4090. }
  4091. return '';
  4092. }