← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Script_loader.php
+147
-7
3.10.10
→
3.11.10
View file →
| @@ -30,12 +30,8 @@ | ||
| 30 | 30 | * |
| 31 | 31 | * @return bool Should we load ? |
| 32 | 32 | */ |
| 33 | 33 | public function can_load( $product ) { |
| 34 | - if ( apply_filters( 'themeisle_sdk_ran_promos', false ) === true ) { | |
| 35 | - return false; | |
| 36 | - } | |
| 37 | - | |
| 38 | 34 | if ( $this->is_from_partner( $product ) ) { |
| 39 | 35 | return false; |
| 40 | 36 | } |
| 41 | 37 | |
| @@ -55,16 +51,43 @@ | ||
| 55 | 51 | return $this; |
| 56 | 52 | } |
| 57 | 53 | |
| 58 | 54 | /** |
| 59 | - * Setup actions. | |
| 55 | + * Setup actions. Once for all products. | |
| 60 | 56 | */ |
| 61 | 57 | private function setup_actions() { |
| 58 | + | |
| 59 | + if ( apply_filters( 'themeisle_sdk_script_setup', false ) ) { | |
| 60 | + return; | |
| 61 | + } | |
| 62 | + | |
| 62 | 63 | add_filter( 'themeisle_sdk_dependency_script_handler', [ $this, 'get_script_handler' ], 10, 1 ); |
| 63 | 64 | add_action( 'themeisle_sdk_dependency_enqueue_script', [ $this, 'enqueue_script' ], 10, 1 ); |
| 65 | + add_filter( 'themeisle_sdk_secret_masking', [ $this, 'secret_masking' ], 10, 1 ); | |
| 66 | + | |
| 67 | + add_filter( 'themeisle_sdk_script_setup', '__return_true' ); | |
| 68 | + | |
| 69 | + add_action( 'themeisle_internal_page', [ $this, 'load_survey_for_product' ], 10, 2 ); | |
| 64 | 70 | } |
| 65 | 71 | |
| 66 | 72 | /** |
| 73 | + * Load survey for product using internal pages. | |
| 74 | + * | |
| 75 | + * @param string $product_slug Product slug. | |
| 76 | + * @param string $page_slug Page slug. | |
| 77 | + */ | |
| 78 | + public function load_survey_for_product( $product_slug, $page_slug ) { | |
| 79 | + $data = apply_filters( 'themeisle-sdk/survey/' . $product_slug, [], $page_slug ); | |
| 80 | + | |
| 81 | + if ( empty( $data ) || ! is_array( $data ) ) { | |
| 82 | + return; | |
| 83 | + } | |
| 84 | + | |
| 85 | + $handler = $this->get_script_handler( 'survey' ); | |
| 86 | + $this->load_survey( $handler, $data ); | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 67 | 90 | * Get the script handler. |
| 68 | 91 | * |
| 69 | 92 | * @param string $slug The slug of the script. |
| 70 | 93 | * |
| @@ -74,9 +97,9 @@ | ||
| 74 | 97 | if ( ! is_string( $slug ) ) { |
| 75 | 98 | return ''; |
| 76 | 99 | } |
| 77 | 100 | |
| 78 | - if ( 'tracking' !== $slug && 'survey' !== $slug ) { | |
| 101 | + if ( 'tracking' !== $slug && 'survey' !== $slug && 'banner' !== $slug ) { | |
| 79 | 102 | return ''; |
| 80 | 103 | } |
| 81 | 104 | |
| 82 | 105 | return apply_filters( 'themeisle_sdk_dependency_script_handler_name', 'themeisle_sdk_' . $slug . '_script', $slug ); |
| @@ -96,8 +119,10 @@ | ||
| 96 | 119 | if ( 'tracking' === $slug ) { |
| 97 | 120 | $this->load_tracking( $handler ); |
| 98 | 121 | } elseif ( 'survey' === $slug ) { |
| 99 | 122 | $this->load_survey( $handler ); |
| 123 | + } elseif ( 'banner' === $slug ) { | |
| 124 | + $this->load_banner( $handler ); | |
| 100 | 125 | } |
| 101 | 126 | } |
| 102 | 127 | |
| 103 | 128 | /** |
| @@ -103,12 +128,13 @@ | ||
| 103 | 128 | /** |
| 104 | 129 | * Load the survey script. |
| 105 | 130 | * |
| 106 | 131 | * @param string $handler The script handler. |
| 132 | + * @param array $data The survey data. | |
| 107 | 133 | * |
| 108 | 134 | * @return void |
| 109 | 135 | */ |
| 110 | - public function load_survey( $handler ) { | |
| 136 | + public function load_survey( $handler, $data = array() ) { | |
| 111 | 137 | global $themeisle_sdk_max_path; |
| 112 | 138 | $asset_file = require $themeisle_sdk_max_path . '/assets/js/build/survey/survey_deps.asset.php'; |
| 113 | 139 | |
| 114 | 140 | wp_enqueue_script( |
| @@ -117,11 +143,82 @@ | ||
| 117 | 143 | $asset_file['dependencies'], |
| 118 | 144 | $asset_file['version'], |
| 119 | 145 | true |
| 120 | 146 | ); |
| 147 | + | |
| 148 | + $data = array_replace_recursive( $this->get_survey_common_data( $data ), $data ); | |
| 149 | + | |
| 150 | + wp_localize_script( $handler, 'tsdk_survey_data', $data ); | |
| 121 | 151 | } |
| 122 | 152 | |
| 123 | 153 | /** |
| 154 | + * Get the common data in the Formbrick survey format. | |
| 155 | + * | |
| 156 | + * @param array $reference_data Reference data to extrapolate common properties. | |
| 157 | + * | |
| 158 | + * @return array | |
| 159 | + */ | |
| 160 | + public function get_survey_common_data( $reference_data = array() ) { | |
| 161 | + $language = apply_filters( 'themeisle_sdk_current_lang', get_user_locale() ); | |
| 162 | + $available_languages = [ | |
| 163 | + 'de_DE' => 'de', | |
| 164 | + 'de_DE_formal' => 'de', | |
| 165 | + ]; | |
| 166 | + $lang_code = isset( $available_languages[ $language ] ) ? $available_languages[ $language ] : 'en'; | |
| 167 | + | |
| 168 | + $url_parts = wp_parse_url( apply_filters( 'themeisle_sdk_current_site_url', get_site_url() ) ); | |
| 169 | + $clean_url = str_replace( 'www.', '', $url_parts['host'] ); | |
| 170 | + if ( isset( $url_parts['path'] ) ) { | |
| 171 | + $clean_url .= $url_parts['path']; | |
| 172 | + } | |
| 173 | + $user_id = 'u_' . hash( 'crc32b', $clean_url ); | |
| 174 | + | |
| 175 | + $common_data = [ | |
| 176 | + 'userId' => $user_id, | |
| 177 | + 'apiHost' => 'https://app.formbricks.com', | |
| 178 | + 'attributes' => [ | |
| 179 | + 'language' => $lang_code, | |
| 180 | + ], | |
| 181 | + ]; | |
| 182 | + | |
| 183 | + if ( | |
| 184 | + isset( $reference_data['attributes'], $reference_data['attributes']['install_days_number'] ) | |
| 185 | + && is_int( $reference_data['attributes']['install_days_number'] ) | |
| 186 | + ) { | |
| 187 | + $common_data['attributes']['days_since_install'] = $this->install_time_category( $reference_data['attributes']['install_days_number'] ); | |
| 188 | + } | |
| 189 | + | |
| 190 | + return $common_data; | |
| 191 | + } | |
| 192 | + | |
| 193 | + /** | |
| 194 | + * Compute the install time category. | |
| 195 | + * | |
| 196 | + * @param int $install_days_number The number of days passed since installation. | |
| 197 | + * | |
| 198 | + * @return int The category. | |
| 199 | + */ | |
| 200 | + private function install_time_category( $install_days_number ) { | |
| 201 | + if ( 1 < $install_days_number && 8 > $install_days_number ) { | |
| 202 | + return 7; | |
| 203 | + } | |
| 204 | + | |
| 205 | + if ( 8 <= $install_days_number && 31 > $install_days_number ) { | |
| 206 | + return 30; | |
| 207 | + } | |
| 208 | + | |
| 209 | + if ( 30 < $install_days_number && 90 > $install_days_number ) { | |
| 210 | + return 90; | |
| 211 | + } | |
| 212 | + | |
| 213 | + if ( 90 <= $install_days_number ) { | |
| 214 | + return 91; | |
| 215 | + } | |
| 216 | + | |
| 217 | + return 0; | |
| 218 | + } | |
| 219 | + | |
| 220 | + /** | |
| 124 | 221 | * Load the tracking script. |
| 125 | 222 | * |
| 126 | 223 | * @param string $handler The script handler. |
| 127 | 224 | * |
| @@ -137,6 +234,49 @@ | ||
| 137 | 234 | $asset_file['dependencies'], |
| 138 | 235 | $asset_file['version'], |
| 139 | 236 | true |
| 140 | 237 | ); |
| 238 | + } | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * Load the banner script. | |
| 242 | + * | |
| 243 | + * @param string $handler The script handler. | |
| 244 | + * | |
| 245 | + * @return void | |
| 246 | + */ | |
| 247 | + public function load_banner( $handler ) { | |
| 248 | + global $themeisle_sdk_max_path; | |
| 249 | + $asset_file = require $themeisle_sdk_max_path . '/assets/js/build/banner/banner.asset.php'; | |
| 250 | + | |
| 251 | + wp_enqueue_script( | |
| 252 | + $handler, | |
| 253 | + $this->get_sdk_uri() . 'assets/js/build/banner/banner.js', | |
| 254 | + $asset_file['dependencies'], | |
| 255 | + $asset_file['version'], | |
| 256 | + true | |
| 257 | + ); | |
| 258 | + | |
| 259 | + wp_enqueue_style( | |
| 260 | + $handler . '_style', | |
| 261 | + $this->get_sdk_uri() . 'assets/css/banner.css', | |
| 262 | + [], | |
| 263 | + $asset_file['version'] | |
| 264 | + ); | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 268 | + * Mask a secret with `*` for half of its length. | |
| 269 | + * | |
| 270 | + * @param mixed $secret The secret. | |
| 271 | + * | |
| 272 | + * @return mixed The masked secret if secret is a valid string. | |
| 273 | + */ | |
| 274 | + public function secret_masking( $secret ) { | |
| 275 | + if ( empty( $secret ) || ! is_string( $secret ) ) { | |
| 276 | + return $secret; | |
| 277 | + } | |
| 278 | + | |
| 279 | + $half_len = intval( strlen( $secret ) / 2 ); | |
| 280 | + return str_repeat( '*', $half_len ) . substr( $secret, $half_len ); | |
| 141 | 281 | } |
| 142 | 282 | } |