| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: Import an entire WDesignKit website kit (all pages) in one call. |
| 4 |
* |
| 5 |
* Wraps the wdkit_create_full_site master hook (includes/admin/hooks/class-wdkit-kit-import-hook.php), |
| 6 |
* whose own docblock says "use this from Sprout MCP". Runs the full orchestration: |
| 7 |
* reset_site -> plugin_settings -> theme_settings -> import_pages -> enable_widgets -> finalize. |
| 8 |
*/ |
| 9 |
|
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit(); |
| 14 |
} |
| 15 |
|
| 16 |
wp_register_ability('wdesignkit/import-full-kit', [ |
| 17 |
'label' => __('Import Full WDesignKit Site Kit', 'wdesignkit'), |
| 18 |
'description' => __( |
| 19 |
'Imports an entire website kit in one call: creates a WordPress page for every template in the kit, applies the relevant plugin/theme settings, enables required widgets, and sets the homepage/site name/tagline. DESTRUCTIVE by default — the reset_site step drafts every currently-published page; pass skip:["reset_site"] to keep existing content. Get the kit_id from wdesignkit/browse-kits and the templates list from wdesignkit/browse-kit-pages.', |
| 20 |
'wdesignkit', |
| 21 |
), |
| 22 |
'category' => 'wdesignkit', |
| 23 |
'input_schema' => [ |
| 24 |
'type' => 'object', |
| 25 |
'properties' => [ |
| 26 |
'kit_id' => [ |
| 27 |
'type' => 'string', |
| 28 |
'description' => 'WDesignKit kit ID (from wdesignkit/browse-kits).', |
| 29 |
], |
| 30 |
'editor' => [ |
| 31 |
'type' => 'string', |
| 32 |
'description' => 'Builder the kit is authored in.', |
| 33 |
'enum' => ['elementor', 'gutenberg'], |
| 34 |
], |
| 35 |
'templates' => [ |
| 36 |
'type' => 'array', |
| 37 |
'description' => 'The kit\'s page templates (from wdesignkit/browse-kit-pages). Each item: {"id": "<template id>", "title": "Home", "type": "page", "wp_post_type": "page"}. id and title are used; a title containing "home"/"landing" becomes the front page.', |
| 38 |
'items' => ['type' => 'object'], |
| 39 |
'minItems' => 1, |
| 40 |
], |
| 41 |
'site_name' => [ |
| 42 |
'type' => 'string', |
| 43 |
'description' => 'Optional site title (blogname).', |
| 44 |
], |
| 45 |
'tagline' => [ |
| 46 |
'type' => 'string', |
| 47 |
'description' => 'Optional site tagline (blogdescription).', |
| 48 |
], |
| 49 |
'skip' => [ |
| 50 |
'type' => 'array', |
| 51 |
'description' => 'Optional steps to skip. Use ["reset_site"] to avoid drafting existing published pages.', |
| 52 |
'items' => [ |
| 53 |
'type' => 'string', |
| 54 |
'enum' => ['reset_site', 'plugin_settings', 'theme_settings', 'enable_widgets'], |
| 55 |
], |
| 56 |
], |
| 57 |
], |
| 58 |
'required' => ['kit_id', 'templates'], |
| 59 |
'additionalProperties' => false, |
| 60 |
], |
| 61 |
'output_schema' => [ |
| 62 |
'type' => 'object', |
| 63 |
'properties' => [ |
| 64 |
'success' => ['type' => 'boolean'], |
| 65 |
'message' => ['type' => 'string'], |
| 66 |
'site_url' => ['type' => 'string'], |
| 67 |
'home_page_id' => ['type' => 'integer'], |
| 68 |
'shop_page_id' => ['type' => 'integer'], |
| 69 |
'pages' => ['type' => 'array'], |
| 70 |
'steps' => ['type' => ['object', 'array']], |
| 71 |
'errors' => ['type' => 'array'], |
| 72 |
], |
| 73 |
], |
| 74 |
'execute_callback' => 'wdesignkit_mcp_import_full_kit', |
| 75 |
'permission_callback' => 'wdesignkit_mcp_permission_callback', |
| 76 |
'meta' => [ |
| 77 |
'public' => true, |
| 78 |
'show_in_rest' => true, |
| 79 |
'mcp' => ['public' => true], |
| 80 |
'annotations' => [ |
| 81 |
'instructions' => implode("\n", [ |
| 82 |
'⚠ DESTRUCTIVE — by default this drafts EVERY currently-published page (the reset_site step) before importing. Confirm with the user first, or pass skip:["reset_site"] to preserve existing content.', |
| 83 |
'Requires WDesignKit cloud login.', |
| 84 |
'Flow: wdesignkit/browse-kits (find the kit → kit_id) → wdesignkit/browse-kit-pages (list the kit\'s pages → templates) → this ability.', |
| 85 |
'templates is the array of the kit\'s pages; each needs at least an id and title.', |
| 86 |
'Returns per-page results plus a per-step breakdown; a page in "errors" failed to import.', |
| 87 |
]), |
| 88 |
'readonly' => false, |
| 89 |
'destructive' => true, |
| 90 |
'idempotent' => false, |
| 91 |
], |
| 92 |
], |
| 93 |
]); |
| 94 |
|
| 95 |
function wdesignkit_mcp_import_full_kit(array $input): array { |
| 96 |
set_time_limit(300); |
| 97 |
|
| 98 |
if (!defined('WDKIT_BUILDER_PATH') || !class_exists('WDesignKit_Data_Query')) { |
| 99 |
return ['success' => false, 'message' => 'WDesignKit plugin is not active.']; |
| 100 |
} |
| 101 |
|
| 102 |
$kit_id = sanitize_text_field((string) ($input['kit_id'] ?? '')); |
| 103 |
if ($kit_id === '') { |
| 104 |
return ['success' => false, 'message' => 'kit_id is required (get it from wdesignkit/browse-kits).']; |
| 105 |
} |
| 106 |
|
| 107 |
// templates may arrive as an array (preferred) or a JSON string. |
| 108 |
$templates = $input['templates'] ?? null; |
| 109 |
if (is_string($templates)) { |
| 110 |
$decoded = json_decode($templates, true); |
| 111 |
$templates = is_array($decoded) ? $decoded : null; |
| 112 |
} |
| 113 |
if (!is_array($templates) || $templates === []) { |
| 114 |
return ['success' => false, 'message' => 'templates is required and must be a non-empty array (get it from wdesignkit/browse-kit-pages).']; |
| 115 |
} |
| 116 |
|
| 117 |
$editor = sanitize_text_field((string) ($input['editor'] ?? 'elementor')); |
| 118 |
if (!in_array($editor, ['elementor', 'gutenberg'], true)) { |
| 119 |
$editor = 'elementor'; |
| 120 |
} |
| 121 |
|
| 122 |
$skip = $input['skip'] ?? []; |
| 123 |
if (is_string($skip)) { |
| 124 |
$decoded = json_decode($skip, true); |
| 125 |
$skip = is_array($decoded) ? $decoded : [$skip]; |
| 126 |
} |
| 127 |
$skip = is_array($skip) ? array_values(array_map('sanitize_text_field', $skip)) : []; |
| 128 |
|
| 129 |
$args = [ |
| 130 |
'kit_id' => $kit_id, |
| 131 |
'editor' => $editor, |
| 132 |
'templates' => $templates, |
| 133 |
'site_name' => sanitize_text_field((string) ($input['site_name'] ?? '')), |
| 134 |
'tagline' => sanitize_text_field((string) ($input['tagline'] ?? '')), |
| 135 |
'skip' => $skip, |
| 136 |
]; |
| 137 |
|
| 138 |
// The master hook (wdkit_create_full_site) does all six steps and returns a fully-formed |
| 139 |
// result array. It is registered in class-wdkit-kit-import-hook.php. |
| 140 |
if (!has_filter('wdkit_create_full_site')) { |
| 141 |
return ['success' => false, 'message' => 'Full-site import hook is not available in this WDesignKit build.']; |
| 142 |
} |
| 143 |
|
| 144 |
$result = apply_filters('wdkit_create_full_site', [], $args); |
| 145 |
|
| 146 |
if (!is_array($result)) { |
| 147 |
return ['success' => false, 'message' => 'Full-site import returned an unexpected response.']; |
| 148 |
} |
| 149 |
|
| 150 |
return [ |
| 151 |
'success' => (bool) ($result['success'] ?? false), |
| 152 |
'message' => (string) ($result['message'] ?? ''), |
| 153 |
'site_url' => (string) ($result['site_url'] ?? ''), |
| 154 |
'home_page_id' => (int) ($result['home_page_id'] ?? 0), |
| 155 |
'shop_page_id' => (int) ($result['shop_page_id'] ?? 0), |
| 156 |
'pages' => is_array($result['pages'] ?? null) ? $result['pages'] : [], |
| 157 |
'steps' => is_array($result['steps'] ?? null) ? $result['steps'] : [], |
| 158 |
'errors' => is_array($result['errors'] ?? null) ? $result['errors'] : [], |
| 159 |
]; |
| 160 |
} |
| 161 |
|