Skip to content

Commit 4e36c6e

Browse files
committed
Post promotion via email.
0 parents  commit 4e36c6e

File tree

13 files changed

+3614
-0
lines changed

13 files changed

+3614
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
composer.lock
3+
vendor
4+
node_modules

Gulpfile.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var gulp = require( 'gulp' );
2+
var sass = require( 'gulp-sass' );
3+
var rename = require( 'gulp-rename' );
4+
var cssmin = require( 'gulp-cssmin' );
5+
var uglify = require( 'gulp-uglify' );
6+
var livereload = require( 'gulp-livereload' );
7+
8+
gulp.task( 'sass', function() {
9+
return gulp.src( 'assets/scss/**/*.scss' )
10+
.pipe( sass().on( 'error', sass.logError ) )
11+
.pipe( gulp.dest( 'assets/css' ) )
12+
.pipe( livereload() );
13+
} );
14+
15+
gulp.task( 'css', [ 'sass' ], function() {
16+
return gulp.src( [ 'assets/css/**/*.css', '!assets/css/**/*.min.css' ] )
17+
.pipe( rename( { suffix: '.min' } ) )
18+
.pipe( cssmin() )
19+
.pipe( gulp.dest( 'assets/css' ) )
20+
} );
21+
22+
gulp.task( 'js', function() {
23+
return gulp.src( [ 'assets/js/**/*.js', '!assets/js/**/*.min.js' ] )
24+
.pipe( uglify( {
25+
mangle: false
26+
} ) )
27+
.pipe( rename( { suffix: '.min' } ) )
28+
.pipe( gulp.dest( 'assets/js' ) )
29+
.pipe( livereload() );
30+
} );
31+
32+
gulp.task( 'default', [ 'css', 'js' ] );
33+
34+
gulp.task( 'watch', [ 'css', 'js' ], function() {
35+
livereload.listen();
36+
gulp.watch( [ 'assets/scss/**/*.scss' ], [ 'css' ] );
37+
gulp.watch( [ 'assets/js/**/*.js' ], [ 'js' ] );
38+
} );

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Delicious Brains WordPress Promoter
2+
3+
WordPress must-use plugin for promoting posts via email and social media.

assets/css/article-promo-email.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#article-promo-email input[type=text] {
2+
width: 100%; }
3+
4+
#article-promo-email textarea {
5+
margin: 10px 0 0 0;
6+
font-family: 'Helvetica', 'Arial', sans-serif;
7+
font-size: 14px;
8+
line-height: 1.4;
9+
background-color: transparent;
10+
box-shadow: none;
11+
border: 0;
12+
padding: 0;
13+
width: 100%; }

assets/css/article-promo-email.min.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#article-promo-email {
2+
input[type=text] {
3+
width: 100%;
4+
}
5+
6+
textarea {
7+
margin: 10px 0 0 0;
8+
font-family: 'Helvetica', 'Arial', sans-serif;
9+
font-size: 14px;
10+
line-height: 1.4;
11+
background-color: transparent;
12+
box-shadow: none;
13+
border: 0;
14+
padding: 0;
15+
width: 100%;
16+
}
17+
}

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "deliciousbrains/wp-post-promoter",
3+
"description": "WordPress must-use plugin for promoting posts via email and social media.",
4+
"license": "GPL-2.0+",
5+
"authors": [
6+
{
7+
"name": "Delicious Brains",
8+
"email": "nom@deliciousbrains.com",
9+
"homepage": "https://deliciousbrains.com/"
10+
}
11+
],
12+
"type": "wordpress-muplugin",
13+
"require": {
14+
"composer/installers": "~1.0",
15+
"lkwdwrd/wp-muplugin-loader": "~1.0.0"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"DeliciousBrains\\WPPromoter\\": "src/"
20+
}
21+
}
22+
}

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "wp-promoter",
3+
"private": true,
4+
"devDependencies": {
5+
"gulp": "^3.9.1",
6+
"gulp-rename": "^1.2.2",
7+
"gulp-cssmin": "^0.2.0",
8+
"gulp-concat": "^2.6.1",
9+
"gulp-livereload": "^4.0.1",
10+
"gulp-sass": "^4.0.2",
11+
"gulp-uglify": "^3.0.1"
12+
}
13+
}

src/Display.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace DeliciousBrains\WPPromoter;
4+
5+
class Display {
6+
7+
/**
8+
* @param $file
9+
* @param $name
10+
* @param array $deps
11+
* @param null $version
12+
* @param bool $in_footer
13+
*
14+
* @return mixed
15+
*/
16+
public static function enqueue( $file, $name, $deps = array(), $version = null, $in_footer = false ) {
17+
$parts = explode( '.', $file );
18+
$ext = array_pop( $parts );
19+
$file = implode( '.', $parts );
20+
21+
$base = '/assets/' . $ext . '/' . $file . '.' . $ext;
22+
23+
$path = DBI_PROMOTER_BASE_DIR . $base;
24+
$src = DBI_PROMOTER_BASE_URL . $base;
25+
26+
if ( is_null( $version ) ) {
27+
$version = filemtime( $path );
28+
}
29+
30+
if ( 'js' === $ext ) {
31+
wp_enqueue_script( $name, $src, $deps, $version, $in_footer );
32+
} else {
33+
wp_enqueue_style( $name, $src, $deps, $version );
34+
}
35+
36+
return $name;
37+
}
38+
}

src/Email.php

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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>&nbsp;&nbsp;</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 &lt;br&gt; 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

Comments
 (0)