WPSEO_OpenGraph::publish_date PHP Method

publish_date() public method

Output the article publish and last modification date
public publish_date ( ) : boolean;
return boolean;
    public function publish_date()
    {
        if (!is_singular('post')) {
            /**
             * Filter: 'wpseo_opengraph_show_publish_date' - Allow showing publication date for other post types
             *
             * @api bool $unsigned Whether or not to show publish date
             *
             * @param string $post_type The current URL's post type.
             */
            if (false === apply_filters('wpseo_opengraph_show_publish_date', false, get_post_type())) {
                return false;
            }
        }
        $pub = get_the_date(DATE_W3C);
        $this->og_tag('article:published_time', $pub);
        $mod = get_the_modified_date(DATE_W3C);
        if ($mod != $pub) {
            $this->og_tag('article:modified_time', $mod);
            $this->og_tag('og:updated_time', $mod);
        }
        return true;
    }

Usage Example

 /**
  * @covers WPSEO_OpenGraph::publish_date
  */
 public function test_publish_date()
 {
     // not on singular, should return false
     $this->assertFalse(self::$class_instance->publish_date());
     // create post, without tags
     $post_id = $this->factory->post->create();
     $this->go_to(get_permalink($post_id));
     // test published_time tags output
     $published_time = get_the_date('c');
     $published_output = '<meta property="article:published_time" content="' . $published_time . '" />' . "\n";
     $this->assertTrue(self::$class_instance->publish_date());
     $this->expectOutput($published_output);
     // modify post time
     global $post;
     $post = get_post($post_id);
     $post->post_modified = gmdate('Y-m-d H:i:s', time() + 1);
     $post->post_modified_gmt = gmdate('Y-m-d H:i:s', time() + 1 + get_option('gmt_offset') * HOUR_IN_SECONDS);
     // test modified tags output
     $modified_time = get_the_modified_date('c');
     $modified_output = '<meta property="article:modified_time" content="' . $modified_time . '" />' . "\n" . '<meta property="og:updated_time" content="' . $modified_time . '" />' . "\n";
     $this->assertTrue(self::$class_instance->publish_date());
     $this->expectOutput($published_output . $modified_output);
 }