Skip to content

Commit

Permalink
Merge pull request #3370 from google/feature/3271-custom-draft-label
Browse files Browse the repository at this point in the history
Feature/3271 custom draft label
  • Loading branch information
tofumatt authored May 17, 2021
2 parents 513ad00 + 1d3cffa commit 1181d49
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
22 changes: 22 additions & 0 deletions includes/Modules/Idea_Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ public function register() {
$post_meta = new Post_Meta();

$this->register_scopes_hook();
if ( $this->is_connected() ) {
/**
* Changes the posts view to have a custom label in place of Draft for Idea Hub Drafts.
*/
add_filter(
'display_post_states',
function( $post_states, $post ) {
if ( 'draft' !== $post->post_status ) {
return $post_states;
}
$idea = $this->get_post_idea( $post->ID );
if ( is_null( $idea ) ) {
return $post_states;
}
/* translators: %s: Idea Hub Idea Title */
$post_states['draft'] = sprintf( __( 'Idea Hub Draft “%s”', 'google-site-kit' ), $idea['text'] );
return $post_states;
},
10,
2
);
}

$this->post_name_setting = new Post_Idea_Name( $post_meta );
$this->post_name_setting->register();
Expand Down
53 changes: 53 additions & 0 deletions tests/phpunit/integration/Modules/Idea_HubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,59 @@ public function test_is_connected() {
$this->assertTrue( $idea_hub->is_connected() );
}

public function test_draft_labels() {
$post1 = $this->factory()->post->create_and_get( array( 'post_status' => 'draft' ) );
$topics = array(
array(
'mid' => '/m/05z6w',
'display_name' => 'Penguins',
),
);
add_post_meta( $post1->ID, 'googlesitekitpersistent_idea_name', 'ideas/2285812891948871921' );
add_post_meta( $post1->ID, 'googlesitekitpersistent_idea_text', 'Using Site Kit to analyze your success' );
add_post_meta( $post1->ID, 'googlesitekitpersistent_idea_topics', $topics );
$post_states1 = apply_filters( 'display_post_states', array( 'draft' => 'Draft' ), $post1 );

// Idea Hub module is not enabled yet.
$this->assertEquals( $post_states1, array( 'draft' => 'Draft' ) );

// Connect the module
$options = new Options( $this->context );
$idea_hub = new Idea_Hub( $this->context, $options );

$options->set(
Settings::OPTION,
array(
'ideaLocale' => 'en_US',
)
);

// Create the post
$post2 = $this->factory()->post->create_and_get( array( 'post_status' => 'draft' ) );
$post3 = $this->factory()->post->create_and_get( array( 'post_status' => 'draft' ) );
$idea = array(
'name' => 'ideas/17450692223393508734',
'text' => 'Why Penguins are guanotelic?',
'topics' => array(
'/m/05z6w' => 'Penguins',
),
);

$this->idea_hub->register();
$this->idea_hub->set_post_idea( $post2->ID, $idea );

// With an IdeaHub post
$post_states2 = apply_filters( 'display_post_states', array( 'draft' => 'Draft' ), $post2 );
// With a regular draft post
$post_states3 = apply_filters( 'display_post_states', array( 'draft' => 'Draft' ), $post3 );

$post_states1 = apply_filters( 'display_post_states', array( 'draft' => 'Draft' ), $post1 );

$this->assertEquals( $post_states1, array( 'draft' => 'Idea Hub Draft “Using Site Kit to analyze your success”' ) );
$this->assertEquals( $post_states2, array( 'draft' => 'Idea Hub Draft “Why Penguins are guanotelic?”' ) );
$this->assertEquals( $post_states3, array( 'draft' => 'Draft' ) );
}

public function test_on_deactivation() {
$options = new Options( $this->context );
$options->set( Settings::OPTION, 'test-value' );
Expand Down

0 comments on commit 1181d49

Please sign in to comment.