PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.2
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.2
8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 7.8.6 7.8.5 All 44 releases
gamipress / includes / emails.php

emails.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 8.0.2, at includes/emails.php

1,248 lines 42.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Emails
4 *
5 * @package GamiPress\Emails
6 * @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com>
7 * @since 1.3.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Get an array of registered email templates
14 *
15 * @since 1.3.0
16 *
17 * @return array The registered email pattern tags
18 */
19 function gamipress_get_email_templates() {
20
21 return apply_filters( 'gamipress_email_templates', array(
22 'default' => __( 'Default Template', 'gamipress' ),
23 'plain' => __( 'Plain Text', 'gamipress' ),
24 ) );
25
26 }
27
28 /**
29 * Send the email
30 *
31 * @since 1.3.0
32 *
33 * @param string $to The To address to send to.
34 * @param string $subject The subject line of the email to send.
35 * @param string $message The body of the email to send.
36 * @param string|array $attachments Attachments to the email in a format supported by wp_mail()
37 *
38 * @return bool
39 */
40 function gamipress_send_email( $to, $subject, $message, $attachments = '' ) {
41
42 // Email headers
43 $headers = gamipress_get_email_headers( $to, $subject, $message, $attachments );
44
45 // Parse tags on subject
46 $subject = gamipress_parse_email_tags( $subject, $to, $subject, $message, $attachments );
47
48 $subject = strip_tags( $subject );
49
50 // Parse tags on message
51 $message = gamipress_parse_email_tags( $message, $to, $subject, $message, $attachments );
52
53 // Apply the email template and parses the message to it
54 $message = gamipress_get_email_body( $to, $subject, $message, $attachments );
55
56 // Get the email attachments
57 $attachments = gamipress_get_email_attachments( $to, $subject, $message, $attachments );
58
59 add_filter( 'wp_mail_from', 'gamipress_get_email_from_address' );
60 add_filter( 'wp_mail_from_name', 'gamipress_get_email_from_name' );
61 add_filter( 'wp_mail_content_type', 'gamipress_get_email_content_type' );
62
63 // Use WordPress email function
64 $sent = wp_mail( $to, $subject, $message, $headers, $attachments );
65
66 remove_filter( 'wp_mail_from', 'gamipress_get_email_from_address' );
67 remove_filter( 'wp_mail_from_name', 'gamipress_get_email_from_name' );
68 remove_filter( 'wp_mail_content_type', 'gamipress_get_email_content_type' );
69
70 // Check for log errors
71 $log_errors = apply_filters( 'gamipress_log_email_errors', true, $to, $subject, $message );
72
73 if( ! $sent && $log_errors === true) {
74
75 if ( is_array( $to ) ) {
76 $to = implode( ',', $to );
77 }
78
79 $log_message = sprintf(
80 __( "[GamiPress] Email failed to send to %s with subject: %s", 'gamipress' ),
81 $to,
82 $subject
83 );
84
85 error_log( $log_message );
86
87 }
88
89 // Return WordPress email function response
90 return $sent;
91
92 }
93
94 /**
95 * Function to get the mail from email address
96 *
97 * @since 1.0.0
98 *
99 * @param string $from
100 *
101 * @return string
102 */
103 function gamipress_get_email_from_address( $from = '' ) {
104
105 $from_address = gamipress_get_option( 'email_from_address', get_bloginfo( 'admin_email' ) );
106
107 return sanitize_email( $from_address );
108 }
109
110 /**
111 * Function to get the mail from name
112 *
113 * @since 1.0.0
114 *
115 * @param string $from_name
116 *
117 * @return string
118 */
119 function gamipress_get_email_from_name( $from_name = '' ) {
120
121 $from_name = gamipress_get_option( 'email_from_name', get_bloginfo( 'name' ) );
122
123 return wp_specialchars_decode( $from_name );
124 }
125
126 /**
127 * Function to get the mail content type
128 *
129 * @since 1.0.0
130 *
131 * @param string $content_type
132 *
133 * @return string
134 */
135 function gamipress_get_email_content_type( $content_type = 'text/html' ) {
136 return 'text/html';
137 }
138
139 /**
140 * Build the email headers based on the email settings
141 *
142 * @since 1.3.0
143 *
144 * @param string|array $to
145 * @param string $subject
146 * @param string $message
147 * @param string|array $attachments
148 *
149 * @return string
150 */
151 function gamipress_get_email_headers( $to, $subject, $message, $attachments = '' ) {
152
153 $from_name = gamipress_get_option( 'email_from_name', get_bloginfo( 'name' ) );
154 $from_email = gamipress_get_option( 'email_from_address', get_bloginfo( 'admin_email' ) );
155 $charset = get_bloginfo( 'charset' );
156
157 // Setup email headers
158 $headers = "From: {$from_name} <{$from_email}>\r\n";
159 $headers .= "Reply-To: {$from_email}\r\n";
160 $headers .= "Content-Type: text/html; charset={$charset}\r\n";
161
162 return apply_filters( 'gamipress_email_headers', $headers, $to, $subject, $message, $attachments );
163
164 }
165
166 /**
167 * Get the configured email template and applies it ot the given message
168 *
169 * @since 1.3.0
170 *
171 * @param string|array $to
172 * @param string $subject
173 * @param string $message
174 * @param string|array $attachments
175 *
176 * @return string
177 */
178 function gamipress_get_email_body( $to, $subject, $message, $attachments = '' ) {
179
180 $template = gamipress_get_option( 'email_template', 'default' );
181
182 ob_start();
183
184 // Get the email header template
185 gamipress_get_template_part( 'emails/header', $template );
186
187 // Get the email body template
188 gamipress_get_template_part( 'emails/body', $template );
189
190 // Get the email footer template
191 gamipress_get_template_part( 'emails/footer', $template );
192
193 $body = ob_get_clean();
194
195 // Apply wpautop on message text
196 $message = wpautop( $message );
197
198 $body = str_replace( '{email}', $message, $body );
199
200 return apply_filters( 'gamipress_email_body', $body, $to, $subject, $message, $attachments );
201
202 }
203
204 /**
205 * Get the email attachments (actually not used in GamiPress but the filters allows to extend it)
206 *
207 * @since 2.0.4
208 *
209 * @param string|array $to
210 * @param string $subject
211 * @param string $message
212 * @param string|array $attachments
213 *
214 * @return string
215 */
216 function gamipress_get_email_attachments( $to, $subject, $message, $attachments = '' ) {
217
218 return apply_filters( 'gamipress_email_attachments', $attachments, $to, $subject, $message );
219
220 }
221
222 /**
223 * Parse the email tags to the given content
224 *
225 * @since 1.3.0
226 *
227 * @param string $content
228 * @param string|array $to
229 * @param string $subject
230 * @param string $message
231 * @param string|array $attachments
232 *
233 * @return string
234 */
235 function gamipress_parse_email_tags( $content, $to, $subject, $message, $attachments = '' ) {
236
237 global $gamipress_email_template_args;
238
239 if( ! is_array( $gamipress_email_template_args ) ) {
240 $gamipress_email_template_args = array();
241 }
242
243 // Ensure vars
244 if( ! isset( $gamipress_email_template_args['user_id'] ) ) {
245 $gamipress_email_template_args['user_id'] = get_current_user_id();
246 }
247
248 if( ! isset( $gamipress_email_template_args['type'] ) ) {
249 $gamipress_email_template_args['type'] = '';
250 }
251
252 // Shorthand
253 $a = $gamipress_email_template_args;
254
255 // Setup replacements
256 $replacements = array();
257
258 if( $a['type'] === 'achievement_earned' && isset( $a['achievement_id'] ) ) {
259
260 $replacements = gamipress_get_achievement_earned_tags_replacements( $a['achievement_id'], $a['user_id'] );
261
262 } else if( $a['type'] === 'step_completed' && isset( $a['step_id'] ) ) {
263
264 $replacements = gamipress_get_step_completed_tags_replacements( $a['step_id'], $a['user_id'] );
265
266 } else if( $a['type'] === 'points_award_completed' && isset( $a['points_award_id'] ) ) {
267
268 $replacements = gamipress_get_points_award_completed_tags_replacements( $a['points_award_id'], $a['user_id'] );
269
270 } else if( $a['type'] === 'points_deduct_completed' && isset( $a['points_deduct_id'] ) ) {
271
272 $replacements = gamipress_get_points_deduct_completed_tags_replacements( $a['points_deduct_id'], $a['user_id'] );
273
274 } else if( $a['type'] === 'rank_earned' && isset( $a['rank_id'] ) ) {
275
276 $replacements = gamipress_get_rank_earned_tags_replacements( $a['rank_id'], $a['user_id'] );
277
278 } else if( $a['type'] === 'rank_requirement_completed' && isset( $a['rank_requirement_id'] ) ) {
279
280 $replacements = gamipress_get_rank_requirement_completed_tags_replacements( $a['rank_requirement_id'], $a['user_id'] );
281
282 } else if( $a['type'] === 'email_footer' && isset( $a['user_id'] ) ) {
283
284 $replacements = gamipress_get_site_tags_replacements();
285 $replacements = array_merge( $replacements, gamipress_get_user_tags_replacements( $a['user_id'] ) );
286
287 }
288
289 // Setup the user
290 $user_id = absint( $a['user_id'] );
291 $user = ( $user_id !== 0 ? get_userdata( $user_id ) : false );
292
293 /**
294 * Parse email tags
295 *
296 * @since 1.3.4
297 *
298 * @param array $replacements
299 * @param WP_User $user
300 * @param array $template_args
301 */
302 $replacements = apply_filters( 'gamipress_parse_email_tags', $replacements, $user, $a );
303
304 return str_replace( array_keys( $replacements ), $replacements, $content );
305
306 }
307
308 /**
309 * Similar to gamipress_parse_email_tags() but with sample data for preview
310 *
311 * @since 1.3.0
312 *
313 * @param string $content
314 * @param string|array $to
315 * @param string $subject
316 * @param string $message
317 * @param string|array $attachments
318 *
319 * @return mixed
320 */
321 function gamipress_parse_preview_email_tags( $content, $to, $subject, $message, $attachments = '' ) {
322
323 global $gamipress_email_template_args;
324
325 $user = wp_get_current_user();
326
327 $replacements = array(
328 '{user_id}' => $user->ID,
329 '{user}' => $user->display_name,
330 '{user_first}' => $user->first_name,
331 '{user_last}' => $user->last_name,
332 '{user_email}' => $user->user_email,
333 '{user_username}' => $user->user_login,
334 '{site_title}' => get_bloginfo( 'name' ),
335 '{site_link}' => '<a href="' . esc_url( home_url() ) . '">' . get_bloginfo( 'name' ) . '</a>',
336 );
337
338 $img_placeholder_style = "
339 display: inline-block;
340 line-height: 100px;
341 width: 100px;
342 text-align: center;
343 font-weight: bold;
344 background-color: #eee;
345 color: #888;
346 ";
347
348 if( $gamipress_email_template_args['type'] === 'achievement_earned' ) {
349
350 $replacements['{achievement_id}'] = 1;
351 $replacements['{achievement_title}'] = __( 'Sample Achievement', 'gamipress' );
352 $replacements['{achievement_url}'] = '#';
353 $replacements['{achievement_link}'] = '<a href="#" title="' . $replacements['{achievement_title}'] . '">' . $replacements['{achievement_title}'] . '</a>';
354 $replacements['{achievement_excerpt}'] = __( 'Sample Achievement Excerpt', 'gamipress' );
355 $replacements['{achievement_image}'] = '<div style="' . $img_placeholder_style . '">100x100</div>';
356 $replacements['{achievement_steps}'] = '<ul>'
357 . '<li>' . __( 'Not earned achievement step.', 'gamipress' ) . '</li>'
358 . '<li style="text-decoration: line-through;">' . __( 'Earned achievement step.', 'gamipress' ) . '</li>'
359 . '</ul>';
360 $replacements['{achievement_type}'] = __( 'Sample Achievement Type', 'gamipress' );
361 $replacements['{achievement_congratulations}'] = __( 'Sample Achievement Congratulations Text', 'gamipress' );
362
363 } else if( $gamipress_email_template_args['type'] === 'step_completed' ) {
364
365 $replacements['{label}'] = __( 'Sample Step Label', 'gamipress' );
366
367 // Set a temporal type to parse achievement tags
368 $gamipress_email_template_args['type'] = 'achievement_earned';
369
370 $content = gamipress_parse_preview_email_tags( $content, $to, $subject, $message, $attachments );
371
372 // Restore the original type
373 $gamipress_email_template_args['type'] = 'step_completed';
374
375 } else if( $gamipress_email_template_args['type'] === 'points_award_completed' ) {
376
377 $replacements['{label}'] = __( 'Sample Points Award Label', 'gamipress' );
378 $replacements['{points}'] = 100;
379 $replacements['{points_balance}'] = 1000;
380 $replacements['{points_type}'] = __( 'Sample Points Type', 'gamipress' );
381 $replacements['{points_image}'] = '<div style="' . $img_placeholder_style . '">100x100</div>';;
382
383 } else if( $gamipress_email_template_args['type'] === 'points_deduct_completed' ) {
384
385 $replacements['{label}'] = __( 'Sample Points Deduction Label', 'gamipress' );
386 $replacements['{points}'] = 100;
387 $replacements['{points_balance}'] = 1000;
388 $replacements['{points_type}'] = __( 'Sample Points Type', 'gamipress' );
389 $replacements['{points_image}'] = '<div style="' . $img_placeholder_style . '">100x100</div>';;
390
391 } else if( $gamipress_email_template_args['type'] === 'rank_earned' ) {
392
393 $replacements['{rank_id}'] = 1;
394 $replacements['{rank_title}'] = __( 'Sample Rank', 'gamipress' );
395 $replacements['{rank_url}'] = '#';
396 $replacements['{rank_link}'] = '<a href="#" title="' . $replacements['{rank_title}'] . '">' . $replacements['{rank_title}'] . '</a>';
397 $replacements['{rank_excerpt}'] = __( 'Sample Rank Excerpt', 'gamipress' );
398 $replacements['{rank_image}'] = '<div style="' . $img_placeholder_style . '">100x100</div>';
399 $replacements['{rank_requirements}'] = '<ul>'
400 . '<li>' . __( 'Not completed rank requirement.', 'gamipress' ) . '</li>'
401 . '<li style="text-decoration: line-through;">' . __( 'Completed rank requirement.', 'gamipress' ) . '</li>'
402 . '</ul>';
403 $replacements['{rank_type}'] = __( 'Sample Rank Type', 'gamipress' );
404 $replacements['{rank_congratulations}'] = __( 'Sample Rank Congratulations Text', 'gamipress' );
405
406 } else if( $gamipress_email_template_args['type'] === 'rank_requirement_completed' ) {
407
408 $replacements['{label}'] = __( 'Sample Rank Requirement Label', 'gamipress' );
409
410 // Set a temporal type to parse rank tags
411 $gamipress_email_template_args['type'] = 'rank_earned';
412
413 $content = gamipress_parse_preview_email_tags( $content, $to, $subject, $message, $attachments );
414
415 // Restore the original type
416 $gamipress_email_template_args['type'] = 'rank_requirement_completed';
417
418 } else if( $gamipress_email_template_args['type'] === 'email_footer' ) {
419
420 $content = gamipress_get_option( 'email_footer_text' );
421
422 }
423
424 /**
425 * Parse email tags for preview email
426 *
427 * @since 1.3.4
428 *
429 * @param array $replacements
430 * @param WP_User $user
431 * @param array $template_args
432 */
433 $replacements = apply_filters( 'gamipress_parse_preview_email_tags', $replacements, $user, $gamipress_email_template_args );
434
435 return str_replace( array_keys( $replacements ), $replacements, $content );
436
437 }
438
439 /**
440 * Preview the desired email
441 *
442 * @since 1.3.0
443 *
444 * @param string $subject
445 * @param string $message
446 */
447 function gamipress_preview_email( $subject, $message ) {
448
449 if( ! current_user_can( gamipress_get_manager_capability() ) ) {
450 return;
451 }
452
453 // Parse tags on subject
454 $subject = gamipress_parse_preview_email_tags( $subject, '', $subject, $message );
455
456 $subject = strip_tags( $subject );
457
458 // Parse tags on message
459 $message = gamipress_parse_preview_email_tags( $message, '', $subject, $message );
460
461 echo gamipress_get_email_body( '', $subject, $message );
462
463 }
464
465 /**
466 * Preview achievement earned email action
467 *
468 * @since 1.3.0
469 */
470 function gamipress_preview_achievement_earned_email() {
471
472 if( ! current_user_can( gamipress_get_manager_capability() ) ) {
473 return;
474 }
475
476 global $gamipress_email_template_args;
477
478 $gamipress_email_template_args = array(
479 'type' => 'achievement_earned'
480 );
481
482 $subject = apply_filters( 'gamipress_preview_achievement_earned_email_subject', gamipress_get_option( 'achievement_earned_email_subject' ) );
483 $message = apply_filters( 'gamipress_preview_achievement_earned_email_content', gamipress_get_option( 'achievement_earned_email_content' ) );
484
485 gamipress_preview_email( $subject, $message );
486
487 exit;
488
489 }
490 add_action( 'gamipress_action_get_preview_achievement_earned_email', 'gamipress_preview_achievement_earned_email' );
491
492 /**
493 * Preview step completed email action
494 *
495 * @since 1.3.0
496 */
497 function gamipress_preview_step_completed_email() {
498
499 if( ! current_user_can( gamipress_get_manager_capability() ) ) {
500 return;
501 }
502
503 global $gamipress_email_template_args;
504
505 $gamipress_email_template_args = array(
506 'type' => 'step_completed'
507 );
508
509 $subject = apply_filters( 'gamipress_preview_step_completed_email_subject', gamipress_get_option( 'step_completed_email_subject' ) );
510 $message = apply_filters( 'gamipress_preview_step_completed_email_content', gamipress_get_option( 'step_completed_email_content' ) );
511
512 gamipress_preview_email( $subject, $message );
513
514 exit;
515
516 }
517 add_action( 'gamipress_action_get_preview_step_completed_email', 'gamipress_preview_step_completed_email' );
518
519 /**
520 * Preview points award completed email action
521 *
522 * @since 1.3.0
523 */
524 function gamipress_preview_points_award_completed_email() {
525
526 if( ! current_user_can( gamipress_get_manager_capability() ) ) {
527 return;
528 }
529
530 global $gamipress_email_template_args;
531
532 $gamipress_email_template_args = array(
533 'type' => 'points_award_completed'
534 );
535
536 $subject = apply_filters( 'gamipress_preview_points_award_completed_email_subject', gamipress_get_option( 'points_award_completed_email_subject' ) );
537 $message = apply_filters( 'gamipress_preview_points_award_completed_email_content', gamipress_get_option( 'points_award_completed_email_content' ) );
538
539 gamipress_preview_email( $subject, $message );
540
541 exit;
542
543 }
544 add_action( 'gamipress_action_get_preview_points_award_completed_email', 'gamipress_preview_points_award_completed_email' );
545
546 /**
547 * Preview points deduct completed email action
548 *
549 * @since 1.3.7
550 */
551 function gamipress_preview_points_deduct_completed_email() {
552
553 if( ! current_user_can( gamipress_get_manager_capability() ) ) {
554 return;
555 }
556
557 global $gamipress_email_template_args;
558
559 $gamipress_email_template_args = array(
560 'type' => 'points_deduct_completed'
561 );
562
563 $subject = apply_filters( 'gamipress_preview_points_deduct_completed_email_subject', gamipress_get_option( 'points_deduct_completed_email_subject' ) );
564 $message = apply_filters( 'gamipress_preview_points_deduct_completed_email_content', gamipress_get_option( 'points_deduct_completed_email_content' ) );
565
566 gamipress_preview_email( $subject, $message );
567
568 exit;
569
570 }
571 add_action( 'gamipress_action_get_preview_points_deduct_completed_email', 'gamipress_preview_points_deduct_completed_email' );
572
573 /**
574 * Preview rank earned email action
575 *
576 * @since 1.3.1
577 */
578 function gamipress_preview_rank_earned_email() {
579
580 if( ! current_user_can( gamipress_get_manager_capability() ) ) {
581 return;
582 }
583
584 global $gamipress_email_template_args;
585
586 $gamipress_email_template_args = array(
587 'type' => 'rank_earned'
588 );
589
590 $subject = apply_filters( 'gamipress_preview_rank_earned_email_subject', gamipress_get_option( 'rank_earned_email_subject' ) );
591 $message = apply_filters( 'gamipress_preview_rank_earned_email_content', gamipress_get_option( 'rank_earned_email_content' ) );
592
593 gamipress_preview_email( $subject, $message );
594
595 exit;
596
597 }
598 add_action( 'gamipress_action_get_preview_rank_earned_email', 'gamipress_preview_rank_earned_email' );
599
600 /**
601 * Preview rank requirement completed email action
602 *
603 * @since 1.3.1
604 */
605 function gamipress_preview_rank_requirement_completed_email() {
606
607 if( ! current_user_can( gamipress_get_manager_capability() ) ) {
608 return;
609 }
610
611 global $gamipress_email_template_args;
612
613 $gamipress_email_template_args = array(
614 'type' => 'rank_requirement_completed'
615 );
616
617 $subject = apply_filters( 'gamipress_preview_rank_requirement_completed_email_subject', gamipress_get_option( 'rank_requirement_completed_email_subject' ) );
618 $message = apply_filters( 'gamipress_preview_rank_requirement_completed_email_content', gamipress_get_option( 'rank_requirement_completed_email_content' ) );
619
620 gamipress_preview_email( $subject, $message );
621
622 exit;
623
624 }
625 add_action( 'gamipress_action_get_preview_rank_requirement_completed_email', 'gamipress_preview_rank_requirement_completed_email' );
626
627 /**
628 * Send the desired email to the site admin
629 *
630 * @since 1.3.0
631 *
632 * @param string $subject
633 * @param string $message
634 *
635 * @return bool
636 */
637 function gamipress_send_test_email( $subject, $message ) {
638
639 global $gamipress_email_template_args;
640
641 $is_ajax = ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' );
642
643 $user = wp_get_current_user();
644
645 $gamipress_email_template_args['user_id'] = $user->ID;
646
647 // Parse tags on subject
648 $subject = gamipress_parse_preview_email_tags( $subject, '', $subject, $message );
649
650 $subject = strip_tags( $subject );
651
652 // Parse tags on message
653 $message = gamipress_parse_preview_email_tags( $message, '', $subject, $message );
654
655 if( gamipress_send_email( $user->user_email, $subject, $message ) ) {
656 if( $is_ajax ) {
657 wp_send_json_success( __( 'Email sent successfully!', 'gamipress' ) );
658 }
659 } else {
660 if( $is_ajax ) {
661 wp_send_json_error( __( 'There was a problem sending the email. Check your WordPress email configuration.', 'gamipress' ) );
662 }
663 }
664 }
665
666 /**
667 * Send a test achievement earned email
668 *
669 * @since 1.3.0
670 */
671 function gamipress_send_test_achievement_earned_email() {
672
673 global $gamipress_email_template_args;
674
675 $gamipress_email_template_args = array(
676 'type' => 'achievement_earned',
677 );
678
679 $subject = apply_filters( 'gamipress_send_test_achievement_earned_email_subject', gamipress_get_option( 'achievement_earned_email_subject' ) );
680 $message = apply_filters( 'gamipress_send_test_achievement_earned_email_content', gamipress_get_option( 'achievement_earned_email_content' ) );
681
682 gamipress_send_test_email( $subject, $message );
683
684 exit;
685
686 }
687 add_action( 'gamipress_action_get_send_test_achievement_earned_email', 'gamipress_send_test_achievement_earned_email' );
688
689 /**
690 * Send a test step completed email
691 *
692 * @since 1.3.0
693 */
694 function gamipress_send_test_step_completed_email() {
695
696 global $gamipress_email_template_args;
697
698 $gamipress_email_template_args = array(
699 'type' => 'step_completed',
700 );
701
702 $subject = apply_filters( 'gamipress_send_test_step_completed_email_subject', gamipress_get_option( 'step_completed_email_subject' ) );
703 $message = apply_filters( 'gamipress_send_test_step_completed_email_content', gamipress_get_option( 'step_completed_email_content' ) );
704
705 gamipress_send_test_email( $subject, $message );
706
707 exit;
708
709 }
710 add_action( 'gamipress_action_get_send_test_step_completed_email', 'gamipress_send_test_step_completed_email' );
711
712 /**
713 * Send a test points award completed email
714 *
715 * @since 1.3.0
716 */
717 function gamipress_send_test_points_award_completed_email() {
718
719 global $gamipress_email_template_args;
720
721 $gamipress_email_template_args = array(
722 'type' => 'points_award_completed',
723 );
724
725 $subject = apply_filters( 'gamipress_send_test_points_award_completed_email_subject', gamipress_get_option( 'points_award_completed_email_subject' ) );
726 $message = apply_filters( 'gamipress_send_test_points_award_completed_email_content', gamipress_get_option( 'points_award_completed_email_content' ) );
727
728 gamipress_send_test_email( $subject, $message );
729
730 exit;
731
732 }
733 add_action( 'gamipress_action_get_send_test_points_award_completed_email', 'gamipress_send_test_points_award_completed_email' );
734
735 /**
736 * Send a test points deduct completed email
737 *
738 * @since 1.3.0
739 */
740 function gamipress_send_test_points_deduct_completed_email() {
741
742 global $gamipress_email_template_args;
743
744 $gamipress_email_template_args = array(
745 'type' => 'points_deduct_completed',
746 );
747
748 $subject = apply_filters( 'gamipress_send_test_points_deduct_completed_email_subject', gamipress_get_option( 'points_deduct_completed_email_subject' ) );
749 $message = apply_filters( 'gamipress_send_test_points_deduct_completed_email_content', gamipress_get_option( 'points_deduct_completed_email_content' ) );
750
751 gamipress_send_test_email( $subject, $message );
752
753 exit;
754
755 }
756 add_action( 'gamipress_action_get_send_test_points_deduct_completed_email', 'gamipress_send_test_points_deduct_completed_email' );
757
758 /**
759 * Send a test rank earned email
760 *
761 * @since 1.3.1
762 */
763 function gamipress_send_test_rank_earned_email() {
764
765 global $gamipress_email_template_args;
766
767 $gamipress_email_template_args = array(
768 'type' => 'rank_earned',
769 );
770
771 $subject = apply_filters( 'gamipress_send_test_rank_earned_email_subject', gamipress_get_option( 'rank_earned_email_subject' ) );
772 $message = apply_filters( 'gamipress_send_test_rank_earned_email_content', gamipress_get_option( 'rank_earned_email_content' ) );
773
774 gamipress_send_test_email( $subject, $message );
775
776 exit;
777
778 }
779 add_action( 'gamipress_action_get_send_test_rank_earned_email', 'gamipress_send_test_rank_earned_email' );
780
781 /**
782 * Send a test rank requirement completed email
783 *
784 * @since 1.3.1
785 */
786 function gamipress_send_test_rank_requirement_completed_email() {
787
788 global $gamipress_email_template_args;
789
790 $gamipress_email_template_args = array(
791 'type' => 'rank_requirement_completed',
792 );
793
794 $subject = apply_filters( 'gamipress_send_test_rank_requirement_completed_email_subject', gamipress_get_option( 'rank_requirement_completed_email_subject' ) );
795 $message = apply_filters( 'gamipress_send_test_rank_requirement_completed_email_content', gamipress_get_option( 'rank_requirement_completed_email_content' ) );
796
797 gamipress_send_test_email( $subject, $message );
798
799 exit;
800
801 }
802 add_action( 'gamipress_action_get_send_test_rank_requirement_completed_email', 'gamipress_send_test_rank_requirement_completed_email' );
803
804 /**
805 * Function that check for each awarded achievement if it should be emailed to the user
806 *
807 * @since 1.3.0
808 *
809 * @param int $user_id
810 * @param int $achievement_id
811 * @param string $trigger
812 * @param int $site_id
813 * @param array $args
814 */
815 function gamipress_maybe_send_email_to_user( $user_id, $achievement_id, $trigger, $site_id, $args ) {
816
817 global $gamipress_email_template_args;
818
819 // Setup out achievement types
820 $achievement_types = gamipress_get_achievement_types_slugs();
821
822 // Get the achievement type
823 $achievement_type = gamipress_get_post_type( $achievement_id );
824
825 if( in_array( $achievement_type, $achievement_types ) ) {
826
827 // Check if email disabled from settings
828 if( (bool) apply_filters( 'gamipress_disable_achievement_earned_email', gamipress_get_option( 'disable_achievement_earned_email', false ), $user_id, $achievement_id ) ) {
829 return;
830 }
831
832 // Check if email disabled from user preferences
833 if( gamipress_is_email_disabled_from_user_settings( $user_id, $achievement_id ) ) {
834 return;
835 }
836
837 $gamipress_email_template_args = array(
838 'user_id' => $user_id,
839 'achievement_id' => $achievement_id,
840 'type' => 'achievement_earned',
841 );
842
843 $user = get_userdata( $user_id );
844 $subject = apply_filters( 'gamipress_achievement_earned_email_subject', gamipress_get_option( 'achievement_earned_email_subject' ), $user_id, $achievement_id );
845 $message = apply_filters( 'gamipress_achievement_earned_email_content', gamipress_get_option( 'achievement_earned_email_content' ), $user_id, $achievement_id );
846
847 gamipress_send_email( $user->user_email, $subject, $message );
848
849 } else if( $achievement_type === 'step' ) {
850
851 $achievement = gamipress_get_step_achievement( $achievement_id );
852
853 // Check if step was assigned to an achievement
854 if( ! $achievement ) {
855 return;
856 }
857
858 if( (bool) apply_filters( 'gamipress_disable_step_completed_email', gamipress_get_option( 'disable_step_completed_email', false ), $user_id, $achievement_id, $achievement ) ) {
859 return;
860 }
861
862 // Check if email disabled from user preferences
863 if( gamipress_is_email_disabled_from_user_settings( $user_id, $achievement_id ) ) {
864 return;
865 }
866
867 $all_steps_earned = true;
868 $steps = gamipress_get_achievement_steps( $achievement->ID );
869
870 // Just loop if achievement has more than 1 step
871 if( is_array( $steps ) && count( $steps ) > 1 ) {
872
873 foreach( $steps as $step ) {
874 // check if user has earned this step
875 $earned = gamipress_get_earnings_count( array(
876 'user_id' => absint( $user_id ),
877 'post_id' => absint( $step->ID ),
878 'since' => absint( gamipress_achievement_last_user_activity( $achievement->ID, $user_id ) )
879 ) ) > 0;
880
881 if( ! $earned ) {
882 // Not all steps has been earned, so continue
883 $all_steps_earned = false;
884 break;
885 }
886 }
887 }
888
889 // Just send the email if user has not earned all steps, because user will receive another email that he has earned the achievement
890 if( ! $all_steps_earned ) {
891
892 $gamipress_email_template_args = array(
893 'user_id' => $user_id,
894 'step_id' => $achievement_id,
895 'type' => 'step_completed',
896 );
897
898 $user = get_userdata( $user_id );
899 $subject = apply_filters( 'gamipress_step_completed_email_subject', gamipress_get_option( 'step_completed_email_subject' ), $user_id, $achievement_id, $achievement );
900 $message = apply_filters( 'gamipress_step_completed_email_content', gamipress_get_option( 'step_completed_email_content' ), $user_id, $achievement_id, $achievement );
901
902 gamipress_send_email( $user->user_email, $subject, $message );
903
904 }
905
906 } else if( $achievement_type === 'points-award' ) {
907
908 if( (bool) apply_filters( 'gamipress_disable_points_award_completed_email', gamipress_get_option( 'disable_points_award_completed_email', false ), $user_id, $achievement_id ) ) {
909 return;
910 }
911
912 // Check if email disabled from user preferences
913 if( gamipress_is_email_disabled_from_user_settings( $user_id, $achievement_id ) ) {
914 return;
915 }
916
917 $gamipress_email_template_args = array(
918 'user_id' => $user_id,
919 'points_award_id' => $achievement_id,
920 'type' => 'points_award_completed',
921 );
922
923 $user = get_userdata( $user_id );
924 $subject = apply_filters( 'gamipress_points_award_completed_email_subject', gamipress_get_option( 'points_award_completed_email_subject' ), $user_id, $achievement_id );
925 $message = apply_filters( 'gamipress_points_award_completed_email_content', gamipress_get_option( 'points_award_completed_email_content' ), $user_id, $achievement_id );
926
927 gamipress_send_email( $user->user_email, $subject, $message );
928
929 } else if( $achievement_type === 'points-deduct' ) {
930
931 if( (bool) apply_filters( 'gamipress_disable_points_deduct_completed_email', gamipress_get_option( 'disable_points_deduct_completed_email', false ), $user_id, $achievement_id ) ) {
932 return;
933 }
934
935 // Check if email disabled from user preferences
936 if( gamipress_is_email_disabled_from_user_settings( $user_id, $achievement_id ) ) {
937 return;
938 }
939
940 $gamipress_email_template_args = array(
941 'user_id' => $user_id,
942 'points_deduct_id' => $achievement_id,
943 'type' => 'points_deduct_completed',
944 );
945
946 $user = get_userdata( $user_id );
947 $subject = apply_filters( 'gamipress_points_deduct_completed_email_subject', gamipress_get_option( 'points_deduct_completed_email_subject' ), $user_id, $achievement_id );
948 $message = apply_filters( 'gamipress_points_deduct_completed_email_content', gamipress_get_option( 'points_deduct_completed_email_content' ), $user_id, $achievement_id );
949
950 gamipress_send_email( $user->user_email, $subject, $message );
951
952 } else if( $achievement_type === 'rank-requirement' ) {
953
954 $rank = gamipress_get_rank_requirement_rank( $achievement_id );
955
956 // Check if requirement was assigned to a rank
957 if( ! $rank ) {
958 return;
959 }
960
961 if( (bool) apply_filters( 'gamipress_disable_rank_requirement_completed_email', gamipress_get_option( 'disable_rank_requirement_completed_email', false ), $user_id, $achievement_id, $rank ) ) {
962 return;
963 }
964
965 // Check if email disabled from user preferences
966 if( gamipress_is_email_disabled_from_user_settings( $user_id, $achievement_id ) ) {
967 return;
968 }
969
970 $all_requirements_earned = true;
971 $requirements = gamipress_get_rank_requirements( $rank->ID );
972
973 // Just loop if rank has more than 1 requirement
974 if( is_array( $requirements ) && count( $requirements ) > 1 ) {
975
976 foreach( $requirements as $requirement ) {
977 // Check if user has earned this requirement
978 $earned = gamipress_get_earnings_count( array(
979 'user_id' => absint( $user_id ),
980 'post_id' => absint( $requirement->ID ),
981 'since' => absint( gamipress_achievement_last_user_activity( $requirement->ID, $user_id ) )
982 ) ) > 0;
983
984 if( ! $earned ) {
985 // Not all requirements has been earned, so continue
986 $all_requirements_earned = false;
987 break;
988 }
989 }
990 }
991
992 // Just send the email if user has not earned all rank requirements, because user will receive another email that he has earned the rank
993 if( ! $all_requirements_earned ) {
994
995 $gamipress_email_template_args = array(
996 'user_id' => $user_id,
997 'rank_requirement_id' => $achievement_id,
998 'type' => 'rank_requirement_completed',
999 );
1000
1001 $user = get_userdata( $user_id );
1002 $subject = apply_filters( 'gamipress_rank_requirement_completed_email_subject', gamipress_get_option( 'rank_requirement_completed_email_subject' ), $user_id, $achievement_id, $rank );
1003 $message = apply_filters( 'gamipress_rank_requirement_completed_email_content', gamipress_get_option( 'rank_requirement_completed_email_content' ), $user_id, $achievement_id, $rank );
1004
1005 gamipress_send_email( $user->user_email, $subject, $message );
1006
1007 }
1008
1009 }
1010
1011
1012 }
1013 add_action( 'gamipress_award_achievement', 'gamipress_maybe_send_email_to_user', 20, 5 );
1014
1015 /**
1016 * Function that check for each awarded rank if it should be emailed to the user
1017 *
1018 * @since 1.3.1
1019 * @updated 1.3.8 Added filters to allow override anything
1020 *
1021 * @param int $user_id
1022 * @param WP_Post $new_rank
1023 * @param WP_Post $old_rank
1024 * @param int $admin_id
1025 * @param int $achievement_id
1026 */
1027 function gamipress_maybe_send_email_to_user_for_rank_earned( $user_id, $new_rank, $old_rank, $admin_id = 0, $achievement_id = null ) {
1028
1029 global $gamipress_email_template_args;
1030
1031 if( (bool) apply_filters( 'gamipress_disable_rank_earned_email', gamipress_get_option( 'disable_rank_earned_email', false ), $user_id, $new_rank ) ) {
1032 return;
1033 }
1034
1035 // Check if email disabled from user preferences
1036 if( gamipress_is_email_disabled_from_user_settings( $user_id, $new_rank->ID ) ) {
1037 return;
1038 }
1039
1040 $gamipress_email_template_args = array(
1041 'user_id' => $user_id,
1042 'rank_id' => $new_rank->ID,
1043 'type' => 'rank_earned',
1044 );
1045
1046 $user = get_userdata( $user_id );
1047
1048 // Available filters to allow override subject
1049 $subject = apply_filters( 'gamipress_rank_earned_email_subject', gamipress_get_option( 'rank_earned_email_subject' ), $user_id, $new_rank->ID );
1050 $message = apply_filters( 'gamipress_rank_earned_email_content', gamipress_get_option( 'rank_earned_email_content' ), $user_id, $new_rank->ID );
1051
1052 gamipress_send_email( $user->user_email, $subject, $message );
1053 }
1054 add_action( 'gamipress_update_user_rank', 'gamipress_maybe_send_email_to_user_for_rank_earned', 20, 5 );
1055
1056 /**
1057 * Get the user email settings
1058 *
1059 * @since 2.2.1
1060 *
1061 * @param int $user_id The user ID
1062 *
1063 * @return array The user email settings
1064 */
1065 function gamipress_get_user_email_settings( $user_id ) {
1066
1067 $user_id = absint( $user_id );
1068
1069 $cache = gamipress_get_cache( 'user_email_settings', array(), false );
1070
1071 // If result already cached, return it
1072 if( isset( $cache[$user_id] ) ) {
1073 return $cache[$user_id];
1074 }
1075
1076 $email_settings = gamipress_get_user_meta( $user_id, 'gamipress_email_settings', true );
1077
1078 // Ensure that is an array
1079 if( ! is_array( $email_settings ) ) {
1080 $email_settings = array();
1081 }
1082
1083 // Setup the default values
1084 $default_settings =array(
1085 'all' => 'yes',
1086 'points_types' => 'yes',
1087 'achievement_types' => 'yes',
1088 'rank_types' => 'yes',
1089 );
1090
1091 // Setup the default values per points type
1092 foreach ( gamipress_get_points_types() as $type => $data ) {
1093 $default_settings['points_types_' . $type] = 'yes';
1094 }
1095
1096 // Setup the default values per achievement type
1097 foreach ( gamipress_get_achievement_types() as $type => $data ) {
1098 $default_settings['achievement_types_' . $type] = 'yes';
1099 }
1100
1101 // Setup the default values per rank type
1102 foreach ( gamipress_get_rank_types() as $type => $data ) {
1103 $default_settings['rank_types_' . $type] = 'yes';
1104 }
1105
1106 // Apply the default values
1107 $email_settings = wp_parse_args( $email_settings, $default_settings );
1108
1109 /**
1110 * Filter to override the user email settings
1111 *
1112 * @since 2.2.1
1113 *
1114 * @param array $email_settings The user email settings
1115 * @param int $user_id The user ID
1116 *
1117 * @return array
1118 */
1119 $email_settings = apply_filters( 'gamipress_get_user_email_settings', $email_settings, $user_id );
1120
1121 // Cache the user email settings
1122 $cache[$user_id] = $email_settings;
1123 gamipress_set_cache( 'user_email_settings', $cache );
1124
1125 return $email_settings;
1126
1127
1128 }
1129
1130 /**
1131 * Check if email for a specific type is disabled via the user email settings
1132 *
1133 * @since 2.2.1
1134 *
1135 * @param int $user_id The user ID
1136 * @param int $post_id The post ID to check if emails are disabled or not
1137 *
1138 * @return bool
1139 */
1140 function gamipress_is_email_disabled_from_user_settings( $user_id, $post_id ) {
1141
1142 $disabled = false;
1143
1144 // Get the user email settings
1145 $user_email_settings = gamipress_get_user_email_settings( $user_id );
1146
1147 // Skip the whole function checks if user has disabled all emails
1148 if( $user_email_settings['all'] === 'no' ) {
1149 $disabled = true;
1150 }
1151
1152 if( ! $disabled ) {
1153
1154 // Get the post type of the element to check
1155 $post_type = gamipress_get_post_type( $post_id );
1156 $group = '';
1157 $type = '';
1158
1159 if( in_array( $post_type, gamipress_get_achievement_types() ) ) {
1160 // Achievement
1161 $group = 'achievement_types';
1162 $type = $post_type;
1163 } else if( in_array( $post_type, gamipress_get_rank_types() ) ) {
1164 // Rank
1165 $group = 'rank_types';
1166 $type = $post_type;
1167 } else if( $post_type === 'step' ) {
1168 // Step
1169 $group = 'achievement_types';
1170
1171 $achievement = gamipress_get_step_achievement( $post_id );
1172
1173 if( $achievement ) {
1174 $type = $achievement->post_type;
1175 }
1176
1177 } else if( $post_type === 'rank-requirement' ) {
1178 // Rank requirement
1179 $group = 'rank_types';
1180
1181 $rank = gamipress_get_rank_requirement_rank( $post_id );
1182
1183 if( $rank ) {
1184 $type = $rank->post_type;
1185 }
1186
1187 } else if( $post_type === 'points-award' || $post_type === 'points-deduct' ) {
1188 // Points Award/Deduct
1189 $group = 'points_types';
1190
1191 $points_type = gamipress_get_points_award_points_type( $post_id );
1192
1193 if( $points_type ) {
1194 $type = $points_type->post_name;
1195 }
1196 }
1197
1198 // Check if emails has been disabled for the group (points, achievements or ranks) or for the type
1199 if( ( isset( $user_email_settings[$group] ) && $user_email_settings[$group] === 'no' )
1200 || ( isset( $user_email_settings[$group . '_' . $type] ) && $user_email_settings[$group . '_' . $type] === 'no' ) ) {
1201 $disabled = true;
1202 }
1203 }
1204
1205 /**
1206 * Check if email for a specific type is disabled via the user email settings
1207 *
1208 * @since 2.2.1
1209 *
1210 * @param int $user_id The user ID
1211 * @param int $post_id The post ID to check if emails are disabled or not
1212 *
1213 * @return bool
1214 */
1215 return apply_filters( 'gamipress_is_email_disabled_from_user_settings', $disabled, $user_id, $post_id );
1216
1217 }
1218
1219 /**
1220 * Parse the email tags to footer
1221 *
1222 * @since 1.3.0
1223 *
1224 * @return string
1225 */
1226
1227 function gamipress_get_email_footer_content() {
1228
1229 global $gamipress_email_template_args;
1230
1231 if( ! is_array( $gamipress_email_template_args ) ) {
1232 $gamipress_email_template_args = array();
1233 }
1234
1235 // Ensure vars
1236 if( ! isset( $gamipress_email_template_args['user_id'] ) ) {
1237 $gamipress_email_template_args['user_id'] = get_current_user_id();
1238 }
1239
1240 $gamipress_email_template_args['type'] = 'email_footer';
1241
1242 $content = gamipress_get_option( 'email_footer_text' );
1243
1244 $content_footer = gamipress_parse_email_tags( $content, '', '', '', $attachments = '' );
1245
1246 return apply_filters( 'gamipress_email_footer_content', $content_footer );
1247
1248 }