| | 2024 | |
| | 2025 | |
| | 2026 | /** |
| | 2027 | * Prints the html for the comment meta box |
| | 2028 | * |
| | 2029 | * @uses get_comment_history() to get comment meta |
| | 2030 | * @param Object $comment WP comment object |
| | 2031 | * @return void |
| | 2032 | */ |
| | 2033 | |
| | 2034 | // |
| | 2035 | // Comment History |
| | 2036 | // |
| | 2037 | |
| | 2038 | /** |
| | 2039 | * Add a history entry to a comment with a messaged tailored to the event being recorded. |
| | 2040 | * |
| | 2041 | * @global object $current_user the current logged in user, if any |
| | 2042 | * @param int $comment_id the id of the comment to add a history entry |
| | 2043 | * @param string $status optional comment status string |
| | 2044 | * @return type |
| | 2045 | */ |
| | 2046 | function _wp_update_comment_history( $comment_id, $status = '' ) { |
| | 2047 | |
| | 2048 | global $current_user; |
| | 2049 | |
| | 2050 | // some hooks pass in the comment object, not just the id |
| | 2051 | // in those cases we get the id from the object |
| | 2052 | if( is_object( $comment_id ) ) |
| | 2053 | $comment_id = $comment_id->comment_ID; |
| | 2054 | |
| | 2055 | $action = current_filter(); // what action happened that we will log as an event in the comment's history? |
| | 2056 | $current_user_login = 'anonymous user'; // what is the name of this user for the comment's history? 'anonymous user' by default. |
| | 2057 | $message = ''; // what message should we log in the comment's history? |
| | 2058 | |
| | 2059 | // no action or no comment id then we can't log history and must stop |
| | 2060 | if( ! $action || ! $comment_id ) |
| | 2061 | return; |
| | 2062 | |
| | 2063 | // get the current logged in user's login name for the history message |
| | 2064 | if ( is_object($current_user) && isset($current_user->user_login) ) |
| | 2065 | $current_user_login = $current_user->user_login; |
| | 2066 | |
| | 2067 | // create a custom message for the comment history based on the action ( the hook ) |
| | 2068 | if( 'edit_comment' === $action ) |
| | 2069 | $message = sprintf( __( "edited by %s" ), $current_user_login ); |
| | 2070 | elseif( 'trashed_comment' === $action ) |
| | 2071 | $message = sprintf( __( "trashed by %s" ), $current_user_login ); |
| | 2072 | elseif( 'untrashed_comment' === $action ) |
| | 2073 | $message = sprintf( __( "untrashed by %s" ), $current_user_login ); |
| | 2074 | elseif( 'comment_unapproved_to_approved' === $action ) |
| | 2075 | $message = sprintf( __( "%s approved this comment" ), $current_user_login ); |
| | 2076 | elseif( 'comment_approved_to_unapproved' === $action ) |
| | 2077 | $message = sprintf( __( "%s unapproved this comment" ), $current_user_login ); |
| | 2078 | elseif( 'spammed_comment' === $action ) |
| | 2079 | $message = sprintf( __( "%s marked this comment as spam" ), $current_user_login ); |
| | 2080 | elseif( 'unspammed_comment' === $action ) |
| | 2081 | $message = sprintf( __( "%s marked this comment as ham" ), $current_user_login ); |
| | 2082 | else |
| | 2083 | $message = sprintf( __( "%s by %s" ), str_replace( '_', ' ', $action ), $current_user_login ); // default message is based on the hook name |
| | 2084 | |
| | 2085 | // insert the history message into comment meta and return result |
| | 2086 | |
| | 2087 | $r = false; |
| | 2088 | |
| | 2089 | if( $message ) { |
| | 2090 | $entry = array( |
| | 2091 | 'time' => time(), |
| | 2092 | 'message' => $message, |
| | 2093 | 'event' => $action, |
| | 2094 | 'user' => $current_user_login, |
| | 2095 | ); |
| | 2096 | |
| | 2097 | $r = add_comment_meta( $comment_id, '_wp_history', $entry, false ); |
| | 2098 | } |
| | 2099 | |
| | 2100 | return $r; |
| | 2101 | } |
| | 2102 | |
| | 2103 | /** |
| | 2104 | * Comment history events: |
| | 2105 | * Use comment-related hooks call to _wp_update_comment_history in order to log the comment event. |
| | 2106 | * Add additional actions here, along with corresponding support in _wp_update_comment_history to log in the comment history |
| | 2107 | */ |
| | 2108 | add_action( 'edit_comment', '_wp_update_comment_history', 10 ); |
| | 2109 | add_action( 'trashed_comment', '_wp_update_comment_history', 10 ); |
| | 2110 | add_action( 'untrashed_comment', '_wp_update_comment_history', 10 ); |
| | 2111 | add_action( 'comment_unapproved_to_approved', '_wp_update_comment_history', 10 ); |
| | 2112 | add_action( 'comment_approved_to_unapproved', '_wp_update_comment_history', 10 ); |
| | 2113 | add_action( 'spammed_comment', '_wp_update_comment_history', 10 ); |
| | 2114 | add_action( 'unspammed_comment', '_wp_update_comment_history', 10 ); |
| | 2115 | |
| | 2116 | |
| | 2117 | /** |
| | 2118 | * Get the comment history for a given comment id, as an array. |
| | 2119 | * Allows filtering of the history array for custom reordering or editing of history |
| | 2120 | * |
| | 2121 | * @param int $comment_id Comment ID |
| | 2122 | * @return array Associative array of a comment's history. Keys are time, message, event, and user. |
| | 2123 | */ |
| | 2124 | function get_comment_history( $comment_id ) { |
| | 2125 | $history = get_comment_meta( $comment_id, '_wp_history', false ); |
| | 2126 | return apply_filters('get_comment_history', $history, $comment_id ); |
| | 2127 | } |
| | 2128 | |
| | 2129 | |
| | 2130 | /** |
| | 2131 | * Sort the comment history in reverse chronological order. |
| | 2132 | * |
| | 2133 | * @uses _sort_comment_history_reverse_chronological() to sort the history entries in reverse chronological order |
| | 2134 | * @param Array $history array of history entries |
| | 2135 | * @return Array sorted array of history events |
| | 2136 | */ |
| | 2137 | function sort_comment_history( $history ){ |
| | 2138 | usort( $history, '_sort_comment_history_reverse_chronological' ); |
| | 2139 | return $history; |
| | 2140 | } |
| | 2141 | add_filter( 'get_comment_history', 'sort_comment_history', 1, 1 ); |
| | 2142 | |
| | 2143 | /** |
| | 2144 | * Used with usort() to sort comment history entries in reverse chronological order |
| | 2145 | * |
| | 2146 | * @access private |
| | 2147 | * |
| | 2148 | * @param array $a History entry for a comment |
| | 2149 | * @param array $b Another history entry for the same comment |
| | 2150 | * @return int -1 if history entry a is older than history entry b, 1 otherwise |
| | 2151 | */ |
| | 2152 | function _sort_comment_history_reverse_chronological( $a, $b ) { |
| | 2153 | return $a['time'] > $b['time'] ? -1 : 1; |
| | 2154 | } |
| | 2155 | |
| | 2156 | |
| | 2157 | /** |
| | 2158 | * Adds the comment history meta box to comment pages in the admin interface |
| | 2159 | * @return void |
| | 2160 | */ |
| | 2161 | function add_comment_history_meta_box(){ |
| | 2162 | add_meta_box( 'comment-history', __('Comment History'), 'comment_history_meta_box_html', 'comment', 'normal' ); |
| | 2163 | } |
| | 2164 | |
| | 2165 | function comment_history_meta_box_html( $comment ) { |
| | 2166 | $history = get_comment_history( $comment->comment_ID ); |
| | 2167 | |
| | 2168 | echo '<div class="comment-history" style="margin: 13px;">'; |
| | 2169 | |
| | 2170 | if ( $history ) { |
| | 2171 | |
| | 2172 | foreach ( $history as $row ) { |
| | 2173 | $time = date( 'D d M Y @ h:i:m a', (int) $row['time'] ) . ' GMT'; |
| | 2174 | $human_time = sprintf( __('%s ago'), human_time_diff( $row['time'] ) ); |
| | 2175 | $message = esc_html( $row['message'] ); |
| | 2176 | |
| | 2177 | printf( '<div style="margin-bottom: 13px;"><span style="color: #999;" alt="%1$s" title="%1$s">%2$s</span> - %3$s</div>', |
| | 2178 | $time, |
| | 2179 | $human_time, |
| | 2180 | $message |
| | 2181 | ); |
| | 2182 | } |
| | 2183 | |
| | 2184 | } else { |
| | 2185 | |
| | 2186 | echo '<em>'. __('There is no history for this comment.') . '</em>'; |
| | 2187 | |
| | 2188 | } |
| | 2189 | |
| | 2190 | echo '</div> <!-- .comment-history -->'; |
| | 2191 | |
| | 2192 | } |
| | 2193 | add_action( 'admin_init', 'add_comment_history_meta_box' ); |