# ai-builder/trunk/includes/class-js-handler.php

AI Builder – Generate pages, blocks, images &amp; translate with AI, version trunk. 62 lines.

- Page: https://pluginprobe.com/plugins/ai-builder/trunk/code/includes/class-js-handler.php
- Raw: https://pluginprobe.com/plugins/ai-builder/trunk/raw/includes/class-js-handler.php
- Modified: 2026-09-08T10:53:48+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ai-builder/trunk/code/includes/class-js-handler.php#L10-L20`.

```php
<?php

if (!defined('ABSPATH'))
    exit; // Exit if accessed directly

class AIBUI_JS_Handler
{
    /**
     * Clés de post meta contenant du JavaScript ré-émis tel quel sur le front.
     */
    const JS_META_KEYS = array(
        'ai_builder_page_js_content',
        'ai_builder_block_js_content',
        'ai_builder_js_content',
    );

    public function __construct()
    {
        // Ajouter le JS personnalisé sur le frontend
        add_action('wp_footer', array($this, 'add_custom_js'));

        // Marquer les meta JS comme protégées : elles ne peuvent alors plus être
        // écrites via la boîte « Champs personnalisés » de l'éditeur ni via XML-RPC
        // par un utilisateur qui a seulement edit_post. Le seul point d'écriture
        // reste le handler AJAX, gardé par unfiltered_html.
        add_filter('is_protected_meta', array($this, 'protect_js_meta'), 10, 3);
    }

    /**
     * @param bool   $protected
     * @param string $meta_key
     * @param string $meta_type
     * @return bool
     */
    public function protect_js_meta($protected, $meta_key, $meta_type)
    {
        if ($meta_type === 'post' && in_array($meta_key, self::JS_META_KEYS, true)) {
            return true;
        }
        return $protected;
    }

    public function add_custom_js()
    {
        // Récupérer l'ID du post actuel
        $post_id = get_the_ID();

        if (!$post_id) {
            return;
        }

        // Récupérer le JS personnalisé depuis les meta du post
        $js_content = get_post_meta($post_id, 'ai_builder_js_content', true);

        if (!empty($js_content)) {
            echo '<script id="ai-builder-frontend-js" type="text/javascript">' . $js_content . '</script>';
        }
    }
}

new AIBUI_JS_Handler();

```
