Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Admin/Dashboard/
?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Admin
<?php
namespace KS_PAC_DCFH\Admin\Dashboard;
use WP_Error;
use WP_REST_Request;
use WP_REST_Server;
if (!defined('ABSPATH')) {
exit;
}
class DashboardAPI
{
private string $namespace = 'dcfh/v1';
private string $plugin_dir_path;
private string $base_api_url;
private string $transient_preffix;
private string $settings_name;
public function load(): void
{
$this->settings_name = KS_PAC_DCFH_SETTINGS_OPTION;
add_action('rest_api_init', [$this, 'register_endpoints'], 11);
}
public function register_endpoints()
{
$this->transient_preffix = $this->namespace;
$this->plugin_dir_path = plugin_dir_path(KS_PAC_DCFH_PLUGIN_FILE);
$this->base_api_url = KS_PAC_DCFH_BASE_API_URL;
register_rest_route($this->namespace, '/plugins', [
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_installed_plugins_info'],
'permission_callback' => [$this, 'permission_check']
]);
register_rest_route($this->namespace, '/plugins/activate', [
'methods' => WP_REST_Server::CREATABLE,
'callback' => [$this, 'activate_plugin'],
'permission_callback' => [$this, 'permission_check']
]);
register_rest_route($this->namespace, '/changelog', [
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_current_plugin_changelog'],
'permission_callback' => [$this, 'permission_check']
]);
register_rest_route($this->namespace, '/plugin-news', [
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_current_plugin_news'],
'permission_callback' => [$this, 'permission_check']
]);
register_rest_route($this->namespace, '/newsletter', [
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_newsletter'],
'permission_callback' => [$this, 'permission_check']
]);
register_rest_route($this->namespace, '/feature-request', [
'methods' => WP_REST_Server::CREATABLE,
'callback' => [$this, 'send_feature_request'],
'permission_callback' => [$this, 'permission_check']
]);
register_rest_route($this->namespace, '/get-plugin-settings', [
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_plugin_settings'],
'permission_callback' => [$this, 'permission_check']
]);
register_rest_route($this->namespace, '/save-plugin-settings', [
'methods' => WP_REST_Server::CREATABLE,
'callback' => [$this, 'save_plugin_settings'],
'permission_callback' => [$this, 'permission_check']
]);
register_rest_route($this->namespace, '/export-settings', [
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'export_settings'],
'permission_callback' => [$this, 'permission_check'],
'args' => [
'filename' => [
'required' => false,
'sanitize_callback' => 'sanitize_text_field',
],
],
]);
register_rest_route($this->namespace, '/import-settings', [
'methods' => WP_REST_Server::CREATABLE,
'callback' => [$this, 'import_settings'],
'permission_callback' => [$this, 'permission_check'],
'args' => [
'data' => [
'required' => false,
],
],
]);
register_rest_route($this->namespace, '/reset-settings', [
'methods' => WP_REST_Server::CREATABLE,
'callback' => [$this, 'reset_settings'],
'permission_callback' => [$this, 'permission_check'],
]);
register_rest_route($this->namespace, '/send-test-email', [
'methods' => 'POST',
'callback' => [$this, 'send_test_email'],
'permission_callback' => [$this, 'permission_check'],
'args' => [
'email' => [
'required' => true,
'sanitize_callback' => 'sanitize_email',
],
],
]);
register_rest_route($this->namespace, '/clear-ics-cache', [
'methods' => WP_REST_Server::CREATABLE,
'callback' => [$this, 'clear_ics_cache'],
'permission_callback' => [$this, 'permission_check'],
]);
register_rest_route($this->namespace, '/entry-logs', [
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_entry_logs'],
'permission_callback' => [$this, 'permission_check'],
]);
register_rest_route($this->namespace, '/delete-entry-logs', [
'methods' => WP_REST_Server::DELETABLE,
'callback' => [$this, 'delete_entry_logs'],
'permission_callback' => [$this, 'permission_check'],
]);
}
public function get_installed_plugins_info(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid nonce.', 'divi-contact-form-helper'),
['status' => 403]
);
}
$plugins_map = [
'divi-assistant' => 'Divi Assistant',
'divi-dynamic-helper' => 'Divi Dynamic Content Helper',
'divi-contact-form-helper' => 'Divi Contact Form Helper',
'divi-custom-fields-helper' => 'Divi Custom Fields Helper',
'divi-event-calendar-module' => 'Divi Events Calendar',
'divi-taxonomy-helper' => 'Divi Taxonomy Helper',
'divi-search-helper' => 'Divi Search Helper',
'divi-table-of-contents-maker' => 'Divi Table Of Contents Maker',
'divi-carousel-maker' => 'Divi Carousel Maker',
'divi-responsive-helper' => 'Divi Responsive Helper',
'divi-social-sharing-buttons' => 'Divi Social Sharing Buttons Maker',
'divi-timer-pro' => 'Divi Timer Pro',
'divi-tabs-maker' => 'Divi Tabs Maker',
'divi-builder-experience-helper' => 'Divi Builder Experience Helper',
];
$data = array_map(fn($title) => [
'title' => $title,
'status' => 'notinstalled',
], $plugins_map);
$all_plugins = get_plugins();
foreach ($all_plugins as $plugin_file => $plugin_info) {
$slug = strtok($plugin_file, '/');
if (isset($data[$slug])) {
$data[$slug]['status'] = is_plugin_active($plugin_file) ? 'active' : 'inactive';
$data[$slug]['key'] = $plugin_file;
}
}
return rest_ensure_response($data);
}
public function activate_plugin(WP_REST_Request $request)
{
$plugin_file = sanitize_text_field($request->get_param('plugin_file'));
$plugin_path = WP_PLUGIN_DIR.'/'.$plugin_file;
if (!file_exists($plugin_path)) {
return new WP_Error(
'plugin_not_found',
__('Plugin file does not exist.', 'divi-contact-form-helper'),
['status' => 404]
);
}
if (is_plugin_active($plugin_file)) {
return rest_ensure_response([
'success' => true,
'message' => __('Plugin is already active.', 'divi-contact-form-helper'),
]);
}
$result = activate_plugin($plugin_file);
if (is_wp_error($result)) {
return rest_ensure_response([
'success' => false,
'message' => $result->get_error_message(),
]);
}
return rest_ensure_response([
'success' => true,
'message' => __('Plugin activated successfully.', 'divi-contact-form-helper'),
]);
}
public function get_current_plugin_changelog(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid nonce.', 'divi-contact-form-helper'),
['status' => 403]
);
}
$points = [];
$changelog = [];
$fs = et_()->WPFS();
$fh = $fs->get_contents($this->plugin_dir_path.'/readme.txt');
if ($fh === false) {
return rest_ensure_response(null);
}
$lines = explode("\n", $fh);
$start = array_search('== Changelog ==', $lines);
if ($start !== false) {
$lines = array_slice($lines, $start + 1);
}
$version = null;
$date = null;
foreach ($lines as $line) {
$line = trim($line);
if (preg_match('/^= ([0-9.]+) =$/', $line, $matches)) {
$version = $matches[1];
} elseif (preg_match('/^= ([0-9]{2}-[0-9]{2}-[0-9]{4}) =$/', $line, $matches)) {
$date = $this->change_date_format($matches[1]);
} elseif (strpos($line, '*') === 0) {
$points[] = trim(ltrim($line, '* '));
} elseif ($line === '') {
if (!empty($version) || !empty($points)) {
$changelog[] = [
'version' => $version,
'date' => $date,
'points' => $points,
];
}
$version = null;
$date = null;
$points = [];
}
}
if (!empty($version) || !empty($points)) {
$changelog[] = [
'version' => $version,
'date' => $date,
'points' => $points,
];
}
return rest_ensure_response($changelog);
}
public function get_current_plugin_news(WP_REST_Request $request)
{
$transient_key = $this->transient_preffix.'_current_plugin_new';
$cached_data = get_transient($transient_key);
if ($cached_data !== false) {
return rest_ensure_response($cached_data);
}
$current_plugin_data = ks_pac_dcfh_get_plugin_data();
$response = wp_remote_get($this->base_api_url."/posts/{$current_plugin_data['pluginExtras']['PluginNews']}.json", ['timeout' => 15]);
if (is_wp_error($response)) {
return rest_ensure_response([
'success' => false,
'message' => 'Unable to fetch plugin news from the server.'
]);
}
if (wp_remote_retrieve_response_code($response) !== 200) {
return rest_ensure_response([
'success' => false,
'message' => 'Unexpected response from API.'
]);
}
$body = wp_remote_retrieve_body($response);
$news_data = json_decode($body, true);
if (!is_array($news_data) || empty($news_data)) {
return rest_ensure_response([
'success' => false,
'message' => 'No plugin news found.'
]);
}
set_transient($transient_key, $news_data, 12 * HOUR_IN_SECONDS);
return rest_ensure_response($news_data);
}
public function get_newsletter(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid nonce.', 'divi-contact-form-helper'),
['status' => 403]
);
}
$transient_key = $this->transient_preffix.'_newsletter';
$cached_data = get_transient($transient_key);
if ($cached_data !== false) {
return rest_ensure_response($cached_data);
}
$response = wp_remote_get($this->base_api_url.'/posts/all-posts.json', ['timeout' => 15]);
if (is_wp_error($response)) {
return rest_ensure_response([
'success' => false,
'message' => 'Unable to fetch newsletter data'
]);
}
if (wp_remote_retrieve_response_code($response) !== 200) {
return rest_ensure_response([
'success' => false,
'message' => 'Unexpected response from API'
]);
}
$body = wp_remote_retrieve_body($response);
$posts = json_decode($body, true);
if (!is_array($posts) || empty($posts)) {
return rest_ensure_response([
'success' => false,
'message' => 'No newsletter posts found'
]);
}
$newsletter_data = [];
foreach ($posts as $post) {
if (!empty($post['categories'])) {
$newsletter_data[] = $post;
if (count($newsletter_data) >= 6) {
break;
}
}
}
set_transient($transient_key, $newsletter_data, 12 * HOUR_IN_SECONDS);
return rest_ensure_response($newsletter_data);
}
public function send_feature_request(WP_REST_Request $request)
{
$current_user_data = ks_pac_dcfh_get_current_user_data();
$current_plugin_data = ks_pac_dcfh_get_plugin_data();
$from_name = sanitize_text_field($request->get_param('name'));
$title = sanitize_text_field($request->get_param('title'));
$message = wp_strip_all_tags($request->get_param('message'));
$from_email = sanitize_email($current_user_data['email']);
if (empty($from_name) || empty($title) || empty($message) || empty($from_email)) {
return rest_ensure_response([
'success' => false,
'message' => 'Missing required fields.',
]);
}
$headers = [
'From: '.$from_name." <$from_email>",
'Content-Type: text/plain; charset=UTF-8',
];
$subject = sprintf('%s Feature Request: %s', $current_plugin_data['Name'], $title);
$mail_sent = wp_mail('nelson@peeayecreative.com', $subject, $message, $headers);
return rest_ensure_response([
'success' => (bool)$mail_sent,
'message' => $mail_sent ? 'Feature request sent successfully.' : 'Failed to send email. Please try again later.',
]);
}
public function get_plugin_settings(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid nonce.', 'divi-contact-form-helper'),
['status' => 403]
);
}
$migrator = new DashboardSettingsMigration();
$defaults = $migrator->get_default_settings();
$settings = get_option($this->settings_name, $defaults);
$settings = wp_parse_args($settings, $defaults);
return rest_ensure_response($settings);
}
public function save_plugin_settings(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid nonce.', 'divi-contact-form-helper'),
['status' => 403]
);
}
$new_settings = (array)$request->get_param('plugin_settings');
$current_settings = (array)get_option($this->settings_name, []);
$updated_settings = array_merge($current_settings, $new_settings);
update_option($this->settings_name, $updated_settings);
return rest_ensure_response([
'success' => true,
'message' => __('Settings saved successfully!', 'divi-contact-form-helper'),
'data' => $updated_settings,
]);
}
public function export_settings(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid nonce.', 'divi-contact-form-helper'),
['status' => 403]
);
}
$filename = $request->get_param('filename');
if (empty($filename)) {
return rest_ensure_response([
'success' => false,
'message' => 'Filename is required.'
]);
}
$options = get_option($this->settings_name, []);
return rest_ensure_response([
'success' => true,
'message' => __('Settings saved successfully!', 'divi-contact-form-helper'),
'filename' => sanitize_file_name($filename).'.json',
'data' => $options,
]);
}
public function import_settings(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid nonce.', 'divi-contact-form-helper'),
['status' => 403]
);
}
$params = $request->get_json_params();
if (empty($params['data']) || !is_array($params['data'])) {
return rest_ensure_response([
'success' => false,
'message' => 'Invalid or missing data in import file.'
]);
}
$settings = $params['data'];
update_option($this->settings_name, $settings);
return rest_ensure_response([
'success' => true,
'message' => __('Settings imported successfully!', 'divi-contact-form-helper'),
]);
}
public function reset_settings(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid nonce.', 'divi-contact-form-helper'),
['status' => 403]
);
}
$migrator = new DashboardSettingsMigration();
$defaults = $migrator->get_default_settings();
$current_settings = get_option($this->settings_name, []);
$defaults['is_license_alert_dismissed'] = $current_settings['is_license_alert_dismissed'] ?? $defaults['is_license_alert_dismissed'];
update_option($this->settings_name, $defaults);
return rest_ensure_response([
'success' => true,
'message' => __('Settings reset successfully!', 'divi-contact-form-helper'),
]);
}
public function send_test_email(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!$nonce || !wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid security token. Please refresh the page and try again.', 'divi-contact-form-helper'),
['status' => 403]
);
}
$email = sanitize_email($request->get_param('email'));
if (empty($email) || !is_email($email)) {
return rest_ensure_response([
'success' => false,
'message' => 'Please provide a valid email address'
]);
}
$subject = __('Email Sending Test by Divi Contact Form Helper', 'divi-contact-form-helper');
$message = __(
'This is a test message sent by Divi Contact Form Helper to confirm that your website can send emails correctly. If you received this email, congratulations — your email sending configuration works!',
'divi-contact-form-helper'
);
$headers = ['Content-Type: text/plain; charset=UTF-8'];
$sent = wp_mail($email, $subject, $message, $headers);
if ($sent) {
return rest_ensure_response([
'success' => true,
'message' => sprintf(
__('Test email successfully sent to %s. Please check your inbox (and spam folder).', 'divi-contact-form-helper'),
esc_html($email)
),
]);
}
$error_message = get_transient('email_test_error_message');
if (!empty($error_message)) {
$error_message = make_clickable(esc_html($error_message));
} else {
$error_message = __('Failed to send the test email. Please verify your SMTP configuration and try again.', 'divi-contact-form-helper');
}
return rest_ensure_response([
'success' => false,
'message' => $error_message
]);
}
public function clear_ics_cache(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!$nonce || !wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid security token. Please refresh the page and try again.', 'divi-contact-form-helper'),
['status' => 403]
);
}
global $wpdb;
$like = $wpdb->esc_like('_transient_dcfh_calendar_').'%';
$transients = $wpdb->get_results($wpdb->prepare("SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", $like)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
foreach ($transients as $transient) {
$key = str_replace('_transient_', '', $transient->option_name);
delete_transient($key);
}
return rest_ensure_response([
'success' => true,
'message' => __('All ICS cached data has been cleared.', 'divi-contact-form-helper'),
]);
}
public function get_entry_logs(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!$nonce || !wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid security token. Please refresh the page and try again.', 'divi-contact-form-helper'),
['status' => 403]
);
}
$et_ = et_();
$file_path = realpath(WP_CONTENT_DIR.'/divi_contact_form.log');
if ($et_->WPFS()->exists($file_path) && $et_->WPFS()->is_readable($file_path)) {
$file_content = $et_->WPFS()->get_contents($file_path);
$bytes = $et_->WPFS()->size($file_path);
$units = ['bytes', 'KB', 'MB', 'GB', 'TB'];
$power = $bytes > 0 ? floor(log($bytes, 1024)) : 0;
$file_size = round($bytes / (1024 ** $power), 2).' '.$units[$power];
$log_content = '';
$lines = explode("\n", $file_content);
$log_count = 1;
foreach ($lines as $line) {
if (preg_match('/^\[(.*?)]/', $line, $matches)) {
$timestamp = date_i18n('Y-m-d H:i:sa', strtotime($matches[1]));
$log_content .= "\n------ Entry#$log_count Timestamp: $timestamp ------\n";
$log_count++;
} elseif (trim($line) === '') {
continue;
} else {
$log_content .= "$line\n";
}
}
return rest_ensure_response([
'success' => true,
'data' => $log_content,
]);
}
return rest_ensure_response([
'success' => true,
'data' => '',
'message' => __('No log file found.', 'divi-contact-form-helper'),
]);
}
public function delete_entry_logs(WP_REST_Request $request)
{
$nonce = $request->get_header('x_wp_nonce');
if (!$nonce || !wp_verify_nonce($nonce, 'wp_rest')) {
return new WP_Error(
'rest_forbidden',
__('Invalid security token. Please refresh the page and try again.', 'divi-contact-form-helper'),
['status' => 403]
);
}
$et_ = et_();
$file_path = realpath(WP_CONTENT_DIR.'/divi_contact_form.log');
if ($et_->WPFS()->exists($file_path) && $et_->WPFS()->is_readable($file_path)) {
$et_->WPFS()->delete($file_path);
return rest_ensure_response([
'success' => true,
'message' => __('Log deleted successfully!', 'divi-contact-form-helper'),
]);
}
return rest_ensure_response([
'success' => true,
'message' => __('No log file found to delete.', 'divi-contact-form-helper'),
]);
}
public function permission_check(): bool
{
return defined('KS_PAC_DCFH_IS_ADMIN_OR_EDITOR')
? KS_PAC_DCFH_IS_ADMIN_OR_EDITOR
: current_user_can('manage_options');
}
private function change_date_format($date): string
{
$date = explode("-", $date);
$months = [
'01' => 'January',
'02' => 'February',
'03' => 'March',
'04' => 'April',
'05' => 'May',
'06' => 'June',
'07' => 'July',
'08' => 'August',
'09' => 'September',
'10' => 'October',
'11' => 'November',
'12' => 'December',
];
if (isset($months[$date[0]])) {
$date[0] = $months[$date[0]];
}
if (isset($date[1])) {
$date[1] = $date[1].',';
}
return implode(" ", $date);
}
}