Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable filtering whether Distributor should update distributed post statuses when the origin post status changes #446

Open
wants to merge 35 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
682d32b
dt_distribute_post_status first pass
Aug 19, 2019
b8c6988
distribute status to external connections
Aug 19, 2019
c0a23bc
fix typo
Aug 20, 2019
d59c765
changes for phpcs
Aug 21, 2019
0297cdc
Merge branch 'develop' into feature/distribute_post_status
Sep 27, 2019
a92e40d
status distribution tests part 1, add helper plugin
Oct 15, 2019
51efd70
complete testPushStatusDistribution
Oct 15, 2019
5e7c887
consolidate testing login into TestCase
Oct 15, 2019
826107f
test cleanup
Oct 15, 2019
dbaa76b
Complete network tests
Oct 15, 2019
4731ed7
Add helper for creating an external connection
Oct 15, 2019
a0f6ddb
Add tests for testExternalPushStatusDistribution
Oct 15, 2019
a0ef118
Add test for testExternalPullStatusDistribution
Oct 15, 2019
8716047
when a subscription update includes status, update the post status
Oct 15, 2019
e0d36ca
cleanup
Oct 15, 2019
0817a99
Subscriptions: respect dt_distribute_post_status filter
Oct 15, 2019
795aaa4
external push: initial push can stay as is - status is controlled in …
Oct 15, 2019
1a2fe66
cleanup
Oct 15, 2019
85446c2
Add @hook notation
Oct 15, 2019
e1ffa7f
fixes from phpcbf
Oct 15, 2019
9c95b15
Don’t run testDistributedCount in block editor
Oct 16, 2019
32a9cfa
refactor(internal-connections): reduce indentation level
dinhtungdu Feb 10, 2020
4dabefd
refactor: wrap `dt_distribute_post_status` filter in a helper function
dinhtungdu Feb 10, 2020
c6a89c6
fix: merge conflict
dinhtungdu Feb 25, 2020
589ef5c
Merge branch 'develop' into feature/distribute_post_status
dinhtungdu Feb 25, 2020
f0d9398
test: fix pull external test case
dinhtungdu Feb 25, 2020
b44c41b
test: change expected author to `admin`
dinhtungdu Feb 25, 2020
3a564fd
fix: pull test cases
dinhtungdu Feb 27, 2020
68c74c7
test: fix pull external test case
dinhtungdu Feb 27, 2020
4c6cb39
test: set status of pulled post throught external connection to publish
dinhtungdu Feb 28, 2020
43b8867
test: active test plugin for entire network
dinhtungdu Feb 28, 2020
8c11dfe
test: fix editorHasBlocks check
dinhtungdu Feb 28, 2020
3dbb3b5
test: fix editorHasBlocks calls
dinhtungdu Feb 29, 2020
3cbe0b5
Merge branch 'develop' into feature/distribute_post_status
dinhtungdu Mar 16, 2020
3910c73
Merge branch 'develop' into feature/distribute_post_status
dinhtungdu Sep 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions includes/classes/API/SubscriptionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,19 @@ public function receive_item( $request ) {
return $response;
}

wp_update_post(
[
'ID' => $request['post_id'],
'post_title' => $request['post_data']['title'],
'post_content' => $content,
'post_excerpt' => $request['post_data']['excerpt'],
'post_name' => $request['post_data']['slug'],
]
);
$post_update = [
'ID' => $request['post_id'],
'post_title' => $request['post_data']['title'],
'post_content' => $content,
'post_excerpt' => $request['post_data']['excerpt'],
'post_name' => $request['post_data']['slug'],
];

if ( isset( $request['post_data']['status'] ) ) {
$post_update['post_status'] = $request['post_data']['status'];
}

wp_update_post( $post_update );

/**
* We check if each of these exist since the API removes empty arrays from requests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public static function credentials_form( $args = array() ) {
// Calculate the redirect_uri to use for authorization (the current admin url & query vars).
$redirect_uri = esc_url(
( is_ssl() ? 'https://' : 'http://' ) .
sanitize_text_field( isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' ) . // Input var okay. WPCS: CSRF ok.
sanitize_text_field( isset( $_SERVER['SCRIPT_NAME'] ) ? $_SERVER['SCRIPT_NAME'] : '' ) . // WPCS: input var ok.
sanitize_text_field( isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' ) .
sanitize_text_field( isset( $_SERVER['SCRIPT_NAME'] ) ? $_SERVER['SCRIPT_NAME'] : '' ) .
'?' .
sanitize_text_field( isset( $_SERVER['QUERY_STRING'] ) ? $_SERVER['QUERY_STRING'] : '' ) // WPCS: input var ok.
sanitize_text_field( isset( $_SERVER['QUERY_STRING'] ) ? $_SERVER['QUERY_STRING'] : '' )
);
$args[ self::API_REDIRECT_URI ] = $redirect_uri;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function push( $post_id, $args = array() ) {
'post_excerpt' => $post->post_excerpt,
'post_type' => $post->post_type,
'post_author' => isset( $post->post_author ) ? $post->post_author : get_current_user_id(),
'post_status' => 'publish',
'post_status' => Utils\can_distribute_post_status() ? $post->post_status : 'publish',
);

$post = Utils\prepare_post( $post );
Expand All @@ -97,8 +97,7 @@ public function push( $post_id, $args = array() ) {
}

if ( empty( $args['post_status'] ) ) {
if ( isset( $new_post_args['ID'] ) ) {

if ( isset( $new_post_args['ID'] ) && ! Utils\can_distribute_post_status() ) {
// Avoid updating the status of previously distributed posts.
$existing_status = get_post_status( (int) $new_post_args['ID'] );
if ( $existing_status ) {
Expand Down
4 changes: 4 additions & 0 deletions includes/subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ function send_notifications( $post ) {
}
}

if ( Utils\can_distribute_post_status() ) {
$post_body['post_data']['status'] = $post->post_status;
}

$request = wp_remote_post(
untrailingslashit( $target_url ) . '/wp/v2/dt_subscription/receive',
[
Expand Down
17 changes: 17 additions & 0 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,23 @@ function get_processed_content( $post_content ) {
return $post_content;
}

/**
* Check if post status can be distributed.
*
* @return bool
*/
function can_distribute_post_status() {
/**
* Filter whether Distributor should update post statuses when the origin post status changes.
*
* False by default, return true to have post statuses distributed.
*
* @since 2.0.0
* @hook dt_distribute_post_status
*/
return apply_filters( 'dt_distribute_post_status', false );
}

/**
* Gets the REST URL for a post.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Plugin Name: Auto publish pulled posts.
* Description: Helper plugin for Distributor tests. Set pulled post status of external connections to publish.
* Version: 1.0.0
* Author: 10up Inc.
* Author URI: https://distributorplugin.com
* License: GPLv2 or later
* Text Domain: distributor-tests
* Domain Path: /lang/
* GitHub Plugin URI: https://github.com/10up/distributor-tests
*
* @package distributor-tests
*/

add_filter( 'dt_pull_post_args', function( $post_array ) {
$post_array[ 'post_status' ] = 'publish';

return $post_array;
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Plugin Name: Enable post status distribution
* Description: Helper plugin for Distributor tests. Enables post status distribution with the `dt_distribute_post_status` filter.
* Version: 1.0.0
* Author: 10up Inc.
* Author URI: https://distributorplugin.com
* License: GPLv2 or later
* Text Domain: distributor-tests
* Domain Path: /lang/
* GitHub Plugin URI: https://github.com/10up/distributor-tests
*
* @package distributor-tests
*/

// Enable post status distribution.
add_filter( 'dt_distribute_post_status', '__return_true' );
97 changes: 93 additions & 4 deletions tests/wpacceptance/DistributedPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ public function testDistributedCount() {

$I->loginAs( 'wpsnapshots' );

self::assertPostFieldContains( 40, 'post_title', 'Test Post' );
// Don't test in block editor.
$editor_has_blocks = $this->editorHasBlocks( $I );
if ( $editor_has_blocks ) {
return;
}

return;
self::assertPostFieldContains( 40, 'post_title', 'Test Post' );

// Distribute post
$post_info = $this->pushPost( $I, 40, 2 );
Expand Down Expand Up @@ -65,12 +69,12 @@ public function testDistributedFrom() {

$I->waitUntilElementVisible( 'body.post-php' );

$editor_has_blocks = $this->editorHasBlocks( $I );
$editor_has_blocks = $this->editorHasBlocks( $I, true );
// Make sure we see distributed time in publish box
if ( $editor_has_blocks ) {
$I->seeText( 'Distributed on:', '#distributed-from' );
} else {
$I->seeText( 'Distributed on', '#syndicate-time' );
$I->seeText( 'Distributed on:', '#syndicate-time' );
}

// Make sure we see distributed status admin notice and that it shows as linked
Expand Down Expand Up @@ -102,4 +106,89 @@ public function testDistributedFrom() {

$this->assertTrue( ( false !== strpos( $source, '<link rel="canonical" href="' . rtrim( $post_info['original_front_url'], '/' ) ) ) );
}

/**
* Test network push status updates with the `dt_distribute_post_status` filter.
*/
public function testNetworkPushStatusDistribution() {
$I = $this->openBrowserPage();
$I->loginAs( 'wpsnapshots' );

// Don't test in block editor.
$editor_has_blocks = $this->editorHasBlocks( $I );
if ( $editor_has_blocks ) {
return;
}

$post_info = $this->pushPost( $I, 40, 2 );
$this->statusDistributionTest( $post_info, $I );
}
/**
* Test network pull status updates with the `dt_distribute_post_status` filter.
*/
public function testNetworkPullStatusDistribution() {
$I = $this->openBrowserPage();
$I->loginAs( 'wpsnapshots' );

// Don't test in block editor.
$editor_has_blocks = $this->editorHasBlocks( $I );
if ( $editor_has_blocks ) {
return;
}

$post_info = $this->pullPost( $I, 40, 'two', '' );
$this->statusDistributionTest( $post_info, $I );
}

/**
* Test external push status updates with the `dt_distribute_post_status` filter.
*/
public function testExternalPushStatusDistribution() {
$I = $this->openBrowserPage();
$I->loginAs( 'wpsnapshots' );

// Don't test in block editor.
$editor_has_blocks = $this->editorHasBlocks( $I );
if ( $editor_has_blocks ) {
return;
}

// Create an external connection.
$this->createExternalConnection( $I );
$url = $I->getCurrentUrl();
preg_match( '/post=(\d+)/', $url, $matches );

$post_info = $this->pushPost( $I, 40, (int) $matches[1], '', 'publish', true );
$I->moveTo( 'two/wp-admin/edit.php' );

// Grab the distributed post edit URL.
$I->waitUntilElementVisible( '#the-list' );
$I->click( 'a.row-title' );
$I->waitUntilNavigation();
$url = $I->getCurrentUrl();
$post_info['distributed_edit_url'] = $url;

$this->statusDistributionTest( $post_info, $I );
}

/**
* Test external pull status updates with the `dt_distribute_post_status` filter.
*/
public function testExternalPullStatusDistribution() {
$I = $this->openBrowserPage();
$I->loginAs( 'wpsnapshots' );

// Don't test in block editor.
$editor_has_blocks = $this->editorHasBlocks( $I );
if ( $editor_has_blocks ) {
return;
}

// Create an external connection.
$this->createExternalConnection( $I, 'two', '' );
// Pull post from external connection.
$post_info = $this->pullPost( $I, 40, 'two', '', 'Test External Connection' );
$this->statusDistributionTest( $post_info, $I );
}

}
8 changes: 4 additions & 4 deletions tests/wpacceptance/EditorBlocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testBlocksNetworkPushedContent() {
$I->moveTo( '/wp-admin/post.php?post=40&action=edit' );

// Only test for the block editor.
if ( ! $this->editorHasBlocks( $I ) ) {
if ( ! $this->editorHasBlocks( $I, true ) ) {
return;
}
$this->addContentToTestPost( $I );
Expand Down Expand Up @@ -83,7 +83,7 @@ public function testBlocksNetworkPulledContent() {
$I->moveTo( '/wp-admin/post.php?post=40&action=edit' );

// Only test for the block editor.
if ( ! $this->editorHasBlocks( $I ) ) {
if ( ! $this->editorHasBlocks( $I, true ) ) {
return;
}
$this->addContentToTestPost( $I );
Expand Down Expand Up @@ -112,7 +112,7 @@ public function testBlocksExternalPushedContent() {
$I->moveTo( '/wp-admin/post.php?post=40&action=edit' );

// Only test for the block editor.
if ( ! $this->editorHasBlocks( $I ) ) {
if ( ! $this->editorHasBlocks( $I, true ) ) {
return;
}
$this->addContentToTestPost( $I );
Expand Down Expand Up @@ -170,7 +170,7 @@ public function testBlocksExternalPulledContent() {
$I->moveTo( '/wp-admin/post.php?post=40&action=edit' );

// Only test for the block editor.
if ( ! $this->editorHasBlocks( $I ) ) {
if ( ! $this->editorHasBlocks( $I, true ) ) {
return;
}
$this->addContentToTestPost( $I );
Expand Down
2 changes: 1 addition & 1 deletion tests/wpacceptance/InternalPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testPushDataSync() {

$I->waitUntilElementVisible( '#wpadminbar' );

$editor_has_blocks = $this->editorHasBlocks( $I );
$editor_has_blocks = $this->editorHasBlocks( $I, true );

// Skip this test in Gutenberg for now.
// @todo This needs to be re-written for the Gutenberg UI or easier a prebuilt post added to the docker images we can test push data sync with.
Expand Down
2 changes: 1 addition & 1 deletion tests/wpacceptance/LinkUnlinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testUnlinkPublishPost() {

$I->waitUntilElementVisible( 'body.post-php' );

$editor_has_blocks = $this->editorHasBlocks( $I );
$editor_has_blocks = $this->editorHasBlocks( $I, true );

// I see linked link
if ( $editor_has_blocks ) {
Expand Down
Loading