getInputs(); $slug = $inputs['slug'] ?? '?'; $status = $inputs['status'] ?? '?'; return "Turn the \"{$slug}\" module {$status}"; } /** * Human-readable inputs shown in the approval card table. * * @return array */ public function get_approval_inputs(): array { return $this->getInputs(); } /** * Execute the tool after human approval. * * @param string $slug Module/integration slug. * @param string $status on|off. * @return array */ public function __invoke( string $slug, string $status ): array { $check = $this->guard_admin(); if ( $check ) { return $check; } $slug = sanitize_key( $slug ); if ( '' === $slug ) { return [ 'error' => 'A module slug is required.' ]; } if ( ! in_array( $status, self::ALLOWED_STATUSES, true ) ) { return [ 'error' => "Invalid status '{$status}'. Accepted values: " . implode( ', ', self::ALLOWED_STATUSES ) . '.' ]; } // Confirm the slug is a real, in-place feature toggle (not a plugin/addon // install) before touching the controller, which would otherwise accept // install/activate actions for plugin-type extensions. $modules = function_exists( 'booktics_modules_list' ) ? booktics_modules_list() : []; $module = $modules[ $slug ] ?? null; if ( ! is_array( $module ) ) { return [ 'error' => "Module '{$slug}' was not found. Use booktics-list-modules to see valid slugs." ]; } if ( ! in_array( $module['type'] ?? '', self::TOGGLEABLE_TYPES, true ) ) { return [ 'error' => "Module '{$slug}' is not a toggleable feature module and cannot be changed with this tool." ]; } try { $request = $this->build_json_request( 'POST', '/booktics/v1/module', [ 'name' => $slug, 'status' => $status, ] ); if ( is_array( $request ) ) { return $request; } $controller = new \Booktics\Extensions\Controllers\Module_Controller(); $data = $this->extract_response( $controller->update_item( $request ), 'Failed to toggle module.' ); if ( isset( $data['error'] ) ) { return $data; } $label = $module['title'] ?? $slug; return [ 'success' => true, 'slug' => $slug, 'status' => $status, 'message' => "The \"{$label}\" module was turned {$status}.", ]; } catch ( \Throwable $e ) { Log::error( 'Booktics_Toggle_Module_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to toggle module: ' . $e->getMessage() ]; } } }