Brak opisu

comment-template.php 94KB

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