prefix = $prefix; } /** * Create temporary state code * * @param bool $short_time 15 or 30 minutes long. * @return string */ public function create_state_code( bool $short_time = true ) : string { $nonce = wp_generate_password( 24, false, false ) . (string) time(); $valid_until = time() + ( $short_time ? 15 : 30 ) * MINUTE_IN_SECONDS; update_option( $this->get_option_name(), [ $nonce, $valid_until ] ); return $nonce; } /** * Get state code * * @return ?string Code value if exists and not expired. */ public function get_state_code() : ?string { $value = get_option( $this->get_option_name() ); if ( is_array( $value ) && ( 2 === count( $value ) ) ) { list( $value, $valid_until ) = $value; if ( is_int( $valid_until ) && ( $valid_until >= time() ) && is_string( $value ) ) { return $value; } else { delete_option( $this->get_option_name() ); } } return null; } /** * Verify state code * * @param string $nonce Code to check. * @return bool */ public function verify_code( string $nonce ) : bool { return $this->get_state_code() === $nonce; } /** * @return string */ protected function get_option_name() : string { return self::OPTION_BASE . $this->prefix; } }