|
| 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 | + * Text domain. |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + const TEXT_DOMAIN = 'nodeinfo'; |
| 37 | + |
| 38 | + /** |
| 39 | + * Whether the class has been initialized. |
| 40 | + * |
| 41 | + * @var boolean |
| 42 | + */ |
| 43 | + private $initialized = false; |
| 44 | + |
| 45 | + /** |
| 46 | + * Get the instance of the class. |
| 47 | + * |
| 48 | + * @return Nodeinfo |
| 49 | + */ |
| 50 | + public static function get_instance() { |
| 51 | + if ( null === self::$instance ) { |
| 52 | + self::$instance = new self(); |
| 53 | + } |
| 54 | + |
| 55 | + return self::$instance; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Do not allow multiple instances of the class. |
| 60 | + */ |
| 61 | + private function __construct() { |
| 62 | + // Do nothing. |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Initialize the plugin. |
| 67 | + */ |
| 68 | + public function init() { |
| 69 | + if ( $this->initialized ) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + $this->register_hooks(); |
| 74 | + $this->register_admin_hooks(); |
| 75 | + |
| 76 | + // Load language files. |
| 77 | + \load_plugin_textdomain( |
| 78 | + self::TEXT_DOMAIN, |
| 79 | + false, |
| 80 | + \dirname( \plugin_basename( NODEINFO_PLUGIN_FILE ) ) . '/languages' |
| 81 | + ); |
| 82 | + |
| 83 | + $this->initialized = true; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Register hooks. |
| 88 | + */ |
| 89 | + public function register_hooks() { |
| 90 | + // Initialize NodeInfo version integrations. |
| 91 | + \add_action( 'init', array( Nodeinfo10::class, 'init' ) ); |
| 92 | + \add_action( 'init', array( Nodeinfo11::class, 'init' ) ); |
| 93 | + \add_action( 'init', array( Nodeinfo20::class, 'init' ) ); |
| 94 | + \add_action( 'init', array( Nodeinfo21::class, 'init' ) ); |
| 95 | + \add_action( 'init', array( Nodeinfo22::class, 'init' ) ); |
| 96 | + |
| 97 | + // Register REST routes. |
| 98 | + \add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 99 | + |
| 100 | + // Add WebFinger and Host-Meta discovery. |
| 101 | + \add_filter( 'webfinger_user_data', array( Controller_Nodeinfo::class, 'jrd' ), 10, 3 ); |
| 102 | + \add_filter( 'webfinger_post_data', array( Controller_Nodeinfo::class, 'jrd' ), 10, 3 ); |
| 103 | + \add_filter( 'host_meta', array( Controller_Nodeinfo::class, 'jrd' ) ); |
| 104 | + |
| 105 | + // Add rewrite rules for well-known endpoints. |
| 106 | + \add_action( 'init', array( $this, 'add_rewrite_rules' ), 1 ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Register admin hooks. |
| 111 | + */ |
| 112 | + public function register_admin_hooks() { |
| 113 | + // Initialize Site Health checks. |
| 114 | + \add_action( 'admin_init', array( Health_Check::class, 'init' ) ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Register REST API routes. |
| 119 | + */ |
| 120 | + public function register_routes() { |
| 121 | + $nodeinfo_controller = new Controller_Nodeinfo(); |
| 122 | + $nodeinfo_controller->register_routes(); |
| 123 | + |
| 124 | + $nodeinfo2_controller = new Controller_Nodeinfo2(); |
| 125 | + $nodeinfo2_controller->register_routes(); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Add rewrite rules for well-known endpoints. |
| 130 | + */ |
| 131 | + public function add_rewrite_rules() { |
| 132 | + \add_rewrite_rule( '^.well-known/nodeinfo', 'index.php?rest_route=/nodeinfo/discovery', 'top' ); |
| 133 | + \add_rewrite_rule( '^.well-known/x-nodeinfo2', 'index.php?rest_route=/nodeinfo2/1.0', 'top' ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Flush rewrite rules. |
| 138 | + * |
| 139 | + * Should be called on plugin activation. |
| 140 | + */ |
| 141 | + public static function activate() { |
| 142 | + self::get_instance()->add_rewrite_rules(); |
| 143 | + \flush_rewrite_rules(); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Deactivate the plugin. |
| 148 | + * |
| 149 | + * Should be called on plugin deactivation. |
| 150 | + */ |
| 151 | + public static function deactivate() { |
| 152 | + \flush_rewrite_rules(); |
| 153 | + } |
| 154 | +} |
0 commit comments