'application/ld+json' ) ); } } public static function ld_for_query(): array { if ( ! Settings::get( 'course_jsonld_faq' ) && ! Settings::get( 'course_jsonld_rating' ) ) { return array(); } if ( ! is_singular( PostTypes::COURSE ) ) { return array(); } $post = get_queried_object(); return $post instanceof \WP_Post ? self::ld( $post ) : array(); } public static function ld( \WP_Post $post ): array { if ( 'publish' !== $post->post_status || '' !== $post->post_password ) { return array(); } $course_id = Services::courses()->resolve_post( (int) $post->ID ); $page = CoursePage::for( $course_id ); if ( null === $page || ! $page->course->is_active() || CourseVisibility::PUBLIC !== $page->course->visibility ) { return array(); } $graph = array(); $url = (string) get_permalink( $post ); if ( Settings::get( 'course_jsonld_rating' ) && $page->reviews_on() && $page->rating_count > 0 && null !== $page->rating_avg && ! $page->coming_soon() ) { $graph[] = array( '@type' => 'Course', '@id' => $url . '#course', 'name' => wp_strip_all_tags( get_the_title( $post ) ), 'description' => self::plain( '' !== trim( $post->post_excerpt ) ? $post->post_excerpt : wp_trim_words( excerpt_remove_blocks( strip_shortcodes( $post->post_content ) ), 40 ) ), 'url' => $url, 'provider' => array( '@type' => 'Organization', 'name' => wp_strip_all_tags( (string) get_bloginfo( 'name' ) ), 'sameAs' => home_url( '/' ), ), 'aggregateRating' => array( '@type' => 'AggregateRating', 'ratingValue' => round( $page->rating_avg, 2 ), 'ratingCount' => $page->rating_count, 'bestRating' => 5, 'worstRating' => 1, ), ); } if ( Settings::get( 'course_jsonld_faq' ) && $page->faq_links > 0 && ! $page->hides_details( 0 ) ) { $faqs = FaqQuery::for_target( FaqTarget::COURSE, $course_id ); $texts = Services::faqs()->texts( $faqs ); $main = array(); foreach ( $faqs as $faq ) { $question = self::plain( (string) ( $texts[ $faq->id ]['question'] ?? $faq->question ) ); $answer = self::plain( (string) ( $texts[ $faq->id ]['answer'] ?? $faq->answer ) ); if ( '' !== $question && '' !== $answer ) { $main[] = array( '@type' => 'Question', 'name' => $question, 'acceptedAnswer' => array( '@type' => 'Answer', 'text' => $answer, ), ); } } if ( array() !== $main ) { $graph[] = array( '@type' => 'FAQPage', '@id' => $url . '#faq', 'mainEntity' => $main, ); } } if ( array() === $graph ) { return array(); } return array( '@context' => 'https://schema.org', '@graph' => $graph, ); } private static function plain( string $html ): string { return trim( (string) preg_replace( '/\s+/u', ' ', html_entity_decode( wp_strip_all_tags( $html ), ENT_QUOTES, 'UTF-8' ) ) ); } }