Skip to content

Commit 516651e

Browse files
authored
Merge pull request #874 from Codeinwp/refactor/internal-pages
feat: load Formbricks survey via internal pages hooks
2 parents ba13250 + 4cf3df1 commit 516651e

File tree

7 files changed

+76
-45
lines changed

7 files changed

+76
-45
lines changed

assets/src/dashboard/parts/connected/index.js

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,8 @@ import Help from './help';
2121
import Sidebar from './Sidebar';
2222
import CSAT from './CSAT';
2323
import { retrieveConflicts } from '../../utils/api';
24-
import formbricks from '@formbricks/js/app';
2524
import BlackFridayBanner from '../components/BlackFridayBanner';
26-
if ( 'undefined' !== typeof window && optimoleDashboardApp.user_data.plan ) {
27-
formbricks.init({
28-
environmentId: 'clo8wxwzj44orpm0gjchurujm',
29-
apiHost: 'https://app.formbricks.com',
30-
userId: 'optml_' + ( optimoleDashboardApp.user_data.id ),
31-
attributes: {
32-
plan: optimoleDashboardApp.user_data.plan,
33-
status: optimoleDashboardApp.user_data.status,
34-
language: optimoleDashboardApp.language,
35-
cname_assigned: optimoleDashboardApp.user_data.is_cname_assigned || 'no',
36-
connected_websites: optimoleDashboardApp.user_data.whitelist && optimoleDashboardApp.user_data.whitelist.length.toString(),
37-
traffic: convertToCategory( optimoleDashboardApp.user_data.traffic || 0, 500 ).toString(),
38-
images_number: convertToCategory( optimoleDashboardApp.user_data.images_number || 0, 100 ).toString(),
39-
days_since_install: convertToCategory( optimoleDashboardApp.days_since_install ).toString()
40-
}
41-
});
4225

43-
}
44-
function convertToCategory( number, scale = 1 ) {
45-
46-
const normalizedNumber = Math.round( number / scale );
47-
if ( 0 === normalizedNumber || 1 === normalizedNumber ) {
48-
return 0;
49-
} else if ( 1 < normalizedNumber && 8 > normalizedNumber ) {
50-
return 7;
51-
} else if ( 8 <= normalizedNumber && 31 > normalizedNumber ) {
52-
return 30;
53-
} else if ( 30 < normalizedNumber && 90 > normalizedNumber ) {
54-
return 90;
55-
} else if ( 90 < normalizedNumber ) {
56-
return 91;
57-
}
58-
}
5926
const ConnectedLayout = ({
6027
tab,
6128
setTab

inc/admin.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public function __construct() {
9595
); // phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.upload_mimes
9696
add_filter( 'wp_handle_upload_prefilter', [ $this, 'check_svg_and_sanitize' ] );
9797
}
98+
99+
add_filter( 'themeisle-sdk/survey/' . OPTML_PRODUCT_SLUG, [ $this, 'get_survey_metadata' ], 10, 2 );
98100
}
99101
/**
100102
* Check if the file is an SVG, if so handle appropriately
@@ -1257,6 +1259,8 @@ public function enqueue() {
12571259
],
12581260
$asset_file['version']
12591261
);
1262+
1263+
do_action( 'themeisle_internal_page', OPTML_PRODUCT_SLUG, 'dashboard' );
12601264
}
12611265

12621266
/**
@@ -1980,4 +1984,71 @@ public function allow_svg( $mimes ) {
19801984

19811985
return $mimes;
19821986
}
1987+
1988+
/**
1989+
* Get the Formbricks survey metadata.
1990+
*
1991+
* @param array $data The data in Formbricks format.
1992+
* @param string $page_slug The slug of the page.
1993+
*
1994+
* @return array - The data in Formbricks format.
1995+
*/
1996+
public function get_survey_metadata( $data, $page_slug ) {
1997+
1998+
if ( 'dashboard' !== $page_slug ) {
1999+
return $data;
2000+
}
2001+
2002+
$dashboard_data = $this->localize_dashboard_app();
2003+
$user_data = $dashboard_data['user_data'];
2004+
2005+
if ( ! isset( $user_data['plan'] ) ) {
2006+
return $data;
2007+
}
2008+
2009+
$data = [
2010+
'environmentId' => 'clo8wxwzj44orpm0gjchurujm',
2011+
'attributes' => [
2012+
'plan' => $user_data['plan'],
2013+
'status' => $user_data['status'],
2014+
'cname_assigned' => ! empty( $user_data['is_cname_assigned'] ) ? $user_data['is_cname_assigned'] : 'no',
2015+
'connected_websites' => isset( $user_data['whitelist'] ) ? strval( count( $user_data['whitelist'] ) ) : '0',
2016+
'install_days_number' => intval( $dashboard_data['days_since_install'] ),
2017+
'traffic' => strval( isset( $user_data['traffic'] ) ? $this->survey_category( $user_data['traffic'], 500 ) : 0 ),
2018+
'images_number' => strval( isset( $user_data['images_number'] ) ? $this->survey_category( $user_data['images_number'], 100 ) : 0 ),
2019+
],
2020+
];
2021+
2022+
return $data;
2023+
}
2024+
2025+
/**
2026+
* Categorize a number for survey based on its scale.
2027+
*
2028+
* @param int $value The value.
2029+
* @param int $scale The scale.
2030+
*
2031+
* @return int - The category.
2032+
*/
2033+
public function survey_category( $value, $scale = 1 ) {
2034+
$value = intval( $value / $scale );
2035+
2036+
if ( 1 < $value && 8 > $value ) {
2037+
return 7;
2038+
}
2039+
2040+
if ( 8 <= $value && 31 > $value ) {
2041+
return 30;
2042+
}
2043+
2044+
if ( 30 < $value && 90 > $value ) {
2045+
return 90;
2046+
}
2047+
2048+
if ( 90 <= $value ) {
2049+
return 91;
2050+
}
2051+
2052+
return 0;
2053+
}
19832054
}

inc/dam.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ public function enqueue_admin_page_scripts() {
536536
wp_enqueue_script( OPTML_NAMESPACE . '-admin-page' );
537537

538538
wp_enqueue_style( OPTML_NAMESPACE . '-admin-page', OPTML_URL . 'assets/build/media/admin-page.css' );
539+
540+
do_action( 'themeisle_internal_page', OPTML_PRODUCT_SLUG, 'dam' );
539541
}
540542

541543
/**

optimole-wp.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function optml() {
9292
define( 'OPTML_VERSION', '3.14.1' );
9393
define( 'OPTML_NAMESPACE', 'optml' );
9494
define( 'OPTML_BASEFILE', __FILE__ );
95+
define( 'OPTML_PRODUCT_SLUG', basename( OPTML_PATH ) );
9596
// Fallback for old PHP versions when this constant is not defined.
9697
if ( ! defined( 'PHP_INT_MIN' ) ) {
9798
define( 'PHP_INT_MIN', - 999999 );

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"tailwindcss": "^3.3.2"
5858
},
5959
"dependencies": {
60-
"@formbricks/js": "^2.0.0",
6160
"classnames": "^2.3.2",
6261
"react-compare-image": "^3.4.0",
6362
"usehooks-ts": "^2.9.1"

tests/static-analysis-stubs/optimole-wp.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
define( 'OPTML_VERSION', '3.7.0' );
1111
define( 'OPTML_NAMESPACE', 'optml' );
1212
define( 'OPTML_BASEFILE', __FILE__ );
13+
define( 'OPTML_PRODUCT_SLUG', basename( OPTML_PATH ) );
14+
1315
if ( ! defined( 'OPTML_DEBUG' ) ) {
1416
define( 'OPTML_DEBUG', false );
1517
}

0 commit comments

Comments
 (0)