settings = $settings; } /** * Registers hooks. * * @return void */ public function register(): void { if ( aiblackout_is_disabled() || ! $this->is_enabled() ) { return; } if ( ! $this->core_has_connectors() ) { return; } // Late, so the items exist to be removed. add_action( 'admin_menu', array( $this, 'remove_connectors_menu' ), PHP_INT_MAX ); add_action( 'load-' . self::CONNECTORS_PAGE, array( $this, 'block_connectors_page' ) ); add_action( 'admin_init', array( $this, 'maybe_block_connectors_slug' ) ); } /** * Whether this module is switched on. * * @return bool True when enabled. */ public function is_enabled(): bool { return $this->settings->is_blackout_enabled( Settings::BLACKOUT_CONNECTORS_UI ); } /** * Whether this WordPress ships the Connectors screen. * * @return bool True when the screen exists. */ public function core_has_connectors(): bool { return function_exists( 'wp_get_connectors' ); } /** * Removes the Connectors item from the Settings menu. * * @return void */ public function remove_connectors_menu(): void { remove_submenu_page( self::CONNECTORS_PARENT, self::CONNECTORS_PAGE ); $this->remove_menu_entries( self::CONNECTORS_PAGE_SLUG ); } /** * Drops every menu and submenu entry pointing at a slug. * * @param string $slug Page slug to remove. * @return void */ private function remove_menu_entries( string $slug ): void { if ( isset( $GLOBALS['menu'] ) && is_array( $GLOBALS['menu'] ) ) { foreach ( $GLOBALS['menu'] as $position => $item ) { if ( isset( $item[2] ) && $slug === $item[2] ) { unset( $GLOBALS['menu'][ $position ] ); } } } if ( ! isset( $GLOBALS['submenu'] ) || ! is_array( $GLOBALS['submenu'] ) ) { return; } foreach ( $GLOBALS['submenu'] as $parent => $items ) { foreach ( (array) $items as $position => $item ) { if ( isset( $item[2] ) && $slug === $item[2] ) { unset( $GLOBALS['submenu'][ $parent ][ $position ] ); } } if ( empty( $GLOBALS['submenu'][ $parent ] ) ) { unset( $GLOBALS['submenu'][ $parent ] ); } } } /** * Refuses the 7.1 route to the Connectors screen. * * @return void */ public function maybe_block_connectors_slug(): void { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading which admin screen was asked for; nothing is acted on. if ( ! isset( $_GET['page'] ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- As above. if ( self::CONNECTORS_PAGE_SLUG !== sanitize_key( wp_unslash( $_GET['page'] ) ) ) { return; } $this->block_connectors_page(); } /** * Refuses a direct request for the Connectors screen. * * @return void */ public function block_connectors_page(): void { wp_die( esc_html__( 'The AI Connectors screen is switched off by AI Blackout. This is where WordPress stores API keys for AI providers.', 'ai-blackout' ), esc_html__( 'AI Connectors disabled', 'ai-blackout' ), array( 'response' => 403, 'back_link' => true, 'link_url' => esc_url( admin_url( 'admin.php?page=' . BlackoutScreen::SLUG ) ), 'link_text' => esc_html__( 'Change this on the AI inside WordPress screen', 'ai-blackout' ), ) ); } }