> */ public static function authors( WP_Post $post ): array { $people = array(); try { if ( function_exists( 'get_coauthors' ) ) { foreach ( (array) get_coauthors( $post->ID ) as $author ) { $name = (string) ( $author->display_name ?? '' ); if ( '' !== $name ) { $people[] = self::person( $name, (string) ( $author->user_url ?? '' ) ); } } } elseif ( function_exists( 'get_multiple_authors' ) ) { foreach ( (array) get_multiple_authors( $post->ID ) as $author ) { $name = (string) ( $author->display_name ?? '' ); if ( '' !== $name ) { $people[] = self::person( $name, (string) ( $author->user_url ?? '' ) ); } } } } catch ( \Throwable $e ) { return array(); } return $people; } /** @return array */ private static function person( string $name, string $url = '' ): array { return array_filter( array( '@type' => 'Person', 'name' => $name, 'url' => '' !== $url ? $url : null, ) ); } /** * A rating from whichever rating plugin is installed. * * @return array|null */ public static function rating( WP_Post $post ): ?array { try { // WP-PostRatings. if ( function_exists( 'get_post_ratings' ) ) { $count = (int) get_post_meta( $post->ID, 'ratings_users', true ); $score = (float) get_post_meta( $post->ID, 'ratings_average', true ); if ( $count > 0 && $score > 0 ) { return self::aggregate( $score, $count, 5 ); } } // Rate my Post. $rmp_count = (int) get_post_meta( $post->ID, 'rmp_vote_count', true ); $rmp_sum = (float) get_post_meta( $post->ID, 'rmp_rating_val_sum', true ); if ( $rmp_count > 0 && $rmp_sum > 0 ) { return self::aggregate( $rmp_sum / $rmp_count, $rmp_count, 5 ); } } catch ( \Throwable $e ) { return null; } return null; } /** @return array */ private static function aggregate( float $value, int $count, int $best ): array { return array( '@type' => 'AggregateRating', 'ratingValue' => (string) round( $value, 1 ), 'ratingCount' => (string) $count, 'bestRating' => (string) $best, 'worstRating' => '1', ); } /** * Recipe schema from WP Recipe Maker. * * @return array|null */ public static function recipe( WP_Post $post ): ?array { if ( ! class_exists( 'WPRM_Recipe_Manager' ) ) { return null; } try { $recipes = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post->ID ); if ( empty( $recipes ) ) { return null; } $recipe = \WPRM_Recipe_Manager::get_recipe( (int) $recipes[0] ); if ( ! $recipe ) { return null; } $ingredients = array(); foreach ( (array) $recipe->ingredients_without_groups() as $ingredient ) { $line = trim( implode( ' ', array_filter( array( (string) ( $ingredient['amount'] ?? '' ), (string) ( $ingredient['unit'] ?? '' ), (string) ( $ingredient['name'] ?? '' ), ) ) ) ); if ( '' !== $line ) { $ingredients[] = $line; } } $steps = array(); foreach ( (array) $recipe->instructions_without_groups() as $instruction ) { $text = wp_strip_all_tags( (string) ( $instruction['text'] ?? '' ) ); if ( '' !== $text ) { $steps[] = array( '@type' => 'HowToStep', 'text' => $text, ); } } return array_filter( array( '@type' => 'Recipe', '@id' => get_permalink( $post ) . '#recipe', 'name' => (string) $recipe->name(), 'description' => wp_strip_all_tags( (string) $recipe->summary() ), 'image' => (string) $recipe->image_url(), 'recipeYield' => (string) $recipe->servings(), 'prepTime' => self::minutes( (int) $recipe->prep_time() ), 'cookTime' => self::minutes( (int) $recipe->cook_time() ), 'totalTime' => self::minutes( (int) $recipe->total_time() ), 'recipeIngredient' => ! empty( $ingredients ) ? $ingredients : null, 'recipeInstructions' => ! empty( $steps ) ? $steps : null, 'recipeCuisine' => (string) implode( ', ', (array) $recipe->tags( 'course' ) ) ?: null, ) ); } catch ( \Throwable $e ) { return null; } } /** * Event schema from The Events Calendar. * * @return array|null */ public static function event( WP_Post $post ): ?array { if ( ! class_exists( 'Tribe__Events__Main' ) || ! function_exists( 'tribe_get_start_date' ) ) { return null; } if ( 'tribe_events' !== $post->post_type ) { return null; } try { $venue = array(); if ( function_exists( 'tribe_get_venue_id' ) && tribe_get_venue_id( $post->ID ) ) { $venue = array_filter( array( '@type' => 'Place', 'name' => function_exists( 'tribe_get_venue' ) ? (string) tribe_get_venue( $post->ID ) : null, 'address' => array_filter( array( '@type' => 'PostalAddress', 'streetAddress' => function_exists( 'tribe_get_address' ) ? (string) tribe_get_address( $post->ID ) : null, 'addressLocality' => function_exists( 'tribe_get_city' ) ? (string) tribe_get_city( $post->ID ) : null, 'addressRegion' => function_exists( 'tribe_get_region' ) ? (string) tribe_get_region( $post->ID ) : null, 'postalCode' => function_exists( 'tribe_get_zip' ) ? (string) tribe_get_zip( $post->ID ) : null, 'addressCountry' => function_exists( 'tribe_get_country' ) ? (string) tribe_get_country( $post->ID ) : null, ) ), ) ); } return array_filter( array( '@type' => 'Event', '@id' => get_permalink( $post ) . '#event', 'name' => get_the_title( $post ), 'description' => wp_strip_all_tags( (string) $post->post_excerpt ), 'startDate' => (string) tribe_get_start_date( $post->ID, true, 'c' ), 'endDate' => function_exists( 'tribe_get_end_date' ) ? (string) tribe_get_end_date( $post->ID, true, 'c' ) : null, 'eventAttendanceMode' => 'https://schema.org/OfflineEventAttendanceMode', 'eventStatus' => 'https://schema.org/EventScheduled', 'location' => ! empty( $venue ) ? $venue : null, 'url' => (string) get_permalink( $post ), ) ); } catch ( \Throwable $e ) { return null; } } /** * JobPosting schema from WP Job Manager. * * @return array|null */ public static function job( WP_Post $post ): ?array { if ( ! class_exists( 'WP_Job_Manager' ) || 'job_listing' !== $post->post_type ) { return null; } try { $types = wp_get_post_terms( $post->ID, 'job_listing_type', array( 'fields' => 'names' ) ); $map = array( 'full time' => 'FULL_TIME', 'part time' => 'PART_TIME', 'freelance' => 'CONTRACTOR', 'temporary' => 'TEMPORARY', 'internship' => 'INTERN', ); $type = is_array( $types ) && ! empty( $types ) ? ( $map[ strtolower( (string) $types[0] ) ] ?? 'OTHER' ) : null; return array_filter( array( '@type' => 'JobPosting', '@id' => get_permalink( $post ) . '#job', 'title' => get_the_title( $post ), 'description' => (string) apply_filters( 'the_content', $post->post_content ), 'datePosted' => (string) get_the_date( 'c', $post ), 'validThrough' => (string) get_post_meta( $post->ID, '_job_expires', true ), 'employmentType' => $type, 'hiringOrganization' => array_filter( array( '@type' => 'Organization', 'name' => (string) get_post_meta( $post->ID, '_company_name', true ), 'sameAs' => (string) get_post_meta( $post->ID, '_company_website', true ) ?: null, ) ), 'jobLocation' => array( '@type' => 'Place', 'address' => array( '@type' => 'PostalAddress', 'addressLocality' => (string) get_post_meta( $post->ID, '_job_location', true ), ), ), ) ); } catch ( \Throwable $e ) { return null; } } /** * Translations of the current URL, keyed by hreflang code. * * @return array */ public static function translations(): array { $out = array(); try { // Polylang. if ( function_exists( 'pll_the_languages' ) ) { $languages = pll_the_languages( array( 'raw' => 1 ) ); foreach ( (array) $languages as $language ) { $url = (string) ( $language['url'] ?? '' ); $loc = (string) ( $language['locale'] ?? $language['slug'] ?? '' ); if ( '' !== $url && '' !== $loc ) { $out[ str_replace( '_', '-', $loc ) ] = $url; } } return $out; } // WPML. if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { $languages = apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 1 ) ); foreach ( (array) $languages as $language ) { $url = (string) ( $language['url'] ?? '' ); $loc = (string) ( $language['default_locale'] ?? $language['language_code'] ?? '' ); if ( '' !== $url && '' !== $loc ) { $out[ str_replace( '_', '-', $loc ) ] = $url; } } } } catch ( \Throwable $e ) { return array(); } return $out; } /** * Minutes → ISO 8601 duration, which is the only format Google parses. */ private static function minutes( int $minutes ): ?string { if ( $minutes <= 0 ) { return null; } $hours = intdiv( $minutes, 60 ); $rest = $minutes % 60; return 'PT' . ( $hours > 0 ? $hours . 'H' : '' ) . ( $rest > 0 ? $rest . 'M' : '' ); } }