Plugin Directory

Changeset 2696275


Ignore:
Timestamp:
03/18/2022 10:01:41 PM (4 years ago)
Author:
JPry
Message:

Tagging version 1.2.0

Location:
redirect-emails-on-staging
Files:
44 added
2 deleted
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • redirect-emails-on-staging/tags/1.2.0/README.txt

    r871476 r2696275  
    33Tags: email, wpengine
    44Requires at least: 3.5.2
    5 Tested up to: 3.8.1
    6 Stable Tag: 1.1
     5Tested up to: 5.9
     6Requires PHP: 5.3.2
     7Stable tag: 1.2.0
    78License: GPL2
    89
     
    4344== Screenshots ==
    4445
    45 (none yet)
     46(Not applicable to this plugin)
    4647
    4748== Changelog ==
    4849
    49 = 1.1 =
     50= 1.2.0 - 2022-03-18 =
     51* Add - Native wp_get_environment_type() support.
     52* Add - Support for checking with Jetpack for staging.
     53* Add - Support for modifying CC and BCC lists in emails.
     54* Dev - Add separate changelog file.
     55* Dev - add automated build process.
     56* Tweak - WP 5.9 compatibility.
     57
     58= 1.1.0 =
    5059* Under-the-hood change: use a class for the main plugin functionality
    5160* Check PHP version with an activation hook
    5261* Update the "Tested up to" tag
    5362
    54 = 1.0 =
     63= 1.0.0 =
    5564* Created the plugin
    5665
  • redirect-emails-on-staging/tags/1.2.0/redirect-emails-on-staging.php

    r871476 r2696275  
    11<?php
    22/**
    3  * Plugin Name: Redirect Emails on Staging
    4  * Plugin URI: http://wordpress.org/plugins/redirect-emails-on-staging/
    5  * Description: (For WP Engine only) On the Staging site, redirect all emails to the site admin. This is useful in making sure that the staging site doesn't send out confusing emails to your users.
    6  * Version: 1.1
    7  * Author: Jeremy Pry
    8  * Author URI: http://jeremypry.com/
    9  * License: GPL2
     3 * Plugin Name:       Redirect Emails on Staging
     4 * Plugin URI:        http://wordpress.org/plugins/redirect-emails-on-staging/
     5 * Description:       On a Staging site, redirect all emails to the site admin. This is useful in making sure that the staging site doesn't send out confusing emails to your users.
     6 * Version:           1.2.0
     7 * Author:            Jeremy Pry
     8 * Author URI:        https://jeremypry.com/
     9 * License:           GPL2
     10 * Requires at least: 5.5.0
     11 * Tested up to: 5.9
     12 * Requires PHP:      5.3.2
     13 * Text Domain:       redirect-emails-on-staging
    1014 */
     15
     16use Automattic\Jetpack\Status;
    1117
    1218// Prevent direct access to this file
     
    1521}
    1622
    17 // Activation check
    18 register_activation_hook( plugin_basename( __FILE__ ), 'jpry_res_activation_check' );
     23define( 'JPRY_REDIRECT_EMAILS_ON_STAGING_VERSION', '1.2.0' ); // WRCS: DEFINED_VERSION.
    1924
    20 /**
    21  * Check to ensure the plugin is able to run
    22  *
    23  * We're specifically looking to make sure there is a PHP version of 5.3.2 or greater
    24  *
    25  * @since 1.1
    26  */
    27 function jpry_res_activation_check() {
    28     $min_php_version = '5.3.2';
    29     $installed_php_version = phpversion();
     25add_filter(
     26    'wp_mail',
     27    function( $args ) {
     28        // Jetpack is the most comprehensive, so use that if available.
     29        if ( class_exists( Status::class ) && method_exists( Status::class, 'is_staging_site' ) ) {
     30            $is_staging = ( new Status() )->is_staging_site();
     31        } else {
     32            // Check WP environment.
     33            $wp_staging = function_exists( 'wp_get_environment_type' ) && 'staging' === wp_get_environment_type();
    3034
    31     if ( version_compare( $min_php_version, $installed_php_version, '>' ) ) {
    32         deactivate_plugins( plugin_basename( __FILE__ ) );
    33         wp_die( sprintf( "This plugin requires a minimum PHP version of %s. You only have version %s installed. Please upgrade PHP to use this plugin.", $min_php_version, $installed_php_version ) );
    34     }
    35 }
     35            // Check on WP Engine hosting (the original purpose of this plugin).
     36            $wpe_staging = function_exists( 'is_wpe_snapshot' ) && is_wpe_snapshot();
    3637
    37 // Some constants
    38 define( 'RES_FILE', __FILE__ );
    39 define( 'RES_DIR', dirname( RES_FILE ) );
     38            $is_staging = $wp_staging || $wpe_staging;
     39        }
    4040
    41 // Pull in the class files
    42 require_once( RES_DIR . "/classes/class-jpry_singleton.php" );
    43 require_once( RES_DIR . "/classes/class-jpry_redirect_staging_email.php" );
     41        // Bail if we're not in a staging situation.
     42        if ( ! $is_staging ) {
     43            return $args;
     44        }
    4445
    45 // Instantiate the class
    46 JPry_Redirect_Staging_Email::get_instance();
     46        /**
     47         * Filter jpry_redirect_staging_emails_email.
     48         *
     49         * @since 1.2.0
     50         *
     51         * @var string $email The email address where all emails generated by the site should be sent.
     52         */
     53        $email = apply_filters( 'jpry_redirect_staging_emails_email', get_site_option( 'admin_email' ) );
     54
     55        // If the email is already to the admin, then no need to modify anything.
     56        if ( $email === $args['to'] ) {
     57            return $args;
     58        }
     59
     60        // We've established that we need to modify the email arguments, so let's do so.
     61        $args['message'] = sprintf(
     62            /* translators: 1: original email address, 2: the original message */
     63            esc_html__( "Originally sent to: %1\$s\n\n%2\$s", 'redirect-emails-on-staging' ),
     64            $args['to'],
     65            $args['message']
     66        );
     67
     68        $args['subject'] = sprintf(
     69            /* translators: %s is the original email subject */
     70            esc_html__( 'REDIRECTED EMAIL | %s', 'redirect-emails-on-staging' ),
     71            $args['subject']
     72        );
     73
     74        $args['to'] = $email;
     75
     76        // Try to handle CC and BCC headers. If there aren't any headers, bail early.
     77        if ( empty( $args['headers'] ) ) {
     78            return $args;
     79        }
     80
     81        $ccs = $bccs = [];
     82
     83        $headers = (array) $args['headers'];
     84        foreach ( $headers as $index => $header ) {
     85            if ( false === strpos( $header, ':' ) ) {
     86                continue;
     87            }
     88
     89            list( $name, $content ) = explode( ':', trim( $header ), 2 );
     90
     91            $name = trim( $name );
     92
     93            switch ( strtolower( $name ) ) {
     94                case 'cc':
     95                    $ccs = array_merge( $ccs, explode( ',', $content ) );
     96                    break;
     97
     98                case 'bcc':
     99                    $bccs = array_merge( $bccs, explode( ',', $content ) );
     100                    break;
     101
     102                default:
     103                    continue 2;
     104            }
     105
     106            // If we got this far, remove the header from the array.
     107            unset( $headers[ $index ] );
     108        }
     109
     110        // Update the headers array.
     111        $args['headers'] = $headers;
     112
     113        // Add a note to the message body about what was removed.
     114        $additional_message = '';
     115        if ( ! empty( $ccs ) ) {
     116            $additional_message .= sprintf(
     117                /* translators: %s is the comma-separated list of any CC emails removed */
     118                esc_html__( 'CCs removed: %s', 'redirect-emails-on-staging' ),
     119                join( ', ', $ccs )
     120            ) . PHP_EOL;
     121        }
     122
     123        if ( ! empty( $bccs ) ) {
     124            $additional_message .= sprintf(
     125                /* translators: %s is the comma-separated list of any BCC emails removed */
     126                esc_html__( 'BCCs removed: %s', 'redirect-emails-on-staging' ),
     127                join( ', ', $bccs )
     128            ) . PHP_EOL;
     129        }
     130
     131        $args['message'] = $additional_message . $args['message'];
     132
     133        return $args;
     134    },
     135    99999
     136);
  • redirect-emails-on-staging/trunk/README.txt

    r871476 r2696275  
    33Tags: email, wpengine
    44Requires at least: 3.5.2
    5 Tested up to: 3.8.1
    6 Stable Tag: 1.1
     5Tested up to: 5.9
     6Requires PHP: 5.3.2
     7Stable tag: 1.2.0
    78License: GPL2
    89
     
    4344== Screenshots ==
    4445
    45 (none yet)
     46(Not applicable to this plugin)
    4647
    4748== Changelog ==
    4849
    49 = 1.1 =
     50= 1.2.0 - 2022-03-18 =
     51* Add - Native wp_get_environment_type() support.
     52* Add - Support for checking with Jetpack for staging.
     53* Add - Support for modifying CC and BCC lists in emails.
     54* Dev - Add separate changelog file.
     55* Dev - add automated build process.
     56* Tweak - WP 5.9 compatibility.
     57
     58= 1.1.0 =
    5059* Under-the-hood change: use a class for the main plugin functionality
    5160* Check PHP version with an activation hook
    5261* Update the "Tested up to" tag
    5362
    54 = 1.0 =
     63= 1.0.0 =
    5564* Created the plugin
    5665
  • redirect-emails-on-staging/trunk/redirect-emails-on-staging.php

    r871476 r2696275  
    11<?php
    22/**
    3  * Plugin Name: Redirect Emails on Staging
    4  * Plugin URI: http://wordpress.org/plugins/redirect-emails-on-staging/
    5  * Description: (For WP Engine only) On the Staging site, redirect all emails to the site admin. This is useful in making sure that the staging site doesn't send out confusing emails to your users.
    6  * Version: 1.1
    7  * Author: Jeremy Pry
    8  * Author URI: http://jeremypry.com/
    9  * License: GPL2
     3 * Plugin Name:       Redirect Emails on Staging
     4 * Plugin URI:        http://wordpress.org/plugins/redirect-emails-on-staging/
     5 * Description:       On a Staging site, redirect all emails to the site admin. This is useful in making sure that the staging site doesn't send out confusing emails to your users.
     6 * Version:           1.2.0
     7 * Author:            Jeremy Pry
     8 * Author URI:        https://jeremypry.com/
     9 * License:           GPL2
     10 * Requires at least: 5.5.0
     11 * Tested up to: 5.9
     12 * Requires PHP:      5.3.2
     13 * Text Domain:       redirect-emails-on-staging
    1014 */
     15
     16use Automattic\Jetpack\Status;
    1117
    1218// Prevent direct access to this file
     
    1521}
    1622
    17 // Activation check
    18 register_activation_hook( plugin_basename( __FILE__ ), 'jpry_res_activation_check' );
     23define( 'JPRY_REDIRECT_EMAILS_ON_STAGING_VERSION', '1.2.0' ); // WRCS: DEFINED_VERSION.
    1924
    20 /**
    21  * Check to ensure the plugin is able to run
    22  *
    23  * We're specifically looking to make sure there is a PHP version of 5.3.2 or greater
    24  *
    25  * @since 1.1
    26  */
    27 function jpry_res_activation_check() {
    28     $min_php_version = '5.3.2';
    29     $installed_php_version = phpversion();
     25add_filter(
     26    'wp_mail',
     27    function( $args ) {
     28        // Jetpack is the most comprehensive, so use that if available.
     29        if ( class_exists( Status::class ) && method_exists( Status::class, 'is_staging_site' ) ) {
     30            $is_staging = ( new Status() )->is_staging_site();
     31        } else {
     32            // Check WP environment.
     33            $wp_staging = function_exists( 'wp_get_environment_type' ) && 'staging' === wp_get_environment_type();
    3034
    31     if ( version_compare( $min_php_version, $installed_php_version, '>' ) ) {
    32         deactivate_plugins( plugin_basename( __FILE__ ) );
    33         wp_die( sprintf( "This plugin requires a minimum PHP version of %s. You only have version %s installed. Please upgrade PHP to use this plugin.", $min_php_version, $installed_php_version ) );
    34     }
    35 }
     35            // Check on WP Engine hosting (the original purpose of this plugin).
     36            $wpe_staging = function_exists( 'is_wpe_snapshot' ) && is_wpe_snapshot();
    3637
    37 // Some constants
    38 define( 'RES_FILE', __FILE__ );
    39 define( 'RES_DIR', dirname( RES_FILE ) );
     38            $is_staging = $wp_staging || $wpe_staging;
     39        }
    4040
    41 // Pull in the class files
    42 require_once( RES_DIR . "/classes/class-jpry_singleton.php" );
    43 require_once( RES_DIR . "/classes/class-jpry_redirect_staging_email.php" );
     41        // Bail if we're not in a staging situation.
     42        if ( ! $is_staging ) {
     43            return $args;
     44        }
    4445
    45 // Instantiate the class
    46 JPry_Redirect_Staging_Email::get_instance();
     46        /**
     47         * Filter jpry_redirect_staging_emails_email.
     48         *
     49         * @since 1.2.0
     50         *
     51         * @var string $email The email address where all emails generated by the site should be sent.
     52         */
     53        $email = apply_filters( 'jpry_redirect_staging_emails_email', get_site_option( 'admin_email' ) );
     54
     55        // If the email is already to the admin, then no need to modify anything.
     56        if ( $email === $args['to'] ) {
     57            return $args;
     58        }
     59
     60        // We've established that we need to modify the email arguments, so let's do so.
     61        $args['message'] = sprintf(
     62            /* translators: 1: original email address, 2: the original message */
     63            esc_html__( "Originally sent to: %1\$s\n\n%2\$s", 'redirect-emails-on-staging' ),
     64            $args['to'],
     65            $args['message']
     66        );
     67
     68        $args['subject'] = sprintf(
     69            /* translators: %s is the original email subject */
     70            esc_html__( 'REDIRECTED EMAIL | %s', 'redirect-emails-on-staging' ),
     71            $args['subject']
     72        );
     73
     74        $args['to'] = $email;
     75
     76        // Try to handle CC and BCC headers. If there aren't any headers, bail early.
     77        if ( empty( $args['headers'] ) ) {
     78            return $args;
     79        }
     80
     81        $ccs = $bccs = [];
     82
     83        $headers = (array) $args['headers'];
     84        foreach ( $headers as $index => $header ) {
     85            if ( false === strpos( $header, ':' ) ) {
     86                continue;
     87            }
     88
     89            list( $name, $content ) = explode( ':', trim( $header ), 2 );
     90
     91            $name = trim( $name );
     92
     93            switch ( strtolower( $name ) ) {
     94                case 'cc':
     95                    $ccs = array_merge( $ccs, explode( ',', $content ) );
     96                    break;
     97
     98                case 'bcc':
     99                    $bccs = array_merge( $bccs, explode( ',', $content ) );
     100                    break;
     101
     102                default:
     103                    continue 2;
     104            }
     105
     106            // If we got this far, remove the header from the array.
     107            unset( $headers[ $index ] );
     108        }
     109
     110        // Update the headers array.
     111        $args['headers'] = $headers;
     112
     113        // Add a note to the message body about what was removed.
     114        $additional_message = '';
     115        if ( ! empty( $ccs ) ) {
     116            $additional_message .= sprintf(
     117                /* translators: %s is the comma-separated list of any CC emails removed */
     118                esc_html__( 'CCs removed: %s', 'redirect-emails-on-staging' ),
     119                join( ', ', $ccs )
     120            ) . PHP_EOL;
     121        }
     122
     123        if ( ! empty( $bccs ) ) {
     124            $additional_message .= sprintf(
     125                /* translators: %s is the comma-separated list of any BCC emails removed */
     126                esc_html__( 'BCCs removed: %s', 'redirect-emails-on-staging' ),
     127                join( ', ', $bccs )
     128            ) . PHP_EOL;
     129        }
     130
     131        $args['message'] = $additional_message . $args['message'];
     132
     133        return $args;
     134    },
     135    99999
     136);
Note: See TracChangeset for help on using the changeset viewer.