Skip to content

Commit

Permalink
Merge pull request #585 from ConvertKit/broadcasts-to-posts-author
Browse files Browse the repository at this point in the history
Broadcasts to Posts: Add Post Author Option
  • Loading branch information
n7studios authored Nov 17, 2023
2 parents ae37730 + 880cba4 commit ed4b1c8
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
39 changes: 39 additions & 0 deletions admin/section/class-convertkit-admin-settings-broadcasts.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ public function register_fields() {
)
);

add_settings_field(
'author_id',
__( 'Author', 'convertkit' ),
array( $this, 'author_id_callback' ),
$this->settings_key,
$this->name,
array(
'name' => 'author_id',
'label_for' => 'author_id',
'description' => __( 'The WordPress User to set as the author for WordPress Posts created from imported broadcasts.', 'convertkit' ),
)
);

add_settings_field(
'category_id',
__( 'Category', 'convertkit' ),
Expand Down Expand Up @@ -358,6 +371,32 @@ public function post_status_callback( $args ) {

}

/**
* Renders the input for the author setting.
*
* @since 2.3.9
*
* @param array $args Setting field arguments (name,description).
*/
public function author_id_callback( $args ) {

// Build field.
$select_field = wp_dropdown_users(
array(
'echo' => false,
'selected' => $this->settings->author_id(),
'include_selected' => true,
'name' => $this->settings_key . '[' . $args['name'] . ']',
'id' => $this->settings_key . '_' . $args['name'],
'class' => 'enabled convertkit-select2',
)
);

// Output field.
echo '<div class="convertkit-select2-container">' . $select_field . '</div>' . $this->get_description( $args['description'] ); // phpcs:ignore WordPress.Security.EscapeOutput

}

/**
* Renders the input for the category setting.
*
Expand Down
1 change: 1 addition & 0 deletions tests/_support/Helper/Acceptance/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ public function setupConvertKitPluginBroadcasts($I, $settings = false)

case 'post_status':
case 'category_id':
case 'author_id':
$I->fillSelect2Field($I, '#select2-_wp_convertkit_settings_broadcasts_' . $key . '-container', $value);

break;
Expand Down
60 changes: 60 additions & 0 deletions tests/acceptance/broadcasts/import/BroadcastsToPostsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,66 @@ public function testBroadcastsImportWithPostStatusEnabled(AcceptanceTester $I)
}
}

/**
* Tests that Broadcasts import when enabled in the Plugin's settings,
* an Author is defined and the Author is assigned to the created
* WordPress Posts.
*
* @since 2.3.8
*
* @param AcceptanceTester $I Tester.
*/
public function testBroadcastsImportWithAuthorIDEnabled(AcceptanceTester $I)
{
// Add a WordPress User with an Editor role.
$I->haveUserInDatabase( 'editor', 'editor' );

// Enable Broadcasts to Posts.
$I->setupConvertKitPluginBroadcasts(
$I,
[
'enabled' => true,
'author_id' => 'editor',
'category_id' => $this->categoryName,
'published_at_min_date' => '01/01/2020',
]
);

// Run the WordPress Cron event to import Broadcasts to WordPress Posts.
$I->runCronEvent($I, $this->cronEventName);

// Wait a few seconds for the Cron event to complete importing Broadcasts.
$I->wait(7);

// Load the Posts screen.
$I->amOnAdminPage('edit.php');

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Confirm expected Broadcasts exist as Posts.
$I->see($_ENV['CONVERTKIT_API_BROADCAST_FIRST_TITLE']);
$I->see($_ENV['CONVERTKIT_API_BROADCAST_SECOND_TITLE']);
$I->see($_ENV['CONVERTKIT_API_BROADCAST_THIRD_TITLE']);

// Get created Post IDs.
$postIDs = [
(int) str_replace('post-', '', $I->grabAttributeFrom('tbody#the-list > tr:nth-child(1)', 'id')),
(int) str_replace('post-', '', $I->grabAttributeFrom('tbody#the-list > tr:nth-child(2)', 'id')),
(int) str_replace('post-', '', $I->grabAttributeFrom('tbody#the-list > tr:nth-child(3)', 'id')),
];

// Confirm each Post's status is private.
foreach ($postIDs as $postID) {
$I->seePostInDatabase(
[
'ID' => $postID,
'post_author' => '2',
]
);
}
}

/**
* Tests that Broadcasts import when enabled in the Plugin's settings
* a Category is defined and the Category is assigned to the created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testAccessibility(AcceptanceTester $I)
// Confirm that settings have label[for] attributes.
$I->seeInSource('<label for="enabled">');
$I->seeInSource('<label for="post_status">');
$I->seeInSource('<label for="author_id">');
$I->seeInSource('<label for="category_id">');
$I->seeInSource('<label for="published_at_min_date">');
$I->seeInSource('<label for="no_styles">');
Expand Down Expand Up @@ -75,6 +76,7 @@ public function testEnableDisableImport(AcceptanceTester $I)
$I->seeCheckboxIsChecked('#enabled');
$I->seeElement('table.form-table tbody tr td a.button');
$I->seeElement('span[aria-labelledby="select2-_wp_convertkit_settings_broadcasts_post_status-container"]');
$I->seeElement('span[aria-labelledby="select2-_wp_convertkit_settings_broadcasts_author_id-container"]');
$I->seeElement('span[aria-labelledby="select2-_wp_convertkit_settings_broadcasts_category_id-container"]');
$I->seeElement('div.convertkit-select2-container');
$I->seeElement('input#published_at_min_date');
Expand All @@ -98,6 +100,7 @@ public function testEnableDisableImport(AcceptanceTester $I)
$I->dontSeeCheckboxIsChecked('#enabled');
$I->dontSeeElement('table.form-table tbody tr td a.button');
$I->dontSeeElement('span[aria-labelledby="select2-_wp_convertkit_settings_broadcasts_post_status-container"]');
$I->dontSeeElement('span[aria-labelledby="select2-_wp_convertkit_settings_broadcasts_author_id-container"]');
$I->dontSeeElement('span[aria-labelledby="select2-_wp_convertkit_settings_broadcasts_category_id-container"]');
$I->dontSeeElement('input#published_at_min_date');

Expand All @@ -120,6 +123,7 @@ public function testSaveSettings(AcceptanceTester $I)
// Enable Broadcasts to Posts, and modify settings.
$I->checkOption('#enabled');
$I->fillSelect2Field($I, '#select2-_wp_convertkit_settings_broadcasts_post_status-container', 'Draft');
$I->fillSelect2Field($I, '#select2-_wp_convertkit_settings_broadcasts_author_id-container', 'admin');
$I->fillSelect2Field($I, '#select2-_wp_convertkit_settings_broadcasts_category_id-container', 'ConvertKit Broadcasts to Posts');
$I->fillField('_wp_convertkit_settings_broadcasts[published_at_min_date]', '01/01/2023');
$I->checkOption('#enabled_export');
Expand All @@ -134,6 +138,7 @@ public function testSaveSettings(AcceptanceTester $I)
// Confirm that settings saved.
$I->seeCheckboxIsChecked('#enabled');
$I->seeInField('_wp_convertkit_settings_broadcasts[post_status]', 'Draft');
$I->seeInField('_wp_convertkit_settings_broadcasts[author_id]', 'admin');
$I->seeInField('_wp_convertkit_settings_broadcasts[category_id]', 'ConvertKit Broadcasts to Posts');
$I->seeInField('_wp_convertkit_settings_broadcasts[published_at_min_date]', '2023-01-01');
$I->seeCheckboxIsChecked('#enabled_export');
Expand Down

0 comments on commit ed4b1c8

Please sign in to comment.