Current path: home/webdevt/www/cryptoimpot.fr/wp-content/plugins/divi-contact-form-helper/app/Handlers/
?? Go up: /home/webdevt/www/cryptoimpot.fr/wp-content/plugins/divi-contact-form-helper/app
<?php
namespace KS_PAC_DCFH\Handlers;
class DB_Handler
{
private static $_instance;
public static function instance(): self
{
if (self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}
public static function is_setting_enabled($key): bool
{
$options = get_option(ks_pac_dcfh_wp_helper()::get_theme_option_name());
return isset($options[$key]) && 'on' === $options[$key];
}
public static function get_setting($key, $default = ''): string
{
$options = get_option(ks_pac_dcfh_wp_helper()::get_theme_option_name());
return $options[$key] ?? $default;
}
public static function update_contact_form_details($unique_id, $args): void
{
$option_key = "_pwh_dcfh_contact_form_settings_$unique_id";
$old_options = get_option($option_key);
$options = [
'unique_id' => $unique_id,
'title' => $args['title'],
'page_id' => get_the_ID(),
'entries_threshold' => $args['threshold'],
];
$options = wp_parse_args($options, $old_options);
if (!ks_pac_dcfh_general_helper()::is_robot()) {
$options['views'] = isset($old_options['views']) ? $old_options['views'] + 1 : 1;
$ip_address = ks_pac_dcfh_general_helper()::get_ip_address();
$cookie_name = 'pwh_dcfh_uniqueviews_'.md5($ip_address).'_'.$unique_id;
if (!isset($_COOKIE[$cookie_name])
&& false === ks_pac_dcfh_post_meta()::is_user_ip_exists($unique_id)) {
$options['unique_views'] = isset($old_options['unique_views']) ? $old_options['unique_views'] + 1 : 1;
}
}
update_option($option_key, $options, 'no');
}
public static function update_contact_form_query_params($unique_id, $query_params): void
{
$query_params = wp_list_pluck($query_params, 'label', 'field_id');
$option_key = "_pwh_dcfh_contact_form_settings_$unique_id";
$options = get_option($option_key);
if ($options !== false) {
if (!isset($options['query_params'])) {
$options['query_params'] = [];
}
$new_query_params = wp_parse_args($query_params, $options['query_params']);
$new_query_params = array_unique($new_query_params);
$options['query_params'] = $new_query_params;
update_option($option_key, $options, 'no');
}
}
public static function update_contact_form_total_entries($unique_id, $entries): void
{
$option_key = "_pwh_dcfh_contact_form_settings_$unique_id";
$options = get_option($option_key);
if ($options !== false) {
if (!isset($options['total_entries']) || $options['total_entries'] !== $entries) {
$options['total_entries'] = $entries;
update_option($option_key, $options, 'no');
}
}
}
public static function reset_contact_form_views($unique_id): void
{
$option_key = "_pwh_dcfh_contact_form_settings_$unique_id";
$options = get_option($option_key);
$options['views'] = 0;
$options['unique_views'] = 0;
update_option($option_key, $options, 'no');
}
public static function delete_contact_form_options($unique_id): void
{
$option_key = "_pwh_dcfh_contact_form_settings_$unique_id";
delete_option($option_key);
}
public static function get_contact_form_title($unique_id)
{
$option_key = "_pwh_dcfh_contact_form_settings_$unique_id";
$options = get_option($option_key);
if ($options !== false && isset($options['title'])) {
return $options['title'];
}
return $unique_id;
}
public static function get_contact_forms_list()
{
$key = 'dcfh_contact_forms_'.md5(__FUNCTION__);
$contact_forms = wp_cache_get($key);
if ($contact_forms === false) {
$contact_forms = []; // Initialize as an array to avoid the deprecated behavior
global $wpdb;
$results = $wpdb->get_results($wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name LIKE %s", '_pwh_dcfh_contact_form_settings_%')); // db call ok; no-cache ok
if (!empty($results)) {
foreach ($results as $result) {
$options = maybe_unserialize($result->option_value);
if (!empty($options['unique_id'])) {
$contact_form = [
'unique_id' => $options['unique_id'],
'title' => $options['title'] ?? '',
'page_id' => $options['page_id'] ?? 0,
'entries_threshold' => $options['entries_threshold'] ?? '-1',
'total_entries' => $options['total_entries'] ?? 0,
'views' => $options['views'] ?? 0,
'unique_views' => $options['unique_views'] ?? 0,
];
$contact_forms[$options['unique_id']] = $contact_form;
}
}
wp_cache_set($key, $contact_forms, 'dcfh_cached_queries', 2 * MINUTE_IN_SECONDS);
}
}
return $contact_forms;
}
public static function get_contact_form_csv_data()
{
$key = 'dcfh_contact_form_csv_'.md5(__FUNCTION__);
$cached_data = wp_cache_get($key);
if (!$cached_data) {
$cached_data = []; // Initialize as an array to avoid the deprecated behavior
$contact_forms = self::get_contact_forms_list();
if (!empty($contact_forms)) {
foreach ($contact_forms as $contact_form) {
$contact_form_id = $contact_form['unique_id'];
$contact_form_title = $contact_form['title'];
$total_rows = $contact_form['total_entries'];
$form_entries = self::get_latest_form_submission_data($contact_form_id);
if (!empty($form_entries)) {
$output = '<h4>'.__('Form Fields', 'divi-contact-form-helper').'</h4>';
$output .= "<ul>";
foreach ($form_entries as $form_entry) {
$field_id = $form_entry['id'];
$field_label = $form_entry['label'];
$output .= sprintf(
"<li><input type='checkbox' id='%s' name='contact_form_fields[%s]' value='%s' checked>".
"<label for='%s'>%s</label></li>",
esc_attr($field_id),
esc_attr($field_id),
esc_attr($field_label),
esc_attr($field_id),
esc_html($field_label)
);
}
$output .= "</ul>";
$output .= '<h4>'.__('Meta Fields', 'divi-contact-form-helper').'</h4>';
$output .= "<ul>";
foreach (ks_pac_dcfh_constant()::form_placeholders() as $key => $value) {
$output .= sprintf(
"<li><input type='checkbox' id='%s' name='contact_form_fields[%s]' value='%s' checked><label for='%s'>%s</label></li>",
esc_attr($key),
esc_attr($key),
esc_attr($value),
esc_attr($key),
esc_html($value)
);
}
$output .= "</ul>";
$cached_data[$contact_form_id] = [
'contact_form_id' => $contact_form_id,
'contact_form_title' => $contact_form_title,
'contact_form_fields_html' => $output,
'contact_form_total_entries' => number_format_i18n($total_rows)
];
wp_cache_set($key, $cached_data, 'dcfh_cached_queries', 2 * MINUTE_IN_SECONDS);
}
}
}
}
return $cached_data;
}
public static function get_contact_form_template_data()
{
$key = 'dcfh_contact_form_template_'.md5(__FUNCTION__);
$cached_data = wp_cache_get($key);
if (!$cached_data) {
$cached_data = [];
$contact_forms = self::get_contact_forms_list();
if (!empty($contact_forms)) {
foreach ($contact_forms as $contact_form) {
$contact_form_id = $contact_form['unique_id'];
$contact_form_title = $contact_form['title'];
$total_rows = $contact_form['total_entries'];
$form_entries = self::get_latest_form_submission_data($contact_form_id);
if (!empty($form_entries)) {
$output = "<ul>";
foreach ($form_entries as $form_entry) {
$field_id = $form_entry['id'];
$output .= sprintf("<li class='keyword'>%%%%%s%%%%</li>", $field_id);
}
foreach (ks_pac_dcfh_constant()::site_placeholders() as $placeholder) {
$output .= sprintf("<li class='keyword'>%%%%%s%%%%</li>", $placeholder);
}
$output .= "</ul>";
$cached_data[$contact_form_id] = [
'contact_form_id' => $contact_form_id,
'contact_form_title' => $contact_form_title,
'contact_form_fields_html' => $output,
'contact_form_total_entries' => number_format_i18n($total_rows)
];
wp_cache_set($key, $cached_data, 'dcfh_cached_queries', 2 * MINUTE_IN_SECONDS);
}
}
}
}
return $cached_data;
}
public static function get_latest_form_submission_data(string $contact_form_id): array
{
$cache_key = 'latest_form_submission_'.md5($contact_form_id);
$cached_data = wp_cache_get($cache_key, 'dcfh_cached_queries');
if ($cached_data !== false) {
return $cached_data;
}
global $wpdb;
$data = [];
$post_excerpt = $wpdb->get_var(
$wpdb->prepare(
"SELECT p.post_excerpt
FROM $wpdb->posts p
INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id
WHERE p.post_type = 'pwh_dcfh'
AND p.post_status != 'trash'
AND pm.meta_key = '_pwh_dcfh_contact_form_id'
AND pm.meta_value = %s
ORDER BY p.post_date DESC
LIMIT 1",
$contact_form_id
)
);
if (!empty($post_excerpt) && is_serialized($post_excerpt)) {
$post_excerpt = ks_pac_dcfh_general_helper()::maybe_unserialize($post_excerpt);
if (!empty($post_excerpt)) {
$data = $post_excerpt;
}
}
// Cache the result for 15 minutes
wp_cache_set($cache_key, $data, 'dcfh_cached_queries', 15 * MINUTE_IN_SECONDS);
return $data;
}
public static function dynamic_entry_columns(): array
{
$columns = [];
$option = et_get_option('pwh_dcfh_entry_list_columns', 'name');
if (!empty($option) && is_string($option)) {
$option = preg_replace('/\s*,\s*/', ',', $option);
$columns = explode(',', strtolower($option));
$columns = array_filter($columns, 'strlen');
}
return $columns;
}
}