diff --git a/lib/discourse-publish.php b/lib/discourse-publish.php index 3bae97a2..c83d8eaa 100644 --- a/lib/discourse-publish.php +++ b/lib/discourse-publish.php @@ -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 ); } /** @@ -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'] ); @@ -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. * diff --git a/tests/phpunit/test-discourse-publish.php b/tests/phpunit/test-discourse-publish.php index 49a3bea5..c93d6ee6 100644 --- a/tests/phpunit/test-discourse-publish.php +++ b/tests/phpunit/test-discourse-publish.php @@ -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' ); @@ -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' ); @@ -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 @@ -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. */