> $graph Yoast schema graph pieces array. * @param \WPSEO_Schema_Context $context Yoast schema context object. * @return array> Modified graph. */ public function enrich_webpage_piece( array $graph, $context ): array { if ( ! is_singular( PR_Core_Peptide_CPT::POST_TYPE ) ) { return $graph; } $post_id = get_the_ID(); $enrichments = $this->build_webpage_enrichments( (int) $post_id ); if ( empty( $enrichments ) ) { return $graph; } foreach ( $graph as &$piece ) { if ( ! is_array( $piece ) ) { continue; } // Yoast may set @type as a plain string OR an array of strings. // Normalise to array so in_array() works regardless of shape. $raw_type = $piece['@type'] ?? ''; $types = is_array( $raw_type ) ? $raw_type : array( $raw_type ); if ( in_array( 'WebPage', $types, true ) || in_array( 'MedicalWebPage', $types, true ) ) { foreach ( $enrichments as $key => $value ) { $piece[ $key ] = $value; } break; } } return $graph; } /** * Build MedicalWebPage enrichment fields for a peptide post. * * @param int $post_id WordPress post ID. * @return array Enrichment fields to merge into the WebPage node. */ private function build_webpage_enrichments( int $post_id ): array { $enrichments = array(); // lastReviewed: prefer _pr_last_reviewed, fall back to _pr_last_source_verified,. // then post modified date. $last_reviewed = (string) get_post_meta( $post_id, self::META_LAST_REVIEWED, true ); if ( '' === $last_reviewed ) { $last_reviewed = (string) get_post_meta( $post_id, self::META_LAST_VERIFIED, true ); } if ( '' === $last_reviewed ) { $last_reviewed = get_post_modified_time( 'Y-m-d', false, $post_id ) ?: ''; // phpcs:ignore Universal.Operators.DisallowShortTernary.Found } if ( '' !== $last_reviewed ) { $enrichments['lastReviewed'] = sanitize_text_field( $last_reviewed ); } // reviewedBy: Person only when a medical editor is assigned; otherwise Organization. $editor_id = (int) get_post_meta( $post_id, 'medical_editor_id', true ); if ( $editor_id > 0 ) { $editor = get_userdata( $editor_id ); if ( $editor ) { $enrichments['reviewedBy'] = array( '@type' => 'Person', 'name' => sanitize_text_field( $editor->display_name ), 'url' => get_author_posts_url( $editor_id ), ); } } if ( ! isset( $enrichments['reviewedBy'] ) ) { $enrichments['reviewedBy'] = array( '@type' => 'Organization', 'name' => 'Peptide Repo', 'url' => home_url(), ); } // audience: MedicalAudience for researchers. $enrichments['audience'] = array( '@type' => 'MedicalAudience', 'audienceType' => 'Researcher', ); return $enrichments; } }