PluginProbe ʕ •ᴥ•ʔ
Presto Player / 2.0.7
Presto Player v2.0.7
4.5.0 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.4 4.2.3 4.2.2 4.2.0 4.2.1 trunk 1.10.0 1.10.1 1.10.2 1.11.0 1.12.0 1.13.0 1.14.0 1.14.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.13 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3-beta1 2.3.0 2.3.1 2.3.2 2.3.3 3.0.0 3.0.0-beta1 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1.0 3.1.1 3.1.2 3.1.3 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4
presto-player / inc / Integrations / LearnDash / LearnDash.php
presto-player / inc / Integrations / LearnDash Last commit date
LearnDash.php 5 years ago
LearnDash.php
398 lines
1 <?php
2
3 namespace PrestoPlayer\Integrations\LearnDash;
4
5 use PrestoPlayer\Models\Post;
6 use PrestoPlayer\Support\Utility;
7 use PrestoPlayer\Contracts\Service;
8
9 class LearnDash implements Service
10 {
11 public function register()
12 {
13 add_action('plugins_loaded', function () {
14 if (!self::isEnabled()) {
15 return;
16 }
17
18 add_filter('learndash_settings_fields', [$this, 'settingsFields'], 10, 2);
19 add_filter('ld_video_provider', [$this, 'addProvider'], 10, 2);
20 add_filter('presto-settings-block-js-options', [$this, 'jsOptions']);
21 add_filter('get_post_metadata', [$this, 'filterVideoURL'], 10, 3);
22
23 add_filter('presto_player/block/default_attributes', [$this, 'addVideoAttributes']);
24 add_filter('presto_player/templates/player_tag', [$this, 'addPlayerTags']);
25 });
26 }
27
28 /**
29 * Add tags to player
30 *
31 * @param array $data
32 * @return void
33 */
34 public function addPlayerTags($data)
35 {
36 if (empty($data['cookieKey']) || empty($data['videoProgress'])) {
37 return;
38 }
39
40 ob_start();
41 ?>
42 data-video-cookie-key="<?php echo $data['cookieKey']; ?>"
43 data-video-progression="<?php echo $data['videoProgress']; ?>"
44 data-video-provider="presto"
45 <?php
46 echo ob_get_clean();
47 }
48
49 /**
50 * Add attributes to video for video progression
51 *
52 * @param array $attributes
53 * @return array
54 */
55 public function addVideoAttributes($attributes)
56 {
57 global $post;
58
59 // bail if not a learndash post type
60 if (!self::isLearnDashPost($post)) {
61 return $attributes;
62 }
63
64 $logic_video = false;
65 $video_id = !empty($attributes['id']) ? '(presto-' . $attributes['id'] . ')' : '(presto)';
66 $video_completed = $this->stepIsCompleted($post->ID);
67
68 if ($video_completed) {
69 $logic_video = false;
70 } else {
71 // get lesson settings
72 $step_settings = learndash_get_setting($post);
73
74 if ('BEFORE' === $step_settings['lesson_video_shown']) {
75 $logic_video = true;
76
77 $topics = learndash_get_topic_list($post->ID);
78 if (!empty($topics)) {
79 $progress = learndash_get_course_progress(null, $topics[0]->ID);
80 if (!empty($progress)) {
81 $topics_completed = 0;
82 foreach ($progress['posts'] as $topic) {
83 if ((int) 1 === (int) $topic->completed) {
84 ++$topics_completed;
85 break;
86 }
87 }
88
89 if (!empty($topics_completed)) {
90 $logic_video = false;
91 }
92 }
93 }
94 }
95 }
96
97 if (true === $logic_video) {
98 $logic_video_str = 'true';
99 } else {
100 $logic_video_str = 'false';
101 }
102
103 $attributes['cookieKey'] = $this->buildVideoCookieKey($video_id);
104 $attributes['videoProgress'] = $logic_video_str;
105
106 return $attributes;
107 }
108
109 /**
110 * Build unique video progress cookie key. This is used to track the video state
111 * in the user's browser.
112 *
113 * @param integer $attach_id attachment ID of the video
114 *
115 * @return string $cookie_key.
116 */
117 public function buildVideoCookieKey($attach_id = '')
118 {
119 $cookie_key = '';
120 $cookie_key = $this->getNonceSlug();
121
122 if ((isset($attach_id)) && (!empty($attach_id))) {
123 $lesson_video_url = trim($attach_id);
124 $lesson_video_url = html_entity_decode($lesson_video_url);
125
126 $cookie_key .= '_' . $lesson_video_url;
127 }
128 $cookie_key = 'learndash-video-progress-' . md5($cookie_key);
129
130 return $cookie_key;
131 }
132
133 /**
134 * Utility function to get the nonce slug.
135 *
136 * @since 3.2.3
137 */
138 protected function getNonceSlug()
139 {
140 $post_id = get_the_ID();
141 $step_id = $post_id;
142 $course_id = learndash_get_course_id($post_id);
143 $user_id = (int) get_current_user_id();
144
145 return 'learndash_video_' . $user_id . '_' . $course_id . '_' . $step_id;
146 }
147
148 /**
149 * Dynamically replace (presto) with (presto-$video_id)
150 *
151 * @param mixed $value
152 * @param integer $object_id
153 * @param string $meta_key
154 *
155 * @return mixed
156 */
157 public function filterVideoURL($value, $object_id, $meta_key)
158 {
159 // prevent recursion
160 remove_filter(current_filter(), __FUNCTION__);
161
162 // only learndash meta
163 if (!in_array($meta_key, ['_sfwd-topic', '_sfwd-lessons'])) {
164 return $value;
165 }
166
167 // get meta
168 $meta_cache = wp_cache_get($object_id, 'post_meta');
169 if (!$meta_cache) {
170 $meta_cache = update_meta_cache('post', array($object_id));
171 if (isset($meta_cache[$object_id])) {
172 $meta_cache = $meta_cache[$object_id];
173 } else {
174 $meta_cache = null;
175 }
176 }
177 if (!$meta_key) {
178 return $meta_cache;
179 }
180
181 if (isset($meta_cache[$meta_key])) {
182 $saved = maybe_unserialize($meta_cache[$meta_key][0]);
183
184 $key = array_key_exists('sfwd-lessons_lesson_video_url', $saved) ? 'sfwd-lessons_lesson_video_url' : '';
185 $key = array_key_exists('sfwd-topic_lesson_video_url', $saved) ? 'sfwd-topic_lesson_video_url' : $key;
186
187 if (!$key) {
188 return $value;
189 }
190
191 if (strpos($saved[$key], '(presto') === false) {
192 return $value;
193 }
194
195 $post_model = new Post(get_post($object_id));
196 $video_id = $post_model->findVideoId();
197 $saved[$key] = "(presto-$video_id)";
198 $meta_cache[$meta_key][0] = $saved;
199 return $meta_cache[$meta_key];
200 }
201
202 return $value;
203 }
204
205 /**
206 * Pass javascript options to presto player
207 *
208 * @param array $options
209 * @return void
210 */
211 public function jsOptions($options)
212 {
213 if (self::isEnabled()) {
214 global $post;
215 $settings = learndash_get_setting($post);
216
217 if (!empty($settings['lesson_video_auto_complete_delay'])) {
218 $post_type_obj = get_post_type_object($post->post_type);
219 $settings['videos_auto_complete_delay_message'] = sprintf(
220 // translators: placeholders: 1. Lesson or Topic label, 2. span for counter.
221 wp_kses_post(_x('<p class="ld-video-delay-message">%1$s will auto complete in %2$s seconds</p>', 'placeholders: 1. Lesson or Topic label, 2. span for counter', 'learndash')),
222 $post_type_obj->labels->singular_name,
223 '<span class="time-countdown">' . $settings['lesson_video_auto_complete_delay'] . '</span>'
224 );
225 }
226
227 $options['learndash'] = $settings;
228 }
229
230 return $options;
231 }
232
233 /**
234 * Is LearnDash enabled?
235 *
236 * @return boolean
237 */
238 public static function isEnabled()
239 {
240 return defined('LEARNDASH_VERSION');
241 }
242
243 /**
244 * Should the video load on the learndash page
245 *
246 * @return boolean
247 */
248 public static function shouldVideoLoad()
249 {
250 global $post;
251
252 // bail if not a learndash post type
253 if (!self::isLearnDashPost($post)) {
254 return true;
255 }
256
257 // step is completed, load video
258 if (self::stepIsCompleted($post)) {
259 return true;
260 }
261
262 // check if lesson steps are complete
263 return self::areStepsComplete($post);
264 }
265
266 /**
267 * Is this a learndash post?
268 *
269 * @param \WP_Post $post
270 * @return boolean
271 */
272 public static function isLearnDashPost($post)
273 {
274 if (!$post) {
275 return false;
276 }
277
278 return in_array($post->post_type, ['sfwd-lessons', 'sfwd-topic']);
279 }
280
281 /**
282 * Is the learndash step completed
283 *
284 * @param \WP_Post $post
285 * @return bool
286 */
287 public static function stepIsCompleted($post)
288 {
289 if (!function_exists('learndash_get_course_progress')) {
290 return true;
291 }
292
293 global $post;
294 $progress = learndash_get_course_progress(null, $post->ID);
295 return (!empty($progress['this'])) && ($progress['this'] instanceof \WP_Post) && (true === (bool) $progress['this']->completed);
296 }
297
298 /**
299 * Are lesson/topic steps complete?
300 *
301 * @param \WP_Post $post
302 * @return boolean
303 */
304 public static function areStepsComplete(\WP_Post $post)
305 {
306 // get lesson settings
307 $lesson_settings = learndash_get_setting($post);
308
309 // we're only concerned with "AFTER"
310 if ('AFTER' !== $lesson_settings['lesson_video_shown']) {
311 return true;
312 }
313
314 // if this is a lesson, check if topics are completed
315 if ($post->post_type === 'sfwd-lessons') {
316 if (!learndash_lesson_topics_completed($post->ID)) {
317 return false;
318 }
319 }
320
321 // quizes must also be completed
322 return self::areQuizzesCompleted($post);
323 }
324
325 /**
326 * Are quizzes completed?
327 *
328 * @param \WP_Post $post
329 * @return boolean
330 */
331 public static function areQuizzesCompleted(\WP_Post $post)
332 {
333 // quizes must also be completed
334 $quizzes_completed = true;
335 $lesson_quizzes_list = learndash_get_lesson_quiz_list($post->ID);
336 if (!empty($lesson_quizzes_list)) {
337 foreach ($lesson_quizzes_list as $quiz) {
338 if ('completed' !== $quiz['status']) {
339 $quizzes_completed = false;
340 break;
341 }
342 }
343 }
344 return (bool) $quizzes_completed;
345 }
346
347 /**
348 * Add our video provider to learndash
349 *
350 * @param array $video_data
351 * @param array $step_settings
352 * @return array
353 */
354 public function addProvider($video_data, $step_settings)
355 {
356 if (strpos($step_settings['lesson_video_url'], '(presto') !== false) {
357 return 'presto';
358 }
359 return $video_data;
360 }
361
362 /**
363 * Adds our setting to the lesson settings page
364 *
365 * @param array $settings
366 * @param string $meta_box_key
367 * @return array
368 */
369 public function settingsFields($settings, $meta_box_key)
370 {
371 // if it's not one of these settings pages, bail
372 if (!in_array($meta_box_key, ['learndash-lesson-display-content-settings', 'learndash-topic-display-content-settings'])) {
373 return $settings;
374 }
375
376 $setting = [
377 'lesson_use_presto_video' => [
378 'name' => 'lesson_use_presto_video',
379 'label' => esc_html__('Use Presto Video', 'learndash'),
380 'type' => 'checkbox-switch',
381 'value' => !empty($settings['use_presto_video']) ? $settings['use_presto_video'] : '',
382 'help_text' => esc_html__('Use the Presto Player video in your post content for video progression.', 'learndash'),
383 'default' => '',
384 'options' => array(
385 'on' => esc_html__('The presto video in this post will be used for video progression.', 'learndash'),
386 '' => '',
387 ),
388 'parent_setting' => 'lesson_video_enabled',
389 ]
390 ];
391
392 // insert before video url
393 $settings = Utility::arrayInsert($settings, $setting, 'lesson_video_url', 'before');
394
395 return $settings;
396 }
397 }
398