?? GreyFile — Mystic File Browser

Current path: home/webdevt/www/cryptoimpot.fr/wp-content/plugins/divi-contact-form-helper/app/Admin/Handlers/



?? Go up: /home/webdevt/www/cryptoimpot.fr/wp-content/plugins/divi-contact-form-helper/app/Admin

?? Viewing: Post_Meta_Handler.php

<?php

namespace KS_PAC_DCFH\Admin\Handlers;

class Post_Meta_Handler
{
    private static $_instance;

    public static function instance(): Post_Meta_Handler
    {
        if (self::$_instance === null) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    public static function get_post_meta($post_id, $meta_key, $single = true)
    {
        return get_post_meta($post_id, $meta_key, $single);
    }

    public static function get_page_id_meta_value($post_id)
    {
        return self::get_post_meta($post_id, '_pwh_dcfh_page_id');
    }

    public static function get_contact_form_id_meta_value($post_id)
    {
        return self::get_post_meta($post_id, '_pwh_dcfh_contact_form_id');
    }

    public static function get_contact_form_fields_meta_value($post_id)
    {
        return self::get_post_meta($post_id, '_pwh_dcfh_form_fields');
    }

    public static function get_contact_email_meta_value($post_id)
    {
        return self::get_post_meta($post_id, '_pwh_dcfh_contact_email');
    }

    public static function get_read_by_meta_value($post_id)
    {
        return self::get_post_meta($post_id, '_pwh_dcfh_read_by');
    }

    public static function get_ip_address_meta_value($post_id)
    {
        return self::get_post_meta($post_id, '_pwh_dcfh_ip_address');
    }

    public static function get_user_agent_meta_value($post_id)
    {
        return self::get_post_meta($post_id, '_pwh_dcfh_user_agent');
    }

    public static function get_referer_url_meta_value($post_id)
    {
        return self::get_post_meta($post_id, '_pwh_dcfh_referer_url');
    }

    public static function get_clones_meta_value($post_id)
    {
        return self::get_post_meta($post_id, '_pwh_dcfh_clone_log');
    }

    public static function update_cloned_history($post_id, $cloned_post_id): void
    {
        if (!ks_pac_dcfh_db()::is_setting_enabled('pwh_dcfh_enable_clone_log')) {
            return;
        }
        $old_meta = self::get_clones_meta_value($post_id);
        if (empty($old_meta)) {
            $old_meta = [];
        }
        $new_meta = [
            'post_id' => $cloned_post_id,
            'author' => get_current_user_id(),
            'date' => current_time('mysql'),
        ];
        $old_meta[] = $new_meta;
        update_post_meta($post_id, '_pwh_dcfh_clone_log', $old_meta);
    }

    public static function get_attachment_id_by_name($name): string
    {
        global $wpdb;
        $name = pathinfo($name, PATHINFO_FILENAME);

        return $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = '%s'", $name));  // phpcs:ignore
    }

    public static function update_post_status_and_modified_date($post_id, $post_status = 'draft', $post_modified = '0000-00-00 00:00:00', $post_modified_gmt = '0000-00-00 00:00:00'): void
    {
        global $wpdb;
        $wpdb->update($wpdb->posts, ['post_status' => $post_status, 'post_modified' => $post_modified, 'post_modified_gmt' => $post_modified_gmt], ['post_status' => 'pwh_dcfh', 'ID' => $post_id], ['%s', '%s', '%s'], ['%d']); //db call ok; no-cache ok
    }

    public static function is_spam($email): bool
    {
        global $wpdb;

        return (bool)$wpdb->get_var($wpdb->prepare("SELECT EXISTS(SELECT 1 FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id WHERE pm.meta_key = %s AND pm.meta_value = %s AND p.post_status = 'spam' LIMIT 1)", '_pwh_dcfh_contact_email', $email)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    }

    public static function update_posts_status_to_spam($email, $post_type = 'pwh_dcfh')
    {
        global $wpdb;

        return $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET post_status = %s WHERE ID IN (SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_pwh_dcfh_contact_email' AND meta_value = %s) AND post_type = %s AND post_status IN (%s, %s)", 'spam', strtolower($email), $post_type, 'private', 'draft')); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    }

    public static function update_spam_posts_status($email, $post_status, $post_type = 'pwh_dcfh')
    {
        global $wpdb;

        return $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts AS p JOIN $wpdb->postmeta AS pm ON p.ID = pm.post_id SET p.post_status = %s WHERE pm.meta_key = '_pwh_dcfh_contact_email' AND pm.meta_value = %s AND p.post_type = %s AND p.post_status = 'spam'", $post_status, strtolower($email), $post_type)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    }

    public static function get_contact_form_last_post_id($contact_form_id): int
    {
        global $wpdb;
        $count = $wpdb->get_var($wpdb->prepare("SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_value LIKE %s ORDER BY meta_id DESC", $contact_form_id)); //db call ok; no-cache ok
        if ($count === null) {
            return 0;
        }

        return (int)$count;
    }

    public static function get_contact_form_total_entries($contact_form_id): int
    {
        global $wpdb;
        $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) AS total_entries FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value LIKE %s", '_pwh_dcfh_contact_form_id', $wpdb->esc_like($contact_form_id))); //db call ok; no-cache ok
        if ($count === null) {
            return 0;
        }

        return (int)$count;
    }

    public static function get_contact_form_send_email_data($post_id): void
    {
        $fields_string = ks_pac_dcfh_post_meta()::get_contact_form_fields_meta_value($post_id);
        $output = "<ul>";
        if (!empty($fields_string)) {
            $fields = explode(',', $fields_string);
            foreach ($fields as $field) {
                $field_id = $field;
                $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>';
        echo force_balance_tags($output);  // phpcs:ignore
    }

    public static function is_user_ip_exists($contact_form_id): bool
    {
        global $wpdb;
        $ip_address = ks_pac_dcfh_general_helper()::get_ip_address();
        $result = $wpdb->get_var($wpdb->prepare(" SELECT COUNT(*) AS count FROM $wpdb->postmeta AS pm1 INNER JOIN $wpdb->postmeta AS pm2 ON pm1.post_id = pm2.post_id WHERE (pm1.meta_key = '_pwh_dcfh_contact_form_id' AND pm1.meta_value = %s) AND (pm2.meta_key = '_pwh_dcfh_ip_address' AND pm2.meta_value = %s)", $contact_form_id, $ip_address));// db call ok; no-cache ok

        return (int)$result > 0;
    }

    public function get_post_type_terms(): void
    {
        if (!empty($_REQUEST['_wpnonce'])) {
            $_wpnonce = sanitize_text_field($_REQUEST['_wpnonce']);
            if (wp_verify_nonce($_wpnonce, 'admin-ajax-nonce')) {
                $post_type = isset($_REQUEST['post_type']) ? sanitize_text_field($_REQUEST['post_type']) : null;
                if ('' !== $post_type) {
                    if ($post_type !== 'page') {
                        $taxonomies = get_object_taxonomies($post_type);
                        $taxonomies_new = [];
                        foreach ($taxonomies as $taxonomy) {
                            if (!strpos($taxonomy, 'tag')) {
                                $taxonomies_new[] = $taxonomy;
                            }
                        }
                        $terms = get_terms([
                            'taxonomy' => $taxonomies_new,
                            'hide_empty' => false,
                        ]);
                        if (!is_wp_error($terms)) {
                            $output = "";
                            foreach ($terms as $term) {
                                $output .= "<option value=\"$term->term_id\">".$term->name."</option>";
                            }
                            wp_send_json_success($output);
                            wp_die();
                        }
                    } else {
                        wp_send_json_error('No data');
                        wp_die();
                    }
                }
            } else {
                wp_send_json_error('Nope! Security check failed!', '400');
                wp_die();
            }
        }
    }

    public function get_post_meta_keys(): void
    {
        if (!empty($_REQUEST['_wpnonce'])) {
            $_wpnonce = sanitize_text_field($_REQUEST['_wpnonce']);
            if (wp_verify_nonce($_wpnonce, 'admin-ajax-nonce')) {
                $search_meta = isset($_REQUEST['search_meta']) ? sanitize_text_field($_REQUEST['search_meta']) : null;
                if ('' !== $search_meta) {
                    $list = [];
                    global $wpdb;
                    // phpcs:ignore
                    $meta_keys = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE meta_key LIKE %s AND meta_key NOT LIKE '%_pwh_dcfh_%' AND meta_key NOT LIKE '%_edit%' AND meta_key NOT LIKE '%_menu%' AND meta_key NOT LIKE '%_et_%' AND meta_key NOT LIKE '%_thumbnail%'",
                        '%'.$wpdb->esc_like($search_meta).'%'));
                    if (!empty($meta_keys)) {
                        foreach ($meta_keys as $meta_key) {
                            $list[] = $meta_key->meta_key;
                        }
                    }
                    array_filter($list, 'strlen');
                    wp_send_json_success($list);
                }
            }
        }
    }

    public static function get_contact_forms_dashboard_stats()
    {
        $dashboard_stats = get_transient('dcfh_dashboard_stats');
        // phpcs:disable
        if (false === $dashboard_stats) {
            global $wpdb;
            $query = "
            SELECT 
                CASE
                    WHEN DATE(wpp.post_date) = CURDATE() THEN 'today'
                    WHEN DATE(wpp.post_date) = DATE_SUB(CURDATE(), INTERVAL 1 DAY) THEN 'yesterday'
                    WHEN DATE(wpp.post_date) BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 WEEK) AND CURDATE() THEN 'last_week'
                    WHEN DATE(wpp.post_date) BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND CURDATE() THEN 'last_month'
                    ELSE 'other' 
                END AS time_period,
                COUNT(wpp.ID) AS total_posts,
                wppm.meta_value AS contact_form_id
            FROM $wpdb->posts AS wpp
            LEFT JOIN $wpdb->postmeta wppm 
                ON wppm.post_id = wpp.ID
            WHERE DATE(wpp.post_date) BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND CURDATE()
              AND wpp.post_type = 'pwh_dcfh'
              AND wppm.meta_key = '_pwh_dcfh_contact_form_id'
            GROUP BY time_period, wppm.meta_value
            ORDER BY wppm.meta_value
        ";
            $results = $wpdb->get_results($query);
            if (!empty($results)) {
                foreach ($results as $result) {
                    if (!empty($result->contact_form_id)) {
                        $contact_form_id = ks_pac_dcfh_db()::get_contact_form_title($result->contact_form_id);
                        $dashboard_stats[$contact_form_id][$result->time_period] = $result->total_posts;
                    }
                }
                set_transient('dcfh_dashboard_stats', $dashboard_stats, 2 * HOUR_IN_SECONDS);
            }
        }

        // phpcs:enable
        return $dashboard_stats;
    }
}


??

??