2
0

(wordpress) Add query params exclusion support

This commit is contained in:
Baptiste Arnaud
2023-09-25 11:07:08 +02:00
parent 90cf2e9f81
commit 2307231d28
6 changed files with 82 additions and 53 deletions

View File

@@ -12,6 +12,18 @@ Of course, before using it, you need to create and publish your first typebot.
The code snippet to paste is easily configurable in the Share tab of your bot after clicking on the "Wordpress" button. The code snippet to paste is easily configurable in the Share tab of your bot after clicking on the "Wordpress" button.
## Excluded pages
The excluded pages input is a comma-separated list of pages where you don't want your typebot to appear.
Examples:
- `/app/*` will exclude all pages starting with `/app/`
- `/app` will only exclude the `/app` page
- `/app?param=1` will only exclude the `/app` page **and** with the `param` query parameter set to `1`
- `/app?param=*` will exclude the page at `/app` **and** with the `param` query parameter set to anything
- `/app/*?param=*` will exclude all pages starting with `/app/` **and** with the `param` query parameter set to anything
## Personalize user experience ## Personalize user experience
You can leverage the [prefilled variables](/editor/variables#prefilled-variables) and inject your user information directly into your typebot so that the experience is entirely customized to your user. You can leverage the [prefilled variables](/editor/variables#prefilled-variables) and inject your user information directly into your typebot so that the experience is entirely customized to your user.

View File

@@ -1,13 +1,13 @@
{ {
"name": "@typebot.io/wordpress", "name": "@typebot.io/wordpress",
"version": "3.2.0", "version": "3.3.0",
"main": "index.js", "main": "index.js",
"repository": "https://github.com/baptisteArno/typebot.io", "repository": "https://github.com/baptisteArno/typebot.io",
"author": "baptisteArno", "author": "baptisteArno",
"license": "AGPL-3.0-or-later", "license": "AGPL-3.0-or-later",
"scripts": { "scripts": {
"deploy": "pnpm copy && pnpm commit", "deploy": "pnpm copy && pnpm commit",
"copy": "svn copy ./trunk ./tags/3.2.0", "copy": "svn copy ./trunk ./tags/3.3.0",
"commit": "svn ci -m 'Update embed lib to 0.1'" "commit": "svn ci -m 'Fix loading issue with Gravity Forms'"
} }
} }

View File

@@ -5,7 +5,7 @@ Requires at least: 5.0
Tested up to: 6.0 Tested up to: 6.0
License: GPL 2.0 License: GPL 2.0
License URI: http://www.gnu.org/licenses/gpl-2.0.txt License URI: http://www.gnu.org/licenses/gpl-2.0.txt
Stable Tag: 3.2.0 Stable Tag: 3.3.0
== Description == == Description ==
Collect 4x more responses with conversational apps using Typebot. Collect 4x more responses with conversational apps using Typebot.
@@ -24,6 +24,9 @@ This plugin relies on Typebot which is a tool that allows you to create conversa
3. Activate your Typebot with the "Typebot" admin button located in the sidebar 3. Activate your Typebot with the "Typebot" admin button located in the sidebar
== Changelog == == Changelog ==
= 3.3.0 =
* Fix loading with Gravity Forms and add query params exclusion
= 3.2.0 = = 3.2.0 =
* Update embed lib to 0.1 * Update embed lib to 0.1

View File

@@ -16,7 +16,7 @@
<div style="display: flex; flex-direction: column; margin-top: 1rem"> <div style="display: flex; flex-direction: column; margin-top: 1rem">
<label>Excluded pages (optionnal):</label> <label>Excluded pages (optionnal):</label>
<p style="color: gray">Example: /app/*, /user/*, /admin/settings</p> <p style="color: gray">Example: /app/*, /user/*, /admin/settings, /app?param=*</p>
<input name="excluded_pages" value="<?php echo esc_attr(get_option('excluded_pages')); ?>" style="padding: .5rem" /> <input name="excluded_pages" value="<?php echo esc_attr(get_option('excluded_pages')); ?>" style="padding: .5rem" />
</div> </div>

View File

@@ -1,78 +1,92 @@
<?php <?php
class Typebot_Public class Typebot_Public
{ {
public function add_head_code() public function __construct()
{ {
function parse_wp_user() add_action('wp_head', array($this, 'parse_wp_user'));
{ add_action('wp_footer', array($this, 'typebot_script'));
$wp_user = wp_get_current_user(); }
echo '<script>
public function parse_wp_user()
{
$wp_user = wp_get_current_user();
echo '<script>
if(typeof window.typebotWpUser === "undefined"){ if(typeof window.typebotWpUser === "undefined"){
window.typebotWpUser = { window.typebotWpUser = {
"WP ID":"' . "WP ID":"' .
$wp_user->ID . $wp_user->ID .
'", '",
"WP Username":"' . "WP Username":"' .
$wp_user->user_login . $wp_user->user_login .
'", '",
"WP Email":"' . "WP Email":"' .
$wp_user->user_email . $wp_user->user_email .
'", '",
"WP First name":"' . "WP First name":"' .
$wp_user->user_firstname . $wp_user->user_firstname .
'", '",
"WP Last name":"' . "WP Last name":"' .
$wp_user->user_lastname . $wp_user->user_lastname .
'", '"
} }
} }
</script>'; </script>';
}
public function add_head_code()
{
$this->parse_wp_user();
}
function typebot_script()
{
echo '<script type="module">import Typebot from "https://cdn.jsdelivr.net/npm/@typebot.io/js@0.1/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;
} else {
echo 'const typebotExcludePaths = null;';
} }
function typebot_script() if (get_option('init_snippet') && get_option('init_snippet') !== '') {
{ echo 'if(!typebotExcludePaths || typebotExcludePaths.every((path) => {
echo '<script type="module"> let [excludePath, excludeSearch] = path.trim().split("?");
import Typebot from "https://cdn.jsdelivr.net/npm/@typebot.io/js@0.1/dist/web.js";'; const excludeSearchParams = excludeSearch ? new URLSearchParams(excludeSearch) : null;
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;
} else {
echo 'const typebotExcludePaths = null;';
}
if (get_option('init_snippet') && get_option('init_snippet') !== '') {
echo 'if(!typebotExcludePaths || typebotExcludePaths.every((path) => {
let excludePath = path.trim();
let windowPath = window.location.pathname; let windowPath = window.location.pathname;
let windowSearchParams = window.location.search.length > 0 ? new URLSearchParams(window.location.search) : null;
if (excludePath.endsWith("*")) { if (excludePath.endsWith("*")) {
if(excludeSearchParams){
if(!windowSearchParams) return true
return !windowPath.startsWith(excludePath.slice(0, -1)) || !Array.from(excludeSearchParams.keys()).every((key) => excludeSearchParams.get(key) === "*" || (excludeSearchParams.get(key) === windowSearchParams.get(key)));
}
return !windowPath.startsWith(excludePath.slice(0, -1)); return !windowPath.startsWith(excludePath.slice(0, -1));
} }
if (excludePath.endsWith("/")) { if (excludePath.endsWith("/")) {
excludePath = path.slice(0, -1); excludePath = excludePath.slice(0, -1);
} }
if (windowPath.endsWith("/")) { if (windowPath.endsWith("/")) {
windowPath = windowPath.slice(0, -1); windowPath = windowPath.slice(0, -1);
} }
return windowPath !== excludePath; if(excludeSearchParams){
if(!windowSearchParams) return true
return windowPath !== excludePath || !Array.from(excludeSearchParams.keys()).every((key) => excludeSearchParams.get(key) === "*" || (excludeSearchParams.get(key) === windowSearchParams.get(key)));
} else {
return windowPath !== excludePath;
}
})) { })) {
' . get_option('init_snippet') . ' ' . get_option('init_snippet') . '
Typebot.setPrefilledVariables({ ...typebotWpUser }); Typebot.setPrefilledVariables({ ...typebotWpUser });
}'; }';
}
echo '</script>';
} }
add_action('wp_head', 'parse_wp_user'); echo '</script>';
add_action('wp_footer', 'typebot_script');
} }
public function add_typebot_container($attributes = []) public function add_typebot_container($attributes = [])

View File

@@ -3,7 +3,7 @@
/** /**
* Plugin Name: Typebot * Plugin Name: Typebot
* Description: Convert more with conversational forms * Description: Convert more with conversational forms
* Version: 3.2.0 * Version: 3.3.0
* Author: Typebot * Author: Typebot
* Author URI: http://typebot.io/ * Author URI: http://typebot.io/
* License: GPL-2.0+ * License: GPL-2.0+
@@ -16,7 +16,7 @@ if (!defined('WPINC')) {
die(); die();
} }
define('TYPEBOT_VERSION', '3.2.0'); define('TYPEBOT_VERSION', '3.3.0');
function activate_typebot() function activate_typebot()
{ {