<?php

namespace PWH_DCFH\App\Admin\Settings;

if (!defined('ABSPATH')) exit;
if (!class_exists('PWH_DCFH_Epanel_Settings')) {
    class PWH_DCFH_Epanel_Settings
    {
        private $wp_mail_failed;

        /**
         * Initializer Of The Class
         * Add/Remove Necessary Actions/Filters
         */
        public function init()
        {
            add_filter('et_epanel_tab_names', [$this, 'maybe_register_tab']);
            add_filter('et_epanel_layout_data', [$this, 'maybe_register_content']);
            add_action('admin_init', [$this, 'maybe_admin_hooks']);
            add_action('admin_menu', [$this, 'maybe_add_menu_links']);
            // add_action('update_option_et_divi', [$this, 'maybe_settings_update'], 10, 3);
        }

        /**
         * Add Menu Links
         * @return void
         */
        public function maybe_add_menu_links()
        {
            global $submenu;
            $menu_page = pwh_dcfh_hc()::ADMIN_MENU_PAGE_SLUG;
            // phpcs:disable
            $submenu[$menu_page][] = [
                __('SMTP Integration', 'pwh-dcfh'),
                'manage_options',
                esc_url(admin_url('admin.php?page=et_divi_options#wrap-pwh-dcfh-epanel'))
            ];
            $submenu[$menu_page][] = [
                __('Automations', 'pwh-dcfh'),
                'manage_options',
                esc_url(admin_url('admin.php?page=et_divi_options#wrap-pwh-dcfh-epanel'))
            ];
            // phpcs:enable
        }

        /**
         * Register Admin Hooks
         */
        public function maybe_admin_hooks()
        {
            add_action('wp_mail_failed', [$this, 'get_wp_mail_failed_error']);
            add_action('wp_ajax_process_smtp_test', [$this, 'process_smtp_test']);
        }

        /**
         * Add Custom Tab To Panel
         *
         * @param $tabs
         *
         * @return array
         */
        public function maybe_register_tab($tabs)
        {
            return array_merge($tabs, ['pwh-dcfh-epanel' => __('Divi Contact Form Helper', 'pwh-dcfh')]);
        }

        /**
         * Add Options To Custom Tab
         *
         * @param $options
         *
         * @return mixed
         */
        public function maybe_register_content($options)
        {

            $options[] = [
                'name' => 'wrap-pwh-dcfh-epanel',
                'type' => 'contenttab-wrapstart',
            ];
            $options[] = [
                'type' => 'subnavtab-start',
            ];
            $options[] = [
                'name' => 'pwh-dcfh-tab-miscellaneous',
                'type' => 'subnav-tab',
                'desc' => __('Miscellaneous', 'pwh-dcfh')
            ];
            $options[] = [
                'name' => 'pwh-dcfh-tab-smtp',
                'type' => 'subnav-tab',
                'desc' => __('SMTP Integration', 'pwh-dcfh')
            ];
            $options[] = [
                'name' => 'pwh-dcfh-tab-spam-protection',
                'type' => 'subnav-tab',
                'desc' => __('Spam Protection', 'pwh-dcfh')
            ];
            $options[] = [
                'name' => 'pwh-dcfh-tab-cron-jobs',
                'type' => 'subnav-tab',
                'desc' => __('Automations', 'pwh-dcfh')
            ];
            $options[] = [
                'name' => 'pwh-dcfh-tab-labels',
                'type' => 'subnav-tab',
                'desc' => __('Custom Text', 'pwh-dcfh')
            ];
            $options[] = [
                'type' => 'subnavtab-end',
            ];
            $options = $this->general_fields($options);
            $options = $this->smtp_fields($options);
            $options = $this->spam_protection_fields($options);
            $options = $this->cronjobs_fields($options);
            $options = $this->custom_label_text_fields($options);
            $options[] = [
                'name' => 'wrap-pwh-dcfh-epanel',
                'type' => 'contenttab-wrapend',
            ];

            return $options;
        }

        /**
         * @param $options
         *
         * @return mixed
         */
        private function general_fields($options)
        {
            $options[] = [
                'name' => 'pwh-dcfh-tab-miscellaneous',
                'type' => 'subcontent-start',
            ];
            $options[] = [
                'name' => __('Enable Contact Forms Stats', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_stats_enabled',
                'type' => 'checkbox',
                'desc' => __('Choose to show contact form data stats widget on admin dashboard.', 'pwh-dcfh'),
                'std' => 'off',
            ];
            $options[] = [
                'name' => __('Enable Sent Email Logs', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_enable_sent_email_log',
                'type' => 'checkbox',
                'desc' => __('Choose to log email sent history to submitter from send email page.', 'pwh-dcfh'),
                'std' => 'off',
            ];
            $options[] = [
                'name' => __('Enable Clone Logs', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_enable_clone_log',
                'type' => 'checkbox',
                'desc' => __('Choose to log clone history of contact form data to other post types.', 'pwh-dcfh'),
                'std' => 'off',
            ];
            $options[] = [
                'name' => __('Remove Plugin Data When Uninstalled', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_delete_plugin_data',
                'type' => 'checkbox',
                'desc' => __('This setting will remove all plugin data and saved settings when you uninstall the plugin. Please use this setting carefully with the understanding that if you enable the setting you will lose all saved settings if you uninstall the plugin.', 'pwh-dcfh'),
                'std' => 'off',
            ];
            $options[] = [
                'name' => __('Allowed MIME Types', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_custom_mimes_types',
                'type' => 'textarea',
                'desc' => __('Enter any MIME types that you want to enabled on your WordPress site.', 'pwh-dcfh'),
                'std' => 'psd|image/vnd.adobe.photoshop,rar|application/x-rar',
                'validation_type' => 'nohtml'
            ];
            $options[] = [
                'name' => __('Optional Entry List Columns', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_entry_list_columns',
                'type' => 'text',
                'desc' => __('Enter any field ID from the contact form(s) which will be added as additional columns in the list of entries. You can add multiple IDs as needed separated by commas.', 'pwh-dcfh'),
                'std' => 'name',
                'validation_type' => 'nohtml'
            ];
            $options[] = [
                'name' => __('Entries Date Format', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_entries_date_format',
                'type' => 'text',
                'desc' => __('Enter a custom date format for the contact form entries using your preferred PHP parameters.', 'pwh-dcfh'),
                'std' => 'Y/m/d',
                'validation_type' => 'nohtml'
            ];
            $options[] = [
                'name' => __('Entries Time Format', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_entries_time_format',
                'type' => 'text',
                'desc' => __('Enter a custom time format for the contact form entries using your preferred PHP parameters.', 'pwh-dcfh'),
                'std' => 'h:i:sa',
                'validation_type' => 'nohtml'
            ];
            $options[] = [
                'name' => __('Merge Tag Date Format', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_mergetag_date_format',
                'type' => 'text',
                'desc' => __('Enter a custom date format for the contact form message merge tags using your preferred PHP parameters.', 'pwh-dcfh'),
                'std' => 'Y-m-d',
                'validation_type' => 'nohtml'
            ];
            $options[] = [
                'name' => __('Merge Tag Time Format', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_mergetag_time_format',
                'type' => 'text',
                'desc' => __('Enter a custom time format for the contact form message merge tags using your preferred PHP parameters.', 'pwh-dcfh'),
                'std' => 'h:i:sa',
                'validation_type' => 'nohtml'
            ];
            $options[] = [
                'name' => 'pwh-dcfh-tab-miscellaneous',
                'type' => 'subcontent-end',
            ];

            return $options;
        }

        /**
         * @param $options
         *
         * @return mixed
         */
        private function smtp_fields($options)
        {
            $options[] = [
                'name' => 'pwh-dcfh-tab-smtp',
                'type' => 'subcontent-start',
            ];
            $options[] = [
                'name' => __('Enable SMTP Settings', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_smtp_enabled',
                'type' => 'checkbox2',
                'desc' => __('Choose to use SMTP configuration when sending emails WordPress must utilise a legitimate SMTP provider.', 'pwh-dcfh'),
                'std' => 'off',
            ];
            $options[] = [
                'name' => __('SMTP Host', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_smtp_host',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('This is the SMTP host address provided by your hosting company.', 'pwh-dcfh'),
                'std' => '',
            ];
            $options[] = [
                'name' => __('SMTP From Email', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_smtp_from_email',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('This is the email address which will be used to send all WordPress emails.', 'pwh-dcfh'),
                'std' => get_bloginfo('admin_email'),
            ];
            $options[] = [
                'name' => __('SMTP From Name', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_smtp_from_name',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('This name will be used to send emails. We recommend using your website’s title as from name.', 'pwh-dcfh'),
                'std' => get_bloginfo('name'),
            ];
            $options[] = [
                'name' => __('Encryption', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_smtp_encryption',
                'type' => 'select',
                'options' => [
                    'none' => __('None', 'pwh-dcfh'),
                    'ssl' => __('SSL', 'pwh-dcfh'),
                    'tls' => __('TLS', 'pwh-dcfh'),
                ],
                'et_save_values' => true,
                'desc' => __('The encryption method used by your mail server to send emails. Usually it is TLS.', 'pwh-dcfh'),
                'std' => 'none',
            ];
            $options[] = [
                'name' => __('SMTP Port', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_smtp_port',
                'type' => 'text',
                'validation_type' => 'number',
                'desc' => __('This is the port used by outgoing mail server.', 'pwh-dcfh'),
                'std' => '25',
            ];
            $options[] = [
                'name' => __('Auto TLS', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_smtp_autotls',
                'type' => 'checkbox2',
                'desc' => __('By default, TLS encryption is automatically used if the server supports it (recommended). In some cases, due to server misconfigurations, this can cause issues and may need to be disabled.',
                    'pwh-dcfh'),
                'std' => 'off',
            ];
            $options[] = [
                'name' => __('Authentication', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_smtp_authentication',
                'type' => 'checkbox2',
                'desc' => __('Email senders (clients) must have permission to use the email server in order to use SMTP authentication. Outgoing messages can only be sent by authorised users.', 'pwh-dcfh'),
                'std' => 'off',
            ];
            $options[] = [
                'name' => __('SMTP Username', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_smtp_username',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('This is usually the email address you are using to send emails.', 'pwh-dcfh'),
                'std' => '',
            ];
            $options[] = [
                'name' => __('SMTP Password', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_smtp_password',
                'type' => 'password',
                'validation_type' => 'nohtml',
                'desc' => __('This is the password for the email account you are using to send emails.', 'pwh-dcfh'),
                'std' => '',
            ];
            $options[] = [
                'name' => '',
                'id' => 'pwh_dcfh_smtp_test',
                'type' => 'callback_function',
                'function_name' => [$this, 'render_smtp_button'],
                'desc' => '',
            ];
            $options[] = [
                'name' => 'pwh-dcfh-tab-smtp',
                'type' => 'subcontent-end',
            ];

            return $options;
        }

        /**
         * @param $options
         *
         * @return mixed
         */
        private function spam_protection_fields($options)
        {
            $options[] = [
                'name' => 'pwh-dcfh-tab-spam-protection',
                'type' => 'subcontent-start',
            ];
            $options[] = [
                'name' => __('Blacklisted Emails', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_blacklisted_emails',
                'type' => 'textarea',
                'desc' => __('Enter any specific email addresses or entire domains to block from submitting entries in the contact forms. Be sure to separate each with a comma.', 'pwh-dcfh'),
                'std' => '',
                'validation_type' => 'nohtml'
            ];
            $options[] = [
                'name' => 'pwh-dcfh-tab-spam-protection',
                'type' => 'subcontent-end',
            ];

            return $options;
        }

        /**
         * @param $options
         *
         * @return mixed
         */
        private function cronjobs_fields($options)
        {
            $options[] = [
                'name' => 'pwh-dcfh-tab-cron-jobs',
                'type' => 'subcontent-start',
            ];
            $options[] = [
                'name' => __('Auto Entries Backup', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_backup_enabled',
                'type' => 'checkbox2',
                'desc' => __('Choose to create an automatic backup of your contact form data.', 'pwh-dcfh'),
                'std' => 'off',
            ];
            $options[] = [
                'name' => __('Backup Email Address', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_backup_email',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter the email address where you want to receive the auto backup file. The default is the site administrator email address.', 'pwh-dcfh'),
                'std' => get_bloginfo('admin_email'),
            ];
            $options[] = [
                'name' => __('Backup Schedule', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_backup_schedule',
                'type' => 'select',
                'options' => [
                    'hourly' => __('Every Hour', 'pwh-dcfh'),
                    'twicedaily' => __('Every 12 Hours', 'pwh-dcfh'),
                    'daily' => __('Every Day', 'pwh-dcfh'),
                    'weekly' => __('Every Week', 'pwh-dcfh'),
                    'monthly' => __('Every Month', 'pwh-dcfh'),
                ],
                'et_save_values' => true,
                'desc' => __('Select the desired frequency schedule for the auto backup.', 'pwh-dcfh'),
                'std' => 'weekly',
            ];
            $options[] = [
                'name' => __('Auto Delete Entries', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_delete_entries_enabled',
                'type' => 'checkbox2',
                'desc' => __('Choose to automatically delete the contact form entries after a specific number of days.', 'pwh-dcfh'),
                'std' => 'off',
            ];
            $options[] = [
                'name' => __('Delete Entries Type', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_delete_entries_status',
                'type' => 'checkbox_list',
                'usefor' => 'custom',
                'index' => -1,
                'desc' => __('Choose which type of entries should be automatically deleted after the selected number of days.', 'et_builder'),
                'options' => [
                    'published' => __('Read', 'pwh-dcfh'),
                    'draft' => __('Unread', 'pwh-dcfh'),
                    'trash' => __('Trash', 'pwh-dcfh'),
                ],
                'default' => ['published' => 'on', 'trash' => 'on'],
                'validation_type' => 'on_off_array',
                'et_save_values' => true,
            ];
            $options[] = [
                'name' => __('Delete Entries After Number Of Days', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_delete_entries_schedule',
                'type' => 'text',
                'validation_type' => 'number',
                'desc' => __('Enter the number of days after the entries are received to when they should be automatically deleted.', 'pwh-dcfh'),
                'std' => '',
            ];
            $options[] = [
                'name' => 'pwh-dcfh-tab-cron-jobs',
                'type' => 'subcontent-end',
            ];

            return $options;
        }

        /**
         * @param $options
         *
         * @return mixed
         */
        private function custom_label_text_fields($options)
        {
            $options[] = [
                'name' => 'pwh-dcfh-tab-labels',
                'type' => 'subcontent-start',
            ];
            $options[] = [
                'name' => '',
                'id' => 'pwh_dcfh_divider_1',
                'type' => 'callback_function',
                'function_name' => function () {
                    echo sprintf('<b>%s</b>', __('File Upload Custom Labels', 'pwh-dcfh')); // phpcs:ignore
                },
                'desc' => '',
            ];
            $options[] = [
                'name' => __('Upload File Button Text', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_file_upload_btn_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for upload file button.', 'pwh-dcfh'),
                'std' => 'Choose Files',
            ];
            $options[] = [
                'name' => __('Accepted File Types Text', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_accepted_file_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for accepted file types.', 'pwh-dcfh'),
                'std' => 'Accepted file types:',
            ];
            $options[] = [
                'name' => __('Max File Size Text', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_max_filesize_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for max file size.', 'pwh-dcfh'),
                'std' => 'Max. file size:',
            ];
            $options[] = [
                'name' => __('No Files Chosen Text', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_chosen_file_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for no file chosen.', 'pwh-dcfh'),
                'std' => 'No Files Chosen',
            ];
            $options[] = [
                'name' => __('Number Of Chosen Files Text', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_selected_files_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for selected files.', 'pwh-dcfh'),
                'std' => '{chosen_files_count} file{_s_} selected',
            ];
            $options[] = [
                'name' => __('Uploading Files Text', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_uploading_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text while uploading files.', 'pwh-dcfh'),
                'std' => '{percentage} uploaded your files, please wait for the system to continue.',
            ];
            $options[] = [
                'name' => __('File Already Attached Text', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_already_attached_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for files already attached.', 'pwh-dcfh'),
                'std' => 'File {filename} has already attached. Please choose another file.',
            ];
            $options[] = [
                'name' => __('File Size Warning Text', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_allow_filesize_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for maximum allow file size.', 'pwh-dcfh'),
                'std' => 'File {filename} not uploaded. Maximum file size {allowed_filesize}.',
            ];
            $options[] = [
                'name' => __('Number Of Allowed Files Text', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_x_files_allow_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for allow to upload only number of files.', 'pwh-dcfh'),
                'std' => 'Only {allowed_files} files are allowed to upload.',
            ];
            $options[] = [
                'name' => __('Remove File Text', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_remove_file_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for remove file.', 'pwh-dcfh'),
                'std' => 'Are you sure you want to remove {filename}?',
            ];
            $options[] = [
                'name' => __('Delete File Icon Hover Text', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_delete_file_icon_hover_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for delete file icon hover text.', 'pwh-dcfh'),
                'std' => 'Remove an attachment',
            ];
            $options[] = [
                'name' => __('Not Alllowed To Upload', 'pwh-dcfh'),
                'id' => 'security_reason_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for file type not allowed to upload.', 'pwh-dcfh'),
                'std' => 'File {filename} has failed to upload. Sorry, this file type is not permitted for security reasons.',
            ];
            $options[] = [
                'name' => __('Filesize To Upload', 'pwh-dcfh'),
                'id' => 'pwh_dcfh_allowedfilesize_text',
                'type' => 'text',
                'validation_type' => 'nohtml',
                'desc' => __('Enter custom text for max file size allowed to upload.', 'pwh-dcfh'),
                'std' => 'File {filename} not uploaded. Maximum file size {filesize}',
            ];
            $options[] = [
                'name' => 'pwh-dcfh-tab-labels',
                'type' => 'subcontent-end',
            ];

            return $options;
        }

        /**
         * SMTP Test Button HTML
         */
        public function render_smtp_button()
        {
            ?>
            <button type="button" id="pwh-dcfh-smtp-button" class="et-button pwh-dcfh-smtp-button">
                <?php esc_html_e('Check SMTP Integration Working', 'pwh-dcfh'); ?>
            </button><p id="pwh-dcfh-smtp-response" class="pwh-dcfh-smtp-response"></p>
            <?php
        }

        /**
         * Get Wp Mail Errors
         *
         * @param $wp_error
         */
        public function get_wp_mail_failed_error($wp_error)
        {
            if (isset($wp_error->errors)) {
                $wp_mail_failed = isset($wp_error->errors['wp_mail_failed']) ? $wp_error->errors['wp_mail_failed'] : null;
                if (!empty($wp_mail_failed)) {
                    $this->wp_mail_failed = implode(', ', $wp_mail_failed);
                }
            }
        }

        /**
         * Process SMTP Intgeration Test
         */
        public function process_smtp_test()
        {
            if (isset($_REQUEST['_wpnonce']) && !empty($_REQUEST['_wpnonce'])) {
                $_wpnonce = sanitize_text_field($_REQUEST['_wpnonce']);
                if (wp_verify_nonce($_wpnonce, 'admin-ajax-nonce')) {
                    $smtp_settings = get_option('et_divi');
                    if (isset($smtp_settings['pwh_dcfh_smtp_enabled'])) {
                        $errors = [];
                        if ('on' !== trim($smtp_settings['pwh_dcfh_smtp_enabled'])) {
                            $errors[] = __('Please save changes.', 'pwh-dcfh');
                        }
                        if ('' === trim($smtp_settings['pwh_dcfh_smtp_host'])) {
                            $errors[] = __('SMTP Host required.', 'pwh-dcfh');
                        }
                        if ('on' === trim($smtp_settings['pwh_dcfh_smtp_authentication'])) {
                            if ('' === trim($smtp_settings['pwh_dcfh_smtp_username']) && '' === trim($smtp_settings['pwh_dcfh_smtp_password'])) {
                                $errors[] = __('SMTP SMTP Username & Password required.', 'pwh-dcfh');
                            }
                        }
                        if (!empty($errors)) {
                            $smtp_settings['pwh_dcfh_smtp_enabled'] = false;
                            update_option('et_divi', $smtp_settings);
                            wp_send_json_error(implode('<br>', $errors));
                        }
                        if (empty($errors)) {
                            $admin_email = get_bloginfo('admin_email');
                            $blog_name = get_bloginfo('name');
                            if (wp_mail($admin_email, __('SMTP Integrated', 'pwh-dcfh'), sprintf(__('Congrats, SMTP has been integrated on your site %s successfully!', 'pwh-dcfh'), $blog_name))) {
                                wp_send_json_success(sprintf(__('Test email is sent at %s successfully! Please check your inbox to make sure it was delivered.', 'pwh-dcfh'), $admin_email));
                            } else {
                                $failed_reason = $this->wp_mail_failed;
                                if (!empty($failed_reason)) {
                                    wp_send_json_error(make_clickable($failed_reason));
                                }
                            }
                        }
                    }
                }
            }
        }

        /**
         * When Divi Theme Settings Update
         *
         * @param $old_value
         * @param $value
         * @param $option
         *
         * @return void
         */
        public function maybe_settings_update($old_value, $value, $option)
        {
            if (isset($old_value['pwh_dcfh_backup_schedule'])) {
                wp_clear_scheduled_hook('pwh_dcfh_backup_entries_'.$old_value['pwh_dcfh_backup_schedule']);
            }
            if (isset($old_value['pwh_dcfh_delete_entries_schedule'])) {
                wp_clear_scheduled_hook('pwh_dcfh_delete_entries_'.$old_value['pwh_dcfh_delete_entries_schedule']);
            }
        }
    }
}