/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.5'; 
  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. fs_redirect( $_SERVER['REQUEST_URI'] ); 
  82. } else { 
  83. if ( ! function_exists( 'get_plugins' ) ) { 
  84. require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  85.  
  86. $is_newest_sdk_plugin_activate = is_plugin_active( $fs_active_plugins->newest->plugin_path ); 
  87.  
  88. if ( $is_current_sdk_newest && 
  89. ! $is_newest_sdk_plugin_activate && 
  90. ! $fs_active_plugins->newest->in_activation 
  91. ) { 
  92. // If current SDK is the newest and the plugin is NOT active, it means 
  93. // that the current plugin in activation mode. 
  94. $fs_active_plugins->newest->in_activation = true; 
  95. update_option( 'fs_active_plugins', $fs_active_plugins ); 
  96.  
  97. $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' ) ); 
  98.  
  99. if ( ! $is_newest_sdk_path_valid && ! $is_current_sdk_newest ) { 
  100. // Plugin with newest SDK is no longer active, or SDK was moved to a different location. 
  101. unset( $fs_active_plugins->plugins[ $fs_active_plugins->newest->sdk_path ] ); 
  102.  
  103. if ( ! ( $is_newest_sdk_plugin_activate || $fs_active_plugins->newest->in_activation ) || 
  104. ! $is_newest_sdk_path_valid || 
  105. // Is newest SDK downgraded. 
  106. ( $this_sdk_relative_path == $fs_active_plugins->newest->sdk_path && 
  107. version_compare( $fs_active_plugins->newest->version, $this_sdk_version, '>' ) ) 
  108. ) { 
  109. /** 
  110. * Plugin with newest SDK is no longer active. 
  111. * OR 
  112. * The newest SDK was in the current plugin. BUT, seems like the version of 
  113. * the SDK was downgraded to a lower SDK. 
  114. */ 
  115. // Find the active plugin with the newest SDK version and update the newest reference. 
  116. fs_fallback_to_newest_active_sdk(); 
  117. } else { 
  118. if ( $is_newest_sdk_plugin_activate && 
  119. $this_sdk_relative_path == $fs_active_plugins->newest->sdk_path && 
  120. ( $fs_active_plugins->newest->in_activation || 
  121. ( class_exists( 'Freemius' ) && ( ! defined( 'WP_FS__SDK_VERSION' ) || version_compare( WP_FS__SDK_VERSION, $this_sdk_version, '<' ) ) ) 
  122.  
  123. ) { 
  124. if ( $fs_active_plugins->newest->in_activation ) { 
  125. // Plugin no more in activation. 
  126. $fs_active_plugins->newest->in_activation = false; 
  127. update_option( 'fs_active_plugins', $fs_active_plugins ); 
  128.  
  129. // Reorder plugins to load plugin with newest SDK first. 
  130. if ( fs_newest_sdk_plugin_first() ) { 
  131. // Refresh page after re-order to make sure activated plugin loads newest SDK. 
  132. if ( class_exists( 'Freemius' ) ) { 
  133. fs_redirect( $_SERVER['REQUEST_URI'] ); 
  134.  
  135. if ( class_exists( 'Freemius' ) ) { 
  136. // SDK was already loaded. 
  137. return; 
  138.  
  139. if ( version_compare( $this_sdk_version, $fs_active_plugins->newest->version, '<' ) ) { 
  140. $newest_sdk_starter = fs_normalize_path( WP_PLUGIN_DIR . '/' . $fs_active_plugins->newest->sdk_path . '/start.php' ); 
  141.  
  142. if ( file_exists( $newest_sdk_starter ) ) { 
  143. // Reorder plugins to load plugin with newest SDK first. 
  144. fs_newest_sdk_plugin_first(); 
  145.  
  146. // There's a newer SDK version, load it instead of the current one! 
  147. require_once $newest_sdk_starter; 
  148.  
  149. return; 
  150.  
  151. #endregion SDK Selection Logic -------------------------------------------------------------------- 
  152.  
  153. #region Hooks & Filters Collection -------------------------------------------------------------------- 
  154.  
  155. /** 
  156. * Freemius hooks (actions & filters) tags structure: 
  157. * 
  158. * fs_{filter/action_name}_{plugin_slug} 
  159. * 
  160. * -------------------------------------------------------- 
  161. * 
  162. * Usage with WordPress' add_action() / add_filter(): 
  163. * 
  164. * add_action('fs_{filter/action_name}_{plugin_slug}', $callable); 
  165. * 
  166. * -------------------------------------------------------- 
  167. * 
  168. * Usage with Freemius' instance add_action() / add_filter(): 
  169. * 
  170. * // No need to add 'fs_' prefix nor '_{plugin_slug}' suffix. 
  171. * my_freemius()->add_action('{action_name}', $callable); 
  172. * 
  173. * -------------------------------------------------------- 
  174. * 
  175. * Freemius filters collection: 
  176. * 
  177. * fs_connect_url_{plugin_slug} 
  178. * fs_trial_promotion_message_{plugin_slug} 
  179. * fs_is_long_term_user_{plugin_slug} 
  180. * fs_uninstall_reasons_{plugin_slug} 
  181. * fs_is_plugin_update_{plugin_slug} 
  182. * fs_api_domains_{plugin_slug} 
  183. * fs_email_template_sections_{plugin_slug} 
  184. * fs_support_forum_submenu_{plugin_slug} 
  185. * fs_support_forum_url_{plugin_slug} 
  186. * fs_connect_message_{plugin_slug} 
  187. * fs_connect_message_on_update_{plugin_slug} 
  188. * fs_uninstall_confirmation_message_{plugin_slug} 
  189. * fs_pending_activation_message_{plugin_slug} 
  190. * fs_is_submenu_visible_{plugin_slug} 
  191. * fs_plugin_icon_{plugin_slug} 
  192. * fs_show_trial_{plugin_slug} 
  193. * 
  194. * -------------------------------------------------------- 
  195. * 
  196. * Freemius actions collection: 
  197. * 
  198. * fs_after_license_loaded_{plugin_slug} 
  199. * fs_after_license_change_{plugin_slug} 
  200. * fs_after_plans_sync_{plugin_slug} 
  201. * 
  202. * fs_after_account_details_{plugin_slug} 
  203. * fs_after_account_user_sync_{plugin_slug} 
  204. * fs_after_account_plan_sync_{plugin_slug} 
  205. * fs_before_account_load_{plugin_slug} 
  206. * fs_after_account_connection_{plugin_slug} 
  207. * fs_account_property_edit_{plugin_slug} 
  208. * fs_account_email_verified_{plugin_slug} 
  209. * fs_account_page_load_before_departure_{plugin_slug} 
  210. * fs_before_account_delete_{plugin_slug} 
  211. * fs_after_account_delete_{plugin_slug} 
  212. * 
  213. * fs_sdk_version_update_{plugin_slug} 
  214. * fs_plugin_version_update_{plugin_slug} 
  215. * 
  216. * fs_initiated_{plugin_slug} 
  217. * fs_after_init_plugin_registered_{plugin_slug} 
  218. * fs_after_init_plugin_anonymous_{plugin_slug} 
  219. * fs_after_init_plugin_pending_activations_{plugin_slug} 
  220. * fs_after_init_addon_registered_{plugin_slug} 
  221. * fs_after_init_addon_anonymous_{plugin_slug} 
  222. * fs_after_init_addon_pending_activations_{plugin_slug} 
  223. * 
  224. * fs_after_premium_version_activation_{plugin_slug} 
  225. * fs_after_free_version_reactivation_{plugin_slug} 
  226. * 
  227. * fs_after_uninstall_{plugin_slug} 
  228. * fs_before_admin_menu_init_{plugin_slug} 
  229. */ 
  230.  
  231. #endregion Hooks & Filters Collection -------------------------------------------------------------------- 
  232.  
  233. if ( ! class_exists( 'Freemius' ) ) { 
  234.  
  235. if ( ! defined( 'WP_FS__SDK_VERSION' ) ) { 
  236. define( 'WP_FS__SDK_VERSION', $this_sdk_version ); 
  237.  
  238. // Load SDK files. 
  239. require_once dirname( __FILE__ ) . '/require.php'; 
  240.  
  241. /** 
  242. * Quick shortcut to get Freemius for specified plugin. 
  243. * Used by various templates. 
  244. * 
  245. * @param string $slug 
  246. * 
  247. * @return Freemius 
  248. */ 
  249. function freemius( $slug ) { 
  250. return Freemius::instance( $slug ); 
  251.  
  252. /** 
  253. * @param string $slug 
  254. * @param number $plugin_id 
  255. * @param string $public_key 
  256. * @param bool $is_live Is live or test plugin. 
  257. * @param bool $is_premium Hints freemius if running the premium plugin or not. 
  258. * 
  259. * @return Freemius 
  260. * 
  261. * @deprecated Please use fs_dynamic_init(). 
  262. */ 
  263. function fs_init( $slug, $plugin_id, $public_key, $is_live = true, $is_premium = true ) { 
  264. $fs = Freemius::instance( $slug, true ); 
  265. $fs->init( $plugin_id, $public_key, $is_live, $is_premium ); 
  266.  
  267. return $fs; 
  268.  
  269. /** 
  270. * @param array<string, string> $module Plugin or Theme details. 
  271. * 
  272. * @return Freemius 
  273. * @throws Freemius_Exception 
  274. */ 
  275. function fs_dynamic_init( $module ) { 
  276. $fs = Freemius::instance( $module['slug'], true ); 
  277. $fs->dynamic_init( $module ); 
  278.  
  279. return $fs; 
  280.  
  281. function fs_dump_log() { 
  282. FS_Logger::dump(); 
.