| @@ -1,459 +1,418 @@ | ||
| 1 | -<?php | |
| 2 | - /** | |
| 3 | - * @package Freemius | |
| 4 | - * @copyright Copyright (c) 2015, Freemius, Inc. | |
| 5 | - * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License | |
| 6 | - * @since 1.1.5 | |
| 7 | - */ | |
| 8 | - | |
| 9 | - if ( ! function_exists( 'fs_normalize_path' ) ) { | |
| 10 | - if ( function_exists( 'wp_normalize_path' ) ) { | |
| 11 | - /** | |
| 12 | - * Normalize a filesystem path. | |
| 13 | - * | |
| 14 | - * Replaces backslashes with forward slashes for Windows systems, and ensures | |
| 15 | - * no duplicate slashes exist. | |
| 16 | - * | |
| 17 | - * @param string $path Path to normalize. | |
| 18 | - * | |
| 19 | - * @return string Normalized path. | |
| 20 | - */ | |
| 21 | - function fs_normalize_path( $path ) { | |
| 22 | - return wp_normalize_path( $path ); | |
| 23 | - } | |
| 24 | - } else { | |
| 25 | - function fs_normalize_path( $path ) { | |
| 26 | - $path = str_replace( '\\', '/', $path ); | |
| 27 | - $path = preg_replace( '|/+|', '/', $path ); | |
| 28 | - | |
| 29 | - return $path; | |
| 30 | - } | |
| 31 | - } | |
| 32 | - } | |
| 33 | - | |
| 34 | - #region Core Redirect (copied from BuddyPress) ----------------------------------------- | |
| 35 | - | |
| 36 | - if ( ! function_exists( 'fs_redirect' ) ) { | |
| 37 | - /** | |
| 38 | - * Redirects to another page, with a workaround for the IIS Set-Cookie bug. | |
| 39 | - * | |
| 40 | - * @link http://support.microsoft.com/kb/q176113/ | |
| 41 | - * @since 1.5.1 | |
| 42 | - * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status. | |
| 43 | - * | |
| 44 | - * @param string $location The path to redirect to. | |
| 45 | - * @param bool $exit If true, exit after redirect (Since 1.2.1.5). | |
| 46 | - * @param int $status Status code to use. | |
| 47 | - * | |
| 48 | - * @return bool False if $location is not set | |
| 49 | - */ | |
| 50 | - function fs_redirect( $location, $exit = true, $status = 302 ) { | |
| 51 | - global $is_IIS; | |
| 52 | - | |
| 53 | - $file = ''; | |
| 54 | - $line = ''; | |
| 55 | - if ( headers_sent( $file, $line ) ) { | |
| 56 | - if ( WP_FS__DEBUG_SDK && class_exists( 'FS_Admin_Notice_Manager' ) ) { | |
| 57 | - $notices = FS_Admin_Notice_Manager::instance( 'global' ); | |
| 58 | - | |
| 59 | - $notices->add( "Freemius failed to redirect the page because the headers have been already sent from line <b><code>{$line}</code></b> in file <b><code>{$file}</code></b>. If it's unexpected, it usually happens due to invalid space and/or EOL character(s).", 'Oops...', 'error' ); | |
| 60 | - } | |
| 61 | - | |
| 62 | - return false; | |
| 63 | - } | |
| 64 | - | |
| 65 | - if ( defined( 'DOING_AJAX' ) ) { | |
| 66 | - // Don't redirect on AJAX calls. | |
| 67 | - return false; | |
| 68 | - } | |
| 69 | - | |
| 70 | - if ( ! $location ) // allows the wp_redirect filter to cancel a redirect | |
| 71 | - { | |
| 72 | - return false; | |
| 73 | - } | |
| 74 | - | |
| 75 | - $location = fs_sanitize_redirect( $location ); | |
| 76 | - | |
| 77 | - if ( $is_IIS ) { | |
| 78 | - header( "Refresh: 0;url=$location" ); | |
| 79 | - } else { | |
| 80 | - if ( php_sapi_name() != 'cgi-fcgi' ) { | |
| 81 | - status_header( $status ); | |
| 82 | - } // This causes problems on IIS and some FastCGI setups | |
| 83 | - header( "Location: $location" ); | |
| 84 | - } | |
| 85 | - | |
| 86 | - if ( $exit ) { | |
| 87 | - exit(); | |
| 88 | - } | |
| 89 | - | |
| 90 | - return true; | |
| 91 | - } | |
| 92 | - | |
| 93 | - if ( ! function_exists( 'fs_sanitize_redirect' ) ) { | |
| 94 | - /** | |
| 95 | - * Sanitizes a URL for use in a redirect. | |
| 96 | - * | |
| 97 | - * @since 2.3 | |
| 98 | - * | |
| 99 | - * @param string $location | |
| 100 | - * | |
| 101 | - * @return string redirect-sanitized URL | |
| 102 | - */ | |
| 103 | - function fs_sanitize_redirect( $location ) { | |
| 104 | - $location = preg_replace( '|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location ); | |
| 105 | - $location = fs_kses_no_null( $location ); | |
| 106 | - | |
| 107 | - // remove %0d and %0a from location | |
| 108 | - $strip = array( '%0d', '%0a' ); | |
| 109 | - $found = true; | |
| 110 | - while ( $found ) { | |
| 111 | - $found = false; | |
| 112 | - foreach ( (array) $strip as $val ) { | |
| 113 | - while ( strpos( $location, $val ) !== false ) { | |
| 114 | - $found = true; | |
| 115 | - $location = str_replace( $val, '', $location ); | |
| 116 | - } | |
| 117 | - } | |
| 118 | - } | |
| 119 | - | |
| 120 | - return $location; | |
| 121 | - } | |
| 122 | - } | |
| 123 | - | |
| 124 | - if ( ! function_exists( 'fs_kses_no_null' ) ) { | |
| 125 | - /** | |
| 126 | - * Removes any NULL characters in $string. | |
| 127 | - * | |
| 128 | - * @since 1.0.0 | |
| 129 | - * | |
| 130 | - * @param string $string | |
| 131 | - * | |
| 132 | - * @return string | |
| 133 | - */ | |
| 134 | - function fs_kses_no_null( $string ) { | |
| 135 | - $string = preg_replace( '/\0+/', '', $string ); | |
| 136 | - $string = preg_replace( '/(\\\\0)+/', '', $string ); | |
| 137 | - | |
| 138 | - return $string; | |
| 139 | - } | |
| 140 | - } | |
| 141 | - } | |
| 142 | - | |
| 143 | - #endregion Core Redirect (copied from BuddyPress) ----------------------------------------- | |
| 144 | - | |
| 145 | - if ( ! function_exists( '__fs' ) ) { | |
| 146 | - global $fs_text_overrides; | |
| 147 | - | |
| 148 | - if ( ! isset( $fs_text_overrides ) ) { | |
| 149 | - $fs_text_overrides = array(); | |
| 150 | - } | |
| 151 | - | |
| 152 | - /** | |
| 153 | - * Retrieve a translated text by key. | |
| 154 | - * | |
| 155 | - * @deprecated Use `fs_text()` instead since methods starting with `__` trigger warnings in Php 7. | |
| 156 | - * | |
| 157 | - * @author Vova Feldman (@svovaf) | |
| 158 | - * @since 1.1.4 | |
| 159 | - * | |
| 160 | - * @param string $key | |
| 161 | - * @param string $slug | |
| 162 | - * | |
| 163 | - * @return string | |
| 164 | - * | |
| 165 | - * @global $fs_text, $fs_text_overrides | |
| 166 | - */ | |
| 167 | - function __fs( $key, $slug = 'freemius' ) { | |
| 168 | - global $fs_text, | |
| 169 | - $fs_module_info_text, | |
| 170 | - $fs_text_overrides; | |
| 171 | - | |
| 172 | - if ( isset( $fs_text_overrides[ $slug ] ) ) { | |
| 173 | - if ( isset( $fs_text_overrides[ $slug ][ $key ] ) ) { | |
| 174 | - return $fs_text_overrides[ $slug ][ $key ]; | |
| 175 | - } | |
| 176 | - | |
| 177 | - $lower_key = strtolower( $key ); | |
| 178 | - if ( isset( $fs_text_overrides[ $slug ][ $lower_key ] ) ) { | |
| 179 | - return $fs_text_overrides[ $slug ][ $lower_key ]; | |
| 180 | - } | |
| 181 | - } | |
| 182 | - | |
| 183 | - if ( ! isset( $fs_text ) ) { | |
| 184 | - $dir = defined( 'WP_FS__DIR_INCLUDES' ) ? | |
| 185 | - WP_FS__DIR_INCLUDES : | |
| 186 | - dirname( __FILE__ ); | |
| 187 | - | |
| 188 | - require_once $dir . '/i18n.php'; | |
| 189 | - } | |
| 190 | - | |
| 191 | - if ( isset( $fs_text[ $key ] ) ) { | |
| 192 | - return $fs_text[ $key ]; | |
| 193 | - } | |
| 194 | - | |
| 195 | - if ( isset( $fs_module_info_text[ $key ] ) ) { | |
| 196 | - return $fs_module_info_text[ $key ]; | |
| 197 | - } | |
| 198 | - | |
| 199 | - return $key; | |
| 200 | - } | |
| 201 | - | |
| 202 | - /** | |
| 203 | - * Output a translated text by key. | |
| 204 | - * | |
| 205 | - * @deprecated Use `fs_echo()` instead for consistency with `fs_text()`. | |
| 206 | - * | |
| 207 | - * @author Vova Feldman (@svovaf) | |
| 208 | - * @since 1.1.4 | |
| 209 | - * | |
| 210 | - * @param string $key | |
| 211 | - * @param string $slug | |
| 212 | - */ | |
| 213 | - function _efs( $key, $slug = 'freemius' ) { | |
| 214 | - fs_echo( $key, $slug ); | |
| 215 | - } | |
| 216 | - } | |
| 217 | - | |
| 218 | - if ( ! function_exists( 'fs_override_i18n' ) ) { | |
| 219 | - /** | |
| 220 | - * Override default i18n text phrases. | |
| 221 | - * | |
| 222 | - * @author Vova Feldman (@svovaf) | |
| 223 | - * @since 1.1.6 | |
| 224 | - * | |
| 225 | - * @param string[] $key_value | |
| 226 | - * @param string $slug | |
| 227 | - * | |
| 228 | - * @global $fs_text_overrides | |
| 229 | - */ | |
| 230 | - function fs_override_i18n( array $key_value, $slug = 'freemius' ) { | |
| 231 | - global $fs_text_overrides; | |
| 232 | - | |
| 233 | - if ( ! isset( $fs_text_overrides[ $slug ] ) ) { | |
| 234 | - $fs_text_overrides[ $slug ] = array(); | |
| 235 | - } | |
| 236 | - | |
| 237 | - foreach ( $key_value as $key => $value ) { | |
| 238 | - $fs_text_overrides[ $slug ][ $key ] = $value; | |
| 239 | - } | |
| 240 | - } | |
| 241 | - } | |
| 242 | - | |
| 243 | - if ( ! function_exists( 'fs_get_ip' ) ) { | |
| 244 | - /** | |
| 245 | - * Get client IP. | |
| 246 | - * | |
| 247 | - * @author Vova Feldman (@svovaf) | |
| 248 | - * @since 1.1.2 | |
| 249 | - * | |
| 250 | - * @return string|null | |
| 251 | - */ | |
| 252 | - function fs_get_ip() { | |
| 253 | - $fields = array( | |
| 254 | - 'HTTP_CF_CONNECTING_IP', | |
| 255 | - 'HTTP_CLIENT_IP', | |
| 256 | - 'HTTP_X_FORWARDED_FOR', | |
| 257 | - 'HTTP_X_FORWARDED', | |
| 258 | - 'HTTP_FORWARDED_FOR', | |
| 259 | - 'HTTP_FORWARDED', | |
| 260 | - 'REMOTE_ADDR', | |
| 261 | - ); | |
| 262 | - | |
| 263 | - foreach ( $fields as $ip_field ) { | |
| 264 | - if ( ! empty( $_SERVER[ $ip_field ] ) ) { | |
| 265 | - return $_SERVER[ $ip_field ]; | |
| 266 | - } | |
| 267 | - } | |
| 268 | - | |
| 269 | - return null; | |
| 270 | - } | |
| 271 | - } | |
| 272 | - | |
| 273 | - /** | |
| 274 | - * Leverage backtrace to find caller plugin main file path. | |
| 275 | - * | |
| 276 | - * @author Vova Feldman (@svovaf) | |
| 277 | - * @since 1.0.6 | |
| 278 | - * | |
| 279 | - * @return string | |
| 280 | - */ | |
| 281 | - function fs_find_caller_plugin_file() { | |
| 282 | - /** | |
| 283 | - * All the code below will be executed once on activation. | |
| 284 | - * If the user changes the main plugin's file name, the file_exists() | |
| 285 | - * will catch it. | |
| 286 | - */ | |
| 287 | - if ( ! function_exists( 'get_plugins' ) ) { | |
| 288 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 289 | - } | |
| 290 | - | |
| 291 | - $all_plugins = get_plugins(); | |
| 292 | - $all_plugins_paths = array(); | |
| 293 | - | |
| 294 | - // Get active plugin's main files real full names (might be symlinks). | |
| 295 | - foreach ( $all_plugins as $relative_path => &$data ) { | |
| 296 | - $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) ); | |
| 297 | - } | |
| 298 | - | |
| 299 | - $plugin_file = null; | |
| 300 | - for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) { | |
| 301 | - if ( empty( $bt[ $i ]['file'] ) ) { | |
| 302 | - continue; | |
| 303 | - } | |
| 304 | - | |
| 305 | - if ( in_array( fs_normalize_path( $bt[ $i ]['file'] ), $all_plugins_paths ) ) { | |
| 306 | - $plugin_file = $bt[ $i ]['file']; | |
| 307 | - break; | |
| 308 | - } | |
| 309 | - } | |
| 310 | - | |
| 311 | - if ( is_null( $plugin_file ) ) { | |
| 312 | - // Throw an error to the developer in case of some edge case dev environment. | |
| 313 | - wp_die( | |
| 314 | - "Freemius SDK couldn't find the plugin's main file. Please contact sdk@freemius.com with the current error.", | |
| 315 | - 'Error', | |
| 316 | - array( 'back_link' => true ) | |
| 317 | - ); | |
| 318 | - } | |
| 319 | - | |
| 320 | - return $plugin_file; | |
| 321 | - } | |
| 322 | - | |
| 323 | - require_once dirname( __FILE__ ) . '/supplements/fs-essential-functions-1.1.7.1.php'; | |
| 324 | - | |
| 325 | - /** | |
| 326 | - * Update SDK newest version reference. | |
| 327 | - * | |
| 328 | - * @author Vova Feldman (@svovaf) | |
| 329 | - * @since 1.1.6 | |
| 330 | - * | |
| 331 | - * @param string $sdk_relative_path | |
| 332 | - * @param string|bool $plugin_file | |
| 333 | - * | |
| 334 | - * @global $fs_active_plugins | |
| 335 | - */ | |
| 336 | - function fs_update_sdk_newest_version( $sdk_relative_path, $plugin_file = false ) { | |
| 337 | - global $fs_active_plugins; | |
| 338 | - | |
| 339 | - if ( ! is_string( $plugin_file ) ) { | |
| 340 | - $plugin_file = plugin_basename( fs_find_caller_plugin_file() ); | |
| 341 | - } | |
| 342 | - | |
| 343 | - $fs_active_plugins->newest = (object) array( | |
| 344 | - 'plugin_path' => $plugin_file, | |
| 345 | - 'sdk_path' => $sdk_relative_path, | |
| 346 | - 'version' => $fs_active_plugins->plugins[ $sdk_relative_path ]->version, | |
| 347 | - 'in_activation' => ! is_plugin_active( $plugin_file ), | |
| 348 | - 'timestamp' => time(), | |
| 349 | - ); | |
| 350 | - | |
| 351 | - // Update DB with latest SDK version and path. | |
| 352 | - update_option( 'fs_active_plugins', $fs_active_plugins ); | |
| 353 | - } | |
| 354 | - | |
| 355 | - /** | |
| 356 | - * Reorder the plugins load order so the plugin with the newest Freemius SDK is loaded first. | |
| 357 | - * | |
| 358 | - * @author Vova Feldman (@svovaf) | |
| 359 | - * @since 1.1.6 | |
| 360 | - * | |
| 361 | - * @return bool Was plugin order changed. Return false if plugin was loaded first anyways. | |
| 362 | - * | |
| 363 | - * @global $fs_active_plugins | |
| 364 | - */ | |
| 365 | - function fs_newest_sdk_plugin_first() { | |
| 366 | - global $fs_active_plugins; | |
| 367 | - | |
| 368 | - /** | |
| 369 | - * @todo Multi-site network activated plugin are always loaded prior to site plugins so if there's a a plugin activated in the network mode that has an older version of the SDK of another plugin which is site activated that has new SDK version, the fs-essential-functions.php will be loaded from the older SDK. Same thing about MU plugins (loaded even before network activated plugins). | |
| 370 | - * | |
| 371 | - * @link https://github.com/Freemius/wordpress-sdk/issues/26 | |
| 372 | - */ | |
| 373 | -// $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' ); | |
| 374 | - | |
| 375 | - $active_plugins = get_option( 'active_plugins' ); | |
| 376 | - $newest_sdk_plugin_key = array_search( $fs_active_plugins->newest->plugin_path, $active_plugins ); | |
| 377 | - if ( 0 == $newest_sdk_plugin_key ) { | |
| 378 | - // if it's 0 it's the first plugin already, no need to continue | |
| 379 | - return false; | |
| 380 | - } | |
| 381 | - | |
| 382 | - array_splice( $active_plugins, $newest_sdk_plugin_key, 1 ); | |
| 383 | - array_unshift( $active_plugins, $fs_active_plugins->newest->plugin_path ); | |
| 384 | - update_option( 'active_plugins', $active_plugins ); | |
| 385 | - | |
| 386 | - return true; | |
| 387 | - } | |
| 388 | - | |
| 389 | - /** | |
| 390 | - * Go over all Freemius SDKs in the system and find and "remember" | |
| 391 | - * the newest SDK which is associated with an active plugin. | |
| 392 | - * | |
| 393 | - * @author Vova Feldman (@svovaf) | |
| 394 | - * @since 1.1.6 | |
| 395 | - * | |
| 396 | - * @global $fs_active_plugins | |
| 397 | - */ | |
| 398 | - function fs_fallback_to_newest_active_sdk() { | |
| 399 | - global $fs_active_plugins; | |
| 400 | - | |
| 401 | - /** | |
| 402 | - * @var object $newest_sdk_data | |
| 403 | - */ | |
| 404 | - $newest_sdk_data = null; | |
| 405 | - $newest_sdk_path = null; | |
| 406 | - | |
| 407 | - foreach ( $fs_active_plugins->plugins as $sdk_relative_path => $data ) { | |
| 408 | - if ( is_null( $newest_sdk_data ) || version_compare( $data->version, $newest_sdk_data->version, '>' ) | |
| 409 | - ) { | |
| 410 | - // If plugin inactive or SDK starter file doesn't exist, remove SDK reference. | |
| 411 | - if ( ! is_plugin_active( $data->plugin_path ) || | |
| 412 | - ! file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $sdk_relative_path . '/start.php' ) ) | |
| 413 | - ) { | |
| 414 | - unset( $fs_active_plugins->plugins[ $sdk_relative_path ] ); | |
| 415 | - | |
| 416 | - // No need to store the data since it will be stored in fs_update_sdk_newest_version() | |
| 417 | - // or explicitly with update_option(). | |
| 418 | - } else { | |
| 419 | - $newest_sdk_data = $data; | |
| 420 | - $newest_sdk_path = $sdk_relative_path; | |
| 421 | - } | |
| 422 | - } | |
| 423 | - } | |
| 424 | - | |
| 425 | - if ( is_null( $newest_sdk_data ) ) { | |
| 426 | - // Couldn't find any SDK reference. | |
| 427 | - $fs_active_plugins = new stdClass(); | |
| 428 | - update_option( 'fs_active_plugins', $fs_active_plugins ); | |
| 429 | - } else { | |
| 430 | - fs_update_sdk_newest_version( $newest_sdk_path, $newest_sdk_data->plugin_path ); | |
| 431 | - } | |
| 432 | - } | |
| 433 | - | |
| 434 | - #region Actions / Filters ----------------------------------------- | |
| 435 | - | |
| 436 | - /** | |
| 437 | - * Apply filter for specific plugin. | |
| 438 | - * | |
| 439 | - * @author Vova Feldman (@svovaf) | |
| 440 | - * @since 1.0.9 | |
| 441 | - * | |
| 442 | - * @param string $slug Plugin slug | |
| 443 | - * @param string $tag The name of the filter hook. | |
| 444 | - * @param mixed $value The value on which the filters hooked to `$tag` are applied on. | |
| 445 | - * | |
| 446 | - * @return mixed The filtered value after all hooked functions are applied to it. | |
| 447 | - * | |
| 448 | - * @uses apply_filters() | |
| 449 | - */ | |
| 450 | - function fs_apply_filter( $slug, $tag, $value ) { | |
| 451 | - $args = func_get_args(); | |
| 452 | - | |
| 453 | - return call_user_func_array( 'apply_filters', array_merge( | |
| 454 | - array( "fs_{$tag}_{$slug}" ), | |
| 455 | - array_slice( $args, 2 ) ) | |
| 456 | - ); | |
| 457 | - } | |
| 458 | - | |
| 459 | - #endregion Actions / Filters ----------------------------------------- | |
| 1 | +<?php | |
| 2 | + /** | |
| 3 | + * IMPORTANT: | |
| 4 | + * This file will be loaded based on the order of the plugins/themes load. | |
| 5 | + * If there's a theme and a plugin using Freemius, the plugin's essential | |
| 6 | + * file will always load first. | |
| 7 | + * | |
| 8 | + * @package Freemius | |
| 9 | + * @copyright Copyright (c) 2015, Freemius, Inc. | |
| 10 | + * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 | |
| 11 | + * @since 1.1.5 | |
| 12 | + */ | |
| 13 | + | |
| 14 | + if ( ! function_exists( 'fs_normalize_path' ) ) { | |
| 15 | + if ( function_exists( 'wp_normalize_path' ) ) { | |
| 16 | + /** | |
| 17 | + * Normalize a filesystem path. | |
| 18 | + * | |
| 19 | + * Replaces backslashes with forward slashes for Windows systems, and ensures | |
| 20 | + * no duplicate slashes exist. | |
| 21 | + * | |
| 22 | + * @param string $path Path to normalize. | |
| 23 | + * | |
| 24 | + * @return string Normalized path. | |
| 25 | + */ | |
| 26 | + function fs_normalize_path( $path ) { | |
| 27 | + return wp_normalize_path( $path ); | |
| 28 | + } | |
| 29 | + } else { | |
| 30 | + function fs_normalize_path( $path ) { | |
| 31 | + $path = str_replace( '\\', '/', $path ); | |
| 32 | + $path = preg_replace( '|/+|', '/', $path ); | |
| 33 | + | |
| 34 | + return $path; | |
| 35 | + } | |
| 36 | + } | |
| 37 | + } | |
| 38 | + | |
| 39 | + require_once dirname( __FILE__ ) . '/supplements/fs-essential-functions-2.2.1.php'; | |
| 40 | + | |
| 41 | + #region Core Redirect (copied from BuddyPress) ----------------------------------------- | |
| 42 | + | |
| 43 | + if ( ! function_exists( 'fs_redirect' ) ) { | |
| 44 | + /** | |
| 45 | + * Redirects to another page, with a workaround for the IIS Set-Cookie bug. | |
| 46 | + * | |
| 47 | + * @link http://support.microsoft.com/kb/q176113/ | |
| 48 | + * @since 1.5.1 | |
| 49 | + * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status. | |
| 50 | + * | |
| 51 | + * @param string $location The path to redirect to. | |
| 52 | + * @param bool $exit If true, exit after redirect (Since 1.2.1.5). | |
| 53 | + * @param int $status Status code to use. | |
| 54 | + * | |
| 55 | + * @return bool False if $location is not set | |
| 56 | + */ | |
| 57 | + function fs_redirect( $location, $exit = true, $status = 302 ) { | |
| 58 | + global $is_IIS; | |
| 59 | + | |
| 60 | + $file = ''; | |
| 61 | + $line = ''; | |
| 62 | + if ( headers_sent($file, $line) ) { | |
| 63 | + if ( WP_FS__DEBUG_SDK && class_exists( 'FS_Admin_Notices' ) ) { | |
| 64 | + $notices = FS_Admin_Notices::instance( 'global' ); | |
| 65 | + | |
| 66 | + $notices->add( "Freemius failed to redirect the page because the headers have been already sent from line <b><code>{$line}</code></b> in file <b><code>{$file}</code></b>. If it's unexpected, it usually happens due to invalid space and/or EOL character(s).", 'Oops...', 'error' ); | |
| 67 | + } | |
| 68 | + | |
| 69 | + return false; | |
| 70 | + } | |
| 71 | + | |
| 72 | + if ( defined( 'DOING_AJAX' ) ) { | |
| 73 | + // Don't redirect on AJAX calls. | |
| 74 | + return false; | |
| 75 | + } | |
| 76 | + | |
| 77 | + if ( ! $location ) // allows the wp_redirect filter to cancel a redirect | |
| 78 | + { | |
| 79 | + return false; | |
| 80 | + } | |
| 81 | + | |
| 82 | + $location = fs_sanitize_redirect( $location ); | |
| 83 | + | |
| 84 | + if ( $is_IIS ) { | |
| 85 | + header( "Refresh: 0;url=$location" ); | |
| 86 | + } else { | |
| 87 | + if ( php_sapi_name() != 'cgi-fcgi' ) { | |
| 88 | + status_header( $status ); | |
| 89 | + } // This causes problems on IIS and some FastCGI setups | |
| 90 | + header( "Location: $location" ); | |
| 91 | + } | |
| 92 | + | |
| 93 | + if ( $exit ) { | |
| 94 | + exit(); | |
| 95 | + } | |
| 96 | + | |
| 97 | + return true; | |
| 98 | + } | |
| 99 | + | |
| 100 | + if ( ! function_exists( 'fs_sanitize_redirect' ) ) { | |
| 101 | + /** | |
| 102 | + * Sanitizes a URL for use in a redirect. | |
| 103 | + * | |
| 104 | + * @since 2.3 | |
| 105 | + * | |
| 106 | + * @param string $location | |
| 107 | + * | |
| 108 | + * @return string redirect-sanitized URL | |
| 109 | + */ | |
| 110 | + function fs_sanitize_redirect( $location ) { | |
| 111 | + $location = preg_replace( '|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location ); | |
| 112 | + $location = fs_kses_no_null( $location ); | |
| 113 | + | |
| 114 | + // remove %0d and %0a from location | |
| 115 | + $strip = array( '%0d', '%0a' ); | |
| 116 | + $found = true; | |
| 117 | + while ( $found ) { | |
| 118 | + $found = false; | |
| 119 | + foreach ( (array) $strip as $val ) { | |
| 120 | + while ( strpos( $location, $val ) !== false ) { | |
| 121 | + $found = true; | |
| 122 | + $location = str_replace( $val, '', $location ); | |
| 123 | + } | |
| 124 | + } | |
| 125 | + } | |
| 126 | + | |
| 127 | + return $location; | |
| 128 | + } | |
| 129 | + } | |
| 130 | + | |
| 131 | + if ( ! function_exists( 'fs_kses_no_null' ) ) { | |
| 132 | + /** | |
| 133 | + * Removes any NULL characters in $string. | |
| 134 | + * | |
| 135 | + * @since 1.0.0 | |
| 136 | + * | |
| 137 | + * @param string $string | |
| 138 | + * | |
| 139 | + * @return string | |
| 140 | + */ | |
| 141 | + function fs_kses_no_null( $string ) { | |
| 142 | + $string = preg_replace( '/\0+/', '', $string ); | |
| 143 | + $string = preg_replace( '/(\\\\0)+/', '', $string ); | |
| 144 | + | |
| 145 | + return $string; | |
| 146 | + } | |
| 147 | + } | |
| 148 | + } | |
| 149 | + | |
| 150 | + #endregion Core Redirect (copied from BuddyPress) ----------------------------------------- | |
| 151 | + | |
| 152 | + if ( ! function_exists( 'fs_get_ip' ) ) { | |
| 153 | + /** | |
| 154 | + * Get server IP. | |
| 155 | + * | |
| 156 | + * @since 2.5.1 This method returns the server IP. | |
| 157 | + * | |
| 158 | + * @author Vova Feldman (@svovaf) | |
| 159 | + * @since 1.1.2 | |
| 160 | + * | |
| 161 | + * @return string|null | |
| 162 | + */ | |
| 163 | + function fs_get_ip() { | |
| 164 | + return empty( $_SERVER[ 'SERVER_ADDR' ] ) ? | |
| 165 | + null : | |
| 166 | + $_SERVER[ 'SERVER_ADDR' ]; | |
| 167 | + } | |
| 168 | + } | |
| 169 | + | |
| 170 | + if ( ! function_exists( 'fs_find_caller_plugin_file' ) ) { | |
| 171 | + /** | |
| 172 | + * Leverage backtrace to find caller plugin main file path. | |
| 173 | + * | |
| 174 | + * @author Vova Feldman (@svovaf) | |
| 175 | + * @since 1.0.6 | |
| 176 | + * | |
| 177 | + * @return string | |
| 178 | + */ | |
| 179 | + function fs_find_caller_plugin_file() { | |
| 180 | + /** | |
| 181 | + * All the code below will be executed once on activation. | |
| 182 | + * If the user changes the main plugin's file name, the file_exists() | |
| 183 | + * will catch it. | |
| 184 | + */ | |
| 185 | + if ( ! function_exists( 'get_plugins' ) ) { | |
| 186 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 187 | + } | |
| 188 | + | |
| 189 | + $all_plugins = fs_get_plugins( true ); | |
| 190 | + $all_plugins_paths = array(); | |
| 191 | + | |
| 192 | + // Get active plugin's main files real full names (might be symlinks). | |
| 193 | + foreach ( $all_plugins as $relative_path => $data ) { | |
| 194 | + $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) ); | |
| 195 | + } | |
| 196 | + | |
| 197 | + $plugin_file = null; | |
| 198 | + for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) { | |
| 199 | + if ( empty( $bt[ $i ]['file'] ) ) { | |
| 200 | + continue; | |
| 201 | + } | |
| 202 | + | |
| 203 | + if ( in_array( fs_normalize_path( $bt[ $i ]['file'] ), $all_plugins_paths ) ) { | |
| 204 | + $plugin_file = $bt[ $i ]['file']; | |
| 205 | + break; | |
| 206 | + } | |
| 207 | + } | |
| 208 | + | |
| 209 | + if ( is_null( $plugin_file ) ) { | |
| 210 | + // Throw an error to the developer in case of some edge case dev environment. | |
| 211 | + wp_die( | |
| 212 | + 'Freemius SDK couldn\'t find the plugin\'s main file. Please contact sdk@freemius.com with the current error.', | |
| 213 | + 'Error', | |
| 214 | + array( 'back_link' => true ) | |
| 215 | + ); | |
| 216 | + } | |
| 217 | + | |
| 218 | + return $plugin_file; | |
| 219 | + } | |
| 220 | + } | |
| 221 | + | |
| 222 | + require_once dirname( __FILE__ ) . '/supplements/fs-essential-functions-1.1.7.1.php'; | |
| 223 | + | |
| 224 | + if ( ! function_exists( 'fs_update_sdk_newest_version' ) ) { | |
| 225 | + /** | |
| 226 | + * Update SDK newest version reference. | |
| 227 | + * | |
| 228 | + * @author Vova Feldman (@svovaf) | |
| 229 | + * @since 1.1.6 | |
| 230 | + * | |
| 231 | + * @param string $sdk_relative_path | |
| 232 | + * @param string|bool $plugin_file | |
| 233 | + * | |
| 234 | + * @global $fs_active_plugins | |
| 235 | + */ | |
| 236 | + function fs_update_sdk_newest_version( $sdk_relative_path, $plugin_file = false ) { | |
| 237 | + /** | |
| 238 | + * If there is a plugin running an older version of FS (1.2.1 or below), the `fs_update_sdk_newest_version()` | |
| 239 | + * function in the older version will be used instead of this one. But since the older version is using | |
| 240 | + * the `is_plugin_active` function to check if a plugin is active, passing the theme's `plugin_path` to the | |
| 241 | + * `is_plugin_active` function will return false since the path is not a plugin path, so `in_activation` will be | |
| 242 | + * `true` for theme modules and the upgrading of the SDK version to 1.2.2 or newer version will work fine. | |
| 243 | + * | |
| 244 | + * Future versions that will call this function will use the proper logic here instead of just relying on the | |
| 245 | + * `is_plugin_active` function to fail for themes. | |
| 246 | + * | |
| 247 | + * @author Leo Fajardo (@leorw) | |
| 248 | + * @since 1.2.2 | |
| 249 | + */ | |
| 250 | + | |
| 251 | + global $fs_active_plugins; | |
| 252 | + | |
| 253 | + $newest_sdk = $fs_active_plugins->plugins[ $sdk_relative_path ]; | |
| 254 | + | |
| 255 | + if ( ! is_string( $plugin_file ) ) { | |
| 256 | + $plugin_file = plugin_basename( fs_find_caller_plugin_file() ); | |
| 257 | + } | |
| 258 | + | |
| 259 | + if ( ! isset( $newest_sdk->type ) || 'theme' !== $newest_sdk->type ) { | |
| 260 | + if ( ! function_exists( 'is_plugin_active' ) ) { | |
| 261 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 262 | + } | |
| 263 | + | |
| 264 | + $in_activation = ( ! is_plugin_active( $plugin_file ) ); | |
| 265 | + } else { | |
| 266 | + $theme = wp_get_theme(); | |
| 267 | + $in_activation = ( $newest_sdk->plugin_path == $theme->stylesheet ); | |
| 268 | + } | |
| 269 | + | |
| 270 | + $fs_active_plugins->newest = (object) array( | |
| 271 | + 'plugin_path' => $plugin_file, | |
| 272 | + 'sdk_path' => $sdk_relative_path, | |
| 273 | + 'version' => $newest_sdk->version, | |
| 274 | + 'in_activation' => $in_activation, | |
| 275 | + 'timestamp' => time(), | |
| 276 | + ); | |
| 277 | + | |
| 278 | + // Update DB with latest SDK version and path. | |
| 279 | + update_option( 'fs_active_plugins', $fs_active_plugins ); | |
| 280 | + } | |
| 281 | + } | |
| 282 | + | |
| 283 | + if ( ! function_exists( 'fs_newest_sdk_plugin_first' ) ) { | |
| 284 | + /** | |
| 285 | + * Reorder the plugins load order so the plugin with the newest Freemius SDK is loaded first. | |
| 286 | + * | |
| 287 | + * @author Vova Feldman (@svovaf) | |
| 288 | + * @since 1.1.6 | |
| 289 | + * | |
| 290 | + * @return bool Was plugin order changed. Return false if plugin was loaded first anyways. | |
| 291 | + * | |
| 292 | + * @global $fs_active_plugins | |
| 293 | + */ | |
| 294 | + function fs_newest_sdk_plugin_first() { | |
| 295 | + global $fs_active_plugins; | |
| 296 | + | |
| 297 | + /** | |
| 298 | + * @todo Multi-site network activated plugin are always loaded prior to site plugins so if there's a plugin activated in the network mode that has an older version of the SDK of another plugin which is site activated that has new SDK version, the fs-essential-functions.php will be loaded from the older SDK. Same thing about MU plugins (loaded even before network activated plugins). | |
| 299 | + * | |
| 300 | + * @link https://github.com/Freemius/wordpress-sdk/issues/26 | |
| 301 | + */ | |
| 302 | + | |
| 303 | + $newest_sdk_plugin_path = $fs_active_plugins->newest->plugin_path; | |
| 304 | + | |
| 305 | + $active_plugins = get_option( 'active_plugins', array() ); | |
| 306 | + $updated_active_plugins = array( $newest_sdk_plugin_path ); | |
| 307 | + | |
| 308 | + $plugin_found = false; | |
| 309 | + $is_first_path = true; | |
| 310 | + | |
| 311 | + foreach ( $active_plugins as $key => $plugin_path ) { | |
| 312 | + if ( $plugin_path === $newest_sdk_plugin_path ) { | |
| 313 | + if ( $is_first_path ) { | |
| 314 | + // if it's the first plugin already, no need to continue | |
| 315 | + return false; | |
| 316 | + } | |
| 317 | + | |
| 318 | + $plugin_found = true; | |
| 319 | + | |
| 320 | + // Skip the plugin (it is already added as the 1st item of $updated_active_plugins). | |
| 321 | + continue; | |
| 322 | + } | |
| 323 | + | |
| 324 | + $updated_active_plugins[] = $plugin_path; | |
| 325 | + | |
| 326 | + if ( $is_first_path ) { | |
| 327 | + $is_first_path = false; | |
| 328 | + } | |
| 329 | + } | |
| 330 | + | |
| 331 | + if ( $plugin_found ) { | |
| 332 | + update_option( 'active_plugins', $updated_active_plugins ); | |
| 333 | + | |
| 334 | + return true; | |
| 335 | + } | |
| 336 | + | |
| 337 | + if ( is_multisite() ) { | |
| 338 | + // Plugin is network active. | |
| 339 | + $network_active_plugins = get_site_option( 'active_sitewide_plugins', array() ); | |
| 340 | + | |
| 341 | + if ( isset( $network_active_plugins[ $newest_sdk_plugin_path ] ) ) { | |
| 342 | + reset( $network_active_plugins ); | |
| 343 | + if ( $newest_sdk_plugin_path === key( $network_active_plugins ) ) { | |
| 344 | + // Plugin is already activated first on the network level. | |
| 345 | + return false; | |
| 346 | + } else { | |
| 347 | + $time = $network_active_plugins[ $newest_sdk_plugin_path ]; | |
| 348 | + | |
| 349 | + // Remove plugin from its current position. | |
| 350 | + unset( $network_active_plugins[ $newest_sdk_plugin_path ] ); | |
| 351 | + | |
| 352 | + // Set it to be included first. | |
| 353 | + $network_active_plugins = array( $newest_sdk_plugin_path => $time ) + $network_active_plugins; | |
| 354 | + | |
| 355 | + update_site_option( 'active_sitewide_plugins', $network_active_plugins ); | |
| 356 | + | |
| 357 | + return true; | |
| 358 | + } | |
| 359 | + } | |
| 360 | + } | |
| 361 | + | |
| 362 | + return false; | |
| 363 | + } | |
| 364 | + } | |
| 365 | + | |
| 366 | + if ( ! function_exists( 'fs_fallback_to_newest_active_sdk' ) ) { | |
| 367 | + /** | |
| 368 | + * Go over all Freemius SDKs in the system and find and "remember" | |
| 369 | + * the newest SDK which is associated with an active plugin. | |
| 370 | + * | |
| 371 | + * @author Vova Feldman (@svovaf) | |
| 372 | + * @since 1.1.6 | |
| 373 | + * | |
| 374 | + * @global $fs_active_plugins | |
| 375 | + */ | |
| 376 | + function fs_fallback_to_newest_active_sdk() { | |
| 377 | + global $fs_active_plugins; | |
| 378 | + | |
| 379 | + /** | |
| 380 | + * @var object $newest_sdk_data | |
| 381 | + */ | |
| 382 | + $newest_sdk_data = null; | |
| 383 | + $newest_sdk_path = null; | |
| 384 | + | |
| 385 | + foreach ( $fs_active_plugins->plugins as $sdk_relative_path => $data ) { | |
| 386 | + if ( is_null( $newest_sdk_data ) || version_compare( $data->version, $newest_sdk_data->version, '>' ) | |
| 387 | + ) { | |
| 388 | + // If plugin inactive or SDK starter file doesn't exist, remove SDK reference. | |
| 389 | + if ( 'plugin' === $data->type ) { | |
| 390 | + $is_module_active = is_plugin_active( $data->plugin_path ); | |
| 391 | + } else { | |
| 392 | + $active_theme = wp_get_theme(); | |
| 393 | + $is_module_active = ( $data->plugin_path === $active_theme->get_template() ); | |
| 394 | + } | |
| 395 | + | |
| 396 | + $is_sdk_exists = file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $sdk_relative_path . '/start.php' ) ); | |
| 397 | + | |
| 398 | + if ( ! $is_module_active || ! $is_sdk_exists ) { | |
| 399 | + unset( $fs_active_plugins->plugins[ $sdk_relative_path ] ); | |
| 400 | + | |
| 401 | + // No need to store the data since it will be stored in fs_update_sdk_newest_version() | |
| 402 | + // or explicitly with update_option(). | |
| 403 | + } else { | |
| 404 | + $newest_sdk_data = $data; | |
| 405 | + $newest_sdk_path = $sdk_relative_path; | |
| 406 | + } | |
| 407 | + } | |
| 408 | + } | |
| 409 | + | |
| 410 | + if ( is_null( $newest_sdk_data ) ) { | |
| 411 | + // Couldn't find any SDK reference. | |
| 412 | + $fs_active_plugins = new stdClass(); | |
| 413 | + update_option( 'fs_active_plugins', $fs_active_plugins ); | |
| 414 | + } else { | |
| 415 | + fs_update_sdk_newest_version( $newest_sdk_path, $newest_sdk_data->plugin_path ); | |
| 416 | + } | |
| 417 | + } | |
| 418 | + } | |