PluginProbe
NodeInfo(2) / trunk
NodeInfo(2) vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 3.0.0 3.1.0
← All changes | includes/class-nodeinfo.php +150 -88 2.1.1trunk View file →
@@ -1,121 +1,183 @@
1 1 <?php
2 2 /**
3 - * Nodeinfo class
3 + * Nodeinfo Class
4 4 *
5 - * @link https://github.com/jhass/nodeinfo
5 + * @package Nodeinfo
6 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 + */
7 23 class Nodeinfo {
8 - public $version = '2.0';
9 - public $software = array();
10 - public $usage = array();
11 - public $openRegistrations = false; // phpcs:ignore
12 - public $services = array(
13 - 'inbound' => array(),
14 - 'outbound' => array(),
15 - );
16 - public $protocols = array();
17 - public $metadata = array();
24 + /**
25 + * Instance of the class.
26 + *
27 + * @var Nodeinfo
28 + */
29 + private static $instance;
18 30
19 - public function __construct( $version = '2.0' ) {
20 - if ( in_array( $version, array( '1.0', '1.1', '2.0', '2.1' ), true ) ) {
21 - $this->version = $version;
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();
22 46 }
23 47
24 - $this->generate_software();
25 - $this->generate_usage();
26 - $this->generate_protocols();
27 - $this->generate_services();
28 - $this->generate_metadata();
29 - $this->openRegistrations = (boolean) get_option( 'users_can_register', false ); // phpcs:ignore
48 + return self::$instance;
30 49 }
31 50
32 - public function generate_usage() {
33 - $users = get_users(
34 - array(
35 - 'capability__in' => array( 'publish_posts' ),
36 - )
37 - );
51 + /**
52 + * Do not allow multiple instances of the class.
53 + */
54 + private function __construct() {
55 + // Do nothing.
56 + }
38 57
39 - if ( is_array( $users ) ) {
40 - $users = count( $users );
41 - } else {
42 - $users = 1;
58 + /**
59 + * Initialize the plugin.
60 + */
61 + public function init() {
62 + if ( $this->initialized ) {
63 + return;
43 64 }
44 65
45 - $posts = wp_count_posts();
46 - $comments = wp_count_comments();
66 + $this->register_integrations();
67 + $this->register_hooks();
47 68
48 - $this->usage = apply_filters(
49 - 'nodeinfo_data_usage',
50 - array(
51 - 'users' => array(
52 - 'total' => $users,
53 - ),
54 - 'localPosts' => (int) $posts->publish,
55 - 'localComments' => (int) $comments->approved,
56 - ),
57 - $this->version
58 - );
69 + if ( \is_admin() ) {
70 + $this->register_admin_hooks();
71 + }
72 +
73 + $this->initialized = true;
59 74 }
60 75
61 - public function generate_software() {
62 - $software = array(
63 - 'name' => 'wordpress',
64 - 'version' => get_bloginfo( 'version' ),
65 - );
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 + }
66 88
67 - if ( '2.1' === $this->version ) {
68 - $software['repository'] = 'https://github.com/wordpress/wordpress';
69 - }
89 + /**
90 + * Register hooks.
91 + */
92 + public function register_hooks() {
93 + // Register REST routes.
94 + \add_action( 'rest_api_init', array( $this, 'register_routes' ) );
70 95
71 - $this->software = apply_filters(
72 - 'nodeinfo_data_software',
73 - $software,
74 - $this->version
75 - );
76 - }
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' ) );
77 100
78 - public function generate_protocols() {
79 - $protocols = $this->protocols;
101 + // Add rewrite rules for well-known endpoints (only during flush).
102 + \add_filter( 'rewrite_rules_array', array( $this, 'add_rewrite_rules' ) );
80 103
81 - if ( version_compare( $this->version, '2.0', '>=' ) ) {
82 - $protocols = array();
83 - } else {
84 - $protocols['inbound'] = array( 'smtp' );
85 - $protocols['outbound'] = array( 'smtp' );
86 - }
104 + // Register deprecated filter handlers.
105 + \add_filter( 'nodeinfo_discovery', array( $this, 'deprecated_wellknown_nodeinfo_data' ), 99 );
106 + }
87 107
88 - $this->protocols = apply_filters( 'nodeinfo_data_protocols', $protocols, $this->version );
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 + );
89 128 }
90 129
91 - public function generate_services() {
92 - $services = $this->services;
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 + }
93 137
94 - if ( version_compare( $this->version, '2.0', '>=' ) ) {
95 - $services['inbound'] = array( 'atom1.0', 'rss2.0', 'pop3' );
96 - $services['outbound'] = array( 'atom1.0', 'rss2.0', 'wordpress', 'smtp' );
97 - } else {
98 - $services['outbound'] = array( 'smtp' );
99 - }
138 + /**
139 + * Register REST API routes.
140 + */
141 + public function register_routes() {
142 + $nodeinfo_controller = new Controller_Nodeinfo();
143 + $nodeinfo_controller->register_routes();
100 144
101 - $this->services = apply_filters( 'nodeinfo_data_services', $services, $this->version );
145 + $nodeinfo2_controller = new Controller_Nodeinfo2();
146 + $nodeinfo2_controller->register_routes();
102 147 }
103 148
104 - public function generate_metadata() {
105 - $metadata = $this->metadata;
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 + );
106 160
107 - $metadata['email'] = get_option( 'admin_email' );
161 + return \array_merge( $new_rules, $rules );
162 + }
108 163
109 - $metadata['generator'] = array(
110 - 'name' => 'NodeInfo WordPress-Plugin',
111 - 'version' => nodeinfo_version(),
112 - 'repository' => 'https://github.com/pfefferle/wordpress-nodeinfo/',
113 - );
114 -
115 - $this->metadata = apply_filters( 'nodeinfo_data_metadata', $metadata, $this->version );
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();
116 173 }
117 174
118 - public function to_array() {
119 - return apply_filters( 'nodeinfo_data', get_object_vars( $this ), $this->version );
175 + /**
176 + * Handle plugin deactivation.
177 + *
178 + * Should be called on plugin deactivation.
179 + */
180 + public static function deactivate() {
181 + \flush_rewrite_rules();
120 182 }
121 183 }