Files
AsterionWP2026/wp-content/themes/asterion-bricks/functions.php
j.foucher f18a57e83f feat(wpml): refactor to post_content + duplicate site in French via WPML
This commit makes the site editable from WP admin (Gutenberg or Bricks
"Edit with Bricks") and translatable via WPML Translation Editor by
shifting page rendering away from Custom Templates and into post_content.

Architecture change
-------------------
Before: Custom Templates assigned to each page (page-solution-detail.php
etc.) generated the page HTML at runtime by reading from inc/*-data.php
arrays. WPML had nothing to translate, Bricks Builder had nothing to
edit, the client could only modify content by editing PHP.

After:
- Pages now hold their full Gutenberg HTML inside post_content
- The default page.php template renders the_content() — no Custom
  Templates assigned anymore
- Bricks Builder picks up post_content and converts it to its element
  tree when "Edit with Bricks" is clicked
- WPML Translation Editor exposes every block for translation
- The client can edit pages directly in WP admin

What this commit ships
----------------------
- inc/render-blocks.php: data array → Gutenberg HTML payload generator
  with helpers per section type (prose / cards / list / cta) and per
  page archetype (solution / industry / technology / editorial / form
  / overview hub).
- inc/shortcodes.php: [av_form slug="request-demo"] for dynamic form
  blocks (forms can't live as static HTML — they need nonces and
  per-request state).
- inc/i18n.php: av_lang() / av_t() helpers for inline string translation
  in header.php and footer.php (covers ~80 UI strings); switches based
  on WPML's wpml_current_language filter.
- page.php: minimal default template — get_header() + the_content() +
  get_footer().
- header.php: real WPML language switcher driven by
  wpml_active_languages filter (replaces the placeholder EN · FR
  toggle); UI strings now route through av_t().
- footer.php: same UI string treatment.
- Custom Templates retained but no longer assigned (pages were stripped
  by the migration mu-plugin); they remain available if a page wants
  to opt back into the data-file pattern.

Operational migrations (one-shot mu-plugins, not versioned)
-----------------------------------------------------------
1. asterion-migrate-to-blocks.php walked all 26 PHP-templated pages,
   pre-rendered them via the new render-blocks helpers, wrote the
   result to post_content, and stripped _wp_page_template.
2. asterion-duplicate-fr.php cloned all 35 pages into French via WPML
   API, preserved parent/child hierarchy, linked translations through
   the trid table, marked them as duplicates pending refinement.
3. asterion-fix-fr-slugs.php aligned French slugs with English ones
   (WP's wp_unique_post_slug() had appended -2 suffixes; we bypass
   via $wpdb->update so /fr/solutions/ resolves correctly).

Verified
--------
- All 35 EN URLs return HTTP 200
- All 35 FR URLs return HTTP 200 under /fr/<slug>/
- Language switcher in header outputs correct hreflang links
- WPML emits hreflang en / fr / x-default in <head>
- Body class confirms wp-child-theme-asterion-bricks active
- Bricks frontend CSS still enqueued
- Inter Variable still preloaded

Next steps
----------
- French content is currently the duplicated EN copy. Translate via
  WP admin → WPML → Translation Editor (per-block UI), or edit pages
  directly in /fr/<slug>/ via Gutenberg / Bricks
- WPML String Translation can scan the theme to surface av_t() / __()
  strings for UI-level translation overrides

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 13:11:42 +02:00

162 lines
4.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Asterion Bricks — child theme bootstrap
*
* Loads design tokens, utilities, components, and main JS in a deterministic
* order. Registers theme support, image sizes, and includes modular pieces
* (custom post types, SEO schemas).
*
* @package AsterionBricks
* @since 0.1.0
*/
defined('ABSPATH') || exit;
define('AV_THEME_VERSION', '0.1.0');
define('AV_THEME_DIR', get_stylesheet_directory());
define('AV_THEME_URI', get_stylesheet_directory_uri());
/**
* Enqueue child theme styles and scripts.
*
* Bricks parent stylesheet is loaded by the parent theme itself — no need to
* re-enqueue. We layer our tokens → utilities → components on top.
*
* Cache busting via `filemtime()` so dev changes are picked up without manual
* version bumps. In production, swap to AV_THEME_VERSION + a build step.
*/
add_action('wp_enqueue_scripts', function () {
$css_dir = AV_THEME_DIR . '/assets/css';
$css_uri = AV_THEME_URI . '/assets/css';
$js_dir = AV_THEME_DIR . '/assets/js';
$js_uri = AV_THEME_URI . '/assets/js';
if (file_exists($css_dir . '/tokens.css')) {
wp_enqueue_style(
'av-tokens',
$css_uri . '/tokens.css',
[],
filemtime($css_dir . '/tokens.css')
);
}
if (file_exists($css_dir . '/utilities.css')) {
wp_enqueue_style(
'av-utilities',
$css_uri . '/utilities.css',
['av-tokens'],
filemtime($css_dir . '/utilities.css')
);
}
if (file_exists($css_dir . '/components.css')) {
wp_enqueue_style(
'av-components',
$css_uri . '/components.css',
['av-tokens', 'av-utilities'],
filemtime($css_dir . '/components.css')
);
}
if (file_exists($js_dir . '/main.js')) {
wp_enqueue_script(
'av-main',
$js_uri . '/main.js',
[],
filemtime($js_dir . '/main.js'),
['strategy' => 'defer', 'in_footer' => true]
);
}
}, 20);
/**
* Preload self-hosted Inter fonts to reduce LCP.
*
* Only emits <link rel="preload"> for fonts that actually exist on disk —
* silent during early dev when the WOFF2 files have not been dropped yet.
*/
add_action('wp_head', function () {
$fonts = [
'Inter-Variable.woff2',
// 'InterDisplay-Variable.woff2',
];
foreach ($fonts as $font) {
$path = AV_THEME_DIR . '/assets/fonts/' . $font;
if (file_exists($path)) {
printf(
'<link rel="preload" href="%s/assets/fonts/%s" as="font" type="font/woff2" crossorigin>' . "\n",
esc_url(AV_THEME_URI),
esc_attr($font)
);
}
}
}, 1);
/**
* Modular includes.
*
* Each file is opt-in via file_exists() so partial scaffolds don't fatal.
*/
$av_includes = [
'inc/i18n.php', // av_lang(), av_t() — load FIRST.
'inc/cpt.php', // Custom post types: case_study, scenario.
'inc/seo.php', // Schema.org JSON-LD overrides.
'inc/form-handler.php', // Conversion form POST handler.
'inc/render-blocks.php', // Data array → Gutenberg HTML for the migrator.
'inc/shortcodes.php', // [av_form slug="..."] etc.
];
foreach ($av_includes as $av_include) {
$path = AV_THEME_DIR . '/' . $av_include;
if (file_exists($path)) {
require_once $path;
}
}
unset($av_include, $path);
/**
* Theme support and custom image sizes.
*
* Image sizes per Design Handoff section 8.3:
* - hero 2400×1350 (16:9)
* - card 800×600 (4:3)
* - thumb 400×300 (4:3)
*/
add_action('after_setup_theme', function () {
add_theme_support('post-thumbnails');
add_theme_support('html5', [
'search-form', 'comment-form', 'comment-list',
'gallery', 'caption', 'style', 'script',
]);
add_theme_support('responsive-embeds');
add_theme_support('align-wide');
add_theme_support('editor-styles');
add_image_size('av-hero', 2400, 1350, true);
add_image_size('av-card', 800, 600, true);
add_image_size('av-thumb', 400, 300, true);
load_child_theme_textdomain('asterion-bricks', AV_THEME_DIR . '/languages');
});
/**
* Hide the WP admin bar on frontend for non-admins (cleaner preview screenshots).
*/
add_action('after_setup_theme', function () {
if (! current_user_can('administrator')) {
show_admin_bar(false);
}
});
/**
* Disable Gutenberg block library default styles when Bricks is active —
* we own typography end-to-end through tokens.css.
*/
add_action('wp_enqueue_scripts', function () {
wp_dequeue_style('wp-block-library');
wp_dequeue_style('wp-block-library-theme');
wp_dequeue_style('global-styles');
wp_dequeue_style('classic-theme-styles');
}, 100);