| 1 |
<?php |
| 2 |
/** |
| 3 |
* Loader for the ThemeIsleSDK |
| 4 |
* |
| 5 |
* Logic for loading always the latest SDK from the installed themes/plugins. |
| 6 |
* |
| 7 |
* @package ThemeIsleSDK |
| 8 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
* @since 1.1.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
return; |
| 15 |
} |
| 16 |
// Current SDK version and path. |
| 17 |
$themeisle_sdk_version = '3.3.61'; |
| 18 |
$themeisle_sdk_path = dirname( __FILE__ ); |
| 19 |
|
| 20 |
global $themeisle_sdk_max_version; |
| 21 |
global $themeisle_sdk_max_path; |
| 22 |
|
| 23 |
// If this is the latest SDK and it comes from a theme, we should load licenser separately. |
| 24 |
$themeisle_sdk_relative_licenser_path = '/src/Modules/Licenser.php'; |
| 25 |
|
| 26 |
global $themeisle_sdk_abs_licenser_path; |
| 27 |
if ( ! is_file( $themeisle_sdk_path . $themeisle_sdk_relative_licenser_path ) && ! empty( $themeisle_sdk_max_path ) && is_file( $themeisle_sdk_max_path . $themeisle_sdk_relative_licenser_path ) ) { |
| 28 |
$themeisle_sdk_abs_licenser_path = $themeisle_sdk_max_path . $themeisle_sdk_relative_licenser_path; |
| 29 |
add_filter( 'themeisle_sdk_required_files', 'themeisle_sdk_load_licenser_if_present' ); |
| 30 |
} |
| 31 |
|
| 32 |
if ( ( is_null( $themeisle_sdk_max_path ) || version_compare( $themeisle_sdk_version, $themeisle_sdk_max_version ) == 0 ) && |
| 33 |
apply_filters( 'themeisle_sdk_should_overwrite_path', false, $themeisle_sdk_path, $themeisle_sdk_max_path ) ) { |
| 34 |
$themeisle_sdk_max_path = $themeisle_sdk_path; |
| 35 |
} |
| 36 |
|
| 37 |
if ( is_null( $themeisle_sdk_max_version ) || version_compare( $themeisle_sdk_version, $themeisle_sdk_max_version ) > 0 ) { |
| 38 |
$themeisle_sdk_max_version = $themeisle_sdk_version; |
| 39 |
$themeisle_sdk_max_path = $themeisle_sdk_path; |
| 40 |
} |
| 41 |
|
| 42 |
// load the latest sdk version from the active Themeisle products. |
| 43 |
if ( ! function_exists( 'themeisle_sdk_load_licenser_if_present' ) ) : |
| 44 |
/** |
| 45 |
* Always load the licenser, if present. |
| 46 |
* |
| 47 |
* @param array $to_load Previously files to load. |
| 48 |
*/ |
| 49 |
function themeisle_sdk_load_licenser_if_present( $to_load ) { |
| 50 |
global $themeisle_sdk_abs_licenser_path; |
| 51 |
$to_load[] = $themeisle_sdk_abs_licenser_path; |
| 52 |
|
| 53 |
return $to_load; |
| 54 |
} |
| 55 |
endif; |
| 56 |
|
| 57 |
// load the latest sdk version from the active Themeisle products. |
| 58 |
if ( ! function_exists( 'themeisle_sdk_load_latest' ) ) : |
| 59 |
/** |
| 60 |
* Always load the latest sdk version. |
| 61 |
*/ |
| 62 |
function themeisle_sdk_load_latest() { |
| 63 |
/** |
| 64 |
* Don't load the library if we are on < 5.4. |
| 65 |
*/ |
| 66 |
if ( version_compare( PHP_VERSION, '5.4.32', '<' ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
global $themeisle_sdk_max_path; |
| 70 |
require_once $themeisle_sdk_max_path . '/start.php'; |
| 71 |
} |
| 72 |
endif; |
| 73 |
add_action( 'init', 'themeisle_sdk_load_latest' ); |
| 74 |
|
| 75 |
if ( ! function_exists( 'themeisle_sdk_crash_sentinel' ) ) : |
| 76 |
/** |
| 77 |
* Early crash sentinel: minimal shutdown-time capture of fatal errors |
| 78 |
* originating from a registered ThemeIsle product directory. |
| 79 |
* |
| 80 |
* Runs only when the full crash reporter module never registered (e.g. the |
| 81 |
* fatal happened during plugin bootstrap, before the SDK loaded on init). |
| 82 |
* Stores one raw record locally; the module sanitizes and adopts it on the |
| 83 |
* next normal load. Never outputs, never exits, fails silently. |
| 84 |
* |
| 85 |
* Must stay parseable on PHP 5.4, like the rest of this file. |
| 86 |
*/ |
| 87 |
function themeisle_sdk_crash_sentinel() { |
| 88 |
if ( defined( 'THEMEISLE_SDK_CRASH_HANDLER' ) ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
if ( defined( 'WP_SANDBOX_SCRAPING' ) && WP_SANDBOX_SCRAPING ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
if ( ! function_exists( 'apply_filters' ) || ! function_exists( 'get_option' ) || ! function_exists( 'update_option' ) || ! function_exists( 'add_option' ) ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
$error = error_get_last(); |
| 98 |
if ( empty( $error ) || ! isset( $error['type'] ) || ! in_array( (int) $error['type'], array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR ), true ) ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
$error_file = isset( $error['file'] ) ? str_replace( '\\', '/', (string) $error['file'] ) : ''; |
| 102 |
if ( '' === $error_file ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
$products = apply_filters( 'themeisle_sdk_products', array() ); |
| 106 |
if ( empty( $products ) || ! is_array( $products ) ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
foreach ( $products as $basefile ) { |
| 110 |
$product_dir = rtrim( str_replace( '\\', '/', dirname( (string) $basefile ) ), '/' ) . '/'; |
| 111 |
if ( '/' === $product_dir || 0 !== strpos( $error_file, $product_dir ) ) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
$key = str_replace( '-', '_', strtolower( trim( basename( $product_dir ) ) ) ); |
| 115 |
$option = $key . '_crash_data'; |
| 116 |
$data = get_option( $option, array() ); |
| 117 |
if ( ! is_array( $data ) ) { |
| 118 |
$data = array(); |
| 119 |
} |
| 120 |
if ( ! isset( $data['raw'] ) || ! is_array( $data['raw'] ) ) { |
| 121 |
$data['raw'] = array(); |
| 122 |
} |
| 123 |
if ( count( $data['raw'] ) >= 3 ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
foreach ( $data['raw'] as $record ) { |
| 127 |
if ( isset( $record['file'] ) && isset( $record['line'] ) && $record['file'] === $error_file && (int) $record['line'] === (int) $error['line'] ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
} |
| 131 |
$data['raw'][] = array( |
| 132 |
'type' => (int) $error['type'], |
| 133 |
'message' => substr( isset( $error['message'] ) ? (string) $error['message'] : '', 0, 2000 ), |
| 134 |
'file' => $error_file, |
| 135 |
'line' => isset( $error['line'] ) ? (int) $error['line'] : 0, |
| 136 |
'time' => time(), |
| 137 |
); |
| 138 |
if ( false === get_option( $option, false ) ) { |
| 139 |
add_option( $option, $data, '', 'no' ); |
| 140 |
} else { |
| 141 |
update_option( $option, $data ); |
| 142 |
} |
| 143 |
|
| 144 |
return; |
| 145 |
} |
| 146 |
} |
| 147 |
endif; |
| 148 |
|
| 149 |
if ( empty( $GLOBALS['themeisle_sdk_crash_sentinel'] ) ) { |
| 150 |
$GLOBALS['themeisle_sdk_crash_sentinel'] = true; |
| 151 |
register_shutdown_function( 'themeisle_sdk_crash_sentinel' ); |
| 152 |
} |
| 153 |
|
| 154 |
if ( ! function_exists( 'tsdk_utmify' ) ) { |
| 155 |
/** |
| 156 |
* Utmify a link. |
| 157 |
* |
| 158 |
* @param string $url URL to add utms. |
| 159 |
* @param string $area Area in page where this is used ( CTA, image, section name). |
| 160 |
* @param string $location Location, such as customizer, about page. |
| 161 |
* |
| 162 |
* @return string |
| 163 |
*/ |
| 164 |
function tsdk_utmify( $url, $area, $location = null ) { |
| 165 |
static $current_page = null; |
| 166 |
if ( $location === null && $current_page === null ) { |
| 167 |
global $pagenow; |
| 168 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : $pagenow; |
| 169 |
$current_page = isset( $screen->id ) ? $screen->id : ( ( $screen === null ) ? 'non-admin' : $screen ); |
| 170 |
$current_page = sanitize_key( str_replace( '.php', '', $current_page ) ); |
| 171 |
} |
| 172 |
$location = $location === null ? $current_page : $location; |
| 173 |
$is_upgrade_url = strpos( $url, '/upgrade' ) !== false; |
| 174 |
$content = sanitize_key( |
| 175 |
trim( |
| 176 |
str_replace( |
| 177 |
[ |
| 178 |
'https://', |
| 179 |
'themeisle.com', |
| 180 |
'/themes/', |
| 181 |
'/plugins/', |
| 182 |
'/upgrade', |
| 183 |
], |
| 184 |
'', |
| 185 |
$url |
| 186 |
), |
| 187 |
'/' |
| 188 |
) |
| 189 |
); |
| 190 |
$filter_key = sanitize_key( $content ); |
| 191 |
$url_args = [ |
| 192 |
'utm_source' => 'wpadmin', |
| 193 |
'utm_medium' => $location, |
| 194 |
'utm_campaign' => $area, |
| 195 |
'utm_content' => $content, |
| 196 |
]; |
| 197 |
$query_arguments = apply_filters( 'tsdk_utmify_' . $filter_key, $url_args, $url ); |
| 198 |
$utmify_url = esc_url_raw( |
| 199 |
add_query_arg( |
| 200 |
$query_arguments, |
| 201 |
$url |
| 202 |
) |
| 203 |
); |
| 204 |
|
| 205 |
/** |
| 206 |
* Check if there is an affiliate URL for this upgrade link, if so use it. |
| 207 |
*/ |
| 208 |
if ( $is_upgrade_url ) { |
| 209 |
$option_content_key = str_replace( '-', '_', $filter_key ); |
| 210 |
$theme_upgrade_option_name = 'themeisle_af_' . $option_content_key . '_themes_upgrade'; |
| 211 |
$plugin_upgrade_option_name = 'themeisle_af_' . $option_content_key . '_plugins_upgrade'; |
| 212 |
|
| 213 |
$theme_option_url = get_option( $theme_upgrade_option_name, false ); |
| 214 |
if ( ! empty( $theme_option_url ) ) { |
| 215 |
$utmify_url = esc_url_raw( $theme_option_url ); |
| 216 |
} |
| 217 |
$plugin_option_url = get_option( $plugin_upgrade_option_name, false ); |
| 218 |
if ( ! empty( $plugin_option_url ) ) { |
| 219 |
$utmify_url = esc_url_raw( $plugin_option_url ); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
return apply_filters( 'tsdk_utmify_url_' . $filter_key, $utmify_url, $url ); |
| 226 |
} |
| 227 |
|
| 228 |
add_filter( 'tsdk_utmify', 'tsdk_utmify', 10, 3 ); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
if ( ! function_exists( 'tsdk_lstatus' ) ) { |
| 233 |
/** |
| 234 |
* Check license status. |
| 235 |
* |
| 236 |
* @param string $file Product basefile. |
| 237 |
* |
| 238 |
* @return string Status. |
| 239 |
*/ |
| 240 |
function tsdk_lstatus( $file ) { |
| 241 |
return \ThemeisleSDK\Modules\Licenser::status( $file ); |
| 242 |
} |
| 243 |
} |
| 244 |
if ( ! function_exists( 'tsdk_lis_valid' ) ) { |
| 245 |
/** |
| 246 |
* Check if license is valid. |
| 247 |
* |
| 248 |
* @param string $file Product basefile. |
| 249 |
* |
| 250 |
* @return bool Validness. |
| 251 |
*/ |
| 252 |
function tsdk_lis_valid( $file ) { |
| 253 |
return \ThemeisleSDK\Modules\Licenser::is_valid( $file ); |
| 254 |
} |
| 255 |
} |
| 256 |
if ( ! function_exists( 'tsdk_lplan' ) ) { |
| 257 |
/** |
| 258 |
* Get license plan. |
| 259 |
* |
| 260 |
* @param string $file Product basefile. |
| 261 |
* |
| 262 |
* @return string Plan. |
| 263 |
*/ |
| 264 |
function tsdk_lplan( $file ) { |
| 265 |
return \ThemeisleSDK\Modules\Licenser::plan( $file ); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
if ( ! function_exists( 'tsdk_lkey' ) ) { |
| 270 |
/** |
| 271 |
* Get license key. |
| 272 |
* |
| 273 |
* @param string $file Product basefile. |
| 274 |
* |
| 275 |
* @return string Key. |
| 276 |
*/ |
| 277 |
function tsdk_lkey( $file ) { |
| 278 |
return \ThemeisleSDK\Modules\Licenser::key( $file ); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
if ( ! function_exists( 'tsdk_translate_link' ) ) { |
| 283 |
|
| 284 |
/** |
| 285 |
* Function to translate a link based on the current language. |
| 286 |
* |
| 287 |
* @param string $url URL to translate. |
| 288 |
* @param string{'path'|'query'|'domain'} $type Type of localization. Supports path, query and domain. |
| 289 |
* @param array $available_languages Available language to choose from. |
| 290 |
* |
| 291 |
* @return string |
| 292 |
*/ |
| 293 |
function tsdk_translate_link( |
| 294 |
$url, $type = 'path', $available_languages = [ |
| 295 |
'de_DE' => 'de', |
| 296 |
'de_DE_formal' => 'de', |
| 297 |
'es_ES' => 'es', |
| 298 |
'fr_FR' => 'fr', |
| 299 |
'it_IT' => 'it', |
| 300 |
'ja' => 'ja', |
| 301 |
'nl_NL' => 'nl', |
| 302 |
'nl_NL_formal' => 'nl', |
| 303 |
'ro_RO' => 'ro', |
| 304 |
] |
| 305 |
) { |
| 306 |
$language = get_user_locale(); |
| 307 |
if ( ! isset( $available_languages[ $language ] ) ) { |
| 308 |
return $url; |
| 309 |
} |
| 310 |
$code = $available_languages[ $language ]; |
| 311 |
// We asume that false is based on query and add the code via query arg. |
| 312 |
if ( $type === 'query' ) { |
| 313 |
return add_query_arg( 'lang', $code, $url ); |
| 314 |
} |
| 315 |
|
| 316 |
$parsed_url = wp_parse_url( $url ); |
| 317 |
// we replace the domain here based on the localized one. |
| 318 |
if ( $type === 'domain' ) { |
| 319 |
return $parsed_url['scheme'] . '://' . $code . ( isset( $parsed_url['path'] ) ? $parsed_url['path'] : '' ) . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' ) . ( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' ); |
| 320 |
} |
| 321 |
// default is the path based approach. |
| 322 |
$new_path = isset( $parsed_url['path'] ) ? "/$code" . $parsed_url['path'] : "/$code"; |
| 323 |
|
| 324 |
return $parsed_url['scheme'] . '://' . $parsed_url['host'] . $new_path . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' ) . ( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' ); |
| 325 |
|
| 326 |
} |
| 327 |
} |
| 328 |
if ( ! function_exists( 'tsdk_support_link' ) ) { |
| 329 |
|
| 330 |
/** |
| 331 |
* Get Themeisle Support URL. |
| 332 |
* |
| 333 |
* @param string $file Product basefile. |
| 334 |
* |
| 335 |
* @return false|string Return support URL or false if no license is active. |
| 336 |
*/ |
| 337 |
function tsdk_support_link( $file ) { |
| 338 |
|
| 339 |
if ( ! did_action( 'init' ) ) { |
| 340 |
_doing_it_wrong( __FUNCTION__, 'tsdk_support_link() should not be called before the init action.', '3.2.39' ); |
| 341 |
} |
| 342 |
$params = []; |
| 343 |
if ( ! tsdk_lis_valid( $file ) ) { |
| 344 |
return false; |
| 345 |
} |
| 346 |
$product = \ThemeisleSDK\Product::get( $file ); |
| 347 |
if ( ! $product->requires_license() ) { |
| 348 |
return false; |
| 349 |
} |
| 350 |
static $site_params = null; |
| 351 |
if ( $site_params === null ) { |
| 352 |
if ( is_user_logged_in() && function_exists( 'wp_get_current_user' ) ) { |
| 353 |
$current_user = wp_get_current_user(); |
| 354 |
$site_params['semail'] = urlencode( $current_user->user_email ); |
| 355 |
} |
| 356 |
$site_params['swb'] = urlencode( home_url() ); |
| 357 |
global $wp_version; |
| 358 |
$site_params['snv'] = urlencode( sprintf( 'WP-%s-PHP-%s', $wp_version, ( PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION ) ) ); |
| 359 |
} |
| 360 |
$params['slkey'] = tsdk_lkey( $file ); |
| 361 |
$params['sprd'] = urlencode( $product->get_name() ); |
| 362 |
$params['svrs'] = urlencode( $product->get_version() ); |
| 363 |
|
| 364 |
|
| 365 |
return add_query_arg( |
| 366 |
array_merge( $site_params, $params ), |
| 367 |
'https://store.themeisle.com/direct-support/' |
| 368 |
); |
| 369 |
} |
| 370 |
} |
| 371 |
|