class-ld-settings-metaboxes.php 32.3 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
<?php
/**
 * LearnDash Settings Metabox Abstract Class.
 *
 * @package LearnDash\Settings
 */

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

if ( ! class_exists( 'LearnDash_Settings_Metabox' ) ) {
	/**
	 * Class for LearnDash Settings Sections.
	 */
	class LearnDash_Settings_Metabox {

		/**
		 * Static array of section instances.
		 *
		 * @var array $_instances
		 */
		protected static $_instances = array(); // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore

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

		/**
		 * Store for all the sub fields in this section
		 *
		 * @var array $settings_sub_option_fields Array of section sub fields.
		 */
		protected $settings_sub_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 for if settings fields have been loaded.
		 *
		 * @var boolean $settings_fields_loaded Flag.
		 */

		protected $settings_fields_loaded = false;

		/**
		 * This is used as the option_name when the settings
		 * are saved to the options table.
		 *
		 * @var string $settings_section_key
		 */
		protected $settings_metabox_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 = '';

		/**
		 * 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 = '';


		/**
		 * Map internal settings field ID to legacy field ID.
		 *
		 * @var array $settings_fields_map
		 */
		protected $settings_fields_map = array();

		/**
		 * Settings screen ID
		 *
		 * @var string
		 */
		protected $settings_screen_id = '';

		/**
		 * Legacy Settings fields.
		 *
		 * @var array $settings_fields_legacy
		 */
		protected $settings_fields_legacy = array();

		/**
		 * Legacy Settings values.
		 *
		 * @var array $settings_values_legacy
		 */
		protected $settings_values_legacy = array();

		/**
		 * Current Post being edited.
		 *
		 * @var WP_Post|null $_post WP_Post object.
		 */
		protected $_post = null; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore

		/**
		 * Current metabox
		 *
		 * @var object $_metabox Metabox object.
		 */
		protected $_metabox = null; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore

		/**
		 * Public constructor for class
		 */
		public function __construct() {
			add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );

			add_filter(
				$this->settings_screen_id . '_display_settings',
				array( $this, 'display_settings_filter' ),
				100,
				3
			);
		}

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

		/**
		 * Add instance to static tracking array
		 *
		 * @since 2.4.0
		 */
		final public static function add_metabox_instance() {
			$metabox_key = get_called_class();
			if ( ! isset( self::$_instances[ $metabox_key ] ) ) {
				self::$_instances[ $metabox_key ] = new $metabox_key();
			}
			return self::$_instances[ $metabox_key ];
		}

		/**
		 * Initialize Metabox instance.
		 *
		 * @param WP_Post $post Post object instance to initialize instance.
		 * @param boolean $force True to force init. This will load values and settings again.
		 */
		public function init( $post = null, $force = false ) {
			if ( ( ! $post ) || ( ! is_a( $post, 'WP_Post' ) ) ) {
				return false;
			}

			if ( $post->post_type !== $this->settings_screen_id ) {
				return false;
			}

			if ( ( ! is_a( $this->_post, 'WP_Post' ) ) || ( absint( $this->_post->ID ) !== absint( $post->ID ) ) ) {
				$force = true;
			}

			$this->_post = $post;

			if ( true === $force ) {
				$this->settings_values_loaded = false;
				$this->settings_fields_loaded = false;
			}

			if ( ! $this->settings_values_loaded ) {
				$this->load_settings_values();
			}

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

		/**
		 * Load the section settings values.
		 */
		public function load_settings_values() {

			if ( ( is_admin() ) && ( function_exists( 'get_current_screen' ) ) ) {
				$screen = get_current_screen();
				if ( ! $screen || $screen->id !== $this->settings_screen_id ) {
					return false;
				}
			}

			if ( ( ! $this->_post ) || ( ! is_a( $this->_post, 'WP_Post' ) ) ) {
				return false;
			}

			if ( $this->_post->post_type !== $this->settings_screen_id ) {
				return false;
			}

			$setting_values = learndash_get_setting( $this->_post );

			if ( ! empty( $setting_values ) ) {
				foreach ( $this->settings_fields_map as $_internal => $_legacy ) {
					if ( isset( $setting_values[ $_legacy ] ) ) {
						$this->setting_option_values[ $_internal ]                                    = $setting_values[ $_legacy ];
						$this->settings_values_legacy[ $this->settings_screen_id . '_' . $_internal ] = $setting_values[ $_legacy ];
					} else {
						$this->setting_option_values[ $_internal ] = '';
					}
				}
			}

			$this->settings_values_loaded = true;

			return true;
		}

		/**
		 * Get save settings fields map from post values.
		 *
		 * @param array $post_values Post values.
		 * @return array
		 */
		public function get_save_settings_fields_map_form_post_values( $post_values = array() ) {
			return $this->settings_fields_map;
		}

		/**
		 * Load the section settings fields.
		 */
		public function load_settings_fields() {
			if ( ! empty( $this->setting_option_fields ) ) {
				foreach ( $this->setting_option_fields as $setting_option_key => &$setting_option_field ) {
					$setting_option_field = $this->load_settings_field( $setting_option_field );
				}
			}

			/**
			 * We only set the $settings_fields_loaded var if we are loading
			 * on a normal screen.
			 */
			if ( ( ! $this->_post ) || ( ! is_a( $this->_post, 'WP_Post' ) ) ) {
				return false;
			}

			if ( $this->_post->post_type !== $this->settings_screen_id ) {
				return false;
			}

			$this->settings_fields_loaded = true;
		}

		/**
		 * Load settings fields
		 *
		 * @param array $setting_option_field Settings option field.
		 *
		 * @return array
		 */
		public function load_settings_field( $setting_option_field = array() ) {
			if ( ! isset( $setting_option_field['type'] ) ) {
				return $setting_option_field;
			}

			$field_ref = LearnDash_Settings_Fields::get_field_instance( $setting_option_field['type'] );
			if ( ! $field_ref ) {
				return $setting_option_field;
			}

			$setting_option_field['setting_option_key'] = $this->settings_metabox_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['label_none'] ) ) {
				$setting_option_field['label_none'] = false;
			}

			if ( ! isset( $setting_option_field['label_full'] ) ) {
				$setting_option_field['label_full'] = false;
			}

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

			if ( ! isset( $setting_option_field['input_full'] ) ) {
				$setting_option_field['input_full'] = false;
			}

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

			if ( ! isset( $setting_option_field['value'] ) ) {
				$setting_option_field['value'] = '';
			}
			if ( ( ! isset( $setting_option_field['value'] ) ) || ( empty( $setting_option_field['value'] ) ) ) {
				if ( 'radio' === $setting_option_field['type'] ) {
					if ( isset( $setting_option_field['default'] ) ) {
						$setting_option_field['value'] = $setting_option_field['default'];
					}
				}
			}

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

			if ( ! isset( $setting_option_field['display_callback'] ) ) {
				$display_ref = $field_ref->get_creation_function_ref();
				if ( $display_ref ) {
					$setting_option_field['display_callback'] = $display_ref;
				}
			}

			if ( ! isset( $setting_option_field['validate_callback'] ) ) {
				$validate_ref = $field_ref->get_validation_function_ref();
				if ( $validate_ref ) {
					$setting_option_field['validate_callback'] = $validate_ref;
				}
			}

			if ( ! isset( $setting_option_field['value_callback'] ) ) {
				$value_ref = $field_ref->get_value_function_ref();
				if ( $value_ref ) {
					$setting_option_field['value_callback'] = $value_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';
			}

			// Now we reorganize the field.
			if ( ! isset( $setting_option_field['args'] ) ) {
				$setting_option_field['args'] = array();
				foreach ( $setting_option_field as $field_key => $field_val ) {

					$setting_option_field['args'][ $field_key ] = $field_val;
					if ( 'label' === $field_key ) {
						$setting_option_field['title'] = $field_val;
					}

					if ( ! in_array( $field_key, array( 'id', 'name', 'args', 'callback' ), true ) ) {
						unset( $setting_option_field[ $field_key ] );
					}
				}
			}
			return $setting_option_field;
		}

		/**
		 * Show Settings Section Description
		 */
		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>';
			}
		}

		/**
		 * Added Settings Section meta box.
		 *
		 * @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 ) {
				/**
				 * Filters whether to show settings section meta box.
				 *
				 * @param boolean $show_metabox       Whether to show settings metabox.
				 * @param string  $settings_key       Settings key used as option name while saving settings.
				 * @param string  $settings_screen_id Settings screen ID.
				 */
				if ( apply_filters( 'learndash_show_metabox', true, $this->settings_metabox_key, $this->settings_screen_id ) ) {

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

					add_meta_box(
						$this->settings_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->settings_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->settings_metabox_key, $classes, true ) ) {
				$classes[] = 'ld_settings_postbox_' . $this->settings_screen_id . '_' . $this->settings_metabox_key;
			}

			return $classes;
		}

		/**
		 * Show Settings Section meta box.
		 *
		 * @param WP_Post $post Post.
		 * @param object  $metabox Metabox.
		 */
		public function show_meta_box( $post = null, $metabox = null ) {
			if ( $post ) {
				$this->init( $post );
				$this->show_metabox_nonce_field();
				$this->show_settings_metabox( $this );
			}
		}

		/**
		 * Output Metabox nonce field.
		 */
		public function show_metabox_nonce_field() {
			wp_nonce_field( $this->settings_metabox_key, $this->settings_metabox_key . '[nonce]' );
		}

		/**
		 * Verify Metabox nonce field POST value.
		 */
		public function verify_metabox_nonce_field() {
			if ( ( isset( $_POST[ $this->settings_metabox_key ]['nonce'] ) ) && ( ! empty( $_POST[ $this->settings_metabox_key ]['nonce'] ) ) && ( wp_verify_nonce( esc_attr( $_POST[ $this->settings_metabox_key ]['nonce'] ), $this->settings_metabox_key ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
				return true;
			}
		}

		/**
		 * Save Settings Metabox
		 *
		 * @param integer $post_id $Post ID is post being saved.
		 * @param object  $saved_post WP_Post object being saved.
		 * @param boolean $update If update true, otherwise false.
		 * @param array   $settings_field_updates array of settings fields to update.
		 */
		public function save_post_meta_box( $post_id = 0, $saved_post = null, $update = null, $settings_field_updates = null ) {
			if ( is_null( $settings_field_updates ) ) {
				$settings_field_updates = $this->get_post_settings_field_updates( $post_id, $saved_post, $update );
			}
			if ( ( ! empty( $settings_field_updates ) ) && ( is_array( $settings_field_updates ) ) ) {
				foreach ( $settings_field_updates as $_key => $_val ) {
					learndash_update_setting( $saved_post, $_key, $_val );
				}
			}
		}

		/**
		 * Save fields to post
		 *
		 * @param object $pro_quiz_edit   WpProQuiz_Controller_Quiz instance (not used).
		 * @param array  $settings_values Settings values.
		 *
		 * @return void;
		 */
		public function save_fields_to_post( $pro_quiz_edit, $settings_values = array() ) {}

		/**
		 * Get Settings Metabox post updates.
		 *
		 * @since 3.4.0
		 *
		 * @param integer $post_id $Post ID is post being saved.
		 * @param object  $saved_post WP_Post object being saved.
		 * @param boolean $update If update true, otherwise false.
		 *
		 * @return array Array of settings fields to update.
		 */
		public function get_post_settings_field_updates( $post_id = 0, $saved_post = null, $update = null ) {
			$settings_field_updates = array();

			if ( ( $saved_post ) && ( is_a( $saved_post, 'WP_Post' ) ) && ( $saved_post->post_type === $this->settings_screen_id ) ) {
				// nonce verify performed in the parent::verify_metabox_nonce_field() function.
				// phpcs:ignore WordPress.Security.NonceVerification.Missing
				if ( ( true === $this->verify_metabox_nonce_field() ) && ( isset( $_POST[ $this->settings_metabox_key ] ) ) ) {
					$post_values = $_POST[ $this->settings_metabox_key ]; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

					$this->init( $saved_post );
					$settings_field_updates = $this->validate_metabox_settings_post_updates( $post_values );
				}
			}

			$settings_field_updates = $this->trigger_metabox_settings_post_filters( $settings_field_updates );

			return $settings_field_updates;
		}

		/**
		 * Validate Settings Metabox post updates.
		 *
		 * @since 3.4.0
		 *
		 * @param array $post_values Array of settings values to validate.
		 *
		 * @return array Array of validated settings values.
		 */
		public function validate_metabox_settings_post_updates( $post_values = array() ) {
			$settings_field_updates = array();

			$settings_fields_map = $this->get_save_settings_fields_map_form_post_values( $post_values );

			if ( ! empty( $settings_fields_map ) ) {

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

				foreach ( $settings_fields_map as $_internal => $_legacy ) {
					$settings_field = $this->get_settings_field_by_key( $_internal );
					if ( $settings_field ) {
						if ( isset( $post_values[ $_internal ] ) ) {
							$post_value = $post_values[ $_internal ];
						} else {
							$post_value = '';
						}

						$validate_args['field'] = $settings_field['args'];

						if ( ( isset( $settings_field['args']['value_callback'] ) ) && ( ! empty( $settings_field['args']['value_callback'] ) ) && ( is_callable( $settings_field['args']['value_callback'] ) ) ) {
							$post_value = call_user_func( $settings_field['args']['value_callback'], $post_value, $_internal, $validate_args, $post_values );
						} else {
							$post_value = esc_attr( $post_value );
						}

						if ( ( isset( $settings_field['args']['validate_callback'] ) ) && ( ! empty( $settings_field['args']['validate_callback'] ) ) && ( is_callable( $settings_field['args']['validate_callback'] ) ) ) {
							$post_value = call_user_func( $settings_field['args']['validate_callback'], $post_value, $_internal, $validate_args );
						} else {
							$post_value = esc_attr( $post_value );
						}
						$settings_field_updates[ $_legacy ] = $post_value;
					}
				}
			}

			return $settings_field_updates;
		}

		/**
		 * Trigger Filters forSettings Metabox post updates.
		 *
		 * @since 3.4.0
		 *
		 * @param array $settings_field_updates Array of Settings field.
		 *
		 * @return array Array of Settings field.
		 */
		public function trigger_metabox_settings_post_filters( $settings_field_updates = array() ) {
			/**
			 * Filters settings meta box save fields.
			 *
			 * @param array  $settings_field_updates An array of setting fields data.
			 * @param string $settings_key          Settings key used as option name while saving settings.
			 * @param string $settings_screen_id    Settings screen ID.
			 */
			$settings_field_updates = apply_filters( 'learndash_metabox_save_fields', $settings_field_updates, $this->settings_metabox_key, $this->settings_screen_id );
			/**
			 * Filters settings meta box save fields.
			 *
			 * The dynamic portion of the hook `$this->settings_metabox_key` refers to the settings key also
			 * used as option name while saving settings in options table.
			 *
			 * @param array  $settings_field_updates An array of setting fields data.
			 * @param string $settings_key          Settings key used as option name while saving settings.
			 * @param string $settings_screen_id    Settings screen ID.
			 */
			$settings_field_updates = apply_filters( 'learndash_metabox_save_fields_' . $this->settings_metabox_key, $settings_field_updates, $this->settings_metabox_key, $this->settings_screen_id );

			return $settings_field_updates;
		}

		/**
		 * Show the meta box settings
		 *
		 * @param object $metabox Metabox.
		 */
		public function show_settings_metabox( $metabox = null ) {
			if ( ( $metabox ) && ( is_object( $metabox ) ) && ( isset( self::$_instances[ get_class( $metabox ) ] ) ) ) {
				// If this section defined its own display callback logic.
				if ( ( isset( $metabox->settings_fields_callback ) ) && ( ! empty( $metabox->settings_fields_callback ) ) && ( is_callable( $metabox->settings_fields_callback ) ) ) {
					call_user_func( $metabox->settings_fields_callback, $this->settings_metabox_key );
				} else {
					/**
					 * Fires before the metabox description.
					 *
					 * @param string $settings_key Settings key used as option name while saving settings.
					 */
					do_action( 'learndash_metabox_description_before', $metabox->settings_metabox_key );
					$this->show_settings_section_description();

					/**
					 * Fires after the metabox description.
					 *
					 * @param string $settings_key Settings key used as option name while saving settings.
					 */
					do_action( 'learndash_metabox_description_after', $metabox->settings_metabox_key );

					if ( learndash_get_post_type_slug( 'quiz' ) === $this->settings_screen_id ) {
						echo '<div class="wrap wpProQuiz_quizEdit">';
					}

					/**
					 * Fires before metabox options div.
					 *
					 * @param string $settings_key Settings key used as option name while saving settings.
					 */
					do_action( 'learndash_metabox_options_div_before', $metabox->settings_metabox_key );
					echo '<div class="sfwd sfwd_options ' . esc_attr( $metabox->settings_metabox_key ) . '">';

					/**
					 * Fires inside the meta box options div at the top.
					 *
					 * @param string $settings_key Settings key used as option name while saving settings.
					 */
					do_action( 'learndash_metabox_options_div_inside_top', $metabox->settings_metabox_key );
					$this->show_settings_metabox_fields( $metabox );

					/**
					 * Fires inside the meta box options div at the bottom.
					 */
					do_action( 'learndash_metabox_options_div_inside_bottom', $metabox->settings_metabox_key );
					echo '</div>';

					/**
					 * Fires after the meta box options div.
					 *
					 * @param string $settings_key Settings key used as option name while saving settings.
					 */
					do_action( 'learndash_metabox_options_div_after', $metabox->settings_metabox_key );

					if ( learndash_get_post_type_slug( 'quiz' ) === $this->settings_screen_id ) {
						echo '</div>';
					}
				}
			}
		}

		/**
		 * Show Settings Section Fields.
		 *
		 * @param object $metabox Metabox.
		 */
		protected function show_settings_metabox_fields( $metabox = null ) {
			if ( $metabox ) {
				LearnDash_Settings_Fields::show_section_fields( $metabox->setting_option_fields );
			}
		}

		/**
		 * Get Settings Metabox Fields.
		 *
		 * @return array Array of settings fields.
		 */
		public function get_settings_metabox_fields() {
			return $this->setting_option_fields;
		}

		/**
		 * Get Settings Metabox Values.
		 *
		 * @return array Array of settings values.
		 */
		public function get_settings_metabox_values() {
			return $this->setting_option_values;
		}

		/**
		 * Get Settings Metabox Field Value by key.
		 *
		 * @since 3.4.0
		 *
		 * @param string $field_key Settings Field Key for value.
		 *
		 * @return mixed.
		 */
		public function get_metabox_settings_value_by_key( $field_key = '' ) {
			if ( ! empty( $field_key ) ) {
				if ( ! $this->settings_values_loaded ) {
					$this->load_settings_values();
				}

				if ( isset( $this->setting_option_values[ $field_key ] ) ) {
					return $this->setting_option_values[ $field_key ];
				}
			}
		}

		/**
		 * Update Metabox Settings values.
		 *
		 * @since 3.4.0
		 *
		 * @param array $settings_field_updates Array of key/value settings changes.
		 */
		public function apply_metabox_settings_fields_changes( $settings_field_updates = array() ) {
			$settings_field_values = $this->get_settings_metabox_values();
			if ( ! empty( $settings_field_updates ) ) {
				foreach ( $settings_field_updates as $setting_key => $setting_value ) {
					if ( ( isset( $settings_field_values[ $setting_key ] ) ) && ( $settings_field_values[ $setting_key ] !== $setting_value ) ) {
						$settings_field_values[ $setting_key ] = $setting_value;
					}
				}
			}

			return $settings_field_values;
		}

		/**
		 * Filter the legacy settings fields when display to remove items
		 * handled by this metabox,
		 *
		 * @since 3.0
		 * @param array  $settings_fields Array of settings fields.
		 * @param string $location Screen/Post Type location.
		 * @param array  $settings_values Array of current field values.
		 */
		public function display_settings_filter( $settings_fields = array(), $location = '', $settings_values = array() ) {
			if ( ( $location === $this->settings_screen_id ) && ( ! empty( $settings_fields ) ) ) {

				$this->settings_fields_legacy = array();
				$this->settings_values_legacy = array();

				foreach ( $settings_fields as $setting_field_key => $setting_field ) {
					$settings_field_key_name = str_replace( $this->settings_screen_id . '_', '', $setting_field_key );

					$settings_key = array_search( $settings_field_key_name, $this->settings_fields_map, true );
					if ( false !== $settings_key ) {
						$this->settings_fields_legacy[ $settings_field_key_name ] = $setting_field;
						unset( $settings_fields[ $setting_field_key ] );
					}
				}

				foreach ( $settings_values as $setting_value_key => $setting_value ) {
					$settings_value_key_name = str_replace( $this->settings_screen_id . '_', '', $setting_value_key );

					$settings_key = array_search( $settings_value_key_name, $this->settings_fields_map, true );
					if ( false !== $settings_key ) {
						$this->settings_values_legacy[ $settings_value_key_name ] = $setting_value;
					}
				}
			}

			return $settings_fields;
		}

		/**
		 * Get settings by field key
		 *
		 * @param string $field_key Field key.
		 */
		public function get_settings_field_by_key( $field_key = '' ) {
			if ( ! empty( $field_key ) ) {
				if ( ! empty( $this->setting_option_fields ) ) {
					if ( isset( $this->setting_option_fields[ $field_key ] ) ) {
						return $this->setting_option_fields[ $field_key ];
					}
				}

				if ( ! empty( $this->settings_sub_option_fields ) ) {
					foreach ( $this->settings_sub_option_fields as $sub_option_key => $sub_option_fields ) {
						if ( isset( $sub_option_fields[ $field_key ] ) ) {
							return $sub_option_fields[ $field_key ];
						}
					}
				}
			}
		}

		/**
		 * Initialize quiz edit.
		 *
		 * @param WP_Post $post            Post object.
		 * @param boolean $reload_pro_quiz Whether to reload the quiz.
		 *
		 * @return object
		 */
		public function init_quiz_edit( $post, $reload_pro_quiz = false ) {
			static $pro_quiz_edit = array();

			if ( ( $post ) && ( is_a( $post, 'WP_Post' ) ) ) {
				$quiz_mapper         = new WpProQuiz_Model_QuizMapper();
				$prerequisite_mapper = new WpProQuiz_Model_PrerequisiteMapper();
				$form_mapper         = new WpProQuiz_Model_FormMapper();

				$pro_quiz_id = absint( learndash_get_setting( $post->ID, 'quiz_pro' ) );
				$pro_quiz_id = absint( $pro_quiz_id );

				// Note: $pro_quiz_id is allowed to be zero here.
				if ( ( true === $reload_pro_quiz ) && ( isset( $pro_quiz_edit[ $pro_quiz_id ] ) ) ) {
					unset( $pro_quiz_edit[ $pro_quiz_id ] );
				}

				if ( ! isset( $pro_quiz_edit[ $pro_quiz_id ] ) ) {
					if ( ! empty( $pro_quiz_id ) ) {
						$pro_quiz_edit[ $pro_quiz_id ] = array(
							'quiz'                 => $quiz_mapper->fetch( $pro_quiz_id ),
							'prerequisiteQuizList' => $prerequisite_mapper->fetchQuizIds( $pro_quiz_id ),
							'forms'                => $form_mapper->fetch( $pro_quiz_id ),
						);
						$pro_quiz_edit[ $pro_quiz_id ]['quiz']->setPostId( absint( $post->ID ) );

					} else {
						$pro_quiz_edit[ $pro_quiz_id ] = array(
							'quiz'                 => $quiz_mapper->fetch( 0 ),
							'prerequisiteQuizList' => $prerequisite_mapper->fetchQuizIds( 0 ),
							'forms'                => $form_mapper->fetch( 0 ),
						);
					}

					// phpcs:ignore WordPress.Security.NonceVerification.Recommended
					if ( ( isset( $_GET['templateLoadId'] ) ) && ( ! empty( $_GET['templateLoadId'] ) ) ) {
						// phpcs:ignore WordPress.Security.NonceVerification.Recommended
						$template_id = absint( $_GET['templateLoadId'] );
						if ( ! empty( $template_id ) ) {
							$template_mapper = new WpProQuiz_Model_TemplateMapper();
							$template        = $template_mapper->fetchById( $template_id );
							$data            = $template->getData();
							if ( null !== $data ) {
								if ( ( isset( $data['quiz'] ) ) && ( is_a( $data['quiz'], 'WpProQuiz_Model_Quiz' ) ) ) {
									$data['quiz']->setId( $pro_quiz_edit[ $pro_quiz_id ]['quiz']->getId() );
									$data['quiz']->setPostId( $post->ID );
									$data['quiz']->setName( $pro_quiz_edit[ $pro_quiz_id ]['quiz']->getName() );
									$data['quiz']->setText( 'AAZZAAZZ' ); // cspell:disable-line.
								} else {
									$data['quiz'] = $quiz_mapper->fetch( 0 );
								}

								if ( ! isset( $data['forms'] ) ) {
									$data['forms'] = array();
								}

								if ( ! isset( $data['prerequisiteQuizList'] ) ) {
									$data['prerequisiteQuizList'] = array();
								}

								if ( isset( $data[ '_' . learndash_get_post_type_slug( 'quiz' ) ] ) ) {
									$quiz_postmeta = $data[ '_' . learndash_get_post_type_slug( 'quiz' ) ];
									// phpcs:ignore WordPress.Security.NonceVerification.Recommended
									if ( ( ! isset( $_GET['templateLoadReplaceCourse'] ) ) || ( 'on' !== $_GET['templateLoadReplaceCourse'] ) ) {
										if ( isset( $quiz_postmeta['course'] ) ) {
											$quiz_postmeta['course'] = absint( learndash_get_setting( $post->ID, 'course' ) );
										}
										if ( isset( $quiz_postmeta['lesson'] ) ) {
											$quiz_postmeta['lesson'] = absint( learndash_get_setting( $post->ID, 'lesson' ) );
										}
									}

									// phpcs:ignore WordPress.Security.NonceVerification.Recommended
									if ( ( ! isset( $_GET['templateLoadReplaceQuestions'] ) ) || ( 'on' !== $_GET['templateLoadReplaceQuestions'] ) ) {
										if ( isset( $quiz_postmeta['quiz_pro'] ) ) {
											$quiz_postmeta['quiz_pro'] = absint( learndash_get_setting( $post->ID, 'quiz_pro' ) );
										}
									}
									$data[ '_' . learndash_get_post_type_slug( 'quiz' ) ] = $quiz_postmeta;

									foreach ( $this->settings_fields_map as $_internal => $_external ) {
										if ( isset( $data[ '_' . learndash_get_post_type_slug( 'quiz' ) ][ $_external ] ) ) {
											$this->setting_option_values[ $_internal ] = $data[ '_' . learndash_get_post_type_slug( 'quiz' ) ][ $_external ];
										}
									}
								} else {
									$quiz_postmeta = array();
								}

								$pro_quiz_edit[ $pro_quiz_id ] = array(
									'quiz'                 => $data['quiz'],
									'prerequisiteQuizList' => $data['prerequisiteQuizList'],
									'forms'                => $data['forms'],
									'quiz_postmeta'        => $quiz_postmeta,
								);
							}
						}
					}
				}

				return $pro_quiz_edit[ $pro_quiz_id ];
			}

			return null;
		}

		/**
		 * Check legacy metabox fields
		 *
		 * @param array $legacy_fields Array of legacy fields.
		 *
		 * @return array
		 */
		public function check_legacy_metabox_fields( $legacy_fields = array() ) {
			if ( ! empty( $legacy_fields ) ) {
				foreach ( $legacy_fields as $field_key => $field_value ) {

					if ( in_array( $field_key, $this->settings_fields_map, true ) ) {
						unset( $legacy_fields[ $field_key ] );
					}
				}
			}

			return $legacy_fields;
		}

		/**
		 * Generate the JSON data attribute for select2 AJAX.
		 *
		 * @since 3.2.3
		 *
		 * @param array $field_settings Field Settings encoded array.
		 *
		 * @return string HTML output.
		 */
		protected function build_settings_select2_lib_ajax_fetch_json( $field_settings = array() ) {
			$settings_element_json   = wp_json_encode( $field_settings['settings_element'], JSON_FORCE_OBJECT );
			$field_settings['nonce'] = wp_create_nonce( $settings_element_json );

			return htmlspecialchars( wp_json_encode( $field_settings, JSON_FORCE_OBJECT ) );
		}

		// End of functions.
	}
}