Yoast\WP\SEO\Schema_Aggregator\Application\Enhancement

Article_Schema_Enhancer::get_excerptprivateYoast 1.0

Get article excerpt for description field

Retrieves post excerpt with robust validation (no empty/whitespace-only). Falls back to auto-generated excerpt from content unless prefer_manual is enabled.

Метод класса: Article_Schema_Enhancer{}

Хуков нет.

Возвращает

Строку|null. Excerpt or null if unavailable/invalid.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->get_excerpt( $post_id ): ?string;
$post_id(int) (обязательный)
Post ID.

Код Article_Schema_Enhancer::get_excerpt() Yoast 27.7

private function get_excerpt( int $post_id ): ?string {
	try {
		$excerpt = \get_post_field( 'post_excerpt', $post_id );
		if ( \is_wp_error( $excerpt ) ) {
			$excerpt = '';
		}

		if ( empty( $excerpt ) || \trim( $excerpt ) === '' ) {
			if ( $this->config->get_config_value( 'excerpt_prefer_manual', false ) ) {
				return null;
			}

			$content = \get_post_field( 'post_content', $post_id );

			if ( \is_wp_error( $content ) || empty( $content ) ) {
				return null;
			}
			$excerpt = \wp_trim_excerpt( $content, $post_id );

			if ( empty( $excerpt ) || \trim( $excerpt ) === '' ) {
				return null;
			}
		}
		$excerpt = \wp_strip_all_tags( $excerpt );

		// Apply max length if configured.
		$max_length = $this->config->get_config_value( 'excerpt_max_length', 0 );
		$excerpt    = $this->trim_content_to_max_length( $max_length, $excerpt );

		$excerpt = \trim( $excerpt );

		return ( $excerpt !== '' ) ? $excerpt : null;
	} catch ( Exception $e ) {
		return null;
	}
}