Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
71 changes: 61 additions & 10 deletions includes/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,73 @@
/**
* Deprecated classes for backwards compatibility.
*
* Version 4.0.0 moved all classes into the `Webfinger` namespace. The
* pre-4.0.0 global class names are kept working here, so third-party
* checks like `class_exists( 'Webfinger' )` (e.g. in the ActivityPub
* plugin) and calls to the old static methods keep working.
*
* @package Webfinger
*/

// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound, Squiz.Commenting.ClassComment.Missing
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound

/**
* Legacy Webfinger class (deprecated).
/*
* The aliases are registered lazily, so a deprecation notice is triggered
* the moment a legacy class name is actually referenced.
*/
class Webfinger {}
\spl_autoload_register(
function ( $class_name ) {
$deprecated = array(
'Webfinger_Admin' => \Webfinger\Admin::class,
'Webfinger_Legacy' => \Webfinger\Legacy::class,
);
Comment thread
pfefferle marked this conversation as resolved.

/**
* Legacy Webfinger_Admin class (deprecated).
*/
class Webfinger_Admin {}
if ( ! isset( $deprecated[ $class_name ] ) ) {
return;
}

\_deprecated_class( $class_name, '4.0.0', $deprecated[ $class_name ] );

\class_alias( $deprecated[ $class_name ], $class_name );
}
);

/**
* Legacy Webfinger_Legacy class (deprecated).
* Legacy Webfinger class (deprecated).
*
* A subclass instead of a plain alias, because two static methods moved
* to `Webfinger\User` and would otherwise be lost.
*/
class Webfinger_Legacy {}
class Webfinger extends \Webfinger\Webfinger {

/**
* Returns a users default WebFinger resource.
*
* @deprecated 4.0.0 Use `Webfinger\User::get_resource()` instead.
*
* @param mixed $id_or_name_or_object User ID, login name or object.
* @param boolean $with_protocol Whether to prepend the `acct:` scheme.
*
* @return string|null The users default WebFinger resource.
*/
Comment thread
pfefferle marked this conversation as resolved.
public static function get_user_resource( $id_or_name_or_object, $with_protocol = true ) {
\_deprecated_function( __METHOD__, '4.0.0', '\Webfinger\User::get_resource()' );

return \Webfinger\User::get_resource( $id_or_name_or_object, $with_protocol );
}

/**
* Returns all WebFinger resources of a user.
*
* @deprecated 4.0.0 Use `Webfinger\User::get_resources()` instead.
*
* @param mixed $id_or_name_or_object User ID, login name or object.
*
* @return string[] The users WebFinger resources.
*/
Comment thread
pfefferle marked this conversation as resolved.
public static function get_user_resources( $id_or_name_or_object ) {
\_deprecated_function( __METHOD__, '4.0.0', '\Webfinger\User::get_resources()' );

return \Webfinger\User::get_resources( $id_or_name_or_object );
}
}
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- Contributors: pfefferle, willnorris
- Donate link: https://notiz.blog/donate/
- Tags: discovery, webfinger, JRD, ostatus, activitypub
- Requires at least: 4.2
- Requires at least: 6.4
- Tested up to: 7.0
- Stable tag: 4.0.1
- Requires PHP: 7.4
Expand Down Expand Up @@ -92,6 +92,12 @@ This version drops classic WebFinger (XRD) support to keep the plugin lightweigh

Project maintained on github at [pfefferle/wordpress-webfinger](https://github.com/pfefferle/wordpress-webfinger).

### 4.0.2

* Fixed: The deprecated global classes are now real aliases of their namespaced replacements instead of empty stubs, so `class_exists()` checks and calls to the old static methods keep working
* Added: Deprecation notices when the legacy class names or the moved `Webfinger::get_user_resource()`/`get_user_resources()` methods are used
* Changed: Raise the minimum required WordPress version to 6.4
Comment thread
pfefferle marked this conversation as resolved.

### 4.0.1

* Fixed: Handle WP_Error objects in `filter_by_rel` to prevent errors when WebFinger lookup fails
Expand Down
104 changes: 104 additions & 0 deletions tests/phpunit/includes/Test_Deprecated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Tests for the deprecated pre-4.0.0 global class names.
*
* @package Webfinger
*/

/**
* Test class for the backward-compatibility layer in includes/deprecated.php.
*/
class Test_Deprecated extends \WP_UnitTestCase {

/**
* Test user ID.
*
* @var int
*/
protected static $user_id;

/**
* Create a test user.
*
* @param WP_UnitTest_Factory $factory Factory instance.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$user_id = $factory->user->create(
array(
'user_login' => 'legacyuser',
)
);
}

/**
* Delete the test user.
*/
public static function wpTearDownAfterClass() {
self::delete_user( self::$user_id );
}
Comment thread
pfefferle marked this conversation as resolved.

/**
* Webfinger_Admin must be a real alias of Webfinger\Admin, not an empty stub,
* and referencing it must trigger a deprecation notice.
*
* @expectedDeprecated Webfinger_Admin
*/
public function test_webfinger_admin_is_alias_of_replacement() {
$this->assertTrue( class_exists( 'Webfinger_Admin' ) );

$reflection = new ReflectionClass( 'Webfinger_Admin' );
$this->assertSame( \Webfinger\Admin::class, $reflection->getName() );
}

/**
* Webfinger_Legacy must be a real alias of Webfinger\Legacy, not an empty stub,
* and referencing it must trigger a deprecation notice.
*
* @expectedDeprecated Webfinger_Legacy
*/
public function test_webfinger_legacy_is_alias_of_replacement() {
$this->assertTrue( class_exists( 'Webfinger_Legacy' ) );

$reflection = new ReflectionClass( 'Webfinger_Legacy' );
$this->assertSame( \Webfinger\Legacy::class, $reflection->getName() );
}

/**
* The global Webfinger class must provide the full pre-4.0.0 static API.
*
* The ActivityPub plugin gates its own WebFinger handling on
* `class_exists( 'Webfinger' )`, and third parties called the static
* methods directly.
*/
public function test_webfinger_class_is_replacement() {
$this->assertTrue( class_exists( 'Webfinger' ) );
$this->assertTrue( is_a( 'Webfinger', \Webfinger\Webfinger::class, true ) );
$this->assertTrue( is_callable( array( 'Webfinger', 'render_jrd' ) ) );
$this->assertTrue( is_callable( array( 'Webfinger', 'get_user_resource' ) ) );
$this->assertTrue( is_callable( array( 'Webfinger', 'get_user_resources' ) ) );
}

/**
* Webfinger::get_user_resource() moved to Webfinger\User::get_resource().
*
* @expectedDeprecated Webfinger::get_user_resource
*/
public function test_get_user_resource_wrapper() {
$this->assertSame(
\Webfinger\User::get_resource( self::$user_id ),
\Webfinger::get_user_resource( self::$user_id )
);
}

/**
* Webfinger::get_user_resources() moved to Webfinger\User::get_resources().
*
* @expectedDeprecated Webfinger::get_user_resources
*/
public function test_get_user_resources_wrapper() {
$this->assertSame(
\Webfinger\User::get_resources( self::$user_id ),
\Webfinger::get_user_resources( self::$user_id )
);
}
}
1 change: 1 addition & 0 deletions webfinger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Plugin URI: https://github.com/pfefferle/wordpress-webfinger
* Description: WebFinger for WordPress
* Version: 4.0.1
* Requires at least: 6.4
* Author: Matthias Pfefferle
* Author URI: https://notiz.blog/
* License: MIT
Expand Down