"ehdi_sanitize_api_key"] ); register_setting( "ehdi_settings_group", "ehdi_selected_company_id", ["sanitize_callback" => "ehdi_sanitize_company_id"] ); register_setting("ehdi_settings_group", "ehdi_dni_script", [ "sanitize_callback" => "ehdi_sanitize_dni_script" ]); register_setting("ehdi_settings_group", "ehdi_wrap_script", [ "default" => 0, "sanitize_callback" => "ehdi_sanitize_wrap_script" ]); // API Section add_settings_section( "ehdi_api_section", "API Configuration", "ehdi_api_section_callback", "eight_hundred_dni_injector" ); add_settings_field( "ehdi_api_key_field", "800.com API Key", "ehdi_api_key_field_render", "eight_hundred_dni_injector", "ehdi_api_section" ); // Company Selection Section add_settings_section( "ehdi_company_section", "Company Selection", "ehdi_company_section_callback", "eight_hundred_dni_injector" ); add_settings_field( "ehdi_company_select_field", "Select Company", "ehdi_company_select_field_render", "eight_hundred_dni_injector", "ehdi_company_section" ); // DNI Script Display Section add_settings_section( "ehdi_script_display_section", "Fetched DNI Script", "ehdi_script_display_section_callback", "eight_hundred_dni_injector" ); add_settings_field( "ehdi_script_display_field", "Current DNI Script", "ehdi_script_display_field_render", "eight_hundred_dni_injector", "ehdi_script_display_section" ); add_settings_field( "ehdi_wrap_script_field", "Script Injection Method", "ehdi_wrap_script_field_render", "eight_hundred_dni_injector", "ehdi_script_display_section" ); } add_action('admin_init', 'ehdi_settings_init'); /** * Callback for API section. */ function ehdi_api_section_callback() { ?>
Enter your 800.com API key below obtained from your Integrations page inside of 800.com. This will be used to configure DNI
Please enter and save your API key above to fetch and select a company.'); } else { echo wp_kses_post('Select the company for which you want to inject the DNI script.
'); // Company list will be populated here via another function later } } /** * Render Company Select field. * Fetches companies if API key is available and populates dropdown. */ function ehdi_company_select_field_render() { $api_key = get_option("ehdi_api_key"); $selected_company_id = get_option("ehdi_selected_company_id"); if (empty($api_key)) { echo '' . esc_html("API Key not set. Cannot fetch companies.") . '
'; return; } $companies_result = ehdi_fetch_companies($api_key); if (is_wp_error($companies_result)) { echo ''; // Still show the currently selected ID if one exists if ($selected_company_id) { echo '' . esc_html("Currently saved company ID:") . ' ' . esc_html($selected_company_id) . '
'; // Provide a hidden input so the value isn't lost on save if API fails temporarily echo ''; } return; } if (empty($companies_result)) { echo '' . esc_html("No companies found or API response was empty.") . '
'; return; } // We have companies, render the select dropdown ?>%s
The following DNI script has been fetched based on your selected company and will be injected into the <head> section of all frontend pages.
Current DNI Script:
'; // Add error display area (separate from textarea) echo ''; // Add a textarea for the actual form input with smaller, more reasonable sizing $rows = $has_script ? '6' : '3'; $placeholder = $has_script ? '' : 'No script loaded. Select a company above to fetch the DNI script.'; echo ''; // Add description echo '' . esc_html('This script is automatically fetched when you select a company above. It will be injected into your website pages.') . '
'; // Add spinner for auto-refresh functionality echo ''; echo '
array( 'type' => array(), 'src' => array(), 'async' => array(), 'defer' => array(), 'id' => array(), 'class' => array(), 'data-*' => true, // Allow data attributes 'crossorigin' => array(), 'integrity' => array(), 'nonce' => array(), ) ); return wp_kses($input, $allowed_html); } /** * Sanitize callback for wrap script option. * * @param mixed $input The value submitted via the form. * @return int The sanitized wrap script value. */ function ehdi_sanitize_wrap_script($input) { return absint($input); } /** * Enqueue admin scripts for the settings page. */ /** * Enqueue admin scripts for the settings page. * * Note: This function uses $_GET['page'] for script enqueueing only, not for processing form data * that changes anything on the server, so nonce verification is not required here. */ function ehdi_enqueue_admin_scripts($hook_suffix) { // Only load on our specific settings page if ($hook_suffix === 'settings_page_eight_hundred_dni_injector') { // We're on the settings page, enqueue scripts } else { // Check if we're on the options.php page after saving settings $screen = get_current_screen(); if ($screen && $screen->id === 'options') { // We might be on options.php after saving our plugin settings // Get the page parameter safely $page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS); if ($page !== 'eight_hundred_dni_injector') { return; } } else { return; } } wp_enqueue_script( 'ehdi-admin-script', plugin_dir_url(__FILE__) . 'admin-script.js', ['jquery'], // Dependency EHDI_PLUGIN_VERSION, // Use defined version true // Load in footer ); // Localize script to pass data like nonce and ajax url wp_localize_script('ehdi-admin-script', 'ehdi_ajax_object', [ 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('ehdi_refresh_script_nonce'), 'loading_text' => 'Loading...', 'error_text' => 'Error fetching script.', 'no_company_text' => 'Please select a company first.', ]); // Enqueue admin styles wp_enqueue_style( 'ehdi-admin-style', plugin_dir_url(__FILE__) . 'admin-style.css', [], // No dependencies EHDI_PLUGIN_VERSION // Use defined version ); } add_action('admin_enqueue_scripts', 'ehdi_enqueue_admin_scripts'); /** * AJAX handler for refreshing the DNI script. */ function ehdi_ajax_refresh_script_handler() { // Verify nonce check_ajax_referer('ehdi_refresh_script_nonce', 'nonce'); // Check user capabilities if (!current_user_can('manage_options')) { wp_send_json_error(['message' => 'Permission denied.'], 403); } // Get API key $api_key = get_option('ehdi_api_key'); // Get company ID from request if provided, otherwise use the saved one $company_id = isset($_POST['company_id']) && !empty($_POST['company_id']) ? sanitize_text_field(wp_unslash($_POST['company_id'])) : get_option('ehdi_selected_company_id'); if (empty($company_id)) { wp_send_json_error(['message' => 'No company selected. Please select a company from the dropdown.'], 400); } if (empty($api_key)) { wp_send_json_error(['message' => 'API Key is missing.'], 400); } // Fetch the script $script_result = ehdi_fetch_dni_script($company_id, $api_key); if (is_wp_error($script_result)) { wp_send_json_error(['message' => $script_result->get_error_message()], 500); } else { // Force the script to be a string if it's not already if (!is_string($script_result)) { $script_result = (string)$script_result; } // Send success response with the script content for preview only // Do not save to database - that will happen when user clicks "Save Settings" wp_send_json_success(['script' => $script_result]); } } add_action('wp_ajax_ehdi_refresh_script', 'ehdi_ajax_refresh_script_handler'); ?>