Skip to content

Instantly share code, notes, and snippets.

@gaswirth
Last active August 2, 2022 18:18
Show Gist options
  • Save gaswirth/7595f20e7e9b6e0c9fae54e3fb818c18 to your computer and use it in GitHub Desktop.
Save gaswirth/7595f20e7e9b6e0c9fae54e3fb818c18 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: RHD Environment
* Description: Enables automatic updates and SMTP email via PHPMailer.
* Author: Roundhouse Designs
* Author URI: https://roundhouse-designs.com
*
* @package rhdwp
*/
/**
* Set the true visitor IP.
*/
function rhd_set_real_ip() {
$ip_addr = null;
// Check for Cloudflare
if ( ! empty( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
$ips = explode( ',', $_SERVER['HTTP_CF_CONNECTING_IP'] );
$ip_addr = trim( $ips[0] );
}
// Check X-Real-IP header (Working with Traefik...)
elseif ( ! empty( $_SERVER['X_REAL_IP'] ) ) {
$ip_addr = trim( $_SERVER['X_REAL_IP'] );
} elseif ( ! empty( $_SERVER['HTTP_X_REAL_IP'] ) ) {
$ip_addr = trim( $_SERVER['HTTP_X_REAL_IP'] );
}
// Check X-Forwarded-For
elseif ( ! empty( $_SERVER['X_FORWARDED_FOR'] ) ) {
$ips = explode( ',', $_SERVER['X_FORWARDED_FOR'] );
$ip_addr = trim( $ips[0] );
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$ips = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] );
$ip_addr = trim( $ips[0] );
}
$_SERVER['REMOTE_ADDR'] = $ip_addr;
}
// First available hook.
add_action( 'mu_plugin_loaded', 'rhd_set_real_ip' );
/**
* Trims both single and double quotes from strings.
*
* @param string $string The string to filter.
* @return string The filtered string.
*/
function rhd_trim_quotes( string $string ) {
return trim( trim( $string, "'" ), '"' );
}
/**
* Auto-updates.
*/
add_filter( 'allow_minor_auto_core_updates', '__return_true' );
add_filter( 'auto_update_plugin', '__return_true' );
add_filter( 'auto_update_theme', '__return_true' );
/**
* Disable auto-update emails.
*/
add_filter( 'auto_core_update_send_email', '__return_false' );
add_filter( 'auto_plugin_update_send_email', '__return_false' );
add_filter( 'auto_theme_update_send_email', '__return_false' );
/**
* Set the proper FROM address.
*
* @param string $email The FROM address.
* @return string The filtered email address.
*/
function rhd_mail_from( $email ) {
return rhd_trim_quotes( getenv( 'WORDPRESS_SMTP_FROM' ) );
}
add_filter( 'wp_mail_from', 'rhd_mail_from', 10, 1 );
/**
* Sets up the custom PHP mailer to connect MailGun to WP.
*
* @param PHPMailer $phpmailer The PHPMailer object.
* @return void
*/
function rhd_phpmail( $phpmailer ) {
$host = getenv( 'WORDPRESS_SMTP_HOST' );
$username = getenv( 'WORDPRESS_SMTP_USERNAME' );
$password = getenv( 'WORDPRESS_SMTP_PASSWORD' );
$from = rhd_trim_quotes( getenv( 'WORDPRESS_SMTP_FROM' ) );
$from_name = rhd_trim_quotes( getenv( 'WORDPRESS_SMTP_FROM_NAME' ) );
$phpmailer->isSMTP();
// phpcs:disable
$phpmailer->Host = $host;
$phpmailer->SMTPAutoTLS = true;
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 587;
$phpmailer->Username = $username;
$phpmailer->Password = $password;
// Additional settings
$phpmailer->SMTPSecure = 'tls';
$phpmailer->From = $from;
$phpmailer->FromName = $from_name;
// phpcs:enable
}
add_action( 'phpmailer_init', 'rhd_phpmail', 10, 1 );
/**
* Disable Admin Notification of User Password Change.
*
* @see pluggable.php
*/
if ( ! function_exists( 'wp_password_change_notification' ) ) {
function wp_password_change_notification( $user ) {
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment