Skip to content
Merged
11 changes: 11 additions & 0 deletions src/helpers/post-type-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@ public function strip_shortcodes( $content ) {
public function get_the_excerpt( $post_id ) {
return \wp_strip_all_tags( \get_the_excerpt( $post_id ) );
}

/**
* Retrieves the post type of the current post.
*
* @param WP_Post $post The post.
*
* @return string|false Post type on success, false on failure.
Comment thread
IreneStr marked this conversation as resolved.
*/
public function get_post_type( $post = null ) {
Comment thread
IreneStr marked this conversation as resolved.
return \get_post_type( $post );
}
}
4 changes: 2 additions & 2 deletions src/integrations/front-end-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function get_conditionals() {
'Open_Graph\Site_Name',
'Open_Graph\Article_Publisher',
'Open_Graph\Article_Author',
'Open_Graph\Article_Publish_Time',
'Open_Graph\Article_Published_Time',
'Open_Graph\Article_Modified_Time',
'Open_Graph\Image',
];
Expand All @@ -116,7 +116,7 @@ public static function get_conditionals() {
protected $singular_presenters = [
'Open_Graph\Article_Author',
'Open_Graph\Article_Publisher',
'Open_Graph\Article_Publish_Time',
'Open_Graph\Article_Published_Time',
'Open_Graph\Article_Modified_Time',
'Twitter\Creator',
];
Expand Down
35 changes: 35 additions & 0 deletions src/presentations/indexable-post-type-presentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,41 @@ public function generate_og_article_publisher() {
return '';
}

/**
* Generates the open graph article published time.
*
* @return string The open graph article published time.
*/
public function generate_og_article_published_time() {
if ( $this->model->object_sub_type !== 'post' ) {
/**
* Filter: 'wpseo_opengraph_show_publish_date' - Allow showing publication date for other post types.
*
* @api bool Whether or not to show publish date.
*
* @param string $post_type The current URL's post type.
*/
if ( ! apply_filters( 'wpseo_opengraph_show_publish_date', false, $this->post_type->get_post_type( $this->context->post ) ) ) {
Comment thread
IreneStr marked this conversation as resolved.
return '';
}
}

return \mysql2date( DATE_W3C, $this->context->post->post_date_gmt, false );
}

/**
* Generates the open graph article modified time.
*
* @return string The open graph article modified time.
*/
public function generate_og_article_modified_time() {
if ( $this->context->post->post_modified_gmt !== $this->context->post->post_date_gmt ) {
return \mysql2date( DATE_W3C, $this->context->post->post_modified_gmt, false );
}

return '';
}

/**
* @inheritDoc
*/
Expand Down
8 changes: 4 additions & 4 deletions src/presentations/indexable-presentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @property string og_url
* @property string og_article_publisher
* @property string og_article_author
* @property string og_article_publish_time
* @property string og_article_published_time
* @property string og_article_modified_time
* @property string og_locale
* @property array schema
Expand Down Expand Up @@ -265,11 +265,11 @@ public function generate_og_article_author() {
}

/**
* Generates the open graph article publish time.
* Generates the open graph article published time.
*
* @return string The open graph article publish time.
* @return string The open graph article published time.
*/
public function generate_og_article_publish_time() {
public function generate_og_article_published_time() {
return '';
}

Expand Down
33 changes: 33 additions & 0 deletions src/presenters/open-graph/article-modified-time-presenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Presenter class for the OpenGraph article modified time.
*
* @package Yoast\YoastSEO\Presenters\Open_Graph
*/

namespace Yoast\WP\Free\Presenters\Open_Graph;

use Yoast\WP\Free\Presentations\Indexable_Presentation;
use Yoast\WP\Free\Presenters\Abstract_Indexable_Presenter;

/**
* Class Article_Modified_Time_Presenter
*/
class Article_Modified_Time_Presenter extends Abstract_Indexable_Presenter {
/**
* Returns the article modified time tag.
*
* @param Indexable_Presentation $presentation The presentation of an indexable.
*
* @return string The article modified time tag.
*/
public function present( Indexable_Presentation $presentation ) {
$modified_time = $presentation->og_article_modified_time;

if ( is_string( $modified_time ) && $modified_time !== '' ) {
return sprintf( '<meta property="article:modified_time" content="%s" />', \esc_attr( $modified_time ) );
}

return '';
}
}
33 changes: 33 additions & 0 deletions src/presenters/open-graph/article-published-time-presenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Presenter class for the OpenGraph article published time.
*
* @package Yoast\YoastSEO\Presenters\Open_Graph
*/

namespace Yoast\WP\Free\Presenters\Open_Graph;

use Yoast\WP\Free\Presentations\Indexable_Presentation;
use Yoast\WP\Free\Presenters\Abstract_Indexable_Presenter;

/**
* Class Article_Published_Time_Presenter
*/
class Article_Published_Time_Presenter extends Abstract_Indexable_Presenter {
/**
* Returns the article published time tag.
*
* @param Indexable_Presentation $presentation The presentation of an indexable.
*
* @return string The article published time tag.
*/
public function present( Indexable_Presentation $presentation ) {
$published_time = $presentation->og_article_published_time;

if ( is_string( $published_time ) && $published_time !== '' ) {
return sprintf( '<meta property="article:published_time" content="%s" />', \esc_attr( $published_time ) );
}

return '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @coversDefaultClass \Yoast\WP\Free\Presentations\Indexable_Post_Type_Presentation
*
* @group presentations
* @group open-graph
* @group opengraph
*/
class OG_Article_Author_Test extends TestCase {
use Presentation_Instance_Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Yoast\WP\Free\Tests\Presentations\Indexable_Post_Type_Presentation;

use Yoast\WP\Free\Tests\TestCase;

/**
* Class OG_Article_Modified_Time_Test
*
* @coversDefaultClass \Yoast\WP\Free\Presentations\Indexable_Post_Type_Presentation
*
* @group presentations
* @group opengraph
*/
class OG_Article_Modified_Time_Test extends TestCase {
Comment thread
IreneStr marked this conversation as resolved.
use Presentation_Instance_Builder;

/**
* Sets up the test class.
*/
public function setUp() {
parent::setUp();

$this->setInstance();
}

/**
* Tests that no modified time is returned if it is the same as the published time
*
* @covers ::generate_og_article_modified_time
*/
public function test_generate_og_article_modified_time_same_as_published_time() {
$this->context->post = (object) [
'post_date_gmt' => '2019-10-08T12:26:31+00:00',
'post_modified_gmt' => '2019-10-08T12:26:31+00:00',
];
$actual = $this->instance->generate_og_article_modified_time();
$this->assertEmpty( $actual );
}

/**
* Tests that the modified time is returned if it differs from the published time.
*
* @covers ::generate_og_article_modified_time
*/
public function test_generate_og_article_modified_time_differs_from_published_time() {
$this->context->post = (object) [
'post_date_gmt' => '2019-10-08T12:26:31+00:00',
'post_modified_gmt' => '2019-11-09T12:34:56+00:00',
];

$actual = $this->instance->generate_og_article_modified_time();
$expected = '2019-11-09T12:34:56+00:00';
$this->assertEquals( $expected, $actual );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Yoast\WP\Free\Tests\Presentations\Indexable_Post_Type_Presentation;

use Yoast\WP\Free\Tests\TestCase;
use Brain\Monkey;

/**
* Class OG_Article_Published_Time_Test
*
* @coversDefaultClass \Yoast\WP\Free\Presentations\Indexable_Post_Type_Presentation
*
* @group presentations
* @group opengraph
*/
class OG_Article_Published_Time_Test extends TestCase {
use Presentation_Instance_Builder;

/**
* Sets up the test class.
*/
public function setUp() {
parent::setUp();

$this->setInstance();
}

/**
* Tests that the published time is returned for a post.
*
* @covers ::generate_og_article_published_time
*/
public function test_generate_og_article_published_time_post() {
$this->indexable->object_sub_type = 'post';
$this->context->post = (object) [ 'post_date_gmt' => '2019-10-08T12:26:31+00:00' ];
$actual = $this->instance->generate_og_article_published_time();
$expected = '2019-10-08T12:26:31+00:00';

$this->assertEquals( $expected, $actual );
}

/**
* Tests that no published time is returned for a page.
*
* @covers ::generate_og_article_published_time
*/
public function test_generate_og_article_published_time_page() {
$this->indexable->object_sub_type = 'page';

$this->post_type_helper
->expects( 'get_post_type' )
->once()
->andReturn( 'page' );

$actual = $this->instance->generate_og_article_published_time();
$this->assertEmpty( $actual );
}

/**
* Tests that a published time is returned for a page when the publish date is enabled with the wpseo_opengraph_show_publish_date filter.
*
* @covers ::generate_og_article_published_time
*/
public function test_generate_og_article_published_time_page_enabled() {
$this->context->post = (object) [ 'post_date_gmt' => '2019-10-08T12:26:31+00:00' ];
$this->indexable->object_sub_type = 'page';

$this->post_type_helper
->expects( 'get_post_type' )
->once()
->andReturn( 'page' );

Monkey\Filters\expectApplied( 'wpseo_opengraph_show_publish_date' )
->once()
->andReturn( true );


$actual = $this->instance->generate_og_article_published_time();
$expected = '2019-10-08T12:26:31+00:00';
$this->assertEquals( $expected, $actual );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @coversDefaultClass \Yoast\WP\Free\Presentations\Indexable_Post_Type_Presentation
*
* @group presentations
* @group open-graph
* @group opengraph
*/
class OG_Article_Publisher_Test extends TestCase {
use Presentation_Instance_Builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @coversDefaultClass \Yoast\WP\Free\Presentations\Indexable_Post_Type_Presentation
*
* @group presentations
* @group open-graph
* @group opengraph
*/
class Open_Graph_Description_Test extends TestCase {
use Presentation_Instance_Builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Yoast\WP\Free\Tests\TestCase;

/**
* Class OG_Article_Publish_Time_Test
* Class OG_Article_Published_Time_Test
*
* @coversDefaultClass \Yoast\WP\Free\Presentations\Indexable_Presentation
*
* @group presentations
* @group opengraph
*/
class OG_Article_Publish_Time_Test extends TestCase {
class OG_Article_Published_Time_Test extends TestCase {
use Presentation_Instance_Builder;

/**
Expand All @@ -26,9 +27,9 @@ public function setUp() {
/**
* Tests whether an empty string is returned.
*
* @covers ::generate_og_article_publish_time
* @covers ::generate_og_article_published_time
*/
public function test_generate_og_article_publish_time_and_return_empty() {
$this->assertEmpty( $this->instance->generate_og_article_publish_time() );
public function test_generate_og_article_published_time_and_return_empty() {
$this->assertEmpty( $this->instance->generate_og_article_published_time() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ protected function setInstance() {
$this->current_page_helper = Mockery::mock( Current_Page_Helper::class );
$this->og_image_helper = Mockery::mock( OG_Image_Helper::class );
$this->url_helper = Mockery::mock( Url_Helper::class );
$this->post_type_helper = Mockery::mock( Post_Type_Helper::class );

$this->context = Mockery::mock( Meta_Tags_Context::class );

Expand All @@ -108,7 +109,8 @@ protected function setInstance() {
$this->robots_helper,
$this->image_helper,
$this->options_helper,
$this->current_page_helper
$this->current_page_helper,
$this->post_type_helper
);

$this->set_instance_generators();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @coversDefaultClass \Yoast\WP\Free\Presenters\Open_Graph\Article_Author_Presenter
*
* @group presenters
* @group open-graph
* @group opengraph
*/
class Article_Author_Presenter_Test extends TestCase {

Expand Down
Loading