| 1 |
<?php |
| 2 |
/** |
| 3 |
* Nodeinfo Class |
| 4 |
* |
| 5 |
* @package Nodeinfo |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Nodeinfo; |
| 9 |
|
| 10 |
use Nodeinfo\Controller\Nodeinfo as Controller_Nodeinfo; |
| 11 |
use Nodeinfo\Controller\Nodeinfo2 as Controller_Nodeinfo2; |
| 12 |
use Nodeinfo\Integration\Nodeinfo10; |
| 13 |
use Nodeinfo\Integration\Nodeinfo11; |
| 14 |
use Nodeinfo\Integration\Nodeinfo20; |
| 15 |
use Nodeinfo\Integration\Nodeinfo21; |
| 16 |
use Nodeinfo\Integration\Nodeinfo22; |
| 17 |
|
| 18 |
/** |
| 19 |
* Nodeinfo Class |
| 20 |
* |
| 21 |
* @package Nodeinfo |
| 22 |
*/ |
| 23 |
class Nodeinfo { |
| 24 |
/** |
| 25 |
* Instance of the class. |
| 26 |
* |
| 27 |
* @var Nodeinfo |
| 28 |
*/ |
| 29 |
private static $instance; |
| 30 |
|
| 31 |
/** |
| 32 |
* Whether the class has been initialized. |
| 33 |
* |
| 34 |
* @var boolean |
| 35 |
*/ |
| 36 |
private $initialized = false; |
| 37 |
|
| 38 |
/** |
| 39 |
* Get the instance of the class. |
| 40 |
* |
| 41 |
* @return Nodeinfo |
| 42 |
*/ |
| 43 |
public static function get_instance() { |
| 44 |
if ( null === self::$instance ) { |
| 45 |
self::$instance = new self(); |
| 46 |
} |
| 47 |
|
| 48 |
return self::$instance; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Do not allow multiple instances of the class. |
| 53 |
*/ |
| 54 |
private function __construct() { |
| 55 |
// Do nothing. |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Initialize the plugin. |
| 60 |
*/ |
| 61 |
public function init() { |
| 62 |
if ( $this->initialized ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$this->register_integrations(); |
| 67 |
$this->register_hooks(); |
| 68 |
|
| 69 |
if ( \is_admin() ) { |
| 70 |
$this->register_admin_hooks(); |
| 71 |
} |
| 72 |
|
| 73 |
$this->initialized = true; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Register NodeInfo version integrations. |
| 78 |
* |
| 79 |
* These only register filters, so they can be called directly. |
| 80 |
*/ |
| 81 |
public function register_integrations() { |
| 82 |
Nodeinfo10::init(); |
| 83 |
Nodeinfo11::init(); |
| 84 |
Nodeinfo20::init(); |
| 85 |
Nodeinfo21::init(); |
| 86 |
Nodeinfo22::init(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Register hooks. |
| 91 |
*/ |
| 92 |
public function register_hooks() { |
| 93 |
// Register REST routes. |
| 94 |
\add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 95 |
|
| 96 |
// Add WebFinger and Host-Meta discovery. |
| 97 |
\add_filter( 'webfinger_user_data', array( Controller_Nodeinfo::class, 'jrd' ), 10, 3 ); |
| 98 |
\add_filter( 'webfinger_post_data', array( Controller_Nodeinfo::class, 'jrd' ), 10, 3 ); |
| 99 |
\add_filter( 'host_meta', array( Controller_Nodeinfo::class, 'jrd' ) ); |
| 100 |
|
| 101 |
// Add rewrite rules for well-known endpoints (only during flush). |
| 102 |
\add_filter( 'rewrite_rules_array', array( $this, 'add_rewrite_rules' ) ); |
| 103 |
|
| 104 |
// Register deprecated filter handlers. |
| 105 |
\add_filter( 'nodeinfo_discovery', array( $this, 'deprecated_wellknown_nodeinfo_data' ), 99 ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Handles the deprecated wellknown_nodeinfo_data filter. |
| 110 |
* |
| 111 |
* @param array $discovery The discovery document. |
| 112 |
* @return array The filtered discovery document. |
| 113 |
*/ |
| 114 |
public function deprecated_wellknown_nodeinfo_data( $discovery ) { |
| 115 |
/** |
| 116 |
* Filters the NodeInfo discovery document. |
| 117 |
* |
| 118 |
* @deprecated 3.0.0 Use nodeinfo_discovery instead. |
| 119 |
* |
| 120 |
* @param array $discovery The discovery document. |
| 121 |
*/ |
| 122 |
return \apply_filters_deprecated( |
| 123 |
'wellknown_nodeinfo_data', |
| 124 |
array( $discovery ), |
| 125 |
'3.0.0', |
| 126 |
'nodeinfo_discovery' |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Register admin hooks. |
| 132 |
*/ |
| 133 |
public function register_admin_hooks() { |
| 134 |
// Initialize Site Health checks. |
| 135 |
\add_action( 'admin_init', array( Health_Check::class, 'init' ) ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Register REST API routes. |
| 140 |
*/ |
| 141 |
public function register_routes() { |
| 142 |
$nodeinfo_controller = new Controller_Nodeinfo(); |
| 143 |
$nodeinfo_controller->register_routes(); |
| 144 |
|
| 145 |
$nodeinfo2_controller = new Controller_Nodeinfo2(); |
| 146 |
$nodeinfo2_controller->register_routes(); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Add rewrite rules for well-known endpoints. |
| 151 |
* |
| 152 |
* @param array $rules The existing rewrite rules. |
| 153 |
* @return array The modified rewrite rules. |
| 154 |
*/ |
| 155 |
public function add_rewrite_rules( $rules ) { |
| 156 |
$new_rules = array( |
| 157 |
'^.well-known/nodeinfo' => 'index.php?rest_route=/nodeinfo/discovery', |
| 158 |
'^.well-known/x-nodeinfo2' => 'index.php?rest_route=/nodeinfo2/1.0', |
| 159 |
); |
| 160 |
|
| 161 |
return \array_merge( $new_rules, $rules ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Handle plugin activation. |
| 166 |
* |
| 167 |
* Initializes the plugin and flushes rewrite rules. The rewrite_rules_array |
| 168 |
* filter will add our rules during the flush. |
| 169 |
*/ |
| 170 |
public static function activate() { |
| 171 |
self::get_instance()->init(); |
| 172 |
\flush_rewrite_rules(); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Handle plugin deactivation. |
| 177 |
* |
| 178 |
* Should be called on plugin deactivation. |
| 179 |
*/ |
| 180 |
public static function deactivate() { |
| 181 |
\flush_rewrite_rules(); |
| 182 |
} |
| 183 |
} |
| 184 |
|