comment.php 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626
  1. <?php
  2. /**
  3. * Core Comment API
  4. *
  5. * @package WordPress
  6. * @subpackage Comment
  7. */
  8. /**
  9. * Check whether a comment passes internal checks to be allowed to add.
  10. *
  11. * If manual comment moderation is set in the administration, then all checks,
  12. * regardless of their type and whitelist, will fail and the function will
  13. * return false.
  14. *
  15. * If the number of links exceeds the amount in the administration, then the
  16. * check fails. If any of the parameter contents match the blacklist of words,
  17. * then the check fails.
  18. *
  19. * If the comment author was approved before, then the comment is automatically
  20. * whitelisted.
  21. *
  22. * If all checks pass, the function will return true.
  23. *
  24. * @since 1.2.0
  25. *
  26. * @global wpdb $wpdb WordPress database abstraction object.
  27. *
  28. * @param string $author Comment author name.
  29. * @param string $email Comment author email.
  30. * @param string $url Comment author URL.
  31. * @param string $comment Content of the comment.
  32. * @param string $user_ip Comment author IP address.
  33. * @param string $user_agent Comment author User-Agent.
  34. * @param string $comment_type Comment type, either user-submitted comment,
  35. * trackback, or pingback.
  36. * @return bool If all checks pass, true, otherwise false.
  37. */
  38. function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) {
  39. global $wpdb;
  40. // If manual moderation is enabled, skip all checks and return false.
  41. if ( 1 == get_option( 'comment_moderation' ) ) {
  42. return false;
  43. }
  44. /** This filter is documented in wp-includes/comment-template.php */
  45. $comment = apply_filters( 'comment_text', $comment, null, array() );
  46. // Check for the number of external links if a max allowed number is set.
  47. $max_links = get_option( 'comment_max_links' );
  48. if ( $max_links ) {
  49. $num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out );
  50. /**
  51. * Filters the number of links found in a comment.
  52. *
  53. * @since 3.0.0
  54. * @since 4.7.0 Added the `$comment` parameter.
  55. *
  56. * @param int $num_links The number of links found.
  57. * @param string $url Comment author's URL. Included in allowed links total.
  58. * @param string $comment Content of the comment.
  59. */
  60. $num_links = apply_filters( 'comment_max_links_url', $num_links, $url, $comment );
  61. /*
  62. * If the number of links in the comment exceeds the allowed amount,
  63. * fail the check by returning false.
  64. */
  65. if ( $num_links >= $max_links ) {
  66. return false;
  67. }
  68. }
  69. $mod_keys = trim( get_option( 'moderation_keys' ) );
  70. // If moderation 'keys' (keywords) are set, process them.
  71. if ( ! empty( $mod_keys ) ) {
  72. $words = explode( "\n", $mod_keys );
  73. foreach ( (array) $words as $word ) {
  74. $word = trim( $word );
  75. // Skip empty lines.
  76. if ( empty( $word ) ) {
  77. continue;
  78. }
  79. /*
  80. * Do some escaping magic so that '#' (number of) characters in the spam
  81. * words don't break things:
  82. */
  83. $word = preg_quote( $word, '#' );
  84. /*
  85. * Check the comment fields for moderation keywords. If any are found,
  86. * fail the check for the given field by returning false.
  87. */
  88. $pattern = "#$word#i";
  89. if ( preg_match( $pattern, $author ) ) {
  90. return false;
  91. }
  92. if ( preg_match( $pattern, $email ) ) {
  93. return false;
  94. }
  95. if ( preg_match( $pattern, $url ) ) {
  96. return false;
  97. }
  98. if ( preg_match( $pattern, $comment ) ) {
  99. return false;
  100. }
  101. if ( preg_match( $pattern, $user_ip ) ) {
  102. return false;
  103. }
  104. if ( preg_match( $pattern, $user_agent ) ) {
  105. return false;
  106. }
  107. }
  108. }
  109. /*
  110. * Check if the option to approve comments by previously-approved authors is enabled.
  111. *
  112. * If it is enabled, check whether the comment author has a previously-approved comment,
  113. * as well as whether there are any moderation keywords (if set) present in the author
  114. * email address. If both checks pass, return true. Otherwise, return false.
  115. */
  116. if ( 1 == get_option( 'comment_whitelist' ) ) {
  117. if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) {
  118. $comment_user = get_user_by( 'email', wp_unslash( $email ) );
  119. if ( ! empty( $comment_user->ID ) ) {
  120. $ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved FROM $wpdb->comments WHERE user_id = %d AND comment_approved = '1' LIMIT 1", $comment_user->ID ) );
  121. } else {
  122. // expected_slashed ($author, $email)
  123. $ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved FROM $wpdb->comments WHERE comment_author = %s AND comment_author_email = %s and comment_approved = '1' LIMIT 1", $author, $email ) );
  124. }
  125. if ( ( 1 == $ok_to_comment ) &&
  126. ( empty( $mod_keys ) || false === strpos( $email, $mod_keys ) ) ) {
  127. return true;
  128. } else {
  129. return false;
  130. }
  131. } else {
  132. return false;
  133. }
  134. }
  135. return true;
  136. }
  137. /**
  138. * Retrieve the approved comments for post $post_id.
  139. *
  140. * @since 2.0.0
  141. * @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.
  142. *
  143. * @param int $post_id The ID of the post.
  144. * @param array $args Optional. See WP_Comment_Query::__construct() for information on accepted arguments.
  145. * @return int|array $comments The approved comments, or number of comments if `$count`
  146. * argument is true.
  147. */
  148. function get_approved_comments( $post_id, $args = array() ) {
  149. if ( ! $post_id ) {
  150. return array();
  151. }
  152. $defaults = array(
  153. 'status' => 1,
  154. 'post_id' => $post_id,
  155. 'order' => 'ASC',
  156. );
  157. $parsed_args = wp_parse_args( $args, $defaults );
  158. $query = new WP_Comment_Query;
  159. return $query->query( $parsed_args );
  160. }
  161. /**
  162. * Retrieves comment data given a comment ID or comment object.
  163. *
  164. * If an object is passed then the comment data will be cached and then returned
  165. * after being passed through a filter. If the comment is empty, then the global
  166. * comment variable will be used, if it is set.
  167. *
  168. * @since 2.0.0
  169. *
  170. * @global WP_Comment $comment Global comment object.
  171. *
  172. * @param WP_Comment|string|int $comment Comment to retrieve.
  173. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
  174. * a WP_Comment object, an associative array, or a numeric array, respectively. Default OBJECT.
  175. * @return WP_Comment|array|null Depends on $output value.
  176. */
  177. function get_comment( &$comment = null, $output = OBJECT ) {
  178. if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) {
  179. $comment = $GLOBALS['comment'];
  180. }
  181. if ( $comment instanceof WP_Comment ) {
  182. $_comment = $comment;
  183. } elseif ( is_object( $comment ) ) {
  184. $_comment = new WP_Comment( $comment );
  185. } else {
  186. $_comment = WP_Comment::get_instance( $comment );
  187. }
  188. if ( ! $_comment ) {
  189. return null;
  190. }
  191. /**
  192. * Fires after a comment is retrieved.
  193. *
  194. * @since 2.3.0
  195. *
  196. * @param mixed $_comment Comment data.
  197. */
  198. $_comment = apply_filters( 'get_comment', $_comment );
  199. if ( $output == OBJECT ) {
  200. return $_comment;
  201. } elseif ( $output == ARRAY_A ) {
  202. return $_comment->to_array();
  203. } elseif ( $output == ARRAY_N ) {
  204. return array_values( $_comment->to_array() );
  205. }
  206. return $_comment;
  207. }
  208. /**
  209. * Retrieve a list of comments.
  210. *
  211. * The comment list can be for the blog as a whole or for an individual post.
  212. *
  213. * @since 2.7.0
  214. *
  215. * @param string|array $args Optional. Array or string of arguments. See WP_Comment_Query::__construct()
  216. * for information on accepted arguments. Default empty.
  217. * @return int|array List of comments or number of found comments if `$count` argument is true.
  218. */
  219. function get_comments( $args = '' ) {
  220. $query = new WP_Comment_Query;
  221. return $query->query( $args );
  222. }
  223. /**
  224. * Retrieve all of the WordPress supported comment statuses.
  225. *
  226. * Comments have a limited set of valid status values, this provides the comment
  227. * status values and descriptions.
  228. *
  229. * @since 2.7.0
  230. *
  231. * @return array List of comment statuses.
  232. */
  233. function get_comment_statuses() {
  234. $status = array(
  235. 'hold' => __( 'Unapproved' ),
  236. 'approve' => _x( 'Approved', 'comment status' ),
  237. 'spam' => _x( 'Spam', 'comment status' ),
  238. 'trash' => _x( 'Trash', 'comment status' ),
  239. );
  240. return $status;
  241. }
  242. /**
  243. * Gets the default comment status for a post type.
  244. *
  245. * @since 4.3.0
  246. *
  247. * @param string $post_type Optional. Post type. Default 'post'.
  248. * @param string $comment_type Optional. Comment type. Default 'comment'.
  249. * @return string Expected return value is 'open' or 'closed'.
  250. */
  251. function get_default_comment_status( $post_type = 'post', $comment_type = 'comment' ) {
  252. switch ( $comment_type ) {
  253. case 'pingback':
  254. case 'trackback':
  255. $supports = 'trackbacks';
  256. $option = 'ping';
  257. break;
  258. default:
  259. $supports = 'comments';
  260. $option = 'comment';
  261. break;
  262. }
  263. // Set the status.
  264. if ( 'page' === $post_type ) {
  265. $status = 'closed';
  266. } elseif ( post_type_supports( $post_type, $supports ) ) {
  267. $status = get_option( "default_{$option}_status" );
  268. } else {
  269. $status = 'closed';
  270. }
  271. /**
  272. * Filters the default comment status for the given post type.
  273. *
  274. * @since 4.3.0
  275. *
  276. * @param string $status Default status for the given post type,
  277. * either 'open' or 'closed'.
  278. * @param string $post_type Post type. Default is `post`.
  279. * @param string $comment_type Type of comment. Default is `comment`.
  280. */
  281. return apply_filters( 'get_default_comment_status', $status, $post_type, $comment_type );
  282. }
  283. /**
  284. * The date the last comment was modified.
  285. *
  286. * @since 1.5.0
  287. * @since 4.7.0 Replaced caching the modified date in a local static variable
  288. * with the Object Cache API.
  289. *
  290. * @global wpdb $wpdb WordPress database abstraction object.
  291. *
  292. * @param string $timezone Which timezone to use in reference to 'gmt', 'blog', or 'server' locations.
  293. * @return string|false Last comment modified date on success, false on failure.
  294. */
  295. function get_lastcommentmodified( $timezone = 'server' ) {
  296. global $wpdb;
  297. $timezone = strtolower( $timezone );
  298. $key = "lastcommentmodified:$timezone";
  299. $comment_modified_date = wp_cache_get( $key, 'timeinfo' );
  300. if ( false !== $comment_modified_date ) {
  301. return $comment_modified_date;
  302. }
  303. switch ( $timezone ) {
  304. case 'gmt':
  305. $comment_modified_date = $wpdb->get_var( "SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" );
  306. break;
  307. case 'blog':
  308. $comment_modified_date = $wpdb->get_var( "SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" );
  309. break;
  310. case 'server':
  311. $add_seconds_server = gmdate( 'Z' );
  312. $comment_modified_date = $wpdb->get_var( $wpdb->prepare( "SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1", $add_seconds_server ) );
  313. break;
  314. }
  315. if ( $comment_modified_date ) {
  316. wp_cache_set( $key, $comment_modified_date, 'timeinfo' );
  317. return $comment_modified_date;
  318. }
  319. return false;
  320. }
  321. /**
  322. * Retrieves the total comment counts for the whole site or a single post.
  323. *
  324. * Unlike wp_count_comments(), this function always returns the live comment counts without caching.
  325. *
  326. * @since 2.0.0
  327. *
  328. * @global wpdb $wpdb WordPress database abstraction object.
  329. *
  330. * @param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that
  331. * comment counts for the whole site will be retrieved.
  332. * @return array() {
  333. * The number of comments keyed by their status.
  334. *
  335. * @type int|string $approved The number of approved comments.
  336. * @type int|string $awaiting_moderation The number of comments awaiting moderation (a.k.a. pending).
  337. * @type int|string $spam The number of spam comments.
  338. * @type int|string $trash The number of trashed comments.
  339. * @type int|string $post-trashed The number of comments for posts that are in the trash.
  340. * @type int $total_comments The total number of non-trashed comments, including spam.
  341. * @type int $all The total number of pending or approved comments.
  342. * }
  343. */
  344. function get_comment_count( $post_id = 0 ) {
  345. global $wpdb;
  346. $post_id = (int) $post_id;
  347. $where = '';
  348. if ( $post_id > 0 ) {
  349. $where = $wpdb->prepare( 'WHERE comment_post_ID = %d', $post_id );
  350. }
  351. $totals = (array) $wpdb->get_results(
  352. "
  353. SELECT comment_approved, COUNT( * ) AS total
  354. FROM {$wpdb->comments}
  355. {$where}
  356. GROUP BY comment_approved
  357. ",
  358. ARRAY_A
  359. );
  360. $comment_count = array(
  361. 'approved' => 0,
  362. 'awaiting_moderation' => 0,
  363. 'spam' => 0,
  364. 'trash' => 0,
  365. 'post-trashed' => 0,
  366. 'total_comments' => 0,
  367. 'all' => 0,
  368. );
  369. foreach ( $totals as $row ) {
  370. switch ( $row['comment_approved'] ) {
  371. case 'trash':
  372. $comment_count['trash'] = $row['total'];
  373. break;
  374. case 'post-trashed':
  375. $comment_count['post-trashed'] = $row['total'];
  376. break;
  377. case 'spam':
  378. $comment_count['spam'] = $row['total'];
  379. $comment_count['total_comments'] += $row['total'];
  380. break;
  381. case '1':
  382. $comment_count['approved'] = $row['total'];
  383. $comment_count['total_comments'] += $row['total'];
  384. $comment_count['all'] += $row['total'];
  385. break;
  386. case '0':
  387. $comment_count['awaiting_moderation'] = $row['total'];
  388. $comment_count['total_comments'] += $row['total'];
  389. $comment_count['all'] += $row['total'];
  390. break;
  391. default:
  392. break;
  393. }
  394. }
  395. return $comment_count;
  396. }
  397. //
  398. // Comment meta functions
  399. //
  400. /**
  401. * Add meta data field to a comment.
  402. *
  403. * @since 2.9.0
  404. * @link https://developer.wordpress.org/reference/functions/add_comment_meta/
  405. *
  406. * @param int $comment_id Comment ID.
  407. * @param string $meta_key Metadata name.
  408. * @param mixed $meta_value Metadata value.
  409. * @param bool $unique Optional, default is false. Whether the same key should not be added.
  410. * @return int|bool Meta ID on success, false on failure.
  411. */
  412. function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) {
  413. return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
  414. }
  415. /**
  416. * Remove metadata matching criteria from a comment.
  417. *
  418. * You can match based on the key, or key and value. Removing based on key and
  419. * value, will keep from removing duplicate metadata with the same key. It also
  420. * allows removing all metadata matching key, if needed.
  421. *
  422. * @since 2.9.0
  423. * @link https://developer.wordpress.org/reference/functions/delete_comment_meta/
  424. *
  425. * @param int $comment_id comment ID
  426. * @param string $meta_key Metadata name.
  427. * @param mixed $meta_value Optional. Metadata value.
  428. * @return bool True on success, false on failure.
  429. */
  430. function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) {
  431. return delete_metadata( 'comment', $comment_id, $meta_key, $meta_value );
  432. }
  433. /**
  434. * Retrieve comment meta field for a comment.
  435. *
  436. * @since 2.9.0
  437. * @link https://developer.wordpress.org/reference/functions/get_comment_meta/
  438. *
  439. * @param int $comment_id Comment ID.
  440. * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys.
  441. * @param bool $single Whether to return a single value.
  442. * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
  443. * is true.
  444. */
  445. function get_comment_meta( $comment_id, $key = '', $single = false ) {
  446. return get_metadata( 'comment', $comment_id, $key, $single );
  447. }
  448. /**
  449. * Update comment meta field based on comment ID.
  450. *
  451. * Use the $prev_value parameter to differentiate between meta fields with the
  452. * same key and comment ID.
  453. *
  454. * If the meta field for the comment does not exist, it will be added.
  455. *
  456. * @since 2.9.0
  457. * @link https://developer.wordpress.org/reference/functions/update_comment_meta/
  458. *
  459. * @param int $comment_id Comment ID.
  460. * @param string $meta_key Metadata key.
  461. * @param mixed $meta_value Metadata value.
  462. * @param mixed $prev_value Optional. Previous value to check before removing.
  463. * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
  464. */
  465. function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) {
  466. return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value );
  467. }
  468. /**
  469. * Queues comments for metadata lazy-loading.
  470. *
  471. * @since 4.5.0
  472. *
  473. * @param WP_Comment[] $comments Array of comment objects.
  474. */
  475. function wp_queue_comments_for_comment_meta_lazyload( $comments ) {
  476. // Don't use `wp_list_pluck()` to avoid by-reference manipulation.
  477. $comment_ids = array();
  478. if ( is_array( $comments ) ) {
  479. foreach ( $comments as $comment ) {
  480. if ( $comment instanceof WP_Comment ) {
  481. $comment_ids[] = $comment->comment_ID;
  482. }
  483. }
  484. }
  485. if ( $comment_ids ) {
  486. $lazyloader = wp_metadata_lazyloader();
  487. $lazyloader->queue_objects( 'comment', $comment_ids );
  488. }
  489. }
  490. /**
  491. * Sets the cookies used to store an unauthenticated commentator's identity. Typically used
  492. * to recall previous comments by this commentator that are still held in moderation.
  493. *
  494. * @since 3.4.0
  495. * @since 4.9.6 The `$cookies_consent` parameter was added.
  496. *
  497. * @param WP_Comment $comment Comment object.
  498. * @param WP_User $user Comment author's user object. The user may not exist.
  499. * @param boolean $cookies_consent Optional. Comment author's consent to store cookies. Default true.
  500. */
  501. function wp_set_comment_cookies( $comment, $user, $cookies_consent = true ) {
  502. // If the user already exists, or the user opted out of cookies, don't set cookies.
  503. if ( $user->exists() ) {
  504. return;
  505. }
  506. if ( false === $cookies_consent ) {
  507. // Remove any existing cookies.
  508. $past = time() - YEAR_IN_SECONDS;
  509. setcookie( 'comment_author_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
  510. setcookie( 'comment_author_email_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
  511. setcookie( 'comment_author_url_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
  512. return;
  513. }
  514. /**
  515. * Filters the lifetime of the comment cookie in seconds.
  516. *
  517. * @since 2.8.0
  518. *
  519. * @param int $seconds Comment cookie lifetime. Default 30000000.
  520. */
  521. $comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', 30000000 );
  522. $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
  523. setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
  524. setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
  525. setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
  526. }
  527. /**
  528. * Sanitizes the cookies sent to the user already.
  529. *
  530. * Will only do anything if the cookies have already been created for the user.
  531. * Mostly used after cookies had been sent to use elsewhere.
  532. *
  533. * @since 2.0.4
  534. */
  535. function sanitize_comment_cookies() {
  536. if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) {
  537. /**
  538. * Filters the comment author's name cookie before it is set.
  539. *
  540. * When this filter hook is evaluated in wp_filter_comment(),
  541. * the comment author's name string is passed.
  542. *
  543. * @since 1.5.0
  544. *
  545. * @param string $author_cookie The comment author name cookie.
  546. */
  547. $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE[ 'comment_author_' . COOKIEHASH ] );
  548. $comment_author = wp_unslash( $comment_author );
  549. $comment_author = esc_attr( $comment_author );
  550. $_COOKIE[ 'comment_author_' . COOKIEHASH ] = $comment_author;
  551. }
  552. if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) {
  553. /**
  554. * Filters the comment author's email cookie before it is set.
  555. *
  556. * When this filter hook is evaluated in wp_filter_comment(),
  557. * the comment author's email string is passed.
  558. *
  559. * @since 1.5.0
  560. *
  561. * @param string $author_email_cookie The comment author email cookie.
  562. */
  563. $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] );
  564. $comment_author_email = wp_unslash( $comment_author_email );
  565. $comment_author_email = esc_attr( $comment_author_email );
  566. $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] = $comment_author_email;
  567. }
  568. if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) {
  569. /**
  570. * Filters the comment author's URL cookie before it is set.
  571. *
  572. * When this filter hook is evaluated in wp_filter_comment(),
  573. * the comment author's URL string is passed.
  574. *
  575. * @since 1.5.0
  576. *
  577. * @param string $author_url_cookie The comment author URL cookie.
  578. */
  579. $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] );
  580. $comment_author_url = wp_unslash( $comment_author_url );
  581. $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] = $comment_author_url;
  582. }
  583. }
  584. /**
  585. * Validates whether this comment is allowed to be made.
  586. *
  587. * @since 2.0.0
  588. * @since 4.7.0 The `$avoid_die` parameter was added, allowing the function to
  589. * return a WP_Error object instead of dying.
  590. *
  591. * @global wpdb $wpdb WordPress database abstraction object.
  592. *
  593. * @param array $commentdata Contains information on the comment.
  594. * @param bool $avoid_die When true, a disallowed comment will result in the function
  595. * returning a WP_Error object, rather than executing wp_die().
  596. * Default false.
  597. * @return int|string|WP_Error Allowed comments return the approval status (0|1|'spam').
  598. * If `$avoid_die` is true, disallowed comments return a WP_Error.
  599. */
  600. function wp_allow_comment( $commentdata, $avoid_die = false ) {
  601. global $wpdb;
  602. // Simple duplicate check
  603. // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
  604. $dupe = $wpdb->prepare(
  605. "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",
  606. wp_unslash( $commentdata['comment_post_ID'] ),
  607. wp_unslash( $commentdata['comment_parent'] ),
  608. wp_unslash( $commentdata['comment_author'] )
  609. );
  610. if ( $commentdata['comment_author_email'] ) {
  611. $dupe .= $wpdb->prepare(
  612. 'AND comment_author_email = %s ',
  613. wp_unslash( $commentdata['comment_author_email'] )
  614. );
  615. }
  616. $dupe .= $wpdb->prepare(
  617. ') AND comment_content = %s LIMIT 1',
  618. wp_unslash( $commentdata['comment_content'] )
  619. );
  620. $dupe_id = $wpdb->get_var( $dupe );
  621. /**
  622. * Filters the ID, if any, of the duplicate comment found when creating a new comment.
  623. *
  624. * Return an empty value from this filter to allow what WP considers a duplicate comment.
  625. *
  626. * @since 4.4.0
  627. *
  628. * @param int $dupe_id ID of the comment identified as a duplicate.
  629. * @param array $commentdata Data for the comment being created.
  630. */
  631. $dupe_id = apply_filters( 'duplicate_comment_id', $dupe_id, $commentdata );
  632. if ( $dupe_id ) {
  633. /**
  634. * Fires immediately after a duplicate comment is detected.
  635. *
  636. * @since 3.0.0
  637. *
  638. * @param array $commentdata Comment data.
  639. */
  640. do_action( 'comment_duplicate_trigger', $commentdata );
  641. /**
  642. * Filters duplicate comment error message.
  643. *
  644. * @since 5.2.0
  645. *
  646. * @param string $comment_duplicate_message Duplicate comment error message.
  647. */
  648. $comment_duplicate_message = apply_filters( 'comment_duplicate_message', __( 'Duplicate comment detected; it looks as though you&#8217;ve already said that!' ) );
  649. if ( true === $avoid_die ) {
  650. return new WP_Error( 'comment_duplicate', $comment_duplicate_message, 409 );
  651. } else {
  652. if ( wp_doing_ajax() ) {
  653. die( $comment_duplicate_message );
  654. }
  655. wp_die( $comment_duplicate_message, 409 );
  656. }
  657. }
  658. /**
  659. * Fires immediately before a comment is marked approved.
  660. *
  661. * Allows checking for comment flooding.
  662. *
  663. * @since 2.3.0
  664. * @since 4.7.0 The `$avoid_die` parameter was added.
  665. *
  666. * @param string $comment_author_IP Comment author's IP address.
  667. * @param string $comment_author_email Comment author's email.
  668. * @param string $comment_date_gmt GMT date the comment was posted.
  669. * @param bool $avoid_die Whether to prevent executing wp_die()
  670. * or die() if a comment flood is occurring.
  671. */
  672. do_action(
  673. 'check_comment_flood',
  674. $commentdata['comment_author_IP'],
  675. $commentdata['comment_author_email'],
  676. $commentdata['comment_date_gmt'],
  677. $avoid_die
  678. );
  679. /**
  680. * Filters whether a comment is part of a comment flood.
  681. *
  682. * The default check is wp_check_comment_flood(). See check_comment_flood_db().
  683. *
  684. * @since 4.7.0
  685. *
  686. * @param bool $is_flood Is a comment flooding occurring? Default false.
  687. * @param string $comment_author_IP Comment author's IP address.
  688. * @param string $comment_author_email Comment author's email.
  689. * @param string $comment_date_gmt GMT date the comment was posted.
  690. * @param bool $avoid_die Whether to prevent executing wp_die()
  691. * or die() if a comment flood is occurring.
  692. */
  693. $is_flood = apply_filters(
  694. 'wp_is_comment_flood',
  695. false,
  696. $commentdata['comment_author_IP'],
  697. $commentdata['comment_author_email'],
  698. $commentdata['comment_date_gmt'],
  699. $avoid_die
  700. );
  701. if ( $is_flood ) {
  702. /** This filter is documented in wp-includes/comment-template.php */
  703. $comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) );
  704. return new WP_Error( 'comment_flood', $comment_flood_message, 429 );
  705. }
  706. if ( ! empty( $commentdata['user_id'] ) ) {
  707. $user = get_userdata( $commentdata['user_id'] );
  708. $post_author = $wpdb->get_var(
  709. $wpdb->prepare(
  710. "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",
  711. $commentdata['comment_post_ID']
  712. )
  713. );
  714. }
  715. if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
  716. // The author and the admins get respect.
  717. $approved = 1;
  718. } else {
  719. // Everyone else's comments will be checked.
  720. if ( check_comment(
  721. $commentdata['comment_author'],
  722. $commentdata['comment_author_email'],
  723. $commentdata['comment_author_url'],
  724. $commentdata['comment_content'],
  725. $commentdata['comment_author_IP'],
  726. $commentdata['comment_agent'],
  727. $commentdata['comment_type']
  728. ) ) {
  729. $approved = 1;
  730. } else {
  731. $approved = 0;
  732. }
  733. if ( wp_blacklist_check(
  734. $commentdata['comment_author'],
  735. $commentdata['comment_author_email'],
  736. $commentdata['comment_author_url'],
  737. $commentdata['comment_content'],
  738. $commentdata['comment_author_IP'],
  739. $commentdata['comment_agent']
  740. ) ) {
  741. $approved = EMPTY_TRASH_DAYS ? 'trash' : 'spam';
  742. }
  743. }
  744. /**
  745. * Filters a comment's approval status before it is set.
  746. *
  747. * @since 2.1.0
  748. * @since 4.9.0 Returning a WP_Error value from the filter will shortcircuit comment insertion and
  749. * allow skipping further processing.
  750. *
  751. * @param int|string|WP_Error $approved The approval status. Accepts 1, 0, 'spam' or WP_Error.
  752. * @param array $commentdata Comment data.
  753. */
  754. $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
  755. return $approved;
  756. }
  757. /**
  758. * Hooks WP's native database-based comment-flood check.
  759. *
  760. * This wrapper maintains backward compatibility with plugins that expect to
  761. * be able to unhook the legacy check_comment_flood_db() function from
  762. * 'check_comment_flood' using remove_action().
  763. *
  764. * @since 2.3.0
  765. * @since 4.7.0 Converted to be an add_filter() wrapper.
  766. */
  767. function check_comment_flood_db() {
  768. add_filter( 'wp_is_comment_flood', 'wp_check_comment_flood', 10, 5 );
  769. }
  770. /**
  771. * Checks whether comment flooding is occurring.
  772. *
  773. * Won't run, if current user can manage options, so to not block
  774. * administrators.
  775. *
  776. * @since 4.7.0
  777. *
  778. * @global wpdb $wpdb WordPress database abstraction object.
  779. *
  780. * @param bool $is_flood Is a comment flooding occurring?
  781. * @param string $ip Comment author's IP address.
  782. * @param string $email Comment author's email address.
  783. * @param string $date MySQL time string.
  784. * @param bool $avoid_die When true, a disallowed comment will result in the function
  785. * returning a WP_Error object, rather than executing wp_die().
  786. * Default false.
  787. * @return bool Whether comment flooding is occurring.
  788. */
  789. function wp_check_comment_flood( $is_flood, $ip, $email, $date, $avoid_die = false ) {
  790. global $wpdb;
  791. // Another callback has declared a flood. Trust it.
  792. if ( true === $is_flood ) {
  793. return $is_flood;
  794. }
  795. // don't throttle admins or moderators
  796. if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) {
  797. return false;
  798. }
  799. $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
  800. if ( is_user_logged_in() ) {
  801. $user = get_current_user_id();
  802. $check_column = '`user_id`';
  803. } else {
  804. $user = $ip;
  805. $check_column = '`comment_author_IP`';
  806. }
  807. $sql = $wpdb->prepare(
  808. "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( $check_column = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1",
  809. $hour_ago,
  810. $user,
  811. $email
  812. );
  813. $lasttime = $wpdb->get_var( $sql );
  814. if ( $lasttime ) {
  815. $time_lastcomment = mysql2date( 'U', $lasttime, false );
  816. $time_newcomment = mysql2date( 'U', $date, false );
  817. /**
  818. * Filters the comment flood status.
  819. *
  820. * @since 2.1.0
  821. *
  822. * @param bool $bool Whether a comment flood is occurring. Default false.
  823. * @param int $time_lastcomment Timestamp of when the last comment was posted.
  824. * @param int $time_newcomment Timestamp of when the new comment was posted.
  825. */
  826. $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment );
  827. if ( $flood_die ) {
  828. /**
  829. * Fires before the comment flood message is triggered.
  830. *
  831. * @since 1.5.0
  832. *
  833. * @param int $time_lastcomment Timestamp of when the last comment was posted.
  834. * @param int $time_newcomment Timestamp of when the new comment was posted.
  835. */
  836. do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
  837. if ( true === $avoid_die ) {
  838. return true;
  839. } else {
  840. /**
  841. * Filters the comment flood error message.
  842. *
  843. * @since 5.2.0
  844. *
  845. * @param string $comment_flood_message Comment flood error message.
  846. */
  847. $comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) );
  848. if ( wp_doing_ajax() ) {
  849. die( $comment_flood_message );
  850. }
  851. wp_die( $comment_flood_message, 429 );
  852. }
  853. }
  854. }
  855. return false;
  856. }
  857. /**
  858. * Separates an array of comments into an array keyed by comment_type.
  859. *
  860. * @since 2.7.0
  861. *
  862. * @param WP_Comment[] $comments Array of comments
  863. * @return WP_Comment[] Array of comments keyed by comment_type.
  864. */
  865. function separate_comments( &$comments ) {
  866. $comments_by_type = array(
  867. 'comment' => array(),
  868. 'trackback' => array(),
  869. 'pingback' => array(),
  870. 'pings' => array(),
  871. );
  872. $count = count( $comments );
  873. for ( $i = 0; $i < $count; $i++ ) {
  874. $type = $comments[ $i ]->comment_type;
  875. if ( empty( $type ) ) {
  876. $type = 'comment';
  877. }
  878. $comments_by_type[ $type ][] = &$comments[ $i ];
  879. if ( 'trackback' == $type || 'pingback' == $type ) {
  880. $comments_by_type['pings'][] = &$comments[ $i ];
  881. }
  882. }
  883. return $comments_by_type;
  884. }
  885. /**
  886. * Calculate the total number of comment pages.
  887. *
  888. * @since 2.7.0
  889. *
  890. * @uses Walker_Comment
  891. *
  892. * @global WP_Query $wp_query WordPress Query object.
  893. *
  894. * @param WP_Comment[] $comments Optional. Array of WP_Comment objects. Defaults to $wp_query->comments.
  895. * @param int $per_page Optional. Comments per page.
  896. * @param bool $threaded Optional. Control over flat or threaded comments.
  897. * @return int Number of comment pages.
  898. */
  899. function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
  900. global $wp_query;
  901. if ( null === $comments && null === $per_page && null === $threaded && ! empty( $wp_query->max_num_comment_pages ) ) {
  902. return $wp_query->max_num_comment_pages;
  903. }
  904. if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) ) {
  905. $comments = $wp_query->comments;
  906. }
  907. if ( empty( $comments ) ) {
  908. return 0;
  909. }
  910. if ( ! get_option( 'page_comments' ) ) {
  911. return 1;
  912. }
  913. if ( ! isset( $per_page ) ) {
  914. $per_page = (int) get_query_var( 'comments_per_page' );
  915. }
  916. if ( 0 === $per_page ) {
  917. $per_page = (int) get_option( 'comments_per_page' );
  918. }
  919. if ( 0 === $per_page ) {
  920. return 1;
  921. }
  922. if ( ! isset( $threaded ) ) {
  923. $threaded = get_option( 'thread_comments' );
  924. }
  925. if ( $threaded ) {
  926. $walker = new Walker_Comment;
  927. $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
  928. } else {
  929. $count = ceil( count( $comments ) / $per_page );
  930. }
  931. return $count;
  932. }
  933. /**
  934. * Calculate what page number a comment will appear on for comment paging.
  935. *
  936. * @since 2.7.0
  937. *
  938. * @global wpdb $wpdb WordPress database abstraction object.
  939. *
  940. * @param int $comment_ID Comment ID.
  941. * @param array $args {
  942. * Array of optional arguments.
  943. * @type string $type Limit paginated comments to those matching a given type. Accepts 'comment',
  944. * 'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'.
  945. * Default is 'all'.
  946. * @type int $per_page Per-page count to use when calculating pagination. Defaults to the value of the
  947. * 'comments_per_page' option.
  948. * @type int|string $max_depth If greater than 1, comment page will be determined for the top-level parent of
  949. * `$comment_ID`. Defaults to the value of the 'thread_comments_depth' option.
  950. * } *
  951. * @return int|null Comment page number or null on error.
  952. */
  953. function get_page_of_comment( $comment_ID, $args = array() ) {
  954. global $wpdb;
  955. $page = null;
  956. $comment = get_comment( $comment_ID );
  957. if ( ! $comment ) {
  958. return;
  959. }
  960. $defaults = array(
  961. 'type' => 'all',
  962. 'page' => '',
  963. 'per_page' => '',
  964. 'max_depth' => '',
  965. );
  966. $args = wp_parse_args( $args, $defaults );
  967. $original_args = $args;
  968. // Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option.
  969. if ( get_option( 'page_comments' ) ) {
  970. if ( '' === $args['per_page'] ) {
  971. $args['per_page'] = get_query_var( 'comments_per_page' );
  972. }
  973. if ( '' === $args['per_page'] ) {
  974. $args['per_page'] = get_option( 'comments_per_page' );
  975. }
  976. }
  977. if ( empty( $args['per_page'] ) ) {
  978. $args['per_page'] = 0;
  979. $args['page'] = 0;
  980. }
  981. if ( $args['per_page'] < 1 ) {
  982. $page = 1;
  983. }
  984. if ( null === $page ) {
  985. if ( '' === $args['max_depth'] ) {
  986. if ( get_option( 'thread_comments' ) ) {
  987. $args['max_depth'] = get_option( 'thread_comments_depth' );
  988. } else {
  989. $args['max_depth'] = -1;
  990. }
  991. }
  992. // Find this comment's top level parent if threading is enabled
  993. if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) {
  994. return get_page_of_comment( $comment->comment_parent, $args );
  995. }
  996. $comment_args = array(
  997. 'type' => $args['type'],
  998. 'post_id' => $comment->comment_post_ID,
  999. 'fields' => 'ids',
  1000. 'count' => true,
  1001. 'status' => 'approve',
  1002. 'parent' => 0,
  1003. 'date_query' => array(
  1004. array(
  1005. 'column' => "$wpdb->comments.comment_date_gmt",
  1006. 'before' => $comment->comment_date_gmt,
  1007. ),
  1008. ),
  1009. );
  1010. $comment_query = new WP_Comment_Query();
  1011. $older_comment_count = $comment_query->query( $comment_args );
  1012. // No older comments? Then it's page #1.
  1013. if ( 0 == $older_comment_count ) {
  1014. $page = 1;
  1015. // Divide comments older than this one by comments per page to get this comment's page number
  1016. } else {
  1017. $page = ceil( ( $older_comment_count + 1 ) / $args['per_page'] );
  1018. }
  1019. }
  1020. /**
  1021. * Filters the calculated page on which a comment appears.
  1022. *
  1023. * @since 4.4.0
  1024. * @since 4.7.0 Introduced the `$comment_ID` parameter.
  1025. *
  1026. * @param int $page Comment page.
  1027. * @param array $args {
  1028. * Arguments used to calculate pagination. These include arguments auto-detected by the function,
  1029. * based on query vars, system settings, etc. For pristine arguments passed to the function,
  1030. * see `$original_args`.
  1031. *
  1032. * @type string $type Type of comments to count.
  1033. * @type int $page Calculated current page.
  1034. * @type int $per_page Calculated number of comments per page.
  1035. * @type int $max_depth Maximum comment threading depth allowed.
  1036. * }
  1037. * @param array $original_args {
  1038. * Array of arguments passed to the function. Some or all of these may not be set.
  1039. *
  1040. * @type string $type Type of comments to count.
  1041. * @type int $page Current comment page.
  1042. * @type int $per_page Number of comments per page.
  1043. * @type int $max_depth Maximum comment threading depth allowed.
  1044. * }
  1045. * @param int $comment_ID ID of the comment.
  1046. */
  1047. return apply_filters( 'get_page_of_comment', (int) $page, $args, $original_args, $comment_ID );
  1048. }
  1049. /**
  1050. * Retrieves the maximum character lengths for the comment form fields.
  1051. *
  1052. * @since 4.5.0
  1053. *
  1054. * @global wpdb $wpdb WordPress database abstraction object.
  1055. *
  1056. * @return array Maximum character length for the comment form fields.
  1057. */
  1058. function wp_get_comment_fields_max_lengths() {
  1059. global $wpdb;
  1060. $lengths = array(
  1061. 'comment_author' => 245,
  1062. 'comment_author_email' => 100,
  1063. 'comment_author_url' => 200,
  1064. 'comment_content' => 65525,
  1065. );
  1066. if ( $wpdb->is_mysql ) {
  1067. foreach ( $lengths as $column => $length ) {
  1068. $col_length = $wpdb->get_col_length( $wpdb->comments, $column );
  1069. $max_length = 0;
  1070. // No point if we can't get the DB column lengths
  1071. if ( is_wp_error( $col_length ) ) {
  1072. break;
  1073. }
  1074. if ( ! is_array( $col_length ) && (int) $col_length > 0 ) {
  1075. $max_length = (int) $col_length;
  1076. } elseif ( is_array( $col_length ) && isset( $col_length['length'] ) && intval( $col_length['length'] ) > 0 ) {
  1077. $max_length = (int) $col_length['length'];
  1078. if ( ! empty( $col_length['type'] ) && 'byte' === $col_length['type'] ) {
  1079. $max_length = $max_length - 10;
  1080. }
  1081. }
  1082. if ( $max_length > 0 ) {
  1083. $lengths[ $column ] = $max_length;
  1084. }
  1085. }
  1086. }
  1087. /**
  1088. * Filters the lengths for the comment form fields.
  1089. *
  1090. * @since 4.5.0
  1091. *
  1092. * @param array $lengths Associative array `'field_name' => 'maximum length'`.
  1093. */
  1094. return apply_filters( 'wp_get_comment_fields_max_lengths', $lengths );
  1095. }
  1096. /**
  1097. * Compares the lengths of comment data against the maximum character limits.
  1098. *
  1099. * @since 4.7.0
  1100. *
  1101. * @param array $comment_data Array of arguments for inserting a comment.
  1102. * @return WP_Error|true WP_Error when a comment field exceeds the limit,
  1103. * otherwise true.
  1104. */
  1105. function wp_check_comment_data_max_lengths( $comment_data ) {
  1106. $max_lengths = wp_get_comment_fields_max_lengths();
  1107. if ( isset( $comment_data['comment_author'] ) && mb_strlen( $comment_data['comment_author'], '8bit' ) > $max_lengths['comment_author'] ) {
  1108. return new WP_Error( 'comment_author_column_length', __( '<strong>ERROR</strong>: your name is too long.' ), 200 );
  1109. }
  1110. if ( isset( $comment_data['comment_author_email'] ) && strlen( $comment_data['comment_author_email'] ) > $max_lengths['comment_author_email'] ) {
  1111. return new WP_Error( 'comment_author_email_column_length', __( '<strong>ERROR</strong>: your email address is too long.' ), 200 );
  1112. }
  1113. if ( isset( $comment_data['comment_author_url'] ) && strlen( $comment_data['comment_author_url'] ) > $max_lengths['comment_author_url'] ) {
  1114. return new WP_Error( 'comment_author_url_column_length', __( '<strong>ERROR</strong>: your url is too long.' ), 200 );
  1115. }
  1116. if ( isset( $comment_data['comment_content'] ) && mb_strlen( $comment_data['comment_content'], '8bit' ) > $max_lengths['comment_content'] ) {
  1117. return new WP_Error( 'comment_content_column_length', __( '<strong>ERROR</strong>: your comment is too long.' ), 200 );
  1118. }
  1119. return true;
  1120. }
  1121. /**
  1122. * Does comment contain blacklisted characters or words.
  1123. *
  1124. * @since 1.5.0
  1125. *
  1126. * @param string $author The author of the comment
  1127. * @param string $email The email of the comment
  1128. * @param string $url The url used in the comment
  1129. * @param string $comment The comment content
  1130. * @param string $user_ip The comment author's IP address
  1131. * @param string $user_agent The author's browser user agent
  1132. * @return bool True if comment contains blacklisted content, false if comment does not
  1133. */
  1134. function wp_blacklist_check( $author, $email, $url, $comment, $user_ip, $user_agent ) {
  1135. /**
  1136. * Fires before the comment is tested for blacklisted characters or words.
  1137. *
  1138. * @since 1.5.0
  1139. *
  1140. * @param string $author Comment author.
  1141. * @param string $email Comment author's email.
  1142. * @param string $url Comment author's URL.
  1143. * @param string $comment Comment content.
  1144. * @param string $user_ip Comment author's IP address.
  1145. * @param string $user_agent Comment author's browser user agent.
  1146. */
  1147. do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent );
  1148. $mod_keys = trim( get_option( 'blacklist_keys' ) );
  1149. if ( '' == $mod_keys ) {
  1150. return false; // If moderation keys are empty
  1151. }
  1152. // Ensure HTML tags are not being used to bypass the blacklist.
  1153. $comment_without_html = wp_strip_all_tags( $comment );
  1154. $words = explode( "\n", $mod_keys );
  1155. foreach ( (array) $words as $word ) {
  1156. $word = trim( $word );
  1157. // Skip empty lines
  1158. if ( empty( $word ) ) {
  1159. continue; }
  1160. // Do some escaping magic so that '#' chars in the
  1161. // spam words don't break things:
  1162. $word = preg_quote( $word, '#' );
  1163. $pattern = "#$word#i";
  1164. if ( preg_match( $pattern, $author )
  1165. || preg_match( $pattern, $email )
  1166. || preg_match( $pattern, $url )
  1167. || preg_match( $pattern, $comment )
  1168. || preg_match( $pattern, $comment_without_html )
  1169. || preg_match( $pattern, $user_ip )
  1170. || preg_match( $pattern, $user_agent )
  1171. ) {
  1172. return true;
  1173. }
  1174. }
  1175. return false;
  1176. }
  1177. /**
  1178. * Retrieves the total comment counts for the whole site or a single post.
  1179. *
  1180. * The comment stats are cached and then retrieved, if they already exist in the
  1181. * cache.
  1182. *
  1183. * @see get_comment_count() Which handles fetching the live comment counts.
  1184. *
  1185. * @since 2.5.0
  1186. *
  1187. * @param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that
  1188. * comment counts for the whole site will be retrieved.
  1189. * @return stdClass {
  1190. * The number of comments keyed by their status.
  1191. *
  1192. * @type int|string $approved The number of approved comments.
  1193. * @type int|string $moderated The number of comments awaiting moderation (a.k.a. pending).
  1194. * @type int|string $spam The number of spam comments.
  1195. * @type int|string $trash The number of trashed comments.
  1196. * @type int|string $post-trashed The number of comments for posts that are in the trash.
  1197. * @type int $total_comments The total number of non-trashed comments, including spam.
  1198. * @type int $all The total number of pending or approved comments.
  1199. * }
  1200. */
  1201. function wp_count_comments( $post_id = 0 ) {
  1202. $post_id = (int) $post_id;
  1203. /**
  1204. * Filters the comments count for a given post or the whole site.
  1205. *
  1206. * @since 2.7.0
  1207. *
  1208. * @param array|stdClass $count An empty array or an object containing comment counts.
  1209. * @param int $post_id The post ID. Can be 0 to represent the whole site.
  1210. */
  1211. $filtered = apply_filters( 'wp_count_comments', array(), $post_id );
  1212. if ( ! empty( $filtered ) ) {
  1213. return $filtered;
  1214. }
  1215. $count = wp_cache_get( "comments-{$post_id}", 'counts' );
  1216. if ( false !== $count ) {
  1217. return $count;
  1218. }
  1219. $stats = get_comment_count( $post_id );
  1220. $stats['moderated'] = $stats['awaiting_moderation'];
  1221. unset( $stats['awaiting_moderation'] );
  1222. $stats_object = (object) $stats;
  1223. wp_cache_set( "comments-{$post_id}", $stats_object, 'counts' );
  1224. return $stats_object;
  1225. }
  1226. /**
  1227. * Trashes or deletes a comment.
  1228. *
  1229. * The comment is moved to trash instead of permanently deleted unless trash is
  1230. * disabled, item is already in the trash, or $force_delete is true.
  1231. *
  1232. * The post comment count will be updated if the comment was approved and has a
  1233. * post ID available.
  1234. *
  1235. * @since 2.0.0
  1236. *
  1237. * @global wpdb $wpdb WordPress database abstraction object.
  1238. *
  1239. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1240. * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
  1241. * @return bool True on success, false on failure.
  1242. */
  1243. function wp_delete_comment( $comment_id, $force_delete = false ) {
  1244. global $wpdb;
  1245. $comment = get_comment( $comment_id );
  1246. if ( ! $comment ) {
  1247. return false;
  1248. }
  1249. if ( ! $force_delete && EMPTY_TRASH_DAYS && ! in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ) ) ) {
  1250. return wp_trash_comment( $comment_id );
  1251. }
  1252. /**
  1253. * Fires immediately before a comment is deleted from the database.
  1254. *
  1255. * @since 1.2.0
  1256. * @since 4.9.0 Added the `$comment` parameter.
  1257. *
  1258. * @param int $comment_id The comment ID.
  1259. * @param WP_Comment $comment The comment to be deleted.
  1260. */
  1261. do_action( 'delete_comment', $comment->comment_ID, $comment );
  1262. // Move children up a level.
  1263. $children = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID ) );
  1264. if ( ! empty( $children ) ) {
  1265. $wpdb->update( $wpdb->comments, array( 'comment_parent' => $comment->comment_parent ), array( 'comment_parent' => $comment->comment_ID ) );
  1266. clean_comment_cache( $children );
  1267. }
  1268. // Delete metadata
  1269. $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) );
  1270. foreach ( $meta_ids as $mid ) {
  1271. delete_metadata_by_mid( 'comment', $mid );
  1272. }
  1273. if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) ) {
  1274. return false;
  1275. }
  1276. /**
  1277. * Fires immediately after a comment is deleted from the database.
  1278. *
  1279. * @since 2.9.0
  1280. * @since 4.9.0 Added the `$comment` parameter.
  1281. *
  1282. * @param int $comment_id The comment ID.
  1283. * @param WP_Comment $comment The deleted comment.
  1284. */
  1285. do_action( 'deleted_comment', $comment->comment_ID, $comment );
  1286. $post_id = $comment->comment_post_ID;
  1287. if ( $post_id && $comment->comment_approved == 1 ) {
  1288. wp_update_comment_count( $post_id );
  1289. }
  1290. clean_comment_cache( $comment->comment_ID );
  1291. /** This action is documented in wp-includes/comment.php */
  1292. do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' );
  1293. wp_transition_comment_status( 'delete', $comment->comment_approved, $comment );
  1294. return true;
  1295. }
  1296. /**
  1297. * Moves a comment to the Trash
  1298. *
  1299. * If trash is disabled, comment is permanently deleted.
  1300. *
  1301. * @since 2.9.0
  1302. *
  1303. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1304. * @return bool True on success, false on failure.
  1305. */
  1306. function wp_trash_comment( $comment_id ) {
  1307. if ( ! EMPTY_TRASH_DAYS ) {
  1308. return wp_delete_comment( $comment_id, true );
  1309. }
  1310. $comment = get_comment( $comment_id );
  1311. if ( ! $comment ) {
  1312. return false;
  1313. }
  1314. /**
  1315. * Fires immediately before a comment is sent to the Trash.
  1316. *
  1317. * @since 2.9.0
  1318. * @since 4.9.0 Added the `$comment` parameter.
  1319. *
  1320. * @param int $comment_id The comment ID.
  1321. * @param WP_Comment $comment The comment to be trashed.
  1322. */
  1323. do_action( 'trash_comment', $comment->comment_ID, $comment );
  1324. if ( wp_set_comment_status( $comment, 'trash' ) ) {
  1325. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
  1326. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
  1327. add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
  1328. add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );
  1329. /**
  1330. * Fires immediately after a comment is sent to Trash.
  1331. *
  1332. * @since 2.9.0
  1333. * @since 4.9.0 Added the `$comment` parameter.
  1334. *
  1335. * @param int $comment_id The comment ID.
  1336. * @param WP_Comment $comment The trashed comment.
  1337. */
  1338. do_action( 'trashed_comment', $comment->comment_ID, $comment );
  1339. return true;
  1340. }
  1341. return false;
  1342. }
  1343. /**
  1344. * Removes a comment from the Trash
  1345. *
  1346. * @since 2.9.0
  1347. *
  1348. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1349. * @return bool True on success, false on failure.
  1350. */
  1351. function wp_untrash_comment( $comment_id ) {
  1352. $comment = get_comment( $comment_id );
  1353. if ( ! $comment ) {
  1354. return false;
  1355. }
  1356. /**
  1357. * Fires immediately before a comment is restored from the Trash.
  1358. *
  1359. * @since 2.9.0
  1360. * @since 4.9.0 Added the `$comment` parameter.
  1361. *
  1362. * @param int $comment_id The comment ID.
  1363. * @param WP_Comment $comment The comment to be untrashed.
  1364. */
  1365. do_action( 'untrash_comment', $comment->comment_ID, $comment );
  1366. $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
  1367. if ( empty( $status ) ) {
  1368. $status = '0';
  1369. }
  1370. if ( wp_set_comment_status( $comment, $status ) ) {
  1371. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
  1372. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
  1373. /**
  1374. * Fires immediately after a comment is restored from the Trash.
  1375. *
  1376. * @since 2.9.0
  1377. * @since 4.9.0 Added the `$comment` parameter.
  1378. *
  1379. * @param int $comment_id The comment ID.
  1380. * @param WP_Comment $comment The untrashed comment.
  1381. */
  1382. do_action( 'untrashed_comment', $comment->comment_ID, $comment );
  1383. return true;
  1384. }
  1385. return false;
  1386. }
  1387. /**
  1388. * Marks a comment as Spam
  1389. *
  1390. * @since 2.9.0
  1391. *
  1392. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1393. * @return bool True on success, false on failure.
  1394. */
  1395. function wp_spam_comment( $comment_id ) {
  1396. $comment = get_comment( $comment_id );
  1397. if ( ! $comment ) {
  1398. return false;
  1399. }
  1400. /**
  1401. * Fires immediately before a comment is marked as Spam.
  1402. *
  1403. * @since 2.9.0
  1404. * @since 4.9.0 Added the `$comment` parameter.
  1405. *
  1406. * @param int $comment_id The comment ID.
  1407. * @param WP_Comment $comment The comment to be marked as spam.
  1408. */
  1409. do_action( 'spam_comment', $comment->comment_ID, $comment );
  1410. if ( wp_set_comment_status( $comment, 'spam' ) ) {
  1411. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
  1412. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
  1413. add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
  1414. add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );
  1415. /**
  1416. * Fires immediately after a comment is marked as Spam.
  1417. *
  1418. * @since 2.9.0
  1419. * @since 4.9.0 Added the `$comment` parameter.
  1420. *
  1421. * @param int $comment_id The comment ID.
  1422. * @param WP_Comment $comment The comment marked as spam.
  1423. */
  1424. do_action( 'spammed_comment', $comment->comment_ID, $comment );
  1425. return true;
  1426. }
  1427. return false;
  1428. }
  1429. /**
  1430. * Removes a comment from the Spam
  1431. *
  1432. * @since 2.9.0
  1433. *
  1434. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  1435. * @return bool True on success, false on failure.
  1436. */
  1437. function wp_unspam_comment( $comment_id ) {
  1438. $comment = get_comment( $comment_id );
  1439. if ( ! $comment ) {
  1440. return false;
  1441. }
  1442. /**
  1443. * Fires immediately before a comment is unmarked as Spam.
  1444. *
  1445. * @since 2.9.0
  1446. * @since 4.9.0 Added the `$comment` parameter.
  1447. *
  1448. * @param int $comment_id The comment ID.
  1449. * @param WP_Comment $comment The comment to be unmarked as spam.
  1450. */
  1451. do_action( 'unspam_comment', $comment->comment_ID, $comment );
  1452. $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
  1453. if ( empty( $status ) ) {
  1454. $status = '0';
  1455. }
  1456. if ( wp_set_comment_status( $comment, $status ) ) {
  1457. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
  1458. delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
  1459. /**
  1460. * Fires immediately after a comment is unmarked as Spam.
  1461. *
  1462. * @since 2.9.0
  1463. * @since 4.9.0 Added the `$comment` parameter.
  1464. *
  1465. * @param int $comment_id The comment ID.
  1466. * @param WP_Comment $comment The comment unmarked as spam.
  1467. */
  1468. do_action( 'unspammed_comment', $comment->comment_ID, $comment );
  1469. return true;
  1470. }
  1471. return false;
  1472. }
  1473. /**
  1474. * The status of a comment by ID.
  1475. *
  1476. * @since 1.0.0
  1477. *
  1478. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object
  1479. * @return false|string Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
  1480. */
  1481. function wp_get_comment_status( $comment_id ) {
  1482. $comment = get_comment( $comment_id );
  1483. if ( ! $comment ) {
  1484. return false;
  1485. }
  1486. $approved = $comment->comment_approved;
  1487. if ( $approved == null ) {
  1488. return false;
  1489. } elseif ( $approved == '1' ) {
  1490. return 'approved';
  1491. } elseif ( $approved == '0' ) {
  1492. return 'unapproved';
  1493. } elseif ( $approved == 'spam' ) {
  1494. return 'spam';
  1495. } elseif ( $approved == 'trash' ) {
  1496. return 'trash';
  1497. } else {
  1498. return false;
  1499. }
  1500. }
  1501. /**
  1502. * Call hooks for when a comment status transition occurs.
  1503. *
  1504. * Calls hooks for comment status transitions. If the new comment status is not the same
  1505. * as the previous comment status, then two hooks will be ran, the first is
  1506. * {@see 'transition_comment_status'} with new status, old status, and comment data. The
  1507. * next action called is {@see comment_$old_status_to_$new_status'}. It has the
  1508. * comment data.
  1509. *
  1510. * The final action will run whether or not the comment statuses are the same. The
  1511. * action is named {@see 'comment_$new_status_$comment->comment_type'}.
  1512. *
  1513. * @since 2.7.0
  1514. *
  1515. * @param string $new_status New comment status.
  1516. * @param string $old_status Previous comment status.
  1517. * @param object $comment Comment data.
  1518. */
  1519. function wp_transition_comment_status( $new_status, $old_status, $comment ) {
  1520. /*
  1521. * Translate raw statuses to human readable formats for the hooks.
  1522. * This is not a complete list of comment status, it's only the ones
  1523. * that need to be renamed
  1524. */
  1525. $comment_statuses = array(
  1526. 0 => 'unapproved',
  1527. 'hold' => 'unapproved', // wp_set_comment_status() uses "hold"
  1528. 1 => 'approved',
  1529. 'approve' => 'approved', // wp_set_comment_status() uses "approve"
  1530. );
  1531. if ( isset( $comment_statuses[ $new_status ] ) ) {
  1532. $new_status = $comment_statuses[ $new_status ];
  1533. }
  1534. if ( isset( $comment_statuses[ $old_status ] ) ) {
  1535. $old_status = $comment_statuses[ $old_status ];
  1536. }
  1537. // Call the hooks
  1538. if ( $new_status != $old_status ) {
  1539. /**
  1540. * Fires when the comment status is in transition.
  1541. *
  1542. * @since 2.7.0
  1543. *
  1544. * @param int|string $new_status The new comment status.
  1545. * @param int|string $old_status The old comment status.
  1546. * @param object $comment The comment data.
  1547. */
  1548. do_action( 'transition_comment_status', $new_status, $old_status, $comment );
  1549. /**
  1550. * Fires when the comment status is in transition from one specific status to another.
  1551. *
  1552. * The dynamic portions of the hook name, `$old_status`, and `$new_status`,
  1553. * refer to the old and new comment statuses, respectively.
  1554. *
  1555. * @since 2.7.0
  1556. *
  1557. * @param WP_Comment $comment Comment object.
  1558. */
  1559. do_action( "comment_{$old_status}_to_{$new_status}", $comment );
  1560. }
  1561. /**
  1562. * Fires when the status of a specific comment type is in transition.
  1563. *
  1564. * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`,
  1565. * refer to the new comment status, and the type of comment, respectively.
  1566. *
  1567. * Typical comment types include an empty string (standard comment), 'pingback',
  1568. * or 'trackback'.
  1569. *
  1570. * @since 2.7.0
  1571. *
  1572. * @param int $comment_ID The comment ID.
  1573. * @param WP_Comment $comment Comment object.
  1574. */
  1575. do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
  1576. }
  1577. /**
  1578. * Clear the lastcommentmodified cached value when a comment status is changed.
  1579. *
  1580. * Deletes the lastcommentmodified cache key when a comment enters or leaves
  1581. * 'approved' status.
  1582. *
  1583. * @since 4.7.0
  1584. * @access private
  1585. *
  1586. * @param string $new_status The new comment status.
  1587. * @param string $old_status The old comment status.
  1588. */
  1589. function _clear_modified_cache_on_transition_comment_status( $new_status, $old_status ) {
  1590. if ( 'approved' === $new_status || 'approved' === $old_status ) {
  1591. foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
  1592. wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' );
  1593. }
  1594. }
  1595. }
  1596. /**
  1597. * Get current commenter's name, email, and URL.
  1598. *
  1599. * Expects cookies content to already be sanitized. User of this function might
  1600. * wish to recheck the returned array for validity.
  1601. *
  1602. * @see sanitize_comment_cookies() Use to sanitize cookies
  1603. *
  1604. * @since 2.0.4
  1605. *
  1606. * @return array Comment author, email, url respectively.
  1607. */
  1608. function wp_get_current_commenter() {
  1609. // Cookies should already be sanitized.
  1610. $comment_author = '';
  1611. if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) {
  1612. $comment_author = $_COOKIE[ 'comment_author_' . COOKIEHASH ];
  1613. }
  1614. $comment_author_email = '';
  1615. if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) {
  1616. $comment_author_email = $_COOKIE[ 'comment_author_email_' . COOKIEHASH ];
  1617. }
  1618. $comment_author_url = '';
  1619. if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) {
  1620. $comment_author_url = $_COOKIE[ 'comment_author_url_' . COOKIEHASH ];
  1621. }
  1622. /**
  1623. * Filters the current commenter's name, email, and URL.
  1624. *
  1625. * @since 3.1.0
  1626. *
  1627. * @param array $comment_author_data {
  1628. * An array of current commenter variables.
  1629. *
  1630. * @type string $comment_author The name of the author of the comment. Default empty.
  1631. * @type string $comment_author_email The email address of the `$comment_author`. Default empty.
  1632. * @type string $comment_author_url The URL address of the `$comment_author`. Default empty.
  1633. * }
  1634. */
  1635. return apply_filters( 'wp_get_current_commenter', compact( 'comment_author', 'comment_author_email', 'comment_author_url' ) );
  1636. }
  1637. /**
  1638. * Get unapproved comment author's email.
  1639. *
  1640. * Used to allow the commenter to see their pending comment.
  1641. *
  1642. * @since 5.1.0
  1643. *
  1644. * @return string The unapproved comment author's email (when supplied).
  1645. */
  1646. function wp_get_unapproved_comment_author_email() {
  1647. $commenter_email = '';
  1648. if ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) {
  1649. $comment_id = (int) $_GET['unapproved'];
  1650. $comment = get_comment( $comment_id );
  1651. if ( $comment && hash_equals( $_GET['moderation-hash'], wp_hash( $comment->comment_date_gmt ) ) ) {
  1652. $commenter_email = $comment->comment_author_email;
  1653. }
  1654. }
  1655. if ( ! $commenter_email ) {
  1656. $commenter = wp_get_current_commenter();
  1657. $commenter_email = $commenter['comment_author_email'];
  1658. }
  1659. return $commenter_email;
  1660. }
  1661. /**
  1662. * Inserts a comment into the database.
  1663. *
  1664. * @since 2.0.0
  1665. * @since 4.4.0 Introduced `$comment_meta` argument.
  1666. *
  1667. * @global wpdb $wpdb WordPress database abstraction object.
  1668. *
  1669. * @param array $commentdata {
  1670. * Array of arguments for inserting a new comment.
  1671. *
  1672. * @type string $comment_agent The HTTP user agent of the `$comment_author` when
  1673. * the comment was submitted. Default empty.
  1674. * @type int|string $comment_approved Whether the comment has been approved. Default 1.
  1675. * @type string $comment_author The name of the author of the comment. Default empty.
  1676. * @type string $comment_author_email The email address of the `$comment_author`. Default empty.
  1677. * @type string $comment_author_IP The IP address of the `$comment_author`. Default empty.
  1678. * @type string $comment_author_url The URL address of the `$comment_author`. Default empty.
  1679. * @type string $comment_content The content of the comment. Default empty.
  1680. * @type string $comment_date The date the comment was submitted. To set the date
  1681. * manually, `$comment_date_gmt` must also be specified.
  1682. * Default is the current time.
  1683. * @type string $comment_date_gmt The date the comment was submitted in the GMT timezone.
  1684. * Default is `$comment_date` in the site's GMT timezone.
  1685. * @type int $comment_karma The karma of the comment. Default 0.
  1686. * @type int $comment_parent ID of this comment's parent, if any. Default 0.
  1687. * @type int $comment_post_ID ID of the post that relates to the comment, if any.
  1688. * Default 0.
  1689. * @type string $comment_type Comment type. Default empty.
  1690. * @type array $comment_meta Optional. Array of key/value pairs to be stored in commentmeta for the
  1691. * new comment.
  1692. * @type int $user_id ID of the user who submitted the comment. Default 0.
  1693. * }
  1694. * @return int|false The new comment's ID on success, false on failure.
  1695. */
  1696. function wp_insert_comment( $commentdata ) {
  1697. global $wpdb;
  1698. $data = wp_unslash( $commentdata );
  1699. $comment_author = ! isset( $data['comment_author'] ) ? '' : $data['comment_author'];
  1700. $comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email'];
  1701. $comment_author_url = ! isset( $data['comment_author_url'] ) ? '' : $data['comment_author_url'];
  1702. $comment_author_IP = ! isset( $data['comment_author_IP'] ) ? '' : $data['comment_author_IP'];
  1703. $comment_date = ! isset( $data['comment_date'] ) ? current_time( 'mysql' ) : $data['comment_date'];
  1704. $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt'];
  1705. $comment_post_ID = ! isset( $data['comment_post_ID'] ) ? 0 : $data['comment_post_ID'];
  1706. $comment_content = ! isset( $data['comment_content'] ) ? '' : $data['comment_content'];
  1707. $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma'];
  1708. $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved'];
  1709. $comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent'];
  1710. $comment_type = ! isset( $data['comment_type'] ) ? '' : $data['comment_type'];
  1711. $comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent'];
  1712. $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id'];
  1713. $compacted = compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id' );
  1714. if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
  1715. return false;
  1716. }
  1717. $id = (int) $wpdb->insert_id;
  1718. if ( $comment_approved == 1 ) {
  1719. wp_update_comment_count( $comment_post_ID );
  1720. foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
  1721. wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' );
  1722. }
  1723. }
  1724. clean_comment_cache( $id );
  1725. $comment = get_comment( $id );
  1726. // If metadata is provided, store it.
  1727. if ( isset( $commentdata['comment_meta'] ) && is_array( $commentdata['comment_meta'] ) ) {
  1728. foreach ( $commentdata['comment_meta'] as $meta_key => $meta_value ) {
  1729. add_comment_meta( $comment->comment_ID, $meta_key, $meta_value, true );
  1730. }
  1731. }
  1732. /**
  1733. * Fires immediately after a comment is inserted into the database.
  1734. *
  1735. * @since 2.8.0
  1736. *
  1737. * @param int $id The comment ID.
  1738. * @param WP_Comment $comment Comment object.
  1739. */
  1740. do_action( 'wp_insert_comment', $id, $comment );
  1741. return $id;
  1742. }
  1743. /**
  1744. * Filters and sanitizes comment data.
  1745. *
  1746. * Sets the comment data 'filtered' field to true when finished. This can be
  1747. * checked as to whether the comment should be filtered and to keep from
  1748. * filtering the same comment more than once.
  1749. *
  1750. * @since 2.0.0
  1751. *
  1752. * @param array $commentdata Contains information on the comment.
  1753. * @return array Parsed comment information.
  1754. */
  1755. function wp_filter_comment( $commentdata ) {
  1756. if ( isset( $commentdata['user_ID'] ) ) {
  1757. /**
  1758. * Filters the comment author's user id before it is set.
  1759. *
  1760. * The first time this filter is evaluated, 'user_ID' is checked
  1761. * (for back-compat), followed by the standard 'user_id' value.
  1762. *
  1763. * @since 1.5.0
  1764. *
  1765. * @param int $user_ID The comment author's user ID.
  1766. */
  1767. $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_ID'] );
  1768. } elseif ( isset( $commentdata['user_id'] ) ) {
  1769. /** This filter is documented in wp-includes/comment.php */
  1770. $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_id'] );
  1771. }
  1772. /**
  1773. * Filters the comment author's browser user agent before it is set.
  1774. *
  1775. * @since 1.5.0
  1776. *
  1777. * @param string $comment_agent The comment author's browser user agent.
  1778. */
  1779. $commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
  1780. /** This filter is documented in wp-includes/comment.php */
  1781. $commentdata['comment_author'] = apply_filters( 'pre_comment_author_name', $commentdata['comment_author'] );
  1782. /**
  1783. * Filters the comment content before it is set.
  1784. *
  1785. * @since 1.5.0
  1786. *
  1787. * @param string $comment_content The comment content.
  1788. */
  1789. $commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] );
  1790. /**
  1791. * Filters the comment author's IP address before it is set.
  1792. *
  1793. * @since 1.5.0
  1794. *
  1795. * @param string $comment_author_ip The comment author's IP address.
  1796. */
  1797. $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
  1798. /** This filter is documented in wp-includes/comment.php */
  1799. $commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] );
  1800. /** This filter is documented in wp-includes/comment.php */
  1801. $commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] );
  1802. $commentdata['filtered'] = true;
  1803. return $commentdata;
  1804. }
  1805. /**
  1806. * Whether a comment should be blocked because of comment flood.
  1807. *
  1808. * @since 2.1.0
  1809. *
  1810. * @param bool $block Whether plugin has already blocked comment.
  1811. * @param int $time_lastcomment Timestamp for last comment.
  1812. * @param int $time_newcomment Timestamp for new comment.
  1813. * @return bool Whether comment should be blocked.
  1814. */
  1815. function wp_throttle_comment_flood( $block, $time_lastcomment, $time_newcomment ) {
  1816. if ( $block ) { // a plugin has already blocked... we'll let that decision stand
  1817. return $block;
  1818. }
  1819. if ( ( $time_newcomment - $time_lastcomment ) < 15 ) {
  1820. return true;
  1821. }
  1822. return false;
  1823. }
  1824. /**
  1825. * Adds a new comment to the database.
  1826. *
  1827. * Filters new comment to ensure that the fields are sanitized and valid before
  1828. * inserting comment into database. Calls {@see 'comment_post'} action with comment ID
  1829. * and whether comment is approved by WordPress. Also has {@see 'preprocess_comment'}
  1830. * filter for processing the comment data before the function handles it.
  1831. *
  1832. * We use `REMOTE_ADDR` here directly. If you are behind a proxy, you should ensure
  1833. * that it is properly set, such as in wp-config.php, for your environment.
  1834. *
  1835. * See {@link https://core.trac.wordpress.org/ticket/9235}
  1836. *
  1837. * @since 1.5.0
  1838. * @since 4.3.0 'comment_agent' and 'comment_author_IP' can be set via `$commentdata`.
  1839. * @since 4.7.0 The `$avoid_die` parameter was added, allowing the function to
  1840. * return a WP_Error object instead of dying.
  1841. *
  1842. * @see wp_insert_comment()
  1843. * @global wpdb $wpdb WordPress database abstraction object.
  1844. *
  1845. * @param array $commentdata {
  1846. * Comment data.
  1847. *
  1848. * @type string $comment_author The name of the comment author.
  1849. * @type string $comment_author_email The comment author email address.
  1850. * @type string $comment_author_url The comment author URL.
  1851. * @type string $comment_content The content of the comment.
  1852. * @type string $comment_date The date the comment was submitted. Default is the current time.
  1853. * @type string $comment_date_gmt The date the comment was submitted in the GMT timezone.
  1854. * Default is `$comment_date` in the GMT timezone.
  1855. * @type int $comment_parent The ID of this comment's parent, if any. Default 0.
  1856. * @type int $comment_post_ID The ID of the post that relates to the comment.
  1857. * @type int $user_id The ID of the user who submitted the comment. Default 0.
  1858. * @type int $user_ID Kept for backward-compatibility. Use `$user_id` instead.
  1859. * @type string $comment_agent Comment author user agent. Default is the value of 'HTTP_USER_AGENT'
  1860. * in the `$_SERVER` superglobal sent in the original request.
  1861. * @type string $comment_author_IP Comment author IP address in IPv4 format. Default is the value of
  1862. * 'REMOTE_ADDR' in the `$_SERVER` superglobal sent in the original request.
  1863. * }
  1864. * @param bool $avoid_die Should errors be returned as WP_Error objects instead of
  1865. * executing wp_die()? Default false.
  1866. * @return int|false|WP_Error The ID of the comment on success, false or WP_Error on failure.
  1867. */
  1868. function wp_new_comment( $commentdata, $avoid_die = false ) {
  1869. global $wpdb;
  1870. if ( isset( $commentdata['user_ID'] ) ) {
  1871. $commentdata['user_ID'] = (int) $commentdata['user_ID'];
  1872. $commentdata['user_id'] = $commentdata['user_ID'];
  1873. }
  1874. $prefiltered_user_id = ( isset( $commentdata['user_id'] ) ) ? (int) $commentdata['user_id'] : 0;
  1875. /**
  1876. * Filters a comment's data before it is sanitized and inserted into the database.
  1877. *
  1878. * @since 1.5.0
  1879. *
  1880. * @param array $commentdata Comment data.
  1881. */
  1882. $commentdata = apply_filters( 'preprocess_comment', $commentdata );
  1883. $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
  1884. if ( isset( $commentdata['user_ID'] ) && $prefiltered_user_id !== (int) $commentdata['user_ID'] ) {
  1885. $commentdata['user_ID'] = (int) $commentdata['user_ID'];
  1886. $commentdata['user_id'] = $commentdata['user_ID'];
  1887. } elseif ( isset( $commentdata['user_id'] ) ) {
  1888. $commentdata['user_id'] = (int) $commentdata['user_id'];
  1889. }
  1890. $commentdata['comment_parent'] = isset( $commentdata['comment_parent'] ) ? absint( $commentdata['comment_parent'] ) : 0;
  1891. $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status( $commentdata['comment_parent'] ) : '';
  1892. $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
  1893. if ( ! isset( $commentdata['comment_author_IP'] ) ) {
  1894. $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR'];
  1895. }
  1896. $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] );
  1897. if ( ! isset( $commentdata['comment_agent'] ) ) {
  1898. $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
  1899. }
  1900. $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 );
  1901. if ( empty( $commentdata['comment_date'] ) ) {
  1902. $commentdata['comment_date'] = current_time( 'mysql' );
  1903. }
  1904. if ( empty( $commentdata['comment_date_gmt'] ) ) {
  1905. $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 );
  1906. }
  1907. $commentdata = wp_filter_comment( $commentdata );
  1908. $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die );
  1909. if ( is_wp_error( $commentdata['comment_approved'] ) ) {
  1910. return $commentdata['comment_approved'];
  1911. }
  1912. $comment_ID = wp_insert_comment( $commentdata );
  1913. if ( ! $comment_ID ) {
  1914. $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );
  1915. foreach ( $fields as $field ) {
  1916. if ( isset( $commentdata[ $field ] ) ) {
  1917. $commentdata[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $commentdata[ $field ] );
  1918. }
  1919. }
  1920. $commentdata = wp_filter_comment( $commentdata );
  1921. $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die );
  1922. if ( is_wp_error( $commentdata['comment_approved'] ) ) {
  1923. return $commentdata['comment_approved'];
  1924. }
  1925. $comment_ID = wp_insert_comment( $commentdata );
  1926. if ( ! $comment_ID ) {
  1927. return false;
  1928. }
  1929. }
  1930. /**
  1931. * Fires immediately after a comment is inserted into the database.
  1932. *
  1933. * @since 1.2.0
  1934. * @since 4.5.0 The `$commentdata` parameter was added.
  1935. *
  1936. * @param int $comment_ID The comment ID.
  1937. * @param int|string $comment_approved 1 if the comment is approved, 0 if not, 'spam' if spam.
  1938. * @param array $commentdata Comment data.
  1939. */
  1940. do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'], $commentdata );
  1941. return $comment_ID;
  1942. }
  1943. /**
  1944. * Send a comment moderation notification to the comment moderator.
  1945. *
  1946. * @since 4.4.0
  1947. *
  1948. * @param int $comment_ID ID of the comment.
  1949. * @return bool True on success, false on failure.
  1950. */
  1951. function wp_new_comment_notify_moderator( $comment_ID ) {
  1952. $comment = get_comment( $comment_ID );
  1953. // Only send notifications for pending comments.
  1954. $maybe_notify = ( '0' == $comment->comment_approved );
  1955. /** This filter is documented in wp-includes/comment.php */
  1956. $maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_ID );
  1957. if ( ! $maybe_notify ) {
  1958. return false;
  1959. }
  1960. return wp_notify_moderator( $comment_ID );
  1961. }
  1962. /**
  1963. * Send a notification of a new comment to the post author.
  1964. *
  1965. * @since 4.4.0
  1966. *
  1967. * Uses the {@see 'notify_post_author'} filter to determine whether the post author
  1968. * should be notified when a new comment is added, overriding site setting.
  1969. *
  1970. * @param int $comment_ID Comment ID.
  1971. * @return bool True on success, false on failure.
  1972. */
  1973. function wp_new_comment_notify_postauthor( $comment_ID ) {
  1974. $comment = get_comment( $comment_ID );
  1975. $maybe_notify = get_option( 'comments_notify' );
  1976. /**
  1977. * Filters whether to send the post author new comment notification emails,
  1978. * overriding the site setting.
  1979. *
  1980. * @since 4.4.0
  1981. *
  1982. * @param bool $maybe_notify Whether to notify the post author about the new comment.
  1983. * @param int $comment_ID The ID of the comment for the notification.
  1984. */
  1985. $maybe_notify = apply_filters( 'notify_post_author', $maybe_notify, $comment_ID );
  1986. /*
  1987. * wp_notify_postauthor() checks if notifying the author of their own comment.
  1988. * By default, it won't, but filters can override this.
  1989. */
  1990. if ( ! $maybe_notify ) {
  1991. return false;
  1992. }
  1993. // Only send notifications for approved comments.
  1994. if ( ! isset( $comment->comment_approved ) || '1' != $comment->comment_approved ) {
  1995. return false;
  1996. }
  1997. return wp_notify_postauthor( $comment_ID );
  1998. }
  1999. /**
  2000. * Sets the status of a comment.
  2001. *
  2002. * The {@see 'wp_set_comment_status'} action is called after the comment is handled.
  2003. * If the comment status is not in the list, then false is returned.
  2004. *
  2005. * @since 1.0.0
  2006. *
  2007. * @global wpdb $wpdb WordPress database abstraction object.
  2008. *
  2009. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  2010. * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
  2011. * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
  2012. * @return bool|WP_Error True on success, false or WP_Error on failure.
  2013. */
  2014. function wp_set_comment_status( $comment_id, $comment_status, $wp_error = false ) {
  2015. global $wpdb;
  2016. switch ( $comment_status ) {
  2017. case 'hold':
  2018. case '0':
  2019. $status = '0';
  2020. break;
  2021. case 'approve':
  2022. case '1':
  2023. $status = '1';
  2024. add_action( 'wp_set_comment_status', 'wp_new_comment_notify_postauthor' );
  2025. break;
  2026. case 'spam':
  2027. $status = 'spam';
  2028. break;
  2029. case 'trash':
  2030. $status = 'trash';
  2031. break;
  2032. default:
  2033. return false;
  2034. }
  2035. $comment_old = clone get_comment( $comment_id );
  2036. if ( ! $wpdb->update( $wpdb->comments, array( 'comment_approved' => $status ), array( 'comment_ID' => $comment_old->comment_ID ) ) ) {
  2037. if ( $wp_error ) {
  2038. return new WP_Error( 'db_update_error', __( 'Could not update comment status' ), $wpdb->last_error );
  2039. } else {
  2040. return false;
  2041. }
  2042. }
  2043. clean_comment_cache( $comment_old->comment_ID );
  2044. $comment = get_comment( $comment_old->comment_ID );
  2045. /**
  2046. * Fires immediately before transitioning a comment's status from one to another
  2047. * in the database.
  2048. *
  2049. * @since 1.5.0
  2050. *
  2051. * @param int $comment_id Comment ID.
  2052. * @param string|bool $comment_status Current comment status. Possible values include
  2053. * 'hold', 'approve', 'spam', 'trash', or false.
  2054. */
  2055. do_action( 'wp_set_comment_status', $comment->comment_ID, $comment_status );
  2056. wp_transition_comment_status( $comment_status, $comment_old->comment_approved, $comment );
  2057. wp_update_comment_count( $comment->comment_post_ID );
  2058. return true;
  2059. }
  2060. /**
  2061. * Updates an existing comment in the database.
  2062. *
  2063. * Filters the comment and makes sure certain fields are valid before updating.
  2064. *
  2065. * @since 2.0.0
  2066. * @since 4.9.0 Add updating comment meta during comment update.
  2067. *
  2068. * @global wpdb $wpdb WordPress database abstraction object.
  2069. *
  2070. * @param array $commentarr Contains information on the comment.
  2071. * @return int Comment was updated if value is 1, or was not updated if value is 0.
  2072. */
  2073. function wp_update_comment( $commentarr ) {
  2074. global $wpdb;
  2075. // First, get all of the original fields
  2076. $comment = get_comment( $commentarr['comment_ID'], ARRAY_A );
  2077. if ( empty( $comment ) ) {
  2078. return 0;
  2079. }
  2080. // Make sure that the comment post ID is valid (if specified).
  2081. if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
  2082. return 0;
  2083. }
  2084. // Escape data pulled from DB.
  2085. $comment = wp_slash( $comment );
  2086. $old_status = $comment['comment_approved'];
  2087. // Merge old and new fields with new fields overwriting old ones.
  2088. $commentarr = array_merge( $comment, $commentarr );
  2089. $commentarr = wp_filter_comment( $commentarr );
  2090. // Now extract the merged array.
  2091. $data = wp_unslash( $commentarr );
  2092. /**
  2093. * Filters the comment content before it is updated in the database.
  2094. *
  2095. * @since 1.5.0
  2096. *
  2097. * @param string $comment_content The comment data.
  2098. */
  2099. $data['comment_content'] = apply_filters( 'comment_save_pre', $data['comment_content'] );
  2100. $data['comment_date_gmt'] = get_gmt_from_date( $data['comment_date'] );
  2101. if ( ! isset( $data['comment_approved'] ) ) {
  2102. $data['comment_approved'] = 1;
  2103. } elseif ( 'hold' == $data['comment_approved'] ) {
  2104. $data['comment_approved'] = 0;
  2105. } elseif ( 'approve' == $data['comment_approved'] ) {
  2106. $data['comment_approved'] = 1;
  2107. }
  2108. $comment_ID = $data['comment_ID'];
  2109. $comment_post_ID = $data['comment_post_ID'];
  2110. /**
  2111. * Filters the comment data immediately before it is updated in the database.
  2112. *
  2113. * Note: data being passed to the filter is already unslashed.
  2114. *
  2115. * @since 4.7.0
  2116. *
  2117. * @param array $data The new, processed comment data.
  2118. * @param array $comment The old, unslashed comment data.
  2119. * @param array $commentarr The new, raw comment data.
  2120. */
  2121. $data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr );
  2122. $keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id', 'comment_agent', 'comment_author_IP' );
  2123. $data = wp_array_slice_assoc( $data, $keys );
  2124. $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
  2125. // If metadata is provided, store it.
  2126. if ( isset( $commentarr['comment_meta'] ) && is_array( $commentarr['comment_meta'] ) ) {
  2127. foreach ( $commentarr['comment_meta'] as $meta_key => $meta_value ) {
  2128. update_comment_meta( $comment_ID, $meta_key, $meta_value );
  2129. }
  2130. }
  2131. clean_comment_cache( $comment_ID );
  2132. wp_update_comment_count( $comment_post_ID );
  2133. /**
  2134. * Fires immediately after a comment is updated in the database.
  2135. *
  2136. * The hook also fires immediately before comment status transition hooks are fired.
  2137. *
  2138. * @since 1.2.0
  2139. * @since 4.6.0 Added the `$data` parameter.
  2140. *
  2141. * @param int $comment_ID The comment ID.
  2142. * @param array $data Comment data.
  2143. */
  2144. do_action( 'edit_comment', $comment_ID, $data );
  2145. $comment = get_comment( $comment_ID );
  2146. wp_transition_comment_status( $comment->comment_approved, $old_status, $comment );
  2147. return $rval;
  2148. }
  2149. /**
  2150. * Whether to defer comment counting.
  2151. *
  2152. * When setting $defer to true, all post comment counts will not be updated
  2153. * until $defer is set to false. When $defer is set to false, then all
  2154. * previously deferred updated post comment counts will then be automatically
  2155. * updated without having to call wp_update_comment_count() after.
  2156. *
  2157. * @since 2.5.0
  2158. * @staticvar bool $_defer
  2159. *
  2160. * @param bool $defer
  2161. * @return bool
  2162. */
  2163. function wp_defer_comment_counting( $defer = null ) {
  2164. static $_defer = false;
  2165. if ( is_bool( $defer ) ) {
  2166. $_defer = $defer;
  2167. // flush any deferred counts
  2168. if ( ! $defer ) {
  2169. wp_update_comment_count( null, true );
  2170. }
  2171. }
  2172. return $_defer;
  2173. }
  2174. /**
  2175. * Updates the comment count for post(s).
  2176. *
  2177. * When $do_deferred is false (is by default) and the comments have been set to
  2178. * be deferred, the post_id will be added to a queue, which will be updated at a
  2179. * later date and only updated once per post ID.
  2180. *
  2181. * If the comments have not be set up to be deferred, then the post will be
  2182. * updated. When $do_deferred is set to true, then all previous deferred post
  2183. * IDs will be updated along with the current $post_id.
  2184. *
  2185. * @since 2.1.0
  2186. * @see wp_update_comment_count_now() For what could cause a false return value
  2187. *
  2188. * @staticvar array $_deferred
  2189. *
  2190. * @param int|null $post_id Post ID.
  2191. * @param bool $do_deferred Optional. Whether to process previously deferred
  2192. * post comment counts. Default false.
  2193. * @return bool|void True on success, false on failure or if post with ID does
  2194. * not exist.
  2195. */
  2196. function wp_update_comment_count( $post_id, $do_deferred = false ) {
  2197. static $_deferred = array();
  2198. if ( empty( $post_id ) && ! $do_deferred ) {
  2199. return false;
  2200. }
  2201. if ( $do_deferred ) {
  2202. $_deferred = array_unique( $_deferred );
  2203. foreach ( $_deferred as $i => $_post_id ) {
  2204. wp_update_comment_count_now( $_post_id );
  2205. unset( $_deferred[ $i ] );
  2206. /** @todo Move this outside of the foreach and reset $_deferred to an array instead */
  2207. }
  2208. }
  2209. if ( wp_defer_comment_counting() ) {
  2210. $_deferred[] = $post_id;
  2211. return true;
  2212. } elseif ( $post_id ) {
  2213. return wp_update_comment_count_now( $post_id );
  2214. }
  2215. }
  2216. /**
  2217. * Updates the comment count for the post.
  2218. *
  2219. * @since 2.5.0
  2220. *
  2221. * @global wpdb $wpdb WordPress database abstraction object.
  2222. *
  2223. * @param int $post_id Post ID
  2224. * @return bool True on success, false if the post does not exist.
  2225. */
  2226. function wp_update_comment_count_now( $post_id ) {
  2227. global $wpdb;
  2228. $post_id = (int) $post_id;
  2229. if ( ! $post_id ) {
  2230. return false;
  2231. }
  2232. wp_cache_delete( 'comments-0', 'counts' );
  2233. wp_cache_delete( "comments-{$post_id}", 'counts' );
  2234. $post = get_post( $post_id );
  2235. if ( ! $post ) {
  2236. return false;
  2237. }
  2238. $old = (int) $post->comment_count;
  2239. /**
  2240. * Filters a post's comment count before it is updated in the database.
  2241. *
  2242. * @since 4.5.0
  2243. *
  2244. * @param int|null $new The new comment count. Default null.
  2245. * @param int $old The old comment count.
  2246. * @param int $post_id Post ID.
  2247. */
  2248. $new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id );
  2249. if ( is_null( $new ) ) {
  2250. $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id ) );
  2251. } else {
  2252. $new = (int) $new;
  2253. }
  2254. $wpdb->update( $wpdb->posts, array( 'comment_count' => $new ), array( 'ID' => $post_id ) );
  2255. clean_post_cache( $post );
  2256. /**
  2257. * Fires immediately after a post's comment count is updated in the database.
  2258. *
  2259. * @since 2.3.0
  2260. *
  2261. * @param int $post_id Post ID.
  2262. * @param int $new The new comment count.
  2263. * @param int $old The old comment count.
  2264. */
  2265. do_action( 'wp_update_comment_count', $post_id, $new, $old );
  2266. /** This action is documented in wp-includes/post.php */
  2267. do_action( "edit_post_{$post->post_type}", $post_id, $post );
  2268. /** This action is documented in wp-includes/post.php */
  2269. do_action( 'edit_post', $post_id, $post );
  2270. return true;
  2271. }
  2272. //
  2273. // Ping and trackback functions.
  2274. //
  2275. /**
  2276. * Finds a pingback server URI based on the given URL.
  2277. *
  2278. * Checks the HTML for the rel="pingback" link and x-pingback headers. It does
  2279. * a check for the x-pingback headers first and returns that, if available. The
  2280. * check for the rel="pingback" has more overhead than just the header.
  2281. *
  2282. * @since 1.5.0
  2283. *
  2284. * @param string $url URL to ping.
  2285. * @param int $deprecated Not Used.
  2286. * @return false|string False on failure, string containing URI on success.
  2287. */
  2288. function discover_pingback_server_uri( $url, $deprecated = '' ) {
  2289. if ( ! empty( $deprecated ) ) {
  2290. _deprecated_argument( __FUNCTION__, '2.7.0' );
  2291. }
  2292. $pingback_str_dquote = 'rel="pingback"';
  2293. $pingback_str_squote = 'rel=\'pingback\'';
  2294. /** @todo Should use Filter Extension or custom preg_match instead. */
  2295. $parsed_url = parse_url( $url );
  2296. if ( ! isset( $parsed_url['host'] ) ) { // Not a URL. This should never happen.
  2297. return false;
  2298. }
  2299. //Do not search for a pingback server on our own uploads
  2300. $uploads_dir = wp_get_upload_dir();
  2301. if ( 0 === strpos( $url, $uploads_dir['baseurl'] ) ) {
  2302. return false;
  2303. }
  2304. $response = wp_safe_remote_head(
  2305. $url,
  2306. array(
  2307. 'timeout' => 2,
  2308. 'httpversion' => '1.0',
  2309. )
  2310. );
  2311. if ( is_wp_error( $response ) ) {
  2312. return false;
  2313. }
  2314. if ( wp_remote_retrieve_header( $response, 'x-pingback' ) ) {
  2315. return wp_remote_retrieve_header( $response, 'x-pingback' );
  2316. }
  2317. // Not an (x)html, sgml, or xml page, no use going further.
  2318. if ( preg_match( '#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' ) ) ) {
  2319. return false;
  2320. }
  2321. // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file)
  2322. $response = wp_safe_remote_get(
  2323. $url,
  2324. array(
  2325. 'timeout' => 2,
  2326. 'httpversion' => '1.0',
  2327. )
  2328. );
  2329. if ( is_wp_error( $response ) ) {
  2330. return false;
  2331. }
  2332. $contents = wp_remote_retrieve_body( $response );
  2333. $pingback_link_offset_dquote = strpos( $contents, $pingback_str_dquote );
  2334. $pingback_link_offset_squote = strpos( $contents, $pingback_str_squote );
  2335. if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
  2336. $quote = ( $pingback_link_offset_dquote ) ? '"' : '\'';
  2337. $pingback_link_offset = ( $quote == '"' ) ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
  2338. $pingback_href_pos = strpos( $contents, 'href=', $pingback_link_offset );
  2339. $pingback_href_start = $pingback_href_pos + 6;
  2340. $pingback_href_end = strpos( $contents, $quote, $pingback_href_start );
  2341. $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
  2342. $pingback_server_url = substr( $contents, $pingback_href_start, $pingback_server_url_len );
  2343. // We may find rel="pingback" but an incomplete pingback URL
  2344. if ( $pingback_server_url_len > 0 ) { // We got it!
  2345. return $pingback_server_url;
  2346. }
  2347. }
  2348. return false;
  2349. }
  2350. /**
  2351. * Perform all pingbacks, enclosures, trackbacks, and send to pingback services.
  2352. *
  2353. * @since 2.1.0
  2354. *
  2355. * @global wpdb $wpdb WordPress database abstraction object.
  2356. */
  2357. function do_all_pings() {
  2358. global $wpdb;
  2359. // Do pingbacks.
  2360. $pings = get_posts(
  2361. array(
  2362. 'post_type' => get_post_types(),
  2363. 'suppress_filters' => false,
  2364. 'nopaging' => true,
  2365. 'meta_key' => '_pingme',
  2366. 'fields' => 'ids',
  2367. )
  2368. );
  2369. foreach ( $pings as $ping ) {
  2370. delete_post_meta( $ping, '_pingme' );
  2371. pingback( null, $ping );
  2372. }
  2373. // Do enclosures.
  2374. $enclosures = get_posts(
  2375. array(
  2376. 'post_type' => get_post_types(),
  2377. 'suppress_filters' => false,
  2378. 'nopaging' => true,
  2379. 'meta_key' => '_encloseme',
  2380. 'fields' => 'ids',
  2381. )
  2382. );
  2383. foreach ( $enclosures as $enclosure ) {
  2384. delete_post_meta( $enclosure, '_encloseme' );
  2385. do_enclose( null, $enclosure );
  2386. }
  2387. // Do trackbacks.
  2388. $trackbacks = get_posts(
  2389. array(
  2390. 'post_type' => get_post_types(),
  2391. 'suppress_filters' => false,
  2392. 'nopaging' => true,
  2393. 'meta_key' => '_trackbackme',
  2394. 'fields' => 'ids',
  2395. )
  2396. );
  2397. foreach ( $trackbacks as $trackback ) {
  2398. delete_post_meta( $trackback, '_trackbackme' );
  2399. do_trackbacks( $trackback );
  2400. }
  2401. // Do Update Services/Generic Pings.
  2402. generic_ping();
  2403. }
  2404. /**
  2405. * Perform trackbacks.
  2406. *
  2407. * @since 1.5.0
  2408. * @since 4.7.0 `$post_id` can be a WP_Post object.
  2409. *
  2410. * @global wpdb $wpdb WordPress database abstraction object.
  2411. *
  2412. * @param int|WP_Post $post_id Post object or ID to do trackbacks on.
  2413. */
  2414. function do_trackbacks( $post_id ) {
  2415. global $wpdb;
  2416. $post = get_post( $post_id );
  2417. if ( ! $post ) {
  2418. return false;
  2419. }
  2420. $to_ping = get_to_ping( $post );
  2421. $pinged = get_pung( $post );
  2422. if ( empty( $to_ping ) ) {
  2423. $wpdb->update( $wpdb->posts, array( 'to_ping' => '' ), array( 'ID' => $post->ID ) );
  2424. return;
  2425. }
  2426. if ( empty( $post->post_excerpt ) ) {
  2427. /** This filter is documented in wp-includes/post-template.php */
  2428. $excerpt = apply_filters( 'the_content', $post->post_content, $post->ID );
  2429. } else {
  2430. /** This filter is documented in wp-includes/post-template.php */
  2431. $excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
  2432. }
  2433. $excerpt = str_replace( ']]>', ']]&gt;', $excerpt );
  2434. $excerpt = wp_html_excerpt( $excerpt, 252, '&#8230;' );
  2435. /** This filter is documented in wp-includes/post-template.php */
  2436. $post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
  2437. $post_title = strip_tags( $post_title );
  2438. if ( $to_ping ) {
  2439. foreach ( (array) $to_ping as $tb_ping ) {
  2440. $tb_ping = trim( $tb_ping );
  2441. if ( ! in_array( $tb_ping, $pinged ) ) {
  2442. trackback( $tb_ping, $post_title, $excerpt, $post->ID );
  2443. $pinged[] = $tb_ping;
  2444. } else {
  2445. $wpdb->query(
  2446. $wpdb->prepare(
  2447. "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s,
  2448. '')) WHERE ID = %d",
  2449. $tb_ping,
  2450. $post->ID
  2451. )
  2452. );
  2453. }
  2454. }
  2455. }
  2456. }
  2457. /**
  2458. * Sends pings to all of the ping site services.
  2459. *
  2460. * @since 1.2.0
  2461. *
  2462. * @param int $post_id Post ID.
  2463. * @return int Same as Post ID from parameter
  2464. */
  2465. function generic_ping( $post_id = 0 ) {
  2466. $services = get_option( 'ping_sites' );
  2467. $services = explode( "\n", $services );
  2468. foreach ( (array) $services as $service ) {
  2469. $service = trim( $service );
  2470. if ( '' != $service ) {
  2471. weblog_ping( $service );
  2472. }
  2473. }
  2474. return $post_id;
  2475. }
  2476. /**
  2477. * Pings back the links found in a post.
  2478. *
  2479. * @since 0.71
  2480. * @since 4.7.0 `$post_id` can be a WP_Post object.
  2481. *
  2482. * @param string $content Post content to check for links. If empty will retrieve from post.
  2483. * @param int|WP_Post $post_id Post Object or ID.
  2484. */
  2485. function pingback( $content, $post_id ) {
  2486. include_once( ABSPATH . WPINC . '/class-IXR.php' );
  2487. include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
  2488. // original code by Mort (http://mort.mine.nu:8080)
  2489. $post_links = array();
  2490. $post = get_post( $post_id );
  2491. if ( ! $post ) {
  2492. return;
  2493. }
  2494. $pung = get_pung( $post );
  2495. if ( empty( $content ) ) {
  2496. $content = $post->post_content;
  2497. }
  2498. // Step 1
  2499. // Parsing the post, external links (if any) are stored in the $post_links array
  2500. $post_links_temp = wp_extract_urls( $content );
  2501. // Step 2.
  2502. // Walking thru the links array
  2503. // first we get rid of links pointing to sites, not to specific files
  2504. // Example:
  2505. // http://dummy-weblog.org
  2506. // http://dummy-weblog.org/
  2507. // http://dummy-weblog.org/post.php
  2508. // We don't wanna ping first and second types, even if they have a valid <link/>
  2509. foreach ( (array) $post_links_temp as $link_test ) :
  2510. if ( ! in_array( $link_test, $pung ) && ( url_to_postid( $link_test ) != $post->ID ) // If we haven't pung it already and it isn't a link to itself
  2511. && ! is_local_attachment( $link_test ) ) : // Also, let's never ping local attachments.
  2512. $test = @parse_url( $link_test );
  2513. if ( $test ) {
  2514. if ( isset( $test['query'] ) ) {
  2515. $post_links[] = $link_test;
  2516. } elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) ) {
  2517. $post_links[] = $link_test;
  2518. }
  2519. }
  2520. endif;
  2521. endforeach;
  2522. $post_links = array_unique( $post_links );
  2523. /**
  2524. * Fires just before pinging back links found in a post.
  2525. *
  2526. * @since 2.0.0
  2527. *
  2528. * @param string[] $post_links Array of link URLs to be checked (passed by reference).
  2529. * @param string[] $pung Array of link URLs already pinged (passed by reference).
  2530. * @param int $post_ID The post ID.
  2531. */
  2532. do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post->ID ) );
  2533. foreach ( (array) $post_links as $pagelinkedto ) {
  2534. $pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
  2535. if ( $pingback_server_url ) {
  2536. set_time_limit( 60 );
  2537. // Now, the RPC call
  2538. $pagelinkedfrom = get_permalink( $post );
  2539. // using a timeout of 3 seconds should be enough to cover slow servers
  2540. $client = new WP_HTTP_IXR_Client( $pingback_server_url );
  2541. $client->timeout = 3;
  2542. /**
  2543. * Filters the user agent sent when pinging-back a URL.
  2544. *
  2545. * @since 2.9.0
  2546. *
  2547. * @param string $concat_useragent The user agent concatenated with ' -- WordPress/'
  2548. * and the WordPress version.
  2549. * @param string $useragent The useragent.
  2550. * @param string $pingback_server_url The server URL being linked to.
  2551. * @param string $pagelinkedto URL of page linked to.
  2552. * @param string $pagelinkedfrom URL of page linked from.
  2553. */
  2554. $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . get_bloginfo( 'version' ), $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom );
  2555. // when set to true, this outputs debug messages by itself
  2556. $client->debug = false;
  2557. if ( $client->query( 'pingback.ping', $pagelinkedfrom, $pagelinkedto ) || ( isset( $client->error->code ) && 48 == $client->error->code ) ) { // Already registered
  2558. add_ping( $post, $pagelinkedto );
  2559. }
  2560. }
  2561. }
  2562. }
  2563. /**
  2564. * Check whether blog is public before returning sites.
  2565. *
  2566. * @since 2.1.0
  2567. *
  2568. * @param mixed $sites Will return if blog is public, will not return if not public.
  2569. * @return mixed Empty string if blog is not public, returns $sites, if site is public.
  2570. */
  2571. function privacy_ping_filter( $sites ) {
  2572. if ( '0' != get_option( 'blog_public' ) ) {
  2573. return $sites;
  2574. } else {
  2575. return '';
  2576. }
  2577. }
  2578. /**
  2579. * Send a Trackback.
  2580. *
  2581. * Updates database when sending trackback to prevent duplicates.
  2582. *
  2583. * @since 0.71
  2584. *
  2585. * @global wpdb $wpdb WordPress database abstraction object.
  2586. *
  2587. * @param string $trackback_url URL to send trackbacks.
  2588. * @param string $title Title of post.
  2589. * @param string $excerpt Excerpt of post.
  2590. * @param int $ID Post ID.
  2591. * @return int|false|void Database query from update.
  2592. */
  2593. function trackback( $trackback_url, $title, $excerpt, $ID ) {
  2594. global $wpdb;
  2595. if ( empty( $trackback_url ) ) {
  2596. return;
  2597. }
  2598. $options = array();
  2599. $options['timeout'] = 10;
  2600. $options['body'] = array(
  2601. 'title' => $title,
  2602. 'url' => get_permalink( $ID ),
  2603. 'blog_name' => get_option( 'blogname' ),
  2604. 'excerpt' => $excerpt,
  2605. );
  2606. $response = wp_safe_remote_post( $trackback_url, $options );
  2607. if ( is_wp_error( $response ) ) {
  2608. return;
  2609. }
  2610. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID ) );
  2611. return $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID ) );
  2612. }
  2613. /**
  2614. * Send a pingback.
  2615. *
  2616. * @since 1.2.0
  2617. *
  2618. * @param string $server Host of blog to connect to.
  2619. * @param string $path Path to send the ping.
  2620. */
  2621. function weblog_ping( $server = '', $path = '' ) {
  2622. include_once( ABSPATH . WPINC . '/class-IXR.php' );
  2623. include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
  2624. // using a timeout of 3 seconds should be enough to cover slow servers
  2625. $client = new WP_HTTP_IXR_Client( $server, ( ( ! strlen( trim( $path ) ) || ( '/' == $path ) ) ? false : $path ) );
  2626. $client->timeout = 3;
  2627. $client->useragent .= ' -- WordPress/' . get_bloginfo( 'version' );
  2628. // when set to true, this outputs debug messages by itself
  2629. $client->debug = false;
  2630. $home = trailingslashit( home_url() );
  2631. if ( ! $client->query( 'weblogUpdates.extendedPing', get_option( 'blogname' ), $home, get_bloginfo( 'rss2_url' ) ) ) { // then try a normal ping
  2632. $client->query( 'weblogUpdates.ping', get_option( 'blogname' ), $home );
  2633. }
  2634. }
  2635. /**
  2636. * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI
  2637. *
  2638. * @since 3.5.1
  2639. * @see wp_http_validate_url()
  2640. *
  2641. * @param string $source_uri
  2642. * @return string
  2643. */
  2644. function pingback_ping_source_uri( $source_uri ) {
  2645. return (string) wp_http_validate_url( $source_uri );
  2646. }
  2647. /**
  2648. * Default filter attached to xmlrpc_pingback_error.
  2649. *
  2650. * Returns a generic pingback error code unless the error code is 48,
  2651. * which reports that the pingback is already registered.
  2652. *
  2653. * @since 3.5.1
  2654. * @link https://www.hixie.ch/specs/pingback/pingback#TOC3
  2655. *
  2656. * @param IXR_Error $ixr_error
  2657. * @return IXR_Error
  2658. */
  2659. function xmlrpc_pingback_error( $ixr_error ) {
  2660. if ( $ixr_error->code === 48 ) {
  2661. return $ixr_error;
  2662. }
  2663. return new IXR_Error( 0, '' );
  2664. }
  2665. //
  2666. // Cache
  2667. //
  2668. /**
  2669. * Removes a comment from the object cache.
  2670. *
  2671. * @since 2.3.0
  2672. *
  2673. * @param int|array $ids Comment ID or an array of comment IDs to remove from cache.
  2674. */
  2675. function clean_comment_cache( $ids ) {
  2676. foreach ( (array) $ids as $id ) {
  2677. wp_cache_delete( $id, 'comment' );
  2678. /**
  2679. * Fires immediately after a comment has been removed from the object cache.
  2680. *
  2681. * @since 4.5.0
  2682. *
  2683. * @param int $id Comment ID.
  2684. */
  2685. do_action( 'clean_comment_cache', $id );
  2686. }
  2687. wp_cache_set( 'last_changed', microtime(), 'comment' );
  2688. }
  2689. /**
  2690. * Updates the comment cache of given comments.
  2691. *
  2692. * Will add the comments in $comments to the cache. If comment ID already exists
  2693. * in the comment cache then it will not be updated. The comment is added to the
  2694. * cache using the comment group with the key using the ID of the comments.
  2695. *
  2696. * @since 2.3.0
  2697. * @since 4.4.0 Introduced the `$update_meta_cache` parameter.
  2698. *
  2699. * @param WP_Comment[] $comments Array of comment objects
  2700. * @param bool $update_meta_cache Whether to update commentmeta cache. Default true.
  2701. */
  2702. function update_comment_cache( $comments, $update_meta_cache = true ) {
  2703. foreach ( (array) $comments as $comment ) {
  2704. wp_cache_add( $comment->comment_ID, $comment, 'comment' );
  2705. }
  2706. if ( $update_meta_cache ) {
  2707. // Avoid `wp_list_pluck()` in case `$comments` is passed by reference.
  2708. $comment_ids = array();
  2709. foreach ( $comments as $comment ) {
  2710. $comment_ids[] = $comment->comment_ID;
  2711. }
  2712. update_meta_cache( 'comment', $comment_ids );
  2713. }
  2714. }
  2715. /**
  2716. * Adds any comments from the given IDs to the cache that do not already exist in cache.
  2717. *
  2718. * @since 4.4.0
  2719. * @access private
  2720. *
  2721. * @see update_comment_cache()
  2722. * @global wpdb $wpdb WordPress database abstraction object.
  2723. *
  2724. * @param int[] $comment_ids Array of comment IDs.
  2725. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true.
  2726. */
  2727. function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) {
  2728. global $wpdb;
  2729. $non_cached_ids = _get_non_cached_ids( $comment_ids, 'comment' );
  2730. if ( ! empty( $non_cached_ids ) ) {
  2731. $fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) );
  2732. update_comment_cache( $fresh_comments, $update_meta_cache );
  2733. }
  2734. }
  2735. //
  2736. // Internal
  2737. //
  2738. /**
  2739. * Close comments on old posts on the fly, without any extra DB queries. Hooked to the_posts.
  2740. *
  2741. * @access private
  2742. * @since 2.7.0
  2743. *
  2744. * @param WP_Post $posts Post data object.
  2745. * @param WP_Query $query Query object.
  2746. * @return array
  2747. */
  2748. function _close_comments_for_old_posts( $posts, $query ) {
  2749. if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) ) {
  2750. return $posts;
  2751. }
  2752. /**
  2753. * Filters the list of post types to automatically close comments for.
  2754. *
  2755. * @since 3.2.0
  2756. *
  2757. * @param string[] $post_types An array of post type names.
  2758. */
  2759. $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
  2760. if ( ! in_array( $posts[0]->post_type, $post_types ) ) {
  2761. return $posts;
  2762. }
  2763. $days_old = (int) get_option( 'close_comments_days_old' );
  2764. if ( ! $days_old ) {
  2765. return $posts;
  2766. }
  2767. if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) {
  2768. $posts[0]->comment_status = 'closed';
  2769. $posts[0]->ping_status = 'closed';
  2770. }
  2771. return $posts;
  2772. }
  2773. /**
  2774. * Close comments on an old post. Hooked to comments_open and pings_open.
  2775. *
  2776. * @access private
  2777. * @since 2.7.0
  2778. *
  2779. * @param bool $open Comments open or closed
  2780. * @param int $post_id Post ID
  2781. * @return bool $open
  2782. */
  2783. function _close_comments_for_old_post( $open, $post_id ) {
  2784. if ( ! $open ) {
  2785. return $open;
  2786. }
  2787. if ( ! get_option( 'close_comments_for_old_posts' ) ) {
  2788. return $open;
  2789. }
  2790. $days_old = (int) get_option( 'close_comments_days_old' );
  2791. if ( ! $days_old ) {
  2792. return $open;
  2793. }
  2794. $post = get_post( $post_id );
  2795. /** This filter is documented in wp-includes/comment.php */
  2796. $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
  2797. if ( ! in_array( $post->post_type, $post_types ) ) {
  2798. return $open;
  2799. }
  2800. // Undated drafts should not show up as comments closed.
  2801. if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
  2802. return $open;
  2803. }
  2804. if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) {
  2805. return false;
  2806. }
  2807. return $open;
  2808. }
  2809. /**
  2810. * Handles the submission of a comment, usually posted to wp-comments-post.php via a comment form.
  2811. *
  2812. * This function expects unslashed data, as opposed to functions such as `wp_new_comment()` which
  2813. * expect slashed data.
  2814. *
  2815. * @since 4.4.0
  2816. *
  2817. * @param array $comment_data {
  2818. * Comment data.
  2819. *
  2820. * @type string|int $comment_post_ID The ID of the post that relates to the comment.
  2821. * @type string $author The name of the comment author.
  2822. * @type string $email The comment author email address.
  2823. * @type string $url The comment author URL.
  2824. * @type string $comment The content of the comment.
  2825. * @type string|int $comment_parent The ID of this comment's parent, if any. Default 0.
  2826. * @type string $_wp_unfiltered_html_comment The nonce value for allowing unfiltered HTML.
  2827. * }
  2828. * @return WP_Comment|WP_Error A WP_Comment object on success, a WP_Error object on failure.
  2829. */
  2830. function wp_handle_comment_submission( $comment_data ) {
  2831. $comment_post_ID = 0;
  2832. $comment_parent = 0;
  2833. $user_ID = 0;
  2834. $comment_author = null;
  2835. $comment_author_email = null;
  2836. $comment_author_url = null;
  2837. $comment_content = null;
  2838. if ( isset( $comment_data['comment_post_ID'] ) ) {
  2839. $comment_post_ID = (int) $comment_data['comment_post_ID'];
  2840. }
  2841. if ( isset( $comment_data['author'] ) && is_string( $comment_data['author'] ) ) {
  2842. $comment_author = trim( strip_tags( $comment_data['author'] ) );
  2843. }
  2844. if ( isset( $comment_data['email'] ) && is_string( $comment_data['email'] ) ) {
  2845. $comment_author_email = trim( $comment_data['email'] );
  2846. }
  2847. if ( isset( $comment_data['url'] ) && is_string( $comment_data['url'] ) ) {
  2848. $comment_author_url = trim( $comment_data['url'] );
  2849. }
  2850. if ( isset( $comment_data['comment'] ) && is_string( $comment_data['comment'] ) ) {
  2851. $comment_content = trim( $comment_data['comment'] );
  2852. }
  2853. if ( isset( $comment_data['comment_parent'] ) ) {
  2854. $comment_parent = absint( $comment_data['comment_parent'] );
  2855. }
  2856. $post = get_post( $comment_post_ID );
  2857. if ( empty( $post->comment_status ) ) {
  2858. /**
  2859. * Fires when a comment is attempted on a post that does not exist.
  2860. *
  2861. * @since 1.5.0
  2862. *
  2863. * @param int $comment_post_ID Post ID.
  2864. */
  2865. do_action( 'comment_id_not_found', $comment_post_ID );
  2866. return new WP_Error( 'comment_id_not_found' );
  2867. }
  2868. // get_post_status() will get the parent status for attachments.
  2869. $status = get_post_status( $post );
  2870. if ( ( 'private' == $status ) && ! current_user_can( 'read_post', $comment_post_ID ) ) {
  2871. return new WP_Error( 'comment_id_not_found' );
  2872. }
  2873. $status_obj = get_post_status_object( $status );
  2874. if ( ! comments_open( $comment_post_ID ) ) {
  2875. /**
  2876. * Fires when a comment is attempted on a post that has comments closed.
  2877. *
  2878. * @since 1.5.0
  2879. *
  2880. * @param int $comment_post_ID Post ID.
  2881. */
  2882. do_action( 'comment_closed', $comment_post_ID );
  2883. return new WP_Error( 'comment_closed', __( 'Sorry, comments are closed for this item.' ), 403 );
  2884. } elseif ( 'trash' == $status ) {
  2885. /**
  2886. * Fires when a comment is attempted on a trashed post.
  2887. *
  2888. * @since 2.9.0
  2889. *
  2890. * @param int $comment_post_ID Post ID.
  2891. */
  2892. do_action( 'comment_on_trash', $comment_post_ID );
  2893. return new WP_Error( 'comment_on_trash' );
  2894. } elseif ( ! $status_obj->public && ! $status_obj->private ) {
  2895. /**
  2896. * Fires when a comment is attempted on a post in draft mode.
  2897. *
  2898. * @since 1.5.1
  2899. *
  2900. * @param int $comment_post_ID Post ID.
  2901. */
  2902. do_action( 'comment_on_draft', $comment_post_ID );
  2903. if ( current_user_can( 'read_post', $comment_post_ID ) ) {
  2904. return new WP_Error( 'comment_on_draft', __( 'Sorry, comments are not allowed for this item.' ), 403 );
  2905. } else {
  2906. return new WP_Error( 'comment_on_draft' );
  2907. }
  2908. } elseif ( post_password_required( $comment_post_ID ) ) {
  2909. /**
  2910. * Fires when a comment is attempted on a password-protected post.
  2911. *
  2912. * @since 2.9.0
  2913. *
  2914. * @param int $comment_post_ID Post ID.
  2915. */
  2916. do_action( 'comment_on_password_protected', $comment_post_ID );
  2917. return new WP_Error( 'comment_on_password_protected' );
  2918. } else {
  2919. /**
  2920. * Fires before a comment is posted.
  2921. *
  2922. * @since 2.8.0
  2923. *
  2924. * @param int $comment_post_ID Post ID.
  2925. */
  2926. do_action( 'pre_comment_on_post', $comment_post_ID );
  2927. }
  2928. // If the user is logged in
  2929. $user = wp_get_current_user();
  2930. if ( $user->exists() ) {
  2931. if ( empty( $user->display_name ) ) {
  2932. $user->display_name = $user->user_login;
  2933. }
  2934. $comment_author = $user->display_name;
  2935. $comment_author_email = $user->user_email;
  2936. $comment_author_url = $user->user_url;
  2937. $user_ID = $user->ID;
  2938. if ( current_user_can( 'unfiltered_html' ) ) {
  2939. if ( ! isset( $comment_data['_wp_unfiltered_html_comment'] )
  2940. || ! wp_verify_nonce( $comment_data['_wp_unfiltered_html_comment'], 'unfiltered-html-comment_' . $comment_post_ID )
  2941. ) {
  2942. kses_remove_filters(); // start with a clean slate
  2943. kses_init_filters(); // set up the filters
  2944. remove_filter( 'pre_comment_content', 'wp_filter_post_kses' );
  2945. add_filter( 'pre_comment_content', 'wp_filter_kses' );
  2946. }
  2947. }
  2948. } else {
  2949. if ( get_option( 'comment_registration' ) ) {
  2950. return new WP_Error( 'not_logged_in', __( 'Sorry, you must be logged in to comment.' ), 403 );
  2951. }
  2952. }
  2953. $comment_type = '';
  2954. if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
  2955. if ( '' == $comment_author_email || '' == $comment_author ) {
  2956. return new WP_Error( 'require_name_email', __( '<strong>ERROR</strong>: please fill the required fields (name, email).' ), 200 );
  2957. } elseif ( ! is_email( $comment_author_email ) ) {
  2958. return new WP_Error( 'require_valid_email', __( '<strong>ERROR</strong>: please enter a valid email address.' ), 200 );
  2959. }
  2960. }
  2961. $commentdata = compact(
  2962. 'comment_post_ID',
  2963. 'comment_author',
  2964. 'comment_author_email',
  2965. 'comment_author_url',
  2966. 'comment_content',
  2967. 'comment_type',
  2968. 'comment_parent',
  2969. 'user_ID'
  2970. );
  2971. /**
  2972. * Filters whether an empty comment should be allowed.
  2973. *
  2974. * @since 5.1.0
  2975. *
  2976. * @param bool $allow_empty_comment Whether to allow empty comments. Default false.
  2977. * @param array $commentdata Array of comment data to be sent to wp_insert_comment().
  2978. */
  2979. $allow_empty_comment = apply_filters( 'allow_empty_comment', false, $commentdata );
  2980. if ( '' === $comment_content && ! $allow_empty_comment ) {
  2981. return new WP_Error( 'require_valid_comment', __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
  2982. }
  2983. $check_max_lengths = wp_check_comment_data_max_lengths( $commentdata );
  2984. if ( is_wp_error( $check_max_lengths ) ) {
  2985. return $check_max_lengths;
  2986. }
  2987. $comment_id = wp_new_comment( wp_slash( $commentdata ), true );
  2988. if ( is_wp_error( $comment_id ) ) {
  2989. return $comment_id;
  2990. }
  2991. if ( ! $comment_id ) {
  2992. return new WP_Error( 'comment_save_error', __( '<strong>ERROR</strong>: The comment could not be saved. Please try again later.' ), 500 );
  2993. }
  2994. return get_comment( $comment_id );
  2995. }
  2996. /**
  2997. * Registers the personal data exporter for comments.
  2998. *
  2999. * @since 4.9.6
  3000. *
  3001. * @param array $exporters An array of personal data exporters.
  3002. * @return array $exporters An array of personal data exporters.
  3003. */
  3004. function wp_register_comment_personal_data_exporter( $exporters ) {
  3005. $exporters['wordpress-comments'] = array(
  3006. 'exporter_friendly_name' => __( 'WordPress Comments' ),
  3007. 'callback' => 'wp_comments_personal_data_exporter',
  3008. );
  3009. return $exporters;
  3010. }
  3011. /**
  3012. * Finds and exports personal data associated with an email address from the comments table.
  3013. *
  3014. * @since 4.9.6
  3015. *
  3016. * @param string $email_address The comment author email address.
  3017. * @param int $page Comment page.
  3018. * @return array $return An array of personal data.
  3019. */
  3020. function wp_comments_personal_data_exporter( $email_address, $page = 1 ) {
  3021. // Limit us to 500 comments at a time to avoid timing out.
  3022. $number = 500;
  3023. $page = (int) $page;
  3024. $data_to_export = array();
  3025. $comments = get_comments(
  3026. array(
  3027. 'author_email' => $email_address,
  3028. 'number' => $number,
  3029. 'paged' => $page,
  3030. 'order_by' => 'comment_ID',
  3031. 'order' => 'ASC',
  3032. 'update_comment_meta_cache' => false,
  3033. )
  3034. );
  3035. $comment_prop_to_export = array(
  3036. 'comment_author' => __( 'Comment Author' ),
  3037. 'comment_author_email' => __( 'Comment Author Email' ),
  3038. 'comment_author_url' => __( 'Comment Author URL' ),
  3039. 'comment_author_IP' => __( 'Comment Author IP' ),
  3040. 'comment_agent' => __( 'Comment Author User Agent' ),
  3041. 'comment_date' => __( 'Comment Date' ),
  3042. 'comment_content' => __( 'Comment Content' ),
  3043. 'comment_link' => __( 'Comment URL' ),
  3044. );
  3045. foreach ( (array) $comments as $comment ) {
  3046. $comment_data_to_export = array();
  3047. foreach ( $comment_prop_to_export as $key => $name ) {
  3048. $value = '';
  3049. switch ( $key ) {
  3050. case 'comment_author':
  3051. case 'comment_author_email':
  3052. case 'comment_author_url':
  3053. case 'comment_author_IP':
  3054. case 'comment_agent':
  3055. case 'comment_date':
  3056. $value = $comment->{$key};
  3057. break;
  3058. case 'comment_content':
  3059. $value = get_comment_text( $comment->comment_ID );
  3060. break;
  3061. case 'comment_link':
  3062. $value = get_comment_link( $comment->comment_ID );
  3063. $value = sprintf(
  3064. '<a href="%s" target="_blank" rel="noreferrer noopener">%s</a>',
  3065. esc_url( $value ),
  3066. esc_html( $value )
  3067. );
  3068. break;
  3069. }
  3070. if ( ! empty( $value ) ) {
  3071. $comment_data_to_export[] = array(
  3072. 'name' => $name,
  3073. 'value' => $value,
  3074. );
  3075. }
  3076. }
  3077. $data_to_export[] = array(
  3078. 'group_id' => 'comments',
  3079. 'group_label' => __( 'Comments' ),
  3080. 'group_description' => __( 'User&#8217;s comment data.' ),
  3081. 'item_id' => "comment-{$comment->comment_ID}",
  3082. 'data' => $comment_data_to_export,
  3083. );
  3084. }
  3085. $done = count( $comments ) < $number;
  3086. return array(
  3087. 'data' => $data_to_export,
  3088. 'done' => $done,
  3089. );
  3090. }
  3091. /**
  3092. * Registers the personal data eraser for comments.
  3093. *
  3094. * @since 4.9.6
  3095. *
  3096. * @param array $erasers An array of personal data erasers.
  3097. * @return array $erasers An array of personal data erasers.
  3098. */
  3099. function wp_register_comment_personal_data_eraser( $erasers ) {
  3100. $erasers['wordpress-comments'] = array(
  3101. 'eraser_friendly_name' => __( 'WordPress Comments' ),
  3102. 'callback' => 'wp_comments_personal_data_eraser',
  3103. );
  3104. return $erasers;
  3105. }
  3106. /**
  3107. * Erases personal data associated with an email address from the comments table.
  3108. *
  3109. * @since 4.9.6
  3110. *
  3111. * @param string $email_address The comment author email address.
  3112. * @param int $page Comment page.
  3113. * @return array
  3114. */
  3115. function wp_comments_personal_data_eraser( $email_address, $page = 1 ) {
  3116. global $wpdb;
  3117. if ( empty( $email_address ) ) {
  3118. return array(
  3119. 'items_removed' => false,
  3120. 'items_retained' => false,
  3121. 'messages' => array(),
  3122. 'done' => true,
  3123. );
  3124. }
  3125. // Limit us to 500 comments at a time to avoid timing out.
  3126. $number = 500;
  3127. $page = (int) $page;
  3128. $items_removed = false;
  3129. $items_retained = false;
  3130. $comments = get_comments(
  3131. array(
  3132. 'author_email' => $email_address,
  3133. 'number' => $number,
  3134. 'paged' => $page,
  3135. 'order_by' => 'comment_ID',
  3136. 'order' => 'ASC',
  3137. 'include_unapproved' => true,
  3138. )
  3139. );
  3140. /* translators: Name of a comment's author after being anonymized. */
  3141. $anon_author = __( 'Anonymous' );
  3142. $messages = array();
  3143. foreach ( (array) $comments as $comment ) {
  3144. $anonymized_comment = array();
  3145. $anonymized_comment['comment_agent'] = '';
  3146. $anonymized_comment['comment_author'] = $anon_author;
  3147. $anonymized_comment['comment_author_email'] = '';
  3148. $anonymized_comment['comment_author_IP'] = wp_privacy_anonymize_data( 'ip', $comment->comment_author_IP );
  3149. $anonymized_comment['comment_author_url'] = '';
  3150. $anonymized_comment['user_id'] = 0;
  3151. $comment_id = (int) $comment->comment_ID;
  3152. /**
  3153. * Filters whether to anonymize the comment.
  3154. *
  3155. * @since 4.9.6
  3156. *
  3157. * @param bool|string Whether to apply the comment anonymization (bool).
  3158. * Custom prevention message (string). Default true.
  3159. * @param WP_Comment $comment WP_Comment object.
  3160. * @param array $anonymized_comment Anonymized comment data.
  3161. */
  3162. $anon_message = apply_filters( 'wp_anonymize_comment', true, $comment, $anonymized_comment );
  3163. if ( true !== $anon_message ) {
  3164. if ( $anon_message && is_string( $anon_message ) ) {
  3165. $messages[] = esc_html( $anon_message );
  3166. } else {
  3167. /* translators: %d: Comment ID. */
  3168. $messages[] = sprintf( __( 'Comment %d contains personal data but could not be anonymized.' ), $comment_id );
  3169. }
  3170. $items_retained = true;
  3171. continue;
  3172. }
  3173. $args = array(
  3174. 'comment_ID' => $comment_id,
  3175. );
  3176. $updated = $wpdb->update( $wpdb->comments, $anonymized_comment, $args );
  3177. if ( $updated ) {
  3178. $items_removed = true;
  3179. clean_comment_cache( $comment_id );
  3180. } else {
  3181. $items_retained = true;
  3182. }
  3183. }
  3184. $done = count( $comments ) < $number;
  3185. return array(
  3186. 'items_removed' => $items_removed,
  3187. 'items_retained' => $items_retained,
  3188. 'messages' => $messages,
  3189. 'done' => $done,
  3190. );
  3191. }
  3192. /**
  3193. * Sets the last changed time for the 'comment' cache group.
  3194. *
  3195. * @since 5.0.0
  3196. */
  3197. function wp_cache_set_comments_last_changed() {
  3198. wp_cache_set( 'last_changed', microtime(), 'comment' );
  3199. }