| 1 |
<?php |
| 2 |
/** |
| 3 |
* The dependency model class for ThemeIsle SDK |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Modules |
| 7 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 3.3 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace ThemeisleSDK\Modules; |
| 13 |
|
| 14 |
use ThemeisleSDK\Common\Abstract_Module; |
| 15 |
use ThemeisleSDK\Product; |
| 16 |
|
| 17 |
// Exit if accessed directly. |
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Script loader module for ThemeIsle SDK. |
| 24 |
*/ |
| 25 |
class Script_Loader extends Abstract_Module { |
| 26 |
/** |
| 27 |
* Check if we should load the module for this product. |
| 28 |
* |
| 29 |
* @param Product $product Product to load the module for. |
| 30 |
* |
| 31 |
* @return bool Should we load ? |
| 32 |
*/ |
| 33 |
public function can_load( $product ) { |
| 34 |
if ( $this->is_from_partner( $product ) ) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Load module logic. |
| 43 |
* |
| 44 |
* @param Product $product Product to load. |
| 45 |
* |
| 46 |
* @return Dependancy Module object. |
| 47 |
*/ |
| 48 |
public function load( $product ) { |
| 49 |
$this->product = $product; |
| 50 |
$this->setup_actions(); |
| 51 |
return $this; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Setup actions. Once for all products. |
| 56 |
*/ |
| 57 |
private function setup_actions() { |
| 58 |
|
| 59 |
if ( apply_filters( 'themeisle_sdk_script_setup', false ) ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
add_filter( 'themeisle_sdk_dependency_script_handler', [ $this, 'get_script_handler' ], 10, 1 ); |
| 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 ); |
| 70 |
} |
| 71 |
|
| 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 |
/** |
| 90 |
* Get the script handler. |
| 91 |
* |
| 92 |
* @param string $slug The slug of the script. |
| 93 |
* |
| 94 |
* @return string The script handler. Empty if slug is not a string or not implemented. |
| 95 |
*/ |
| 96 |
public function get_script_handler( $slug ) { |
| 97 |
if ( ! is_string( $slug ) ) { |
| 98 |
return ''; |
| 99 |
} |
| 100 |
|
| 101 |
if ( 'tracking' !== $slug && 'survey' !== $slug ) { |
| 102 |
return ''; |
| 103 |
} |
| 104 |
|
| 105 |
return apply_filters( 'themeisle_sdk_dependency_script_handler_name', 'themeisle_sdk_' . $slug . '_script', $slug ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Enqueue the script. |
| 110 |
* |
| 111 |
* @param string $slug The slug of the script. |
| 112 |
*/ |
| 113 |
public function enqueue_script( $slug ) { |
| 114 |
$handler = apply_filters( 'themeisle_sdk_dependency_script_handler', $slug ); |
| 115 |
if ( empty( $handler ) ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
if ( 'tracking' === $slug ) { |
| 120 |
$this->load_tracking( $handler ); |
| 121 |
} elseif ( 'survey' === $slug ) { |
| 122 |
$this->load_survey( $handler ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Load the survey script. |
| 128 |
* |
| 129 |
* @param string $handler The script handler. |
| 130 |
* @param array $data The survey data. |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function load_survey( $handler, $data = array() ) { |
| 135 |
global $themeisle_sdk_max_path; |
| 136 |
$asset_file = require $themeisle_sdk_max_path . '/assets/js/build/survey/survey_deps.asset.php'; |
| 137 |
|
| 138 |
wp_enqueue_script( |
| 139 |
$handler, |
| 140 |
$this->get_sdk_uri() . 'assets/js/build/survey/survey_deps.js', |
| 141 |
$asset_file['dependencies'], |
| 142 |
$asset_file['version'], |
| 143 |
true |
| 144 |
); |
| 145 |
|
| 146 |
$data = array_replace_recursive( $this->get_survey_common_data( $data ), $data ); |
| 147 |
|
| 148 |
wp_localize_script( $handler, 'tsdk_survey_data', $data ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get the common data in the Formbrick survey format. |
| 153 |
* |
| 154 |
* @param array $reference_data Reference data to extrapolate common properties. |
| 155 |
* |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
public function get_survey_common_data( $reference_data = array() ) { |
| 159 |
$language = apply_filters( 'themeisle_sdk_current_lang', get_user_locale() ); |
| 160 |
$available_languages = [ |
| 161 |
'de_DE' => 'de', |
| 162 |
'de_DE_formal' => 'de', |
| 163 |
]; |
| 164 |
$lang_code = isset( $available_languages[ $language ] ) ? $available_languages[ $language ] : 'en'; |
| 165 |
|
| 166 |
$url_parts = wp_parse_url( apply_filters( 'themeisle_sdk_current_site_url', get_site_url() ) ); |
| 167 |
$clean_url = str_replace( 'www.', '', $url_parts['host'] ); |
| 168 |
if ( isset( $url_parts['path'] ) ) { |
| 169 |
$clean_url .= $url_parts['path']; |
| 170 |
} |
| 171 |
$user_id = 'u_' . hash( 'crc32b', $clean_url ); |
| 172 |
|
| 173 |
$common_data = [ |
| 174 |
'userId' => $user_id, |
| 175 |
'appUrl' => 'https://app.formbricks.com', |
| 176 |
'attributes' => [ |
| 177 |
'language' => $lang_code, |
| 178 |
], |
| 179 |
]; |
| 180 |
|
| 181 |
if ( |
| 182 |
isset( $reference_data['attributes'], $reference_data['attributes']['install_days_number'] ) |
| 183 |
&& is_int( $reference_data['attributes']['install_days_number'] ) |
| 184 |
) { |
| 185 |
$common_data['attributes']['days_since_install'] = $this->install_time_category( $reference_data['attributes']['install_days_number'] ); |
| 186 |
} |
| 187 |
|
| 188 |
return $common_data; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Compute the install time category. |
| 193 |
* |
| 194 |
* @param int $install_days_number The number of days passed since installation. |
| 195 |
* |
| 196 |
* @return int The category. |
| 197 |
*/ |
| 198 |
private function install_time_category( $install_days_number ) { |
| 199 |
if ( 1 < $install_days_number && 8 > $install_days_number ) { |
| 200 |
return 7; |
| 201 |
} |
| 202 |
|
| 203 |
if ( 8 <= $install_days_number && 31 > $install_days_number ) { |
| 204 |
return 30; |
| 205 |
} |
| 206 |
|
| 207 |
if ( 30 < $install_days_number && 90 > $install_days_number ) { |
| 208 |
return 90; |
| 209 |
} |
| 210 |
|
| 211 |
if ( 90 <= $install_days_number ) { |
| 212 |
return 91; |
| 213 |
} |
| 214 |
|
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Load the tracking script. |
| 220 |
* |
| 221 |
* @param string $handler The script handler. |
| 222 |
* |
| 223 |
* @return void |
| 224 |
*/ |
| 225 |
public function load_tracking( $handler ) { |
| 226 |
global $themeisle_sdk_max_path; |
| 227 |
$asset_file = require $themeisle_sdk_max_path . '/assets/js/build/tracking/tracking.asset.php'; |
| 228 |
|
| 229 |
wp_enqueue_script( |
| 230 |
$handler, |
| 231 |
$this->get_sdk_uri() . 'assets/js/build/tracking/tracking.js', |
| 232 |
$asset_file['dependencies'], |
| 233 |
$asset_file['version'], |
| 234 |
true |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Mask a secret with `*` for half of its length. |
| 240 |
* |
| 241 |
* @param mixed $secret The secret. |
| 242 |
* |
| 243 |
* @return mixed The masked secret if secret is a valid string. |
| 244 |
*/ |
| 245 |
public function secret_masking( $secret ) { |
| 246 |
if ( empty( $secret ) || ! is_string( $secret ) ) { |
| 247 |
return $secret; |
| 248 |
} |
| 249 |
|
| 250 |
$half_len = intval( strlen( $secret ) / 2 ); |
| 251 |
return str_repeat( '*', $half_len ) . substr( $secret, $half_len ); |
| 252 |
} |
| 253 |
} |
| 254 |
|