Initial commit
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace FluentMail\App\Http\Controllers;
|
||||
|
||||
use FluentMail\App\App;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
/**
|
||||
* @var \FluentMail\App\Plugin
|
||||
*/
|
||||
protected $app = null;
|
||||
|
||||
/**
|
||||
* @var \FluentMail\Includes\Request\Request
|
||||
*/
|
||||
protected $request = null;
|
||||
|
||||
/**
|
||||
* @var \FluentMail\Includes\Response\Response
|
||||
*/
|
||||
protected $response = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->app = App::getInstance();
|
||||
$this->request = $this->app['request'];
|
||||
$this->response = $this->app['response'];
|
||||
}
|
||||
|
||||
public function send($data = null, $code = 200)
|
||||
{
|
||||
return $this->response->send($data, $code);
|
||||
}
|
||||
|
||||
public function sendSuccess($data = null, $code = 200)
|
||||
{
|
||||
return $this->response->sendSuccess($data, $code);
|
||||
}
|
||||
|
||||
public function sendError($data = null, $code = 422)
|
||||
{
|
||||
return $this->response->sendError($data, $code);
|
||||
}
|
||||
|
||||
public function verify()
|
||||
{
|
||||
$permission = 'manage_options';
|
||||
if(!current_user_can($permission)) {
|
||||
wp_send_json_error([
|
||||
'message' => __('You do not have permission to do this action', 'fluent-smtp')
|
||||
]);
|
||||
die();
|
||||
}
|
||||
|
||||
$nonce = $this->request->get('nonce');
|
||||
if(!wp_verify_nonce($nonce, FLUENTMAIL)) {
|
||||
wp_send_json_error([
|
||||
'message' => __('Security Failed. Please reload the page', 'fluent-smtp')
|
||||
]);
|
||||
die();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace FluentMail\App\Http\Controllers;
|
||||
|
||||
use FluentMail\App\Models\Logger;
|
||||
use FluentMail\App\Services\Mailer\Manager;
|
||||
use FluentMail\App\Services\Reporting;
|
||||
use FluentMail\Includes\Request\Request;
|
||||
use FluentMail\Includes\Support\Arr;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
public function index(Logger $logger, Manager $manager)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$connections = $manager->getSettings('connections', []);
|
||||
|
||||
return $this->send([
|
||||
'stats' => $logger->getStats(),
|
||||
'settings_stat' => [
|
||||
'connection_counts' => count($connections),
|
||||
'active_senders' => count($manager->getSettings('mappings', [])),
|
||||
'auto_delete_days' => $manager->getSettings('misc.log_saved_interval_days'),
|
||||
'log_enabled' => $manager->getSettings('misc.log_emails')
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function getDayTimeStats()
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$lastDay = 0;
|
||||
if (isset($_REQUEST['last_day'])) {
|
||||
$lastDay = (int)$_REQUEST['last_day'];
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
if ($lastDay > 6) {
|
||||
$results = $wpdb->get_results("SELECT
|
||||
DAYNAME(created_at) AS day_of_week,
|
||||
HOUR(created_at) AS hour_of_day,
|
||||
COUNT(*) AS count
|
||||
FROM
|
||||
{$wpdb->prefix}fsmpt_email_logs
|
||||
WHERE
|
||||
created_at >= NOW() - INTERVAL {$lastDay} DAY
|
||||
GROUP BY
|
||||
DAYNAME(created_at),
|
||||
HOUR(created_at)
|
||||
ORDER BY
|
||||
FIELD(DAYNAME(created_at), 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'),
|
||||
HOUR(created_at)");
|
||||
} else {
|
||||
$results = $wpdb->get_results("SELECT
|
||||
DAYNAME(created_at) AS day_of_week,
|
||||
HOUR(created_at) AS hour_of_day,
|
||||
COUNT(*) AS count
|
||||
FROM
|
||||
{$wpdb->prefix}fsmpt_email_logs
|
||||
GROUP BY
|
||||
DAYNAME(created_at),
|
||||
HOUR(created_at)
|
||||
ORDER BY
|
||||
FIELD(DAYNAME(created_at), 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'),
|
||||
HOUR(created_at)");
|
||||
}
|
||||
|
||||
// Assuming $results is the array of records fetched from the database.
|
||||
$dataItems = [
|
||||
'Mon' => [], 'Tue' => [], 'Wed' => [], 'Thu' => [], 'Fri' => [], 'Sat' => [], 'Sun' => []
|
||||
];
|
||||
|
||||
$hours = ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00', '8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'];
|
||||
|
||||
foreach ($dataItems as $day => $data) {
|
||||
foreach ($hours as $hour) {
|
||||
$dataItems[$day][$hour] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($results as $row) {
|
||||
$day = substr($row->day_of_week, 0, 3); // Shorten 'Monday' to 'Mon', etc.
|
||||
$hour = $row->hour_of_day . ":00"; // Format hour as '0:00', '1:00', etc.
|
||||
$dataItems[$day][$hour] = (int)$row->count;
|
||||
}
|
||||
|
||||
return $this->send([
|
||||
'stats' => $dataItems
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
public function getSendingStats(Request $request, Reporting $reporting)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
list($from, $to) = $request->get('date_range');
|
||||
|
||||
return $this->send([
|
||||
'stats' => $reporting->getSendingStats($from, $to)
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
public function getDocs()
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$request = wp_remote_get('https://fluentsmtp.com/wp-json/wp/v2/docs?per_page=100');
|
||||
|
||||
$docs = json_decode(wp_remote_retrieve_body($request), true);
|
||||
|
||||
|
||||
$formattedDocs = [];
|
||||
|
||||
foreach ($docs as $doc) {
|
||||
$primaryCategory = Arr::get($doc, 'taxonomy_info.doc_category.0', ['value' => 'none', 'label' => 'Other']);
|
||||
$formattedDocs[] = [
|
||||
'title' => $doc['title']['rendered'],
|
||||
'content' => $doc['content']['rendered'],
|
||||
'link' => $doc['link'],
|
||||
'category' => $primaryCategory
|
||||
];
|
||||
}
|
||||
|
||||
return $this->send([
|
||||
'docs' => $formattedDocs
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace FluentMail\App\Http\Controllers;
|
||||
|
||||
use FluentMail\App\Models\Settings;
|
||||
use FluentMail\App\Services\NotificationHelper;
|
||||
use FluentMail\App\Services\Notification\Manager as NotificationManager;
|
||||
use FluentMail\Includes\Request\Request;
|
||||
use FluentMail\Includes\Support\Arr;
|
||||
|
||||
class DiscordController extends Controller
|
||||
{
|
||||
public function registerSite(Request $request)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$formData = $request->get('settings', []);
|
||||
|
||||
if (empty($formData['webhook_url'])) {
|
||||
return $this->sendError([
|
||||
'message' => __('Webhook URL is required', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
// validate the webhook URL
|
||||
$webhookUrl = Arr::get($formData, 'webhook_url');
|
||||
if (!filter_var($webhookUrl, FILTER_VALIDATE_URL)) {
|
||||
return $this->sendError([
|
||||
'message' => __('Please provide a valid Webhook URL', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
if (empty($formData['channel_name'])) {
|
||||
return $this->sendError([
|
||||
'message' => __('Channel Name required', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
NotificationHelper::updateChannelSettings('discord', [
|
||||
'status' => 'yes',
|
||||
'channel_name' => sanitize_text_field(Arr::get($formData, 'channel_name')),
|
||||
'webhook_url' => sanitize_url(Arr::get($formData, 'webhook_url')),
|
||||
]);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Your settings has been saved', 'fluent-smtp'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendTestMessage(Request $request)
|
||||
{
|
||||
// Let's update the notification status
|
||||
$settings = (new Settings())->notificationSettings();
|
||||
|
||||
if (Arr::get($settings, 'discord.status') != 'yes') {
|
||||
return $this->sendError([
|
||||
'message' => __('Slack notification is not enabled', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
$message = 'This is a test message for ' . site_url() . '. If you get this message, then your site is connected successfully.';
|
||||
|
||||
$result = NotificationHelper::sendDiscordMessage($message, Arr::get($settings, 'discord.webhook_url'));
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return $this->sendError([
|
||||
'message' => $result->get_error_message(),
|
||||
'errors' => $result->get_error_data(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Test message sent successfully', 'fluent-smtp'),
|
||||
'server_response' => $result
|
||||
]);
|
||||
}
|
||||
|
||||
public function disconnect()
|
||||
{
|
||||
NotificationHelper::updateChannelSettings('discord', [
|
||||
'status' => 'no',
|
||||
'webhook_url' => '',
|
||||
'channel_name' => ''
|
||||
]);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Discord connection has been disconnected successfully', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace FluentMail\App\Http\Controllers;
|
||||
|
||||
use FluentMail\App\Models\Logger;
|
||||
use FluentMail\Includes\Request\Request;
|
||||
|
||||
class LoggerController extends Controller
|
||||
{
|
||||
public function get(Request $request, Logger $logger)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
return $this->send(
|
||||
$logger->get(
|
||||
$request->except(['nonce', 'action'])
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function show(Request $request, Logger $logger)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$result = $logger->navigate($request->all());
|
||||
|
||||
return $this->sendSuccess($result);
|
||||
}
|
||||
|
||||
public function delete(Request $request, Logger $logger)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$id = (array) $request->get('id');
|
||||
|
||||
$logger->delete($id);
|
||||
|
||||
if ($id && $id[0] == 'all') {
|
||||
$subject = 'All logs';
|
||||
} else {
|
||||
$count = count($id);
|
||||
$subject = $count > 1 ? "{$count} Logs" : 'Log';
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => sprintf(__('%s deleted successfully.', 'fluent-smtp'), $subject)
|
||||
]);
|
||||
}
|
||||
|
||||
public function retry(Request $request, Logger $logger)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
try {
|
||||
$this->app->addAction('wp_mail_failed', function($response) use ($logger, $request) {
|
||||
$log = $logger->find($id = $request->get('id'));
|
||||
$log['retries'] = $log['retries'] + 1;
|
||||
$logger->updateLog($log, ['id' => $id]);
|
||||
|
||||
return $this->sendError([
|
||||
'message' => $response->get_error_message(),
|
||||
'errors' => $response->get_error_data()
|
||||
], $response->get_error_code());
|
||||
});
|
||||
|
||||
if ($email = $logger->resendEmailFromLog($request->get('id'), $request->get('type'))) {
|
||||
return $this->sendSuccess([
|
||||
'email' => $email,
|
||||
'message' => __('Email sent successfully.', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
throw new \Exception(esc_html__('Something went wrong', 'fluent-smtp'), 400);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return $this->sendError([
|
||||
'message' => $e->getMessage()
|
||||
], $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
public function retryBulk(Request $request, Logger $logger)
|
||||
{
|
||||
$this->verify();
|
||||
$logIds = $request->get('log_ids', []);
|
||||
|
||||
$failedCount = 0;
|
||||
$this->app->addAction('wp_mail_failed', function($response) use (&$failedCount) {
|
||||
$failedCount++;
|
||||
});
|
||||
|
||||
$failedInitiated = 0;
|
||||
$successCount = 0;
|
||||
foreach ($logIds as $logId) {
|
||||
try {
|
||||
$email = $logger->resendEmailFromLog($logId, 'check_realtime');
|
||||
$successCount++;
|
||||
} catch (\Exception $exception) {
|
||||
$failedInitiated++;
|
||||
}
|
||||
}
|
||||
$message = __('Selected Emails have been proceed to send.', 'fluent-smtp');
|
||||
|
||||
if ($failedCount) {
|
||||
$message .= sprintf(__(' But %d emails are reported to failed to send.', 'fluent-smtp'), $failedCount);
|
||||
}
|
||||
|
||||
if ($failedInitiated) {
|
||||
$message .= sprintf(__(' And %d emails are failed to init the emails', 'fluent-smtp'), $failedInitiated);
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => $message
|
||||
]);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace FluentMail\App\Http\Controllers;
|
||||
|
||||
use FluentMail\App\Models\Settings;
|
||||
use FluentMail\App\Services\NotificationHelper;
|
||||
use FluentMail\Includes\Request\Request;
|
||||
use FluentMail\Includes\Support\Arr;
|
||||
|
||||
class PushoverController extends Controller
|
||||
{
|
||||
public function registerSite(Request $request)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$formData = $request->get('settings', []);
|
||||
|
||||
if (empty($formData['api_token'])) {
|
||||
return $this->sendError([
|
||||
'message' => __('API Token is required', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
if (empty($formData['user_key'])) {
|
||||
return $this->sendError([
|
||||
'message' => __('User Key is required', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
NotificationHelper::updateChannelSettings('pushover', [
|
||||
'status' => 'yes',
|
||||
'api_token' => sanitize_text_field(Arr::get($formData, 'api_token')),
|
||||
'user_key' => sanitize_text_field(Arr::get($formData, 'user_key')),
|
||||
]);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Your settings has been saved', 'fluent-smtp'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendTestMessage(Request $request)
|
||||
{
|
||||
$settings = (new Settings())->notificationSettings();
|
||||
|
||||
if (Arr::get($settings, 'pushover.status') != 'yes') {
|
||||
return $this->sendError([
|
||||
'message' => __('Pushover notification is not enabled', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
$result = NotificationHelper::sendTestPushoverMessage(
|
||||
Arr::get($settings, 'pushover.api_token'),
|
||||
Arr::get($settings, 'pushover.user_key')
|
||||
);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return $this->sendError([
|
||||
'message' => $result->get_error_message(),
|
||||
'errors' => $result->get_error_data(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Test message sent successfully', 'fluent-smtp'),
|
||||
'server_response' => $result
|
||||
]);
|
||||
}
|
||||
|
||||
public function disconnect()
|
||||
{
|
||||
NotificationHelper::updateChannelSettings('pushover', [
|
||||
'status' => 'no',
|
||||
'api_token' => '',
|
||||
'user_key' => ''
|
||||
]);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Pushover connection has been disconnected successfully', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,728 @@
|
||||
<?php
|
||||
|
||||
namespace FluentMail\App\Http\Controllers;
|
||||
|
||||
use Exception;
|
||||
use FluentMail\App\Models\Settings;
|
||||
use FluentMail\App\Services\Notification\Manager as NotificationManager;
|
||||
use FluentMail\Includes\Request\Request;
|
||||
use FluentMail\Includes\Support\Arr;
|
||||
use FluentMail\Includes\Support\ValidationException;
|
||||
use FluentMail\App\Services\Mailer\Providers\Factory;
|
||||
|
||||
class SettingsController extends Controller
|
||||
{
|
||||
public function index(Settings $settings)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
try {
|
||||
$setting = $settings->get();
|
||||
|
||||
return $this->sendSuccess([
|
||||
'settings' => $setting
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
return $this->sendError([
|
||||
'message' => $e->getMessage()
|
||||
], $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
public function validate(Request $request, Settings $settings, Factory $factory)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
try {
|
||||
$data = $request->except(['action', 'nonce']);
|
||||
|
||||
$provider = $factory->make($data['provider']['key']);
|
||||
|
||||
$provider->validateBasicInformation($data);
|
||||
|
||||
$this->sendSuccess();
|
||||
} catch (ValidationException $e) {
|
||||
$this->sendError($e->errors(), $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
public function store(Request $request, Settings $settings, Factory $factory)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$passWordKeys = ['password', 'access_key', 'secret_key', 'api_key', 'client_id', 'client_secret', 'auth_token', 'access_token', 'refresh_token'];
|
||||
|
||||
try {
|
||||
$data = $request->except(['action', 'nonce']);
|
||||
|
||||
$data = wp_unslash($data);
|
||||
|
||||
$provider = $factory->make($data['connection']['provider']);
|
||||
|
||||
$connection = $data['connection'];
|
||||
|
||||
foreach ($connection as $index => $value) {
|
||||
if ($index == 'sender_email') {
|
||||
$connection['sender_email'] = sanitize_email($connection['sender_email']);
|
||||
}
|
||||
|
||||
if (in_array($index, $passWordKeys)) {
|
||||
if ($value) {
|
||||
$connection[$index] = trim($value);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_string($value) && $value) {
|
||||
$connection[$index] = sanitize_text_field($value);
|
||||
}
|
||||
}
|
||||
|
||||
$data['connection'] = $connection;
|
||||
|
||||
$this->validateConnection($provider, $connection);
|
||||
|
||||
$provider->checkConnection($connection);
|
||||
|
||||
$data['valid_senders'] = $provider->getValidSenders($connection);
|
||||
|
||||
$data = apply_filters('fluentmail_saving_connection_data', $data, $data['connection']['provider']);
|
||||
|
||||
$settings->store($data);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Settings saved successfully.', 'fluent-smtp'),
|
||||
'connections' => $settings->getConnections(),
|
||||
'mappings' => $settings->getMappings(),
|
||||
'misc' => $settings->getMisc()
|
||||
]);
|
||||
} catch (ValidationException $e) {
|
||||
return $this->sendError($e->errors(), 422);
|
||||
} catch (Exception $e) {
|
||||
return $this->sendError([
|
||||
'message' => $e->getMessage()
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
|
||||
public function storeMiscSettings(Request $request, Settings $settings)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$misc = $request->get('settings');
|
||||
$settings->updateMiscSettings($misc);
|
||||
$this->sendSuccess([
|
||||
'message' => __('General Settings has been updated', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
public function delete(Request $request, Settings $settings)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$settings = $settings->delete($request->get('key'));
|
||||
|
||||
return $this->sendSuccess($settings);
|
||||
}
|
||||
|
||||
public function storeGlobals(Request $request, Settings $settings)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$settings->saveGlobalSettings(
|
||||
$data = $request->except(['action', 'nonce'])
|
||||
);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'form' => $data,
|
||||
'message' => __('Settings saved successfully.', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendTestEmil(Request $request, Settings $settings)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
try {
|
||||
$this->app->addAction('wp_mail_failed', [$this, 'onFail']);
|
||||
|
||||
$data = $request->except(['action', 'nonce']);
|
||||
|
||||
if (!isset($data['email'])) {
|
||||
return $this->sendError([
|
||||
'email_error' => __('The email field is required.', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
if (!defined('FLUENTMAIL_EMAIL_TESTING')) {
|
||||
define('FLUENTMAIL_EMAIL_TESTING', true);
|
||||
}
|
||||
|
||||
$settings->sendTestEmail($data, $settings->get());
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Email delivered successfully.', 'fluent-smtp')
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
return $this->sendError([
|
||||
'message' => $e->getMessage()
|
||||
], $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
public function onFail($response)
|
||||
{
|
||||
return $this->sendError([
|
||||
'message' => $response->get_error_message(),
|
||||
'errors' => $response->get_error_data()
|
||||
], 422);
|
||||
}
|
||||
|
||||
public function validateConnection($provider, $connection)
|
||||
{
|
||||
$errors = [];
|
||||
|
||||
try {
|
||||
$provider->validateBasicInformation($connection);
|
||||
} catch (ValidationException $e) {
|
||||
$errors = $e->errors();
|
||||
}
|
||||
|
||||
try {
|
||||
$provider->validateProviderInformation($connection);
|
||||
} catch (ValidationException $e) {
|
||||
$errors = array_merge($errors, $e->errors());
|
||||
}
|
||||
|
||||
if ($errors) {
|
||||
throw new ValidationException(esc_html__('Unprocessable Entity', 'fluent-smtp'), 422, null, $errors); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
|
||||
}
|
||||
}
|
||||
|
||||
public function getConnectionInfo(Request $request, Settings $settings, Factory $factory)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$connectionId = $request->get('connection_id');
|
||||
$connections = $settings->getConnections();
|
||||
|
||||
if (!isset($connections[$connectionId]['provider_settings'])) {
|
||||
return $this->sendSuccess([
|
||||
'info' => __('Sorry no connection found. Please reload the page and try again', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
$connection = $connections[$connectionId]['provider_settings'];
|
||||
|
||||
$provider = $factory->make($connection['provider']);
|
||||
|
||||
return $this->sendSuccess($provider->getConnectionInfo($connection));
|
||||
}
|
||||
|
||||
public function addNewSenderEmail(Request $request, Settings $settings, Factory $factory)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$connectionId = $request->get('connection_id');
|
||||
$connections = $settings->getConnections();
|
||||
|
||||
if (!isset($connections[$connectionId]['provider_settings'])) {
|
||||
return $this->sendSuccess([
|
||||
'info' => __('Sorry no connection found. Please reload the page and try again', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
$connection = $connections[$connectionId]['provider_settings'];
|
||||
|
||||
$provider = $factory->make($connection['provider']);
|
||||
$email = sanitize_email($request->get('new_sender'));
|
||||
|
||||
if (!is_email($email)) {
|
||||
return $this->sendError([
|
||||
'message' => __('Please provide a valid email address', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
$result = $provider->addNewSenderEmail($connection, $email);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return $this->sendError([
|
||||
'message' => $result->get_error_message()
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Email has been added successfully', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
public function removeSenderEmail(Request $request, Settings $settings, Factory $factory)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$connectionId = $request->get('connection_id');
|
||||
$connections = $settings->getConnections();
|
||||
|
||||
if (!isset($connections[$connectionId]['provider_settings'])) {
|
||||
return $this->sendSuccess([
|
||||
'info' => __('Sorry no connection found. Please reload the page and try again', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
$connection = $connections[$connectionId]['provider_settings'];
|
||||
|
||||
$provider = $factory->make($connection['provider']);
|
||||
$email = sanitize_email($request->get('email'));
|
||||
|
||||
if (!is_email($email)) {
|
||||
return $this->sendError([
|
||||
'message' => __('Please provide a valid email address', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
$result = $provider->removeSenderEmail($connection, $email);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return $this->sendError([
|
||||
'message' => $result->get_error_message()
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Email has been removed successfully', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
public function installPlugin(Request $request)
|
||||
{
|
||||
$this->verify();
|
||||
$pluginSlug = $request->get('plugin_slug');
|
||||
$plugin = [
|
||||
'name' => $pluginSlug,
|
||||
'repo-slug' => $pluginSlug,
|
||||
'file' => $pluginSlug . '.php'
|
||||
];
|
||||
|
||||
$UrlMaps = [
|
||||
'fluentform' => [
|
||||
'admin_url' => admin_url('admin.php?page=fluent_forms'),
|
||||
'title' => __('Go to Fluent Forms Dashboard', 'fluent-smtp')
|
||||
],
|
||||
'fluent-crm' => [
|
||||
'admin_url' => admin_url('admin.php?page=fluentcrm-admin'),
|
||||
'title' => __('Go to FluentCRM Dashboard', 'fluent-smtp')
|
||||
],
|
||||
'ninja-tables' => [
|
||||
'admin_url' => admin_url('admin.php?page=ninja_tables#/'),
|
||||
'title' => __('Go to Ninja Tables Dashboard', 'fluent-smtp')
|
||||
]
|
||||
];
|
||||
|
||||
if (!isset($UrlMaps[$pluginSlug]) || !wp_is_file_mod_allowed('install_plugins')) {
|
||||
$this->sendError([
|
||||
'message' => __('Sorry, You can not install this plugin', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->backgroundInstaller($plugin);
|
||||
$this->send([
|
||||
'message' => __('Plugin has been successfully installed.', 'fluent-smtp'),
|
||||
'info' => $UrlMaps[$pluginSlug]
|
||||
]);
|
||||
} catch (\Exception $exception) {
|
||||
$this->sendError([
|
||||
'message' => $exception->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function backgroundInstaller($plugin_to_install)
|
||||
{
|
||||
if (!empty($plugin_to_install['repo-slug'])) {
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
|
||||
WP_Filesystem();
|
||||
|
||||
$skin = new \Automatic_Upgrader_Skin();
|
||||
$upgrader = new \WP_Upgrader($skin);
|
||||
$installed_plugins = array_keys(\get_plugins());
|
||||
$plugin_slug = $plugin_to_install['repo-slug'];
|
||||
$plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php';
|
||||
$installed = false;
|
||||
$activate = false;
|
||||
|
||||
// See if the plugin is installed already.
|
||||
if (isset($installed_plugins[$plugin_file])) {
|
||||
$installed = true;
|
||||
$activate = !is_plugin_active($installed_plugins[$plugin_file]);
|
||||
}
|
||||
|
||||
// Install this thing!
|
||||
if (!$installed) {
|
||||
// Suppress feedback.
|
||||
ob_start();
|
||||
|
||||
try {
|
||||
$plugin_information = plugins_api(
|
||||
'plugin_information',
|
||||
array(
|
||||
'slug' => $plugin_slug,
|
||||
'fields' => array(
|
||||
'short_description' => false,
|
||||
'sections' => false,
|
||||
'requires' => false,
|
||||
'rating' => false,
|
||||
'ratings' => false,
|
||||
'downloaded' => false,
|
||||
'last_updated' => false,
|
||||
'added' => false,
|
||||
'tags' => false,
|
||||
'homepage' => false,
|
||||
'donate_link' => false,
|
||||
'author_profile' => false,
|
||||
'author' => false,
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
if (is_wp_error($plugin_information)) {
|
||||
throw new \Exception(wp_kses_post($plugin_information->get_error_message()));
|
||||
}
|
||||
|
||||
$package = $plugin_information->download_link;
|
||||
$download = $upgrader->download_package($package);
|
||||
|
||||
if (is_wp_error($download)) {
|
||||
throw new \Exception(wp_kses_post($download->get_error_message()));
|
||||
}
|
||||
|
||||
$working_dir = $upgrader->unpack_package($download, true);
|
||||
|
||||
if (is_wp_error($working_dir)) {
|
||||
throw new \Exception(wp_kses_post($working_dir->get_error_message()));
|
||||
}
|
||||
|
||||
$result = $upgrader->install_package(
|
||||
array(
|
||||
'source' => $working_dir,
|
||||
'destination' => WP_PLUGIN_DIR,
|
||||
'clear_destination' => false,
|
||||
'abort_if_destination_exists' => false,
|
||||
'clear_working' => true,
|
||||
'hook_extra' => array(
|
||||
'type' => 'plugin',
|
||||
'action' => 'install',
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
throw new \Exception(wp_kses_post($result->get_error_message()));
|
||||
}
|
||||
|
||||
$activate = true;
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception(esc_html($e->getMessage()));
|
||||
}
|
||||
|
||||
// Discard feedback.
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
wp_clean_plugins_cache();
|
||||
|
||||
// Activate this thing.
|
||||
if ($activate) {
|
||||
try {
|
||||
$result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
throw new \Exception(esc_html($result->get_error_message()));
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception(esc_html($e->getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function subscribe()
|
||||
{
|
||||
$this->verify();
|
||||
$email = sanitize_text_field($_REQUEST['email']); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
$displayName = '';
|
||||
|
||||
if (isset($_REQUEST['display_name'])) {
|
||||
$displayName = sanitize_text_field($_REQUEST['display_name']);
|
||||
}
|
||||
|
||||
if (!is_email($email)) {
|
||||
return $this->sendError([
|
||||
'message' => __('Sorry! The provider email is not valid', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
$shareEssentials = 'no';
|
||||
|
||||
if ($_REQUEST['share_essentials'] == 'yes') {
|
||||
update_option('_fluentsmtp_sub_update', 'shared', 'no');
|
||||
$shareEssentials = 'yes';
|
||||
} else {
|
||||
update_option('_fluentsmtp_sub_update', 'yes', 'no');
|
||||
}
|
||||
|
||||
$this->pushData($email, $shareEssentials, $displayName);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('You are subscribed to plugin update and monthly tips', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
public function subscribeDismiss()
|
||||
{
|
||||
$this->verify();
|
||||
update_option('_fluentsmtp_dismissed_timestamp', time(), 'no');
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => 'success'
|
||||
]);
|
||||
}
|
||||
|
||||
private function pushData($optinEmail, $shareEssentials, $displayName = '')
|
||||
{
|
||||
$user = get_user_by('ID', get_current_user_id());
|
||||
|
||||
$url = 'https://fluentsmtp.com/wp-admin/?fluentcrm=1&route=contact&hash=6012116c-90d8-42a5-a65b-3649aa34b356';
|
||||
|
||||
|
||||
if (!$displayName) {
|
||||
$displayName = trim($user->first_name . ' ' . $user->last_name);
|
||||
if (!$displayName) {
|
||||
$displayName = $user->display_name;
|
||||
}
|
||||
}
|
||||
|
||||
wp_remote_post($url, [
|
||||
'body' => json_encode([ // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
|
||||
'full_name' => $displayName,
|
||||
'email' => $optinEmail,
|
||||
'source' => 'smtp',
|
||||
'optin_website' => site_url(),
|
||||
'share_essential' => $shareEssentials
|
||||
])
|
||||
]);
|
||||
}
|
||||
|
||||
public function getGmailAuthUrl(Request $request)
|
||||
{
|
||||
$this->verify();
|
||||
$connection = wp_unslash($request->get('connection'));
|
||||
|
||||
$clientId = Arr::get($connection, 'client_id');
|
||||
$clientSecret = Arr::get($connection, 'client_secret');
|
||||
|
||||
if (Arr::get($connection, 'key_store') == 'wp_config') {
|
||||
if (defined('FLUENTMAIL_GMAIL_CLIENT_ID')) {
|
||||
$clientId = FLUENTMAIL_GMAIL_CLIENT_ID;
|
||||
} else {
|
||||
return $this->sendError([
|
||||
'client_id' => [
|
||||
'required' => __('Please define FLUENTMAIL_GMAIL_CLIENT_ID in your wp-config.php file', 'fluent-smtp')
|
||||
]
|
||||
]);
|
||||
}
|
||||
if (defined('FLUENTMAIL_GMAIL_CLIENT_SECRET')) {
|
||||
$clientSecret = FLUENTMAIL_GMAIL_CLIENT_SECRET;
|
||||
} else {
|
||||
return $this->sendError([
|
||||
'client_secret' => [
|
||||
'required' => __('Please define FLUENTMAIL_GMAIL_CLIENT_SECRET in your wp-config.php file', 'fluent-smtp')
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$clientId) {
|
||||
return $this->sendError([
|
||||
'client_id' => [
|
||||
'required' => __('Please provide application client id', 'fluent-smtp')
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
if (!$clientSecret) {
|
||||
return $this->sendError([
|
||||
'client_secret' => [
|
||||
'required' => __('Please provide application client secret', 'fluent-smtp')
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
$authUrl = add_query_arg([
|
||||
'response_type' => 'code',
|
||||
'access_type' => 'offline',
|
||||
'client_id' => $clientId,
|
||||
'redirect_uri' => apply_filters('fluentsmtp_gapi_callback', 'https://fluentsmtp.com/gapi/'),
|
||||
'state' => admin_url('options-general.php?page=fluent-mail&gapi=1'),
|
||||
'scope' => 'https://mail.google.com/',
|
||||
'approval_prompt' => 'force',
|
||||
'include_granted_scopes' => 'true'
|
||||
], 'https://accounts.google.com/o/oauth2/auth');
|
||||
|
||||
return $this->sendSuccess([
|
||||
'auth_url' => filter_var($authUrl, FILTER_SANITIZE_URL)
|
||||
]);
|
||||
}
|
||||
|
||||
public function getOutlookAuthUrl(Request $request)
|
||||
{
|
||||
$this->verify();
|
||||
$connection = wp_unslash($request->get('connection'));
|
||||
|
||||
$clientId = Arr::get($connection, 'client_id');
|
||||
$clientSecret = Arr::get($connection, 'client_secret');
|
||||
|
||||
delete_option('_fluentsmtp_intended_outlook_info');
|
||||
|
||||
if (Arr::get($connection, 'key_store') == 'wp_config') {
|
||||
if (defined('FLUENTMAIL_OUTLOOK_CLIENT_ID')) {
|
||||
$clientId = FLUENTMAIL_OUTLOOK_CLIENT_ID;
|
||||
} else {
|
||||
return $this->sendError([
|
||||
'client_id' => [
|
||||
'required' => __('Please define FLUENTMAIL_OUTLOOK_CLIENT_ID in your wp-config.php file', 'fluent-smtp')
|
||||
]
|
||||
]);
|
||||
}
|
||||
if (defined('FLUENTMAIL_OUTLOOK_CLIENT_SECRET')) {
|
||||
$clientSecret = FLUENTMAIL_OUTLOOK_CLIENT_SECRET;
|
||||
} else {
|
||||
return $this->sendError([
|
||||
'client_secret' => [
|
||||
'required' => __('Please define FLUENTMAIL_OUTLOOK_CLIENT_SECRET in your wp-config.php file', 'fluent-smtp')
|
||||
]
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
update_option('_fluentsmtp_intended_outlook_info', [
|
||||
'client_id' => $clientId,
|
||||
'client_secret' => $clientSecret
|
||||
]);
|
||||
}
|
||||
|
||||
if (!$clientId) {
|
||||
return $this->sendError([
|
||||
'client_id' => [
|
||||
'required' => __('Please provide application client id', 'fluent-smtp')
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
if (!$clientSecret) {
|
||||
return $this->sendError([
|
||||
'client_secret' => [
|
||||
'required' => __('Please provide application client secret', 'fluent-smtp')
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'auth_url' => (new \FluentMail\App\Services\Mailer\Providers\Outlook\API($clientId, $clientSecret))->getAuthUrl()
|
||||
]);
|
||||
}
|
||||
|
||||
public function getNotificationSettings()
|
||||
{
|
||||
$settings = (new Settings())->notificationSettings();
|
||||
$this->verify();
|
||||
|
||||
$settings['telegram_notify_token'] = '';
|
||||
|
||||
return $this->sendSuccess([
|
||||
'settings' => $settings
|
||||
]);
|
||||
}
|
||||
|
||||
public function saveNotificationSettings(Request $request)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$settings = $request->get('settings', []);
|
||||
|
||||
$settings = Arr::only($settings, ['enabled', 'notify_email', 'notify_days']);
|
||||
|
||||
$settings['notify_email'] = sanitize_text_field($settings['notify_email']);
|
||||
$settings['enabled'] = sanitize_text_field($settings['enabled']);
|
||||
|
||||
$defaults = [
|
||||
'enabled' => 'no',
|
||||
'notify_email' => '{site_admin}',
|
||||
'notify_days' => ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
||||
];
|
||||
|
||||
$oldSettings = (new Settings())->notificationSettings();
|
||||
$defaults = wp_parse_args($defaults, $oldSettings);
|
||||
|
||||
$settings = wp_parse_args($settings, $defaults);
|
||||
|
||||
update_option('_fluent_smtp_notify_settings', $settings, false);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Settings has been updated successfully', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
public function getNotificationChannels()
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$notificationManager = new NotificationManager();
|
||||
$channels = $notificationManager->getAllChannels();
|
||||
$settings = (new Settings())->notificationSettings();
|
||||
$activeChannel = Arr::get($settings, 'active_channel', []);
|
||||
|
||||
// Add status and active state to each channel
|
||||
$channelsWithStatus = [];
|
||||
foreach ($channels as $key => $channel) {
|
||||
$channelSettings = Arr::get($settings, $key, []);
|
||||
$channelsWithStatus[$key] = array_merge($channel, [
|
||||
'status' => Arr::get($channelSettings, 'status', 'no'),
|
||||
'is_active' => in_array($key, $activeChannel),
|
||||
'settings' => $channelSettings
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'channels' => $channelsWithStatus,
|
||||
'active_channel' => $activeChannel
|
||||
]);
|
||||
}
|
||||
|
||||
public function toggleNotificationChannel(Request $request)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$channelKeys = $request->get('channel_keys', []);
|
||||
$channelKeys = array_map('sanitize_text_field', $channelKeys);
|
||||
$allChanelKeys = (new NotificationManager())->getAllChannelKeys();
|
||||
$channelKeys = array_filter($channelKeys, function ($key) use ($allChanelKeys) {
|
||||
return in_array($key, $allChanelKeys);
|
||||
});
|
||||
|
||||
$settings = (new Settings())->notificationSettings();
|
||||
|
||||
$settings['active_channel'] = $channelKeys;
|
||||
|
||||
update_option('_fluent_smtp_notify_settings', $settings, false);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Notification channel updated successfully', 'fluent-smtp'),
|
||||
'active_channels' => $channelKeys
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace FluentMail\App\Http\Controllers;
|
||||
|
||||
use FluentMail\App\Models\Settings;
|
||||
use FluentMail\App\Services\NotificationHelper;
|
||||
use FluentMail\Includes\Request\Request;
|
||||
use FluentMail\Includes\Support\Arr;
|
||||
|
||||
class SlackController extends Controller
|
||||
{
|
||||
public function registerSite(Request $request)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$formData = $request->get('settings', []);
|
||||
|
||||
$userEmail = sanitize_email(Arr::get($formData, 'user_email'));
|
||||
|
||||
if (!is_email($userEmail)) {
|
||||
return $this->sendError([
|
||||
'message' => __('Please provide a valid email address', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
$nonce = wp_create_nonce('fluent_smtp_slack_register_site');
|
||||
|
||||
$payload = [
|
||||
'admin_email' => $userEmail,
|
||||
'smtp_url' => admin_url('options-general.php?_slacK_nonce=' . $nonce . '&page=fluent-mail#/'),
|
||||
'site_url' => site_url(),
|
||||
'site_title' => get_bloginfo('name'),
|
||||
'site_lang' => get_bloginfo('language'),
|
||||
];
|
||||
|
||||
|
||||
$activationData = NotificationHelper::registerSlackSite($payload);
|
||||
|
||||
if (is_wp_error($activationData)) {
|
||||
return $this->sendError([
|
||||
'message' => $activationData->get_error_message(),
|
||||
'errors' => $activationData->get_error_data(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
NotificationHelper::updateChannelSettings('slack', [
|
||||
'status' => 'pending',
|
||||
'token' => Arr::get($activationData, 'site_token'),
|
||||
'redirect_url' => ''
|
||||
]);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Awesome! You are redirecting to slack', 'fluent-smtp'),
|
||||
'redirect_url' => Arr::get($activationData, 'redirect_url')
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendTestMessage(Request $request)
|
||||
{
|
||||
// Let's update the notification status
|
||||
$settings = (new Settings())->notificationSettings();
|
||||
|
||||
if (Arr::get($settings, 'slack.status') != 'yes') {
|
||||
return $this->sendError([
|
||||
'message' => __('Slack notification is not enabled', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
$message = 'This is a test message for ' . site_url() . '. If you get this message, then your site is connected successfully.';
|
||||
|
||||
$result = NotificationHelper::sendSlackMessage($message, Arr::get($settings, 'slack.webhook_url'));
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return $this->sendError([
|
||||
'message' => $result->get_error_message(),
|
||||
'errors' => $result->get_error_data(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Test message sent successfully', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
public function disconnect()
|
||||
{
|
||||
NotificationHelper::updateChannelSettings('slack', [
|
||||
'status' => 'no',
|
||||
'webhook_url' => '',
|
||||
'token' => ''
|
||||
]);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Slack connection has been disconnected successfully', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace FluentMail\App\Http\Controllers;
|
||||
|
||||
use FluentMail\App\Models\Settings;
|
||||
use FluentMail\App\Services\NotificationHelper;
|
||||
use FluentMail\App\Services\Notification\Manager as NotificationManager;
|
||||
use FluentMail\Includes\Request\Request;
|
||||
use FluentMail\Includes\Support\Arr;
|
||||
|
||||
class TelegramController extends Controller
|
||||
{
|
||||
public function issuePinCode(Request $request)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$formData = $request->get('settings', []);
|
||||
|
||||
$userEmail = sanitize_email(Arr::get($formData, 'user_email'));
|
||||
|
||||
if (!is_email($userEmail)) {
|
||||
return $this->sendError([
|
||||
'message' => __('Please provide a valid email address', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'admin_email' => $userEmail,
|
||||
'smtp_url' => admin_url('options-general.php?page=fluent-mail#/'),
|
||||
'site_url' => site_url(),
|
||||
'site_title' => get_bloginfo('name'),
|
||||
'site_lang' => get_bloginfo('language'),
|
||||
];
|
||||
|
||||
|
||||
$activationData = NotificationHelper::issueTelegramPinCode($payload);
|
||||
|
||||
if (is_wp_error($activationData)) {
|
||||
return $this->sendError([
|
||||
'message' => $activationData->get_error_message(),
|
||||
'errors' => $activationData->get_error_data(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Awesome! Please activate the connection from your telegram account.', 'fluent-smtp'),
|
||||
'site_token' => Arr::get($activationData, 'site_token'),
|
||||
'site_pin' => Arr::get($activationData, 'site_pin'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function confirmConnection(Request $request)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$siteToken = $request->get('site_token', '');
|
||||
|
||||
if (empty($siteToken)) {
|
||||
return $this->sendError([
|
||||
'message' => __('Please provide site token', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
|
||||
$connectionInfo = NotificationHelper::getTelegramConnectionInfo($siteToken);
|
||||
|
||||
if (is_wp_error($connectionInfo)) {
|
||||
return $this->sendError([
|
||||
'message' => $connectionInfo->get_error_message(),
|
||||
'errors' => $connectionInfo->get_error_data(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
NotificationHelper::updateChannelSettings('telegram', [
|
||||
'status' => 'yes',
|
||||
'token' => $siteToken
|
||||
]);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'success' => true,
|
||||
'message' => __('Connection successful', 'fluent-smtp'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function getTelegramConnectionInfo(Request $request)
|
||||
{
|
||||
$this->verify();
|
||||
|
||||
$settings = (new Settings())->notificationSettings();
|
||||
|
||||
if (Arr::get($settings, 'telegram.status') != 'yes') {
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Telegram notification is not enabled', 'fluent-smtp'),
|
||||
'telegram_notify_status' => 'no'
|
||||
], 200);
|
||||
}
|
||||
|
||||
$siteToken = Arr::get($settings, 'telegram.token');
|
||||
|
||||
$connectionInfo = NotificationHelper::getTelegramConnectionInfo($siteToken);
|
||||
|
||||
if (is_wp_error($connectionInfo)) {
|
||||
return $this->sendSuccess([
|
||||
'telegram_notify_status' => 'failed',
|
||||
'message' => $connectionInfo->get_error_message(),
|
||||
'errors' => $connectionInfo->get_error_data(),
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'telegram_notify_status' => 'yes',
|
||||
'telegram_receiver' => Arr::get($connectionInfo, 'telegram_receiver', []),
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendTestMessage(Request $request)
|
||||
{
|
||||
// Let's update the notification status
|
||||
$settings = (new Settings())->notificationSettings();
|
||||
|
||||
if (Arr::get($settings, 'telegram.status') != 'yes') {
|
||||
return $this->sendError([
|
||||
'message' => __('Telegram notification is not enabled', 'fluent-smtp')
|
||||
], 422);
|
||||
}
|
||||
|
||||
$result = NotificationHelper::sendTestTelegramMessage(Arr::get($settings, 'telegram.token'));
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return $this->sendError([
|
||||
'message' => $result->get_error_message(),
|
||||
'errors' => $result->get_error_data(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Test message sent successfully', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
|
||||
public function disconnect()
|
||||
{
|
||||
$settings = (new Settings())->notificationSettings();
|
||||
|
||||
$token = Arr::get($settings, 'telegram.token');
|
||||
|
||||
// Only call disconnect API if we have a token
|
||||
if ($token) {
|
||||
NotificationHelper::disconnectTelegram($token);
|
||||
}
|
||||
|
||||
NotificationHelper::updateChannelSettings('telegram', [
|
||||
'status' => 'no',
|
||||
'token' => ''
|
||||
]);
|
||||
|
||||
return $this->sendSuccess([
|
||||
'message' => __('Telegram connection has been disconnected successfully', 'fluent-smtp')
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user