find_by_id( $post_id ); if ( ! $peptide ) { return; } $schema = $this->build_drug_schema( $peptide ); /** * Filter the JSON-LD schema for a peptide page. * * @param array $schema Schema.org data array. * @param PR_Core_Peptide_DTO $peptide The peptide DTO. */ $schema = apply_filters( 'pr_core_jsonld_peptide', $schema, $peptide ); if ( empty( $schema ) ) { return; } printf( '' . "\n", wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ) ); } /** * Build a schema.org Drug object for a peptide. * * @param PR_Core_Peptide_DTO $peptide Peptide data. * @return array Schema.org JSON-LD structure. */ private function build_drug_schema( PR_Core_Peptide_DTO $peptide ): array { $schema = [ '@context' => 'https://schema.org', '@type' => 'Drug', 'name' => $peptide->display_name ?: $peptide->title, 'url' => get_permalink( $peptide->id ), 'description' => $peptide->excerpt, ]; if ( ! empty( $peptide->aliases ) ) { $schema['alternateName'] = $peptide->aliases; } if ( '' !== $peptide->molecular_formula ) { $schema['molecularFormula'] = $peptide->molecular_formula; } if ( $peptide->molecular_weight > 0 ) { $schema['molecularWeight'] = [ '@type' => 'QuantitativeValue', 'value' => $peptide->molecular_weight, 'unitText' => 'Da', ]; } // External identifiers as codes. $codes = []; if ( '' !== $peptide->cas_number ) { $codes[] = [ '@type' => 'MedicalCode', 'codeValue' => $peptide->cas_number, 'codingSystem' => 'CAS', ]; } if ( '' !== $peptide->drugbank_id ) { $codes[] = [ '@type' => 'MedicalCode', 'codeValue' => $peptide->drugbank_id, 'codingSystem' => 'DrugBank', ]; } if ( ! empty( $codes ) ) { $schema['code'] = $codes; } return $schema; } }