Files
AsterionWP2026/tools/av-fix-link-types.php
j.foucher d2badf49d4 feat(bricks+wpml): full programmatic page generation + native WPML CTE integration
Sessions 3-4 deliverables. Establishes the Asterion content pipeline :
EN pages generated programmatically from content-source/*-data.php, FR
translations rebuilt natively by Bricks's WPML integration from EN structure
+ WPML strings (no direct FR _bricks_page_content_2 writes).

## tools/_av-bricks-shared.php (the library)
Shared helpers + element atoms (eyebrow/heading/text/button/text-link) +
section builders (hero/prose/list/cards/cta/upsell/closer) + page write
pipeline that handles : write EN content via $wpdb (bypass Bricks's filter),
force Bricks::Elements::load_elements() so controls are populated, fire
wpml_page_builder_register_strings, push dict FR into icl_string_translations
(skip user CTE edits, auto-prefix /fr on internal URLs), trigger Bricks
native FR rebuild via wpml_page_builder_string_translated, and ensure a CTE
job exists (icl_translate_job.editor='wpml' + revision=NULL + populate
icl_translate rows with base64+gzip-encoded EN/FR strings).

## tools/av-generate-{industries,solutions}.php
Thin wrappers : URL trigger (template_redirect, not init — Bricks elements
load on 'wp' hook), add_filter('av_fr_translation_dict', ...) to register
FR strings, content-type-specific hub builder. ~150 FR strings per type.

## tools/av-translate-{industries,solutions}-fr.php
One-shot seeds for the initial FR linked posts via WPML trid mapping.

## tools/av-wpml-* operational mu-plugins
- set-cte : switch doc_translation_method to ICL_TM_TMETHOD_EDITOR (=1)
- create-jobs : reset CTE jobs (status=10 + needs_update=1 + editor=wpml +
  revision=NULL). Integrated into pipeline via av_ensure_wpml_cte_job().
- fix-status / repair : align FR posts to correct trid + cleanup orphans.
- push-translations : push dict into icl_string_translations.
- plugins-check / debug / dump-strings / icl-dump / rescan : diagnostics.

## tools/av-fix-link-types.php
One-shot DB migration : convert legacy link.type='internal'+url to 'external'
across all _bricks_page_*_2 meta values (JSON and PHP-serialized). Bricks's
set_link_attributes() only renders href= for type='external' — older session-3
templates lost their href silently.

## Other tools/ mu-plugins
- av-bricks-rest-bridge : pivot — exposes Bricks CPTs over REST, dual-format
  read filter (JSON for MCP / array for renderer), auto-mirror content→header/
  footer for templates.
- av-apply-bricks-styles, av-cleanup-orphans, av-create-wp-menu, av-debug-meta,
  av-dump-bricks, av-fix-template-meta : misc operational scripts.

## Bricks Config/
JSON snapshots of bricks_theme_styles, bricks_color_palette,
bricks_global_settings for diffable inspection.

## .gitignore
Add .mcp.json — holds per-developer WP App Password for the Bricks MCP server.

## wp-content/themes/asterion-bricks/assets/css/utilities.css
Note that all responsive overrides are now emitted natively by the page
generator via Bricks per-breakpoint settings, no custom CSS needed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 19:40:28 +02:00

82 lines
3.2 KiB
PHP

<?php
/**
* Plugin Name: Asterion — Migrate Bricks links from type:'internal'+url to type:'external'+url
* Description: /?av_fix_link_types=1
* Bricks's set_link_attributes() in base.php expects 'internal' to come with a numeric
* 'postId' (it ignores 'url' when type='internal'), while 'external' uses the 'url'
* string verbatim. Older session-3 templates were built with the wrong shape and lost
* their href at render time. This one-shot scans _bricks_page_content_2,
* _bricks_page_header_2, _bricks_page_footer_2 across all posts (pages + bricks_template),
* rewrites every link block where type==='internal' && isset(url), and writes back.
*
* Idempotent : already-correct links are skipped. Safe to re-run.
*/
defined('ABSPATH') || exit;
add_action('init', function () {
if (empty($_GET['av_fix_link_types'])) return;
if (! in_array($_SERVER['SERVER_NAME'] ?? '', ['localhost', '127.0.0.1'], true)) wp_die('localhost only');
global $wpdb;
header('Content-Type: text/plain; charset=utf-8');
$keys = ['_bricks_page_content_2', '_bricks_page_header_2', '_bricks_page_footer_2'];
$rows = $wpdb->get_results(
"SELECT meta_id, post_id, meta_key, meta_value
FROM {$wpdb->postmeta}
WHERE meta_key IN ('" . implode("','", $keys) . "')"
);
$patched = 0;
$touched_posts = 0;
foreach ($rows as $row) {
$val = $row->meta_value;
if (! is_string($val) || $val === '') continue;
$is_json = ($val[0] === '[');
$elements = null;
if ($is_json) {
$elements = json_decode($val, true);
} else {
// Mirror keys (_bricks_page_header_2, _bricks_page_footer_2) are stored as
// PHP-serialized arrays by av-bricks-rest-bridge → maybe_unserialize.
$maybe = @unserialize($val);
if (is_array($maybe)) $elements = $maybe;
}
if (! is_array($elements)) continue;
$count = 0;
av_walk_fix_links($elements, $count);
if ($count === 0) continue;
// Write back in same format as it was stored
$new = $is_json ? wp_json_encode($elements) : maybe_serialize($elements);
$wpdb->update($wpdb->postmeta, ['meta_value' => $new], ['meta_id' => $row->meta_id], ['%s'], ['%d']);
wp_cache_delete($row->post_id, 'post_meta');
$patched += $count;
$touched_posts++;
$fmt = $is_json ? 'json' : 'serialized';
echo "post #{$row->post_id} ({$row->meta_key}, $fmt) : $count link(s) patched\n";
}
echo "\nTotal : $patched links across $touched_posts post(s) patched (type internal\u{2192}external).\n";
exit;
});
function av_walk_fix_links(&$elements, &$count) {
$walk = function (&$node) use (&$walk, &$count) {
if (! is_array($node)) return;
// Link block shape : has type and url, type wrongly set to 'internal' with a non-numeric url
if (isset($node['type']) && $node['type'] === 'internal' && isset($node['url']) && is_string($node['url']) && $node['url'] !== '') {
$node['type'] = 'external';
$count++;
}
foreach ($node as &$value) {
if (is_array($value)) $walk($value);
}
};
foreach ($elements as &$el) $walk($el);
}