Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Helpers/
?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server
<?php
namespace KS_PAC_DCFH\Helpers;
if (!defined('ABSPATH')) {
exit;
}
class LogUtils
{
private static $_instance;
private string $post_type = 'pwh_dcfh_log';
private string $taxonomy_type = 'pwh_dcfh_log_taxonomies';
public static function instance(): self
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
public function load(): void
{
add_action('init', [$this, 'maybe_register_logtype'], 1);
}
public function maybe_register_logtype(): void
{
register_post_type($this->post_type, [
'labels' => ['name' => __('Logs', 'divi-contact-form-helper')],
'public' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'show_ui' => false,
'query_var' => false,
'rewrite' => false,
'capability_type' => 'post',
'supports' => ['title', 'editor'],
'can_export' => false,
]);
register_taxonomy($this->taxonomy_type, $this->post_type, ['public' => false]);
}
public function add_log(string $title, string $message, string $log_category, int $parent = 0, array $log_meta = [])
{
return $this->save_log([
'post_type' => $this->post_type,
'post_title' => $title,
'post_content' => $message,
'post_parent' => $parent,
'log_category' => $log_category,
'post_status' => 'private',
'post_author' => get_current_user_id(),
], $log_meta);
}
public function update_log(array $log_data, array $log_meta = [])
{
if (empty($log_data['ID'])) {
return new \WP_Error('missing_id', __('Log ID is required for update.', 'divi-contact-form-helper'));
}
return $this->save_log($log_data, $log_meta, true);
}
private function save_log(array $log_data, array $log_meta = [], bool $is_update = false)
{
$method = $is_update ? 'wp_update_post' : 'wp_insert_post';
$log_id = $method($log_data, true);
if (is_wp_error($log_id)) {
return $log_id;
}
$this->add_category($log_id, $log_data['log_category'] ?? '');
$this->update_meta($log_id, $log_meta);
return $log_id;
}
private function add_category(int $log_id, string $log_category): void
{
if (!empty($log_category)) {
wp_set_object_terms($log_id, sanitize_text_field($log_category), $this->taxonomy_type);
}
}
private function update_meta(int $log_id, array $log_meta): void
{
foreach ($log_meta as $key => $value) {
if (!empty($value)) {
update_post_meta($log_id, '_pwh_dcfh_log_'.sanitize_key($key), sanitize_text_field($value));
}
}
}
public function get_logs(string $log_category, int $parent = 0, int $paged = 0): array
{
return $this->fetch_logs([
'post_parent' => $parent,
'paged' => $paged,
'log_category' => $log_category,
]);
}
public function delete_logs(string $log_category, int $parent = 0, array $meta_query = []): void
{
$logs = $this->fetch_logs([
'post_parent' => $parent,
'posts_per_page' => -1,
'fields' => 'ids',
'log_category' => $log_category,
'meta_query' => $meta_query,
]);
foreach ($logs as $log_id) {
wp_delete_post($log_id, true);
}
}
private function fetch_logs(array $args): array
{
$defaults = [
'post_type' => $this->post_type,
'posts_per_page' => 20,
'post_status' => 'private',
'paged' => get_query_var('paged'),
];
$query_args = wp_parse_args($args, $defaults);
if (!empty($query_args['log_category'])) {
$query_args['tax_query'] = [
[
'taxonomy' => $this->taxonomy_type,
'field' => 'slug',
'terms' => sanitize_text_field($query_args['log_category']),
]
];
}
return get_posts($query_args) ?: [];
}
public function get_log_count(string $log_category, int $parent = 0, array $meta_query = [], array $date_query = []): int
{
return count($this->fetch_logs([
'post_parent' => $parent,
'posts_per_page' => -1,
'fields' => 'ids',
'log_category' => $log_category,
'meta_query' => $meta_query,
'date_query' => $date_query,
]));
}
}