14
14
*/
15
15
class PPOM_Survey {
16
16
17
+ /**
18
+ * The maximum limit of created groups to count. Used in the duration of the cached value.
19
+ *
20
+ * @var int
21
+ */
22
+ public const GROUPS_COUNT_LIMIT = 100 ;
23
+
17
24
/**
18
25
* Reference to singleton insance.
19
26
*
@@ -25,7 +32,7 @@ class PPOM_Survey {
25
32
* Init hooks.
26
33
*/
27
34
public function init () {
28
- add_action ( 'admin_enqueue_scripts ' , array ( $ this , 'enqueue_scripts ' ) );
35
+ add_filter ( 'themeisle-sdk/survey/ ' . PPOM_PRODUCT_SLUG , array ( $ this , 'get_survey_metadata ' ), 10 , 2 );
29
36
}
30
37
31
38
/**
@@ -44,91 +51,46 @@ public static function get_instance() {
44
51
* @return array
45
52
* @see survey.js
46
53
*/
47
- public function get_survey_metadata () {
48
- $ license_data = get_option ( 'ppom_pro_license_data ' , array () );
49
- $ attributes = array ();
50
- $ user_id = 'ppom_ ' . ( ! 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.
51
-
52
- $ days_since_install = round ( ( time () - get_option ( 'woocommerce_product_addon_install ' , 0 ) ) / DAY_IN_SECONDS );
53
- $ install_category = 0 ; // Normalized value.
54
- if ( 0 === $ days_since_install || 1 === $ days_since_install ) {
55
- $ install_category = 0 ;
56
- } elseif ( 1 < $ days_since_install && 8 > $ days_since_install ) {
57
- $ install_category = 7 ;
58
- } elseif ( 8 <= $ days_since_install && 31 > $ days_since_install ) {
59
- $ install_category = 30 ;
60
- } elseif ( 30 < $ days_since_install && 90 > $ days_since_install ) {
61
- $ install_category = 90 ;
62
- } elseif ( 90 <= $ days_since_install ) {
63
- $ install_category = 91 ;
54
+ public function get_survey_metadata ( $ data , $ page_slug ) {
55
+ if ( defined ( 'CYPRESS_TESTING ' ) ) {
56
+ return $ data ;
64
57
}
65
58
66
- $ attributes ['days_since_install ' ] = strval ( $ install_category );
67
- $ attributes ['license_status ' ] = ! empty ( $ license_data ->license ) ? $ license_data ->license : 'invalid ' ;
68
- $ attributes ['free_version ' ] = PPOM_VERSION ;
59
+ $ license_status = apply_filters ( 'product_ppom_license_status ' , 'invalid ' );
60
+ $ license_plan = intval ( apply_filters ( 'product_ppom_license_plan ' , -1 ) );
61
+ $ license_key = apply_filters ( 'product_ppom_license_key ' , '' );
62
+ $ group_fields_count = get_transient ( PPOM_GROUPS_COUNT_CACHE_KEY );
69
63
70
- if ( ! empty ( $ license_data ->plan ) ) {
71
- $ attributes ['plan ' ] = $ this ->plan_category ( $ license_data );
64
+ if ( false === $ group_fields_count ) {
65
+ $ group_fields_count = NM_PersonalizedProduct::get_product_meta_count ( self ::GROUPS_COUNT_LIMIT );
66
+ set_transient ( PPOM_GROUPS_COUNT_CACHE_KEY , $ group_fields_count , self ::GROUPS_COUNT_LIMIT === $ group_fields_count ? WEEK_IN_SECONDS : 12 * HOUR_IN_SECONDS );
72
67
}
73
68
74
- if ( defined ( 'PPOM_PRO_VERSION ' ) ) {
75
- $ attributes ['pro_version ' ] = PPOM_PRO_VERSION ;
76
- }
69
+ $ install_days_number = intval ( ( time () - get_option ( 'woocommerce_product_addon_install ' , time () ) ) / DAY_IN_SECONDS );
77
70
78
- return array (
79
- 'userId ' => $ user_id ,
80
- 'attributes ' => $ attributes ,
71
+ $ data = array (
72
+ 'environmentId ' => 'clza3s4zm000h10km1699nlli ' ,
73
+ 'attributes ' => array (
74
+ 'install_days_number ' => $ install_days_number ,
75
+ 'free_version ' => PPOM_VERSION ,
76
+ 'license_status ' => $ license_status ,
77
+ 'field_groups_count ' => intval ( $ group_fields_count )
78
+ )
81
79
);
82
- }
83
-
84
- /**
85
- * Enqueue scripts.
86
- */
87
- public function enqueue_scripts () {
88
-
89
- if ( defined ( 'CYPRESS_TESTING ' ) ) {
90
- return ;
91
- }
92
80
93
- $ survey_handler = apply_filters ( 'themeisle_sdk_dependency_script_handler ' , 'survey ' );
94
- if ( empty ( $ survey_handler ) ) {
95
- return ;
81
+ if ( 1 <= $ license_plan ) {
82
+ $ data ['attributes ' ]['plan ' ] = NM_PersonalizedProduct::get_license_category ( $ license_plan );
96
83
}
97
84
98
- do_action ( 'themeisle_sdk_dependency_enqueue_script ' , 'survey ' );
99
- wp_enqueue_script ( 'ppom_survey ' , PPOM_URL . '/js/survey.js ' , array ( $ survey_handler ), PPOM_VERSION , true );
100
- wp_localize_script ( 'ppom_survey ' , 'PPOMSurveyData ' , $ this ->get_survey_metadata () );
101
- }
102
-
103
- /**
104
- * Get the plan category for the product plan ID.
105
- *
106
- * @param object $license_data The license data.
107
- * @return int
108
- */
109
- private static function plan_category ( $ license_data ) {
110
-
111
- if ( ! isset ( $ license_data ->plan ) || ! is_numeric ( $ license_data ->plan ) ) {
112
- return 0 ; // Free.
85
+ if ( ! empty ( $ license_key ) ) {
86
+ $ data ['attributes ' ]['license_key ' ] = apply_filters ( 'themeisle_sdk_secret_masking ' , $ license_key );
113
87
}
114
88
115
- $ plan = (int ) $ license_data ->plan ;
116
- $ current_category = -1 ;
117
-
118
- $ categories = array (
119
- '1 ' => array ( 1 , 4 , 9 ), // Personal.
120
- '2 ' => array ( 2 , 5 , 8 ), // Business/Developer.
121
- '3 ' => array ( 3 , 6 , 7 , 10 ), // Agency.
122
- );
123
-
124
- foreach ( $ categories as $ category => $ plans ) {
125
- if ( in_array ( $ plan , $ plans , true ) ) {
126
- $ current_category = (int ) $ category ;
127
- break ;
128
- }
89
+ if ( defined ( 'PPOM_PRO_VERSION ' ) ) {
90
+ $ data ['attributes ' ]['pro_version ' ] = PPOM_PRO_VERSION ;
129
91
}
130
92
131
- return $ current_category ;
93
+ return $ data ;
132
94
}
133
95
}
134
96
}
0 commit comments