From 32bf8a3dd7b7840f6ef07438a8f51183dedf04f7 Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Wed, 14 Feb 2024 15:41:33 +0200 Subject: [PATCH 1/6] feat: add nps survey --- includes/admin/feedzy-rss-feeds-admin.php | 88 +++++++++++++++++++++++ js/survey.js | 13 ++++ 2 files changed, 101 insertions(+) create mode 100644 js/survey.js diff --git a/includes/admin/feedzy-rss-feeds-admin.php b/includes/admin/feedzy-rss-feeds-admin.php index f0a3d49c..b734e36f 100644 --- a/includes/admin/feedzy-rss-feeds-admin.php +++ b/includes/admin/feedzy-rss-feeds-admin.php @@ -128,6 +128,12 @@ public function enqueue_styles_admin() { wp_enqueue_script( $this->plugin_name . '_telemetry', FEEDZY_ABSURL . 'js/telemetry.js', array(), $this->version, true ); } + if ( 'feedzy_imports' === $screen->post_type && 'edit' === $screen->base ) { + if ( ! defined( 'CYPRESS_TESTING' ) ) { + $this->register_survey(); + } + } + if ( 'feedzy_categories' === $screen->post_type ) { wp_enqueue_script( $this->plugin_name . '_categories', @@ -152,6 +158,10 @@ public function enqueue_styles_admin() { ), ) ); + + if ( ! defined( 'CYPRESS_TESTING' ) ) { + $this->register_survey(); + } } if ( 'feedzy_page_feedzy-settings' === $screen->base ) { @@ -171,6 +181,10 @@ public function enqueue_styles_admin() { ), ) ); + + if ( ! defined( 'CYPRESS_TESTING' ) ) { + $this->register_survey(); + } } $upsell_screens = array( 'feedzy-rss_page_feedzy-settings', 'feedzy-rss_page_feedzy-admin-menu-pro-upsell' ); @@ -195,6 +209,10 @@ public function enqueue_styles_admin() { ) ); wp_enqueue_style( 'wp-block-editor' ); + + if ( ! defined( 'CYPRESS_TESTING' ) ) { + $this->register_survey(); + } } if ( ! defined( 'TI_CYPRESS_TESTING' ) && ( 'edit' !== $screen->base && 'feedzy_imports' === $screen->post_type && feedzy_show_import_tour() ) ) { wp_enqueue_script( $this->plugin_name . '_on_boarding', FEEDZY_ABSURL . 'js/Onboarding/import-onboarding.min.js', array( 'react', 'react-dom', 'wp-editor', 'wp-api', 'lodash' ), $this->version, true ); @@ -205,6 +223,9 @@ public function enqueue_styles_admin() { } if ( 'feedzy_page_feedzy-support' === $screen->base || ( 'edit' !== $screen->base && 'feedzy_imports' === $screen->post_type ) ) { + if ( ! defined( 'CYPRESS_TESTING' ) ) { + $this->register_survey(); + } wp_enqueue_script( $this->plugin_name . '_feedback', FEEDZY_ABSURL . 'js/FeedBack/feedback.min.js', array( 'react', 'react-dom', 'wp-editor', 'wp-api', 'lodash' ), $this->version, true ); wp_enqueue_style( 'wp-block-editor' ); @@ -1565,10 +1586,13 @@ public function api_license_status() { 'spinnerChiefStatus' => false, 'wordaiStatus' => false, 'openaiStatus' => false, + 'amazonStatus' => false, ); + if ( ! feedzy_is_pro() ) { return $data; } + if ( apply_filters( 'feedzy_is_license_of_type', false, 'agency' ) ) { if ( isset( $pro_options['spinnerchief_licence'] ) && 'yes' === $pro_options['spinnerchief_licence'] ) { $data['spinnerChiefStatus'] = true; @@ -1577,11 +1601,75 @@ public function api_license_status() { $data['wordaiStatus'] = true; } } + if ( isset( $pro_options['openai_licence'] ) && 'yes' === $pro_options['openai_licence'] ) { if ( apply_filters( 'feedzy_is_license_of_type', false, 'business' ) || apply_filters( 'feedzy_is_license_of_type', false, 'agency' ) ) { $data['openaiStatus'] = true; } } + + if ( ! empty( $pro_options['amazon_access_key'] ) && ! empty( $pro_options['amazon_secret_key'] ) ) { + $data['amazonStatus'] = true; + } return $data; } + + /** + * Get the data used for the survey. + * + * @return array + * @see survey.js + */ + public function get_survery_metadata() { + + $user_id = 'feedzy_'; + $license = get_option( 'feedzy_rss_feeds_pro_license_data', array() ); + + if ( ! empty( $license->key ) ) { + $user_id .= $license->key; + } else { + $user_id .= 'free'; + } + + $integration_status = $this->api_license_status(); + + $days_since_install = round( ( time() - get_option( 'feedzy_rss_feeds_install', 0 ) ) / DAY_IN_SECONDS ); + $install_category = null; + if ( 0 === $days_since_install || 1 === $days_since_install ) { + $install_category = 0; + } elseif ( 1 < $days_since_install && 8 > $days_since_install ) { + $install_category = 7; + } elseif ( 8 <= $days_since_install && 31 > $days_since_install ) { + $install_category = 30; + } elseif ( 30 < $days_since_install && 90 > $days_since_install ) { + $install_category = 90; + } elseif ( 90 <= $days_since_install ) { + $install_category = 91; + } + + return array( + 'userId' => $user_id, + 'attributes' => array( + 'free_version' => $this->version, + 'pro_version' => defined( 'FEEDZY_PRO_VERSION' ) ? FEEDZY_PRO_VERSION : '', + 'openai' => $integration_status['openaiStatus'] ? 'valid' : 'invalid', + 'amazon' => $integration_status['amazonStatus'] ? 'valid' : 'invalid', + 'spinnerchief' => $integration_status['spinnerChiefStatus'] ? 'valid' : 'invalid', + 'wordai' => $integration_status['wordaiStatus'] ? 'valid' : 'invalid', + 'plan' => apply_filters( 'product_feedzy_license_plan', 0 ), + 'days_since_install' => $install_category, + ), + ); + } + + /** + * Register the survey script. + */ + public function register_survey() { + + // Register the survey script. + wp_register_script( $this->plugin_name . '_survey_formbrick', 'https://unpkg.com/@formbricks/js@^1.5.1/dist/index.umd.js', array(), $this->version, true ); + wp_enqueue_script( $this->plugin_name . '_survey', FEEDZY_ABSURL . 'js/survey.js', array( $this->plugin_name . '_survey_formbrick' ), $this->version, true ); + wp_localize_script( $this->plugin_name . '_survey', 'feedzySurveyData', $this->get_survery_metadata() ); + } } diff --git a/js/survey.js b/js/survey.js new file mode 100644 index 00000000..49a4befc --- /dev/null +++ b/js/survey.js @@ -0,0 +1,13 @@ +/** + * Initialize the formbricks survey. + * + * @see https://github.com/formbricks/setup-examples/tree/main/html + */ +window.addEventListener('load', function () { + window?.formbricks?.init?.({ + environmentId: "clskgehf78eu5podwdrnzciti", + apiHost: "https://app.formbricks.com", + debug: true, + ...(window?.feedzySurveyData ?? {}) + }); +}); \ No newline at end of file From 114c75c4d1a3875c27f91b98975747943ed4f4d2 Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Wed, 14 Feb 2024 15:53:22 +0200 Subject: [PATCH 2/6] feat: normalized url for user id survey --- includes/admin/feedzy-rss-feeds-admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/admin/feedzy-rss-feeds-admin.php b/includes/admin/feedzy-rss-feeds-admin.php index b734e36f..8c557bd9 100644 --- a/includes/admin/feedzy-rss-feeds-admin.php +++ b/includes/admin/feedzy-rss-feeds-admin.php @@ -1628,7 +1628,7 @@ public function get_survery_metadata() { if ( ! empty( $license->key ) ) { $user_id .= $license->key; } else { - $user_id .= 'free'; + $user_id .= preg_replace( '/[^\w\d]*/', '', get_site_url() ); // Use a normalized version of the site URL as a user ID for free users. } $integration_status = $this->api_license_status(); From 4782d62cfc9f19c0e93840d0e7485f3ba538e3bf Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Thu, 15 Feb 2024 14:50:18 +0200 Subject: [PATCH 3/6] feat: nps plan category & license status --- includes/admin/feedzy-rss-feeds-admin.php | 42 ++++++++++++++++++++--- js/survey.js | 3 +- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/includes/admin/feedzy-rss-feeds-admin.php b/includes/admin/feedzy-rss-feeds-admin.php index 8c557bd9..11d81106 100644 --- a/includes/admin/feedzy-rss-feeds-admin.php +++ b/includes/admin/feedzy-rss-feeds-admin.php @@ -1614,6 +1614,37 @@ public function api_license_status() { return $data; } + /** + * Get the plan category for the product plan ID. + * + * @param object $license_data The license data. + * @return int + */ + private static function plan_category( $license_data ) { + + if ( ! isset( $license_data->plan ) || ! is_numeric( $license_data->plan ) ) { + return 0; // Free + } + + $plan = (int) $license_data->plan; + $current_category = 0; + + $categories = array( + '1' => array(1, 4, 9), // Personal + '2' => array(2, 5, 8), // Business/Developer + '3' => array(3, 6, 7, 10), // Agency + ); + + foreach ( $categories as $category => $plans ) { + if ( in_array( $plan, $plans, true ) ) { + $current_category = (int) $category; + break; + } + } + + return $current_category; + } + /** * Get the data used for the survey. * @@ -1623,10 +1654,10 @@ public function api_license_status() { public function get_survery_metadata() { $user_id = 'feedzy_'; - $license = get_option( 'feedzy_rss_feeds_pro_license_data', array() ); + $license_data = get_option( 'feedzy_rss_feeds_pro_license_data', array() ); - if ( ! empty( $license->key ) ) { - $user_id .= $license->key; + if ( ! empty( $license_data->key ) ) { + $user_id .= $license_data->key; } else { $user_id .= preg_replace( '/[^\w\d]*/', '', get_site_url() ); // Use a normalized version of the site URL as a user ID for free users. } @@ -1634,7 +1665,7 @@ public function get_survery_metadata() { $integration_status = $this->api_license_status(); $days_since_install = round( ( time() - get_option( 'feedzy_rss_feeds_install', 0 ) ) / DAY_IN_SECONDS ); - $install_category = null; + $install_category = 0; if ( 0 === $days_since_install || 1 === $days_since_install ) { $install_category = 0; } elseif ( 1 < $days_since_install && 8 > $days_since_install ) { @@ -1656,8 +1687,9 @@ public function get_survery_metadata() { 'amazon' => $integration_status['amazonStatus'] ? 'valid' : 'invalid', 'spinnerchief' => $integration_status['spinnerChiefStatus'] ? 'valid' : 'invalid', 'wordai' => $integration_status['wordaiStatus'] ? 'valid' : 'invalid', - 'plan' => apply_filters( 'product_feedzy_license_plan', 0 ), + 'plan' => $this->plan_category( $license_data ), 'days_since_install' => $install_category, + 'license_status' => ! empty( $license_data->license ) ? $license_data->license : 'invalid', ), ); } diff --git a/js/survey.js b/js/survey.js index 49a4befc..1071763b 100644 --- a/js/survey.js +++ b/js/survey.js @@ -7,7 +7,6 @@ window.addEventListener('load', function () { window?.formbricks?.init?.({ environmentId: "clskgehf78eu5podwdrnzciti", apiHost: "https://app.formbricks.com", - debug: true, - ...(window?.feedzySurveyData ?? {}) + ...(window?.feedzySurveyData ?? {}), }); }); \ No newline at end of file From 2ecb1eb0dd72d4b9c85a62f6f04658cb7d73fbe9 Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Thu, 15 Feb 2024 14:59:40 +0200 Subject: [PATCH 4/6] chore: unknown nps plan category --- includes/admin/feedzy-rss-feeds-admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/admin/feedzy-rss-feeds-admin.php b/includes/admin/feedzy-rss-feeds-admin.php index 11d81106..ccfcb189 100644 --- a/includes/admin/feedzy-rss-feeds-admin.php +++ b/includes/admin/feedzy-rss-feeds-admin.php @@ -1627,7 +1627,7 @@ private static function plan_category( $license_data ) { } $plan = (int) $license_data->plan; - $current_category = 0; + $current_category = -1; $categories = array( '1' => array(1, 4, 9), // Personal From 211a3497e62ab905d8e556a098c3a941620d026d Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Mon, 26 Feb 2024 12:59:23 +0200 Subject: [PATCH 5/6] refactor: CI env checking for survey loading --- includes/admin/feedzy-rss-feeds-admin.php | 30 +++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/includes/admin/feedzy-rss-feeds-admin.php b/includes/admin/feedzy-rss-feeds-admin.php index ccfcb189..fc3199a4 100644 --- a/includes/admin/feedzy-rss-feeds-admin.php +++ b/includes/admin/feedzy-rss-feeds-admin.php @@ -129,9 +129,7 @@ public function enqueue_styles_admin() { } if ( 'feedzy_imports' === $screen->post_type && 'edit' === $screen->base ) { - if ( ! defined( 'CYPRESS_TESTING' ) ) { - $this->register_survey(); - } + $this->register_survey(); } if ( 'feedzy_categories' === $screen->post_type ) { @@ -159,9 +157,7 @@ public function enqueue_styles_admin() { ) ); - if ( ! defined( 'CYPRESS_TESTING' ) ) { - $this->register_survey(); - } + $this->register_survey(); } if ( 'feedzy_page_feedzy-settings' === $screen->base ) { @@ -182,9 +178,7 @@ public function enqueue_styles_admin() { ) ); - if ( ! defined( 'CYPRESS_TESTING' ) ) { - $this->register_survey(); - } + $this->register_survey(); } $upsell_screens = array( 'feedzy-rss_page_feedzy-settings', 'feedzy-rss_page_feedzy-admin-menu-pro-upsell' ); @@ -210,9 +204,7 @@ public function enqueue_styles_admin() { ); wp_enqueue_style( 'wp-block-editor' ); - if ( ! defined( 'CYPRESS_TESTING' ) ) { - $this->register_survey(); - } + $this->register_survey(); } if ( ! defined( 'TI_CYPRESS_TESTING' ) && ( 'edit' !== $screen->base && 'feedzy_imports' === $screen->post_type && feedzy_show_import_tour() ) ) { wp_enqueue_script( $this->plugin_name . '_on_boarding', FEEDZY_ABSURL . 'js/Onboarding/import-onboarding.min.js', array( 'react', 'react-dom', 'wp-editor', 'wp-api', 'lodash' ), $this->version, true ); @@ -223,9 +215,9 @@ public function enqueue_styles_admin() { } if ( 'feedzy_page_feedzy-support' === $screen->base || ( 'edit' !== $screen->base && 'feedzy_imports' === $screen->post_type ) ) { - if ( ! defined( 'CYPRESS_TESTING' ) ) { - $this->register_survey(); - } + + $this->register_survey(); + wp_enqueue_script( $this->plugin_name . '_feedback', FEEDZY_ABSURL . 'js/FeedBack/feedback.min.js', array( 'react', 'react-dom', 'wp-editor', 'wp-api', 'lodash' ), $this->version, true ); wp_enqueue_style( 'wp-block-editor' ); @@ -1696,9 +1688,17 @@ public function get_survery_metadata() { /** * Register the survey script. + * + * It does register if we are in CI environment. + * + * @return void */ public function register_survey() { + if ( defined( 'CYPRESS_TESTING' ) ) { + return; + } + // Register the survey script. wp_register_script( $this->plugin_name . '_survey_formbrick', 'https://unpkg.com/@formbricks/js@^1.5.1/dist/index.umd.js', array(), $this->version, true ); wp_enqueue_script( $this->plugin_name . '_survey', FEEDZY_ABSURL . 'js/survey.js', array( $this->plugin_name . '_survey_formbrick' ), $this->version, true ); From 4c4fff28a9677580e4b6fb2305f928a7149d9d9b Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Tue, 27 Feb 2024 14:41:52 +0200 Subject: [PATCH 6/6] refactor: use formbricks deps from SDK --- includes/admin/feedzy-rss-feeds-admin.php | 10 +++++++--- js/survey.js | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/includes/admin/feedzy-rss-feeds-admin.php b/includes/admin/feedzy-rss-feeds-admin.php index fc3199a4..fa4a95b2 100644 --- a/includes/admin/feedzy-rss-feeds-admin.php +++ b/includes/admin/feedzy-rss-feeds-admin.php @@ -1699,9 +1699,13 @@ public function register_survey() { return; } - // Register the survey script. - wp_register_script( $this->plugin_name . '_survey_formbrick', 'https://unpkg.com/@formbricks/js@^1.5.1/dist/index.umd.js', array(), $this->version, true ); - wp_enqueue_script( $this->plugin_name . '_survey', FEEDZY_ABSURL . 'js/survey.js', array( $this->plugin_name . '_survey_formbrick' ), $this->version, true ); + $survey_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'survey' ); + if ( empty( $survey_handler ) ) { + return; + } + + do_action( 'themeisle_sdk_dependency_enqueue_script', 'survey' ); + wp_enqueue_script( $this->plugin_name . '_survey', FEEDZY_ABSURL . 'js/survey.js', array( $survey_handler ), $this->version, true ); wp_localize_script( $this->plugin_name . '_survey', 'feedzySurveyData', $this->get_survery_metadata() ); } } diff --git a/js/survey.js b/js/survey.js index 1071763b..88dc55f2 100644 --- a/js/survey.js +++ b/js/survey.js @@ -3,8 +3,8 @@ * * @see https://github.com/formbricks/setup-examples/tree/main/html */ -window.addEventListener('load', function () { - window?.formbricks?.init?.({ +window.addEventListener('themeisle:survey:loaded', function () { + window?.tsdk_formbricks?.init?.({ environmentId: "clskgehf78eu5podwdrnzciti", apiHost: "https://app.formbricks.com", ...(window?.feedzySurveyData ?? {}),