Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions includes/class-openclawp-admin-menu-visibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
* of passing `null` as the parent slug to `add_submenu_page()`, which
* registers the page handler but skips the sidebar entry.
*
* This class also owns the *single* set of population checks for each
* capability surface: both the menu-gating logic and the "Discover"
* panel (rendered on the Chat page) consult `is_surface_populated()` /
* `surface_count()` so there is no risk of the menu and the discovery
* surface disagreeing about whether a capability is set up.
*
* @package OpenclaWP
* @since 0.9.0
*/
Expand Down Expand Up @@ -96,4 +102,124 @@
}
return null;
}

/**
* Convenience wrapper: resolve the parent slug for a hide-when-empty
* surface using the centralised population check.
*
* Equivalent to `parent_for( $slug, self::is_surface_populated( $slug ) )`.
* Prefer this from registration sites so the same store call powers both
* the menu gating and the Discover panel.
*
* @param string $slug The submenu slug being registered.
*
* @return string|null
*/
public static function parent_for_slug( string $slug ): ?string {
return self::parent_for( $slug, self::is_surface_populated( $slug ) );
}

/**
* Whether the capability surface backing `$slug` currently has at least
* one entry. Returns `true` for surfaces this class doesn't know about
* — unknown slugs are assumed to be set up by some host-supplied module,
* so they aren't suppressed.
*
* @param string $slug Submenu slug, e.g. `openclawp-mcp-servers`.
*/
public static function is_surface_populated( string $slug ): bool {
$count = self::surface_count( $slug );
return null === $count ? true : $count > 0;
}

/**
* Count the entries for a capability surface. Returns `null` for
* surfaces this class doesn't know how to introspect.
*
* Cheap on purpose — every check is either a `posts_per_page=1` query, an
* already-cached option read, or a function-exists guard. The Discover
* panel calls each of these on every Chat page load.
*
* @param string $slug Submenu slug, e.g. `openclawp-channels`.
*/
public static function surface_count( string $slug ): ?int {
switch ( $slug ) {
case 'openclawp-channels':
if ( ! class_exists( 'OpenclaWP_Channels_Admin' ) ) {
return null;
}
return count( OpenclaWP_Channels_Admin::get_channels() );

case 'openclawp-workflows':
if ( ! class_exists( 'OpenclaWP_Workflow_Store' ) ) {
return null;
}
$store = OpenclaWP_Workflow_Store::instance();
return count( $store->all() );

Check failure on line 158 in includes/class-openclawp-admin-menu-visibility.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHPCS + PHPStan)

Call to method all() on an unknown class AgentsAPI\AI\Workflows\WP_Agent_Workflow_Store.

case 'openclawp-custom-tools':
if ( ! class_exists( 'OpenclaWP_Custom_Tools_Store' ) ) {
return null;
}
return count( OpenclaWP_Custom_Tools_Store::all() );

case 'openclawp-routines':
if ( ! function_exists( 'wp_get_routines' ) ) {
return 0;
}
return count( (array) wp_get_routines() );

case 'openclawp-usage':
if ( ! class_exists( 'OpenclaWP_Usage_Store' ) ) {
return null;
}
// `get_recent( 1 )` keeps the existence check cheap; treat it
// as a boolean signal, since the user-facing count would be
// expensive to compute.
return count( OpenclaWP_Usage_Store::get_recent( 1 ) );

case 'openclawp-knowledge-base':
if ( ! class_exists( 'OpenclaWP_Knowledge_Base_Sources' ) ) {
return null;
}
$config = OpenclaWP_Knowledge_Base_Sources::get();
return count( (array) ( $config['post_types'] ?? array() ) )

Check failure on line 186 in includes/class-openclawp-admin-menu-visibility.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHPCS + PHPStan)

Offset 'post_types' on array{post_types: array<int, string>, urls: array<int, array{url: string, interval: string}>} on left side of ?? always exists and is not nullable.
+ count( (array) ( $config['urls'] ?? array() ) );

Check failure on line 187 in includes/class-openclawp-admin-menu-visibility.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHPCS + PHPStan)

Offset 'urls' on array{post_types: array<int, string>, urls: array<int, array{url: string, interval: string}>} on left side of ?? always exists and is not nullable.

case 'openclawp-mcp-servers':
if ( ! class_exists( 'OpenclaWP_Mcp_Server_Store' ) ) {
return null;
}
return count( OpenclaWP_Mcp_Server_Store::all() );

case 'openclawp-whatsapp':
if ( ! class_exists( 'OpenclaWP_Whatsapp' ) ) {
return null;
}
$settings = OpenclaWP_Whatsapp::settings();
$has_token = '' !== trim( (string) ( $settings['access_token'] ?? '' ) );
$has_phone = '' !== trim( (string) ( $settings['phone_number_id'] ?? '' ) );
return ( $has_token || $has_phone ) ? 1 : 0;

case 'openclawp-connected-clients':
if ( ! class_exists( 'OpenclaWP_Oauth_Store' ) ) {
return null;
}
return count( OpenclaWP_Oauth_Store::all_clients() );

case 'openclawp-decisions':
if ( ! class_exists( 'OpenclaWP_Decisions_Store' ) ) {
return null;
}
return count( OpenclaWP_Decisions_Store::recent( array( 'limit' => 1 ) ) );

case 'openclawp-mcp-clients':
if ( ! class_exists( 'OpenclaWP_Mcp_Client_Store' ) ) {
return null;
}
return count( OpenclaWP_Mcp_Client_Store::all() );
}

return null;
}
}
Loading
Loading