| 1 |
<?php |
| 2 |
/************************************************************* |
| 3 |
* |
| 4 |
* comment.class.php |
| 5 |
* |
| 6 |
* Get comments |
| 7 |
* |
| 8 |
* |
| 9 |
* Copyright (c) 2011 Prelovac Media |
| 10 |
* www.prelovac.com |
| 11 |
**************************************************************/ |
| 12 |
if(basename($_SERVER['SCRIPT_FILENAME']) == "comment.class.php"): |
| 13 |
exit; |
| 14 |
endif; |
| 15 |
class MMB_Comment extends MMB_Core |
| 16 |
{ |
| 17 |
function __construct() |
| 18 |
{ |
| 19 |
parent::__construct(); |
| 20 |
} |
| 21 |
|
| 22 |
function change_status($args) |
| 23 |
{ |
| 24 |
|
| 25 |
global $wpdb; |
| 26 |
$comment_id = $args['comment_id']; |
| 27 |
$status = $args['status']; |
| 28 |
|
| 29 |
if ( 'approve' == $status ) |
| 30 |
$status_sql = '1'; |
| 31 |
elseif ( 'unapprove' == $status ) |
| 32 |
$status_sql = '0'; |
| 33 |
elseif ( 'spam' == $status ) |
| 34 |
$status_sql = 'spam'; |
| 35 |
elseif ( 'trash' == $status ) |
| 36 |
$status_sql = 'trash'; |
| 37 |
$sql = "update ".$wpdb->prefix."comments set comment_approved = '%s' where comment_ID = '%s'"; |
| 38 |
$success = $wpdb->query($wpdb->prepare($sql, array($status_sql, $comment_id))); |
| 39 |
|
| 40 |
|
| 41 |
return $success; |
| 42 |
} |
| 43 |
|
| 44 |
function get_comments($args){ |
| 45 |
global $wpdb; |
| 46 |
|
| 47 |
$where=''; |
| 48 |
|
| 49 |
extract($args); |
| 50 |
|
| 51 |
if(!empty($filter_comments)) |
| 52 |
{ |
| 53 |
$where.=" AND (c.comment_author LIKE '%".mysql_real_escape_string($filter_comments)."%' OR c.comment_content LIKE '%".mysql_real_escape_string($filter_comments)."%')"; |
| 54 |
} |
| 55 |
$comment_array = array(); |
| 56 |
$comment_statuses = array('approved', 'pending', 'spam', 'trash'); |
| 57 |
foreach ($args as $checkbox => $checkbox_val) |
| 58 |
{ |
| 59 |
if($checkbox_val=="on") { |
| 60 |
$status_val = str_replace("mwp_get_comments_","",$checkbox); |
| 61 |
if($status_val == 'approved'){ |
| 62 |
$status_val = 1; |
| 63 |
}elseif($status_val == 'pending'){ |
| 64 |
$status_val = 0; |
| 65 |
} |
| 66 |
$comment_array[]="'".$status_val."'"; |
| 67 |
} |
| 68 |
} |
| 69 |
if(!empty($comment_array)) |
| 70 |
{ |
| 71 |
$where.=" AND c.comment_approved IN (".implode(",",$comment_array).")"; |
| 72 |
} |
| 73 |
|
| 74 |
$sql_query = "$wpdb->comments as c, $wpdb->posts as p WHERE c.comment_post_ID = p.ID ".$where; |
| 75 |
|
| 76 |
$comments_total = $wpdb->get_results("SELECT count(*) as total_comments FROM ".$sql_query); |
| 77 |
$total=$comments_total[0]->total_comments; |
| 78 |
$comments_approved = $this->comment_total(); |
| 79 |
|
| 80 |
$query_comments = $wpdb->get_results("SELECT c.comment_ID, c.comment_post_ID, c.comment_author, c.comment_author_email, c.comment_author_url, c.comment_author_IP, c.comment_date, c.comment_content, c.comment_approved, c.comment_parent, p.post_title, p.post_type, p.guid FROM ".$sql_query." ORDER BY c.comment_date DESC LIMIT 500"); |
| 81 |
$comments = array(); |
| 82 |
foreach ( $query_comments as $comments_info ) |
| 83 |
{ |
| 84 |
$comment_total_approved = 0; |
| 85 |
if(isset($comments_approved[$comments_info->comment_post_ID]['approved'])){ |
| 86 |
$comment_total_approved = $comments_approved[$comments_info->comment_post_ID]['approved']; |
| 87 |
} |
| 88 |
$comment_total_pending = 0; |
| 89 |
if(isset($comments_approved[$comments_info->comment_post_ID]['pending'])){ |
| 90 |
$comment_total_pending = $comments_approved[$comments_info->comment_post_ID]['pending']; |
| 91 |
} |
| 92 |
$comment_parent_author = ''; |
| 93 |
if($comments_info->comment_parent > 0){ |
| 94 |
$select_parent_author = "SELECT comment_author FROM $wpdb->comments WHERE comment_ID = ".$comments_info->comment_parent; |
| 95 |
$select_parent_author_res = $wpdb->get_row($select_parent_author); |
| 96 |
$comment_parent_author = $select_parent_author_res->comment_author; |
| 97 |
} |
| 98 |
|
| 99 |
$comments[$comments_info->comment_ID] = array( |
| 100 |
"comment_post_ID" => $comments_info->comment_post_ID, |
| 101 |
"comment_author" => $comments_info->comment_author, |
| 102 |
"comment_author_email" => $comments_info->comment_author_email, |
| 103 |
"comment_author_url" => $comments_info->comment_author_url, |
| 104 |
"comment_author_IP" => $comments_info->comment_author_IP, |
| 105 |
"comment_date" => $comments_info->comment_date, |
| 106 |
"comment_content" => $comments_info->comment_content, |
| 107 |
"comment_approved" => $comments_info->comment_approved, |
| 108 |
"comment_parent" => $comments_info->comment_parent, |
| 109 |
"comment_parent_author" => $comment_parent_author, |
| 110 |
"post_title" => $comments_info->post_title, |
| 111 |
"post_type" => $comments_info->post_type, |
| 112 |
"guid" => $comments_info->guid, |
| 113 |
"comment_total_approved" => $comment_total_approved, |
| 114 |
"comment_total_pending" => $comment_total_pending, |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
return array('comments' => $comments, 'total' => $total); |
| 119 |
} |
| 120 |
|
| 121 |
function comment_total(){ |
| 122 |
global $wpdb; |
| 123 |
$totals = array(); |
| 124 |
$select_total_approved = "SELECT COUNT(*) as total, p.ID FROM $wpdb->comments as c, $wpdb->posts as p WHERE c.comment_post_ID = p.ID AND c.comment_approved = 1 GROUP BY p.ID"; |
| 125 |
$select_total_approved_res = $wpdb->get_results($select_total_approved); |
| 126 |
|
| 127 |
if(!empty($select_total_approved_res)){ |
| 128 |
foreach($select_total_approved_res as $row){ |
| 129 |
$totals[$row->ID]['approved'] = $row->total; |
| 130 |
} |
| 131 |
} |
| 132 |
$select_total_pending = "SELECT COUNT(*) as total, p.ID FROM $wpdb->comments as c, $wpdb->posts as p WHERE c.comment_post_ID = p.ID AND c.comment_approved = 0 GROUP BY p.ID"; |
| 133 |
$select_total_pending_res = $wpdb->get_results($select_total_pending); |
| 134 |
if(!empty($select_total_pending_res)){ |
| 135 |
foreach($select_total_pending_res as $row){ |
| 136 |
$totals[$row->ID]['pending'] = $row->total; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return $totals; |
| 141 |
} |
| 142 |
|
| 143 |
function action_comment($args){ |
| 144 |
global $wpdb; |
| 145 |
extract($args); |
| 146 |
if($docomaction == 'approve'){ |
| 147 |
$docomaction = '1'; |
| 148 |
}else if($docomaction == 'unapprove' || $docomaction == 'untrash' || $docomaction == 'unspam'){ |
| 149 |
$docomaction = '0'; |
| 150 |
} |
| 151 |
|
| 152 |
if(!empty($comment_id)){ |
| 153 |
if($docomaction == 'delete'){ |
| 154 |
$update_query = "DELETE FROM $wpdb->comments WHERE comment_ID = ".$comment_id; |
| 155 |
$delete_query = "DELETE FROM $wpdb->commentmeta WHERE comment_id = ".$comment_id; |
| 156 |
$wpdb->query($delete_query); |
| 157 |
}else{ |
| 158 |
$update_query = "UPDATE $wpdb->comments SET comment_approved = '".$docomaction."' WHERE comment_ID = ".$comment_id; |
| 159 |
} |
| 160 |
$wpdb->query($update_query); |
| 161 |
|
| 162 |
return 'Comment updated.'; |
| 163 |
}else{ |
| 164 |
return 'No ID...'; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
function bulk_action_comments($args){ |
| 169 |
global $wpdb; |
| 170 |
extract($args); |
| 171 |
|
| 172 |
if($docomaction=='delete'){ |
| 173 |
$update_query_intro = "DELETE FROM $wpdb->comments WHERE comment_ID = "; |
| 174 |
}else{ |
| 175 |
if($docomaction=='unapprove' || $docomaction == 'untrash' || $docomaction == 'unspam'){ |
| 176 |
$docomaction = '0'; |
| 177 |
}else if($docomaction == 'approve'){ |
| 178 |
$docomaction = '1'; |
| 179 |
} |
| 180 |
$update_query_intro = "UPDATE $wpdb->comments SET comment_approved = '".$docomaction."' WHERE comment_ID = "; |
| 181 |
} |
| 182 |
foreach($args as $key=>$val){ |
| 183 |
|
| 184 |
if(!empty($val) && is_numeric($val)) |
| 185 |
{ |
| 186 |
if($docomaction=='delete'){ |
| 187 |
$delete_query = "DELETE FROM $wpdb->commentmeta WHERE comment_id = ".$val; |
| 188 |
$wpdb->query($delete_query); |
| 189 |
} |
| 190 |
$update_query = $update_query_intro.$val; |
| 191 |
|
| 192 |
$wpdb->query($update_query); |
| 193 |
} |
| 194 |
} |
| 195 |
return "comments updated"; |
| 196 |
} |
| 197 |
|
| 198 |
function reply_comment($args){ |
| 199 |
global $wpdb, $current_user; |
| 200 |
extract($args); |
| 201 |
$comments = array(); |
| 202 |
|
| 203 |
if(!empty($comment_id) && !empty($reply_text)){ |
| 204 |
$admins = get_userdata($current_user->ID); |
| 205 |
$now = time(); |
| 206 |
$insert_reply = "INSERT INTO $wpdb->comments(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_parent, user_id) VALUES(".$post_id.", '".$admins->user_login."', '".$admins->user_email."', '".$admins->user_url."', '".$_SERVER['REMOTE_ADDR']."', NOW(), NOW(), '%s', 0, 1, '".$_SERVER['HTTP_USER_AGENT']."', ".$comment_id.", ".$current_user->ID.")"; |
| 207 |
$insert_reply_res = $wpdb->query($wpdb->prepare($insert_reply, base64_decode($reply_text))); |
| 208 |
$lastid = $wpdb->insert_id; |
| 209 |
|
| 210 |
$comments_approved = $this->comment_total(); |
| 211 |
|
| 212 |
$comment_total_approved = 0; |
| 213 |
if(isset($comments_approved[$post_id]['approved'])){ |
| 214 |
$comment_total_approved = $comments_approved[$post_id]['approved']; |
| 215 |
} |
| 216 |
$comment_total_pending = 0; |
| 217 |
if(isset($comments_approved[$post_id]['pending'])){ |
| 218 |
$comment_total_pending = $comments_approved[$post_id]['pending']; |
| 219 |
} |
| 220 |
|
| 221 |
$comment_parent_author = ''; |
| 222 |
if($comment_id > 0){ |
| 223 |
$select_parent_author = "SELECT c.comment_author, p.post_title, p.post_type, p.guid FROM $wpdb->comments as c, $wpdb->posts as p WHERE c.comment_post_ID = p.ID AND c.comment_ID = ".$comment_id; |
| 224 |
$select_parent_author_res = $wpdb->get_row($select_parent_author); |
| 225 |
$comment_parent_author = $select_parent_author_res->comment_author; |
| 226 |
} |
| 227 |
|
| 228 |
$comments[$lastid] = array( |
| 229 |
"comment_post_ID" => $post_id, |
| 230 |
"comment_author" => $admins->user_login, |
| 231 |
"comment_author_email" => $admins->user_email, |
| 232 |
"comment_author_url" => $admins->user_url, |
| 233 |
"comment_author_IP" => $_SERVER['REMOTE_ADDR'], |
| 234 |
"comment_date" => $now, |
| 235 |
"comment_content" => $reply_text, |
| 236 |
"comment_approved" => '1', |
| 237 |
"comment_parent" => $comment_id, |
| 238 |
"comment_parent_author" => $comment_parent_author, |
| 239 |
"post_title" => $select_parent_author_res->post_title, |
| 240 |
"post_type" => $select_parent_author_res->post_type, |
| 241 |
"guid" => $select_parent_author_res->guid, |
| 242 |
"comment_total_approved" => $comment_total_approved, |
| 243 |
"comment_total_pending" => $comment_total_pending, |
| 244 |
); |
| 245 |
|
| 246 |
|
| 247 |
} |
| 248 |
return $comments; |
| 249 |
} |
| 250 |
|
| 251 |
} |
| 252 |
?> |