-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserdeck.module
More file actions
136 lines (116 loc) · 3.62 KB
/
Copy pathuserdeck.module
File metadata and controls
136 lines (116 loc) · 3.62 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
<?php
/**
* @file
* UserDeck module file.
*/
/**
* Implements hook_menu().
*/
function userdeck_menu() {
$items = array();
$items['admin/config/content/userdeck'] = array(
'title' => 'UserDeck Settings',
'description' => 'Configure settings for the UserDeck system.',
'page callback' => 'drupal_get_form',
'page arguments' => array('userdeck_admin'),
'access arguments' => array('administer site configuration'),
'file' => 'includes/userdeck.admin.inc',
);
$items['userdeck/guides/setkey/%/%'] = array(
'title' => 'UserDeck Settings',
'description' => 'Configure settings for the UserDeck system.',
'page callback' => 'userdeck_guides_setkey',
'page arguments' => array(3, 4),
'access callback' => TRUE,
);
return $items;
}
/**
* Implements hook_node_view().
*/
function userdeck_node_view($node, $view_mode, $langcode) {
if ('full' != $view_mode || !array_key_exists('body', $node->content) || FALSE === strpos($node->content['body'][0]['#markup'], 'userdeck_guides')) {
return $node;
}
$node->content['body'][0]['#markup'] = preg_replace_callback("/\[userdeck_guides (.+?)]/", "userdeck_process_shortcode", $node->content['body'][0]['#markup']);
return $node;
}
/**
* Page callback for setting the UserDeck Guides key.
*/
function userdeck_guides_setkey($key, $nonce) {
if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest' || !$key || !$nonce) {
drupal_not_found();
return;
}
$set_nonce = variable_get('userdeck_guides_key_nonce', FALSE);
if (!$set_nonce || $nonce != $set_nonce) {
drupal_not_found();
return;
}
variable_del('userdeck_guides_key_nonce');
variable_set('userdeck_guides_key', $key);
}
/**
* Builds a shortcode to add to be added to a page.
*/
function userdeck_get_guides_shortcode() {
$guides_key = variable_get('userdeck_guides_key');
if (!$guides_key) {
return FALSE;
}
return "[userdeck_guides key='$guides_key']";
}
/**
* Markup used to replace a Guides shortcode.
*/
function userdeck_output_guides_code($key) {
return "<a href='http://userdeck.com' data-userdeck-guides='$key'>Customer Support Software</a>
<script src='//widgets.userdeck.com/guides.js'></script>";
}
/**
* A preg_replace callback that replaces a shortcode with markup.
*/
function userdeck_process_shortcode($matches) {
$params = explode(" ", $matches[1]);
foreach ($params as $param) {
list($key, $val) = explode("=", $param);
if ($key == 'key') {
return userdeck_output_guides_code(str_replace("'", '', $val));
}
}
return '';
}
/**
* Builds a new basic page node with the Guides shortcode as the body.
*/
function userdeck_create_new_guides_page($title) {
global $user;
$node = new stdClass();
$node->title = $title;
$node->type = 'page';
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
$node->uid = $user->uid;
$node->status = 1;
$node->promote = 0;
$node->comment = 0;
$node->body[LANGUAGE_NONE][0]['value'] = userdeck_get_guides_shortcode();
$node->body[LANGUAGE_NONE][0]['summary'] = 'Customer Support Software';
$node->body[LANGUAGE_NONE][0]['format'] = filter_default_format();
$node = node_submit($node);
node_save($node);
return $node->nid;
}
/**
* Adds the Guides shortcode to the body of the given node.
*/
function userdeck_add_guide_to_page($nid) {
$node = node_load($nid);
if (!$node || !property_exists($node, 'body')) {
return FALSE;
}
$node->body[LANGUAGE_NONE][0]['value'] .= userdeck_get_guides_shortcode();
node_save($node);
return $nid;
}