<commit_before><?php

if ( ! class_exists( 'Kirki_Field' ) ) {
	class Kirki_Field {

		private $args = null;

		public function __construct( $config_id = 'global', $args = array() ) {
			$this->add_field( $config_id, $args );;
		}

		private function add_field( $config_id = 'global', $args = array() ) {

			// Sanitize $config_id
			$args      = $this->sanitize_config_id( $config_id, $args );
			$config_id = $args['kirki_config'];

			// Get the config arguments
			$config = Kirki::$config[ $config_id ];
			/**
			 * Sanitize option_name
			 */
			$args['option_name'] = $this->sanitize_option_name( $config_id, $args );
			/**
			 * If we've set an option in the configuration
			 * then make sure we're using options and not theme_mods
			 */
			if ( isset( $config['option_name'] ) && ! empty( $config['option_name'] ) ) {
				$args['option_type'] = 'option';
			}
			/**
			 * Sanitize option_type
			 */
			$args['option_type'] = $this->sanitize_option_type( $config_id, $args );
			/**
			 * Sanitize capability
			 */
			$args['capability'] = $this->sanitize_capability( $config_id, $args );
			/**
			 * Get the 'disable_output' argument from the config
			 */
			$args['disable_output'] = $config['disable_output'];
			/**
			 * Sanitize settings
			 */
			$args['settings'] = $this->sanitize_settings( $config_id, $args );
			/**
			 * Sanitize tooltip messages
			 */
			$args['tooltip'] = $this->sanitize_tooltip( $config_id, $args );
			/**
			 * Sanitize active_callback
			 */
			$args['active_callback'] = $this->sanitize_active_callback( $config_id, $args );
			/**
			 * Sanitize control type
			 */
			$args['type'] = $this->sanitize_control_type( $config_id, $args );
			/**
			 * If no choices have been defined, use an empty array
			 */
			$args['choices'] = ( isset( $args['choices'] ) ) ? $args['choices'] : array();
			/**
			 * Tweaks for simple controls
			 */
			if ( 'kirki-text' == $args['type'] ) {
				$args['type']               = 'kirki-generic';
				$args['choices']['element'] = 'input';
				$args['choices']['type']    = 'text';
				if ( ! isset( $args['sanitize_callback'] ) ) {
					$args['sanitize_callback'] = 'wp_kses_post';
				}
			} elseif ( 'kirki-textarea' == $args['type'] ) {
				$args['type']               = 'kirki-generic';
				$args['choices']['element'] = 'textarea';
				$args['choices']['rows']    = '5';
				if ( ! isset( $args['sanitize_callback'] ) ) {
					$args['sanitize_callback'] = 'wp_kses_post';
				}
			}
			if ( 'kirki-generic' == $args['type'] && ! isset( $args['choices']['element'] ) ) {
				$args['choices']['element'] = 'input';
			}
			/**
			 * Sanitize the sanitize_callback argument.
			 */
			$args['sanitize_callback'] = $this->sanitize_callback( $config_id, $args );
			/**
			 * set choices for color controls
			 */
			if ( 'kirki-color' == $args['type'] ) {
				$args['choices']['alpha'] = false;
				$args['type'] = 'color-alpha';
			} elseif ( 'color-alpha' == $args['type'] ) {
				$args['choices']['alpha'] = true;
			}
			/**
			 * If no output argument has been defined, use an empty array
			 */
			$args['output'] = isset( $args['output'] ) ? $args['output'] : array();
			/**
			 * If "variables" has not been defined, set to null.
			 */
			$args['variables'] = ( isset( $args['variables'] ) && is_array( $args['variables'] ) ) ? $args['variables'] : null;
			/**
			 * Sanitize the id (for internal use)
			 */
			$args['id'] = $this->sanitize_id( $config_id, $args );
			/**
			 * Make sure the "multiple" argument is properly formatted for <select> controls
			 */
			if ( 'kirki-select' == $args['type'] ) {
				$args['multiple'] = ( isset( $args['multiple'] ) ) ? intval( $args['multiple'] ) : 1;
			}

			/**
			 * Add the field to the static $fields variable properly indexed
			 */
			Kirki::$fields[ $args['settings'] ] = $args;

			if ( 'background' == $args['type'] ) {
				/**
				 * Build the background fields
				 */
				Kirki::$fields = Kirki_Explode_Background_Field::process_fields( Kirki::$fields );
			}

		}

		/**
		 * Gets the $config_id and $args specified in the field,
		 * and then checks the validity of $config_id.
		 * If $config_id is not valid, then fallback to using the 'global' config.
		 *
		 * @param   string  $config_id
		 * @param   array   $args
		 * @return  string
		 */
		private function sanitize_config_id( $config_id = 'global', $args = array() ) {

			// Check if 'kirki_config' has been defined inside the $args.
			// In that case, it will override the $config.
			if ( isset( $args['kirki_config'] ) ) {
				$config_id = $args['kirki_config'];
			}
			// If $args is not used, then assume that $config_id took its place
			if ( is_array( $config_id ) && empty( $args ) ) {
				$args      = $config_id;
				$config_id = 'global';
			}
			// If $config_id is empty, use global config.
			if ( empty( $config_id ) ) {
				$config_id = 'global';
			}
			// If the defined config does not exist, use global.
			if ( ! isset( Kirki::$config[ $config_id ] ) ) {
				$config_id = 'global';
			}
			// Sanitize $config_id using the esc_attr() function
			$args['kirki_config'] = esc_attr( $config_id );

			return $args;

		}

		/**
		 * Sanitizes the setting name.
		 *
		 * @param   string  $config_id
		 * @param   array   $args
		 * @return  string
		 */
		private function sanitize_option_name( $config_id = 'global', $args = array() ) {

			/**
			 * If an option_name has been defined in the field itself,
			 * then escape it and return it.
			 */
			if ( isset( $args['option_name'] ) ) {
				return esc_attr( $args['option_name'] );
			}
			/**
			 * Try to get the option_name from the config
			 */
			if ( isset( Kirki::$config[ $config_id ]['option_name'] ) ) {
				return esc_attr( Kirki::$config[ $config_id ]['option_name'] );
			}
			/**
			 * If all else fails, return empty.
			 */
			return '';

		}

		/**
		 * Sanitizes the capability.
		 *
		 * @param   string  $config_id
		 * @param   array   $args
		 * @return  string
		 */
		private function sanitize_capability( $config_id = 'global', $args = array() ) {

			/**
			 * If an capability has been defined in the field itself,
			 * then escape it and return it.
			 */
			if ( isset( $args['capability'] ) ) {
				return esc_attr( $args['capability'] );
			}
			/**
			 * Try to get the capability from the config
			 */
			if ( isset( Kirki::$config[ $config_id ]['capability'] ) ) {
				return esc_attr( Kirki::$config[ $config_id ]['capability'] );
			}
			/**
			 * If all else fails, return edit_theme_options.
			 */<commit_msg>refactor the sanitize_option_name method<commit_after><?php

if ( ! class_exists( 'Kirki_Field' ) ) {
	class Kirki_Field {

		private $args = null;

		public function __construct( $config_id = 'global', $args = array() ) {
			$this->add_field( $config_id, $args );;
		}

		private function add_field( $config_id = 'global', $args = array() ) {

			// Sanitize $config_id
			$args      = $this->sanitize_config_id( $config_id, $args );
			$config_id = $args['kirki_config'];

			// Get the config arguments
			$config = Kirki::$config[ $config_id ];

			// Sanitize option_name
			$args = $this->sanitize_option_name( $args );
			/**
			 * If we've set an option in the configuration
			 * then make sure we're using options and not theme_mods
			 */
			if ( isset( $config['option_name'] ) && ! empty( $config['option_name'] ) ) {
				$args['option_type'] = 'option';
			}
			/**
			 * Sanitize option_type
			 */
			$args['option_type'] = $this->sanitize_option_type( $config_id, $args );
			/**
			 * Sanitize capability
			 */
			$args['capability'] = $this->sanitize_capability( $config_id, $args );
			/**
			 * Get the 'disable_output' argument from the config
			 */
			$args['disable_output'] = $config['disable_output'];
			/**
			 * Sanitize settings
			 */
			$args['settings'] = $this->sanitize_settings( $config_id, $args );
			/**
			 * Sanitize tooltip messages
			 */
			$args['tooltip'] = $this->sanitize_tooltip( $config_id, $args );
			/**
			 * Sanitize active_callback
			 */
			$args['active_callback'] = $this->sanitize_active_callback( $config_id, $args );
			/**
			 * Sanitize control type
			 */
			$args['type'] = $this->sanitize_control_type( $config_id, $args );
			/**
			 * If no choices have been defined, use an empty array
			 */
			$args['choices'] = ( isset( $args['choices'] ) ) ? $args['choices'] : array();
			/**
			 * Tweaks for simple controls
			 */
			if ( 'kirki-text' == $args['type'] ) {
				$args['type']               = 'kirki-generic';
				$args['choices']['element'] = 'input';
				$args['choices']['type']    = 'text';
				if ( ! isset( $args['sanitize_callback'] ) ) {
					$args['sanitize_callback'] = 'wp_kses_post';
				}
			} elseif ( 'kirki-textarea' == $args['type'] ) {
				$args['type']               = 'kirki-generic';
				$args['choices']['element'] = 'textarea';
				$args['choices']['rows']    = '5';
				if ( ! isset( $args['sanitize_callback'] ) ) {
					$args['sanitize_callback'] = 'wp_kses_post';
				}
			}
			if ( 'kirki-generic' == $args['type'] && ! isset( $args['choices']['element'] ) ) {
				$args['choices']['element'] = 'input';
			}
			/**
			 * Sanitize the sanitize_callback argument.
			 */
			$args['sanitize_callback'] = $this->sanitize_callback( $config_id, $args );
			/**
			 * set choices for color controls
			 */
			if ( 'kirki-color' == $args['type'] ) {
				$args['choices']['alpha'] = false;
				$args['type'] = 'color-alpha';
			} elseif ( 'color-alpha' == $args['type'] ) {
				$args['choices']['alpha'] = true;
			}
			/**
			 * If no output argument has been defined, use an empty array
			 */
			$args['output'] = isset( $args['output'] ) ? $args['output'] : array();
			/**
			 * If "variables" has not been defined, set to null.
			 */
			$args['variables'] = ( isset( $args['variables'] ) && is_array( $args['variables'] ) ) ? $args['variables'] : null;
			/**
			 * Sanitize the id (for internal use)
			 */
			$args['id'] = $this->sanitize_id( $config_id, $args );
			/**
			 * Make sure the "multiple" argument is properly formatted for <select> controls
			 */
			if ( 'kirki-select' == $args['type'] ) {
				$args['multiple'] = ( isset( $args['multiple'] ) ) ? intval( $args['multiple'] ) : 1;
			}

			/**
			 * Add the field to the static $fields variable properly indexed
			 */
			Kirki::$fields[ $args['settings'] ] = $args;

			if ( 'background' == $args['type'] ) {
				/**
				 * Build the background fields
				 */
				Kirki::$fields = Kirki_Explode_Background_Field::process_fields( Kirki::$fields );
			}

		}

		/**
		 * Gets the $config_id and $args specified in the field,
		 * and then checks the validity of $config_id.
		 * If $config_id is not valid, then fallback to using the 'global' config.
		 *
		 * @param   string  $config_id
		 * @param   array   $args
		 * @return  string
		 */
		private function sanitize_config_id( $config_id = 'global', $args = array() ) {

			// Check if 'kirki_config' has been defined inside the $args.
			// In that case, it will override the $config.
			if ( isset( $args['kirki_config'] ) ) {
				$config_id = $args['kirki_config'];
			}
			// If $args is not used, then assume that $config_id took its place
			if ( is_array( $config_id ) && empty( $args ) ) {
				$args      = $config_id;
				$config_id = 'global';
			}
			// If $config_id is empty, use global config.
			if ( empty( $config_id ) ) {
				$config_id = 'global';
			}
			// If the defined config does not exist, use global.
			if ( ! isset( Kirki::$config[ $config_id ] ) ) {
				$config_id = 'global';
			}
			// Sanitize $config_id using the esc_attr() function
			$args['kirki_config'] = esc_attr( $config_id );

			return $args;

		}

		/**
		 * Sanitizes the setting name.
		 *
		 * @param   string  $config_id
		 * @param   array   $args
		 * @return  string
		 */
		private function sanitize_option_name( $args = array() ) {

			if ( isset( Kirki::$config[ $args['kirki_config'] ]['option_name'] ) ) {
				// use from config if not defined in the option
				$defaults = array(
					'option_name' => Kirki::$config[ $args['kirki_config'] ]['option_name'],
				);
				$args = wp_parse_args( $args, $defaults );
			}
			// fallback to empty if undefined
			if ( ! isset( $args['option_name'] ) ) {
				$args['option_name'] = '';
			}
			// sanitize value
			$args['option_name'] = esc_attr( $args['option_name'] );

			return $args;

		}

		/**
		 * Sanitizes the capability.
		 *
		 * @param   string  $config_id
		 * @param   array   $args
		 * @return  string
		 */
		private function sanitize_capability( $config_id = 'global', $args = array() ) {

			/**
			 * If an capability has been defined in the field itself,
			 * then escape it and return it.
			 */
			if ( isset( $args['capability'] ) ) {
				return esc_attr( $args['capability'] );
			}
			/**
			 * Try to get the capability from the config
			 */
			if ( isset( Kirki::$config[ $config_id ]['capability'] ) ) {
				return esc_attr( Kirki::$config[ $config_id ]['capability'] );
			}
			/**
			 * If all else fails, return edit_theme_options.
			 */
{'fraction_non_alphanumeric': 0.1759499465893484, 'fraction_numerical': 0.0003052037234854265, 'mean_word_length': 2.850470035252644, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5604673', 'n_tokens_mistral': 5222, 'n_tokens_neox': 4373, 'n_words': 1065}