$fields Field values. * @param WP_Post $post The post. * @param Node_Context $c Page information. * @param Graph $graph The graph. * @return array|null */ public function build( string $type, array $fields, WP_Post $post, Node_Context $c, Graph $graph ): ?array { $headline = $this->text( $fields, 'headline', $this->title( $post ) ); if ( '' === $headline ) { return null; } if ( mb_strlen( $headline ) > self::HEADLINE_LENGTH ) { $headline = rtrim( mb_substr( $headline, 0, self::HEADLINE_LENGTH - 1 ) ) . '…'; } return array( '@type' => $type, '@id' => $c->id( 'article' ), 'isPartOf' => array( '@id' => $c->id( 'webpage' ) ), 'mainEntityOfPage' => array( '@id' => $c->id( 'webpage' ) ), 'headline' => $headline, 'description' => $this->text( $fields, 'description', $this->description( $post, $c ) ), 'datePublished' => get_post_time( 'c', false, $post ), 'dateModified' => get_post_modified_time( 'c', false, $post ), 'author' => $this->author( $post, $graph ), 'publisher' => array( '@id' => $c->entity_id() ), 'image' => $graph->has( $c->id( 'primaryimage' ) ) ? array( '@id' => $c->id( 'primaryimage' ) ) : null, 'wordCount' => $this->word_count( $post ), 'articleSection' => $this->text( $fields, 'section', $this->first_category( $post ) ), 'inLanguage' => get_bloginfo( 'language' ), ); } /** * Number of words in the post's text. * * @since 1.0.0 * * @param WP_Post $post Post. * @return int */ private function word_count( WP_Post $post ): int { $text = trim( wp_strip_all_tags( strip_shortcodes( $post->post_content ) ) ); $words = '' === $text ? false : preg_split( '/\s+/u', $text ); return is_array( $words ) ? count( $words ) : 0; } /** * Name of the first category of a post. * * @since 1.0.0 * * @param WP_Post $post Post. * @return string */ private function first_category( WP_Post $post ): string { if ( ! is_object_in_taxonomy( $post->post_type, 'category' ) ) { return ''; } $terms = get_the_terms( $post, 'category' ); return is_array( $terms ) && $terms ? html_entity_decode( (string) $terms[0]->name, ENT_QUOTES, 'UTF-8' ) : ''; } }