version; } /** * Absolute filesystem path to a file inside the plugin. * * @param string $relative Path relative to the plugin root. */ public function path( string $relative = '' ): string { return plugin_dir_path( $this->file ) . ltrim( $relative, '/' ); } /** * URL of a file inside the plugin. * * @param string $relative Path relative to the plugin root. */ public function url( string $relative = '' ): string { return plugin_dir_url( $this->file ) . ltrim( $relative, '/' ); } /** * Registers hooks. Nothing here makes a network request or reads the database; that happens * only when a hook fires. */ public function register(): void { $urls = new Url_Validator(); $dashboards = new Dashboard_Repository( $urls ); $cache = new Feed_Cache(); $refresher = new Feed_Refresher( $dashboards, new Feed_Fetcher( $urls, new Feed_Normalizer( $urls ), $this->version ), $cache, new Feed_Lock() ); $scheduler = new Feed_Scheduler(); ( new Dashboard_Post_Type() )->register(); if ( is_admin() ) { ( new Dashboard_Screen( $dashboards, $cache ) )->register(); ( new Actions( $dashboards, $refresher ) )->register(); ( new Notices() )->register(); } ( new Public_Controller( $dashboards, new Payload_Builder( $cache ) ) )->register(); ( new Display_Route( $dashboards ) )->register(); // A closure, so arguments WordPress passes to the hook (do_action() adds an empty string) never reach run(). $scheduler->register( static function () use ( $refresher ): void { $refresher->run(); } ); // Load a published dashboard's feeds soon after it is saved, through cron rather than during the request. add_action( 'pulsecast_dashboard_saved', static function ( int $post_id ) use ( $scheduler, $dashboards, $cache ): void { $dashboard = $dashboards->find( $post_id ); if ( null === $dashboard || ! $dashboard->enabled ) { return; } // Only ask for a refresh when a feed has never been loaded, so editing an existing // dashboard does not queue needless jobs. foreach ( $dashboard->feeds as $feed ) { if ( null === $cache->get( $feed['url'] ) ) { $scheduler->request_refresh(); return; } } } ); } }