) { if ( isset( $schema['isCustom'] ) ) { unset( $schema['isCustom'] ); return; } // Remove empty ImageObject. if ( isset( $schema['image'] ) && empty( $schema['image']['url'] ) && ! is_array( $schema['image'] ) ) { unset( $schema['image'] ); } $jsonld->parts['canonical'] = ! empty( $jsonld->parts['canonical'] ) ? $jsonld->parts['canonical'] : \RankMath\Paper\Paper::get()->get_canonical(); $schema['@id'] = $jsonld->parts['canonical'] . '#' . $id; $types = array_map( 'strtolower', (array) $schema['@type'] ); foreach ( $types as $type ) { $is_event = Str::contains( 'event', $type ); // Add publisher entity @id in the organizer property of Event schema. if ( $is_event ) { $jsonld->add_prop( 'publisher', $schema, 'organizer', $schemas ); } $props = [ 'is_part_of' => [ 'key' => 'webpage', 'value' => ! in_array( $type, [ 'jobposting', 'musicgroup', 'person', 'product', 'productgroup', 'restaurant', 'service' ], true ) && ! $is_event, ], 'publisher' => [ 'key' => 'publisher', 'value' => ! in_array( $type, [ 'jobposting', 'musicgroup', 'person', 'product', 'productgroup', 'restaurant', 'service' ], true ) && ! $is_event, ], 'thumbnail' => [ 'key' => 'image', 'value' => ! in_array( $type, [ 'videoobject' ], true ) || isset( $schema['image'] ), ], 'language' => [ 'key' => 'inLanguage', 'value' => ! in_array( $type, [ 'person', 'service', 'restaurant', 'product', 'productgroup', 'musicgroup', 'musicalbum', 'jobposting' ], true ), ], ]; if ( isset( $schema['image'] ) && 'product' === $type && is_array( $schema['image'] ) ) { $props['thumbnail']['value'] = false; } foreach ( $props as $prop => $data ) { if ( ! $data['value'] ) { continue; } $jsonld->add_prop( $prop, $schema, $data['key'], $schemas ); } } } /** * Add mainEntityOfPage property to Primary schema entity. * * @param array $schema Schema Entity. * @param JsonLD $jsonld JsonLD Instance. */ private function add_main_entity_of_page( &$schema, $jsonld ) { if ( ! isset( $schema['isPrimary'] ) ) { return; } if ( ! empty( $schema['isPrimary'] ) ) { $schema['mainEntityOfPage'] = [ '@id' => $jsonld->parts['canonical'] . '#webpage' ]; } unset( $schema['isPrimary'] ); } /** * Remove Person entity if it is not referenced in any other entities. * * @param array $data Array of json-ld data. * * @return array */ public function remove_person_entity( $data ) { if ( empty( $data['ProfilePage'] ) || empty( $data['ProfilePage']['@id'] ) || ! is_singular() ) { return $data; } if ( function_exists( 'bp_is_user' ) && bp_is_user() ) { return $data; } $temp_data = $data; $id = $temp_data['ProfilePage']['@id']; $ids = []; unset( $temp_data['ProfilePage'] ); array_walk_recursive( $temp_data, function ( $value, $key ) use ( &$ids, $id ) { if ( '@id' === $key && $value === $id ) { $ids[] = $value; } } ); if ( empty( $ids ) ) { unset( $data['ProfilePage'] ); } return $data; } /** * Change WebPage entity type depending on the schemas on the page. * * @param array $schemas Schema data. * @param array $types Schema types. * * @return array */ private function change_webpage_entity( $schemas, $types ) { if ( in_array( 'Product', $types, true ) ) { $schemas['WebPage']['@type'] = 'ItemPage'; } if ( isset( $schemas['howto'] ) && ! empty( $schemas['WebPage'] ) ) { $schemas['howto']['mainEntityOfPage'] = [ '@id' => $schemas['WebPage']['@id'] ]; } $faq_data = array_map( function ( $schema ) { return isset( $schema['@type'] ) && 'FAQPage' === $schema['@type']; }, $schemas ); $faq_key = is_array( $faq_data ) && ! empty( $faq_data ) ? key( array_filter( $faq_data ) ) : ''; if ( ! $faq_key ) { return $schemas; } if ( in_array( $faq_key, array_keys( $schemas ), true ) ) { $schemas['WebPage']['@type'] = ! empty( $types ) ? array_merge( (array) $schemas['WebPage']['@type'], [ 'FAQPage' ] ) : 'FAQPage'; $schemas['WebPage']['mainEntity'] = $schemas[ $faq_key ]['mainEntity']; unset( $schemas[ $faq_key ] ); } return $schemas; } }