$signature, 'context' => $context), self::CACHE_TTL); } $context['customInstructions'] = self::get_custom_instructions(); // Sections de l'utilisateur (id, titre, type, description) : hors cache, // pour refléter tout de suite une section ajoutée ou modifiée. if (class_exists('AIBUI_Sections')) { $context['userSections'] = AIBUI_Sections::for_ai(); } return $context; } public static function get_custom_instructions() { return (string) get_option(self::CUSTOM_INSTRUCTIONS_OPTION, ''); } public static function sanitize_custom_instructions($value) { $value = sanitize_textarea_field((string) $value); return mb_substr($value, 0, self::CUSTOM_INSTRUCTIONS_MAX_CHARS); } /** * AJAX : enregistrement des « Instructions pour l'IA » (page Réglages). */ public static function ajax_save_custom_instructions() { check_ajax_referer('aibui_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error('Insufficient permissions', 403); } $value = isset($_POST['custom_instructions']) ? wp_unslash($_POST['custom_instructions']) : ''; update_option(self::CUSTOM_INSTRUCTIONS_OPTION, self::sanitize_custom_instructions($value), false); wp_send_json_success(); } private static function build() { $theme = wp_get_theme(); $settings = function_exists('wp_get_global_settings') ? wp_get_global_settings() : array(); $styles = function_exists('wp_get_global_styles') ? wp_get_global_styles() : array(); $settings = is_array($settings) ? $settings : array(); $styles = is_array($styles) ? $styles : array(); $palette = self::presets($settings, array('color', 'palette'), array('slug', 'name', 'color')); $gradients = self::presets($settings, array('color', 'gradients'), array('slug', 'name', 'gradient')); $font_families = self::presets($settings, array('typography', 'fontFamilies'), array('slug', 'name', 'fontFamily')); $font_sizes = self::presets($settings, array('typography', 'fontSizes'), array('slug', 'name', 'size')); $spacing_sizes = self::presets($settings, array('spacing', 'spacingSizes'), array('slug', 'name', 'size')); $lookup = array( 'color' => self::index($palette, 'color'), 'gradient' => self::index($gradients, 'gradient'), 'font-family' => self::index($font_families, 'fontFamily'), 'font-size' => self::index($font_sizes, 'size'), 'spacing' => self::index($spacing_sizes, 'size'), ); $body_font = self::resolve(self::dig($styles, array('typography', 'fontFamily')), $lookup); $heading_font = self::resolve(self::dig($styles, array('elements', 'heading', 'typography', 'fontFamily')), $lookup); return array( 'name' => (string) $theme->get('Name'), 'isBlockTheme' => function_exists('wp_is_block_theme') ? (bool) wp_is_block_theme() : false, 'locale' => get_locale(), 'background' => self::resolve(self::dig($styles, array('color', 'background')), $lookup), 'text' => self::resolve(self::dig($styles, array('color', 'text')), $lookup), 'contentSize' => (string) self::dig($settings, array('layout', 'contentSize')), 'wideSize' => (string) self::dig($settings, array('layout', 'wideSize')), // Sans police propre, les titres héritent de celle du texte. 'headingFont' => $heading_font !== '' ? $heading_font : $body_font, 'bodyFont' => $body_font, 'palette' => $palette, 'gradients' => $gradients, 'fontFamilies' => $font_families, 'fontSizes' => $font_sizes, 'spacingSizes' => $spacing_sizes, 'patterns' => self::patterns(), ); } private static function dig($array, array $path) { foreach ($path as $key) { if (!is_array($array) || !array_key_exists($key, $array)) { return ''; } $array = $array[$key]; } return $array; } /** * Presets d'un réglage, sources « theme » puis « custom » fusionnées (un * slug custom remplace celui du thème), sans les valeurs « default » de core. */ private static function presets(array $settings, array $path, array $fields) { $node = self::dig($settings, $path); if (!is_array($node)) { return array(); } // Réglage déjà à plat (pas de clés d'origine) : on le prend tel quel. $by_origin = array_intersect_key($node, array_flip(array('default', 'theme', 'custom'))); $sources = $by_origin ? array($node['theme'] ?? array(), $node['custom'] ?? array()) : array($node); $merged = array(); foreach ($sources as $items) { if (!is_array($items)) { continue; } foreach ($items as $item) { if (!is_array($item) || empty($item['slug'])) { continue; } $entry = array(); foreach ($fields as $field) { $value = $item[$field] ?? ''; $entry[$field] = is_scalar($value) ? (string) $value : ''; } $merged[$item['slug']] = $entry; } } return array_values($merged); } private static function index(array $presets, $value_field) { $map = array(); foreach ($presets as $preset) { $map[$preset['slug']] = $preset[$value_field]; } return $map; } /** * Remplace une référence de preset par sa valeur : * "var:preset|color|base" ou "var(--wp--preset--color--base)" → "#ffffff". */ private static function resolve($value, array $lookup) { if (!is_string($value) || $value === '') { return ''; } if (preg_match('/^var:preset\|([a-z-]+)\|([a-z0-9_-]+)$/i', $value, $m) || preg_match('/^var\(--wp--preset--([a-z-]+)--([a-z0-9_-]+)\)$/i', $value, $m)) { $type = strtolower($m[1]); $slug = $m[2]; if (isset($lookup[$type][$slug]) && $lookup[$type][$slug] !== '') { return $lookup[$type][$slug]; } } return $value; } /** * Patterns enregistrés, hors core/ : ceux du thème d'abord. Le contenu * n'est joint qu'aux premiers patterns du thème, tronqué. */ private static function patterns() { if (!class_exists('WP_Block_Patterns_Registry')) { return array(); } $all = WP_Block_Patterns_Registry::get_instance()->get_all_registered(); if (!is_array($all)) { return array(); } $theme_prefixes = array_unique(array(get_stylesheet() . '/', get_template() . '/')); $theme_dirs = array_unique(array(get_stylesheet_directory(), get_template_directory())); $theme_patterns = array(); $other_patterns = array(); foreach ($all as $pattern) { $name = isset($pattern['name']) ? (string) $pattern['name'] : ''; if ($name === '' || strpos($name, 'core/') === 0) { continue; } $is_theme = false; foreach ($theme_prefixes as $prefix) { if (strpos($name, $prefix) === 0) { $is_theme = true; } } if (!$is_theme && !empty($pattern['filePath'])) { foreach ($theme_dirs as $dir) { if (strpos((string) $pattern['filePath'], $dir) === 0) { $is_theme = true; } } } if ($is_theme) { $theme_patterns[] = $pattern; } else { $other_patterns[] = $pattern; } } $result = array(); $with_content = 0; foreach (array_merge($theme_patterns, $other_patterns) as $index => $pattern) { if (count($result) >= self::MAX_PATTERNS) { break; } $entry = array( 'name' => (string) $pattern['name'], 'title' => isset($pattern['title']) ? (string) $pattern['title'] : '', ); $is_theme = $index < count($theme_patterns); if ($is_theme && $with_content < self::PATTERN_CONTENT_COUNT && !empty($pattern['content'])) { $entry['content'] = mb_substr((string) $pattern['content'], 0, self::PATTERN_CONTENT_MAX_CHARS); $with_content++; } $result[] = $entry; } return $result; } } AIBUI_Theme_Context::init();