| 1 |
<?php |
| 2 |
/** |
| 3 |
* Abilities: Get and toggle WDesignKit design template visibility states. |
| 4 |
*/ |
| 5 |
|
| 6 |
declare(strict_types=1); |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit(); |
| 10 |
} |
| 11 |
|
| 12 |
wp_register_ability('wdesignkit/get-design-templates-toggle', [ |
| 13 |
'label' => __('Get WDesignKit Design Template Toggle States', 'wdesignkit'), |
| 14 |
'description' => __( |
| 15 |
'Returns the current visibility state for the WDesignKit template library and per-builder template sources: Elementor templates and Gutenberg templates.', |
| 16 |
'wdesignkit', |
| 17 |
), |
| 18 |
'category' => 'wdesignkit', |
| 19 |
'input_schema' => [ |
| 20 |
'type' => 'object', |
| 21 |
], |
| 22 |
'output_schema' => [ |
| 23 |
'type' => 'object', |
| 24 |
'properties' => [ |
| 25 |
'success' => ['type' => 'boolean'], |
| 26 |
'templates' => ['type' => 'object'], |
| 27 |
], |
| 28 |
], |
| 29 |
'execute_callback' => 'wdesignkit_mcp_get_design_templates_toggle', |
| 30 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 31 |
'meta' => [ |
| 32 |
'public' => true, |
| 33 |
'show_in_rest' => true, |
| 34 |
'mcp' => ['public' => true], |
| 35 |
'annotations' => [ |
| 36 |
'instructions' => implode("\n", [ |
| 37 |
'Returns the current state of template library visibility toggles.', |
| 38 |
'Keys: template (master library toggle), elementor_template, gutenberg_template.', |
| 39 |
'Use wdesignkit/toggle-design-templates to change these states.', |
| 40 |
]), |
| 41 |
'readonly' => true, |
| 42 |
'destructive' => false, |
| 43 |
'idempotent' => true, |
| 44 |
], |
| 45 |
], |
| 46 |
]); |
| 47 |
|
| 48 |
wp_register_ability('wdesignkit/toggle-design-templates', [ |
| 49 |
'label' => __('Toggle WDesignKit Design Template Visibility', 'wdesignkit'), |
| 50 |
'description' => __( |
| 51 |
'Enables or disables the WDesignKit template library and per-builder template sources. Turning off a builder-specific toggle hides that builder\'s templates without disabling the builder itself. Only the keys you pass are changed.', |
| 52 |
'wdesignkit', |
| 53 |
), |
| 54 |
'category' => 'wdesignkit', |
| 55 |
'input_schema' => [ |
| 56 |
'type' => 'object', |
| 57 |
'properties' => [ |
| 58 |
'template' => [ |
| 59 |
'type' => 'boolean', |
| 60 |
'description' => 'Master toggle for the entire template library.', |
| 61 |
], |
| 62 |
'elementor_template' => [ |
| 63 |
'type' => 'boolean', |
| 64 |
'description' => 'Show or hide Elementor templates in the library.', |
| 65 |
], |
| 66 |
'gutenberg_template' => [ |
| 67 |
'type' => 'boolean', |
| 68 |
'description' => 'Show or hide Gutenberg templates in the library.', |
| 69 |
], |
| 70 |
], |
| 71 |
'additionalProperties' => false, |
| 72 |
], |
| 73 |
'output_schema' => [ |
| 74 |
'type' => 'object', |
| 75 |
'properties' => [ |
| 76 |
'success' => ['type' => 'boolean'], |
| 77 |
'message' => ['type' => 'string'], |
| 78 |
'templates' => ['type' => 'object'], |
| 79 |
], |
| 80 |
], |
| 81 |
'execute_callback' => 'wdesignkit_mcp_toggle_design_templates', |
| 82 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 83 |
'meta' => [ |
| 84 |
'public' => true, |
| 85 |
'show_in_rest' => true, |
| 86 |
'mcp' => ['public' => true], |
| 87 |
'annotations' => [ |
| 88 |
'instructions' => implode("\n", [ |
| 89 |
'Toggles template library visibility settings.', |
| 90 |
'Setting template to false hides the entire template library.', |
| 91 |
'elementor_template and gutenberg_template control which builder sources appear in the library.', |
| 92 |
'Only the keys you provide are updated; others retain their current values.', |
| 93 |
]), |
| 94 |
'readonly' => false, |
| 95 |
'destructive' => false, |
| 96 |
'idempotent' => true, |
| 97 |
], |
| 98 |
], |
| 99 |
]); |
| 100 |
|
| 101 |
function wdesignkit_mcp_get_design_templates_toggle(array $input): array { |
| 102 |
$settings = get_option('wkit_settings_panel', []); |
| 103 |
if (!is_array($settings)) { |
| 104 |
$settings = []; |
| 105 |
} |
| 106 |
|
| 107 |
$defaults = [ |
| 108 |
'template' => true, |
| 109 |
'elementor_template' => true, |
| 110 |
'gutenberg_template' => true, |
| 111 |
]; |
| 112 |
|
| 113 |
$templates = []; |
| 114 |
foreach ($defaults as $key => $default) { |
| 115 |
$templates[$key] = isset($settings[$key]) ? (bool) $settings[$key] : $default; |
| 116 |
} |
| 117 |
|
| 118 |
return [ |
| 119 |
'success' => true, |
| 120 |
'templates' => $templates, |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
function wdesignkit_mcp_toggle_design_templates(array $input): array { |
| 125 |
$settings = get_option('wkit_settings_panel', []); |
| 126 |
if (!is_array($settings)) { |
| 127 |
$settings = []; |
| 128 |
} |
| 129 |
|
| 130 |
$allowed = ['template', 'elementor_template', 'gutenberg_template']; |
| 131 |
$changed = []; |
| 132 |
|
| 133 |
foreach ($allowed as $key) { |
| 134 |
if (isset($input[$key])) { |
| 135 |
$settings[$key] = (bool) $input[$key]; |
| 136 |
$changed[] = $key; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
$defaults = [ |
| 141 |
'template' => true, |
| 142 |
'elementor_template' => true, |
| 143 |
'gutenberg_template' => true, |
| 144 |
]; |
| 145 |
$templates = []; |
| 146 |
foreach ($defaults as $key => $default) { |
| 147 |
$templates[$key] = isset($settings[$key]) ? (bool) $settings[$key] : $default; |
| 148 |
} |
| 149 |
|
| 150 |
if (empty($changed)) { |
| 151 |
return [ |
| 152 |
'success' => true, |
| 153 |
'message' => 'No template settings provided to update.', |
| 154 |
'templates' => $templates, |
| 155 |
]; |
| 156 |
} |
| 157 |
|
| 158 |
update_option('wkit_settings_panel', $settings); |
| 159 |
|
| 160 |
return [ |
| 161 |
'success' => true, |
| 162 |
'message' => 'Template settings updated: ' . implode(', ', $changed), |
| 163 |
'templates' => $templates, |
| 164 |
]; |
| 165 |
} |
| 166 |
|