label.
* @return array
*/
public function add_layouts( $layouts ) {
$layouts['tabs'] = __( 'Tabs on desktop, accordion on mobile', 'accordions-wp' );
$layouts['horizontal'] = __( 'Horizontal accordion', 'accordions-wp' );
return $layouts;
}
/**
* CSS for the Tabs and Horizontal layouts (screens 768px and wider).
*
* @param string $css CSS built so far.
* @param string $layout Layout slug.
* @param array $styles Styles map.
* @param string $selector Wrapper selector (#id).
* @param TCAccordion_Renderer $renderer Renderer.
* @return string
*/
public function layout_css( $css, $layout, $styles, $selector, $renderer ) {
$ul = $selector . ' > .responsive-accordion';
$li = $ul . ' > li';
$head = $li . ' > .responsive-accordion-head';
$panel = $li . ' > .responsive-accordion-panel';
$mq = '@media (min-width: 768px) {';
if ( 'horizontal' === $layout ) {
$height = absint( $renderer->get_setting( '_tcacc_horizontal_height', '380' ) );
$height = max( 200, min( 900, $height ? $height : 380 ) );
$speed = ! empty( $styles['anim_speed'] ) ? absint( $styles['anim_speed'] ) : 300;
$css .= $mq;
$css .= $ul . ' { display: flex !important; flex-direction: row; align-items: stretch; gap: 8px; height: ' . $height . 'px; }';
$css .= $li . ' { display: flex !important; flex-direction: row; flex: 0 0 64px; min-width: 0; margin-bottom: 0 !important; overflow: hidden; transition: flex-grow ' . $speed . 'ms ease; }';
$css .= $li . '.tc-acc-open { flex-grow: 1; }';
$css .= $li . '.tc-acc-filtered-out, ' . $li . '.tc-acc-hidden { display: none !important; }';
$css .= $head . ' { flex: 0 0 64px; width: 64px !important; height: auto !important; flex-direction: column !important; justify-content: flex-start !important; align-items: center !important; padding: 16px 0 !important; }';
$css .= $head . ' > .responsive-accordion-icon { order: 0 !important; margin-bottom: 12px !important; }';
$css .= $head . ' > .responsive-accordion-title { order: 1 !important; flex: 0 1 auto !important; width: auto !important; writing-mode: vertical-rl; transform: rotate(180deg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: start !important; }';
$css .= $panel . ' { flex: 1 1 0; min-width: 0; width: auto !important; overflow: auto; box-sizing: border-box; }';
$css .= '}';
}
if ( 'tabs' === $layout ) {
$border = '';
$border_style = ! empty( $styles['border_style'] ) ? $styles['border_style'] : 'none';
if ( 'none' !== $border_style ) {
$border_width = isset( $styles['border_width'] ) ? absint( $styles['border_width'] ) : 1;
$border_color = ! empty( $styles['border_color'] ) ? sanitize_hex_color( $styles['border_color'] ) : '';
$border_color = $border_color ? $border_color : '#e2e8f0';
$border = 'border: ' . $border_width . 'px ' . esc_attr( $border_style ) . ' ' . $border_color . ' !important;';
}
$css .= $mq;
$css .= $ul . ' { display: flex !important; flex-wrap: wrap; align-items: flex-end; column-gap: 6px; }';
$css .= $li . ' { display: contents !important; }';
$css .= $li . '.tc-acc-filtered-out, ' . $li . '.tc-acc-hidden { display: none !important; }';
$css .= $head . ' { order: 1; flex: 0 0 auto; width: auto !important; margin: 0 !important; }';
$css .= $head . ' > .responsive-accordion-title { width: auto !important; flex: 0 0 auto !important; }';
$css .= $head . ' > .responsive-accordion-icon { display: none !important; }';
$css .= $panel . ' { order: 2; flex: 0 0 100%; width: 100% !important; box-sizing: border-box; ' . $border . ' }';
$css .= '}';
}
return $css;
}
/**
* Height field for the Horizontal layout (Settings tab).
*
* @param WP_Post $post Accordion post.
* @return void
*/
public function render_height_setting( $post ) {
$height = absint( get_post_meta( $post->ID, '_tcacc_horizontal_height', true ) );
$height = $height ? $height : 380;
?>
esc_url_raw( rest_url( 'tcaccordion/v1/track' ) ),
)
);
$localized = true;
}
}
// -------------------------------------------------------------------------
// Behavior
// -------------------------------------------------------------------------
/**
* Open event and animation speed.
*
* @param array $behavior Defaults (open_event, anim_speed).
* @param TCAccordion_Renderer $renderer Renderer.
* @return array
*/
public function behavior_settings( $behavior, $renderer ) {
$event = $renderer->get_setting( '_tcacc_open_event', 'click' );
$speed = absint( $renderer->get_setting( '_tcacc_anim_speed', '300' ) );
$behavior['open_event'] = in_array( $event, array( 'click', 'hover' ), true ) ? $event : 'click';
$behavior['anim_speed'] = $speed ? $speed : 300;
return $behavior;
}
/**
* Save the Pro settings. The nonce and permissions are checked by the free plugin before this runs.
*
* @param int $post_id Accordion ID.
* @return void
*/
public function save_settings( $post_id ) {
// phpcs:disable WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['_tcacc_horizontal_height'] ) ) {
$height = absint( wp_unslash( $_POST['_tcacc_horizontal_height'] ) );
update_post_meta( $post_id, '_tcacc_horizontal_height', max( 200, min( 900, $height ? $height : 380 ) ) );
}
if ( isset( $_POST['_tcacc_open_event'] ) ) {
$event = sanitize_text_field( wp_unslash( $_POST['_tcacc_open_event'] ) );
update_post_meta( $post_id, '_tcacc_open_event', in_array( $event, array( 'click', 'hover' ), true ) ? $event : 'click' );
}
if ( isset( $_POST['_tcacc_anim_speed'] ) ) {
update_post_meta( $post_id, '_tcacc_anim_speed', absint( wp_unslash( $_POST['_tcacc_anim_speed'] ) ) );
}
// phpcs:enable WordPress.Security.NonceVerification.Missing
}
/**
* Allow the Pro settings in the Gutenberg Inline block.
*
* @param array $schema Allowed settings.
* @return array
*/
public function inline_option_schema( $schema ) {
$schema['_tcacc_horizontal_height'] = array( 'int', 200, 900 );
$schema['_tcacc_open_event'] = array( 'enum', array( 'click', 'hover' ) );
$schema['_tcacc_anim_speed'] = array( 'int', 0, 3000 );
return $schema;
}
// -------------------------------------------------------------------------
// Nested accordions
// -------------------------------------------------------------------------
/**
* Keep the nested accordion ID 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['nested_id'] = isset( $raw['nested_id'] ) ? absint( $raw['nested_id'] ) : 0;
return $item;
}
/**
* Keep the nested accordion ID when items are saved.
*
* @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 ) {
$nested_id = isset( $raw['nested_id'] ) ? absint( $raw['nested_id'] ) : 0;
// An accordion can never contain itself.
$clean['nested_id'] = ( $nested_id === (int) $post_id ) ? 0 : $nested_id;
return $clean;
}
/**
* Accordions that can be nested inside an item (every accordion except the current one).
*
* @return array List of array( 'id' => int, 'label' => string ).
*/
private function get_nested_choices() {
if ( null !== $this->nested_choices ) {
return $this->nested_choices;
}
$this->nested_choices = array();
$current = (int) get_the_ID();
$posts = get_posts(
array(
'post_type' => 'accordion_tp',
'post_status' => array( 'publish', 'draft', 'pending', 'private' ),
'posts_per_page' => 200,
'orderby' => 'title',
'order' => 'ASC',
'exclude' => $current ? array( $current ) : array(),
)
);
foreach ( $posts as $nested_post ) {
$label = '' !== $nested_post->post_title ? $nested_post->post_title : __( '(no title)', 'accordions-wp' );
$label = sprintf( '%s (#%d)', $label, $nested_post->ID );
if ( 'publish' !== $nested_post->post_status ) {
$label .= ' - ' . __( 'not published', 'accordions-wp' );
}
$this->nested_choices[] = array(
'id' => (int) $nested_post->ID,
'label' => $label,
);
}
return $this->nested_choices;
}
/**
* Nested accordion picker 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_nested_field( $index, $item ) {
$nested_id = isset( $item['nested_id'] ) ? absint( $item['nested_id'] ) : 0;
$nested_choices = $this->get_nested_choices();
?>
>
= self::MAX_NESTING_DEPTH || $nested_id === (int) $context['post_id'] || TCAccordion_Renderer::is_rendering( $nested_id ) ) {
return $html;
}
$nested_post = get_post( $nested_id );
if ( ! $nested_post || 'accordion_tp' !== $nested_post->post_type || 'publish' !== $nested_post->post_status ) {
return $html;
}
$child = new TCAccordion_Renderer( $nested_id );
$child->set_nested_context( $context['scope_id'] . '-nested-' . $context['item_num'], $context['theme'], $context['depth'] + 1 );
$child_html = $child->render();
return $html . ( '' !== $child_html ? '' . $child_html . '
' : '' );
}
}
new TCAccordion_Pro();
// The other Pro features. Each file registers its own hooks.
require_once __DIR__ . '/class-tcaccordion-pro-items__premium_only.php';
require_once __DIR__ . '/class-tcaccordion-pro-dynamic__premium_only.php';
require_once __DIR__ . '/class-tcaccordion-pro-analytics__premium_only.php';
require_once __DIR__ . '/class-tcaccordion-pro-woo__premium_only.php';