| 1 |
<?php
|
| 2 |
// $Id: comment.tokens.inc,v 1.5 2009/10/11 03:07:18 webchick Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Builds placeholder replacement tokens for comment-related data.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implement hook_token_info().
|
| 11 |
*/
|
| 12 |
function comment_token_info() {
|
| 13 |
$type = array(
|
| 14 |
'name' => t('Comments'),
|
| 15 |
'description' => t('Tokens for comments posted on the site.'),
|
| 16 |
'needs-data' => 'comment',
|
| 17 |
);
|
| 18 |
|
| 19 |
// Comment-related tokens for nodes
|
| 20 |
$node['comment-count'] = array(
|
| 21 |
'name' => t("Comment count"),
|
| 22 |
'description' => t("The number of comments posted on a node."),
|
| 23 |
);
|
| 24 |
$node['comment-count-new'] = array(
|
| 25 |
'name' => t("New comment count"),
|
| 26 |
'description' => t("The number of comments posted on a node since the reader last viewed it."),
|
| 27 |
);
|
| 28 |
|
| 29 |
// Core comment tokens
|
| 30 |
$comment['cid'] = array(
|
| 31 |
'name' => t("Comment ID"),
|
| 32 |
'description' => t("The unique ID of the comment."),
|
| 33 |
);
|
| 34 |
$comment['pid'] = array(
|
| 35 |
'name' => t("Parent ID"),
|
| 36 |
'description' => t("The unique ID of the comment's parent, if comment threading is active."),
|
| 37 |
);
|
| 38 |
$comment['nid'] = array(
|
| 39 |
'name' => t("Node ID"),
|
| 40 |
'description' => t("The unique ID of the node the comment was posted to."),
|
| 41 |
);
|
| 42 |
$comment['uid'] = array(
|
| 43 |
'name' => t("User ID"),
|
| 44 |
'description' => t("The unique ID of the user who posted the comment."),
|
| 45 |
);
|
| 46 |
$comment['hostname'] = array(
|
| 47 |
'name' => t("IP Address"),
|
| 48 |
'description' => t("The IP address of the computer the comment was posted from."),
|
| 49 |
);
|
| 50 |
$comment['name'] = array(
|
| 51 |
'name' => t("Name"),
|
| 52 |
'description' => t("The name left by the comment author."),
|
| 53 |
);
|
| 54 |
$comment['mail'] = array(
|
| 55 |
'name' => t("Email address"),
|
| 56 |
'description' => t("The email address left by the comment author."),
|
| 57 |
);
|
| 58 |
$comment['homepage'] = array(
|
| 59 |
'name' => t("Home page"),
|
| 60 |
'description' => t("The home page URL left by the comment author."),
|
| 61 |
);
|
| 62 |
$comment['title'] = array(
|
| 63 |
'name' => t("Title"),
|
| 64 |
'description' => t("The title of the comment."),
|
| 65 |
);
|
| 66 |
$comment['body'] = array(
|
| 67 |
'name' => t("Content"),
|
| 68 |
'description' => t("The formatted content of the comment itself."),
|
| 69 |
);
|
| 70 |
$comment['url'] = array(
|
| 71 |
'name' => t("URL"),
|
| 72 |
'description' => t("The URL of the comment."),
|
| 73 |
);
|
| 74 |
$comment['edit-url'] = array(
|
| 75 |
'name' => t("Edit URL"),
|
| 76 |
'description' => t("The URL of the comment's edit page."),
|
| 77 |
);
|
| 78 |
|
| 79 |
// Chained tokens for comments
|
| 80 |
$comment['created'] = array(
|
| 81 |
'name' => t("Date created"),
|
| 82 |
'description' => t("The date the comment was posted."),
|
| 83 |
'type' => 'date',
|
| 84 |
);
|
| 85 |
$comment['parent'] = array(
|
| 86 |
'name' => t("Parent"),
|
| 87 |
'description' => t("The comment's parent, if comment threading is active."),
|
| 88 |
'type' => 'comment',
|
| 89 |
);
|
| 90 |
$comment['node'] = array(
|
| 91 |
'name' => t("Node"),
|
| 92 |
'description' => t("The node the comment was posted to."),
|
| 93 |
'type' => 'node',
|
| 94 |
);
|
| 95 |
$comment['author'] = array(
|
| 96 |
'name' => t("Author"),
|
| 97 |
'description' => t("The author of the comment, if they were logged in."),
|
| 98 |
'type' => 'user',
|
| 99 |
);
|
| 100 |
|
| 101 |
return array(
|
| 102 |
'types' => array('comment' => $type),
|
| 103 |
'tokens' => array(
|
| 104 |
'node' => $node,
|
| 105 |
'comment' => $comment,
|
| 106 |
),
|
| 107 |
);
|
| 108 |
}
|
| 109 |
|
| 110 |
/**
|
| 111 |
* Implement hook_tokens().
|
| 112 |
*/
|
| 113 |
function comment_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
| 114 |
$url_options = array('absolute' => TRUE);
|
| 115 |
if (isset($options['language'])) {
|
| 116 |
$url_options['language'] = $options['language'];
|
| 117 |
$language_code = $options['language']->language;
|
| 118 |
}
|
| 119 |
else {
|
| 120 |
$language_code = NULL;
|
| 121 |
}
|
| 122 |
$sanitize = !empty($options['sanitize']);
|
| 123 |
|
| 124 |
$replacements = array();
|
| 125 |
|
| 126 |
if ($type == 'comment' && !empty($data['comment'])) {
|
| 127 |
$comment = $data['comment'];
|
| 128 |
|
| 129 |
foreach ($tokens as $name => $original) {
|
| 130 |
switch ($name) {
|
| 131 |
// Simple key values on the comment.
|
| 132 |
case 'cid':
|
| 133 |
$replacements[$original] = $comment->cid;
|
| 134 |
break;
|
| 135 |
|
| 136 |
case 'nid':
|
| 137 |
$replacements[$original] = $comment->nid;
|
| 138 |
break;
|
| 139 |
|
| 140 |
case 'uid':
|
| 141 |
$replacements[$original] = $comment->uid;
|
| 142 |
break;
|
| 143 |
|
| 144 |
case 'pid':
|
| 145 |
$replacements[$original] = $comment->pid;
|
| 146 |
break;
|
| 147 |
|
| 148 |
// Poster identity information for comments
|
| 149 |
case 'hostname':
|
| 150 |
$replacements[$original] = $sanitize ? check_plain($comment->hostname) : $comment->hostname;
|
| 151 |
break;
|
| 152 |
|
| 153 |
case 'name':
|
| 154 |
$name = ($comment->uid == 0) ? variable_get('anonymous', t('Anonymous')) : $comment->name;
|
| 155 |
$replacements[$original] = $sanitize ? filter_xss($name) : $name;
|
| 156 |
break;
|
| 157 |
|
| 158 |
case 'mail':
|
| 159 |
if ($comment->uid != 0) {
|
| 160 |
$account = user_load($comment->uid);
|
| 161 |
$mail = $account->mail;
|
| 162 |
}
|
| 163 |
else {
|
| 164 |
$mail = $comment->mail;
|
| 165 |
}
|
| 166 |
$replacements[$original] = $sanitize ? check_plain($mail) : $mail;
|
| 167 |
break;
|
| 168 |
|
| 169 |
case 'homepage':
|
| 170 |
$replacements[$original] = $sanitize ? filter_xss_bad_protocol($comment->homepage) : $comment->homepage;
|
| 171 |
break;
|
| 172 |
|
| 173 |
case 'title':
|
| 174 |
$replacements[$original] = $sanitize ? filter_xss($comment->subject) : $comment->subject;
|
| 175 |
break;
|
| 176 |
|
| 177 |
case 'body':
|
| 178 |
$replacements[$original] = $sanitize ? check_markup($comment->comment, $comment->format, '', TRUE) : $comment->comment;
|
| 179 |
break;
|
| 180 |
|
| 181 |
// Comment related URLs.
|
| 182 |
case 'url':
|
| 183 |
$replacements[$original] = url('comment/' . $comment->cid, array('absolute' => TRUE, 'fragment' => 'comment-' . $comment->cid));
|
| 184 |
break;
|
| 185 |
|
| 186 |
case 'edit-url':
|
| 187 |
$replacements[$original] = url('comment/' . $comment->cid . '/edit', array('absolute' => TRUE));
|
| 188 |
break;
|
| 189 |
|
| 190 |
// Default values for the chained tokens handled below.
|
| 191 |
case 'author':
|
| 192 |
$replacements[$original] = $sanitize ? filter_xss($comment->name) : $comment->name;
|
| 193 |
break;
|
| 194 |
|
| 195 |
case 'parent':
|
| 196 |
if (!empty($comment->pid)) {
|
| 197 |
$parent = comment_load($comment->pid);
|
| 198 |
$replacements[$original] = $sanitize ? filter_xss($parent->subject) : $parent->subject;
|
| 199 |
}
|
| 200 |
break;
|
| 201 |
|
| 202 |
case 'created':
|
| 203 |
$replacements[$original] = format_date($comment->created, 'medium', '', NULL, $language_code);
|
| 204 |
break;
|
| 205 |
|
| 206 |
case 'changed':
|
| 207 |
$replacements[$original] = format_date($comment->changed, 'medium', '', NULL, $language_code);
|
| 208 |
break;
|
| 209 |
|
| 210 |
case 'node':
|
| 211 |
$node = node_load($comment->nid);
|
| 212 |
$title = $node->title[FIELD_LANGUAGE_NONE][0]['value'];
|
| 213 |
$replacements[$original] = $sanitize ? filter_xss($title) : $title;
|
| 214 |
break;
|
| 215 |
}
|
| 216 |
}
|
| 217 |
|
| 218 |
// Chained token relationships.
|
| 219 |
if ($node_tokens = token_find_with_prefix($tokens, 'node')) {
|
| 220 |
$node = node_load($comment->nid);
|
| 221 |
$replacements += token_generate('node', $node_tokens, array('node' => $node), $options);
|
| 222 |
}
|
| 223 |
|
| 224 |
if ($date_tokens = token_find_with_prefix($tokens, 'created')) {
|
| 225 |
$replacements += token_generate('date', $date_tokens, array('date' => $comment->created), $options);
|
| 226 |
}
|
| 227 |
|
| 228 |
if (($parent_tokens = token_find_with_prefix($tokens, 'parent')) && $parent = comment_load($comment->pid)) {
|
| 229 |
$replacements += token_generate('comment', $parent_tokens, array('comment' => $parent), $options);
|
| 230 |
}
|
| 231 |
|
| 232 |
if (($author_tokens = token_find_with_prefix($tokens, 'author')) && $account = user_load($comment->uid)) {
|
| 233 |
$replacements += token_generate('user', $author_tokens, array('user' => $account), $options);
|
| 234 |
}
|
| 235 |
}
|
| 236 |
elseif ($type == 'node' & !empty($data['node'])) {
|
| 237 |
$node = $data['node'];
|
| 238 |
|
| 239 |
foreach ($tokens as $name => $original) {
|
| 240 |
switch($name) {
|
| 241 |
case 'comment-count':
|
| 242 |
$replacements[$original] = $node->comment_count;
|
| 243 |
break;
|
| 244 |
|
| 245 |
case 'comment-count-new':
|
| 246 |
$replacements[$original] = comment_num_new($node->nid);
|
| 247 |
break;
|
| 248 |
}
|
| 249 |
}
|
| 250 |
}
|
| 251 |
|
| 252 |
return $replacements;
|
| 253 |
}
|