<commit_before><?php

namespace Amostajo\LightweightMVC\Traits;

/**
 * Trait related to all meta functionality of a model.
 *
 * @author <NAME>
 * @license MIT
 * @package Amostajo\LightweightMVC
 */
trait MetaTrait
{
	/**
	 * Meta data.
	 * @var array
	 */
	protected $meta = array();

	/**
	 * Loads meta values into objet.
	 */
	public function load_meta()
	{
		if ( empty( $this->attributes ) ) return;

		foreach ( get_post_meta( $this->attributes['ID'] ) as $key => $value ) {
			if ( ! preg_match( '/_wp_/', $key )
				|| in_array( 'meta_' . $key, $this->aliases )
			) {
				$value = $value[0];

				$this->meta[$key] = is_string( $value )
					? ( preg_match( '/_wp_/', $key )
						? $value
						: json_decode( $value )
					)
					: ( is_integer( $value )
						? intval( $value )
						: floatval( $value )
					);
			}
		}
	}

	/**
	 * Returns flag indicating if object has meta key.
	 *
	 * @param string $key Key.
	 *
	 * @return bool
	 */
	public function has_meta( $key )
	{
		return array_key_exists( $key, $this->meta );
	}

	/**
	 * Sets meta value.
	 *
	 * @param string $key   Key.
	 * @param mixed  $value Value.
	 */
	public function set_meta( $key, $value )
	{
		$this->meta[$key] = $value;
	}

	/**
	 * Gets value from meta.
	 *
	 * @param string $key Key.
	 *
	 * @return mixed.
	 */
	public function get_meta( $key )
	{
		return $this->has_meta( $key ) ? $this->meta[$key] : null;
	}

	/**
	 * Deletes meta..
	 *
	 * @param string $key Key.
	 */
	public function delete_meta( $key )
	{
		if ( ! $this->has_meta( $key ) ) return;

		delete_post_meta( $this->attributes['ID'], $key );

		unset( $this->meta[$key] );
	}

	/**
	 * Either adds or updates a meta.
	 *
	 * @param string $key   Key.
	 * @param mixed  $value Value.
	 */
	public function save_meta( $key, $value, $update_array = true )
	{
		if ( $update_array )
			$this->meta[$key] = $value;

		\update_post_meta( 
			$this->attributes['ID'],
			$key,
			is_numeric( $value )
				? $value<commit_msg>Wordpress meta save restriction<commit_after><?php

namespace Amostajo\LightweightMVC\Traits;

/**
 * Trait related to all meta functionality of a model.
 *
 * @author <NAME>
 * @license MIT
 * @package Amostajo\LightweightMVC
 */
trait MetaTrait
{
	/**
	 * Meta data.
	 * @var array
	 */
	protected $meta = array();

	/**
	 * Loads meta values into objet.
	 */
	public function load_meta()
	{
		if ( empty( $this->attributes ) ) return;

		foreach ( get_post_meta( $this->attributes['ID'] ) as $key => $value ) {
			if ( ! preg_match( '/_wp_/', $key )
				|| in_array( 'meta_' . $key, $this->aliases )
			) {
				$value = $value[0];

				$this->meta[$key] = is_string( $value )
					? ( preg_match( '/_wp_/', $key )
						? $value
						: json_decode( $value )
					)
					: ( is_integer( $value )
						? intval( $value )
						: floatval( $value )
					);
			}
		}
	}

	/**
	 * Returns flag indicating if object has meta key.
	 *
	 * @param string $key Key.
	 *
	 * @return bool
	 */
	public function has_meta( $key )
	{
		return array_key_exists( $key, $this->meta );
	}

	/**
	 * Sets meta value.
	 *
	 * @param string $key   Key.
	 * @param mixed  $value Value.
	 */
	public function set_meta( $key, $value )
	{
		$this->meta[$key] = $value;
	}

	/**
	 * Gets value from meta.
	 *
	 * @param string $key Key.
	 *
	 * @return mixed.
	 */
	public function get_meta( $key )
	{
		return $this->has_meta( $key ) ? $this->meta[$key] : null;
	}

	/**
	 * Deletes meta..
	 *
	 * @param string $key Key.
	 */
	public function delete_meta( $key )
	{
		if ( ! $this->has_meta( $key ) ) return;

		delete_post_meta( $this->attributes['ID'], $key );

		unset( $this->meta[$key] );
	}

	/**
	 * Either adds or updates a meta.
	 *
	 * @param string $key   Key.
	 * @param mixed  $value Value.
	 */
	public function save_meta( $key, $value, $update_array = true )
	{
		if ( preg_match( '/_wp_/', $key ) ) return;
		
		if ( $update_array )
			$this->meta[$key] = $value;

		\update_post_meta( 
			$this->attributes['ID'],
			$key,
			is_numeric( $value )
				? $value
{'fraction_non_alphanumeric': 0.17478368355995055, 'fraction_numerical': 0.0004944375772558715, 'mean_word_length': 2.5213228894691038, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, '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': '85910', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1474, 'n_words': 365}