Skip to content

Commit 35c3b18

Browse files
authored
Merge pull request #16 from pfefferle/refactor/filter-based-architecture
Add singleton-based plugin loading mechanism
2 parents bc227e1 + b52bf7f commit 35c3b18

5 files changed

Lines changed: 397 additions & 107 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- Tags: nodeinfo, fediverse, ostatus, diaspora, activitypub
66
- Requires at least: 6.6
77
- Tested up to: 6.9
8-
- Stable tag: 3.0.0
8+
- Stable tag: 3.1.0
99
- Requires PHP: 7.2
1010
- License: MIT
1111
- License URI: https://opensource.org/licenses/MIT
@@ -81,6 +81,11 @@ If either check fails, you'll see recommendations on how to fix the issue.
8181

8282
Project and support maintained on github at [pfefferle/wordpress-nodeinfo](https://github.com/pfefferle/wordpress-nodeinfo).
8383

84+
### 3.1.0
85+
86+
* Added singleton-based plugin loading mechanism for better extensibility
87+
* Added backwards compatibility handler for deprecated `wellknown_nodeinfo_data` filter
88+
8489
### 3.0.0
8590

8691
* Refactored to filter-based architecture for better extensibility

includes/class-nodeinfo.php

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
}

languages/nodeinfo.pot

Lines changed: 0 additions & 45 deletions
This file was deleted.

nodeinfo.php

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
* Plugin Name: NodeInfo
44
* Plugin URI: https://github.com/pfefferle/wordpress-nodeinfo/
55
* Description: NodeInfo is an effort to create a standardized way of exposing metadata about a server running one of the distributed social networks.
6-
* Version: 3.0.0
6+
* Version: 3.1.0
77
* Author: Matthias Pfefferle
88
* Author URI: https://notiz.blog/
99
* License: MIT
1010
* License URI: http://opensource.org/licenses/MIT
1111
* Text Domain: nodeinfo
12-
* Domain Path: /languages
1312
*
1413
* @package Nodeinfo
1514
*/
@@ -32,65 +31,22 @@
3231
require_once NODEINFO_PLUGIN_DIR . 'includes/class-nodeinfo-endpoint.php';
3332

3433
/**
35-
* Initialize the plugin.
36-
*/
37-
function nodeinfo_init() {
38-
// Initialize NodeInfo version integrations.
39-
Nodeinfo\Integration\Nodeinfo10::init();
40-
Nodeinfo\Integration\Nodeinfo11::init();
41-
Nodeinfo\Integration\Nodeinfo20::init();
42-
Nodeinfo\Integration\Nodeinfo21::init();
43-
Nodeinfo\Integration\Nodeinfo22::init();
44-
45-
// Register REST routes.
46-
add_action( 'rest_api_init', 'nodeinfo_register_routes' );
47-
48-
// Add WebFinger and Host-Meta discovery.
49-
add_filter( 'webfinger_user_data', array( Nodeinfo\Controller\Nodeinfo::class, 'jrd' ), 10, 3 );
50-
add_filter( 'webfinger_post_data', array( Nodeinfo\Controller\Nodeinfo::class, 'jrd' ), 10, 3 );
51-
add_filter( 'host_meta', array( Nodeinfo\Controller\Nodeinfo::class, 'jrd' ) );
52-
}
53-
add_action( 'init', 'nodeinfo_init', 9 );
54-
55-
/**
56-
* Initialize admin-only features.
57-
*/
58-
function nodeinfo_admin_init() {
59-
// Initialize Site Health checks.
60-
Nodeinfo\Health_Check::init();
61-
}
62-
add_action( 'admin_init', 'nodeinfo_admin_init' );
63-
64-
/**
65-
* Register REST API routes.
66-
*/
67-
function nodeinfo_register_routes() {
68-
$nodeinfo_controller = new Nodeinfo\Controller\Nodeinfo();
69-
$nodeinfo_controller->register_routes();
70-
71-
$nodeinfo2_controller = new Nodeinfo\Controller\Nodeinfo2();
72-
$nodeinfo2_controller->register_routes();
73-
}
74-
75-
/**
76-
* Add rewrite rules for well-known endpoints.
77-
*/
78-
function nodeinfo_add_rewrite_rules() {
79-
add_rewrite_rule( '^.well-known/nodeinfo', 'index.php?rest_route=/nodeinfo/discovery', 'top' );
80-
add_rewrite_rule( '^.well-known/x-nodeinfo2', 'index.php?rest_route=/nodeinfo2/1.0', 'top' );
81-
}
82-
add_action( 'init', 'nodeinfo_add_rewrite_rules', 1 );
83-
84-
/**
85-
* Flush rewrite rules on activation.
34+
* Plugin initialization function.
35+
*
36+
* @return Nodeinfo\Nodeinfo The plugin instance.
8637
*/
87-
function nodeinfo_activate() {
88-
nodeinfo_add_rewrite_rules();
89-
flush_rewrite_rules();
38+
function nodeinfo_plugin() {
39+
return Nodeinfo\Nodeinfo::get_instance();
9040
}
91-
register_activation_hook( __FILE__, 'nodeinfo_activate' );
9241

93-
/**
94-
* Flush rewrite rules on deactivation.
95-
*/
96-
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
42+
// Initialize the plugin after all plugins are loaded.
43+
add_action(
44+
'plugins_loaded',
45+
function () {
46+
nodeinfo_plugin()->init();
47+
}
48+
);
49+
50+
// Register activation and deactivation hooks.
51+
register_activation_hook( __FILE__, array( Nodeinfo\Nodeinfo::class, 'activate' ) );
52+
register_deactivation_hook( __FILE__, array( Nodeinfo\Nodeinfo::class, 'deactivate' ) );

0 commit comments

Comments
 (0)