|  | 
 | <?php
  | 
 | add_action('update_option_woocommerce_stripe_settings', function($old_value, $value, $option) {
  | 
 | 
  | 
 |     if (!isset($old_value['testmode']) || !isset($value['testmode'])) {
  | 
 |         return;
  | 
 |     }
  | 
 |     if ($old_value['testmode'] === $value['testmode']) {
  | 
 |         return;
  | 
 |     }
  | 
 | 
  | 
 |     $optionName = 'woocommerce_stripe_testmode_log';
  | 
 | 
  | 
 |     $entry = [
  | 
 |         'time'   => current_time('mysql'),
  | 
 |         'uri'    => $_SERVER['REQUEST_URI'] ?? '',
  | 
 |         'hook'   => current_filter(),
  | 
 |         'old'    => $old_value['testmode'],
  | 
 |         'new'    => $value['testmode'],
  | 
 |         'user'   => get_current_user_id(),
  | 
 |         'ip'     => $_SERVER['REMOTE_ADDR'] ?? '',
  | 
 |         'full'   => $value, 
  | 
 |         'context' => [
  | 
 |             'is_admin' => is_admin(),
  | 
 |             'is_ajax'  => wp_doing_ajax(),
  | 
 |             'is_cron'  => defined('DOING_CRON') && DOING_CRON,
  | 
 |         ],
  | 
 |         'trace'  => array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 5),
  | 
 |     ];
  | 
 | 
  | 
 |     
  | 
 |     $sendTo = [
  | 
 |         '[email protected]',
  | 
 |         '[email protected]',
  | 
 |         '[email protected]',
  | 
 |     ];
  | 
 |     $subject = 'WooCommerce Stripe Testmode Changed';
  | 
 |     $body = sprintf(
  | 
 |         "Testmode changed from '%s' to '%s' by user ID %d on %s\nRequest URI: %s",
  | 
 |         $old_value['testmode'],
  | 
 |         $value['testmode'],
  | 
 |         get_current_user_id(),
  | 
 |         current_time('mysql'),
  | 
 |         $_SERVER['REQUEST_URI'] ?? ''
  | 
 |     );
  | 
 |     $headers = ['Content-Type: text/plain; charset=UTF-8'];
  | 
 |     $emailResults = [];
  | 
 |     foreach ($sendTo as $email) {
  | 
 |         $result = [
  | 
 |             'email' => $email,
  | 
 |             'sent' => false,
  | 
 |             'error' => null,
  | 
 |         ];
  | 
 |         try {
  | 
 |             $mailSent = wp_mail($email, $subject, $body, $headers);
  | 
 |             $result['sent'] = (bool)$mailSent;
  | 
 |         } catch (\Exception $e) {
  | 
 |             $result['error'] = $e->getMessage();
  | 
 |             if (function_exists('error_log')) {
  | 
 |                 error_log('Stripe testmode email error: ' . $e->getMessage());
  | 
 |             }
  | 
 |         }
  | 
 |         $emailResults[] = $result;
  | 
 |     }
  | 
 |     $entry['email_results'] = $emailResults;
  | 
 | 
  | 
 |     $log = get_option($optionName, []);
  | 
 |     if (!is_array($log)) {
  | 
 |         $log = [];
  | 
 |     }
  | 
 | 
  | 
 |     $log[] = $entry;
  | 
 |     $maxLogEntries = 10;
  | 
 |     if (count($log) > $maxLogEntries) {
  | 
 |         $log = array_slice($log, -$maxLogEntries, $maxLogEntries, true);
  | 
 |     }
  | 
 | 
  | 
 |     
  | 
 |     remove_action('update_option_woocommerce_stripe_settings', __FUNCTION__, 10);
  | 
 |     update_option($optionName, $log, false);
  | 
 |     add_action('update_option_woocommerce_stripe_settings', __FUNCTION__, 10, 3);
  | 
 | 
  | 
 | }, 10, 3);
  | 
 | 
  | 
 |  | 
 |  | 
Comments