PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.3
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.3
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.0.3, at class/class-mainwp-notification-settings.php

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