2
0

🐛 (wordpress) Fix admin critical bug and better lib import

This commit is contained in:
Baptiste Arnaud
2023-02-25 17:13:26 +01:00
parent 2b2b1c3d6d
commit c889f302f6
18 changed files with 177 additions and 127 deletions

View File

@@ -4,55 +4,78 @@ class Typebot_Public
{
public function add_head_code()
{
function add_module_type($tag, $handle)
function parse_wp_user()
{
if ('typebot' !== $handle) {
return $tag;
}
$tag = str_replace(
'<script',
'<script type ="module"',
$tag
);
return $tag;
}
wp_enqueue_script('typebot', 'whatever.js');
add_filter('script_loader_tag', 'add_module_type', 10, 2);
wp_add_inline_script('typebot', $this->parse_wp_user());
if (get_option('init_snippet') && get_option('init_snippet') !== '') {
wp_add_inline_script('typebot', get_option('init_snippet'));
wp_add_inline_script('typebot', 'Typebot.setPrefilledVariables({ typebotWpUser });');
}
}
private function parse_wp_user()
{
$wp_user = wp_get_current_user();
return 'if(typeof window.typebotWpUser === "undefined"){
$wp_user = wp_get_current_user();
echo '<script>
if(typeof window.typebotWpUser === "undefined"){
window.typebotWpUser = {
wp_id:"' .
$wp_user->ID .
'",
$wp_user->ID .
'",
wp_username:"' .
$wp_user->user_login .
'",
$wp_user->user_login .
'",
wp_email:"' .
$wp_user->user_email .
'",
$wp_user->user_email .
'",
wp_first_name:"' .
$wp_user->user_firstname .
'",
$wp_user->user_firstname .
'",
wp_last_name:"' .
$wp_user->user_lastname .
'",
$wp_user->user_lastname .
'",
}
}';
}
</script>';
}
function typebot_script()
{
echo '<script type="module">
import Typebot from "https://cdn.jsdelivr.net/npm/@typebot.io/js@0.0/dist/web.js";';
if (
get_option('excluded_pages') !== null &&
get_option('excluded_pages') !== ''
) {
$paths = explode(',', get_option('excluded_pages'));
$arr_js = 'const typebotExcludePaths = [';
foreach ($paths as $path) {
$arr_js = $arr_js . '"' . $path . '",';
}
$arr_js = substr($arr_js, 0, -1) . '];';
echo $arr_js;
}
if (get_option('init_snippet') && get_option('init_snippet') !== '') {
echo 'if(!typebotExcludePaths || typebotExcludePaths.every((path) => {
let excludePath = path.trim();
let windowPath = window.location.pathname;
if (excludePath.endsWith("*")) {
return !windowPath.startsWith(excludePath.slice(0, -1));
}
if (excludePath.endsWith("/")) {
excludePath = path.slice(0, -1);
}
if (windowPath.endsWith("/")) {
windowPath = windowPath.slice(0, -1);
}
return windowPath !== excludePath;
})) {
' . get_option('init_snippet') . '
Typebot.setPrefilledVariables({ typebotWpUser });
}';
}
echo '</script>';
}
add_action('wp_head', 'parse_wp_user');
add_action('wp_footer', 'typebot_script');
}
public function add_typebot_container($attributes = [])
{
$lib_url = "https://cdn.jsdelivr.net/npm/@typebot.io/js@0.0.14/dist/web.js";
$lib_url = "https://cdn.jsdelivr.net/npm/@typebot.io/js@0.0/dist/web.js";
$width = '100%';
$height = '500px';
if (array_key_exists('width', $attributes)) {