diff --git a/.wp-env.json b/.wp-env.json index 53e74110d..b8919eb91 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -3,6 +3,9 @@ "plugins": [ "." ], "env": { "tests": { + "config": { + "TI_TESTING": true + } } } } \ No newline at end of file diff --git a/includes/admin/class-rop-admin.php b/includes/admin/class-rop-admin.php index e1ebbf29b..18b324f87 100644 --- a/includes/admin/class-rop-admin.php +++ b/includes/admin/class-rop-admin.php @@ -400,6 +400,7 @@ public function enqueue_scripts() { wp_deregister_script( 'vue-libs' ); } + $this->register_survey(); } /** @@ -1784,4 +1785,68 @@ public static function rop_check_reached_sharing_limit( $sharing_type = 'tw' ) { } return false; } + + /** + * Get the data used for the survey. + * + * @return array The survey metadata. + */ + public function get_survey_metadata() { + $license_data = get_option( 'tweet_old_post_pro_license_data', array() ); + $attributes = array(); + $user_id = 'rop_' . ( ! empty( $license_data->key ) ? $license_data->key : preg_replace( '/[^\w\d]*/', '', get_site_url() ) ); // Use a normalized version of the site URL as a user ID for free users. + + $days_since_install = round( ( time() - get_option( 'rop_first_install_date', 0 ) ) / DAY_IN_SECONDS ); + $install_category = 0; // Normalized value. + 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; + } + + $attributes['days_since_install'] = strval( $install_category ); + $attributes['license_status'] = ! empty( $license_data->license ) ? $license_data->license : 'invalid'; + $attributes['free_version'] = $this->version; + + if ( ! empty( $license_data->plan ) ) { + $attributes['plan'] = strval( $license_data->plan ); + } + + if ( defined( 'ROP_PRO_VERSION' ) ) { + $attributes['pro_version'] = ROP_PRO_VERSION; + } + + return array( + 'userId' => $user_id, + 'attributes' => $attributes, + ); + } + + /** + * Register the survey script. + * + * It does not register if we are in a testing environment. + * + * @return void + */ + public function register_survey() { + + if ( defined( 'TI_TESTING' ) ) { + return; + } + + $survey_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'survey' ); + if ( empty( $survey_handler ) ) { + return; + } + + do_action( 'themeisle_sdk_dependency_enqueue_script', 'survey' ); + wp_localize_script( $survey_handler, 'ropSurveyData', $this->get_survey_metadata() ); + } } diff --git a/vue/src/rop_main.js b/vue/src/rop_main.js index c13c21087..db20db356 100644 --- a/vue/src/rop_main.js +++ b/vue/src/rop_main.js @@ -22,3 +22,16 @@ window.addEventListener( 'load', function () { }, } ); } ); + +/** + * 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: "clwgcs7ia03df11mgz7gh15od", + apiHost: "https://app.formbricks.com", + ...(window?.ropSurveyData ?? {}), + }); +});