Fix blog-wide account when WordPress is in subdirectory (#744)

* add broken tests

to fix them

* respect subpathes
This commit is contained in:
Matthias Pfefferle 2024-05-02 14:59:57 +02:00 committed by GitHub
parent bea7ee1868
commit cdd303cea6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 10 deletions

View file

@ -144,24 +144,31 @@ class Users {
// try to extract the scheme and the host // try to extract the scheme and the host
if ( preg_match( '/^([a-zA-Z^:]+):(.*)$/i', $resource, $match ) ) { if ( preg_match( '/^([a-zA-Z^:]+):(.*)$/i', $resource, $match ) ) {
// extract the scheme // extract the scheme
$scheme = esc_attr( $match[1] ); $scheme = \esc_attr( $match[1] );
} }
switch ( $scheme ) { switch ( $scheme ) {
// check for http(s) URIs // check for http(s) URIs
case 'http': case 'http':
case 'https': case 'https':
$url_parts = wp_parse_url( $resource ); $resource_path = \wp_parse_url( $resource, PHP_URL_PATH );
// check for http(s)://blog.example.com/@username if ( $resource_path ) {
if ( $blog_path = \wp_parse_url( \home_url(), PHP_URL_PATH );
isset( $url_parts['path'] ) &&
str_starts_with( $url_parts['path'], '/@' )
) {
$identifier = str_replace( '/@', '', $url_parts['path'] );
$identifier = untrailingslashit( $identifier );
return self::get_by_username( $identifier ); if ( $blog_path ) {
$resource_path = \str_replace( $blog_path, '', $resource_path );
}
$resource_path = \trim( $resource_path, '/' );
// check for http(s)://blog.example.com/@username
if ( str_starts_with( $resource_path, '@' ) ) {
$identifier = \str_replace( '@', '', $resource_path );
$identifier = \trim( $identifier, '/' );
return self::get_by_username( $identifier );
}
} }
// check for http(s)://blog.example.com/author/username // check for http(s)://blog.example.com/author/username

View file

@ -11,6 +11,18 @@ class Test_Activitypub_Users_Collection extends WP_UnitTestCase {
* @dataProvider the_resource_provider * @dataProvider the_resource_provider
*/ */
public function test_get_by_various( $resource, $expected ) { public function test_get_by_various( $resource, $expected ) {
$path = wp_parse_url( $resource, PHP_URL_PATH );
if ( str_starts_with( $path, '/blog/' ) ) {
add_filter(
'home_url',
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable, Generic.CodeAnalysis.UnusedFunctionParameter.Found
function ( $url ) {
return 'http://example.org/blog/';
}
);
}
$user = Activitypub\Collection\Users::get_by_resource( $resource ); $user = Activitypub\Collection\Users::get_by_resource( $resource );
$this->assertInstanceOf( $expected, $user ); $this->assertInstanceOf( $expected, $user );
@ -20,6 +32,7 @@ class Test_Activitypub_Users_Collection extends WP_UnitTestCase {
return array( return array(
array( 'http://example.org/?author=1', 'Activitypub\Model\User' ), array( 'http://example.org/?author=1', 'Activitypub\Model\User' ),
array( 'https://example.org/?author=1', 'Activitypub\Model\User' ), array( 'https://example.org/?author=1', 'Activitypub\Model\User' ),
array( 'https://example.org?author=1', 'Activitypub\Model\User' ),
array( 'http://example.org/?author=7', 'WP_Error' ), array( 'http://example.org/?author=7', 'WP_Error' ),
array( 'acct:admin@example.org', 'Activitypub\Model\User' ), array( 'acct:admin@example.org', 'Activitypub\Model\User' ),
array( 'acct:blog@example.org', 'Activitypub\Model\Blog_User' ), array( 'acct:blog@example.org', 'Activitypub\Model\Blog_User' ),
@ -32,6 +45,10 @@ class Test_Activitypub_Users_Collection extends WP_UnitTestCase {
array( 'http://example.org/@blog', 'Activitypub\Model\Blog_User' ), array( 'http://example.org/@blog', 'Activitypub\Model\Blog_User' ),
array( 'https://example.org/@blog', 'Activitypub\Model\Blog_User' ), array( 'https://example.org/@blog', 'Activitypub\Model\Blog_User' ),
array( 'http://example.org/@blog/', 'Activitypub\Model\Blog_User' ), array( 'http://example.org/@blog/', 'Activitypub\Model\Blog_User' ),
array( 'http://example.org/blog/@blog', 'Activitypub\Model\Blog_User' ),
array( 'http://example.org/blog/@blog/', 'Activitypub\Model\Blog_User' ),
array( 'http://example.org/error/@blog', 'WP_Error' ),
array( 'http://example.org/error/@blog/', 'WP_Error' ),
array( 'http://example.org/', 'Activitypub\Model\Blog_User' ), array( 'http://example.org/', 'Activitypub\Model\Blog_User' ),
array( 'http://example.org', 'Activitypub\Model\Blog_User' ), array( 'http://example.org', 'Activitypub\Model\Blog_User' ),
array( 'https://example.org/', 'Activitypub\Model\Blog_User' ), array( 'https://example.org/', 'Activitypub\Model\Blog_User' ),