2
0
Fork 0
mirror of https://github.com/discourse/wp-discourse.git synced 2025-10-03 08:59:21 +08:00

Improve Discourse Username support && request and test updates (#437)

* Add proper support for Discourse Username

* Update tests config

* Update version

* Bump version
This commit is contained in:
Angus McLeod 2022-02-22 19:38:46 +01:00 committed by GitHub
parent 0269054ee6
commit 90db807e1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 115 additions and 23 deletions

View file

@ -149,7 +149,7 @@ install_db() {
fi
fi

local exists=$(mysql -s -N -e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='$DB_NAME'");
local exists=$(mysql -u $DB_USER -p"$DB_PASS" -s -N -e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='$DB_NAME'");
if [ -n "$exists" ]; then
yes | mysqladmin drop $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
fi

View file

@ -50,5 +50,11 @@
"tests/",
"vendor_namespaced/"
]
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/installers": true
}
}
}

View file

@ -9,6 +9,10 @@ Please make sure you check the version of the PHPUnit Documentation you're viewi

### Setup

#### Dependencies
- svn
- mysql (and mysqladmin)

#### Files and Database

First, set up your tests database by running the following command in the root plugin directory. Make sure you substitute your local root mysql password.

View file

@ -261,10 +261,6 @@ class DiscoursePublish extends DiscourseBase {
if ( ! empty( $featured ) ) {
$baked = str_replace( '{featuredimage}', '![image](' . $featured['0'] . ')', $baked );
}
$username = apply_filters( 'wpdc_discourse_username', get_the_author_meta( 'discourse_username', $post->post_author ), $author_id );
if ( ! $username || strlen( $username ) < 2 ) {
$username = $options['publish-username'];
}

// Get publish category of a post.
$publish_post_category = get_post_meta( $post_id, 'publish_post_category', true );
@ -372,6 +368,15 @@ class DiscoursePublish extends DiscourseBase {
}
}

$username = apply_filters( 'wpdc_discourse_username', get_the_author_meta( 'discourse_username', $post->post_author ), $author_id );
if ( $username && strlen( $username ) > 1 ) {
$change_response = $this->change_post_owner( $post_id, $username );

if ( is_wp_error( $change_response ) ) {
return $change_response;
}
}

// The topic has been created and its associated post's metadata has been updated.
return null;
}
@ -448,6 +453,31 @@ class DiscoursePublish extends DiscourseBase {
return $response;
}

/**
* Changes the owner of a Discourse topic associated with a WordPress post.
*
* @param int $post_id The WordPress post_id.
* @param string $username The username of the Discourse user to change ownership to.
*
* @return null|\WP_Error
*/
protected function change_post_owner( $post_id, $username ) {
$discourse_post_id = get_post_meta( $post_id, 'discourse_post_id', true );
$discourse_topic_id = get_post_meta( $post_id, 'discourse_topic_id', true );

$path = "/t/$discourse_topic_id/change-owner";
$body = array(
'username' => $username,
'post_ids' => array( $discourse_post_id ),
);
$options = array(
'method' => 'POST',
'body' => $body,
);

return $this->remote_post( $path, $options, 'change_owner', $post_id );
}

/**
* Creates an admin_notice and calls the publish_failure_notification method after a bad response is returned from Discourse.
*

View file

@ -399,7 +399,7 @@ trait PluginUtilities {
}

if ( isset( $args['method'] ) ) {
$opts['method'] = $args['method']; // default GET.
$opts['method'] = strtoupper( $args['method'] ); // default GET.
}

$response = wp_remote_request( $url, $opts );

View file

@ -4,7 +4,7 @@ Tags: discourse, forum, comments, sso
Requires at least: 4.7
Tested up to: 5.9
Requires PHP: 5.6.0
Stable tag: 2.3.8
Stable tag: 2.3.9
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

@ -123,6 +123,12 @@ To create a coherent top menu, see our tutorial on how to make a [Custom nav hea

== Changelog ==

#### 2.3.9 02/21/2022

- Add proper Discourse Username support
- Ensure discourse_request method is uppercase.
- Update tests config.

#### 2.3.8 02/04/2022

- Add Wordpress 5.9 support.

View file

@ -108,9 +108,13 @@ trait RemotePost {

if ( $request['method'] != $args['method'] ) {
return new \WP_Error( 'http_request_failed', 'Incorrect method' );
} else {
return $request['response'];
}

if ( isset( $request['body'] ) && $request['body'] != $args['body'] ) {
return new \WP_Error( 'http_request_failed', 'Incorrect body' );
}

return $request['response'];
},
10,
3

View file

@ -306,6 +306,49 @@ class DiscoursePublishTest extends UnitTest {
wp_delete_post( $post_id );
}

/**
* Sync_to_discourse when creating a new post with Discourse Username set.
*/
public function test_sync_to_discourse_discourse_username() {
$discourse_username = 'angus';

// Setup the user
$user_id = $this->factory->user->create();
add_user_meta( $user_id, 'discourse_username', $discourse_username, true );

// Set up a response body for creating a new post
$discourse_post = json_decode( $this->response_body_file( "post_create" ) );
$second_request = array(
'url' => self::$discourse_url . "/t/{$discourse_post->topic_id}/change-owner",
'method' => 'POST',
'body' => json_encode(array(
'username' => $discourse_username,
'post_ids' => [ "{$discourse_post->id}" ]
)),
'response' => $this->build_response( 'success' ),
);
$body = $this->mock_remote_post_success( 'post_create', 'POST', $second_request );

// Add the post.
$post_atts = self::$post_atts;
$post_atts['post_author'] = $user_id;
$post_id = wp_insert_post( $post_atts, false, false );

// Run the publication.
$post = get_post( $post_id );
$response = $this->publish->sync_to_discourse_without_lock(
$post_id,
$post->title,
$post->post_content
);

// Ensure the right result.
$this->assertFalse( is_wp_error( $response ) );

// Cleanup.
wp_delete_post( $post_id );
}

/**
* Sync_to_discourse when updating a post.
*/
@ -654,7 +697,7 @@ class DiscoursePublishTest extends UnitTest {
'method' => 'POST',
'headers' => array(
'Api-Key' => '1234',
'Api-Username' => 'angus',
'Api-Username' => 'system',
),
'body' => http_build_query(
array(
@ -674,4 +717,3 @@ class DiscoursePublishTest extends UnitTest {
);
}
}


View file

@ -2,7 +2,7 @@
/**
* Plugin Name: WP-Discourse
* Description: Use Discourse as a community engine for your WordPress blog
* Version: 2.3.8
* Version: 2.3.9
* Author: Discourse
* Text Domain: wp-discourse
* Domain Path: /languages
@ -34,7 +34,7 @@ define( 'WPDISCOURSE_PATH', plugin_dir_path( __FILE__ ) );
define( 'WPDISCOURSE_URL', plugins_url( '', __FILE__ ) );
define( 'MIN_WP_VERSION', '4.7' );
define( 'MIN_PHP_VERSION', '5.6.0' );
define( 'WPDISCOURSE_VERSION', '2.3.8' );
define( 'WPDISCOURSE_VERSION', '2.3.9' );

require_once WPDISCOURSE_PATH . 'lib/plugin-utilities.php';
require_once WPDISCOURSE_PATH . 'lib/template-functions.php';