Skip to content

Commit 843605a

Browse files
committed
feat: add nps form
1 parent e8b5806 commit 843605a

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

inc/Admin.php

+52-3
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ class Admin {
2020
* Admin constructor.
2121
*/
2222
public function __construct() {
23-
$this->setup_otter_notice();
23+
$this->setup_admin_hooks();
2424
}
2525

2626

2727
/**
28-
* Setup the Otter Blocks notice.
28+
* Setup admin hooks.
2929
*
3030
* @return void
3131
*/
32-
public function setup_otter_notice() {
32+
public function setup_admin_hooks() {
3333
add_action( 'admin_notices', array( $this, 'render_welcome_notice' ), 0 );
3434
add_action( 'wp_ajax_fork_dismiss_welcome_notice', array( $this, 'remove_welcome_notice' ) );
3535
add_action( 'wp_ajax_fork_set_otter_ref', array( $this, 'set_otter_ref' ) );
36+
add_action( 'admin_print_scripts', array( $this, 'add_nps_form' ) );
3637
}
3738

3839
/**
@@ -230,4 +231,52 @@ private function get_otter_status(): string {
230231

231232
return $status;
232233
}
234+
235+
/**
236+
* Add NPS form.
237+
*
238+
* @return void
239+
*/
240+
public function add_nps_form() {
241+
$screen = get_current_screen();
242+
243+
if ( current_user_can( 'manage_options' ) && ( 'dashboard' === $screen->id || 'themes' === $screen->id ) ) {
244+
$website_url = preg_replace( '/[^a-zA-Z0-9]+/', '', get_site_url() );
245+
246+
$config = array(
247+
'environmentId' => 'clr7hsvmjeu0u8up04ydg210b',
248+
'apiHost' => 'https://app.formbricks.com',
249+
'userId' => 'fork_' . $website_url,
250+
'attributes' => array(
251+
'days_since_install' => self::convert_to_category( round( ( time() - get_option( 'fork_install', 0 ) ) / DAY_IN_SECONDS ) ),
252+
),
253+
);
254+
255+
echo '<script type="text/javascript">!function(){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src="https://unpkg.com/@formbricks/js@^1.2.0/dist/index.umd.js";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e),setTimeout(function(){window.formbricks.init(' . wp_json_encode( $config ) . ')},500)}();</script>';
256+
}
257+
}
258+
259+
/**
260+
* Convert a number to a category.
261+
*
262+
* @param int $number Number to convert.
263+
* @param int $scale Scale.
264+
*
265+
* @return int
266+
*/
267+
public static function convert_to_category( $number, $scale = 1 ) {
268+
$normalized_number = intval( round( $number / $scale ) );
269+
270+
if ( 0 === $normalized_number || 1 === $normalized_number ) {
271+
return 0;
272+
} elseif ( $normalized_number > 1 && $normalized_number < 8 ) {
273+
return 7;
274+
} elseif ( $normalized_number >= 8 && $normalized_number < 31 ) {
275+
return 30;
276+
} elseif ( $normalized_number > 30 && $normalized_number < 90 ) {
277+
return 90;
278+
} elseif ( $normalized_number > 90 ) {
279+
return 91;
280+
}
281+
}
233282
}

0 commit comments

Comments
 (0)