-
Notifications
You must be signed in to change notification settings - Fork 0
/
page-email-view.php
executable file
·235 lines (210 loc) · 11.2 KB
/
page-email-view.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Template Name: Email
*
* @todo move into shortcode, this isn't theme territory! (half done, was worse)
* (hint: http://wordpress.stackexchange.com/questions/3396/create-custom-page-templates-with-plugins)
*/
global $current_user, $wpdb,
$vca_asm_geography, $vca_asm_mailer, $vca_asm_utilities;
function pool_email_view_scripts() {
wp_enqueue_script('jquery');
}
add_action( 'wp_enqueue_scripts', 'pool_email_view_scripts' );
$message_placeholder = '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer turpis lacus, posuere id porttitor et, ultrices a nisl. In euismod, tortor nec aliquam sodales, dolor turpis tincidunt neque, sed pharetra tellus mauris vitae erat. In ut convallis tellus. Nunc porttitor luctus sem, in varius eros cursus nec. Etiam in purus quam. Curabitur eleifend facilisis orci quis cursus. Fusce consectetur urna quis nulla pharetra ac suscipit eros accumsan. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Phasellus quis leo nisi, quis dignissim leo. Aliquam ullamcorper metus semper urna rhoncus imperdiet. Morbi elementum orci enim, et fermentum lacus.</p><p>Nam vulputate neque at urna porta scelerisque. Morbi sollicitudin leo sed tellus vestibulum facilisis. Praesent eleifend nunc et enim semper eu porta lorem consectetur. Sed lacinia pharetra ultricies. Fusce rutrum, dolor quis suscipit mattis, dui erat dapibus eros, at consequat elit nisl sit amet dolor. Nulla non suscipit sapien. Donec sollicitudin lacus at risus scelerisque dignissim. Nullam tristique tincidunt metus non porttitor. Etiam egestas arcu et enim euismod lacinia. Donec rhoncus iaculis arcu vitae pellentesque.</p><p>Fusce porta leo dictum eros tempor varius. Donec magna nibh, condimentum quis mollis vel, laoreet quis magna. Cras sit amet dolor eu est rhoncus consequat. Morbi vel porta mauris. Donec pretium metus sed velit ultricies mattis. Vivamus euismod dolor non risus bibendum viverra. Pellentesque ut elit at enim tempus iaculis. Donec varius lobortis metus, in dignissim odio lobortis in. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quis mi ligula. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>';
$subject_placeholder = 'Newsletter';
if( isset( $_POST['mail_submit'] ) && __( 'Preview', 'vca-theme' ) === $_POST['mail_submit'] ) {
$subject = empty( $_POST['subject'] ) ? $subject_placeholder : $_POST['subject'];
$message = nl2br( empty( $_POST['message'] ) ? $message_placeholder : $_POST['message'], true);
$mail_type = empty( $_POST['mail_type'] ) ? 'newsletter' : $_POST['mail_type'];
$time = time();
if ( 'activity' === $mail_type && ! empty( $_POST['activity'] ) ) {
$activity_type = get_post_type( $_POST['activity'] );
if ( 'goldeimerfestival' === $activity_type ) {
$mail_nation = 'goldeimer';
} else {
$mail_nation = $vca_asm_geography->get_alpha_code( get_post_meta( $_POST['activity'], 'nation', true ) );
}
} else {
if ( ! in_array( 'city', $current_user->roles ) ) {
$mail_nation = $vca_asm_geography->get_alpha_code( get_user_meta( $current_user->ID, 'nation', true ) );
} else {
$mail_nation = $vca_asm_geography->get_alpha_code( get_user_meta( $current_user->ID, 'nation', true ) );
}
}
if ( ! in_array( 'city', $current_user->roles ) ) {
$from = trim( $current_user->first_name . ' ' . $current_user->last_name );
} else {
$city_id = get_user_meta( $current_user->ID, 'city', true );
$city_name = $vca_asm_geography->get_name( $city_id );
$from = $vca_asm_geography->get_status( $city_id ) . ' ' . $city_name;
}
$membership = isset( $_POST['membership'] ) ? $_POST['membership'] : 0;
$receipient_group = $_POST['receipient-group'];
$receipient_id = $vca_asm_mailer->receipient_id_from_group( $receipient_group, false );
$for = $vca_asm_mailer->determine_for_field( $receipient_group, $receipient_id, $membership );
$type = isset( $_POST['mail_type'] ) ? $_POST['mail_type'] : 'newsletter';
} elseif ( isset( $_GET['id'] ) ) {
if ( isset( $_GET['auto_action'] ) && in_array( $_GET['auto_action'], array( 'applied', 'accepted', 'denied', 'reg_revoked', 'mem_accepted', 'mem_denied', 'mem_cancelled' ) ) ) {
if ( in_array( $_GET['auto_action'], array( 'applied', 'accepted', 'denied', 'reg_revoked' ) ) ) {
$activity_type = get_post_type( $_GET['id'] );
if ( 'goldeimerfestival' === $activity_type ) {
$mail_nation = 'goldeimer';
$scope = 'ge';
$from = __( 'Goldeimer', 'vca-theme' );
} else {
$nation_id = get_post_meta( $_GET['id'], 'nation', true );
$mail_nation = $vca_asm_geography->get_alpha_code( $nation_id );
$scope = $nation_id;
$from = __( 'Viva con Agua', 'vca-theme' );
}
} else {
$nation_id = $vca_asm_geography->has_nation( $_GET['id'] );
$mail_nation = $vca_asm_geography->get_alpha_code( $nation_id );
$scope = $nation_id;
$from = __( 'Viva con Agua', 'vca-theme' );
}
if ( ! in_array( $scope, array( 40, 42, 68, 'ge' ) ) ) {
$scope = 40;
}
$email_query = $wpdb->get_results(
"SELECT * FROM " . $wpdb->prefix . "vca_asm_auto_responses " .
"WHERE action = '" . $_GET['auto_action'] . "' AND scope = '" . $scope . "' LIMIT 1", ARRAY_A
);
$email = ! empty( $email_query ) ? $email_query[0] : array();
if ( empty( $email ) ) {
$email = $vca_asm_mailer->default_responses[$_GET['auto_action']];
}
if ( in_array( $_GET['auto_action'], array( 'applied', 'accepted', 'denied', 'reg_revoked' ) ) ) {
$email['type'] = 'activity';
$replacement = get_the_title( $_GET['id'] );
$replacement = ! empty( $replacement ) ? $replacement : __( 'Activity', 'vca-theme' );
} else {
$email['type'] = 'membership';
$replacement = $vca_asm_geography->get_name( $_GET['id'] );
$replacement = ! empty( $replacement ) ? $replacement : __( 'Your city', 'vca-theme' );
}
if ( ! empty( $_GET['uid'] ) ) {
$the_user = new WP_User( intval( $_GET['uid'] ) );
$first_name = isset( $the_user->first_name ) ? $the_user->first_name : '';
$last_name = isset( $the_user->last_name ) ? $the_user->last_name : '';
if( ! empty( $first_name ) && ! empty( $last_name ) ) {
$name = $first_name . " " . $last_name;
} elseif( ! empty( $first_name ) ) {
$name = $first_name;
} elseif( ! empty( $last_name ) ) {
$name = $last_name;
} else {
$name = __( 'Supporter', 'vca-theme' );
}
} else {
$name = __( 'Supporter', 'vca-theme' );
}
$placeholders = array( '%event%', '%region%', '%name%' );
$replacements = array( $replacement, $replacement, $name );
$email['subject'] = str_replace( $placeholders, $replacements, $email['subject'] );
$email['message'] = str_replace( $placeholders, $replacements, $email['message'] );
$email['message'] = '<p>' .
preg_replace( '#(<br */?>\s*){2,}#i', '<br><br>' , preg_replace( '/[\r|\n]/', '<br>' , $vca_asm_utilities->urls_to_links( $email['message'] ) ) ) .
'</p>';
} else {
$email_query = $wpdb->get_results(
"SELECT * FROM " . $wpdb->prefix."vca_asm_emails" .
" WHERE id = " . $_GET['id'] . " LIMIT 1", ARRAY_A
);
$email = ! empty( $email_query ) ? $email_query[0] : array();
if ( ! empty( $email['type'] ) && 'activity' === $email['type'] ) {
$activity_type = get_post_type( $email['receipient_id'] );
if ( 'goldeimerfestival' === $activity_type ) {
$mail_nation = 'goldeimer';
} else {
$mail_nation = $vca_asm_geography->get_alpha_code( get_post_meta( $email['receipient_id'], 'nation', true ) );
}
} else {
$mail_nation = $vca_asm_geography->get_alpha_code( get_user_meta( $email['sent_by'], 'nation', true ) );
}
$mail_nation = ! empty( $mail_nation ) ? $mail_nation : 'de';
}
$email['sent_by'] = isset( $email['sent_by'] ) ? $email['sent_by'] : 0;
$email['membership'] = isset( $email['membership'] ) ? $email['membership'] : 0;
$email['receipient_id'] = isset( $email['receipient_id'] ) ? $email['receipient_id'] : 0;
$email['receipient_group'] = isset( $email['receipient_group'] ) ? $email['receipient_group'] : 0;
$city_id = get_user_meta($current_user->ID, 'city', true);
$city_user_id = $wpdb->get_var(
"SELECT user_id FROM " .
$wpdb->prefix . "vca_asm_geography " .
"WHERE id = " . $city_id
);
$author_city_id = get_user_meta($email['sent_by'], 'city', true);
$author_city_id = empty($author_city_id) ? $email['sent_by'] : $author_city_id;
$author_city_user_id = $wpdb->get_var(
"SELECT user_id FROM " .
$wpdb->prefix . "vca_asm_geography " .
"WHERE id = " . $author_city_id
);
$is_readable = false;
if ( $city_user_id == $current_user->ID || $city_user_id == $author_city_user_id || $current_user->ID == $email['sent_by'] || $city_user_id == $email['sent_by'] ) {
$is_readable = true;
}
if(
$current_user->has_cap( 'vca_asm_view_emails_global' ) ||
(
$current_user->has_cap( 'vca_asm_view_emails_nation' ) &&
$vca_asm_geography->has_nation( get_user_meta( $current_user->ID, 'region', true ) ) &&
$sender_nation &&
$sender_nation === $vca_asm_geography->has_nation( get_user_meta( $current_user->ID, 'region', true ) )
) ||
(
$current_user->has_cap( 'vca_asm_view_emails' ) && $is_readable
) ||
isset($_GET['hash']) && md5( $email['time'] ) == $_GET['hash']
||
(
isset( $_GET['auto_action'] ) && in_array( $_GET['auto_action'], array( 'applied', 'accepted', 'denied', 'reg_revoked', 'mem_accepted', 'mem_denied', 'mem_cancelled' ) )
)
) {
$message = empty( $email['message'] ) ? $message_placeholder : $email['message'];
$subject = empty( $email['subject'] ) ? $subject_placeholder : $email['subject'];
$time = empty( $email['time'] ) ? time() : $email['time'];
$from = empty( $from ) ? ( empty( $email['from_name'] ) ? ( empty( $email['sent_by'] ) ? 'Viva con Agua' : $email['sent_by'] ) : $email['from_name'] ) : $from;
if( is_numeric( $from ) ) {
$user = new WP_User( $email['sent_by'] );
if( ! in_array( 'city', $user->roles ) ) {
$from = trim( $user->first_name . ' ' . $user->last_name );
} else {
$city_id = get_user_meta( $user->ID, 'region', true );
$city_name = $vca_asm_geography->get_name( $city_id );
$from = $vca_asm_geography->get_status( $city_id ) . ' ' . $city_name;
}
}
$for = isset( $_GET['auto_action'] ) ? $name : $vca_asm_mailer->determine_for_field( $email['receipient_group'], $email['receipient_id'], $email['membership'] );
$type = empty( $email['type'] ) ? 'newsletter' : $email['type'];
} else {
$message = $message_placeholder;
$subject = $subject_placeholder;
$time = time();
$from = 'Viva con Agua';
$for = __( 'Supporter', 'vca-theme' );
$type = 'newsletter';
}
} else {
$message = $message_placeholder;
$subject = $subject_placeholder;
$time = time();
$from = 'Viva con Agua';
$for = __( 'Supporter', 'vca-theme' );
$mail_nation = 'de';
$type = 'newsletter';
}
$template = new VCA_ASM_Email_Html( array(
'mail_id' => 1,
'message' => $message,
'subject' => $subject,
'reason' => $type,
'from_name' => $from,
'time' => $time,
'mail_nation' => $mail_nation,
'for' => $for,
'in_browser' => true
));
echo $template->output();