__( 'Switch Theme', 'airo-wp' ), 'description' => __( 'Switches to an already-installed theme', 'airo-wp' ), 'input_schema' => $this->get_input_schema(), 'output_schema' => $this->get_output_schema(), 'execute_callback' => array( $this, 'execute' ), 'permission_callback' => array( $this, 'check_permissions' ), 'category' => 'theme-management', ) ); } /** * Get the input schema for the tool. * * @return array */ private function get_input_schema(): array { return array( 'type' => 'object', 'properties' => array( 'theme_slug' => array( 'type' => 'string', 'description' => __( 'The theme slug (e.g. "twentytwentyfour")', 'airo-wp' ), 'minLength' => 1, ), ), 'required' => array( 'theme_slug' ), ); } /** * Get the output schema for the tool. * * @return array */ private function get_output_schema(): array { return $this->build_output_schema( __( 'Theme switch result', 'airo-wp' ), array( 'theme' => array( 'type' => 'string', 'description' => __( 'The theme slug', 'airo-wp' ), ), 'previous_theme' => array( 'type' => 'string', 'description' => __( 'The previously active theme slug', 'airo-wp' ), ), ) ); } /** * Execute the tool. * * @param array $input Input parameters. * @return array Execution result. */ public function execute( array $input ): array { $slug = isset( $input['theme_slug'] ) ? sanitize_text_field( $input['theme_slug'] ) : ''; if ( empty( $slug ) ) { return array( 'success' => false, 'message' => __( 'Theme slug is required.', 'airo-wp' ), ); } $current_theme = get_stylesheet(); if ( $current_theme === $slug ) { return array( 'success' => true, 'message' => __( 'Theme is already active.', 'airo-wp' ), 'theme' => $slug, 'previous_theme' => $current_theme, ); } $theme = wp_get_theme( $slug ); if ( ! $theme->exists() ) { return array( 'success' => false, 'message' => sprintf( /* translators: %s: theme slug */ __( 'Theme "%s" is not installed. Use the activate-theme tool to install and activate it.', 'airo-wp' ), $slug ), ); } switch_theme( $slug ); if ( get_stylesheet() !== $slug ) { return array( 'success' => false, 'message' => __( 'Theme switch failed.', 'airo-wp' ), 'theme' => $slug, ); } return array( 'success' => true, 'message' => sprintf( /* translators: 1: new theme name, 2: previous theme name */ __( 'Successfully switched from "%2$s" to "%1$s".', 'airo-wp' ), $theme->get( 'Name' ), wp_get_theme( $current_theme )->get( 'Name' ) ), 'theme' => $slug, 'previous_theme' => $current_theme, ); } }