Edit File by line
/home/barbar84/www/wp-conte.../plugins/ninja-fo.../includes/Actions
File: Email.php
<?php if ( ! defined( 'ABSPATH' ) ) exit;
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Class NF_Action_Email
[3] Fix | Delete
*/
[4] Fix | Delete
final class NF_Actions_Email extends NF_Abstracts_Action
[5] Fix | Delete
{
[6] Fix | Delete
/**
[7] Fix | Delete
* @var string
[8] Fix | Delete
*/
[9] Fix | Delete
protected $_name = 'email';
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* @var array
[13] Fix | Delete
*/
[14] Fix | Delete
protected $_tags = array();
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* @var string
[18] Fix | Delete
*/
[19] Fix | Delete
protected $_timing = 'late';
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* @var int
[23] Fix | Delete
*/
[24] Fix | Delete
protected $_priority = 10;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Constructor
[28] Fix | Delete
*/
[29] Fix | Delete
public function __construct()
[30] Fix | Delete
{
[31] Fix | Delete
parent::__construct();
[32] Fix | Delete
[33] Fix | Delete
$this->_nicename = esc_html__( 'Email', 'ninja-forms' );
[34] Fix | Delete
[35] Fix | Delete
$settings = Ninja_Forms::config( 'ActionEmailSettings' );
[36] Fix | Delete
[37] Fix | Delete
$this->_settings = array_merge( $this->_settings, $settings );
[38] Fix | Delete
[39] Fix | Delete
$this->_backwards_compatibility();
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
/*
[43] Fix | Delete
* PUBLIC METHODS
[44] Fix | Delete
*/
[45] Fix | Delete
[46] Fix | Delete
public function process( $action_settings, $form_id, $data )
[47] Fix | Delete
{
[48] Fix | Delete
$action_settings = $this->sanitize_address_fields( $action_settings );
[49] Fix | Delete
[50] Fix | Delete
$errors = $this->check_for_errors( $action_settings );
[51] Fix | Delete
[52] Fix | Delete
$headers = $this->_get_headers( $action_settings );
[53] Fix | Delete
[54] Fix | Delete
if ( has_filter( 'ninja_forms_get_fields_sorted' ) ) {
[55] Fix | Delete
$fields_by_key = array();
[56] Fix | Delete
foreach( $data[ 'fields' ] as $field ){
[57] Fix | Delete
if( is_null( $field ) ) continue;
[58] Fix | Delete
if( is_array( $field ) ){
[59] Fix | Delete
if( ! isset( $field[ 'key' ] ) ) continue;
[60] Fix | Delete
$key = $field[ 'key' ];
[61] Fix | Delete
} else {
[62] Fix | Delete
$key = $field->get_setting('key');
[63] Fix | Delete
}
[64] Fix | Delete
$fields_by_key[ $key ] = $field;
[65] Fix | Delete
}
[66] Fix | Delete
$data[ 'fields' ] = apply_filters( 'ninja_forms_get_fields_sorted', array(), $data[ 'fields' ], $fields_by_key, $form_id );
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
$attachments = $this->_get_attachments( $action_settings, $data );
[70] Fix | Delete
[71] Fix | Delete
if( 'html' == $action_settings[ 'email_format' ] ) {
[72] Fix | Delete
$message = wpautop( $action_settings['email_message'] );
[73] Fix | Delete
} else {
[74] Fix | Delete
$message = $this->format_plain_text_message( $action_settings[ 'email_message_plain' ] );
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
$message = apply_filters( 'ninja_forms_action_email_message', $message, $data, $action_settings );
[78] Fix | Delete
[79] Fix | Delete
try {
[80] Fix | Delete
/**
[81] Fix | Delete
* Hook into the email send to override functionality.
[82] Fix | Delete
* @return bool True if already sent. False to fallback to default behavior. Throw a new Exception if there is an error.
[83] Fix | Delete
*/
[84] Fix | Delete
if( ! $sent = apply_filters( 'ninja_forms_action_email_send', false, $action_settings, $message, $headers, $attachments ) ){
[85] Fix | Delete
$sent = wp_mail($action_settings['to'], strip_tags( $action_settings['email_subject'] ), $message, $headers, $attachments);
[86] Fix | Delete
}
[87] Fix | Delete
} catch ( Exception $e ){
[88] Fix | Delete
$sent = false;
[89] Fix | Delete
$errors[ 'email_not_sent' ] = $e->getMessage();
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
if( is_user_logged_in() && current_user_can( 'manage_options' ) ) {
[93] Fix | Delete
$data[ 'actions' ][ 'email' ][ 'to' ] = $action_settings[ 'to' ];
[94] Fix | Delete
$data[ 'actions' ][ 'email' ][ 'headers' ] = $headers;
[95] Fix | Delete
$data[ 'actions' ][ 'email' ][ 'attachments' ] = $attachments;
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
$data[ 'actions' ][ 'email' ][ 'sent' ] = $sent;
[99] Fix | Delete
[100] Fix | Delete
// Only show errors to Administrators.
[101] Fix | Delete
if( $errors && current_user_can( 'manage_options' ) ){
[102] Fix | Delete
$data[ 'errors' ][ 'form' ] = $errors;
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
if ( ! empty( $attachments ) ) {
[106] Fix | Delete
$this->_drop_csv();
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
return $data;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
/**
[113] Fix | Delete
* Sanitizes email address settings
[114] Fix | Delete
* @since 3.2.2
[115] Fix | Delete
*
[116] Fix | Delete
* @param array $action_settings
[117] Fix | Delete
* @return array
[118] Fix | Delete
*/
[119] Fix | Delete
protected function sanitize_address_fields( $action_settings )
[120] Fix | Delete
{
[121] Fix | Delete
// Build a look array to compare our email address settings to.
[122] Fix | Delete
$email_address_settings = array( 'to', 'from_address', 'reply_to', 'cc', 'bcc' );
[123] Fix | Delete
[124] Fix | Delete
// Loop over the look up values.
[125] Fix | Delete
foreach( $email_address_settings as $setting ) {
[126] Fix | Delete
// If the loop up values are not set in the action settings continue.
[127] Fix | Delete
if ( ! isset( $action_settings[ $setting ] ) ) continue;
[128] Fix | Delete
[129] Fix | Delete
// If action settings do not match the look up values continue.
[130] Fix | Delete
if ( ! $action_settings[ $setting ] ) continue;
[131] Fix | Delete
[132] Fix | Delete
// This is the array that will contain the sanitized email address values.
[133] Fix | Delete
$sanitized_array = array();
[134] Fix | Delete
[135] Fix | Delete
/*
[136] Fix | Delete
* Checks to see action settings is array,
[137] Fix | Delete
* if not explodes to comma delimited array.
[138] Fix | Delete
*/
[139] Fix | Delete
if( is_array( $action_settings[ $setting ] ) ) {
[140] Fix | Delete
$email_addresses = $action_settings[ $setting ];
[141] Fix | Delete
} else {
[142] Fix | Delete
$email_addresses = explode( ',', $action_settings[ $setting ] );
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
// Loop over our email addresses.
[146] Fix | Delete
foreach( $email_addresses as $email ) {
[147] Fix | Delete
[148] Fix | Delete
// Updated to trim values in case there is a value with spaces/tabs/etc to remove whitespace
[149] Fix | Delete
$email = trim( $email );
[150] Fix | Delete
if ( empty( $email ) ) continue;
[151] Fix | Delete
[152] Fix | Delete
// Build our array of the email addresses.
[153] Fix | Delete
$sanitized_array[] = $email;
[154] Fix | Delete
}
[155] Fix | Delete
// Sanitized our array of settings.
[156] Fix | Delete
$action_settings[ $setting ] = implode( ',' ,$sanitized_array );
[157] Fix | Delete
}
[158] Fix | Delete
return $action_settings;
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
protected function check_for_errors( $action_settings )
[162] Fix | Delete
{
[163] Fix | Delete
$errors = array();
[164] Fix | Delete
[165] Fix | Delete
$email_address_settings = array( 'to', 'from_address', 'reply_to', 'cc', 'bcc' );
[166] Fix | Delete
[167] Fix | Delete
foreach( $email_address_settings as $setting ){
[168] Fix | Delete
if( ! isset( $action_settings[ $setting ] ) ) continue;
[169] Fix | Delete
if( ! $action_settings[ $setting ] ) continue;
[170] Fix | Delete
[171] Fix | Delete
[172] Fix | Delete
$email_addresses = is_array( $action_settings[ $setting ] ) ? $action_settings[ $setting ] : explode( ',', $action_settings[ $setting ] );
[173] Fix | Delete
[174] Fix | Delete
foreach( (array) $email_addresses as $email ){
[175] Fix | Delete
$email = trim( $email );
[176] Fix | Delete
if ( false !== strpos( $email, '<' ) && false !== strpos( $email, '>' ) ) {
[177] Fix | Delete
preg_match('/(?:<)([^>]*)(?:>)/', $email, $email);
[178] Fix | Delete
$email = $email[ 1 ];
[179] Fix | Delete
}
[180] Fix | Delete
if( ! is_email( $email ) ) {
[181] Fix | Delete
$errors[ 'invalid_email' ] = sprintf( esc_html__( 'Your email action "%s" has an invalid value for the "%s" setting. Please check this setting and try again.', 'ninja-forms'), $action_settings[ 'label' ], $setting );
[182] Fix | Delete
}
[183] Fix | Delete
}
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
return $errors;
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
private function _get_headers( $settings )
[190] Fix | Delete
{
[191] Fix | Delete
$headers = array();
[192] Fix | Delete
[193] Fix | Delete
$headers[] = 'Content-Type: text/' . $settings[ 'email_format' ];
[194] Fix | Delete
$headers[] = 'charset=UTF-8';
[195] Fix | Delete
$headers[] = 'X-Ninja-Forms:ninja-forms'; // Flag for transactional email.
[196] Fix | Delete
[197] Fix | Delete
$headers[] = $this->_format_from( $settings );
[198] Fix | Delete
[199] Fix | Delete
$headers = array_merge( $headers, $this->_format_recipients( $settings ) );
[200] Fix | Delete
[201] Fix | Delete
return $headers;
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
private function _get_attachments( $settings, $data )
[205] Fix | Delete
{
[206] Fix | Delete
$attachments = array();
[207] Fix | Delete
[208] Fix | Delete
if( isset( $settings[ 'attach_csv' ] ) && 1 == $settings[ 'attach_csv' ] ){
[209] Fix | Delete
$attachments[] = $this->_create_csv( $data[ 'fields' ] );
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
if( ! isset( $settings[ 'id' ] ) ) $settings[ 'id' ] = '';
[213] Fix | Delete
[214] Fix | Delete
// Allow admins to attach files from media library
[215] Fix | Delete
if (isset($settings['file_attachment']) && 0 < strlen($settings['file_attachment'])) {
[216] Fix | Delete
$file_path = '';
[217] Fix | Delete
$media_id = attachment_url_to_postid($settings['file_attachment']);
[218] Fix | Delete
[219] Fix | Delete
if($media_id !== 0) {
[220] Fix | Delete
$file_path = get_attached_file($media_id);
[221] Fix | Delete
if (0 < strlen($file_path)) {
[222] Fix | Delete
$attachments[] = $file_path;
[223] Fix | Delete
}
[224] Fix | Delete
}
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
$attachments = apply_filters( 'ninja_forms_action_email_attachments', $attachments, $data, $settings );
[228] Fix | Delete
[229] Fix | Delete
return $attachments;
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
private function _format_from( $settings )
[233] Fix | Delete
{
[234] Fix | Delete
$from_name = get_bloginfo( 'name', 'raw' );
[235] Fix | Delete
$from_name = apply_filters( 'ninja_forms_action_email_from_name', $from_name );
[236] Fix | Delete
$from_name = ( $settings[ 'from_name' ] ) ? $settings[ 'from_name' ] : $from_name;
[237] Fix | Delete
[238] Fix | Delete
$from_address = get_bloginfo( 'admin_email' );
[239] Fix | Delete
$from_address = apply_filters( 'ninja_forms_action_email_from_address', $from_address );
[240] Fix | Delete
$from_address = ( $settings[ 'from_address' ] ) ? $settings[ 'from_address' ] : $from_address;
[241] Fix | Delete
[242] Fix | Delete
return $this->_format_recipient( 'from', $from_address, $from_name );
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
private function _format_recipients( $settings )
[246] Fix | Delete
{
[247] Fix | Delete
$headers = array();
[248] Fix | Delete
[249] Fix | Delete
$recipient_settings = array(
[250] Fix | Delete
'Cc' => $settings[ 'cc' ],
[251] Fix | Delete
'Bcc' => $settings[ 'bcc' ],
[252] Fix | Delete
'Reply-to' => $settings[ 'reply_to' ],
[253] Fix | Delete
);
[254] Fix | Delete
[255] Fix | Delete
foreach( $recipient_settings as $type => $emails ){
[256] Fix | Delete
[257] Fix | Delete
$emails = explode( ',', $emails );
[258] Fix | Delete
[259] Fix | Delete
foreach( $emails as $email ) {
[260] Fix | Delete
[261] Fix | Delete
if( ! $email ) continue;
[262] Fix | Delete
[263] Fix | Delete
$matches = array();
[264] Fix | Delete
if (preg_match('/^"?(?<name>[^<"]+)"? <(?<email>[^>]+)>$/', $email, $matches)) {
[265] Fix | Delete
$headers[] = $this->_format_recipient($type, $matches['email'], $matches['name']);
[266] Fix | Delete
} else {
[267] Fix | Delete
$headers[] = $this->_format_recipient($type, $email);
[268] Fix | Delete
}
[269] Fix | Delete
}
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
return $headers;
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
private function _format_recipient( $type, $email, $name = '' )
[276] Fix | Delete
{
[277] Fix | Delete
$type = ucfirst( $type );
[278] Fix | Delete
[279] Fix | Delete
if( ! $name ) $name = $email;
[280] Fix | Delete
[281] Fix | Delete
$recipient = "$type: $name <$email>";
[282] Fix | Delete
[283] Fix | Delete
return $recipient;
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
private function _create_csv( $fields )
[287] Fix | Delete
{
[288] Fix | Delete
$csv_array = array();
[289] Fix | Delete
[290] Fix | Delete
// Get our current date.
[291] Fix | Delete
$date_format = Ninja_Forms()->get_setting( 'date_format' );
[292] Fix | Delete
$today = date( $date_format, current_time( 'timestamp' ) );
[293] Fix | Delete
$csv_array[ 0 ][] = 'Date Submitted';
[294] Fix | Delete
$csv_array[ 1 ][] = $today;
[295] Fix | Delete
[296] Fix | Delete
foreach( $fields as $field ){
[297] Fix | Delete
[298] Fix | Delete
$ignore = array(
[299] Fix | Delete
'hr',
[300] Fix | Delete
'submit',
[301] Fix | Delete
'html',
[302] Fix | Delete
'creditcardcvc',
[303] Fix | Delete
'creditcardexpiration',
[304] Fix | Delete
'creditcardfullname',
[305] Fix | Delete
'creditcardnumber',
[306] Fix | Delete
'creditcardzip',
[307] Fix | Delete
);
[308] Fix | Delete
[309] Fix | Delete
$ignore = apply_filters( 'ninja_forms_csv_ignore_fields', $ignore );
[310] Fix | Delete
[311] Fix | Delete
if( ! isset( $field[ 'label' ] ) ) continue;
[312] Fix | Delete
if( in_array( $field[ 'type' ], $ignore ) ) continue;
[313] Fix | Delete
[314] Fix | Delete
$label = ( '' != $field[ 'admin_label' ] ) ? $field[ 'admin_label' ] : $field[ 'label' ];
[315] Fix | Delete
[316] Fix | Delete
$value = WPN_Helper::stripslashes( $field[ 'value' ] );
[317] Fix | Delete
if ( empty( $value ) && ! isset( $value ) ) {
[318] Fix | Delete
$value = '';
[319] Fix | Delete
}
[320] Fix | Delete
if ( is_array( $value ) ) {
[321] Fix | Delete
$value = implode( ',', $value );
[322] Fix | Delete
}
[323] Fix | Delete
[324] Fix | Delete
// add filter to add single quote if first character in value is '='
[325] Fix | Delete
$value = apply_filters( 'ninja_forms_subs_export_field_value_' . $field[ 'type' ], $value, $field );
[326] Fix | Delete
[327] Fix | Delete
$csv_array[ 0 ][] = $label;
[328] Fix | Delete
$csv_array[ 1 ][] = $value;
[329] Fix | Delete
}
[330] Fix | Delete
[331] Fix | Delete
$csv_content = WPN_Helper::str_putcsv( $csv_array,
[332] Fix | Delete
apply_filters( 'ninja_forms_sub_csv_delimiter', ',' ),
[333] Fix | Delete
apply_filters( 'ninja_forms_sub_csv_enclosure', '"' ),
[334] Fix | Delete
apply_filters( 'ninja_forms_sub_csv_terminator', "\n" )
[335] Fix | Delete
);
[336] Fix | Delete
[337] Fix | Delete
$upload_dir = wp_upload_dir();
[338] Fix | Delete
$path = trailingslashit( $upload_dir['path'] );
[339] Fix | Delete
[340] Fix | Delete
// create temporary file
[341] Fix | Delete
$path = tempnam( $path, 'Sub' );
[342] Fix | Delete
$temp_file = fopen( $path, 'r+' );
[343] Fix | Delete
[344] Fix | Delete
// write to temp file
[345] Fix | Delete
fwrite( $temp_file, $csv_content );
[346] Fix | Delete
fclose( $temp_file );
[347] Fix | Delete
[348] Fix | Delete
// find the directory we will be using for the final file
[349] Fix | Delete
$path = pathinfo( $path );
[350] Fix | Delete
$dir = $path['dirname'];
[351] Fix | Delete
$basename = $path['basename'];
[352] Fix | Delete
[353] Fix | Delete
// create name for file
[354] Fix | Delete
$new_name = apply_filters( 'ninja_forms_submission_csv_name', 'ninja-forms-submission' );
[355] Fix | Delete
[356] Fix | Delete
// remove a file if it already exists
[357] Fix | Delete
if( file_exists( $dir.'/'.$new_name.'.csv' ) ) {
[358] Fix | Delete
unlink( $dir.'/'.$new_name.'.csv' );
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
// move file
[362] Fix | Delete
rename( $dir.'/'.$basename, $dir.'/'.$new_name.'.csv' );
[363] Fix | Delete
return $dir.'/'.$new_name.'.csv';
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
/**
[367] Fix | Delete
* Function to delete csv file from temp directory after Email Action has completed.
[368] Fix | Delete
*/
[369] Fix | Delete
private function _drop_csv()
[370] Fix | Delete
{
[371] Fix | Delete
$upload_dir = wp_upload_dir();
[372] Fix | Delete
$path = trailingslashit( $upload_dir['path'] );
[373] Fix | Delete
[374] Fix | Delete
// create name for file
[375] Fix | Delete
$new_name = apply_filters( 'ninja_forms_submission_csv_name', 'ninja-forms-submission' );
[376] Fix | Delete
[377] Fix | Delete
// remove a file if it already exists
[378] Fix | Delete
if( file_exists( $path.'/'.$new_name.'.csv' ) ) {
[379] Fix | Delete
unlink( $path.'/'.$new_name.'.csv' );
[380] Fix | Delete
}
[381] Fix | Delete
}
[382] Fix | Delete
[383] Fix | Delete
/*
[384] Fix | Delete
* Backwards Compatibility
[385] Fix | Delete
*/
[386] Fix | Delete
[387] Fix | Delete
private function _backwards_compatibility()
[388] Fix | Delete
{
[389] Fix | Delete
add_filter( 'ninja_forms_sub_csv_delimiter', array( $this, 'ninja_forms_sub_csv_delimiter' ), 10, 1 );
[390] Fix | Delete
add_filter( 'ninja_sub_csv_enclosure', array( $this, 'ninja_sub_csv_enclosure' ), 10, 1 );
[391] Fix | Delete
add_filter( 'ninja_sub_csv_terminator', array( $this, 'ninja_sub_csv_terminator' ), 10, 1 );
[392] Fix | Delete
add_filter( 'ninja_forms_action_email_attachments', array( $this, 'ninja_forms_action_email_attachments' ), 10, 3 );
[393] Fix | Delete
}
[394] Fix | Delete
[395] Fix | Delete
public function ninja_forms_sub_csv_delimiter( $delimiter )
[396] Fix | Delete
{
[397] Fix | Delete
return apply_filters( 'nf_sub_csv_delimiter', $delimiter );
[398] Fix | Delete
}
[399] Fix | Delete
[400] Fix | Delete
public function ninja_sub_csv_enclosure( $enclosure )
[401] Fix | Delete
{
[402] Fix | Delete
return apply_filters( 'nf_sub_csv_enclosure', $enclosure );
[403] Fix | Delete
}
[404] Fix | Delete
[405] Fix | Delete
public function ninja_sub_csv_terminator( $terminator )
[406] Fix | Delete
{
[407] Fix | Delete
return apply_filters( 'nf_sub_csv_terminator', $terminator );
[408] Fix | Delete
}
[409] Fix | Delete
[410] Fix | Delete
public function ninja_forms_action_email_attachments( $attachments, $form_data, $action_settings )
[411] Fix | Delete
{
[412] Fix | Delete
return apply_filters( 'nf_email_notification_attachments', $attachments, $action_settings[ 'id' ] );
[413] Fix | Delete
}
[414] Fix | Delete
[415] Fix | Delete
private function format_plain_text_message( $message )
[416] Fix | Delete
{
[417] Fix | Delete
$message = str_replace( array( '<table>', '</table>', '<tr><td>', '' ), '', $message );
[418] Fix | Delete
$message = str_replace( '</td><td>', ' ', $message );
[419] Fix | Delete
$message = str_replace( '</td></tr>', "\r\n", $message );
[420] Fix | Delete
return strip_tags( $message );
[421] Fix | Delete
}
[422] Fix | Delete
}
[423] Fix | Delete
[424] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function