Last active
September 29, 2022 20:41
-
-
Save wpmudev-sls/09156154e4e93aefe374e25651d04393 to your computer and use it in GitHub Desktop.
[Defender Pro] - Whitelabel ALL Defender emails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Defender Pro] - White Label ALL Defender emails | |
* Description: Allows the modification of almost all email fields/parameters via filters or an additional GUI at Settings > General. | |
* Author: Anderson Salas @ WPMUDEV | |
* Task: SLS-3842 | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
// Available filters: | |
// | |
// apply_filters( 'wdf_email_whitelabel_title', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_content_body', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_company_name', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_company_logo', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_company_logo_2x', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_cheers', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_follow_us', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_icon_facebook_url', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_icon_instagram_url', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_icon_twitter_url', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_facebook_url', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_instagram_url', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_twitter_url', function( $value, $default_value ) { ... }, 10, 2 ); | |
// apply_filters( 'wdf_email_whitelabel_company_address', function( $value, $default_value ) { ... }, 10, 2 ); | |
add_action( 'plugins_loaded', function() { | |
if ( ! defined( 'DEFENDER_VERSION' ) || class_exists( 'WPMU_Defender_Whitelabel_Emails' ) ) { | |
return; | |
} | |
class WPMU_Defender_Whitelabel_Emails | |
{ | |
// Set this to TRUE to add | |
const USE_VISUAL_EDITOR = TRUE; | |
private static $instance; | |
private $stored_settings; | |
public static function get_instance() { | |
if ( null === self::$instance ) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
public function __construct() { | |
add_action( 'admin_init', array( $this, 'download_base_template' ) ); | |
add_action( 'admin_footer', array( $this, 'render_email_whitelabel_settings' ) ); | |
add_filter( 'wp_mail', array( $this, 'replace_defender_emails' ) ); | |
if ( $this->can_edit_branding() ) { | |
add_action( 'wp_ajax_wp_defender/v1/hub/', array( $this, 'save_email_whitelabel_settings' ), 9 ); | |
} | |
} | |
private function is_defender_settings_page() { | |
return defender_current_page() && 'wdf-setting' === $_GET['page']; | |
} | |
private function can_edit_branding() { | |
return current_user_can( 'manage_options' ) && self::USE_VISUAL_EDITOR; | |
} | |
private function get_template_override_filename() { | |
return substr( md5( AUTH_SALT ), 0, 8 ) . '_defender_email_template.html'; | |
} | |
private function template_override_exists( &$path = null ) { | |
$upload_dir = wp_upload_dir()['basedir']; | |
if ( file_exists( $upload_dir . '/' . $this->get_template_override_filename() ) ) { | |
$path = $upload_dir . '/' . $this->get_template_override_filename() ; | |
return true; | |
} | |
return false; | |
} | |
private function get_default_view_as_template() { | |
$template = file_get_contents( WP_DEFENDER_DIR . '/src/view/email/index.php' ); | |
foreach( $this->get_default_view_placeholders() as $placeholder => $target ) { | |
$template = str_ireplace( $target, '{{ ' . $placeholder . ' }}', $template ); | |
} | |
return $template; | |
} | |
private function get_new_email_template() { | |
if ( $this->template_override_exists( $template_path ) ) { | |
return file_get_contents( $template_path ); | |
} | |
return $this->get_default_view_as_template(); | |
} | |
private function get_default_view_placeholders() { | |
return array ( | |
/* 0, 2, 3 */ 'title' => array( '<?php echo $title; ?>', '<?php echo esc_attr( $title ); ?>' ), | |
/* 1 */ 'company_logo' => '<?php echo defender_asset_url( \'/assets/email-images/logo.png\' ); ?>', | |
/* 4 */ 'content_body' => '<?php echo $content_body; ?>', | |
/* 5 */ 'cheers' => '<?php esc_html_e( "Cheers,", \'wpdef\' ) ?>', | |
/* 6 */ 'company_name' => '<?php esc_html_e( "The WPMU DEV Team.", \'wpdef\' ) ?>', | |
/* 7 */ 'company_logo_2x' => '<?php echo defender_asset_url( \'/assets/email-images/wpmudev-logo@2x.png\' ); ?>', | |
/* 8 */ 'follow_us' => '<?php _e( \'Follow us\', \'wpdef\' ); ?>', | |
/* 9 */ 'icon_facebook_url' => '<?php echo defender_asset_url( \'/assets/email-images/icon-fb.png\' ); ?>', | |
/* 10 */ 'icon_instagram_url' => '<?php echo defender_asset_url( \'/assets/email-images/icon-instagram.png\' ); ?>', | |
/* 11 */ 'icon_twitter_url' => '<?php echo defender_asset_url( \'/assets/email-images/icon-twitter.png\' ); ?>', | |
/* 12 */ 'facebook_url' => 'https://www.facebook.com/wpmudev', | |
/* 13 */ 'instagram_url' => 'https://www.instagram.com/wpmu_dev/', | |
/* 14 */ 'twitter_url' => 'https://twitter.com/wpmudev', | |
/* N/A */ 'company_address' => 'INCSUB PO BOX 163, ALBERT PARK, VICTORIA.3206 AUSTRALIA' | |
); | |
} | |
public function download_base_template() { | |
if ( ! $this->can_edit_branding() || ! $this->is_defender_settings_page() || empty( $_GET['download_email_template'] ) ) { | |
return; | |
} | |
if ( ! wp_verify_nonce( $_GET['download_email_template'], 'wdf-whitelabel-download-template' ) ) { | |
return; | |
} | |
header( "Content-type: text/html", true, 200 ); | |
header( "Content-Disposition: attachment; filename=" . $this->get_template_override_filename() ); | |
header( "Pragma: no-cache" ); | |
header( "Expires: 0" ); | |
die( $this->get_default_view_as_template() ); | |
} | |
private function get_setting( $name, $default_value = null ) { | |
if ( null === $this->stored_settings ) { | |
$this->stored_settings = ! is_multisite() | |
? get_option( 'wdf_email_whitelabel_settings' ) | |
: get_site_option( 'wdf_email_whitelabel_settings' ); | |
} | |
$value = isset( $this->stored_settings[ $name ] ) ? $this->stored_settings[ $name ] : $default_value; | |
return apply_filters( 'wdf_email_whitelabel_' . $name, $value, $default_value ); | |
} | |
public function save_email_whitelabel_settings() { | |
if ( ! $this->can_edit_branding() ) { | |
return; | |
} | |
if ( empty( $_POST['wdf_branding_nonce'] ) || ! wp_verify_nonce( $_POST['wdf_branding_nonce'], 'wdf_branding' ) ) { | |
return; | |
} | |
$settings = array(); | |
$setting_names = array( 'company_name', 'company_address', 'company_logo', 'company_logo_2x', 'facebook_url', 'twitter_url', 'instagram_url' ); | |
foreach( $setting_names as $setting_name ) { | |
$settings[ $setting_name ] = ! empty( $_POST[ $setting_name ] ) ? sanitize_text_field( $_POST[ $setting_name ] ) : null; | |
} | |
if ( ! is_multisite() ) { | |
update_option( 'wdf_email_whitelabel_settings', $settings ); | |
} else { | |
update_site_option( 'wdf_email_whitelabel_settings', $settings ); | |
} | |
} | |
public function replace_defender_emails( $mail ) { | |
if ( empty( $mail['message'] ) || false === strpos( $mail['message'], defender_asset_url( '/assets/email-images/logo.png' ) ) ) { | |
return $mail; | |
} | |
$view = file_get_contents( WP_DEFENDER_DIR . '/src/view/email/index.php' ); | |
$regex = '/<\?php.*?\?>/m'; | |
$result = $mail['message']; | |
$fragments = explode( '%|%', preg_replace( $regex, '%|%', $view ) ); | |
foreach ( $fragments as $i => $fragment ) { | |
$result = trim( str_ireplace( trim( $fragment ), '%|%', trim( $result ) ), '%|%' ) ; | |
} | |
$result = explode( '%|%', $result ); | |
$vars = array( | |
'title' => apply_filters( 'wdf_email_whitelabel_title', trim( $result[0] ), null ), | |
'company_logo' => $this->get_setting( 'company_logo', trim( $result[1] ) ), | |
'content_body' => apply_filters( 'wdf_email_whitelabel_content_body', trim( $result[4] ), null ), | |
'cheers' => apply_filters( 'wdf_email_whitelabel_cheers', trim( $result[5] ), null ), | |
'company_name' => $this->get_setting( 'company_name', trim( $result[6] ) ), | |
'company_logo_2x' => $this->get_setting( 'company_logo_2x', trim( $result[7] ) ), | |
'follow_us' => apply_filters( 'wdf_email_whitelabel_follow_us', trim( $result[8] ), null ), | |
'icon_facebook_url' => apply_filters( 'wdf_email_whitelabel_icon_facebook_url', trim( $result[9] ), null ), | |
'icon_instagram_url' => apply_filters( 'wdf_email_whitelabel_icon_instagram_url', trim( $result[10] ), null ), | |
'icon_twitter_url' => apply_filters( 'wdf_email_whitelabel_icon_twitter_url', trim( $result[11] ), null ), | |
'facebook_url' => $this->get_setting( 'facebook_url', 'https://www.facebook.com/wpmudev' ), | |
'instagram_url' => $this->get_setting( 'instagram_url', 'https://www.instagram.com/wpmu_dev' ), | |
'twitter_url' => $this->get_setting( 'twitter_url', 'https://twitter.com/wpmudev' ), | |
'company_address' => $this->get_setting( 'company_address', 'INCSUB PO BOX 163, ALBERT PARK, VICTORIA.3206 AUSTRALIA' ), | |
); | |
$new_view = $this->get_new_email_template(); | |
foreach( $vars as $placeholder => $value ) { | |
if ( empty( $value ) ) { | |
continue; | |
} | |
$new_view = str_ireplace( '{{ ' . $placeholder . ' }}', $value, $new_view ); | |
} | |
$mail['message'] = $new_view; | |
return $mail; | |
} | |
public function render_email_whitelabel_settings() { | |
if ( ! $this->is_defender_settings_page() || ! $this->can_edit_branding() ) { | |
return; | |
} | |
$upload_dir = wp_upload_dir()['basedir']; | |
$file_exists = $this->template_override_exists(); | |
?> | |
<template id="whitelabel-email-template-row"> | |
<div class="sui-box-settings-row"> | |
<div class="sui-box-settings-col-1"> | |
<span class="sui-settings-label">Email White Label <span class="sui-tag sui-tag-blue sui-tag-sm" style="vertical-align:middle">Unofficial</span></span> | |
<span class="sui-description">Customize the emails sent by Defender with your own brand.</span> | |
</div> | |
<div class="sui-box-settings-col-2"> | |
<fieldset id="wdf_branding"> | |
<label style="font-weight:bold">Basic branding</label> | |
<div style="margin-bottom:30px"> | |
<input type="hidden" name="wdf_branding_nonce" value="<?php echo wp_create_nonce( 'wdf_branding' ); ?>" /> | |
<div class="sui-form-field"> | |
<label class="sui-label">Company name</label> | |
<input class="sui-form-control wdf-branding" name="company_name" value="<?php echo $this->get_setting( 'company_name' ); ?>" placeholder="The WPMU DEV Team." /> | |
</div> | |
<div class="sui-form-field"> | |
<label class="sui-label">Company address</label> | |
<input class="sui-form-control wdf-branding" name="company_address" value="<?php echo $this->get_setting( 'company_address' ); ?>" placeholder="INCSUB PO BOX 163, ALBERT PARK, VICTORIA.3206 AUSTRALIA"/> | |
</div> | |
<div class="sui-form-field"> | |
<label class="sui-label">Header logo (27x30)</label> | |
<input class="sui-form-control wdf-branding" name="company_logo" value="<?php echo $this->get_setting( 'company_logo' ); ?>" placeholder="<?php echo defender_asset_url( '/assets/email-images/logo.png' ); ?>"> | |
</div> | |
<div class="sui-form-field"> | |
<label class="sui-label">Footer logo (170x30)</label> | |
<input class="sui-form-control wdf-branding" name="company_logo_2x" value="<?php echo $this->get_setting( 'company_logo_2x' ); ?>" placeholder="<?php echo defender_asset_url( '/assets/email-images/wpmudev-logo@2x.png' ); ?>"> | |
</div> | |
<div class="sui-form-field"> | |
<label class="sui-label">Facebook page URL</label> | |
<input class="sui-form-control wdf-branding" name="facebook_url" value="<?php echo $this->get_setting( 'facebook_url' ); ?>" placeholder="https://www.facebook.com/wpmudev"> | |
</div> | |
<div class="sui-form-field"> | |
<label class="sui-label">Instagram profile URL</label> | |
<input class="sui-form-control wdf-branding" name="instagram_url" value="<?php echo $this->get_setting( 'instagram_url' ); ?>" placeholder="https://www.instagram.com/wpmu_dev/"> | |
</div> | |
<div class="sui-form-field"> | |
<label class="sui-label">Twitter profile URL</label> | |
<input class="sui-form-control wdf-branding" name="twitter_url" value="<?php echo $this->get_setting( 'twitter_url' ); ?>" placeholder="https://twitter.com/wpmudev"> | |
</div> | |
</div> | |
</fieldset> | |
<label style="font-weight:bold">Template override (Advanced)</label> | |
<p> | |
<a href="?page=wdf-setting&download_email_template=<?php echo wp_create_nonce( 'wdf-whitelabel-download-template' );?>" class="sui-button sui-button-ghost sui-button-compound"> | |
<span class="sui-button-text-default"> | |
<i aria-hidden="true" class="sui-icon-download"></i> | |
Download template | |
</span> | |
</a> | |
Current status: | |
<span class="sui-status-dot <?php echo $file_exists ? 'sui-published' : ''; ?>"><span aria-hidden="true"></span></span> | |
<strong style="color:<?php echo $file_exists ? '#17a8e3' : '#a7a7a7'; ?>"><?php echo $file_exists ? 'Active' : 'Inactive'; ?></strong> | |
</p> | |
<p class="sui-description" style="margin-bottom:15px">To override the default email template, please download it by clicking in the <strong>Download template</strong> button, then edit it with your favorite code editor and finally upload the file to the following path via FTP:</p> | |
<p class="sui-description" style="margin-bottom:15px"><code><?php echo $upload_dir; ?>/<?php echo $this->get_template_override_filename() ?></code></p> | |
<p class="sui-description"><strong>Important:</strong> the file MUST be named <strong><?php echo $this->get_template_override_filename(); ?></strong>, otherwise it will not be detected. To disable the template override, just delete that file from the server.</p> | |
</div> | |
</div> | |
</template> | |
<script> | |
( function( $ ) { | |
setTimeout( function() { | |
$( $( '.sui-row-with-sidenav > div:nth-child(2) .sui-box-body' )[0] ).append( | |
$( '#whitelabel-email-template-row' ).html() | |
); | |
XMLHttpRequest.prototype.reallySend = XMLHttpRequest.prototype.send; | |
XMLHttpRequest.prototype.send = function(body) { | |
if ( body instanceof FormData ) { | |
if ( 'string' === typeof body.get('data') && -1 !== (body.get('data')).indexOf('uninstall_settings') ) { | |
jQuery.each( jQuery('fieldset#wdf_branding input').serializeArray(), function( index, field ) { | |
body.append( field.name, field.value ); | |
}); | |
} | |
} | |
this.reallySend( body ); | |
}; | |
}, 2000 ); | |
})( window.jQuery ) | |
</script> | |
<?php | |
} | |
} | |
WPMU_Defender_Whitelabel_Emails::get_instance(); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment