Forked from wpmudev-sls/wpmudev-forminator-omit-field-from-email.php
Created
June 4, 2022 15:06
-
-
Save glaubersilva/603372ce9a40fd5f8621c3e5c2471d07 to your computer and use it in GitHub Desktop.
[Forminator] Omit field on email notifications.
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: [Forminator] Omit "none" on email notifications. | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Omit the "select" field type in email notifications if the selected value is "none". | |
* Author: Glauber Silva @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* Task: 0/11289012348292/1168248790047528 | |
* License: GPLv2 or later | |
* | |
* @package Forminator_omit_none_on_email_notifications | |
*/ | |
defined( 'ABSPATH' ) || exit; | |
/** | |
* Message data filter - This filter is present in: plugins/forminator/library/modules/custom-forms/front/front-mail.php | |
* | |
* @since 1.0.4 | |
* | |
* @param array $data - the post data. | |
* @param Forminator_Custom_Form_Model $custom_form - the form. | |
* @param Forminator_Form_Entry_Model $entry - saved entry @since 1.0.3. | |
* | |
* @return array $data | |
*/ | |
function wpmudev_omit_none_on_email_notifications( $data, $custom_form, $entry ) { | |
$fields_list = array( 'select', 'checkbox', 'radio' ); | |
foreach ( $custom_form->fields as $key_field => $value_field ) { | |
if ( array_key_exists( $value_field->slug, $data ) && in_array( $value_field->raw['type'], $fields_list, true ) ) { | |
if ( is_array( $data[ $value_field->slug ] ) && 1 === count( $data[ $value_field->slug ] ) && strpos( strtolower( $data[ $value_field->slug ][0] ), 'none' ) !== false ) { | |
unset( $custom_form->fields[ $key_field ] ); | |
} elseif ( is_array( $data[ $value_field->slug ] ) ) { | |
foreach ( $data[ $value_field->slug ] as $key_array => $value_array ) { | |
if ( strpos( strtolower( $value_array ), 'none' ) !== false ) { | |
unset( $entry->meta_data[ $value_field->slug ]['value'][ $key_array ] ); | |
} | |
} | |
} elseif ( strpos( strtolower( $data[ $value_field->slug ] ), 'none' ) !== false ) { | |
unset( $custom_form->fields[ $key_field ] ); | |
} | |
} elseif ( in_array( $value_field->raw['type'], $fields_list, true ) ) { | |
unset( $custom_form->fields[ $key_field ] ); | |
} | |
} | |
return $data; | |
} | |
add_filter( 'forminator_custom_form_mail_data', 'wpmudev_omit_none_on_email_notifications', 999, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment