-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstealourcontent.module
More file actions
492 lines (421 loc) · 13.4 KB
/
Copy pathstealourcontent.module
File metadata and controls
492 lines (421 loc) · 13.4 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
/**
* @file
* Contains block, theme, and hooks for StealOurContent module.
*/
define('STEALOURCONTENT_DEFAULT_LICENSE', 'cc-by-nd-4');
/**
* Implements hook_menu().
*/
function stealourcontent_menu() {
$items = array();
$items['stealourcontent/%ctools_js/%'] = array(
'title' => 'Steal Our Content',
'page arguments' => array(1, 2),
'page callback' => 'stealourcontent_modal_page',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_entity_info_alter().
*
* Add our custom stealourcontent_node view mode.
*/
function stealourcontent_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes'] += array(
'stealourcontent_node' => array(
'label' => t('StealOurContent'),
'custom settings' => FALSE,
),
);
return $entity_info;
}
/**
* Menu callback to generate the republish popup/modal.
*
* @param string $js
* 'ajax' if js is available.
*
* @param string $nid
* The node nid being viewed in the modal.
*
* @return string
* The content to be rendered.
*/
function stealourcontent_modal_page($js = NULL, $nid = NULL) {
if ($nid == NULL) {
return t('Sorry, node not found.');
}
// See template_preprocess_soc_button()
ctools_include('modal');
ctools_include('ajax');
ctools_modal_add_js();
$node = node_load($nid);
$content = theme('soc_modal_window', array('node' => $node));
return ctools_modal_render(t('Steal Our Content!'), $content);
}
/**
* Implements hook_block_info().
*/
function stealourcontent_block_info() {
$blocks['stealourcontent_block'] = array(
'info' => t('Republish Button'),
'cache' => DRUPAL_CACHE_GLOBAL,
);
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function stealourcontent_block_configure($delta = '') {
$form = array();
if ($delta == 'stealourcontent_block') {
stealourcontent_settings_form($form);
}
return $form;
}
/**
* Implements hook_block_save().
*/
function stealourcontent_block_save($delta = '', $edit = array()) {
if ($delta == 'stealourcontent_block') {
variable_set('soc_link_text', $edit['soc_link_text']);
variable_set('soc_title', $edit['soc_title']);
variable_set('soc_guidelines', $edit['soc_guidelines']);
variable_set('soc_guidelines_link', $edit['soc_guidelines_link']);
variable_set('soc_instructions', $edit['soc_instructions']);
variable_set('soc_branding', $edit['soc_branding']);
variable_set('soc_license', $edit['soc_license']);
}
}
/**
* Implements hook_block_view().
*/
function stealourcontent_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'stealourcontent_block':
$block['subject'] = '';
$block['content'] = array(
'#markup' => stealourcontent_block(),
'#attached' => array('css' => array(drupal_get_path('module', 'stealourcontent') . '/css/stealourcontent.css')),
);
break;
}
return $block;
}
/**
* Display a Republish button to open a modal.
*
* @return string
* The html to be displayed.
*/
function stealourcontent_block() {
global $user;
// Only display the block if we are on a node page
// and the user has access to view that node.
$node = menu_get_object();
if (($node) && node_access('view', $node, $user)) {
$out = theme('soc_button', array('node' => $node));
}
else {
$out = '';
}
return $out;
}
/**
* Builds Settings Form for the StealOurContent Bock.
*/
function stealourcontent_settings_form(&$form, $form_state = array(), $conf = array()) {
$form['soc_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Republish Link Text'),
'#description' => t('Enter text to be displayed on the Republish button.'),
'#default_value' => variable_get('soc_link_text', stealourcontent_text('soc_link_text')),
);
$form['soc_title'] = array(
'#type' => 'textfield',
'#title' => t('Republish Box Title'),
'#description' => t('Enter the title for Republish popup box.'),
'#default_value' => variable_get('soc_title', stealourcontent_text('soc_title')),
);
$form['soc_guidelines'] = array(
'#type' => 'textarea',
'#title' => t('Republish Guidelines'),
'#description' => t('Enter a descriptive guideline to be displayed on the Republish popup box above the main body. Supports Filtered HTML format.'),
'#default_value' => variable_get('soc_guidelines', stealourcontent_text('soc_guidelines', NULL, FALSE)),
);
$form['soc_instructions'] = array(
'#type' => 'textarea',
'#title' => t('Republish Instructions'),
'#description' => t('Enter instructions to be displayed on the Republish overlay box below the main body.'),
'#default_value' => variable_get('soc_instructions', stealourcontent_text('soc_instructions')),
);
$form['soc_branding'] = array(
'#type' => 'textarea',
'#title' => t('Branding Text'),
'#description' => t('Enter the Branding text to be included in editor.'),
'#default_value' => variable_get('soc_branding', stealourcontent_text('soc_branding', NULL, FALSE)),
);
if (module_exists("token")) {
$form['soc_token_help'] = array(
'#type' => 'markup',
'#token_types' => array('node'),
'#theme' => 'token_tree_link',
);
}
// You can override and display a custom license via the theme layer.
// @see template_preprocess_soc_license().
$licenses = stealourcontent_licenses();
$license_options = array();
foreach ($licenses as $key => $info) {
$license_options[$key] = $info['title'];
}
$form['soc_license'] = array(
'#type' => 'select',
'#title' => t('License'),
'#options' => $license_options,
'#default_value' => variable_get('soc_license', STEALOURCONTENT_DEFAULT_LICENSE),
'#description' => t('Select a license for content reuse.'),
);
$form['soc_guidelines_link'] = array(
'#type' => 'textfield',
'#title' => t('Additional Guidelines Link'),
'#description' => t('Optionally provide a link to page containing additional guidelines for republishing.'),
'#default_value' => variable_get('soc_guidelines_link', ''),
);
}
/**
* Implements hook_theme().
*/
function stealourcontent_theme() {
$path = drupal_get_path('module', 'stealourcontent');
return array(
'soc_button' => array(
'template' => 'soc_button',
'file' => 'theme.inc',
'path' => $path . '/theme',
'variables' => array(
'node' => NULL,
),
),
'soc_modal_window' => array(
'template' => 'soc_modal_window',
'file' => 'theme.inc',
'path' => $path . '/theme',
'variables' => array(
'node' => NULL,
),
),
'soc_license' => array(
'template' => 'soc_license',
'file' => 'theme.inc',
'path' => $path . '/theme',
),
);
}
/**
* Implements hook_preprocess_node().
*/
function stealourcontent_preprocess_node(&$vars) {
if ($vars['view_mode'] == 'stealourcontent_node') {
$vars['theme_hook_suggestions'][] = 'node__socmodal';
// If you want to provide a node type specific template use the format below.
// Name your template like: node--article--socmodal.tpl.php
// Where "article" is the node type.
// You can copy node--socmodal.tpl.php as a starting point.
$vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__socmodal';
}
}
/**
* Implements hook_theme_registry_alter().
*/
function stealourcontent_theme_registry_alter(&$theme_registry) {
$module_path = drupal_get_path('module', 'stealourcontent');
// Find all .tpl.php files in this module's folder recursively.
$template_file_objects = drupal_find_theme_templates($theme_registry, '.tpl.php', $module_path);
// Itterate through all found template file objects.
foreach ($template_file_objects as $key => $template_file_object) {
// If the template has not already been overridden by a theme.
if (!isset($theme_registry[$key]['theme path']) || !preg_match('#/themes/#', $theme_registry[$key]['theme path'])) {
// Alter the theme path and template elements.
$theme_registry[$key]['theme path'] = $module_path;
$theme_registry[$key] = array_merge($theme_registry[$key], $template_file_object);
}
}
}
/**
* Default text. Can be overridden in the block config.
*
* @example $text = stealourcontent_text('branding', NULL, TRUE, $vars['node']);
*/
function stealourcontent_text($key, $language = NULL, $replace = TRUE, $node = array()) {
$langcode = isset($language) ? $language->language : NULL;
switch ($key) {
case 'soc_link_text':
$text = t('Republish', array(), array('langcode' => $langcode));
break;
case 'soc_title':
$text = t('Republish this content', array(), array('langcode' => $langcode));
break;
case 'soc_guidelines':
$text = t('Like this content? Republish it. But please do not edit the piece. Also make sure that you attribute the author, and mention the article was originally published on [site:name].', array(), array('langcode' => $langcode));
break;
case 'soc_instructions':
$text = t('Copy the above code and paste it into your website to republish.', array(), array('langcode' => $langcode));
break;
case 'soc_branding':
$text = t('This article was originally published on <a href="[site:url]">[site:name]</a>. Read the <a href="[node:url]">original article</a>.', array(), array('langcode' => $langcode));
break;
}
if ($replace) {
// Token Replace the text.
return token_replace($text, array('node' => $node));
}
return $text;
}
/**
* Get the available licenses for content reuse.
*
* NOTE: You can manually override and display your own custom license
* via the theme layer.
* @see template_preprocess_soc_license()
*
* @return array
* An array of available licenses.
*/
function stealourcontent_licenses() {
$img_path = drupal_get_path('module', 'stealourcontent') . '/images';
$licenses = array(
'cc0' => array(
'title' => 'CC: Public Domain',
'link' => 'https://creativecommons.org/publicdomain/zero/1.0/',
'image' => $img_path . '/cc0.png',
),
'cc-by-4' => array(
'title' => 'CC: Attribution',
'link' => 'https://creativecommons.org/licenses/by/4.0/',
'image' => $img_path . '/cc-by-4.png',
),
'cc-by-nd-4' => array(
'title' => 'CC: Attribution - No Derivatives',
'link' => 'http://creativecommons.org/licenses/by-nd/4.0/',
'image' => $img_path . '/cc-by-nd-4.png',
),
);
return $licenses;
}
/**
* Find image/file fields.
*
* Attempts to determine which fields belonging to the specified node are
* image/file fields.
*
* @return array
* An array of any node fields that contain files. Otherwise an empty array.
*/
function soc_get_file_fields($node) {
$files = array();
// Get the extra fields assigned to a node type.
$fields = field_info_instances('node', $node->type);
$file_fields = array();
foreach ($fields as $type => $info) {
// Assumption being made that this will identify a file/image field.
if (isset($info['settings']) && isset($info['settings']['file_directory'])) {
$file_fields[] = $type;
}
}
if (count($file_fields)) {
foreach ($file_fields as $field) {
// Because this doesn't work: $node->$field['und']
$info = $node->$field;
if (isset($node->$field) && isset($info['und'][0]['uri'])) {
$files[] = $info['und'][0]['uri'];
}
}
}
return $files;
}
/**
* Check if a set of files contain a private file.
*
* @param array $uris
* An array of file uris.
*
* @return bool
* TRUE if there is at least one private file otherwise FALSE.
*/
function soc_private_files($uris) {
foreach ($uris as $uri) {
$scheme = file_uri_scheme($uri);
if ($scheme == 'private') {
return TRUE;
}
}
return FALSE;
}
/**
* Convert relative to absolute urls.
*
* Code from https://www.drupal.org/project/rel_to_abs
*
* @param string $text
* The text/html to convert.
*
* @return string
* The text with any relative urls converted to absolute.
*/
function soc_rel_to_abs($text) {
$lang = language_default();
$front = url('<front>', array(
'absolute' => TRUE,
'language' => $lang,
));
$base_url = $front;
$text = soc_absolute_url($text, $base_url);
return $text;
}
/**
* Convert relative to absolute urls.
*
* Code from https://www.drupal.org/project/rel_to_abs
*
* @param string $txt
* The text/html to convert.
*
* @param string $base_url
* The base url to use in replacements.
*
* @return string
* The text with any relative urls converted to absolute.
*/
function soc_absolute_url($txt, $base_url) {
$needles = array('href="', 'src="', 'background="');
$new_txt = '';
if (substr($base_url, -1) != '/') {
$base_url .= '/';
}
$new_base_url = $base_url;
$base_url_parts = parse_url($base_url);
foreach ($needles as $needle) {
while ($pos = strpos($txt, $needle)) {
$pos += strlen($needle);
if (substr($txt, $pos, 7) != 'http://' && substr($txt, $pos, 8) != 'https://' && substr($txt, $pos, 6) != 'ftp://' && substr($txt, $pos, 7) != 'mailto:' && substr($txt, $pos, 2) != '//' && substr($txt, $pos, 1) != '#') {
// If the relative url starts with a slash remove it.
if ($txt[$pos] == '/' || $txt[$pos] == '\\') {
$txt = substr_replace($txt, '', $pos, 1);
}
$new_txt .= substr($txt, 0, $pos) . $new_base_url;
}
else {
$new_txt .= substr($txt, 0, $pos);
}
$txt = substr($txt, $pos);
}
$txt = $new_txt . $txt;
$new_txt = '';
}
return $txt;
}