find( $post, $meta )['text']; } /** * Finds the description of a page and where it came from. * * @param WP_Post $post The page. * @param string|null $meta Meta description read from the page just rendered, or null to use the stored one. * @return array{text: string, source: string} The description and one of the source constants. */ public function find( WP_Post $post, ?string $meta = null ): array { $source = self::EDITOR; $text = trim( (string) get_post_meta( $post->ID, PostMeta::DESCRIPTION, true ) ); if ( '' === $text ) { $source = self::META; $text = $this->clean( $meta ?? (string) get_post_meta( $post->ID, PostMeta::PAGE_DESCRIPTION, true ) ); } if ( '' === $text ) { $source = self::EXCERPT; $text = $this->clean( $post->post_excerpt ); } $text = (string) apply_filters( 'agentwelcome_description', $text, $post ); return array( 'text' => $text, 'source' => '' === $text ? self::NONE : $source, ); } /** * Turns an excerpt or meta description into plain text of at most 30 words. * * @param string $text Excerpt or meta description. * @return string Plain text. */ private function clean( string $text ): string { $text = wp_strip_all_tags( strip_shortcodes( $text ) ); $text = (string) preg_replace( '/\s+/', ' ', html_entity_decode( $text, ENT_QUOTES, 'UTF-8' ) ); return wp_trim_words( trim( $text ), 30 ); } }