ID, "clef_id", true); } public static function associate_clef_id($clef_id, $user_id=false) { if (!$user_id) { $user_id = wp_get_current_user()->ID; } $user = get_users(array( 'meta_key' => 'clef_id', 'meta_value' => $clef_id, 'blog_id' => false )); if (!empty($user)) { return new WP_Error( 'clef_id_already_associated', __("The Clef account you're trying to connect is already associated to a different WordPress account", "clef") ); } update_user_meta($user_id, 'clef_id', $clef_id); } public static function dissociate_clef_id($user_id=false) { if (!$user_id) { $user_id = wp_get_current_user()->ID; } delete_user_meta($user_id, "clef_id"); } public static function exchange_oauth_code_for_info($code, $settings=null, $app_id=false, $app_secret=false) { ClefUtils::verify_state(); if ($settings) { if (!$app_id) $app_id = $settings->get( 'clef_settings_app_id' ); if (!$app_secret) $app_secret = $settings->get( 'clef_settings_app_secret' ); } $args = array( 'code' => $code, 'app_id' => $app_id, 'app_secret' => $app_secret, ); $response = wp_remote_post( CLEF_API_BASE . 'authorize', array( 'method'=> 'POST', 'body' => $args, 'timeout' => 20 ) ); if ( is_wp_error($response) ) { throw new LoginException(__( "Something went wrong: ", 'clef' ) . $response->get_error_message()); } $body = json_decode( $response['body'] ); if ( !isset($body->success) || $body->success != 1 ) { throw new LoginException(__( 'Error retrieving Clef access token: ', 'clef') . $body->error); } $access_token = $body->access_token; // Get info $response = wp_remote_get( CLEF_API_BASE . "info?access_token={$access_token}" ); if ( is_wp_error($response) ) { throw new LoginException(__( "Something went wrong: ", 'clef' ) . $response->get_error_message()); } $body = json_decode( $response['body'] ); if ( !isset($body->success) || $body->success != 1 ) { throw new LoginException(__('Error retrieving Clef user data: ', 'clef') . $body->error); } return $body->info; } public static function user_fulfills_role($user, $role) { $fulfills_role = false; $role_map = array( "subscriber", "contributor", "author", "editor", "administrator", "super administrator" ); foreach ($user->roles as &$user_role) { $rank = array_search($user_role, $role_map); if ($rank != 0 && $rank >= array_search($role, $role_map)) { $fulfills_role = true; break; } } if ($role == "super administrator" && is_super_admin($user->ID)) { $fulfills_role = true; } return $fulfills_role; } public static function get_custom_roles() { $all_roles = get_editable_roles(); $custom_roles = array(); foreach($all_roles as $role => $role_obj) { if (isset($role_obj['name'])) { $role_name = $role_obj['name']; if (!in_array($role_name, self::$default_roles)) { $custom_roles[$role] = $role_obj; } } } return $custom_roles; } public static function initialize_state($override = false) { if (!$override && isset($_COOKIE['_clef_state']) && $_COOKIE['_clef_state']) return; $state = wp_generate_password(24, false); @setcookie('_clef_state', $state, (time() + 60 * 60 * 24), '/', '', is_ssl(), true); $_COOKIE['_clef_state'] = $state; return $state; } public static function get_state() { if (!isset($$_COOKIE['_clef_state']) || !$_COOKIE['_clef_state']) ClefUtils::initialize_state(); return $_COOKIE['_clef_state']; } public static function verify_state() { $request_state = ClefUtils::isset_GET('state') ? ClefUtils::isset_GET('state') : ClefUtils::isset_POST('state'); $correct_state = ClefUtils::get_state(); if ($request_state && $correct_state && $correct_state == $request_state) { ClefUtils::initialize_state(true); return true; } else { throw new ClefStateException('The state parameter is not verified. This may be due to this page being cached by another WordPress plugin. Please refresh your page and try again'); } } } ?>