function prepare_items() { $this->_column_headers = array($this->get_columns(), array(), array()); $post = get_post($_REQUEST['post']); $course = \Ekko\Core\CoursePostType::singleton()->get_course($post); $hub = \Ekko\Core\Services\Hub::singleton(); $thekey = \Ekko\Core\Services\TheKey::singleton(); $this->items = array(); $enrolled = $hub->get_enrolled($course->course_ID); $course->set_transient('enrolled_users', $enrolled, 15 * MINUTE_IN_SECONDS); foreach ($enrolled as $guid) { $user = $thekey->get_user_by_guid($guid); if ($user instanceof \WP_User) { $this->items[] = $user; } else { if ($attributes = $thekey->get_user_attributes_by_guid($guid)) { $user = $thekey->create_user($attributes->guid, array('user_login' => $attributes->email, 'user_email' => $attributes->email, 'first_name' => $attributes->firstName, 'last_name' => $attributes->lastName, 'display_name' => "{$attributes->firstName} {$attributes->lastName}", 'nickname' => '')); if ($user instanceof \WP_User) { $this->items[] = $user; } } } } $this->set_pagination_args(array('total_items' => count($this->items), 'per_page' => 10, 'total_pages' => ceil(count($this->items) / 10))); }
/** * Return a valid Ekko Hub session ID * * @param bool $superuser * * @return string */ public function get_session($superuser = false) { $user = $superuser ? \Ekko\Core\Services\TheKey::singleton()->get_user_by_guid(\Ekko\GUID_SUPER_ADMIN) : wp_get_current_user(); //Retrieve session from WordPress user meta $session = \GTO\Framework\Util\User::get_user_transient($user->ID, self::TRANSIENT_SESSION); //Return session ID if session is valid if ($session && $session !== false) { return $session; } //Session was not valid, fetch a new session ID from the Ekko Hub $err_code = null; $err_msg = null; $ticket = $superuser ? \Ekko\Core\Services\TheKey::singleton()->get_ticket($this->get_service(), \Ekko\OAUTH_REFRESH_TOKEN) : \Ekko\Core\Services\TheKey::singleton()->cas_client()->retrievePT($this->get_service(), $err_code, $err_msg); $response = wp_remote_post(\GTO\Framework\Util\String::vnsprintf(self::ENDPOINT_LOGIN, array('hub' => \Ekko\URI_HUB)), array('redirection' => 0, 'headers' => array('Accept' => 'application/json'), 'body' => array('ticket' => $ticket))); if ($this->is_response_OK($response) && ($json = json_decode($response['body'], true))) { //Store the new Session into the WordPress user meta \GTO\Framework\Util\User::set_user_transient($user->ID, self::TRANSIENT_SESSION, $json['id'], 5 * \HOUR_IN_SECONDS); //Return the Session ID return $session; } return false; }