/freemius/start.php

  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. * Freemius SDK Version. 
  14. * 
  15. * @var string 
  16. */ 
  17. $this_sdk_version = '1.2.1'; 
  18.  
  19. #region SDK Selection Logic -------------------------------------------------------------------- 
  20.  
  21. /** 
  22. * Special logic added on 1.1.6 to make sure that every Freemius powered plugin 
  23. * will ALWAYS be loaded with the newest SDK from the active Freemius powered plugins. 
  24. * 
  25. * Since Freemius SDK is backward compatible, this will make sure that all Freemius powered 
  26. * plugins will run correctly. 
  27. * 
  28. * @since 1.1.6 
  29. */ 
  30.  
  31. global $fs_active_plugins; 
  32.  
  33. $this_sdk_relative_path = plugin_basename( dirname( __FILE__ ) ); 
  34.  
  35. if ( ! isset( $fs_active_plugins ) ) { 
  36. // Require SDK essentials. 
  37. require_once dirname( __FILE__ ) . '/includes/fs-essential-functions.php'; 
  38.  
  39. // Load all Freemius powered active plugins. 
  40. $fs_active_plugins = get_option( 'fs_active_plugins', new stdClass() ); 
  41.  
  42. if ( ! isset( $fs_active_plugins->plugins ) ) { 
  43. $fs_active_plugins->plugins = array(); 
  44.  
  45. if ( ! function_exists( 'fs_find_direct_caller_plugin_file' ) ) { 
  46. require_once dirname( __FILE__ ) . '/includes/supplements/fs-essential-functions-1.1.7.1.php'; 
  47.  
  48. // Update current SDK info based on the SDK path. 
  49. if ( ! isset( $fs_active_plugins->plugins[ $this_sdk_relative_path ] ) || 
  50. $this_sdk_version != $fs_active_plugins->plugins[ $this_sdk_relative_path ]->version 
  51. ) { 
  52. $fs_active_plugins->plugins[ $this_sdk_relative_path ] = (object) array( 
  53. 'version' => $this_sdk_version,  
  54. 'timestamp' => time(),  
  55. 'plugin_path' => plugin_basename( fs_find_direct_caller_plugin_file( __FILE__ ) ),  
  56. ); 
  57.  
  58. $is_current_sdk_newest = isset( $fs_active_plugins->newest ) && ( $this_sdk_relative_path == $fs_active_plugins->newest->sdk_path ); 
  59.  
  60. if ( ! isset( $fs_active_plugins->newest ) ) { 
  61. /** 
  62. * This will be executed only once, for the first time a Freemius powered plugin is activated. 
  63. */ 
  64. fs_update_sdk_newest_version( $this_sdk_relative_path, $fs_active_plugins->plugins[ $this_sdk_relative_path ]->plugin_path ); 
  65.  
  66. $is_current_sdk_newest = true; 
  67. } else if ( version_compare( $fs_active_plugins->newest->version, $this_sdk_version, '<' ) ) { 
  68. /** 
  69. * Current SDK is newer than the newest stored SDK. 
  70. */ 
  71. fs_update_sdk_newest_version( $this_sdk_relative_path, $fs_active_plugins->plugins[ $this_sdk_relative_path ]->plugin_path ); 
  72.  
  73. if ( class_exists( 'Freemius' ) ) { 
  74. // Older SDK version was already loaded. 
  75.  
  76. if ( ! $fs_active_plugins->newest->in_activation ) { 
  77. // Re-order plugins to load this plugin first. 
  78. fs_newest_sdk_plugin_first(); 
  79.  
  80. // Refresh page. 
  81. if ( fs_redirect( $_SERVER['REQUEST_URI'] ) ) { 
  82. exit(); 
  83. } else { 
  84. if ( ! function_exists( 'get_plugins' ) ) { 
  85. require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  86.  
  87. $is_newest_sdk_plugin_activate = is_plugin_active( $fs_active_plugins->newest->plugin_path ); 
  88.  
  89. if ( $is_current_sdk_newest && 
  90. ! $is_newest_sdk_plugin_activate && 
  91. ! $fs_active_plugins->newest->in_activation 
  92. ) { 
  93. // If current SDK is the newest and the plugin is NOT active, it means 
  94. // that the current plugin in activation mode. 
  95. $fs_active_plugins->newest->in_activation = true; 
  96. update_option( 'fs_active_plugins', $fs_active_plugins ); 
  97.  
  98. $is_newest_sdk_path_valid = ( $is_newest_sdk_plugin_activate || $fs_active_plugins->newest->in_activation ) && file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this_sdk_relative_path . '/start.php' ) ); 
  99.  
  100. if ( ! $is_newest_sdk_path_valid && ! $is_current_sdk_newest ) { 
  101. // Plugin with newest SDK is no longer active, or SDK was moved to a different location. 
  102. unset( $fs_active_plugins->plugins[ $fs_active_plugins->newest->sdk_path ] ); 
  103.  
  104. if ( ! ( $is_newest_sdk_plugin_activate || $fs_active_plugins->newest->in_activation ) || 
  105. ! $is_newest_sdk_path_valid || 
  106. // Is newest SDK downgraded. 
  107. ( $this_sdk_relative_path == $fs_active_plugins->newest->sdk_path && 
  108. version_compare( $fs_active_plugins->newest->version, $this_sdk_version, '>' ) ) 
  109. ) { 
  110. /** 
  111. * Plugin with newest SDK is no longer active. 
  112. * OR 
  113. * The newest SDK was in the current plugin. BUT, seems like the version of 
  114. * the SDK was downgraded to a lower SDK. 
  115. */ 
  116. // Find the active plugin with the newest SDK version and update the newest reference. 
  117. fs_fallback_to_newest_active_sdk(); 
  118. } else { 
  119. if ( $is_newest_sdk_plugin_activate && 
  120. $this_sdk_relative_path == $fs_active_plugins->newest->sdk_path && 
  121. ( $fs_active_plugins->newest->in_activation || 
  122. ( class_exists( 'Freemius' ) && ( ! defined( 'WP_FS__SDK_VERSION' ) || version_compare( WP_FS__SDK_VERSION, $this_sdk_version, '<' ) ) ) 
  123.  
  124. ) { 
  125. if ( $fs_active_plugins->newest->in_activation ) { 
  126. // Plugin no more in activation. 
  127. $fs_active_plugins->newest->in_activation = false; 
  128. update_option( 'fs_active_plugins', $fs_active_plugins ); 
  129.  
  130. // Reorder plugins to load plugin with newest SDK first. 
  131. if ( fs_newest_sdk_plugin_first() ) { 
  132. // Refresh page after re-order to make sure activated plugin loads newest SDK. 
  133. if ( class_exists( 'Freemius' ) ) { 
  134. if ( fs_redirect( $_SERVER['REQUEST_URI'] ) ) { 
  135. exit(); 
  136.  
  137. if ( class_exists( 'Freemius' ) ) { 
  138. // SDK was already loaded. 
  139. return; 
  140.  
  141. if ( version_compare( $this_sdk_version, $fs_active_plugins->newest->version, '<' ) ) { 
  142. $newest_sdk_starter = fs_normalize_path( WP_PLUGIN_DIR . '/' . $fs_active_plugins->newest->sdk_path . '/start.php' ); 
  143.  
  144. if ( file_exists( $newest_sdk_starter ) ) { 
  145. // Reorder plugins to load plugin with newest SDK first. 
  146. fs_newest_sdk_plugin_first(); 
  147.  
  148. // There's a newer SDK version, load it instead of the current one! 
  149. require_once $newest_sdk_starter; 
  150.  
  151. return; 
  152.  
  153. #endregion SDK Selection Logic -------------------------------------------------------------------- 
  154.  
  155. #region Hooks & Filters Collection -------------------------------------------------------------------- 
  156.  
  157. /** 
  158. * Freemius hooks (actions & filters) tags structure: 
  159. * 
  160. * fs_{filter/action_name}_{plugin_slug} 
  161. * 
  162. * -------------------------------------------------------- 
  163. * 
  164. * Usage with WordPress' add_action() / add_filter(): 
  165. * 
  166. * add_action('fs_{filter/action_name}_{plugin_slug}', $callable); 
  167. * 
  168. * -------------------------------------------------------- 
  169. * 
  170. * Usage with Freemius' instance add_action() / add_filter(): 
  171. * 
  172. * // No need to add 'fs_' prefix nor '_{plugin_slug}' suffix. 
  173. * my_freemius()->add_action('{action_name}', $callable); 
  174. * 
  175. * -------------------------------------------------------- 
  176. * 
  177. * Freemius filters collection: 
  178. * 
  179. * fs_connect_url_{plugin_slug} 
  180. * fs_trial_promotion_message_{plugin_slug} 
  181. * fs_is_long_term_user_{plugin_slug} 
  182. * fs_uninstall_reasons_{plugin_slug} 
  183. * fs_is_plugin_update_{plugin_slug} 
  184. * fs_api_domains_{plugin_slug} 
  185. * fs_email_template_sections_{plugin_slug} 
  186. * fs_support_forum_submenu_{plugin_slug} 
  187. * fs_support_forum_url_{plugin_slug} 
  188. * fs_connect_message_{plugin_slug} 
  189. * fs_connect_message_on_update_{plugin_slug} 
  190. * fs_uninstall_confirmation_message_{plugin_slug} 
  191. * fs_pending_activation_message_{plugin_slug} 
  192. * fs_is_submenu_visible_{plugin_slug} 
  193. * fs_plugin_icon_{plugin_slug} 
  194. * fs_show_trial_{plugin_slug} 
  195. * 
  196. * -------------------------------------------------------- 
  197. * 
  198. * Freemius actions collection: 
  199. * 
  200. * fs_after_license_loaded_{plugin_slug} 
  201. * fs_after_license_change_{plugin_slug} 
  202. * fs_after_plans_sync_{plugin_slug} 
  203. * 
  204. * fs_after_account_details_{plugin_slug} 
  205. * fs_after_account_user_sync_{plugin_slug} 
  206. * fs_after_account_plan_sync_{plugin_slug} 
  207. * fs_before_account_load_{plugin_slug} 
  208. * fs_after_account_connection_{plugin_slug} 
  209. * fs_account_property_edit_{plugin_slug} 
  210. * fs_account_email_verified_{plugin_slug} 
  211. * fs_account_page_load_before_departure_{plugin_slug} 
  212. * fs_before_account_delete_{plugin_slug} 
  213. * fs_after_account_delete_{plugin_slug} 
  214. * 
  215. * fs_sdk_version_update_{plugin_slug} 
  216. * fs_plugin_version_update_{plugin_slug} 
  217. * 
  218. * fs_initiated_{plugin_slug} 
  219. * fs_after_init_plugin_registered_{plugin_slug} 
  220. * fs_after_init_plugin_anonymous_{plugin_slug} 
  221. * fs_after_init_plugin_pending_activations_{plugin_slug} 
  222. * fs_after_init_addon_registered_{plugin_slug} 
  223. * fs_after_init_addon_anonymous_{plugin_slug} 
  224. * fs_after_init_addon_pending_activations_{plugin_slug} 
  225. * 
  226. * fs_after_premium_version_activation_{plugin_slug} 
  227. * fs_after_free_version_reactivation_{plugin_slug} 
  228. * 
  229. * fs_after_uninstall_{plugin_slug} 
  230. * fs_before_admin_menu_init_{plugin_slug} 
  231. */ 
  232.  
  233. #endregion Hooks & Filters Collection -------------------------------------------------------------------- 
  234.  
  235. if ( ! class_exists( 'Freemius' ) ) { 
  236.  
  237. if ( ! defined( 'WP_FS__SDK_VERSION' ) ) { 
  238. define( 'WP_FS__SDK_VERSION', $this_sdk_version ); 
  239.  
  240. // Load SDK files. 
  241. require_once dirname( __FILE__ ) . '/require.php'; 
  242.  
  243. /** 
  244. * Quick shortcut to get Freemius for specified plugin. 
  245. * Used by various templates. 
  246. * 
  247. * @param string $slug 
  248. * 
  249. * @return Freemius 
  250. */ 
  251. function freemius( $slug ) { 
  252. return Freemius::instance( $slug ); 
  253.  
  254. /** 
  255. * @param string $slug 
  256. * @param number $plugin_id 
  257. * @param string $public_key 
  258. * @param bool $is_live Is live or test plugin. 
  259. * @param bool $is_premium Hints freemius if running the premium plugin or not. 
  260. * 
  261. * @return Freemius 
  262. * 
  263. * @deprecated Please use fs_dynamic_init(). 
  264. */ 
  265. function fs_init( $slug, $plugin_id, $public_key, $is_live = true, $is_premium = true ) { 
  266. $fs = Freemius::instance( $slug, true ); 
  267. $fs->init( $plugin_id, $public_key, $is_live, $is_premium ); 
  268.  
  269. return $fs; 
  270.  
  271. /** 
  272. * @param array<string, string> $module Plugin or Theme details. 
  273. * 
  274. * @return Freemius 
  275. * @throws Freemius_Exception 
  276. */ 
  277. function fs_dynamic_init( $module ) { 
  278. $fs = Freemius::instance( $module['slug'], true ); 
  279. $fs->dynamic_init( $module ); 
  280.  
  281. return $fs; 
  282.  
  283. function fs_dump_log() { 
  284. FS_Logger::dump(); 
.