| 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.0.3 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Freemius SDK Version. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
$this_sdk_version = '1.2.1.7.1'; |
| 19 |
|
| 20 |
#region SDK Selection Logic -------------------------------------------------------------------- |
| 21 |
|
| 22 |
/** |
| 23 |
* Special logic added on 1.1.6 to make sure that every Freemius powered plugin |
| 24 |
* will ALWAYS be loaded with the newest SDK from the active Freemius powered plugins. |
| 25 |
* |
| 26 |
* Since Freemius SDK is backward compatible, this will make sure that all Freemius powered |
| 27 |
* plugins will run correctly. |
| 28 |
* |
| 29 |
* @since 1.1.6 |
| 30 |
*/ |
| 31 |
|
| 32 |
global $fs_active_plugins; |
| 33 |
|
| 34 |
if ( ! function_exists( 'fs_find_caller_plugin_file' ) ) { |
| 35 |
// Require SDK essentials. |
| 36 |
require_once dirname( __FILE__ ) . '/includes/fs-essential-functions.php'; |
| 37 |
} |
| 38 |
|
| 39 |
$file_path = fs_normalize_path( __FILE__ ); |
| 40 |
$fs_root_path = dirname( $file_path ); |
| 41 |
$this_sdk_relative_path = plugin_basename( $fs_root_path ); |
| 42 |
|
| 43 |
if ( ! isset( $fs_active_plugins ) ) { |
| 44 |
if ( ! function_exists( '__fs' ) ) { |
| 45 |
// Require SDK essentials. |
| 46 |
require_once dirname( __FILE__ ) . '/includes/fs-essential-functions.php'; |
| 47 |
} |
| 48 |
|
| 49 |
// Load all Freemius powered active plugins. |
| 50 |
$fs_active_plugins = get_option( 'fs_active_plugins', new stdClass() ); |
| 51 |
|
| 52 |
if ( ! isset( $fs_active_plugins->plugins ) ) { |
| 53 |
$fs_active_plugins->plugins = array(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if ( empty( $fs_active_plugins->abspath ) ) { |
| 58 |
/** |
| 59 |
* Store the WP install absolute path reference to identify environment change |
| 60 |
* while replicating the storage. |
| 61 |
* |
| 62 |
* @author Vova Feldman (@svovaf) |
| 63 |
* @since 1.2.1.7 |
| 64 |
*/ |
| 65 |
$fs_active_plugins->abspath = ABSPATH; |
| 66 |
} else { |
| 67 |
if ( ABSPATH !== $fs_active_plugins->abspath ) { |
| 68 |
/** |
| 69 |
* WordPress path has changed, cleanup the SDK references cache. |
| 70 |
* This resolves issues triggered when spinning a staging environments |
| 71 |
* while replicating the database. |
| 72 |
* |
| 73 |
* @author Vova Feldman (@svovaf) |
| 74 |
* @since 1.2.1.7 |
| 75 |
*/ |
| 76 |
$fs_active_plugins->abspath = ABSPATH; |
| 77 |
$fs_active_plugins->plugins = array(); |
| 78 |
unset( $fs_active_plugins->newest ); |
| 79 |
} else { |
| 80 |
/** |
| 81 |
* Make sure SDK references are still valid. This resolves |
| 82 |
* issues when users hard delete modules via FTP. |
| 83 |
* |
| 84 |
* @author Vova Feldman (@svovaf) |
| 85 |
* @since 1.2.1.7 |
| 86 |
*/ |
| 87 |
foreach ( $fs_active_plugins->plugins as $sdk_path => &$data ) { |
| 88 |
if ( ! file_exists( WP_PLUGIN_DIR . '/' . $sdk_path ) ) { |
| 89 |
unset( $fs_active_plugins->plugins[ $sdk_path ] ); |
| 90 |
} |
| 91 |
|
| 92 |
update_option( 'fs_active_plugins', $fs_active_plugins ); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! function_exists( 'fs_find_direct_caller_plugin_file' ) ) { |
| 98 |
require_once dirname( __FILE__ ) . '/includes/supplements/fs-essential-functions-1.1.7.1.php'; |
| 99 |
} |
| 100 |
|
| 101 |
// Update current SDK info based on the SDK path. |
| 102 |
if ( ! isset( $fs_active_plugins->plugins[ $this_sdk_relative_path ] ) || |
| 103 |
$this_sdk_version != $fs_active_plugins->plugins[ $this_sdk_relative_path ]->version |
| 104 |
) { |
| 105 |
$fs_active_plugins->plugins[ $this_sdk_relative_path ] = (object) array( |
| 106 |
'version' => $this_sdk_version, |
| 107 |
'timestamp' => time(), |
| 108 |
'plugin_path' => plugin_basename( fs_find_direct_caller_plugin_file( __FILE__ ) ), |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
$is_current_sdk_newest = isset( $fs_active_plugins->newest ) && ( $this_sdk_relative_path == $fs_active_plugins->newest->sdk_path ); |
| 113 |
|
| 114 |
if ( ! isset( $fs_active_plugins->newest ) ) { |
| 115 |
/** |
| 116 |
* This will be executed only once, for the first time a Freemius powered plugin is activated. |
| 117 |
*/ |
| 118 |
fs_update_sdk_newest_version( $this_sdk_relative_path, $fs_active_plugins->plugins[ $this_sdk_relative_path ]->plugin_path ); |
| 119 |
|
| 120 |
$is_current_sdk_newest = true; |
| 121 |
} else if ( version_compare( $fs_active_plugins->newest->version, $this_sdk_version, '<' ) ) { |
| 122 |
/** |
| 123 |
* Current SDK is newer than the newest stored SDK. |
| 124 |
*/ |
| 125 |
fs_update_sdk_newest_version( $this_sdk_relative_path, $fs_active_plugins->plugins[ $this_sdk_relative_path ]->plugin_path ); |
| 126 |
|
| 127 |
if ( class_exists( 'Freemius' ) ) { |
| 128 |
// Older SDK version was already loaded. |
| 129 |
|
| 130 |
if ( ! $fs_active_plugins->newest->in_activation ) { |
| 131 |
// Re-order plugins to load this plugin first. |
| 132 |
fs_newest_sdk_plugin_first(); |
| 133 |
} |
| 134 |
|
| 135 |
// Refresh page. |
| 136 |
fs_redirect( $_SERVER['REQUEST_URI'] ); |
| 137 |
} |
| 138 |
} else { |
| 139 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 140 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 141 |
} |
| 142 |
|
| 143 |
$is_newest_sdk_plugin_active = is_plugin_active( $fs_active_plugins->newest->plugin_path ); |
| 144 |
|
| 145 |
if ( $is_current_sdk_newest && |
| 146 |
! $is_newest_sdk_plugin_active && |
| 147 |
! $fs_active_plugins->newest->in_activation |
| 148 |
) { |
| 149 |
// If current SDK is the newest and the plugin is NOT active, it means |
| 150 |
// that the current plugin in activation mode. |
| 151 |
$fs_active_plugins->newest->in_activation = true; |
| 152 |
update_option( 'fs_active_plugins', $fs_active_plugins ); |
| 153 |
} |
| 154 |
|
| 155 |
$is_newest_sdk_path_valid = ( $is_newest_sdk_plugin_active || $fs_active_plugins->newest->in_activation ) && file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this_sdk_relative_path . '/start.php' ) ); |
| 156 |
|
| 157 |
if ( ! $is_newest_sdk_path_valid && ! $is_current_sdk_newest ) { |
| 158 |
// Plugin with newest SDK is no longer active, or SDK was moved to a different location. |
| 159 |
unset( $fs_active_plugins->plugins[ $fs_active_plugins->newest->sdk_path ] ); |
| 160 |
} |
| 161 |
|
| 162 |
if ( ! ( $is_newest_sdk_plugin_active || $fs_active_plugins->newest->in_activation ) || |
| 163 |
! $is_newest_sdk_path_valid || |
| 164 |
// Is newest SDK downgraded. |
| 165 |
( $this_sdk_relative_path == $fs_active_plugins->newest->sdk_path && |
| 166 |
version_compare( $fs_active_plugins->newest->version, $this_sdk_version, '>' ) ) |
| 167 |
) { |
| 168 |
/** |
| 169 |
* Plugin with newest SDK is no longer active. |
| 170 |
* OR |
| 171 |
* The newest SDK was in the current plugin. BUT, seems like the version of |
| 172 |
* the SDK was downgraded to a lower SDK. |
| 173 |
*/ |
| 174 |
// Find the active plugin with the newest SDK version and update the newest reference. |
| 175 |
fs_fallback_to_newest_active_sdk(); |
| 176 |
} else { |
| 177 |
if ( $is_newest_sdk_plugin_active && |
| 178 |
$this_sdk_relative_path == $fs_active_plugins->newest->sdk_path && |
| 179 |
( $fs_active_plugins->newest->in_activation || |
| 180 |
( class_exists( 'Freemius' ) && ( ! defined( 'WP_FS__SDK_VERSION' ) || version_compare( WP_FS__SDK_VERSION, $this_sdk_version, '<' ) ) ) |
| 181 |
) |
| 182 |
|
| 183 |
) { |
| 184 |
if ( $fs_active_plugins->newest->in_activation ) { |
| 185 |
// Plugin no more in activation. |
| 186 |
$fs_active_plugins->newest->in_activation = false; |
| 187 |
update_option( 'fs_active_plugins', $fs_active_plugins ); |
| 188 |
} |
| 189 |
|
| 190 |
// Reorder plugins to load plugin with newest SDK first. |
| 191 |
if ( fs_newest_sdk_plugin_first() ) { |
| 192 |
// Refresh page after re-order to make sure activated plugin loads newest SDK. |
| 193 |
if ( class_exists( 'Freemius' ) ) { |
| 194 |
fs_redirect( $_SERVER['REQUEST_URI'] ); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if ( class_exists( 'Freemius' ) ) { |
| 202 |
// SDK was already loaded. |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
if ( version_compare( $this_sdk_version, $fs_active_plugins->newest->version, '<' ) ) { |
| 207 |
$newest_sdk_starter = fs_normalize_path( WP_PLUGIN_DIR . '/' . $fs_active_plugins->newest->sdk_path . '/start.php' ); |
| 208 |
|
| 209 |
if ( file_exists( $newest_sdk_starter ) ) { |
| 210 |
// Reorder plugins to load plugin with newest SDK first. |
| 211 |
fs_newest_sdk_plugin_first(); |
| 212 |
|
| 213 |
// There's a newer SDK version, load it instead of the current one! |
| 214 |
require_once $newest_sdk_starter; |
| 215 |
|
| 216 |
return; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
#endregion SDK Selection Logic -------------------------------------------------------------------- |
| 221 |
|
| 222 |
#region Hooks & Filters Collection -------------------------------------------------------------------- |
| 223 |
|
| 224 |
/** |
| 225 |
* Freemius hooks (actions & filters) tags structure: |
| 226 |
* |
| 227 |
* fs_{filter/action_name}_{plugin_slug} |
| 228 |
* |
| 229 |
* -------------------------------------------------------- |
| 230 |
* |
| 231 |
* Usage with WordPress' add_action() / add_filter(): |
| 232 |
* |
| 233 |
* add_action('fs_{filter/action_name}_{plugin_slug}', $callable); |
| 234 |
* |
| 235 |
* -------------------------------------------------------- |
| 236 |
* |
| 237 |
* Usage with Freemius' instance add_action() / add_filter(): |
| 238 |
* |
| 239 |
* // No need to add 'fs_' prefix nor '_{plugin_slug}' suffix. |
| 240 |
* my_freemius()->add_action('{action_name}', $callable); |
| 241 |
* |
| 242 |
* -------------------------------------------------------- |
| 243 |
* |
| 244 |
* Freemius filters collection: |
| 245 |
* |
| 246 |
* fs_connect_url_{plugin_slug} |
| 247 |
* fs_trial_promotion_message_{plugin_slug} |
| 248 |
* fs_is_long_term_user_{plugin_slug} |
| 249 |
* fs_uninstall_reasons_{plugin_slug} |
| 250 |
* fs_is_plugin_update_{plugin_slug} |
| 251 |
* fs_api_domains_{plugin_slug} |
| 252 |
* fs_email_template_sections_{plugin_slug} |
| 253 |
* fs_support_forum_submenu_{plugin_slug} |
| 254 |
* fs_support_forum_url_{plugin_slug} |
| 255 |
* fs_connect_message_{plugin_slug} |
| 256 |
* fs_connect_message_on_update_{plugin_slug} |
| 257 |
* fs_uninstall_confirmation_message_{plugin_slug} |
| 258 |
* fs_pending_activation_message_{plugin_slug} |
| 259 |
* fs_is_submenu_visible_{plugin_slug} |
| 260 |
* fs_plugin_icon_{plugin_slug} |
| 261 |
* fs_show_trial_{plugin_slug} |
| 262 |
* |
| 263 |
* -------------------------------------------------------- |
| 264 |
* |
| 265 |
* Freemius actions collection: |
| 266 |
* |
| 267 |
* fs_after_license_loaded_{plugin_slug} |
| 268 |
* fs_after_license_change_{plugin_slug} |
| 269 |
* fs_after_plans_sync_{plugin_slug} |
| 270 |
* |
| 271 |
* fs_after_account_details_{plugin_slug} |
| 272 |
* fs_after_account_user_sync_{plugin_slug} |
| 273 |
* fs_after_account_plan_sync_{plugin_slug} |
| 274 |
* fs_before_account_load_{plugin_slug} |
| 275 |
* fs_after_account_connection_{plugin_slug} |
| 276 |
* fs_account_property_edit_{plugin_slug} |
| 277 |
* fs_account_email_verified_{plugin_slug} |
| 278 |
* fs_account_page_load_before_departure_{plugin_slug} |
| 279 |
* fs_before_account_delete_{plugin_slug} |
| 280 |
* fs_after_account_delete_{plugin_slug} |
| 281 |
* |
| 282 |
* fs_sdk_version_update_{plugin_slug} |
| 283 |
* fs_plugin_version_update_{plugin_slug} |
| 284 |
* |
| 285 |
* fs_initiated_{plugin_slug} |
| 286 |
* fs_after_init_plugin_registered_{plugin_slug} |
| 287 |
* fs_after_init_plugin_anonymous_{plugin_slug} |
| 288 |
* fs_after_init_plugin_pending_activations_{plugin_slug} |
| 289 |
* fs_after_init_addon_registered_{plugin_slug} |
| 290 |
* fs_after_init_addon_anonymous_{plugin_slug} |
| 291 |
* fs_after_init_addon_pending_activations_{plugin_slug} |
| 292 |
* |
| 293 |
* fs_after_premium_version_activation_{plugin_slug} |
| 294 |
* fs_after_free_version_reactivation_{plugin_slug} |
| 295 |
* |
| 296 |
* fs_after_uninstall_{plugin_slug} |
| 297 |
* fs_before_admin_menu_init_{plugin_slug} |
| 298 |
*/ |
| 299 |
|
| 300 |
#endregion Hooks & Filters Collection -------------------------------------------------------------------- |
| 301 |
|
| 302 |
if ( ! class_exists( 'Freemius' ) ) { |
| 303 |
|
| 304 |
if ( ! defined( 'WP_FS__SDK_VERSION' ) ) { |
| 305 |
define( 'WP_FS__SDK_VERSION', $this_sdk_version ); |
| 306 |
} |
| 307 |
|
| 308 |
$plugins_or_theme_dir_path = WP_PLUGIN_DIR; |
| 309 |
|
| 310 |
if ( 0 === strpos( $file_path, fs_normalize_path( $plugins_or_theme_dir_path ) ) ) { |
| 311 |
// No symlinks |
| 312 |
} else { |
| 313 |
/** |
| 314 |
* This logic finds the SDK symlink and set WP_FS__DIR to use it. |
| 315 |
* |
| 316 |
* @author Vova Feldman (@svovaf) |
| 317 |
* @since 1.2.2.5 |
| 318 |
*/ |
| 319 |
$sdk_symlink = null; |
| 320 |
|
| 321 |
// Try to load SDK's symlink from cache. |
| 322 |
if ( isset( $fs_active_plugins->plugins[ $this_sdk_relative_path ] ) && |
| 323 |
is_object( $fs_active_plugins->plugins[ $this_sdk_relative_path ] ) && |
| 324 |
! empty( $fs_active_plugins->plugins[ $this_sdk_relative_path ]->sdk_symlink ) |
| 325 |
) { |
| 326 |
$sdk_symlink = $fs_active_plugins->plugins[ $this_sdk_relative_path ]->sdk_symlink; |
| 327 |
$realpath = realpath( $sdk_symlink ); |
| 328 |
|
| 329 |
if ( ! is_string( $realpath ) || ! file_exists( $realpath ) ) { |
| 330 |
$sdk_symlink = null; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
if ( empty( $sdk_symlink ) ) // Has symlinks, therefore, we need to configure WP_FS__DIR based on the symlink. |
| 335 |
{ |
| 336 |
$partial_path_right = basename( $file_path ); |
| 337 |
$partial_path_left = dirname( $file_path ); |
| 338 |
$realpath = realpath( $plugins_or_theme_dir_path . $partial_path_right ); |
| 339 |
|
| 340 |
while ( '/' !== $partial_path_left && |
| 341 |
( false === $realpath || $file_path !== fs_normalize_path( $realpath ) ) |
| 342 |
) { |
| 343 |
$partial_path_right = trailingslashit( basename( $partial_path_left ) ) . $partial_path_right; |
| 344 |
$partial_path_left = dirname( $partial_path_left ); |
| 345 |
$realpath = realpath( $plugins_or_theme_dir_path . $partial_path_right ); |
| 346 |
} |
| 347 |
|
| 348 |
if ( '/' !== $partial_path_left ) { |
| 349 |
$sdk_symlink = fs_normalize_path( $plugins_or_theme_dir_path . dirname( $partial_path_right ) ); |
| 350 |
|
| 351 |
// Cache value. |
| 352 |
if ( isset( $fs_active_plugins->plugins[ $this_sdk_relative_path ] ) && |
| 353 |
is_object( $fs_active_plugins->plugins[ $this_sdk_relative_path ] ) |
| 354 |
) { |
| 355 |
$fs_active_plugins->plugins[ $this_sdk_relative_path ]->sdk_symlink = $sdk_symlink; |
| 356 |
update_option( 'fs_active_plugins', $fs_active_plugins ); |
| 357 |
} |
| 358 |
|
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
if ( ! empty( $sdk_symlink ) ) { |
| 363 |
// Set SDK dir to the symlink path. |
| 364 |
define( 'WP_FS__DIR', $sdk_symlink ); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
// Load SDK files. |
| 369 |
require_once dirname( __FILE__ ) . '/require.php'; |
| 370 |
|
| 371 |
/** |
| 372 |
* Quick shortcut to get Freemius for specified plugin. |
| 373 |
* Used by various templates. |
| 374 |
* |
| 375 |
* @param string $slug |
| 376 |
* |
| 377 |
* @return Freemius |
| 378 |
*/ |
| 379 |
function freemius( $slug ) { |
| 380 |
return Freemius::instance( $slug ); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* @param string $slug |
| 385 |
* @param number $plugin_id |
| 386 |
* @param string $public_key |
| 387 |
* @param bool $is_live Is live or test plugin. |
| 388 |
* @param bool $is_premium Hints freemius if running the premium plugin or not. |
| 389 |
* |
| 390 |
* @return Freemius |
| 391 |
* |
| 392 |
* @deprecated Please use fs_dynamic_init(). |
| 393 |
*/ |
| 394 |
function fs_init( $slug, $plugin_id, $public_key, $is_live = true, $is_premium = true ) { |
| 395 |
$fs = Freemius::instance( $slug, true ); |
| 396 |
$fs->init( $plugin_id, $public_key, $is_live, $is_premium ); |
| 397 |
|
| 398 |
return $fs; |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* @param array <string,string> $module Plugin or Theme details. |
| 403 |
* |
| 404 |
* @return Freemius |
| 405 |
* @throws Freemius_Exception |
| 406 |
*/ |
| 407 |
function fs_dynamic_init( $module ) { |
| 408 |
$fs = Freemius::instance( $module['slug'], true ); |
| 409 |
$fs->dynamic_init( $module ); |
| 410 |
|
| 411 |
return $fs; |
| 412 |
} |
| 413 |
|
| 414 |
function fs_dump_log() { |
| 415 |
FS_Logger::dump(); |
| 416 |
} |
| 417 |
} |