PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-notification-settings.php

class-mainwp-notification-settings.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.1, at class/class-mainwp-notification-settings.php

733 lines 35.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Notification Settings
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 /**
11 * Class MainWP_Notification_Settings
12 *
13 * @package MainWP\Dashboard
14 */
15 class MainWP_Notification_Settings { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
16
17
18 /**
19 * Private static variable to hold the single instance of the class.
20 *
21 * @static
22 *
23 * @var mixed Default null.
24 */
25 private static $instance = null;
26
27
28 /**
29 * Get class name.
30 *
31 * @return string Class name.
32 */
33 public static function get_class_name() {
34 return __CLASS__;
35 }
36
37 /**
38 * Create a new self instance.
39 *
40 * @return mixed static::$instance
41 */
42 public static function instance() {
43 if ( null === static::$instance ) {
44 static::$instance = new self();
45 }
46 return static::$instance;
47 }
48
49 /**
50 * MainWP_Notification_Settings constructor.
51 *
52 * Run each time the class is called.
53 */
54 public function __construct() {
55 }
56
57 /**
58 * Manage general email settings.
59 *
60 * @return bool True if saved successfully, false if not.
61 *
62 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
63 */
64 public static function emails_general_settings_handle() {
65 if ( isset( $_POST['submit'] ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'SettingsEmail' ) ) {
66 $emails_settings = get_option( 'mainwp_settings_notification_emails' );
67 if ( ! is_array( $emails_settings ) ) {
68 $emails_settings = array();
69 }
70
71 $type = isset( $_POST['mainwp_setting_emails_type'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_setting_emails_type'] ) ) : '';
72 $update_settings = isset( $_POST['mainwp_settingEmails'][ $type ] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['mainwp_settingEmails'][ $type ] ) ) : '';
73 $update_settings['disable'] = ( isset( $_POST['mainwp_settingEmails'][ $type ] ) && isset( $_POST['mainwp_settingEmails'][ $type ]['disable'] ) ) ? 0 : 1; // to set 'disable' values.
74 $emails_settings[ $type ] = $update_settings;
75
76 /**
77 * Action: mainwp_before_save_email_settings
78 *
79 * Fires before save email settings.
80 *
81 * @since 4.1
82 */
83 do_action( 'mainwp_before_save_email_settings', $type, $update_settings );
84
85 MainWP_Utility::update_option( 'mainwp_settings_notification_emails', $emails_settings );
86
87 /**
88 * Action: mainwp_after_save_email_settings
89 *
90 * Fires after save email settings.
91 *
92 * @since 4.1
93 */
94 do_action( 'mainwp_after_save_email_settings', $emails_settings );
95
96 return true;
97 }
98 return false;
99 }
100
101 /**
102 * Render all email settings options.
103 *
104 * Credits.
105 *
106 * Plugin-Name: WooCommerce.
107 * Plugin URI: https://woocommerce.com/.
108 * Author: Automattic.
109 * Author URI: https://woocommerce.com.
110 * License: GPLv3 or later.
111 *
112 * @param bool $updated True if page loaded after update, false if not.
113 */
114 public function render_all_settings( $updated ) {
115
116 if ( ! mainwp_current_user_have_right( 'dashboard', 'manage_dashboard_settings' ) ) {
117 mainwp_do_not_have_permissions( esc_html__( 'manage dashboard settings', 'mainwp' ) );
118 return;
119 }
120
121 $email_description = '';
122 $notification_emails = static::get_notification_types();
123 $emails_settings = get_option( 'mainwp_settings_notification_emails' );
124 if ( ! is_array( $emails_settings ) ) {
125 $emails_settings = array();
126 }
127
128 ?>
129 <div id="mainwp-all-emails-settings" class="ui segment">
130 <?php if ( $updated ) : ?>
131 <div class="ui green message"><i class="close icon"></i><?php esc_html_e( 'Settings have been saved successfully!', 'mainwp' ); ?></div>
132 <?php endif; ?>
133 <div class="ui info message">
134 <?php printf( esc_html__( 'Email notifications sent from MainWP Dashboard are listed below. Click on an email to configure it. For additional help, please see %1$sthis help document%2$s.', 'mainwp' ), '<a href="https://kb.mainwp.com/docs/email-settings/">', '</a> <i class="external alternate icon"></i>' ); // NOSONAR - noopener - open safe. ?>
135 </div>
136 <table class="ui unstackable table" id="mainwp-emails-settings-table">
137 <thead>
138 <tr>
139 <th scope="col" class="collapsing"><?php esc_html_e( 'Status', 'mainwp' ); ?></th>
140 <th scope="col" data-priority="1"><?php esc_html_e( 'Email', 'mainwp' ); ?></th>
141 <th scope="col"><?php esc_html_e( 'Description', 'mainwp' ); ?></th>
142 <th scope="col"><?php esc_html_e( 'Recipient(s)', 'mainwp' ); ?></th>
143 <th scope="col" class="no-sort collapsing" data-priority="2" style="text-align:right">&nbsp;</th>
144 </tr>
145 </thead>
146 <tbody>
147 <?php foreach ( $notification_emails as $type => $name ) : ?>
148 <?php
149 $options = isset( $emails_settings[ $type ] ) ? $emails_settings[ $type ] : array();
150 $default = static::get_default_emails_fields( $type, '', true );
151 $options = array_merge( $default, $options );
152
153 $email_description = static::get_settings_desc( $type );
154 ?>
155 <tr>
156 <td><?php echo ( ! $options['disable'] ) ? '<span data-tooltip="Enabled." data-position="right center" data-inverted=""><i class="circular green check inverted icon"></i></span>' : '<span data-tooltip="Disabled." data-position="right center" data-inverted=""><i class="circular x icon inverted disabled"></i></span>'; ?></td>
157 <td><a href="admin.php?page=SettingsEmail&edit-email=<?php echo esc_html( rawurlencode( $type ) ); ?>" data-tooltip="<?php esc_attr_e( 'Click to configure the email.', 'mainwp' ); ?>" data-position="right center" data-inverted=""><?php echo esc_html( $name ); ?></a></td>
158 <td><?php echo esc_html( $email_description ); ?></td>
159 <td><?php echo esc_html( $options['recipients'] ); ?></td>
160 <td style="text-align:right"><a href="admin.php?page=SettingsEmail&edit-email=<?php echo esc_html( rawurlencode( $type ) ); ?>" data-tooltip="<?php esc_attr_e( 'Click to configure the email.', 'mainwp' ); ?>" data-position="left center" data-inverted="" class="ui green mini button"><?php esc_html_e( 'Manage', 'mainwp' ); ?></a></td>
161 </tr>
162 <?php endforeach; ?>
163 </tbody>
164 <tfoot>
165 <tr>
166 <th scope="col" class="collapsing"><?php esc_html_e( 'Status', 'mainwp' ); ?></th>
167 <th scope="col"><?php esc_html_e( 'Email', 'mainwp' ); ?></th>
168 <th scope="col"><?php esc_html_e( 'Description', 'mainwp' ); ?></th>
169 <th scope="col"><?php esc_html_e( 'Recipient(s)', 'mainwp' ); ?></th>
170 <th scope="col">&nbsp;</th>
171 </tr>
172 </tfoot>
173 </table>
174 <?php
175 /**
176 * Action: mainwp_settings_email_settings
177 *
178 * Fires after the default email settings.
179 *
180 * @since 4.1
181 */
182 do_action( 'mainwp_settings_email_settings' );
183 ?>
184 <script type="text/javascript">
185 var responsive = true;
186 if( jQuery( window ).width() > 1140 ) {
187 responsive = false;
188 }
189 jQuery( document ).ready( function() {
190 jQuery( '#mainwp-emails-settings-table' ).DataTable( {
191 "stateSave": true,
192 "paging": false,
193 "ordering": true,
194 "columnDefs": [ { "orderable": false, "targets": [ 4 ] } ],
195 "order": [ [ 2, "asc" ] ],
196 "responsive": responsive,
197 } );
198 } );
199 </script>
200 </div>
201 <?php
202 }
203
204 /**
205 * Render email settings page.
206 *
207 * Credits.
208 *
209 * Plugin-Name: WooCommerce.
210 * Plugin URI: https://woocommerce.com/.
211 * Author: Automattic.
212 * Author URI: https://woocommerce.com.
213 * License: GPLv3 or later.
214 *
215 * @param string $type Email notification type.
216 * @param bool $updated_templ True if page loaded after update, false if not.
217 *
218 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_View::render_edit_template()
219 * @uses \MainWP\Dashboard\MainWP_Notification_Template::get_template_name_by_notification_type()
220 * @uses \MainWP\Dashboard\MainWP_Notification_Template::is_overrided_template()
221 */
222 public function render_edit_settings( $type, $updated_templ ) {
223
224 if ( ! mainwp_current_user_have_right( 'dashboard', 'manage_dashboard_settings' ) ) {
225 mainwp_do_not_have_permissions( esc_html__( 'manage dashboard settings', 'mainwp' ) );
226 return;
227 }
228
229 $emails_settings = get_option( 'mainwp_settings_notification_emails' );
230 if ( ! is_array( $emails_settings ) ) {
231 $emails_settings = array();
232 }
233
234 $options = isset( $emails_settings[ $type ] ) ? $emails_settings[ $type ] : array();
235
236 $default = static::get_default_emails_fields( $type, '', true );
237 $options = array_merge( $default, $options );
238
239 $title = static::get_notification_types( $type );
240
241 $email_description = static::get_settings_desc( $type );
242
243 ?>
244 <div id="mainwp-emails-settings" class="ui segment">
245 <?php static::render_update_template_message( $updated_templ ); ?>
246 <div class="ui form">
247 <form method="POST" action="admin.php?page=SettingsEmail">
248 <input type="hidden" name="wp_nonce" value="<?php echo esc_attr( wp_create_nonce( 'SettingsEmail' ) ); ?>" />
249 <input type="hidden" name="mainwp_setting_emails_type" value="<?php echo esc_html( $type ); ?>" />
250 <?php if ( MainWP_Utility::show_mainwp_message( 'notice', 'mainwp-email-tokens-info-message' ) ) : ?>
251 <div class="ui info message">
252 <i class="close icon mainwp-notice-dismiss" notice-id="mainwp-manage-updates-message"></i>
253 <?php printf( esc_html__( '%1$sBoilerplate%2$s and %3$sReports%4$s extensions tokens are supported in the email settings and templates if Extensions are in use.', 'mainwp' ), '<a href="https://mainwp.com/extension/boilerplate/" target="_blank">', '</a> <i class="external alternate icon"></i>', '<a href="https://mainwp.com/extension/pro-reports/" target="_blank">', '</a> <i class="external alternate icon"></i>' ); // NOSONAR - noopener - open safe. ?>
254 </div>
255 <?php endif; ?>
256 <h3 class="ui header">
257 <?php MainWP_Settings_Indicator::render_indicator( 'header', 'settings-field-indicator-email-' . esc_attr( $type ) ); ?>
258 <?php echo esc_html( $title ); ?></h3>
259 <div class="sub header"><?php echo esc_html( $email_description ); ?></h3></div>
260 <div class="ui divider"></div>
261 <?php
262 $def_val = MainWP_Settings_Indicator::get_defaults_email_settings_value( $type, 'disable' );
263 ?>
264 <div class="ui grid field settings-field-indicator-wrapper settings-field-indicator-email-<?php echo esc_attr( $type ); ?>" default-indi-value="<?php echo esc_attr( $def_val ); ?>">
265 <label class="six wide column middle aligned">
266 <?php
267 MainWP_Settings_Indicator::render_not_default_email_settings_indicator( $type, 'disable', (int) $options['disable'] );
268 esc_html_e( 'Enable', 'mainwp' );
269 ?>
270 </label>
271 <div class="ten wide column ui toggle checkbox" data-tooltip="<?php esc_attr_e( 'Enable this email notification.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
272 <input type="checkbox" inverted-value="1" class="settings-field-value-change-handler" name="mainwp_settingEmails[<?php echo esc_html( $type ); ?>][disable]" id="mainwp_settingEmails[<?php echo esc_html( $type ); ?>][disable]" <?php echo ( 0 === (int) $options['disable'] ) ? 'checked="true"' : ''; ?>/>
273 </div>
274 </div>
275 <?php
276 $def_val = MainWP_Settings_Indicator::get_defaults_email_settings_value( $type, 'recipients' );
277 ?>
278 <div class="ui grid field settings-field-indicator-wrapper settings-field-indicator-email-<?php echo esc_attr( $type ); ?>" default-indi-value="<?php echo esc_attr( $def_val ); ?>">
279 <label class="six wide column middle aligned">
280 <?php
281 MainWP_Settings_Indicator::render_not_default_email_settings_indicator( $type, 'recipients', $options['recipients'] );
282 esc_html_e( 'Recipient(s)', 'mainwp' );
283 ?>
284 </label>
285 <div class="ten wide column" data-tooltip="<?php esc_attr_e( 'You can add multiple emails by separating them with comma.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
286 <input type="text" class="settings-field-value-change-handler" name="mainwp_settingEmails[<?php echo esc_html( $type ); ?>][recipients]" id="mainwp_settingEmails[<?php echo esc_html( $type ); ?>][recipients]" value="<?php echo esc_html( $options['recipients'] ); ?>"/>
287 </div>
288 </div>
289 <?php
290 $def_val = MainWP_Settings_Indicator::get_defaults_email_settings_value( $type, 'subject' );
291 ?>
292 <div class="ui grid field settings-field-indicator-wrapper settings-field-indicator-email-<?php echo esc_attr( $type ); ?>" default-indi-value="<?php echo esc_attr( $def_val ); ?>">
293 <label class="six wide column middle aligned">
294 <?php
295 MainWP_Settings_Indicator::render_not_default_email_settings_indicator( $type, 'subject', $options['subject'] );
296 esc_html_e( 'Subject', 'mainwp' );
297 ?>
298 </label>
299 <div class="ten wide column" data-tooltip="<?php esc_attr_e( 'Enter the email subject.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
300 <input type="text" class="settings-field-value-change-handler" name="mainwp_settingEmails[<?php echo esc_html( $type ); ?>][subject]" id="mainwp_settingEmails[<?php echo esc_html( $type ); ?>][subject]" value="<?php echo esc_html( $options['subject'] ); ?>"/>
301 </div>
302 </div>
303 <?php
304 $def_val = MainWP_Settings_Indicator::get_defaults_email_settings_value( $type, 'heading' );
305 ?>
306 <div class="ui grid field settings-field-indicator-wrapper settings-field-indicator-email-<?php echo esc_attr( $type ); ?>" default-indi-value="<?php echo esc_attr( $def_val ); ?>">
307 <label class="six wide column middle aligned">
308 <?php
309 MainWP_Settings_Indicator::render_not_default_email_settings_indicator( $type, 'heading', $options['heading'] );
310 esc_html_e( 'Email heading', 'mainwp' );
311 ?>
312 </label>
313 <div class="ten wide column" data-tooltip="<?php esc_attr_e( 'Enter the email heading.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
314 <input type="text" class="settings-field-value-change-handler" name="mainwp_settingEmails[<?php echo esc_html( $type ); ?>][heading]" id="mainwp_settingEmails[<?php echo esc_html( $type ); ?>][heading]" value="<?php echo esc_html( $options['heading'] ); ?>"/>
315 </div>
316 </div>
317 <div class="ui grid field settings-field-indicator-wrapper settings-field-indicator-email-<?php echo esc_attr( $type ); ?>" >
318 <?php
319 $templ = MainWP_Notification_Template::get_template_name_by_notification_type( $type );
320 $overrided = MainWP_Notification_Template::instance()->is_overrided_template( $type );
321 ?>
322 <label class="six wide column middle aligned">
323 <?php
324 MainWP_Settings_Indicator::render_not_default_email_settings_indicator( $type, 'overrided', (int) $overrided );
325 esc_html_e( 'HTML template', 'mainwp' );
326 ?>
327 </label>
328 <div class="ui ten wide column" data-tooltip="<?php esc_attr_e( 'Manage the email HTML template.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
329 <?php
330 /**
331 *
332 * Use mainwp_notification_template_copy_message instead.
333 *
334 * @deprecated Since v4.3.
335 */
336 $copy_message = apply_filters( 'minwp_notification_template_copy_message', '', $templ, $type, $overrided );
337 /**
338 *
339 * Filter mainwp_notification_template_copy_message.
340 *
341 * @since v4.3
342 */
343 $copy_message = apply_filters( 'mainwp_notification_template_copy_message', $copy_message, $templ, $type, $overrided );
344 if ( empty( $copy_message ) ) {
345 $copy_message = $overrided ? esc_html__( 'This template has been overridden and can be found in:', 'mainwp' ) . ' <code>wp-content/uploads/mainwp/templates/' . esc_html( $templ ) . '</code>' : esc_html__( 'To override and edit this email template copy:', 'mainwp' ) . ' <code>mainwp/templates/' . esc_html( $templ ) . '</code> ' . esc_html__( 'to the folder:', 'mainwp' ) . ' <code>wp-content/uploads/mainwp/templates/' . esc_html( $templ ) . '</code>';
346 }
347 echo $copy_message; // phpcs:ignore WordPress.Security.EscapeOutput
348 ?>
349 </div>
350 </div>
351 <div class="ui grid field settings-field-indicator-wrapper settings-field-indicator-email-<?php echo esc_attr( $type ); ?>" >
352 <label class="six wide column middle aligned"></label>
353 <div class="ui ten wide column" data-tooltip="<?php esc_attr_e( 'Manage the email HTML template.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
354 <?php if ( $overrided ) : ?>
355 <a href="<?php echo esc_url( wp_nonce_url( 'admin.php?page=SettingsEmail&edit-email=' . $type, 'delete-email-template' ) ); ?>" onclick="mainwp_confirm('<?php echo esc_js( 'Are you sure you want to delete this template file?', 'mainwp' ); ?>', function(){ window.location = jQuery('a#email-delete-template').attr('href');}); return false;" id="email-delete-template" class="ui button"><?php esc_html_e( 'Return to Default Template', 'mainwp' ); ?></a>
356 <?php else : ?>
357 <a href="<?php echo esc_url( wp_nonce_url( 'admin.php?page=SettingsEmail&edit-email=' . $type, 'copy-email-template' ) ); ?>" class="ui button"><?php esc_html_e( 'Copy file to uploads', 'mainwp' ); ?></a>
358 <?php endif; ?>
359 <?php if ( $overrided ) : ?>
360 <a href="javascript:void(0)" class="ui button" onclick="mainwp_view_template('<?php echo esc_js( $type ); ?>', true ); return false;"><?php esc_html_e( 'Edit Template', 'mainwp' ); ?></a>
361 <?php else : ?>
362 <a href="javascript:void(0)" class="ui button" onclick="mainwp_view_template('<?php echo esc_js( $type ); ?>', true ); return false;"><?php esc_html_e( 'View Template', 'mainwp' ); ?></a>
363 <?php endif; ?>
364 </div>
365 </div>
366 <div class="ui divider"></div>
367 <a href="admin.php?page=SettingsEmail" class="ui big basic green button"><?php esc_html_e( 'Back', 'mainwp' ); ?></a>
368 <input type="submit" name="submit" id="submit" class="ui green big button right floated" value="<?php esc_attr_e( 'Save Settings', 'mainwp' ); ?>"/>
369 <div style="clear:both"></div>
370 </form>
371 </div>
372 </div>
373 <?php MainWP_Manage_Sites_View::render_edit_template( $type ); ?>
374 <?php
375 }
376
377
378 /**
379 * Get email settings description.
380 *
381 * @param string $type Email notification type.
382 *
383 * @return string $email_description Email settings Description.
384 */
385 public static function get_settings_desc( $type ) {
386 $email_description = '';
387 if ( 'daily_digest' === $type ) {
388 $email_description = esc_html__( 'Daily notification about available updates and disconnected sites.', 'mainwp' );
389 } elseif ( 'uptime' === $type ) {
390 $email_description = esc_html__( 'Alert if any of your websites is down.', 'mainwp' );
391 } elseif ( 'site_health' === $type ) {
392 $email_description = esc_html__( 'Alert if any of your websites site health goes under the threshold.', 'mainwp' );
393 } elseif ( 'http_check' === $type ) {
394 $email_description = esc_html__( 'Alert if any of your websites return unexpected HTTP status after running updates.', 'mainwp' );
395 } elseif ( 'deactivated_license_alert' === $type ) {
396 $email_description = esc_html__( 'Receive a notification when an extension\'s license is deactivated.', 'mainwp' );
397 }
398
399 $addition_desc = apply_filters( 'mainwp_notification_type_desc', '', $type );
400 if ( ! empty( $addition_desc ) ) {
401 $email_description = $addition_desc;
402 }
403
404 return $email_description;
405 }
406
407 /**
408 * Render the notification after the update process.
409 *
410 * @param int $updated Update code (copied, saved or deleted).
411 */
412 public static function render_update_template_message( $updated ) {
413 ?>
414 <?php if ( 1 === (int) $updated ) : ?>
415 <div class="ui message green"><i class="close icon"></i> <?php esc_html_e( 'Custom email template deleted successfully.', 'mainwp' ); ?></div>
416 <?php endif; ?>
417 <?php if ( 2 === (int) $updated ) : ?>
418 <div class="ui message green"><i class="close icon"></i> <?php esc_html_e( 'Email template copied successfully.', 'mainwp' ); ?></div>
419 <?php endif; ?>
420 <?php if ( 3 === (int) $updated ) : ?>
421 <div class="ui message green"><i class="close icon"></i> <?php esc_html_e( 'Email template updated successfully.', 'mainwp' ); ?></div>
422 <?php endif; ?>
423 <?php
424 }
425
426 /**
427 * Get email notification types.
428 *
429 * @param string $type Email notification type.
430 *
431 * @return mixed Notification types.
432 */
433 public static function get_notification_types( $type = '' ) {
434 $types = array(
435 'daily_digest' => esc_html__( 'Daily Digest Email', 'mainwp' ),
436 'uptime' => esc_html__( 'Basic Uptime Monitoring Email', 'mainwp' ),
437 'site_health' => esc_html__( 'Site Health Monitoring Email', 'mainwp' ),
438 'deactivated_license_alert' => esc_html__( 'Extension License Deactivation Notification Email', 'mainwp' ),
439 );
440
441 $enable_http_check = get_option( 'mainwp_check_http_response', 0 );
442
443 if ( $enable_http_check ) {
444 $types['http_check'] = esc_html__( 'After Updates HTTP Check Email', 'mainwp' );
445 }
446
447 $addition_types = apply_filters( 'mainwp_notification_types', array(), $type );
448 if ( ! empty( $addition_types ) ) {
449 $types = array_merge( $types, $addition_types );
450 }
451
452 if ( empty( $type ) ) {
453 return $types;
454 } else {
455 return ( isset( $types[ $type ] ) ) ? $types[ $type ] : false;
456 }
457 }
458
459 /**
460 * Get general email notifications settings.
461 *
462 * @param string $type Email type.
463 *
464 * @see function get_notification_types()
465 *
466 * @return array An array containing the email settings.
467 */
468 public static function get_general_email_settings( $type ) {
469 $settings = get_option( 'mainwp_settings_notification_emails', array() );
470 if ( ! is_array( $settings ) ) {
471 $settings = array();
472 }
473
474 $options = isset( $settings[ $type ] ) ? $settings[ $type ] : array();
475 $default = static::get_default_emails_fields( $type, '', true );
476 return array_merge( $default, $options );
477 }
478
479 /**
480 * Get general notification email.
481 *
482 * @return string|empty recipients.
483 */
484 public static function get_general_email() {
485 $gen_email_settings = static::get_general_email_settings( 'daily_digest' );
486 return isset( $gen_email_settings['recipients'] ) ? $gen_email_settings['recipients'] : '';
487 }
488
489 /**
490 * Update general notification email.
491 *
492 * @param string $emails Emails.
493 *
494 * @return void
495 */
496 public static function update_general_email( $emails ) {
497 $emails_settings = get_option( 'mainwp_settings_notification_emails' );
498 if ( ! is_array( $emails_settings ) ) {
499 $emails_settings = array();
500 }
501 $type = 'daily_digest';
502 $update_settings = isset( $emails_settings[ $type ] ) ? $emails_settings[ $type ] : array();
503 $update_settings['recipients'] = $emails;
504 $emails_settings[ $type ] = $update_settings;
505 MainWP_Utility::update_option( 'mainwp_settings_notification_emails', $emails_settings );
506 }
507
508 /**
509 * Get email settings for a single site.
510 *
511 * @param string $type Email type.
512 * @param object $website Object containing the child site information.
513 *
514 * @return array An array containing the email settings.
515 *
516 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_tokens_site_values()
517 * @uses \MainWP\Dashboard\MainWP_System_Utility::replace_tokens_values()
518 */
519 public static function get_site_email_settings( $type, $website ) {
520 if ( empty( $website ) || ! property_exists( $website, 'settings_notification_emails' ) ) {
521 return array( 'disable' => 1 );
522 }
523 $settings = array();
524 if ( ! empty( $website->settings_notification_emails ) ) {
525 $settings = json_decode( $website->settings_notification_emails, true );
526 }
527
528 if ( ! is_array( $settings ) ) {
529 $settings = array();
530 }
531
532 $options = isset( $settings[ $type ] ) ? $settings[ $type ] : array();
533 $default = static::get_default_emails_fields( $type );
534 $options = array_merge( $default, $options );
535
536 if ( preg_match( '/\[[^\]]+\]/is', $options['subject'] . $options['heading'], $matches ) ) {
537 $tokens_values = MainWP_System_Utility::get_tokens_site_values( $website );
538 $options['subject'] = MainWP_System_Utility::replace_tokens_values( $options['subject'], $tokens_values );
539 $options['heading'] = MainWP_System_Utility::replace_tokens_values( $options['heading'], $tokens_values );
540 }
541
542 if ( preg_match( '/\[[^\]]+\]/is', $options['recipients'] . $options['subject'] . $options['heading'], $matches ) ) {
543 // support boilerplate and reports tokens.
544 $fields = array( 'recipients', 'subject', 'heading' );
545 $options = static::replace_tokens_for_settings( $options, $fields, $website );
546 }
547
548 return $options;
549 }
550
551 /**
552 * Get default email notifications values.
553 *
554 * @param string $type Email type.
555 * @param string $field Field name.
556 * @param bool $general General or individual site settings.
557 *
558 * @return array|string Email field settings value.
559 *
560 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_notification_email()
561 */
562 public static function get_default_emails_fields( $type, $field = '', $general = false ) { // phpcs:ignore -- NOSONAR - complex.
563
564 $recipients = MainWP_System_Utility::get_notification_email();
565 $disable = $general ? 0 : 1;
566
567 $default_fields = array(
568 'daily_digest' => array(
569 'disable' => $disable,
570 'recipients' => $recipients,
571 'subject' => $general ? 'Daily Digest' : '[site.name] Daily Digest',
572 'heading' => $general ? 'Daily Digest' : '[site.name] Daily Digest',
573 ),
574 'uptime' => array(
575 'disable' => $disable,
576 'recipients' => $recipients,
577 'subject' => $general ? 'Uptime Monitoring' : '[site.name] Uptime Monitoring',
578 'heading' => $general ? 'Uptime Monitoring' : '[site.name] Uptime Monitoring',
579 ),
580 'site_health' => array(
581 'disable' => $disable,
582 'recipients' => $recipients,
583 'subject' => $general ? 'Site Health Monitoring' : '[site.name] Site Health Monitoring',
584 'heading' => $general ? 'Site Health Monitoring' : '[site.name] Site Health Monitoring',
585 ),
586 'http_check' => array(
587 'disable' => $disable,
588 'recipients' => $recipients,
589 'subject' => 'HTTP response check',
590 'heading' => 'Sites Check',
591 ),
592 'deactivated_license_alert' => array(
593 'disable' => $disable,
594 'recipients' => $recipients,
595 'subject' => 'Extension License Deactivation Notification',
596 'heading' => 'Extension License Deactivation Notification',
597 ),
598 );
599
600 $addition_default_fields = apply_filters( 'mainwp_default_emails_fields', array(), $recipients, $type, $field, $general );
601 if ( ! empty( $addition_default_fields ) ) {
602 $default_fields = array_merge( $default_fields, $addition_default_fields );
603 }
604
605 if ( ! empty( $type ) && ! empty( $field ) ) {
606 return isset( $default_fields[ $type ] ) && isset( $default_fields[ $type ][ $field ] ) ? $default_fields[ $type ][ $field ] : '';
607 }
608
609 if ( ! empty( $type ) && empty( $field ) ) {
610 return isset( $default_fields[ $type ] ) ? $default_fields[ $type ] : array();
611 }
612
613 return array();
614 }
615
616 /**
617 * Replace site tokens for settings.
618 *
619 * @param array $options array of fields to find and replace tokens.
620 * @param array $fields fields names to find.
621 * @param object $website The website.
622 *
623 * @return array $options array of fields.
624 *
625 * @uses \MainWP\Dashboard\MainWP_System_Utility::replace_tokens_values()
626 */
627 public static function replace_tokens_for_settings( $options, $fields, $website ) { // phpcs:ignore -- NOSONAR - complex.
628
629 /**
630 * Filter: mainwp_boilerplate_get_tokens
631 *
632 * Enables and filters the Boilerplate extension tokens.
633 *
634 * @param object $website Object containing the child site data.
635 *
636 * @since 4.1
637 */
638 $boilerplate_tokens = apply_filters( 'mainwp_boilerplate_get_tokens', false, $website );
639
640 if ( is_array( $boilerplate_tokens ) ) {
641 foreach ( $fields as $field ) {
642 if ( isset( $options[ $field ] ) ) {
643 $options[ $field ] = MainWP_System_Utility::replace_tokens_values( $options[ $field ], $boilerplate_tokens );
644 }
645 }
646 }
647
648 /**
649 * Filter: mainwp_pro_reports_get_site_tokens
650 *
651 * Enables and filters the Pro Reports extension tokens.
652 *
653 * @param object $website Object containing the child site data.
654 *
655 * @since 4.1
656 */
657 $report_tokens = apply_filters( 'mainwp_pro_reports_get_site_tokens', false, $website->id );
658
659 if ( is_array( $report_tokens ) ) {
660 foreach ( $fields as $field ) {
661 if ( isset( $options[ $field ] ) ) {
662 $options[ $field ] = MainWP_System_Utility::replace_tokens_values( $options[ $field ], $report_tokens );
663 }
664 }
665 }
666
667 /**
668 * Filter: mainwp_client_report_get_site_tokens
669 *
670 * Enables and filters the Client Reports extension tokens.
671 *
672 * @param object $website Object containing the child site data.
673 *
674 * @since 4.1
675 */
676 $client_report_tokens = apply_filters( 'mainwp_client_report_get_site_tokens', false, $website->id );
677 if ( is_array( $client_report_tokens ) ) {
678 foreach ( $fields as $field ) {
679 if ( isset( $options[ $field ] ) ) {
680 $options[ $field ] = MainWP_System_Utility::replace_tokens_values( $options[ $field ], $client_report_tokens );
681 }
682 }
683 }
684
685 return $options;
686 }
687
688 /**
689 * Replace site tokens for content.
690 *
691 * @param string $content content to find and replace tokens.
692 * @param object $website The website.
693 *
694 * @return string $content after replaced tokens.
695 *
696 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_tokens_site_values()
697 * @uses \MainWP\Dashboard\MainWP_System_Utility::replace_tokens_values()
698 */
699 public static function replace_tokens_for_content( $content, $website ) {
700
701 $tokens_values = MainWP_System_Utility::get_tokens_site_values( $website );
702
703 $content = MainWP_System_Utility::replace_tokens_values( $content, $tokens_values );
704
705 // if tokens existed.
706 if ( preg_match( '/\[[^\]]+\]/is', $content, $matches ) ) {
707
708 /** This filter is documented in ../class/class-mainwp-notification-settings.php */
709 $boilerplate_tokens = apply_filters( 'mainwp_boilerplate_get_tokens', false, $website );
710
711 if ( is_array( $boilerplate_tokens ) ) {
712 $content = MainWP_System_Utility::replace_tokens_values( $content, $boilerplate_tokens );
713 }
714
715 /** This filter is documented in ../class/class-mainwp-notification-settings.php */
716 $report_tokens = apply_filters( 'mainwp_pro_reports_get_site_tokens', false, $website->id );
717
718 if ( is_array( $report_tokens ) ) {
719 $content = MainWP_System_Utility::replace_tokens_values( $content, $report_tokens );
720 }
721
722 /** This filter is documented in ../class/class-mainwp-notification-settings.php */
723 $client_report_tokens = apply_filters( 'mainwp_client_report_get_site_tokens', false, $website->id );
724
725 if ( is_array( $client_report_tokens ) ) {
726 $content = MainWP_System_Utility::replace_tokens_values( $content, $client_report_tokens );
727 }
728 }
729
730 return $content;
731 }
732 }
733