| Conditions | 23 |
| Paths | 1020 |
| Total Lines | 139 |
| Code Lines | 80 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 11 | public static function trigger_email( $action, $entry, $form ) { |
||
| 12 | $notification = $action->post_content; |
||
| 13 | $email_key = $action->ID; |
||
| 14 | |||
| 15 | // Set the subject |
||
| 16 | if ( empty($notification['email_subject']) ) { |
||
| 17 | $notification['email_subject'] = sprintf(__( '%1$s Form submitted on %2$s', 'formidable' ), $form->name, '[sitename]'); |
||
| 18 | } |
||
| 19 | |||
| 20 | $plain_text = $notification['plain_text'] ? true : false; |
||
| 21 | |||
| 22 | //Filter these fields |
||
| 23 | $filter_fields = array( |
||
| 24 | 'email_to', 'cc', 'bcc', |
||
| 25 | 'reply_to', 'from', |
||
| 26 | 'email_subject', 'email_message', |
||
| 27 | ); |
||
| 28 | |||
| 29 | add_filter('frm_plain_text_email', ($plain_text ? '__return_true' : '__return_false')); |
||
| 30 | |||
| 31 | //Get all values in entry in order to get User ID field ID |
||
| 32 | $values = FrmEntryMeta::getAll( array( 'it.field_id !' => 0, 'it.item_id' => $entry->id ), ' ORDER BY fi.field_order' ); |
||
| 33 | $user_id_field = $user_id_key = ''; |
||
| 34 | foreach ( $values as $value ) { |
||
| 35 | if ( $value->field_type == 'user_id' ) { |
||
| 36 | $user_id_field = $value->field_id; |
||
| 37 | $user_id_key = $value->field_key; |
||
| 38 | break; |
||
| 39 | } |
||
| 40 | unset($value); |
||
| 41 | } |
||
| 42 | |||
| 43 | //Filter and prepare the email fields |
||
| 44 | foreach ( $filter_fields as $f ) { |
||
| 45 | //Don't allow empty From |
||
| 46 | if ( $f == 'from' && empty( $notification[ $f ] ) ) { |
||
| 47 | $notification[ $f ] = '[admin_email]'; |
||
| 48 | } else if ( in_array( $f, array( 'email_to', 'cc', 'bcc', 'reply_to', 'from' ) ) ) { |
||
| 49 | //Remove brackets |
||
| 50 | //Add a space in case there isn't one |
||
| 51 | $notification[ $f ] = str_replace( '<', ' ', $notification[ $f ] ); |
||
| 52 | $notification[ $f ] = str_replace( array( '"', '>' ), '', $notification[ $f ] ); |
||
| 53 | |||
| 54 | //Switch userID shortcode to email address |
||
| 55 | if ( strpos( $notification[ $f ], '[' . $user_id_field . ']' ) !== false || strpos( $notification[ $f ], '[' . $user_id_key . ']' ) !== false ) { |
||
| 56 | $user_data = get_userdata( $entry->metas[ $user_id_field ] ); |
||
| 57 | $user_email = $user_data->user_email; |
||
| 58 | $notification[ $f ] = str_replace( array( '[' . $user_id_field . ']', '[' . $user_id_key . ']' ), $user_email, $notification[ $f ] ); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | $notification[ $f ] = FrmFieldsHelper::basic_replace_shortcodes( $notification[ $f ], $form, $entry ); |
||
| 63 | } |
||
| 64 | |||
| 65 | //Put recipients, cc, and bcc into an array if they aren't empty |
||
| 66 | $to_emails = self::explode_emails( $notification['email_to'] ); |
||
| 67 | $cc = self::explode_emails( $notification['cc'] ); |
||
| 68 | $bcc = self::explode_emails( $notification['bcc'] ); |
||
| 69 | |||
| 70 | $to_emails = apply_filters('frm_to_email', $to_emails, $values, $form->id, compact('email_key', 'entry', 'form')); |
||
| 71 | |||
| 72 | // Stop now if there aren't any recipients |
||
| 73 | if ( empty( $to_emails ) && empty( $cc ) && empty( $bcc ) ) { |
||
| 74 | return; |
||
| 75 | } |
||
| 76 | |||
| 77 | $to_emails = array_unique( (array) $to_emails ); |
||
| 78 | |||
| 79 | $prev_mail_body = $mail_body = $notification['email_message']; |
||
| 80 | $mail_body = FrmEntriesHelper::replace_default_message($mail_body, array( |
||
| 81 | 'id' => $entry->id, 'entry' => $entry, 'plain_text' => $plain_text, |
||
| 82 | 'user_info' => (isset($notification['inc_user_info']) ? $notification['inc_user_info'] : false), |
||
| 83 | ) ); |
||
| 84 | |||
| 85 | // Add the user info if it isn't already included |
||
| 86 | if ( $notification['inc_user_info'] && $prev_mail_body == $mail_body ) { |
||
| 87 | $data = maybe_unserialize($entry->description); |
||
| 88 | $mail_body .= "\r\n\r\n" . __( 'User Information', 'formidable' ) . "\r\n"; |
||
| 89 | $mail_body .= __( 'IP Address', 'formidable' ) . ': ' . $entry->ip . "\r\n"; |
||
|
|
|||
| 90 | $mail_body .= __( 'User-Agent (Browser/OS)', 'formidable' ) . ': ' . FrmEntryFormat::get_browser( $data['browser'] ) . "\r\n"; |
||
| 91 | $mail_body .= __( 'Referrer', 'formidable' ) . ': ' . $data['referrer'] . "\r\n"; |
||
| 92 | } |
||
| 93 | unset($prev_mail_body); |
||
| 94 | |||
| 95 | // Add attachments |
||
| 96 | $attachments = apply_filters('frm_notification_attachment', array(), $form, compact('entry', 'email_key') ); |
||
| 97 | |||
| 98 | if ( ! empty($notification['email_subject']) ) { |
||
| 99 | $notification['email_subject'] = apply_filters('frm_email_subject', $notification['email_subject'], compact('form', 'entry', 'email_key')); |
||
| 100 | } |
||
| 101 | |||
| 102 | // check for a phone number |
||
| 103 | foreach ( (array) $to_emails as $email_key => $e ) { |
||
| 104 | if ( $e != '[admin_email]' && ! is_email($e) ) { |
||
| 105 | $e = explode(' ', $e); |
||
| 106 | |||
| 107 | //If to_email has name <[email protected]> format |
||
| 108 | if ( is_email(end($e)) ) { |
||
| 109 | continue; |
||
| 110 | } |
||
| 111 | |||
| 112 | do_action('frm_send_to_not_email', array( |
||
| 113 | 'e' => $e, |
||
| 114 | 'subject' => $notification['email_subject'], |
||
| 115 | 'mail_body' => $mail_body, |
||
| 116 | 'reply_to' => $notification['reply_to'], |
||
| 117 | 'from' => $notification['from'], |
||
| 118 | 'plain_text' => $plain_text, |
||
| 119 | 'attachments' => $attachments, |
||
| 120 | 'form' => $form, |
||
| 121 | 'email_key' => $email_key, |
||
| 122 | ) ); |
||
| 123 | |||
| 124 | unset( $to_emails[ $email_key ] ); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Send a separate email for email address in the "to" section |
||
| 130 | * @since 2.2.13 |
||
| 131 | */ |
||
| 132 | $send_single_recipient = apply_filters( 'frm_send_separate_emails', false, compact( 'action', 'entry', 'form' ) ); |
||
| 133 | |||
| 134 | // Send the email now |
||
| 135 | $sent_to = self::send_email( array( |
||
| 136 | 'to_email' => $to_emails, |
||
| 137 | 'subject' => $notification['email_subject'], |
||
| 138 | 'message' => $mail_body, |
||
| 139 | 'from' => $notification['from'], |
||
| 140 | 'plain_text' => $plain_text, |
||
| 141 | 'reply_to' => $notification['reply_to'], |
||
| 142 | 'attachments' => $attachments, |
||
| 143 | 'cc' => $cc, |
||
| 144 | 'bcc' => $bcc, |
||
| 145 | 'single_recipient' => $send_single_recipient, |
||
| 146 | ) ); |
||
| 147 | |||
| 148 | return $sent_to; |
||
| 149 | } |
||
| 150 | |||
| 445 |