-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm-github-connection.php
299 lines (257 loc) · 11.6 KB
/
mm-github-connection.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
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
<?php
/**
* Plugin Name: MM Github Connection
* Plugin URI: http://marsminds.com/
* Description: Making simple the login and connection process with Github.
* Version: 1
* Author: Rodrigo Techera
* Author URI: http://rodmind.com/
* License: GPL2
*/
add_action('init', 'mm_github_connection_process_github_actions');
function mm_github_connection_process_github_actions() {
global $user_ID;
$current_url = mm_github_connection_get_current_url();
if(mm_github_connection_is_conifgured()) {
if(isset($_GET['mm_unlink_github']) && $_GET['mm_unlink_github']=='true') {
update_user_meta($user_ID, 'mm_github_connection_github_id', '');
return wp_redirect($current_url);
}
if(isset($_GET['code']) && $_GET['code'] && isset($_GET['mm_github_connection']) && $_GET['mm_github_connection']=='true') {
$data_array = mm_github_connection_get_data($_GET['code'], $current_url);
if(is_array($data_array) && count($data_array)>0) {
update_user_meta($user_ID, 'mm_github_connection_github_id', $data_array['github_internal_id']);
return wp_redirect($current_url);
}
}
if(isset($_GET['code']) && $_GET['code'] && isset($_GET['mm_github_login']) && $_GET['mm_github_login']=='true') {
$data_array = mm_github_connection_get_data($_GET['code'], $current_url);
$users_array = get_users(array('meta_key'=>'mm_github_connection_github_id', 'meta_value'=>$data_array['github_internal_id']));
if(is_array($users_array) && count($users_array)>0) {
$user_to_auth_obj = $users_array[0];
if($user_to_auth_obj) {
wp_set_current_user($user_to_auth_obj->ID, $user_to_auth_obj->user_login);
wp_set_auth_cookie($user_to_auth_obj->ID);
do_action('wp_login', $user_to_auth_obj->user_login);
return wp_redirect(home_url('/'));
}
}
}
}
}
add_action('admin_init', 'mm_github_connection_register_setting_fields');
function mm_github_connection_register_setting_fields() {
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'mm_github_connection_plugins_bar_links');
add_settings_section('mm_connection_settings_section', '<a id="mm_connection_settings"></a>'.__('MM Connection Global Settings', 'marsminds'), 'mm_github_connection_global_settings_section', 'general');
register_setting('general', 'mm_connection_global_css', 'esc_attr');
add_settings_field('mm_connection_global_css', '<label for="mm_connection_global_css">'.__('MM Connection Global CSS', 'marsminds').'</label>' , 'mm_github_connection_print_global_css_field', 'mm_connection_settings_section');
add_settings_section('mm_github_connection_settings_section', '<a id="mm_github_connection_settings"></a>'.__('MM Github Connection Settings', 'marsminds'), 'mm_github_connection_settings_section', 'general');
register_setting('general', 'mm_github_connection_client_id', 'esc_attr');
register_setting('general', 'mm_github_connection_client_secret', 'esc_attr');
add_settings_field('mm_github_connection_client_id', '<label for="mm_github_connection_client_id">'.__('MM Github Client ID', 'marsminds').'</label>' , 'mm_github_connection_print_client_id_field', 'mm_github_connection_settings_section');
add_settings_field('mm_github_connection_client_secret', '<label for="mm_github_connection_client_secret">'.__('MM Github Client Secret', 'marsminds').'</label>' , 'mm_github_connection_print_client_secret_field', 'mm_github_connection_settings_section');
}
function mm_github_connection_plugins_bar_links($links) {
return array_merge(
array( '<a href="'.admin_url('options-general.php#mm_connection_settings').'">'.__('CSS Settings', 'marsminds').'</a>',
'<a href="'.admin_url('options-general.php#mm_github_connection_settings').'">'.__('Github Connection Settings', 'marsminds').'</a>'),
$links);
}
function mm_github_connection_print_global_css_field() {
$global_css_default_value = '<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">'."\n".
'<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">'."\n".
'<style type="text/css">'."\n".
' a.mm-sl-btn{'."\n".
' background-color:#fa5b49;'."\n".
' color:#fff;'."\n".
' display:block;'."\n".
' font:14px/20px "Montserrat",helvetica;'."\n".
' margin:0 auto 10px;'."\n".
' max-width:180px;'."\n".
' padding:10px 18px 9px;'."\n".
' text-decoration:none;'."\n".
' transition:opacity 0.4s ease,padding 0.2s ease,box-shadow 0.2s ease;'."\n".
' }'."\n".
' a.mm-sl-btn:hover{'."\n".
' opacity:0.9;'."\n".
' }'."\n".
' a.mm-sl-btn:active{'."\n".
' padding:12px 18px 7px;'."\n".
' box-shadow:inset 0 2px 0 rgba(0,0,0,0.3);'."\n".
' }'."\n".
' a.mm-sl-btn i{'."\n".
' margin-right:8px;'."\n".
' }'."\n".
' a.mm-sl-fb{'."\n".
' background-color:#3b5998;'."\n".
' }'."\n".
' a.mm-sl-tw{'."\n".
' background-color:#4099FF;'."\n".
' }'."\n".
' a.mm-sl-go{'."\n".
' background-color:#d34836;'."\n".
' }'."\n".
' a.mm-sl-li{'."\n".
' background-color:#007bb5;'."\n".
' }'."\n".
' a.mm-sl-in{'."\n".
' background-color:#125688;'."\n".
' }'."\n".
' a.mm-sl-pi{'."\n".
' background-color:#cb2027;'."\n".
' }'."\n".
' a.mm-sl-gi{'."\n".
' background-color:#333;'."\n".
' }'."\n".
'</style>';
$global_css_value = get_option('mm_connection_global_css', $global_css_default_value);
echo '<textarea name="mm_connection_global_css" style="width:80%;height:250px;">'.$global_css_value.'</textarea>';
}
function mm_github_connection_print_client_id_field() {
$client_id_value = get_option('mm_github_connection_client_id');
echo '<input type="text" name="mm_github_connection_client_id" class="regular-text" value="'.$client_id_value.'">';
}
function mm_github_connection_print_client_secret_field() {
$client_secret_value = get_option('mm_github_connection_client_secret');
echo '<input type="text" name="mm_github_connection_client_secret" class="regular-text" value="'.$client_secret_value.'">';
}
function mm_github_connection_global_settings_section($args) {
?>
<table class="form-table">
<tbody>
<?php do_settings_fields('mm_connection_settings_section', 'default');?>
</tbody>
</table>
<?php
}
function mm_github_connection_settings_section($args) {
?>
<table class="form-table">
<tbody>
<tr>
<th colspan="2">
<label>Procedure:</label>
<p class="description">1 - Create a Github Application here <a href="https://github.com/settings/applications/new" target="_blank">https://github.com/settings/applications/new</a>.</p>
<p class="description">2 - Authorization callback URL needs to be this: <?php echo mm_github_connection_get_current_domain();?></p>
<p class="description">3 - Take the Client ID and Client Secret from your recent created App, and complete the fields below.</p>
</td>
</tr>
<?php do_settings_fields('mm_github_connection_settings_section', 'default');?>
</tbody>
</table>
<?php
}
function mm_github_connection_get_current_domain() {
$server_protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!='off' || $_SERVER['SERVER_PORT']==443)?'https://':'http://';
return $server_protocol.$_SERVER["HTTP_HOST"];
}
function mm_github_connection_get_current_url() {
return mm_github_connection_get_current_domain().$_SERVER["PHP_SELF"];
}
add_action('admin_head','mm_github_connection_hook_css');
add_action('login_head', 'mm_github_connection_hook_css');
add_action('wp_head','mm_github_connection_hook_css');
function mm_github_connection_hook_css() {
global $mm_connection_global_css_value;
if(!isset($mm_connection_global_css_value)) {
$mm_connection_global_css_value = get_option('mm_connection_global_css');
echo html_entity_decode($mm_connection_global_css_value);
}
}
function mm_github_connection_is_conifgured() {
$client_id = get_option('mm_github_connection_client_id');
$client_secret = get_option('mm_github_connection_client_secret');
if($client_id && $client_secret)
return true;
else
return false;
}
function mm_github_connection_is_linked($user_id) {
$github_internal_id = get_user_meta($user_id, 'mm_github_connection_github_id', true);
if($github_internal_id)
return true;
else
return false;
}
function mm_github_connection_get_authorize_url($login=false) {
$client_id = get_option('mm_github_connection_client_id');
$current_url = mm_github_connection_get_current_url();
if($login)
$redirect_url = urlencode($current_url.'?mm_github_login=true');
else
$redirect_url = urlencode($current_url.'?mm_github_connection=true');
return 'https://github.com/#/oauth/authorize?client_id='.$client_id.'&redirect_uri='.$redirect_url.'&scope=user';
}
add_action('profile_personal_options', 'mm_github_connection_profile_personal_options');
function mm_github_connection_profile_personal_options() {
global $user_ID;
$current_url = mm_github_connection_get_current_url();
?>
<table class="form-table">
<tbody>
<tr>
<th>
<label><?php echo __('MM Github Connect', 'marsminds');?>:</label>
</th>
<td>
<?php if(mm_github_connection_is_conifgured()) { ?>
<?php if(mm_github_connection_is_linked($user_ID)) { ?>
<a class="mm-sl-btn mm-sl-gi" href="<?php echo $current_url.'?mm_unlink_github=true';?>"><i class="fa fa-github"></i><?php echo __('Unlink Account', 'marsminds');?></a>
<?php } else { ?>
<a class="mm-sl-btn mm-sl-gi" href="<?php echo mm_github_connection_get_authorize_url();?>"><i class="fa fa-github"></i><?php echo __('Link Account', 'marsminds');?></a>
<?php } ?>
<?php } elseif(current_user_can('manage_options')) { ?>
<a href="<?php echo admin_url('options-general.php#mm_github_connection_settings');?>"><?php echo __('Configure it first!', 'marsminds');?></a>
<?php } ?>
</td>
</tr>
</tbody>
</table>
<?php
}
add_action('login_form', 'mm_github_connection_login_form');
function mm_github_connection_login_form() {
if(mm_github_connection_is_conifgured()) {
echo '<a class="mm-sl-btn mm-sl-gi" href="'.mm_github_connection_get_authorize_url(true).'"><i class="fa fa-github"></i>Login with Github</a>';
}
}
function mm_github_connection_get_data($github_code, $current_url) {
$client_id = get_option('mm_github_connection_client_id');
$client_secret = get_option('mm_github_connection_client_secret');
$endpoint = 'https://github.com/#/oauth/access_token';
$params = array('client_id='.$client_id,
'redirect_uri='.$current_url,
'client_secret='.$client_secret,
'code='.$github_code);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(!$result) {
echo curl_error($ch);
curl_close($ch);
return;
}
curl_close($ch);
parse_str($result);
if(isset($access_token) && $access_token) {
$endpoint = 'https://api.github.com/user?access_token='.$access_token;
$headers = array("User-Agent: Github-Connect-for-Wordpress");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if($result_array = json_decode($result, ARRAY_A)) {
return array( 'access_token' => $access_token,
'login_name' => $result_array['login'],
'github_internal_id' => $result_array['id'],
'name' => $result_array['name'],
'email' => $result_array['email']);
}
}
return false;
}
?>