$value) { if (isset($allowedKeys[$key])) { $result[$key] = $value; } elseif (is_array($value)) { // Recursively filter nested arrays $filtered = self::filterArrayByProperties($value, $allowedKeys); if (!empty($filtered)) { $result[$key] = $filtered; } } } return $result; } /** * Validates if a variation contains only specified properties. * * This function checks whether the variation array contains exclusively the * specified properties throughout its entire hierarchy. * * @param array $variation The theme variation arrays to validate * @param array $allowedKeys List of property names that should be the only ones present * @return bool TRUE if only specified properties exist, FALSE otherwise */ protected static function variationHasProperties(array $variation, array $allowedKeys) { if (empty($variation) || empty($allowedKeys)) { return false; } $allowedKeys = array_flip($allowedKeys); $data = array_diff_key($variation, array_flip(self::$ignoredKeys)); $filtered = self::filterArrayByProperties($data, $allowedKeys); return serialize($filtered) === serialize($data); } /** * Get the CSS for each variation. * * @param array $variations The theme variations to process. * @param \WP_Theme_JSON $current The current theme JSON data. * @param bool $includeLayoutStyles Whether to include layout styles in the CSS. * @return array The variations with their corresponding CSS. */ protected static function getCss($variations, $current, $includeLayoutStyles) { $deduped = []; foreach ($variations as $variation) { $title = $variation['title'] ?? null; if (!$title || isset($deduped[$title])) { continue; } $theme = new \WP_Theme_JSON(); $theme->merge($current); $theme->merge(new \WP_Theme_JSON($variation)); $css = $theme->get_stylesheet( ["variables", "styles", "presets"], null, ["skip_root_layout_styles" => !$includeLayoutStyles, 'include_block_style_variations' => true] ); $variation['css'] = $css; // to make sure we exit early $deduped[$title] = $variation; } return array_values($deduped); } /** * Get Theme Variations and the compiled CSS for each variation. * * @param \WP_REST_Request $request The REST API request object. * @return \WP_REST_Response */ public static function getVariations($request) { $includeLayoutStyles = $request->has_param('includeLayoutStyles'); $current = \WP_Theme_JSON_Resolver::get_merged_data(); $unfiltered = \WP_Theme_JSON_Resolver::get_style_variations(); $variations = array_filter($unfiltered, function ($variation) { return self::variationHasProperties($variation, ['color']); }); $buildSlugMap = function ($unfiltered) { $slugMap = []; if (!is_array($unfiltered)) { return $slugMap; } foreach ($unfiltered as $rawSlug => $rawVariation) { $title = is_array($rawVariation) ? ($rawVariation['title'] ?? null) : null; $slug = is_array($rawVariation) ? ($rawVariation['slug'] ?? (is_string($rawSlug) ? $rawSlug : null)) : null; if ($title && $slug && !isset($slugMap[$title])) { $slugMap[$title] = $slug; } } return $slugMap; }; $slugMap = $buildSlugMap($unfiltered); array_walk($variations, function (&$variation) use ($slugMap) { if (!is_array($variation) || isset($variation['slug'])) { return; } $title = $variation['title'] ?? null; if ($title && isset($slugMap[$title])) { $variation['slug'] = $slugMap[$title]; } }); $deduped = static::getCss($variations, $current, $includeLayoutStyles); // if the theme is extendable we need to filter the variations using the allowed variations list if (\get_option('stylesheet') === 'extendable') { $deduped = array_filter($deduped, function ($variation) { return in_array($variation['slug'], self::$allowedVariationsList); }); } return new \WP_REST_Response(array_values($deduped)); } /** * Get Theme fonts Variations and the compiled CSS for each variation. * * @param \WP_REST_Request $request The REST API request object. * @return \WP_REST_Response */ public static function getFontsVariations($request) { $includeLayoutStyles = $request->has_param('includeLayoutStyles'); $current = \WP_Theme_JSON_Resolver::get_merged_data(); $unfiltered = \WP_Theme_JSON_Resolver::get_style_variations(); $fontsVariations = array_filter($unfiltered, function ($variation) { return self::variationHasProperties($variation, ['elements', 'typography']); }); $processedFonts = array_map(function ($variation) { if (!isset($variation['styles']['elements']) || !is_array($variation['styles']['elements'])) { return $variation; } $variation['styles']['elements'] = array_map( [self::class, 'normalizeElementTypography'], $variation['styles']['elements'] ); if (!isset($variation['styles']['typography'])) { $variation['styles']['typography'] = [ 'fontFamily' => 'var(--wp--preset--font-family--inter)' ]; } // Removing the settings that cause the style to change. unset($variation['settings']); return $variation; }, $fontsVariations); $deduped = static::getCss($processedFonts, $current, $includeLayoutStyles); return new \WP_REST_Response($deduped); } /** * Get block style variations (vibes) from merged global styles * * @param \WP_REST_Request $request The request. * @return \WP_REST_Response */ public static function getBlockStyleVariations($request) { // Get theme + DB merged Global Styles $merged = wp_get_global_styles(); $blocks = $merged['blocks'] ?? []; $variations = []; foreach ($blocks as $blockName => $blockData) { if (!isset($blockData['variations'])) { continue; } $variations[$blockName] = $blockData['variations']; } return new \WP_REST_Response($variations, 200); } /** * Normalize typography properties for theme element styles. * * @param array $elementStyles The element styles array containing typography configuration * @return array Normalized typography properties with filtered null values */ protected static function normalizeElementTypography(array $elementStyles) { $typography = $elementStyles['typography'] ?? []; return [ 'typography' => array_filter([ 'fontFamily' => $typography['fontFamily'] ?? null, 'fontSize' => $typography['fontSize'] ?? null, 'lineHeight' => $typography['lineHeight'] ?? null, 'letterSpacing' => $typography['letterSpacing'] ?? null, 'fontStyle' => $typography['fontStyle'] ?? null, 'fontWeight' => $typography['fontWeight'] ?? null, 'textTransform' => $typography['textTransform'] ?? 'none', ], function ($v) { return $v !== null; }) ]; } /** * Get the HTML of a specific tagged block code * * @param \WP_REST_Request $request The REST API request object. * @return \WP_REST_Response */ public static function getBlockCode(\WP_REST_Request $request) { $blockId = (int) $request->get_param('blockId'); if ($blockId < 1) { return new \WP_REST_Response(['error' => 'Invalid blockId'], 400); } // A template-part source resolves the part and walks it with the shared // preorder finder, instead of the post path below. $partSlug = (string) $request->get_param('partSlug'); if ($partSlug !== '') { return self::getTemplatePartBlockCode($partSlug, $blockId); } $postId = (int) $request->get_param('postId'); $post = \get_post($postId); if (!$post) { return new \WP_REST_Response(['error' => 'Post not found'], 404); } $found = \Extendify\Agent\PostBlockFinder::find(parse_blocks($post->post_content), $blockId); if (!$found || empty($found['block']['blockName'])) { return new \WP_REST_Response(['error' => 'Block id not found in this post'], 404); } $block = $found['block']; return new \WP_REST_Response([ 'postId' => $postId, 'blockId' => $blockId, 'name' => $block['blockName'], 'attrs' => $block['attrs'] ?? (object)[], 'block' => serialize_blocks([$block]), ], 200); } // Resolves the part the same way SaveController does — active-theme-scoped // get_block_template, not a raw get_posts by name — then walks it with the // shared preorder finder so the blockId lands on the block save will write. private static function getTemplatePartBlockCode(string $slug, int $blockId) { $stylesheet = wp_get_theme()->get_stylesheet(); $template = get_block_template("{$stylesheet}//{$slug}", 'wp_template_part'); if (!$template || empty($template->wp_id)) { return new \WP_REST_Response(['error' => 'Template part not found'], 404); } $post = \get_post($template->wp_id); if (!$post) { return new \WP_REST_Response(['error' => 'Template part not found'], 404); } $found = \Extendify\Agent\TemplatePartBlockFinder::find( parse_blocks($post->post_content), $blockId ); if (!is_array($found) || empty($found['block']['blockName'])) { return new \WP_REST_Response(['error' => 'Block id not found in this template part'], 404); } $block = $found['block']; return new \WP_REST_Response([ 'partSlug' => $slug, 'blockId' => $blockId, 'name' => $block['blockName'], 'attrs' => $block['attrs'] ?? (object)[], 'block' => serialize_blocks([$block]), ], 200); } /** * Get the rendered HTML of some block code * * @param \WP_REST_Request $request The REST API request object. * @return \WP_REST_Response */ public static function getBlockHtml($request) { $blockCode = $request->get_param('blockCode'); $content = \do_blocks($blockCode); // Layout supports register per-container CSS for a page-side enqueue // that never happens on a REST fragment — ship it with the markup. $styles = function_exists('wp_style_engine_get_stylesheet_from_context') ? \wp_style_engine_get_stylesheet_from_context('block-supports') : ''; return new \WP_REST_Response(['content' => trim($content), 'styles' => $styles]); } /** * Get the Hero Patterns from the API * * @param string $title The title to replace in the pattern code * @param string $description The description to replace in the pattern code * @param array $images The images to replace in the pattern code, as an array of urls * @param array $cta The cta to replace in the pattern code, as an array with 'label' and 'link' keys * @param bool $featuredOnly Whether to limit to featured patterns * @param string|null $source What triggered the request, e.g. 'change-site-design-workflow' * @return array|\WP_Error|array The hero patterns data or a WP_Error on failure */ protected static function getHeroPatternsData( $title, $description, $images, $cta, $featuredOnly = false, $source = null ) { $response = \wp_remote_post( Constants::PATTERNS_HOST . '/api/heros', [ 'headers' => [ 'Content-Type' => 'application/json', 'Accept' => 'application/json', ], 'body' => wp_json_encode([ "wpVersion" => \get_bloginfo('version'), "wpLanguage" => \get_locale(), "featured" => $featuredOnly, "source" => $source, ]) ] ); if (\is_wp_error($response)) { return $response; } $body = json_decode(\wp_remote_retrieve_body($response), true); $cursor = 0; $imageCount = count($images); $heroPatterns = []; foreach ($body as $heroPattern) { $code = $heroPattern['code'] ?? ''; if ($title) { $code = preg_replace( '/([\s\S]*?]*>)[\s\S]*?(<\/h1>[\s\S]*?)/m', '${1}' . esc_html($title) . '${2}', $code, 1 ); } if ($description) { $code = preg_replace( '/([\s\S]*?]*>)[\s\S]*?(<\/p>[\s\S]*?)/m', '${1}' . esc_html($description) . '${2}', $code, 1 ); } if ($cta['label'] ?? null) { $code = preg_replace( '/([\s\S]*?]*>)[\s\S]*?(<\/a>[\s\S]*?)/m', '${1}' . esc_html($cta['label']) . '${2}', $code, 1 ); } if ($cta['link'] ?? null) { $code = preg_replace( '/(