plugin_name = $plugin_name; $this->version = $version; $this->api = $api; } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Accessibility_Assistant_Loader as all of the hooks are defined * in that particular class. * * The Accessibility_Assistant_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/accessibility-assistant-public.css', array(), $this->version, 'all'); } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Accessibility_Assistant_Loader as all of the hooks are defined * in that particular class. * * The Accessibility_Assistant_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/accessibility-assistant-public.js', array('jquery'), $this->version, false); } public function accessibility_addjsfooter() { $base_url = ASSISTANT_API_URL . '/wordpress/script'; $site_id = sanitize_text_field(get_option('accessibility_shopid')); if (empty($site_id)) { return; } $widgetSettings = []; $transient_key = 'accessibility_widget_data_' . $site_id; $cached_data = get_transient($transient_key); // 1. Try to use cached data if ($cached_data !== false && is_array($cached_data) && !empty($cached_data)) { $widgetSettings = $cached_data; } else { // 2. Cache miss or invalid - Fetch from API $data = array('shopid' => $site_id); // Use a short timeout for the frontend to prevent long blocking // We can't easily change timeout in the current API class without modifying it, // but the current default is 30s. ideally we'd want shorter here. $api_response = $this->api->call('/NewGetShopData', $data, 'get'); if ($api_response && isset($api_response['data']) && is_array($api_response['data'])) { // 3. Success - Process and Cache $settings_data = $api_response['data']; $widgetSettings = $this->map_widget_settings($settings_data); // Save to Transient (6 hours) set_transient($transient_key, $widgetSettings, 6 * HOUR_IN_SECONDS); // Save to Backup Option (Permanent) update_option('accessibility_widget_backup_' . $site_id, $widgetSettings); } else { // 4. API Failure - Fallback Strategy // A. Try Backup $backup_data = get_option('accessibility_widget_backup_' . $site_id); if ($backup_data && is_array($backup_data) && !empty($backup_data)) { $widgetSettings = $backup_data; } else { // B. No Backup - Fail Gracefully (Empty settings will prevent widget render) $widgetSettings = []; } // Set a short "failure cache" to prevent retrying every request immediately // e.g. 5 minutes set_transient($transient_key, $widgetSettings, 5 * MINUTE_IN_SECONDS); } } if (empty($widgetSettings) || empty($widgetSettings['status'])) { return; } ?> display_name); } /** * Map raw API data to widget settings format. * * @param array $widget_settings Raw data from API. * @return array Formatted settings. */ private function map_widget_settings($widget_settings) { // Ensure nested indexes exist to prevent warnings $widget_settings['shop_text'] = $widget_settings['shop_text'] ?? []; // Helper closures for class names (moved from inside accessibility_addjsfooter) $get_desktop_class = function($position) { switch ($position) { case 1: return "aa-cc-top-left"; case 2: return "aa-cc-top-right"; case 3: return "aa-cc-center-left"; case 4: return "aa-cc-center-right"; case 5: return "aa-cc-bottom-left"; case 6: return "aa-cc-bottom-right"; default: return "aa-cc-bottom-right"; } }; $get_mobile_class = function($position) { switch ($position) { case 1: return "aa-mm-cc-top-left"; case 2: return "aa-mm-cc-top-right"; case 3: return "aa-mm-cc-center-left"; case 4: return "aa-mm-cc-center-right"; case 5: return "aa-mm-cc-bottom-left"; case 6: return "aa-mm-cc-bottom-right"; default: return "aa-mm-cc-bottom-right"; } }; return [ "status" => !empty($widget_settings['status']), "show_loader" => isset($widget_settings['button_link_status']) && (int)$widget_settings['button_link_status'] === 1, "desktop_position" => $get_desktop_class($widget_settings['position'] ?? 6), "mobile_position" => $get_mobile_class($widget_settings['mobile_position'] ?? 6), // SAFE: shop_text exists? widget_size exists? else default 0 "size" => isset($widget_settings['shop_text']['widget_size']) ? (int)$widget_settings['shop_text']['widget_size'] : 0, "bottom_padding" => $widget_settings['bottom_padding'] ?? 0, "widget_outercolor" => $widget_settings['widget_outer_color'] ?? '#000', "widget_innercolor" => $widget_settings['widget_inner_color'] ?? '#fff', "svg" => $widget_settings['widget_icon'] ?? '', // Prevent undefined array key "plan" "plan_name" => $widget_settings['plan'] ?? '', // Prevent undefined array key "hover_text" "widget_title_on_hover" => isset($widget_settings['shop_text']['hover_text']) ? $widget_settings['shop_text']['hover_text'] : '' ]; } }