Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nps form #107

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions inc/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Admin {
* Admin constructor.
*/
public function __construct() {
$this->setup_otter_notice();
$this->setup_admin_hooks();
$this->add_install_time();
}

Expand All @@ -46,15 +46,16 @@ private function add_install_time() {


/**
* 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( 'admin_notices', array( $this, 'render_survey_notice' ) );
add_action( 'wp_ajax_neve_fse_dismiss_welcome_notice', array( $this, 'remove_welcome_notice' ) );
add_action( 'wp_ajax_neve_fse_dismiss_survey_notice', array( $this, 'remove_survey_notice' ) );
add_action( 'admin_print_scripts', array( $this, 'add_nps_form' ) );
}

/**
Expand Down Expand Up @@ -350,4 +351,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' => 'clr7hcws7et2g8up0tpz8u8es',
'apiHost' => 'https://app.formbricks.com',
'userId' => 'neve_fse_' . $website_url,
'attributes' => array(
'days_since_install' => self::convert_to_category( round( ( time() - get_option( 'neve_fse_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;
}
}
}
Loading