Dynamic Template Tags

/*
 * tct_template_tags
 *
 * @function Automatically calls template tags
 * @author Taylor Drayson
 * @since 26/03/2023
 * @updated 15/11/2023
 */
add_filter("render_block", "tct_template_tags", 10, 2);
function tct_template_tags($block_content, $block){
    $prefixes = ["fn", "query", "theme", "field", "user", "userm", "author", "hook"];
    $pattern = "/(?:http:\/\/)?{{(" . implode("|", $prefixes) . ")\.([\w-]+)}}/";

    $new_content = preg_replace_callback($pattern, function ($matches) {
        $dynamic_tag = trim($matches[2]);

        switch ($matches[1]) {
            case "fn":
                if (!function_exists($dynamic_tag)) {
                    return;
                }
                return call_user_func($dynamic_tag);
            case "query": // Query param
                return isset($_GET[$dynamic_tag])
                    ? sanitize_text_field($_GET[$dynamic_tag])
                    : "";
            case "theme": // Options page
                if (!function_exists("get_field")) break;
                return get_field($dynamic_tag, "options");
            case "field": // ACF Field
                if (!function_exists("get_field")) break;
                return get_field($dynamic_tag);
            case "user": // Supported user info https://tct.so/User-info
                $current_user = wp_get_current_user();
                return $current_user->$dynamic_tag;
            case "userm": // User meta
                $user_id = get_current_user_id();
                return get_user_meta($user_id, $dynamic_tag, true);
            case "author": // Supported author info https://tct.so/author-values
                return get_the_author_meta($dynamic_tag);
            case "hook":
                ob_start();
                do_action($dynamic_tag);
                return ob_get_clean();
        }

        // Otherwise, return the original text
        return $matches[0];
    }, $block_content);

    return $new_content;
}