Drupal 7 - Supprimer tous les commentaires non validés

<?php
/*Delete all capproval comments*/

/**
 * Implements hook_menu().
 */

function latrach_custom_menu() { 
   
$items['delete-all-approval-comments'] = array(
       
'title' => 'delete-all-approval-comments',
       
'description' =>  'delete-all-approval-comments',
       
'type' => MENU_CALLBACK,
       
'page callback' => 'latrach_custom_delete_comments',
       
'access callback' => 'user_access',
       
'access arguments' => array('administer comments'),
    );

    return

$items;
}

function

latrach_custom_delete_comments() {
 
$cids = db_query('SELECT cid FROM {comment} WHERE status = :status', array(
   
':status' => 0,
  ))->
fetchCol();
 
//dpm($cids);
 
$comments = comment_load_multiple($cids);
  if (
$comments) {
   
$transaction = db_transaction();
    try {
     
// Delete the comments.
     
db_delete('comment')
        ->
condition('cid', array_keys($comments), 'IN')
        ->
execute();
      foreach (
$comments as $comment) {
       
field_attach_delete('comment', $comment);
       
module_invoke_all('comment_delete', $comment);
       
module_invoke_all('entity_delete', $comment, 'comment');

       

// Delete the comment's replies.
       
$child_cids = db_query('SELECT cid FROM {comment} WHERE pid = :cid', array(':cid' => $comment->cid))->fetchCol();
       
comment_delete_multiple($child_cids);
       
_comment_update_node_statistics($comment->nid);
      }
    }
    catch (
Exception $e) {
     
$transaction->rollback();
     
watchdog_exception('comment', $e);
      throw
$e;
    }
  }
}
?>

Tags: 

Catégories: 

Share/Save