hooks = $hooks; $this->page = $page; $this->widget = $widget; $this->repository = $repository; } /** * Queues the service's WordPress hooks via the hook loader. * * @return void */ public function register(): void { $this->register_invalidation(); if ( ! is_admin() ) { return; } $this->hooks->add_action( 'admin_menu', array( $this->page, 'register_menu' ) ); $this->hooks->add_filter( 'plugin_action_links_' . BLOCKSIEVE_BASENAME, array( $this->page, 'add_action_links' ) ); $this->hooks->add_action( 'wp_dashboard_setup', array( $this->widget, 'register' ) ); $this->hooks->add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); } /** * Queues the cache-invalidation hooks. * * Registered outside the admin too, because content is routinely saved over * the REST API (the block editor) and WP-CLI. * * @return void */ private function register_invalidation(): void { $this->hooks->add_action( 'save_post', array( $this->repository, 'on_post_change' ), 10, 2 ); $this->hooks->add_action( 'deleted_post', array( $this->repository, 'on_post_change' ), 10, 2 ); $this->hooks->add_action( 'add_option_widget_block', array( $this->repository, 'invalidate' ) ); $this->hooks->add_action( 'update_option_widget_block', array( $this->repository, 'invalidate' ) ); // Moving a widget between sidebars (or to Inactive) only touches this option. $this->hooks->add_action( 'update_option_sidebars_widgets', array( $this->repository, 'invalidate' ) ); $this->hooks->add_action( 'switch_theme', array( $this->repository, 'invalidate' ) ); // The scan limit and post types shape the report, so any settings write stales it. foreach ( array( 'add_option_', 'update_option_', 'delete_option_' ) as $hook ) { $this->hooks->add_action( $hook . Settings::OPTION, array( $this->repository, 'invalidate' ) ); } } /** * Enqueues the admin stylesheet on the screens that use it. * * @param string $hook_suffix Current admin page hook suffix. * @return void */ public function enqueue_styles( $hook_suffix ): void { $screens = array( 'index.php', 'tools_page_' . ReportPage::SLUG ); if ( ! in_array( (string) $hook_suffix, $screens, true ) ) { return; } $relative = 'assets/css/admin.css'; if ( ! is_readable( BLOCKSIEVE_PATH . $relative ) ) { return; } wp_enqueue_style( 'blocksieve-admin', BLOCKSIEVE_URL . $relative, array(), BLOCKSIEVE_VERSION ); } }