Skip to content

Instantly share code, notes, and snippets.

@ihslimn
Created April 24, 2024 09:29
Show Gist options
  • Save ihslimn/02661ec3cae3ec8a6f20ff7156d82bc2 to your computer and use it in GitHub Desktop.
Save ihslimn/02661ec3cae3ec8a6f20ff7156d82bc2 to your computer and use it in GitHub Desktop.
JetTabs Combined FAQ
<?php
class Jet_Tabs_Combined_Faq {
private $schema = array();
private $widget = null;
private $store = false;
public function __construct() {
add_filter( 'jet-tabs/widget/loop-items', array( $this, 'store_widget' ), 100, 3 );
add_action( 'jet-engine-query-gateway/do-item', array( $this, 'store_schema' ) );
add_shortcode( 'jet_tabs_combined_faq', array( $this, 'output_schema' ) );
}
public function output_schema() {
if ( ! function_exists( 'jet_tabs_integration' ) || jet_tabs_integration()->is_edit_mode() ) {
return;
}
if ( ! $this->store ) {
$this->store = true;
return;
}
return sprintf( '<script type="application/ld+json">%s</script>', wp_json_encode( $this->schema ) );
}
public function store_widget( $items, $type, $widget ) {
$this->widget = $widget;
return $items;
}
public function parse_text_editor( $content ) {
$content = shortcode_unautop( $content );
$content = do_shortcode( $content );
$content = wptexturize( $content );
if ( $GLOBALS['wp_embed'] instanceof \WP_Embed ) {
$content = $GLOBALS['wp_embed']->autoembed( $content );
}
return $content;
}
public function store_schema( $item ) {
if ( empty( $this->schema ) ) {
$this->schema = array(
'@context' => 'https://schema.org',
'@type' => 'FAQPage',
'mainEntity' => [],
);
}
$content_html = '';
switch ( $item[ 'content_type' ] ) {
case 'template':
if ( ! empty( $item['item_template_id'] ) ) {
// for multi-language plugins
$template_id = apply_filters( 'jet-tabs/widgets/template_id', $item['item_template_id'], $this );
$template_content = jet_tabs()->elementor()->frontend->get_builder_content( $template_id );
if ( ! empty( $template_content ) ) {
if ( ! $ajax_template ) {
$content_html .= $template_content;
} else {
$content_html .= '<div class="jet-tabs-loader"></div>';
}
if ( jet_tabs_integration()->is_edit_mode() ) {
$link = add_query_arg(
array(
'elementor' => '',
'jet-tabs-canvas' => '',
),
get_permalink( $item['item_template_id'] )
);
$content_html .= sprintf( '<div class="jet-toggle__edit-cover" data-template-edit-link="%s"><i class="fas fa-pencil-alt"></i><span>%s</span></div>', $link, esc_html__( 'Edit Template', 'jet-tabs' ) );
}
} else {
$content_html = $this->widget->no_template_content_message();
}
} else {
$content_html = $this->widget->no_templates_message();
}
break;
case 'editor':
$content_html = $this->parse_text_editor( $item['item_editor_content'] );
break;
}
$this->schema['mainEntity'][] = array(
'@type' => 'Question',
'name' => wp_strip_all_tags( $item['item_label'] ),
'acceptedAnswer' => array(
'@type' => 'Answer',
'text' => wp_strip_all_tags( $content_html ),
),
);
}
}
new Jet_Tabs_Combined_Faq();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment