WPCF7_Mail{} │ CF7 1.0
Class that represents an attempt to compose and send email.
Хуки из класса
Использование
$WPCF7_Mail = new WPCF7_Mail(); // use class methods
Методы
- private __construct( $name, $template )
- private attachments( $template = null )
- private compose( $send = true )
- public get( $component, $replace_tags = false )
- public get_component_name()
- public static get_current()
- public static get_current_component_name()
- public static get_current_template_name()
- public get_template_name()
- private htmlize( $body )
- public name()
- public replace_tags( $content, $options = '' )
- public static send( $template, $name = '' )
Код WPCF7_Mail{} WPCF7 Mail{} CF7 5.9.8
class WPCF7_Mail { private static $current = null; private $name = ''; private $locale = ''; private $template = array(); private $component = ''; private $use_html = false; private $exclude_blank = false; /** * Returns the singleton instance of this class. */ public static function get_current() { return self::$current; } /** * Returns the name of the email template currently processed. * * Expected output: 'mail' or 'mail_2' */ public static function get_current_template_name() { $current = self::get_current(); if ( $current instanceof self ) { return $current->get_template_name(); } } /** * Returns the name of the email template component currently processed. * * Expected output: 'recipient', 'sender', 'subject', * 'additional_headers', 'body', or 'attachments' */ public static function get_current_component_name() { $current = self::get_current(); if ( $current instanceof self ) { return $current->get_component_name(); } } /** * Composes and sends email based on the specified template. * * @param array $template Array of email template. * @param string $name Optional name of the template, such as * 'mail' or 'mail_2'. Default empty string. * @return bool Whether the email was sent successfully. */ public static function send( $template, $name = '' ) { self::$current = new self( $name, $template ); return self::$current->compose(); } /** * The constructor method. * * @param string $name The name of the email template. * Such as 'mail' or 'mail_2'. * @param array $template Array of email template. */ private function __construct( $name, $template ) { $this->name = trim( $name ); $this->use_html = ! empty( $template['use_html'] ); $this->exclude_blank = ! empty( $template['exclude_blank'] ); $this->template = wp_parse_args( $template, array( 'subject' => '', 'sender' => '', 'body' => '', 'recipient' => '', 'additional_headers' => '', 'attachments' => '', ) ); if ( $submission = WPCF7_Submission::get_instance() ) { $contact_form = $submission->get_contact_form(); $this->locale = $contact_form->locale(); } } /** * Returns the name of the email template. */ public function name() { return $this->name; } /** * Returns the name of the email template. A wrapper method of name(). */ public function get_template_name() { return $this->name(); } /** * Returns the name of the email template component currently processed. */ public function get_component_name() { return $this->component; } /** * Retrieves a component from the email template. * * @param string $component The name of the component. * @param bool $replace_tags Whether to replace mail-tags * within the component. * @return string The text representation of the email component. */ public function get( $component, $replace_tags = false ) { $this->component = $component; $use_html = ( $this->use_html && 'body' === $component ); $exclude_blank = ( $this->exclude_blank && 'body' === $component ); $template = $this->template; $component = isset( $template[$component] ) ? $template[$component] : ''; if ( $replace_tags ) { $component = $this->replace_tags( $component, array( 'html' => $use_html, 'exclude_blank' => $exclude_blank, ) ); if ( $use_html ) { // Convert <example@example.com> to <example@example.com>. $component = preg_replace_callback( '/<(.*?)>/', static function ( $matches ) { if ( is_email( $matches[1] ) ) { return sprintf( '<%s>', $matches[1] ); } else { return $matches[0]; } }, $component ); if ( ! preg_match( '%<html[>\s].*</html>%is', $component ) ) { $component = $this->htmlize( $component ); } } } $this->component = ''; return $component; } /** * Creates HTML message body by adding the header and footer. * * @param string $body The body part of HTML. * @return string Formatted HTML. */ private function htmlize( $body ) { if ( $this->locale ) { $lang_atts = sprintf( ' %s', wpcf7_format_atts( array( 'dir' => wpcf7_is_rtl( $this->locale ) ? 'rtl' : 'ltr', 'lang' => str_replace( '_', '-', $this->locale ), ) ) ); } else { $lang_atts = ''; } $header = apply_filters( 'wpcf7_mail_html_header', '<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"' . $lang_atts . '> <head> <title>' . esc_html( $this->get( 'subject', true ) ) . '</title> </head> <body> ', $this ); $body = apply_filters( 'wpcf7_mail_html_body', $body, $this ); $footer = apply_filters( 'wpcf7_mail_html_footer', '</body> </html>', $this ); return $header . $body . $footer; } /** * Composes an email message and attempts to send it. * * @param bool $send Whether to attempt to send email. Default true. */ private function compose( $send = true ) { $components = array( 'subject' => $this->get( 'subject', true ), 'sender' => $this->get( 'sender', true ), 'body' => $this->get( 'body', true ), 'recipient' => $this->get( 'recipient', true ), 'additional_headers' => $this->get( 'additional_headers', true ), 'attachments' => $this->attachments(), ); $components = apply_filters( 'wpcf7_mail_components', $components, wpcf7_get_current_contact_form(), $this ); if ( ! $send ) { return $components; } $subject = wpcf7_strip_newline( $components['subject'] ); $sender = wpcf7_strip_newline( $components['sender'] ); $recipient = wpcf7_strip_newline( $components['recipient'] ); $body = $components['body']; $additional_headers = trim( $components['additional_headers'] ); $headers = "From: $sender\n"; if ( $this->use_html ) { $headers .= "Content-Type: text/html\n"; $headers .= "X-WPCF7-Content-Type: text/html\n"; } else { $headers .= "X-WPCF7-Content-Type: text/plain\n"; } if ( $additional_headers ) { $headers .= $additional_headers . "\n"; } $attachments = array_filter( (array) $components['attachments'], function ( $attachment ) { $path = path_join( WP_CONTENT_DIR, $attachment ); if ( ! wpcf7_is_file_path_in_content_dir( $path ) ) { if ( WP_DEBUG ) { trigger_error( sprintf( /* translators: %s: Attachment file path. */ __( 'Failed to attach a file. %s is not in the allowed directory.', 'contact-form-7' ), $path ), E_USER_NOTICE ); } return false; } if ( ! is_readable( $path ) or ! is_file( $path ) ) { if ( WP_DEBUG ) { trigger_error( sprintf( /* translators: %s: Attachment file path. */ __( 'Failed to attach a file. %s is not a readable file.', 'contact-form-7' ), $path ), E_USER_NOTICE ); } return false; } static $total_size = array(); if ( ! isset( $total_size[$this->name] ) ) { $total_size[$this->name] = 0; } $file_size = (int) @filesize( $path ); if ( 25 * MB_IN_BYTES < $total_size[$this->name] + $file_size ) { if ( WP_DEBUG ) { trigger_error( __( 'Failed to attach a file. The total file size exceeds the limit of 25 megabytes.', 'contact-form-7' ), E_USER_NOTICE ); } return false; } $total_size[$this->name] += $file_size; return true; } ); return wp_mail( $recipient, $subject, $body, $headers, $attachments ); } /** * Replaces mail-tags within the given text. */ public function replace_tags( $content, $options = '' ) { if ( true === $options ) { $options = array( 'html' => true ); } $options = wp_parse_args( $options, array( 'html' => false, 'exclude_blank' => false, ) ); return wpcf7_mail_replace_tags( $content, $options ); } /** * Creates an array of attachments based on uploaded files and local files. */ private function attachments( $template = null ) { if ( ! $template ) { $template = $this->get( 'attachments' ); } $attachments = array(); if ( $submission = WPCF7_Submission::get_instance() ) { $uploaded_files = $submission->uploaded_files(); foreach ( (array) $uploaded_files as $name => $paths ) { if ( false !== strpos( $template, "[{$name}]" ) ) { $attachments = array_merge( $attachments, (array) $paths ); } } } foreach ( explode( "\n", $template ) as $line ) { $line = trim( $line ); if ( '' === $line or '[' == substr( $line, 0, 1 ) ) { continue; } $attachments[] = path_join( WP_CONTENT_DIR, $line ); } if ( $submission = WPCF7_Submission::get_instance() ) { $attachments = array_merge( $attachments, (array) $submission->extra_attachments( $this->name ) ); } return $attachments; } }