class-ld-settings-fields-password.php 3.23 KB
<?php
/**
 * LearnDash Input Text Settings Field.
 *
 * @since 4.6.0
 * @package LearnDash\Settings\Field
 */

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

if ( class_exists( 'LearnDash_Settings_Fields' ) && ! class_exists( 'LearnDash_Settings_Fields_Password' ) ) {

	/**
	 * Class LearnDash Input Text Settings Field.
	 *
	 * @since 4.6.0
	 * @uses LearnDash_Settings_Fields
	 */
	class LearnDash_Settings_Fields_Password extends LearnDash_Settings_Fields {

		/**
		 * Public constructor for class.
		 *
		 * @since 4.6.0
		 */
		public function __construct() {
			$this->field_type = 'password';

			parent::__construct();
		}

		/**
		 * Function to crete the settings field.
		 *
		 * @since 4.6.0
		 *
		 * @param array<string, mixed> $field_args An array of field arguments used to process the output.
		 * @return void
		 */
		public function create_section_field( $field_args = array() ) {

			/** This filter is documented in includes/settings/settings-fields/class-ld-settings-fields-checkbox-switch.php */
			$field_args = apply_filters( 'learndash_settings_field', $field_args );

			/** This filter is documented in includes/settings/settings-fields/class-ld-settings-fields-checkbox-switch.php */
			$html = apply_filters( 'learndash_settings_field_html_before', '', $field_args );

			if ( ( isset( $field_args['value_prefix'] ) ) && ( ! empty( $field_args['value_prefix'] ) ) ) {
				$html .= '<span class="learndash_settings_field_text_prefix ' . $this->get_field_attribute_class( $field_args, false ) . '">' . $field_args['value_prefix'];

			}

			$html .= '<input autocomplete="off" ';
			$html .= $this->get_field_attribute_type( $field_args );
			$html .= $this->get_field_attribute_name( $field_args );
			$html .= $this->get_field_attribute_id( $field_args );
			$html .= $this->get_field_attribute_class( $field_args );
			$html .= $this->get_field_attribute_placeholder( $field_args );
			$html .= $this->get_field_attribute_misc( $field_args );
			$html .= $this->get_field_attribute_required( $field_args );

			if ( isset( $field_args['value'] ) ) {
				$html .= ' value="' . esc_attr( $field_args['value'] ) . '" ';
			} else {
				$html .= ' value="" ';
			}
			$html .= ' />';

			if ( ( isset( $field_args['value_prefix'] ) ) && ( ! empty( $field_args['value_prefix'] ) ) ) {
				$html .= '</span>';
			}

			$html .= $this->get_field_attribute_input_label( $field_args );

			/** This filter is documented in includes/settings/settings-fields/class-ld-settings-fields-checkbox-switch.php */
			$html = apply_filters( 'learndash_settings_field_html_after', $html, $field_args );

			echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Need to output HTML
		}

		/**
		 * Validate field.
		 *
		 * @since 4.6.0
		 *
		 * @param mixed        $val  Value to validate.
		 * @param string       $key  Key of value being validated.
		 * @param array<mixed> $args Array of field args.
		 *
		 * @return string value.
		 */
		public function validate_section_field( $val, $key, $args = array() ) {
			return is_scalar( $val ) ? sanitize_text_field( strval( $val ) ) : '';
		}

		// End of functions.
	}
}
add_action(
	'learndash_settings_sections_fields_init',
	function() {
		LearnDash_Settings_Fields_Password::add_field_instance( 'password' );
	}
);