| 1 |
<?php |
| 2 |
|
| 3 |
namespace NotificationX\Admin\Rating; |
| 4 |
|
| 5 |
use NotificationX\GetInstance; |
| 6 |
use WP_REST_Server; |
| 7 |
|
| 8 |
/** |
| 9 |
* This class is responsible for sending weekly email with reports |
| 10 |
* @method static RatingEmail get_instance($args = null) |
| 11 |
* |
| 12 |
* @since 1.4.4 |
| 13 |
*/ |
| 14 |
class RatingEmail { |
| 15 |
private static $_namespace = 'notificationx'; |
| 16 |
private static $_version = 1; |
| 17 |
private static $_mail_sendto = 'support@wpdeveloper.com'; |
| 18 |
/** |
| 19 |
* Instance of RatingEmail |
| 20 |
* |
| 21 |
* @var RatingEmail |
| 22 |
*/ |
| 23 |
use GetInstance; |
| 24 |
|
| 25 |
/** |
| 26 |
* Initially Invoked by Default. |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
add_action('rest_api_init', [$this, 'register_routes']); |
| 30 |
} |
| 31 |
|
| 32 |
public function register_routes() |
| 33 |
{ |
| 34 |
$namespace = self::_namespace(); |
| 35 |
|
| 36 |
register_rest_route($namespace, '/send-rating', array( |
| 37 |
array( |
| 38 |
'methods' => WP_REST_Server::EDITABLE, |
| 39 |
'callback' => array($this, 'send_rating'), |
| 40 |
'permission_callback' => '__return_true', |
| 41 |
'args' => [], |
| 42 |
), |
| 43 |
)); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Set Email Subject |
| 49 |
* By Default, subject will be "Weekly Reporting for NotificationX" |
| 50 |
* Admin can set Custom Subject from NotificationX Advanced Settings Panel |
| 51 |
* @return subject||String |
| 52 |
*/ |
| 53 |
public function email_subject() { |
| 54 |
$subject = __( "[IMPORTANT] New feedback received from a NotificationX user", 'notificationx' ); |
| 55 |
return $subject; |
| 56 |
} |
| 57 |
|
| 58 |
public function send_rating($request) |
| 59 |
{ |
| 60 |
update_option('nx_feedback_shared', true); |
| 61 |
$params = $request->get_params(); |
| 62 |
$rating = isset($params['rating']) ? intval($params['rating']) : null; |
| 63 |
$review = isset($params['review']) ? sanitize_text_field($params['review']) : ''; |
| 64 |
|
| 65 |
if (!$rating) { |
| 66 |
return new \WP_REST_Response(['message' => 'Invalid rating'], 400); |
| 67 |
} |
| 68 |
|
| 69 |
$to = self::$_mail_sendto; // Email recipient |
| 70 |
$subject = $this->email_subject(); |
| 71 |
|
| 72 |
$data = [ |
| 73 |
'rating' => $rating, |
| 74 |
'review' => $review |
| 75 |
]; |
| 76 |
|
| 77 |
$template = new EmailTemplate(); |
| 78 |
$message = $template->template_body($data, 'weekly'); // Pass data |
| 79 |
|
| 80 |
$headers = ['Content-Type: text/html; charset=UTF-8', "From: NotificationX <support@wpdeveloper.com>"]; |
| 81 |
|
| 82 |
// Send email |
| 83 |
$sent = wp_mail($to, $subject, $message, $headers); |
| 84 |
|
| 85 |
if ($sent) { |
| 86 |
return new \WP_REST_Response(['message' => 'Email sent successfully'], 200); |
| 87 |
} else { |
| 88 |
return new \WP_REST_Response(['message' => 'Failed to send email'], 500); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public static function _namespace(){ |
| 95 |
return self::$_namespace . '/v' . self::$_version; |
| 96 |
} |
| 97 |
} |