Няма описание

comment-template.php 93KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729
  1. <?php
  2. /**
  3. * Comment template functions
  4. *
  5. * These functions are meant to live inside of the WordPress loop.
  6. *
  7. * @package WordPress
  8. * @subpackage Template
  9. */
  10. /**
  11. * Retrieves the author of the current comment.
  12. *
  13. * If the comment has an empty comment_author field, then 'Anonymous' person is
  14. * assumed.
  15. *
  16. * @since 1.5.0
  17. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  18. *
  19. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to retrieve the author.
  20. * Default current comment.
  21. * @return string The comment author
  22. */
  23. function get_comment_author( $comment_ID = 0 ) {
  24. $comment = get_comment( $comment_ID );
  25. if ( empty( $comment->comment_author ) ) {
  26. $user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
  27. if ( $user ) {
  28. $author = $user->display_name;
  29. } else {
  30. $author = __( 'Anonymous' );
  31. }
  32. } else {
  33. $author = $comment->comment_author;
  34. }
  35. /**
  36. * Filters the returned comment author name.
  37. *
  38. * @since 1.5.0
  39. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  40. *
  41. * @param string $author The comment author's username.
  42. * @param int $comment_ID The comment ID.
  43. * @param WP_Comment $comment The comment object.
  44. */
  45. return apply_filters( 'get_comment_author', $author, $comment->comment_ID, $comment );
  46. }
  47. /**
  48. * Displays the author of the current comment.
  49. *
  50. * @since 0.71
  51. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  52. *
  53. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author.
  54. * Default current comment.
  55. */
  56. function comment_author( $comment_ID = 0 ) {
  57. $comment = get_comment( $comment_ID );
  58. $author = get_comment_author( $comment );
  59. /**
  60. * Filters the comment author's name for display.
  61. *
  62. * @since 1.2.0
  63. * @since 4.1.0 The `$comment_ID` parameter was added.
  64. *
  65. * @param string $author The comment author's username.
  66. * @param int $comment_ID The comment ID.
  67. */
  68. echo apply_filters( 'comment_author', $author, $comment->comment_ID );
  69. }
  70. /**
  71. * Retrieves the email of the author of the current comment.
  72. *
  73. * @since 1.5.0
  74. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  75. *
  76. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's email.
  77. * Default current comment.
  78. * @return string The current comment author's email
  79. */
  80. function get_comment_author_email( $comment_ID = 0 ) {
  81. $comment = get_comment( $comment_ID );
  82. /**
  83. * Filters the comment author's returned email address.
  84. *
  85. * @since 1.5.0
  86. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  87. *
  88. * @param string $comment_author_email The comment author's email address.
  89. * @param int $comment_ID The comment ID.
  90. * @param WP_Comment $comment The comment object.
  91. */
  92. return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment->comment_ID, $comment );
  93. }
  94. /**
  95. * Displays the email of the author of the current global $comment.
  96. *
  97. * Care should be taken to protect the email address and assure that email
  98. * harvesters do not capture your commenter's email address. Most assume that
  99. * their email address will not appear in raw form on the site. Doing so will
  100. * enable anyone, including those that people don't want to get the email
  101. * address and use it for their own means good and bad.
  102. *
  103. * @since 0.71
  104. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  105. *
  106. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's email.
  107. * Default current comment.
  108. */
  109. function comment_author_email( $comment_ID = 0 ) {
  110. $comment = get_comment( $comment_ID );
  111. $author_email = get_comment_author_email( $comment );
  112. /**
  113. * Filters the comment author's email for display.
  114. *
  115. * @since 1.2.0
  116. * @since 4.1.0 The `$comment_ID` parameter was added.
  117. *
  118. * @param string $author_email The comment author's email address.
  119. * @param int $comment_ID The comment ID.
  120. */
  121. echo apply_filters( 'author_email', $author_email, $comment->comment_ID );
  122. }
  123. /**
  124. * Displays the HTML email link to the author of the current comment.
  125. *
  126. * Care should be taken to protect the email address and assure that email
  127. * harvesters do not capture your commenter's email address. Most assume that
  128. * their email address will not appear in raw form on the site. Doing so will
  129. * enable anyone, including those that people don't want to get the email
  130. * address and use it for their own means good and bad.
  131. *
  132. * @since 0.71
  133. * @since 4.6.0 Added the `$comment` parameter.
  134. *
  135. * @param string $linktext Optional. Text to display instead of the comment author's email address.
  136. * Default empty.
  137. * @param string $before Optional. Text or HTML to display before the email link. Default empty.
  138. * @param string $after Optional. Text or HTML to display after the email link. Default empty.
  139. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment.
  140. */
  141. function comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) {
  142. $link = get_comment_author_email_link( $linktext, $before, $after, $comment );
  143. if ( $link ) {
  144. echo $link;
  145. }
  146. }
  147. /**
  148. * Returns the HTML email link to the author of the current comment.
  149. *
  150. * Care should be taken to protect the email address and assure that email
  151. * harvesters do not capture your commenter's email address. Most assume that
  152. * their email address will not appear in raw form on the site. Doing so will
  153. * enable anyone, including those that people don't want to get the email
  154. * address and use it for their own means good and bad.
  155. *
  156. * @since 2.7.0
  157. * @since 4.6.0 Added the `$comment` parameter.
  158. *
  159. * @param string $linktext Optional. Text to display instead of the comment author's email address.
  160. * Default empty.
  161. * @param string $before Optional. Text or HTML to display before the email link. Default empty.
  162. * @param string $after Optional. Text or HTML to display after the email link. Default empty.
  163. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment.
  164. * @return string HTML markup for the comment author email link. By default, the email address is obfuscated
  165. * via the {@see 'comment_email'} filter with antispambot().
  166. */
  167. function get_comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) {
  168. $comment = get_comment( $comment );
  169. /**
  170. * Filters the comment author's email for display.
  171. *
  172. * Care should be taken to protect the email address and assure that email
  173. * harvesters do not capture your commenter's email address.
  174. *
  175. * @since 1.2.0
  176. * @since 4.1.0 The `$comment` parameter was added.
  177. *
  178. * @param string $comment_author_email The comment author's email address.
  179. * @param WP_Comment $comment The comment object.
  180. */
  181. $email = apply_filters( 'comment_email', $comment->comment_author_email, $comment );
  182. if ( ( ! empty( $email ) ) && ( '@' !== $email ) ) {
  183. $display = ( '' !== $linktext ) ? $linktext : $email;
  184. $return = $before;
  185. $return .= sprintf( '<a href="%1$s">%2$s</a>', esc_url( 'mailto:' . $email ), esc_html( $display ) );
  186. $return .= $after;
  187. return $return;
  188. } else {
  189. return '';
  190. }
  191. }
  192. /**
  193. * Retrieves the HTML link to the URL of the author of the current comment.
  194. *
  195. * Both get_comment_author_url() and get_comment_author() rely on get_comment(),
  196. * which falls back to the global comment variable if the $comment_ID argument is empty.
  197. *
  198. * @since 1.5.0
  199. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  200. *
  201. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's link.
  202. * Default current comment.
  203. * @return string The comment author name or HTML link for author's URL.
  204. */
  205. function get_comment_author_link( $comment_ID = 0 ) {
  206. $comment = get_comment( $comment_ID );
  207. $url = get_comment_author_url( $comment );
  208. $author = get_comment_author( $comment );
  209. if ( empty( $url ) || 'http://' === $url ) {
  210. $return = $author;
  211. } else {
  212. $return = "<a href='$url' rel='external nofollow ugc' class='url'>$author</a>";
  213. }
  214. /**
  215. * Filters the comment author's link for display.
  216. *
  217. * @since 1.5.0
  218. * @since 4.1.0 The `$author` and `$comment_ID` parameters were added.
  219. *
  220. * @param string $return The HTML-formatted comment author link.
  221. * Empty for an invalid URL.
  222. * @param string $author The comment author's username.
  223. * @param int $comment_ID The comment ID.
  224. */
  225. return apply_filters( 'get_comment_author_link', $return, $author, $comment->comment_ID );
  226. }
  227. /**
  228. * Displays the HTML link to the URL of the author of the current comment.
  229. *
  230. * @since 0.71
  231. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  232. *
  233. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's link.
  234. * Default current comment.
  235. */
  236. function comment_author_link( $comment_ID = 0 ) {
  237. echo get_comment_author_link( $comment_ID );
  238. }
  239. /**
  240. * Retrieve the IP address of the author of the current comment.
  241. *
  242. * @since 1.5.0
  243. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  244. *
  245. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's IP address.
  246. * Default current comment.
  247. * @return string Comment author's IP address.
  248. */
  249. function get_comment_author_IP( $comment_ID = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  250. $comment = get_comment( $comment_ID );
  251. /**
  252. * Filters the comment author's returned IP address.
  253. *
  254. * @since 1.5.0
  255. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  256. *
  257. * @param string $comment_author_IP The comment author's IP address.
  258. * @param int $comment_ID The comment ID.
  259. * @param WP_Comment $comment The comment object.
  260. */
  261. return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment->comment_ID, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  262. }
  263. /**
  264. * Displays the IP address of the author of the current comment.
  265. *
  266. * @since 0.71
  267. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  268. *
  269. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's IP address.
  270. * Default current comment.
  271. */
  272. function comment_author_IP( $comment_ID = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  273. echo esc_html( get_comment_author_IP( $comment_ID ) );
  274. }
  275. /**
  276. * Retrieves the URL of the author of the current comment, not linked.
  277. *
  278. * @since 1.5.0
  279. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  280. *
  281. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's URL.
  282. * Default current comment.
  283. * @return string Comment author URL, if provided, an empty string otherwise.
  284. */
  285. function get_comment_author_url( $comment_ID = 0 ) {
  286. $comment = get_comment( $comment_ID );
  287. $url = '';
  288. $id = 0;
  289. if ( ! empty( $comment ) ) {
  290. $author_url = ( 'http://' === $comment->comment_author_url ) ? '' : $comment->comment_author_url;
  291. $url = esc_url( $author_url, array( 'http', 'https' ) );
  292. $id = $comment->comment_ID;
  293. }
  294. /**
  295. * Filters the comment author's URL.
  296. *
  297. * @since 1.5.0
  298. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  299. *
  300. * @param string $url The comment author's URL.
  301. * @param int $comment_ID The comment ID.
  302. * @param WP_Comment $comment The comment object.
  303. */
  304. return apply_filters( 'get_comment_author_url', $url, $id, $comment );
  305. }
  306. /**
  307. * Displays the URL of the author of the current comment, not linked.
  308. *
  309. * @since 0.71
  310. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  311. *
  312. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's URL.
  313. * Default current comment.
  314. */
  315. function comment_author_url( $comment_ID = 0 ) {
  316. $comment = get_comment( $comment_ID );
  317. $author_url = get_comment_author_url( $comment );
  318. /**
  319. * Filters the comment author's URL for display.
  320. *
  321. * @since 1.2.0
  322. * @since 4.1.0 The `$comment_ID` parameter was added.
  323. *
  324. * @param string $author_url The comment author's URL.
  325. * @param int $comment_ID The comment ID.
  326. */
  327. echo apply_filters( 'comment_url', $author_url, $comment->comment_ID );
  328. }
  329. /**
  330. * Retrieves the HTML link of the URL of the author of the current comment.
  331. *
  332. * $linktext parameter is only used if the URL does not exist for the comment
  333. * author. If the URL does exist then the URL will be used and the $linktext
  334. * will be ignored.
  335. *
  336. * Encapsulate the HTML link between the $before and $after. So it will appear
  337. * in the order of $before, link, and finally $after.
  338. *
  339. * @since 1.5.0
  340. * @since 4.6.0 Added the `$comment` parameter.
  341. *
  342. * @param string $linktext Optional. The text to display instead of the comment
  343. * author's email address. Default empty.
  344. * @param string $before Optional. The text or HTML to display before the email link.
  345. * Default empty.
  346. * @param string $after Optional. The text or HTML to display after the email link.
  347. * Default empty.
  348. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object.
  349. * Default is the current comment.
  350. * @return string The HTML link between the $before and $after parameters.
  351. */
  352. function get_comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) {
  353. $url = get_comment_author_url( $comment );
  354. $display = ( '' !== $linktext ) ? $linktext : $url;
  355. $display = str_replace( 'http://www.', '', $display );
  356. $display = str_replace( 'http://', '', $display );
  357. if ( '/' === substr( $display, -1 ) ) {
  358. $display = substr( $display, 0, -1 );
  359. }
  360. $return = "$before<a href='$url' rel='external'>$display</a>$after";
  361. /**
  362. * Filters the comment author's returned URL link.
  363. *
  364. * @since 1.5.0
  365. *
  366. * @param string $return The HTML-formatted comment author URL link.
  367. */
  368. return apply_filters( 'get_comment_author_url_link', $return );
  369. }
  370. /**
  371. * Displays the HTML link of the URL of the author of the current comment.
  372. *
  373. * @since 0.71
  374. * @since 4.6.0 Added the `$comment` parameter.
  375. *
  376. * @param string $linktext Optional. Text to display instead of the comment author's
  377. * email address. Default empty.
  378. * @param string $before Optional. Text or HTML to display before the email link.
  379. * Default empty.
  380. * @param string $after Optional. Text or HTML to display after the email link.
  381. * Default empty.
  382. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object.
  383. * Default is the current comment.
  384. */
  385. function comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) {
  386. echo get_comment_author_url_link( $linktext, $before, $after, $comment );
  387. }
  388. /**
  389. * Generates semantic classes for each comment element.
  390. *
  391. * @since 2.7.0
  392. * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
  393. *
  394. * @param string|string[] $class Optional. One or more classes to add to the class list.
  395. * Default empty.
  396. * @param int|WP_Comment $comment Comment ID or WP_Comment object. Default current comment.
  397. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  398. * @param bool $echo Optional. Whether to echo or return the output.
  399. * Default true.
  400. * @return void|string Void if `$echo` argument is true, comment classes if `$echo` is false.
  401. */
  402. function comment_class( $class = '', $comment = null, $post_id = null, $echo = true ) {
  403. // Separates classes with a single space, collates classes for comment DIV.
  404. $class = 'class="' . implode( ' ', get_comment_class( $class, $comment, $post_id ) ) . '"';
  405. if ( $echo ) {
  406. echo $class;
  407. } else {
  408. return $class;
  409. }
  410. }
  411. /**
  412. * Returns the classes for the comment div as an array.
  413. *
  414. * @since 2.7.0
  415. * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
  416. *
  417. * @global int $comment_alt
  418. * @global int $comment_depth
  419. * @global int $comment_thread_alt
  420. *
  421. * @param string|string[] $class Optional. One or more classes to add to the class list. Default empty.
  422. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. Default current comment.
  423. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  424. * @return string[] An array of classes.
  425. */
  426. function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
  427. global $comment_alt, $comment_depth, $comment_thread_alt;
  428. $classes = array();
  429. $comment = get_comment( $comment_id );
  430. if ( ! $comment ) {
  431. return $classes;
  432. }
  433. // Get the comment type (comment, trackback).
  434. $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
  435. // Add classes for comment authors that are registered users.
  436. $user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
  437. if ( $user ) {
  438. $classes[] = 'byuser';
  439. $classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id );
  440. // For comment authors who are the author of the post.
  441. $post = get_post( $post_id );
  442. if ( $post ) {
  443. if ( $comment->user_id === $post->post_author ) {
  444. $classes[] = 'bypostauthor';
  445. }
  446. }
  447. }
  448. if ( empty( $comment_alt ) ) {
  449. $comment_alt = 0;
  450. }
  451. if ( empty( $comment_depth ) ) {
  452. $comment_depth = 1;
  453. }
  454. if ( empty( $comment_thread_alt ) ) {
  455. $comment_thread_alt = 0;
  456. }
  457. if ( $comment_alt % 2 ) {
  458. $classes[] = 'odd';
  459. $classes[] = 'alt';
  460. } else {
  461. $classes[] = 'even';
  462. }
  463. $comment_alt++;
  464. // Alt for top-level comments.
  465. if ( 1 == $comment_depth ) {
  466. if ( $comment_thread_alt % 2 ) {
  467. $classes[] = 'thread-odd';
  468. $classes[] = 'thread-alt';
  469. } else {
  470. $classes[] = 'thread-even';
  471. }
  472. $comment_thread_alt++;
  473. }
  474. $classes[] = "depth-$comment_depth";
  475. if ( ! empty( $class ) ) {
  476. if ( ! is_array( $class ) ) {
  477. $class = preg_split( '#\s+#', $class );
  478. }
  479. $classes = array_merge( $classes, $class );
  480. }
  481. $classes = array_map( 'esc_attr', $classes );
  482. /**
  483. * Filters the returned CSS classes for the current comment.
  484. *
  485. * @since 2.7.0
  486. *
  487. * @param string[] $classes An array of comment classes.
  488. * @param string[] $class An array of additional classes added to the list.
  489. * @param int $comment_id The comment ID.
  490. * @param WP_Comment $comment The comment object.
  491. * @param int|WP_Post $post_id The post ID or WP_Post object.
  492. */
  493. return apply_filters( 'comment_class', $classes, $class, $comment->comment_ID, $comment, $post_id );
  494. }
  495. /**
  496. * Retrieves the comment date of the current comment.
  497. *
  498. * @since 1.5.0
  499. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  500. *
  501. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  502. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the date.
  503. * Default current comment.
  504. * @return string The comment's date.
  505. */
  506. function get_comment_date( $format = '', $comment_ID = 0 ) {
  507. $comment = get_comment( $comment_ID );
  508. $_format = ! empty( $format ) ? $format : get_option( 'date_format' );
  509. $date = mysql2date( $_format, $comment->comment_date );
  510. /**
  511. * Filters the returned comment date.
  512. *
  513. * @since 1.5.0
  514. *
  515. * @param string|int $date Formatted date string or Unix timestamp.
  516. * @param string $format PHP date format.
  517. * @param WP_Comment $comment The comment object.
  518. */
  519. return apply_filters( 'get_comment_date', $date, $format, $comment );
  520. }
  521. /**
  522. * Displays the comment date of the current comment.
  523. *
  524. * @since 0.71
  525. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  526. *
  527. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  528. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the date.
  529. * Default current comment.
  530. */
  531. function comment_date( $format = '', $comment_ID = 0 ) {
  532. echo get_comment_date( $format, $comment_ID );
  533. }
  534. /**
  535. * Retrieves the excerpt of the given comment.
  536. *
  537. * Returns a maximum of 20 words with an ellipsis appended if necessary.
  538. *
  539. * @since 1.5.0
  540. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  541. *
  542. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the excerpt.
  543. * Default current comment.
  544. * @return string The possibly truncated comment excerpt.
  545. */
  546. function get_comment_excerpt( $comment_ID = 0 ) {
  547. $comment = get_comment( $comment_ID );
  548. if ( ! post_password_required( $comment->comment_post_ID ) ) {
  549. $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
  550. } else {
  551. $comment_text = __( 'Password protected' );
  552. }
  553. /* translators: Maximum number of words used in a comment excerpt. */
  554. $comment_excerpt_length = (int) _x( '20', 'comment_excerpt_length' );
  555. /**
  556. * Filters the maximum number of words used in the comment excerpt.
  557. *
  558. * @since 4.4.0
  559. *
  560. * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
  561. */
  562. $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );
  563. $excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '&hellip;' );
  564. /**
  565. * Filters the retrieved comment excerpt.
  566. *
  567. * @since 1.5.0
  568. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  569. *
  570. * @param string $excerpt The comment excerpt text.
  571. * @param int $comment_ID The comment ID.
  572. * @param WP_Comment $comment The comment object.
  573. */
  574. return apply_filters( 'get_comment_excerpt', $excerpt, $comment->comment_ID, $comment );
  575. }
  576. /**
  577. * Displays the excerpt of the current comment.
  578. *
  579. * @since 1.2.0
  580. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  581. *
  582. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the excerpt.
  583. * Default current comment.
  584. */
  585. function comment_excerpt( $comment_ID = 0 ) {
  586. $comment = get_comment( $comment_ID );
  587. $comment_excerpt = get_comment_excerpt( $comment );
  588. /**
  589. * Filters the comment excerpt for display.
  590. *
  591. * @since 1.2.0
  592. * @since 4.1.0 The `$comment_ID` parameter was added.
  593. *
  594. * @param string $comment_excerpt The comment excerpt text.
  595. * @param int $comment_ID The comment ID.
  596. */
  597. echo apply_filters( 'comment_excerpt', $comment_excerpt, $comment->comment_ID );
  598. }
  599. /**
  600. * Retrieves the comment ID of the current comment.
  601. *
  602. * @since 1.5.0
  603. *
  604. * @return int The comment ID.
  605. */
  606. function get_comment_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  607. $comment = get_comment();
  608. /**
  609. * Filters the returned comment ID.
  610. *
  611. * @since 1.5.0
  612. * @since 4.1.0 The `$comment_ID` parameter was added.
  613. *
  614. * @param int $comment_ID The current comment ID.
  615. * @param WP_Comment $comment The comment object.
  616. */
  617. return apply_filters( 'get_comment_ID', $comment->comment_ID, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  618. }
  619. /**
  620. * Displays the comment ID of the current comment.
  621. *
  622. * @since 0.71
  623. */
  624. function comment_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  625. echo get_comment_ID();
  626. }
  627. /**
  628. * Retrieves the link to a given comment.
  629. *
  630. * @since 1.5.0
  631. * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. Added `$cpage` argument.
  632. *
  633. * @see get_page_of_comment()
  634. *
  635. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  636. * @global bool $in_comment_loop
  637. *
  638. * @param WP_Comment|int|null $comment Comment to retrieve. Default current comment.
  639. * @param array $args {
  640. * An array of optional arguments to override the defaults.
  641. *
  642. * @type string $type Passed to get_page_of_comment().
  643. * @type int $page Current page of comments, for calculating comment pagination.
  644. * @type int $per_page Per-page value for comment pagination.
  645. * @type int $max_depth Passed to get_page_of_comment().
  646. * @type int|string $cpage Value to use for the comment's "comment-page" or "cpage" value.
  647. * If provided, this value overrides any value calculated from `$page`
  648. * and `$per_page`.
  649. * }
  650. * @return string The permalink to the given comment.
  651. */
  652. function get_comment_link( $comment = null, $args = array() ) {
  653. global $wp_rewrite, $in_comment_loop;
  654. $comment = get_comment( $comment );
  655. // Back-compat.
  656. if ( ! is_array( $args ) ) {
  657. $args = array( 'page' => $args );
  658. }
  659. $defaults = array(
  660. 'type' => 'all',
  661. 'page' => '',
  662. 'per_page' => '',
  663. 'max_depth' => '',
  664. 'cpage' => null,
  665. );
  666. $args = wp_parse_args( $args, $defaults );
  667. $link = get_permalink( $comment->comment_post_ID );
  668. // The 'cpage' param takes precedence.
  669. if ( ! is_null( $args['cpage'] ) ) {
  670. $cpage = $args['cpage'];
  671. // No 'cpage' is provided, so we calculate one.
  672. } else {
  673. if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) {
  674. $args['per_page'] = get_option( 'comments_per_page' );
  675. }
  676. if ( empty( $args['per_page'] ) ) {
  677. $args['per_page'] = 0;
  678. $args['page'] = 0;
  679. }
  680. $cpage = $args['page'];
  681. if ( '' == $cpage ) {
  682. if ( ! empty( $in_comment_loop ) ) {
  683. $cpage = get_query_var( 'cpage' );
  684. } else {
  685. // Requires a database hit, so we only do it when we can't figure out from context.
  686. $cpage = get_page_of_comment( $comment->comment_ID, $args );
  687. }
  688. }
  689. /*
  690. * If the default page displays the oldest comments, the permalinks for comments on the default page
  691. * do not need a 'cpage' query var.
  692. */
  693. if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) {
  694. $cpage = '';
  695. }
  696. }
  697. if ( $cpage && get_option( 'page_comments' ) ) {
  698. if ( $wp_rewrite->using_permalinks() ) {
  699. if ( $cpage ) {
  700. $link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
  701. }
  702. $link = user_trailingslashit( $link, 'comment' );
  703. } elseif ( $cpage ) {
  704. $link = add_query_arg( 'cpage', $cpage, $link );
  705. }
  706. }
  707. if ( $wp_rewrite->using_permalinks() ) {
  708. $link = user_trailingslashit( $link, 'comment' );
  709. }
  710. $link = $link . '#comment-' . $comment->comment_ID;
  711. /**
  712. * Filters the returned single comment permalink.
  713. *
  714. * @since 2.8.0
  715. * @since 4.4.0 Added the `$cpage` parameter.
  716. *
  717. * @see get_page_of_comment()
  718. *
  719. * @param string $link The comment permalink with '#comment-$id' appended.
  720. * @param WP_Comment $comment The current comment object.
  721. * @param array $args An array of arguments to override the defaults.
  722. * @param int $cpage The calculated 'cpage' value.
  723. */
  724. return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage );
  725. }
  726. /**
  727. * Retrieves the link to the current post comments.
  728. *
  729. * @since 1.5.0
  730. *
  731. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
  732. * @return string The link to the comments.
  733. */
  734. function get_comments_link( $post_id = 0 ) {
  735. $hash = get_comments_number( $post_id ) ? '#comments' : '#respond';
  736. $comments_link = get_permalink( $post_id ) . $hash;
  737. /**
  738. * Filters the returned post comments permalink.
  739. *
  740. * @since 3.6.0
  741. *
  742. * @param string $comments_link Post comments permalink with '#comments' appended.
  743. * @param int|WP_Post $post_id Post ID or WP_Post object.
  744. */
  745. return apply_filters( 'get_comments_link', $comments_link, $post_id );
  746. }
  747. /**
  748. * Displays the link to the current post comments.
  749. *
  750. * @since 0.71
  751. *
  752. * @param string $deprecated Not Used.
  753. * @param string $deprecated_2 Not Used.
  754. */
  755. function comments_link( $deprecated = '', $deprecated_2 = '' ) {
  756. if ( ! empty( $deprecated ) ) {
  757. _deprecated_argument( __FUNCTION__, '0.72' );
  758. }
  759. if ( ! empty( $deprecated_2 ) ) {
  760. _deprecated_argument( __FUNCTION__, '1.3.0' );
  761. }
  762. echo esc_url( get_comments_link() );
  763. }
  764. /**
  765. * Retrieves the amount of comments a post has.
  766. *
  767. * @since 1.5.0
  768. *
  769. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
  770. * @return string|int If the post exists, a numeric string representing the number of comments
  771. * the post has, otherwise 0.
  772. */
  773. function get_comments_number( $post_id = 0 ) {
  774. $post = get_post( $post_id );
  775. if ( ! $post ) {
  776. $count = 0;
  777. } else {
  778. $count = $post->comment_count;
  779. $post_id = $post->ID;
  780. }
  781. /**
  782. * Filters the returned comment count for a post.
  783. *
  784. * @since 1.5.0
  785. *
  786. * @param string|int $count A string representing the number of comments a post has, otherwise 0.
  787. * @param int $post_id Post ID.
  788. */
  789. return apply_filters( 'get_comments_number', $count, $post_id );
  790. }
  791. /**
  792. * Displays the language string for the number of comments the current post has.
  793. *
  794. * @since 0.71
  795. * @since 5.4.0 The `$deprecated` parameter was changed to `$post_id`.
  796. *
  797. * @param string|false $zero Optional. Text for no comments. Default false.
  798. * @param string|false $one Optional. Text for one comment. Default false.
  799. * @param string|false $more Optional. Text for more than one comment. Default false.
  800. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
  801. */
  802. function comments_number( $zero = false, $one = false, $more = false, $post_id = 0 ) {
  803. echo get_comments_number_text( $zero, $one, $more, $post_id );
  804. }
  805. /**
  806. * Displays the language string for the number of comments the current post has.
  807. *
  808. * @since 4.0.0
  809. * @since 5.4.0 Added the `$post_id` parameter to allow using the function outside of the loop.
  810. *
  811. * @param string $zero Optional. Text for no comments. Default false.
  812. * @param string $one Optional. Text for one comment. Default false.
  813. * @param string $more Optional. Text for more than one comment. Default false.
  814. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
  815. * @return string Language string for the number of comments a post has.
  816. */
  817. function get_comments_number_text( $zero = false, $one = false, $more = false, $post_id = 0 ) {
  818. $number = get_comments_number( $post_id );
  819. if ( $number > 1 ) {
  820. if ( false === $more ) {
  821. /* translators: %s: Number of comments. */
  822. $output = sprintf( _n( '%s Comment', '%s Comments', $number ), number_format_i18n( $number ) );
  823. } else {
  824. // % Comments
  825. /*
  826. * translators: If comment number in your language requires declension,
  827. * translate this to 'on'. Do not translate into your own language.
  828. */
  829. if ( 'on' === _x( 'off', 'Comment number declension: on or off' ) ) {
  830. $text = preg_replace( '#<span class="screen-reader-text">.+?</span>#', '', $more );
  831. $text = preg_replace( '/&.+?;/', '', $text ); // Kill entities.
  832. $text = trim( strip_tags( $text ), '% ' );
  833. // Replace '% Comments' with a proper plural form.
  834. if ( $text && ! preg_match( '/[0-9]+/', $text ) && false !== strpos( $more, '%' ) ) {
  835. /* translators: %s: Number of comments. */
  836. $new_text = _n( '%s Comment', '%s Comments', $number );
  837. $new_text = trim( sprintf( $new_text, '' ) );
  838. $more = str_replace( $text, $new_text, $more );
  839. if ( false === strpos( $more, '%' ) ) {
  840. $more = '% ' . $more;
  841. }
  842. }
  843. }
  844. $output = str_replace( '%', number_format_i18n( $number ), $more );
  845. }
  846. } elseif ( 0 == $number ) {
  847. $output = ( false === $zero ) ? __( 'No Comments' ) : $zero;
  848. } else { // Must be one.
  849. $output = ( false === $one ) ? __( '1 Comment' ) : $one;
  850. }
  851. /**
  852. * Filters the comments count for display.
  853. *
  854. * @since 1.5.0
  855. *
  856. * @see _n()
  857. *
  858. * @param string $output A translatable string formatted based on whether the count
  859. * is equal to 0, 1, or 1+.
  860. * @param int $number The number of post comments.
  861. */
  862. return apply_filters( 'comments_number', $output, $number );
  863. }
  864. /**
  865. * Retrieves the text of the current comment.
  866. *
  867. * @since 1.5.0
  868. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  869. * @since 5.4.0 Added 'In reply to %s.' prefix to child comments in comments feed.
  870. *
  871. * @see Walker_Comment::comment()
  872. *
  873. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the text.
  874. * Default current comment.
  875. * @param array $args Optional. An array of arguments. Default empty array.
  876. * @return string The comment content.
  877. */
  878. function get_comment_text( $comment_ID = 0, $args = array() ) {
  879. $comment = get_comment( $comment_ID );
  880. $comment_content = $comment->comment_content;
  881. if ( is_comment_feed() && $comment->comment_parent ) {
  882. $parent = get_comment( $comment->comment_parent );
  883. if ( $parent ) {
  884. $parent_link = esc_url( get_comment_link( $parent ) );
  885. $name = get_comment_author( $parent );
  886. $comment_content = sprintf(
  887. /* translators: %s: Comment link. */
  888. ent2ncr( __( 'In reply to %s.' ) ),
  889. '<a href="' . $parent_link . '">' . $name . '</a>'
  890. ) . "\n\n" . $comment_content;
  891. }
  892. }
  893. /**
  894. * Filters the text of a comment.
  895. *
  896. * @since 1.5.0
  897. *
  898. * @see Walker_Comment::comment()
  899. *
  900. * @param string $comment_content Text of the comment.
  901. * @param WP_Comment $comment The comment object.
  902. * @param array $args An array of arguments.
  903. */
  904. return apply_filters( 'get_comment_text', $comment_content, $comment, $args );
  905. }
  906. /**
  907. * Displays the text of the current comment.
  908. *
  909. * @since 0.71
  910. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  911. *
  912. * @see Walker_Comment::comment()
  913. *
  914. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the text.
  915. * Default current comment.
  916. * @param array $args Optional. An array of arguments. Default empty array.
  917. */
  918. function comment_text( $comment_ID = 0, $args = array() ) {
  919. $comment = get_comment( $comment_ID );
  920. $comment_text = get_comment_text( $comment, $args );
  921. /**
  922. * Filters the text of a comment to be displayed.
  923. *
  924. * @since 1.2.0
  925. *
  926. * @see Walker_Comment::comment()
  927. *
  928. * @param string $comment_text Text of the current comment.
  929. * @param WP_Comment|null $comment The comment object. Null if not found.
  930. * @param array $args An array of arguments.
  931. */
  932. echo apply_filters( 'comment_text', $comment_text, $comment, $args );
  933. }
  934. /**
  935. * Retrieves the comment time of the current comment.
  936. *
  937. * @since 1.5.0
  938. *
  939. * @param string $format Optional. PHP time format. Defaults to the 'time_format' option.
  940. * @param bool $gmt Optional. Whether to use the GMT date. Default false.
  941. * @param bool $translate Optional. Whether to translate the time (for use in feeds).
  942. * Default true.
  943. * @return string The formatted time.
  944. */
  945. function get_comment_time( $format = '', $gmt = false, $translate = true ) {
  946. $comment = get_comment();
  947. $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
  948. $_format = ! empty( $format ) ? $format : get_option( 'time_format' );
  949. $date = mysql2date( $_format, $comment_date, $translate );
  950. /**
  951. * Filters the returned comment time.
  952. *
  953. * @since 1.5.0
  954. *
  955. * @param string|int $date The comment time, formatted as a date string or Unix timestamp.
  956. * @param string $format PHP date format.
  957. * @param bool $gmt Whether the GMT date is in use.
  958. * @param bool $translate Whether the time is translated.
  959. * @param WP_Comment $comment The comment object.
  960. */
  961. return apply_filters( 'get_comment_time', $date, $format, $gmt, $translate, $comment );
  962. }
  963. /**
  964. * Displays the comment time of the current comment.
  965. *
  966. * @since 0.71
  967. *
  968. * @param string $format Optional. PHP time format. Defaults to the 'time_format' option.
  969. */
  970. function comment_time( $format = '' ) {
  971. echo get_comment_time( $format );
  972. }
  973. /**
  974. * Retrieves the comment type of the current comment.
  975. *
  976. * @since 1.5.0
  977. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  978. *
  979. * @param int|WP_Comment $comment_ID Optional. WP_Comment or ID of the comment for which to get the type.
  980. * Default current comment.
  981. * @return string The comment type.
  982. */
  983. function get_comment_type( $comment_ID = 0 ) {
  984. $comment = get_comment( $comment_ID );
  985. if ( '' === $comment->comment_type ) {
  986. $comment->comment_type = 'comment';
  987. }
  988. /**
  989. * Filters the returned comment type.
  990. *
  991. * @since 1.5.0
  992. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  993. *
  994. * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'.
  995. * @param int $comment_ID The comment ID.
  996. * @param WP_Comment $comment The comment object.
  997. */
  998. return apply_filters( 'get_comment_type', $comment->comment_type, $comment->comment_ID, $comment );
  999. }
  1000. /**
  1001. * Displays the comment type of the current comment.
  1002. *
  1003. * @since 0.71
  1004. *
  1005. * @param string|false $commenttxt Optional. String to display for comment type. Default false.
  1006. * @param string|false $trackbacktxt Optional. String to display for trackback type. Default false.
  1007. * @param string|false $pingbacktxt Optional. String to display for pingback type. Default false.
  1008. */
  1009. function comment_type( $commenttxt = false, $trackbacktxt = false, $pingbacktxt = false ) {
  1010. if ( false === $commenttxt ) {
  1011. $commenttxt = _x( 'Comment', 'noun' );
  1012. }
  1013. if ( false === $trackbacktxt ) {
  1014. $trackbacktxt = __( 'Trackback' );
  1015. }
  1016. if ( false === $pingbacktxt ) {
  1017. $pingbacktxt = __( 'Pingback' );
  1018. }
  1019. $type = get_comment_type();
  1020. switch ( $type ) {
  1021. case 'trackback':
  1022. echo $trackbacktxt;
  1023. break;
  1024. case 'pingback':
  1025. echo $pingbacktxt;
  1026. break;
  1027. default:
  1028. echo $commenttxt;
  1029. }
  1030. }
  1031. /**
  1032. * Retrieves the current post's trackback URL.
  1033. *
  1034. * There is a check to see if permalink's have been enabled and if so, will
  1035. * retrieve the pretty path. If permalinks weren't enabled, the ID of the
  1036. * current post is used and appended to the correct page to go to.
  1037. *
  1038. * @since 1.5.0
  1039. *
  1040. * @return string The trackback URL after being filtered.
  1041. */
  1042. function get_trackback_url() {
  1043. if ( get_option( 'permalink_structure' ) ) {
  1044. $tb_url = trailingslashit( get_permalink() ) . user_trailingslashit( 'trackback', 'single_trackback' );
  1045. } else {
  1046. $tb_url = get_option( 'siteurl' ) . '/wp-trackback.php?p=' . get_the_ID();
  1047. }
  1048. /**
  1049. * Filters the returned trackback URL.
  1050. *
  1051. * @since 2.2.0
  1052. *
  1053. * @param string $tb_url The trackback URL.
  1054. */
  1055. return apply_filters( 'trackback_url', $tb_url );
  1056. }
  1057. /**
  1058. * Displays the current post's trackback URL.
  1059. *
  1060. * @since 0.71
  1061. *
  1062. * @param bool $deprecated_echo Not used.
  1063. * @return void|string Should only be used to echo the trackback URL, use get_trackback_url()
  1064. * for the result instead.
  1065. */
  1066. function trackback_url( $deprecated_echo = true ) {
  1067. if ( true !== $deprecated_echo ) {
  1068. _deprecated_argument(
  1069. __FUNCTION__,
  1070. '2.5.0',
  1071. sprintf(
  1072. /* translators: %s: get_trackback_url() */
  1073. __( 'Use %s instead if you do not want the value echoed.' ),
  1074. '<code>get_trackback_url()</code>'
  1075. )
  1076. );
  1077. }
  1078. if ( $deprecated_echo ) {
  1079. echo get_trackback_url();
  1080. } else {
  1081. return get_trackback_url();
  1082. }
  1083. }
  1084. /**
  1085. * Generates and displays the RDF for the trackback information of current post.
  1086. *
  1087. * Deprecated in 3.0.0, and restored in 3.0.1.
  1088. *
  1089. * @since 0.71
  1090. *
  1091. * @param int|string $deprecated Not used (Was $timezone = 0).
  1092. */
  1093. function trackback_rdf( $deprecated = '' ) {
  1094. if ( ! empty( $deprecated ) ) {
  1095. _deprecated_argument( __FUNCTION__, '2.5.0' );
  1096. }
  1097. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'W3C_Validator' ) ) {
  1098. return;
  1099. }
  1100. echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  1101. xmlns:dc="http://purl.org/dc/elements/1.1/"
  1102. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  1103. <rdf:Description rdf:about="';
  1104. the_permalink();
  1105. echo '"' . "\n";
  1106. echo ' dc:identifier="';
  1107. the_permalink();
  1108. echo '"' . "\n";
  1109. echo ' dc:title="' . str_replace( '--', '&#x2d;&#x2d;', wptexturize( strip_tags( get_the_title() ) ) ) . '"' . "\n";
  1110. echo ' trackback:ping="' . get_trackback_url() . '"' . " />\n";
  1111. echo '</rdf:RDF>';
  1112. }
  1113. /**
  1114. * Determines whether the current post is open for comments.
  1115. *
  1116. * For more information on this and similar theme functions, check out
  1117. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  1118. * Conditional Tags} article in the Theme Developer Handbook.
  1119. *
  1120. * @since 1.5.0
  1121. *
  1122. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  1123. * @return bool True if the comments are open.
  1124. */
  1125. function comments_open( $post_id = null ) {
  1126. $_post = get_post( $post_id );
  1127. $post_id = $_post ? $_post->ID : 0;
  1128. $open = ( 'open' === $_post->comment_status );
  1129. /**
  1130. * Filters whether the current post is open for comments.
  1131. *
  1132. * @since 2.5.0
  1133. *
  1134. * @param bool $open Whether the current post is open for comments.
  1135. * @param int $post_id The post ID.
  1136. */
  1137. return apply_filters( 'comments_open', $open, $post_id );
  1138. }
  1139. /**
  1140. * Determines whether the current post is open for pings.
  1141. *
  1142. * For more information on this and similar theme functions, check out
  1143. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  1144. * Conditional Tags} article in the Theme Developer Handbook.
  1145. *
  1146. * @since 1.5.0
  1147. *
  1148. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  1149. * @return bool True if pings are accepted
  1150. */
  1151. function pings_open( $post_id = null ) {
  1152. $_post = get_post( $post_id );
  1153. $post_id = $_post ? $_post->ID : 0;
  1154. $open = ( 'open' === $_post->ping_status );
  1155. /**
  1156. * Filters whether the current post is open for pings.
  1157. *
  1158. * @since 2.5.0
  1159. *
  1160. * @param bool $open Whether the current post is open for pings.
  1161. * @param int $post_id The post ID.
  1162. */
  1163. return apply_filters( 'pings_open', $open, $post_id );
  1164. }
  1165. /**
  1166. * Displays form token for unfiltered comments.
  1167. *
  1168. * Will only display nonce token if the current user has permissions for
  1169. * unfiltered html. Won't display the token for other users.
  1170. *
  1171. * The function was backported to 2.0.10 and was added to versions 2.1.3 and
  1172. * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
  1173. * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
  1174. *
  1175. * Backported to 2.0.10.
  1176. *
  1177. * @since 2.1.3
  1178. */
  1179. function wp_comment_form_unfiltered_html_nonce() {
  1180. $post = get_post();
  1181. $post_id = $post ? $post->ID : 0;
  1182. if ( current_user_can( 'unfiltered_html' ) ) {
  1183. wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );
  1184. echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n";
  1185. }
  1186. }
  1187. /**
  1188. * Loads the comment template specified in $file.
  1189. *
  1190. * Will not display the comments template if not on single post or page, or if
  1191. * the post does not have comments.
  1192. *
  1193. * Uses the WordPress database object to query for the comments. The comments
  1194. * are passed through the {@see 'comments_array'} filter hook with the list of comments
  1195. * and the post ID respectively.
  1196. *
  1197. * The `$file` path is passed through a filter hook called {@see 'comments_template'},
  1198. * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
  1199. * first and if it fails it will require the default comment template from the
  1200. * default theme. If either does not exist, then the WordPress process will be
  1201. * halted. It is advised for that reason, that the default theme is not deleted.
  1202. *
  1203. * Will not try to get the comments if the post has none.
  1204. *
  1205. * @since 1.5.0
  1206. *
  1207. * @global WP_Query $wp_query WordPress Query object.
  1208. * @global WP_Post $post Global post object.
  1209. * @global wpdb $wpdb WordPress database abstraction object.
  1210. * @global int $id
  1211. * @global WP_Comment $comment Global comment object.
  1212. * @global string $user_login
  1213. * @global string $user_identity
  1214. * @global bool $overridden_cpage
  1215. * @global bool $withcomments
  1216. *
  1217. * @param string $file Optional. The file to load. Default '/comments.php'.
  1218. * @param bool $separate_comments Optional. Whether to separate the comments by comment type.
  1219. * Default false.
  1220. */
  1221. function comments_template( $file = '/comments.php', $separate_comments = false ) {
  1222. global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage;
  1223. if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) {
  1224. return;
  1225. }
  1226. if ( empty( $file ) ) {
  1227. $file = '/comments.php';
  1228. }
  1229. $req = get_option( 'require_name_email' );
  1230. /*
  1231. * Comment author information fetched from the comment cookies.
  1232. */
  1233. $commenter = wp_get_current_commenter();
  1234. /*
  1235. * The name of the current comment author escaped for use in attributes.
  1236. * Escaped by sanitize_comment_cookies().
  1237. */
  1238. $comment_author = $commenter['comment_author'];
  1239. /*
  1240. * The email address of the current comment author escaped for use in attributes.
  1241. * Escaped by sanitize_comment_cookies().
  1242. */
  1243. $comment_author_email = $commenter['comment_author_email'];
  1244. /*
  1245. * The URL of the current comment author escaped for use in attributes.
  1246. */
  1247. $comment_author_url = esc_url( $commenter['comment_author_url'] );
  1248. $comment_args = array(
  1249. 'orderby' => 'comment_date_gmt',
  1250. 'order' => 'ASC',
  1251. 'status' => 'approve',
  1252. 'post_id' => $post->ID,
  1253. 'no_found_rows' => false,
  1254. 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
  1255. );
  1256. if ( get_option( 'thread_comments' ) ) {
  1257. $comment_args['hierarchical'] = 'threaded';
  1258. } else {
  1259. $comment_args['hierarchical'] = false;
  1260. }
  1261. if ( is_user_logged_in() ) {
  1262. $comment_args['include_unapproved'] = array( get_current_user_id() );
  1263. } else {
  1264. $unapproved_email = wp_get_unapproved_comment_author_email();
  1265. if ( $unapproved_email ) {
  1266. $comment_args['include_unapproved'] = array( $unapproved_email );
  1267. }
  1268. }
  1269. $per_page = 0;
  1270. if ( get_option( 'page_comments' ) ) {
  1271. $per_page = (int) get_query_var( 'comments_per_page' );
  1272. if ( 0 === $per_page ) {
  1273. $per_page = (int) get_option( 'comments_per_page' );
  1274. }
  1275. $comment_args['number'] = $per_page;
  1276. $page = (int) get_query_var( 'cpage' );
  1277. if ( $page ) {
  1278. $comment_args['offset'] = ( $page - 1 ) * $per_page;
  1279. } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) {
  1280. $comment_args['offset'] = 0;
  1281. } else {
  1282. // If fetching the first page of 'newest', we need a top-level comment count.
  1283. $top_level_query = new WP_Comment_Query();
  1284. $top_level_args = array(
  1285. 'count' => true,
  1286. 'orderby' => false,
  1287. 'post_id' => $post->ID,
  1288. 'status' => 'approve',
  1289. );
  1290. if ( $comment_args['hierarchical'] ) {
  1291. $top_level_args['parent'] = 0;
  1292. }
  1293. if ( isset( $comment_args['include_unapproved'] ) ) {
  1294. $top_level_args['include_unapproved'] = $comment_args['include_unapproved'];
  1295. }
  1296. /**
  1297. * Filters the arguments used in the top level comments query.
  1298. *
  1299. * @since 5.6.0
  1300. *
  1301. * @see WP_Comment_Query::__construct()
  1302. *
  1303. * @param array $top_level_args {
  1304. * The top level query arguments for the comments template.
  1305. *
  1306. * @type bool $count Whether to return a comment count.
  1307. * @type string|array $orderby The field(s) to order by.
  1308. * @type int $post_id The post ID.
  1309. * @type string|array $status The comment status to limit results by.
  1310. * }
  1311. */
  1312. $top_level_args = apply_filters( 'comments_template_top_level_query_args', $top_level_args );
  1313. $top_level_count = $top_level_query->query( $top_level_args );
  1314. $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
  1315. }
  1316. }
  1317. /**
  1318. * Filters the arguments used to query comments in comments_template().
  1319. *
  1320. * @since 4.5.0
  1321. *
  1322. * @see WP_Comment_Query::__construct()
  1323. *
  1324. * @param array $comment_args {
  1325. * Array of WP_Comment_Query arguments.
  1326. *
  1327. * @type string|array $orderby Field(s) to order by.
  1328. * @type string $order Order of results. Accepts 'ASC' or 'DESC'.
  1329. * @type string $status Comment status.
  1330. * @type array $include_unapproved Array of IDs or email addresses whose unapproved comments
  1331. * will be included in results.
  1332. * @type int $post_id ID of the post.
  1333. * @type bool $no_found_rows Whether to refrain from querying for found rows.
  1334. * @type bool $update_comment_meta_cache Whether to prime cache for comment meta.
  1335. * @type bool|string $hierarchical Whether to query for comments hierarchically.
  1336. * @type int $offset Comment offset.
  1337. * @type int $number Number of comments to fetch.
  1338. * }
  1339. */
  1340. $comment_args = apply_filters( 'comments_template_query_args', $comment_args );
  1341. $comment_query = new WP_Comment_Query( $comment_args );
  1342. $_comments = $comment_query->comments;
  1343. // Trees must be flattened before they're passed to the walker.
  1344. if ( $comment_args['hierarchical'] ) {
  1345. $comments_flat = array();
  1346. foreach ( $_comments as $_comment ) {
  1347. $comments_flat[] = $_comment;
  1348. $comment_children = $_comment->get_children(
  1349. array(
  1350. 'format' => 'flat',
  1351. 'status' => $comment_args['status'],
  1352. 'orderby' => $comment_args['orderby'],
  1353. )
  1354. );
  1355. foreach ( $comment_children as $comment_child ) {
  1356. $comments_flat[] = $comment_child;
  1357. }
  1358. }
  1359. } else {
  1360. $comments_flat = $_comments;
  1361. }
  1362. /**
  1363. * Filters the comments array.
  1364. *
  1365. * @since 2.1.0
  1366. *
  1367. * @param array $comments Array of comments supplied to the comments template.
  1368. * @param int $post_ID Post ID.
  1369. */
  1370. $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID );
  1371. $comments = &$wp_query->comments;
  1372. $wp_query->comment_count = count( $wp_query->comments );
  1373. $wp_query->max_num_comment_pages = $comment_query->max_num_pages;
  1374. if ( $separate_comments ) {
  1375. $wp_query->comments_by_type = separate_comments( $comments );
  1376. $comments_by_type = &$wp_query->comments_by_type;
  1377. } else {
  1378. $wp_query->comments_by_type = array();
  1379. }
  1380. $overridden_cpage = false;
  1381. if ( '' == get_query_var( 'cpage' ) && $wp_query->max_num_comment_pages > 1 ) {
  1382. set_query_var( 'cpage', 'newest' === get_option( 'default_comments_page' ) ? get_comment_pages_count() : 1 );
  1383. $overridden_cpage = true;
  1384. }
  1385. if ( ! defined( 'COMMENTS_TEMPLATE' ) ) {
  1386. define( 'COMMENTS_TEMPLATE', true );
  1387. }
  1388. $theme_template = STYLESHEETPATH . $file;
  1389. /**
  1390. * Filters the path to the theme template file used for the comments template.
  1391. *
  1392. * @since 1.5.1
  1393. *
  1394. * @param string $theme_template The path to the theme template file.
  1395. */
  1396. $include = apply_filters( 'comments_template', $theme_template );
  1397. if ( file_exists( $include ) ) {
  1398. require $include;
  1399. } elseif ( file_exists( TEMPLATEPATH . $file ) ) {
  1400. require TEMPLATEPATH . $file;
  1401. } else { // Backward compat code will be removed in a future release.
  1402. require ABSPATH . WPINC . '/theme-compat/comments.php';
  1403. }
  1404. }
  1405. /**
  1406. * Displays the link to the comments for the current post ID.
  1407. *
  1408. * @since 0.71
  1409. *
  1410. * @param false|string $zero Optional. String to display when no comments. Default false.
  1411. * @param false|string $one Optional. String to display when only one comment is available. Default false.
  1412. * @param false|string $more Optional. String to display when there are more than one comment. Default false.
  1413. * @param string $css_class Optional. CSS class to use for comments. Default empty.
  1414. * @param false|string $none Optional. String to display when comments have been turned off. Default false.
  1415. */
  1416. function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
  1417. $post_id = get_the_ID();
  1418. $post_title = get_the_title();
  1419. $number = get_comments_number( $post_id );
  1420. if ( false === $zero ) {
  1421. /* translators: %s: Post title. */
  1422. $zero = sprintf( __( 'No Comments<span class="screen-reader-text"> on %s</span>' ), $post_title );
  1423. }
  1424. if ( false === $one ) {
  1425. /* translators: %s: Post title. */
  1426. $one = sprintf( __( '1 Comment<span class="screen-reader-text"> on %s</span>' ), $post_title );
  1427. }
  1428. if ( false === $more ) {
  1429. /* translators: 1: Number of comments, 2: Post title. */
  1430. $more = _n( '%1$s Comment<span class="screen-reader-text"> on %2$s</span>', '%1$s Comments<span class="screen-reader-text"> on %2$s</span>', $number );
  1431. $more = sprintf( $more, number_format_i18n( $number ), $post_title );
  1432. }
  1433. if ( false === $none ) {
  1434. /* translators: %s: Post title. */
  1435. $none = sprintf( __( 'Comments Off<span class="screen-reader-text"> on %s</span>' ), $post_title );
  1436. }
  1437. if ( 0 == $number && ! comments_open() && ! pings_open() ) {
  1438. echo '<span' . ( ( ! empty( $css_class ) ) ? ' class="' . esc_attr( $css_class ) . '"' : '' ) . '>' . $none . '</span>';
  1439. return;
  1440. }
  1441. if ( post_password_required() ) {
  1442. _e( 'Enter your password to view comments.' );
  1443. return;
  1444. }
  1445. echo '<a href="';
  1446. if ( 0 == $number ) {
  1447. $respond_link = get_permalink() . '#respond';
  1448. /**
  1449. * Filters the respond link when a post has no comments.
  1450. *
  1451. * @since 4.4.0
  1452. *
  1453. * @param string $respond_link The default response link.
  1454. * @param int $post_id The post ID.
  1455. */
  1456. echo apply_filters( 'respond_link', $respond_link, $post_id );
  1457. } else {
  1458. comments_link();
  1459. }
  1460. echo '"';
  1461. if ( ! empty( $css_class ) ) {
  1462. echo ' class="' . $css_class . '" ';
  1463. }
  1464. $attributes = '';
  1465. /**
  1466. * Filters the comments link attributes for display.
  1467. *
  1468. * @since 2.5.0
  1469. *
  1470. * @param string $attributes The comments link attributes. Default empty.
  1471. */
  1472. echo apply_filters( 'comments_popup_link_attributes', $attributes );
  1473. echo '>';
  1474. comments_number( $zero, $one, $more );
  1475. echo '</a>';
  1476. }
  1477. /**
  1478. * Retrieves HTML content for reply to comment link.
  1479. *
  1480. * @since 2.7.0
  1481. * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
  1482. *
  1483. * @param array $args {
  1484. * Optional. Override default arguments.
  1485. *
  1486. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1487. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1488. * concatenated as $add_below-$comment->comment_ID. Default 'comment'.
  1489. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1490. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1491. * Default 'respond'.
  1492. * @type string $reply_text The text of the Reply link. Default 'Reply'.
  1493. * @type string $login_text The text of the link to reply if logged out. Default 'Log in to Reply'.
  1494. * @type int $max_depth The max depth of the comment tree. Default 0.
  1495. * @type int $depth The depth of the new comment. Must be greater than 0 and less than the value
  1496. * of the 'thread_comments_depth' option set in Settings > Discussion. Default 0.
  1497. * @type string $before The text or HTML to add before the reply link. Default empty.
  1498. * @type string $after The text or HTML to add after the reply link. Default empty.
  1499. * }
  1500. * @param int|WP_Comment $comment Comment being replied to. Default current comment.
  1501. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1502. * Default current post.
  1503. * @return string|false|null Link to show comment form, if successful. False, if comments are closed.
  1504. */
  1505. function get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
  1506. $defaults = array(
  1507. 'add_below' => 'comment',
  1508. 'respond_id' => 'respond',
  1509. 'reply_text' => __( 'Reply' ),
  1510. /* translators: Comment reply button text. %s: Comment author name. */
  1511. 'reply_to_text' => __( 'Reply to %s' ),
  1512. 'login_text' => __( 'Log in to Reply' ),
  1513. 'max_depth' => 0,
  1514. 'depth' => 0,
  1515. 'before' => '',
  1516. 'after' => '',
  1517. );
  1518. $args = wp_parse_args( $args, $defaults );
  1519. if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] ) {
  1520. return;
  1521. }
  1522. $comment = get_comment( $comment );
  1523. if ( empty( $comment ) ) {
  1524. return;
  1525. }
  1526. if ( empty( $post ) ) {
  1527. $post = $comment->comment_post_ID;
  1528. }
  1529. $post = get_post( $post );
  1530. if ( ! comments_open( $post->ID ) ) {
  1531. return false;
  1532. }
  1533. if ( get_option( 'page_comments' ) ) {
  1534. $permalink = str_replace( '#comment-' . $comment->comment_ID, '', get_comment_link( $comment ) );
  1535. } else {
  1536. $permalink = get_permalink( $post->ID );
  1537. }
  1538. /**
  1539. * Filters the comment reply link arguments.
  1540. *
  1541. * @since 4.1.0
  1542. *
  1543. * @param array $args Comment reply link arguments. See get_comment_reply_link()
  1544. * for more information on accepted arguments.
  1545. * @param WP_Comment $comment The object of the comment being replied to.
  1546. * @param WP_Post $post The WP_Post object.
  1547. */
  1548. $args = apply_filters( 'comment_reply_link_args', $args, $comment, $post );
  1549. if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
  1550. $link = sprintf(
  1551. '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
  1552. esc_url( wp_login_url( get_permalink() ) ),
  1553. $args['login_text']
  1554. );
  1555. } else {
  1556. $data_attributes = array(
  1557. 'commentid' => $comment->comment_ID,
  1558. 'postid' => $post->ID,
  1559. 'belowelement' => $args['add_below'] . '-' . $comment->comment_ID,
  1560. 'respondelement' => $args['respond_id'],
  1561. 'replyto' => sprintf( $args['reply_to_text'], $comment->comment_author ),
  1562. );
  1563. $data_attribute_string = '';
  1564. foreach ( $data_attributes as $name => $value ) {
  1565. $data_attribute_string .= " data-${name}=\"" . esc_attr( $value ) . '"';
  1566. }
  1567. $data_attribute_string = trim( $data_attribute_string );
  1568. $link = sprintf(
  1569. "<a rel='nofollow' class='comment-reply-link' href='%s' %s aria-label='%s'>%s</a>",
  1570. esc_url(
  1571. add_query_arg(
  1572. array(
  1573. 'replytocom' => $comment->comment_ID,
  1574. 'unapproved' => false,
  1575. 'moderation-hash' => false,
  1576. ),
  1577. $permalink
  1578. )
  1579. ) . '#' . $args['respond_id'],
  1580. $data_attribute_string,
  1581. esc_attr( sprintf( $args['reply_to_text'], $comment->comment_author ) ),
  1582. $args['reply_text']
  1583. );
  1584. }
  1585. /**
  1586. * Filters the comment reply link.
  1587. *
  1588. * @since 2.7.0
  1589. *
  1590. * @param string $link The HTML markup for the comment reply link.
  1591. * @param array $args An array of arguments overriding the defaults.
  1592. * @param WP_Comment $comment The object of the comment being replied.
  1593. * @param WP_Post $post The WP_Post object.
  1594. */
  1595. return apply_filters( 'comment_reply_link', $args['before'] . $link . $args['after'], $args, $comment, $post );
  1596. }
  1597. /**
  1598. * Displays the HTML content for reply to comment link.
  1599. *
  1600. * @since 2.7.0
  1601. *
  1602. * @see get_comment_reply_link()
  1603. *
  1604. * @param array $args Optional. Override default options. Default empty array.
  1605. * @param int|WP_Comment $comment Comment being replied to. Default current comment.
  1606. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1607. * Default current post.
  1608. */
  1609. function comment_reply_link( $args = array(), $comment = null, $post = null ) {
  1610. echo get_comment_reply_link( $args, $comment, $post );
  1611. }
  1612. /**
  1613. * Retrieves HTML content for reply to post link.
  1614. *
  1615. * @since 2.7.0
  1616. *
  1617. * @param array $args {
  1618. * Optional. Override default arguments.
  1619. *
  1620. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1621. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1622. * concatenated as $add_below-$comment->comment_ID. Default is 'post'.
  1623. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1624. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1625. * Default 'respond'.
  1626. * @type string $reply_text Text of the Reply link. Default is 'Leave a Comment'.
  1627. * @type string $login_text Text of the link to reply if logged out. Default is 'Log in to leave a Comment'.
  1628. * @type string $before Text or HTML to add before the reply link. Default empty.
  1629. * @type string $after Text or HTML to add after the reply link. Default empty.
  1630. * }
  1631. * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on.
  1632. * Default current post.
  1633. * @return string|false|null Link to show comment form, if successful. False, if comments are closed.
  1634. */
  1635. function get_post_reply_link( $args = array(), $post = null ) {
  1636. $defaults = array(
  1637. 'add_below' => 'post',
  1638. 'respond_id' => 'respond',
  1639. 'reply_text' => __( 'Leave a Comment' ),
  1640. 'login_text' => __( 'Log in to leave a Comment' ),
  1641. 'before' => '',
  1642. 'after' => '',
  1643. );
  1644. $args = wp_parse_args( $args, $defaults );
  1645. $post = get_post( $post );
  1646. if ( ! comments_open( $post->ID ) ) {
  1647. return false;
  1648. }
  1649. if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
  1650. $link = sprintf(
  1651. '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
  1652. wp_login_url( get_permalink() ),
  1653. $args['login_text']
  1654. );
  1655. } else {
  1656. $onclick = sprintf(
  1657. 'return addComment.moveForm( "%1$s-%2$s", "0", "%3$s", "%2$s" )',
  1658. $args['add_below'],
  1659. $post->ID,
  1660. $args['respond_id']
  1661. );
  1662. $link = sprintf(
  1663. "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s'>%s</a>",
  1664. get_permalink( $post->ID ) . '#' . $args['respond_id'],
  1665. $onclick,
  1666. $args['reply_text']
  1667. );
  1668. }
  1669. $formatted_link = $args['before'] . $link . $args['after'];
  1670. /**
  1671. * Filters the formatted post comments link HTML.
  1672. *
  1673. * @since 2.7.0
  1674. *
  1675. * @param string $formatted The HTML-formatted post comments link.
  1676. * @param int|WP_Post $post The post ID or WP_Post object.
  1677. */
  1678. return apply_filters( 'post_comments_link', $formatted_link, $post );
  1679. }
  1680. /**
  1681. * Displays the HTML content for reply to post link.
  1682. *
  1683. * @since 2.7.0
  1684. *
  1685. * @see get_post_reply_link()
  1686. *
  1687. * @param array $args Optional. Override default options. Default empty array.
  1688. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1689. * Default current post.
  1690. */
  1691. function post_reply_link( $args = array(), $post = null ) {
  1692. echo get_post_reply_link( $args, $post );
  1693. }
  1694. /**
  1695. * Retrieves HTML content for cancel comment reply link.
  1696. *
  1697. * @since 2.7.0
  1698. *
  1699. * @param string $text Optional. Text to display for cancel reply link. If empty,
  1700. * defaults to 'Click here to cancel reply'. Default empty.
  1701. * @return string
  1702. */
  1703. function get_cancel_comment_reply_link( $text = '' ) {
  1704. if ( empty( $text ) ) {
  1705. $text = __( 'Click here to cancel reply.' );
  1706. }
  1707. $style = isset( $_GET['replytocom'] ) ? '' : ' style="display:none;"';
  1708. $link = esc_html( remove_query_arg( array( 'replytocom', 'unapproved', 'moderation-hash' ) ) ) . '#respond';
  1709. $formatted_link = '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>';
  1710. /**
  1711. * Filters the cancel comment reply link HTML.
  1712. *
  1713. * @since 2.7.0
  1714. *
  1715. * @param string $formatted_link The HTML-formatted cancel comment reply link.
  1716. * @param string $link Cancel comment reply link URL.
  1717. * @param string $text Cancel comment reply link text.
  1718. */
  1719. return apply_filters( 'cancel_comment_reply_link', $formatted_link, $link, $text );
  1720. }
  1721. /**
  1722. * Displays HTML content for cancel comment reply link.
  1723. *
  1724. * @since 2.7.0
  1725. *
  1726. * @param string $text Optional. Text to display for cancel reply link. If empty,
  1727. * defaults to 'Click here to cancel reply'. Default empty.
  1728. */
  1729. function cancel_comment_reply_link( $text = '' ) {
  1730. echo get_cancel_comment_reply_link( $text );
  1731. }
  1732. /**
  1733. * Retrieves hidden input HTML for replying to comments.
  1734. *
  1735. * @since 3.0.0
  1736. *
  1737. * @param int $post_id Optional. Post ID. Defaults to the current post ID.
  1738. * @return string Hidden input HTML for replying to comments.
  1739. */
  1740. function get_comment_id_fields( $post_id = 0 ) {
  1741. if ( empty( $post_id ) ) {
  1742. $post_id = get_the_ID();
  1743. }
  1744. $reply_to_id = isset( $_GET['replytocom'] ) ? (int) $_GET['replytocom'] : 0;
  1745. $result = "<input type='hidden' name='comment_post_ID' value='$post_id' id='comment_post_ID' />\n";
  1746. $result .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$reply_to_id' />\n";
  1747. /**
  1748. * Filters the returned comment ID fields.
  1749. *
  1750. * @since 3.0.0
  1751. *
  1752. * @param string $result The HTML-formatted hidden ID field comment elements.
  1753. * @param int $post_id The post ID.
  1754. * @param int $reply_to_id The ID of the comment being replied to.
  1755. */
  1756. return apply_filters( 'comment_id_fields', $result, $post_id, $reply_to_id );
  1757. }
  1758. /**
  1759. * Outputs hidden input HTML for replying to comments.
  1760. *
  1761. * Adds two hidden inputs to the comment form to identify the `comment_post_ID`
  1762. * and `comment_parent` values for threaded comments.
  1763. *
  1764. * This tag must be within the `<form>` section of the `comments.php` template.
  1765. *
  1766. * @since 2.7.0
  1767. *
  1768. * @see get_comment_id_fields()
  1769. *
  1770. * @param int $post_id Optional. Post ID. Defaults to the current post ID.
  1771. */
  1772. function comment_id_fields( $post_id = 0 ) {
  1773. echo get_comment_id_fields( $post_id );
  1774. }
  1775. /**
  1776. * Displays text based on comment reply status.
  1777. *
  1778. * Only affects users with JavaScript disabled.
  1779. *
  1780. * @internal The $comment global must be present to allow template tags access to the current
  1781. * comment. See https://core.trac.wordpress.org/changeset/36512.
  1782. *
  1783. * @since 2.7.0
  1784. *
  1785. * @global WP_Comment $comment Global comment object.
  1786. *
  1787. * @param string|false $no_reply_text Optional. Text to display when not replying to a comment.
  1788. * Default false.
  1789. * @param string|false $reply_text Optional. Text to display when replying to a comment.
  1790. * Default false. Accepts "%s" for the author of the comment
  1791. * being replied to.
  1792. * @param bool $link_to_parent Optional. Boolean to control making the author's name a link
  1793. * to their comment. Default true.
  1794. */
  1795. function comment_form_title( $no_reply_text = false, $reply_text = false, $link_to_parent = true ) {
  1796. global $comment;
  1797. if ( false === $no_reply_text ) {
  1798. $no_reply_text = __( 'Leave a Reply' );
  1799. }
  1800. if ( false === $reply_text ) {
  1801. /* translators: %s: Author of the comment being replied to. */
  1802. $reply_text = __( 'Leave a Reply to %s' );
  1803. }
  1804. $reply_to_id = isset( $_GET['replytocom'] ) ? (int) $_GET['replytocom'] : 0;
  1805. if ( 0 == $reply_to_id ) {
  1806. echo $no_reply_text;
  1807. } else {
  1808. // Sets the global so that template tags can be used in the comment form.
  1809. $comment = get_comment( $reply_to_id );
  1810. if ( $link_to_parent ) {
  1811. $author = '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author( $comment ) . '</a>';
  1812. } else {
  1813. $author = get_comment_author( $comment );
  1814. }
  1815. printf( $reply_text, $author );
  1816. }
  1817. }
  1818. /**
  1819. * Displays a list of comments.
  1820. *
  1821. * Used in the comments.php template to list comments for a particular post.
  1822. *
  1823. * @since 2.7.0
  1824. *
  1825. * @see WP_Query->comments
  1826. *
  1827. * @global WP_Query $wp_query WordPress Query object.
  1828. * @global int $comment_alt
  1829. * @global int $comment_depth
  1830. * @global int $comment_thread_alt
  1831. * @global bool $overridden_cpage
  1832. * @global bool $in_comment_loop
  1833. *
  1834. * @param string|array $args {
  1835. * Optional. Formatting options.
  1836. *
  1837. * @type object $walker Instance of a Walker class to list comments. Default null.
  1838. * @type int $max_depth The maximum comments depth. Default empty.
  1839. * @type string $style The style of list ordering. Accepts 'ul', 'ol', or 'div'.
  1840. * 'div' will result in no additional list markup. Default 'ul'.
  1841. * @type callable $callback Callback function to use. Default null.
  1842. * @type callable $end-callback Callback function to use at the end. Default null.
  1843. * @type string $type Type of comments to list. Accepts 'all', 'comment',
  1844. * 'pingback', 'trackback', 'pings'. Default 'all'.
  1845. * @type int $page Page ID to list comments for. Default empty.
  1846. * @type int $per_page Number of comments to list per page. Default empty.
  1847. * @type int $avatar_size Height and width dimensions of the avatar size. Default 32.
  1848. * @type bool $reverse_top_level Ordering of the listed comments. If true, will display
  1849. * newest comments first. Default null.
  1850. * @type bool $reverse_children Whether to reverse child comments in the list. Default null.
  1851. * @type string $format How to format the comments list. Accepts 'html5', 'xhtml'.
  1852. * Default 'html5' if the theme supports it.
  1853. * @type bool $short_ping Whether to output short pings. Default false.
  1854. * @type bool $echo Whether to echo the output or return it. Default true.
  1855. * }
  1856. * @param WP_Comment[] $comments Optional. Array of WP_Comment objects.
  1857. * @return void|string Void if 'echo' argument is true, or no comments to list.
  1858. * Otherwise, HTML list of comments.
  1859. */
  1860. function wp_list_comments( $args = array(), $comments = null ) {
  1861. global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
  1862. $in_comment_loop = true;
  1863. $comment_alt = 0;
  1864. $comment_thread_alt = 0;
  1865. $comment_depth = 1;
  1866. $defaults = array(
  1867. 'walker' => null,
  1868. 'max_depth' => '',
  1869. 'style' => 'ul',
  1870. 'callback' => null,
  1871. 'end-callback' => null,
  1872. 'type' => 'all',
  1873. 'page' => '',
  1874. 'per_page' => '',
  1875. 'avatar_size' => 32,
  1876. 'reverse_top_level' => null,
  1877. 'reverse_children' => '',
  1878. 'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',
  1879. 'short_ping' => false,
  1880. 'echo' => true,
  1881. );
  1882. $parsed_args = wp_parse_args( $args, $defaults );
  1883. /**
  1884. * Filters the arguments used in retrieving the comment list.
  1885. *
  1886. * @since 4.0.0
  1887. *
  1888. * @see wp_list_comments()
  1889. *
  1890. * @param array $parsed_args An array of arguments for displaying comments.
  1891. */
  1892. $parsed_args = apply_filters( 'wp_list_comments_args', $parsed_args );
  1893. // Figure out what comments we'll be looping through ($_comments).
  1894. if ( null !== $comments ) {
  1895. $comments = (array) $comments;
  1896. if ( empty( $comments ) ) {
  1897. return;
  1898. }
  1899. if ( 'all' !== $parsed_args['type'] ) {
  1900. $comments_by_type = separate_comments( $comments );
  1901. if ( empty( $comments_by_type[ $parsed_args['type'] ] ) ) {
  1902. return;
  1903. }
  1904. $_comments = $comments_by_type[ $parsed_args['type'] ];
  1905. } else {
  1906. $_comments = $comments;
  1907. }
  1908. } else {
  1909. /*
  1910. * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
  1911. * perform a separate comment query and allow Walker_Comment to paginate.
  1912. */
  1913. if ( $parsed_args['page'] || $parsed_args['per_page'] ) {
  1914. $current_cpage = get_query_var( 'cpage' );
  1915. if ( ! $current_cpage ) {
  1916. $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
  1917. }
  1918. $current_per_page = get_query_var( 'comments_per_page' );
  1919. if ( $parsed_args['page'] != $current_cpage || $parsed_args['per_page'] != $current_per_page ) {
  1920. $comment_args = array(
  1921. 'post_id' => get_the_ID(),
  1922. 'orderby' => 'comment_date_gmt',
  1923. 'order' => 'ASC',
  1924. 'status' => 'approve',
  1925. );
  1926. if ( is_user_logged_in() ) {
  1927. $comment_args['include_unapproved'] = array( get_current_user_id() );
  1928. } else {
  1929. $unapproved_email = wp_get_unapproved_comment_author_email();
  1930. if ( $unapproved_email ) {
  1931. $comment_args['include_unapproved'] = array( $unapproved_email );
  1932. }
  1933. }
  1934. $comments = get_comments( $comment_args );
  1935. if ( 'all' !== $parsed_args['type'] ) {
  1936. $comments_by_type = separate_comments( $comments );
  1937. if ( empty( $comments_by_type[ $parsed_args['type'] ] ) ) {
  1938. return;
  1939. }
  1940. $_comments = $comments_by_type[ $parsed_args['type'] ];
  1941. } else {
  1942. $_comments = $comments;
  1943. }
  1944. }
  1945. // Otherwise, fall back on the comments from `$wp_query->comments`.
  1946. } else {
  1947. if ( empty( $wp_query->comments ) ) {
  1948. return;
  1949. }
  1950. if ( 'all' !== $parsed_args['type'] ) {
  1951. if ( empty( $wp_query->comments_by_type ) ) {
  1952. $wp_query->comments_by_type = separate_comments( $wp_query->comments );
  1953. }
  1954. if ( empty( $wp_query->comments_by_type[ $parsed_args['type'] ] ) ) {
  1955. return;
  1956. }
  1957. $_comments = $wp_query->comments_by_type[ $parsed_args['type'] ];
  1958. } else {
  1959. $_comments = $wp_query->comments;
  1960. }
  1961. if ( $wp_query->max_num_comment_pages ) {
  1962. $default_comments_page = get_option( 'default_comments_page' );
  1963. $cpage = get_query_var( 'cpage' );
  1964. if ( 'newest' === $default_comments_page ) {
  1965. $parsed_args['cpage'] = $cpage;
  1966. /*
  1967. * When first page shows oldest comments, post permalink is the same as
  1968. * the comment permalink.
  1969. */
  1970. } elseif ( 1 == $cpage ) {
  1971. $parsed_args['cpage'] = '';
  1972. } else {
  1973. $parsed_args['cpage'] = $cpage;
  1974. }
  1975. $parsed_args['page'] = 0;
  1976. $parsed_args['per_page'] = 0;
  1977. }
  1978. }
  1979. }
  1980. if ( '' === $parsed_args['per_page'] && get_option( 'page_comments' ) ) {
  1981. $parsed_args['per_page'] = get_query_var( 'comments_per_page' );
  1982. }
  1983. if ( empty( $parsed_args['per_page'] ) ) {
  1984. $parsed_args['per_page'] = 0;
  1985. $parsed_args['page'] = 0;
  1986. }
  1987. if ( '' === $parsed_args['max_depth'] ) {
  1988. if ( get_option( 'thread_comments' ) ) {
  1989. $parsed_args['max_depth'] = get_option( 'thread_comments_depth' );
  1990. } else {
  1991. $parsed_args['max_depth'] = -1;
  1992. }
  1993. }
  1994. if ( '' === $parsed_args['page'] ) {
  1995. if ( empty( $overridden_cpage ) ) {
  1996. $parsed_args['page'] = get_query_var( 'cpage' );
  1997. } else {
  1998. $threaded = ( -1 != $parsed_args['max_depth'] );
  1999. $parsed_args['page'] = ( 'newest' === get_option( 'default_comments_page' ) ) ? get_comment_pages_count( $_comments, $parsed_args['per_page'], $threaded ) : 1;
  2000. set_query_var( 'cpage', $parsed_args['page'] );
  2001. }
  2002. }
  2003. // Validation check.
  2004. $parsed_args['page'] = (int) $parsed_args['page'];
  2005. if ( 0 == $parsed_args['page'] && 0 != $parsed_args['per_page'] ) {
  2006. $parsed_args['page'] = 1;
  2007. }
  2008. if ( null === $parsed_args['reverse_top_level'] ) {
  2009. $parsed_args['reverse_top_level'] = ( 'desc' === get_option( 'comment_order' ) );
  2010. }
  2011. wp_queue_comments_for_comment_meta_lazyload( $_comments );
  2012. if ( empty( $parsed_args['walker'] ) ) {
  2013. $walker = new Walker_Comment;
  2014. } else {
  2015. $walker = $parsed_args['walker'];
  2016. }
  2017. $output = $walker->paged_walk( $_comments, $parsed_args['max_depth'], $parsed_args['page'], $parsed_args['per_page'], $parsed_args );
  2018. $in_comment_loop = false;
  2019. if ( $parsed_args['echo'] ) {
  2020. echo $output;
  2021. } else {
  2022. return $output;
  2023. }
  2024. }
  2025. /**
  2026. * Outputs a complete commenting form for use within a template.
  2027. *
  2028. * Most strings and form fields may be controlled through the `$args` array passed
  2029. * into the function, while you may also choose to use the {@see 'comment_form_default_fields'}
  2030. * filter to modify the array of default fields if you'd just like to add a new
  2031. * one or remove a single field. All fields are also individually passed through
  2032. * a filter of the {@see 'comment_form_field_$name'} where `$name` is the key used
  2033. * in the array of fields.
  2034. *
  2035. * @since 3.0.0
  2036. * @since 4.1.0 Introduced the 'class_submit' argument.
  2037. * @since 4.2.0 Introduced the 'submit_button' and 'submit_fields' arguments.
  2038. * @since 4.4.0 Introduced the 'class_form', 'title_reply_before', 'title_reply_after',
  2039. * 'cancel_reply_before', and 'cancel_reply_after' arguments.
  2040. * @since 4.5.0 The 'author', 'email', and 'url' form fields are limited to 245, 100,
  2041. * and 200 characters, respectively.
  2042. * @since 4.6.0 Introduced the 'action' argument.
  2043. * @since 4.9.6 Introduced the 'cookies' default comment field.
  2044. * @since 5.5.0 Introduced the 'class_container' argument.
  2045. *
  2046. * @param array $args {
  2047. * Optional. Default arguments and form fields to override.
  2048. *
  2049. * @type array $fields {
  2050. * Default comment fields, filterable by default via the {@see 'comment_form_default_fields'} hook.
  2051. *
  2052. * @type string $author Comment author field HTML.
  2053. * @type string $email Comment author email field HTML.
  2054. * @type string $url Comment author URL field HTML.
  2055. * @type string $cookies Comment cookie opt-in field HTML.
  2056. * }
  2057. * @type string $comment_field The comment textarea field HTML.
  2058. * @type string $must_log_in HTML element for a 'must be logged in to comment' message.
  2059. * @type string $logged_in_as HTML element for a 'logged in as [user]' message.
  2060. * @type string $comment_notes_before HTML element for a message displayed before the comment fields
  2061. * if the user is not logged in.
  2062. * Default 'Your email address will not be published.'.
  2063. * @type string $comment_notes_after HTML element for a message displayed after the textarea field.
  2064. * @type string $action The comment form element action attribute. Default '/wp-comments-post.php'.
  2065. * @type string $id_form The comment form element id attribute. Default 'commentform'.
  2066. * @type string $id_submit The comment submit element id attribute. Default 'submit'.
  2067. * @type string $class_container The comment form container class attribute. Default 'comment-respond'.
  2068. * @type string $class_form The comment form element class attribute. Default 'comment-form'.
  2069. * @type string $class_submit The comment submit element class attribute. Default 'submit'.
  2070. * @type string $name_submit The comment submit element name attribute. Default 'submit'.
  2071. * @type string $title_reply The translatable 'reply' button label. Default 'Leave a Reply'.
  2072. * @type string $title_reply_to The translatable 'reply-to' button label. Default 'Leave a Reply to %s',
  2073. * where %s is the author of the comment being replied to.
  2074. * @type string $title_reply_before HTML displayed before the comment form title.
  2075. * Default: '<h3 id="reply-title" class="comment-reply-title">'.
  2076. * @type string $title_reply_after HTML displayed after the comment form title.
  2077. * Default: '</h3>'.
  2078. * @type string $cancel_reply_before HTML displayed before the cancel reply link.
  2079. * @type string $cancel_reply_after HTML displayed after the cancel reply link.
  2080. * @type string $cancel_reply_link The translatable 'cancel reply' button label. Default 'Cancel reply'.
  2081. * @type string $label_submit The translatable 'submit' button label. Default 'Post a comment'.
  2082. * @type string $submit_button HTML format for the Submit button.
  2083. * Default: '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />'.
  2084. * @type string $submit_field HTML format for the markup surrounding the Submit button and comment hidden
  2085. * fields. Default: '<p class="form-submit">%1$s %2$s</p>', where %1$s is the
  2086. * submit button markup and %2$s is the comment hidden fields.
  2087. * @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'.
  2088. * }
  2089. * @param int|WP_Post $post_id Post ID or WP_Post object to generate the form for. Default current post.
  2090. */
  2091. function comment_form( $args = array(), $post_id = null ) {
  2092. if ( null === $post_id ) {
  2093. $post_id = get_the_ID();
  2094. }
  2095. // Exit the function when comments for the post are closed.
  2096. if ( ! comments_open( $post_id ) ) {
  2097. /**
  2098. * Fires after the comment form if comments are closed.
  2099. *
  2100. * @since 3.0.0
  2101. */
  2102. do_action( 'comment_form_comments_closed' );
  2103. return;
  2104. }
  2105. $commenter = wp_get_current_commenter();
  2106. $user = wp_get_current_user();
  2107. $user_identity = $user->exists() ? $user->display_name : '';
  2108. $args = wp_parse_args( $args );
  2109. if ( ! isset( $args['format'] ) ) {
  2110. $args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
  2111. }
  2112. $req = get_option( 'require_name_email' );
  2113. $html_req = ( $req ? " required='required'" : '' );
  2114. $html5 = 'html5' === $args['format'];
  2115. $fields = array(
  2116. 'author' => sprintf(
  2117. '<p class="comment-form-author">%s %s</p>',
  2118. sprintf(
  2119. '<label for="author">%s%s</label>',
  2120. __( 'Name' ),
  2121. ( $req ? ' <span class="required">*</span>' : '' )
  2122. ),
  2123. sprintf(
  2124. '<input id="author" name="author" type="text" value="%s" size="30" maxlength="245"%s />',
  2125. esc_attr( $commenter['comment_author'] ),
  2126. $html_req
  2127. )
  2128. ),
  2129. 'email' => sprintf(
  2130. '<p class="comment-form-email">%s %s</p>',
  2131. sprintf(
  2132. '<label for="email">%s%s</label>',
  2133. __( 'Email' ),
  2134. ( $req ? ' <span class="required">*</span>' : '' )
  2135. ),
  2136. sprintf(
  2137. '<input id="email" name="email" %s value="%s" size="30" maxlength="100" aria-describedby="email-notes"%s />',
  2138. ( $html5 ? 'type="email"' : 'type="text"' ),
  2139. esc_attr( $commenter['comment_author_email'] ),
  2140. $html_req
  2141. )
  2142. ),
  2143. 'url' => sprintf(
  2144. '<p class="comment-form-url">%s %s</p>',
  2145. sprintf(
  2146. '<label for="url">%s</label>',
  2147. __( 'Website' )
  2148. ),
  2149. sprintf(
  2150. '<input id="url" name="url" %s value="%s" size="30" maxlength="200" />',
  2151. ( $html5 ? 'type="url"' : 'type="text"' ),
  2152. esc_attr( $commenter['comment_author_url'] )
  2153. )
  2154. ),
  2155. );
  2156. if ( has_action( 'set_comment_cookies', 'wp_set_comment_cookies' ) && get_option( 'show_comments_cookies_opt_in' ) ) {
  2157. $consent = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';
  2158. $fields['cookies'] = sprintf(
  2159. '<p class="comment-form-cookies-consent">%s %s</p>',
  2160. sprintf(
  2161. '<input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"%s />',
  2162. $consent
  2163. ),
  2164. sprintf(
  2165. '<label for="wp-comment-cookies-consent">%s</label>',
  2166. __( 'Save my name, email, and website in this browser for the next time I comment.' )
  2167. )
  2168. );
  2169. // Ensure that the passed fields include cookies consent.
  2170. if ( isset( $args['fields'] ) && ! isset( $args['fields']['cookies'] ) ) {
  2171. $args['fields']['cookies'] = $fields['cookies'];
  2172. }
  2173. }
  2174. $required_text = sprintf(
  2175. /* translators: %s: Asterisk symbol (*). */
  2176. ' ' . __( 'Required fields are marked %s' ),
  2177. '<span class="required">*</span>'
  2178. );
  2179. /**
  2180. * Filters the default comment form fields.
  2181. *
  2182. * @since 3.0.0
  2183. *
  2184. * @param string[] $fields Array of the default comment fields.
  2185. */
  2186. $fields = apply_filters( 'comment_form_default_fields', $fields );
  2187. $defaults = array(
  2188. 'fields' => $fields,
  2189. 'comment_field' => sprintf(
  2190. '<p class="comment-form-comment">%s %s</p>',
  2191. sprintf(
  2192. '<label for="comment">%s</label>',
  2193. _x( 'Comment', 'noun' )
  2194. ),
  2195. '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea>'
  2196. ),
  2197. 'must_log_in' => sprintf(
  2198. '<p class="must-log-in">%s</p>',
  2199. sprintf(
  2200. /* translators: %s: Login URL. */
  2201. __( 'You must be <a href="%s">logged in</a> to post a comment.' ),
  2202. /** This filter is documented in wp-includes/link-template.php */
  2203. wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) )
  2204. )
  2205. ),
  2206. 'logged_in_as' => sprintf(
  2207. '<p class="logged-in-as">%s</p>',
  2208. sprintf(
  2209. /* translators: 1: Edit user link, 2: Accessibility text, 3: User name, 4: Logout URL. */
  2210. __( '<a href="%1$s" aria-label="%2$s">Logged in as %3$s</a>. <a href="%4$s">Log out?</a>' ),
  2211. get_edit_user_link(),
  2212. /* translators: %s: User name. */
  2213. esc_attr( sprintf( __( 'Logged in as %s. Edit your profile.' ), $user_identity ) ),
  2214. $user_identity,
  2215. /** This filter is documented in wp-includes/link-template.php */
  2216. wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) )
  2217. )
  2218. ),
  2219. 'comment_notes_before' => sprintf(
  2220. '<p class="comment-notes">%s%s</p>',
  2221. sprintf(
  2222. '<span id="email-notes">%s</span>',
  2223. __( 'Your email address will not be published.' )
  2224. ),
  2225. ( $req ? $required_text : '' )
  2226. ),
  2227. 'comment_notes_after' => '',
  2228. 'action' => site_url( '/wp-comments-post.php' ),
  2229. 'id_form' => 'commentform',
  2230. 'id_submit' => 'submit',
  2231. 'class_container' => 'comment-respond',
  2232. 'class_form' => 'comment-form',
  2233. 'class_submit' => 'submit',
  2234. 'name_submit' => 'submit',
  2235. 'title_reply' => __( 'Leave a Reply' ),
  2236. /* translators: %s: Author of the comment being replied to. */
  2237. 'title_reply_to' => __( 'Leave a Reply to %s' ),
  2238. 'title_reply_before' => '<h3 id="reply-title" class="comment-reply-title">',
  2239. 'title_reply_after' => '</h3>',
  2240. 'cancel_reply_before' => ' <small>',
  2241. 'cancel_reply_after' => '</small>',
  2242. 'cancel_reply_link' => __( 'Cancel reply' ),
  2243. 'label_submit' => __( 'Post Comment' ),
  2244. 'submit_button' => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',
  2245. 'submit_field' => '<p class="form-submit">%1$s %2$s</p>',
  2246. 'format' => 'xhtml',
  2247. );
  2248. /**
  2249. * Filters the comment form default arguments.
  2250. *
  2251. * Use {@see 'comment_form_default_fields'} to filter the comment fields.
  2252. *
  2253. * @since 3.0.0
  2254. *
  2255. * @param array $defaults The default comment form arguments.
  2256. */
  2257. $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
  2258. // Ensure that the filtered arguments contain all required default values.
  2259. $args = array_merge( $defaults, $args );
  2260. // Remove `aria-describedby` from the email field if there's no associated description.
  2261. if ( isset( $args['fields']['email'] ) && false === strpos( $args['comment_notes_before'], 'id="email-notes"' ) ) {
  2262. $args['fields']['email'] = str_replace(
  2263. ' aria-describedby="email-notes"',
  2264. '',
  2265. $args['fields']['email']
  2266. );
  2267. }
  2268. /**
  2269. * Fires before the comment form.
  2270. *
  2271. * @since 3.0.0
  2272. */
  2273. do_action( 'comment_form_before' );
  2274. ?>
  2275. <div id="respond" class="<?php echo esc_attr( $args['class_container'] ); ?>">
  2276. <?php
  2277. echo $args['title_reply_before'];
  2278. comment_form_title( $args['title_reply'], $args['title_reply_to'] );
  2279. echo $args['cancel_reply_before'];
  2280. cancel_comment_reply_link( $args['cancel_reply_link'] );
  2281. echo $args['cancel_reply_after'];
  2282. echo $args['title_reply_after'];
  2283. if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) :
  2284. echo $args['must_log_in'];
  2285. /**
  2286. * Fires after the HTML-formatted 'must log in after' message in the comment form.
  2287. *
  2288. * @since 3.0.0
  2289. */
  2290. do_action( 'comment_form_must_log_in_after' );
  2291. else :
  2292. printf(
  2293. '<form action="%s" method="post" id="%s" class="%s"%s>',
  2294. esc_url( $args['action'] ),
  2295. esc_attr( $args['id_form'] ),
  2296. esc_attr( $args['class_form'] ),
  2297. ( $html5 ? ' novalidate' : '' )
  2298. );
  2299. /**
  2300. * Fires at the top of the comment form, inside the form tag.
  2301. *
  2302. * @since 3.0.0
  2303. */
  2304. do_action( 'comment_form_top' );
  2305. if ( is_user_logged_in() ) :
  2306. /**
  2307. * Filters the 'logged in' message for the comment form for display.
  2308. *
  2309. * @since 3.0.0
  2310. *
  2311. * @param string $args_logged_in The logged-in-as HTML-formatted message.
  2312. * @param array $commenter An array containing the comment author's
  2313. * username, email, and URL.
  2314. * @param string $user_identity If the commenter is a registered user,
  2315. * the display name, blank otherwise.
  2316. */
  2317. echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );
  2318. /**
  2319. * Fires after the is_user_logged_in() check in the comment form.
  2320. *
  2321. * @since 3.0.0
  2322. *
  2323. * @param array $commenter An array containing the comment author's
  2324. * username, email, and URL.
  2325. * @param string $user_identity If the commenter is a registered user,
  2326. * the display name, blank otherwise.
  2327. */
  2328. do_action( 'comment_form_logged_in_after', $commenter, $user_identity );
  2329. else :
  2330. echo $args['comment_notes_before'];
  2331. endif;
  2332. // Prepare an array of all fields, including the textarea.
  2333. $comment_fields = array( 'comment' => $args['comment_field'] ) + (array) $args['fields'];
  2334. /**
  2335. * Filters the comment form fields, including the textarea.
  2336. *
  2337. * @since 4.4.0
  2338. *
  2339. * @param array $comment_fields The comment fields.
  2340. */
  2341. $comment_fields = apply_filters( 'comment_form_fields', $comment_fields );
  2342. // Get an array of field names, excluding the textarea.
  2343. $comment_field_keys = array_diff( array_keys( $comment_fields ), array( 'comment' ) );
  2344. // Get the first and the last field name, excluding the textarea.
  2345. $first_field = reset( $comment_field_keys );
  2346. $last_field = end( $comment_field_keys );
  2347. foreach ( $comment_fields as $name => $field ) {
  2348. if ( 'comment' === $name ) {
  2349. /**
  2350. * Filters the content of the comment textarea field for display.
  2351. *
  2352. * @since 3.0.0
  2353. *
  2354. * @param string $args_comment_field The content of the comment textarea field.
  2355. */
  2356. echo apply_filters( 'comment_form_field_comment', $field );
  2357. echo $args['comment_notes_after'];
  2358. } elseif ( ! is_user_logged_in() ) {
  2359. if ( $first_field === $name ) {
  2360. /**
  2361. * Fires before the comment fields in the comment form, excluding the textarea.
  2362. *
  2363. * @since 3.0.0
  2364. */
  2365. do_action( 'comment_form_before_fields' );
  2366. }
  2367. /**
  2368. * Filters a comment form field for display.
  2369. *
  2370. * The dynamic portion of the filter hook, `$name`, refers to the name
  2371. * of the comment form field. Such as 'author', 'email', or 'url'.
  2372. *
  2373. * @since 3.0.0
  2374. *
  2375. * @param string $field The HTML-formatted output of the comment form field.
  2376. */
  2377. echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
  2378. if ( $last_field === $name ) {
  2379. /**
  2380. * Fires after the comment fields in the comment form, excluding the textarea.
  2381. *
  2382. * @since 3.0.0
  2383. */
  2384. do_action( 'comment_form_after_fields' );
  2385. }
  2386. }
  2387. }
  2388. $submit_button = sprintf(
  2389. $args['submit_button'],
  2390. esc_attr( $args['name_submit'] ),
  2391. esc_attr( $args['id_submit'] ),
  2392. esc_attr( $args['class_submit'] ),
  2393. esc_attr( $args['label_submit'] )
  2394. );
  2395. /**
  2396. * Filters the submit button for the comment form to display.
  2397. *
  2398. * @since 4.2.0
  2399. *
  2400. * @param string $submit_button HTML markup for the submit button.
  2401. * @param array $args Arguments passed to comment_form().
  2402. */
  2403. $submit_button = apply_filters( 'comment_form_submit_button', $submit_button, $args );
  2404. $submit_field = sprintf(
  2405. $args['submit_field'],
  2406. $submit_button,
  2407. get_comment_id_fields( $post_id )
  2408. );
  2409. /**
  2410. * Filters the submit field for the comment form to display.
  2411. *
  2412. * The submit field includes the submit button, hidden fields for the
  2413. * comment form, and any wrapper markup.
  2414. *
  2415. * @since 4.2.0
  2416. *
  2417. * @param string $submit_field HTML markup for the submit field.
  2418. * @param array $args Arguments passed to comment_form().
  2419. */
  2420. echo apply_filters( 'comment_form_submit_field', $submit_field, $args );
  2421. /**
  2422. * Fires at the bottom of the comment form, inside the closing form tag.
  2423. *
  2424. * @since 1.5.0
  2425. *
  2426. * @param int $post_id The post ID.
  2427. */
  2428. do_action( 'comment_form', $post_id );
  2429. echo '</form>';
  2430. endif;
  2431. ?>
  2432. </div><!-- #respond -->
  2433. <?php
  2434. /**
  2435. * Fires after the comment form.
  2436. *
  2437. * @since 3.0.0
  2438. */
  2439. do_action( 'comment_form_after' );
  2440. }