learn-dash-resume.php 15.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
<?php

namespace uncanny_learndash_toolkit;

if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Class LearnDashResume
 * @package uncanny_learndash_toolkit
 */
class LearnDashResume extends Config implements RequiredFunctions {

	/**
	 * @var string
	 */
	static $topic_type = 'sfwd-topic';


	/**
	 * Class constructor
	 */
	public function __construct() {
		add_action( 'plugins_loaded', array( __CLASS__, 'run_frontend_hooks' ) );
	}

	/*
	 * Initialize frontend actions and filters
	 */
	/**
	 *
	 */
	public static function run_frontend_hooks() {

		if ( true === self::dependants_exist() ) {
			add_action( 'wp', [ __CLASS__, 'disable_rel_link' ] );
			add_action( 'wp_head', array( __CLASS__, 'find_last_known_learndash_page' ) );
			add_shortcode( 'uo-learndash-resume', array( __CLASS__, 'learndash_resume' ) );
			add_shortcode( 'uo_learndash_resume', array( __CLASS__, 'learndash_resume' ) );
			add_shortcode( 'uo_learndash_resume_link', array( __CLASS__, 'learndash_resume_link' ) );
			add_shortcode( 'uo_course_resume', array( __CLASS__, 'uo_course_resume' ) );
			add_action( 'init', [ __CLASS__, 'reset_resume_data' ] );
			add_action( 'learndash_delete_user_data', [ __CLASS__, 'reset_resume_data_by_learndash' ] );
			add_action( 'learndash_update_user_activity', [ __CLASS__, 'reset_resume_data_by_learndash_activity' ] );
		}

	}

	/**
	 * Does the plugin rely on another function or plugin
	 *
	 * return boolean || string Return either true or name of function or plugin
	 */
	public static function dependants_exist() {
		global $learndash_post_types;
		if ( ! isset( $learndash_post_types ) ) {
			return 'Plugin: LearnDash';
		}

		return true;
	}

	/**
	 * Description of class in Admin View
	 *
	 * @return array
	 */
	public static function get_details() {
		$module_id         = 'resume-button';
		$class_title       = esc_html__( 'Resume Button', 'uncanny-learndash-toolkit' );
		$kb_link           = 'https://www.uncannyowl.com/knowledge-base/learndash-resume/';
		$class_description = esc_html__( 'Inserts a button that allows learners to return to the course, lesson or topic they last visited.', 'uncanny-learndash-toolkit' );
		$class_icon        = '<i class="uo_icon_fa fa fa-refresh"></i>';
		$category          = 'learndash';
		$type              = 'free';

		return array(
			'id'               => $module_id,
			'title'            => $class_title,
			'type'             => $type,
			'category'         => $category,
			'kb_link'          => $kb_link,
			'description'      => $class_description,
			'dependants_exist' => self::dependants_exist(),
			'settings'         => self::get_class_settings( $class_title ),
			'icon'             => $class_icon,
		);

	}

	/**
	 * HTML for modal to create settings
	 *
	 * @static
	 *
	 * @param $class_title
	 *
	 * @return string
	 */
	public static function get_class_settings( $class_title ) {

		// Create options
		$options = array(

			array(
				'type'        => 'text',
				'label'       => esc_html__( 'Resume Button Text', 'uncanny-learndash-toolkit' ),
				'option_name' => 'learn-dash-resume-button-text',
			),
			array(
				'type'        => 'checkbox',
				'label'       => esc_html__( 'Show Name of Last Course / Lesson / Topic', 'uncanny-learndash-toolkit' ),
				'option_name' => 'learn-dash-resume-show-name',
			),

		);

		// Build html
		$html = self::settings_output(
			array(
				'class'   => __CLASS__,
				'title'   => $class_title,
				'options' => $options,
			) );

		return $html;
	}

	/**
	 *Adding wp_head action so that we capture the type of post / page user is on and add that to wordpress options table.
	 *
	 * @static
	 */
	public static function find_last_known_learndash_page() {

		$user = wp_get_current_user();

		if ( is_user_logged_in() ) {

			/* declare $post as global so we get the post->ID of the current page / post */
			global $post;

			// Sanity check page doesn't exist
			if ( ! is_object( $post ) ) {
				return;
			}

			/* Limit the plugin to LearnDash specific post types */
			$learn_dash_post_types = apply_filters(
				'last_known_learndash_post_types',
				array(
					'sfwd-courses',
					'sfwd-lessons',
					'sfwd-topic',
					'sfwd-quiz',
					'sfwd-certificates',
					'sfwd-assignment',
				)
			);

			$step_id        = $post->ID;
			$step_course_id = learndash_get_course_id( $step_id );

			if ( empty( $step_course_id ) ) {
				$step_course_id = 0;
			}

			if ( is_singular( $learn_dash_post_types ) ) {
				update_user_meta( $user->ID, 'learndash_last_known_page', $step_id . ',' . $step_course_id );
				if ( 'sfwd-courses' !== $post->post_type ) {
					update_user_meta( $user->ID, 'learndash_last_known_course_' . $step_course_id, $step_id );
				}
			}

		}
	}

	/**
	 * @param $atts
	 *
	 * @return string
	 */
	public static function uo_course_resume( $atts ) {
		$atts = shortcode_atts( array(
			'course_id' => '',
			'show_name' => 'yes',
		), $atts, 'uo_course_resume' );

		if ( is_user_logged_in() ) {
			if ( ! empty( $atts['course_id'] ) ) {
				$user           = wp_get_current_user();
				$step_course_id = $atts['course_id'];
				$course         = get_post( $step_course_id );

				if ( isset( $course ) && 'sfwd-courses' === $course->post_type ) {
					$last_know_step = get_user_meta( $user->ID, 'learndash_last_known_course_' . $step_course_id, true );

					// User has not hit a LD module yet
					if ( empty( $last_know_step ) ) {

						return '';
					}

					//$step_course_id = 0;
					// Sanity Check
					if ( absint( $last_know_step ) ) {
						$step_id = $last_know_step;
					} else {
						return '';
					}


					$last_know_post_object = get_post( $step_id );

					// Make sure the post exists and that the user hit a page that was a post
					// if $last_know_page_id returns '' then get post will return current pages post object
					// so we need to make sure first that the $last_know_page_id is returning something and
					// that the something is a valid post
					if ( null !== $last_know_post_object ) {

						$post_type        = $last_know_post_object->post_type; // getting post_type of last page.
						$label            = get_post_type_object( $post_type ); // getting Labels of the post type.
						$title            = $last_know_post_object->post_title;
						$resume_link_text = __( 'RESUME', 'uncanny-learndash-toolkit' );

						// Resume Link Text
						$link_text = self::get_settings_value( 'learn-dash-resume-button-text', __CLASS__ );
						$show_name = self::get_settings_value( 'learn-dash-resume-show-name', __CLASS__ );

						if ( strlen( trim( $link_text ) ) ) {
							$resume_link_text = __( $link_text, 'uncanny-learndash-toolkit' );
						} else {
							$resume_link_text = __( 'Resume', 'uncanny-learndash-toolkit' );
						}

						$resume_link_text = apply_filters( 'learndash_resume_link_text', $resume_link_text );

						$css_classes = apply_filters( 'learndash_resume_css_classes', 'learndash-resume-button' );

						ob_start();

						if ( function_exists( 'learndash_get_step_permalink' ) ) {
							$permalink = learndash_get_step_permalink( $step_id, $step_course_id );
						} else {
							$permalink = get_permalink( $step_id );
						}
						$permalink = apply_filters( 'uo_resume_button_permalink', $permalink, $step_id, $step_course_id );

						printf(
							'<a href="%s" title="%s" class="%s"><input type="submit" value="%s" class=""></a>',
							$permalink,
							esc_attr(
								sprintf(
									esc_html_x( 'Resume %s: %s', 'LMS shortcode Resume link title "Resume post_type_name: Post_title ', 'uncanny-learndash-toolkit' ),
									$label->labels->singular_name,
									$title
								)
							),
							esc_attr( $css_classes ),
							esc_attr( $resume_link_text )
						);

						if ( $show_name === 'on' && 'yes' === $atts['show_name'] ) {
							printf(
								'<div class="resume-item-name">%s</div>',
								$title
							);
						}

						$resume_link = ob_get_contents();
						ob_end_clean();

						return $resume_link;
					}
				}
			}
		}

		return '';
	}

	/**
	 *Adding [uo-learndash-resume] shortcode functionality which can be used anywhere on the website to take user back to last known page of LearnDash.
	 *
	 * @static
	 * @return string
	 */
	public static function learndash_resume() {
		return self::resume_function();
	}

	/**
	 * @param $atts
	 *
	 * @return false|string
	 */
	public static function learndash_resume_link( $atts ) {
		$atts = shortcode_atts( array(
			'url_only' => 'no',
		), $atts, 'uo_learndash_resume_link' );

		return self::resume_function( true, $atts['url_only'] );
	}

	/**
	 * @param bool $link
	 * @param string $url
	 *
	 * @return false|string
	 */
	public static function resume_function( $link = false, $url = 'no' ) {
		$user = wp_get_current_user();

		if ( is_user_logged_in() ) {

			$last_know_step = get_user_meta( $user->ID, 'learndash_last_known_page', true );

			// User has not hit a LD module yet
			if ( empty( $last_know_step ) ) {

				return '';
			}

			$step_course_id = 0;

			if ( false !== strpos( $last_know_step, ',' ) ) {
				$last_know_step = explode( ',', $last_know_step );
				$step_id        = $last_know_step[0];
				$step_course_id = $last_know_step[1];
			} else {

				// Sanity Check
				if ( absint( $last_know_step ) ) {
					$step_id = $last_know_step;
				} else {
					return '';
				}

			}

			$ld_course_id = learndash_get_course_id( $step_id );

			if ( true === apply_filters( 'uo_resume_button_do_not_show_for_completed_course', false, $ld_course_id ) ) {
				if ( learndash_course_completed( $user->ID, $ld_course_id ) ) {
					return '';
				}
			}

			$last_know_post_object = get_post( $step_id );

			// Check if current step_id is same as current post then no need to show this button.
			// Someone may use this shortcode on any LearnDash post page which will create a loop.
			global $post;
			if ( (int) $post->ID === (int) $step_id ) {
				$learn_dash_post_types = apply_filters(
					'last_known_learndash_post_types',
					array(
						'sfwd-courses',
						'sfwd-lessons',
						'sfwd-topic',
						'sfwd-quiz',
						'sfwd-certificates',
						'sfwd-assignment',
					)
				);
				if ( is_singular( $learn_dash_post_types ) ) {
					return '';
				}
			}


			// Make sure the post exists and that the user hit a page that was a post
			// if $last_know_page_id returns '' then get post will return current pages post object
			// so we need to make sure first that the $last_know_page_id is returning something and
			// that the something is a valid post
			if ( null !== $last_know_post_object ) {

				$post_type        = $last_know_post_object->post_type; // getting post_type of last page.
				$label            = get_post_type_object( $post_type ); // getting Labels of the post type.
				$title            = $last_know_post_object->post_title;
				$resume_link_text = __( 'RESUME', 'uncanny-learndash-toolkit' );

				// Resume Link Text
				$link_text = self::get_settings_value( 'learn-dash-resume-button-text', __CLASS__ );
				$show_name = self::get_settings_value( 'learn-dash-resume-show-name', __CLASS__ );

				if ( strlen( trim( $link_text ) ) ) {
					$resume_link_text = __( $link_text, 'uncanny-learndash-toolkit' );
				} else {
					$resume_link_text = __( 'Resume', 'uncanny-learndash-toolkit' );
				}

				$resume_link_text = apply_filters( 'learndash_resume_link_text', $resume_link_text );

				$css_classes = apply_filters( 'learndash_resume_css_classes', 'learndash-resume-button' );

				if ( function_exists( 'learndash_get_step_permalink' ) ) {
					$permalink = learndash_get_step_permalink( $step_id, $step_course_id );
				} else {
					$permalink = get_permalink( $step_id );
				}

				$permalink = apply_filters( 'uo_resume_button_permalink', $permalink, $step_id, $step_course_id );
				if ( 'yes' === (string) $url ) {
					return $permalink;
				}

				ob_start();
				if ( $link ) {
					printf(
						'<a href="%s" title="%s" class="%s">%s</a>',
						$permalink,
						esc_attr(
							sprintf(
								esc_html_x( 'Resume %s: %s', 'LMS shortcode Resume link title "Resume post_type_name: Post_title ', 'uncanny-learndash-toolkit' ),
								$label->labels->singular_name,
								$title
							)
						),
						esc_attr( $css_classes ),
						esc_attr( $resume_link_text )
					);
				} else {
					printf(
						'<a href="%s" title="%s" class="%s"><input type="submit" value="%s" class=""></a>',
						$permalink,
						esc_attr(
							sprintf(
								esc_html_x( 'Resume %s: %s', 'LMS shortcode Resume link title "Resume post_type_name: Post_title ', 'uncanny-learndash-toolkit' ),
								$label->labels->singular_name,
								$title
							)
						),
						esc_attr( $css_classes ),
						esc_attr( $resume_link_text )
					);
				}

				if ( $show_name === 'on' ) {
					printf(
						'<div class="resume-item-name">%s</div>',
						$title
					);
				}
				$resume_link = ob_get_contents();
				ob_end_clean();

				return $resume_link;
			}

		}

		return '';
	}

	/**
	 * prevent wordpress from adding <link rel="next" />
	 */
	public static function disable_rel_link() {
		global $post;
		$learndash_post_types = [ 'sfwd-courses', 'sfwd-lessons', 'sfwd-topic', 'sfwd-quiz' ];
		if ( isset( $post->post_type ) && in_array( $post->post_type, $learndash_post_types ) ) {
			remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
		}
	}

	/**
	 * Using Pro reset form for resume reset
	 */
	public static function reset_resume_data() {
		if ( isset( $_POST['learndash_reset_course_nonce'] ) && wp_verify_nonce( $_POST['learndash_reset_course_nonce'], 'learndash_reset_course' ) ) {
			if ( is_user_logged_in() ) {
				$user           = wp_get_current_user();
				$course_id      = intval( $_POST['ld_reset_course_id'] );
				$last_know_step = get_user_meta( $user->ID, 'learndash_last_known_page', true );
				if ( false !== strpos( $last_know_step, ',' ) ) {
					$last_know_step = explode( ',', $last_know_step );
					$step_id        = $last_know_step[0];
					$step_course_id = $last_know_step[1];
					if ( $step_course_id == $course_id ) {
						delete_user_meta( $user->ID, 'learndash_last_known_page' );
						delete_user_meta( $user->ID, 'learndash_last_known_course_' . $step_course_id );
					}
				}
			}
		}
	}

	/**
	 * By using Learndash reset user data from profile.
	 */
	public static function reset_resume_data_by_learndash( $user_id ) {
		$last_know_step = get_user_meta( $user_id, 'learndash_last_known_page', true );
		if ( empty( $last_know_step ) ) {
			return;
		}

		if ( false !== strpos( $last_know_step, ',' ) ) {
			$last_know_step = explode( ',', $last_know_step );
			$step_id        = $last_know_step[0];
			$step_course_id = $last_know_step[1];
			delete_user_meta( $user_id, 'learndash_last_known_course_' . $step_course_id );
		}
		delete_user_meta( $user_id, 'learndash_last_known_page' );

	}

	/**
	 * By using Learndash reset user data from profile.
	 */
	public static function reset_resume_data_by_learndash_activity( $args ) {
		if ( isset( $args['activity_type'] ) && $args['activity_type'] === 'course' && $args['activity_action'] == 'update' ) {
			if ( ! empty( $args['activity_id'] ) && ! empty( $args['activity_meta'] ) && isset( $args['activity_meta']['steps_completed'] ) && $args['activity_meta']['steps_completed'] == 0 ) {
				$user_id        = $args['user_id'];
				$course_id      = $args['course_id'];
				$last_know_step = get_user_meta( $user_id, 'learndash_last_known_page', true );
				if ( false !== strpos( $last_know_step, ',' ) ) {
					$last_know_step = explode( ',', $last_know_step );
					$step_id        = $last_know_step[0];
					$step_course_id = $last_know_step[1];
					if ( $step_course_id == $course_id ) {
						delete_user_meta( $user_id, 'learndash_last_known_page' );
						delete_user_meta( $user_id, 'learndash_last_known_course_' . $step_course_id );
					}
				}
			}
		}
	}

}