comment-template.php 89 KB

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