-
Notifications
You must be signed in to change notification settings - Fork 12
/
functions.php
178 lines (163 loc) · 3.59 KB
/
functions.php
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
<?php //phpcs:disable Files.SideEffects.FoundWithSymbols
/**
* Theme functions file.
*
* @package capsule-server
*/
require_once 'config.php';
require_once 'ui/functions.php';
require_once 'capsule-server-import-export.php';
require_once 'inc/class-capsule-server.php';
$cap_server = new Capsule_Server();
$cap_server->add_actions();
/**
* Generate new API key. AJAX handler.
*
* @return void
*/
function capsule_server_ajax_new_api()
{
$nonce = $_GET['_wpnonce']; //phpcs:ignore
$user_id = (int) $_POST['user_id'];
if (
$user_id && wp_verify_nonce($nonce, 'cap-regenerate-key') &&
( current_user_can('edit_users') || get_current_user_id() === $user_id )
) {
$cap = new Capsule_Server($user_id);
$key = $cap->generate_api_key();
$cap->set_api_key($key);
echo esc_html($key);
}
die();
}
add_action('wp_ajax_cap_new_api_key', 'capsule_server_ajax_new_api');
/**
* Generate the user meta key for the api key value.
* This generates a different key for each blog if it is a multisite install
*
* @return string meta key
**/
function capsule_server_api_meta_key()
{
if (is_multisite()) {
$blog_id = get_current_blog_id();
$api_meta_key = ( 1 === $blog_id ) ? '_capsule_api_key' : '_capsule_api_key_' . $blog_id;
} else {
$api_meta_key = '_capsule_api_key';
}
return $api_meta_key;
}
/**
* Validates a user's existance in the db against an api key.
*
* @param string $api_key The api key to use for the validation.
* @return integer User ID or 0 if none can be found.
*/
function capsule_server_validate_user($api_key)
{
global $wpdb;
$meta_key = capsule_server_api_meta_key();
return (int) $wpdb->get_var(
$wpdb->prepare(
'
SELECT `user_id`
FROM ' . $wpdb->usermeta . '
WHERE `meta_key` = %s
AND `meta_value` = %s',
$meta_key,
$api_key
)
);
}
/**
* Display admin notices.
*
* @return void
*/
function capsule_server_admin_notice()
{
if (empty($_GET['page']) || false !== strpos($_GET['page'], 'capsule')) { //phpcs:ignore
return;
}
?>
<section class="capsule-welcome">
<h1><?php esc_html_e('Welcome to Capsule Server', 'capsule-server'); ?></h1>
<p>
<?php
// Translators: %s is the admin Capsule page url.
printf(
esc_html__(
'Please read the overview, FAQs and more about <a href="%s">how Capsule Server works</a>.',
'capsule-server'
),
esc_url(admin_url('admin.php?page=capsule'))
);
?>
</p>
</section>
<?php
}
add_action('admin_notices', 'capsule_server_admin_notice');
/**
* Add menu entries.
*
* @return void
*/
function capsule_server_menu()
{
global $menu;
$menu['3'] = array( '', 'read', 'separator-capsule', '', 'wp-menu-separator' );
add_menu_page(
esc_html__('Capsule Server', 'capsule-server'),
esc_html__('Capsule Server', 'capsule-server'),
'read',
'capsule',
'capsule_server_help',
'',
'3.1'
);
// Needed to make separator show up.
ksort($menu);
add_submenu_page(
'capsule',
esc_html__('Projects', 'capsule-server'),
esc_html__('Projects', 'capsule-server'),
'manage_categories',
'capsule-projects',
'capsule_server_admin_page_projects'
);
add_submenu_page(
'capsule',
esc_html__('Users', 'capsule-server'),
esc_html__('Users', 'capsule-server'),
'create_users',
'capsule-users',
'capsule_server_admin_page_users'
);
}
add_action('admin_menu', 'capsule_server_menu');
/**
* Help page.
*
* @return void
*/
function capsule_server_help()
{
include 'inc/server-help.php';
}
/**
* Empty placeholder.
*
* @return void
*/
function capsule_server_admin_page_projects()
{
}
/**
* Empty placeholder.
*
* @return void
*/
function capsule_server_admin_page_users()
{
}