link-template.php 140 KB

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