WPSEO_OpenGraph::description PHP Méthode

description() public méthode

Output the OpenGraph description, specific OG description first, if not, grab the meta description.
public description ( boolean $echo = true ) : string
$echo boolean Whether to echo or return the description.
Résultat string $ogdesc
    public function description($echo = true)
    {
        $ogdesc = '';
        $frontend = WPSEO_Frontend::get_instance();
        if (is_front_page()) {
            if (isset($this->options['og_frontpage_desc']) && $this->options['og_frontpage_desc'] !== '') {
                $ogdesc = wpseo_replace_vars($this->options['og_frontpage_desc'], null);
            } else {
                $ogdesc = $frontend->metadesc(false);
            }
        }
        $is_posts_page = $frontend->is_posts_page();
        if (is_singular() || $is_posts_page) {
            $post_id = $is_posts_page ? get_option('page_for_posts') : get_the_ID();
            $post = get_post($post_id);
            $ogdesc = WPSEO_Meta::get_value('opengraph-description', $post_id);
            // Replace Yoast SEO Variables.
            $ogdesc = wpseo_replace_vars($ogdesc, $post);
            // Use metadesc if $ogdesc is empty.
            if ($ogdesc === '') {
                $ogdesc = $frontend->metadesc(false);
            }
            // Tag og:description is still blank so grab it from get_the_excerpt().
            if (!is_string($ogdesc) || is_string($ogdesc) && $ogdesc === '') {
                $ogdesc = str_replace('[…]', '…', strip_tags(get_the_excerpt()));
            }
        }
        if (is_category() || is_tag() || is_tax()) {
            $ogdesc = WPSEO_Taxonomy_Meta::get_meta_without_term('opengraph-description');
            if ($ogdesc === '') {
                $ogdesc = $frontend->metadesc(false);
            }
            if ($ogdesc === '') {
                $ogdesc = trim(strip_tags(term_description()));
            }
            if ($ogdesc === '') {
                $ogdesc = WPSEO_Taxonomy_Meta::get_meta_without_term('desc');
            }
        }
        // Strip shortcodes if any.
        $ogdesc = strip_shortcodes($ogdesc);
        /**
         * Filter: 'wpseo_opengraph_desc' - Allow changing the OpenGraph description
         *
         * @api string $ogdesc The description string.
         */
        $ogdesc = trim(apply_filters('wpseo_opengraph_desc', $ogdesc));
        if (is_string($ogdesc) && $ogdesc !== '') {
            if ($echo !== false) {
                $this->og_tag('og:description', $ogdesc);
            }
        }
        return $ogdesc;
    }

Usage Example

 /**
  * @covers WPSEO_OpenGraph::description
  */
 public function test_description_category()
 {
     $expected_meta_description = '';
     $category_id = wp_create_category('WordPress SEO');
     $this->go_to(get_category_link($category_id));
     // Checking meta-description and after obtaining its value, reset the meta value for it
     $meta_description = self::$class_instance->description(false);
     $this->assertEquals($expected_meta_description, $meta_description);
 }