plugin = $plugin; } public function render_faq_shortcode($atts) { $output = $this->build_output($atts); /** * Filters the HTML printed by [dotes_faq] and by the dotes-faq/faq block. * * @param string $output HTML. * @param array|string $atts Raw shortcode attributes. */ return apply_filters('dotes_faq_render_output', $output, $atts); } /** * Build the shortcode HTML. * * @param array|string $atts Raw shortcode attributes. * @return string */ private function build_output($atts) { global $wpdb; // Get plugin settings for default values $settings = $this->plugin->settings->get_current_settings(); $default_limit = isset($settings['items_per_page']) ? intval($settings['items_per_page']) : 10; // Display mode precedence: per-FAQ setting > toggle="" attribute > global default. $explicit_toggle = is_array($atts) && !empty($atts['toggle']); $atts = shortcode_atts(array( 'id' => '', 'category' => '', 'title' => 'FAQ', 'limit' => $default_limit, 'featured' => false, 'search' => !empty($settings['show_search']), 'toggle' => 'accordion' ), $atts, 'dotes_faq'); if (!$explicit_toggle) { $atts['toggle'] = isset($settings['default_mode']) ? $settings['default_mode'] : 'accordion'; } $atts['toggle'] = sanitize_key($atts['toggle']); // Check if ID refers to a group or individual item if (!empty($atts['id'])) { // First check if it's a group $group = $wpdb->get_row($wpdb->prepare( "SELECT g.*, c.name as category_name, c.color as category_color FROM {$wpdb->prefix}dotes_faq_groups g LEFT JOIN {$wpdb->prefix}dotes_faq_categories c ON g.category_id = c.id WHERE g.id = %d AND g.is_active = 1", intval($atts['id']) )); if ($group) { // Get all questions in this group $faqs = $wpdb->get_results($wpdb->prepare( "SELECT f.*, c.name as category_name, c.color as category_color FROM {$wpdb->prefix}dotes_faq_items f LEFT JOIN {$wpdb->prefix}dotes_faq_categories c ON f.category_id = c.id WHERE f.faq_group_id = %d AND f.is_active = 1 ORDER BY f.sort_order ASC", $group->id )); if ($faqs) { return $this->plugin->renderer->render_faq_group_html($group, $faqs, $atts); } } else { // Check if it's an individual FAQ item (legacy support) $faq = $wpdb->get_row($wpdb->prepare( "SELECT f.*, c.name as category_name, c.color as category_color FROM {$wpdb->prefix}dotes_faq_items f LEFT JOIN {$wpdb->prefix}dotes_faq_categories c ON f.category_id = c.id WHERE f.id = %d AND f.is_active = 1", intval($atts['id']) )); if ($faq) { return $this->plugin->renderer->render_single_faq_html($faq, $atts); } } return '

' . esc_html__('FAQ não encontrada.', 'dotes-faq-generator') . '

'; } // Listing: optional filters are switched on/off by flags so the SQL stays a single literal. // The category accepts the category ID or its slug (as documented in the help tab). $category_id = !empty($atts['category']) ? $this->plugin->renderer->resolve_category_id($atts['category']) : 0; $filter_category = !empty($atts['category']) ? 1 : 0; $only_featured = ($atts['featured'] === 'true' || $atts['featured'] === true) ? 1 : 0; $faqs = $wpdb->get_results($wpdb->prepare( "SELECT f.*, GREATEST(f.featured, COALESCE(g.featured, 0)) AS featured, c.name as category_name, c.color as category_color FROM {$wpdb->prefix}dotes_faq_items f LEFT JOIN {$wpdb->prefix}dotes_faq_groups g ON f.faq_group_id = g.id LEFT JOIN {$wpdb->prefix}dotes_faq_categories c ON f.category_id = c.id WHERE f.is_active = 1 AND (g.id IS NULL OR g.is_active = 1) AND (%d = 0 OR f.category_id = %d) AND (%d = 0 OR f.featured = 1 OR g.featured = 1) ORDER BY featured DESC, f.sort_order ASC, f.id DESC LIMIT %d", $filter_category, $category_id, $only_featured, intval($atts['limit']) )); /** * Filters the FAQ items listed by [dotes_faq] (without "id"). * * @param array $faqs Item rows. * @param array $atts Shortcode attributes. */ $faqs = (array) apply_filters('dotes_faq_query', $faqs, $atts); if (empty($faqs)) { return ''; } $settings = $this->plugin->settings->get_current_settings(); // Determine display mode (single-FAQ shortcodes return earlier) $display_mode = $this->plugin->valid_display_mode(apply_filters('dotes_faq_display_mode', $atts['toggle'], null, $atts)); // Start building output $output = '
'; // Add title if provided (title="false" hides it, as in the FAQ groups). if (!empty($atts['title']) && 'false' !== $atts['title']) { $output .= '

' . esc_html($atts['title']) . '

'; } // Add search if enabled if ($atts['search'] === 'true' || $atts['search'] === true) { $output .= '
'; $output .= ''; $output .= '
'; } // Add FAQ items $output .= '
'; // Generate unique ID for this shortcode instance $shortcode_unique_id = 'dotes-faq-' . uniqid(); $item_counter = 0; foreach ($faqs as $faq) { $item_counter++; $unique_item_id = $shortcode_unique_id . '-item-' . $faq->id . '-' . $item_counter; $unique_answer_id = $unique_item_id . '-answer'; $output .= '
'; // Question $output .= '
'; $output .= ''; $output .= '
'; // Answer $output .= ''; $output .= '
'; } $output .= '
'; $output .= '
'; // Add frontend CSS $output .= $this->plugin->inline_assets->get_frontend_css($settings, $faqs); // Add frontend JavaScript $output .= $this->plugin->inline_assets->get_frontend_js($settings, $display_mode); return $output; } }