$chunk) { if (is_string($chunk)) { $target_block['innerContent'][$i] = $payload['inner_html']; $found = true; break; } } if (!$found) array_unshift($target_block['innerContent'], $payload['inner_html']); } } } elseif ($action === 'remove') { array_splice($current_array, $target_index, 1); if ($parent_block !== null) { $null_count = 0; for ($i = 0; $i < count($parent_block['innerContent']); $i++) { if ($parent_block['innerContent'][$i] === null) { if ($null_count === $target_index) { array_splice($parent_block['innerContent'], $i, 1); break; } $null_count++; } } } } elseif (in_array($action, ['insert_before', 'insert_after', 'insert_prepend', 'insert_append'])) { $new_blocks_array = $payload['new_blocks']; if (empty($new_blocks_array)) return true; $num_new = count($new_blocks_array); $insert_index = 0; if ($action === 'insert_before') { $insert_index = $target_index; } elseif ($action === 'insert_after') { $insert_index = $target_index + 1; } elseif ($action === 'insert_prepend') { $parent_block = &$current_array[$target_index]; if (!isset($parent_block['innerBlocks'])) { $parent_block['innerBlocks'] = []; } if (!isset($parent_block['innerContent'])) { $parent_block['innerContent'] = []; } elseif (count($parent_block['innerBlocks']) === 0 && count($parent_block['innerContent']) === 1 && is_string($parent_block['innerContent'][0])) { if (preg_match('/^(.*)(<\/[a-z0-9]+>\s*)$/si', $parent_block['innerContent'][0], $m)) { $parent_block['innerContent'] = [$m[1], $m[2]]; } } $current_array = &$parent_block['innerBlocks']; $insert_index = 0; } elseif ($action === 'insert_append') { $parent_block = &$current_array[$target_index]; if (!isset($parent_block['innerBlocks'])) { $parent_block['innerBlocks'] = []; } if (!isset($parent_block['innerContent'])) { $parent_block['innerContent'] = []; } elseif (count($parent_block['innerBlocks']) === 0 && count($parent_block['innerContent']) === 1 && is_string($parent_block['innerContent'][0])) { if (preg_match('/^(.*)(<\/[a-z0-9]+>\s*)$/si', $parent_block['innerContent'][0], $m)) { $parent_block['innerContent'] = [$m[1], $m[2]]; } } $current_array = &$parent_block['innerBlocks']; $insert_index = count($current_array); } array_splice($current_array, $insert_index, 0, $new_blocks_array); if ($parent_block !== null) { $null_count = 0; $content_insert_index = count($parent_block['innerContent']); for ($i = 0; $i < count($parent_block['innerContent']); $i++) { if ($parent_block['innerContent'][$i] === null) { if ($null_count === $insert_index) { $content_insert_index = $i; break; } $null_count++; } } if ($insert_index === (count($current_array) - $num_new)) { $last_idx = count($parent_block['innerContent']) - 1; if ($last_idx >= 0 && is_string($parent_block['innerContent'][$last_idx])) { $content_insert_index = $last_idx; } else { $content_insert_index = $last_idx + 1; } } $nulls = array_fill(0, $num_new, null); array_splice($parent_block['innerContent'], $content_insert_index, 0, $nulls); } } return true; } /** * Validates raw block HTML string or block structure. * * @param string|array $blocks Raw HTML string or block tree array. * @return array|\WP_Error Array with valid blocks and count, or WP_Error on syntax issues. */ private function validate_block_syntax($blocks) { if (is_string($blocks)) { $raw_html = trim($blocks); if (empty($raw_html)) { return new \WP_Error('empty_block_html', __('Block HTML cannot be empty.', 'aiutoma')); } // Check for unclosed block comment tags $open_comments = preg_match_all('//i', $raw_html); if ($open_comments > $close_comments) { return new \WP_Error('unclosed_block_comment', __('Malformed block comment: unclosed "/s'; $single_script_pattern = '/^]*)?>[\s\S]*<\/script>\s*$/i'; $single_svg_pattern = '/^]*)?>[\s\S]*<\/svg>\s*$/i'; $interaction_markup_pattern = '/<(marquee)\b/i'; $form_markup_pattern = '/<(form|input|select|textarea|fieldset)\b/i'; $html = trim(preg_replace($block_comment_pattern, '', $content)); if ($html === '') { return []; } if (preg_match($single_script_pattern, $html) || preg_match($single_svg_pattern, $html) || preg_match($interaction_markup_pattern, $html)) { return []; } if (preg_match($form_markup_pattern, $html)) { return [__('core/html contains form markup. Load the plugin-recommendations skill and use editable plugin blocks such as Jetpack Forms. This keeps forms editable in the block editor and handles submission without custom backend code.', 'aiutoma')]; } return [__('core/html contains markup that should use editable core blocks. Load the block-content skill and use core/group, core/columns, core/heading, core/paragraph, core/list, core/image, core/buttons, and theme CSS instead. Keep core/html only for inline SVG, interaction markup with no block equivalent (marquee, cursor), or a single script block.', 'aiutoma')]; } private function validate_html_block_policy($content) { $html_block_pattern = '/([\s\S]*?)/s'; $invalid_html_blocks = []; $total_html_blocks = 0; if (preg_match_all($html_block_pattern, $content, $matches, PREG_OFFSET_CAPTURE)) { $total_html_blocks = count($matches[0]); for ($i = 0; $i < $total_html_blocks; $i++) { $block_content = $matches[1][$i][0] ?? ''; $offset = $matches[0][$i][1] ?? 0; $issues = $this->get_html_block_policy_issues($block_content); if (empty($issues)) { continue; } $line_number = substr_count(substr($content, 0, $offset), "\n") + 1; $trimmed_content = trim($block_content); $compact = preg_replace('/\s+/', ' ', $trimmed_content); $preview = strlen($compact) > 500 ? substr($compact, 0, 500) . '…' : $compact; $invalid_html_blocks[] = [ 'blockNumber' => $i + 1, 'line' => $line_number, 'content' => $preview, 'issues' => $issues, ]; } } return [ 'totalHtmlBlocks' => $total_html_blocks, 'invalidHtmlBlocks' => $invalid_html_blocks, ]; } private function generate_unified_patch($file_name, $old_str, $new_str) { $old_lines = explode("\n", str_replace("\r\n", "\n", $old_str)); $new_lines = explode("\n", str_replace("\r\n", "\n", $new_str)); $diff = ["--- a/{$file_name}", "+++ b/{$file_name}", "@@ -1," . count($old_lines) . " +1," . count($new_lines) . " @@"]; foreach ($old_lines as $l) { $diff[] = "- {$l}"; } foreach ($new_lines as $l) { $diff[] = "+ {$l}"; } return implode("\n", $diff); } private function register_validate_blocks() { \Aiutoma\Modules\Ai\Abilities::register('aiutoma/validate-blocks', [ 'category' => 'gutenberg', 'label' => __('Validate Blocks', 'aiutoma'), 'meta' => [ 'plugin_name' => 'Aiutoma', 'mcp' => ['public' => true] ], 'description' => __('Validates WordPress block content in two stages and returns a combined report. First runs a static core/html block policy check; if it finds invalid core/html blocks, it returns only those without touching the editor. Once the policy check passes, it validates the content in the site\'s real block editor and returns any serialization fixes along with a unified diff for CSS review.', 'aiutoma'), 'execute_callback' => function ($input) { $block_content = null; $file_name = 'block-content.html'; if (isset($input['content']) && is_string($input['content'])) { $block_content = $input['content']; } else { return new \WP_Error('missing_input', __('Block content must be provided in the "content" parameter.', 'aiutoma')); } // Stage 1: HTML block policy check $html_report = $this->validate_html_block_policy($block_content); if (!empty($html_report['invalidHtmlBlocks'])) { $lines = [ sprintf('HTML block policy: %d/%d core/html blocks invalid', count($html_report['invalidHtmlBlocks']), $html_report['totalHtmlBlocks']), '', 'Invalid HTML blocks:' ]; foreach ($html_report['invalidHtmlBlocks'] as $block) { $lines[] = sprintf(' - #%d line %d', $block['blockNumber'], $block['line']); foreach ($block['issues'] as $issue) { $lines[] = ' ' . $issue; } $lines[] = ' Content: ' . $block['content']; } $lines[] = ''; $lines[] = 'Rewrite each invalid core/html block as editable core or plugin blocks, then call validate_blocks again. Editor validation was skipped until the HTML policy passes.'; return implode("\n", $lines); } $html_summary = ($html_report['totalHtmlBlocks'] === 0) ? 'HTML block policy: no core/html blocks found.' : sprintf('HTML block policy: all %d core/html blocks within policy.', $html_report['totalHtmlBlocks']); // Stage 2: Live Editor Validation $parsed_blocks = parse_blocks($block_content); $results = []; $registry = \WP_Block_Type_Registry::get_instance(); $validate_recursive = function ($blocks) use (&$validate_recursive, &$results, $registry) { foreach ($blocks as $b) { $name = $b['blockName'] ?? null; if (!$name || $name === 'core/freeform' || $name === 'core/missing') { continue; } $is_registered = $registry->is_registered($name); $issues = []; $is_valid = true; if (!$is_registered) { $is_valid = false; $issues[] = sprintf('Block type "%s" is not registered. It may require a plugin that is not active.', $name); } $results[] = [ 'blockName' => $name, 'isValid' => $is_valid, 'issues' => $issues ]; if (!empty($b['innerBlocks'])) { $validate_recursive($b['innerBlocks']); } } }; $validate_recursive($parsed_blocks); $total_blocks = count($results); $valid_blocks = count(array_filter($results, function ($r) { return $r['isValid']; })); $invalid_blocks = $total_blocks - $valid_blocks; // Normalize and serialize $fixed_content = serialize_blocks($parsed_blocks); $content_matches = (trim($fixed_content) === trim($block_content)); if ($invalid_blocks === 0 && $content_matches) { return implode("\n", [ $html_summary, sprintf('Validation: %d/%d blocks valid', $valid_blocks, $total_blocks), 'No editor serialization fixes needed.' ]); } $lines = [ $html_summary, sprintf('Validation: %d/%d blocks valid', $valid_blocks, $total_blocks), '' ]; if ($invalid_blocks > 0) { $lines[] = 'Invalid blocks:'; foreach ($results as $r) { if (!$r['isValid']) { $lines[] = ' - ' . $r['blockName']; foreach ($r['issues'] as $iss) { $lines[] = ' ' . $iss; } } } $lines[] = ''; } if (!$content_matches) { $diff = $this->generate_unified_patch($file_name, $block_content, $fixed_content); $lines[] = sprintf('Auto-fix proposal: %d/%d blocks valid after live-editor serialization.', $valid_blocks, $total_blocks); $lines[] = 'Use the fixed block content below as the replacement block content. Use the diff to review class/nesting changes and update CSS selectors if needed.'; $lines[] = ''; $lines[] = 'Fixed block content:'; $lines[] = "```html\n{$fixed_content}\n```"; $lines[] = ''; $lines[] = 'Diff for CSS review:'; $lines[] = "```diff\n{$diff}\n```"; } else { $lines[] = 'No automatic editor serialization fix was available.'; } return implode("\n", $lines); }, 'permission_callback' => function () { return current_user_can('edit_posts'); }, 'input_schema' => [ 'type' => 'object', 'properties' => [ 'content' => [ 'type' => 'string', 'description' => 'Raw WordPress block content (HTML with block comments) to validate and fix.' ] ], 'required' => ['content'] ] ]); } }