(string) $item['q'],
'a' => (string) $item['a'],
);
}
}
return $out;
}
/**
* Save FAQ items.
*
* @param int $post_id Post ID.
* @param array $items Items.
*/
public static function set_faq( $post_id, array $items ) {
$clean = array();
foreach ( $items as $item ) {
$q = isset( $item['q'] ) ? sanitize_text_field( $item['q'] ) : '';
$a = isset( $item['a'] ) ? wp_kses_post( $item['a'] ) : '';
if ( '' !== $q && '' !== trim( wp_strip_all_tags( $a ) ) ) {
$clean[] = array(
'q' => $q,
'a' => $a,
);
}
}
if ( $clean ) {
update_post_meta( $post_id, '_aeoora_faq', array_slice( $clean, 0, 30 ) );
} else {
delete_post_meta( $post_id, '_aeoora_faq' );
}
}
/**
* Small front-end stylesheet, only when a box is shown.
*/
public static function styles() {
if ( ! is_singular() ) {
return;
}
$id = get_queried_object_id();
if ( ! self::get_faq( $id ) && ! get_post_meta( $id, '_aeoora_tldr', true ) ) {
return;
}
wp_register_style( 'aeoora-front', false, array(), AEOORA_VERSION );
wp_enqueue_style( 'aeoora-front' );
wp_add_inline_style( 'aeoora-front', '.aeoora-tldr-box{border-left:4px solid #635bff;background:rgba(99,91,255,.06);padding:1em 1.25em;margin:1.5em 0;border-radius:6px}.aeoora-tldr-box p{margin:.25em 0}.aeoora-faq{margin:2em 0}.aeoora-faq details{border:1px solid rgba(127,127,127,.35);border:1px solid color-mix(in srgb,currentColor 18%,transparent);border-radius:6px;padding:.75em 1em;margin:.5em 0}.aeoora-faq summary{cursor:pointer;font-weight:600}.aeoora-faq .aeoora-faq-a{margin-top:.5em}' );
}
/**
* Append the FAQ / TL;DR boxes to content when configured.
*
* @param string $content Content.
* @return string
*/
public static function content_boxes( $content ) {
if ( Aeoora_Content::$rendering || is_admin() || ! is_singular() || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$post_id = get_the_ID();
if ( (int) get_queried_object_id() !== (int) $post_id ) {
return $content;
}
$s = Aeoora_Settings::get( 'schema' );
$tldr = trim( (string) get_post_meta( $post_id, '_aeoora_tldr', true ) );
if ( '' !== $tldr && 'off' !== $s['tldr_display'] && false === strpos( $content, 'aeoora-tldr-box' ) ) {
$box = self::tldr_html( $tldr );
$content = 'bottom' === $s['tldr_display'] ? $content . $box : $box . $content;
}
if ( 'auto' === $s['faq_display'] && self::get_faq( $post_id ) && false === strpos( $content, 'aeoora-faq' ) ) {
$content .= self::faq_html( $post_id );
}
return $content;
}
/**
* TL;DR box HTML.
*
* @param string $tldr Text.
* @return string
*/
private static function tldr_html( $tldr ) {
return '
' . esc_html__( 'Key takeaway', 'aeoora' ) . ': ' . esc_html( $tldr ) . '
';
}
/**
* FAQ HTML (native , no JS).
*
* @param int $post_id Post ID.
* @return string
*/
public static function faq_html( $post_id ) {
$faq = self::get_faq( $post_id );
if ( ! $faq ) {
return '';
}
$level = (int) apply_filters( 'aeoora_faq_heading_level', 2, $post_id );
$level = max( 2, min( 6, $level ) );
$html = '' . esc_html__( 'Frequently asked questions', 'aeoora' ) . '';
foreach ( $faq as $item ) {
$html .= '' . esc_html( $item['q'] ) . '
' . wpautop( wp_kses_post( $item['a'] ) ) . '
';
}
if ( ! is_admin() && ! Aeoora_Content::$rendering && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
self::$faq_printed[ (int) $post_id ] = true;
}
return $html . '';
}
/**
* Print FAQPage JSON-LD in the footer, only for FAQs that were really shown.
*/
public static function faq_schema() {
if ( ! Aeoora_Settings::get( 'schema', 'faq', true ) || ! self::$faq_printed ) {
return;
}
$graph = array();
foreach ( array_keys( self::$faq_printed ) as $post_id ) {
$post = get_post( $post_id );
if ( ! $post || self::has_foreign_faq_schema( $post ) ) {
continue;
}
$entities = array();
foreach ( self::get_faq( $post_id ) as $item ) {
$entities[] = array(
'@type' => 'Question',
'name' => Aeoora_Util::one_line( $item['q'] ),
'acceptedAnswer' => array(
'@type' => 'Answer',
'text' => trim( wp_kses( wpautop( $item['a'] ), array( 'p' => array(), 'br' => array(), 'strong' => array(), 'em' => array(), 'a' => array( 'href' => array() ), 'ul' => array(), 'ol' => array(), 'li' => array() ) ) ),
),
);
}
if ( $entities ) {
$graph[] = array(
'@type' => 'FAQPage',
'@id' => get_permalink( $post ) . '#faq',
'url' => get_permalink( $post ),
'mainEntity' => $entities,
);
}
}
if ( ! $graph ) {
return;
}
$json = wp_json_encode(
array(
'@context' => 'https://schema.org',
'@graph' => $graph,
),
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP
);
self::print_json_ld( $json, 'aeoora-schema-faq' );
}
/**
* Print JSON-LD through core's script-tag API. The JSON is encoded with
* JSON_HEX_TAG and JSON_HEX_AMP, so no "<", ">" or "&" (in any case, so no
* "" either) can appear inside the tag.
*
* @param string|false $json JSON from wp_json_encode().
* @param string $class Class of the script tag.
*/
private static function print_json_ld( $json, $class ) {
if ( ! is_string( $json ) || '' === $json ) {
return;
}
wp_print_inline_script_tag(
$json,
array(
'type' => 'application/ld+json',
'class' => $class,
)
);
}
/**
* [aeoora_faq] shortcode.
*
* @param array $atts Attributes.
* @return string
*/
public static function shortcode_faq( $atts ) {
$atts = shortcode_atts( array( 'id' => get_the_ID() ), $atts, 'aeoora_faq' );
$id = (int) $atts['id'];
return self::can_show( $id ) ? self::faq_html( $id ) : '';
}
/**
* May the FAQ / TL;DR of a post be shown here? Always for the post being
* displayed; another post's only when it is public (or the visitor may
* read it), so a shortcode can't reveal a draft's or a private post's text.
*
* @param int $id Post ID.
* @return bool
*/
private static function can_show( $id ) {
if ( $id <= 0 ) {
return false;
}
if ( (int) get_the_ID() === $id ) {
return true;
}
$post = get_post( $id );
if ( ! $post ) {
return false;
}
if ( 'publish' === $post->post_status && '' === $post->post_password && is_post_type_viewable( $post->post_type ) ) {
return true;
}
return current_user_can( 'read_post', $id ) && ! post_password_required( $post );
}
/**
* [aeoora_tldr] shortcode.
*
* @param array $atts Attributes.
* @return string
*/
public static function shortcode_tldr( $atts ) {
$atts = shortcode_atts( array( 'id' => get_the_ID() ), $atts, 'aeoora_tldr' );
if ( ! self::can_show( (int) $atts['id'] ) ) {
return '';
}
$tldr = trim( (string) get_post_meta( (int) $atts['id'], '_aeoora_tldr', true ) );
return '' === $tldr ? '' : self::tldr_html( $tldr );
}
/**
* Should a schema type be printed, given the "auto" rule?
*
* @param string $key article|organization|breadcrumbs.
* @return bool
*/
private static function wants( $key ) {
$v = Aeoora_Settings::get( 'schema', $key, 'auto' );
if ( 'on' === $v ) {
return true;
}
if ( 'off' === $v ) {
return false;
}
return ! Aeoora_Seo::outputs_schema();
}
/**
* Print JSON-LD.
*/
public static function output() {
if ( is_admin() || is_feed() ) {
return;
}
$graph = array();
$s = Aeoora_Settings::get( 'schema' );
if ( ( is_front_page() || is_home() ) && self::wants( 'organization' ) ) {
$graph[] = self::organization();
$graph[] = array(
'@type' => 'WebSite',
'@id' => home_url( '/#website' ),
'url' => home_url( '/' ),
'name' => get_bloginfo( 'name' ),
'publisher' => array( '@id' => home_url( '/#organization' ) ),
'inLanguage' => get_bloginfo( 'language' ),
);
}
if ( is_singular() ) {
$post = get_queried_object();
if ( $post instanceof WP_Post && Aeoora_Content::is_public( $post, 'audit' ) ) {
if ( 'post' === $post->post_type && self::wants( 'article' ) ) {
$graph[] = self::article( $post, ! empty( $s['speakable'] ) );
if ( ! is_front_page() ) {
$graph['org'] = self::organization(); // Referenced by @id from the article.
}
} elseif ( ! empty( $s['speakable'] ) ) {
$graph[] = array(
'@type' => 'WebPage',
'@id' => get_permalink( $post ) . '#webpage',
'url' => get_permalink( $post ),
'name' => get_the_title( $post ),
'speakable' => self::speakable(),
);
}
if ( self::wants( 'breadcrumbs' ) && ! is_front_page() ) {
$crumbs = self::breadcrumbs( $post );
if ( $crumbs ) {
$graph[] = $crumbs;
}
}
}
}
$graph = apply_filters( 'aeoora_schema_graph', array_values( array_filter( $graph ) ) );
if ( ! $graph ) {
return;
}
$json = wp_json_encode(
array(
'@context' => 'https://schema.org',
'@graph' => $graph,
),
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP
);
self::print_json_ld( $json, 'aeoora-schema' );
}
/**
* Does the post already carry FAQ schema from another plugin's block?
*
* @param WP_Post $post Post.
* @return bool
*/
private static function has_foreign_faq_schema( WP_Post $post ) {
return has_block( 'yoast/faq-block', $post ) || has_block( 'rank-math/faq-block', $post ) || false !== strpos( $post->post_content, 'aioseo/faq' );
}
/**
* Organization / Person node.
*
* @return array
*/
public static function organization() {
$s = Aeoora_Settings::get( 'schema' );
$name = '' !== trim( (string) $s['org_name'] ) ? $s['org_name'] : get_bloginfo( 'name' );
$logo = $s['org_logo'];
if ( ! $logo ) {
$icon = get_site_icon_url( 512 );
$logo = $icon ? $icon : '';
$custom = get_theme_mod( 'custom_logo' );
if ( $custom ) {
$src = wp_get_attachment_image_url( $custom, 'full' );
$logo = $src ? $src : $logo;
}
}
$node = array(
'@type' => $s['org_type'],
'@id' => home_url( '/#organization' ),
'name' => $name,
'url' => home_url( '/' ),
);
$desc = get_bloginfo( 'description' );
if ( $desc ) {
$node['description'] = $desc;
}
if ( $logo ) {
$node['Person' === $s['org_type'] ? 'image' : 'logo'] = $logo;
}
$same = array_filter( array_map( 'esc_url_raw', array_map( 'trim', preg_split( '/[\r\n,]+/', (string) $s['same_as'] ) ) ) );
if ( $same ) {
$node['sameAs'] = array_values( $same );
}
return $node;
}
/**
* Article node.
*
* @param WP_Post $post Post.
* @param bool $speakable Add speakable.
* @return array
*/
private static function article( WP_Post $post, $speakable ) {
$author = get_userdata( $post->post_author );
$node = array(
'@type' => 'BlogPosting',
'@id' => get_permalink( $post ) . '#article',
'mainEntityOfPage' => get_permalink( $post ),
'headline' => Aeoora_Util::trim_chars( get_the_title( $post ), 110 ),
'datePublished' => get_post_time( 'c', true, $post ),
'dateModified' => get_post_modified_time( 'c', true, $post ),
'inLanguage' => get_bloginfo( 'language' ),
'publisher' => array( '@id' => home_url( '/#organization' ) ),
);
$desc = Aeoora_Seo::get_description( $post->ID );
if ( $desc ) {
$node['description'] = $desc;
}
if ( $author ) {
$person = array(
'@type' => 'Person',
'name' => $author->display_name,
'url' => get_author_posts_url( $author->ID ),
);
if ( $author->description ) {
$person['description'] = Aeoora_Util::trim_chars( $author->description, 300 );
}
if ( $author->user_url ) {
$person['sameAs'] = array( $author->user_url );
}
$node['author'] = $person;
}
$img = get_the_post_thumbnail_url( $post, 'full' );
if ( $img ) {
$node['image'] = $img;
}
$node['wordCount'] = Aeoora_Util::word_count( wp_strip_all_tags( $post->post_content ) );
if ( $speakable ) {
$node['speakable'] = self::speakable();
}
return $node;
}
/**
* Speakable specification.
*
* @return array
*/
private static function speakable() {
return array(
'@type' => 'SpeakableSpecification',
'cssSelector' => array( 'h1', '.aeoora-tldr-box', '.entry-content > p:first-of-type' ),
);
}
/**
* BreadcrumbList node.
*
* @param WP_Post $post Post.
* @return array|null
*/
private static function breadcrumbs( WP_Post $post ) {
$items = array(
array(
'name' => get_bloginfo( 'name' ),
'url' => home_url( '/' ),
),
);
if ( 'post' === $post->post_type ) {
$cats = get_the_category( $post->ID );
if ( $cats ) {
$items[] = array(
'name' => $cats[0]->name,
'url' => get_category_link( $cats[0] ),
);
}
} elseif ( is_post_type_hierarchical( $post->post_type ) ) {
foreach ( array_reverse( get_post_ancestors( $post ) ) as $ancestor ) {
$items[] = array(
'name' => get_the_title( $ancestor ),
'url' => get_permalink( $ancestor ),
);
}
}
$items[] = array(
'name' => get_the_title( $post ),
'url' => get_permalink( $post ),
);
$list = array();
foreach ( $items as $i => $item ) {
$list[] = array(
'@type' => 'ListItem',
'position' => $i + 1,
'name' => Aeoora_Util::one_line( $item['name'] ),
'item' => $item['url'],
);
}
return array(
'@type' => 'BreadcrumbList',
'@id' => get_permalink( $post ) . '#breadcrumb',
'itemListElement' => $list,
);
}
}