class-ld-settings-sections.php 39.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
<?php
/**
 * LearnDash Settings Sections Abstract Class.
 *
 * @since 2.4.0
 * @package LearnDash\Settings\Sections
 */

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

if ( ! class_exists( 'LearnDash_Settings_Section' ) ) {

	/**
	 * Class for LearnDash Settings Sections.
	 *
	 * @since 2.4.0
	 */
	class LearnDash_Settings_Section {
		/**
		 * Static array of section instances.
		 *
		 * @var array $_instances
		 */
		protected static $_instances = array(); // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore

		/**
		 * Match the WP Screen ID
		 *
		 * @var string $settings_screen_id Settings Screen ID.
		 */
		protected $settings_screen_id = '';

		/**
		 * Match the  Settings Page ID
		 *
		 * @var string $settings_page_id Settings Page ID.
		 */
		protected $settings_page_id = '';

		/**
		 * Store for all the fields in this section
		 *
		 * @var array $setting_option_fields Array of section fields.
		 */
		protected $setting_option_fields = array();

		/**
		 * Holds the values for the fields. Read in from the wp_options item.
		 *
		 * @var array $setting_option_values Array of section values.
		 */
		protected $setting_option_values = array();

		/**
		 * Flag for if settings values have been loaded.
		 *
		 * @var boolean $settings_values_loaded Flag.
		 */
		protected $settings_values_loaded = false;

		/**
		 * Flag to save settings values on load.
		 *
		 * This is needed is the settings values loaded
		 * is false.
		 *
		 * @var boolean $settings_values_save_on_load Flag.
		 */
		protected $settings_values_save_on_load = false;

		/**
		 * Flag for if settings fields have been loaded.
		 *
		 * @var boolean $settings_fields_loaded Flag.
		 */

		protected $settings_fields_loaded = false;

		/**
		 * This is the 'option_name' key used to store the section values.
		 *
		 * @var string setting_option_key
		 */
		protected $setting_option_key = '';

		/**
		 * This is the HTML form field prefix used.
		 *
		 * @var string setting_field_prefix
		 */
		protected $setting_field_prefix = '';

		/**
		 * This is used as the option_name when the settings
		 * are saved to the options table.
		 *
		 * @var string $settings_section_key
		 */
		protected $settings_section_key = '';

		/**
		 * Section label/header
		 * This setting is used to show in the title of the metabox or section.
		 *
		 * @var string $settings_section_label
		 */
		protected $settings_section_label = '';

		/**
		 * Used to show the section description above the fields. Can be empty.
		 *
		 * @var string $settings_section_description
		 */
		protected $settings_section_description = '';

		/**
		 * Used to associate a section with a parent section.
		 *
		 * @since 3.6.0
		 *
		 * @var string $settings_parent_section_key
		 */
		protected $settings_parent_section_key = '';

		/**
		 * Used to transition settings from one class to another. Can be empty.
		 * Set at the class then combined into the $settings_deprecated array.
		 *
		 * @var string $settings_deprecated
		 */
		protected $settings_deprecated = array();

		/**
		 * Unique ID used for metabox on page. Will be derived from
		 * settings_option_key + setting_section_key
		 *
		 * @var string $metabox_key
		 */
		protected $metabox_key = '';

		/**
		 * Controls metabox context on page
		 * See WordPress add_meta_box() function 'context' parameter.
		 *
		 * @var string $metabox_context
		 */
		protected $metabox_context = 'normal';

		/**
		 * Controls metabox priority on page
		 * See WordPress add_meta_box() function 'priority' parameter.
		 *
		 * @var string $metabox_priority
		 */
		protected $metabox_priority = 'default';

		/**
		 * Lets the section define it's own display function instead of using the Settings API
		 *
		 * @var mixed $settings_fields_callback
		 */
		protected $settings_fields_callback = null;

		/**
		 * Used on submit metaboxes to display reset confirmation popup message.
		 *
		 * @var string $reset_confirm_message
		 */
		protected $reset_confirm_message = '';

		/**
		 * Static array of deprecated section and fields.
		 *
		 * @var array $settings_deprecated
		 */
		protected static $global_settings_deprecated = array();

		/**
		 * Controls if we need to load the Section settings from the wp_options table.
		 *
		 * @since 3.4.0
		 *
		 * @var boolean $load_options
		 */
		protected $load_options = true;

		/**
		 * Label used for metabox sub menu.
		 *
		 * @since 3.6.0
		 *
		 * @var string $settings_section_sub_label;
		 */
		protected $settings_section_sub_label = '';

		/**
		 * Section Listing label.
		 *
		 * Used when the sections are listed in a table.
		 *
		 * @since 3.6.0
		 *
		 * @var string $settings_section_listing_label;
		 */
		protected $settings_section_listing_label = '';

		/**
		 * Section Listing description.
		 *
		 * Used when the sections are listed in a table.
		 *
		 * @since 3.6.0
		 *
		 * @var string $settings_section_listing_description;
		 */
		protected $settings_section_listing_description = '';

		/**
		 * Controls if nonce validation is required.
		 *
		 * @since 3.6.0
		 *
		 * @var boolean $settings_bypass_nonce_check
		 */
		protected $settings_bypass_nonce_check = false;


		/**
		 * Public constructor for class
		 *
		 * @since 2.4.0
		 */
		protected function __construct() {
			add_action( 'init', array( $this, 'init' ) );
			add_action( 'learndash_settings_page_init', array( $this, 'settings_page_init' ) );
			if ( defined( 'LEARNDASH_SETTINGS_SECTION_TYPE' ) && ( 'metabox' === LEARNDASH_SETTINGS_SECTION_TYPE ) ) {
				add_action( 'learndash_add_meta_boxes', array( $this, 'add_meta_boxes' ) );
			}

			add_filter( 'pre_update_option_' . $this->setting_option_key, array( $this, 'section_pre_update_option' ), 30, 3 );
			add_action( 'update_option_' . $this->setting_option_key, array( $this, 'section_update_option' ), 30, 3 );
			add_filter( 'learndash_settings_section_save_fields_' . $this->setting_option_key, array( $this, 'filter_section_save_fields' ), 30, 4 );

			$this->metabox_key = $this->setting_option_key . '_' . $this->settings_section_key;

			if ( empty( $this->settings_screen_id ) ) {
				$this->settings_screen_id = 'admin_page_' . $this->settings_page_id;
			}

			if ( ! empty( $this->settings_deprecated ) ) {
				foreach ( $this->settings_deprecated as $old_class => $old_settings ) {
					if ( ! isset( self::$global_settings_deprecated[ $old_class ] ) ) {
						if ( ! isset( $old_settings['class'] ) ) {
							$old_settings['class'] = get_called_class();
						}
						self::$global_settings_deprecated[ $old_class ] = $old_settings;
					}
				}
			}
		}

		/**
		 * Returns the placeholder text for keys.
		 *
		 * @since 4.6.0
		 *
		 * @return string
		 */
		protected static function get_placeholder_for_keys(): string {
			return esc_attr__( 'This key is stored secretly.', 'learndash' );
		}

		/**
		 * Get the instance of our class based on the section_key
		 *
		 * @since 2.4.0
		 *
		 * @param string $section_key Unique section key used to identify instance.
		 */
		final public static function get_section_instance( $section_key = '' ) {
			if ( ! empty( $section_key ) ) {
				if ( isset( self::$_instances[ $section_key ] ) ) {
					return self::$_instances[ $section_key ];
				}
			}
		}

		/**
		 * Get the instance of our class based on the metabox_key
		 *
		 * @since 3.6.0
		 *
		 * @param string $criteria_field Section field to compare.
		 * @param string $criteria_value Section field value to compare.
		 * @param string $return_field   Section field to return. If empty object is returned.
		 *
		 * @return mixed Section object or individual field per `$return_field` value.
		 */
		final public static function get_section_instance_by( $criteria_field = '', $criteria_value = '', $return_field = '' ) {
			if ( ( ! empty( $criteria_field ) ) && ( ! empty( $criteria_value ) ) ) {
				if ( ! empty( self::$_instances ) ) {
					foreach ( self::$_instances as $_instance_key => $_instance_object ) {
						if ( ( $_instance_object ) && ( is_a( $_instance_object, 'LearnDash_Settings_Section' ) ) ) {
							if ( ( property_exists( $_instance_object, $criteria_field ) ) && ( $_instance_object->$criteria_field === $criteria_value ) ) {
								if ( ( ! empty( $return_field ) ) && ( property_exists( $_instance_object, $return_field ) ) ) {
									return $_instance_object->$return_field;
								}
								return $_instance_object;
							}
						}
					}
				}
			}
		}

		/**
		 * Get the instance of our class based on the page_id
		 *
		 * @since 3.6.0
		 *
		 * @param string $criteria_field Section field to compare.
		 * @param string $criteria_value Section field value to compare.
		 * @param string $return_field   Section field to return. If empty object is returned.
		 * @param string $return_key     Section field to user as array key for return.
		 *
		 * @return array An array of sections.
		 */
		final public static function get_all_sections_by( $criteria_field = '', $criteria_value = '', $return_field = '', $return_key = '' ) {
			$sections = array();
			if ( ( ! empty( $criteria_field ) ) && ( ! empty( $criteria_value ) ) ) {
				if ( ! empty( self::$_instances ) ) {
					foreach ( self::$_instances as $_instance_key => $_instance_object ) {
						if ( ( $_instance_object ) && ( is_a( $_instance_object, 'LearnDash_Settings_Section' ) ) ) {
							if ( ( property_exists( $_instance_object, $criteria_field ) ) && ( $_instance_object->$criteria_field === $criteria_value ) ) {
								$return_key_value = '';
								if ( ( ! empty( $return_key ) ) && ( property_exists( $_instance_object, $return_key ) ) ) {
									$field_type = gettype( $_instance_object->$return_key );
									if ( in_array( gettype( $_instance_object->$return_key ), array( 'boolean', 'integer', 'double', 'float', 'string' ), true ) ) {
										$return_key_value = $_instance_object->$return_key;
									}
								}

								if ( ( ! empty( $return_field ) ) && ( property_exists( $_instance_object, $return_field ) ) ) {
									if ( ! empty( $return_key_value ) ) {
										$sections[ $return_key_value ] = $return_field;
									} else {
										$sections[] = $return_field;
									}
								} else {
									if ( ! empty( $return_key_value ) ) {
										$sections[ $return_key_value ] = $_instance_object;
									} else {
										$sections[] = $_instance_object;
									}
								}
							}
						}
					}
				}
			}

			return $sections;
		}

		/**
		 * Add instance to static tracking array
		 *
		 * @since 2.4.0
		 */
		final public static function add_section_instance() {
			$section = get_called_class();

			if ( ! isset( self::$_instances[ $section ] ) ) {
				self::$_instances[ $section ] = new $section();
			}
		}

		/**
		 * Initialize self
		 *
		 * @since 2.4.0
		 */
		public function init() {
			if ( ! $this->settings_values_loaded ) {
				$this->load_settings_values();
				$this->after_load_settings_values();
			}

			if ( ! $this->settings_fields_loaded ) {
				$this->load_settings_fields();
			}
		}

		/**
		 * Load the section settings values.
		 *
		 * @since 2.4.0
		 */
		public function load_settings_values() {
			$this->settings_values_loaded = true;

			if ( true === $this->load_options ) {
				$this->setting_option_values = get_option( $this->setting_option_key );
				if ( ( false === $this->setting_option_values ) || ( '' === $this->setting_option_values ) ) {
					// Track that the option value is not set. See after_load_settings_values().
					$this->settings_values_save_on_load = true;
				} else {
					/**
					 * Added to correct issues with Group Leader User capabilities.
					 * See LEARNDASH-5707. See changes in
					 * includes/settings/settings-sections/class-ld-settings-section-groups-group-leader-user.php
					 *
					 * @since 3.4.0.2
					 */
					$gl_user_activate = get_option( 'learndash_groups_group_leader_user_activate', '' );
					if ( ! empty( $gl_user_activate ) ) {
						$this->settings_values_save_on_load = true;
					}
				}
			}
		}

		/**
		 * Load the section settings fields.
		 *
		 * @since 2.4.0
		 */
		public function load_settings_fields() {
			$this->settings_fields_loaded = true;

			foreach ( $this->setting_option_fields as &$setting_option_field ) {
				if ( ! isset( $setting_option_field['type'] ) ) {
					continue;
				}

				$field_instance = LearnDash_Settings_Fields::get_field_instance( $setting_option_field['type'] );
				if ( ( $field_instance ) && ( 'LearnDash_Settings_Fields' === get_parent_class( $field_instance ) ) ) {
					$setting_option_field['setting_option_key'] = $this->setting_option_key;

					if ( ! isset( $setting_option_field['id'] ) ) {
						$setting_option_field['id'] = $setting_option_field['setting_option_key'] . '_' . $setting_option_field['name'];
					}

					if ( ! isset( $setting_option_field['label_for'] ) ) {
						$setting_option_field['label_for'] = $setting_option_field['id'];
					}

					if ( ! isset( $setting_option_field['name_wrap'] ) ) {
						$setting_option_field['name_wrap'] = true;
					}

					if ( ! isset( $setting_option_field['input_show'] ) ) {
						$setting_option_field['input_show'] = true;
					}

					if ( ! isset( $setting_option_field['display_callback'] ) ) {
						$display_ref = LearnDash_Settings_Fields::get_field_instance( $setting_option_field['type'] )->get_creation_function_ref();
						if ( $display_ref ) {
							$setting_option_field['display_callback'] = $display_ref;
						}
					}

					if ( ( ! isset( $setting_option_field['value_type'] ) ) || ( empty( $setting_option_field['value_type'] ) ) ) {
						$setting_option_field['value_type'] = 'sanitize_text_field';
					}
					if ( ! is_callable( $setting_option_field['value_type'] ) ) {
						$setting_option_field['value_type'] = 'sanitize_text_field';
					}

					if ( ! isset( $setting_option_field['validate_callback'] ) ) {
						$validate_ref = LearnDash_Settings_Fields::get_field_instance( $setting_option_field['type'] )->get_validation_function_ref();
						if ( $validate_ref ) {
							$setting_option_field['validate_callback'] = $validate_ref;
						}
					}

					if ( ! isset( $setting_option_field['value_callback'] ) ) {
						$value_ref = LearnDash_Settings_Fields::get_field_instance( $setting_option_field['type'] )->get_value_function_ref();
						if ( $value_ref ) {
							$setting_option_field['value_callback'] = $value_ref;
						}
					}
				}
			}
		}

		/**
		 * Save Section Settings values
		 *
		 * @since 2.4.0
		 */
		public function save_settings_values() {
			$this->settings_values_loaded = false;

			// Turn off the nonce verify logic.
			$this->settings_bypass_nonce_check = true;

			update_option( $this->setting_option_key, $this->setting_option_values );

			// Turn on the nonce verify logic.
			$this->settings_bypass_nonce_check = false;
		}

		/**
		 * Update/Set the Section Settings values after loading.
		 *
		 * This is done to ensure the options DB record is present.
		 *
		 * @since 3.4.0
		 */
		protected function after_load_settings_values() {
			if ( ( true === $this->settings_values_loaded ) && ( true === $this->load_options ) ) {
				if ( true === $this->settings_values_save_on_load ) {
					$this->save_settings_values();

					// Set settings_values_loaded back to true as save_settings_values() will reset to false.
					$this->settings_values_loaded = true;
				}
			}
		}

		/**
		 * Initialize the Settings page.
		 *
		 * @since 2.4.0
		 *
		 * @param string $settings_page_id ID of page being initialized.
		 */
		public function settings_page_init( $settings_page_id = '' ) {

			// Ensure settings_page_id is not empty and that it matches the page_id we want to display this section on.
			if ( ( ! empty( $settings_page_id ) ) && ( $settings_page_id === $this->settings_page_id ) && ( ! empty( $this->setting_option_fields ) ) ) {

				add_settings_section(
					$this->settings_section_key,
					$this->settings_section_label,
					array( $this, 'show_settings_section_description' ),
					$this->settings_page_id
				);

				foreach ( $this->setting_option_fields as $setting_option_field ) {
					if ( ! isset( $setting_option_field['name'] ) ) {
						continue;
					}

					add_settings_field(
						$setting_option_field['name'],
						$setting_option_field['label'],
						$setting_option_field['display_callback'],
						$this->settings_page_id,
						$this->settings_section_key,
						$setting_option_field
					);
				}

				register_setting(
					$this->settings_page_id,
					$this->setting_option_key,
					array( $this, 'settings_section_fields_validate' )
				);
			}
		}

		/**
		 * Show Settings Section Description
		 *
		 * @since 2.4.0
		 */
		public function show_settings_section_description() {

			if ( ! empty( $this->settings_section_description ) ) {
				echo '<div class="ld-metabox-description">' . wp_kses_post( wpautop( $this->settings_section_description ) ) . '</div>';
			}
		}

		/**
		 * Output Settings Section nonce field.
		 *
		 * @since 3.0.0 Initial release.
		 * @since 3.6.0 Added $referer and $echo params.
		 *
		 * @param bool $referer (Optional) Whether to set the referer field for validation. Default value: true @since 3.6.0.
		 * @param bool $echo    (Optional) Whether to display or return hidden form field. Default value: true @since 3.6.0.
		 */
		public function show_settings_section_nonce_field( $referer = true, $echo = true ) {
			wp_nonce_field( $this->setting_option_key, $this->setting_option_key . '_nonce', $referer, $echo );
		}

		/**
		 * Intercept the WP options save logic and check that we have a valid nonce.
		 *
		 * @since 3.0.0
		 *
		 * @param array  $value              Array of section fields values.
		 * @param array  $old_value          Array of old values.
		 * @param string $setting_option_key Section option key should match $this->setting_option_key.
		 */
		public function section_pre_update_option( $value, $old_value, $setting_option_key = '' ) {
			if ( ( empty( $setting_option_key ) ) || ( $setting_option_key !== $this->setting_option_key ) ) {
				return $old_value;
			}

			if ( ! (bool) $this->verify_metabox_nonce_field() ) {
				return $old_value;
			}

			if ( is_array( $value ) ) {
				foreach ( $value as $key => $val ) {
					if (
						'password' === ( $this->setting_option_fields[ $key ]['type'] ?? '' )
						&& $val === self::get_placeholder_for_keys()
					) {
						$value[ $key ] = $old_value[ $key ];
					}
				}
			}

			/**
			 * Filters settings section save fields.
			 *
			 * The dynamic portion of the hook `$section_key` refers to the settings_section_key also
			 * used as option name while saving settings in options table.
			 *
			 * @param array  $value                An array of setting fields values.
			 * @param array  $old_value            An array of setting fields old values.
			 * @param string $settings_section_key Settings section key.
			 * @param string $settings_screen_id   Settings screen ID.
			 */
			$value = apply_filters( 'learndash_settings_section_save_fields_' . $this->setting_option_key, $value, $old_value, $this->settings_section_key, $this->settings_screen_id );

			return $value;
		}

		/**
		 * Filter the section saved values.
		 *
		 * @since 3.6.0
		 *
		 * @param array  $value                An array of setting fields values.
		 * @param array  $old_value            An array of setting fields old values.
		 * @param string $settings_section_key Settings section key.
		 * @param string $settings_screen_id   Settings screen ID.
		 */
		public function filter_section_save_fields( $value, $old_value, $settings_section_key, $settings_screen_id ) {
			return $value;
		}

		/**
		 * Called AFTER section settings are update.
		 *
		 * @since 3.0.0
		 *
		 * @param array  $old_value   Array of section fields values.
		 * @param array  $value       Array of old values.
		 * @param string $section_key Section option key should match $this->setting_option_key.
		 */
		public function section_update_option( $old_value = '', $value = '', $section_key = '' ) {
			if ( ( ! empty( $section_key ) ) && ( $section_key === $this->setting_option_key ) ) {
				// When a section values change we update our internal set and also trigger reload fresh.

				if ( ! defined( 'LEARNDASH_SETTINGS_UPDATING' ) ) {
					/**
					 * Define LearnDash LMS - Set during settings save processing
					 *
					 * @since 3.1.0
					 * @internal Will be set by LearnDash LMS.
					 *
					 * @var bool true when settings are being saved.
					 */
					define( 'LEARNDASH_SETTINGS_UPDATING', true );
				}
				$this->setting_option_values  = $value;
				$this->settings_values_loaded = false;
				$this->settings_fields_loaded = false;
				return true;
			}
		}

		/**
		 * Verify Settings Section nonce field POST value.
		 *
		 * @since 3.0.0
		 */
		public function verify_metabox_nonce_field() {
			if ( true === $this->settings_bypass_nonce_check ) {
				return true;
			}

			if ( ( isset( $_POST[ $this->setting_option_key . '_nonce' ] ) ) && ( ! empty( $_POST[ $this->setting_option_key . '_nonce' ] ) ) && ( wp_verify_nonce( esc_attr( $_POST[ $this->setting_option_key . '_nonce' ] ), $this->setting_option_key ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
				return true;
			}

			return false;
		}

		/**
		 * Show Settings Section reset link
		 *
		 * @since 2.6.0
		 */
		public function show_settings_section_reset_confirm_link() {
			if ( ! empty( $this->reset_confirm_message ) ) {
				$reset_url = add_query_arg(
					array(
						'action'     => 'ld_reset_settings',
						'ld_wpnonce' => wp_create_nonce( get_current_user_id() . '-' . $this->setting_option_key ),
					)
				);
				?>
				<p class="delete-action sfwd_input">
					<a href="<?php echo esc_url( $reset_url ); ?>" class="button-secondary submitdelete deletion" data-confirm="<?php echo esc_html( $this->reset_confirm_message ); ?>"><?php esc_html_e( 'Reset Settings', 'learndash' ); ?></a>
				</p>
				<?php
			}
		}

		/**
		 * Added Settings Section meta box.
		 *
		 * @since 2.4.0
		 *
		 * @param string $settings_screen_id Settings Screen ID.
		 */
		public function add_meta_boxes( $settings_screen_id = '' ) {
			global $learndash_metaboxes;

			if ( $settings_screen_id === $this->settings_screen_id ) {
				$show_section = true;

				/** This filter is documented in includes/settings/class-ld-settings-metaboxes.php */
				$show_section = apply_filters( 'learndash_show_metabox', $show_section, $this->metabox_key, $this->settings_screen_id );

				/**
				 * Filters whether to show settings section.
				 *
				 * @since 3.6.0
				 *
				 * @param boolean $show_section         Whether to show settings metabox.
				 * @param string  $settings_section_key Settings section key.
				 * @param string  $settings_screen_id   Settings screen ID.
				 */
				$show_section = apply_filters( 'learndash_show_section', $show_section, $this->settings_section_key, $this->settings_screen_id );

				if ( true === $show_section ) {
					if ( ! isset( $learndash_metaboxes[ $this->settings_screen_id ] ) ) {
						$learndash_metaboxes[ $this->settings_screen_id ] = array();
					}
					$learndash_metaboxes[ $this->settings_screen_id ][ $this->metabox_key ] = $this->metabox_key;

					add_meta_box(
						$this->metabox_key,
						$this->settings_section_label,
						array( $this, 'show_meta_box' ),
						$this->settings_screen_id,
						$this->metabox_context,
						$this->metabox_priority
					);

					add_filter( 'postbox_classes_' . $this->settings_screen_id . '_' . $this->metabox_key, array( $this, 'add_meta_box_classes' ), 30, 1 );
				}
			}
		}

		/**
		 * Add custom classes to postbox wrapper.
		 *
		 * @since 3.2.3
		 *
		 * @param array $classes Array of classes for postbox.
		 *
		 * @return array $classes.
		 */
		public function add_meta_box_classes( $classes ) {
			if ( ! in_array( 'ld_settings_postbox', $classes, true ) ) {
				$classes[] = 'ld_settings_postbox';
			}

			if ( ! in_array( 'ld_settings_postbox_' . $this->settings_screen_id, $classes, true ) ) {
				$classes[] = 'ld_settings_postbox_' . $this->settings_screen_id;
			}

			if ( ! in_array( 'ld_settings_postbox_' . $this->settings_screen_id . '_' . $this->metabox_key, $classes, true ) ) {
				$classes[] = 'ld_settings_postbox_' . $this->settings_screen_id . '_' . $this->metabox_key;
			}

			return $classes;
		}

		/**
		 * Show Settings Section meta box.
		 *
		 * @since 2.4.0
		 */
		public function show_meta_box() {
			global $wp_settings_sections;

			$this->show_settings_section_nonce_field();

			if ( isset( $wp_settings_sections[ $this->settings_page_id ][ $this->settings_section_key ] ) ) {
				$this->show_settings_section( $wp_settings_sections[ $this->settings_page_id ][ $this->settings_section_key ] );
			} else {
				$this->show_settings_section();
			}
		}

		/**
		 * Show the meta box settings
		 *
		 * @since 2.4.0
		 *
		 * @param string $section Section to be shown.
		 */
		public function show_settings_section( $section = null ) {
			/**
			 * The 'callback' attribute is set if/when the section description is
			 * to be displayed. See the WP add_settings_section() argument #3.
			 */
			if ( ( ! is_null( $section ) ) && ( isset( $section['callback'] ) ) && ( ! empty( $section['callback'] ) ) && ( is_callable( $section['callback'] ) ) ) {
				call_user_func( $section['callback'] );
			}

			// If this section defined its own display callback logic.
			if ( ( isset( $this->settings_fields_callback ) ) && ( ! empty( $this->settings_fields_callback ) ) && ( is_callable( $this->settings_fields_callback ) ) ) {
				call_user_func( $this->settings_fields_callback, $this->settings_page_id, $this->settings_section_key );
			} else {
				/**
				 * Note here we are calling a custom version of the WP function
				 * do_settings_fields because we want to control the label and help icons
				 */

				/**
				 * Fires before settings sections.
				 *
				 * @param string $settings_section_key Settings section key.
				 * @param string $setting_screen_id    Settings screen ID.
				 */
				do_action( 'learndash_section_before', $this->settings_section_key, $this->settings_screen_id );

				echo '<div class="sfwd sfwd_options ' . esc_attr( $this->settings_section_key ) . '">';

				/**
				 * Fires before settings section fields.
				 *
				 * @param string $settings_section_key Settings section key.
				 * @param string $setting_screen_id    Settings screen ID.
				 */
				do_action( 'learndash_section_fields_before', $this->settings_section_key, $this->settings_screen_id );
				$this->show_settings_section_fields( $this->settings_page_id, $this->settings_section_key );

				/**
				 * Fires after the settings section fields.
				 *
				 * @param string $settings_section_key Settings section key.
				 * @param string $setting_screen_id    Settings screen ID.
				 */
				do_action( 'learndash_section_fields_after', $this->settings_section_key, $this->settings_screen_id );

				/**
				 * Fires before settings section reset link.
				 *
				 * @param string $settings_section_key Settings section key.
				 * @param string $setting_screen_id    Settings screen ID.
				 */
				do_action( 'learndash_section_reset_before', $this->settings_section_key, $this->settings_screen_id );
				$this->show_settings_section_reset_confirm_link();

				/**
				 * Fires after settings section reset link.
				 *
				 * @param string $settings_section_key Settings section key.
				 * @param string $setting_screen_id    Settings screen ID.
				 */
				do_action( 'learndash_section_reset_after', $this->settings_section_key, $this->settings_screen_id );

				echo '</div>';

				/**
				 * Fires after settings sections.
				 *
				 * @param string $settings_section_key Settings section key.
				 * @param string $setting_screen_id    Settings screen ID.
				 */
				do_action( 'learndash_section_after', $this->settings_section_key, $this->settings_screen_id );
			}
		}

		/**
		 * Show Settings Section Fields.
		 *
		 * @since 2.4.0
		 *
		 * @param string $page Page shown.
		 * @param string $section Section shown.
		 */
		public function show_settings_section_fields( $page, $section ) {
			global $wp_settings_fields;

			if ( ! isset( $wp_settings_fields[ $page ][ $section ] ) ) {
				return;
			}

			LearnDash_Settings_Fields::show_section_fields( $wp_settings_fields[ $page ][ $section ] );
		}

		/**
		 * This validation function is set via the call to 'register_setting'
		 * and will be called for each section.
		 *
		 * @since 2.4.0
		 *
		 * @param array $post_fields Array of section fields.
		 */
		public function settings_section_fields_validate( $post_fields = array() ) {
			$setting_option_values = array();

			// This validate_args array will be passed to the validation function for context.
			$validate_args = array(
				'settings_page_id'   => $this->settings_page_id,
				'setting_option_key' => $this->setting_option_key,
				'post_fields'        => $post_fields,
				'field'              => null,
			);

			if ( ! empty( $post_fields ) ) {
				foreach ( $post_fields as $key => $val ) {
					if ( isset( $this->setting_option_fields[ $key ] ) ) {
						if ( ( isset( $this->setting_option_fields[ $key ]['validate_callback'] ) ) && ( ! empty( $this->setting_option_fields[ $key ]['validate_callback'] ) ) && ( is_callable( $this->setting_option_fields[ $key ]['validate_callback'] ) ) ) {
							$validate_args['field']        = $this->setting_option_fields[ $key ];
							$setting_option_values[ $key ] = call_user_func( $this->setting_option_fields[ $key ]['validate_callback'], $val, $key, $validate_args );
						}
					}
				}
			}
			return $setting_option_values;
		}

		/**
		 * Static function to get section setting.
		 *
		 * @since 2.4.0
		 *
		 * @param string $field_key Section field key.
		 * @param mixed  $default_return Default value if field not found.
		 */
		public static function get_setting( $field_key = '', $default_return = '' ) {
			return self::get_section_setting( get_called_class(), $field_key, $default_return );
		}

		/**
		 * Static function to set section setting.
		 *
		 * @since 4.0.0
		 *
		 * @param string $field_key   Section field key.
		 * @param mixed  $field_value Section field value.
		 */
		public static function set_setting( $field_key = '', $field_value = '' ) {
			return self::set_section_setting( get_called_class(), $field_key, $field_value );
		}

		/**
		 * Static function to get all section settings.
		 *
		 * @since 2.4.0
		 */
		public static function get_settings_all() {
			return self::get_section_settings_all( get_called_class() );
		}

		/**
		 * Static function to get section to get option label.
		 *
		 * @since 2.4.0
		 *
		 * @param string $field_key Section field key.
		 * @param string $option_key Section option key.
		 */
		public static function get_setting_select_option_label( $field_key = '', $option_key = '' ) {
			return self::get_section_setting_select_option_label( get_called_class(), $field_key, $option_key );
		}

		/**
		 * Static function to get a Section Setting value.
		 *
		 * @since 2.4.0
		 *
		 * @param string $section Settings Section.
		 * @param string $field_key Settings Section field key.
		 * @param mixed  $default_return Default value if field not found.
		 */
		public static function get_section_setting( $section = '', $field_key = '', $default_return = '' ) {
			if ( empty( $section ) ) {
				$section = get_called_class();
			}

			$field_key = self::check_deprecated_field_key( $field_key, $section );
			$section   = self::check_deprecated_class( $section );

			if ( isset( self::$_instances[ $section ] ) ) {
				self::$_instances[ $section ]->init();

				if ( isset( self::$_instances[ $section ]->setting_option_fields[ $field_key ] ) ) {
					$default_return = self::$_instances[ $section ]->setting_option_fields[ $field_key ]['value'];
				} elseif ( isset( self::$_instances[ $section ]->setting_option_values[ $field_key ] ) ) {
					$default_return = self::$_instances[ $section ]->setting_option_values[ $field_key ];
				}
			}

			return $default_return;
		}

		/**
		 * Static function to set a Section Setting value.
		 *
		 * @since 2.5.0
		 *
		 * @param string $section Settings Section.
		 * @param string $field_key Settings Section field key.
		 * @param mixed  $new_value new value for field.
		 */
		public static function set_section_setting( $section = '', $field_key = '', $new_value = '' ) {
			if ( ( ! empty( $section ) ) && ( ! empty( $field_key ) ) ) {

				$field_key = self::check_deprecated_field_key( $field_key, $section );
				$section   = self::check_deprecated_class( $section );

				if ( isset( self::$_instances[ $section ] ) ) {
					self::$_instances[ $section ]->init();

					$value_changed = false;
					if ( isset( self::$_instances[ $section ]->setting_option_fields[ $field_key ] ) ) {
						self::$_instances[ $section ]->setting_option_fields[ $field_key ]['value'] = $new_value;

						$value_changed = true;
					}

					if ( isset( self::$_instances[ $section ]->setting_option_values[ $field_key ] ) ) {
						self::$_instances[ $section ]->setting_option_values[ $field_key ] = $new_value;

						$value_changed = true;
					}

					if ( true === $value_changed ) {
						self::$_instances[ $section ]->save_settings_values();
					}
				}
			}
		}

		/**
		 * Static function to set Section Setting values.
		 *
		 * @since 4.3.0
		 *
		 * @param string $section Settings Section.
		 * @param array  $fields  Settings Section fields.
		 *
		 * @return void
		 */
		public static function set_section_settings_all( string $section, array $fields ): void {
			if ( empty( $section ) || empty( $fields ) ) {
				return;
			}

			$section = self::check_deprecated_class( $section );

			if ( ! isset( self::$_instances[ $section ] ) ) {
				return;
			}

			self::$_instances[ $section ]->init();

			if ( ! isset( self::$_instances[ $section ]->setting_option_values ) ) {
				self::$_instances[ $section ]->setting_option_values = array();
			}

			$value_changed = false;

			foreach ( $fields as $field_key => $new_value ) {
				$field_key = self::check_deprecated_field_key( $field_key, $section );

				if ( isset( self::$_instances[ $section ]->setting_option_fields[ $field_key ] ) ) {
					self::$_instances[ $section ]->setting_option_fields[ $field_key ]['value'] = $new_value;
					self::$_instances[ $section ]->setting_option_values[ $field_key ]          = $new_value;

					$value_changed = true;
				}
			}

			if ( ! $value_changed ) {
				return;
			}

			self::$_instances[ $section ]->save_settings_values();
		}

		/**
		 * Static function to get all Section fields.
		 *
		 * @since 2.4.0
		 *
		 * @param string $section_org Settings Section.
		 * @param string $field       Settings Section field key.
		 */
		public static function get_section_settings_all( $section_org = '', $field = 'value' ) {
			if ( empty( $section_org ) ) {
				$section_org = get_called_class();
			}

			$section = self::check_deprecated_class( $section_org );
			if ( empty( $section ) ) {
				$section = $section_org;
			}

			if ( isset( self::$_instances[ $section ] ) ) {
				self::$_instances[ $section ]->init();
				$fields_values = wp_list_pluck( self::$_instances[ $section ]->setting_option_fields, $field );

				// If we are dealing with deprecated values we provide the old key/value sets as well so easy the logic on the caller.
				if ( $section_org !== $section ) {
					if ( ( isset( self::$global_settings_deprecated[ $section_org ]['fields'] ) ) && ( ! empty( self::$global_settings_deprecated[ $section_org ]['fields'] ) ) ) {
						foreach ( self::$global_settings_deprecated[ $section_org ]['fields'] as $old_field => $new_field ) {
							if ( ( ! isset( $fields_values[ $old_field ] ) ) && ( isset( self::$_instances[ $section ]->setting_option_values[ $new_field ][ $field ] ) ) ) {
								$fields_values[ $old_field ] = self::$_instances[ $section ]->setting_option_fields[ $new_field ][ $field ];
							} elseif ( ( 'value' === $field ) && ( isset( self::$_instances[ $section ]->setting_option_values[ $new_field ] ) ) ) {
								$fields_values[ $old_field ] = self::$_instances[ $section ]->setting_option_values[ $new_field ];
							}
						}
					}
				}

				return $fields_values;
			}
		}

		/**
		 * From a section settings you can access the label used on a select by the option key.
		 *
		 * @since 2.4.0
		 *
		 * @param string $section Settings Section.
		 * @param string $field_key Settings Section field key.
		 * @param string $option_key Option key.
		 */
		public static function get_section_setting_select_option_label( $section = '', $field_key = '', $option_key = '' ) {
			if ( empty( $section ) ) {
				$section = get_called_class();
			}

			if ( ! empty( $field_key ) ) {

				$field_key = self::check_deprecated_field_key( $field_key, $section );
				$section   = self::check_deprecated_class( $section );

				if ( isset( self::$_instances[ $section ] ) ) {
					self::$_instances[ $section ]->init();

					// If the option_key was not passed we default to the current selected value.
					if ( empty( $option_key ) ) {
						$option_key = self::$_instances[ $section ]->get_setting( $field_key );
					}

					// Now we get the option fields by the field_key and then derive the option label from the option_key.
					if ( ( isset( self::$_instances[ $section ]->setting_option_fields[ $field_key ] ) ) && ( 'select' === self::$_instances[ $section ]->setting_option_fields[ $field_key ]['type'] ) && ( self::$_instances[ $section ]->setting_option_fields[ $field_key ]['options'] ) && ( isset( self::$_instances[ $section ]->setting_option_fields[ $field_key ]['options'][ $option_key ] ) ) ) {
						return self::$_instances[ $section ]->setting_option_fields[ $field_key ]['options'][ $option_key ];
					}
				}
			}
		}

		/**
		 * Transition settings from old class into current one.
		 *
		 * @since 3.0.0
		 */
		public function transition_deprecated_settings() {
			if ( ! empty( $this->settings_deprecated ) ) {
				foreach ( $this->settings_deprecated as $old_class => $old_class_settings ) {
					if ( ( isset( $old_class_settings['option_key'] ) ) && ( ! empty( $old_class_settings['option_key'] ) ) ) {
						$old_settings_values = get_option( $old_class_settings['option_key'] );
						if ( ( isset( $old_class_settings['fields'] ) ) && ( ! empty( $old_class_settings['fields'] ) ) ) {
							foreach ( $old_class_settings['fields'] as $old_setting_key => $new_setting_key ) {
								if ( isset( $old_settings_values[ $old_setting_key ] ) ) {
									$this->setting_option_values[ $new_setting_key ] = $old_settings_values[ $old_setting_key ];
								}
							}
						}
					}
				}
			}
		}

		/**
		 * Transition old Settings Class to new one.
		 *
		 * @since 3.0.0
		 *
		 * @param string $section Old section class.
		 * @return string New section Class.
		 */
		public static function check_deprecated_class( $section = '' ) {
			if ( ! empty( $section ) ) {
				if ( isset( self::$global_settings_deprecated[ $section ] ) ) {
					$section = self::$global_settings_deprecated[ $section ]['class'];
				}
			}

			return $section;
		}

		/**
		 * Transition old Settings Class field(s) to new one(s).
		 *
		 * @since 3.0.0
		 *
		 * @param string $field_key Old section field key.
		 * @param string $old_section Old section class.
		 *
		 * @return string new field key.
		 */
		public static function check_deprecated_field_key( $field_key = '', $old_section = '' ) {
			if ( ( ! empty( $old_section ) ) && ( isset( self::$global_settings_deprecated[ $old_section ] ) ) ) {
				if ( isset( self::$global_settings_deprecated[ $old_section ]['fields'] ) ) {
					$section_fields = self::$global_settings_deprecated[ $old_section ]['fields'];
					if ( ! empty( $section_fields ) ) {
						if ( isset( $section_fields[ $field_key ] ) ) {
							$field_key = $section_fields[ $field_key ];
						}
					}
				}
			}

			return $field_key;
		}

		/**
		 * Get the Settings Section sub label.
		 *
		 * @since 3.6.0
		 */
		public function get_settings_section_sub_label() {
			if ( empty( $this->settings_section_sub_label ) ) {
				$this->settings_section_sub_label = $this->settings_section_label;
			}

			return $this->settings_section_sub_label;
		}

		// End of functions.
	}
}