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)));
 }
 public function prepare_items()
 {
     $videos_per_page = 20;
     $paged = $this->get_pagenum();
     $start = ($paged - 1) * $videos_per_page;
     $results = \Ekko\Core\Services\Hub::singleton()->get_videos(array('group' => get_current_blog_id(), 'start' => $start, 'limit' => $videos_per_page));
     $this->items = $results['videos'];
     $this->set_pagination_args(array('total_items' => (int) $results['total'], 'per_page' => $videos_per_page));
 }
 public final function save_changes()
 {
     switch ($this->video_list->current_action()) {
         case 'delete':
             if (array_key_exists('videos', $_POST) && is_array($_POST['videos'])) {
                 $videos = $_POST['videos'];
                 foreach ($videos as $video_id) {
                     \Ekko\Core\Services\Hub::singleton()->delete_video($video_id, get_current_blog_id());
                 }
                 if (count($videos) > 0) {
                     wp_redirect(wp_unslash($_REQUEST['_wp_http_referer']));
                     exit;
                 }
             }
             break;
         default:
             break;
     }
 }
    public final function display_page()
    {
        $hub = \Ekko\Core\Services\Hub::singleton();
        $post = get_post($_REQUEST['post']);
        $course = \Ekko\Core\CoursePostType::singleton()->get_course($post);
        $course_id = $course->course_ID;
        ?>
			<div class="wrap">
				<div id="icon-post" class="icon32"><br /></div>
				<h2>
					<?php 
        _e('Publish Course', \Ekko\TEXT_DOMAIN);
        ?>
					<a href="edit.php?post_type=ekko-course" class="add-new-h2">Return to Courses</a>
				</h2>

				<div class="ekko-bootstrap">
					<div><h1><?php 
        _e(sprintf('Publishing Course: %1$s', $course->post_title));
        ?>
</h1></div>
					<div class="well">
						<p><?php 
        if ($course_id === false) {
            $course_id = $hub->create_course($course->get_manifest());
        } else {
            $hub->update_course($course_id, $course->get_manifest());
        }
        $course->course_ID = $course_id;
        esc_html_e('Updating Course Manifest.', \Ekko\TEXT_DOMAIN);
        ?>
						</p>

						<p><?php 
        esc_html_e('Processing Course Media:', \Ekko\TEXT_DOMAIN);
        $existing_resources = $hub->get_resources($course_id);
        $resources = $course->resources;
        ?>
						<ul><?php 
        foreach ($resources as $resource) {
            if ($resource->type == 'file') {
                echo sprintf('<li>%1$s</li>', basename($resource->file));
                if (!in_array($resource->sha1, $existing_resources['files'])) {
                    $hub->upload_resource($course_id, $resource->file, $resource->mimeType);
                }
            } elseif ($resource->type == 'ecv') {
                echo sprintf('<li>%1$s</li>', basename($resource->title));
                if (!in_array($resource->ecv_id, $existing_resources['videos'])) {
                    $hub->add_course_to_video($resource->ecv_id, $course_id);
                }
            }
        }
        ?>
						</ul>
						</p><?php 
        $result = $hub->publish_course($course_id);
        ?>

						<p class="<?php 
        echo $result === true ? 'text-success' : 'text-error';
        ?>
"><?php 
        if ($result === true) {
            esc_html_e('Course successfully published!', \Ekko\TEXT_DOMAIN);
        } else {
            echo '<strong>' . esc_html__('One or more Errors occured during publish', \Ekko\TEXT_DOMAIN) . ':</strong>';
            if (is_array($result)) {
                foreach ($result as $error) {
                    echo '<div>' . esc_html($error) . '</div>';
                }
            }
        }
        ?>
						</p>

					</div>
				</div>
			</div>
			<?php 
        $admins = array();
        foreach (get_users() as $user) {
            $class = class_exists('\\GlobalTechnology\\CentralAuthenticationService\\CASLogin') ? \GlobalTechnology\CentralAuthenticationService\CASLogin::singleton() : \WPGCXPlugin::singleton();
            $guid = $class->get_user_guid($user);
            if ($user->has_cap('administrator')) {
                $admins[] = $guid;
            }
        }
        $hub->sync_admins($course_id, $admins);
    }
 public final function video_thumbnail()
 {
     $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
     $thumbnail = \Ekko\PLUGIN_URL . 'images/default-video.png';
     $video = \Ekko\Core\Services\Hub::singleton()->get_video($id, get_current_blog_id());
     if ($video) {
         if (array_key_exists('thumbnail', $video)) {
             $thumbnail = $video['thumbnail'];
         }
     }
     wp_redirect($thumbnail);
     wp_die();
 }
 public final function delete_post($post_id)
 {
     if (get_post_type($post_id) == $this->post_type()) {
         $post = get_post($post_id);
         $course = $this->get_post($post);
         $course_id = $course->course_ID;
         if ($course_id !== false) {
             $hub = \Ekko\Core\Services\Hub::singleton();
             $hub->delete_course($course_id);
         }
     }
 }
 public function __get($key)
 {
     switch ($key) {
         case 'lessons':
             $lessons = get_post_meta($this->ID, self::LESSONS, true);
             if (!$lessons || $lessons == '') {
                 $lessons = array();
             }
             return $lessons;
         case 'complete':
             $complete = get_post_meta($this->ID, self::COMPLETE, true);
             if (!$complete || $complete == '') {
                 $complete = (object) array('message' => '', 'active' => false);
             }
             return $complete;
         case 'course_ID':
             $course_id = get_post_meta($this->ID, self::COURSE_ID, true);
             if ($course_id && is_numeric($course_id)) {
                 return "{$course_id}";
             }
             return false;
         case 'description':
             return get_post_meta($this->ID, self::DESCRIPTION, true);
         case 'copyright':
             return get_post_meta($this->ID, self::COPYRIGHT, true);
         case 'author_name':
             $value = get_post_meta($this->ID, self::AUTHOR_NAME, true);
             if ($value) {
                 return $value;
             }
             $user = new \WP_User($this->post_author);
             return $user->display_name;
         case 'author_email':
             $value = get_post_meta($this->ID, self::AUTHOR_EMAIL, true);
             if ($value) {
                 return $value;
             }
             $user = new \WP_User($this->post_author);
             return $user->user_email;
         case 'author_url':
             $value = get_post_meta($this->ID, self::AUTHOR_URL, true);
             if ($value) {
                 return $value;
             }
             $user = new \WP_User($this->post_author);
             return $user->user_url;
         case 'show_metadata':
             return !get_post_meta($this->ID, self::METADATA, true);
         case 'resources':
             $resources = get_post_meta($this->ID, self::RESOURCES, true);
             if (!$resources || !is_array($resources)) {
                 return array();
             }
             return $resources;
         case 'enrollment_type':
             $enrollment_type = get_post_meta($this->ID, self::ENROLLMENT, true);
             if (!$enrollment_type || $enrollment_type == '' || is_object($enrollment_type)) {
                 $enrollment_type = self::ENROLLMENT_OPEN;
             }
             return $enrollment_type;
         case 'enrolled_users':
             $users = $this->get_transient('enrolled_users');
             if (!$users) {
                 $users = \Ekko\Core\Services\Hub::singleton()->get_enrolled($this->course_ID);
                 $this->set_transient('enrolled_users', $users, 15 * MINUTE_IN_SECONDS);
             }
             return $users;
         default:
             return parent::__get($key);
     }
 }
 public final function save_changes()
 {
     $post = get_post($_REQUEST['post']);
     $course = \Ekko\Core\CoursePostType::singleton()->get_course($post);
     switch ($this->enrollment_list->current_action()) {
         case 'enrollment-type':
             check_admin_referer('enrollment-type', '_wpnonce_enrollment-type');
             $enrollment_type = stripslashes($_POST['enrollment-type']);
             //Save changes to Hub and update course with response
             $settings = \Ekko\Core\Services\Hub::singleton()->update_settings($course->course_ID, $course::enrollment_type_to_settings($enrollment_type));
             $enrollment_type = $course::settings_to_enrollment_type($settings);
             $course->enrollment_type = $enrollment_type;
             wp_redirect(wp_get_referer());
             exit;
             break;
     }
 }