label. */ public static function get_available_layouts() { $layouts = apply_filters( 'tcaccordion_layouts', array( 'accordion' => __( 'Accordion (default, stacked)', 'accordions-wp' ), 'two-column' => __( 'Two-column FAQ', 'accordions-wp' ), ) ); return is_array( $layouts ) && isset( $layouts['accordion'] ) ? $layouts : array( 'accordion' => __( 'Accordion (default, stacked)', 'accordions-wp' ) ); } /** * Whether the given accordion is being rendered right now (used by add-ons to avoid loops). * * @param int $post_id Accordion ID. * @return bool */ public static function is_rendering( $post_id ) { return in_array( absint( $post_id ), self::$render_stack, true ); } /** * Read one setting the same way the renderer does (saved value, placement override or default). * * @param string $key Setting key. * @param string $default Default value. * @return mixed */ public function get_setting( $key, $default = '' ) { return $this->get_meta_value( $key, $default ); } /** * Render these items instead of a saved accordion (used by the Gutenberg Inline block). * * @param array $items List of array( 'title' => string, 'description' => HTML ). * @return void */ public function set_items( $items ) { $this->injected_items = is_array( $items ) ? $items : array(); } /** * Force the wrapper element ID (needed when there is no accordion post ID). * * @param string $scope_id Unique element ID. * @return void */ public function set_scope_id( $scope_id ) { $this->scope_override = sanitize_html_class( $scope_id ); } /** * Override saved settings for this one placement (shortcode attributes, Gutenberg block). * * @param array $overrides Values keyed by meta key, already validated by the caller. * @return void */ public function set_overrides( $overrides ) { $this->overrides = is_array( $overrides ) ? $overrides : array(); } /** * Mark this renderer as a nested accordion rendered inside a parent item. * * @param string $scope_id Unique wrapper ID. * @param string $theme Theme class inherited from the parent. * @param int $depth Nesting depth. * @return void */ public function set_nested_context( $scope_id, $theme, $depth ) { $this->scope_override = sanitize_html_class( $scope_id ); $this->theme_override = sanitize_html_class( $theme ); $this->depth = max( 1, absint( $depth ) ); } /** * Constructor. * * @param int $post_id Post ID to render. */ public function __construct( $post_id = 0 ) { $this->post_id = absint( $post_id ); } /** * Inject live unsaved form data for instant preview rendering. * * @param array $data Serialized form values from AJAX. * @return void */ public function set_preview_data( array $data ) { $this->preview_data = $data; $this->is_preview = true; } /** * Retrieve meta value checking preview data, primary key, and fallback meta keys. * * @param string $key Primary form input name / meta key. * @param mixed $default Default fallback value. * @param array $fallback_keys Alternative meta keys used in older database schemas. * @return mixed Meta value. */ private function get_meta_value( $key, $default = '', $fallback_keys = array() ) { // 0. Per-placement overrides (shortcode attributes / Gutenberg block). if ( isset( $this->overrides[ $key ] ) && '' !== $this->overrides[ $key ] ) { return $this->overrides[ $key ]; } // 1. Check Live Preview Data (AJAX) if ( $this->is_preview ) { if ( isset( $this->preview_data[ $key ] ) && '' !== $this->preview_data[ $key ] ) { return $this->preview_data[ $key ]; } foreach ( $fallback_keys as $alt_key ) { if ( isset( $this->preview_data[ $alt_key ] ) && '' !== $this->preview_data[ $alt_key ] ) { return $this->preview_data[ $alt_key ]; } } } // 2. Check Saved Post Meta on Frontend if ( $this->post_id ) { $meta = get_post_meta( $this->post_id, $key, true ); if ( '' !== $meta && false !== $meta ) { return $meta; } // Check alternative keys if primary key returns empty foreach ( $fallback_keys as $alt_key ) { $alt_meta = get_post_meta( $this->post_id, $alt_key, true ); if ( '' !== $alt_meta && false !== $alt_meta ) { return $alt_meta; } } } return $default; } /** * Main Render Method. * * @return string Rendered HTML output. */ public function render() { if ( ! $this->post_id && ! $this->is_preview && empty( $this->injected_items ) ) { return ''; } $items = $this->get_accordion_items(); if ( empty( $items ) ) { return '

' . esc_html__( 'Nothing Found!!', 'accordions-wp' ) . '

'; } $styles = $this->get_style_settings(); $theme = $this->get_theme_class(); self::$render_stack[] = $this->post_id; $html = $this->build_html( $items, $styles, $theme ); array_pop( self::$render_stack ); return $html; } /** * Retrieve and normalize accordion repeater items. * * @return array Normalized accordion items. */ private function get_accordion_items() { // Items handed in directly (Gutenberg Inline block). if ( ! empty( $this->injected_items ) ) { $injected = array(); foreach ( $this->injected_items as $row ) { if ( is_array( $row ) ) { $injected[] = $this->extract_item_data( $row ); } } return array_filter( $injected ); } $raw_features = array(); if ( $this->is_preview && isset( $this->preview_data['custom_accordion_wordpresspro_columns'] ) ) { $raw_features = $this->preview_data['custom_accordion_wordpresspro_columns']; } elseif ( $this->post_id ) { $raw_features = get_post_meta( $this->post_id, 'custom_accordion_wordpresspro_columns', true ); if ( empty( $raw_features ) ) { $raw_features = get_post_meta( $this->post_id, 'custom_accordion_wordpresspro_columns', false ); } } $items = array(); if ( ! empty( $raw_features ) && is_array( $raw_features ) ) { foreach ( $raw_features as $row ) { if ( ! is_array( $row ) ) { continue; } if ( isset( $row[0] ) && is_array( $row[0] ) ) { foreach ( $row as $sub_item ) { if ( is_array( $sub_item ) ) { $items[] = $this->extract_item_data( $sub_item ); } } } else { $items[] = $this->extract_item_data( $row ); } } } /** * Lets add-ons replace the items (for example to build them from posts). * * @param array $items Items read from the accordion. * @param TCAccordion_Renderer $renderer This renderer. */ $items = apply_filters( 'tcaccordion_items', array_filter( $items ), $this ); return is_array( $items ) ? array_values( array_filter( $items ) ) : array(); } /** * Normalize array keys across legacy and updated formats. * * @param array $item Raw meta item array. * @return array Standardized array with 'title' and 'description'. */ private function extract_item_data( $item ) { $title = ''; if ( isset( $item['title'] ) ) { $title = $item['title']; } elseif ( isset( $item['custom_accordions_pro_title'] ) ) { $title = $item['custom_accordions_pro_title']; } $description = ''; if ( isset( $item['description'] ) ) { $description = $item['description']; } elseif ( isset( $item['custom_accordions_pro_details'] ) ) { $description = $item['custom_accordions_pro_details']; } if ( empty( $title ) && empty( $description ) ) { return array(); } /** * Lets add-ons keep extra per-item data (for example a nested accordion ID). * * @param array $data Normalized item with 'title' and 'description'. * @param array $item Raw item as saved. */ return apply_filters( 'tcaccordion_item_data', array( 'title' => $title, 'description' => $description, ), $item ); } /** * Fetch custom styling options with fallback database keys. * * @return array Style values map. */ private function get_style_settings() { // Behavior settings $auto_open_val = $this->get_meta_value( '_tcacc_auto_open_item', '1', array( 'custom_accordion_auto_open' ) ); $closeable_val = $this->get_meta_value( '_tcacc_is_closeable', 'yes', array( 'custom_accordion_is_closeable' ) ); $auto_open = ( '' !== $auto_open_val ) ? absint( $auto_open_val ) : 1; $closeable = ( 'no' === $closeable_val ) ? 'no' : 'yes'; $close_others_val = $this->get_meta_value( '_tcacc_close_others', 'yes', array( 'custom_accordion_close_others' ) ); $close_others = ( 'no' === $close_others_val ) ? 'no' : 'yes'; // Open event and animation speed can be changed by an add-on. $behavior = apply_filters( 'tcaccordion_behavior_settings', array( 'open_event' => 'click', 'anim_speed' => 300, ), $this ); $open_event = ( is_array( $behavior ) && isset( $behavior['open_event'] ) && in_array( $behavior['open_event'], array( 'click', 'hover' ), true ) ) ? $behavior['open_event'] : 'click'; $anim_speed = ( is_array( $behavior ) && ! empty( $behavior['anim_speed'] ) ) ? absint( $behavior['anim_speed'] ) : 300; // Use $this->post_id instead of $post_id $icon_style = $this->get_meta_value( '_tcacc_icon_style', 'chevron' ); $icon_style = ! empty( $icon_style ) ? $icon_style : 'chevron'; // Retrieve FAQ Schema Setting (defaults to 'no') $schema_val = $this->get_meta_value( '_tcacc_enable_faq_schema', 'no', array( 'enable_faq_schema' ) ); $enable_faq_schema = ( 'yes' === $schema_val ) ? 'yes' : 'no'; // Nested accordions never output their own FAQ schema (avoids duplicate FAQPage blocks). if ( $this->depth > 1 ) { $enable_faq_schema = 'no'; } // Layout (the free plugin has accordion and two-column; add-ons can add more). $layout = $this->get_meta_value( '_tcacc_layout', 'accordion' ); $layout = array_key_exists( $layout, self::get_available_layouts() ) ? $layout : 'accordion'; // Search box, Expand/Collapse All and Deep Link settings (all default to 'no'). $show_search = ( 'yes' === $this->get_meta_value( '_tcacc_show_search', 'no' ) ) ? 'yes' : 'no'; $show_toggle_all = ( 'yes' === $this->get_meta_value( '_tcacc_show_toggle_all', 'no' ) ) ? 'yes' : 'no'; $deep_link = ( 'yes' === $this->get_meta_value( '_tcacc_deep_link', 'no' ) ) ? 'yes' : 'no'; $search_placeholder = sanitize_text_field( (string) $this->get_meta_value( '_tcacc_search_placeholder', '' ) ); if ( '' === $search_placeholder ) { $search_placeholder = __( 'Search questions...', 'accordions-wp' ); } return array( 'icon_color' => $this->get_meta_value( 'custom_accordion_icon_color', '', array( 'custom_accordion_icon_color' ) ), 'icon_bg_color' => $this->get_meta_value( 'custom_accordion_icon_bg_color', '', array( 'custom_accordion_icon_bg_color' ) ), 'title_bg' => $this->get_meta_value( 'custom_accordion_title_bg_color', '', array( '_custom_accordion_title_bg_color' ) ), 'title_color' => $this->get_meta_value( 'custom_accordion_title_font_color', '', array( '_custom_accordion_title_font_color' ) ), 'title_bg_hover' => $this->get_meta_value( 'custom_accordion_title_bg_color_hover', '' ), 'title_color_hover' => $this->get_meta_value( 'custom_accordion_title_font_color_hover', '' ), 'icon_color_hover' => $this->get_meta_value( 'custom_accordion_icon_color_hover', '' ), 'title_bg_active' => $this->get_meta_value( 'custom_accordion_title_bg_color_active', '' ), 'title_color_active' => $this->get_meta_value( 'custom_accordion_title_font_color_active', '' ), 'icon_color_active' => $this->get_meta_value( 'custom_accordion_icon_color_active', '' ), 'title_size' => $this->get_meta_value( 'custom_accordion_title_font_size', '18', array( '_custom_accordion_title_font_size' ) ), 'title_line_height' => $this->get_meta_value( 'custom_accordion_title_line_height', '1.4', array( '_custom_accordion_title_line_height' ) ), 'title_position' => $this->get_meta_value( '_tpaccpro_wiki_acc_themes_title_position', 'left', array( 'custom_accordion_title_position' ) ), 'icon_show' => $this->get_meta_value( '_tpaccpro_wiki_acc_themes_show_hide_icons', '1', array( 'custom_accordion_show_hide_icons' ) ), 'icon_position' => $this->get_meta_value( '_tpaccpro_wiki_acc_themes_icon_position', '2', array( 'custom_accordion_icon_position' ) ), 'content_bg' => $this->get_meta_value( 'custom_accordion_content_bg_color', '', array( '_custom_accordion_content_bg_color' ) ), 'content_color' => $this->get_meta_value( 'custom_accordion_content_font_color', '', array( '_custom_accordion_content_font_color' ) ), 'content_size' => $this->get_meta_value( 'custom_accordion_content_font_size', '16', array( '_custom_accordion_content_font_size' ) ), 'content_line_height' => $this->get_meta_value( 'custom_accordion_content_line_height', '1.6', array( '_custom_accordion_content_line_height' ) ), 'content_letter_space' => $this->get_meta_value( 'custom_accordion_content_letter_spacing', '0', array( '_custom_accordion_content_letter_spacing' ) ), 'item_margin' => $this->get_meta_value( '_tpaccpro_wiki_acc_theme_content_margin', '5', array( 'custom_accordion_item_margin' ) ), 'padding' => $this->get_meta_value( 'custom_accordion_content_padding', '15', array( '_custom_accordion_content_padding' ) ), 'border_style' => $this->get_meta_value( 'custom_accordion_border_style', 'none' ), 'border_width' => $this->get_meta_value( 'custom_accordion_border_width', '1' ), 'border_color' => $this->get_meta_value( 'custom_accordion_border_color', '' ), 'enable_faq_schema' => $enable_faq_schema, // Mapped to $styles['enable_faq_schema'] 'layout' => $layout, 'show_search' => $show_search, 'search_placeholder' => $search_placeholder, 'show_toggle_all' => $show_toggle_all, 'deep_link' => $deep_link, 'auto_open' => $auto_open, 'closeable' => $closeable, 'close_others' => $close_others, 'icon_style' => $icon_style, 'open_event' => $open_event, 'anim_speed' => $anim_speed, ); } /** * Get theme option class name. * * @return string Theme CSS class name. */ private function get_theme_class() { if ( '' !== $this->theme_override ) { return $this->theme_override; } $theme = $this->get_meta_value( 'custom_accordion_columns_post_themes', 'theme1', array( '_custom_accordion_columns_post_themes' ) ); return ! empty( $theme ) ? sanitize_html_class( $theme ) : 'theme1'; } /** * Generate dynamic CSS block output for shortcode and live preview. * * @param array $styles Styles map. * @param string $scope_id Unique wrapper element ID. * @return string Dynamic ' : ''; } /** * Build the CSS for the hover and open (active) states of the item headers. * * Title and icon colors are printed inline on the elements, so these rules use * !important. They target only this accordion's own items, never a nested accordion. * * @param array $styles Styles map. * @param string $selector Wrapper selector (#id). * @return string CSS rules. */ private function build_state_css( $styles, $selector ) { $head = $selector . ' > .responsive-accordion > li > .responsive-accordion-head'; $hover = $this->state_rule( array( $head . ':hover' ), $styles, 'hover' ); $focus = $this->state_rule( array( $head . ':focus-visible' ), $styles, 'hover' ); $active = $this->state_rule( array( $head . '.active' ), $styles, 'active' ); $css = ''; if ( '' !== $hover ) { // Hover colors only on devices that really hover, so touch screens don't keep a stuck hover color. $css .= '@media (hover: hover) {' . $hover . '}'; $css .= $focus; } // Printed last so the open state wins over the hover state. $css .= $active; return $css; } /** * Build the declarations for one header state. * * @param string[] $selectors Header selectors for the state. * @param array $styles Styles map. * @param string $state 'hover' or 'active'. * @return string CSS rules or an empty string. */ private function state_rule( $selectors, $styles, $state ) { $bg = ! empty( $styles[ 'title_bg_' . $state ] ) ? sanitize_hex_color( $styles[ 'title_bg_' . $state ] ) : ''; $text = ! empty( $styles[ 'title_color_' . $state ] ) ? sanitize_hex_color( $styles[ 'title_color_' . $state ] ) : ''; $icon = ! empty( $styles[ 'icon_color_' . $state ] ) ? sanitize_hex_color( $styles[ 'icon_color_' . $state ] ) : ''; $targets = array( array( '', 'background-color', $bg ), array( ' .responsive-accordion-title', 'color', $text ), array( ' .responsive-accordion-icon', 'color', $icon ), ); $out = ''; foreach ( $targets as $target ) { if ( empty( $target[2] ) ) { continue; } $list = array(); foreach ( $selectors as $selector ) { $list[] = $selector . $target[0]; } $out .= implode( ',', $list ) . '{' . $target[1] . ':' . $target[2] . ' !important;}'; } return $out; } /** * Build the CSS for the non-default layouts (desktop only, 768px and up). * On smaller screens every layout falls back to the normal stacked accordion. * * @param array $styles Styles map. * @param string $selector Wrapper selector (#id). * @return string CSS rules. */ private function build_layout_css( $styles, $selector ) { $layout = isset( $styles['layout'] ) ? $styles['layout'] : 'accordion'; $css = ''; $ul = $selector . ' > .responsive-accordion'; $mq = '@media (min-width: 768px) {'; if ( 'two-column' === $layout ) { $css .= $mq; $css .= $ul . ' { display: grid !important; grid-template-columns: repeat(2, minmax(0, 1fr)); column-gap: 20px; align-items: start; }'; $css .= '}'; } /** * Lets add-ons print the CSS of their own layouts. * * @param string $css CSS built so far for the layout. * @param string $layout Layout slug. * @param array $styles Styles map. * @param string $selector Wrapper selector (#id). * @param TCAccordion_Renderer $renderer This renderer. */ return apply_filters( 'tcaccordion_layout_css', $css, $layout, $styles, $selector, $this ); } /** * Build inline CSS styles for static element properties. * * @param array $styles Style options map. * @param string $type Target element ('head', 'head_text', or 'panel'). * @return string Safe inline style string. */ private function build_inline_css( $styles, $type ) { $css = array(); if ( 'head' === $type && ! empty( $styles['title_bg'] ) ) { $css[] = 'background-color:' . sanitize_hex_color( $styles['title_bg'] ); } if ( 'icon' === $type ) { if ( ! empty( $styles['icon_color'] ) ) { $css[] = 'color:' . sanitize_hex_color( $styles['icon_color'] ); } // Exclude background-color specifically for theme1 and theme2 $current_theme = $this->get_theme_class(); $is_theme_1_or_2 = in_array( $current_theme, array( 'theme1', 'theme2', 'theme-dark', 'theme-flat' ), true ); if ( ! $is_theme_1_or_2 && ! empty( $styles['icon_bg_color'] ) ) { $css[] = 'background-color:' . sanitize_hex_color( $styles['icon_bg_color'] ); } } if ( 'head_text' === $type ) { if ( ! empty( $styles['title_color'] ) ) { $css[] = 'color:' . sanitize_hex_color( $styles['title_color'] ); } if ( ! empty( $styles['title_size'] ) ) { $css[] = 'font-size:' . absint( $styles['title_size'] ) . 'px'; } } if ( 'panel' === $type ) { if ( ! empty( $styles['content_bg'] ) ) { $css[] = 'background-color:' . sanitize_hex_color( $styles['content_bg'] ); } if ( '' !== $styles['padding'] && null !== $styles['padding'] ) { $css[] = 'padding:' . absint( $styles['padding'] ) . 'px'; } } return ! empty( $css ) ? ' style="' . esc_attr( implode( ';', $css ) ) . '"' : ''; } /** * Sanitize and filter body details content (OEmbed, Shortcodes, HTML). * * @param string $content Raw content. * @return string Processed content. */ private function format_content( $content ) { global $wp_embed; $allowed_html = wp_kses_allowed_html( 'post' ); $allowed_html['iframe'] = array( 'src' => true, 'width' => true, 'height' => true, 'frameborder' => true, 'allow' => true, 'allowfullscreen' => true, 'title' => true, ); if ( is_object( $wp_embed ) ) { $content = $wp_embed->autoembed( $content ); $content = $wp_embed->run_shortcode( $content ); } $content = do_shortcode( $content ); $content = wp_kses( $content, $allowed_html ); return wpautop( $content ); } private function build_html( $items, $styles, $theme ) { $scope_id = '' !== $this->scope_override ? $this->scope_override : 'tc-accordion-' . ( $this->post_id ? $this->post_id : wp_rand( 100, 999 ) ); $dynamic_css = $this->build_dynamic_css( $styles, $scope_id ); $head_style = $this->build_inline_css( $styles, 'head' ); $icon_color = $this->build_inline_css( $styles, 'icon' ); $head_text_style = $this->build_inline_css( $styles, 'head_text' ); $panel_style = $this->build_inline_css( $styles, 'panel' ); $icon_style = ! empty( $styles['icon_style'] ) ? $styles['icon_style'] : 'chevron'; $icon_pair = $this->get_icon_classes( $icon_style, $styles ); $closed_icon = $icon_pair['closed']; $open_icon = $icon_pair['open']; $open_event = ! empty( $styles['open_event'] ) ? $styles['open_event'] : 'click'; $anim_speed = ! empty( $styles['anim_speed'] ) ? intval( $styles['anim_speed'] ) : 300; $output = $dynamic_css; $output .= '
$scope_id, 'post_id' => $this->post_id, 'depth' => $this->depth, 'theme' => $this->get_theme_class(), ); /** * Lets add-ons add attributes to the wrapper element. Returned values must already be escaped. * * @param string $attributes Extra attributes (empty by default). * @param array $styles Styles map. * @param array $context scope_id, post_id, depth and theme. * @param TCAccordion_Renderer $renderer This renderer. */ $extra_attributes = apply_filters( 'tcaccordion_wrapper_attributes', '', $styles, $base_context, $this ); if ( is_string( $extra_attributes ) && '' !== trim( $extra_attributes ) ) { $output .= trim( $extra_attributes ) . ' '; } $output .= 'style="width:100%; height:auto">'; // Search box and Expand / Collapse All controls. $has_search = ( 'yes' === $styles['show_search'] ); $has_toggle_all = ( 'yes' === $styles['show_toggle_all'] ); if ( $has_search || $has_toggle_all ) { $output .= '
'; if ( $has_search ) { $output .= ''; } if ( $has_toggle_all ) { $output .= '
'; $output .= ''; $output .= ''; $output .= '
'; } $output .= '
'; } /** * Lets add-ons print content between the search / tools bar and the list (for example filter buttons). * * @param string $html Extra HTML (empty by default). Must already be escaped. * @param array $items Items being rendered. * @param array $styles Styles map. * @param array $context scope_id, post_id, depth and theme. * @param TCAccordion_Renderer $renderer This renderer. */ $output .= apply_filters( 'tcaccordion_before_list', '', $items, $styles, $base_context, $this ); $output .= ''; if ( $has_search ) { $output .= ''; } $output .= '
'; // Injects JSON-LD Schema if enabled in metabox settings $enable_schema = isset( $styles['enable_faq_schema'] ) ? $styles['enable_faq_schema'] : 'yes'; if ( 'yes' === $enable_schema && ! empty( $schema_faq_items ) ) { $schema_data = array( '@context' => 'https://schema.org', '@type' => 'FAQPage', 'mainEntity' => $schema_faq_items, ); $output .= ''; } return $output; } /** * Helper function to map selected style to Font Awesome classes. */ private function get_icon_classes( $style, $styles = array() ) { $icons = array( 'plus_minus' => array( 'closed' => 'fa-plus', 'open' => 'fa-minus' ), 'chevron' => array( 'closed' => 'fa-chevron-down', 'open' => 'fa-chevron-up' ), 'angle' => array( 'closed' => 'fa-angle-right', 'open' => 'fa-angle-down' ), 'caret' => array( 'closed' => 'fa-caret-right', 'open' => 'fa-caret-down' ), 'folder' => array( 'closed' => 'fa-folder', 'open' => 'fa-folder-open' ), 'custom' => array( 'closed' => ! empty( $styles['custom_closed_icon'] ) ? $styles['custom_closed_icon'] : 'fa-plus', 'open' => ! empty( $styles['custom_open_icon'] ) ? $styles['custom_open_icon'] : 'fa-minus', ), 'none' => array( 'closed' => '', 'open' => '' ), ); return isset( $icons[ $style ] ) ? $icons[ $style ] : $icons['chevron']; } } /** * Backward compatibility shortcode callback wrapper. * * @param array $atts Shortcode attributes. * @return string Rendered accordion HTML. */ function TCP_accordions_wordpress_table_body( $atts_or_id, $overrides = array() ) { $post_id = 0; if ( is_array( $atts_or_id ) ) { $atts = shortcode_atts( array( 'id' => 0 ), $atts_or_id ); $post_id = absint( $atts['id'] ); } else { $post_id = absint( $atts_or_id ); } $renderer = new TCAccordion_Renderer( $post_id ); if ( ! empty( $overrides ) ) { $renderer->set_overrides( $overrides ); } return $renderer->render(); }