-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
231 lines (154 loc) · 7.22 KB
/
functions.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
<?php
function run_wet_email_script(){
global $wet_testing_email_class, $wet_testing_email_class_additional;
// the email type to send
$email_class = get_query_var('wc_email_testing');
// check type is in array
if( !array_key_exists( $email_class, $wet_testing_email_class) && !array_key_exists( $email_class, $wet_testing_email_class_additional ) ){
echo "Invalid email type";
exit();
}
// assign email address and order id variables
if( get_option( "wc_email_testing_order_id", false ) == 'latest' ){
$wc_email_testing_order_id = '';
} else {
$wc_email_testing_order_id = get_option( "wc_email_testing_order_id", false );
}
if( ! $wc_email_testing_order_id ) {
// get a valid and latest order_id ( if no order is has been selected )
global $wpdb;
$order_id_query = 'SELECT order_id FROM '.$wpdb->prefix.'woocommerce_order_items ORDER BY order_item_id DESC LIMIT 1';
$order_id = $wpdb->get_results( $order_id_query );
if( empty( $order_id ) ) {
echo "No order within your WooCommerce shop. Please create a test order first to test the emails";
return;
} else {
$wc_email_testing_order_id = $order_id[0]->order_id ;
}
}
$for_filter = strtolower( str_replace( 'WC_Email_', '' , $email_class ) );
// change email address within order to saved option
add_filter( 'woocommerce_email_recipient_'.$for_filter , 'wet_email_recipient_filter_function', 10, 2);
function wet_email_recipient_filter_function($recipient, $object) {
if( get_option( "wc_email_testing_email", false ) ) {
$wc_email_testing_email = get_option( "wc_email_testing_email", false );
} else {
$wc_email_testing_email = "";
}
$recipient = $wc_email_testing_email;
return $recipient;
}
// change subject link
$subject_filter_prefix = 'woocommerce_email_subject_';
$subject_filter = $for_filter;
add_filter($subject_filter_prefix.$subject_filter , 'wet_change_admin_email_subject', 1, 2);
function wet_change_admin_email_subject( $subject, $order ) {
//global $woocommerce;
$subject = "Testing: ".$subject;
return $subject;
}
// email send toggle
add_filter('woocommerce_email_enabled_'.$for_filter , 'wet_change_email_enabled', 1, 2);
function wet_change_email_enabled( $enabled, $order ) {
if( get_option( "wc_email_testing_email", false ) ) {
return true;
} else {
return false;
}
}
if( isset( $GLOBALS['wc_advanced_notifications'] ) ) {
unset( $GLOBALS['wc_advanced_notifications'] );
}
// load the email classs
$wc_emails = new WC_Emails( );
$emails = $wc_emails->get_emails();
// select the email we want
$new_email = $emails[ $email_class ];
// if it gets a variable from Preview button. Else just send the email out.
if(isset($_GET["preview"])) {
// Make sure email isn't sent to customer (by Muhammad Usama M.)
add_filter( 'woocommerce_email_enabled_' . $new_email->id , '__return_false' );
add_filter( 'woocommerce_email_recipient_' . $new_email->id , '__return_false' );
if( $for_filter == 'customer_note' ) {
$new_email->trigger( array( 'order_id'=>$wc_email_testing_order_id ) );
} else {
$new_email->trigger( $wc_email_testing_order_id );
}
// echo the email content for browser
echo apply_filters( 'woocommerce_mail_content', $new_email->style_inline( $new_email->get_content_html() ) );
} else {
if( $for_filter == 'customer_note' ) {
$new_email->trigger( array( 'order_id'=>$wc_email_testing_order_id ) );
} else {
$new_email->trigger( $wc_email_testing_order_id );
}
// redirect back on the form after email sent
// set GET variable "emailSent" for admin notification "Email Sent".
wp_redirect(admin_url('admin.php?page=wc-email-testing&emailSent'));
}
}
function wet_get_order_id_select_field( $wc_email_testing_order_id ) {
global $wpdb;
$order_id_query = 'SELECT ID as order_id FROM '.$wpdb->prefix . 'posts'.' WHERE post_type = "shop_order" GROUP BY ID ORDER BY ID DESC LIMIT 100';
$order_id = $wpdb->get_results( $order_id_query );
if( empty( $order_id ) ) {
return "<strong style='color:red;'>No orders to display</strong><br><strong style='color:red;'>Please create at least one order</strong>";
} else {
$order_id_select_options = "<option value='latest'>Latest</option>";
foreach( $order_id as $id ) {
$order_id_select_options .= "<option value='{$id->order_id}'>#{$id->order_id}</option>";
}
$order_id_select_options = str_replace( "value='{$wc_email_testing_order_id}'", "value='{$wc_email_testing_order_id}' selected", $order_id_select_options );
$order_id_select = "<select id='wc_email_testing_order_id' name='wc_email_testing_order_id'>{$order_id_select_options}</select>";
return $order_id_select;
}
}
function wet_update_testing_email_options() {
$updated = false;
if( isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'wet_update_form' ) ) {
if( isset( $_POST['wc_email_testing_email'] ) ){
if( $_POST['wc_email_testing_email'] == get_option("wc_email_testing_email") ){
$updated = true;
$email = true;
} else {
$result = update_option( "wc_email_testing_email", sanitize_email( $_POST['wc_email_testing_email'] ) );
if( $result ){
$updated = true;
$email = true;
} else {
$email = false;
}
}
} else {
$result = update_option( "wc_email_testing_email", "" );
$updated = true;
$email = true;
}
if( isset( $_POST['wc_email_testing_order_id'] ) ){
$result = update_option( "wc_email_testing_order_id", intval( $_POST['wc_email_testing_order_id'] ) );
$updated = true;
}
if( !$email ) {
echo "<div id='message' class='error fade'><p><strong>Your email looks invalid</strong></p></div>";
} else {
if( $updated ) {
echo "<div id='message' class='updated fade'><p><strong>Your settings have been saved.</strong></p></div>";
}
}
}
return $updated;
}
function wet_get_testing_email_options() {
$return = array();
if( get_option( "wc_email_testing_email", "false" ) ) {
$return['wc_email_testing_email'] = get_option( "wc_email_testing_email", "" );
} else {
$return['wc_email_testing_email'] = '';
}
if( get_option( "wc_email_testing_order_id", "false" ) ) {
$return['wc_email_testing_order_id'] = get_option( "wc_email_testing_order_id", "false" );
} else {
$return['wc_email_testing_order_id'] = '';
}
return $return;
}