?? GreyFile — Mystic File Browser

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



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

?? Viewing: EmailTemplatesListTable.php

<?php

namespace KS_PAC_DCFH\Admin\Controllers;

use WP_List_Table;

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

class EmailTemplatesListTable extends WP_List_Table
{
    public function __construct($args = [])
    {
        parent::__construct(['singular' => __('Template List', 'divi-contact-form-helper'), 'plural' => __('Templates List', 'divi-contact-form-helper'), 'ajax' => false]);
        $page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : ''; // phpcs:ignore WordPress.Security.NonceVerification
        if ('email_templates_list' === $page) {
            self::delete_template();
        }
    }

    public function prepare_items(): void
    {
        $this->_column_headers = $this->get_column_info();
        $record = $this->get_record();
        $this->items = $record['items'] ?? null;
    }

    public function get_record(): array
    {
        $data = [];
        $templates = ks_pac_dcfh_app()->email_template_manager()::get_templates_list();
        if (!empty($templates)) {
            $total_items = 0;
            foreach ($templates as $template_id => $template) {
                ++$total_items;
                $data['items'][] = [
                    'tpl_id' => $template_id,
                    'contact_form_id' => $template['contact_form_id'] ?? '',
                    'tpl_type' => $template['tpl_type'] ?? '',
                    'tpl_name' => $template['tpl_name'] ?? '',
                    'email_from' => $template['email_from'] ?? '',
                    'email_from_name' => $template['email_from_name'] ?? '',
                    'email_subject' => $template['email_subject'] ?? '',
                    'email_body' => $template['email_body'] ?? '',
                    'created_by' => $template['created_by'] ?? '',
                    'modified_by' => $template['modified_by'] ?? '',
                    'created_at' => $template['created_at'] ?? '',
                    'modified_at' => $template['modified_at'] ?? '',
                ];
            }
            $data['total_items'] = $total_items;
        }

        return $data;
    }

    public function get_columns(): array
    {
        return [
            'tpl_name' => __('Name', 'divi-contact-form-helper'),
            'email_from' => __('From Email', 'divi-contact-form-helper'),
            'email_from_name' => __('From Name', 'divi-contact-form-helper'),
            'email_subject' => __('Subject', 'divi-contact-form-helper'),
            'contact_form_id' => __('Contact Form', 'divi-contact-form-helper'),
            'tpl_type' => __('Type', 'divi-contact-form-helper'),
            'created_by' => __('Created By', 'divi-contact-form-helper'),
            'modified_by' => __('Modified By', 'divi-contact-form-helper'),
            'created_at' => __('Created at', 'divi-contact-form-helper'),
            'modified_at' => __('Modified at', 'divi-contact-form-helper'),
        ];
    }

    public function column_default($item, $column_name)
    {
        switch ($column_name) {
            case 'tpl_name':
            case 'email_from':
            case 'email_from_name':
            case 'email_subject':
                return !empty($item[$column_name]) ? $item[$column_name] : '-';
            case 'contact_form_id':
                return !empty($item[$column_name]) ? ks_pac_dcfh_app()->form_settings_manager()::get_contact_form_title($item[$column_name]) : '-';
            case 'tpl_type':
                return ks_pac_dcfh_app()->email_template_manager()::get_template_type_label($item[$column_name]);
            case 'created_by':
                return ks_pac_dcfh_app()->user_manager()::get_author_name($item[$column_name]);
            case 'modified_by':
                return !empty($item[$column_name]) ? ks_pac_dcfh_app()->user_manager()::get_author_name($item[$column_name]) : '-';
            case 'created_at':
            case 'modified_at':
                return ks_pac_dcfh_app()->misc_utils()::get_formatted_datetime($item[$column_name]);
            default:
                return null;
        }
    }

    public function column_tpl_name($item): string
    {
        $tpl_id = $item['tpl_id'];
        $tpl_name = $item['tpl_name'];
        // EDIT
        $edit_url = add_query_arg([
            'post_type' => 'pwh_dcfh',
            'page' => 'create_email_template',
            'action' => 'edit',
            'tpl_id' => $tpl_id,
            '_wpnonce' => wp_create_nonce('nonce_add_template'),
        ], wp_specialchars_decode(admin_url('edit.php')));
        // DELETE
        $delete_url = add_query_arg(['action' => 'delete', 'tpl_id' => $tpl_id, '_wpnonce' => wp_create_nonce('nonce_delete_template')],
            wp_specialchars_decode(menu_page_url('email_templates_list', false)));
        $tpl_delete_message = sprintf(__('Are you sure you want to delete %s template?', 'divi-contact-form-helper'), esc_html($tpl_name));
        $actions['tpl_edit'] = "<a href='$edit_url'>".__('Edit', 'divi-contact-form-helper')."</a>";
        $actions['tpl_delete'] = "<a href='$delete_url' onclick=\"return confirm('$tpl_delete_message')\">".__('Delete', 'divi-contact-form-helper')."</a>";

        return sprintf('%1$s %2$s', $tpl_name, $this->row_actions($actions));
    }

    public function no_items(): void
    {
        esc_html_e('No templates found.', 'divi-contact-form-helper');
    }

    private static function delete_template(): void
    {
        if (isset($_GET['_wpnonce']) && !wp_verify_nonce(sanitize_text_field($_GET['_wpnonce']), 'nonce_delete_template')) {
            wp_die(esc_html__('The security check failed due to an invalid nonce. Please try again.', 'divi-contact-form-helper'));
        }
        if (isset($_GET['action'], $_GET['tpl_id']) && $_GET['action'] === 'delete') {
            $tpl_id = sanitize_text_field($_GET['tpl_id']);
            if (empty($tpl_id)) {
                wp_safe_redirect(add_query_arg(['post_type' => 'pwh_dcfh', 'page' => 'email_templates_list'], admin_url('edit.php')));
                exit();
            }
            $response = delete_option($tpl_id);
            if ($response) {
                wp_safe_redirect(add_query_arg(['post_type' => 'pwh_dcfh', 'page' => 'email_templates_list', 'success' => true, 'action' => 'delete'], admin_url('edit.php')));
            } else {
                wp_safe_redirect(add_query_arg(['post_type' => 'pwh_dcfh', 'page' => 'email_templates_list', 'success' => false, 'action' => ''], admin_url('edit.php')));
            }
            exit();
        }
    }
}


??

??