-
Notifications
You must be signed in to change notification settings - Fork 2
/
parsely.module
266 lines (229 loc) · 6.92 KB
/
parsely.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* @file
* The main module file.
*
*/
define('_PARSELY_VERSION', '0.1');
/**
* Implements hook_init().
*/
function parsely_init() {
$apikey = variable_get('parsely_apikey', '');
if (parsely_should_add_metas()) {
$parsely_meta = _parsely_get_node_metadata();
drupal_add_html_head(array(
'#tag' => 'script',
'#attributes' => array('type' => 'application/ld+json'),
'#value' => json_encode($parsely_meta),
), 'parsely_metadata');
drupal_add_html_head(array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'drupal_parsely_version',
'id' => 'drupal_parsely_version',
'content' => _PARSELY_VERSION
)
), 'parsely_version');
}
}
/**
* Implements hook_menu().
*/
function parsely_menu() {
$items['admin/config/system/parsely'] = array(
'title' => 'Parse.ly',
'description' => 'Parse.ly configuration settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('parsely_admin_settings'),
'access arguments' => array('administer parsely settings'),
'type' => MENU_NORMAL_ITEM,
'file' => 'parsely.admin.inc',
);
$items['admin/config/system/parsely/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function parsely_permission() {
return array(
'administer parsely settings' => array(
'title' => t('Administer Parse.ly settings'),
),
);
}
/**
* Determines whether or not to place the parsely meta tag on a page. This is
* separate from should_track because we don't need to consider whether users
* are authenticated here.
*/
function parsely_should_add_metas() {
$node = menu_get_object();
if ($node === NULL) {
return FALSE;
}
if (empty(variable_get('parsely_apikey', ''))) {
return FALSE;
}
$path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
if (drupal_match_path($path, implode("\r", parsely_paths_to_ignore()))) {
return FALSE;
}
return TRUE;
}
/**
* Determines whether or not to place the Parsely JavaScript on a page.
*/
function parsely_should_track() {
if (!parsely_should_add_metas()) {
return FALSE;
}
// Check whether or not we should track logged in users
$should_track_authenticated_users = variable_get('parsely_track_authenticated_users') === '0';
if ($should_track_authenticated_users && _parsely_user_is_authenticated()) {
return FALSE;
}
return TRUE;
}
/**
* Returns an array of paths not to track using Parse.ly's javascript/metadata
* tags.
*/
function parsely_paths_to_ignore() {
return array(
'admin*',
'node/add*',
'node/*/edit*',
'node/*/delete*',
);
}
/**
* Implements hook_page_build to insert Parse.ly's JavaScript tracker.
*/
function parsely_page_build(&$page) {
if (!parsely_should_track()) {
return;
}
$apikey = check_plain(variable_get('parsely_apikey'));
$markup = <<<EOT
<!-- START Parse.ly Include: Standard -->
<div id="parsely-root" style="display: none">
<div id="parsely-cfg" data-parsely-site="$apikey"></div>
</div>
<script>
(function(s, p, d) {
var h=d.location.protocol, i=p+"-"+s,
e=d.getElementById(i), r=d.getElementById(p+"-root"),
u=h==="https:"?"d1z2jf7jlzjs58.cloudfront.net"
:"static."+p+".com";
if (e) return;
e = d.createElement(s); e.id = i; e.async = true;
e.src = h+"//"+u+"/p.js"; r.appendChild(e);
})("script", "parsely", document);
</script>
<!-- END Parse.ly Include: Standard -->
EOT;
$page['page_bottom']['parsely'] = array('#markup' => $markup);
}
function _parsely_user_is_authenticated() {
// Anonymous users have a uid of 0, anyone else is authenticated
global $user;
return $user->uid !== 0;
}
/*** Parsely Metadata Functions **********************************************/
function _parsely_get_content_id($node) {
$prefix = variable_get('parsely_content_id_prefix', '');
if (!empty($prefix))
$prefix = $prefix . '-';
return $prefix.$node->nid;
}
function _parsely_get_canonical_url($node) {
$uri = entity_uri('node', $node);
return url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE)));
}
function _parsely_get_authors($node) {
return array($node->name);
}
function _parsely_get_pub_date_utc($node) {
$pub_date = NULL;
// Prefer published_at which is added by the publication_date module
// https://www.drupal.org/project/publication_date
if (property_exists($node, 'published_at') && is_numeric($node->published_at)) {
$pub_date = $node->published_at;
}
// Otherwise, fall back to just Drupal's created date
else {
$pub_date = $node->created;
}
return gmdate("Y-m-d\TH:i:s\Z", $pub_date);
}
/**
* Section is the first active term from the vocabulary we've been told to use
* for sections.
*/
function _parsely_get_section($node) {
$section_vocabulary = variable_get('parsely_section_vocabulary');
if (!module_exists('taxonomy') || $section_vocabulary === NULL)
return 'Uncategorized';
$active_terms_query = db_select('taxonomy_index', 't')
->fields('t')
->condition('nid', $node->nid, '=')
->execute();
$row = $active_terms_query->fetchAssoc();
if (isset($row['tid'])) {
$term = taxonomy_term_load($row['tid']);
return $term->name;
} else {
return 'Uncategorized';
}
}
function _parsely_get_tags($node) {
$supported_vocabularies = variable_get('parsely_tag_vocabularies');
if (!module_exists('taxonomy') || $supported_vocabularies === NULL)
return array();
// Get a mapping of all the terms in this vocabulary so that we can look up
// the name of the active terms by term id
$_terms = array();
foreach ($supported_vocabularies as $vid) {
$_terms = array_merge($_terms, taxonomy_get_tree($vid));
}
$terms = array();
foreach ($_terms as $term) {
$terms[$term->tid] = $term->name;
}
// Check what vocabulary terms are on the page that is loading
$active_terms_query = db_select('taxonomy_index', 't')
->fields('t')
->condition('nid', $node->nid, '=')
->execute();
// Build up and return active_terms
$active_terms = array();
while ($row = $active_terms_query->fetchAssoc()) {
if (isset($row['tid']))
array_push($active_terms, $terms[$row['tid']]);
}
return $active_terms;
}
function _parsely_get_node_metadata() {
$node = menu_get_object();
$parsely_meta = array(
'@context' => 'http://schema.org',
'@type' => 'WebPage',
'articleId' => _parsely_get_content_id($node),
'headline' => $node->title,
'url' => _parsely_get_canonical_url($node),
'thumbnailUrl' => 'FIX ME',
'dateCreated' => _parsely_get_pub_date_utc($node),
'articleSection' => _parsely_get_section($node),
'creator' => _parsely_get_authors($node),
'keywords' => _parsely_get_tags($node),
);
if ($node->type === 'article') {
$parsely_meta['@type'] = 'NewsArticle';
}
return $parsely_meta;
}