Skip to content

Commit

Permalink
feat: add nps form
Browse files Browse the repository at this point in the history
  • Loading branch information
HardeepAsrani committed Jan 24, 2024
1 parent 01cebf8 commit 00a1892
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions inc/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ class Admin {
* Admin constructor.
*/
public function __construct() {
$this->setup_otter_notice();
$this->setup_admin_hooks();
}


/**
* Setup the Otter Blocks notice.
* Setup admin hooks.
*
* @return void
*/
public function setup_otter_notice() {
public function setup_admin_hooks() {
add_action( 'admin_notices', array( $this, 'render_welcome_notice' ), 0 );
add_action( 'wp_ajax_riverbank_dismiss_welcome_notice', array( $this, 'remove_welcome_notice' ) );
add_action( 'wp_ajax_riverbank_set_otter_ref', array( $this, 'set_otter_ref' ) );
add_action( 'admin_print_scripts', array( $this, 'add_nps_form' ) );
}

/**
Expand Down Expand Up @@ -230,4 +231,52 @@ private function get_otter_status(): string {

return $status;
}

/**
* Add NPS form.
*
* @return void
*/
public function add_nps_form() {
$screen = get_current_screen();

if ( current_user_can( 'manage_options' ) && ( 'dashboard' === $screen->id || 'themes' === $screen->id ) ) {
$website_url = preg_replace( '/[^a-zA-Z0-9]+/', '', get_site_url() );

$config = array(
'environmentId' => 'clr7jal6eexcy8up0wdufqz2d',
'apiHost' => 'https://app.formbricks.com',
'userId' => 'riverbank_' . $website_url,
'attributes' => array(
'days_since_install' => self::convert_to_category( round( ( time() - get_option( 'riverbank_install', 0 ) ) / DAY_IN_SECONDS ) ),
),
);

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>';
}
}

/**
* Convert a number to a category.
*
* @param int $number Number to convert.
* @param int $scale Scale.
*
* @return int
*/
public static function convert_to_category( $number, $scale = 1 ) {
$normalized_number = intval( round( $number / $scale ) );

if ( 0 === $normalized_number || 1 === $normalized_number ) {
return 0;
} elseif ( $normalized_number > 1 && $normalized_number < 8 ) {
return 7;
} elseif ( $normalized_number >= 8 && $normalized_number < 31 ) {
return 30;
} elseif ( $normalized_number > 30 && $normalized_number < 90 ) {
return 90;
} elseif ( $normalized_number < 90 ) {
return 91;
}
}
}

0 comments on commit 00a1892

Please sign in to comment.