<commit_before><?php
/**
 * Class Google\Site_Kit\Modules\Thank_With_Google\Settings
 *
 * @package   Google\Site_Kit\Modules\Thank_With_Google
 * @copyright 2021 Google LLC
 * @license   https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link      https://sitekit.withgoogle.com
 */

namespace Google\Site_Kit\Modules\Thank_With_Google;

use Google\Site_Kit\Core\Modules\Module_Settings;
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Interface;
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Trait;

/**
 * Class for Thank with Google settings.
 *
 * @since 1.78.0
 * @access private
 * @ignore
 */
class Settings extends Module_Settings implements Setting_With_Owned_Keys_Interface {
	use Setting_With_Owned_Keys_Trait;

	const OPTION = 'googlesitekit_thank-with-google_settings';

	/**
	 * Registers the setting in WordPress.
	 *
	 * @since 1.78.0
	 */
	public function register() {
		parent::register();

		$this->register_owned_keys();
	}

	/**
	 * Returns keys for owned settings.
	 *
	 * @since 1.78.0
	 *
	 * @return array An array of keys for owned settings.
	 */
	public function get_owned_keys() {
		return array(
			'publicationID',
		);
	}

	/**
	 * Gets the default value.
	 *
	 * @since 1.78.0
	 *
	 * @return array
	 */
	protected function get_default() {
		return array(
			'ownerID'         => '',
			'publicationID'   => '',
			'colorTheme'      => '',
			'buttonPlacement' => '',
			'buttonPostTypes' => array( 'post' ),
		);
	}

	/**
	 * Gets the setting value.
	 *
	 * @since n.e.x.t
	 *
	 * @return array
	 */
	public function get() {
		$option = parent::get();

		if ( is_array( $option ) ) {
			if ( isset( $option['buttonPostTypes'] ) ) {
				$option['buttonPostTypes'] = $this->sanitize_button_post_types( $option['buttonPostTypes'] );
			}
		}

		return $option;
	}

	/**
	 * Gets the callback for sanitizing the setting's value before saving.
	 *
	 * @since n.e.x.t
	 *
	 * @return callable|null
	 */
	protected function get_sanitize_callback() {
		return function( $option ) {
			if ( is_array( $option ) ) {
				if ( isset( $option['colorTheme'] ) ) {
					$option['colorTheme'] = $this->sanitize_color_theme( $option['colorTheme'] );
				}
				if ( isset( $option['buttonPlacement'] ) ) {
					$option['buttonPlacement'] = $this->sanitize_button_placement( $option['buttonPlacement'] );
				}
				if ( isset( $option['buttonPostTypes'] ) ) {
					$option['buttonPostTypes'] = $this->sanitize_button_post_types( $option['buttonPostTypes'] );
				}
			}
			return $option;
		};
	}

	/**
	 * Sanitizes the value of color theme.
	 *
	 * @since n.e.x.t
	 *
	 * @param string $color_theme The value to sanitize.
	 * @return string The sanitized value.
	 */
	private function sanitize_color_theme( $color_theme ) {
		if ( in_array( $color_theme, array( 'blue' ), true ) ) {
			return $color_theme;
		}
		return '';
	}

	/**
	 * Sanitizes the value of button placement.
	 *
	 * @since n.e.x.t
	 *
	 * @param string $button_placement The value to sanitize.
	 * @return string The sanitized value.
	 */
	private function sanitize_button_placement( $button_placement ) {
		if ( in_array(
			$button_placement,
			array(
				'dynamic_low',
				'dynamic_high',
				'static_auto',
				'static_above-content',
				'static_below-content',
				'static_below-first-paragraph',
			),
			true
		) ) {
			return $button_placement;
		}
		return '';
	}<commit_msg>Fix the color setting validation on backend.<commit_after><?php
/**
 * Class Google\Site_Kit\Modules\Thank_With_Google\Settings
 *
 * @package   Google\Site_Kit\Modules\Thank_With_Google
 * @copyright 2021 Google LLC
 * @license   https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link      https://sitekit.withgoogle.com
 */

namespace Google\Site_Kit\Modules\Thank_With_Google;

use Google\Site_Kit\Core\Modules\Module_Settings;
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Interface;
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Trait;

/**
 * Class for Thank with Google settings.
 *
 * @since 1.78.0
 * @access private
 * @ignore
 */
class Settings extends Module_Settings implements Setting_With_Owned_Keys_Interface {
	use Setting_With_Owned_Keys_Trait;

	const OPTION = 'googlesitekit_thank-with-google_settings';

	/**
	 * Registers the setting in WordPress.
	 *
	 * @since 1.78.0
	 */
	public function register() {
		parent::register();

		$this->register_owned_keys();
	}

	/**
	 * Returns keys for owned settings.
	 *
	 * @since 1.78.0
	 *
	 * @return array An array of keys for owned settings.
	 */
	public function get_owned_keys() {
		return array(
			'publicationID',
		);
	}

	/**
	 * Gets the default value.
	 *
	 * @since 1.78.0
	 *
	 * @return array
	 */
	protected function get_default() {
		return array(
			'ownerID'         => '',
			'publicationID'   => '',
			'colorTheme'      => '',
			'buttonPlacement' => '',
			'buttonPostTypes' => array( 'post' ),
		);
	}

	/**
	 * Gets the setting value.
	 *
	 * @since n.e.x.t
	 *
	 * @return array
	 */
	public function get() {
		$option = parent::get();

		if ( is_array( $option ) ) {
			if ( isset( $option['buttonPostTypes'] ) ) {
				$option['buttonPostTypes'] = $this->sanitize_button_post_types( $option['buttonPostTypes'] );
			}
		}

		return $option;
	}

	/**
	 * Gets the callback for sanitizing the setting's value before saving.
	 *
	 * @since n.e.x.t
	 *
	 * @return callable|null
	 */
	protected function get_sanitize_callback() {
		return function( $option ) {
			if ( is_array( $option ) ) {
				if ( isset( $option['colorTheme'] ) ) {
					$option['colorTheme'] = $this->sanitize_color_theme( $option['colorTheme'] );
				}
				if ( isset( $option['buttonPlacement'] ) ) {
					$option['buttonPlacement'] = $this->sanitize_button_placement( $option['buttonPlacement'] );
				}
				if ( isset( $option['buttonPostTypes'] ) ) {
					$option['buttonPostTypes'] = $this->sanitize_button_post_types( $option['buttonPostTypes'] );
				}
			}
			return $option;
		};
	}

	/**
	 * Sanitizes the value of color theme.
	 *
	 * @since n.e.x.t
	 *
	 * @param string $color_theme The value to sanitize.
	 * @return string The sanitized value.
	 */
	private function sanitize_color_theme( $color_theme ) {
		$allowed_colors = array(
			'blue',
			'cyan',
			'green',
			'purple',
			'pink',
			'orange',
			'brown',
			'black',
		);

		if ( in_array( $color_theme, $allowed_colors, true ) ) {
			return $color_theme;
		}
		return '';
	}

	/**
	 * Sanitizes the value of button placement.
	 *
	 * @since n.e.x.t
	 *
	 * @param string $button_placement The value to sanitize.
	 * @return string The sanitized value.
	 */
	private function sanitize_button_placement( $button_placement ) {
		if ( in_array(
			$button_placement,
			array(
				'dynamic_low',
				'dynamic_high',
				'static_auto',
				'static_above-content',
				'static_below-content',
				'static_below-first-paragraph',
			),
			true
		) ) {
			return $button_placement;
		}
		return '';
	}
{'fraction_non_alphanumeric': 0.14429001289583035, 'fraction_numerical': 0.006877776185699957, 'mean_word_length': 3.2483262325015216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '7051383', 'n_tokens_mistral': 2692, 'n_tokens_neox': 2346, 'n_words': 491}