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', ''.__('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', '' , 'mm_github_connection_print_global_css_field', 'mm_connection_settings_section'); add_settings_section('mm_github_connection_settings_section', ''.__('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', '' , 'mm_github_connection_print_client_id_field', 'mm_github_connection_settings_section'); add_settings_field('mm_github_connection_client_secret', '' , 'mm_github_connection_print_client_secret_field', 'mm_github_connection_settings_section'); } function mm_github_connection_plugins_bar_links($links) { return array_merge( array( ''.__('CSS Settings', 'marsminds').'', ''.__('Github Connection Settings', 'marsminds').''), $links); } function mm_github_connection_print_global_css_field() { $global_css_default_value = ''."\n". ''."\n". ''; $global_css_value = get_option('mm_connection_global_css', $global_css_default_value); echo ''; } function mm_github_connection_print_client_id_field() { $client_id_value = get_option('mm_github_connection_client_id'); echo ''; } function mm_github_connection_print_client_secret_field() { $client_secret_value = get_option('mm_github_connection_client_secret'); echo ''; } function mm_github_connection_global_settings_section($args) { ?>

1 - Create a Github Application here https://github.com/settings/applications/new.

2 - Authorization callback URL needs to be this:

3 - Take the Client ID and Client Secret from your recent created App, and complete the fields below.

Login with Github'; } } 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/login/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; } ?>