<commit_before><?php

// Exit if accessed directly
defined( 'ABSPATH' ) || exit;

/**
 * Papi Property.
 *
 * @package Papi
 * @since 1.3.0
 */

class Papi_Property {

	/**
	 * Default options.
	 *
	 * @var array
	 * @since 1.3.0
	 */

	private $default_options = array(
		'array_slug'   => '',
		'capabilities' => array(),
		'default'      => '',
		'description'  => '',
		'disabled'     => false,
		'lang'         => false,
		'raw'          => false,
		'required'     => false,
		'settings'     => array(),
		'sidebar'      => true,
		'slug'         => '',
		'sort_order'   => -1,
		'title'        => '',
		'type'         => '',
		'value'        => ''
	);

	/**
	 * Default value.
	 *
	 * @var string
	 * @since 1.0.0
	 */

	public $default_value = '';

	/**
	 * Current property options object.
	 *
	 * @var object
	 * @since 1.0.0
	 */

	private $options;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 */

	public function __construct() {
		$this->setup_actions();
		$this->setup_filters();
	}

	/**
	 * Convert settings items to properties if they are a property.
	 *
	 * @param array $settings
	 * @since 1.3.0
	 *
	 * @return array
	 */

	private function convert_settings( $settings ) {
		foreach ( $settings as $key => $value ) {
			if ( is_array( $value ) ) {
				if ( isset( $value['type'] ) ) {
					$type = papi_get_property_class_name( $value['type'] );
					if ( class_exists( $type ) ) {
						$settings[$key] = papi_property( $value );
					} else {
						$settings[$key] = $this->convert_settings( $value );
					}
				} else {
					$settings[$key] = $this->convert_settings( $value );
				}
			}
		}

		return $settings;
	}

	/**
	 * Create a property from options.
	 *
	 * @param array|object $options
	 * @since 1.0.0
	 *
	 * @return Papi_Property
	 */

	public static function create( $options = array() ) {
		$property = new self;
		$property->set_options( $options );
		return $property;
	}

	/**
	 * Get default options.
	 *
	 * @since 1.0.0
	 * @return array
	 */

	public static function default_options() {
		$property = new self;
		$default_options = $property->default_options;

		if ( $default_options['sort_order'] === -1 ) {
			$default_options['sort_order'] = papi_filter_settings_sort_order();
		}

		return $default_options;
	}

	/**
	 * Create a new instance of the given property.
	 *
	 * @param string $property_type
	 * @since 1.0.0
	 *
	 * @return object
	 */

	public static function factory( $property_type ) {
		if ( ! is_string( $property_type ) ) {
			return;
		}

		$class_name = papi_get_property_class_name( $property_type );

		if ( empty( $class_name ) || ! class_exists( $class_name ) ) {
			return;
		}

		if ( ! papi()->exists( $class_name ) ) {
			$rc = new ReflectionClass( $class_name );
			$prop = $rc->newInstance();

			// Property class need to be a subclass of Papi Property class.
			if ( ! is_subclass_of( $class_name, __CLASS__ ) ) {
				return null;
			}

			papi()->bind( $class_name, $prop );
		}

		return papi()->make( $class_name );
	}

	/**
	 * Format the value of the property before we output it to the application.
	 *
	 * @param mixed $value
	 * @param string $slug
	 * @param int $post_id
	 * @since 1.0.0
	 *
	 * @return mixed
	 */

	public function format_value( $value, $slug, $post_id ) {
		return $value;
	}

	/**
	 * Get default settings.
	 *
	 * @since 1.2.0
	 *
	 * @return array
	 */

	public function get_default_settings() {
		return array();
	}

	/**
	 * Get option value.
	 *
	 * @param string $key
	 * @since 1.3.0
	 *
	 * @return mixed
	 */

	public function get_option( $key ) {
		if ( isset( $this->options->$key ) ) {
			return $this->options->$key;
		}
	}

	/**
	 * Get the current property options object.
	 *
	 * @since 1.0.0
	 *
	 * @return object
	 */

	public function get_options() {
		return $this->options;
	}

	/**
	 * Get post id.
	 *
	 * @since 1.3.0
	 *
	 * @return int
	 */

	public function get_post_id() {
		return papi_get_post_id();
	}

	/**
	 * Get setting value.
	 *
	 * @param string $key
	 * @since 1.3.0
	 *
	 * @return mixed
	 */

	public function get_setting( $key ) {
		$settings = $this->get_settings();
		if ( isset( $settings->$key ) ) {
			return $settings->$key;
		}
	}

	/**
	 * Get custom property settings.
	 *
	 * @since 1.0.0
	 *
	 * @return object
	 */

	public function get_settings() {
		if ( ! is_object( $this->options ) ) {
			return;
		}

		$settings = wp_parse_args( $this->options->settings, $this->get_default_settings() );

		return (object) $this->convert_settings( $settings );
	}

	/**
	 * Get property value.
	 *
	 * @since 1.3.0
	 * @return array
	 */

	public function get_value( $fetch_value = true ) {
		if ( ! is_object( $this->options ) ) {
			return;
		}

		if ( $fetch_value && papi_is_empty( $this->options->value ) ) {
			$post_id = $this->get_post_id();
			$value = papi_field( $post_id, $this->options->slug, null, array(
				'property' => $this
			) );
		} else {
			$value = $this->options->value;
		}

		if ( is_string( $this->default_value ) ) {
			$value = papi_convert_to_string( $value );
		}

		if ( papi_is_empty( $value ) ) {
			$value = $this->default_value;
		}

		if ( ! $this->get_setting( 'allow_html' ) ) {
			$value = papi_santize_data( $value );
		}

		return $value;<commit_msg>Fixed so the default value is actual the real property type default value<commit_after><?php

// Exit if accessed directly
defined( 'ABSPATH' ) || exit;

/**
 * Papi Property.
 *
 * @package Papi
 * @since 1.3.0
 */

class Papi_Property {

	/**
	 * Default options.
	 *
	 * @var array
	 * @since 1.3.0
	 */

	private $default_options = array(
		'array_slug'   => '',
		'capabilities' => array(),
		'default'      => '',
		'description'  => '',
		'disabled'     => false,
		'lang'         => false,
		'raw'          => false,
		'required'     => false,
		'settings'     => array(),
		'sidebar'      => true,
		'slug'         => '',
		'sort_order'   => -1,
		'title'        => '',
		'type'         => '',
		'value'        => ''
	);

	/**
	 * Default value.
	 *
	 * @var string
	 * @since 1.0.0
	 */

	public $default_value = '';

	/**
	 * Current property options object.
	 *
	 * @var object
	 * @since 1.0.0
	 */

	private $options;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 */

	public function __construct() {
		$this->setup_actions();
		$this->setup_filters();
	}

	/**
	 * Convert settings items to properties if they are a property.
	 *
	 * @param array $settings
	 * @since 1.3.0
	 *
	 * @return array
	 */

	private function convert_settings( $settings ) {
		foreach ( $settings as $key => $value ) {
			if ( is_array( $value ) ) {
				if ( isset( $value['type'] ) ) {
					$type = papi_get_property_class_name( $value['type'] );
					if ( class_exists( $type ) ) {
						$settings[$key] = papi_property( $value );
					} else {
						$settings[$key] = $this->convert_settings( $value );
					}
				} else {
					$settings[$key] = $this->convert_settings( $value );
				}
			}
		}

		return $settings;
	}

	/**
	 * Create a property from options.
	 *
	 * @param array|object $options
	 * @since 1.0.0
	 *
	 * @return Papi_Property
	 */

	public static function create( $options = array() ) {
		$property = new self;
		$property->set_options( $options );
		return $property;
	}

	/**
	 * Get default options.
	 *
	 * @since 1.0.0
	 * @return array
	 */

	public static function default_options() {
		$property = new self;
		$default_options = $property->default_options;

		if ( $default_options['sort_order'] === -1 ) {
			$default_options['sort_order'] = papi_filter_settings_sort_order();
		}

		return $default_options;
	}

	/**
	 * Create a new instance of the given property.
	 *
	 * @param string $property_type
	 * @since 1.0.0
	 *
	 * @return object
	 */

	public static function factory( $property_type ) {
		if ( ! is_string( $property_type ) ) {
			return;
		}

		$class_name = papi_get_property_class_name( $property_type );

		if ( empty( $class_name ) || ! class_exists( $class_name ) ) {
			return;
		}

		if ( ! papi()->exists( $class_name ) ) {
			$rc = new ReflectionClass( $class_name );
			$prop = $rc->newInstance();

			// Property class need to be a subclass of Papi Property class.
			if ( ! is_subclass_of( $class_name, __CLASS__ ) ) {
				return null;
			}

			papi()->bind( $class_name, $prop );
		}

		return papi()->make( $class_name );
	}

	/**
	 * Format the value of the property before we output it to the application.
	 *
	 * @param mixed $value
	 * @param string $slug
	 * @param int $post_id
	 * @since 1.0.0
	 *
	 * @return mixed
	 */

	public function format_value( $value, $slug, $post_id ) {
		return $value;
	}

	/**
	 * Get default settings.
	 *
	 * @since 1.2.0
	 *
	 * @return array
	 */

	public function get_default_settings() {
		return array();
	}

	/**
	 * Get option value.
	 *
	 * @param string $key
	 * @since 1.3.0
	 *
	 * @return mixed
	 */

	public function get_option( $key ) {
		if ( isset( $this->options->$key ) ) {
			return $this->options->$key;
		}
	}

	/**
	 * Get the current property options object.
	 *
	 * @since 1.0.0
	 *
	 * @return object
	 */

	public function get_options() {
		return $this->options;
	}

	/**
	 * Get post id.
	 *
	 * @since 1.3.0
	 *
	 * @return int
	 */

	public function get_post_id() {
		return papi_get_post_id();
	}

	/**
	 * Get setting value.
	 *
	 * @param string $key
	 * @since 1.3.0
	 *
	 * @return mixed
	 */

	public function get_setting( $key ) {
		$settings = $this->get_settings();
		if ( isset( $settings->$key ) ) {
			return $settings->$key;
		}
	}

	/**
	 * Get custom property settings.
	 *
	 * @since 1.0.0
	 *
	 * @return object
	 */

	public function get_settings() {
		if ( ! is_object( $this->options ) ) {
			return;
		}

		$settings = wp_parse_args( $this->options->settings, $this->get_default_settings() );

		return (object) $this->convert_settings( $settings );
	}

	/**
	 * Get property value.
	 *
	 * @since 1.3.0
	 * @return array
	 */

	public function get_value( $fetch_value = true ) {
		if ( ! is_object( $this->options ) ) {
			return;
		}

		if ( $fetch_value && papi_is_empty( $this->options->value ) ) {
			$post_id = $this->get_post_id();
			$value = papi_field( $post_id, $this->options->slug, null, array(
				'property' => $this
			) );
		} else {
			$value = $this->options->value;
		}

		$type = self::factory( $this->options->type );

		if ( ! is_object( $type ) ) {
			return;
		}

		if ( is_string( $type->default_value ) ) {
			$value = papi_convert_to_string( $value );
		}

		if ( papi_is_empty( $value ) ) {
			$value = $type->default_value;
		}

		if ( ! $this->get_setting( 'allow_html' ) ) {
			$value = papi_santize_data( $value );
		}

		return $value;
{'fraction_non_alphanumeric': 0.16788526434195725, 'fraction_numerical': 0.00993625796775403, 'mean_word_length': 2.5433410826967786, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 107, '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': '5316411', 'n_tokens_mistral': 4345, 'n_tokens_neox': 3734, 'n_words': 870}