|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DeliciousBrains\WPPromoter; |
| 4 | + |
| 5 | +class Email { |
| 6 | + |
| 7 | + public function init() { |
| 8 | + add_action( 'add_meta_boxes_post', array( $this, 'add_meta_box' ) ); |
| 9 | + add_action( 'add_meta_boxes_doc', array( $this, 'add_meta_box' ) ); |
| 10 | + |
| 11 | + add_action( 'admin_print_scripts-post-new.php', array( $this, 'enqueue_scripts' ), 11 ); |
| 12 | + add_action( 'admin_print_scripts-post.php', array( $this, 'enqueue_scripts' ), 11 ); |
| 13 | + |
| 14 | + add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); |
| 15 | + add_action( 'draft_to_publish', array( $this, 'convert_email_message' ), 10, 2 ); |
| 16 | + add_action( 'draft_to_future', array( $this, 'convert_email_message' ), 10, 2 ); |
| 17 | + } |
| 18 | + |
| 19 | + public function convert_email_message( $post ) { |
| 20 | + if ( ! in_array( $post->post_type, array( 'post', 'doc' ) ) ) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + if ( isset( $_POST['article_promo_email_message'] ) ) { |
| 25 | + $message = $_POST['article_promo_email_message']; |
| 26 | + } |
| 27 | + else { |
| 28 | + $message = get_post_meta( $post->ID, 'article_promo_email_message', true ); |
| 29 | + } |
| 30 | + |
| 31 | + if ( isset( $_POST['article_promo_email_subject'] ) ) { |
| 32 | + $subject = $_POST['article_promo_email_subject']; |
| 33 | + } |
| 34 | + else { |
| 35 | + $subject = get_post_meta( $post->ID, 'article_promo_email_subject', true ); |
| 36 | + } |
| 37 | + |
| 38 | + $subject = trim( $subject ); |
| 39 | + if ( ! $subject ) { |
| 40 | + $subject = $post->post_title; |
| 41 | + } |
| 42 | + |
| 43 | + if ( isset( $_POST['article_promo_email_subject'] ) ) { |
| 44 | + $_POST['article_promo_email_subject'] = $subject; |
| 45 | + } |
| 46 | + else { |
| 47 | + update_post_meta( $post->ID, 'article_promo_email_subject', $subject ); |
| 48 | + } |
| 49 | + |
| 50 | + if ( ! $message ) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + $clone_post = $post; |
| 55 | + $clone_post->post_status = 'publish'; // Ensure the pretty permalink is used for scheduled posts |
| 56 | + |
| 57 | + $url = get_permalink( $clone_post ); |
| 58 | + |
| 59 | + $utm = array( |
| 60 | + 'utm_source' => urlencode( 'Email marketing software' ), |
| 61 | + 'utm_medium' => 'email', |
| 62 | + 'utm_campaign' => 'weekly-article', |
| 63 | + 'utm_content' => urlencode( $post->post_name ), |
| 64 | + ); |
| 65 | + |
| 66 | + $tagged_url = add_query_arg( $utm, $url ); |
| 67 | + $link = sprintf( '<a href="%s">%s</a>', $tagged_url, $url ); |
| 68 | + |
| 69 | + // Standardize newline characters to "\n". |
| 70 | + $message = str_replace( array( "\r\n", "\r" ), "\n", $message ); |
| 71 | + |
| 72 | + // Normalize <br> |
| 73 | + $message = str_replace( array( '<br>', '<br/>' ), '<br />', $message ); |
| 74 | + |
| 75 | + // Replace any new line characters on their own line with a <br /> |
| 76 | + $message = preg_replace( '|^\n|m', "<br />\n", $message ); |
| 77 | + |
| 78 | + // Replace any new line characters that aren't preceded by a <br /> with a <br />. |
| 79 | + $message = preg_replace( '|(?<!<br />)\s*\n|', "<br />\n", $message ); |
| 80 | + |
| 81 | + $message = str_replace( '[link]', $link, $message ); |
| 82 | + $message = str_replace( '[signature]', $this->get_author_html( $post ), $message ); |
| 83 | + |
| 84 | + if ( isset( $_POST['article_promo_email_message'] ) ) { |
| 85 | + $_POST['article_promo_email_message'] = $message; |
| 86 | + } |
| 87 | + else { |
| 88 | + update_post_meta( $post->ID, 'article_promo_email_message', $message ); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public function save_post( $post_id, $post ) { |
| 93 | + if ( ! isset( $_POST['article_promo_email_message'] ) ) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + if ( ! isset( $_POST['article_promo_email_nonce'] ) || ! wp_verify_nonce( $_POST['article_promo_email_nonce'], 'article-promo-email' ) ) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + $post_type = get_post_type_object( $post->post_type ); |
| 102 | + if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + if ( ! empty( trim( $_POST['article_promo_email_subject'] ) ) ) { |
| 107 | + update_post_meta( $post_id, 'article_promo_email_subject', $_POST['article_promo_email_subject'] ); |
| 108 | + } |
| 109 | + |
| 110 | + update_post_meta( $post_id, 'article_promo_email_message', $_POST['article_promo_email_message'] ); |
| 111 | + } |
| 112 | + |
| 113 | + public function get_author_html( $post ) { |
| 114 | + $user = new \WP_User( $post->post_author ); |
| 115 | + ob_start(); |
| 116 | + ?> |
| 117 | + <table style="border-spacing: 0;"> |
| 118 | + <tr> |
| 119 | + <td valign="top" style="font-size: 14px; line-height: 16px; font-family: Helvetica, Arial, sans-serif; padding-bottom: 14px;"> |
| 120 | + <?php echo $user->first_name, ' ', $user->last_name; ?><br> |
| 121 | + <a style="font-size: 12px; color: #000000;" href="http://deliciousbrains.com?utm_source=Email%20marketing%20software&utm_medium=email&utm_campaign=email-signature">Delicious Brains Inc.</a> |
| 122 | + </td> |
| 123 | + <td> </td> |
| 124 | + <td> |
| 125 | + <img src="<?php echo get_avatar_url( $user->ID ); ?>" alt="" width="40" height="40" style="margin: -4px 0 0 0; font-family: Arial, sans-serif; border-radius: 50%;"> |
| 126 | + </td> |
| 127 | + </tr> |
| 128 | + </table> |
| 129 | + <?php |
| 130 | + $html = ob_get_clean(); |
| 131 | + $html = preg_replace( '@^\t\t@m', '', $html ); |
| 132 | + return $html; |
| 133 | + } |
| 134 | + |
| 135 | + public function add_meta_box( $post ) { |
| 136 | + add_meta_box( 'article-promo-email', 'Email Draft', function( $post, $metabox ) { |
| 137 | + $message = get_post_meta( $post->ID, 'article_promo_email_message', true ); |
| 138 | + $subject = get_post_meta( $post->ID, 'article_promo_email_subject', true ); |
| 139 | + |
| 140 | + if ( ! $message ) { |
| 141 | + $message = "{% if subscriber.first_name %}Hey {{ subscriber.first_name }}{% else %}Hey{% endif %},\n\n\n\n[link]\n\nCheers,\n\n[signature]"; |
| 142 | + } |
| 143 | + |
| 144 | + wp_nonce_field( 'article-promo-email', 'article_promo_email_nonce' ); |
| 145 | + ?> |
| 146 | + |
| 147 | + <input type="text" name="article_promo_email_subject" placeholder="Email Subject" value="<?php echo esc_attr( $subject ); ?>"> |
| 148 | + |
| 149 | + <textarea name="article_promo_email_message" rows="12" placeholder="Email message goes here"><?php echo esc_html( $message ); ?></textarea> |
| 150 | + |
| 151 | + <p> |
| 152 | + <?php if ( ! in_array( get_post_status(), array( 'publish', 'future' ) ) ) : ?> |
| 153 | + When this post is published, the shortcode [link] will be replaced |
| 154 | + with a link to the post with the proper utm tags. The shortcode [signature] |
| 155 | + will be replaced with the signature of the author of the post and email. |
| 156 | + Newline characters will be replaced with <br> tags. The subject will |
| 157 | + be filled with the title of the post if it's empty. |
| 158 | + <?php else : ?> |
| 159 | + <a href="https://www.getdrip.com/6392218/broadcasts">Create a new broadcast in Drip</a> |
| 160 | + <?php endif; ?> |
| 161 | + </p> |
| 162 | + |
| 163 | + <?php |
| 164 | + } ); |
| 165 | + } |
| 166 | + |
| 167 | + public function enqueue_scripts() { |
| 168 | + Display::enqueue( 'article-promo-email.css', 'article-promo-email' ); |
| 169 | + } |
| 170 | +} |
0 commit comments