| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base Service Class |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
* @author Your Name |
| 7 |
* @copyright Copyright (c) 2023, Your Company |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace EasyInvoice\Services; |
| 13 |
|
| 14 |
/** |
| 15 |
* Base Service Class |
| 16 |
* |
| 17 |
* Provides common functionality for all services in the plugin. |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
abstract class BaseService { |
| 22 |
|
| 23 |
/** |
| 24 |
* Service name |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $service_name; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor |
| 32 |
* |
| 33 |
* @since 1.0.0 |
| 34 |
* @param string $service_name The name of the service |
| 35 |
*/ |
| 36 |
public function __construct(string $service_name = '') { |
| 37 |
$this->service_name = $service_name ?: static::class; |
| 38 |
|
| 39 |
// Allow plugins to extend the service initialization |
| 40 |
do_action('easy_invoice_service_initialized', $this); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get service name |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function getServiceName(): string { |
| 50 |
return $this->service_name; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Log a message |
| 55 |
* |
| 56 |
* @since 1.0.0 |
| 57 |
* @param string $message The message to log |
| 58 |
* @param string $level The log level (debug, info, warning, error) |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
protected function log(string $message, string $level = 'info'): void { |
| 62 |
$log_message = sprintf( |
| 63 |
'[%s] [%s] %s: %s', |
| 64 |
date('Y-m-d H:i:s'), |
| 65 |
strtoupper($level), |
| 66 |
$this->service_name, |
| 67 |
$message |
| 68 |
); |
| 69 |
|
| 70 |
// Allow plugins to handle logging |
| 71 |
do_action('easy_invoice_service_log', $log_message, $level, $this->service_name); |
| 72 |
|
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Validate required parameters |
| 78 |
* |
| 79 |
* @since 1.0.0 |
| 80 |
* @param array $data The data to validate |
| 81 |
* @param array $required_fields The required field names |
| 82 |
* @return array Array of validation errors |
| 83 |
*/ |
| 84 |
protected function validateRequiredFields(array $data, array $required_fields): array { |
| 85 |
$errors = []; |
| 86 |
|
| 87 |
foreach ($required_fields as $field) { |
| 88 |
if (!isset($data[$field]) || empty($data[$field])) { |
| 89 |
$errors[] = sprintf(__('Field "%s" is required.', 'easy-invoice'), $field); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Allow plugins to add custom validation |
| 94 |
$errors = apply_filters('easy_invoice_service_validation_errors', $errors, $data, $required_fields, $this->service_name); |
| 95 |
|
| 96 |
return $errors; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Sanitize data |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* @param array $data The data to sanitize |
| 104 |
* @param array $sanitization_rules The sanitization rules |
| 105 |
* @return array The sanitized data |
| 106 |
*/ |
| 107 |
protected function sanitizeData(array $data, array $sanitization_rules = []): array { |
| 108 |
$sanitized = []; |
| 109 |
|
| 110 |
foreach ($data as $key => $value) { |
| 111 |
$rule = $sanitization_rules[$key] ?? 'text_field'; |
| 112 |
|
| 113 |
switch ($rule) { |
| 114 |
case 'email': |
| 115 |
$sanitized[$key] = sanitize_email($value); |
| 116 |
break; |
| 117 |
case 'url': |
| 118 |
$sanitized[$key] = esc_url_raw($value); |
| 119 |
break; |
| 120 |
case 'int': |
| 121 |
$sanitized[$key] = intval($value); |
| 122 |
break; |
| 123 |
case 'float': |
| 124 |
$sanitized[$key] = floatval($value); |
| 125 |
break; |
| 126 |
case 'textarea': |
| 127 |
$sanitized[$key] = sanitize_textarea_field($value); |
| 128 |
break; |
| 129 |
case 'html': |
| 130 |
$sanitized[$key] = wp_kses_post($value); |
| 131 |
break; |
| 132 |
case 'array': |
| 133 |
$sanitized[$key] = is_array($value) ? array_map('sanitize_text_field', $value) : []; |
| 134 |
break; |
| 135 |
default: |
| 136 |
$sanitized[$key] = sanitize_text_field($value); |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// Allow plugins to modify sanitized data |
| 142 |
return apply_filters('easy_invoice_service_sanitized_data', $sanitized, $data, $sanitization_rules, $this->service_name); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Format currency |
| 147 |
* |
| 148 |
* @since 1.0.0 |
| 149 |
* @param float $amount The amount to format |
| 150 |
* @param string $currency_code The currency code |
| 151 |
* @param string $position The currency position (left, right) |
| 152 |
* @return string The formatted currency |
| 153 |
*/ |
| 154 |
protected function formatCurrency(float $amount, string $currency_code = 'USD', string $position = 'left'): string { |
| 155 |
$formatted = number_format($amount, 2); |
| 156 |
|
| 157 |
switch ($position) { |
| 158 |
case 'right': |
| 159 |
$formatted = $formatted . ' ' . $currency_code; |
| 160 |
break; |
| 161 |
case 'left': |
| 162 |
default: |
| 163 |
$formatted = $currency_code . ' ' . $formatted; |
| 164 |
break; |
| 165 |
} |
| 166 |
|
| 167 |
// Allow plugins to modify currency formatting |
| 168 |
return apply_filters('easy_invoice_service_currency_format', $formatted, $amount, $currency_code, $position, $this->service_name); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Send email |
| 173 |
* |
| 174 |
* @since 1.0.0 |
| 175 |
* @param string $to The recipient email |
| 176 |
* @param string $subject The email subject |
| 177 |
* @param string $message The email message |
| 178 |
* @param array $headers The email headers |
| 179 |
* @return bool True if email was sent successfully |
| 180 |
*/ |
| 181 |
protected function sendEmail(string $to, string $subject, string $message, array $headers = []): bool { |
| 182 |
// Allow plugins to modify email data |
| 183 |
$email_data = apply_filters('easy_invoice_service_email_data', [ |
| 184 |
'to' => $to, |
| 185 |
'subject' => $subject, |
| 186 |
'message' => $message, |
| 187 |
'headers' => $headers |
| 188 |
], $this->service_name); |
| 189 |
|
| 190 |
// Allow plugins to handle email sending |
| 191 |
$sent = apply_filters('easy_invoice_service_email_send', null, $email_data, $this->service_name); |
| 192 |
|
| 193 |
if ($sent === null) { |
| 194 |
// Default WordPress email sending |
| 195 |
$sent = wp_mail( |
| 196 |
$email_data['to'], |
| 197 |
$email_data['subject'], |
| 198 |
$email_data['message'], |
| 199 |
$email_data['headers'] |
| 200 |
); |
| 201 |
} |
| 202 |
|
| 203 |
// Log email sending |
| 204 |
$this->log(sprintf('Email sent to %s: %s', $to, $sent ? 'success' : 'failed'), $sent ? 'info' : 'error'); |
| 205 |
|
| 206 |
return $sent; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Get plugin setting |
| 211 |
* |
| 212 |
* @since 1.0.0 |
| 213 |
* @param string $key The setting key |
| 214 |
* @param mixed $default The default value |
| 215 |
* @return mixed The setting value |
| 216 |
*/ |
| 217 |
protected function getSetting(string $key, $default = null) { |
| 218 |
$value = get_option('easy_invoice_' . $key, $default); |
| 219 |
|
| 220 |
// Allow plugins to modify settings |
| 221 |
return apply_filters('easy_invoice_service_setting', $value, $key, $default, $this->service_name); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Update plugin setting |
| 226 |
* |
| 227 |
* @since 1.0.0 |
| 228 |
* @param string $key The setting key |
| 229 |
* @param mixed $value The setting value |
| 230 |
* @return bool True if setting was updated successfully |
| 231 |
*/ |
| 232 |
protected function updateSetting(string $key, $value): bool { |
| 233 |
// Allow plugins to modify setting before update |
| 234 |
$value = apply_filters('easy_invoice_service_setting_update', $value, $key, $this->service_name); |
| 235 |
|
| 236 |
$updated = update_option('easy_invoice_' . $key, $value); |
| 237 |
|
| 238 |
// Allow plugins to perform actions after setting update |
| 239 |
do_action('easy_invoice_service_setting_updated', $key, $value, $updated, $this->service_name); |
| 240 |
|
| 241 |
return $updated; |
| 242 |
} |
| 243 |
} |