ld-course-functions.php 50.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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
<?php
/**
 * Course Functions
 *
 * @since 2.1.0
 *
 * @package LearnDash\Course
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// cspell:ignore prerequities .

/**
 * Gets the course ID for a resource.
 *
 * Determine the type of ID being passed. Should be the ID of
 * anything that belongs to a course (Lesson, Topic, Quiz, etc)
 *
 * @since 2.1.0
 * @since 2.5.0 Added the `$bypass_cb` parameter.
 *
 * @param  WP_Post|int|null $id        Optional. ID of the resource. Default null.
 * @param  boolean          $bypass_cb Optional. If true will bypass course_builder logic. Default false.
 *
 * @return int|bool ID of the course.
 */
function learndash_get_course_id( $id = null, $bypass_cb = false ) {
	if ( is_object( $id ) && $id->ID ) {
		$p  = $id;
		$id = $p->ID;
	} elseif ( is_numeric( $id ) ) {
		$p = get_post( $id );
	}

	if ( empty( $id ) ) {
		if ( ! defined( 'REST_REQUEST' ) ) {
			if ( is_admin() ) {
				global $parent_file, $post_type, $pagenow;
				if ( ( ! in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) ) || ( ! in_array( $post_type, learndash_get_post_types( 'course' ), true ) ) ) {
					return false;
				}
			} elseif ( ! is_single() || is_home() ) {
				return false;
			}
		}

		$post = get_post( get_the_id() );
		if ( ( $post ) && ( $post instanceof WP_Post ) ) {
			$id = $post->ID;
			$p  = $post;
		} else {
			return false;
		}
	}

	if ( empty( $p->ID ) ) {
		return 0;
	}

	if ( learndash_get_post_type_slug( 'course' ) === $p->post_type ) {
		return $p->ID;
	}

	// Somewhat a kludge. Here we try and assume the course_id being handled.
	if ( ( learndash_is_course_shared_steps_enabled() ) && ( false === $bypass_cb ) ) {
		if ( ! is_admin() ) {
			$course_slug = get_query_var( 'sfwd-courses' );
			if ( ! empty( $course_slug ) ) {
				$course_post = learndash_get_page_by_path( $course_slug, 'sfwd-courses' );
				if ( ( $course_post ) && ( $course_post instanceof WP_Post ) ) {
					return $course_post->ID;
				}
			}
		}

		if ( ( isset( $_GET['course_id'] ) ) && ( ! empty( $_GET['course_id'] ) ) ) {
			return intval( $_GET['course_id'] );
		} elseif ( ( isset( $_GET['course'] ) ) && ( ! empty( $_GET['course'] ) ) ) {
			return intval( $_GET['course'] );
		} elseif ( ( isset( $_POST['course_id'] ) ) && ( ! empty( $_POST['course_id'] ) ) ) {
			return intval( $_POST['course_id'] );
		} elseif ( ( isset( $_POST['course'] ) ) && ( ! empty( $_POST['course'] ) ) ) {
			return intval( $_POST['course'] );
		} elseif ( ( isset( $_GET['post'] ) ) && ( ! empty( $_GET['post'] ) ) ) {
			if ( get_post_type( intval( $_GET['post'] ) ) == 'sfwd-courses' ) {
				return intval( $_GET['post'] );
			}
		}
	}

	if ( learndash_get_post_type_slug( LDLMS_Post_Types::EXAM ) === $p->post_type ) {
		return (int) get_post_meta( intval( $id ), 'exam_challenge_course_show', true );
	}

	return (int) get_post_meta( $id, 'course_id', true );
}

/**
 * Gets the lesson ID of a resource.
 *
 * @global WP_Post $post Global post object.
 *
 * @since 2.1.0
 *
 * @param int|null $post_id   Optional. ID of the resource. Default null.
 * @param int|null $course_id Optional. ID of the course. Default null.
 *
 * @return string Lesson ID.
 */
function learndash_get_lesson_id( $post_id = null, $course_id = null ) {
	$post_id   = absint( $post_id );
	$course_id = absint( $course_id );

	if ( empty( $post_id ) ) {
		$the_id = get_the_id();
		if ( empty( $the_id ) ) {
			return false;
		}
		$post = get_post( $the_id );
	} else {
		$post = get_post( $post_id );
	}

	if ( ( ! $post ) || ( ! is_a( $post, 'WP_Post' ) ) || ( ! in_array( $post->post_type, learndash_get_post_types( 'course' ), true ) ) ) {
		return false;
	}

	if ( learndash_get_post_type_slug( 'lesson' ) === $post->post_type ) {
		return $post->ID;
	}

	if ( learndash_is_course_shared_steps_enabled() ) {
		$lesson_slug = get_query_var( learndash_get_post_type_slug( 'lesson' ) );
		if ( ! empty( $lesson_slug ) ) {
			$lesson_post = learndash_get_page_by_path( $lesson_slug, learndash_get_post_type_slug( 'lesson' ) );
			if ( ( $lesson_post ) && ( is_a( $lesson_post, 'WP_Post' ) ) ) {
				return $lesson_post->ID;
			}
		} else {
			if ( empty( $course_id ) ) {
				$course_id = learndash_get_course_id( $post->ID );
			}

			if ( ! empty( $course_id ) ) {
				return learndash_course_get_single_parent_step( $course_id, $post->ID );
			}
		}
	} else {
		if ( in_array( $post->post_type, learndash_get_post_type_slug( array( 'topic', 'quiz' ) ), true ) ) {
			return get_post_meta( $post->ID, 'lesson_id', true );
		}
	}

	return '';
}

/**
 * Checks if the user's course prerequisites are completed for a given course.
 *
 * @since 3.2.0
 * @since 3.2.3 Added `$user_id` parameter.
 *
 * @param int $post_id Optional. The ID of the course. Default 0.
 * @param int $user_id Optional. The ID of the user. Default 0.
 *
 * @return boolean Returns true if the prerequisites are completed.
 */
function learndash_is_course_prerequities_completed( $post_id = 0, $user_id = 0 ) {
	$course_pre_complete = true;

	if ( empty( $user_id ) ) {
		if ( is_user_logged_in() ) {
			$user_id = get_current_user_id();
		} else {
			$user_id = 0;
		}
	}

	if ( ! empty( $post_id ) ) {
		$course_id = learndash_get_course_id( $post_id );
		if ( ( ! empty( $course_id ) ) && ( ! empty( $user_id ) ) && ( learndash_get_course_prerequisite_enabled( $course_id ) ) ) {
			$course_pre = learndash_get_course_prerequisites( $course_id, $user_id );
			if ( ! empty( $course_pre ) ) {
				$course_pre_compare = learndash_get_course_prerequisite_compare( $course_id );
				if ( 'ANY' === $course_pre_compare ) {
					$s_pre = array_search( true, $course_pre, true );
					if ( false !== $s_pre ) {
						$course_pre_complete = true;
					} else {
						$course_pre_complete = false;
					}
				} elseif ( 'ALL' === $course_pre_compare ) {
					$s_pre = array_search( false, $course_pre, true );
					if ( false === array_search( false, $course_pre, true ) ) {
						$course_pre_complete = true;
					} else {
						$course_pre_complete = false;
					}
				}
			}
		}
	}

	return $course_pre_complete;
}

/**
 * Gets the list of course prerequisites and its status for a course.
 *
 * @since 2.4.0
 * @since 3.2.3 Added `$user_id` parameter.
 *
 * @param int $post_id Optional. The ID of the course. Default 0.
 * @param int $user_id Optional. The ID of the user. Default 0.
 *
 * @return array An array of course prerequisites.
 */
function learndash_get_course_prerequisites( $post_id = 0, $user_id = 0 ) {
	$courses_status_array = array();

	if ( empty( $user_id ) ) {
		if ( is_user_logged_in() ) {
			$user_id = get_current_user_id();
		} else {
			$user_id = 0;
		}
	}

	if ( ! empty( $post_id ) ) {
		$course_id = learndash_get_course_id( $post_id );
		if ( ( ! empty( $course_id ) ) && ( ! empty( $user_id ) ) && ( learndash_get_course_prerequisite_enabled( $course_id ) ) ) {

			$course_pre = learndash_get_course_prerequisite( $course_id );
			if ( ! empty( $course_pre ) ) {
				$course_pre_compare = learndash_get_course_prerequisite_compare( $course_id );

				if ( is_string( $course_pre ) ) {
					$course_pre = array( $course_pre );
				}

				foreach ( $course_pre as $c_id ) {
					// Now check if the prerequisites course is completed by user or not.
					$course_status = learndash_course_status( $c_id, $user_id, true );
					if ( 'completed' === $course_status ) {
						$courses_status_array[ $c_id ] = true;
					} else {
						$courses_status_array[ $c_id ] = false;
					}
				}
			}
		}
	}
	return $courses_status_array;
}

/**
 * Gets the list of course prerequisites for a given course.
 *
 * @since 2.1.0
 *
 * @param int $course_id Optional. The ID if the course. Default 0.
 *
 * @return array An array of course prerequisite.
 */
function learndash_get_course_prerequisite( $course_id = 0 ) {
	$course_pre = array();

	if ( ! empty( $course_id ) ) {
		$transient_key        = 'learndash_course_pre_' . $course_id;
		$course_pre_transient = LDLMS_Transients::get( $transient_key );

		if ( false !== $course_pre_transient ) {
			$course_pre = (array) $course_pre_transient;
		} else {
			$course_pre = learndash_get_setting( $course_id, 'course_prerequisite' );
			if ( empty( $course_pre ) ) {
				$course_pre = array();
			}
			$course_pre = array_map( 'absint', $course_pre );
			$course_pre = array_diff( $course_pre, array( 0 ) ); // Removes zeros.
			if ( ! empty( $course_pre ) ) {
				$post_status = learndash_get_step_post_statuses();

				$course_pre_query_args = array(
					'post_type'   => learndash_get_post_type_slug( 'course' ),
					'nopaging'    => true,
					'post_status' => array_keys( $post_status ),
					'fields'      => 'ids',
					'post__in'    => $course_pre,
				);

				$course_pre_query = new WP_Query( $course_pre_query_args );
				if ( ( is_a( $course_pre_query, 'WP_Query' ) ) && ( property_exists( $course_pre_query, 'posts' ) ) && ( ! empty( $course_pre_query->posts ) ) ) {
					$course_pre = $course_pre_query->posts;
					LDLMS_Transients::set( $transient_key, $course_pre, HOUR_IN_SECONDS );
				}
			}
		}
	}

	return $course_pre;
}

/**
 * Sets new prerequisites for a course.
 *
 * @since 2.4.4
 *
 * @param int   $course_id  Optional. ID of the course. Default 0.
 * @param array $course_pre Optional. An array of course prerequisites. Default empty array.
 *
 * @return boolean Returns true if update was successful otherwise false.
 */
function learndash_set_course_prerequisite( $course_id = 0, $course_pre = array() ) {
	if ( ! empty( $course_id ) ) {
		if ( ( ! empty( $course_pre ) ) && ( is_array( $course_pre ) ) ) {
			$course_pre = array_unique( $course_pre );
		}

		$transient_key        = 'learndash_course_pre_' . $course_id;
		$course_pre_transient = LDLMS_Transients::delete( $transient_key );

		return learndash_update_setting( $course_id, 'course_prerequisite', $course_pre );
	}

	return false;
}

/**
 * Checks whether the prerequisites are enabled for a course.
 *
 * @since 2.4.0
 *
 * @param int $course_id The ID of the course.
 *
 * @return boolean Returns true if the prerequisites are enabled otherwise false.
 */
function learndash_get_course_prerequisite_enabled( $course_id ) {
	$course_pre_enabled = false;

	$course_id = learndash_get_course_id( $course_id );
	if ( ! empty( $course_id ) ) {
		$course_pre_enabled = learndash_get_setting( $course_id, 'course_prerequisite_enabled' );
		if ( 'on' === $course_pre_enabled ) {
			$course_pre_courses = learndash_get_setting( $course_id, 'course_prerequisite' );
			if ( ( is_array( $course_pre_courses ) ) && ( ! empty( $course_pre_courses ) ) ) {
				$course_pre_enabled = true;
			}
		}
	}

	return $course_pre_enabled;
}

/**
 * Sets the status of whether the course prerequisite is enabled or disabled.
 *
 * @since 2.4.4
 *
 * @param int     $course_id The ID of the course.
 * @param boolean $enabled   Optional. The value is true to enable course prerequisites. Any other
 *                           value will disable course prerequisites. Default true.
 *
 * @return boolean Returns true if the status was updated successfully otherwise false.
 */
function learndash_set_course_prerequisite_enabled( $course_id, $enabled = true ) {
	if ( true === $enabled ) {
		$enabled = 'on';
	}

	if ( 'on' !== $enabled ) {
		$enabled = '';
	}

	return learndash_update_setting( $course_id, 'course_prerequisite_enabled', $enabled );
}

/**
 * Gets the prerequisites compare value for a course.
 *
 * @since 2.4.0
 *
 * @param int $post_id The ID of the course.
 *
 * @return string The compare value for the prerequisite. Value can be 'ALL' or 'ANY' by default.
 */
function learndash_get_course_prerequisite_compare( $post_id ) {

	$course_pre_compare = 'ANY';

	if ( ! empty( $post_id ) ) {
		$course_id = learndash_get_course_id( $post_id );
		if ( ! empty( $course_id ) ) {
			$course_prerequisite_compare = learndash_get_setting( $course_id, 'course_prerequisite_compare' );
			if ( ( 'ANY' === $course_prerequisite_compare ) || ( 'ALL' === $course_prerequisite_compare ) ) {
				$course_pre_compare = $course_prerequisite_compare;
			}
		}
	}
	return $course_pre_compare;
}

/**
 * Checks if the course points are enabled for a course.
 *
 * @since 2.4.0
 *
 * @param int $post_id Optional. The course ID. Default 0.
 *
 * @return bool Returns true if the course points are enabled otherwise false.
 */
function learndash_get_course_points_enabled( $post_id = 0 ) {
	$course_points_enabled = false;

	if ( ! empty( $post_id ) ) {
		$course_id = learndash_get_course_id( $post_id );
		if ( ! empty( $course_id ) ) {
			$course_points_enabled = learndash_get_setting( $course_id, 'course_points_enabled' );
			if ( 'on' === $course_points_enabled ) {
				$course_points_enabled = true;
			}
		}
	}

	return $course_points_enabled;
}

/**
 * Gets the course points for a given course ID.
 *
 * @since 2.4.0
 *
 * @param int $post_id  Optional. Course Step or Course post ID. Default 0.
 * @param int $decimals Optional. Number of decimal places to round. Default 1.
 *
 * @return int|false Returns false if the course points are disabled otherwise returns course points.
 */
function learndash_get_course_points( $post_id = 0, $decimals = 1 ) {
	$course_points = false;

	if ( ! empty( $post_id ) ) {
		$course_id = learndash_get_course_id( $post_id );
		if ( ! empty( $course_id ) ) {
			if ( learndash_get_course_points_enabled( $course_id ) ) {
				$course_points = 0;

				$course_points = learndash_get_setting( $course_id, 'course_points' );
				if ( ! empty( $course_points ) ) {
					$course_points = learndash_format_course_points( $course_points, $decimals );
				}
			}
		}
	}

	return $course_points;
}

/**
 * Gets the course points access for a given course ID.
 *
 * @since 2.4.0
 *
 * @param int $post_id Optional. The ID of the course. Default 0.
 *
 * @return int|false Returns false if the course points are disabled otherwise returns course points.
 */
function learndash_get_course_points_access( $post_id = 0 ) {
	$course_points_access = false;

	if ( ! empty( $post_id ) ) {
		$course_id = learndash_get_course_id( $post_id );
		if ( ! empty( $course_id ) ) {
			if ( learndash_get_course_points_enabled( $course_id ) ) {
				$course_points_access = 0;

				$course_points_access = learndash_format_course_points( learndash_get_setting( $course_id, 'course_points_access' ) );
			}
		}
	}

	return $course_points_access;
}

/**
 * Checks if a user can access course points.
 *
 * @since 2.4.0
 *
 * @param int $post_id The ID of the post.
 * @param int $user_id Optional. The ID of the user. Default 0.
 *
 * @return boolean Whether a user can access course points.
 */
function learndash_check_user_course_points_access( $post_id, $user_id = 0 ) {
	$user_can_access = true;

	if ( empty( $user_id ) ) {
		if ( is_user_logged_in() ) {
			$user_id = get_current_user_id();
		} else {
			return false;
		}
	}

	if ( ! empty( $post_id ) ) {
		$course_id = learndash_get_course_id( $post_id );
		if ( ( ! empty( $course_id ) ) && ( ! empty( $user_id ) ) ) {
			if ( learndash_get_course_points_enabled( $course_id ) ) {
				$course_access_points = learndash_get_course_points_access( $course_id );

				if ( ! empty( $course_access_points ) ) {
					$user_course_points = learndash_get_user_course_points( $user_id );

					if ( floatval( $user_course_points ) >= floatval( $course_access_points ) ) {
						return true;
					} else {
						return false;
					}
				}
			}
		}
	}

	return true;
}

/**
 * Handles the actions to be made when the user joins a course.
 *
 * Fires on `wp` hook.
 * Redirects user to login URL, adds course access to user.
 *
 * @since 2.1.0
 */
function learndash_process_course_join() {
	$user_id = get_current_user_id();

	if ( ( isset( $_POST['course_join'] ) ) && ( isset( $_POST['course_id'] ) ) ) {
		$post_label_prefix = 'course';
		$post_id           = intval( $_POST['course_id'] );
		$post              = get_post( $post_id );
		if ( ( ! $post ) || ( ! is_a( $post, 'WP_Post' ) ) || ( learndash_get_post_type_slug( 'course' ) !== $post->post_type ) ) {
			return;
		}
	} elseif ( ( isset( $_POST['group_join'] ) ) && ( isset( $_POST['group_id'] ) ) ) {
		$post_label_prefix = 'group';
		$post_id           = intval( $_POST['group_id'] );
		$post              = get_post( $post_id );
		if ( ( ! $post ) || ( ! is_a( $post, 'WP_Post' ) ) || ( learndash_get_post_type_slug( 'group' ) !== $post->post_type ) ) {
			return;
		}
	} else {
		return;
	}

	if ( empty( $user_id ) ) {
		$login_url = wp_login_url( get_permalink( $post_id ) );

		/**
		 * Filters URL that a user should be redirected to after joining a course.
		 *
		 * @since 2.1.0
		 *
		 * @param string $login_url Redirect URL.
		 * @param int    $post_id Course or Group ID.
		 */
		$login_url = apply_filters( 'learndash_' . $post_label_prefix . '_join_redirect', $login_url, $post_id );
		if ( ! empty( $login_url ) ) {
			learndash_safe_redirect( $login_url );
		}
	}

	/**
	 * Verify the form is valid
	 *
	 * @since 2.2.1.2
	 */
	if ( ! wp_verify_nonce( $_POST[ $post_label_prefix . '_join' ], $post_label_prefix . '_join_' . $user_id . '_' . $post_id ) ) {
		return;
	}

	$settings = learndash_get_setting( $post_id );

	if ( learndash_get_post_type_slug( 'group' ) === get_post_type( $post_id ) ) {
		if ( ! isset( $settings['group_price_type'] ) ) {
			if ( ! defined( 'LEARNDASH_DEFAULT_GROUP_PRICE_TYPE' ) ) {
				$settings['group_price_type'] = LEARNDASH_DEFAULT_GROUP_PRICE_TYPE;
			} else {
				$settings['group_price_type'] = '';
			}
		}

		if ( 'free' === $settings['group_price_type'] || 'paynow' === $settings['group_price_type'] && empty( $settings['group_price'] ) && ! empty( $_POST['group_join'] ) || learndash_is_user_in_group( $user_id, $post_id ) ) {
			ld_update_group_access( $user_id, $post_id );
		}
	} elseif ( learndash_get_post_type_slug( 'course' ) === get_post_type( $post_id ) ) {
		if ( ! isset( $settings['course_price_type'] ) ) {
			if ( ! defined( 'LEARNDASH_DEFAULT_COURSE_PRICE_TYPE' ) ) {
				$settings['course_price_type'] = LEARNDASH_DEFAULT_COURSE_PRICE_TYPE;
			} else {
				$settings['course_price_type'] = '';
			}
		}

		if ( 'free' === $settings['course_price_type'] || 'paynow' === $settings['course_price_type'] && empty( $settings['course_price'] ) && ! empty( $settings['course_join'] ) || sfwd_lms_has_access( $post_id, $user_id ) ) {
			ld_update_course_access( $user_id, $post_id );
		}
	}
}

add_action( 'wp', 'learndash_process_course_join' );

/**
 * Gets all the courses with the price type open.
 *
 * Logic for this query was taken from the `sfwd_lms_has_access_fn()` function
 *
 * @since 2.3.0
 *
 * @param boolean $bypass_transient Optional. Whether to bypass transient cache. Default false.
 *
 * @return array An array of course IDs.
 */
function learndash_get_open_courses( $bypass_transient = false ) {
	return learndash_get_posts_by_price_type( learndash_get_post_type_slug( 'course' ), 'open', $bypass_transient );
}

/**
 * Gets all the courses with the price type paynow.
 *
 * Logic for this query was taken from the `sfwd_lms_has_access_fn()` function.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @since 2.3.0
 *
 * @param boolean $bypass_transient Optional. Whether to bypass the transient cache. Default false.
 *
 * @return array An array of course IDs.
 */
function learndash_get_paynow_courses( $bypass_transient = false ) {
	return learndash_get_posts_by_price_type( learndash_get_post_type_slug( 'course' ), 'paynow', $bypass_transient );
}

/**
 * Gets the list of users with expired course access from the user meta.
 *
 * @since 2.6.4
 *
 * @param int $course_id Optional. The ID of the course. Default 0.
 *
 * @return array An array of users with expired course access.
 */
function learndash_get_course_expired_access_from_meta( $course_id = 0 ) {
	global $wpdb;

	$expired_user_ids = array();

	if ( ! empty( $course_id ) ) {
		$expired_user_ids = $wpdb->get_col(
			$wpdb->prepare(
				"SELECT user_id FROM {$wpdb->usermeta} as usermeta WHERE meta_key = %s",
				'learndash_course_expired_' . $course_id
			)
		);
	}

	return array_map( 'absint', $expired_user_ids );
}


/**
 * Gets the course settings from the course meta.
 *
 * @since 2.6.4
 *
 * @TODO Need to convert all references to get_post_meta for '_sfwd-courses' to use this function.
 *
 * @param int    $course_id   Optional. The ID of the course. Default 0.
 * @param string $setting_key Optional. The slug of the setting to get. Default empty.
 *
 * @return mixed Returns course settings. Passing empty setting key gets all the settings.
 */
function learndash_get_course_meta_setting( $course_id = 0, $setting_key = '' ) {
	$course_settings = array();

	if ( empty( $course_id ) ) {
		return $course_settings;
	}

	$meta = get_post_meta( $course_id, '_sfwd-courses', true );
	if ( ( is_null( $meta ) ) || ( ! is_array( $meta ) ) ) {
		$meta = array();
	}

	// we only want/need to reformat the access list of we are returning ALL setting or just the access list.
	if ( ( empty( $setting_key ) ) || ( 'course_access_list' === $setting_key ) ) {
		if ( ! isset( $meta['sfwd-courses_course_access_list'] ) ) {
			$meta['sfwd-courses_course_access_list'] = '';
		}

		if ( ! empty( $meta['sfwd-courses_course_access_list'] ) ) {
			if ( is_string( $meta['sfwd-courses_course_access_list'] ) ) {
				$meta['sfwd-courses_course_access_list'] = array_map( 'absint', explode( ',', $meta['sfwd-courses_course_access_list'] ) );
			} elseif ( is_array( $meta['sfwd-courses_course_access_list'] ) ) {
				$meta['sfwd-courses_course_access_list'] = array_map( 'absint', $meta['sfwd-courses_course_access_list'] );
			} else {
				// Not sure how we can get here. Just in case.
				$meta['sfwd-courses_course_access_list'] = array();
			}
		} else {
			$meta['sfwd-courses_course_access_list'] = array();
		}

		// Need to remove the empty '0' items.
		$meta['sfwd-courses_course_access_list'] = array_diff( $meta['sfwd-courses_course_access_list'], array( 0, '' ) );
	}

	if ( empty( $setting_key ) ) {
		return $meta;
	} elseif ( isset( $meta[ 'sfwd-courses_' . $setting_key ] ) ) {
		return $meta[ 'sfwd-courses_' . $setting_key ];
	}
}

add_filter(
	'sfwd-courses_display_options',
	function( $options, $location ) {
		if ( ( ! isset( $options[ $location . '_course_prerequisite_enabled' ] ) ) || ( empty( $options[ $location . '_course_prerequisite_enabled' ] ) ) ) {
			global $post;
			if ( $post instanceof WP_Post ) {
				$settings = get_post_meta( $post->ID, '_sfwd-courses', true );

				if ( ( isset( $settings[ $location . '_course_prerequisite' ] ) ) && ( ! empty( $settings[ $location . '_course_prerequisite' ] ) ) ) {
					$options[ $location . '_course_prerequisite_enabled' ]  = 'on';
					$settings[ $location . '_course_prerequisite_enabled' ] = 'on';
					update_post_meta( $post->ID, '_sfwd-courses', $settings );
				}
			}
		}

		return $options;
	},
	1,
	2
);

/**
 * Updates the users group course access.
 *
 * Fires on `learndash_update_course_access` hook.
 *
 * @since 2.4.0
 *
 * @param int     $user_id     The ID of the user.
 * @param int     $course_id   The ID of the course.
 * @param array   $access_list An array of course access list.
 * @param boolean $remove      Whether to user group from course access.
 */
function learndash_update_course_users_groups( $user_id, $course_id, $access_list, $remove ) {
	if ( ( ! empty( $user_id ) ) && ( ! empty( $course_id ) ) && ( true !== (bool) $remove ) ) {

		$groups = learndash_get_course_groups( $course_id, true );
		if ( ! empty( $groups ) ) {
			foreach ( $groups as $group_id ) {
				$ld_auto_enroll_group_courses = get_post_meta( $group_id, 'ld_auto_enroll_group_courses', true );
				/**
				 * See settings in includes/settings/settings-metaboxes/class-ld-settings-metabox-group-courses-enroll.php
				 * If the checkbox is set then ALL courses can be used to enroll into group.
				 */
				if ( 'yes' === $ld_auto_enroll_group_courses ) {
					/**
					 * Filters whether to enroll into group for the course.
					 *
					 * @since 3.2.0
					 *
					 * @param boolean $enroll_in_group Whether to enroll the user into the group.
					 * @param integer $group_id        The Group ID.
					 * @param integer $course_id       The Course ID.
					 */
					if ( apply_filters( 'learndash_group_course_auto_enroll', true, $group_id, $course_id ) ) {
						ld_update_group_access( $user_id, $group_id );
					}
				} else {
					/**
					 * Else if the checkbox is not set and there are entries for the selective course enroll. Use those.
					 */
					$ld_auto_enroll_group_course_ids = get_post_meta( $group_id, 'ld_auto_enroll_group_course_ids', true );
					if ( ( is_array( $ld_auto_enroll_group_course_ids ) ) && ( ! empty( $ld_auto_enroll_group_course_ids ) ) ) {
						$ld_auto_enroll_group_course_ids = array_map( 'absint', $ld_auto_enroll_group_course_ids );

						if ( in_array( $course_id, $ld_auto_enroll_group_course_ids, true ) ) {
							/** This filter is documented in includes/course/ld-course-functions.php */
							if ( apply_filters( 'learndash_group_course_auto_enroll', true, $group_id, $course_id ) ) {
								ld_update_group_access( $user_id, $group_id );
							}
						}
					}
				}
			}
		}
	}
}
add_action( 'learndash_update_course_access', 'learndash_update_course_users_groups', 50, 4 );

/**
 * Gets the course completion date for a user.
 *
 * @since 2.4.7
 *
 * @param int $user_id   Optional. The ID of the user. Default 0.
 * @param int $course_id Optional. The ID of the course. Default 0.
 *
 * @return int The timestamp of when the course was completed. The value is 0 if the course is not completed.
 */
function learndash_user_get_course_completed_date( $user_id = 0, $course_id = 0 ) {
	$completed_on_timestamp = 0;
	if ( ( ! empty( $user_id ) ) && ( ! empty( $course_id ) ) ) {
		$completed_on_timestamp = get_user_meta( $user_id, 'course_completed_' . $course_id, true );

		if ( empty( $completed_on_timestamp ) ) {
			$activity_query_args = array(
				'post_ids'       => $course_id,
				'user_ids'       => $user_id,
				'activity_types' => 'course',
				'per_page'       => 1,
			);

			$activity = learndash_reports_get_activity( $activity_query_args );
			if ( ! empty( $activity['results'] ) ) {
				foreach ( $activity['results'] as $activity_item ) {
					if ( property_exists( $activity_item, 'activity_completed' ) ) {
						$completed_on_timestamp = $activity_item->activity_completed;

						// To make the next check easier we update the user meta.
						update_user_meta( $user_id, 'course_completed_' . $course_id, $completed_on_timestamp );
						break;
					}
				}
			}
		}
	}

	return $completed_on_timestamp;
}

/**
 * Gets the page data by page path.
 *
 * @since 2.5.2
 *
 * @param string $slug      Optional. The slug of the page. Default empty.
 * @param string $post_type Optional. The post type slug. Default empty.
 *
 * @return WP_Post|array|null `WP_Post` object or array on success, null on failure.
 */
function learndash_get_page_by_path( $slug = '', $post_type = '' ) {
	$course_post = null;

	if ( ( ! empty( $slug ) ) && ( ! empty( $post_type ) ) ) {

		$course_post = get_page_by_path( $slug, OBJECT, $post_type );

		if ( ( defined( 'ICL_LANGUAGE_CODE' ) ) && ( ICL_LANGUAGE_CODE != '' ) ) {
			if ( function_exists( 'icl_object_id' ) ) {
				$course_post = get_page( icl_object_id( $course_post->ID, $post_type, true, ICL_LANGUAGE_CODE ) );
			}
		}
	}

	return $course_post;
}

/**
 * Gets the course lessons per page setting.
 *
 * This function will initially source the per_page from the course. But if we are using the
 * default lesson options setting we will use that. Then if the lessons options
 * is not set for some reason we use the default system option 'posts_per_page'.
 *
 * @since 2.5.5
 *
 * @param int $course_id Optional. The ID of the course. Default 0.
 *
 * @return int The number of lessons per page or 0.
 */
function learndash_get_course_lessons_per_page( $course_id = 0 ) {
	$course_lessons_per_page = 0;

	// From the WP > Settings > Reading > Posts per page.
	$course_lessons_per_page = (int) get_option( 'posts_per_page' );

	// From the LearnDash > Settings > General > Global Pagination Settings > Shortcodes & Widgets per page.
	$course_lessons_per_page = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_Per_Page', 'per_page', $course_lessons_per_page );

	// From the LearnDash > Courses > Settings > Global Course Management > Course Table Pagination > Lessons per page.
	$course_settings = LearnDash_Settings_Section::get_section_settings_all( 'LearnDash_Settings_Courses_Management_Display' );
	if ( ( isset( $course_settings['course_pagination_enabled'] ) ) && ( 'yes' === $course_settings['course_pagination_enabled'] ) ) {
		if ( isset( $course_settings['course_pagination_lessons'] ) ) {
			$course_lessons_per_page = $course_settings['course_pagination_lessons'];
		} elseif ( isset( $course_settings['posts_per_page'] ) ) {
			$course_lessons_per_page = $course_settings['posts_per_page'];
		}
	}

	// From the specific Course Settings > Custom Pagination.
	if ( ! empty( $course_id ) ) {
		$course_settings = learndash_get_setting( intval( $course_id ) );
		if ( ( isset( $course_settings['course_lesson_per_page'] ) ) && ( 'CUSTOM' === $course_settings['course_lesson_per_page'] ) && ( isset( $course_settings['course_lesson_per_page_custom'] ) ) ) {
			$course_lessons_per_page = $course_settings['course_lesson_per_page_custom'];
		}
	}

	return absint( $course_lessons_per_page );
}

/**
 * Called from within the Course Lessons List processing query SFWD_CPT::loop_shortcode.
 * This action will setup a global pager array to be used in templates.
 */

$course_pager_results = array( 'pager' => array() );
global $course_pager_results;

/**
 * Handles the course lessons list pager.
 *
 * @since 2.5.5
 *
 * Fires on `learndash_course_lessons_list_pager` hook.
 *
 * @global array $course_pager_results
 *
 * @param WP_Query|null $query_result  Optional. Course lesson list `WP_Query` object. Default null.
 * @param string        $pager_context Optional. The context where pagination is shown. Default empty.
 */
function learndash_course_lessons_list_pager( $query_result = null, $pager_context = '' ) {
	global $course_pager_results;

	$course_pager_results['pager']['paged'] = 1;
	if ( ( isset( $query_result->query_vars['paged'] ) ) && ( $query_result->query_vars['paged'] > 1 ) ) {
		$course_pager_results['pager']['paged'] = $query_result->query_vars['paged'];
	}

	$course_pager_results['pager']['total_items'] = absint( $query_result->found_posts );
	$course_pager_results['pager']['total_pages'] = absint( $query_result->max_num_pages );
}
add_action( 'learndash_course_lessons_list_pager', 'learndash_course_lessons_list_pager', 10, 2 );

/**
 * Gets the lesson topic pagination values from HTTP get global array.
 *
 * @since 3.0.0
 *
 * @return array An array of lesson topic pagination values.
 */
function learndash_get_lesson_topic_paged_values() {
	$paged_values = array(
		'lesson' => 0,
		'paged'  => 1,
	);
	if ( ( isset( $_GET['ld-topic-page'] ) ) && ( ! empty( $_GET['ld-topic-page'] ) ) ) {
		list( $paged_values['lesson'], $paged_values['paged'] ) = explode( '-', $_GET['ld-topic-page'] );
		$paged_values['lesson']                                 = absint( $paged_values['lesson'] );
		$paged_values['paged']                                  = absint( $paged_values['paged'] );
		if ( $paged_values['paged'] < 1 ) {
			$paged_values['paged'] = 1;
		}
		if ( ( empty( $paged_values['lesson'] ) ) || ( empty( $paged_values['paged'] ) ) ) {
			$paged_values = array(
				'lesson' => 0,
				'paged'  => 1,
			);
		}
	}

	return $paged_values;
}

/**
 * Processes the lesson topics pagination.
 *
 * @since 3.0.0
 *
 * @global array $course_pager_results
 *
 * @param array $topics Optional. An array of topics. Default empty array.
 * @param array $args {
 *    An array of lesson topic pager arguments. Default empty array.
 *
 *    @type int $course_id Course ID.
 *    @type int $lesson_id Lesson ID.
 * }
 *
 * @return array An array of paged topics.
 */
function learndash_process_lesson_topics_pager( $topics = array(), $args = array() ) {
	global $course_pager_results;

	$paged_values = learndash_get_lesson_topic_paged_values();

	if ( ! empty( $topics ) ) {
		if ( ! isset( $args['per_page'] ) ) {
			$topics_per_page = learndash_get_course_topics_per_page( $args['course_id'], $args['lesson_id'] );
		} else {
			$topics_per_page = intval( $args['per_page'] );
		}
		if ( ( $topics_per_page > 0 ) && ( count( $topics ) > $topics_per_page ) ) {
			$topics_chunks = array_chunk( $topics, $topics_per_page );

			$course_pager_results[ $args['lesson_id'] ]          = array();
			$course_pager_results[ $args['lesson_id'] ]['pager'] = array();

			$topics_paged = 1;

			if ( ( ! empty( $paged_values['lesson'] ) ) && ( $paged_values['lesson'] == $args['lesson_id'] ) ) {
				$topics_paged = $paged_values['paged'];
			} elseif ( get_post_type() === learndash_get_post_type_slug( 'topic' ) ) {
				/**
				 * If we are viewing a Topic and the page is empty we load the
				 * paged set to show the current topic item.
				 */
				foreach ( $topics_chunks as $topics_chunk_page => $topics_chunk_set ) {
					$topics_ids = array_values( wp_list_pluck( $topics_chunk_set, 'ID' ) );

					if ( ( ! empty( $topics_ids ) ) && ( in_array( (int) get_the_ID(), array_map( 'absint', $topics_ids ), true ) ) ) {
						$topics_paged = ++$topics_chunk_page;
						break;
					}
				}
			} elseif ( get_post_type() === learndash_get_post_type_slug( 'quiz' ) ) {
				$parent_step_ids = learndash_course_get_all_parent_step_ids( $args['course_id'], get_the_ID() );
				if ( ! empty( $parent_step_ids ) ) {
					$parent_step_ids = array_map( 'absint', $parent_step_ids );
					$parent_step_ids = array_reverse( $parent_step_ids );

					if ( get_post_type( $parent_step_ids[0] ) === learndash_get_post_type_slug( 'topic' ) ) {
						// If the Quiz has a Topic parent we loop through the topic chunks to find the parent.
						foreach ( $topics_chunks as $topics_chunk_page => $topics_chunk_set ) {
							$topics_ids = array_values( wp_list_pluck( $topics_chunk_set, 'ID' ) );
							if ( ( ! empty( $topics_ids ) ) && ( in_array( $parent_step_ids[0], $topics_ids ) ) ) {
								$topics_paged = ++$topics_chunk_page;
								break;
							}
						}
					} elseif ( get_post_type( $parent_step_ids[0] ) === learndash_get_post_type_slug( 'lesson' ) ) {
						/**
						 * If the Quiz has a LEsson parent we just set the last Topic chunk set because
						 * Lesson Quizzes are shown at the end.
						 */
						$topics_paged = count( $topics_chunks );
					}
				}
			}

			$course_pager_results[ $args['lesson_id'] ]['pager']['paged'] = $topics_paged;

			$course_pager_results[ $args['lesson_id'] ]['pager']['total_items'] = count( $topics );
			$course_pager_results[ $args['lesson_id'] ]['pager']['total_pages'] = count( $topics_chunks );

			$topics = $topics_chunks[ $topics_paged - 1 ];
		}
	}

	return $topics;
}

/**
 * Gets the course lessons order query arguments.
 *
 * The course lessons order can be set in the course or globally defined in
 * the lesson options. This function will check all logic and return the
 * correct setting.
 *
 * @since 2.5.8
 *
 * @param int $course_id Optional. The ID of the course. Default 0.
 *
 * @return array An array of course lessons order query arguments.
 */
function learndash_get_course_lessons_order( $course_id = 0 ) {
	$course_lessons_args = array(
		'order'   => '',
		'orderby' => '',
	);

	if ( learndash_is_course_shared_steps_enabled() ) {
		$course_lessons_args['orderby'] = 'post__in';
		return $course_lessons_args;

	} else {
		$lessons_options = learndash_get_option( 'sfwd-lessons' );
		if ( ( isset( $lessons_options['order'] ) ) && ( ! empty( $lessons_options['order'] ) ) ) {
			$course_lessons_args['order'] = $lessons_options['order'];
		}

		if ( ( isset( $lessons_options['orderby'] ) ) && ( ! empty( $lessons_options['orderby'] ) ) ) {
			$course_lessons_args['orderby'] = $lessons_options['orderby'];
		}
	}

	if ( ! empty( $course_id ) ) {
		$course_settings = learndash_get_setting( $course_id );
		if ( ( isset( $course_settings['course_lesson_order'] ) ) && ( ! empty( $course_settings['course_lesson_order'] ) ) ) {
			$course_lessons_args['order'] = $course_settings['course_lesson_order'];
		}

		if ( ( isset( $course_settings['course_lesson_orderby'] ) ) && ( ! empty( $course_settings['course_lesson_orderby'] ) ) ) {
			$course_lessons_args['orderby'] = $course_settings['course_lesson_orderby'];
		}
	}

	/**
	 * Filters course lessons order query arguments.
	 *
	 * @param array $course_lesson_args An array of course lesson order arguments.
	 * @param int   $course_id          Course ID.
	 */
	return apply_filters( 'learndash_course_lessons_order', $course_lessons_args, $course_id );
}

/**
 * Converts and gets the course access list.
 *
 * The function converts the standard comma-separated list of user IDs
 * used for the course_access_list field. The conversion is to trim and ensure
 * the values are integer and not empty.
 *
 * @since 2.5.9
 *
 * @param string|array $course_access_list Optional. String of comma separated user IDs or array. Default empty.
 * @param boolean      $return_array       Optional. Whether to return array. True to return array and false to return string. Default false.
 *
 * @return string|array The list of user IDs.
 */
function learndash_convert_course_access_list( $course_access_list = '', $return_array = false ) {
	if ( ! empty( $course_access_list ) ) {

		// Convert the comma separated list into an array.
		if ( is_string( $course_access_list ) ) {
			$course_access_list = explode( ',', $course_access_list );
		}

		// Now normalize the array elements.
		if ( is_array( $course_access_list ) ) {
			$course_access_list = array_map( 'absint', $course_access_list );
			$course_access_list = array_unique( $course_access_list, SORT_NUMERIC );
			$course_access_list = array_diff( $course_access_list, array( 0 ) );
		}

		// Prepare the return value.
		if ( true !== $return_array ) {
			$course_access_list = implode( ',', $course_access_list );
		}
	} elseif ( true === $return_array ) {
		$course_access_list = array();
	}

	return $course_access_list;
}

/**
 * Determines the number of lesson topics to display per page.
 *
 * @since 3.0.0
 *
 * @param int $course_id Optional. Parent Course ID. Default 0.
 * @param int $lesson_id Optional. Parent Lesson ID. Default 0.
 *
 * @return int The number of lesson topics per page.
 */
function learndash_get_course_topics_per_page( $course_id = 0, $lesson_id = 0 ) {
	$course_topics_per_page = 0;

	// From the WP > Settings > Reading > Posts per page.
	$course_topics_per_page = intval( get_option( 'posts_per_page' ) );

	// From the LearnDash > Settings > General > Global Pagination Settings > Shortcodes & Widgets per page.
	$course_topics_per_page = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_Per_Page', 'per_page', $course_topics_per_page );

	// From the LearnDash > Courses > Settings > Global Course Management > Course Table Pagination > Lessons per page.
	$course_settings = LearnDash_Settings_Section::get_section_settings_all( 'LearnDash_Settings_Courses_Management_Display' );
	if ( ( isset( $course_settings['course_pagination_enabled'] ) ) && ( 'yes' === $course_settings['course_pagination_enabled'] ) ) {
		if ( isset( $course_settings['course_pagination_topics'] ) ) {
			$course_topics_per_page = absint( $course_settings['course_pagination_topics'] );
		} elseif ( isset( $course_settings['posts_per_page'] ) ) {
			$course_topics_per_page = absint( $course_settings['posts_per_page'] );
		}
	}

	// From the specific Course Settings > Custom Pagination.
	if ( ! empty( $course_id ) ) {
		$course_settings = learndash_get_setting( intval( $course_id ) );
		if ( ( isset( $course_settings['course_lesson_per_page'] ) ) && ( 'CUSTOM' === $course_settings['course_lesson_per_page'] ) && ( isset( $course_settings['course_topic_per_page_custom'] ) ) ) {
			$course_topics_per_page = absint( $course_settings['course_topic_per_page_custom'] );
		}
	}

	return $course_topics_per_page;
}

/**
 * Checks whether to use the legacy course access list.
 *
 * @since 3.1.0
 *
 * @return boolean Returns true to use legacy course access list otherwise false.
 */
function learndash_use_legacy_course_access_list() {
	$use_legacy_course_access_list = true;

	$data_course_access_convert = learndash_data_upgrades_setting( 'course-access-lists-convert' );
	if ( $data_course_access_convert ) {
		$use_legacy_course_access_list = false;

	}
	/**
	 * Filters whether to use legacy course access list or not.
	 *
	 * @param boolean $use_legacy_course_access_list Whether to use legacy course access list.
	 */
	return apply_filters( 'learndash_use_legacy_course_access_list', $use_legacy_course_access_list );
}

/**
 * Gets the user's last active (last updated) course ID.
 *
 * @since 3.1.4
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $user_id Optional. User ID. Default 0.
 *
 * @return int The last active course ID.
 */
function learndash_get_last_active_course( $user_id = 0 ) {
	global $wpdb;

	$last_course_id = 0;

	if ( empty( $user_id ) ) {
		$user_id = get_current_user_id();
	}

	if ( ! empty( $user_id ) ) {
		$query_result   = $wpdb->get_var(
			$wpdb->prepare(
				'SELECT post_id FROM ' . esc_sql( LDLMS_DB::get_table_name( 'user_activity' ) ) . " WHERE user_id=%d AND activity_type='course' AND activity_status = 0 AND activity_completed = '' ORDER BY activity_updated DESC",
				$user_id
			)
		);
		$last_course_id = absint( $query_result );
	}

	return $last_course_id;
}


/**
 * Gets the user's last active step for a course.
 *
 * @since 3.1.4
 *
 * @param int $user_id   Optional. User ID. Default 0.
 * @param int $course_id Optional. Course ID. Default 0.
 *
 * @return int The last active course step ID.
 */
function learndash_user_course_last_step( $user_id = 0, $course_id = 0 ) {
	global $wpdb;

	$last_course_step_id = 0;

	if ( empty( $user_id ) ) {
		$user_id = get_current_user_id();
	}

	if ( ! empty( $user_id ) ) {
		if ( empty( $course_id ) ) {
			$course_id = learndash_get_last_active_course( $user_id );
		}
		if ( ! empty( $course_id ) ) {
			$query_result        = $wpdb->get_var(
				$wpdb->prepare(
					'SELECT user_activity_meta.activity_meta_value FROM ' . esc_sql( LDLMS_DB::get_table_name( 'user_activity' ) ) . ' as user_activity INNER JOIN ' . esc_sql( LDLMS_DB::get_table_name( 'user_activity_meta' ) ) . " as user_activity_meta ON user_activity.activity_id = user_activity_meta.activity_id WHERE user_activity.user_id=%d AND user_activity.post_id=%d AND user_activity.activity_type='course' AND user_activity_meta.activity_meta_key= 'steps_last_id' ORDER BY activity_updated DESC",
					$user_id,
					$course_id
				)
			);
			$last_course_step_id = absint( $query_result );
		}
	}

	return $last_course_step_id;
}


/**
 * Check if user can bypass action ($context).
 *
 * @since 3.2.0
 *
 * @param int    $user_id User ID.
 * @param string $context The specific action to check for.
 * @param array  $args Optional array of args related to the
 * context. Typically starting with an step ID, Course ID, etc.
 * @return bool True if user can bypass. Otherwise false.
 */
function learndash_can_user_bypass( $user_id = 0, $context = 'learndash_course_progression', $args = array() ) {
	if ( empty( $user_id ) ) {
		if ( is_user_logged_in() ) {
			$user_id = get_current_user_id();
		}
	}

	$can_bypass = false;
	if ( ! empty( $user_id ) ) {
		if ( ( learndash_is_admin_user( $user_id ) ) && ( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_Admin_User', 'bypass_course_limits_admin_users' ) ) ) {
			$can_bypass = true;
		} elseif ( ( learndash_is_group_leader_user( $user_id ) ) && ( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_Groups_Group_Leader_User', 'bypass_course_limits' ) ) ) {
			$can_bypass = true;
		}
	}

	/**
	 * Filters user can bypass logic.
	 *
	 * @since 3.2.0
	 *
	 * @param boolean $can_bypass Whether the user can bypass $context.
	 * @param int     $user_id    User ID.
	 * @param string  $context The specific action to check for.
	 * @param array  $args Optional array of args related to the
	 * context. Typically starting with an step ID, Course ID, etc.
	 */
	$can_bypass = apply_filters( 'learndash_user_can_bypass', $can_bypass, $user_id, $context, $args );

	return $can_bypass;
}

/**
 * Check if user can auto-enroll in courses..
 *
 * @since 3.2.3
 *
 * @param int $user_id User ID.
 * @return bool True if user can auto-enroll.
 */
function learndash_can_user_autoenroll_courses( $user_id = 0 ) {
	if ( empty( $user_id ) ) {
		if ( is_user_logged_in() ) {
			$user_id = get_current_user_id();
		}
	}

	$auto_enroll = false;
	if ( ! empty( $user_id ) ) {
		if ( learndash_is_admin_user( $user_id ) ) {
			$auto_enroll = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_Admin_User', 'courses_autoenroll_admin_users' );
		} elseif ( learndash_is_group_leader_user( $user_id ) ) {
			if ( 'yes' === LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_Groups_Group_Leader_User', 'courses_autoenroll' ) ) {
				$auto_enroll = 'yes';
			}
		}
	}

	if ( 'yes' === $auto_enroll ) {
		$auto_enroll = true;
	} else {
		$auto_enroll = false;
	}

	/**
	 * Filters whether to auto enroll a user into a course or not.
	 *
	 * @since 2.3.0
	 *
	 * @param boolean $auto_enroll Whether to auto enroll user or not.
	 * @param int     $user_id     ID of the logged in user to check.
	 */
	return apply_filters( 'learndash_override_course_auto_enroll', $auto_enroll, $user_id );
}

/**
 * Utility function to check if Course Builder is enabled.
 *
 * @since 3.4.0
 */
function learndash_is_course_builder_enabled() {
	if ( 'yes' === LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Management_Display', 'course_builder_enabled' ) ) {
		return true;
	}

	return false;
}

/**
 * Utility function to check if Course Shared steps is enabled.
 *
 * @since 3.4.0
 */
function learndash_is_course_shared_steps_enabled() {
	if ( ( true === learndash_is_course_builder_enabled() ) && ( 'yes' === LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Management_Display', 'course_builder_shared_steps' ) ) ) {
		return true;
	}

	return false;
}

/**
 * Get Courses/Groups by Price Type.
 *
 * @since 3.4.1
 *
 * @param string  $post_type        Post Type slug: sfwd-courses or group.
 * @param string  $price_type       Price Type: open, free, closed, paynow, etc.
 * @param boolean $bypass_transient Optional. Whether to bypass transient cache. Default false.
 *
 * @return @array Array of Course IDs.
 */
function learndash_get_posts_by_price_type( $post_type = '', $price_type = '', $bypass_transient = false ) {
	global $wpdb;

	$post_ids   = array();
	$post_type  = esc_attr( $post_type );
	$price_type = esc_attr( $price_type );

	if ( ( ! empty( $post_type ) ) && ( in_array( $post_type, learndash_get_post_type_slug( array( 'course', 'group' ) ), true ) ) ) {

		$new_logic = false;
		if ( learndash_get_post_type_slug( 'course' ) === $post_type ) {
			if ( empty( $price_type ) ) {
				$price_type = LEARNDASH_DEFAULT_COURSE_PRICE_TYPE;
			}

			$transient_key = 'learndash_' . $price_type . '_courses';

		} elseif ( learndash_get_post_type_slug( 'group' ) === $post_type ) {
			if ( empty( $price_type ) ) {
				$price_type = LEARNDASH_DEFAULT_GROUP_PRICE_TYPE;
			}

			$transient_key = 'learndash_' . $price_type . '_groups';
		}

		if ( ! $bypass_transient ) {
			$post_ids_transient = LDLMS_Transients::get( $transient_key );
		} else {
			$post_ids_transient = false;
		}

		if ( false === $post_ids_transient ) {
			if ( learndash_post_meta_processed( $post_type ) ) {
				$price_query_args = array(
					'post_type'    => $post_type,
					'fields'       => 'ids',
					'nopaging'     => true,
					'meta_key'     => '_ld_price_type',
					'meta_value'   => $price_type,
					'meta_compare' => '=',
				);

				$price_query = new WP_Query( $price_query_args );
				if ( ( property_exists( $price_query, 'posts' ) ) && ( ! empty( $price_query->posts ) ) ) {
					$post_ids = $price_query->posts;
					$post_ids = array_map( 'absint', $post_ids );
				}
			} else {
				$sql_str = $wpdb->prepare(
					"SELECT postmeta.post_id as post_id FROM {$wpdb->postmeta} as postmeta
						INNER JOIN {$wpdb->posts} as posts ON posts.ID = postmeta.post_id
						WHERE posts.post_status='publish' AND posts.post_type=%s AND postmeta.meta_key=%s
						AND ( postmeta.meta_value REGEXP '\"" . $post_type . '_' . learndash_get_post_type_key( $post_type ) . '_price_type";s:' . strlen( $price_type ) . ':"' . $price_type . "\";' )",
					$post_type,
					'_' . $post_type
				);

				$post_ids = $wpdb->get_col( $sql_str );
			}
			if ( ! empty( $post_ids ) ) {
				$post_ids = array_map( 'absint', $post_ids );
			}
			LDLMS_Transients::set( $transient_key, $post_ids, MINUTE_IN_SECONDS );

		} else {
			$post_ids = $post_ids_transient;
		}
	}

	return $post_ids;
}

/**
 * Checks if Post Meta Data Upgrade has completed.
 *
 * @since 3.4.1
 *
 * @param string $post_type The post type slug to check.
 *
 * @return boolean.
 */
function learndash_post_meta_processed( $post_type = '' ) {
	if ( ( ! empty( $post_type ) ) && ( learndash_is_valid_post_type( $post_type ) ) ) {
		$data_settings = learndash_data_upgrades_setting( learndash_get_post_type_key( $post_type ) . '-post-meta' );
		if ( ( ! isset( $data_settings['process_status'] ) ) || ( 'complete' !== $data_settings['process_status'] ) ) {
			$post_meta_processed = false;
		} else {
			$post_meta_processed = true;
		}

		/**
		 * Filters whether to post meta is processed or not.
		 *
		 * @since 3.4.1
		 *
		 * @param boolean $process   True or False to process post meta.
		 * @param string  $post_type The post type slug.
		 */
		return apply_filters( 'learndash_post_meta_processed', $post_meta_processed, $post_type );
	}
	return false;
}

/**
 * Returns true if it's a course post.
 *
 * @param WP_Post|int|null $post Post or Post ID.
 *
 * @since 4.1.0
 *
 * @return bool
 */
function learndash_is_course_post( $post ): bool {
	if ( empty( $post ) ) {
		return false;
	}

	$post_type = is_a( $post, WP_Post::class ) ? $post->post_type : get_post_type( $post );

	return LDLMS_Post_Types::get_post_type_slug( 'course' ) === $post_type;
}

/**
 * Returns course enrollment url.
 *
 * @param WP_Post|int|null $post Post or Post ID.
 *
 * @since 4.1.0
 *
 * @return string
 */
function learndash_get_course_enrollment_url( $post ): string {
	if ( empty( $post ) ) {
		return '';
	}

	if ( is_int( $post ) ) {
		$post = get_post( $post );

		if ( is_null( $post ) ) {
			return '';
		}
	}

	$url = get_permalink( $post );

	$settings = learndash_get_setting( $post );

	if ( 'paynow' === $settings['course_price_type'] && ! empty( $settings['course_price_type_paynow_enrollment_url'] ) ) {
		$url = $settings['course_price_type_paynow_enrollment_url'];
	} elseif ( 'subscribe' === $settings['course_price_type'] && ! empty( $settings['course_price_type_subscribe_enrollment_url'] ) ) {
		$url = $settings['course_price_type_subscribe_enrollment_url'];
	}

	/** This filter is documented in includes/course/ld-course-functions.php */
	return apply_filters( 'learndash_course_join_redirect', $url, $post->ID );
}