'
. ''
. '';
/**
* Constructor.
*
* @param SettingsStore $store Settings store.
* @param ModuleEnableMap $enableMap Module enable map.
* @param SitemapSettings|null $sitemap Sitemap settings store, fresh one when null.
*/
public function __construct(
private readonly SettingsStore $store,
private readonly ModuleEnableMap $enableMap,
private readonly ?SitemapSettings $sitemap = null
) {
// The X handle field belongs to the profile screens, not to the General
// Settings screen, so its hooks must be registered on every admin request.
// This controller is built eagerly, so the registration cannot be deferred
// into a lazily constructed page controller.
( new UserProfileField() )->register();
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueSharedStyles' ] );
// The sitemap screen owns a page scoped feature stylesheet, so its
// enqueue runs on the same hook and gates itself by hook suffix.
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueSitemapAssets' ] );
}
/**
* Enqueue sitemap page assets, gated by hook suffix.
*
* Performance optimization: checks the current screen hook suffix before
* instantiating the SitemapSettingsPage controller to avoid eager object creation
* on unrelated admin requests.
*
* @param string $hookSuffix Current admin page hook suffix.
*/
public function enqueueSitemapAssets( string $hookSuffix = '' ): void {
if ( SitemapSettingsPage::HOOK_SUFFIX !== $hookSuffix ) {
return;
}
$this->getSitemapPage()->enqueueAssets( $hookSuffix );
}
/**
* Is the current screen one of the RankKernel admin screens?
*
* The dashboard registers as a top level page and every submenu registers
* under admin.php, so the top level hook is `toplevel_page_rankkernel` and
* each submenu hook is `rankkernel_page_rankkernel-` followed by its slug.
* Metabox and column screens sit on post.php, edit.php and term.php and are
* deliberately not matched here, because those enqueue their own assets.
*
* @param string $hookSuffix Current admin page hook suffix.
*
* @return bool True when the screen belongs to RankKernel.
*/
public static function isRankKernelScreen( string $hookSuffix ): bool {
return 'toplevel_page_rankkernel' === $hookSuffix
|| str_starts_with( $hookSuffix, 'rankkernel_page_rankkernel' );
}
/**
* Enqueue the shared design layers on every RankKernel admin screen.
*
* The token layer defines the --rk-* custom properties and the UI layer
* defines the rk-ui-* components. Both are scoped under a RankKernel root
* class, so loading them on every RankKernel screen is inert on a screen
* that does not opt in, and it means a new screen cannot silently ship
* without them. Feature stylesheets keep registering their own handle and
* enqueue that from their own page class.
*
* @param string $hookSuffix Current admin page hook suffix.
*/
public function enqueueSharedStyles( string $hookSuffix = '' ): void {
if ( ! self::isRankKernelScreen( $hookSuffix ) ) {
return;
}
$pluginFile = defined( 'RANKKERNEL_FILE' ) ? (string) RANKKERNEL_FILE : '';
AdminStyles::enqueueTokenLayer( $pluginFile );
AdminStyles::enqueueUiLayer( $pluginFile );
AdminStyles::enqueueNojsLayer( $pluginFile );
AdminStyles::enqueueJsFlag();
}
/**
* Get the settings page (lazy instantiated).
*
* Performance optimization: defers page controller and repository construction
* until the page is explicitly accessed or rendered.
*
* @return SettingsPage The result.
*/
public function getPage(): SettingsPage {
return $this->page ??= new SettingsPage( $this->store, $this->enableMap );
}
/**
* Get the sitemap settings page (lazy instantiated).
*
* Performance optimization: defers page controller and repository construction
* until the page is explicitly accessed or rendered.
*
* @return SitemapSettingsPage The result.
*/
public function getSitemapPage(): SitemapSettingsPage {
return $this->sitemapPage ??= new SitemapSettingsPage( $this->sitemap ?? new SitemapSettings() );
}
/**
* Get the schema settings page (lazy instantiated).
*
* Performance optimization: defers page controller and repository construction
* until the page is explicitly accessed or rendered.
*
* @return SchemaSettingsPage The result.
*/
public function getSchemaPage(): SchemaSettingsPage {
return $this->schemaPage ??= new SchemaSettingsPage( $this->store );
}
/**
* Get the redirects page (lazy instantiated).
*
* Performance optimization: defers page controller and repository construction
* until the page is explicitly accessed or rendered.
*
* @return RedirectsPage The result.
*/
public function getRedirectsPage(): RedirectsPage {
return $this->redirectsPage ??= new RedirectsPage( new RedirectRepository(), new RedirectsSettings() );
}
/**
* Get the 404 Monitor page (lazy instantiated).
*
* Performance optimization: defers page controller and repository construction
* until the page is explicitly accessed or rendered.
*
* @return NotFoundPage The result.
*/
public function getMonitorPage(): NotFoundPage {
return $this->monitorPage ??= new NotFoundPage();
}
/**
* Get the Instant Indexing page (lazy instantiated).
*
* Performance optimization: defers page controller and repository construction
* until the page is explicitly accessed or rendered.
*
* @return InstantIndexingPage The result.
*/
public function getInstantIndexingPage(): InstantIndexingPage {
return $this->instantIndexingPage ??= new InstantIndexingPage( null, $this->enableMap );
}
/**
* Get the importer page (lazy instantiated).
*
* Performance optimization: defers page controller construction until
* the page is explicitly accessed or rendered.
*
* @return ImporterPage The result.
*/
public function getImporterPage(): ImporterPage {
return $this->importerPage ??= new ImporterPage( [ new YoastReader(), new RankMathReader(), new AioseoReader() ] );
}
/**
* Get the dashboard page (lazy instantiated).
*
* Performance optimization: defers page controller and repository construction
* until the page is explicitly accessed or rendered.
*
* @return DashboardPage The result.
*/
public function getDashboardPage(): DashboardPage {
return $this->dashboardPage ??= new DashboardPage();
}
/**
* Register hooks.
*/
public function register(): void {
add_filter(
'plugin_action_links_' . plugin_basename( RANKKERNEL_FILE ),
[ $this, 'addActionLinks' ]
);
add_action( 'admin_menu', [ $this, 'addMenuPage' ] );
}
/**
* Add Settings link to the plugin row (first position).
*
* @param string[] $links Existing links.
* @return string[] The result.
*/
public function addActionLinks( array $links ): array {
$url = admin_url( 'admin.php?page=rankkernel' );
$settings = '' . esc_html__( 'Settings', 'rankkernel' ) . '';
array_unshift( $links, $settings );
return $links;
}
/**
* Build the menu icon data URI from the inline SVG.
*/
private function menuIconUrl(): string {
return 'data:image/svg+xml;base64,' . base64_encode( self::MENU_ICON_SVG ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- encodes the inline menu icon SVG above into a data URI, no obfuscation involved.
}
/**
* Register the RankKernel top-level menu.
*/
public function addMenuPage(): void {
$hook = add_menu_page(
'RankKernel',
'RankKernel',
'manage_options',
DashboardPage::SLUG,
[ $this, 'renderDashboard' ],
$this->menuIconUrl(),
80
);
// Registered explicitly, because WordPress fills the first submenu slot
// from the parent menu title when nothing claims the parent slug. Without
// this the submenu would read RankKernel and repeat the top level label.
add_submenu_page(
'rankkernel',
esc_html__( 'Dashboard', 'rankkernel' ),
esc_html__( 'Dashboard', 'rankkernel' ),
'manage_options',
DashboardPage::SLUG,
[ $this, 'renderDashboard' ]
);
// Save handling runs on the load hook, before ANY output, so the
// post-redirect-get pattern can send its Location header.
add_action( 'load-' . $hook, [ $this, 'handleDashboardSave' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueDashboardAssets' ] );
if ( ! $this->enableMap->isEnabled( 'sitemaps' ) ) {
return;
}
$sitemapHook = add_submenu_page(
'rankkernel',
esc_html__( 'Sitemap Settings', 'rankkernel' ),
esc_html__( 'Sitemap', 'rankkernel' ),
'manage_options',
'rankkernel-sitemap',
[ $this, 'renderSitemap' ]
);
// Same load hook save pattern, so the tab redirect stays header safe.
add_action( 'load-' . $sitemapHook, [ $this, 'handleSitemapSave' ] );
}
/**
* Render dashboard page callback.
*/
public function renderDashboard(): void {
$this->getDashboardPage()->render();
}
/**
* Handle dashboard save callback.
*/
public function handleDashboardSave(): void {
$this->getDashboardPage()->maybeHandleSave();
}
/**
* Block a module-owned page when its module is switched OFF.
*
* Hiding the submenu is not enough: the URL stays reachable by direct
* entry, so every render and save callback must re-check the enable map
* and bounce to the dashboard when the module is off.
*
* @param string $moduleId Module id from ModuleRegistry.
* @return bool True when the module is disabled and the caller returned.
*/
private function blockWhenDisabled( string $moduleId ): bool {
if ( $this->enableMap->isEnabled( $moduleId ) ) {
return false;
}
wp_safe_redirect( admin_url( 'admin.php?page=' . DashboardPage::SLUG ) );
if ( ! defined( 'RANKKERNEL_TESTING' ) ) {
exit;
}
return true;
}
/**
* Enqueue dashboard page assets callback.
*
* @param string $hookSuffix Current admin page hook suffix.
*/
public function enqueueDashboardAssets( string $hookSuffix = '' ): void {
if ( DashboardPage::HOOK_SUFFIX !== $hookSuffix ) {
return;
}
$this->getDashboardPage()->enqueueAssets( $hookSuffix );
}
/**
* Render sitemap page callback.
*/
public function renderSitemap(): void {
if ( $this->blockWhenDisabled( 'sitemaps' ) ) {
return;
}
$this->getSitemapPage()->render();
}
/**
* Handle sitemap save callback.
*/
public function handleSitemapSave(): void {
if ( $this->blockWhenDisabled( 'sitemaps' ) ) {
return;
}
$this->getSitemapPage()->maybeHandleSave();
}
/**
* Register the RankKernel General settings submenu page.
*
* Uses the same load hook save pattern as the other pages, so the
* redirect stays header safe.
*/
public function addGeneralPage(): void {
$hook = add_submenu_page(
'rankkernel',
esc_html__( 'General Settings', 'rankkernel' ),
esc_html__( 'General Settings', 'rankkernel' ),
'manage_options',
'rankkernel-general',
[ $this, 'renderGeneral' ]
);
add_action( 'load-' . $hook, [ $this, 'handleGeneralSave' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueGeneralAssets' ] );
}
/**
* Render general settings callback.
*/
public function renderGeneral(): void {
$this->getPage()->render();
}
/**
* Handle general settings save callback.
*/
public function handleGeneralSave(): void {
$this->getPage()->maybeHandleSave();
}
/**
* Enqueue general settings assets callback.
*
* @param string $hookSuffix Current admin page hook suffix.
*/
public function enqueueGeneralAssets( string $hookSuffix = '' ): void {
if ( SettingsPage::HOOK_SUFFIX !== $hookSuffix && 'dashboard_page_rankkernel-general' !== $hookSuffix ) {
return;
}
$this->getPage()->enqueueAssets( $hookSuffix );
}
/**
* Register the RankKernel schema submenu page.
*
* Hooked separately from the top level menu so callers control
* ordering. Uses the same load hook save pattern as the sitemap
* page, so the redirect stays header safe.
*/
public function addSchemaPage(): void {
if ( ! $this->enableMap->isEnabled( 'schema' ) ) {
return;
}
$hook = add_submenu_page(
'rankkernel',
esc_html__( 'Schema Settings', 'rankkernel' ),
esc_html__( 'Schema', 'rankkernel' ),
'manage_options',
'rankkernel-schema',
[ $this, 'renderSchema' ]
);
add_action( 'load-' . $hook, [ $this, 'handleSchemaSave' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueSchemaAssets' ] );
}
/**
* Render schema settings callback.
*/
public function renderSchema(): void {
if ( $this->blockWhenDisabled( 'schema' ) ) {
return;
}
$this->getSchemaPage()->render();
}
/**
* Handle schema settings save callback.
*/
public function handleSchemaSave(): void {
if ( $this->blockWhenDisabled( 'schema' ) ) {
return;
}
$this->getSchemaPage()->maybeHandleSave();
}
/**
* Enqueue schema settings assets callback.
*
* @param string $hookSuffix Current admin page hook suffix.
*/
public function enqueueSchemaAssets( string $hookSuffix = '' ): void {
if ( SchemaSettingsPage::HOOK_SUFFIX !== $hookSuffix ) {
return;
}
$this->getSchemaPage()->enqueueAssets( $hookSuffix );
}
/**
* Register the RankKernel redirects submenu page.
*
* Hooked separately from the top level menu so callers control
* ordering. Uses the same load hook save pattern as the other
* pages, so the redirect stays header safe.
*/
public function addRedirectsPage(): void {
if ( ! $this->enableMap->isEnabled( 'redirects' ) ) {
return;
}
$hook = add_submenu_page(
'rankkernel',
esc_html__( 'Redirects', 'rankkernel' ),
esc_html__( 'Redirects', 'rankkernel' ),
'manage_options',
'rankkernel-redirects',
[ $this, 'renderRedirects' ]
);
add_action( 'load-' . $hook, [ $this, 'handleRedirectsSave' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueRedirectsAssets' ] );
}
/**
* Register the Support submenu.
*
* Last in the RankKernel menu, because it is the fallback for a user who
* needs help rather than a tool they reach for deliberately.
*
* Uses the same load hook save pattern as the other pages, so the
* post-redirect-get redirect stays header safe.
*/
public function addSupportPage(): void {
$hook = add_submenu_page(
'rankkernel',
esc_html__( 'Support', 'rankkernel' ),
esc_html__( 'Support', 'rankkernel' ),
SupportPage::CAPABILITY,
SupportPage::SLUG,
[ $this, 'renderSupport' ]
);
add_action( 'load-' . $hook, [ $this, 'handleSupportSave' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueSupportAssets' ] );
}
/**
* Render support page callback.
*/
public function renderSupport(): void {
$this->getSupportPage()->render();
}
/**
* Handle support save callback.
*/
public function handleSupportSave(): void {
$this->getSupportPage()->maybeHandleSave();
}
/**
* Enqueue support page assets callback.
*
* @param string $hookSuffix Current admin page hook suffix.
*/
public function enqueueSupportAssets( string $hookSuffix = '' ): void {
$this->getSupportPage()->enqueueAssets( $hookSuffix );
}
/**
* Get the Support page (lazy instantiated).
*
* @return SupportPage The result.
*/
public function getSupportPage(): SupportPage {
return $this->supportPage ??= new SupportPage();
}
/**
* Render redirects page callback.
*/
public function renderRedirects(): void {
if ( $this->blockWhenDisabled( 'redirects' ) ) {
return;
}
$this->getRedirectsPage()->render();
}
/**
* Handle redirects save callback.
*/
public function handleRedirectsSave(): void {
if ( $this->blockWhenDisabled( 'redirects' ) ) {
return;
}
$this->getRedirectsPage()->maybeHandleSave();
}
/**
* Enqueue redirects assets callback.
*
* @param string $hookSuffix Current admin page hook suffix.
*/
public function enqueueRedirectsAssets( string $hookSuffix = '' ): void {
if ( RedirectsPage::HOOK_SUFFIX !== $hookSuffix ) {
return;
}
$this->getRedirectsPage()->enqueueAssets( $hookSuffix );
}
/**
* Register the RankKernel 404 Monitor submenu page.
*
* Hooked separately from the top level menu so callers control
* ordering. Uses the same load hook save pattern as the other
* pages, so the redirect stays header safe.
*/
public function addMonitorPage(): void {
if ( ! $this->enableMap->isEnabled( '404' ) ) {
return;
}
$hook = add_submenu_page(
'rankkernel',
esc_html__( '404 Monitor', 'rankkernel' ),
esc_html__( '404 Monitor', 'rankkernel' ),
'manage_options',
'rankkernel-404',
[ $this, 'renderMonitor' ]
);
add_action( 'load-' . $hook, [ $this, 'handleMonitorSave' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueMonitorAssets' ] );
}
/**
* Render monitor page callback.
*/
public function renderMonitor(): void {
if ( $this->blockWhenDisabled( '404' ) ) {
return;
}
$this->getMonitorPage()->render();
}
/**
* Handle monitor save callback.
*/
public function handleMonitorSave(): void {
if ( $this->blockWhenDisabled( '404' ) ) {
return;
}
$this->getMonitorPage()->maybeHandleSave();
}
/**
* Enqueue monitor assets callback.
*
* @param string $hookSuffix Current admin page hook suffix.
*/
public function enqueueMonitorAssets( string $hookSuffix = '' ): void {
if ( NotFoundPage::HOOK_SUFFIX !== $hookSuffix ) {
return;
}
$this->getMonitorPage()->enqueueAssets( $hookSuffix );
}
/**
* Register the RankKernel Instant Indexing submenu page.
*
* Hooked separately from the top level menu so callers control
* ordering. Uses the same load hook save pattern as the other
* pages, so the redirect stays header safe.
*/
public function addInstantIndexingPage(): void {
if ( ! $this->enableMap->isEnabled( 'instant-indexing' ) ) {
return;
}
$hook = add_submenu_page(
'rankkernel',
esc_html__( 'Instant Indexing', 'rankkernel' ),
esc_html__( 'Instant Indexing', 'rankkernel' ),
'manage_options',
InstantIndexingPage::SLUG,
[ $this, 'renderInstantIndexing' ]
);
add_action( 'load-' . $hook, [ $this, 'handleInstantIndexingSave' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueInstantIndexingAssets' ] );
}
/**
* Render instant indexing page callback.
*/
public function renderInstantIndexing(): void {
if ( $this->blockWhenDisabled( 'instant-indexing' ) ) {
return;
}
$this->getInstantIndexingPage()->render();
}
/**
* Handle instant indexing save callback.
*/
public function handleInstantIndexingSave(): void {
if ( $this->blockWhenDisabled( 'instant-indexing' ) ) {
return;
}
$this->getInstantIndexingPage()->maybeHandleSave();
}
/**
* Enqueue instant indexing assets callback.
*
* @param string $hookSuffix Current admin page hook suffix.
*/
public function enqueueInstantIndexingAssets( string $hookSuffix = '' ): void {
if ( InstantIndexingPage::HOOK_SUFFIX !== $hookSuffix ) {
return;
}
$this->getInstantIndexingPage()->enqueueAssets( $hookSuffix );
}
/**
* Register the RankKernel Importer submenu page.
*
* Hooked separately from the top level menu so callers control
* ordering. Phase 1 wires render and assets only; the run endpoint
* and any save handling arrive with later phases.
*/
public function addImporterPage(): void {
if ( ! $this->enableMap->isEnabled( 'importer' ) ) {
return;
}
$hook = add_submenu_page(
'rankkernel',
esc_html__( 'Importer', 'rankkernel' ),
esc_html__( 'Importer', 'rankkernel' ),
'manage_options',
ImporterPage::SLUG,
[ $this, 'renderImporter' ]
);
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueImporterAssets' ] );
}
/**
* Render importer page callback.
*/
public function renderImporter(): void {
if ( $this->blockWhenDisabled( 'importer' ) ) {
return;
}
$this->getImporterPage()->render();
}
/**
* Enqueue importer assets callback.
*
* @param string $hookSuffix Current admin page hook suffix.
*/
public function enqueueImporterAssets( string $hookSuffix = '' ): void {
if ( ImporterPage::HOOK_SUFFIX !== $hookSuffix ) {
return;
}
$this->getImporterPage()->enqueueAssets( $hookSuffix );
}
}