* @since 1.3.0 */ public static function disabled(): array { $disabled = get_option( self::OPTION, [] ); if ( empty( $disabled ) && ! get_option( self::SAVED_OPTION ) ) { return AbilitiesRegistry::get_default_disabled_abilities(); } return array_values( array_map( 'strval', (array) $disabled ) ); } /** * Whether an ability is currently enabled. * * @param string $ability_id Ability ID. * * @return bool * @since 1.3.0 */ public static function is_enabled( string $ability_id ): bool { return ! in_array( $ability_id, self::disabled(), true ); } /** * Enable or disable a single ability. * * Mutates only the requested ability so two concurrent toggles can't clobber * each other, and records that toggles have been saved. * * @param string $ability_id Ability ID. * @param bool $enabled Desired enabled state. * * @return void * @since 1.3.0 */ public static function set_enabled( string $ability_id, bool $enabled ): void { $disabled = self::disabled(); if ( $enabled ) { $disabled = array_values( array_diff( $disabled, [ $ability_id ] ) ); } elseif ( ! in_array( $ability_id, $disabled, true ) ) { $disabled[] = $ability_id; } update_option( self::OPTION, $disabled ); update_option( self::SAVED_OPTION, true ); } /** * Enable or disable many abilities at once with a single option write. * * @param array $ability_ids Ability IDs to change. * @param bool $enabled Desired enabled state for all of them. * * @return void * @since 1.3.0 */ public static function set_enabled_bulk( array $ability_ids, bool $enabled ): void { if ( empty( $ability_ids ) ) { return; } $disabled = array_fill_keys( self::disabled(), true ); foreach ( $ability_ids as $ability_id ) { if ( $enabled ) { unset( $disabled[ $ability_id ] ); } else { $disabled[ $ability_id ] = true; } } update_option( self::OPTION, array_values( array_map( 'strval', array_keys( $disabled ) ) ) ); update_option( self::SAVED_OPTION, true ); } }