From cc5949d474046e0a97f6e70cdfd08a7be6e52653 Mon Sep 17 00:00:00 2001 From: Justin Grant Date: Sat, 18 Oct 2014 23:48:57 -0400 Subject: [PATCH] Adding ability to send email as as HTML and added the capability to use the new [post_content] tag --- admin.php | 77 ++++++++++++++++++++++++++++++++++-------------------- parser.php | 2 ++ router.php | 39 ++++++++++++++++++--------- 3 files changed, 77 insertions(+), 41 deletions(-) diff --git a/admin.php b/admin.php index 69ab463..b1b35ea 100644 --- a/admin.php +++ b/admin.php @@ -121,20 +121,20 @@ function email_add_menu_page_callback() { $send_options = get_option( 'email_settings' ); // variables for the field and option names - $email_action = 'email_action'; - $email_type = 'email_type'; - $email_from = 'email_from'; - $email_from_name= 'email_from_name'; - $email_to = 'email_to'; - $email_to_role = 'email_to_role'; - $email_cc = 'email_cc'; - $email_cc_role = 'email_cc_role'; - $email_bcc = 'email_bcc'; - $email_bcc_role = 'email_bcc_role'; - $email_subject = 'email_subject'; - $email_message = 'email_message'; - $email_hidden = 'email_hidden'; - + $email_action = 'email_action'; + $email_type = 'email_type'; + $email_from = 'email_from'; + $email_from_name = 'email_from_name'; + $email_to = 'email_to'; + $email_to_role = 'email_to_role'; + $email_cc = 'email_cc'; + $email_cc_role = 'email_cc_role'; + $email_bcc = 'email_bcc'; + $email_bcc_role = 'email_bcc_role'; + $email_subject = 'email_subject'; + $email_message = 'email_message'; + $email_hidden = 'email_hidden'; + $email_html_formatted = 'email_html_formatted'; ?>
@@ -235,6 +235,13 @@ function email_add_menu_page_callback() { + + + + + + + @@ -327,6 +334,12 @@ function email_add_menu_page_callback() {
[post_modified_date]
The date the post was modified. This is the timestamp of current action.
+
[post_title]
+
The title of the post.
+ +
[post_content]
+
The content of the post. This will likely be in HTML format, so make sure to set your format accordingly.
+
@@ -338,19 +351,20 @@ function email_add_menu_page_callback() { function email_insert_post() { // variables for the field and option names - $email_action = (isset($_POST['email_action']) ? $_POST['email_action'] : ''); - $email_type = (isset($_POST['email_type']) ? $_POST['email_type'] : ''); - $email_from = (isset($_POST['email_from']) ? $_POST['email_from'] : ''); - $email_from_name = (isset($_POST['email_from_name']) ? $_POST['email_from_name'] : ''); - $email_to = (isset($_POST['email_to']) ? $_POST['email_to'] : ''); - $email_to_role = (isset($_POST['email_to_role']) ? $_POST['email_to_role'] : ''); - $email_cc = (isset($_POST['email_cc']) ? $_POST['email_cc'] : ''); - $email_cc_role = (isset($_POST['email_cc_role']) ? $_POST['email_cc_role'] : ''); - $email_bcc = (isset($_POST['email_bcc']) ? $_POST['email_bcc'] : ''); - $email_bcc_role = (isset($_POST['email_bcc_role']) ? $_POST['email_bcc_role'] : ''); - $email_subject = (isset($_POST['email_subject']) ? $_POST['email_subject'] : ''); - $email_message = (isset($_POST['email_message']) ? $_POST['email_message'] : ''); - $email_hidden = (isset($_POST['email_hidden']) ? $_POST['email_hidden'] : ''); + $email_action = (isset($_POST['email_action']) ? $_POST['email_action'] : ''); + $email_type = (isset($_POST['email_type']) ? $_POST['email_type'] : ''); + $email_from = (isset($_POST['email_from']) ? $_POST['email_from'] : ''); + $email_from_name = (isset($_POST['email_from_name']) ? $_POST['email_from_name'] : ''); + $email_to = (isset($_POST['email_to']) ? $_POST['email_to'] : ''); + $email_to_role = (isset($_POST['email_to_role']) ? $_POST['email_to_role'] : ''); + $email_cc = (isset($_POST['email_cc']) ? $_POST['email_cc'] : ''); + $email_cc_role = (isset($_POST['email_cc_role']) ? $_POST['email_cc_role'] : ''); + $email_bcc = (isset($_POST['email_bcc']) ? $_POST['email_bcc'] : ''); + $email_bcc_role = (isset($_POST['email_bcc_role']) ? $_POST['email_bcc_role'] : ''); + $email_subject = (isset($_POST['email_subject']) ? $_POST['email_subject'] : ''); + $email_message = (isset($_POST['email_message']) ? $_POST['email_message'] : ''); + $email_hidden = (isset($_POST['email_hidden']) ? $_POST['email_hidden'] : ''); + $email_html_formatted = (isset($_POST['email_html_formatted']) ? $_POST['email_html_formatted'] : ''); if( isset($email_hidden) && ( $email_hidden == 'Y' ) ) { @@ -432,6 +446,10 @@ function email_insert_post() { array( 'key' => 'email_message', 'value' => $email_message + ), + array( + 'key' => 'email_html_formatted', + 'value' => $email_html_formatted ) ) ); @@ -511,7 +529,7 @@ function email_insert_post() { $success[] = $email_bcc; } if ( !empty( $email_bcc_role ) ) { - update_post_meta( $post_id, 'email_bcc_role',$email_bcc_role ); + update_post_meta( $post_id, 'email_bcc_role', $email_bcc_role ); $success[] = $email_bcc_role; } if ( !empty( $email_subject ) ) { @@ -519,6 +537,9 @@ function email_insert_post() { $success[] = $email_subject; } + update_post_meta( $post_id, 'email_html_formatted', $email_html_formatted ); + $success[] = $email_html_formatted; + $success[] = 'Edit '. get_the_title( $post_id ) .''; if( !empty( $post_id ) ) { diff --git a/parser.php b/parser.php index 89dc0a1..21699a2 100644 --- a/parser.php +++ b/parser.php @@ -31,6 +31,7 @@ function email_parser( $post_id, $email_id, $old_status, $new_status, $string = '[post_modified_date]', '[post_modified_time]', '[post_title]', + '[post_content]', '[permalink]', '[old_status]', '[new_status]', @@ -65,6 +66,7 @@ function email_parser( $post_id, $email_id, $old_status, $new_status, $string = get_the_modified_date( '', $post_id ), get_the_modified_time( '', $post_id ), get_the_title( $post_id ), + get_post_field('post_content', $post_id ), get_permalink( $post_id ), $old_status, $new_status, diff --git a/router.php b/router.php index be95870..430a845 100644 --- a/router.php +++ b/router.php @@ -38,23 +38,28 @@ function email_action_router( $new_status, $old_status, $post ) { } } +function set_html_content_type() { + return 'text/html'; +} + function email_action( $action, $post_id, $email_id, $old_status, $new_status ) { $post = get_post( $post_id ); - $email_action = get_post_meta( $email_id, 'email_action', true ); - $email_type = get_post_meta( $email_id, 'email_type', true ); - $email_from = get_post_meta( $email_id, 'email_from', true ); - $email_from_name = get_post_meta( $email_id, 'email_from_name', true ); - $email_to = get_post_meta( $email_id, 'email_to', true ); - $email_to_role = get_post_meta( $email_id, 'email_to_role', true ); - $email_cc = get_post_meta( $email_id, 'email_cc', true ); - $email_cc_role = get_post_meta( $email_id, 'email_cc_role', true ); - $email_bcc = get_post_meta( $email_id, 'email_bcc', true ); - $email_bcc_role = get_post_meta( $email_id, 'email_bcc_role', true ); - $email_subject = get_post_meta( $email_id, 'email_subject', true ); - $email_message = get_post_meta( $email_id, 'email_message', true ); - $email_hidden = get_post_meta( $email_id, 'email_hidden', true ); + $email_action = get_post_meta( $email_id, 'email_action', true ); + $email_type = get_post_meta( $email_id, 'email_type', true ); + $email_from = get_post_meta( $email_id, 'email_from', true ); + $email_from_name = get_post_meta( $email_id, 'email_from_name', true ); + $email_to = get_post_meta( $email_id, 'email_to', true ); + $email_to_role = get_post_meta( $email_id, 'email_to_role', true ); + $email_cc = get_post_meta( $email_id, 'email_cc', true ); + $email_cc_role = get_post_meta( $email_id, 'email_cc_role', true ); + $email_bcc = get_post_meta( $email_id, 'email_bcc', true ); + $email_bcc_role = get_post_meta( $email_id, 'email_bcc_role', true ); + $email_subject = get_post_meta( $email_id, 'email_subject', true ); + $email_message = get_post_meta( $email_id, 'email_message', true ); + $email_hidden = get_post_meta( $email_id, 'email_hidden', true ); + $email_html_formatted = get_post_meta( $email_id, 'email_html_formatted', true ) === 'true'? true: false; // Build the comma separated list of email address for the TO field $users_to_list = ''; @@ -104,7 +109,15 @@ function email_action( $action, $post_id, $email_id, $old_status, $new_status ) $parsed_message = email_parser( $post_id, $email_id, $old_status, $new_status ); $parsed_subject = email_parser( $post_id, $email_id, $old_status, $new_status, $email_subject ); + if ( $email_html_formatted ) + add_filter( 'wp_mail_content_type', 'set_html_content_type' ); + $mail = wp_mail( $users_to_list, $parsed_subject, $parsed_message, $headers ); + + + if ( $email_html_formatted ) + // Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578 + remove_filter( 'wp_mail_content_type', 'set_html_content_type' ); // Log successful email if( $mail ) {