From db679e5405567bd9886be1aed10514a39e2fcee2 Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Thu, 13 Feb 2025 14:34:30 +0200 Subject: [PATCH 1/5] feat: load survey via internal pages --- classes/Visualizer/Module/Admin.php | 89 ++++++++++++----------------- index.php | 1 + js/survey.js | 12 ---- 3 files changed, 38 insertions(+), 64 deletions(-) delete mode 100644 js/survey.js diff --git a/classes/Visualizer/Module/Admin.php b/classes/Visualizer/Module/Admin.php index 2bc81b5c0..0a623645a 100644 --- a/classes/Visualizer/Module/Admin.php +++ b/classes/Visualizer/Module/Admin.php @@ -79,6 +79,10 @@ public function __construct( Visualizer_Plugin $plugin ) { $this->_addFilter( 'admin_footer_text', 'render_review_notice' ); + if ( ! defined( 'TI_CYPRESS_TESTING' ) ) { + $this->_addFilter( 'themeisle-sdk/survey/' . VISUALIZER_PRODUCT_SLUG, 'get_survey_metadata', 10, 2 ); + } + if ( defined( 'TI_CYPRESS_TESTING' ) ) { $this->load_cypress_hooks(); } @@ -934,7 +938,7 @@ private function getQuery() { */ public function renderSupportPage() { wp_enqueue_style( 'visualizer-upsell', VISUALIZER_ABSURL . 'css/upsell.css', array(), Visualizer_Plugin::VERSION ); - $this->load_survey(); + do_action( 'themeisle_internal_page', VISUALIZER_PRODUCT_SLUG, 'support' ); include_once VISUALIZER_ABSPATH . '/templates/support.php'; } @@ -1091,7 +1095,7 @@ public function renderLibraryPage() { ) ); - $this->load_survey(); + do_action( 'themeisle_internal_page', VISUALIZER_PRODUCT_SLUG, 'library' ); if ( ! apply_filters( 'visualizer_is_business', false ) ) { do_action( 'themeisle_sdk_load_banner', 'visualizer' ); @@ -1229,28 +1233,30 @@ public static function checkChartStatus( $type ) { /** * Get the survey metadata. + * + * @param array $data The data for survey in Formbricks format. + * @param string $page_slug The slug of the loaded page. * * @return array The survey metadata. */ - private function get_survey_metadata() { - $install_date = get_option( 'visualizer_install', false ); - $install_category = 0; - - if ( false !== $install_date ) { - $days_since_install = round( ( time() - $install_date ) / DAY_IN_SECONDS ); - - 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; - } + public function get_survey_metadata( $data, $page_slug ) { + $install_date = get_option( 'visualizer_install', time() ); + $install_category = 0; + + $install_days_number = intval( ( time() - $install_date ) / DAY_IN_SECONDS ); + + if ( 0 === $install_days_number || 1 === $install_days_number ) { + $install_category = 0; + } elseif ( 1 < $install_days_number && 8 > $install_days_number ) { + $install_category = 7; + } elseif ( 8 <= $install_days_number && 31 > $install_days_number ) { + $install_category = 30; + } elseif ( 30 < $install_days_number && 90 > $install_days_number ) { + $install_category = 90; + } elseif ( 90 <= $install_days_number ) { + $install_category = 91; } + $plugin_data = get_plugin_data( VISUALIZER_BASEFILE, false, false ); $plugin_version = ''; @@ -1258,43 +1264,22 @@ private function get_survey_metadata() { $plugin_version = $plugin_data['Version']; } - $user_id = 'visualizer_' . preg_replace( '/[^\w\d]*/', '', get_site_url() ); // Use a normalized version of the site URL as a user ID. - - $license_data = get_option( 'visualizer_pro_license_data', false ); - if ( false !== $license_data && isset( $license_data->key ) ) { - $user_id = 'visualizer_' . $license_data->key; - } - - return array( - 'userId' => $user_id, + $data = array( + 'environmentId' => 'cltef8cut1s7wyyfxy3rlxzs5', 'attributes' => array( - 'days_since_install' => strval( $install_category ), - 'free_version' => $plugin_version, - 'pro_version' => defined( 'VISUALIZER_PRO_VERSION' ) ? VISUALIZER_PRO_VERSION : '', - 'license_status' => apply_filters( 'product_visualizer_license_status', 'invalid' ), + 'days_since_install' => strval( $install_category ), + 'free_version' => $plugin_version, + 'pro_version' => defined( 'VISUALIZER_PRO_VERSION' ) ? VISUALIZER_PRO_VERSION : '', + 'license_status' => apply_filters( 'product_visualizer_license_status', 'invalid' ), + 'install_days_number' => $install_days_number, ), ); - } - /** - * Load the survey. - */ - private function load_survey() { - - if ( defined( 'TI_CYPRESS_TESTING' ) ) { - return; - } - - $survey_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'survey' ); - - if ( empty( $survey_handler ) ) { - return; + $license_data = get_option( 'visualizer_pro_license_data', false ); + if ( isset( $license_data->key ) ) { + $data['attributes']['license_key'] = apply_filters( 'themeisle_sdk_secret_masking', $license_data->key ); } - $metadata = $this->get_survey_metadata(); - - do_action( 'themeisle_sdk_dependency_enqueue_script', 'survey' ); - wp_enqueue_script( 'visualizer_chart_survey', VISUALIZER_ABSURL . 'js/survey.js', array( $survey_handler ), $metadata['attributes']['free_version'], true ); - wp_localize_script( 'visualizer_chart_survey', 'visualizerSurveyData', $metadata ); + return $data; } } diff --git a/index.php b/index.php index b5cdea884..9f8fa65a3 100644 --- a/index.php +++ b/index.php @@ -69,6 +69,7 @@ function visualizer_launch() { define( 'VISUALIZER_ABSPATH', dirname( __FILE__ ) ); define( 'VISUALIZER_DIRNAME', basename( VISUALIZER_ABSPATH ) ); define( 'VISUALIZER_REST_VERSION', 1 ); + define( 'VISUALIZER_PRODUCT_SLUG', VISUALIZER_DIRNAME ); // if the below is true, then the js/customization.js in the plugin folder will be used instead of the one in the uploads folder (if it exists). // this is also used in Block.php if ( ! defined( 'VISUALIZER_TEST_JS_CUSTOMIZATION' ) ) { diff --git a/js/survey.js b/js/survey.js deleted file mode 100644 index 6e29d56b8..000000000 --- a/js/survey.js +++ /dev/null @@ -1,12 +0,0 @@ -/** - * Initialize the formbricks survey. - * - * @see https://github.com/formbricks/setup-examples/tree/main/html - */ -window.addEventListener('themeisle:survey:loaded', function () { - window?.tsdk_formbricks?.init?.({ - environmentId: "cltef8cut1s7wyyfxy3rlxzs5", - apiHost: "https://app.formbricks.com", - ...(window?.visualizerSurveyData ?? {}), - }); -}); \ No newline at end of file From 000ab63d227726924277299e7bedac391e8ae838 Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Tue, 18 Feb 2025 14:53:07 +0200 Subject: [PATCH 2/5] refactor: remove install category from survey --- classes/Visualizer/Module/Admin.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/classes/Visualizer/Module/Admin.php b/classes/Visualizer/Module/Admin.php index 0a623645a..3a90e5485 100644 --- a/classes/Visualizer/Module/Admin.php +++ b/classes/Visualizer/Module/Admin.php @@ -1241,23 +1241,9 @@ public static function checkChartStatus( $type ) { */ public function get_survey_metadata( $data, $page_slug ) { $install_date = get_option( 'visualizer_install', time() ); - $install_category = 0; $install_days_number = intval( ( time() - $install_date ) / DAY_IN_SECONDS ); - if ( 0 === $install_days_number || 1 === $install_days_number ) { - $install_category = 0; - } elseif ( 1 < $install_days_number && 8 > $install_days_number ) { - $install_category = 7; - } elseif ( 8 <= $install_days_number && 31 > $install_days_number ) { - $install_category = 30; - } elseif ( 30 < $install_days_number && 90 > $install_days_number ) { - $install_category = 90; - } elseif ( 90 <= $install_days_number ) { - $install_category = 91; - } - - $plugin_data = get_plugin_data( VISUALIZER_BASEFILE, false, false ); $plugin_version = ''; if ( ! empty( $plugin_data['Version'] ) ) { @@ -1267,7 +1253,6 @@ public function get_survey_metadata( $data, $page_slug ) { $data = array( 'environmentId' => 'cltef8cut1s7wyyfxy3rlxzs5', 'attributes' => array( - 'days_since_install' => strval( $install_category ), 'free_version' => $plugin_version, 'pro_version' => defined( 'VISUALIZER_PRO_VERSION' ) ? VISUALIZER_PRO_VERSION : '', 'license_status' => apply_filters( 'product_visualizer_license_status', 'invalid' ), From c78363864c4bd93aaf05a4614851b825c68a20b7 Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Thu, 20 Feb 2025 14:13:19 +0200 Subject: [PATCH 3/5] refactor: use filters for getting license data in survey --- classes/Visualizer/Module/Admin.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/classes/Visualizer/Module/Admin.php b/classes/Visualizer/Module/Admin.php index 3a90e5485..5d0cea2e9 100644 --- a/classes/Visualizer/Module/Admin.php +++ b/classes/Visualizer/Module/Admin.php @@ -1233,19 +1233,23 @@ public static function checkChartStatus( $type ) { /** * Get the survey metadata. - * - * @param array $data The data for survey in Formbricks format. + * + * @param array $data The data for survey in Formbricks format. * @param string $page_slug The slug of the loaded page. * * @return array The survey metadata. */ public function get_survey_metadata( $data, $page_slug ) { $install_date = get_option( 'visualizer_install', time() ); - $install_days_number = intval( ( time() - $install_date ) / DAY_IN_SECONDS ); + $license_status = apply_filters( 'product_visualizer_license_status', 'invalid' ); + $license_plan = apply_filters( 'product_visualizer_license_plan', false ); + $license_key = apply_filters( 'product_visualizer_license_key', false ); + $plugin_data = get_plugin_data( VISUALIZER_BASEFILE, false, false ); $plugin_version = ''; + if ( ! empty( $plugin_data['Version'] ) ) { $plugin_version = $plugin_data['Version']; } @@ -1255,14 +1259,17 @@ public function get_survey_metadata( $data, $page_slug ) { 'attributes' => array( 'free_version' => $plugin_version, 'pro_version' => defined( 'VISUALIZER_PRO_VERSION' ) ? VISUALIZER_PRO_VERSION : '', - 'license_status' => apply_filters( 'product_visualizer_license_status', 'invalid' ), + 'license_status' => $license_status, 'install_days_number' => $install_days_number, ), ); - $license_data = get_option( 'visualizer_pro_license_data', false ); - if ( isset( $license_data->key ) ) { - $data['attributes']['license_key'] = apply_filters( 'themeisle_sdk_secret_masking', $license_data->key ); + if ( ! empty( $license_plan ) ) { + $data['attributes']['plan'] = $license_plan; + } + + if ( ! empty( $license_key ) ) { + $data['attributes']['license_key'] = apply_filters( 'themeisle_sdk_secret_masking', $license_key ); } return $data; From e33357594f38d68db27962803bd549e40b07c72c Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Thu, 20 Feb 2025 14:15:23 +0200 Subject: [PATCH 4/5] refactor: use VISUALIZER_DIRNAME for internal pages --- classes/Visualizer/Module/Admin.php | 6 +++--- index.php | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/classes/Visualizer/Module/Admin.php b/classes/Visualizer/Module/Admin.php index 5d0cea2e9..e1f456946 100644 --- a/classes/Visualizer/Module/Admin.php +++ b/classes/Visualizer/Module/Admin.php @@ -80,7 +80,7 @@ public function __construct( Visualizer_Plugin $plugin ) { $this->_addFilter( 'admin_footer_text', 'render_review_notice' ); if ( ! defined( 'TI_CYPRESS_TESTING' ) ) { - $this->_addFilter( 'themeisle-sdk/survey/' . VISUALIZER_PRODUCT_SLUG, 'get_survey_metadata', 10, 2 ); + $this->_addFilter( 'themeisle-sdk/survey/' . VISUALIZER_DIRNAME, 'get_survey_metadata', 10, 2 ); } if ( defined( 'TI_CYPRESS_TESTING' ) ) { @@ -938,7 +938,7 @@ private function getQuery() { */ public function renderSupportPage() { wp_enqueue_style( 'visualizer-upsell', VISUALIZER_ABSURL . 'css/upsell.css', array(), Visualizer_Plugin::VERSION ); - do_action( 'themeisle_internal_page', VISUALIZER_PRODUCT_SLUG, 'support' ); + do_action( 'themeisle_internal_page', VISUALIZER_DIRNAME, 'support' ); include_once VISUALIZER_ABSPATH . '/templates/support.php'; } @@ -1095,7 +1095,7 @@ public function renderLibraryPage() { ) ); - do_action( 'themeisle_internal_page', VISUALIZER_PRODUCT_SLUG, 'library' ); + do_action( 'themeisle_internal_page', VISUALIZER_DIRNAME, 'library' ); if ( ! apply_filters( 'visualizer_is_business', false ) ) { do_action( 'themeisle_sdk_load_banner', 'visualizer' ); diff --git a/index.php b/index.php index 9f8fa65a3..b5cdea884 100644 --- a/index.php +++ b/index.php @@ -69,7 +69,6 @@ function visualizer_launch() { define( 'VISUALIZER_ABSPATH', dirname( __FILE__ ) ); define( 'VISUALIZER_DIRNAME', basename( VISUALIZER_ABSPATH ) ); define( 'VISUALIZER_REST_VERSION', 1 ); - define( 'VISUALIZER_PRODUCT_SLUG', VISUALIZER_DIRNAME ); // if the below is true, then the js/customization.js in the plugin folder will be used instead of the one in the uploads folder (if it exists). // this is also used in Block.php if ( ! defined( 'VISUALIZER_TEST_JS_CUSTOMIZATION' ) ) { From 7635c8dc64044c0b68afaff885614c849d770c5c Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Fri, 21 Feb 2025 10:54:59 +0200 Subject: [PATCH 5/5] feat: add created charts number to survey metadata --- classes/Visualizer/Module/Admin.php | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/classes/Visualizer/Module/Admin.php b/classes/Visualizer/Module/Admin.php index e1f456946..d2b62f742 100644 --- a/classes/Visualizer/Module/Admin.php +++ b/classes/Visualizer/Module/Admin.php @@ -1254,6 +1254,16 @@ public function get_survey_metadata( $data, $page_slug ) { $plugin_version = $plugin_data['Version']; } + $count_charts_cache_key = 'visualizer_count_charts'; + $charts_number = get_transient( $count_charts_cache_key ); + + if ( false === $charts_number ) { + $charts_number = $this->count_charts( 100 ); + set_transient( $count_charts_cache_key, $charts_number, 100 === $charts_number ? WEEK_IN_SECONDS : 6 * HOUR_IN_SECONDS ); + } else { + $charts_number = strval( $charts_number ); + } + $data = array( 'environmentId' => 'cltef8cut1s7wyyfxy3rlxzs5', 'attributes' => array( @@ -1261,6 +1271,7 @@ public function get_survey_metadata( $data, $page_slug ) { 'pro_version' => defined( 'VISUALIZER_PRO_VERSION' ) ? VISUALIZER_PRO_VERSION : '', 'license_status' => $license_status, 'install_days_number' => $install_days_number, + 'charts_number' => $charts_number, ), ); @@ -1274,4 +1285,23 @@ public function get_survey_metadata( $data, $page_slug ) { return $data; } + + /** + * Count the charts. + * + * @param int $limit The count limit (optional). + * + * @return int The number of charts. + */ + public function count_charts( $limit = -1 ) { + $args = array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'publish', + 'posts_per_page' => $limit, + 'fields' => 'ids', + ); + + $query = new WP_Query( $args); + return $query->post_count; + } }