36, 'captcha_provider' => '', 'captcha_site_key' => '', 'captcha_secret' => '', ); /** * Retention periods offered, in months. 0 keeps requests until deleted. */ const RETENTION_CHOICES = array( 6, 12, 24, 36, 60, 0 ); /** * Returns one setting. * * @param string $key Setting name. * @return mixed */ public static function get( $key ) { $saved = get_option( self::OPTION, array() ); $saved = is_array( $saved ) ? $saved : array(); return array_key_exists( $key, $saved ) ? $saved[ $key ] : self::DEFAULTS[ $key ]; } /** * Registers the hooks. */ public function register_hooks() { add_action( 'admin_menu', array( $this, 'add_page' ) ); add_action( 'admin_init', array( $this, 'register' ) ); } /** * Adds the settings page to the Quote requests menu. */ public function add_page() { add_submenu_page( 'edit.php?post_type=' . Post_Types::REQUEST, __( 'Quote request settings', 'primiko' ), __( 'Settings', 'primiko' ), 'manage_options', self::PAGE, array( $this, 'render' ) ); } /** * Registers the setting with its sanitizer. */ public function register() { register_setting( self::PAGE, self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize' ), 'default' => self::DEFAULTS, ) ); } /** * Keeps only known settings with allowed values. * * @param mixed $input Submitted values. * @return array */ public function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); $months = isset( $input['retention_months'] ) ? absint( $input['retention_months'] ) : self::DEFAULTS['retention_months']; $provider = isset( $input['captcha_provider'] ) ? sanitize_key( $input['captcha_provider'] ) : ''; $site_key = isset( $input['captcha_site_key'] ) ? self::key( $input['captcha_site_key'] ) : ''; $secret = isset( $input['captcha_secret'] ) ? self::key( $input['captcha_secret'] ) : ''; // The secret is never printed back: an empty field keeps the saved one. if ( '' === $secret && empty( $input['captcha_secret_clear'] ) ) { $secret = (string) self::get( 'captcha_secret' ); } return array( 'retention_months' => in_array( $months, self::RETENTION_CHOICES, true ) ? $months : self::DEFAULTS['retention_months'], 'captcha_provider' => isset( Captcha::PROVIDERS[ $provider ] ) ? $provider : '', 'captcha_site_key' => $site_key, 'captcha_secret' => $secret, ); } /** * Cleans a captcha key: letters, digits, dashes and underscores only. * * @param mixed $value Submitted key. * @return string */ private static function key( $value ) { $value = is_string( $value ) ? trim( $value ) : ''; return preg_match( '/^[A-Za-z0-9_-]{8,200}$/', $value ) ? $value : ''; } /** * Prints the settings page. */ public function render() { if ( ! current_user_can( 'manage_options' ) ) { return; } $current = (int) self::get( 'retention_months' ); ?>