| 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.37'; |
| 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( 'tsdk_utmify' ) ) { |
| 76 |
/** |
| 77 |
* Utmify a link. |
| 78 |
* |
| 79 |
* @param string $url URL to add utms. |
| 80 |
* @param string $area Area in page where this is used ( CTA, image, section name). |
| 81 |
* @param string $location Location, such as customizer, about page. |
| 82 |
* |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
function tsdk_utmify( $url, $area, $location = null ) { |
| 86 |
static $current_page = null; |
| 87 |
if ( $location === null && $current_page === null ) { |
| 88 |
global $pagenow; |
| 89 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : $pagenow; |
| 90 |
$current_page = isset( $screen->id ) ? $screen->id : ( ( $screen === null ) ? 'non-admin' : $screen ); |
| 91 |
$current_page = sanitize_key( str_replace( '.php', '', $current_page ) ); |
| 92 |
} |
| 93 |
$location = $location === null ? $current_page : $location; |
| 94 |
$is_upgrade_url = strpos( $url, '/upgrade' ) !== false; |
| 95 |
$content = sanitize_key( |
| 96 |
trim( |
| 97 |
str_replace( |
| 98 |
[ |
| 99 |
'https://', |
| 100 |
'themeisle.com', |
| 101 |
'/themes/', |
| 102 |
'/plugins/', |
| 103 |
'/upgrade', |
| 104 |
], |
| 105 |
'', |
| 106 |
$url |
| 107 |
), |
| 108 |
'/' |
| 109 |
) |
| 110 |
); |
| 111 |
$filter_key = sanitize_key( $content ); |
| 112 |
$url_args = [ |
| 113 |
'utm_source' => 'wpadmin', |
| 114 |
'utm_medium' => $location, |
| 115 |
'utm_campaign' => $area, |
| 116 |
'utm_content' => $content, |
| 117 |
]; |
| 118 |
$query_arguments = apply_filters( 'tsdk_utmify_' . $filter_key, $url_args, $url ); |
| 119 |
$utmify_url = esc_url_raw( |
| 120 |
add_query_arg( |
| 121 |
$query_arguments, |
| 122 |
$url |
| 123 |
) |
| 124 |
); |
| 125 |
|
| 126 |
/** |
| 127 |
* Check if there is an affiliate URL for this upgrade link, if so use it. |
| 128 |
*/ |
| 129 |
if ( $is_upgrade_url ) { |
| 130 |
$option_content_key = str_replace( '-', '_', $filter_key ); |
| 131 |
$theme_upgrade_option_name = 'themeisle_af_' . $option_content_key . '_themes_upgrade'; |
| 132 |
$plugin_upgrade_option_name = 'themeisle_af_' . $option_content_key . '_plugins_upgrade'; |
| 133 |
|
| 134 |
$theme_option_url = get_option( $theme_upgrade_option_name, false ); |
| 135 |
if ( ! empty( $theme_option_url ) ) { |
| 136 |
$utmify_url = esc_url_raw( $theme_option_url ); |
| 137 |
} |
| 138 |
$plugin_option_url = get_option( $plugin_upgrade_option_name, false ); |
| 139 |
if ( ! empty( $plugin_option_url ) ) { |
| 140 |
$utmify_url = esc_url_raw( $plugin_option_url ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
return apply_filters( 'tsdk_utmify_url_' . $filter_key, $utmify_url, $url ); |
| 147 |
} |
| 148 |
|
| 149 |
add_filter( 'tsdk_utmify', 'tsdk_utmify', 10, 3 ); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
if ( ! function_exists( 'tsdk_lstatus' ) ) { |
| 154 |
/** |
| 155 |
* Check license status. |
| 156 |
* |
| 157 |
* @param string $file Product basefile. |
| 158 |
* |
| 159 |
* @return string Status. |
| 160 |
*/ |
| 161 |
function tsdk_lstatus( $file ) { |
| 162 |
return \ThemeisleSDK\Modules\Licenser::status( $file ); |
| 163 |
} |
| 164 |
} |
| 165 |
if ( ! function_exists( 'tsdk_lis_valid' ) ) { |
| 166 |
/** |
| 167 |
* Check if license is valid. |
| 168 |
* |
| 169 |
* @param string $file Product basefile. |
| 170 |
* |
| 171 |
* @return bool Validness. |
| 172 |
*/ |
| 173 |
function tsdk_lis_valid( $file ) { |
| 174 |
return \ThemeisleSDK\Modules\Licenser::is_valid( $file ); |
| 175 |
} |
| 176 |
} |
| 177 |
if ( ! function_exists( 'tsdk_lplan' ) ) { |
| 178 |
/** |
| 179 |
* Get license plan. |
| 180 |
* |
| 181 |
* @param string $file Product basefile. |
| 182 |
* |
| 183 |
* @return string Plan. |
| 184 |
*/ |
| 185 |
function tsdk_lplan( $file ) { |
| 186 |
return \ThemeisleSDK\Modules\Licenser::plan( $file ); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
if ( ! function_exists( 'tsdk_lkey' ) ) { |
| 191 |
/** |
| 192 |
* Get license key. |
| 193 |
* |
| 194 |
* @param string $file Product basefile. |
| 195 |
* |
| 196 |
* @return string Key. |
| 197 |
*/ |
| 198 |
function tsdk_lkey( $file ) { |
| 199 |
return \ThemeisleSDK\Modules\Licenser::key( $file ); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if ( ! function_exists( 'tsdk_translate_link' ) ) { |
| 204 |
|
| 205 |
/** |
| 206 |
* Function to translate a link based on the current language. |
| 207 |
* |
| 208 |
* @param string $url URL to translate. |
| 209 |
* @param string{'path'|'query'|'domain'} $type Type of localization. Supports path, query and domain. |
| 210 |
* @param array $available_languages Available language to choose from. |
| 211 |
* |
| 212 |
* @return string |
| 213 |
*/ |
| 214 |
function tsdk_translate_link( |
| 215 |
$url, $type = 'path', $available_languages = [ |
| 216 |
'de_DE' => 'de', |
| 217 |
'de_DE_formal' => 'de', |
| 218 |
] |
| 219 |
) { |
| 220 |
$language = get_user_locale(); |
| 221 |
if ( ! isset( $available_languages[ $language ] ) ) { |
| 222 |
return $url; |
| 223 |
} |
| 224 |
$code = $available_languages[ $language ]; |
| 225 |
// We asume that false is based on query and add the code via query arg. |
| 226 |
if ( $type === 'query' ) { |
| 227 |
return add_query_arg( 'lang', $code, $url ); |
| 228 |
} |
| 229 |
|
| 230 |
$parsed_url = wp_parse_url( $url ); |
| 231 |
// we replace the domain here based on the localized one. |
| 232 |
if ( $type === 'domain' ) { |
| 233 |
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'] : '' ); |
| 234 |
} |
| 235 |
// default is the path based approach. |
| 236 |
$new_path = isset( $parsed_url['path'] ) ? "/$code" . $parsed_url['path'] : "/$code"; |
| 237 |
|
| 238 |
return $parsed_url['scheme'] . '://' . $parsed_url['host'] . $new_path . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' ) . ( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' ); |
| 239 |
|
| 240 |
} |
| 241 |
} |
| 242 |
if ( ! function_exists( 'tsdk_support_link' ) ) { |
| 243 |
|
| 244 |
/** |
| 245 |
* Get Themeisle Support URL. |
| 246 |
* |
| 247 |
* @param string $file Product basefile. |
| 248 |
* |
| 249 |
* @return false|string Return support URL or false if no license is active. |
| 250 |
*/ |
| 251 |
function tsdk_support_link( $file ) { |
| 252 |
|
| 253 |
if ( ! did_action( 'init' ) ) { |
| 254 |
_doing_it_wrong( __FUNCTION__, 'tsdk_support_link() should not be called before the init action.', '3.2.39' ); |
| 255 |
} |
| 256 |
$params = []; |
| 257 |
if ( ! tsdk_lis_valid( $file ) ) { |
| 258 |
return false; |
| 259 |
} |
| 260 |
$product = \ThemeisleSDK\Product::get( $file ); |
| 261 |
if ( ! $product->requires_license() ) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
static $site_params = null; |
| 265 |
if ( $site_params === null ) { |
| 266 |
if ( is_user_logged_in() && function_exists( 'wp_get_current_user' ) ) { |
| 267 |
$current_user = wp_get_current_user(); |
| 268 |
$site_params['semail'] = urlencode( $current_user->user_email ); |
| 269 |
} |
| 270 |
$site_params['swb'] = urlencode( home_url() ); |
| 271 |
global $wp_version; |
| 272 |
$site_params['snv'] = urlencode( sprintf( 'WP-%s-PHP-%s', $wp_version, ( PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION ) ) ); |
| 273 |
} |
| 274 |
$params['slkey'] = tsdk_lkey( $file ); |
| 275 |
$params['sprd'] = urlencode( $product->get_name() ); |
| 276 |
$params['svrs'] = urlencode( $product->get_version() ); |
| 277 |
|
| 278 |
|
| 279 |
return add_query_arg( |
| 280 |
array_merge( $site_params, $params ), |
| 281 |
'https://store.themeisle.com/direct-support/' |
| 282 |
); |
| 283 |
} |
| 284 |
} |
| 285 |
|