public function process_admin_options()

T.Karima❶

عضو نشيط
المشاركات
92
مستوى التفاعل
2
النقاط
6
public function process_admin_options()
{
WC_GivePayments_Logger::log('Processing admin options');

// Get the selected environment from the settings
$environment = isset($_POST['woocommerce_givepayments_environment']) ? $_POST['woocommerce_givepayments_environment'] : 'test';

// Encrypt API keys before storing them
$this->encrypt_api_keys();
update_option('givepayments_merchant_id', $_POST['woocommerce_givepayments_merchant_id']);

// Store the encrypted API keys based on the selected environment
if ($environment === 'test') {
update_option('givepayments_sandbox_api_key', $_POST['woocommerce_givepayments_api_key']);
} else {
update_option('givepayments_production_api_key', $_POST['woocommerce_givepayments_api_key']);
}

return parent::process_admin_options();
}
i want to optimise the logic here , by checking if there was an already stored key , if so , we should decrypt it and compare it to the new input , if they match , we do nothing , if there is a new value , we encrypt it , and store it instead of the old one , also i want to handle if the input also is already encrypted , if the input encrypted or not encypted matches the stored api key value , we should not update or encrypt again
قال ChatGPT:
To optimize the logic in your process_admin_options function based on your requirements, you need to:

Check if the provided API key is already encrypted. If it is, decrypt it and compare it with the new input.
Only update or encrypt the key if there is a new value or if the key has changed (i.e., the decrypted input doesn’t match the stored encrypted value).
Ensure the comparison is done for both encrypted and unencrypted values.
Here’s an optimized version of your method:

php
نسخ
تحرير
public function process_admin_options()
{
WC_GivePayments_Logger::log('Processing admin options');

// Get the selected environment from the settings
$environment = isset($_POST['woocommerce_givepayments_environment']) ? $_POST['woocommerce_givepayments_environment'] : 'test';

// Retrieve the existing API keys from the options
$stored_sandbox_key = get_option('givepayments_sandbox_api_key');
$stored_production_key = get_option('givepayments_production_api_key');

// Handle sandbox API key
$new_sandbox_key = isset($_POST['woocommerce_givepayments_api_key']) ? $_POST['woocommerce_givepayments_api_key'] : '';
if ($environment === 'test') {
if ($new_sandbox_key !== $stored_sandbox_key) {
// If the stored key is encrypted, decrypt it and compare
$decrypted_sandbox_key = $this->decrypt_api_key($stored_sandbox_key);
if ($new_sandbox_key !== $decrypted_sandbox_key) {
// Only encrypt and update if the key has changed
$encrypted_sandbox_key = $this->encrypt_api_key($new_sandbox_key);
update_option('givepayments_sandbox_api_key', $encrypted_sandbox_key);
}
}
} else {
// Handle production API key
if ($new_sandbox_key !== $stored_production_key) {
$decrypted_production_key = $this->decrypt_api_key($stored_production_key);
if ($new_sandbox_key !== $decrypted_production_key) {
$encrypted_production_key = $this->encrypt_api_key($new_sandbox_key);
update_option('givepayments_production_api_key', $encrypted_production_key);
}
}
}

// Encrypt and store merchant ID
$this->encrypt_api_keys();
update_option('givepayments_merchant_id', $_POST['woocommerce_givepayments_merchant_id']);

return parent::process_admin_options();
}

// Helper method to decrypt API key
private function decrypt_api_key($encrypted_key)
{
// Your decryption logic here, for example:
return openssl_decrypt($encrypted_key, 'aes-256-cbc', 'your_secret_key', 0, 'your_iv');
}

// Helper method to encrypt API key
private function encrypt_api_key($key)
{
// Your encryption logic here, for example:
return openssl_encrypt($key, 'aes-256-cbc', 'your_secret_key', 0, 'your_iv');
}
Key Changes:
API Key Decryption: We added the decrypt_api_key() helper function that decrypts the stored key before comparison.
Condition for Encryption: The key is only encrypted and updated if the new input doesn’t match the stored key (decrypted). If they match, no update is performed.
Environment Handling: We ensured that the sandbox and production keys are handled separately and updated accordingly.
Notes:
You'll need to implement your own decrypt_api_key() and encrypt_api_key() logic, which will depend on the encryption method you're using. For example, here I used openssl_decrypt and openssl_encrypt, but you should adapt it to match your current encryption method.
The provided logic ensures minimal updates (only when keys differ) and avoids unnecessary re-encryption.
This should meet your requirements effectively.







أنت قلت:
great , but we need to handle one more case , is when the input is empty , we should just set the key value for it as empty without encryption
قال ChatGPT:
To handle the case where the input is empty and set the key value as empty without encryption, you can add a check for empty input before proceeding with the encryption or comparison logic.

Here's the updated version of your function that includes this additional check:

php
نسخ
تحرير
public function process_admin_options()
{
WC_GivePayments_Logger::log('Processing admin options');

// Get the selected environment from the settings
$environment = isset($_POST['woocommerce_givepayments_environment']) ? $_POST['woocommerce_givepayments_environment'] : 'test';

// Retrieve the existing API keys from the options
$stored_sandbox_key = get_option('givepayments_sandbox_api_key');
$stored_production_key = get_option('givepayments_production_api_key');

// Handle sandbox API key
$new_sandbox_key = isset($_POST['woocommerce_givepayments_api_key']) ? $_POST['woocommerce_givepayments_api_key'] : '';
if ($environment === 'test') {
if ($new_sandbox_key === '') {
// If the new API key is empty, set the stored key to empty
update_option('givepayments_sandbox_api_key', '');
} elseif ($new_sandbox_key !== $stored_sandbox_key) {
// If the stored key is encrypted, decrypt it and compare
$decrypted_sandbox_key = $this->decrypt_api_key($stored_sandbox_key);
if ($new_sandbox_key !== $decrypted_sandbox_key) {
// Only encrypt and update if the key has changed
$encrypted_sandbox_key = $this->encrypt_api_key($new_sandbox_key);
update_option('givepayments_sandbox_api_key', $encrypted_sandbox_key);
}
}
} else {
// Handle production API key
if ($new_sandbox_key === '') {
// If the new API key is empty, set the stored key to empty
update_option('givepayments_production_api_key', '');
} elseif ($new_sandbox_key !== $stored_production_key) {
$decrypted_production_key = $this->decrypt_api_key($stored_production_key);
if ($new_sandbox_key !== $decrypted_production_key) {
$encrypted_production_key = $this->encrypt_api_key($new_sandbox_key);
update_option('givepayments_production_api_key', $encrypted_production_key);
}
}
}

// Encrypt and store merchant ID
$this->encrypt_api_keys();
update_option('givepayments_merchant_id', $_POST['woocommerce_givepayments_merchant_id']);

return parent::process_admin_options();
}

// Helper method to decrypt API key
private function decrypt_api_key($encrypted_key)
{
// Your decryption logic here, for example:
return openssl_decrypt($encrypted_key, 'aes-256-cbc', 'your_secret_key', 0, 'your_iv');
}

// Helper method to encrypt API key
private function encrypt_api_key($key)
{
// Your encryption logic here, for example:
return openssl_encrypt($key, 'aes-256-cbc', 'your_secret_key', 0, 'your_iv');
}
Key Updates:
Empty Input Check: Before proceeding with encryption or comparison, we check if the new key input is empty ($new_sandbox_key === ''). If it is, we update the corresponding option (givepayments_sandbox_api_key or givepayments_production_api_key) to an empty string.
Skipping Encryption for Empty Keys: If the input is empty, no encryption is performed. The stored key is simply set to an empty string.
Flow:
If the user submits an empty key, the stored key will be set to an empty value.
If the key is non-empty and different from the stored key (whether encrypted or not), it will be encrypted and updated.
If the key matches the current stored key (either encrypted or decrypted), no change is made.
This handles the scenario where the input might be empty and ensures the stored key is updated correctly without unnecessary encryption.
 

T.Karima❶

عضو نشيط
المشاركات
92
مستوى التفاعل
2
النقاط
6
combien de gramme de viande haché pour cuisiner une briques
قال ChatGPT:
Pour cuisiner une brique (plat traditionnel d'inspiration tunisienne), il vous faut environ 100 à 150 grammes de viande hachée par brique, selon la taille et les préférences. Cela peut varier en fonction des autres ingrédients utilisés dans la préparation.







أنت قلت:
pour cuisiner courbatures frique 06 personne combien de viande
قال ChatGPT:
Pour préparer des courbatures fricassées (plat tunisien) pour 6 personnes, il vous faudra environ 500 à 600 grammes de viande hachée, en fonction de la quantité de garniture et de vos préférences personnelles. Cela permet d'assurer une portion généreuse pour chaque personne.







أنت قلت:
recette chorba frique
قال ChatGPT:
Voici une recette simple de chorba frikeh pour 6 personnes :

Ingrédients :
300 g de viande (agneau, poulet ou boeuf), coupée en morceaux
150 g de frikeh (blé vert concassé)
2 oignons, émincés
2 tomates, pelées et coupées en dés
2 cuillères à soupe de concentré de tomate
2 gousses d'ail, écrasées
1 cuillère à café de curcuma
1 cuillère à café de gingembre
1 cuillère à café de cannelle (facultatif)
1 bouquet de coriandre frais
1/2 cuillère à café de poivre noir
1/2 cuillère à café de sel
2 cuillères à soupe d'huile d'olive
1,5 L d'eau (ou plus, selon la consistance désirée)
Jus de 1 citron (facultatif)
1 poignée de pois chiches (préalablement cuits, facultatif)
Préparation :
Cuisson de la viande : Dans une grande marmite, faites chauffer l'huile d'olive. Ajoutez les morceaux de viande et faites-les dorer de tous les côtés.
Ajout des légumes : Ajoutez les oignons émincés et l'ail écrasé, puis faites revenir jusqu'à ce qu'ils soient tendres.
Tomates et épices : Incorporez les tomates en dés, le concentré de tomate, le curcuma, le gingembre, la cannelle, le poivre et le sel. Mélangez bien et laissez cuire quelques minutes.
Ajout de l'eau et du frikeh : Versez l'eau et portez à ébullition. Ajoutez le frikeh, puis baissez le feu et laissez mijoter pendant environ 30 minutes ou jusqu'à ce que le frikeh soit cuit et que la viande soit tendre.
Incorporation des pois chiches : Si vous utilisez des pois chiches, ajoutez-les dans la soupe et laissez mijoter encore 10 minutes.
Finition : Goûtez et ajustez l'assaisonnement si nécessaire. Ajoutez du jus de citron pour une touche d'acidité si désiré.
Servir : Décorez avec du coriandre frais haché avant de servir.
Cette chorba frikeh est un plat riche en saveurs, réconfortant et nutritif. Vous pouvez aussi ajuster la quantité de frikeh ou d'eau selon la consistance que vous préférez.
 
أعلى