comment-template.php 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739
  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 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 = 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 ) ) . '"';
  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 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 = 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 );
  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 The post ID or WP_Post object.
  493. */
  494. return apply_filters( 'comment_class', $classes, $css_class, $comment->comment_ID, $comment, $post );
  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 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 = 0 ) {
  737. $hash = get_comments_number( $post ) ? '#comments' : '#respond';
  738. $comments_link = get_permalink( $post ) . $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 Post ID or WP_Post object.
  746. */
  747. return apply_filters( 'get_comments_link', $comments_link, $post );
  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 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 = 0 ) {
  776. $post = get_post( $post );
  777. $count = $post ? $post->comment_count : 0;
  778. $post_id = $post ? $post->ID : 0;
  779. /**
  780. * Filters the returned comment count for a post.
  781. *
  782. * @since 1.5.0
  783. *
  784. * @param string|int $count A string representing the number of comments a post has, otherwise 0.
  785. * @param int $post_id Post ID.
  786. */
  787. return apply_filters( 'get_comments_number', $count, $post_id );
  788. }
  789. /**
  790. * Displays the language string for the number of comments the current post has.
  791. *
  792. * @since 0.71
  793. * @since 5.4.0 The `$deprecated` parameter was changed to `$post`.
  794. *
  795. * @param string|false $zero Optional. Text for no comments. Default false.
  796. * @param string|false $one Optional. Text for one comment. Default false.
  797. * @param string|false $more Optional. Text for more than one comment. Default false.
  798. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is the global `$post`.
  799. */
  800. function comments_number( $zero = false, $one = false, $more = false, $post = 0 ) {
  801. echo get_comments_number_text( $zero, $one, $more, $post );
  802. }
  803. /**
  804. * Displays the language string for the number of comments the current post has.
  805. *
  806. * @since 4.0.0
  807. * @since 5.4.0 Added the `$post` parameter to allow using the function outside of the loop.
  808. *
  809. * @param string $zero Optional. Text for no comments. Default false.
  810. * @param string $one Optional. Text for one comment. Default false.
  811. * @param string $more Optional. Text for more than one comment. Default false.
  812. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is the global `$post`.
  813. * @return string Language string for the number of comments a post has.
  814. */
  815. function get_comments_number_text( $zero = false, $one = false, $more = false, $post = 0 ) {
  816. $number = get_comments_number( $post );
  817. if ( $number > 1 ) {
  818. if ( false === $more ) {
  819. /* translators: %s: Number of comments. */
  820. $output = sprintf( _n( '%s Comment', '%s Comments', $number ), number_format_i18n( $number ) );
  821. } else {
  822. // % Comments
  823. /*
  824. * translators: If comment number in your language requires declension,
  825. * translate this to 'on'. Do not translate into your own language.
  826. */
  827. if ( 'on' === _x( 'off', 'Comment number declension: on or off' ) ) {
  828. $text = preg_replace( '#<span class="screen-reader-text">.+?</span>#', '', $more );
  829. $text = preg_replace( '/&.+?;/', '', $text ); // Kill entities.
  830. $text = trim( strip_tags( $text ), '% ' );
  831. // Replace '% Comments' with a proper plural form.
  832. if ( $text && ! preg_match( '/[0-9]+/', $text ) && false !== strpos( $more, '%' ) ) {
  833. /* translators: %s: Number of comments. */
  834. $new_text = _n( '%s Comment', '%s Comments', $number );
  835. $new_text = trim( sprintf( $new_text, '' ) );
  836. $more = str_replace( $text, $new_text, $more );
  837. if ( false === strpos( $more, '%' ) ) {
  838. $more = '% ' . $more;
  839. }
  840. }
  841. }
  842. $output = str_replace( '%', number_format_i18n( $number ), $more );
  843. }
  844. } elseif ( 0 == $number ) {
  845. $output = ( false === $zero ) ? __( 'No Comments' ) : $zero;
  846. } else { // Must be one.
  847. $output = ( false === $one ) ? __( '1 Comment' ) : $one;
  848. }
  849. /**
  850. * Filters the comments count for display.
  851. *
  852. * @since 1.5.0
  853. *
  854. * @see _n()
  855. *
  856. * @param string $output A translatable string formatted based on whether the count
  857. * is equal to 0, 1, or 1+.
  858. * @param int $number The number of post comments.
  859. */
  860. return apply_filters( 'comments_number', $output, $number );
  861. }
  862. /**
  863. * Retrieves the text of the current comment.
  864. *
  865. * @since 1.5.0
  866. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  867. * @since 5.4.0 Added 'In reply to %s.' prefix to child comments in comments feed.
  868. *
  869. * @see Walker_Comment::comment()
  870. *
  871. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the text.
  872. * Default current comment.
  873. * @param array $args Optional. An array of arguments. Default empty array.
  874. * @return string The comment content.
  875. */
  876. function get_comment_text( $comment_ID = 0, $args = array() ) {
  877. $comment = get_comment( $comment_ID );
  878. $comment_content = $comment->comment_content;
  879. if ( is_comment_feed() && $comment->comment_parent ) {
  880. $parent = get_comment( $comment->comment_parent );
  881. if ( $parent ) {
  882. $parent_link = esc_url( get_comment_link( $parent ) );
  883. $name = get_comment_author( $parent );
  884. $comment_content = sprintf(
  885. /* translators: %s: Comment link. */
  886. ent2ncr( __( 'In reply to %s.' ) ),
  887. '<a href="' . $parent_link . '">' . $name . '</a>'
  888. ) . "\n\n" . $comment_content;
  889. }
  890. }
  891. /**
  892. * Filters the text of a comment.
  893. *
  894. * @since 1.5.0
  895. *
  896. * @see Walker_Comment::comment()
  897. *
  898. * @param string $comment_content Text of the comment.
  899. * @param WP_Comment $comment The comment object.
  900. * @param array $args An array of arguments.
  901. */
  902. return apply_filters( 'get_comment_text', $comment_content, $comment, $args );
  903. }
  904. /**
  905. * Displays the text of the current comment.
  906. *
  907. * @since 0.71
  908. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  909. *
  910. * @see Walker_Comment::comment()
  911. *
  912. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the text.
  913. * Default current comment.
  914. * @param array $args Optional. An array of arguments. Default empty array.
  915. */
  916. function comment_text( $comment_ID = 0, $args = array() ) {
  917. $comment = get_comment( $comment_ID );
  918. $comment_text = get_comment_text( $comment, $args );
  919. /**
  920. * Filters the text of a comment to be displayed.
  921. *
  922. * @since 1.2.0
  923. *
  924. * @see Walker_Comment::comment()
  925. *
  926. * @param string $comment_text Text of the current comment.
  927. * @param WP_Comment|null $comment The comment object. Null if not found.
  928. * @param array $args An array of arguments.
  929. */
  930. echo apply_filters( 'comment_text', $comment_text, $comment, $args );
  931. }
  932. /**
  933. * Retrieves the comment time of the current comment.
  934. *
  935. * @since 1.5.0
  936. *
  937. * @param string $format Optional. PHP time format. Defaults to the 'time_format' option.
  938. * @param bool $gmt Optional. Whether to use the GMT date. Default false.
  939. * @param bool $translate Optional. Whether to translate the time (for use in feeds).
  940. * Default true.
  941. * @return string The formatted time.
  942. */
  943. function get_comment_time( $format = '', $gmt = false, $translate = true ) {
  944. $comment = get_comment();
  945. $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
  946. $_format = ! empty( $format ) ? $format : get_option( 'time_format' );
  947. $date = mysql2date( $_format, $comment_date, $translate );
  948. /**
  949. * Filters the returned comment time.
  950. *
  951. * @since 1.5.0
  952. *
  953. * @param string|int $date The comment time, formatted as a date string or Unix timestamp.
  954. * @param string $format PHP date format.
  955. * @param bool $gmt Whether the GMT date is in use.
  956. * @param bool $translate Whether the time is translated.
  957. * @param WP_Comment $comment The comment object.
  958. */
  959. return apply_filters( 'get_comment_time', $date, $format, $gmt, $translate, $comment );
  960. }
  961. /**
  962. * Displays the comment time of the current comment.
  963. *
  964. * @since 0.71
  965. *
  966. * @param string $format Optional. PHP time format. Defaults to the 'time_format' option.
  967. */
  968. function comment_time( $format = '' ) {
  969. echo get_comment_time( $format );
  970. }
  971. /**
  972. * Retrieves the comment type of the current comment.
  973. *
  974. * @since 1.5.0
  975. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  976. *
  977. * @param int|WP_Comment $comment_ID Optional. WP_Comment or ID of the comment for which to get the type.
  978. * Default current comment.
  979. * @return string The comment type.
  980. */
  981. function get_comment_type( $comment_ID = 0 ) {
  982. $comment = get_comment( $comment_ID );
  983. if ( '' === $comment->comment_type ) {
  984. $comment->comment_type = 'comment';
  985. }
  986. /**
  987. * Filters the returned comment type.
  988. *
  989. * @since 1.5.0
  990. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  991. *
  992. * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'.
  993. * @param string $comment_ID The comment ID as a numeric string.
  994. * @param WP_Comment $comment The comment object.
  995. */
  996. return apply_filters( 'get_comment_type', $comment->comment_type, $comment->comment_ID, $comment );
  997. }
  998. /**
  999. * Displays the comment type of the current comment.
  1000. *
  1001. * @since 0.71
  1002. *
  1003. * @param string|false $commenttxt Optional. String to display for comment type. Default false.
  1004. * @param string|false $trackbacktxt Optional. String to display for trackback type. Default false.
  1005. * @param string|false $pingbacktxt Optional. String to display for pingback type. Default false.
  1006. */
  1007. function comment_type( $commenttxt = false, $trackbacktxt = false, $pingbacktxt = false ) {
  1008. if ( false === $commenttxt ) {
  1009. $commenttxt = _x( 'Comment', 'noun' );
  1010. }
  1011. if ( false === $trackbacktxt ) {
  1012. $trackbacktxt = __( 'Trackback' );
  1013. }
  1014. if ( false === $pingbacktxt ) {
  1015. $pingbacktxt = __( 'Pingback' );
  1016. }
  1017. $type = get_comment_type();
  1018. switch ( $type ) {
  1019. case 'trackback':
  1020. echo $trackbacktxt;
  1021. break;
  1022. case 'pingback':
  1023. echo $pingbacktxt;
  1024. break;
  1025. default:
  1026. echo $commenttxt;
  1027. }
  1028. }
  1029. /**
  1030. * Retrieves the current post's trackback URL.
  1031. *
  1032. * There is a check to see if permalink's have been enabled and if so, will
  1033. * retrieve the pretty path. If permalinks weren't enabled, the ID of the
  1034. * current post is used and appended to the correct page to go to.
  1035. *
  1036. * @since 1.5.0
  1037. *
  1038. * @return string The trackback URL after being filtered.
  1039. */
  1040. function get_trackback_url() {
  1041. if ( get_option( 'permalink_structure' ) ) {
  1042. $trackback_url = trailingslashit( get_permalink() ) . user_trailingslashit( 'trackback', 'single_trackback' );
  1043. } else {
  1044. $trackback_url = get_option( 'siteurl' ) . '/wp-trackback.php?p=' . get_the_ID();
  1045. }
  1046. /**
  1047. * Filters the returned trackback URL.
  1048. *
  1049. * @since 2.2.0
  1050. *
  1051. * @param string $trackback_url The trackback URL.
  1052. */
  1053. return apply_filters( 'trackback_url', $trackback_url );
  1054. }
  1055. /**
  1056. * Displays the current post's trackback URL.
  1057. *
  1058. * @since 0.71
  1059. *
  1060. * @param bool $deprecated_echo Not used.
  1061. * @return void|string Should only be used to echo the trackback URL, use get_trackback_url()
  1062. * for the result instead.
  1063. */
  1064. function trackback_url( $deprecated_echo = true ) {
  1065. if ( true !== $deprecated_echo ) {
  1066. _deprecated_argument(
  1067. __FUNCTION__,
  1068. '2.5.0',
  1069. sprintf(
  1070. /* translators: %s: get_trackback_url() */
  1071. __( 'Use %s instead if you do not want the value echoed.' ),
  1072. '<code>get_trackback_url()</code>'
  1073. )
  1074. );
  1075. }
  1076. if ( $deprecated_echo ) {
  1077. echo get_trackback_url();
  1078. } else {
  1079. return get_trackback_url();
  1080. }
  1081. }
  1082. /**
  1083. * Generates and displays the RDF for the trackback information of current post.
  1084. *
  1085. * Deprecated in 3.0.0, and restored in 3.0.1.
  1086. *
  1087. * @since 0.71
  1088. *
  1089. * @param int|string $deprecated Not used (Was $timezone = 0).
  1090. */
  1091. function trackback_rdf( $deprecated = '' ) {
  1092. if ( ! empty( $deprecated ) ) {
  1093. _deprecated_argument( __FUNCTION__, '2.5.0' );
  1094. }
  1095. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'W3C_Validator' ) ) {
  1096. return;
  1097. }
  1098. echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  1099. xmlns:dc="http://purl.org/dc/elements/1.1/"
  1100. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  1101. <rdf:Description rdf:about="';
  1102. the_permalink();
  1103. echo '"' . "\n";
  1104. echo ' dc:identifier="';
  1105. the_permalink();
  1106. echo '"' . "\n";
  1107. echo ' dc:title="' . str_replace( '--', '&#x2d;&#x2d;', wptexturize( strip_tags( get_the_title() ) ) ) . '"' . "\n";
  1108. echo ' trackback:ping="' . get_trackback_url() . '"' . " />\n";
  1109. echo '</rdf:RDF>';
  1110. }
  1111. /**
  1112. * Determines whether the current post is open for comments.
  1113. *
  1114. * For more information on this and similar theme functions, check out
  1115. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  1116. * Conditional Tags} article in the Theme Developer Handbook.
  1117. *
  1118. * @since 1.5.0
  1119. *
  1120. * @param int|WP_Post $post Post ID or WP_Post object. Default current post.
  1121. * @return bool True if the comments are open.
  1122. */
  1123. function comments_open( $post = null ) {
  1124. $_post = get_post( $post );
  1125. $post_id = $_post ? $_post->ID : 0;
  1126. $open = ( $_post && ( 'open' === $_post->comment_status ) );
  1127. /**
  1128. * Filters whether the current post is open for comments.
  1129. *
  1130. * @since 2.5.0
  1131. *
  1132. * @param bool $open Whether the current post is open for comments.
  1133. * @param int $post_id The post ID.
  1134. */
  1135. return apply_filters( 'comments_open', $open, $post_id );
  1136. }
  1137. /**
  1138. * Determines whether the current post is open for pings.
  1139. *
  1140. * For more information on this and similar theme functions, check out
  1141. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  1142. * Conditional Tags} article in the Theme Developer Handbook.
  1143. *
  1144. * @since 1.5.0
  1145. *
  1146. * @param int|WP_Post $post Post ID or WP_Post object. Default current post.
  1147. * @return bool True if pings are accepted
  1148. */
  1149. function pings_open( $post = null ) {
  1150. $_post = get_post( $post );
  1151. $post_id = $_post ? $_post->ID : 0;
  1152. $open = ( $_post && ( 'open' === $_post->ping_status ) );
  1153. /**
  1154. * Filters whether the current post is open for pings.
  1155. *
  1156. * @since 2.5.0
  1157. *
  1158. * @param bool $open Whether the current post is open for pings.
  1159. * @param int $post_id The post ID.
  1160. */
  1161. return apply_filters( 'pings_open', $open, $post_id );
  1162. }
  1163. /**
  1164. * Displays form token for unfiltered comments.
  1165. *
  1166. * Will only display nonce token if the current user has permissions for
  1167. * unfiltered html. Won't display the token for other users.
  1168. *
  1169. * The function was backported to 2.0.10 and was added to versions 2.1.3 and
  1170. * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
  1171. * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
  1172. *
  1173. * Backported to 2.0.10.
  1174. *
  1175. * @since 2.1.3
  1176. */
  1177. function wp_comment_form_unfiltered_html_nonce() {
  1178. $post = get_post();
  1179. $post_id = $post ? $post->ID : 0;
  1180. if ( current_user_can( 'unfiltered_html' ) ) {
  1181. wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );
  1182. echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n";
  1183. }
  1184. }
  1185. /**
  1186. * Loads the comment template specified in $file.
  1187. *
  1188. * Will not display the comments template if not on single post or page, or if
  1189. * the post does not have comments.
  1190. *
  1191. * Uses the WordPress database object to query for the comments. The comments
  1192. * are passed through the {@see 'comments_array'} filter hook with the list of comments
  1193. * and the post ID respectively.
  1194. *
  1195. * The `$file` path is passed through a filter hook called {@see 'comments_template'},
  1196. * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
  1197. * first and if it fails it will require the default comment template from the
  1198. * default theme. If either does not exist, then the WordPress process will be
  1199. * halted. It is advised for that reason, that the default theme is not deleted.
  1200. *
  1201. * Will not try to get the comments if the post has none.
  1202. *
  1203. * @since 1.5.0
  1204. *
  1205. * @global WP_Query $wp_query WordPress Query object.
  1206. * @global WP_Post $post Global post object.
  1207. * @global wpdb $wpdb WordPress database abstraction object.
  1208. * @global int $id
  1209. * @global WP_Comment $comment Global comment object.
  1210. * @global string $user_login
  1211. * @global string $user_identity
  1212. * @global bool $overridden_cpage
  1213. * @global bool $withcomments
  1214. *
  1215. * @param string $file Optional. The file to load. Default '/comments.php'.
  1216. * @param bool $separate_comments Optional. Whether to separate the comments by comment type.
  1217. * Default false.
  1218. */
  1219. function comments_template( $file = '/comments.php', $separate_comments = false ) {
  1220. global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage;
  1221. if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) {
  1222. return;
  1223. }
  1224. if ( empty( $file ) ) {
  1225. $file = '/comments.php';
  1226. }
  1227. $req = get_option( 'require_name_email' );
  1228. /*
  1229. * Comment author information fetched from the comment cookies.
  1230. */
  1231. $commenter = wp_get_current_commenter();
  1232. /*
  1233. * The name of the current comment author escaped for use in attributes.
  1234. * Escaped by sanitize_comment_cookies().
  1235. */
  1236. $comment_author = $commenter['comment_author'];
  1237. /*
  1238. * The email address of the current comment author escaped for use in attributes.
  1239. * Escaped by sanitize_comment_cookies().
  1240. */
  1241. $comment_author_email = $commenter['comment_author_email'];
  1242. /*
  1243. * The URL of the current comment author escaped for use in attributes.
  1244. */
  1245. $comment_author_url = esc_url( $commenter['comment_author_url'] );
  1246. $comment_args = array(
  1247. 'orderby' => 'comment_date_gmt',
  1248. 'order' => 'ASC',
  1249. 'status' => 'approve',
  1250. 'post_id' => $post->ID,
  1251. 'no_found_rows' => false,
  1252. 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
  1253. );
  1254. if ( get_option( 'thread_comments' ) ) {
  1255. $comment_args['hierarchical'] = 'threaded';
  1256. } else {
  1257. $comment_args['hierarchical'] = false;
  1258. }
  1259. if ( is_user_logged_in() ) {
  1260. $comment_args['include_unapproved'] = array( get_current_user_id() );
  1261. } else {
  1262. $unapproved_email = wp_get_unapproved_comment_author_email();
  1263. if ( $unapproved_email ) {
  1264. $comment_args['include_unapproved'] = array( $unapproved_email );
  1265. }
  1266. }
  1267. $per_page = 0;
  1268. if ( get_option( 'page_comments' ) ) {
  1269. $per_page = (int) get_query_var( 'comments_per_page' );
  1270. if ( 0 === $per_page ) {
  1271. $per_page = (int) get_option( 'comments_per_page' );
  1272. }
  1273. $comment_args['number'] = $per_page;
  1274. $page = (int) get_query_var( 'cpage' );
  1275. if ( $page ) {
  1276. $comment_args['offset'] = ( $page - 1 ) * $per_page;
  1277. } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) {
  1278. $comment_args['offset'] = 0;
  1279. } else {
  1280. // If fetching the first page of 'newest', we need a top-level comment count.
  1281. $top_level_query = new WP_Comment_Query();
  1282. $top_level_args = array(
  1283. 'count' => true,
  1284. 'orderby' => false,
  1285. 'post_id' => $post->ID,
  1286. 'status' => 'approve',
  1287. );
  1288. if ( $comment_args['hierarchical'] ) {
  1289. $top_level_args['parent'] = 0;
  1290. }
  1291. if ( isset( $comment_args['include_unapproved'] ) ) {
  1292. $top_level_args['include_unapproved'] = $comment_args['include_unapproved'];
  1293. }
  1294. /**
  1295. * Filters the arguments used in the top level comments query.
  1296. *
  1297. * @since 5.6.0
  1298. *
  1299. * @see WP_Comment_Query::__construct()
  1300. *
  1301. * @param array $top_level_args {
  1302. * The top level query arguments for the comments template.
  1303. *
  1304. * @type bool $count Whether to return a comment count.
  1305. * @type string|array $orderby The field(s) to order by.
  1306. * @type int $post_id The post ID.
  1307. * @type string|array $status The comment status to limit results by.
  1308. * }
  1309. */
  1310. $top_level_args = apply_filters( 'comments_template_top_level_query_args', $top_level_args );
  1311. $top_level_count = $top_level_query->query( $top_level_args );
  1312. $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
  1313. }
  1314. }
  1315. /**
  1316. * Filters the arguments used to query comments in comments_template().
  1317. *
  1318. * @since 4.5.0
  1319. *
  1320. * @see WP_Comment_Query::__construct()
  1321. *
  1322. * @param array $comment_args {
  1323. * Array of WP_Comment_Query arguments.
  1324. *
  1325. * @type string|array $orderby Field(s) to order by.
  1326. * @type string $order Order of results. Accepts 'ASC' or 'DESC'.
  1327. * @type string $status Comment status.
  1328. * @type array $include_unapproved Array of IDs or email addresses whose unapproved comments
  1329. * will be included in results.
  1330. * @type int $post_id ID of the post.
  1331. * @type bool $no_found_rows Whether to refrain from querying for found rows.
  1332. * @type bool $update_comment_meta_cache Whether to prime cache for comment meta.
  1333. * @type bool|string $hierarchical Whether to query for comments hierarchically.
  1334. * @type int $offset Comment offset.
  1335. * @type int $number Number of comments to fetch.
  1336. * }
  1337. */
  1338. $comment_args = apply_filters( 'comments_template_query_args', $comment_args );
  1339. $comment_query = new WP_Comment_Query( $comment_args );
  1340. $_comments = $comment_query->comments;
  1341. // Trees must be flattened before they're passed to the walker.
  1342. if ( $comment_args['hierarchical'] ) {
  1343. $comments_flat = array();
  1344. foreach ( $_comments as $_comment ) {
  1345. $comments_flat[] = $_comment;
  1346. $comment_children = $_comment->get_children(
  1347. array(
  1348. 'format' => 'flat',
  1349. 'status' => $comment_args['status'],
  1350. 'orderby' => $comment_args['orderby'],
  1351. )
  1352. );
  1353. foreach ( $comment_children as $comment_child ) {
  1354. $comments_flat[] = $comment_child;
  1355. }
  1356. }
  1357. } else {
  1358. $comments_flat = $_comments;
  1359. }
  1360. /**
  1361. * Filters the comments array.
  1362. *
  1363. * @since 2.1.0
  1364. *
  1365. * @param array $comments Array of comments supplied to the comments template.
  1366. * @param int $post_id Post ID.
  1367. */
  1368. $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID );
  1369. $comments = &$wp_query->comments;
  1370. $wp_query->comment_count = count( $wp_query->comments );
  1371. $wp_query->max_num_comment_pages = $comment_query->max_num_pages;
  1372. if ( $separate_comments ) {
  1373. $wp_query->comments_by_type = separate_comments( $comments );
  1374. $comments_by_type = &$wp_query->comments_by_type;
  1375. } else {
  1376. $wp_query->comments_by_type = array();
  1377. }
  1378. $overridden_cpage = false;
  1379. if ( '' == get_query_var( 'cpage' ) && $wp_query->max_num_comment_pages > 1 ) {
  1380. set_query_var( 'cpage', 'newest' === get_option( 'default_comments_page' ) ? get_comment_pages_count() : 1 );
  1381. $overridden_cpage = true;
  1382. }
  1383. if ( ! defined( 'COMMENTS_TEMPLATE' ) ) {
  1384. define( 'COMMENTS_TEMPLATE', true );
  1385. }
  1386. $theme_template = STYLESHEETPATH . $file;
  1387. /**
  1388. * Filters the path to the theme template file used for the comments template.
  1389. *
  1390. * @since 1.5.1
  1391. *
  1392. * @param string $theme_template The path to the theme template file.
  1393. */
  1394. $include = apply_filters( 'comments_template', $theme_template );
  1395. if ( file_exists( $include ) ) {
  1396. require $include;
  1397. } elseif ( file_exists( TEMPLATEPATH . $file ) ) {
  1398. require TEMPLATEPATH . $file;
  1399. } else { // Backward compat code will be removed in a future release.
  1400. require ABSPATH . WPINC . '/theme-compat/comments.php';
  1401. }
  1402. }
  1403. /**
  1404. * Displays the link to the comments for the current post ID.
  1405. *
  1406. * @since 0.71
  1407. *
  1408. * @param false|string $zero Optional. String to display when no comments. Default false.
  1409. * @param false|string $one Optional. String to display when only one comment is available. Default false.
  1410. * @param false|string $more Optional. String to display when there are more than one comment. Default false.
  1411. * @param string $css_class Optional. CSS class to use for comments. Default empty.
  1412. * @param false|string $none Optional. String to display when comments have been turned off. Default false.
  1413. */
  1414. function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
  1415. $post_id = get_the_ID();
  1416. $post_title = get_the_title();
  1417. $number = get_comments_number( $post_id );
  1418. if ( false === $zero ) {
  1419. /* translators: %s: Post title. */
  1420. $zero = sprintf( __( 'No Comments<span class="screen-reader-text"> on %s</span>' ), $post_title );
  1421. }
  1422. if ( false === $one ) {
  1423. /* translators: %s: Post title. */
  1424. $one = sprintf( __( '1 Comment<span class="screen-reader-text"> on %s</span>' ), $post_title );
  1425. }
  1426. if ( false === $more ) {
  1427. /* translators: 1: Number of comments, 2: Post title. */
  1428. $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 );
  1429. $more = sprintf( $more, number_format_i18n( $number ), $post_title );
  1430. }
  1431. if ( false === $none ) {
  1432. /* translators: %s: Post title. */
  1433. $none = sprintf( __( 'Comments Off<span class="screen-reader-text"> on %s</span>' ), $post_title );
  1434. }
  1435. if ( 0 == $number && ! comments_open() && ! pings_open() ) {
  1436. echo '<span' . ( ( ! empty( $css_class ) ) ? ' class="' . esc_attr( $css_class ) . '"' : '' ) . '>' . $none . '</span>';
  1437. return;
  1438. }
  1439. if ( post_password_required() ) {
  1440. _e( 'Enter your password to view comments.' );
  1441. return;
  1442. }
  1443. echo '<a href="';
  1444. if ( 0 == $number ) {
  1445. $respond_link = get_permalink() . '#respond';
  1446. /**
  1447. * Filters the respond link when a post has no comments.
  1448. *
  1449. * @since 4.4.0
  1450. *
  1451. * @param string $respond_link The default response link.
  1452. * @param int $post_id The post ID.
  1453. */
  1454. echo apply_filters( 'respond_link', $respond_link, $post_id );
  1455. } else {
  1456. comments_link();
  1457. }
  1458. echo '"';
  1459. if ( ! empty( $css_class ) ) {
  1460. echo ' class="' . $css_class . '" ';
  1461. }
  1462. $attributes = '';
  1463. /**
  1464. * Filters the comments link attributes for display.
  1465. *
  1466. * @since 2.5.0
  1467. *
  1468. * @param string $attributes The comments link attributes. Default empty.
  1469. */
  1470. echo apply_filters( 'comments_popup_link_attributes', $attributes );
  1471. echo '>';
  1472. comments_number( $zero, $one, $more );
  1473. echo '</a>';
  1474. }
  1475. /**
  1476. * Retrieves HTML content for reply to comment link.
  1477. *
  1478. * @since 2.7.0
  1479. * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
  1480. *
  1481. * @param array $args {
  1482. * Optional. Override default arguments.
  1483. *
  1484. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1485. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1486. * concatenated as $add_below-$comment->comment_ID. Default 'comment'.
  1487. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1488. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1489. * Default 'respond'.
  1490. * @type string $reply_text The text of the Reply link. Default 'Reply'.
  1491. * @type string $login_text The text of the link to reply if logged out. Default 'Log in to Reply'.
  1492. * @type int $max_depth The max depth of the comment tree. Default 0.
  1493. * @type int $depth The depth of the new comment. Must be greater than 0 and less than the value
  1494. * of the 'thread_comments_depth' option set in Settings > Discussion. Default 0.
  1495. * @type string $before The text or HTML to add before the reply link. Default empty.
  1496. * @type string $after The text or HTML to add after the reply link. Default empty.
  1497. * }
  1498. * @param int|WP_Comment $comment Comment being replied to. Default current comment.
  1499. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1500. * Default current post.
  1501. * @return string|false|null Link to show comment form, if successful. False, if comments are closed.
  1502. */
  1503. function get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
  1504. $defaults = array(
  1505. 'add_below' => 'comment',
  1506. 'respond_id' => 'respond',
  1507. 'reply_text' => __( 'Reply' ),
  1508. /* translators: Comment reply button text. %s: Comment author name. */
  1509. 'reply_to_text' => __( 'Reply to %s' ),
  1510. 'login_text' => __( 'Log in to Reply' ),
  1511. 'max_depth' => 0,
  1512. 'depth' => 0,
  1513. 'before' => '',
  1514. 'after' => '',
  1515. );
  1516. $args = wp_parse_args( $args, $defaults );
  1517. if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] ) {
  1518. return;
  1519. }
  1520. $comment = get_comment( $comment );
  1521. if ( empty( $comment ) ) {
  1522. return;
  1523. }
  1524. if ( empty( $post ) ) {
  1525. $post = $comment->comment_post_ID;
  1526. }
  1527. $post = get_post( $post );
  1528. if ( ! comments_open( $post->ID ) ) {
  1529. return false;
  1530. }
  1531. if ( get_option( 'page_comments' ) ) {
  1532. $permalink = str_replace( '#comment-' . $comment->comment_ID, '', get_comment_link( $comment ) );
  1533. } else {
  1534. $permalink = get_permalink( $post->ID );
  1535. }
  1536. /**
  1537. * Filters the comment reply link arguments.
  1538. *
  1539. * @since 4.1.0
  1540. *
  1541. * @param array $args Comment reply link arguments. See get_comment_reply_link()
  1542. * for more information on accepted arguments.
  1543. * @param WP_Comment $comment The object of the comment being replied to.
  1544. * @param WP_Post $post The WP_Post object.
  1545. */
  1546. $args = apply_filters( 'comment_reply_link_args', $args, $comment, $post );
  1547. if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
  1548. $link = sprintf(
  1549. '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
  1550. esc_url( wp_login_url( get_permalink() ) ),
  1551. $args['login_text']
  1552. );
  1553. } else {
  1554. $data_attributes = array(
  1555. 'commentid' => $comment->comment_ID,
  1556. 'postid' => $post->ID,
  1557. 'belowelement' => $args['add_below'] . '-' . $comment->comment_ID,
  1558. 'respondelement' => $args['respond_id'],
  1559. 'replyto' => sprintf( $args['reply_to_text'], get_comment_author( $comment ) ),
  1560. );
  1561. $data_attribute_string = '';
  1562. foreach ( $data_attributes as $name => $value ) {
  1563. $data_attribute_string .= " data-{$name}=\"" . esc_attr( $value ) . '"';
  1564. }
  1565. $data_attribute_string = trim( $data_attribute_string );
  1566. $link = sprintf(
  1567. "<a rel='nofollow' class='comment-reply-link' href='%s' %s aria-label='%s'>%s</a>",
  1568. esc_url(
  1569. add_query_arg(
  1570. array(
  1571. 'replytocom' => $comment->comment_ID,
  1572. 'unapproved' => false,
  1573. 'moderation-hash' => false,
  1574. ),
  1575. $permalink
  1576. )
  1577. ) . '#' . $args['respond_id'],
  1578. $data_attribute_string,
  1579. esc_attr( sprintf( $args['reply_to_text'], get_comment_author( $comment ) ) ),
  1580. $args['reply_text']
  1581. );
  1582. }
  1583. /**
  1584. * Filters the comment reply link.
  1585. *
  1586. * @since 2.7.0
  1587. *
  1588. * @param string $link The HTML markup for the comment reply link.
  1589. * @param array $args An array of arguments overriding the defaults.
  1590. * @param WP_Comment $comment The object of the comment being replied.
  1591. * @param WP_Post $post The WP_Post object.
  1592. */
  1593. return apply_filters( 'comment_reply_link', $args['before'] . $link . $args['after'], $args, $comment, $post );
  1594. }
  1595. /**
  1596. * Displays the HTML content for reply to comment link.
  1597. *
  1598. * @since 2.7.0
  1599. *
  1600. * @see get_comment_reply_link()
  1601. *
  1602. * @param array $args Optional. Override default options. Default empty array.
  1603. * @param int|WP_Comment $comment Comment being replied to. Default current comment.
  1604. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1605. * Default current post.
  1606. */
  1607. function comment_reply_link( $args = array(), $comment = null, $post = null ) {
  1608. echo get_comment_reply_link( $args, $comment, $post );
  1609. }
  1610. /**
  1611. * Retrieves HTML content for reply to post link.
  1612. *
  1613. * @since 2.7.0
  1614. *
  1615. * @param array $args {
  1616. * Optional. Override default arguments.
  1617. *
  1618. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1619. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1620. * concatenated as $add_below-$comment->comment_ID. Default is 'post'.
  1621. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1622. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1623. * Default 'respond'.
  1624. * @type string $reply_text Text of the Reply link. Default is 'Leave a Comment'.
  1625. * @type string $login_text Text of the link to reply if logged out. Default is 'Log in to leave a Comment'.
  1626. * @type string $before Text or HTML to add before the reply link. Default empty.
  1627. * @type string $after Text or HTML to add after the reply link. Default empty.
  1628. * }
  1629. * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on.
  1630. * Default current post.
  1631. * @return string|false|null Link to show comment form, if successful. False, if comments are closed.
  1632. */
  1633. function get_post_reply_link( $args = array(), $post = null ) {
  1634. $defaults = array(
  1635. 'add_below' => 'post',
  1636. 'respond_id' => 'respond',
  1637. 'reply_text' => __( 'Leave a Comment' ),
  1638. 'login_text' => __( 'Log in to leave a Comment' ),
  1639. 'before' => '',
  1640. 'after' => '',
  1641. );
  1642. $args = wp_parse_args( $args, $defaults );
  1643. $post = get_post( $post );
  1644. if ( ! comments_open( $post->ID ) ) {
  1645. return false;
  1646. }
  1647. if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
  1648. $link = sprintf(
  1649. '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
  1650. wp_login_url( get_permalink() ),
  1651. $args['login_text']
  1652. );
  1653. } else {
  1654. $onclick = sprintf(
  1655. 'return addComment.moveForm( "%1$s-%2$s", "0", "%3$s", "%2$s" )',
  1656. $args['add_below'],
  1657. $post->ID,
  1658. $args['respond_id']
  1659. );
  1660. $link = sprintf(
  1661. "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s'>%s</a>",
  1662. get_permalink( $post->ID ) . '#' . $args['respond_id'],
  1663. $onclick,
  1664. $args['reply_text']
  1665. );
  1666. }
  1667. $formatted_link = $args['before'] . $link . $args['after'];
  1668. /**
  1669. * Filters the formatted post comments link HTML.
  1670. *
  1671. * @since 2.7.0
  1672. *
  1673. * @param string $formatted The HTML-formatted post comments link.
  1674. * @param int|WP_Post $post The post ID or WP_Post object.
  1675. */
  1676. return apply_filters( 'post_comments_link', $formatted_link, $post );
  1677. }
  1678. /**
  1679. * Displays the HTML content for reply to post link.
  1680. *
  1681. * @since 2.7.0
  1682. *
  1683. * @see get_post_reply_link()
  1684. *
  1685. * @param array $args Optional. Override default options. Default empty array.
  1686. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1687. * Default current post.
  1688. */
  1689. function post_reply_link( $args = array(), $post = null ) {
  1690. echo get_post_reply_link( $args, $post );
  1691. }
  1692. /**
  1693. * Retrieves HTML content for cancel comment reply link.
  1694. *
  1695. * @since 2.7.0
  1696. *
  1697. * @param string $text Optional. Text to display for cancel reply link. If empty,
  1698. * defaults to 'Click here to cancel reply'. Default empty.
  1699. * @return string
  1700. */
  1701. function get_cancel_comment_reply_link( $text = '' ) {
  1702. if ( empty( $text ) ) {
  1703. $text = __( 'Click here to cancel reply.' );
  1704. }
  1705. $style = isset( $_GET['replytocom'] ) ? '' : ' style="display:none;"';
  1706. $link = esc_html( remove_query_arg( array( 'replytocom', 'unapproved', 'moderation-hash' ) ) ) . '#respond';
  1707. $formatted_link = '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>';
  1708. /**
  1709. * Filters the cancel comment reply link HTML.
  1710. *
  1711. * @since 2.7.0
  1712. *
  1713. * @param string $formatted_link The HTML-formatted cancel comment reply link.
  1714. * @param string $link Cancel comment reply link URL.
  1715. * @param string $text Cancel comment reply link text.
  1716. */
  1717. return apply_filters( 'cancel_comment_reply_link', $formatted_link, $link, $text );
  1718. }
  1719. /**
  1720. * Displays HTML content for cancel comment reply link.
  1721. *
  1722. * @since 2.7.0
  1723. *
  1724. * @param string $text Optional. Text to display for cancel reply link. If empty,
  1725. * defaults to 'Click here to cancel reply'. Default empty.
  1726. */
  1727. function cancel_comment_reply_link( $text = '' ) {
  1728. echo get_cancel_comment_reply_link( $text );
  1729. }
  1730. /**
  1731. * Retrieves hidden input HTML for replying to comments.
  1732. *
  1733. * @since 3.0.0
  1734. *
  1735. * @param int $post_id Optional. Post ID. Defaults to the current post ID.
  1736. * @return string Hidden input HTML for replying to comments.
  1737. */
  1738. function get_comment_id_fields( $post_id = 0 ) {
  1739. if ( empty( $post_id ) ) {
  1740. $post_id = get_the_ID();
  1741. }
  1742. $reply_to_id = isset( $_GET['replytocom'] ) ? (int) $_GET['replytocom'] : 0;
  1743. $result = "<input type='hidden' name='comment_post_ID' value='$post_id' id='comment_post_ID' />\n";
  1744. $result .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$reply_to_id' />\n";
  1745. /**
  1746. * Filters the returned comment ID fields.
  1747. *
  1748. * @since 3.0.0
  1749. *
  1750. * @param string $result The HTML-formatted hidden ID field comment elements.
  1751. * @param int $post_id The post ID.
  1752. * @param int $reply_to_id The ID of the comment being replied to.
  1753. */
  1754. return apply_filters( 'comment_id_fields', $result, $post_id, $reply_to_id );
  1755. }
  1756. /**
  1757. * Outputs hidden input HTML for replying to comments.
  1758. *
  1759. * Adds two hidden inputs to the comment form to identify the `comment_post_ID`
  1760. * and `comment_parent` values for threaded comments.
  1761. *
  1762. * This tag must be within the `<form>` section of the `comments.php` template.
  1763. *
  1764. * @since 2.7.0
  1765. *
  1766. * @see get_comment_id_fields()
  1767. *
  1768. * @param int $post_id Optional. Post ID. Defaults to the current post ID.
  1769. */
  1770. function comment_id_fields( $post_id = 0 ) {
  1771. echo get_comment_id_fields( $post_id );
  1772. }
  1773. /**
  1774. * Displays text based on comment reply status.
  1775. *
  1776. * Only affects users with JavaScript disabled.
  1777. *
  1778. * @internal The $comment global must be present to allow template tags access to the current
  1779. * comment. See https://core.trac.wordpress.org/changeset/36512.
  1780. *
  1781. * @since 2.7.0
  1782. *
  1783. * @global WP_Comment $comment Global comment object.
  1784. *
  1785. * @param string|false $no_reply_text Optional. Text to display when not replying to a comment.
  1786. * Default false.
  1787. * @param string|false $reply_text Optional. Text to display when replying to a comment.
  1788. * Default false. Accepts "%s" for the author of the comment
  1789. * being replied to.
  1790. * @param bool $link_to_parent Optional. Boolean to control making the author's name a link
  1791. * to their comment. Default true.
  1792. */
  1793. function comment_form_title( $no_reply_text = false, $reply_text = false, $link_to_parent = true ) {
  1794. global $comment;
  1795. if ( false === $no_reply_text ) {
  1796. $no_reply_text = __( 'Leave a Reply' );
  1797. }
  1798. if ( false === $reply_text ) {
  1799. /* translators: %s: Author of the comment being replied to. */
  1800. $reply_text = __( 'Leave a Reply to %s' );
  1801. }
  1802. $reply_to_id = isset( $_GET['replytocom'] ) ? (int) $_GET['replytocom'] : 0;
  1803. if ( 0 == $reply_to_id ) {
  1804. echo $no_reply_text;
  1805. } else {
  1806. // Sets the global so that template tags can be used in the comment form.
  1807. $comment = get_comment( $reply_to_id );
  1808. if ( $link_to_parent ) {
  1809. $author = '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author( $comment ) . '</a>';
  1810. } else {
  1811. $author = get_comment_author( $comment );
  1812. }
  1813. printf( $reply_text, $author );
  1814. }
  1815. }
  1816. /**
  1817. * Displays a list of comments.
  1818. *
  1819. * Used in the comments.php template to list comments for a particular post.
  1820. *
  1821. * @since 2.7.0
  1822. *
  1823. * @see WP_Query::$comments
  1824. *
  1825. * @global WP_Query $wp_query WordPress Query object.
  1826. * @global int $comment_alt
  1827. * @global int $comment_depth
  1828. * @global int $comment_thread_alt
  1829. * @global bool $overridden_cpage
  1830. * @global bool $in_comment_loop
  1831. *
  1832. * @param string|array $args {
  1833. * Optional. Formatting options.
  1834. *
  1835. * @type object $walker Instance of a Walker class to list comments. Default null.
  1836. * @type int $max_depth The maximum comments depth. Default empty.
  1837. * @type string $style The style of list ordering. Accepts 'ul', 'ol', or 'div'.
  1838. * 'div' will result in no additional list markup. Default 'ul'.
  1839. * @type callable $callback Callback function to use. Default null.
  1840. * @type callable $end-callback Callback function to use at the end. Default null.
  1841. * @type string $type Type of comments to list. Accepts 'all', 'comment',
  1842. * 'pingback', 'trackback', 'pings'. Default 'all'.
  1843. * @type int $page Page ID to list comments for. Default empty.
  1844. * @type int $per_page Number of comments to list per page. Default empty.
  1845. * @type int $avatar_size Height and width dimensions of the avatar size. Default 32.
  1846. * @type bool $reverse_top_level Ordering of the listed comments. If true, will display
  1847. * newest comments first. Default null.
  1848. * @type bool $reverse_children Whether to reverse child comments in the list. Default null.
  1849. * @type string $format How to format the comments list. Accepts 'html5', 'xhtml'.
  1850. * Default 'html5' if the theme supports it.
  1851. * @type bool $short_ping Whether to output short pings. Default false.
  1852. * @type bool $echo Whether to echo the output or return it. Default true.
  1853. * }
  1854. * @param WP_Comment[] $comments Optional. Array of WP_Comment objects.
  1855. * @return void|string Void if 'echo' argument is true, or no comments to list.
  1856. * Otherwise, HTML list of comments.
  1857. */
  1858. function wp_list_comments( $args = array(), $comments = null ) {
  1859. global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
  1860. $in_comment_loop = true;
  1861. $comment_alt = 0;
  1862. $comment_thread_alt = 0;
  1863. $comment_depth = 1;
  1864. $defaults = array(
  1865. 'walker' => null,
  1866. 'max_depth' => '',
  1867. 'style' => 'ul',
  1868. 'callback' => null,
  1869. 'end-callback' => null,
  1870. 'type' => 'all',
  1871. 'page' => '',
  1872. 'per_page' => '',
  1873. 'avatar_size' => 32,
  1874. 'reverse_top_level' => null,
  1875. 'reverse_children' => '',
  1876. 'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',
  1877. 'short_ping' => false,
  1878. 'echo' => true,
  1879. );
  1880. $parsed_args = wp_parse_args( $args, $defaults );
  1881. /**
  1882. * Filters the arguments used in retrieving the comment list.
  1883. *
  1884. * @since 4.0.0
  1885. *
  1886. * @see wp_list_comments()
  1887. *
  1888. * @param array $parsed_args An array of arguments for displaying comments.
  1889. */
  1890. $parsed_args = apply_filters( 'wp_list_comments_args', $parsed_args );
  1891. // Figure out what comments we'll be looping through ($_comments).
  1892. if ( null !== $comments ) {
  1893. $comments = (array) $comments;
  1894. if ( empty( $comments ) ) {
  1895. return;
  1896. }
  1897. if ( 'all' !== $parsed_args['type'] ) {
  1898. $comments_by_type = separate_comments( $comments );
  1899. if ( empty( $comments_by_type[ $parsed_args['type'] ] ) ) {
  1900. return;
  1901. }
  1902. $_comments = $comments_by_type[ $parsed_args['type'] ];
  1903. } else {
  1904. $_comments = $comments;
  1905. }
  1906. } else {
  1907. /*
  1908. * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
  1909. * perform a separate comment query and allow Walker_Comment to paginate.
  1910. */
  1911. if ( $parsed_args['page'] || $parsed_args['per_page'] ) {
  1912. $current_cpage = get_query_var( 'cpage' );
  1913. if ( ! $current_cpage ) {
  1914. $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
  1915. }
  1916. $current_per_page = get_query_var( 'comments_per_page' );
  1917. if ( $parsed_args['page'] != $current_cpage || $parsed_args['per_page'] != $current_per_page ) {
  1918. $comment_args = array(
  1919. 'post_id' => get_the_ID(),
  1920. 'orderby' => 'comment_date_gmt',
  1921. 'order' => 'ASC',
  1922. 'status' => 'approve',
  1923. );
  1924. if ( is_user_logged_in() ) {
  1925. $comment_args['include_unapproved'] = array( get_current_user_id() );
  1926. } else {
  1927. $unapproved_email = wp_get_unapproved_comment_author_email();
  1928. if ( $unapproved_email ) {
  1929. $comment_args['include_unapproved'] = array( $unapproved_email );
  1930. }
  1931. }
  1932. $comments = get_comments( $comment_args );
  1933. if ( 'all' !== $parsed_args['type'] ) {
  1934. $comments_by_type = separate_comments( $comments );
  1935. if ( empty( $comments_by_type[ $parsed_args['type'] ] ) ) {
  1936. return;
  1937. }
  1938. $_comments = $comments_by_type[ $parsed_args['type'] ];
  1939. } else {
  1940. $_comments = $comments;
  1941. }
  1942. }
  1943. // Otherwise, fall back on the comments from `$wp_query->comments`.
  1944. } else {
  1945. if ( empty( $wp_query->comments ) ) {
  1946. return;
  1947. }
  1948. if ( 'all' !== $parsed_args['type'] ) {
  1949. if ( empty( $wp_query->comments_by_type ) ) {
  1950. $wp_query->comments_by_type = separate_comments( $wp_query->comments );
  1951. }
  1952. if ( empty( $wp_query->comments_by_type[ $parsed_args['type'] ] ) ) {
  1953. return;
  1954. }
  1955. $_comments = $wp_query->comments_by_type[ $parsed_args['type'] ];
  1956. } else {
  1957. $_comments = $wp_query->comments;
  1958. }
  1959. if ( $wp_query->max_num_comment_pages ) {
  1960. $default_comments_page = get_option( 'default_comments_page' );
  1961. $cpage = get_query_var( 'cpage' );
  1962. if ( 'newest' === $default_comments_page ) {
  1963. $parsed_args['cpage'] = $cpage;
  1964. /*
  1965. * When first page shows oldest comments, post permalink is the same as
  1966. * the comment permalink.
  1967. */
  1968. } elseif ( 1 == $cpage ) {
  1969. $parsed_args['cpage'] = '';
  1970. } else {
  1971. $parsed_args['cpage'] = $cpage;
  1972. }
  1973. $parsed_args['page'] = 0;
  1974. $parsed_args['per_page'] = 0;
  1975. }
  1976. }
  1977. }
  1978. if ( '' === $parsed_args['per_page'] && get_option( 'page_comments' ) ) {
  1979. $parsed_args['per_page'] = get_query_var( 'comments_per_page' );
  1980. }
  1981. if ( empty( $parsed_args['per_page'] ) ) {
  1982. $parsed_args['per_page'] = 0;
  1983. $parsed_args['page'] = 0;
  1984. }
  1985. if ( '' === $parsed_args['max_depth'] ) {
  1986. if ( get_option( 'thread_comments' ) ) {
  1987. $parsed_args['max_depth'] = get_option( 'thread_comments_depth' );
  1988. } else {
  1989. $parsed_args['max_depth'] = -1;
  1990. }
  1991. }
  1992. if ( '' === $parsed_args['page'] ) {
  1993. if ( empty( $overridden_cpage ) ) {
  1994. $parsed_args['page'] = get_query_var( 'cpage' );
  1995. } else {
  1996. $threaded = ( -1 != $parsed_args['max_depth'] );
  1997. $parsed_args['page'] = ( 'newest' === get_option( 'default_comments_page' ) ) ? get_comment_pages_count( $_comments, $parsed_args['per_page'], $threaded ) : 1;
  1998. set_query_var( 'cpage', $parsed_args['page'] );
  1999. }
  2000. }
  2001. // Validation check.
  2002. $parsed_args['page'] = (int) $parsed_args['page'];
  2003. if ( 0 == $parsed_args['page'] && 0 != $parsed_args['per_page'] ) {
  2004. $parsed_args['page'] = 1;
  2005. }
  2006. if ( null === $parsed_args['reverse_top_level'] ) {
  2007. $parsed_args['reverse_top_level'] = ( 'desc' === get_option( 'comment_order' ) );
  2008. }
  2009. wp_queue_comments_for_comment_meta_lazyload( $_comments );
  2010. if ( empty( $parsed_args['walker'] ) ) {
  2011. $walker = new Walker_Comment;
  2012. } else {
  2013. $walker = $parsed_args['walker'];
  2014. }
  2015. $output = $walker->paged_walk( $_comments, $parsed_args['max_depth'], $parsed_args['page'], $parsed_args['per_page'], $parsed_args );
  2016. $in_comment_loop = false;
  2017. if ( $parsed_args['echo'] ) {
  2018. echo $output;
  2019. } else {
  2020. return $output;
  2021. }
  2022. }
  2023. /**
  2024. * Outputs a complete commenting form for use within a template.
  2025. *
  2026. * Most strings and form fields may be controlled through the `$args` array passed
  2027. * into the function, while you may also choose to use the {@see 'comment_form_default_fields'}
  2028. * filter to modify the array of default fields if you'd just like to add a new
  2029. * one or remove a single field. All fields are also individually passed through
  2030. * a filter of the {@see 'comment_form_field_$name'} where `$name` is the key used
  2031. * in the array of fields.
  2032. *
  2033. * @since 3.0.0
  2034. * @since 4.1.0 Introduced the 'class_submit' argument.
  2035. * @since 4.2.0 Introduced the 'submit_button' and 'submit_fields' arguments.
  2036. * @since 4.4.0 Introduced the 'class_form', 'title_reply_before', 'title_reply_after',
  2037. * 'cancel_reply_before', and 'cancel_reply_after' arguments.
  2038. * @since 4.5.0 The 'author', 'email', and 'url' form fields are limited to 245, 100,
  2039. * and 200 characters, respectively.
  2040. * @since 4.6.0 Introduced the 'action' argument.
  2041. * @since 4.9.6 Introduced the 'cookies' default comment field.
  2042. * @since 5.5.0 Introduced the 'class_container' argument.
  2043. *
  2044. * @param array $args {
  2045. * Optional. Default arguments and form fields to override.
  2046. *
  2047. * @type array $fields {
  2048. * Default comment fields, filterable by default via the {@see 'comment_form_default_fields'} hook.
  2049. *
  2050. * @type string $author Comment author field HTML.
  2051. * @type string $email Comment author email field HTML.
  2052. * @type string $url Comment author URL field HTML.
  2053. * @type string $cookies Comment cookie opt-in field HTML.
  2054. * }
  2055. * @type string $comment_field The comment textarea field HTML.
  2056. * @type string $must_log_in HTML element for a 'must be logged in to comment' message.
  2057. * @type string $logged_in_as The HTML for the 'logged in as [user]' message, the Edit profile link,
  2058. * and the Log out link.
  2059. * @type string $comment_notes_before HTML element for a message displayed before the comment fields
  2060. * if the user is not logged in.
  2061. * Default 'Your email address will not be published.'.
  2062. * @type string $comment_notes_after HTML element for a message displayed after the textarea field.
  2063. * @type string $action The comment form element action attribute. Default '/wp-comments-post.php'.
  2064. * @type string $id_form The comment form element id attribute. Default 'commentform'.
  2065. * @type string $id_submit The comment submit element id attribute. Default 'submit'.
  2066. * @type string $class_container The comment form container class attribute. Default 'comment-respond'.
  2067. * @type string $class_form The comment form element class attribute. Default 'comment-form'.
  2068. * @type string $class_submit The comment submit element class attribute. Default 'submit'.
  2069. * @type string $name_submit The comment submit element name attribute. Default 'submit'.
  2070. * @type string $title_reply The translatable 'reply' button label. Default 'Leave a Reply'.
  2071. * @type string $title_reply_to The translatable 'reply-to' button label. Default 'Leave a Reply to %s',
  2072. * where %s is the author of the comment being replied to.
  2073. * @type string $title_reply_before HTML displayed before the comment form title.
  2074. * Default: '<h3 id="reply-title" class="comment-reply-title">'.
  2075. * @type string $title_reply_after HTML displayed after the comment form title.
  2076. * Default: '</h3>'.
  2077. * @type string $cancel_reply_before HTML displayed before the cancel reply link.
  2078. * @type string $cancel_reply_after HTML displayed after the cancel reply link.
  2079. * @type string $cancel_reply_link The translatable 'cancel reply' button label. Default 'Cancel reply'.
  2080. * @type string $label_submit The translatable 'submit' button label. Default 'Post a comment'.
  2081. * @type string $submit_button HTML format for the Submit button.
  2082. * Default: '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />'.
  2083. * @type string $submit_field HTML format for the markup surrounding the Submit button and comment hidden
  2084. * fields. Default: '<p class="form-submit">%1$s %2$s</p>', where %1$s is the
  2085. * submit button markup and %2$s is the comment hidden fields.
  2086. * @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'.
  2087. * }
  2088. * @param int|WP_Post $post Post ID or WP_Post object to generate the form for. Default current post.
  2089. */
  2090. function comment_form( $args = array(), $post = null ) {
  2091. $post = get_post( $post );
  2092. // Exit the function if the post is invalid or comments are closed.
  2093. if ( ! $post || ! comments_open( $post ) ) {
  2094. /**
  2095. * Fires after the comment form if comments are closed.
  2096. *
  2097. * For backward compatibility, this action also fires if comment_form()
  2098. * is called with an invalid post object or ID.
  2099. *
  2100. * @since 3.0.0
  2101. */
  2102. do_action( 'comment_form_comments_closed' );
  2103. return;
  2104. }
  2105. $post_id = $post->ID;
  2106. $commenter = wp_get_current_commenter();
  2107. $user = wp_get_current_user();
  2108. $user_identity = $user->exists() ? $user->display_name : '';
  2109. $args = wp_parse_args( $args );
  2110. if ( ! isset( $args['format'] ) ) {
  2111. $args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
  2112. }
  2113. $req = get_option( 'require_name_email' );
  2114. $html5 = 'html5' === $args['format'];
  2115. // Define attributes in HTML5 or XHTML syntax.
  2116. $required_attribute = ( $html5 ? ' required' : ' required="required"' );
  2117. $checked_attribute = ( $html5 ? ' checked' : ' checked="checked"' );
  2118. // Identify required fields visually and create a message about the indicator.
  2119. $required_indicator = ' ' . wp_required_field_indicator();
  2120. $required_text = ' ' . wp_required_field_message();
  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" autocomplete="name"%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" autocomplete="email"%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" autocomplete="url" />',
  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. /**
  2181. * Filters the default comment form fields.
  2182. *
  2183. * @since 3.0.0
  2184. *
  2185. * @param string[] $fields Array of the default comment fields.
  2186. */
  2187. $fields = apply_filters( 'comment_form_default_fields', $fields );
  2188. $defaults = array(
  2189. 'fields' => $fields,
  2190. 'comment_field' => sprintf(
  2191. '<p class="comment-form-comment">%s %s</p>',
  2192. sprintf(
  2193. '<label for="comment">%s%s</label>',
  2194. _x( 'Comment', 'noun' ),
  2195. $required_indicator
  2196. ),
  2197. '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525"' . $required_attribute . '></textarea>'
  2198. ),
  2199. 'must_log_in' => sprintf(
  2200. '<p class="must-log-in">%s</p>',
  2201. sprintf(
  2202. /* translators: %s: Login URL. */
  2203. __( 'You must be <a href="%s">logged in</a> to post a comment.' ),
  2204. /** This filter is documented in wp-includes/link-template.php */
  2205. wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) )
  2206. )
  2207. ),
  2208. 'logged_in_as' => sprintf(
  2209. '<p class="logged-in-as">%s%s</p>',
  2210. sprintf(
  2211. /* translators: 1: User name, 2: Edit user link, 3: Logout URL. */
  2212. __( 'Logged in as %1$s. <a href="%2$s">Edit your profile</a>. <a href="%3$s">Log out?</a>' ),
  2213. $user_identity,
  2214. get_edit_user_link(),
  2215. /** This filter is documented in wp-includes/link-template.php */
  2216. wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) )
  2217. ),
  2218. $required_text
  2219. ),
  2220. 'comment_notes_before' => sprintf(
  2221. '<p class="comment-notes">%s%s</p>',
  2222. sprintf(
  2223. '<span id="email-notes">%s</span>',
  2224. __( 'Your email address will not be published.' )
  2225. ),
  2226. $required_text
  2227. ),
  2228. 'comment_notes_after' => '',
  2229. 'action' => site_url( '/wp-comments-post.php' ),
  2230. 'id_form' => 'commentform',
  2231. 'id_submit' => 'submit',
  2232. 'class_container' => 'comment-respond',
  2233. 'class_form' => 'comment-form',
  2234. 'class_submit' => 'submit',
  2235. 'name_submit' => 'submit',
  2236. 'title_reply' => __( 'Leave a Reply' ),
  2237. /* translators: %s: Author of the comment being replied to. */
  2238. 'title_reply_to' => __( 'Leave a Reply to %s' ),
  2239. 'title_reply_before' => '<h3 id="reply-title" class="comment-reply-title">',
  2240. 'title_reply_after' => '</h3>',
  2241. 'cancel_reply_before' => ' <small>',
  2242. 'cancel_reply_after' => '</small>',
  2243. 'cancel_reply_link' => __( 'Cancel reply' ),
  2244. 'label_submit' => __( 'Post Comment' ),
  2245. 'submit_button' => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',
  2246. 'submit_field' => '<p class="form-submit">%1$s %2$s</p>',
  2247. 'format' => 'xhtml',
  2248. );
  2249. /**
  2250. * Filters the comment form default arguments.
  2251. *
  2252. * Use {@see 'comment_form_default_fields'} to filter the comment fields.
  2253. *
  2254. * @since 3.0.0
  2255. *
  2256. * @param array $defaults The default comment form arguments.
  2257. */
  2258. $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
  2259. // Ensure that the filtered arguments contain all required default values.
  2260. $args = array_merge( $defaults, $args );
  2261. // Remove `aria-describedby` from the email field if there's no associated description.
  2262. if ( isset( $args['fields']['email'] ) && false === strpos( $args['comment_notes_before'], 'id="email-notes"' ) ) {
  2263. $args['fields']['email'] = str_replace(
  2264. ' aria-describedby="email-notes"',
  2265. '',
  2266. $args['fields']['email']
  2267. );
  2268. }
  2269. /**
  2270. * Fires before the comment form.
  2271. *
  2272. * @since 3.0.0
  2273. */
  2274. do_action( 'comment_form_before' );
  2275. ?>
  2276. <div id="respond" class="<?php echo esc_attr( $args['class_container'] ); ?>">
  2277. <?php
  2278. echo $args['title_reply_before'];
  2279. comment_form_title( $args['title_reply'], $args['title_reply_to'] );
  2280. if ( get_option( 'thread_comments' ) ) {
  2281. echo $args['cancel_reply_before'];
  2282. cancel_comment_reply_link( $args['cancel_reply_link'] );
  2283. echo $args['cancel_reply_after'];
  2284. }
  2285. echo $args['title_reply_after'];
  2286. if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) :
  2287. echo $args['must_log_in'];
  2288. /**
  2289. * Fires after the HTML-formatted 'must log in after' message in the comment form.
  2290. *
  2291. * @since 3.0.0
  2292. */
  2293. do_action( 'comment_form_must_log_in_after' );
  2294. else :
  2295. printf(
  2296. '<form action="%s" method="post" id="%s" class="%s"%s>',
  2297. esc_url( $args['action'] ),
  2298. esc_attr( $args['id_form'] ),
  2299. esc_attr( $args['class_form'] ),
  2300. ( $html5 ? ' novalidate' : '' )
  2301. );
  2302. /**
  2303. * Fires at the top of the comment form, inside the form tag.
  2304. *
  2305. * @since 3.0.0
  2306. */
  2307. do_action( 'comment_form_top' );
  2308. if ( is_user_logged_in() ) :
  2309. /**
  2310. * Filters the 'logged in' message for the comment form for display.
  2311. *
  2312. * @since 3.0.0
  2313. *
  2314. * @param string $args_logged_in The HTML for the 'logged in as [user]' message,
  2315. * the Edit profile link, and the Log out link.
  2316. * @param array $commenter An array containing the comment author's
  2317. * username, email, and URL.
  2318. * @param string $user_identity If the commenter is a registered user,
  2319. * the display name, blank otherwise.
  2320. */
  2321. echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );
  2322. /**
  2323. * Fires after the is_user_logged_in() check in the comment form.
  2324. *
  2325. * @since 3.0.0
  2326. *
  2327. * @param array $commenter An array containing the comment author's
  2328. * username, email, and URL.
  2329. * @param string $user_identity If the commenter is a registered user,
  2330. * the display name, blank otherwise.
  2331. */
  2332. do_action( 'comment_form_logged_in_after', $commenter, $user_identity );
  2333. else :
  2334. echo $args['comment_notes_before'];
  2335. endif;
  2336. // Prepare an array of all fields, including the textarea.
  2337. $comment_fields = array( 'comment' => $args['comment_field'] ) + (array) $args['fields'];
  2338. /**
  2339. * Filters the comment form fields, including the textarea.
  2340. *
  2341. * @since 4.4.0
  2342. *
  2343. * @param array $comment_fields The comment fields.
  2344. */
  2345. $comment_fields = apply_filters( 'comment_form_fields', $comment_fields );
  2346. // Get an array of field names, excluding the textarea.
  2347. $comment_field_keys = array_diff( array_keys( $comment_fields ), array( 'comment' ) );
  2348. // Get the first and the last field name, excluding the textarea.
  2349. $first_field = reset( $comment_field_keys );
  2350. $last_field = end( $comment_field_keys );
  2351. foreach ( $comment_fields as $name => $field ) {
  2352. if ( 'comment' === $name ) {
  2353. /**
  2354. * Filters the content of the comment textarea field for display.
  2355. *
  2356. * @since 3.0.0
  2357. *
  2358. * @param string $args_comment_field The content of the comment textarea field.
  2359. */
  2360. echo apply_filters( 'comment_form_field_comment', $field );
  2361. echo $args['comment_notes_after'];
  2362. } elseif ( ! is_user_logged_in() ) {
  2363. if ( $first_field === $name ) {
  2364. /**
  2365. * Fires before the comment fields in the comment form, excluding the textarea.
  2366. *
  2367. * @since 3.0.0
  2368. */
  2369. do_action( 'comment_form_before_fields' );
  2370. }
  2371. /**
  2372. * Filters a comment form field for display.
  2373. *
  2374. * The dynamic portion of the hook name, `$name`, refers to the name
  2375. * of the comment form field.
  2376. *
  2377. * Possible hook names include:
  2378. *
  2379. * - `comment_form_field_comment`
  2380. * - `comment_form_field_author`
  2381. * - `comment_form_field_email`
  2382. * - `comment_form_field_url`
  2383. * - `comment_form_field_cookies`
  2384. *
  2385. * @since 3.0.0
  2386. *
  2387. * @param string $field The HTML-formatted output of the comment form field.
  2388. */
  2389. echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
  2390. if ( $last_field === $name ) {
  2391. /**
  2392. * Fires after the comment fields in the comment form, excluding the textarea.
  2393. *
  2394. * @since 3.0.0
  2395. */
  2396. do_action( 'comment_form_after_fields' );
  2397. }
  2398. }
  2399. }
  2400. $submit_button = sprintf(
  2401. $args['submit_button'],
  2402. esc_attr( $args['name_submit'] ),
  2403. esc_attr( $args['id_submit'] ),
  2404. esc_attr( $args['class_submit'] ),
  2405. esc_attr( $args['label_submit'] )
  2406. );
  2407. /**
  2408. * Filters the submit button for the comment form to display.
  2409. *
  2410. * @since 4.2.0
  2411. *
  2412. * @param string $submit_button HTML markup for the submit button.
  2413. * @param array $args Arguments passed to comment_form().
  2414. */
  2415. $submit_button = apply_filters( 'comment_form_submit_button', $submit_button, $args );
  2416. $submit_field = sprintf(
  2417. $args['submit_field'],
  2418. $submit_button,
  2419. get_comment_id_fields( $post_id )
  2420. );
  2421. /**
  2422. * Filters the submit field for the comment form to display.
  2423. *
  2424. * The submit field includes the submit button, hidden fields for the
  2425. * comment form, and any wrapper markup.
  2426. *
  2427. * @since 4.2.0
  2428. *
  2429. * @param string $submit_field HTML markup for the submit field.
  2430. * @param array $args Arguments passed to comment_form().
  2431. */
  2432. echo apply_filters( 'comment_form_submit_field', $submit_field, $args );
  2433. /**
  2434. * Fires at the bottom of the comment form, inside the closing form tag.
  2435. *
  2436. * @since 1.5.0
  2437. *
  2438. * @param int $post_id The post ID.
  2439. */
  2440. do_action( 'comment_form', $post_id );
  2441. echo '</form>';
  2442. endif;
  2443. ?>
  2444. </div><!-- #respond -->
  2445. <?php
  2446. /**
  2447. * Fires after the comment form.
  2448. *
  2449. * @since 3.0.0
  2450. */
  2451. do_action( 'comment_form_after' );
  2452. }