Browse: Home / Snippets /

Remove video progression cookie on step completion

Contents


Snippet #

Note: This is only required if you are using a version older than 3.2.3

<pre class="wp-block-syntaxhighlighter-code">add_action( 'learndash_topic_completed', function( $args = array() ) {
	learndash_remove_video_cookie( $args['topic']->ID, $args['course']->ID, $args['user']->ID );
}, 30, 1 );


add_action( 'learndash_lesson_completed', function( $args = array() ) {
	learndash_remove_video_cookie( $args['lesson']->ID, $args['course']->ID, $args['user']->ID );
}, 30, 1 );

function learndash_remove_video_cookie( $step_id = 0, $course_id = 0, $user_id = 0 ) {
	if ( ( ! empty( $step_id ) ) && ( ! empty( $course_id ) ) && ( ! empty( $user_id ) ) ) {
		$step_settings = learndash_get_setting( $step_id );
		if ( ( isset( $step_settings['lesson_video_enabled'] ) ) && ( 'on' === $step_settings['lesson_video_enabled'] ) ) {
			if ( ( isset( $step_settings['lesson_video_url'] ) ) && ( ! empty( $step_settings['lesson_video_url'] ) ) ) {
				$step_settings['lesson_video_url'] = trim( $step_settings['lesson_video_url'] );
				$step_settings['lesson_video_url'] = html_entity_decode( $step_settings['lesson_video_url'] );

				$video_data = array();

				if ( ( strpos( $step_settings['lesson_video_url'], 'youtu.be' ) !== false ) || ( strpos( $step_settings['lesson_video_url'], 'youtube.com' ) !== false ) ) {
					$video_data['videos_found_provider'] = 'youtube';
				} elseif ( strpos( $step_settings['lesson_video_url'], 'vimeo.com' ) !== false ) {
					$video_data['videos_found_provider'] = 'vimeo';
				} elseif ( ( strpos( $step_settings['lesson_video_url'], 'wistia.com' ) !== false ) || ( strpos( $step_settings['lesson_video_url'], 'wistia.net' ) !== false ) ) {
					$video_data['videos_found_provider'] = 'wistia';
				} elseif ( strpos( $step_settings['lesson_video_url'], 'amazonaws.com' ) !== false ) {
					$video_data['videos_found_provider'] = 'local';
				} elseif ( ( strpos( $step_settings['lesson_video_url'], 'vooplayer' ) !== false ) || ( strpos( $step_settings['lesson_video_url'], 'spotlightr.com' ) !== false ) ) {
					$video_data['videos_found_provider'] = 'vooplayer';
				} elseif ( strpos( $step_settings['lesson_video_url'], trailingslashit( get_home_url() ) ) !== false ) {
					$video_data['videos_found_provider'] = 'local';
				}

				if ( ( substr( $step_settings['lesson_video_url'], 0, strlen( 'http://' ) ) == 'http://' ) || ( substr( $step_settings['lesson_video_url'], 0, strlen( 'https://' ) ) == 'https://' ) ) {
					if ( 'local' === $video_data['videos_found_provider'] ) {
						$video_data['videos_found_type'] = 'video_shortcode';
						$step_settings['lesson_video_url']          = '[video src="' . $step_settings['lesson_video_url'] . '"][/video]';

					} elseif ( ( 'youtube' === $video_data['videos_found_provider'] ) || ( 'vimeo' === $video_data['videos_found_provider'] ) ) {
						$video_data['videos_found_type'] = 'embed_shortcode';
						$step_settings['lesson_video_url']          = '<a href="http://'%20.%20$step_settings%5B'lesson_video_url'%5D%20.%20'">' . $step_settings['lesson_video_url'] . '</a>';
					} elseif ( 'wistia' === $video_data['videos_found_provider'] ) {
						$video_data['videos_found_type'] = 'embed_shortcode';
						$step_settings['lesson_video_url']          = '<a href="http://'%20.%20$step_settings%5B'lesson_video_url'%5D%20.%20'">' . $step_settings['lesson_video_url'] . '</a>';
					}
				} elseif ( substr( $step_settings['lesson_video_url'], 0, strlen( ' = 'embed_shortcode';
				} elseif ( substr( $step_settings['lesson_video_url'], 0, strlen( '[video' ) ) == '[video' ) {
					$video_data['videos_found_type'] = 'video_shortcode';
				} elseif ( substr( $step_settings['lesson_video_url'], 0, strlen( '<iframe' ) ) == '<iframe' ) {
					$video_data['videos_found_type'] = 'iframe';
				} else {
					if ( 'vooplayer' === $video_data['videos_found_provider'] ) {
						if ( substr( $step_settings['lesson_video_url'], 0, strlen( '[vooplayer' ) ) == '[vooplayer' ) {
							$video_data['videos_found_type'] = 'vooplayer_shortcode';
						} else {
							$video_data['videos_found_type'] = 'iframe';
						}
					}
				}

				$video_cookie_key = $user_id . '_' . $course_id . '_' . $step_id;
				$video_cookie_key .= '_' . $step_settings['lesson_video_url'];
				$video_cookie_key = 'learndash-video-progress-' . md5( $video_cookie_key );

				$video_track_domain = '';
				if ( defined( 'COOKIE_DOMAIN' ) ) {
					$video_track_domain = COOKIE_DOMAIN;
				}

				$video_track_path = '';
				if ( ( is_multisite() ) && ( defined( 'SITECOOKIEPATH' ) ) ) {
					$video_track_path = SITECOOKIEPATH;
				} elseif ( defined( 'COOKIEPATH' ) ) {
					$video_track_path = COOKIEPATH;
				}

				// empty value and expiration one hour before
				$res = setcookie( $video_cookie_key, '', time() - 3600, $video_track_path, $video_track_domain );
			}
		}
	}
}</pre>