-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminder.php
151 lines (135 loc) · 6.54 KB
/
reminder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
function fs_reminder() {
$dt = new DateTime();
$dt->setTimezone(new DateTimeZone( get_option('timezone_string') ) );
update_option('fs_reminder_running', $dt->format('Y-m-d H:i') );
global $wpdb;
$query = "SELECT p.post_date as created, p.ID as ID, e.meta_value as email, s.meta_value as secret FROM $wpdb->posts p "
. "LEFT JOIN $wpdb->postmeta m ON p.ID=m.post_id AND m.meta_key='reminder_sent' "
. "LEFT JOIN $wpdb->postmeta e on p.ID=e.post_id AND e.meta_key='fs_signature_email' "
. "LEFT JOIN $wpdb->postmeta s on p.ID=s.post_id AND s.meta_key='fs_signature_secret' "
. "WHERE post_status=\"draft\" AND post_type=\"fs_signature\" AND m.meta_value IS NULL";
$reminders = $wpdb->get_results ( $query, OBJECT );
$count = 0;
$drafts = 0;
$testing = strpos( site_url(), 'localhost' )!==false;
$template = get_option( 'reminder-template' );
$sender_name = get_option('reminder-sender-name');
$sender_address = get_option('reminder-sender-address');
foreach ( $reminders as $reminder ) {
$createdDT = DateTime::createFromFormat('Y-m-d H:i:s', $reminder->created, new DateTimeZone( get_option('timezone_string') ) );
$todayDT = new DateTime();
$daysago = $createdDT->diff( $todayDT );
$drafts++;
if ( $daysago->d > 3 ) {
$content = str_replace(
array( '{secret}',
),
array( $reminder->secret,
),
$template );
$email = $reminder->email;
if ( $testing ) $email = "nik@cbdweb.net";
$subject = "Can you confirm your email so we can show your support?";
if ( $testing ) $subject .= " - " . $email;
$headers = array();
$headers[] = 'From: "' . $sender_name . '" <' . $sender_address . '>';
$headers[] = "Content-type: text/html";
wp_mail( $email, $subject, $content, $headers );
$count++;
update_post_meta($reminder->ID, 'reminder_sent', 1);
}
}
update_option( 'fs_reminder_count', $count );
update_option( 'fs_reminder_drafts', $drafts );
}
/*
* Signatures options menu item and options page
*/
add_action( 'admin_menu', 'reminder_menu' );
/** Step 1. */
function reminder_menu() {
add_submenu_page( 'edit.php?post_type=fs_signature', 'Reminder Options', 'Options', 'manage_options', basename(__FILE__), 'reminder_options' );
}
/** Step 3. */
function reminder_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
// variables for the field and option names
$hidden_field_name = 'fs_submit_hidden';
$options_array = array (
array('opt_name'=>'reminder-sender-name', 'data_field_name'=>'reminder_sender-name',
'opt_label'=>'Reminder sender (common name)', 'field_type'=>'text'),
array('opt_name'=>'reminder-sender-address', 'data_field_name'=>'reminder-sender-address',
'opt_label'=>'Reminder sender (email address)', 'field_type'=>'email'),
array('opt_name'=>'reminder-template', 'data_field_name'=>'reminder-template',
'opt_label'=>"HTML template for reminders:", 'field_type'=>'textarea' ),
);
// See if the user has posted us some information
// If they did, this hidden field will be set to 'Y'
if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
foreach ($options_array as $option_array ) {
// Read their posted value
$opt_val = stripslashes_deep ( $_POST[ $option_array['data_field_name'] ] );
// Save the posted value in the database
update_option( $option_array ['opt_name'], $opt_val );
}
// Put an settings updated message on the screen
?>
<div class="updated"><p><strong><?php _e('settings saved.' ); ?></strong></p></div>
<?php }
// Now display the settings editing screen
?>
<div class="wrap">
<h2>Reminder Settings</h2>
<form name="reminder_options" id="reminder_options" method="post" action="">
<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
<?php
foreach ( $options_array as $option_array ) {
// Read in existing option value from database
$opt_val = get_option( $option_array[ 'opt_name' ] );
?>
<p><?php _e( $option_array[ 'opt_label' ] );
if($option_array[ 'field_type' ] === 'textarea' ) { ?>
<textarea name="<?php echo $option_array[ 'data_field_name' ]; ?>"><?php echo $opt_val; ?></textarea>
<?php } else { ?>
<input type="<?=$option_array[ 'field_type' ]?>" name="<?=$option_array[ 'data_field_name' ]?>" value="<?=$opt_val?>"/>
<?php } ?>
</p>
<?php } ?>
<hr />
<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
</p>
</form>
<P>Last reminder job ran:
<?=get_option('fs_reminder_running');?>
</p>
<p>
Number of reminders sent:
<?=get_option('fs_reminder_count');?>
</p>
<p>
Number of drafts seen:
<?=get_option('fs_reminder_drafts');?>
</p>
<P>
Test mode is <?=strpos( site_url(), 'localhost' )===false ? "off" : "on";?>
</p>
</div>
<?php
}
/*
* Add link to menu bar for newsletters
*/
add_action( 'admin_bar_menu', 'toolbar_reminder_link', 999 );
function toolbar_reminder_link( $wp_admin_bar ) {
$args = array (
'id'=>'fs-signatures',
'title'=>'Signatures',
'parent'=>'site-name',
'href'=>get_site_url() . '/wp-admin/edit.php?post_type=fs_signature',
);
$wp_admin_bar->add_node( $args );
}