* @author Maximum Software Inc * @license GPL-3.0-only * @link https://agenistrator.com/ * * SPDX-FileCopyrightText: 2025-2026 Alex Ruban * SPDX-FileCopyrightText: 2025-2026 Maximum Software Inc * SPDX-License-Identifier: GPL-3.0-only */ namespace Agenistrator; // Direct access prevention if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Unassign_Menu_From_Location_Tool class. */ class Unassign_Menu_From_Location_Tool extends Tool { /** * Get the tool ID. * * @return string The tool ID. * @since 1.0.0 */ public function get_id() { return 'unassign_menu_from_location'; } /** * Get the tool name. * * @return string The tool name. * @since 1.0.0 */ public function get_name() { return __( 'Unassign Menu from Location', 'agenistrator' ); } /** * Get the description of the tool. * * @return string The description of the tool. * @since 1.0.0 */ public function get_description() { return 'Removes a menu assignment from a theme location.'; } /** * Get the parameters for this tool. * * @return array Parameters schema in JSON Schema format. * @since 1.0.0 */ public function get_parameters() { return array( 'type' => 'object', 'properties' => array( 'location' => array( 'type' => 'string', 'description' => 'The theme location slug to unassign the menu from', ), ), 'required' => array( 'location' ), ); } /** * Check if the current user has permission to execute this tool. * * @return bool Whether the user has permission. * @since 1.0.0 */ public function current_user_can_execute() { return current_user_can( 'edit_theme_options' ); } /** * Check if this tool is read-only. * * @return bool False - this tool modifies data. * @since 1.0.0 */ public function is_read_only() { return false; } /** * Execute the tool with the given arguments. * * @param array $args The arguments for the tool. * @return array The result of executing the tool. * @since 1.0.0 */ protected function execute( $args ) { $location = sanitize_text_field( $args['location'] ); // Verify location is registered. $registered_locations = get_registered_nav_menus(); // Check if theme supports classic menus (block themes typically don't register menu locations) if ( empty( $registered_locations ) ) { $is_block_theme = function_exists( 'wp_is_block_theme' ) && wp_is_block_theme(); if ( $is_block_theme ) { return $this->error_response( \__( 'This theme is a block theme and does not use classic menu locations. Block themes use the Navigation block instead.', 'agenistrator' ) ); } return $this->error_response( \__( 'No menu locations are registered by the current theme.', 'agenistrator' ) ); } if ( ! isset( $registered_locations[ $location ] ) ) { return $this->error_response( "Location '{$location}' is not registered." ); } // Get current locations. $locations = get_nav_menu_locations(); // Check if there's a menu assigned. if ( ! isset( $locations[ $location ] ) || 0 === $locations[ $location ] ) { return $this->error_response( "No menu is assigned to location '{$location}'." ); } $previous_menu_id = $locations[ $location ]; // Remove the assignment. $locations[ $location ] = 0; set_theme_mod( 'nav_menu_locations', $locations ); return $this->success_response( 'Menu unassigned from location successfully', array( 'location' => $location, 'previous_menu_id' => $previous_menu_id, 'unassigned' => true, ) ); } }