Skip to content

Commit

Permalink
Don't auto-publish quick edits (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
angusmcleod authored Nov 27, 2024
1 parent 65f2e6d commit 4d758d9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
23 changes: 18 additions & 5 deletions lib/discourse-publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ protected function exclude_post( $post_id, $post ) {
$publish_status_not_set = 'publish' !== get_post_status( $post_id );
$publish_private = apply_filters( 'wpdc_publish_private_post', false, $post_id );
return wp_is_post_revision( $post_id )
|| ( $publish_status_not_set && ! $publish_private )
|| $plugin_unconfigured
|| empty( $post->post_title )
|| ! $this->is_valid_sync_post_type( $post_id )
|| $this->has_excluded_tag( $post );
|| ( $publish_status_not_set && ! $publish_private )
|| $plugin_unconfigured
|| empty( $post->post_title )
|| ! $this->is_valid_sync_post_type( $post_id )
|| $this->has_excluded_tag( $post );
}

/**
Expand All @@ -142,6 +142,10 @@ protected function exclude_post( $post_id, $post ) {
* @return bool
*/
protected function auto_publish( $post_id ) {
// Don't auto-publish quick edits.
if ( $this->quick_edit() ) {
return false;
}
// If the auto-publish option is enabled publish unpublished topics, unless the setting has been overridden.
$auto_publish_overridden = intval( $this->dc_get_post_meta( $post_id, 'wpdc_auto_publish_overridden', true ) ) === 1;
return ! $auto_publish_overridden && ! empty( $this->options['auto-publish'] );
Expand Down Expand Up @@ -872,6 +876,15 @@ public function topic_blog_id_exists( $topic_id ) {
return $row ? true : false;
}

/**
* Determines if request is a quick edit.
*
* @return bool
*/
public function quick_edit() {
return check_ajax_referer( 'inlineeditnonce', '_inline_edit', false );
}

/**
* Gets post metadata via wp method, or directly from db, depending on the direct-db-publication-flags option.
*
Expand Down
45 changes: 35 additions & 10 deletions tests/phpunit/test-discourse-publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public function setUp(): void {
$this->publish->setup_logger();
}

/**
* Sync_to_discourse handles new posts correctly.
*/
/**
* Sync_to_discourse handles new posts correctly.
*/
public function test_sync_to_discourse_when_creating() {
// Set up a response body for creating a new post.
$body = $this->mock_remote_post_success( 'post_create', 'POST' );
Expand Down Expand Up @@ -853,12 +853,12 @@ public function test_force_publish_max_age_prevents_older_posts_from_being_publi
wp_delete_post( $post_id );
}

/**
* Test that HTML entities are converted to their special characters.
*/
public function test_conversion_of_html_entities_in_title() {
$title_with_entities = 'Title with & and –';
$title_with_decoded_entities = 'Title with & and –';
/**
* Test that HTML entities are converted to their special characters.
*/
public function test_conversion_of_html_entities_in_title() {
$title_with_entities = 'Title with & and –';
$title_with_decoded_entities = 'Title with & and –';
self::$post_atts['post_title'] = $title_with_entities;

$response = $this->build_response( 'success' );
Expand Down Expand Up @@ -891,7 +891,7 @@ function ( $prempt, $args, $url ) use ( $response, $title_with_decoded_entities

// Cleanup.
wp_delete_post( $post_id );
}
}

/**
* Posts can only be published via XMLRPC by hooking into the wp_discourse_before_xmlrpc_publish filter with a function
Expand Down Expand Up @@ -943,6 +943,31 @@ public function test_xmlrpc_publish_failure_notification() {
wp_delete_post( $post_id );
}

/**
* When the auto-publish option is enabled, quick edits of un-published posts are not published
*/
public function test_quick_edits_of_unpublished_posts() {
$plugin_options = self::$plugin_options;
$plugin_options['auto-publish'] = 1;

$publish = \Mockery::mock( $this->publish )->makePartial();
$publish->setup_options( $plugin_options );

$post_atts = self::$post_atts;
$post_id = wp_insert_post( $post_atts, false, false );

$publish->shouldReceive( 'quick_edit' )->andReturn( true );

$post = get_post( $post_id );
$publish->publish_post_after_save( $post_id, $post );

$publish->shouldNotReceive( 'sync_to_discourse' );

$this->assertEmpty( get_post_meta( $post_id, 'discourse_post_id', true ) );

wp_delete_post( $post_id );
}

/**
* Successful remote_post request returns original response.
*/
Expand Down

0 comments on commit 4d758d9

Please sign in to comment.