?? GreyFile — Mystic File Browser

Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Admin/CPT/



?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Admin

?? Viewing: EntryCPTManager.php

<?php

namespace KS_PAC_DCFH\Admin\CPT;

if (!defined('ABSPATH')) {
    exit;
}

class EntryCPTManager
{
    private string $post_type;

    private string $post_type_singular;

    private string $post_type_plural;

    private string $pagenow;

    public function load(): void
    {
        $this->post_type = 'pwh_dcfh';
        add_action('init', [$this, 'register_post_type'], 999);
        add_action('admin_menu', [$this, 'maybe_add_menu_links'], 9999);
        add_filter("manage_{$this->post_type}_posts_columns", [$this, 'maybe_column_header']);
        add_action("manage_{$this->post_type}_posts_custom_column", [$this, 'maybe_column_content'], 10, 2);
        add_filter("views_edit-$this->post_type", [$this, 'maybe_change_post_status_label']);
        add_action('load-post.php', [$this, 'maybe_update_post_modified_time']);
        add_filter('admin_title', [$this, 'maye_change_head_title'], 10, 2);
        add_action('admin_head', [$this, 'maye_change_post_title']);
        add_action('admin_init', [$this, 'maybe_deregister_script']);
        add_action('admin_menu', [$this, 'maybe_menu_hooks']);
        add_filter("manage_edit-{$this->post_type}_sortable_columns", [$this, 'maybe_sortable_columns']);
        add_action('current_screen', [$this, 'modifiy_admin_query']);
        add_action('restrict_manage_posts', [$this, 'maybe_manage_posts'], 10, 2);
        add_action('manage_posts_extra_tablenav', [$this, 'maybe_extra_table_nav']);
        add_filter('screen_options_show_screen', [$this, 'maybe_remove_screen_options'], 10, 2);
        add_action('before_delete_post', [$this, 'maybe_before_delete']);
        add_filter("bulk_actions-edit-$this->post_type", [$this, 'maybe_add_bulk_action']);
        add_action("handle_bulk_actions-edit-$this->post_type", [$this, 'maybe_process_bulk_actions'], 10, 3);
        add_filter('post_row_actions', [$this, 'maybe_custom_row_actions'], 10, 2);
        add_action('admin_init', [$this, 'maybe_process_custom_row_action']);
        add_action('admin_notices', [$this, 'maybe_custom_admin_message']);
        add_filter('bulk_post_updated_messages', [$this, 'maybe_bulk_update_message'], 10, 2);
    }

    public function register_post_type(): void
    {
        global $pagenow;
        $this->pagenow = $pagenow;
        $this->post_type_singular = _x('Entry', 'post type singular name', 'divi-contact-form-helper');
        $this->post_type_plural = _x('Entries', 'post type plural name', 'divi-contact-form-helper');
        $post_arguments = [
                'public' => false,
                'publicly_queryable' => false,
                'show_ui' => true,
                'show_in_menu' => true,
                'query_var' => true,
                'rewrite' => false,
                'capability_type' => $this->post_type,
                'capabilities' => ['create_posts' => 'do_not_allow'],
                'map_meta_cap' => true,
                'has_archive' => false,
                'hierarchical' => false,
                'can_export' => true,
                'show_in_rest' => false,
                'show_in_admin_bar' => true,
                'show_in_nav_menus' => true,
                'menu_position' => 20,
                'menu_icon' => 'dashicons-email',
                'supports' => ['author'],
                'labels' => [
                        'name' => $this->post_type_plural,
                        'singular_name' => $this->post_type_singular,
                        'edit_item' => __('Entry Details', 'divi-contact-form-helper'),
                        'all_items' => sprintf('%s', $this->post_type_plural),
                        'items_list' => sprintf(__('%s list', 'divi-contact-form-helper'), $this->post_type_plural),
                        'not_found' => sprintf(__('No %s found.', 'divi-contact-form-helper'), $this->post_type_plural),
                        'not_found_in_trash' => sprintf(__('No %s found in Trash.', 'divi-contact-form-helper'), $this->post_type_plural),
                        'search_items' => sprintf(__('Search %s', 'divi-contact-form-helper'), $this->post_type_plural),
                        'view_item' => sprintf(__('View %s', 'divi-contact-form-helper'), $this->post_type_singular),
                        'view_items' => sprintf(__('View %s', 'divi-contact-form-helper'), $this->post_type_plural),
                        'menu_name' => __('Contact Form', 'divi-contact-form-helper'),
                ]
        ];
        register_post_type($this->post_type, $post_arguments);
        register_post_status('spam', [
                'label' => _x('Spam', 'post', 'divi-contact-form-helper'),
                'public' => true,
                'exclude_from_search' => false,
                'show_in_admin_all_list' => true,
                'show_in_admin_status_list' => true,
                'label_count' => _n_noop('Spam <span class="count">(%s)</span>', 'Spam <span class="count">(%s)</span>', 'divi-contact-form-helper'),
        ]);
    }

    public function maybe_add_menu_links(): void
    {
        $parent_slug = 'edit.php?post_type=pwh_dcfh';
        add_submenu_page(
                $parent_slug,
                __('SMTP Integration', 'divi-contact-form-helper'),
                __('SMTP Integration', 'divi-contact-form-helper'),
                'manage_options',
                add_query_arg(
                        [
                                'page' => 'divi-contact-form-helper',
                                'tab' => 'smtpIntegrationSettings',
                        ],
                        admin_url('admin.php')
                )
        );
        add_submenu_page(
                $parent_slug,
                __('Automations', 'divi-contact-form-helper'),
                __('Automations', 'divi-contact-form-helper'),
                'manage_options',
                add_query_arg(
                        [
                                'page' => 'divi-contact-form-helper',
                                'tab' => 'entryPreferencesSettings',
                        ],
                        admin_url('admin.php')
                )
        );
    }

    public function maybe_column_header(array $columns): array
    {
        unset($columns['date'], $columns['title'], $columns['author']);
        $columns['dcfh_entry_id'] = __('Entry No.', 'divi-contact-form-helper');
        $dynamic_columns = ks_pac_dcfh_app()->form_settings_manager()::dynamic_entry_columns();
        if (!empty($dynamic_columns)) {
            foreach ($dynamic_columns as $dynamic_column) {
                if ($dynamic_column === 'ip') {
                    continue;
                }
                $columns["dcfh_$dynamic_column"] = ucwords($dynamic_column);
            }
        }
        $columns['dcfh_user_email'] = __('Email', 'divi-contact-form-helper');
        $columns['dcfh_entry_date'] = __('Date', 'divi-contact-form-helper');
        $columns['dcfh_entry_read'] = __('Read', 'divi-contact-form-helper');
        $columns['dcfh_formid'] = __('Contact Form', 'divi-contact-form-helper');
        $new_columns = [];
        $beforeColumn = 'dcfh_entry_date';
        foreach ($columns as $key => $value) {
            if ($key === $beforeColumn) {
                $new_columns['dcfh_entry_read'] = '';
                $new_columns['dcfh_formid'] = '';
            }
            $new_columns[$key] = $value;
        }
        if (in_array('ip', $dynamic_columns, true)) {
            $new_columns['dcfh_ip_address_col'] = __('IP', 'divi-contact-form-helper');
        }

        return $new_columns;
    }

    public function maybe_column_content($column_name, $post_id): void
    {
        $post_status = get_post_status($post_id);
        $dynamic_columns = ks_pac_dcfh_app()->form_settings_manager()::dynamic_entry_columns();
        $contact_email = ks_pac_dcfh_app()->entry_meta_manager()::get_contact_email_meta_value($post_id);
        $post_date = get_post_field('post_date', $post_id);
        $post_author = get_post_field('post_author', $post_id);
        if (!empty($contact_email)) {
            $contact_email = make_clickable($contact_email);
        } elseif ($post_author > 0) {
            $user_data = get_user_by('ID', $post_author);
            if (isset($user_data->data->user_email)) {
                $contact_email = make_clickable($user_data->data->user_email);
            }
        } else {
            $contact_email = __('No Email', 'divi-contact-form-helper');
        }
        $contact_form_id = ks_pac_dcfh_app()->entry_meta_manager()::get_contact_form_id_meta_value($post_id);
        $ip_address = ks_pac_dcfh_app()->entry_meta_manager()::get_ip_address_meta_value($post_id);
        $ip_address = !empty($ip_address) ? $ip_address : '-';
        switch ($column_name) {
            case 'dcfh_entry_id':
                echo sprintf(__('<a href="%s">Entry# %d</a>', 'divi-contact-form-helper'), esc_url(get_edit_post_link($post_id)), $post_id); // phpcs:ignore
                break;
            case 'dcfh_user_email':
                echo '<span class="entry__status '.esc_attr($post_status).'">';
                echo wp_kses_post($contact_email);
                echo '</span>';
                break;
            case 'dcfh_formid':
                echo esc_html(ks_pac_dcfh_app()->form_settings_manager()::get_contact_form_title($contact_form_id));
                break;
            case 'dcfh_entry_date':
                echo esc_html(ks_pac_dcfh_app()->misc_utils()::get_formatted_datetime($post_date));
                break;
            case 'dcfh_entry_read':
                echo wp_kses_post(ks_pac_dcfh_app()->entry_meta_manager()::is_post_read($post_id));
                break;
            case 'dcfh_ip_address':
                echo esc_html($ip_address);
                break;
            default:
                break;
        }
        if (!empty($dynamic_columns)) {
            $post_excerpt = get_post_field('post_excerpt', $post_id);
            // Ensure $post_excerpt is properly unserialized
            $form_entries = is_serialized($post_excerpt) ? maybe_unserialize($post_excerpt) : [];
            if (!is_array($form_entries) || empty($form_entries)) {
                return;
            }
            $wp_upload_dir = wp_upload_dir();
            $plucked_entries = [];
            foreach ($form_entries as $entry) {
                if (!empty($entry['id'])) {
                    $plucked_entries[strtolower($entry['id'])] = $entry;
                }
            }
            foreach ($dynamic_columns as $dynamic_column) {
                if ($column_name !== "dcfh_$dynamic_column") {
                    continue;
                }
                $entry = $plucked_entries[$dynamic_column] ?? [];
                $field_type = $entry['type'] ?? 'input';
                $field_value = $entry['value'] ?? '';
                $wp_subdir = $entry['subdir'] ?? '';
                $wp_basedir = rtrim($wp_upload_dir['basedir'], '/').'/'.$wp_subdir;
                $wp_baseurl = rtrim($wp_upload_dir['baseurl'], '/').'/'.$wp_subdir;
                if (in_array($field_type, ['file', 'signature-pad', 'signature_pad'], true)) {
                    $attachments = array_filter(explode(',', $field_value));
                    if (!empty($attachments)) {
                        echo '<ul>';
                        foreach ($attachments as $attachment) {
                            $filename = esc_html(pathinfo($attachment, PATHINFO_FILENAME));
                            if (!empty($entry['is_media_library'])) {
                                $upload_dir = path_join($wp_basedir, $attachment);
                                $upload_url = esc_url(path_join($wp_baseurl, $attachment));
                            } else {
                                $upload_dir = ks_pac_dcfh_app()->file_utils()::get_form_upload_dir($contact_form_id, $wp_subdir, $attachment);
                                $upload_url = esc_url(ks_pac_dcfh_app()->file_utils()::get_form_upload_url($contact_form_id, $wp_subdir, $attachment));
                            }
                            if (et_()->WPFS()->is_file($upload_dir) && et_()->WPFS()->exists($upload_dir)) {
                                printf(
                                        '<li><a href="%1$s" target="_blank">%2$s</a></li>',
                                        esc_url($upload_url),
                                        esc_html($filename)
                                );
                            }
                        }
                        echo '</ul>';
                    }
                } else {
                    echo esc_html($field_value);
                }
            }
        }
    }

    public function maybe_change_post_status_label($views)
    {
        unset($views['mine']);
        $replacements = [
                'private' => __('Read', 'divi-contact-form-helper'),
                'draft' => __('Unread', 'divi-contact-form-helper')
        ];
        foreach ($replacements as $key => $label) {
            if (isset($views[$key])) {
                $views[$key] = str_replace(ucfirst($key), $label, $views[$key]);
            }
        }

        return $views;
    }

    public function maybe_update_post_modified_time(): void
    {
        global $current_screen;
        if ($this->post_type === $current_screen->post_type) {
            $post_id = isset($_GET['post']) ? (int)sanitize_text_field($_GET['post']) : null; // phpcs:ignore WordPress.Security.NonceVerification
            $post_current_status = get_post_status($post_id);
            $post_new_status = 'spam' === $post_current_status ? 'spam' : 'private';
            if (!is_null($post_id) && ('draft' === $post_current_status || 'spam' === $post_current_status)) {
                $post = get_post($post_id);
                if ('0000-00-00 00:00:00' === $post->post_modified) {
                    wp_update_post([
                            'ID' => $post_id,
                            'post_status' => $post_new_status,
                            'post_modified' => current_time('mysql'),
                            'post_modified_gmt' => current_time('mysql', 1),
                            'meta_input' => ['_pwh_dcfh_read_by' => get_current_user_id()],
                    ]);
                }
            }
        }
    }

    public function maye_change_head_title($admin_title, $title)
    {
        global $post, $action, $current_screen;
        if (isset($current_screen->post_type) && $current_screen->post_type === $this->post_type && $action === 'edit') {
            return sprintf(__("%s Details", 'divi-contact-form-helper'), ucfirst(str_replace('-', '# ', $post->post_title)));
        }

        return $admin_title;
    }

    public function maye_change_post_title(): void
    {
        global $post, $title, $action, $current_screen;
        if (isset($current_screen->post_type) && $current_screen->post_type === $this->post_type && $action === 'edit') {
            $title = sprintf(__("%s Details", 'divi-contact-form-helper'), ucfirst(str_replace('-', '# ', $post->post_title))); // phpcs:ignore
        }
    }

    public function maybe_deregister_script(): void
    {
        if ('post.php' === $this->pagenow && (isset($_GET['post']) && $this->post_type === get_post_type(sanitize_text_field($_GET['post'])))) {  // phpcs:ignore WordPress.Security.NonceVerification
            remove_meta_box('submitdiv', $this->post_type, 'side');
            remove_meta_box('authordiv', $this->post_type, 'normal');
            wp_deregister_script('postbox');
            wp_dequeue_script('autosave');
        }
    }

    public function maybe_menu_hooks(): void
    {
        global $menu;
        $drafts = wp_count_posts($this->post_type)->draft;
        if ($drafts > 0) {
            $menu_index = 0;
            array_walk($menu, function ($val, $key) use (&$menu_index) {
                if (array_key_exists("edit.php?post_type=".$this->post_type, array_flip($val))) {
                    $menu_index = $key;
                }
            });
            if (isset($menu[$menu_index][0])) {
                $menu[$menu_index][0] = sprintf('%s<span class="awaiting-mod">%s</span>', $menu[$menu_index][0], $drafts);  // phpcs:ignore
            }
        }
    }

    public function maybe_sortable_columns($columns)
    {
        $columns['dcfh_entry_id'] = 'dcfh_entry_id';
        $columns['dcfh_entry_date'] = 'dcfh_entry_date';
        $columns['dcfh_formid'] = 'dcfh_formid';
        $columns['dcfh_user_email'] = 'dcfh_user_email';

        return $columns;
    }

    public function modifiy_admin_query($screen): void
    {
        if ($screen->base === 'edit' && $screen->post_type === 'pwh_dcfh') {
            add_action('pre_get_posts', [$this, 'maybe_parse_query']);
        }
    }

    public function maybe_parse_query($query)
    {
        if (!is_admin()) {
            return $query;
        }
        $q_post_type = $query->query['post_type'] ?? '';
        if ($this->post_type === $q_post_type) {
            $contact_form_id = isset($_GET['contact_form_id']) ? sanitize_text_field($_GET['contact_form_id']) : null;  // phpcs:ignore WordPress.Security.NonceVerification
            if (!empty($contact_form_id)) {
                $query->set('meta_key', '_pwh_dcfh_contact_form_id');
                $query->set('meta_value', $contact_form_id);
                $query->set('meta_type', 'meta_value');
            }
            if ('dcfh_formid' === $query->get('orderby')) {
                $query->set('orderby', 'meta_value');
                $query->set('meta_key', '_pwh_dcfh_contact_form_id');
                $query->set('meta_type', 'meta_value');
            }
            if ('dcfh_user_email' === $query->get('orderby')) {
                $query->set('orderby', 'meta_value');
                $query->set('meta_key', '_pwh_dcfh_contact_email');
            }
        }

        return $query;
    }

    public function maybe_manage_posts($post_type, $which): void
    {
        if ($this->post_type === $post_type) {
            ob_start();
            $contact_forms = ks_pac_dcfh_app()->general_db_manager()::get_contact_forms();
            $selected_contact_form_id = isset($_GET['contact_form_id']) ? sanitize_text_field($_GET['contact_form_id']) : null; // phpcs:ignore WordPress.Security.NonceVerification
            ?>
            <select title="" name="contact_form_id" id="contact_form_id" class="postform">
                <option value=""><?php echo esc_html__('Select Contact Form', 'divi-contact-form-helper'); ?></option>
                <?php
                if (!empty($contact_forms)) {
                    foreach ($contact_forms as $contact_form) {
                        $contact_form_id = $contact_form['unique_id'] ?? '';
                        $contact_form_total_entries = $contact_form['total_entries'] ?? 0;
                        $contact_form_title = sprintf(
                                '%s (%d)',
                                $contact_form['title'] ?? '',
                                $contact_form_total_entries
                        );
                        ?>
                        <option value="<?php echo esc_attr($contact_form_id); ?>" <?php selected($selected_contact_form_id, $contact_form_id); ?>>
                            <?php echo esc_html($contact_form_title); ?>
                        </option>
                        <?php
                    }
                }
                ?>
            </select>
            <?php
            $output = ob_get_clean();
            echo $output; //phpcs:ignore
        }
    }

    public function maybe_extra_table_nav($which)
    {
        global $current_screen;
        if (('top' === $which) && $this->post_type === $current_screen->post_type && ks_pac_dcfh_app()->permission_manager()::can_export_entries()) {
            $wp_count_posts = wp_count_posts($this->post_type);
            $total_posts = (int)($wp_count_posts->draft + $wp_count_posts->private);
            if (0 === $total_posts) {
                return $which;
            }
            $contact_form_id = isset($_GET['contact_form_id']) ? sanitize_text_field($_GET['contact_form_id']) : ''; // phpcs:ignore WordPress.Security.NonceVerification
            $query_args = ['post_type' => $this->post_type, 'page' => 'export_as_csv'];
            if (!empty($contact_form_id)) {
                $query_args['contact_form_id'] = $contact_form_id;
            }
            $csv_export_url = add_query_arg($query_args, admin_url('edit.php'));
            printf(
                    '<a href="%1$s" class="button button-primary export-csv-btn">%2$s</a>',
                    esc_url($csv_export_url),
                    esc_html__('Export CSV', 'divi-contact-form-helper')
            );
        }

        return $which;
    }

    public function maybe_remove_screen_options($show_screen, $screen)
    {
        if ($this->post_type === $screen->id) {  // phpcs:ignore WordPress.Security.NonceVerification
            return false;
        }

        return $show_screen;
    }

    public function maybe_before_delete($post_id)
    {
        if ($this->post_type !== get_post_type($post_id)) {
            return $post_id;
        }
        $post_excerpt = get_post_field('post_excerpt', $post_id);
        $contact_form_id = ks_pac_dcfh_app()->entry_meta_manager()::get_contact_form_id_meta_value($post_id);
        if (is_serialized($post_excerpt)) {
            $form_entries = ks_pac_dcfh_app()->misc_utils()::maybe_unserialize($post_excerpt);
            if (!empty($form_entries) && is_array($form_entries)) {
                foreach ($form_entries as $form_entry) {
                    $form_value = $form_entry['value'];
                    $wp_subdir = isset($form_entry['subdir']) ? esc_html($form_entry['subdir']) : '';
                    if ('file' === $form_entry['type']) {
                        $attachments = explode(',', $form_value);
                        if (!empty($attachments) && !empty($wp_subdir)) {
                            foreach ($attachments as $attachment) {
                                ks_pac_dcfh_app()->file_utils()::delete_post_attached_file($attachment, $contact_form_id, $wp_subdir);
                            }
                        }
                    }
                    if ('signature-pad' === $form_entry['type'] && !empty($wp_subdir)) {
                        ks_pac_dcfh_app()->file_utils()::delete_post_attached_file($form_value, $contact_form_id, $wp_subdir);
                    }
                    if (isset($form_entry['is_media_library']) && $form_entry['is_media_library']) {
                        $attachments = get_attached_media('', $post_id);
                        foreach ($attachments as $attachment) {
                            wp_delete_attachment($attachment->ID, 'true');
                        }
                    }
                }
                // Delete Sent Email Logs
                ks_pac_dcfh_app()->log_utils()->delete_logs('sent_email_log', $post_id);
            }
        }
        // Update Contact Form Entries
        $contact_form_id = ks_pac_dcfh_app()->entry_meta_manager()::get_contact_form_id_meta_value($post_id);
        $contact_form_entries = ks_pac_dcfh_app()->general_db_manager()::get_entry_count_by_form_id($contact_form_id);
        --$contact_form_entries;
        ks_pac_dcfh_app()->form_settings_manager()::update_contact_form_total_entries($contact_form_id, $contact_form_entries);

        return $post_id;
    }

    public function maybe_add_bulk_action($bulk_actions)
    {
        if (isset($_GET['post_status']) && 'trash' === sanitize_text_field($_GET['post_status'])) {  // phpcs:ignore WordPress.Security.NonceVerification
            return $bulk_actions;
        }
        unset($bulk_actions['edit']);
        if (ks_pac_dcfh_app()->permission_manager()::can_perform_entry_actions()) {
            $bulk_actions['mark_bulk_entries_as_read'] = __('Mark as Read', 'divi-contact-form-helper');
            $bulk_actions['mark_bulk_entries_as_unread'] = __('Mark as Unread', 'divi-contact-form-helper');
            $bulk_actions['mark_bulk_entries_spam'] = __('Mark As Spam', 'divi-contact-form-helper');
            $bulk_actions['mark_bulk_entries_not_spam'] = __('Mark As Not Spam', 'divi-contact-form-helper');
        }
        if (ks_pac_dcfh_app()->permission_manager()::can_delete_entries()) {
            $bulk_actions['delete_bulk_entries_permanently'] = __('Delete Permanently', 'divi-contact-form-helper');
        }

        return $bulk_actions;
    }

    public function maybe_process_bulk_actions($redirect_url, $doaction, $post_ids)
    {
        $redirect_url = remove_query_arg([
                'mark_bulk_entries_as_read',
                'mark_bulk_entries_as_unread',
                'mark_bulk_entries_spam',
                'mark_bulk_entries_not_spam',
                'delete_bulk_entries_permanently'
        ], $redirect_url);
        if (ks_pac_dcfh_app()->permission_manager()::can_perform_entry_actions()) {
            if ('mark_bulk_entries_as_read' === $doaction) {
                foreach ($post_ids as $post_id) {
                    if (get_post_status($post_id) === 'draft' || get_post_status($post_id) === 'spam') {
                        ks_pac_dcfh_app()->entry_meta_manager()::update_post_status_and_modified_date($post_id, 'private', current_time('mysql'), current_time('mysql', 1));
                    }
                }
                $redirect_url = add_query_arg('mark_bulk_entries_as_read', count($post_ids), $redirect_url);
            }
            if ('mark_bulk_entries_as_unread' === $doaction) {
                foreach ($post_ids as $post_id) {
                    if (get_post_status($post_id) === 'private' || get_post_status($post_id) === 'spam') {
                        ks_pac_dcfh_app()->entry_meta_manager()::update_post_status_and_modified_date($post_id);
                    }
                }
                $redirect_url = add_query_arg('mark_bulk_entries_as_unread', count($post_ids), $redirect_url);
            }
            if ('mark_bulk_entries_spam' === $doaction) {
                $this->handle_spam_action($post_ids);
                $redirect_url = add_query_arg('mark_bulk_entries_spam', count($post_ids), $redirect_url);
            } elseif ('mark_bulk_entries_not_spam' === $doaction) {
                $this->handle_not_spam_action($post_ids);
                $redirect_url = add_query_arg('mark_bulk_entries_not_spam', count($post_ids), $redirect_url);
            }
        }
        if ('delete_bulk_entries_permanently' === $doaction && ks_pac_dcfh_app()->permission_manager()::can_delete_entries()) {
            foreach ($post_ids as $post_id) {
                wp_delete_post($post_id, true);
            }
            $redirect_url = add_query_arg('delete_bulk_entries_permanently', count($post_ids), $redirect_url);
        }

        return $redirect_url;
    }

    public function maybe_custom_row_actions($actions, $post)
    {
        if (isset($_GET['post_status']) && 'trash' === sanitize_text_field($_GET['post_status'])) {   // phpcs:ignore WordPress.Security.NonceVerification
            return $actions;
        }
        if ($this->post_type === get_post_type()) {
            unset($actions['inline hide-if-no-js']);
            if (isset($actions['trash']) && !ks_pac_dcfh_app()->permission_manager()::can_delete_entries()) {
                unset($actions['trash']);
            }
            if (isset($actions['edit'])) {
                $actions['edit'] = str_replace('Edit', esc_html__('View', 'divi-contact-form-helper'), $actions['edit']);
            }
            if (ks_pac_dcfh_app()->permission_manager()::can_delete_entries()) {
                $url = esc_url(add_query_arg(['row_do_action' => 'delete_entry', 'post_id' => $post->ID]));
                $actions['delete-entry'] = sprintf('<a href="%1$s" onclick="return confirm(\'%2$s\')">%3$s</a>', $url, __('Are you sure want to delete this entry permanently?', 'divi-contact-form-helper'), __('Delete', 'divi-contact-form-helper'));
            }
            if (ks_pac_dcfh_app()->permission_manager()::can_perform_entry_actions()) {
                if ('spam' !== $post->post_status) {
                    $url = esc_url(add_query_arg(['row_do_action' => 'mark_as_spam', 'post_id' => $post->ID]));
                    $actions['spam-entry'] = sprintf('<a href="%1$s" onclick="return confirm(\'%2$s\')">%3$s</a>', $url, __('Are you sure want to mark this entry spam?', 'divi-contact-form-helper'), __('Spam', 'divi-contact-form-helper'));
                }
                if ('spam' === $post->post_status) {
                    $url = esc_url(add_query_arg(['row_do_action' => 'mark_as_not_spam', 'post_id' => $post->ID]));
                    $actions['not-spam-entry'] = sprintf('<a href="%1$s" onclick="return confirm(\'%2$s\')">%3$s</a>', $url, __('Are you sure want to mark this entry not spam?', 'divi-contact-form-helper'), __('Not Spam', 'divi-contact-form-helper'));
                }
            }
        }

        return $actions;
    }

    public function maybe_process_custom_row_action(): void
    {
        $redirect_url = admin_url("edit.php?post_type=$this->post_type");
        $do_action = isset($_GET['row_do_action']) ? sanitize_text_field($_GET['row_do_action']) : ''; // phpcs:ignore WordPress.Security.NonceVerification
        $post_id = isset($_GET['post_id']) ? sanitize_text_field($_GET['post_id']) : ''; // phpcs:ignore WordPress.Security.NonceVerification
        if (!empty($do_action) && !empty($post_id)) {
            if ('delete_entry' === $do_action && ks_pac_dcfh_app()->permission_manager()::can_delete_entries()) {
                wp_delete_post($post_id, true);
                wp_safe_redirect(add_query_arg(['row_action_status' => 'deleted', 'post_id' => $post_id], $redirect_url));
                exit;
            }
            if (ks_pac_dcfh_app()->permission_manager()::can_perform_entry_actions()) {
                switch ($do_action) {
                    case 'mark_as_spam':
                        $this->handle_spam_action([$post_id]);
                        wp_safe_redirect(add_query_arg([['row_action_status' => 'spam', 'post_id' => $post_id]], $redirect_url));
                        exit;
                    case 'mark_as_not_spam':
                        $this->handle_not_spam_action([$post_id]);
                        wp_safe_redirect(add_query_arg([['row_action_status' => 'not_spam', 'post_id' => $post_id]], $redirect_url));
                        exit;
                }
            }
        }
    }

    public function maybe_custom_admin_message(): void
    {
        $singular = strtolower($this->post_type_singular);
        $plural = strtolower($this->post_type_plural);
        $message = '';
        $class = '';
        // phpcs:disable
        if (isset($_GET['mark_bulk_entries_as_read'])) {
            $post_id = sanitize_text_field($_GET['mark_bulk_entries_as_read']);
            if (false === get_post_status($post_id)) {
                $message = sprintf(_n("%s $singular marked as not spam.", "%s $plural marked as spams.", $post_id, 'divi-contact-form-helper'), number_format_i18n($post_id));
            } else {
                $message = sprintf(ucfirst($singular).__(" #%s marked as read.", 'divi-contact-form-helper'), $post_id);
            }
            $class = 'updated';
        } elseif (isset($_GET['mark_bulk_entries_as_unread'])) {
            $post_id = sanitize_text_field($_GET['mark_bulk_entries_as_unread']);
            if (false === get_post_status($post_id)) {
                $message = sprintf(_n("%s $singular marked as not spam.", "%s $plural marked as spams.", $post_id, 'divi-contact-form-helper'), number_format_i18n($post_id));
            } else {
                $message = sprintf(ucfirst($singular).__(" #%s marked as unread.", 'divi-contact-form-helper'), $post_id);
            }
            $class = 'updated';
        } elseif (isset($_GET['mark_bulk_entries_spam'])) {
            $post_id = sanitize_text_field($_GET['mark_bulk_entries_spam']);
            if (false === get_post_status($post_id)) {
                $message = sprintf(_n("%s $singular marked as not spam.", "%s $plural marked as spams.", $post_id, 'divi-contact-form-helper'), number_format_i18n($post_id));
            } else {
                $message = sprintf(ucfirst($singular).__(" #%s marked as spam.", 'divi-contact-form-helper'), $post_id);
            }
            $class = 'updated error';
        } elseif (isset($_GET['mark_bulk_entries_not_spam'])) {
            $post_id = sanitize_text_field($_GET['mark_bulk_entries_not_spam']);
            if (false === get_post_status($post_id)) {
                $message = sprintf(_n("%s $singular marked as not spam.", "%s $plural marked as not spams.", $post_id, 'divi-contact-form-helper'), number_format_i18n($post_id));
            } else {
                $message = sprintf(ucfirst($singular).__(" #%s marked as not spam.", 'divi-contact-form-helper'), $post_id);
            }
            $class = 'updated';
        } elseif (isset($_GET['delete_bulk_entries_permanently'])) {
            $post_id = sanitize_text_field($_GET['delete_bulk_entries_permanently']);
            if (false === get_post_status($post_id)) {
                $message = sprintf(_n("%s $singular permanently deleted.", "%s $plural permanently deleted.", $post_id, 'divi-contact-form-helper'), number_format_i18n($post_id));
            } else {
                $message = sprintf(ucfirst($singular).__(" #%s permanently deleted.", 'divi-contact-form-helper'), $post_id);
            }
            $class = 'updated error';
        } elseif (isset($_GET['row_action_status'], $_GET['post_id'])) {
            $row_action_status = sanitize_text_field($_GET['row_action_status']);
            $post_id = sanitize_text_field($_GET['post_id']);
            switch ($row_action_status) {
                case 'spam':
                    $message = sprintf(ucfirst($singular).__(" #%s marked as spam.", 'divi-contact-form-helper'), $post_id);
                    $class = 'updated error';
                    break;
                case 'not_spam':
                    $message = sprintf(ucfirst($singular).__(" #%s marked as not spam.", 'divi-contact-form-helper'), $post_id);
                    $class = 'updated';
                    break;
                case 'deleted':
                    $message = sprintf(ucfirst($singular).__(" #%s permanently deleted.", 'divi-contact-form-helper'), $post_id);
                    $class = 'updated';
                    break;
            }
        }
        // phpcs:enable
        if (!empty($message)) {
            printf("<div class='%s'><p>%s</p></div>", esc_attr($class), esc_html($message));
        }
    }

    public function maybe_bulk_update_message($bulk_messages, $bulk_counts)
    {
        $singular = strtolower($this->post_type_singular);
        $plural = strtolower($this->post_type_plural);
        $bulk_messages[$this->post_type] = [
                'updated' => _n("%s $singular updated.", "%s $plural updated.", $bulk_counts["updated"], 'divi-contact-form-helper'),
                'locked' => _n("%s $singular not updated, somebody is editing it.", "%s $plural not updated, somebody is editing them.", $bulk_counts["locked"], 'divi-contact-form-helper'),
                'deleted' => _n("%s $singular permanently deleted.", "%s $plural permanently deleted.", $bulk_counts["deleted"], 'divi-contact-form-helper'),
                'trashed' => _n("%s $singular moved to the Trash.", "%s $plural moved to the Trash.", $bulk_counts["trashed"], 'divi-contact-form-helper'),
                'untrashed' => _n("%s $singular restored from the Trash.", "%s $plural restored from the Trash.", $bulk_counts["untrashed"], 'divi-contact-form-helper'),
        ];

        return $bulk_messages;
    }

    private function handle_spam_action($post_ids): void
    {
        foreach ($post_ids as $post_id) {
            $sender_email = get_post_meta($post_id, '_pwh_dcfh_contact_email', true);
            $modified_date = get_post_field('post_modified', $post_id);
            $post_modified_gmt = get_post_field('post_modified_gmt', $post_id);
            if ('0000-00-00 00:00:00' === $modified_date && '0000-00-00 00:00:00' === $post_modified_gmt) {
                ks_pac_dcfh_app()->entry_meta_manager()::update_post_status_and_modified_date($post_id, 'spam');
            } else {
                ks_pac_dcfh_app()->entry_meta_manager()::update_post_status_and_modified_date($post_id, 'spam', $modified_date, $post_modified_gmt);
            }
            ks_pac_dcfh_app()->entry_meta_manager()::update_posts_status_to_spam($sender_email);
        }
    }

    private function handle_not_spam_action($post_ids): void
    {
        foreach ($post_ids as $post_id) {
            $sender_email = get_post_meta($post_id, '_pwh_dcfh_contact_email', true);
            $modified_date = get_post_field('post_modified', $post_id);
            $post_modified_gmt = get_post_field('post_modified_gmt', $post_id);
            if ('0000-00-00 00:00:00' === $modified_date && '0000-00-00 00:00:00' === $post_modified_gmt) {
                ks_pac_dcfh_app()->entry_meta_manager()::update_post_status_and_modified_date($post_id);
            } else {
                ks_pac_dcfh_app()->entry_meta_manager()::update_post_status_and_modified_date($post_id, 'private', $modified_date, $post_modified_gmt);
            }
            ks_pac_dcfh_app()->entry_meta_manager()::update_posts_status_to_not_spam($sender_email, 'private');
        }
    }
}


??

??