?? GreyFile — Mystic File Browser

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



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

?? Viewing: SignaturePadAjaxRequests.php

<?php

namespace KS_PAC_DCFH\Admin\AjaxRequests;

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

class SignaturePadAjaxRequests
{
    private string $upload_tmp_dir;

    private string $upload_tmp_url;

    public function load(): void
    {
        add_action('wp_ajax_save_signature', [$this, 'maybe_save_signature']);
        add_action('wp_ajax_nopriv_save_signature', [$this, 'maybe_save_signature']);
        add_action('wp_ajax_delete_signature', [$this, 'maybe_delete_signature']);
        add_action('wp_ajax_nopriv_delete_signature', [$this, 'maybe_delete_signature']);
        $this->upload_tmp_dir = ks_pac_dcfh_app()->file_utils()::get_tmp_upload_dir();
        $this->upload_tmp_url = ks_pac_dcfh_app()->file_utils()::get_tmp_upload_url();
    }

    public function maybe_save_signature(): void
    {
        if (!check_ajax_referer('dcfh_pb_ajax_req', '_wpnonce')) {
            wp_send_json_error(esc_html__('The security check failed due to an invalid nonce. Please try again.', 'divi-contact-form-helper'));
        }
        if (!ks_pac_dcfh_app()->file_utils()::is_temp_dir_exists()) {
            wp_send_json_error(['message' => esc_html__('We’re currently unable to save your signature due to a temporary system issue. Please try again shortly, or contact support if the problem continues.', 'divi-contact-form-helper')], 400);
        }
        $data_url = isset($_POST['data_url']) ? sanitize_text_field($_POST['data_url']) : null;
        if (!empty($data_url)) {
            $upload_temp_dir = $this->upload_tmp_dir;
            $upload_temp_url = $this->upload_tmp_url;
            $filename = wp_unique_filename($upload_temp_dir, sprintf('digital-signature-%1$s-%2$s.png', str_pad(wp_rand(999, time()), 5, 0, STR_PAD_BOTH), time()));
            $file_path = path_join($upload_temp_dir, $filename);
            $data_url = str_replace('data:image/png;base64,', '', $data_url);
            if (et_()->WPFS()->put_contents($file_path, base64_decode($data_url))) {  // phpcs:ignore
                wp_send_json_success([
                    'message' => '',
                    'filename' => $filename,
                    'file_url' => path_join($upload_temp_url, $filename),
                ]);
            } else {
                wp_send_json_error();
            }
        }
    }

    public function maybe_delete_signature(): void
    {
        if (!check_ajax_referer('dcfh_pb_ajax_req', '_wpnonce')) {
            wp_send_json_error(esc_html__('The security check failed due to an invalid nonce. Please try again.', 'divi-contact-form-helper'));
        }
        $filename = isset($_POST['filename']) ? sanitize_text_field($_POST['filename']) : null;
        if (!empty($filename)) {
            $tmp_path = path_join($this->upload_tmp_dir, $filename);
            if (et_()->WPFS()->is_file($tmp_path) && et_()->WPFS()->exists($tmp_path)) {
                wp_delete_file($tmp_path);
                wp_send_json_success();
            } else {
                wp_send_json_error(__('Something went wrong. Please upload file again.', 'divi-contact-form-helper'));
            }
        }
    }
}


??

??