PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.4.0
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.4.0
2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 All 57 releases
easy-invoice / includes / Services / BaseService.php

BaseService.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.4.0, at includes/Services/BaseService.php

251 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 gmdate('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 /* translators: %s: field name. */
90 $errors[] = sprintf(__('Field "%s" is required.', 'easy-invoice'), $field);
91 }
92 }
93
94 // Allow plugins to add custom validation
95 $errors = apply_filters('easy_invoice_service_validation_errors', $errors, $data, $required_fields, $this->service_name);
96
97 return $errors;
98 }
99
100 /**
101 * Sanitize data
102 *
103 * @since 1.0.0
104 * @param array $data The data to sanitize
105 * @param array $sanitization_rules The sanitization rules
106 * @return array The sanitized data
107 */
108 protected function sanitizeData(array $data, array $sanitization_rules = []): array {
109 $sanitized = [];
110
111 foreach ($data as $key => $value) {
112 $rule = $sanitization_rules[$key] ?? 'text_field';
113
114 switch ($rule) {
115 case 'email':
116 $sanitized[$key] = sanitize_email($value);
117 break;
118 case 'url':
119 $sanitized[$key] = esc_url_raw($value);
120 break;
121 case 'int':
122 $sanitized[$key] = intval($value);
123 break;
124 case 'float':
125 $sanitized[$key] = floatval($value);
126 break;
127 case 'textarea':
128 $sanitized[$key] = sanitize_textarea_field($value);
129 break;
130 case 'html':
131 $sanitized[$key] = wp_kses_post($value);
132 break;
133 case 'array':
134 $sanitized[$key] = is_array($value) ? array_map('sanitize_text_field', $value) : [];
135 break;
136 default:
137 $sanitized[$key] = sanitize_text_field($value);
138 break;
139 }
140 }
141
142 // Allow plugins to modify sanitized data
143 return apply_filters('easy_invoice_service_sanitized_data', $sanitized, $data, $sanitization_rules, $this->service_name);
144 }
145
146 /**
147 * Format currency
148 *
149 * @since 1.0.0
150 * @param float $amount The amount to format
151 * @param string $currency_code The currency code
152 * @param string $position The currency position (left, right)
153 * @return string The formatted currency
154 */
155 protected function formatCurrency(float $amount, string $currency_code = 'USD', string $position = 'left'): string {
156 $formatted = number_format($amount, 2);
157
158 switch ($position) {
159 case 'right':
160 $formatted = $formatted . ' ' . $currency_code;
161 break;
162 case 'left':
163 default:
164 $formatted = $currency_code . ' ' . $formatted;
165 break;
166 }
167
168 // Allow plugins to modify currency formatting
169 return apply_filters('easy_invoice_service_currency_format', $formatted, $amount, $currency_code, $position, $this->service_name);
170 }
171
172 /**
173 * Send email
174 *
175 * @since 1.0.0
176 * @param string $to The recipient email
177 * @param string $subject The email subject
178 * @param string $message The email message
179 * @param array $headers The email headers
180 * @return bool True if email was sent successfully
181 */
182 protected function sendEmail(string $to, string $subject, string $message, array $headers = [], array $attachments = []): bool {
183 // Allow plugins to modify email data
184 //
185 // $attachments defaults to empty, so every existing caller behaves exactly as
186 // before. It exists so a document can be attached now that PdfRenderer can
187 // produce one on the server — wp_mail() takes file paths, which is why the
188 // renderer writes to a temp file rather than handing back bytes.
189 $email_data = apply_filters('easy_invoice_service_email_data', [
190 'to' => $to,
191 'subject' => $subject,
192 'message' => $message,
193 'headers' => $headers,
194 'attachments' => $attachments
195 ], $this->service_name);
196
197 // Allow plugins to handle email sending
198 $sent = apply_filters('easy_invoice_service_email_send', null, $email_data, $this->service_name);
199
200 if ($sent === null) {
201 // Default WordPress email sending
202 $sent = wp_mail(
203 $email_data['to'],
204 $email_data['subject'],
205 $email_data['message'],
206 $email_data['headers'],
207 isset($email_data['attachments']) ? (array) $email_data['attachments'] : []
208 );
209 }
210
211 // Log email sending
212 $this->log(sprintf('Email sent to %s: %s', $to, $sent ? 'success' : 'failed'), $sent ? 'info' : 'error');
213
214 return $sent;
215 }
216
217 /**
218 * Get plugin setting
219 *
220 * @since 1.0.0
221 * @param string $key The setting key
222 * @param mixed $default The default value
223 * @return mixed The setting value
224 */
225 protected function getSetting(string $key, $default = null) {
226 $value = get_option('easy_invoice_' . $key, $default);
227
228 // Allow plugins to modify settings
229 return apply_filters('easy_invoice_service_setting', $value, $key, $default, $this->service_name);
230 }
231
232 /**
233 * Update plugin setting
234 *
235 * @since 1.0.0
236 * @param string $key The setting key
237 * @param mixed $value The setting value
238 * @return bool True if setting was updated successfully
239 */
240 protected function updateSetting(string $key, $value): bool {
241 // Allow plugins to modify setting before update
242 $value = apply_filters('easy_invoice_service_setting_update', $value, $key, $this->service_name);
243
244 $updated = update_option('easy_invoice_' . $key, $value);
245
246 // Allow plugins to perform actions after setting update
247 do_action('easy_invoice_service_setting_updated', $key, $value, $updated, $this->service_name);
248
249 return $updated;
250 }
251 }