Skip to content

Commit

Permalink
Merge pull request #42 from Xieyt/master
Browse files Browse the repository at this point in the history
feat: block emails
  • Loading branch information
mrrobot47 committed Jul 13, 2023
2 parents ffa677a + a2720f4 commit f831a9d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 15 deletions.
18 changes: 18 additions & 0 deletions 000-block-emails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Plugin Name: Block Emails
* Description: Blocks sending of emails when using wp_mail function to send emails.
* Author: rtCamp
* Author URI: https://rtcamp.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Version: 1.0
*/

// Comment this code if you don't want to disable emails altogether.
if ( ! function_exists( 'wp_mail' ) ) {
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
return true;
}
}
?>
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ RUN curl -sL https://deb.nodesource.com/setup_16.x | bash && \
apt install -y nodejs && \
rm -rf /var/lib/apt/lists/*

COPY deploy.php hosts.yml /
COPY deploy.php hosts.yml 000-block-emails.php /
COPY *.sh /
RUN chmod +x /*.sh

Expand Down
50 changes: 36 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,46 @@ jobs:
define GitHub branch mapping. Only the GitHub branches mapped in `hosts.yml` will be deployed, rest will be filtered
out. Here is a sample [hosts.yml](https://github.com/rtCamp/wordpress-skeleton/blob/main/.github/hosts.yml).

## Inventory file Variables

### Mandatory

| Variable | Default | Possible Values | Purpose |
|---------------|---------|----------------------------------------------------------------|--------------------------------|
| `user` | null | valid username. eg: `root` | Username for ssh. |
| `deploy_path` | null | valid path. eg: `/opt/easyengine/sites/example.com/app/htdocs` | path where action will deploy. |
| `hostname` | null | hostname. eg: `example.com` | hostname for ssh. |


### Optional

| Variable | Default | Possible Values | Description |
|---------------------------------|------------------|-------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `block_emails` | null | true | If set to true, this will enable email blocking functionality. |
| `block_emails_plugin_path` | null | Accept relative path from wp-content directory, eg: `custom-mu-plugins` | If you have set the `MU_PLUGIN_DIR` constant in your wp-config.php file to specify a custom path for mu-plugins, you can use this variable to install Block Emails into your custom mu-plugins directory. |
| `block_emails_plugin_file_name` | 000-block-emails | String without the .php extension. | If you wish to modify the loading position of this plugin within the mu-plugins loading phase. |
| `WP_VERSION` | null | Any valid WordPress version | If you specify a WordPress version, then that speicifc WordPress version will be downloaded, instead of latest WordPress version. **Note:** Please use double quotes while giving value to this variable. This will have higher priority then the one defined in workflow file. |


## Environment Variables

This GitHub action's behavior can be customized using following environment variables:

Variable | Default | Possible Values | Purpose
------------------|---------|---------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
`MU_PLUGINS_URL` | null | vip, any git repo url | If value is `vip`, then action will clone [VIP's MU plugins](https://github.com/Automattic/vip-mu-plugins-public) as `mu-plugins` folder. If you want to specifiy a non-VIP mu-plugins repo, you can provide a publicly accessible mu-plugins repo URL as the value.
`WP_VERSION` | latest | Any valid WordPress version | If you specify a WordPress version, then that speicifc WordPress version will be downloaded, instead of latest WordPress version. **Note:** Please use double quotes while giving value to this variable. Also, `WP_VERSION`, if defined in hosts.yml will have higher priority then the one defined in workflow file.
`WP_MINOR_UPDATE` | null | `true` / `false` | If set to `true`, latest minor version of `WP_VERSION` will be taken.
`JUMPHOST_SERVER` | null | Hostname/IP address of the jumphost server | If the deployment server is not directly accessible, and needs a jumphost, then this method should be used. (Note: The `SSH_PRIVATE_KEY` env variable should have access to the jumphost as well as deployment server for this to work. Also, this method does not work with vault.)
`SUBMODULE_DEPLOY_KEY` | null | Read access deploy key created in the submodule repo's deploy keys. | Only required for privated submodule repo. For now only one private submodule deploy key is allowed. All public submodules in repo will be fetched by default without the need of this env variable. (To create a deploy key go to: Settings > Deploy Keys > Add deploy key)
`SKIP_WP_TASKS` | null | `true`/`false` | If set to `true`, WordPress specific deplyment tasks will skipped.
`PHP_VERSION` | 7.4 | Valid PHP version | Determines the cachetool version compatible to use for purging opcache.
`NPM_VERSION` | null | Valid NPM Version | NPM Version. If not specified, latest version will be used.
`NODE_VERSION` | null | Valid Node Version | If not specified, default version built into action will be used.
`NODE_BUILD_DIRECTORY` | null | path to valid directory on repository. | Build directory. Generally root directory or directory like frontend.
`NODE_BUILD_COMMAND` | null | `npm run build` or similar command. | Command used to to build the dependencies needed on deployment.
`NODE_BUILD_SCRIPT` | null | path to valid shell script | Custom or predefined script to run after compilation.
| Variable | Default | Possible Values | Purpose |
| ------------------ | --------- | --------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `MU_PLUGINS_URL` | null | vip, any git repo url | If value is `vip`, then action will clone [VIP's MU plugins](https://github.com/Automattic/vip-mu-plugins-public) as `mu-plugins` folder. If you want to specifiy a non-VIP mu-plugins repo, you can provide a publicly accessible mu-plugins repo URL as the value. |
| `WP_VERSION` | latest | Any valid WordPress version | If you specify a WordPress version, then that speicifc WordPress version will be downloaded, instead of latest WordPress version. **Note:** Please use double quotes while giving value to this variable. Also, `WP_VERSION`, if defined in hosts.yml will have higher priority then the one defined in workflow file. |
| `WP_MINOR_UPDATE` | null | `true` / `false` | If set to `true`, latest minor version of `WP_VERSION` will be taken. |
| `JUMPHOST_SERVER` | null | Hostname/IP address of the jumphost server | If the deployment server is not directly accessible, and needs a jumphost, then this method should be used. (Note: The `SSH_PRIVATE_KEY` env variable should have access to the jumphost as well as deployment server for this to work. Also, this method does not work with vault.) |
| `SUBMODULE_DEPLOY_KEY` | null | Read access deploy key created in the submodule repo's deploy keys. | Only required for privated submodule repo. For now only one private submodule deploy key is allowed. All public submodules in repo will be fetched by default without the need of this env variable. (To create a deploy key go to: Settings > Deploy Keys > Add deploy key) |
| `SKIP_WP_TASKS` | null | `true`/`false` | If set to `true`, WordPress specific deplyment tasks will skipped. |
| `PHP_VERSION` | 7.4 | Valid PHP version | Determines the cachetool version compatible to use for purging opcache. |
| `NPM_VERSION` | null | Valid NPM Version | NPM Version. If not specified, latest version will be used. |
| `NODE_VERSION` | null | Valid Node Version | If not specified, default version built into action will be used. |
| `NODE_BUILD_DIRECTORY` | null | path to valid directory on repository. | Build directory. Generally root directory or directory like frontend. |
| `NODE_BUILD_COMMAND` | null | `npm run build` or similar command. | Command used to to build the dependencies needed on deployment. |
| `NODE_BUILD_SCRIPT` | null | path to valid shell script | Custom or predefined script to run after compilation. |
| `BLOCK_EMAILS` | null | true | If set to true, this will enable email blocking functionality. |

All node related variables are completely optional. You can use them if your site needs to have node dependencies built.

Expand Down
53 changes: 53 additions & 0 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,58 @@ function deploy() {
dep deploy "$GITHUB_BRANCH"
}

function block_emails() {

hosts_block_email=$(shyaml get-value "$GITHUB_BRANCH.block_emails" < "$hosts_file" 2>/dev/null || exit 0)

if [[ -n "$hosts_block_email" ]]; then
BLOCK_EMAILS="$hosts_block_email"
fi

if [[ -n "$BLOCK_EMAILS" ]]; then

# priority: 1. hosts.yml 2. vip 3. WP
echo -e "\033[34mSETUP EMAIL BLOCKING\033[0m"

hosts_block_email_dir=$(shyaml get-value "$GITHUB_BRANCH.block_emails_plugin_path" < "$hosts_file" 2>/dev/null || exit 0)

if [[ -n "$hosts_block_email_dir" ]]; then
BLOCK_EMAIL_DIR="$HTDOCS/wp-content/$hosts_block_email_dir"
elif [[ -d "$HTDOCS/wp-content/client-mu-plugins" ]]; then
BLOCK_EMAIL_DIR="$HTDOCS/wp-content/client-mu-plugins"
elif [[ -d "$HTDOCS/wp-content/mu-plugins" ]]; then
BLOCK_EMAIL_DIR="$HTDOCS/wp-content/mu-plugins"
else
BLOCK_EMAIL_DIR="$HTDOCS/wp-content/mu-plugins"
mkdir -p "$BLOCK_EMAIL_DIR"
fi

# remove traling slash
BLOCK_EMAIL_DIR="${BLOCK_EMAIL_DIR%/}"

# using this naming convention by default to load this plugin first in mu-plugin loading phase.
BLOCK_EMAIL_PLUGIN_NAME="000-block-emails.php"

hosts_block_email_file_name=$(shyaml get-value "$GITHUB_BRANCH.block_emails_plugin_file_name" < "$hosts_file" 2>/dev/null || exit 0)

if [[ -n "$hosts_block_email_file_name" ]]; then
BLOCK_EMAIL_PLUGIN_NAME="${hosts_block_email_file_name}.php"
fi

BLOCK_EMAIL_PLUGIN_PATH="$BLOCK_EMAIL_DIR/$BLOCK_EMAIL_PLUGIN_NAME"

if [[ -d "$BLOCK_EMAIL_DIR" ]]; then
rsync -av "/000-block-emails.php" "$BLOCK_EMAIL_PLUGIN_PATH"
echo -e "\033[34mEMAIL BLOCK [ACTIVATED]: $BLOCK_EMAIL_PLUGIN_PATH \033[0m"
else
echo -e "\033[31mEMAIL BLOCK [PATH ERROR]: $BLOCK_EMAIL_DIR doesn't exist.\033[0m"

fi
fi

}


function main() {

init_checks
Expand All @@ -279,6 +331,7 @@ function main() {
maybe_run_node_build
maybe_install_submodules
setup_wordpress_files
block_emails
deploy
fi
}
Expand Down

0 comments on commit f831a9d

Please sign in to comment.