From 00a18920a2d1c4bb8d24e43a20c4af947c01ce51 Mon Sep 17 00:00:00 2001 From: Hardeep Asrani Date: Wed, 24 Jan 2024 19:00:52 +0530 Subject: [PATCH] feat: add nps form --- inc/Admin.php | 55 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/inc/Admin.php b/inc/Admin.php index 544f12d..ef11975 100644 --- a/inc/Admin.php +++ b/inc/Admin.php @@ -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' ) ); } /** @@ -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 ''; + } + } + + /** + * 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; + } + } }