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

691 lines 27.6 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"><?php echo esc_html( $title ); ?></h3>
256 <div class="sub header"><?php echo esc_html( $email_description ); ?></h3></div>
257 <div class="ui divider"></div>
258 <div class="ui grid field">
259 <label class="six wide column middle aligned"><?php esc_html_e( 'Enable', 'mainwp' ); ?></label>
260 <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">
261 <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"' : ''; ?>/>
262 </div>
263 </div>
264 <div class="ui grid field">
265 <label class="six wide column middle aligned"><?php esc_html_e( 'Recipient(s)', 'mainwp' ); ?></label>
266 <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">
267 <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'] ); ?>"/>
268 </div>
269 </div>
270 <div class="ui grid field">
271 <label class="six wide column middle aligned"><?php esc_html_e( 'Subject', 'mainwp' ); ?></label>
272 <div class="ten wide column" data-tooltip="<?php esc_attr_e( 'Enter the email subject.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
273 <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'] ); ?>"/>
274 </div>
275 </div>
276 <div class="ui grid field">
277 <label class="six wide column middle aligned"><?php esc_html_e( 'Email heading', 'mainwp' ); ?></label>
278 <div class="ten wide column" data-tooltip="<?php esc_attr_e( 'Enter the email heading.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
279 <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'] ); ?>"/>
280 </div>
281 </div>
282 <div class="ui grid field" >
283 <label class="six wide column middle aligned"><?php esc_html_e( 'HTML template', 'mainwp' ); ?></label>
284 <div class="ui ten wide column" data-tooltip="<?php esc_attr_e( 'Manage the email HTML template.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
285 <?php
286 $templ = MainWP_Notification_Template::get_template_name_by_notification_type( $type );
287 $overrided = MainWP_Notification_Template::instance()->is_overrided_template( $type );
288 /**
289 *
290 * Use mainwp_notification_template_copy_message instead.
291 *
292 * @deprecated Since v4.3.
293 */
294 $copy_message = apply_filters( 'minwp_notification_template_copy_message', '', $templ, $type, $overrided );
295 /**
296 *
297 * Filter mainwp_notification_template_copy_message.
298 *
299 * @since v4.3
300 */
301 $copy_message = apply_filters( 'mainwp_notification_template_copy_message', $copy_message, $templ, $type, $overrided );
302 if ( empty( $copy_message ) ) {
303 $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>';
304 }
305 echo $copy_message; // phpcs:ignore WordPress.Security.EscapeOutput
306 ?>
307 </div>
308 </div>
309 <div class="ui grid field" >
310 <label class="six wide column middle aligned"></label>
311 <div class="ui ten wide column" data-tooltip="<?php esc_attr_e( 'Manage the email HTML template.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
312 <?php if ( $overrided ) : ?>
313 <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>
314 <?php else : ?>
315 <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>
316 <?php endif; ?>
317 <?php if ( $overrided ) : ?>
318 <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>
319 <?php else : ?>
320 <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>
321 <?php endif; ?>
322 </div>
323 </div>
324 <div class="ui divider"></div>
325 <a href="admin.php?page=SettingsEmail" class="ui big basic green button"><?php esc_html_e( 'Back', 'mainwp' ); ?></a>
326 <input type="submit" name="submit" id="submit" class="ui green big button right floated" value="<?php esc_attr_e( 'Save Settings', 'mainwp' ); ?>"/>
327 <div style="clear:both"></div>
328 </form>
329 </div>
330 </div>
331 <?php MainWP_Manage_Sites_View::render_edit_template( $type ); ?>
332 <?php
333 }
334
335
336 /**
337 * Get email settings description.
338 *
339 * @param string $type Email notification type.
340 *
341 * @return string $email_description Email settings Description.
342 */
343 public static function get_settings_desc( $type ) {
344 $email_description = '';
345 if ( 'daily_digest' === $type ) {
346 $email_description = esc_html__( 'Daily notification about available updates and disconnected sites.', 'mainwp' );
347 } elseif ( 'uptime' === $type ) {
348 $email_description = esc_html__( 'Alert if any of your websites is down.', 'mainwp' );
349 } elseif ( 'site_health' === $type ) {
350 $email_description = esc_html__( 'Alert if any of your websites site health goes under the threshold.', 'mainwp' );
351 } elseif ( 'http_check' === $type ) {
352 $email_description = esc_html__( 'Alert if any of your websites return unexpected HTTP status after running updates.', 'mainwp' );
353 } elseif ( 'deactivated_license_alert' === $type ) {
354 $email_description = esc_html__( 'Receive a notification when an extension\'s license is deactivated.', 'mainwp' );
355 }
356
357 $addition_desc = apply_filters( 'mainwp_notification_type_desc', '', $type );
358 if ( ! empty( $addition_desc ) ) {
359 $email_description = $addition_desc;
360 }
361
362 return $email_description;
363 }
364
365 /**
366 * Render the notification after the update process.
367 *
368 * @param int $updated Update code (copied, saved or deleted).
369 */
370 public static function render_update_template_message( $updated ) {
371 ?>
372 <?php if ( 1 === (int) $updated ) : ?>
373 <div class="ui message green"><i class="close icon"></i> <?php esc_html_e( 'Custom email template deleted successfully.', 'mainwp' ); ?></div>
374 <?php endif; ?>
375 <?php if ( 2 === (int) $updated ) : ?>
376 <div class="ui message green"><i class="close icon"></i> <?php esc_html_e( 'Email template copied successfully.', 'mainwp' ); ?></div>
377 <?php endif; ?>
378 <?php if ( 3 === (int) $updated ) : ?>
379 <div class="ui message green"><i class="close icon"></i> <?php esc_html_e( 'Email template updated successfully.', 'mainwp' ); ?></div>
380 <?php endif; ?>
381 <?php
382 }
383
384 /**
385 * Get email notification types.
386 *
387 * @param string $type Email notification type.
388 *
389 * @return mixed Notification types.
390 */
391 public static function get_notification_types( $type = '' ) {
392 $types = array(
393 'daily_digest' => esc_html__( 'Daily Digest Email', 'mainwp' ),
394 'uptime' => esc_html__( 'Basic Uptime Monitoring Email', 'mainwp' ),
395 'site_health' => esc_html__( 'Site Health Monitoring Email', 'mainwp' ),
396 'deactivated_license_alert' => esc_html__( 'Extension License Deactivation Notification Email', 'mainwp' ),
397 );
398
399 $enable_http_check = get_option( 'mainwp_check_http_response', 0 );
400
401 if ( $enable_http_check ) {
402 $types['http_check'] = esc_html__( 'After Updates HTTP Check Email', 'mainwp' );
403 }
404
405 $addition_types = apply_filters( 'mainwp_notification_types', array(), $type );
406 if ( ! empty( $addition_types ) ) {
407 $types = array_merge( $types, $addition_types );
408 }
409
410 if ( empty( $type ) ) {
411 return $types;
412 } else {
413 return ( isset( $types[ $type ] ) ) ? $types[ $type ] : false;
414 }
415 }
416
417 /**
418 * Get general email notifications settings.
419 *
420 * @param string $type Email type.
421 *
422 * @see function get_notification_types()
423 *
424 * @return array An array containing the email settings.
425 */
426 public static function get_general_email_settings( $type ) {
427 $settings = get_option( 'mainwp_settings_notification_emails', array() );
428 if ( ! is_array( $settings ) ) {
429 $settings = array();
430 }
431
432 $options = isset( $settings[ $type ] ) ? $settings[ $type ] : array();
433 $default = self::get_default_emails_fields( $type, '', true );
434 return array_merge( $default, $options );
435 }
436
437 /**
438 * Get general notification email.
439 *
440 * @return string|empty recipients.
441 */
442 public static function get_general_email() {
443 $gen_email_settings = self::get_general_email_settings( 'daily_digest' );
444 return isset( $gen_email_settings['recipients'] ) ? $gen_email_settings['recipients'] : '';
445 }
446
447 /**
448 * Update general notification email.
449 *
450 * @param string $emails Emails.
451 *
452 * @return void
453 */
454 public static function update_general_email( $emails ) {
455 $emails_settings = get_option( 'mainwp_settings_notification_emails' );
456 if ( ! is_array( $emails_settings ) ) {
457 $emails_settings = array();
458 }
459 $type = 'daily_digest';
460 $update_settings = isset( $emails_settings[ $type ] ) ? $emails_settings[ $type ] : array();
461 $update_settings['recipients'] = $emails;
462 $emails_settings[ $type ] = $update_settings;
463 MainWP_Utility::update_option( 'mainwp_settings_notification_emails', $emails_settings );
464 }
465
466 /**
467 * Get email settings for a single site.
468 *
469 * @param string $type Email type.
470 * @param object $website Object containing the child site information.
471 *
472 * @return array An array containing the email settings.
473 *
474 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_tokens_site_values()
475 * @uses \MainWP\Dashboard\MainWP_System_Utility::replace_tokens_values()
476 */
477 public static function get_site_email_settings( $type, $website ) {
478 if ( empty( $website ) || ! property_exists( $website, 'settings_notification_emails' ) ) {
479 return array( 'disable' => 1 );
480 }
481 $settings = array();
482 if ( ! empty( $website->settings_notification_emails ) ) {
483 $settings = json_decode( $website->settings_notification_emails, true );
484 }
485
486 if ( ! is_array( $settings ) ) {
487 $settings = array();
488 }
489
490 $options = isset( $settings[ $type ] ) ? $settings[ $type ] : array();
491 $default = self::get_default_emails_fields( $type );
492 $options = array_merge( $default, $options );
493
494 if ( preg_match( '/\[[^\]]+\]/is', $options['subject'] . $options['heading'], $matches ) ) {
495 $tokens_values = MainWP_System_Utility::get_tokens_site_values( $website );
496 $options['subject'] = MainWP_System_Utility::replace_tokens_values( $options['subject'], $tokens_values );
497 $options['heading'] = MainWP_System_Utility::replace_tokens_values( $options['heading'], $tokens_values );
498 }
499
500 if ( preg_match( '/\[[^\]]+\]/is', $options['recipients'] . $options['subject'] . $options['heading'], $matches ) ) {
501 // support boilerplate and reports tokens.
502 $fields = array( 'recipients', 'subject', 'heading' );
503 $options = self::replace_tokens_for_settings( $options, $fields, $website );
504 }
505
506 return $options;
507 }
508
509 /**
510 * Get default email notifications values.
511 *
512 * @param string $type Email type.
513 * @param string $field Field name.
514 * @param bool $general General or individual site settings.
515 *
516 * @return array|string Email field settings value.
517 *
518 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_notification_email()
519 */
520 public static function get_default_emails_fields( $type, $field = '', $general = false ) {
521
522 $recipients = MainWP_System_Utility::get_notification_email();
523 $disable = $general ? 0 : 1;
524
525 $default_fields = array(
526 'daily_digest' => array(
527 'disable' => $disable,
528 'recipients' => $recipients,
529 'subject' => $general ? 'Daily Digest' : '[site.name] Daily Digest',
530 'heading' => $general ? 'Daily Digest' : '[site.name] Daily Digest',
531 ),
532 'uptime' => array(
533 'disable' => $disable,
534 'recipients' => $recipients,
535 'subject' => $general ? 'Uptime Monitoring' : '[site.name] Uptime Monitoring',
536 'heading' => $general ? 'Uptime Monitoring' : '[site.name] Uptime Monitoring',
537 ),
538 'site_health' => array(
539 'disable' => $disable,
540 'recipients' => $recipients,
541 'subject' => $general ? 'Site Health Monitoring' : '[site.name] Site Health Monitoring',
542 'heading' => $general ? 'Site Health Monitoring' : '[site.name] Site Health Monitoring',
543 ),
544 'http_check' => array(
545 'disable' => $disable,
546 'recipients' => $recipients,
547 'subject' => 'HTTP response check',
548 'heading' => 'Sites Check',
549 ),
550 'deactivated_license_alert' => array(
551 'disable' => $disable,
552 'recipients' => $recipients,
553 'subject' => 'Extension License Deactivation Notification',
554 'heading' => 'Extension License Deactivation Notification',
555 ),
556 );
557
558 $addition_default_fields = apply_filters( 'mainwp_default_emails_fields', array(), $recipients, $type, $field, $general );
559 if ( ! empty( $addition_default_fields ) ) {
560 $default_fields = array_merge( $default_fields, $addition_default_fields );
561 }
562
563 if ( ! empty( $type ) && ! empty( $field ) ) {
564 return isset( $default_fields[ $type ] ) && isset( $default_fields[ $type ][ $field ] ) ? $default_fields[ $type ][ $field ] : '';
565 }
566
567 if ( ! empty( $type ) && empty( $field ) ) {
568 return isset( $default_fields[ $type ] ) ? $default_fields[ $type ] : array();
569 }
570
571 return array();
572 }
573
574 /**
575 * Replace site tokens for settings.
576 *
577 * @param array $options array of fields to find and replace tokens.
578 * @param array $fields fields names to find.
579 * @param object $website The website.
580 *
581 * @return array $options array of fields.
582 *
583 * @uses \MainWP\Dashboard\MainWP_System_Utility::replace_tokens_values()
584 */
585 public static function replace_tokens_for_settings( $options, $fields, $website ) {
586
587 /**
588 * Filter: mainwp_boilerplate_get_tokens
589 *
590 * Enables and filters the Boilerplate extension tokens.
591 *
592 * @param object $website Object containing the child site data.
593 *
594 * @since 4.1
595 */
596 $boilerplate_tokens = apply_filters( 'mainwp_boilerplate_get_tokens', false, $website );
597
598 if ( is_array( $boilerplate_tokens ) ) {
599 foreach ( $fields as $field ) {
600 if ( isset( $options[ $field ] ) ) {
601 $options[ $field ] = MainWP_System_Utility::replace_tokens_values( $options[ $field ], $boilerplate_tokens );
602 }
603 }
604 }
605
606 /**
607 * Filter: mainwp_pro_reports_get_site_tokens
608 *
609 * Enables and filters the Pro Reports extension tokens.
610 *
611 * @param object $website Object containing the child site data.
612 *
613 * @since 4.1
614 */
615 $report_tokens = apply_filters( 'mainwp_pro_reports_get_site_tokens', false, $website->id );
616
617 if ( is_array( $report_tokens ) ) {
618 foreach ( $fields as $field ) {
619 if ( isset( $options[ $field ] ) ) {
620 $options[ $field ] = MainWP_System_Utility::replace_tokens_values( $options[ $field ], $report_tokens );
621 }
622 }
623 }
624
625 /**
626 * Filter: mainwp_client_report_get_site_tokens
627 *
628 * Enables and filters the Client Reports extension tokens.
629 *
630 * @param object $website Object containing the child site data.
631 *
632 * @since 4.1
633 */
634 $client_report_tokens = apply_filters( 'mainwp_client_report_get_site_tokens', false, $website->id );
635 if ( is_array( $client_report_tokens ) ) {
636 foreach ( $fields as $field ) {
637 if ( isset( $options[ $field ] ) ) {
638 $options[ $field ] = MainWP_System_Utility::replace_tokens_values( $options[ $field ], $client_report_tokens );
639 }
640 }
641 }
642
643 return $options;
644 }
645
646 /**
647 * Replace site tokens for content.
648 *
649 * @param string $content content to find and replace tokens.
650 * @param object $website The website.
651 *
652 * @return string $content after replaced tokens.
653 *
654 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_tokens_site_values()
655 * @uses \MainWP\Dashboard\MainWP_System_Utility::replace_tokens_values()
656 */
657 public static function replace_tokens_for_content( $content, $website ) {
658
659 $tokens_values = MainWP_System_Utility::get_tokens_site_values( $website );
660
661 $content = MainWP_System_Utility::replace_tokens_values( $content, $tokens_values );
662
663 // if tokens existed.
664 if ( preg_match( '/\[[^\]]+\]/is', $content, $matches ) ) {
665
666 /** This filter is documented in ../class/class-mainwp-notification-settings.php */
667 $boilerplate_tokens = apply_filters( 'mainwp_boilerplate_get_tokens', false, $website );
668
669 if ( is_array( $boilerplate_tokens ) ) {
670 $content = MainWP_System_Utility::replace_tokens_values( $content, $boilerplate_tokens );
671 }
672
673 /** This filter is documented in ../class/class-mainwp-notification-settings.php */
674 $report_tokens = apply_filters( 'mainwp_pro_reports_get_site_tokens', false, $website->id );
675
676 if ( is_array( $report_tokens ) ) {
677 $content = MainWP_System_Utility::replace_tokens_values( $content, $report_tokens );
678 }
679
680 /** This filter is documented in ../class/class-mainwp-notification-settings.php */
681 $client_report_tokens = apply_filters( 'mainwp_client_report_get_site_tokens', false, $website->id );
682
683 if ( is_array( $client_report_tokens ) ) {
684 $content = MainWP_System_Utility::replace_tokens_values( $content, $client_report_tokens );
685 }
686 }
687
688 return $content;
689 }
690 }
691