label. */ public static function get_icons() { return array( 'fa-star' => __( 'Star', 'accordions-wp' ), 'fa-fire' => __( 'Fire', 'accordions-wp' ), 'fa-bolt' => __( 'Bolt', 'accordions-wp' ), 'fa-circle-question' => __( 'Question', 'accordions-wp' ), 'fa-circle-info' => __( 'Info', 'accordions-wp' ), 'fa-lightbulb' => __( 'Lightbulb', 'accordions-wp' ), 'fa-gift' => __( 'Gift', 'accordions-wp' ), 'fa-truck' => __( 'Delivery truck', 'accordions-wp' ), 'fa-box' => __( 'Box', 'accordions-wp' ), 'fa-rotate-left' => __( 'Return', 'accordions-wp' ), 'fa-credit-card' => __( 'Credit card', 'accordions-wp' ), 'fa-percent' => __( 'Percent', 'accordions-wp' ), 'fa-tag' => __( 'Tag', 'accordions-wp' ), 'fa-shield-halved' => __( 'Shield', 'accordions-wp' ), 'fa-lock' => __( 'Lock', 'accordions-wp' ), 'fa-circle-check' => __( 'Check', 'accordions-wp' ), 'fa-heart' => __( 'Heart', 'accordions-wp' ), 'fa-bell' => __( 'Bell', 'accordions-wp' ), 'fa-user' => __( 'User', 'accordions-wp' ), 'fa-gear' => __( 'Gear', 'accordions-wp' ), 'fa-book' => __( 'Book', 'accordions-wp' ), 'fa-envelope' => __( 'Envelope', 'accordions-wp' ), 'fa-phone' => __( 'Phone', 'accordions-wp' ), 'fa-clock' => __( 'Clock', 'accordions-wp' ), 'fa-globe' => __( 'Globe', 'accordions-wp' ), ); } /** * Badge colors. * * @return array Slug => label. */ public static function get_badge_colors() { return array( 'blue' => __( 'Blue', 'accordions-wp' ), 'green' => __( 'Green', 'accordions-wp' ), 'orange' => __( 'Orange', 'accordions-wp' ), 'red' => __( 'Red', 'accordions-wp' ), 'purple' => __( 'Purple', 'accordions-wp' ), 'gray' => __( 'Gray', 'accordions-wp' ), ); } /** * Register hooks. */ public function __construct() { // Item data: keep, sanitize and edit. add_filter( 'tcaccordion_item_data', array( $this, 'keep_item_data' ), 11, 2 ); add_filter( 'tcaccordion_admin_item', array( $this, 'keep_item_data' ), 11, 2 ); add_filter( 'tcaccordion_sanitize_item', array( $this, 'sanitize_item' ), 11, 3 ); add_action( 'tcaccordion_item_extra_fields', array( $this, 'render_item_fields' ), 11, 2 ); // Front end output. add_filter( 'tcaccordion_item_title_html', array( $this, 'title_html' ), 10, 3 ); add_filter( 'tcaccordion_item_li_attributes', array( $this, 'li_attributes' ), 10, 3 ); add_filter( 'tcaccordion_before_list', array( $this, 'group_bar' ), 10, 5 ); // Settings (Settings tab) and saving. add_action( 'tcaccordion_pro_settings', array( $this, 'render_settings' ), 10 ); add_action( 'tcaccordion_save_settings', array( $this, 'save_settings' ) ); } /** * Keep the extra item fields when items are read. * * @param array $item Normalized item. * @param array $raw Raw item as saved. * @return array */ public function keep_item_data( $item, $raw ) { $item['group'] = isset( $raw['group'] ) ? (string) $raw['group'] : ''; $item['icon'] = isset( $raw['icon'] ) ? (string) $raw['icon'] : ''; $item['badge'] = isset( $raw['badge'] ) ? (string) $raw['badge'] : ''; $item['badge_color'] = isset( $raw['badge_color'] ) ? (string) $raw['badge_color'] : 'blue'; return $item; } /** * Sanitize the extra item fields on save. * * @param array $clean Sanitized item. * @param array $raw Submitted item. * @param int $post_id Accordion ID. * @return array */ public function sanitize_item( $clean, $raw, $post_id ) { $icon = isset( $raw['icon'] ) ? sanitize_text_field( $raw['icon'] ) : ''; $color = isset( $raw['badge_color'] ) ? sanitize_key( $raw['badge_color'] ) : 'blue'; $clean['group'] = isset( $raw['group'] ) ? mb_substr( sanitize_text_field( $raw['group'] ), 0, 40 ) : ''; $clean['icon'] = array_key_exists( $icon, self::get_icons() ) ? $icon : ''; $clean['badge'] = isset( $raw['badge'] ) ? mb_substr( sanitize_text_field( $raw['badge'] ), 0, 24 ) : ''; $clean['badge_color'] = array_key_exists( $color, self::get_badge_colors() ) ? $color : 'blue'; return $clean; } /** * Group, icon and badge fields shown in every item of the editor. * * @param string|int $index Item index used in field names. * @param array $item Item data. * @return void */ public function render_item_fields( $index, $item ) { $name = TCAccordion_Metabox::META_KEY . '[' . $index . ']'; $group = isset( $item['group'] ) ? $item['group'] : ''; $icon = isset( $item['icon'] ) ? $item['icon'] : ''; $badge = isset( $item['badge'] ) ? $item['badge'] : ''; $color = isset( $item['badge_color'] ) ? $item['badge_color'] : 'blue'; ?>

' . $html; } if ( isset( $item['badge'] ) && '' !== trim( (string) $item['badge'] ) ) { $color = ( ! empty( $item['badge_color'] ) && isset( $colors[ $item['badge_color'] ] ) ) ? $item['badge_color'] : 'blue'; $html .= '' . esc_html( $item['badge'] ) . ''; } return $html; } /** * Mark each item with its group so the filter buttons can show and hide it. * * @param string $attributes Attributes so far. * @param array $item Normalized item. * @param array $context Item context. * @return string */ public function li_attributes( $attributes, $item, $context ) { $slug = $this->group_slug( isset( $item['group'] ) ? $item['group'] : '' ); if ( '' !== $slug ) { $attributes .= ' data-group="' . esc_attr( $slug ) . '"'; } return $attributes; } /** * Slug used to match an item with its filter button. * * @param string $group Group name. * @return string */ private function group_slug( $group ) { $group = trim( (string) $group ); return '' === $group ? '' : urldecode( sanitize_title( $group ) ); } /** * Group filter buttons shown above the list. * * @param string $html HTML so far. * @param array $items Items being rendered. * @param array $styles Styles map. * @param array $context Accordion context. * @param TCAccordion_Renderer $renderer Renderer (reads the settings). * @return string */ public function group_bar( $html, $items, $styles, $context, $renderer ) { unset( $styles, $context ); if ( 'yes' !== $renderer->get_setting( '_tcacc_group_bar', 'no' ) ) { return $html; } $groups = array(); foreach ( $items as $item ) { $label = isset( $item['group'] ) ? trim( (string) $item['group'] ) : ''; $slug = $this->group_slug( $label ); if ( '' !== $slug && ! isset( $groups[ $slug ] ) ) { $groups[ $slug ] = $label; } } if ( count( $groups ) < 2 ) { return $html; } $all_label = trim( (string) $renderer->get_setting( '_tcacc_group_all_label', '' ) ); $all_label = '' !== $all_label ? $all_label : __( 'All', 'accordions-wp' ); $html .= '
'; $html .= ''; foreach ( $groups as $slug => $label ) { $html .= ''; } $html .= '
'; return $html; } /** * Settings rows for the group bar. * * @param WP_Post $post Accordion post. * @return void */ public function render_settings( $post ) { $bar = get_post_meta( $post->ID, '_tcacc_group_bar', true ); $bar = ( 'yes' === $bar ) ? 'yes' : 'no'; $all_label = get_post_meta( $post->ID, '_tcacc_group_all_label', true ); ?>