administrator + shop_manager. Opens the tool surfaces * (dashboard, generators) and their tool-bootstrap REST calls. * - `manage_aipt_settings` -> administrator always; shop_manager only when the store * owner opts in via the `aipt_allow_shop_manager_settings` option. Guards sensitive * surfaces (API keys, license/billing, connectors migration, onboarding, diagnostics). * * Not run per request: a role editor's change (a removed `use_aipt_tools`, a hand-granted * `manage_aipt_settings`) must survive. Only roles that exist are touched, and a * capability that is already granted is not written again, so the call is idempotent. * * @return bool Whether the shop_manager role existed and took the grant. WooCommerce * creates that role, so false means the caller should try again later. */ public function grantDefaults(): bool { $admin_role = get_role( 'administrator' ); if ( $admin_role ) { foreach ( array( 'use_aipt_tools', 'manage_aipt_settings' ) as $cap ) { if ( ! $admin_role->has_cap( $cap ) ) { $admin_role->add_cap( $cap ); } } } $shop_manager_role = get_role( 'shop_manager' ); if ( $shop_manager_role && ! $shop_manager_role->has_cap( 'use_aipt_tools' ) ) { $shop_manager_role->add_cap( 'use_aipt_tools' ); } $this->syncShopManagerSettingsCap(); return null !== $shop_manager_role; } /** * Grant or revoke `manage_aipt_settings` for the shop_manager role based on the * admin opt-in option `aipt_allow_shop_manager_settings`. * * Called by grantDefaults() and immediately whenever the option value changes * (wired in Init via update_option_/add_option_ hooks), so the toggle is the one owner of * shop_manager's `manage_aipt_settings` at the moment it changes. Idempotent. */ public function syncShopManagerSettingsCap(): void { $shop_manager_role = get_role( 'shop_manager' ); if ( ! $shop_manager_role ) { return; } if ( $this->shopManagerSettingsAllowed() ) { if ( ! $shop_manager_role->has_cap( 'manage_aipt_settings' ) ) { $shop_manager_role->add_cap( 'manage_aipt_settings' ); } } elseif ( $shop_manager_role->has_cap( 'manage_aipt_settings' ) ) { $shop_manager_role->remove_cap( 'manage_aipt_settings' ); } } /** * Whether the store owner has opted shop managers into settings management. * * @return bool */ private function shopManagerSettingsAllowed(): bool { return (bool) get_option( 'aipt_allow_shop_manager_settings', false ); } }