Freemius

The WordPress Core Freemius class.

Defined (1)

The class is defined in the following location(s).

/freemius/includes/class-freemius.php  
  1. class Freemius extends Freemius_Abstract { 
  2. /** 
  3. * SDK Version 
  4. * @var string 
  5. */ 
  6. public $version = WP_FS__SDK_VERSION; 
  7.  
  8. #region Plugin Info 
  9.  
  10. /** 
  11. * @since 1.0.1 
  12. * @var string 
  13. */ 
  14. private $_slug; 
  15.  
  16. /** 
  17. * @since 1.0.0 
  18. * @var string 
  19. */ 
  20. private $_plugin_basename; 
  21. /** 
  22. * @since 1.0.0 
  23. * @var string 
  24. */ 
  25. private $_free_plugin_basename; 
  26. /** 
  27. * @since 1.0.0 
  28. * @var string 
  29. */ 
  30. private $_plugin_dir_path; 
  31. /** 
  32. * @since 1.0.0 
  33. * @var string 
  34. */ 
  35. private $_plugin_dir_name; 
  36. /** 
  37. * @since 1.0.0 
  38. * @var string 
  39. */ 
  40. private $_plugin_main_file_path; 
  41. /** 
  42. * @var string[] 
  43. */ 
  44. private $_plugin_data; 
  45. /** 
  46. * @since 1.0.9 
  47. * @var string 
  48. */ 
  49. private $_plugin_name; 
  50.  
  51. #endregion Plugin Info 
  52.  
  53. /** 
  54. * @since 1.0.9 
  55. * @var bool If false, don't turn Freemius on. 
  56. */ 
  57. private $_is_on; 
  58.  
  59. /** 
  60. * @since 1.1.3 
  61. * @var bool If false, don't turn Freemius on. 
  62. */ 
  63. private $_is_anonymous; 
  64.  
  65. /** 
  66. * @since 1.0.9 
  67. * @var bool If false, issues with connectivity to Freemius API. 
  68. */ 
  69. private $_has_api_connection; 
  70.  
  71. /** 
  72. * @since 1.0.9 
  73. * @var bool Hints the SDK if plugin can support anonymous mode (if skip connect is visible). 
  74. */ 
  75. private $_enable_anonymous; 
  76.  
  77. /** 
  78. * @since 1.1.7.5 
  79. * @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form). 
  80. */ 
  81. private $_anonymous_mode; 
  82.  
  83. /** 
  84. * @since 1.1.9 
  85. * @var bool Hints the SDK if plugin have any free plans. 
  86. */ 
  87. private $_is_premium_only; 
  88.  
  89. /** 
  90. * @since 1.0.8 
  91. * @var bool Hints the SDK if the plugin has any paid plans. 
  92. */ 
  93. private $_has_paid_plans; 
  94.  
  95. /** 
  96. * @since 1.2.1.5 
  97. * @var int Hints the SDK if the plugin offers a trial period. If negative, no trial, if zero - has a trial but 
  98. * without a specified period, if positive - the number of trial days. 
  99. */ 
  100. private $_trial_days = - 1; 
  101.  
  102. /** 
  103. * @since 1.2.1.5 
  104. * @var bool Hints the SDK if the trial requires a payment method or not. 
  105. */ 
  106. private $_is_trial_require_payment = false; 
  107.  
  108. /** 
  109. * @since 1.0.7 
  110. * @var bool Hints the SDK if the plugin is WordPress.org compliant. 
  111. */ 
  112. private $_is_org_compliant; 
  113.  
  114. /** 
  115. * @since 1.0.7 
  116. * @var bool Hints the SDK if the plugin is has add-ons. 
  117. */ 
  118. private $_has_addons; 
  119.  
  120. /** 
  121. * @since 1.1.6 
  122. * @var string[]bool. 
  123. */ 
  124. private $_permissions; 
  125.  
  126. /** 
  127. * @var FS_Key_Value_Storage 
  128. */ 
  129. private $_storage; 
  130.  
  131. /** 
  132. * @since 1.0.0 
  133. * @var FS_Logger 
  134. */ 
  135. private $_logger; 
  136. /** 
  137. * @since 1.0.4 
  138. * @var FS_Plugin 
  139. */ 
  140. private $_plugin = false; 
  141. /** 
  142. * @since 1.0.4 
  143. * @var FS_Plugin|false 
  144. */ 
  145. private $_parent_plugin = false; 
  146. /** 
  147. * @since 1.1.1 
  148. * @var Freemius 
  149. */ 
  150. private $_parent = false; 
  151. /** 
  152. * @since 1.0.1 
  153. * @var FS_User 
  154. */ 
  155. private $_user = false; 
  156. /** 
  157. * @since 1.0.1 
  158. * @var FS_Site 
  159. */ 
  160. private $_site = false; 
  161. /** 
  162. * @since 1.0.1 
  163. * @var FS_Plugin_License 
  164. */ 
  165. private $_license; 
  166. /** 
  167. * @since 1.0.2 
  168. * @var FS_Plugin_Plan[] 
  169. */ 
  170. private $_plans = false; 
  171. /** 
  172. * @var FS_Plugin_License[] 
  173. * @since 1.0.5 
  174. */ 
  175. private $_licenses = false; 
  176.  
  177. /** 
  178. * @since 1.0.1 
  179. * @var FS_Admin_Menu_Manager 
  180. */ 
  181. private $_menu; 
  182.  
  183. /** 
  184. * @var FS_Admin_Notice_Manager 
  185. */ 
  186. private $_admin_notices; 
  187.  
  188. /** 
  189. * @since 1.1.6 
  190. * @var FS_Admin_Notice_Manager 
  191. */ 
  192. private static $_global_admin_notices; 
  193.  
  194. /** 
  195. * @var FS_Logger 
  196. * @since 1.0.0 
  197. */ 
  198. private static $_static_logger; 
  199.  
  200. /** 
  201. * @var FS_Option_Manager 
  202. * @since 1.0.2 
  203. */ 
  204. private static $_accounts; 
  205.  
  206. /** 
  207. * @var Freemius[] 
  208. */ 
  209. private static $_instances = array(); 
  210.  
  211. #region Uninstall Reasons IDs 
  212.  
  213. const REASON_NO_LONGER_NEEDED = 1; 
  214. const REASON_FOUND_A_BETTER_PLUGIN = 2; 
  215. const REASON_NEEDED_FOR_A_SHORT_PERIOD = 3; 
  216. const REASON_BROKE_MY_SITE = 4; 
  217. const REASON_SUDDENLY_STOPPED_WORKING = 5; 
  218. const REASON_CANT_PAY_ANYMORE = 6; 
  219. const REASON_OTHER = 7; 
  220. const REASON_DIDNT_WORK = 8; 
  221. const REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION = 9; 
  222. const REASON_COULDNT_MAKE_IT_WORK = 10; 
  223. const REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE = 11; 
  224. const REASON_NOT_WORKING = 12; 
  225. const REASON_NOT_WHAT_I_WAS_LOOKING_FOR = 13; 
  226. const REASON_DIDNT_WORK_AS_EXPECTED = 14; 
  227. const REASON_TEMPORARY_DEACTIVATION = 15; 
  228.  
  229. #endregion 
  230.  
  231. /** Ctor 
  232. ------------------------------------------------------------------------------------------------------------------*/ 
  233.  
  234. /** 
  235. * Main singleton instance. 
  236. * @author Vova Feldman (@svovaf) 
  237. * @since 1.0.0 
  238. * @param string $slug 
  239. * @param bool $is_init Since 1.2.1 Is initiation sequence. 
  240. */ 
  241. private function __construct( $slug, $is_init = false ) { 
  242. $this->_slug = $slug; 
  243.  
  244. $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $slug, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); 
  245.  
  246. $this->_storage = FS_Key_Value_Storage::instance( 'plugin_data', $this->_slug ); 
  247.  
  248. $this->_plugin_main_file_path = $this->_find_caller_plugin_file( $is_init ); 
  249. $this->_plugin_dir_path = plugin_dir_path( $this->_plugin_main_file_path ); 
  250. $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path ); 
  251. $this->_free_plugin_basename = str_replace( '-premium/', '/', $this->_plugin_basename ); 
  252.  
  253. $base_name_split = explode( '/', $this->_plugin_basename ); 
  254. $this->_plugin_dir_name = $base_name_split[0]; 
  255.  
  256. if ( $this->_logger->is_on() ) { 
  257. $this->_logger->info( 'plugin_main_file_path = ' . $this->_plugin_main_file_path ); 
  258. $this->_logger->info( 'plugin_dir_path = ' . $this->_plugin_dir_path ); 
  259. $this->_logger->info( 'plugin_basename = ' . $this->_plugin_basename ); 
  260. $this->_logger->info( 'free_plugin_basename = ' . $this->_free_plugin_basename ); 
  261. $this->_logger->info( 'plugin_dir_name = ' . $this->_plugin_dir_name ); 
  262.  
  263. // Remember link between file to slug. 
  264. $this->store_file_slug_map(); 
  265.  
  266. // Store plugin's initial install timestamp. 
  267. if ( ! isset( $this->_storage->install_timestamp ) ) { 
  268. $this->_storage->install_timestamp = WP_FS__SCRIPT_START_TIME; 
  269.  
  270. $this->_plugin = FS_Plugin_Manager::instance( $this->_slug )->get(); 
  271.  
  272. $this->_admin_notices = FS_Admin_Notice_Manager::instance( 
  273. $slug,  
  274. is_object( $this->_plugin ) ? $this->_plugin->title : '' 
  275. ); 
  276.  
  277. if ( 'true' === fs_request_get( 'fs_clear_api_cache' ) || 
  278. 'true' === fs_request_is_action( 'restart_freemius' ) 
  279. ) { 
  280. FS_Api::clear_cache(); 
  281.  
  282. $this->_register_hooks(); 
  283.  
  284. $this->_load_account(); 
  285.  
  286. $this->_version_updates_handler(); 
  287.  
  288. /** 
  289. * @author Vova Feldman (@svovaf) 
  290. * @since 1.0.9 
  291. */ 
  292. private function _version_updates_handler() { 
  293. if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) { 
  294. // Freemius version upgrade mode. 
  295. $this->_storage->sdk_last_version = $this->_storage->sdk_version; 
  296. $this->_storage->sdk_version = $this->version; 
  297.  
  298. if ( empty( $this->_storage->sdk_last_version ) || 
  299. version_compare( $this->_storage->sdk_last_version, $this->version, '<' ) 
  300. ) { 
  301. $this->_storage->sdk_upgrade_mode = true; 
  302. $this->_storage->sdk_downgrade_mode = false; 
  303. } else { 
  304. $this->_storage->sdk_downgrade_mode = true; 
  305. $this->_storage->sdk_upgrade_mode = false; 
  306.  
  307.  
  308. $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version ); 
  309.  
  310. $plugin_version = $this->get_plugin_version(); 
  311. if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) { 
  312. // Plugin version upgrade mode. 
  313. $this->_storage->plugin_last_version = $this->_storage->plugin_version; 
  314. $this->_storage->plugin_version = $plugin_version; 
  315.  
  316. if ( empty( $this->_storage->plugin_last_version ) || 
  317. version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' ) 
  318. ) { 
  319. $this->_storage->plugin_upgrade_mode = true; 
  320. $this->_storage->plugin_downgrade_mode = false; 
  321. } else { 
  322. $this->_storage->plugin_downgrade_mode = true; 
  323. $this->_storage->plugin_upgrade_mode = false; 
  324.  
  325. if ( ! empty( $this->_storage->plugin_last_version ) ) { 
  326. // Different version of the plugin was installed before, therefore it's an update. 
  327. $this->_storage->is_plugin_new_install = false; 
  328.  
  329. $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version ); 
  330.  
  331. /** 
  332. * @author Vova Feldman (@svovaf) 
  333. * @since 1.1.5 
  334. * @param string $sdk_prev_version 
  335. * @param string $sdk_version 
  336. */ 
  337. function _data_migration( $sdk_prev_version, $sdk_version ) { 
  338. /** 
  339. * @since 1.1.7.3 Fixed unwanted connectivity test cleanup. 
  340. */ 
  341. if ( empty( $sdk_prev_version ) ) { 
  342. return; 
  343.  
  344. if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) && 
  345. version_compare( $sdk_version, '1.1.5', '>=' ) 
  346. ) { 
  347. // On version 1.1.5 merged connectivity and is_on data. 
  348. if ( isset( $this->_storage->connectivity_test ) ) { 
  349. if ( ! isset( $this->_storage->is_on ) ) { 
  350. unset( $this->_storage->connectivity_test ); 
  351. } else { 
  352. $connectivity_data = $this->_storage->connectivity_test; 
  353. $connectivity_data['is_active'] = $this->_storage->is_on['is_active']; 
  354. $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp']; 
  355.  
  356. // Override. 
  357. $this->_storage->connectivity_test = $connectivity_data; 
  358.  
  359. // Remove previous structure. 
  360. unset( $this->_storage->is_on ); 
  361.  
  362.  
  363. /** 
  364. * This action is connected to the 'plugins_loaded' hook and helps to determine 
  365. * if this is a new plugin installation or a plugin update. 
  366. * There are 3 different use-cases: 
  367. * 1) New plugin installation right with Freemius: 
  368. * 1.1 _activate_plugin_event_hook() will be executed first 
  369. * 1.2 Since $this->_storage->is_plugin_new_install is not set,  
  370. * and $this->_storage->plugin_last_version is not set,  
  371. * $this->_storage->is_plugin_new_install will be set to TRUE. 
  372. * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will 
  373. * be already set to TRUE. 
  374. * 2) Plugin update, didn't have Freemius before, and now have the SDK: 
  375. * 2.1 _activate_plugin_event_hook() will not be executed, because 
  376. * the activation hook do NOT fires on updates since WP 3.1. 
  377. * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will 
  378. * be empty, therefore, it will be set to FALSE. 
  379. * 3) Plugin update, had Freemius in prev version as well: 
  380. * 3.1 _version_updates_handler() will be executed 1st, since FS was installed 
  381. * before, $this->_storage->plugin_last_version will NOT be empty,  
  382. * therefore, $this->_storage->is_plugin_new_install will be set to FALSE. 
  383. * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is 
  384. * already set, therefore, it will not be modified. 
  385. * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9. 
  386. * NOTE: 
  387. * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,  
  388. * and then, the next immediate PageView is the plugin's main settings page, it will not 
  389. * show the opt-in right away. The reason it will happen is because Freemius execution 
  390. * will be turned off till the plugin is fully loaded at least once 
  391. * (till $this->_storage->was_plugin_loaded is TRUE). 
  392. * @author Vova Feldman (@svovaf) 
  393. * @since 1.1.9 
  394. */ 
  395. function _plugins_loaded() { 
  396. // Update flag that plugin was loaded with Freemius at least once. 
  397. $this->_storage->was_plugin_loaded = true; 
  398.  
  399. if ( ! isset( $this->_storage->is_plugin_new_install ) ) { 
  400. $this->_storage->is_plugin_new_install = false; 
  401.  
  402. /** 
  403. * @author Vova Feldman (@svovaf) 
  404. * @since 1.0.9 
  405. */ 
  406. private function _register_hooks() { 
  407. $this->_logger->entrance(); 
  408.  
  409. if ( is_admin() ) { 
  410. $plugin_dir = dirname( $this->_plugin_dir_path ) . '/'; 
  411.  
  412. /** 
  413. * @since 1.2.2 
  414. * Hook to both free and premium version activations to support 
  415. * auto deactivation on the other version activation. 
  416. */ 
  417. register_activation_hook( 
  418. $plugin_dir . $this->_free_plugin_basename,  
  419. array( &$this, '_activate_plugin_event_hook' ) 
  420. ); 
  421. register_activation_hook( 
  422. $plugin_dir . $this->premium_plugin_basename(),  
  423. array( &$this, '_activate_plugin_event_hook' ) 
  424. ); 
  425.  
  426. /** 
  427. * Part of the mechanism to identify new plugin install vs. plugin update. 
  428. * @author Vova Feldman (@svovaf) 
  429. * @since 1.1.9 
  430. */ 
  431. if ( empty( $this->_storage->was_plugin_loaded ) ) { 
  432. if ( $this->is_activation_mode( false ) ) { 
  433. add_action( 'plugins_loaded', array( &$this, '_plugins_loaded' ) ); 
  434. } else { 
  435. // If was activated before, then it was already loaded before. 
  436. $this->_plugins_loaded(); 
  437.  
  438. if ( ! $this->is_ajax() ) { 
  439. if ( ! $this->is_addon() ) { 
  440. add_action( 'init', array( &$this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY ); 
  441. add_action( 'admin_menu', array( &$this, '_prepare_admin_menu' ), WP_FS__LOWEST_PRIORITY ); 
  442.  
  443. register_deactivation_hook( $this->_plugin_main_file_path, array( &$this, '_deactivate_plugin_hook' ) ); 
  444.  
  445. add_action( 'init', array( &$this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY ); 
  446.  
  447. add_action( 'admin_init', array( &$this, '_add_tracking_links' ) ); 
  448. add_action( 'admin_init', array( &$this, '_add_license_activation' ) ); 
  449. $this->add_ajax_action( 'update_billing', array( &$this, '_update_billing_ajax_action' ) ); 
  450. $this->add_ajax_action( 'start_trial', array( &$this, '_start_trial_ajax_action' ) ); 
  451.  
  452. $this->add_action( 'after_plans_sync', array( &$this, '_check_for_trial_plans' ) ); 
  453.  
  454. $this->add_action( 'sdk_version_update', array( &$this, '_data_migration' ), WP_FS__DEFAULT_PRIORITY, 2 ); 
  455.  
  456. add_action( 'admin_init', array( &$this, '_add_trial_notice' ) ); 
  457. add_action( 'admin_init', array( &$this, '_enqueue_common_css' ) ); 
  458.  
  459. /** 
  460. * Handle request to reset anonymous mode for `get_reconnect_url()`. 
  461. * @author Vova Feldman (@svovaf) 
  462. * @since 1.2.1.5 
  463. */ 
  464. if ( fs_request_is_action( 'reset_anonymous_mode' ) && 
  465. $this->_slug === fs_request_get( 'fs_slug' ) 
  466. ) { 
  467. add_action( 'admin_init', array( &$this, 'connect_again' ) ); 
  468.  
  469. /** 
  470. * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that 
  471. * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a 
  472. * plugin will trigger inclusion of the free or premium version and if one of them is active during the 
  473. * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined. 
  474. * @author Leo Fajardo (leorw) 
  475. * @since 1.2.0 
  476. */ 
  477. private function unregister_uninstall_hook() { 
  478. $uninstallable_plugins = (array) get_option( 'uninstall_plugins' ); 
  479. unset( $uninstallable_plugins[ $this->_free_plugin_basename ] ); 
  480. unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] ); 
  481.  
  482. update_option( 'uninstall_plugins', $uninstallable_plugins ); 
  483.  
  484. /** 
  485. * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates. 
  486. */ 
  487. private function clear_module_main_file_cache() { 
  488. if ( ! isset( $this->_storage->plugin_main_file ) || 
  489. empty( $this->_storage->plugin_main_file->path ) 
  490. ) { 
  491. return; 
  492.  
  493. $plugin_main_file = clone $this->_storage->plugin_main_file; 
  494.  
  495. // Store cached path (2nd layer cache). 
  496. $plugin_main_file->prev_path = $plugin_main_file->path; 
  497.  
  498. // Clear cached path. 
  499. unset( $plugin_main_file->path ); 
  500.  
  501. $this->_storage->plugin_main_file = $plugin_main_file; 
  502.  
  503. /** 
  504. * @author Vova Feldman (@svovaf) 
  505. * @since 1.0.9 
  506. */ 
  507. private function _register_account_hooks() { 
  508. if ( is_admin() ) { 
  509. // If user is paying or in trial and have the free version installed,  
  510. // assume that the deactivation is for the upgrade process. 
  511. if ( ! $this->is_paying_or_trial() || $this->is_premium() ) { 
  512. $this->add_ajax_action( 
  513. 'submit_uninstall_reason',  
  514. array( &$this, '_submit_uninstall_reason_action' ) 
  515. ); 
  516.  
  517. if ( $this->is_plugins_page() ) { 
  518. add_action( 'admin_footer', array( &$this, '_add_deactivation_feedback_dialog_box' ) ); 
  519.  
  520. if ( ! $this->is_addon() ) { 
  521. if ( $this->is_registered() ) { 
  522. $this->add_filter( 'after_code_type_change', array( &$this, '_after_code_type_change' ) ); 
  523.  
  524. /** 
  525. * Leverage backtrace to find caller plugin file path. 
  526. * @author Vova Feldman (@svovaf) 
  527. * @since 1.0.6 
  528. * @param bool $is_init Is initiation sequence. 
  529. * @return string 
  530. * @uses fs_find_caller_plugin_file 
  531. */ 
  532. private function _find_caller_plugin_file( $is_init = false ) { 
  533. // Try to load the cached value of the file path. 
  534. if ( isset( $this->_storage->plugin_main_file ) ) { 
  535. $plugin_main_file = $this->_storage->plugin_main_file; 
  536. if ( isset( $plugin_main_file->path ) && file_exists( $plugin_main_file->path ) ) { 
  537. return $this->_storage->plugin_main_file->path; 
  538.  
  539. /** 
  540. * @since 1.2.1 
  541. * `clear_module_main_file_cache()` is clearing the plugin's cached path on 
  542. * deactivation. Therefore, if any plugin/theme was initiating `Freemius` 
  543. * with that plugin's slug, it was overriding the empty plugin path with a wrong path. 
  544. * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path` 
  545. * when the class instantiator isn't the module. 
  546. */ 
  547. if ( ! $is_init ) { 
  548. // Fetch prev path cache. 
  549. if ( isset( $this->_storage->plugin_main_file ) && 
  550. isset( $this->_storage->plugin_main_file->prev_path ) 
  551. ) { 
  552. if ( file_exists( $this->_storage->plugin_main_file->prev_path ) ) { 
  553. return $this->_storage->plugin_main_file->prev_path; 
  554.  
  555. wp_die( 
  556. __fs( 'failed-finding-main-path', $this->_slug ),  
  557. __fs( 'error' ),  
  558. array( 'back_link' => true ) 
  559. ); 
  560.  
  561. /** 
  562. * @since 1.2.1 
  563. * Only the original instantiator that calls dynamic_init can modify the module's path. 
  564. */ 
  565. // Find caller module. 
  566. $plugin_file = fs_find_caller_plugin_file(); 
  567.  
  568. $this->_storage->plugin_main_file = (object) array( 
  569. 'path' => fs_normalize_path( $plugin_file ),  
  570. ); 
  571.  
  572. return $plugin_file; 
  573.  
  574.  
  575. #---------------------------------------------------------------------------------- 
  576. #region Deactivation Feedback Form 
  577. #---------------------------------------------------------------------------------- 
  578.  
  579. /** 
  580. * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins 
  581. * page. 
  582. * @author Vova Feldman (@svovaf) 
  583. * @author Leo Fajardo (@leorw) 
  584. * @since 1.1.2 
  585. */ 
  586. function _add_deactivation_feedback_dialog_box() { 
  587. /** Check the type of user: 
  588. * 1. Long-term (long-term) 
  589. * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term). 
  590. * 3. Short-term (short-term) 
  591. */ 
  592. $is_long_term_user = true; 
  593.  
  594. // Check if the site is at least 2 days old. 
  595. $time_installed = $this->_storage->install_timestamp; 
  596.  
  597. // Difference in seconds. 
  598. $date_diff = time() - $time_installed; 
  599.  
  600. // Convert seconds to days. 
  601. $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) ); 
  602.  
  603. if ( $date_diff_days < 2 ) { 
  604. $is_long_term_user = false; 
  605.  
  606. $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user ); 
  607.  
  608. if ( $is_long_term_user ) { 
  609. $user_type = 'long-term'; 
  610. } else { 
  611. if ( ! $this->is_registered() && ! $this->is_anonymous() ) { 
  612. $user_type = 'non-registered-and-non-anonymous-short-term'; 
  613. } else { 
  614. $user_type = 'short-term'; 
  615.  
  616. $uninstall_reasons = $this->_get_uninstall_reasons( $user_type ); 
  617.  
  618. // Load the HTML template for the deactivation feedback dialog box. 
  619. $vars = array( 
  620. 'reasons' => $uninstall_reasons,  
  621. 'slug' => $this->_slug 
  622. ); 
  623.  
  624. /** 
  625. * @todo Deactivation form core functions should be loaded only once! Otherwise, when there are multiple Freemius powered plugins the same code is loaded multiple times. The only thing that should be loaded differently is the various deactivation reasons object based on the state of the plugin. 
  626. */ 
  627. fs_require_template( 'forms/deactivation/form.php', $vars ); 
  628.  
  629. /** 
  630. * @author Leo Fajardo (leorw) 
  631. * @since 1.1.2 
  632. * @param string $user_type 
  633. * @return array The uninstall reasons for the specified user type. 
  634. */ 
  635. function _get_uninstall_reasons( $user_type = 'long-term' ) { 
  636. $internal_message_template_var = array( 
  637. 'slug' => $this->_slug 
  638. ); 
  639.  
  640. if ( $this->is_registered() && false !== $this->get_plan() && $this->get_plan()->has_technical_support() ) { 
  641. $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var ); 
  642. } else { 
  643. $contact_support_template = ''; 
  644.  
  645. $reason_found_better_plugin = array( 
  646. 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,  
  647. 'text' => __fs( 'reason-found-a-better-plugin', $this->_slug ),  
  648. 'input_type' => 'textfield',  
  649. 'input_placeholder' => __fs( 'placeholder-plugin-name', $this->_slug ) 
  650. ); 
  651.  
  652. $reason_temporary_deactivation = array( 
  653. 'id' => self::REASON_TEMPORARY_DEACTIVATION,  
  654. 'text' => __fs( 'reason-temporary-deactivation', $this->_slug ),  
  655. 'input_type' => '',  
  656. 'input_placeholder' => '' 
  657. ); 
  658.  
  659. $reason_other = array( 
  660. 'id' => self::REASON_OTHER,  
  661. 'text' => __fs( 'reason-other', $this->_slug ),  
  662. 'input_type' => 'textfield',  
  663. 'input_placeholder' => '' 
  664. ); 
  665.  
  666. $long_term_user_reasons = array( 
  667. array( 
  668. 'id' => self::REASON_NO_LONGER_NEEDED,  
  669. 'text' => __fs( 'reason-no-longer-needed', $this->_slug ),  
  670. 'input_type' => '',  
  671. 'input_placeholder' => '' 
  672. ),  
  673. $reason_found_better_plugin,  
  674. array( 
  675. 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,  
  676. 'text' => __fs( 'reason-needed-for-a-short-period', $this->_slug ),  
  677. 'input_type' => '',  
  678. 'input_placeholder' => '' 
  679. ),  
  680. array( 
  681. 'id' => self::REASON_BROKE_MY_SITE,  
  682. 'text' => __fs( 'reason-broke-my-site', $this->_slug ),  
  683. 'input_type' => '',  
  684. 'input_placeholder' => '',  
  685. 'internal_message' => $contact_support_template 
  686. ),  
  687. array( 
  688. 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,  
  689. 'text' => __fs( 'reason-suddenly-stopped-working', $this->_slug ),  
  690. 'input_type' => '',  
  691. 'input_placeholder' => '',  
  692. 'internal_message' => $contact_support_template 
  693. ); 
  694.  
  695. if ( $this->is_paying() ) { 
  696. $long_term_user_reasons[] = array( 
  697. 'id' => self::REASON_CANT_PAY_ANYMORE,  
  698. 'text' => __fs( 'reason-cant-pay-anymore', $this->_slug ),  
  699. 'input_type' => 'textfield',  
  700. 'input_placeholder' => __fs( 'placeholder-comfortable-price', $this->_slug ) 
  701. ); 
  702.  
  703. $reason_dont_share_info = array( 
  704. 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,  
  705. 'text' => __fs( 'reason-dont-like-to-share-my-information', $this->_slug ),  
  706. 'input_type' => '',  
  707. 'input_placeholder' => '' 
  708. ); 
  709.  
  710. /** 
  711. * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the 
  712. * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in 
  713. * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is 
  714. * enabled and the user's account is currently not in pending activation state (similar to the way the Skip 
  715. * button in the opt-in form is shown/hidden). 
  716. */ 
  717. if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) { 
  718. $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var ); 
  719.  
  720. $uninstall_reasons = array( 
  721. 'long-term' => $long_term_user_reasons,  
  722. 'non-registered-and-non-anonymous-short-term' => array( 
  723. array( 
  724. 'id' => self::REASON_DIDNT_WORK,  
  725. 'text' => __fs( 'reason-didnt-work', $this->_slug ),  
  726. 'input_type' => '',  
  727. 'input_placeholder' => '' 
  728. ),  
  729. $reason_dont_share_info,  
  730. $reason_found_better_plugin 
  731. ),  
  732. 'short-term' => array( 
  733. array( 
  734. 'id' => self::REASON_COULDNT_MAKE_IT_WORK,  
  735. 'text' => __fs( 'reason-couldnt-make-it-work', $this->_slug ),  
  736. 'input_type' => '',  
  737. 'input_placeholder' => '',  
  738. 'internal_message' => $contact_support_template 
  739. ),  
  740. $reason_found_better_plugin,  
  741. array( 
  742. 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,  
  743. 'text' => __fs( 'reason-great-but-need-specific-feature', $this->_slug ),  
  744. 'input_type' => 'textarea',  
  745. 'input_placeholder' => __fs( 'placeholder-feature', $this->_slug ) 
  746. ),  
  747. array( 
  748. 'id' => self::REASON_NOT_WORKING,  
  749. 'text' => __fs( 'reason-not-working', $this->_slug ),  
  750. 'input_type' => 'textarea',  
  751. 'input_placeholder' => __fs( 'placeholder-share-what-didnt-work', $this->_slug ) 
  752. ),  
  753. array( 
  754. 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,  
  755. 'text' => __fs( 'reason-not-what-i-was-looking-for', $this->_slug ),  
  756. 'input_type' => 'textarea',  
  757. 'input_placeholder' => __fs( 'placeholder-what-youve-been-looking-for', $this->_slug ) 
  758. ),  
  759. array( 
  760. 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,  
  761. 'text' => __fs( 'reason-didnt-work-as-expected', $this->_slug ),  
  762. 'input_type' => 'textarea',  
  763. 'input_placeholder' => __fs( 'placeholder-what-did-you-expect', $this->_slug ) 
  764. ); 
  765.  
  766. // Randomize the reasons for the current user type. 
  767. shuffle( $uninstall_reasons[ $user_type ] ); 
  768.  
  769. // Keep the following reasons as the last items in the list. 
  770. $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation; 
  771. $uninstall_reasons[ $user_type ][] = $reason_other; 
  772.  
  773. $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons ); 
  774.  
  775. return $uninstall_reasons[ $user_type ]; 
  776.  
  777. /** 
  778. * Called after the user has submitted his reason for deactivating the plugin. 
  779. * @author Leo Fajardo (@leorw) 
  780. * @since 1.1.2 
  781. */ 
  782. function _submit_uninstall_reason_action() { 
  783. $reason_id = fs_request_get( 'reason_id' ); 
  784.  
  785. // Check if the given reason ID is an unsigned integer. 
  786. if ( ! ctype_digit( $reason_id ) ) { 
  787. exit; 
  788.  
  789. $reason_info = trim( fs_request_get( 'reason_info', '' ) ); 
  790. if ( ! empty( $reason_info ) ) { 
  791. $reason_info = substr( $reason_info, 0, 128 ); 
  792.  
  793. $reason = (object) array( 
  794. 'id' => $reason_id,  
  795. 'info' => $reason_info,  
  796. 'is_anonymous' => fs_request_get_bool( 'is_anonymous' ) 
  797. ); 
  798.  
  799. $this->_storage->store( 'uninstall_reason', $reason ); 
  800.  
  801. // Print '1' for successful operation. 
  802. echo 1; 
  803. exit; 
  804.  
  805. #endregion 
  806.  
  807. #---------------------------------------------------------------------------------- 
  808. #region Instance 
  809. #---------------------------------------------------------------------------------- 
  810.  
  811. /** 
  812. * Main singleton instance. 
  813. * @author Vova Feldman (@svovaf) 
  814. * @since 1.0.0 
  815. * @param string $slug 
  816. * @param bool $is_init Is initiation sequence. 
  817. * @return Freemius 
  818. */ 
  819. static function instance( $slug, $is_init = false ) { 
  820. $slug = strtolower( $slug ); 
  821.  
  822. if ( ! isset( self::$_instances[ $slug ] ) ) { 
  823. if ( 0 === count( self::$_instances ) ) { 
  824. self::_load_required_static(); 
  825.  
  826. self::$_instances[ $slug ] = new Freemius( $slug, $is_init ); 
  827.  
  828. return self::$_instances[ $slug ]; 
  829.  
  830. /** 
  831. * @author Vova Feldman (@svovaf) 
  832. * @since 1.0.6 
  833. * @param string|number $slug_or_id 
  834. * @return bool 
  835. */ 
  836. private static function has_instance( $slug_or_id ) { 
  837. return ! is_numeric( $slug_or_id ) ? 
  838. isset( self::$_instances[ strtolower( $slug_or_id ) ] ) : 
  839. ( false !== self::get_instance_by_id( $slug_or_id ) ); 
  840.  
  841. /** 
  842. * @author Vova Feldman (@svovaf) 
  843. * @since 1.0.6 
  844. * @param $id 
  845. * @return false|Freemius 
  846. */ 
  847. static function get_instance_by_id( $id ) { 
  848. foreach ( self::$_instances as $slug => $instance ) { 
  849. if ( $id == $instance->get_id() ) { 
  850. return $instance; 
  851.  
  852. return false; 
  853.  
  854. /** 
  855. * @author Vova Feldman (@svovaf) 
  856. * @since 1.0.1 
  857. * @param $plugin_file 
  858. * @return false|Freemius 
  859. */ 
  860. static function get_instance_by_file( $plugin_file ) { 
  861. $slug = self::find_slug_by_basename( $plugin_file ); 
  862.  
  863. return ( false !== $slug ) ? 
  864. self::instance( $slug ) : 
  865. false; 
  866.  
  867. /** 
  868. * @author Vova Feldman (@svovaf) 
  869. * @since 1.0.6 
  870. * @return false|Freemius 
  871. */ 
  872. function get_parent_instance() { 
  873. return self::get_instance_by_id( $this->_plugin->parent_plugin_id ); 
  874.  
  875. /** 
  876. * @author Vova Feldman (@svovaf) 
  877. * @since 1.0.6 
  878. * @param $slug_or_id 
  879. * @return bool|Freemius 
  880. */ 
  881. function get_addon_instance( $slug_or_id ) { 
  882. return ! is_numeric( $slug_or_id ) ? 
  883. self::instance( strtolower( $slug_or_id ) ) : 
  884. self::get_instance_by_id( $slug_or_id ); 
  885.  
  886. #endregion ------------------------------------------------------------------ 
  887.  
  888. /** 
  889. * @author Vova Feldman (@svovaf) 
  890. * @since 1.0.6 
  891. * @return bool 
  892. */ 
  893. function is_parent_plugin_installed() { 
  894. return self::has_instance( $this->_plugin->parent_plugin_id ); 
  895.  
  896. /** 
  897. * Check if add-on parent plugin in activation mode. 
  898. * @author Vova Feldman (@svovaf) 
  899. * @since 1.0.7 
  900. * @return bool 
  901. */ 
  902. function is_parent_in_activation() { 
  903. $parent_fs = $this->get_parent_instance(); 
  904. if ( ! is_object( $parent_fs ) ) { 
  905. return false; 
  906.  
  907. return ( $parent_fs->is_activation_mode() ); 
  908.  
  909. /** 
  910. * Is plugin in activation mode. 
  911. * @author Vova Feldman (@svovaf) 
  912. * @since 1.0.7 
  913. * @param bool $and_on 
  914. * @return bool 
  915. */ 
  916. function is_activation_mode( $and_on = true ) { 
  917. return ( 
  918. ( $this->is_on() || ! $and_on ) && 
  919. ! $this->is_registered() && 
  920. ( ! $this->is_enable_anonymous() || 
  921. ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) ) 
  922. ); 
  923.  
  924. /** 
  925. * Get collection of all active plugins. 
  926. * @author Vova Feldman (@svovaf) 
  927. * @since 1.0.9 
  928. * @return array[string]array 
  929. */ 
  930. private static function get_active_plugins() { 
  931. self::require_plugin_essentials(); 
  932.  
  933. $active_plugin = array(); 
  934. $all_plugins = get_plugins(); 
  935. $active_plugins_basenames = get_option( 'active_plugins' ); 
  936.  
  937. foreach ( $active_plugins_basenames as $plugin_basename ) { 
  938. $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ]; 
  939.  
  940. return $active_plugin; 
  941.  
  942. /** 
  943. * Get collection of all plugins. 
  944. * @author Vova Feldman (@svovaf) 
  945. * @since 1.1.8 
  946. * @return array Key is the plugin file path and the value is an array of the plugin data. 
  947. */ 
  948. private static function get_all_plugins() { 
  949. self::require_plugin_essentials(); 
  950.  
  951. $all_plugins = get_plugins(); 
  952. $active_plugins_basenames = get_option( 'active_plugins' ); 
  953.  
  954. foreach ( $all_plugins as $basename => &$data ) { 
  955. // By default set to inactive (next foreach update the active plugins). 
  956. $data['is_active'] = false; 
  957. // Enrich with plugin slug. 
  958. $data['slug'] = self::get_plugin_slug( $basename ); 
  959.  
  960. // Flag active plugins. 
  961. foreach ( $active_plugins_basenames as $basename ) { 
  962. if ( isset( $all_plugins[ $basename ] ) ) { 
  963. $all_plugins[ $basename ]['is_active'] = true; 
  964.  
  965. return $all_plugins; 
  966.  
  967.  
  968. /** 
  969. * Cached result of get_site_transient( 'update_plugins' ) 
  970. * @author Vova Feldman (@svovaf) 
  971. * @since 1.1.8 
  972. * @var object 
  973. */ 
  974. private static $_plugins_info; 
  975.  
  976. /** 
  977. * Helper function to get specified plugin's slug. 
  978. * @author Vova Feldman (@svovaf) 
  979. * @since 1.1.8 
  980. * @param $basename 
  981. * @return string 
  982. */ 
  983. private static function get_plugin_slug( $basename ) { 
  984. if ( ! isset( self::$_plugins_info ) ) { 
  985. self::$_plugins_info = get_site_transient( 'update_plugins' ); 
  986.  
  987. $slug = ''; 
  988.  
  989. if ( is_object( self::$_plugins_info ) ) { 
  990. if ( isset( self::$_plugins_info->no_update ) && 
  991. isset( self::$_plugins_info->no_update[ $basename ] ) && 
  992. ! empty( self::$_plugins_info->no_update[ $basename ]->slug ) 
  993. ) { 
  994. $slug = self::$_plugins_info->no_update[ $basename ]->slug; 
  995. } else if ( isset( self::$_plugins_info->response ) && 
  996. isset( self::$_plugins_info->response[ $basename ] ) && 
  997. ! empty( self::$_plugins_info->response[ $basename ]->slug ) 
  998. ) { 
  999. $slug = self::$_plugins_info->response[ $basename ]->slug; 
  1000.  
  1001. if ( empty( $slug ) ) { 
  1002. // Try to find slug from FS data. 
  1003. $slug = self::find_slug_by_basename( $basename ); 
  1004.  
  1005. if ( empty( $slug ) ) { 
  1006. // Fallback to plugin's folder name. 
  1007. $slug = dirname( $basename ); 
  1008.  
  1009. return $slug; 
  1010.  
  1011. private static $_statics_loaded = false; 
  1012.  
  1013. /** 
  1014. * Load static resources. 
  1015. * @author Vova Feldman (@svovaf) 
  1016. * @since 1.0.1 
  1017. */ 
  1018. private static function _load_required_static() { 
  1019. if ( self::$_statics_loaded ) { 
  1020. return; 
  1021.  
  1022. self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); 
  1023.  
  1024. self::$_static_logger->entrance(); 
  1025.  
  1026. self::$_accounts = FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true ); 
  1027.  
  1028. self::$_global_admin_notices = FS_Admin_Notice_Manager::instance( 'global' ); 
  1029.  
  1030. // Configure which Freemius powered plugins should be auto updated. 
  1031. // add_filter( 'auto_update_plugin', '_include_plugins_in_auto_update', 10, 2 ); 
  1032.  
  1033. add_action( 'admin_menu', array( 'Freemius', 'add_debug_page' ) ); 
  1034.  
  1035. add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) ); 
  1036.  
  1037. add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 ); 
  1038.  
  1039. self::$_statics_loaded = true; 
  1040.  
  1041. #---------------------------------------------------------------------------------- 
  1042. #region Localization 
  1043. #---------------------------------------------------------------------------------- 
  1044.  
  1045. /** 
  1046. * Load framework's text domain. 
  1047. * @author Vova Feldman (@svovaf) 
  1048. * @since 1.2.1 
  1049. */ 
  1050. static function _load_textdomain() { 
  1051. if ( ! is_admin() ) { 
  1052. return; 
  1053.  
  1054. global $fs_active_plugins; 
  1055.  
  1056. load_plugin_textdomain( 
  1057. 'freemius',  
  1058. false,  
  1059. $fs_active_plugins->newest->sdk_path . '/languages/' 
  1060. ); 
  1061.  
  1062. // @todo Load for themes. 
  1063.  
  1064. #endregion 
  1065.  
  1066. #---------------------------------------------------------------------------------- 
  1067. #region Debugging 
  1068. #---------------------------------------------------------------------------------- 
  1069.  
  1070. /** 
  1071. * @author Vova Feldman (@svovaf) 
  1072. * @since 1.0.8 
  1073. */ 
  1074. static function add_debug_page() { 
  1075. if ( ! current_user_can( 'activate_plugins' ) ) { 
  1076. return; 
  1077.  
  1078. self::$_static_logger->entrance(); 
  1079.  
  1080. $title = sprintf( '%s [v.%s]', __fs( 'freemius-debug' ), WP_FS__SDK_VERSION ); 
  1081.  
  1082. if ( WP_FS__DEV_MODE ) { 
  1083. // Add top-level debug menu item. 
  1084. $hook = add_menu_page( 
  1085. $title,  
  1086. $title,  
  1087. 'manage_options',  
  1088. 'freemius',  
  1089. array( 'Freemius', '_debug_page_render' ) 
  1090. ); 
  1091. } else { 
  1092. // Add hidden debug page. 
  1093. $hook = add_submenu_page( 
  1094. null,  
  1095. $title,  
  1096. $title,  
  1097. 'manage_options',  
  1098. 'freemius',  
  1099. array( 'Freemius', '_debug_page_render' ) 
  1100. ); 
  1101.  
  1102. add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) ); 
  1103.  
  1104. /** 
  1105. * @author Vova Feldman (@svovaf) 
  1106. * @since 1.1.7.3 
  1107. */ 
  1108. static function _toggle_debug_mode() { 
  1109. if ( in_array( $_POST['is_on'], array( 0, 1 ) ) ) { 
  1110. update_option( 'fs_debug_mode', $_POST['is_on'] ); 
  1111.  
  1112. exit; 
  1113.  
  1114. /** 
  1115. * @author Vova Feldman (@svovaf) 
  1116. * @since 1.0.8 
  1117. */ 
  1118. static function _debug_page_actions() { 
  1119. self::_clean_admin_content_section(); 
  1120.  
  1121. if ( fs_request_is_action( 'restart_freemius' ) ) { 
  1122. check_admin_referer( 'restart_freemius' ); 
  1123.  
  1124. // Clear accounts data. 
  1125. self::$_accounts->clear( true ); 
  1126.  
  1127. // Clear SDK reference cache. 
  1128. delete_option( 'fs_active_plugins' ); 
  1129. } else if ( fs_request_is_action( 'simulate_trial' ) ) { 
  1130. check_admin_referer( 'simulate_trial' ); 
  1131.  
  1132. $slug = fs_request_get( 'slug' ); 
  1133.  
  1134. $fs = freemius( $slug ); 
  1135.  
  1136. // Update SDK install to at least 24 hours before. 
  1137. $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC ); 
  1138. // Unset the trial shown timestamp. 
  1139. unset( $fs->_storage->trial_promotion_shown ); 
  1140.  
  1141. /** 
  1142. * @author Vova Feldman (@svovaf) 
  1143. * @since 1.0.8 
  1144. */ 
  1145. static function _debug_page_render() { 
  1146. self::$_static_logger->entrance(); 
  1147.  
  1148. $sites = self::get_all_sites(); 
  1149. $users = self::get_all_users(); 
  1150. $addons = self::get_all_addons(); 
  1151. $account_addons = self::get_all_account_addons(); 
  1152. $licenses = self::get_all_licenses(); 
  1153.  
  1154. // $plans = self::get_all_plans(); 
  1155. // $licenses = self::get_all_licenses(); 
  1156.  
  1157. $vars = array( 
  1158. 'sites' => $sites,  
  1159. 'users' => $users,  
  1160. 'addons' => $addons,  
  1161. 'account_addons' => $account_addons,  
  1162. 'licenses' => $licenses,  
  1163. ); 
  1164.  
  1165. fs_enqueue_local_style( 'fs_account', '/admin/debug.css' ); 
  1166. fs_require_once_template( 'debug.php', $vars ); 
  1167.  
  1168. #endregion 
  1169.  
  1170. #---------------------------------------------------------------------------------- 
  1171. #region Connectivity Issues 
  1172. #---------------------------------------------------------------------------------- 
  1173.  
  1174. /** 
  1175. * Check if Freemius should be turned on for the current plugin install. 
  1176. * Note: 
  1177. * $this->_is_on is updated in has_api_connectivity() 
  1178. * @author Vova Feldman (@svovaf) 
  1179. * @since 1.0.9 
  1180. * @return bool 
  1181. */ 
  1182. function is_on() { 
  1183. self::$_static_logger->entrance(); 
  1184.  
  1185. if ( isset( $this->_is_on ) ) { 
  1186. return $this->_is_on; 
  1187.  
  1188. // If already installed or pending then sure it's on :) 
  1189. if ( $this->is_registered() || $this->is_pending_activation() ) { 
  1190. $this->_is_on = true; 
  1191.  
  1192. return true; 
  1193.  
  1194. return false; 
  1195.  
  1196. /** 
  1197. * @author Vova Feldman (@svovaf) 
  1198. * @since 1.1.7.3 
  1199. * @param bool $flush_if_no_connectivity 
  1200. * @return bool 
  1201. */ 
  1202. private function should_run_connectivity_test( $flush_if_no_connectivity = false ) { 
  1203. if ( ! isset( $this->_storage->connectivity_test ) ) { 
  1204. // Connectivity test was never executed, or cache was cleared. 
  1205. return true; 
  1206.  
  1207. if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) { 
  1208. if ( WP_FS__IS_HTTP_REQUEST ) { 
  1209. if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) { 
  1210. // Domain changed. 
  1211. return true; 
  1212.  
  1213. if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) { 
  1214. // Server IP changed. 
  1215. return true; 
  1216.  
  1217. if ( $this->_storage->connectivity_test['is_connected'] && 
  1218. $this->_storage->connectivity_test['is_active'] 
  1219. ) { 
  1220. // API connected and Freemius is active - no need to run connectivity check. 
  1221. return false; 
  1222.  
  1223. if ( $flush_if_no_connectivity ) { 
  1224. /** 
  1225. * If explicitly asked to flush when no connectivity - do it only 
  1226. * if at least 10 sec passed from the last API connectivity test. 
  1227. */ 
  1228. return ( isset( $this->_storage->connectivity_test['timestamp'] ) && 
  1229. ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 ); 
  1230.  
  1231. /** 
  1232. * @since 1.1.7 Don't check for connectivity on plugin downgrade. 
  1233. */ 
  1234. $version = $this->get_plugin_version(); 
  1235. if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) { 
  1236. // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test. 
  1237. return true; 
  1238.  
  1239. return false; 
  1240.  
  1241. /** 
  1242. * @author Vova Feldman (@svovaf) 
  1243. * @since 1.1.7.4 
  1244. * @return object|false 
  1245. */ 
  1246. private function ping() { 
  1247. if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) { 
  1248. return false; 
  1249.  
  1250. $version = $this->get_plugin_version(); 
  1251.  
  1252. $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() ); 
  1253.  
  1254. return $this->get_api_plugin_scope()->ping( 
  1255. $this->get_anonymous_id(),  
  1256. array( 
  1257. 'is_update' => json_encode( $is_update ),  
  1258. 'version' => $version,  
  1259. 'sdk' => $this->version,  
  1260. 'is_admin' => json_encode( is_admin() ),  
  1261. 'is_ajax' => json_encode( $this->is_ajax() ),  
  1262. 'is_cron' => json_encode( $this->is_cron() ),  
  1263. 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),  
  1264. ); 
  1265.  
  1266. /** 
  1267. * Check if there's any connectivity issue to Freemius API. 
  1268. * @author Vova Feldman (@svovaf) 
  1269. * @since 1.0.9 
  1270. * @param bool $flush_if_no_connectivity 
  1271. * @return bool 
  1272. */ 
  1273. function has_api_connectivity( $flush_if_no_connectivity = false ) { 
  1274. $this->_logger->entrance(); 
  1275.  
  1276. if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) { 
  1277. return $this->_has_api_connection; 
  1278.  
  1279. if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY && 
  1280. isset( $this->_storage->connectivity_test ) && 
  1281. true === $this->_storage->connectivity_test['is_connected'] 
  1282. ) { 
  1283. unset( $this->_storage->connectivity_test ); 
  1284.  
  1285. if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) { 
  1286. $this->_has_api_connection = $this->_storage->connectivity_test['is_connected']; 
  1287. /** 
  1288. * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration. 
  1289. * @since 1.2.1.5 If the user running the premium version then ignore the 'is_active' flag and turn Freemius on to enable license key activation. 
  1290. */ 
  1291. $this->_is_on = $this->_storage->connectivity_test['is_active'] || 
  1292. $this->is_premium() || 
  1293. ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF ); 
  1294.  
  1295. return $this->_has_api_connection; 
  1296.  
  1297. $pong = $this->ping(); 
  1298. $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong ); 
  1299.  
  1300. if ( ! $is_connected ) { 
  1301. // API failure. 
  1302. $this->_add_connectivity_issue_message( $pong ); 
  1303.  
  1304. $this->store_connectivity_info( $pong, $is_connected ); 
  1305.  
  1306. return $this->_has_api_connection; 
  1307.  
  1308. /** 
  1309. * @author Vova Feldman (@svovaf) 
  1310. * @since 1.1.7.4 
  1311. * @param object $pong 
  1312. * @param bool $is_connected 
  1313. */ 
  1314. private function store_connectivity_info( $pong, $is_connected ) { 
  1315. $this->_logger->entrance(); 
  1316.  
  1317. $version = $this->get_plugin_version(); 
  1318.  
  1319. if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) { 
  1320. $is_active = false; 
  1321. } else { 
  1322. $is_active = ( isset( $pong->is_active ) && true == $pong->is_active ); 
  1323.  
  1324. $is_active = $this->apply_filters( 
  1325. 'is_on',  
  1326. $is_active,  
  1327. $this->is_plugin_update(),  
  1328. $version 
  1329. ); 
  1330.  
  1331. $this->_storage->connectivity_test = array( 
  1332. 'is_connected' => $is_connected,  
  1333. 'host' => $_SERVER['HTTP_HOST'],  
  1334. 'server_ip' => WP_FS__REMOTE_ADDR,  
  1335. 'is_active' => $is_active,  
  1336. 'timestamp' => WP_FS__SCRIPT_START_TIME,  
  1337. // Last version with connectivity attempt. 
  1338. 'version' => $version,  
  1339. ); 
  1340.  
  1341. $this->_has_api_connection = $is_connected; 
  1342. $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF ); 
  1343.  
  1344. /** 
  1345. * Force turning Freemius on. 
  1346. * @author Vova Feldman (@svovaf) 
  1347. * @since 1.1.8.1 
  1348. * @return bool TRUE if successfully turned on. 
  1349. */ 
  1350. private function turn_on() { 
  1351. $this->_logger->entrance(); 
  1352.  
  1353. if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) { 
  1354. return false; 
  1355.  
  1356. $updated_connectivity = $this->_storage->connectivity_test; 
  1357. $updated_connectivity['is_active'] = true; 
  1358. $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME; 
  1359. $this->_storage->connectivity_test = $updated_connectivity; 
  1360.  
  1361. $this->_is_on = true; 
  1362.  
  1363. return true; 
  1364.  
  1365. /** 
  1366. * Anonymous and unique site identifier (Hash). 
  1367. * @author Vova Feldman (@svovaf) 
  1368. * @since 1.1.0 
  1369. * @return string 
  1370. */ 
  1371. function get_anonymous_id() { 
  1372. $unique_id = self::$_accounts->get_option( 'unique_id' ); 
  1373.  
  1374. if ( empty( $unique_id ) || ! is_string( $unique_id ) ) { 
  1375. $key = get_site_url(); 
  1376.  
  1377. // If localhost, assign microtime instead of domain. 
  1378. if ( WP_FS__IS_LOCALHOST || 
  1379. false !== strpos( $key, 'localhost' ) || 
  1380. false === strpos( $key, '.' ) 
  1381. ) { 
  1382. $key = microtime(); 
  1383.  
  1384. $unique_id = md5( $key ); 
  1385.  
  1386. self::$_accounts->set_option( 'unique_id', $unique_id, true ); 
  1387.  
  1388. $this->_logger->departure( $unique_id ); 
  1389.  
  1390. return $unique_id; 
  1391.  
  1392. /** 
  1393. * @author Vova Feldman (@svovaf) 
  1394. * @since 1.1.7.4 
  1395. * @return \WP_User 
  1396. */ 
  1397. static function _get_current_wp_user() { 
  1398. self::require_pluggable_essentials(); 
  1399.  
  1400. return wp_get_current_user(); 
  1401.  
  1402. /** 
  1403. * Generate API connectivity issue message. 
  1404. * @author Vova Feldman (@svovaf) 
  1405. * @since 1.0.9 
  1406. * @param mixed $api_result 
  1407. * @param bool $is_first_failure 
  1408. */ 
  1409. function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) { 
  1410. if ( ! $this->is_premium() && $this->_enable_anonymous ) { 
  1411. // Don't add message if it's the free version and can run anonymously. 
  1412. return; 
  1413.  
  1414. if ( ! function_exists( 'wp_nonce_url' ) ) { 
  1415. require_once( ABSPATH . 'wp-includes/functions.php' ); 
  1416.  
  1417. $current_user = self::_get_current_wp_user(); 
  1418. // $admin_email = get_option( 'admin_email' ); 
  1419. $admin_email = $current_user->user_email; 
  1420.  
  1421. $message = false; 
  1422. if ( is_object( $api_result ) && 
  1423. isset( $api_result->error ) && 
  1424. isset( $api_result->error->code ) 
  1425. ) { 
  1426. switch ( $api_result->error->code ) { 
  1427. case 'curl_missing': 
  1428. $message = sprintf( 
  1429. __fs( 'x-requires-access-to-api', $this->_slug ) . ' ' . 
  1430. __fs( 'curl-missing-message', $this->_slug ) . ' ' . 
  1431. ' %s',  
  1432. '<b>' . $this->get_plugin_name() . '</b>',  
  1433. sprintf( 
  1434. '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',  
  1435. sprintf( 
  1436. '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',  
  1437. __fs( 'curl-missing-no-clue-title', $this->_slug ),  
  1438. ' - ' . sprintf( 
  1439. __fs( 'curl-missing-no-clue-desc', $this->_slug ),  
  1440. '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>' 
  1441. ),  
  1442. sprintf( 
  1443. '<b>%s</b> - %s',  
  1444. __fs( 'sysadmin-title', $this->_slug ),  
  1445. __fs( 'curl-missing-sysadmin-desc', $this->_slug ) 
  1446. ),  
  1447. sprintf( 
  1448. '<a href="%s"><b>%s</b></a>%s',  
  1449. wp_nonce_url( 'plugins.php?action=deactivate&plugin=' . $this->_plugin_basename . '&plugin_status=' . 'all' . '&paged=' . '1' . '&s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),  
  1450. __fs( 'deactivate-plugin-title', $this->_slug ),  
  1451. ' - ' . __fs( 'deactivate-plugin-desc', 'freemius', $this->_slug ) 
  1452. ); 
  1453. break; 
  1454. case 'cloudflare_ddos_protection': 
  1455. $message = sprintf( 
  1456. __fs( 'x-requires-access-to-api', $this->_slug ) . ' ' . 
  1457. __fs( 'cloudflare-blocks-connection-message', $this->_slug ) . ' ' . 
  1458. __fs( 'happy-to-resolve-issue-asap', $this->_slug ) . 
  1459. ' %s',  
  1460. '<b>' . $this->get_plugin_name() . '</b>',  
  1461. sprintf( 
  1462. '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',  
  1463. sprintf( 
  1464. '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',  
  1465. __fs( 'fix-issue-title', $this->_slug ),  
  1466. ' - ' . sprintf( 
  1467. __fs( 'fix-issue-desc', $this->_slug ),  
  1468. '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>' 
  1469. ),  
  1470. sprintf( 
  1471. '<a href="%s" target="_blank"><b>%s</b></a>%s',  
  1472. sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),  
  1473. __fs( 'install-previous-title', $this->_slug ),  
  1474. ' - ' . __fs( 'install-previous-desc', $this->_slug ) 
  1475. ),  
  1476. sprintf( 
  1477. '<a href="%s"><b>%s</b></a>%s',  
  1478. wp_nonce_url( 'plugins.php?action=deactivate&plugin=' . $this->_plugin_basename . '&plugin_status=' . 'all' . '&paged=' . '1' . '&s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),  
  1479. __fs( 'deactivate-plugin-title', $this->_slug ),  
  1480. ' - ' . __fs( 'deactivate-plugin-desc', $this->_slug ) 
  1481. ); 
  1482. break; 
  1483. case 'squid_cache_block': 
  1484. $message = sprintf( 
  1485. __fs( 'x-requires-access-to-api', $this->_slug ) . ' ' . 
  1486. __fs( 'squid-blocks-connection-message', $this->_slug ) . 
  1487. ' %s',  
  1488. '<b>' . $this->get_plugin_name() . '</b>',  
  1489. sprintf( 
  1490. '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',  
  1491. sprintf( 
  1492. '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a>%s',  
  1493. __fs( 'squid-no-clue-title', $this->_slug ),  
  1494. ' - ' . sprintf( 
  1495. __fs( 'squid-no-clue-desc', $this->_slug ),  
  1496. '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>' 
  1497. ),  
  1498. sprintf( 
  1499. '<b>%s</b> - %s',  
  1500. __fs( 'sysadmin-title', $this->_slug ),  
  1501. sprintf( 
  1502. __fs( 'squid-sysadmin-desc', $this->_slug ),  
  1503. // We use a filter since the plugin might require additional API connectivity. 
  1504. '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array( 'api.freemius.com' ) ) ) . '</b>' ) 
  1505. ),  
  1506. sprintf( 
  1507. '<a href="%s"><b>%s</b></a>%s',  
  1508. wp_nonce_url( 'plugins.php?action=deactivate&plugin=' . $this->_plugin_basename . '&plugin_status=' . 'all' . '&paged=' . '1' . '&s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),  
  1509. __fs( 'deactivate-plugin-title', $this->_slug ),  
  1510. ' - ' . __fs( 'deactivate-plugin-desc', $this->_slug ) 
  1511. ); 
  1512. break; 
  1513. // default: 
  1514. // $message = __fs( 'connectivity-test-fails-message', $this->_slug ); 
  1515. // break; 
  1516.  
  1517. $message_id = 'failed_connect_api'; 
  1518. $type = 'error'; 
  1519.  
  1520. if ( false === $message ) { 
  1521. if ( $is_first_failure ) { 
  1522. // First attempt failed. 
  1523. $message = sprintf( 
  1524. __fs( 'x-requires-access-to-api', $this->_slug ) . ' ' . 
  1525. __fs( 'connectivity-test-fails-message', $this->_slug ) . ' ' . 
  1526. __fs( 'connectivity-test-maybe-temporary', $this->_slug ) . '<br><br>' . 
  1527. '%s',  
  1528. '<b>' . $this->get_plugin_name() . '</b>',  
  1529. sprintf( 
  1530. '<div id="fs_firewall_issue_options">%s %s</div>',  
  1531. sprintf( 
  1532. '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',  
  1533. __fs( 'yes-do-your-thing', $this->_slug ) 
  1534. ),  
  1535. sprintf( 
  1536. '<a href="%s" class="button">%s</a>',  
  1537. wp_nonce_url( 'plugins.php?action=deactivate&plugin=' . $this->_plugin_basename . '&plugin_status=' . 'all' . '&paged=' . '1' . '&s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),  
  1538. __fs( 'no-deactivate', $this->_slug ) 
  1539. ); 
  1540.  
  1541. $message_id = 'failed_connect_api_first'; 
  1542. $type = 'promotion'; 
  1543. } else { 
  1544. // Second connectivity attempt failed. 
  1545. $message = sprintf( 
  1546. __fs( 'x-requires-access-to-api', $this->_slug ) . ' ' . 
  1547. __fs( 'connectivity-test-fails-message', $this->_slug ) . ' ' . 
  1548. __fs( 'happy-to-resolve-issue-asap', $this->_slug ) . 
  1549. ' %s',  
  1550. '<b>' . $this->get_plugin_name() . '</b>',  
  1551. sprintf( 
  1552. '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',  
  1553. sprintf( 
  1554. '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',  
  1555. __fs( 'fix-issue-title', $this->_slug ),  
  1556. ' - ' . sprintf( 
  1557. __fs( 'fix-issue-desc', $this->_slug ),  
  1558. '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>' 
  1559. ),  
  1560. sprintf( 
  1561. '<a href="%s" target="_blank"><b>%s</b></a>%s',  
  1562. sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),  
  1563. __fs( 'install-previous-title', $this->_slug ),  
  1564. ' - ' . __fs( 'install-previous-desc', $this->_slug ) 
  1565. ),  
  1566. sprintf( 
  1567. '<a href="%s"><b>%s</b></a>%s',  
  1568. wp_nonce_url( 'plugins.php?action=deactivate&plugin=' . $this->_plugin_basename . '&plugin_status=' . 'all' . '&paged=' . '1' . '&s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),  
  1569. __fs( 'deactivate-plugin-title', $this->_slug ),  
  1570. ' - ' . __fs( 'deactivate-plugin-desc', $this->_slug ) 
  1571. ); 
  1572.  
  1573. $this->_admin_notices->add_sticky( 
  1574. $message,  
  1575. $message_id,  
  1576. __fs( 'oops', $this->_slug ) . '...',  
  1577. $type 
  1578. ); 
  1579.  
  1580. /** 
  1581. * Handle user request to resolve connectivity issue. 
  1582. * This method will send an email to Freemius API technical staff for resolution. 
  1583. * The email will contain server's info and installed plugins (might be caching issue). 
  1584. * @author Vova Feldman (@svovaf) 
  1585. * @since 1.0.9 
  1586. */ 
  1587. function _email_about_firewall_issue() { 
  1588. $this->_admin_notices->remove_sticky( 'failed_connect_api' ); 
  1589.  
  1590. $pong = $this->ping(); 
  1591.  
  1592. $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong ); 
  1593.  
  1594. if ( $is_connected ) { 
  1595. $this->store_connectivity_info( $pong, $is_connected ); 
  1596.  
  1597. echo $this->get_after_plugin_activation_redirect_url(); 
  1598. exit; 
  1599.  
  1600. $current_user = self::_get_current_wp_user(); 
  1601. $admin_email = $current_user->user_email; 
  1602.  
  1603. $error_type = fs_request_get( 'error_type', 'general' ); 
  1604.  
  1605. switch ( $error_type ) { 
  1606. case 'squid': 
  1607. $title = 'Squid ACL Blocking Issue'; 
  1608. break; 
  1609. case 'cloudflare': 
  1610. $title = 'CloudFlare Blocking Issue'; 
  1611. break; 
  1612. default: 
  1613. $title = 'API Connectivity Issue'; 
  1614. break; 
  1615.  
  1616. $custom_email_sections = array(); 
  1617.  
  1618. if ( 'squid' === $error_type ) { 
  1619. // Override the 'Site' email section. 
  1620. $custom_email_sections['site'] = array( 
  1621. 'rows' => array( 
  1622. 'hosting_company' => array( 'Hosting Company', fs_request_get( 'hosting_company' ) ) 
  1623. ); 
  1624.  
  1625. // Add 'API Error' custom email section. 
  1626. $custom_email_sections['api_error'] = array( 
  1627. 'title' => 'API Error',  
  1628. 'rows' => array( 
  1629. 'ping' => array( is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong ) ) 
  1630. ); 
  1631.  
  1632. // Add PHP info for deeper investigation. 
  1633. ob_start(); 
  1634. phpinfo(); 
  1635. $php_info = ob_get_clean(); 
  1636. $custom_email_sections['php_info'] = array( 
  1637. 'title' => 'PHP Info',  
  1638. 'rows' => array( 
  1639. 'info' => array( $php_info ) 
  1640. ); 
  1641.  
  1642. // Send email with technical details to resolve CloudFlare's firewall unnecessary protection. 
  1643. $this->send_email( 
  1644. 'api@freemius.com', // recipient 
  1645. $title . ' [' . $this->get_plugin_name() . ']', // subject 
  1646. $custom_email_sections,  
  1647. array( "Reply-To: $admin_email <$admin_email>" ) // headers 
  1648. ); 
  1649.  
  1650. $this->_admin_notices->add_sticky( 
  1651. sprintf( 
  1652. __fs( 'fix-request-sent-message', $this->_slug ),  
  1653. '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>' 
  1654. ),  
  1655. 'server_details_sent' 
  1656. ); 
  1657.  
  1658. // Action was taken, tell that API connectivity troubleshooting should be off now. 
  1659.  
  1660. echo "1"; 
  1661. exit; 
  1662.  
  1663. /** 
  1664. * Handle connectivity test retry approved by the user. 
  1665. * @author Vova Feldman (@svovaf) 
  1666. * @since 1.1.7.4 
  1667. */ 
  1668. function _retry_connectivity_test() { 
  1669. $this->_admin_notices->remove_sticky( 'failed_connect_api_first' ); 
  1670.  
  1671. $pong = $this->ping(); 
  1672.  
  1673. $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong ); 
  1674.  
  1675. if ( $is_connected ) { 
  1676. $this->store_connectivity_info( $pong, $is_connected ); 
  1677.  
  1678. echo $this->get_after_plugin_activation_redirect_url(); 
  1679. } else { 
  1680. // Add connectivity issue message after 2nd failed attempt. 
  1681. $this->_add_connectivity_issue_message( $pong, false ); 
  1682.  
  1683. echo "1"; 
  1684.  
  1685. exit; 
  1686.  
  1687. static function _add_firewall_issues_javascript() { 
  1688. $params = array(); 
  1689. fs_require_once_template( 'firewall-issues-js.php', $params ); 
  1690.  
  1691. #endregion 
  1692.  
  1693. #---------------------------------------------------------------------------------- 
  1694. #region Email 
  1695. #---------------------------------------------------------------------------------- 
  1696.  
  1697. /** 
  1698. * Generates and sends an HTML email with customizable sections. 
  1699. * @author Leo Fajardo (@leorw) 
  1700. * @since 1.1.2 
  1701. * @param string $to_address 
  1702. * @param string $subject 
  1703. * @param array $sections 
  1704. * @param array $headers 
  1705. * @return bool Whether the email contents were sent successfully. 
  1706. */ 
  1707. private function send_email( 
  1708. $to_address,  
  1709. $subject,  
  1710. $sections = array(),  
  1711. $headers = array() 
  1712. ) { 
  1713. $default_sections = $this->get_email_sections(); 
  1714.  
  1715. // Insert new sections or replace the default email sections. 
  1716. if ( is_array( $sections ) && ! empty( $sections ) ) { 
  1717. foreach ( $sections as $section_id => $custom_section ) { 
  1718. if ( ! isset( $default_sections[ $section_id ] ) ) { 
  1719. // If the section does not exist, add it. 
  1720. $default_sections[ $section_id ] = $custom_section; 
  1721. } else { 
  1722. // If the section already exists, override it. 
  1723. $current_section = $default_sections[ $section_id ]; 
  1724.  
  1725. // Replace the current section's title if a custom section title exists. 
  1726. if ( isset( $custom_section['title'] ) ) { 
  1727. $current_section['title'] = $custom_section['title']; 
  1728.  
  1729. // Insert new rows under the current section or replace the default rows. 
  1730. if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) { 
  1731. foreach ( $custom_section['rows'] as $row_id => $row ) { 
  1732. $current_section['rows'][ $row_id ] = $row; 
  1733.  
  1734. $default_sections[ $section_id ] = $current_section; 
  1735.  
  1736. $vars = array( 'sections' => $default_sections ); 
  1737. $message = fs_get_template( 'email.php', $vars ); 
  1738.  
  1739. // Set the type of email to HTML. 
  1740. $headers[] = 'Content-type: text/html'; 
  1741.  
  1742. $header_string = implode( "\r\n", $headers ); 
  1743.  
  1744. return wp_mail( 
  1745. $to_address,  
  1746. $subject,  
  1747. $message,  
  1748. $header_string 
  1749. ); 
  1750.  
  1751. /** 
  1752. * Generates the data for the sections of the email content. 
  1753. * @author Leo Fajardo (@leorw) 
  1754. * @since 1.1.2 
  1755. * @return array 
  1756. */ 
  1757. private function get_email_sections() { 
  1758. // Retrieve the current user's information so that we can get the user's email, first name, and last name below. 
  1759. $current_user = self::_get_current_wp_user(); 
  1760.  
  1761. // Retrieve the cURL version information so that we can get the version number below. 
  1762. $curl_version_information = curl_version(); 
  1763.  
  1764. $active_plugin = self::get_active_plugins(); 
  1765.  
  1766. // Generate the list of active plugins separated by new line.  
  1767. $active_plugin_string = ''; 
  1768. foreach ( $active_plugin as $plugin ) { 
  1769. $active_plugin_string .= sprintf( 
  1770. '<a href="%s">%s</a> [v%s]<br>',  
  1771. $plugin['PluginURI'],  
  1772. $plugin['Name'],  
  1773. $plugin['Version'] 
  1774. ); 
  1775.  
  1776. $server_ip = WP_FS__REMOTE_ADDR; 
  1777.  
  1778. // Generate the default email sections. 
  1779. $sections = array( 
  1780. 'sdk' => array( 
  1781. 'title' => 'SDK',  
  1782. 'rows' => array( 
  1783. 'fs_version' => array( 'FS Version', $this->version ),  
  1784. 'curl_version' => array( 'cURL Version', $curl_version_information['version'] ) 
  1785. ),  
  1786. 'plugin' => array( 
  1787. 'title' => 'Plugin',  
  1788. 'rows' => array( 
  1789. 'name' => array( 'Name', $this->get_plugin_name() ),  
  1790. 'version' => array( 'Version', $this->get_plugin_version() ) 
  1791. ),  
  1792. 'site' => array( 
  1793. 'title' => 'Site',  
  1794. 'rows' => array( 
  1795. 'unique_id' => array( 'Address', $this->get_anonymous_id() ),  
  1796. 'address' => array( 'Address', site_url() ),  
  1797. 'host' => array( 
  1798. 'HTTP_HOST',  
  1799. ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' ) 
  1800. ),  
  1801. 'server_addr' => array( 
  1802. 'SERVER_ADDR',  
  1803. '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>' 
  1804. ),  
  1805. 'user' => array( 
  1806. 'title' => 'User',  
  1807. 'rows' => array( 
  1808. 'email' => array( 'Email', $current_user->user_email ),  
  1809. 'first' => array( 'First', $current_user->user_firstname ),  
  1810. 'last' => array( 'Last', $current_user->user_lastname ) 
  1811. ),  
  1812. 'plugins' => array( 
  1813. 'title' => 'Plugins',  
  1814. 'rows' => array( 
  1815. 'active_plugins' => array( 'Active Plugins', $active_plugin_string ) 
  1816. ),  
  1817. ); 
  1818.  
  1819. // Allow the sections to be modified by other code. 
  1820. $sections = $this->apply_filters( 'email_template_sections', $sections ); 
  1821.  
  1822. return $sections; 
  1823.  
  1824. #endregion 
  1825.  
  1826. #---------------------------------------------------------------------------------- 
  1827. #region Initialization 
  1828. #---------------------------------------------------------------------------------- 
  1829.  
  1830. /** 
  1831. * Init plugin's Freemius instance. 
  1832. * @author Vova Feldman (@svovaf) 
  1833. * @since 1.0.1 
  1834. * @param number $id 
  1835. * @param string $public_key 
  1836. * @param bool $is_live 
  1837. * @param bool $is_premium 
  1838. */ 
  1839. function init( $id, $public_key, $is_live = true, $is_premium = true ) { 
  1840. $this->_logger->entrance(); 
  1841.  
  1842. $this->dynamic_init( array( 
  1843. 'id' => $id,  
  1844. 'public_key' => $public_key,  
  1845. 'is_live' => $is_live,  
  1846. 'is_premium' => $is_premium,  
  1847. ) ); 
  1848.  
  1849. /** 
  1850. * Dynamic initiator, originally created to support initiation 
  1851. * with parent_id for add-ons. 
  1852. * @author Vova Feldman (@svovaf) 
  1853. * @since 1.0.6 
  1854. * @param array $plugin_info 
  1855. * @throws Freemius_Exception 
  1856. */ 
  1857. function dynamic_init( array $plugin_info ) { 
  1858. $this->_logger->entrance(); 
  1859.  
  1860. $this->parse_settings( $plugin_info ); 
  1861.  
  1862. if ( $this->should_stop_execution() ) { 
  1863. return; 
  1864.  
  1865. if ( ! $this->is_registered() ) { 
  1866. if ( $this->is_anonymous() ) { 
  1867. // If user skipped, no need to test connectivity. 
  1868. $this->_has_api_connection = true; 
  1869. $this->_is_on = true; 
  1870. } else { 
  1871. if ( ! $this->has_api_connectivity() ) { 
  1872. if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) || 
  1873. $this->_admin_notices->has_sticky( 'failed_connect_api' ) 
  1874. ) { 
  1875. if ( ! $this->_enable_anonymous || $this->is_premium() ) { 
  1876. // If anonymous mode is disabled, add firewall admin-notice message. 
  1877. add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) ); 
  1878.  
  1879. $this->add_ajax_action( 'resolve_firewall_issues', array( 
  1880. &$this,  
  1881. '_email_about_firewall_issue' 
  1882. ) ); 
  1883.  
  1884. $this->add_ajax_action( 'retry_connectivity_test', array( 
  1885. &$this,  
  1886. '_retry_connectivity_test' 
  1887. ) ); 
  1888.  
  1889. return; 
  1890. } else { 
  1891. $this->_admin_notices->remove_sticky( array( 
  1892. 'failed_connect_api_first',  
  1893. 'failed_connect_api',  
  1894. ) ); 
  1895.  
  1896. if ( $this->_anonymous_mode ) { 
  1897. // Simulate anonymous mode. 
  1898. $this->_is_anonymous = true; 
  1899.  
  1900. // Check if Freemius is on for the current plugin. 
  1901. // This MUST be executed after all the plugin variables has been loaded. 
  1902. if ( ! $this->is_on() ) { 
  1903. return; 
  1904.  
  1905. if ( $this->has_api_connectivity() ) { 
  1906. if ( $this->is_cron() ) { 
  1907. $this->hook_callback_to_sync_cron(); 
  1908. } else if ( $this->is_user_in_admin() ) { 
  1909. /** 
  1910. * Schedule daily data sync cron if: 
  1911. * 1. User opted-in (for tracking). 
  1912. * 2. If skipped, but later upgraded (opted-in via upgrade). 
  1913. * @author Vova Feldman (@svovaf) 
  1914. * @since 1.1.7.3 
  1915. */ 
  1916. if ( $this->is_registered() ) { 
  1917. if ( ! $this->is_sync_cron_on() && $this->is_tracking_allowed() ) { 
  1918. $this->schedule_sync_cron(); 
  1919.  
  1920. /** 
  1921. * Check if requested for manual blocking background sync. 
  1922. */ 
  1923. if ( fs_request_has( 'background_sync' ) ) { 
  1924. $this->run_manual_sync(); 
  1925.  
  1926. if ( $this->is_registered() ) { 
  1927. $this->hook_callback_to_install_sync(); 
  1928.  
  1929. if ( $this->is_addon() ) { 
  1930. if ( $this->is_parent_plugin_installed() ) { 
  1931. // Link to parent FS. 
  1932. $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id ); 
  1933.  
  1934. // Get parent plugin reference. 
  1935. $this->_parent_plugin = $this->_parent->get_plugin(); 
  1936.  
  1937. if ( $this->is_user_in_admin() ) { 
  1938. if ( $this->is_plugins_page() ) { 
  1939. $this->hook_plugin_action_links(); 
  1940.  
  1941. if ( $this->is_addon() ) { 
  1942. if ( ! $this->is_parent_plugin_installed() ) { 
  1943. $parent_name = $this->get_option( $plugin_info, 'parent_name', null ); 
  1944.  
  1945. if ( isset( $plugin_info['parent'] ) ) { 
  1946. $parent_name = $this->get_option( $plugin_info['parent'], 'name', null ); 
  1947.  
  1948. $this->_admin_notices->add( 
  1949. ( ! empty( $parent_name ) ? 
  1950. sprintf( __fs( 'addon-x-cannot-run-without-y', $this->_slug ), $this->get_plugin_name(), $parent_name ) : 
  1951. sprintf( __fs( 'addon-x-cannot-run-without-parent', $this->_slug ), $this->get_plugin_name() ) 
  1952. ),  
  1953. __fs( 'oops', $this->_slug ) . '...',  
  1954. 'error' 
  1955. ); 
  1956.  
  1957. return; 
  1958. } else { 
  1959. if ( $this->_parent->is_registered() && ! $this->is_registered() ) { 
  1960. // If parent plugin activated, automatically install add-on for the user. 
  1961. $this->_activate_addon_account( $this->_parent ); 
  1962.  
  1963. // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic. 
  1964. if ( $this->is_premium() ) { 
  1965. // Remove add-on download admin-notice. 
  1966. $this->_parent->_admin_notices->remove_sticky( array( 
  1967. 'addon_plan_upgraded_' . $this->_slug,  
  1968. 'no_addon_license_' . $this->_slug,  
  1969. ) ); 
  1970.  
  1971. $this->deactivate_premium_only_addon_without_license(); 
  1972. } else { 
  1973. add_action( 'admin_init', array( &$this, '_admin_init_action' ) ); 
  1974.  
  1975. if ( $this->has_addons() && 
  1976. 'plugin-information' === fs_request_get( 'tab', false ) && 
  1977. $this->get_id() == fs_request_get( 'parent_plugin_id', false ) 
  1978. ) { 
  1979. require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php'; 
  1980.  
  1981. new FS_Plugin_Info_Dialog( $this ); 
  1982.  
  1983. if ( $this->is_premium() ) { 
  1984. new FS_Plugin_Updater( $this ); 
  1985.  
  1986. // if ( $this->is_registered() || 
  1987. // $this->is_anonymous() || 
  1988. // $this->is_pending_activation() 
  1989. // ) { 
  1990. // $this->_init_admin(); 
  1991. // } 
  1992.  
  1993. $this->do_action( 'initiated' ); 
  1994.  
  1995. if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) { 
  1996. if ( isset( $this->_storage->prev_is_premium ) ) { 
  1997. $this->apply_filters( 
  1998. 'after_code_type_change',  
  1999. // New code type. 
  2000. $this->_plugin->is_premium 
  2001. ); 
  2002. } else { 
  2003. // Set for code type for the first time. 
  2004. $this->_storage->prev_is_premium = $this->_plugin->is_premium; 
  2005.  
  2006. if ( ! $this->is_addon() ) { 
  2007. if ( $this->is_registered() ) { 
  2008. // Fix for upgrade from versions < 1.0.9. 
  2009. if ( ! isset( $this->_storage->activation_timestamp ) ) { 
  2010. $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME; 
  2011.  
  2012. $this->do_action( 'after_init_plugin_registered' ); 
  2013. } else if ( $this->is_anonymous() ) { 
  2014. $this->do_action( 'after_init_plugin_anonymous' ); 
  2015. } else if ( $this->is_pending_activation() ) { 
  2016. $this->do_action( 'after_init_plugin_pending_activations' ); 
  2017. } else { 
  2018. if ( $this->is_registered() ) { 
  2019. $this->do_action( 'after_init_addon_registered' ); 
  2020. } else if ( $this->is_anonymous() ) { 
  2021. $this->do_action( 'after_init_addon_anonymous' ); 
  2022. } else if ( $this->is_pending_activation() ) { 
  2023. $this->do_action( 'after_init_addon_pending_activations' ); 
  2024.  
  2025. /** 
  2026. * @author Leo Fajardo (@leorw) 
  2027. * @since 1.2.1.5 
  2028. */ 
  2029. function _stop_tracking_callback() { 
  2030. $result = $this->stop_tracking(); 
  2031.  
  2032. if ( true === $result ) { 
  2033. $this->shoot_ajax_success(); 
  2034.  
  2035. $this->_logger->api_error( $result ); 
  2036.  
  2037. $this->shoot_ajax_failure( 
  2038. __fs( 'unexpected-api-error', $this->_slug ) . 
  2039. ( $this->is_api_error( $result ) && isset( $result->error ) ? 
  2040. $result->error->message : 
  2041. var_export( $result, true ) ) 
  2042. ); 
  2043.  
  2044. /** 
  2045. * @author Leo Fajardo (@leorw) 
  2046. * @since 1.2.1.5 
  2047. */ 
  2048. function _allow_tracking_callback() { 
  2049. $result = $this->allow_tracking(); 
  2050.  
  2051. if ( true === $result ) { 
  2052. $this->shoot_ajax_success(); 
  2053.  
  2054. $this->_logger->api_error( $result ); 
  2055.  
  2056. $this->shoot_ajax_failure( 
  2057. __fs( 'unexpected-api-error', $this->_slug ) . 
  2058. ( $this->is_api_error( $result ) && isset( $result->error ) ? 
  2059. $result->error->message : 
  2060. var_export( $result, true ) ) 
  2061. ); 
  2062.  
  2063. /** 
  2064. * Opt-out from usage tracking. 
  2065. * Note: This will not delete the account information but will stop all tracking. 
  2066. * Returns: 
  2067. * 1. FALSE - If the user never opted-in. 
  2068. * 2. TRUE - If successfully opted-out. 
  2069. * 3. object - API result on failure. 
  2070. * @author Leo Fajardo (@leorw) 
  2071. * @since 1.2.1.5 
  2072. * @return bool|object 
  2073. */ 
  2074. function stop_tracking() { 
  2075. $this->_logger->entrance(); 
  2076.  
  2077. if ( ! $this->is_registered() ) { 
  2078. // User never opted-in. 
  2079. return false; 
  2080.  
  2081. if ( $this->is_tracking_prohibited() ) { 
  2082. // Already disconnected. 
  2083. return true; 
  2084.  
  2085. // Send update to FS. 
  2086. $result = $this->get_api_site_scope()->call( '/?fields=is_disconnected', 'put', array( 
  2087. 'is_disconnected' => true 
  2088. ) ); 
  2089.  
  2090. if ( ! $this->is_api_result_entity( $result ) || 
  2091. ! isset( $result->is_disconnected ) || 
  2092. ! $result->is_disconnected 
  2093. ) { 
  2094. $this->_logger->api_error( $result ); 
  2095.  
  2096. return $result; 
  2097.  
  2098. $this->_site->is_disconnected = $result->is_disconnected; 
  2099. $this->_store_site(); 
  2100.  
  2101. $this->clear_sync_cron(); 
  2102.  
  2103. // Successfully disconnected. 
  2104. return true; 
  2105.  
  2106. /** 
  2107. * Opt-in back into usage tracking. 
  2108. * Note: This will only work if the user opted-in previously. 
  2109. * Returns: 
  2110. * 1. FALSE - If the user never opted-in. 
  2111. * 2. TRUE - If successfully opted-in back to usage tracking. 
  2112. * 3. object - API result on failure. 
  2113. * @author Leo Fajardo (@leorw) 
  2114. * @since 1.2.1.5 
  2115. * @return bool|object 
  2116. */ 
  2117. function allow_tracking() { 
  2118. $this->_logger->entrance(); 
  2119.  
  2120. if ( ! $this->is_registered() ) { 
  2121. // User never opted-in. 
  2122. return false; 
  2123.  
  2124. if ( $this->is_tracking_allowed() ) { 
  2125. // Tracking already allowed. 
  2126. return true; 
  2127.  
  2128. $result = $this->get_api_site_scope()->call( '/?is_disconnected', 'put', array( 
  2129. 'is_disconnected' => false 
  2130. ) ); 
  2131.  
  2132. if ( ! $this->is_api_result_entity( $result ) || 
  2133. ! isset( $result->is_disconnected ) || 
  2134. $result->is_disconnected 
  2135. ) { 
  2136. $this->_logger->api_error( $result ); 
  2137.  
  2138. return $result; 
  2139.  
  2140. $this->_site->is_disconnected = $result->is_disconnected; 
  2141. $this->_store_site(); 
  2142.  
  2143. $this->schedule_sync_cron(); 
  2144.  
  2145. // Successfully reconnected. 
  2146. return true; 
  2147.  
  2148. /** 
  2149. * If user opted-in and later disabled usage-tracking,  
  2150. * re-allow tracking for licensing and updates. 
  2151. * @author Leo Fajardo (@leorw) 
  2152. * @since 1.2.1.5 
  2153. */ 
  2154. private function reconnect_locally() { 
  2155. $this->_logger->entrance(); 
  2156.  
  2157. if ( $this->is_tracking_prohibited() && 
  2158. $this->is_registered() 
  2159. ) { 
  2160. $this->_site->is_disconnected = false; 
  2161. $this->_store_site(); 
  2162.  
  2163. /** 
  2164. * Parse plugin's settings (as defined by the plugin dev). 
  2165. * @author Vova Feldman (@svovaf) 
  2166. * @since 1.1.7.3 
  2167. * @param array $plugin_info 
  2168. * @throws \Freemius_Exception 
  2169. */ 
  2170. private function parse_settings( &$plugin_info ) { 
  2171. $this->_logger->entrance(); 
  2172.  
  2173. $id = $this->get_numeric_option( $plugin_info, 'id', false ); 
  2174. $public_key = $this->get_option( $plugin_info, 'public_key', false ); 
  2175. $secret_key = $this->get_option( $plugin_info, 'secret_key', null ); 
  2176. $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null ); 
  2177. $parent_name = $this->get_option( $plugin_info, 'parent_name', null ); 
  2178.  
  2179. /** 
  2180. * @author Vova Feldman (@svovaf) 
  2181. * @since 1.1.9 Try to pull secret key from external config. 
  2182. */ 
  2183. if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) { 
  2184. $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" ); 
  2185.  
  2186. if ( isset( $plugin_info['parent'] ) ) { 
  2187. $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null ); 
  2188. // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null ); 
  2189. // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null ); 
  2190. // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null ); 
  2191.  
  2192. if ( false === $id ) { 
  2193. throw new Freemius_Exception( 'Plugin id parameter is not set.' ); 
  2194. if ( false === $public_key ) { 
  2195. throw new Freemius_Exception( 'Plugin public_key parameter is not set.' ); 
  2196.  
  2197. $plugin = ( $this->_plugin instanceof FS_Plugin ) ? 
  2198. $this->_plugin : 
  2199. new FS_Plugin(); 
  2200.  
  2201. $plugin->update( array( 
  2202. 'id' => $id,  
  2203. 'public_key' => $public_key,  
  2204. 'slug' => $this->_slug,  
  2205. 'parent_plugin_id' => $parent_id,  
  2206. 'version' => $this->get_plugin_version(),  
  2207. 'title' => $this->get_plugin_name(),  
  2208. 'file' => $this->_plugin_basename,  
  2209. 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),  
  2210. 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),  
  2211. // 'secret_key' => $secret_key,  
  2212. ) ); 
  2213.  
  2214. if ( $plugin->is_updated() ) { 
  2215. // Update plugin details. 
  2216. $this->_plugin = FS_Plugin_Manager::instance( $this->_slug )->store( $plugin ); 
  2217. // Set the secret key after storing the plugin, we don't want to store the key in the storage. 
  2218. $this->_plugin->secret_key = $secret_key; 
  2219.  
  2220. if ( ! isset( $plugin_info['menu'] ) ) { 
  2221. // Back compatibility to 1.1.2 
  2222. $plugin_info['menu'] = array( 
  2223. 'slug' => isset( $plugin_info['menu_slug'] ) ? 
  2224. $plugin_info['menu_slug'] : 
  2225. $this->_slug 
  2226. ); 
  2227.  
  2228. $this->_menu = FS_Admin_Menu_Manager::instance( $this->_slug ); 
  2229. $this->_menu->init( $plugin_info['menu'], $this->is_addon() ); 
  2230.  
  2231. $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false ); 
  2232. $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true ); 
  2233. $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true ); 
  2234. $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false ); 
  2235. if ( $this->_is_premium_only ) { 
  2236. // If premium only plugin, disable anonymous mode. 
  2237. $this->_enable_anonymous = false; 
  2238. $this->_anonymous_mode = false; 
  2239. } else { 
  2240. $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true ); 
  2241. $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false ); 
  2242. $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() ); 
  2243.  
  2244. if ( ! empty( $plugin_info['trial'] ) ) { 
  2245. $this->_trial_days = $this->get_numeric_option( 
  2246. $plugin_info['trial'],  
  2247. 'days',  
  2248. // Default to 0 - trial without days specification. 
  2249. ); 
  2250.  
  2251. $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false ); 
  2252.  
  2253. /** 
  2254. * @param string[] $options 
  2255. * @param string $key 
  2256. * @param mixed $default 
  2257. * @return bool 
  2258. */ 
  2259. private function get_option( &$options, $key, $default = false ) { 
  2260. return ! empty( $options[ $key ] ) ? $options[ $key ] : $default; 
  2261.  
  2262. private function get_bool_option( &$options, $key, $default = false ) { 
  2263. return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default; 
  2264.  
  2265. private function get_numeric_option( &$options, $key, $default = false ) { 
  2266. return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default; 
  2267.  
  2268. /** 
  2269. * Gate keeper. 
  2270. * @author Vova Feldman (@svovaf) 
  2271. * @since 1.1.7.3 
  2272. * @return bool 
  2273. */ 
  2274. private function should_stop_execution() { 
  2275. if ( empty( $this->_storage->was_plugin_loaded ) ) { 
  2276. /** 
  2277. * Don't execute Freemius until plugin was fully loaded at least once,  
  2278. * to give the opportunity for the activation hook to run before pinging 
  2279. * the API for connectivity test. This logic is relevant for the 
  2280. * identification of new plugin install vs. plugin update. 
  2281. * @author Vova Feldman (@svovaf) 
  2282. * @since 1.1.9 
  2283. */ 
  2284. return true; 
  2285.  
  2286. if ( $this->is_activation_mode() ) { 
  2287. if ( ! is_admin() ) { 
  2288. /** 
  2289. * If in activation mode, don't execute Freemius outside of the 
  2290. * admin dashboard. 
  2291. * @author Vova Feldman (@svovaf) 
  2292. * @since 1.1.7.3 
  2293. */ 
  2294. return true; 
  2295.  
  2296. if ( ! WP_FS__IS_HTTP_REQUEST ) { 
  2297. /** 
  2298. * If in activation and executed without HTTP context (e.g. CLI, Cronjob),  
  2299. * then don't start Freemius. 
  2300. * @author Vova Feldman (@svovaf) 
  2301. * @since 1.1.6.3 
  2302. * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli 
  2303. */ 
  2304. return true; 
  2305.  
  2306. if ( $this->is_cron() ) { 
  2307. /** 
  2308. * If in activation mode, don't execute Freemius during wp crons 
  2309. * (wp crons have HTTP context - called as HTTP request). 
  2310. * @author Vova Feldman (@svovaf) 
  2311. * @since 1.1.7.3 
  2312. */ 
  2313. return true; 
  2314.  
  2315. if ( $this->is_ajax() && 
  2316. ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) && 
  2317. ! $this->_admin_notices->has_sticky( 'failed_connect_api' ) 
  2318. ) { 
  2319. /** 
  2320. * During activation, if running in AJAX mode, unless there's a sticky 
  2321. * connectivity issue notice, don't run Freemius. 
  2322. * @author Vova Feldman (@svovaf) 
  2323. * @since 1.1.7.3 
  2324. */ 
  2325. return true; 
  2326.  
  2327. return false; 
  2328.  
  2329. /** 
  2330. * Triggered after code type has changed. 
  2331. * @author Vova Feldman (@svovaf) 
  2332. * @since 1.1.9.1 
  2333. */ 
  2334. function _after_code_type_change() { 
  2335. $this->_logger->entrance(); 
  2336.  
  2337. add_action( is_admin() ? 'admin_init' : 'init', array( 
  2338. &$this,  
  2339. '_plugin_code_type_changed' 
  2340. ) ); 
  2341.  
  2342. /** 
  2343. * Handles plugin's code type change (free <--> premium). 
  2344. * @author Vova Feldman (@svovaf) 
  2345. * @since 1.0.9 
  2346. */ 
  2347. function _plugin_code_type_changed() { 
  2348. $this->_logger->entrance(); 
  2349.  
  2350. if ( $this->is_premium() ) { 
  2351. $this->reconnect_locally(); 
  2352.  
  2353. // Activated premium code. 
  2354. $this->do_action( 'after_premium_version_activation' ); 
  2355.  
  2356. // Remove all sticky messages related to download of the premium version. 
  2357. $this->_admin_notices->remove_sticky( array( 
  2358. 'trial_started',  
  2359. 'plan_upgraded',  
  2360. 'plan_changed',  
  2361. 'license_activated',  
  2362. ) ); 
  2363.  
  2364. $this->_admin_notices->add_sticky( 
  2365. __fs( 'premium-activated-message', $this->_slug ),  
  2366. 'premium_activated',  
  2367. __fs( 'woot', $this->_slug ) . '!' 
  2368. ); 
  2369. } else { 
  2370. // Remove sticky message related to premium code activation. 
  2371. $this->_admin_notices->remove_sticky( 'premium_activated' ); 
  2372.  
  2373. // Activated free code (after had the premium before). 
  2374. $this->do_action( 'after_free_version_reactivation' ); 
  2375.  
  2376. if ( $this->is_paying() && ! $this->is_premium() ) { 
  2377. $this->_admin_notices->add_sticky( 
  2378. sprintf( 
  2379. __fs( 'you-have-x-license', $this->_slug ),  
  2380. $this->_site->plan->title 
  2381. ) . $this->get_complete_upgrade_instructions(),  
  2382. 'plan_upgraded',  
  2383. __fs( 'yee-haw', $this->_slug ) . '!' 
  2384. ); 
  2385.  
  2386. // Schedule code type changes event. 
  2387. $this->schedule_install_sync(); 
  2388.  
  2389. /** 
  2390. * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid 
  2391. * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version 
  2392. * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been 
  2393. * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since 
  2394. * the main file of the "free" version will be loaded first before calling the hooked callback. Since the 
  2395. * free and premium versions are almost identical (same class or have same functions), a fatal error like 
  2396. * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur. 
  2397. */ 
  2398. $this->unregister_uninstall_hook(); 
  2399.  
  2400. $this->clear_module_main_file_cache(); 
  2401.  
  2402. // Update is_premium of latest version. 
  2403. $this->_storage->prev_is_premium = $this->_plugin->is_premium; 
  2404.  
  2405. #endregion 
  2406.  
  2407. #---------------------------------------------------------------------------------- 
  2408. #region Add-ons 
  2409. #---------------------------------------------------------------------------------- 
  2410.  
  2411. /** 
  2412. * Check if add-on installed and activated on site. 
  2413. * @author Vova Feldman (@svovaf) 
  2414. * @since 1.0.6 
  2415. * @param string|number $slug_or_id 
  2416. * @return bool 
  2417. */ 
  2418. function is_addon_activated( $slug_or_id ) { 
  2419. return self::has_instance( $slug_or_id ); 
  2420.  
  2421. /** 
  2422. * Check if add-on was connected to install 
  2423. * @author Vova Feldman (@svovaf) 
  2424. * @since 1.1.7 
  2425. * @param string $slug 
  2426. * @return bool 
  2427. */ 
  2428. function is_addon_connected( $slug ) { 
  2429. $sites = self::get_all_sites(); 
  2430.  
  2431. if ( ! isset( $sites[ $slug ] ) ) { 
  2432. return false; 
  2433.  
  2434. $site = $sites[ $slug ]; 
  2435.  
  2436. $plugin = FS_Plugin_Manager::instance( $slug )->get(); 
  2437.  
  2438. if ( $plugin->parent_plugin_id != $this->_plugin->id ) { 
  2439. // The given slug do NOT belong to any of the plugin's add-ons. 
  2440. return false; 
  2441.  
  2442. return ( is_object( $site ) && 
  2443. is_numeric( $site->id ) && 
  2444. is_numeric( $site->user_id ) && 
  2445. is_object( $site->plan ) 
  2446. ); 
  2447.  
  2448. /** 
  2449. * Determines if add-on installed. 
  2450. * NOTE: This is a heuristic and only works if the folder/file named as the slug. 
  2451. * @author Vova Feldman (@svovaf) 
  2452. * @since 1.0.6 
  2453. * @param string $slug 
  2454. * @return bool 
  2455. */ 
  2456. function is_addon_installed( $slug ) { 
  2457. return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $slug ) ) ); 
  2458.  
  2459. /** 
  2460. * Get add-on basename. 
  2461. * @author Vova Feldman (@svovaf) 
  2462. * @since 1.0.6 
  2463. * @param string $slug 
  2464. * @return string 
  2465. */ 
  2466. function get_addon_basename( $slug ) { 
  2467. if ( $this->is_addon_activated( $slug ) ) { 
  2468. self::instance( $slug )->get_plugin_basename(); 
  2469.  
  2470. $premium_basename = $slug . '-premium/' . $slug . '.php'; 
  2471.  
  2472. if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) { 
  2473. return $premium_basename; 
  2474.  
  2475. $free_basename = $slug . '/' . $slug . '.php'; 
  2476.  
  2477. return $free_basename; 
  2478.  
  2479. /** 
  2480. * Get installed add-ons instances. 
  2481. * @author Vova Feldman (@svovaf) 
  2482. * @since 1.0.6 
  2483. * @return Freemius[] 
  2484. */ 
  2485. function get_installed_addons() { 
  2486. $installed_addons = array(); 
  2487. foreach ( self::$_instances as $slug => $instance ) { 
  2488. if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) { 
  2489. if ( $this->_plugin->id == $instance->_parent_plugin->id ) { 
  2490. $installed_addons[] = $instance; 
  2491.  
  2492. return $installed_addons; 
  2493.  
  2494. /** 
  2495. * Check if any add-ons of the plugin are installed. 
  2496. * @author Leo Fajardo (@leorw) 
  2497. * @since 1.1.1 
  2498. * @return bool 
  2499. */ 
  2500. function has_installed_addons() { 
  2501. if ( ! $this->has_addons() ) { 
  2502. return false; 
  2503.  
  2504. foreach ( self::$_instances as $slug => $instance ) { 
  2505. if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) { 
  2506. if ( $this->_plugin->id == $instance->_parent_plugin->id ) { 
  2507. return true; 
  2508.  
  2509. return false; 
  2510.  
  2511. /** 
  2512. * Tell Freemius that the current plugin is an add-on. 
  2513. * @author Vova Feldman (@svovaf) 
  2514. * @since 1.0.6 
  2515. * @param number $parent_plugin_id The parent plugin ID 
  2516. */ 
  2517. function init_addon( $parent_plugin_id ) { 
  2518. $this->_plugin->parent_plugin_id = $parent_plugin_id; 
  2519.  
  2520. /** 
  2521. * @author Vova Feldman (@svovaf) 
  2522. * @since 1.0.6 
  2523. * @return bool 
  2524. */ 
  2525. function is_addon() { 
  2526. return isset( $this->_plugin->parent_plugin_id ) && is_numeric( $this->_plugin->parent_plugin_id ); 
  2527.  
  2528. /** 
  2529. * Deactivate add-on if it's premium only and the user does't have a valid license. 
  2530. * @param bool $is_after_trial_cancel 
  2531. * @return bool If add-on was deactivated. 
  2532. */ 
  2533. private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) { 
  2534. if ( ! $this->has_free_plan() && 
  2535. ! $this->has_features_enabled_license() && 
  2536. ! $this->_has_premium_license() 
  2537. ) { 
  2538. // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons). 
  2539. // if (empty($this->_storage->activation_timestamp) || 
  2540. // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30 
  2541. // ) { 
  2542. /** 
  2543. * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation. 
  2544. * Retry syncing the user add-on licenses. 
  2545. */ 
  2546. // Sync licenses. 
  2547. $this->_sync_licenses(); 
  2548. // } 
  2549.  
  2550. // Try to activate premium license. 
  2551. $this->_activate_license( true ); 
  2552.  
  2553. if ( ! $this->has_free_plan() && 
  2554. ! $this->has_features_enabled_license() && 
  2555. ! $this->_has_premium_license() 
  2556. ) { 
  2557. // @todo Check if deactivate plugins also call the deactivation hook. 
  2558.  
  2559. deactivate_plugins( array( $this->_plugin_basename ), true ); 
  2560.  
  2561. $this->_parent->_admin_notices->add_sticky( 
  2562. sprintf( 
  2563. __fs( ( $is_after_trial_cancel ? 
  2564. 'addon-trial-cancelled-message' : 
  2565. 'addon-no-license-message' ),  
  2566. $this->_parent->_slug 
  2567. ),  
  2568. '<b>' . $this->_plugin->title . '</b>' 
  2569. ) . ' ' . sprintf( 
  2570. '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s  ➜</a>',  
  2571. $this->_parent->addon_url( $this->_slug ),  
  2572. esc_attr( sprintf( __fs( 'more-information-about-x', $this->_parent->_slug ), $this->_plugin->title ) ),  
  2573. __fs( 'purchase-license', $this->_parent->_slug ) 
  2574. ),  
  2575. 'no_addon_license_' . $this->_slug,  
  2576. ( $is_after_trial_cancel ? '' : __fs( 'oops', $this->_parent->_slug ) . '...' ),  
  2577. ( $is_after_trial_cancel ? 'success' : 'error' ) 
  2578. ); 
  2579.  
  2580. return true; 
  2581.  
  2582. return false; 
  2583.  
  2584. #endregion 
  2585.  
  2586. #---------------------------------------------------------------------------------- 
  2587. #region Sandbox 
  2588. #---------------------------------------------------------------------------------- 
  2589.  
  2590. /** 
  2591. * Set Freemius into sandbox mode for debugging. 
  2592. * @author Vova Feldman (@svovaf) 
  2593. * @since 1.0.4 
  2594. * @param string $secret_key 
  2595. */ 
  2596. function init_sandbox( $secret_key ) { 
  2597. $this->_plugin->secret_key = $secret_key; 
  2598.  
  2599. // Update plugin details. 
  2600. FS_Plugin_Manager::instance( $this->_slug )->update( $this->_plugin, true ); 
  2601.  
  2602. /** 
  2603. * Check if running payments in sandbox mode. 
  2604. * @author Vova Feldman (@svovaf) 
  2605. * @since 1.0.4 
  2606. * @return bool 
  2607. */ 
  2608. function is_payments_sandbox() { 
  2609. return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key ); 
  2610.  
  2611. #endregion 
  2612.  
  2613. /** 
  2614. * Check if running test vs. live plugin. 
  2615. * @author Vova Feldman (@svovaf) 
  2616. * @since 1.0.5 
  2617. * @return bool 
  2618. */ 
  2619. function is_live() { 
  2620. return $this->_plugin->is_live; 
  2621.  
  2622. /** 
  2623. * Check if the user skipped connecting the account with Freemius. 
  2624. * @author Vova Feldman (@svovaf) 
  2625. * @since 1.0.7 
  2626. * @return bool 
  2627. */ 
  2628. function is_anonymous() { 
  2629. if ( ! isset( $this->_is_anonymous ) ) { 
  2630. if ( ! isset( $this->_storage->is_anonymous ) ) { 
  2631. // Not skipped. 
  2632. $this->_is_anonymous = false; 
  2633. } else if ( is_bool( $this->_storage->is_anonymous ) ) { 
  2634. // For back compatibility, since the variable was boolean before. 
  2635. $this->_is_anonymous = $this->_storage->is_anonymous; 
  2636.  
  2637. // Upgrade stored data format to 1.1.3 format. 
  2638. $this->set_anonymous_mode( $this->_storage->is_anonymous ); 
  2639. } else { 
  2640. // Version 1.1.3 and later. 
  2641. $this->_is_anonymous = $this->_storage->is_anonymous['is']; 
  2642.  
  2643. return $this->_is_anonymous; 
  2644.  
  2645. /** 
  2646. * Check if user connected his account and install pending email activation. 
  2647. * @author Vova Feldman (@svovaf) 
  2648. * @since 1.0.7 
  2649. * @return bool 
  2650. */ 
  2651. function is_pending_activation() { 
  2652. return $this->_storage->get( 'is_pending_activation', false ); 
  2653.  
  2654. /** 
  2655. * Check if plugin must be WordPress.org compliant. 
  2656. * @since 1.0.7 
  2657. * @return bool 
  2658. */ 
  2659. function is_org_repo_compliant() { 
  2660. return $this->_is_org_compliant; 
  2661.  
  2662. #---------------------------------------------------------------------------------- 
  2663. #region Daily Sync Cron 
  2664. #---------------------------------------------------------------------------------- 
  2665.  
  2666. /** 
  2667. * @author Vova Feldman (@svovaf) 
  2668. * @since 1.1.7.3 
  2669. */ 
  2670. private function run_manual_sync() { 
  2671. $this->require_pluggable_essentials(); 
  2672.  
  2673. if ( ! current_user_can( 'activate_plugins' ) ) { 
  2674. return; 
  2675.  
  2676. // Run manual sync. 
  2677. $this->_sync_cron(); 
  2678.  
  2679. // Reschedule next cron to run 24 hours from now (performance optimization). 
  2680. $this->clear_sync_cron(); 
  2681.  
  2682. $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false ); 
  2683.  
  2684. /** 
  2685. * Data sync cron job. Replaces the background sync non blocking HTTP request 
  2686. * that doesn't halt page loading. 
  2687. * @author Vova Feldman (@svovaf) 
  2688. * @since 1.1.7.3 
  2689. */ 
  2690. function _sync_cron() { 
  2691. $this->_logger->entrance(); 
  2692.  
  2693. // Store the last time data sync was executed. 
  2694. $this->_storage->sync_timestamp = time(); 
  2695.  
  2696. // Check if API is temporary down. 
  2697. if ( FS_Api::is_temporary_down() ) { 
  2698. return; 
  2699.  
  2700. // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours. 
  2701.  
  2702. if ( $this->is_registered() ) { 
  2703. if ( $this->has_paid_plan() ) { 
  2704. // Initiate background plan sync. 
  2705. $this->_sync_license( true ); 
  2706.  
  2707. if ( $this->is_paying() ) { 
  2708. // Check for premium plugin updates. 
  2709. $this->_check_updates( true ); 
  2710. } else { 
  2711. // Sync install (only if something changed locally). 
  2712. $this->sync_install(); 
  2713.  
  2714. $this->do_action( 'after_sync_cron' ); 
  2715.  
  2716. /** 
  2717. * Check if sync was executed in the last $period of seconds. 
  2718. * @author Vova Feldman (@svovaf) 
  2719. * @since 1.1.7.3 
  2720. * @param int $period In seconds 
  2721. * @return bool 
  2722. */ 
  2723. private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) { 
  2724. if ( ! isset( $this->_storage->sync_timestamp ) ) { 
  2725. return false; 
  2726.  
  2727. return ( $this->_storage->sync_timestamp > ( WP_FS__SCRIPT_START_TIME - $period ) ); 
  2728.  
  2729. /** 
  2730. * @author Vova Feldman (@svovaf) 
  2731. * @since 1.1.7.3 
  2732. * @return bool 
  2733. */ 
  2734. private function is_sync_cron_on() { 
  2735. /** 
  2736. * @var object $sync_cron_data 
  2737. */ 
  2738. $sync_cron_data = $this->_storage->get( 'sync_cron', null ); 
  2739.  
  2740. return ( ! is_null( $sync_cron_data ) && true === $sync_cron_data->on ); 
  2741.  
  2742. /** 
  2743. * @author Vova Feldman (@svovaf) 
  2744. * @since 1.1.7.3 
  2745. * @param int $start_at Defaults to now. 
  2746. * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise,  
  2747. * schedule job to start right away. 
  2748. */ 
  2749. private function schedule_sync_cron( $start_at = WP_FS__SCRIPT_START_TIME, $randomize_start = true ) { 
  2750. $this->_logger->entrance(); 
  2751.  
  2752. if ( $randomize_start ) { 
  2753. // Schedule first sync with a random 12 hour time range from now. 
  2754. $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) ); 
  2755.  
  2756. // Schedule daily WP cron. 
  2757. wp_schedule_event( 
  2758. $start_at,  
  2759. 'daily',  
  2760. $this->get_action_tag( 'data_sync' ) 
  2761. ); 
  2762.  
  2763. $this->_storage->store( 'sync_cron', (object) array( 
  2764. 'version' => $this->get_plugin_version(),  
  2765. 'sdk_version' => $this->version,  
  2766. 'timestamp' => WP_FS__SCRIPT_START_TIME,  
  2767. 'on' => true,  
  2768. ) ); 
  2769.  
  2770. /** 
  2771. * Add the actual sync function to the cron job hook. 
  2772. * @author Vova Feldman (@svovaf) 
  2773. * @since 1.1.7.3 
  2774. */ 
  2775. private function hook_callback_to_sync_cron() { 
  2776. $this->add_action( 'data_sync', array( &$this, '_sync_cron' ) ); 
  2777.  
  2778. /** 
  2779. * @author Vova Feldman (@svovaf) 
  2780. * @since 1.1.7.3 
  2781. */ 
  2782. private function clear_sync_cron() { 
  2783. $this->_logger->entrance(); 
  2784.  
  2785. if ( ! $this->is_sync_cron_on() ) { 
  2786. return; 
  2787.  
  2788. $this->_storage->remove( 'sync_cron' ); 
  2789.  
  2790. wp_clear_scheduled_hook( $this->get_action_tag( 'data_sync' ) ); 
  2791.  
  2792. /** 
  2793. * Unix timestamp for next sync cron execution or false if not scheduled. 
  2794. * @author Vova Feldman (@svovaf) 
  2795. * @since 1.1.7.3 
  2796. * @return int|false 
  2797. */ 
  2798. function next_sync_cron() { 
  2799. $this->_logger->entrance(); 
  2800.  
  2801. if ( ! $this->is_sync_cron_on() ) { 
  2802. return false; 
  2803.  
  2804. return wp_next_scheduled( $this->get_action_tag( 'data_sync' ) ); 
  2805.  
  2806. /** 
  2807. * Unix timestamp for previous sync cron execution or false if never executed. 
  2808. * @author Vova Feldman (@svovaf) 
  2809. * @since 1.1.7.3 
  2810. * @return int|false 
  2811. */ 
  2812. function last_sync_cron() { 
  2813. $this->_logger->entrance(); 
  2814.  
  2815. return $this->_storage->get( 'sync_timestamp' ); 
  2816.  
  2817. #endregion Daily Sync Cron ------------------------------------------------------------------ 
  2818.  
  2819. #---------------------------------------------------------------------------------- 
  2820. #region Async Install Sync 
  2821. #---------------------------------------------------------------------------------- 
  2822.  
  2823. /** 
  2824. * @author Vova Feldman (@svovaf) 
  2825. * @since 1.1.7.3 
  2826. * @return bool 
  2827. */ 
  2828. private function is_install_sync_scheduled() { 
  2829. /** 
  2830. * @var object $cron_data 
  2831. */ 
  2832. $cron_data = $this->_storage->get( 'install_sync_cron', null ); 
  2833.  
  2834. return ( ! is_null( $cron_data ) && true === $cron_data->on ); 
  2835.  
  2836. /** 
  2837. * Instead of running blocking install sync event, execute non blocking scheduled wp-cron. 
  2838. * @author Vova Feldman (@svovaf) 
  2839. * @since 1.1.7.3 
  2840. */ 
  2841. private function schedule_install_sync() { 
  2842. $this->_logger->entrance(); 
  2843.  
  2844. $this->clear_install_sync_cron(); 
  2845.  
  2846. // Schedule immediate install sync. 
  2847. wp_schedule_single_event( 
  2848. WP_FS__SCRIPT_START_TIME,  
  2849. $this->get_action_tag( 'install_sync' ) 
  2850. ); 
  2851.  
  2852. $this->_storage->store( 'install_sync_cron', (object) array( 
  2853. 'version' => $this->get_plugin_version(),  
  2854. 'sdk_version' => $this->version,  
  2855. 'timestamp' => WP_FS__SCRIPT_START_TIME,  
  2856. 'on' => true,  
  2857. ) ); 
  2858.  
  2859. /** 
  2860. * Unix timestamp for previous install sync cron execution or false if never executed. 
  2861. * @todo There's some very strange bug that $this->_storage->install_sync_timestamp value is not being 
  2862. * updated. But for sure the sync event is working. 
  2863. * @author Vova Feldman (@svovaf) 
  2864. * @since 1.1.7.3 
  2865. * @return int|false 
  2866. */ 
  2867. function last_install_sync() { 
  2868. $this->_logger->entrance(); 
  2869.  
  2870. return $this->_storage->get( 'install_sync_timestamp' ); 
  2871.  
  2872. /** 
  2873. * Unix timestamp for next install sync cron execution or false if not scheduled. 
  2874. * @author Vova Feldman (@svovaf) 
  2875. * @since 1.1.7.3 
  2876. * @return int|false 
  2877. */ 
  2878. function next_install_sync() { 
  2879. $this->_logger->entrance(); 
  2880.  
  2881. if ( ! $this->is_install_sync_scheduled() ) { 
  2882. return false; 
  2883.  
  2884. return wp_next_scheduled( $this->get_action_tag( 'install_sync' ) ); 
  2885.  
  2886. /** 
  2887. * Add the actual install sync function to the cron job hook. 
  2888. * @author Vova Feldman (@svovaf) 
  2889. * @since 1.1.7.3 
  2890. */ 
  2891. private function hook_callback_to_install_sync() { 
  2892. $this->add_action( 'install_sync', array( &$this, '_run_sync_install' ) ); 
  2893.  
  2894. /** 
  2895. * @author Vova Feldman (@svovaf) 
  2896. * @since 1.1.7.3 
  2897. */ 
  2898. private function clear_install_sync_cron() { 
  2899. $this->_logger->entrance(); 
  2900.  
  2901. if ( ! $this->is_install_sync_scheduled() ) { 
  2902. return; 
  2903.  
  2904. $this->_storage->remove( 'install_sync_cron' ); 
  2905.  
  2906. wp_clear_scheduled_hook( $this->get_action_tag( 'install_sync' ) ); 
  2907.  
  2908. /** 
  2909. * @author Vova Feldman (@svovaf) 
  2910. * @since 1.1.7.3 
  2911. */ 
  2912. public function _run_sync_install() { 
  2913. $this->_logger->entrance(); 
  2914.  
  2915. // Update last install sync timestamp. 
  2916. $this->_storage->install_sync_timestamp = time(); 
  2917.  
  2918. $this->sync_install( array(), true ); 
  2919.  
  2920. #endregion Async Install Sync ------------------------------------------------------------------ 
  2921.  
  2922. /** 
  2923. * Show a notice that activation is currently pending. 
  2924. * @author Vova Feldman (@svovaf) 
  2925. * @since 1.0.7 
  2926. * @param bool|string $email 
  2927. * @param bool $is_pending_trial Since 1.2.1.5 
  2928. */ 
  2929. function _add_pending_activation_notice( $email = false, $is_pending_trial = false ) { 
  2930. if ( ! is_string( $email ) ) { 
  2931. $current_user = self::_get_current_wp_user(); 
  2932. $email = $current_user->user_email; 
  2933.  
  2934. $this->_admin_notices->add_sticky( 
  2935. sprintf( 
  2936. __fs( 'pending-activation-message', $this->_slug ),  
  2937. '<b>' . $this->get_plugin_name() . '</b>',  
  2938. '<b>' . $email . '</b>',  
  2939. __fs( $is_pending_trial ? 'start-the-trial' : 'complete-the-install', $this->_slug ) 
  2940. ),  
  2941. 'activation_pending',  
  2942. 'Thanks!' 
  2943. ); 
  2944.  
  2945. /** 
  2946. * Check if currently in plugin activation. 
  2947. * @author Vova Feldman (@svovaf) 
  2948. * @since 1.1.4 
  2949. * @return bool 
  2950. */ 
  2951. function is_plugin_activation() { 
  2952. return get_option( "fs_{$this->_slug}_activated", false ); 
  2953.  
  2954. /** 
  2955. * NOTE: admin_menu action executed before admin_init. 
  2956. * @author Vova Feldman (@svovaf) 
  2957. * @since 1.0.7 
  2958. */ 
  2959. function _admin_init_action() { 
  2960. /** 
  2961. * Automatically redirect to connect/activation page after plugin activation. 
  2962. * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode. 
  2963. */ 
  2964. if ( $this->is_plugin_activation() ) { 
  2965. delete_option( "fs_{$this->_slug}_activated" ); 
  2966.  
  2967. if ( ! function_exists( 'is_network_admin' ) || ! is_network_admin() ) { 
  2968. $this->_redirect_on_activation_hook(); 
  2969.  
  2970. return; 
  2971.  
  2972. if ( fs_request_is_action( $this->_slug . '_skip_activation' ) ) { 
  2973. check_admin_referer( $this->_slug . '_skip_activation' ); 
  2974.  
  2975. $this->skip_connection(); 
  2976.  
  2977. fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) ); 
  2978.  
  2979. if ( ! $this->is_addon() && ! $this->is_registered() && ! $this->is_anonymous() ) { 
  2980. if ( ! $this->is_pending_activation() ) { 
  2981. if ( ! $this->_menu->is_activation_page() ) { 
  2982. if ( $this->is_plugin_new_install() || $this->is_only_premium() ) { 
  2983. // Show notice for new plugin installations. 
  2984. $this->_admin_notices->add( 
  2985. sprintf( 
  2986. __fs( 'you-are-step-away', $this->_slug ),  
  2987. sprintf( '<b><a href="%s">%s</a></b>',  
  2988. $this->get_activation_url(),  
  2989. sprintf( __fs( 'activate-x-now', $this->_slug ), $this->get_plugin_name() ) 
  2990. ),  
  2991. '',  
  2992. 'update-nag' 
  2993. ); 
  2994. } else { 
  2995. if ( ! isset( $this->_storage->sticky_optin_added ) ) { 
  2996. $this->_storage->sticky_optin_added = true; 
  2997.  
  2998. // Show notice for new plugin installations. 
  2999. $this->_admin_notices->add_sticky( 
  3000. sprintf( 
  3001. __fs( 'few-plugin-tweaks', $this->_slug ),  
  3002. sprintf( '<b><a href="%s">%s</a></b>',  
  3003. $this->get_activation_url(),  
  3004. sprintf( __fs( 'optin-x-now', $this->_slug ), $this->get_plugin_name() ) 
  3005. ),  
  3006. 'connect_account',  
  3007. '',  
  3008. 'update-nag' 
  3009. ); 
  3010.  
  3011. if ( $this->has_filter( 'optin_pointer_element' ) ) { 
  3012. // Don't show admin nag if plugin update. 
  3013. wp_enqueue_script( 'wp-pointer' ); 
  3014. wp_enqueue_style( 'wp-pointer' ); 
  3015.  
  3016. $this->_enqueue_connect_essentials(); 
  3017.  
  3018. add_action( 'admin_print_footer_scripts', array( 
  3019. $this,  
  3020. '_add_connect_pointer_script' 
  3021. ) ); 
  3022.  
  3023.  
  3024. $this->_add_upgrade_action_link(); 
  3025.  
  3026. /** 
  3027. * Enqueue connect requires scripts and styles. 
  3028. * @author Vova Feldman (@svovaf) 
  3029. * @since 1.1.4 
  3030. */ 
  3031. function _enqueue_connect_essentials() { 
  3032. wp_enqueue_script( 'jquery' ); 
  3033. wp_enqueue_script( 'json2' ); 
  3034.  
  3035. fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' ); 
  3036. fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' ); 
  3037.  
  3038. fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' ); 
  3039.  
  3040. /** 
  3041. * Add connect / opt-in pointer. 
  3042. * @author Vova Feldman (@svovaf) 
  3043. * @since 1.1.4 
  3044. */ 
  3045. function _add_connect_pointer_script() { 
  3046. $vars = array( 'slug' => $this->_slug ); 
  3047. $pointer_content = fs_get_template( 'connect.php', $vars ); 
  3048. ?> 
  3049. <script type="text/javascript">// <![CDATA[ 
  3050. jQuery(document).ready(function ($) { 
  3051. if ('undefined' !== typeof(jQuery().pointer)) { 
  3052.  
  3053. var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>; 
  3054.  
  3055. if (element.length > 0) { 
  3056. var optin = $(element).pointer($.extend(true, {}, { 
  3057. content : <?php echo json_encode( $pointer_content ) ?>,  
  3058. position : { 
  3059. edge : 'left',  
  3060. align: 'center' 
  3061. },  
  3062. buttons : function () { 
  3063. // Don't show pointer buttons. 
  3064. return ''; 
  3065. },  
  3066. pointerWidth: 482 
  3067. }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>)); 
  3068.  
  3069. <?php 
  3070. echo $this->apply_filters( 'optin_pointer_execute', " 
  3071.  
  3072. optin.pointer('open'); 
  3073.  
  3074. // Tag the opt-in pointer with custom class. 
  3075. $('.wp-pointer #fs_connect') 
  3076. .parents('.wp-pointer.wp-pointer-top') 
  3077. .addClass('fs-opt-in-pointer'); 
  3078.  
  3079. ", 'element', 'optin' ) ?> 
  3080. }); 
  3081. // ]]></script> 
  3082. <?php 
  3083.  
  3084. /** 
  3085. * Return current page's URL. 
  3086. * @author Vova Feldman (@svovaf) 
  3087. * @since 1.0.7 
  3088. * @return string 
  3089. */ 
  3090. function current_page_url() { 
  3091. $url = 'http'; 
  3092.  
  3093. if ( isset( $_SERVER["HTTPS"] ) ) { 
  3094. if ( $_SERVER["HTTPS"] == "on" ) { 
  3095. $url .= "s"; 
  3096. $url .= "://"; 
  3097. if ( $_SERVER["SERVER_PORT"] != "80" ) { 
  3098. $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"]; 
  3099. } else { 
  3100. $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 
  3101.  
  3102. return esc_url( $url ); 
  3103.  
  3104. /** 
  3105. * Check if the current page is the plugin's main admin settings page. 
  3106. * @author Vova Feldman (@svovaf) 
  3107. * @since 1.0.7 
  3108. * @return bool 
  3109. */ 
  3110. function _is_plugin_page() { 
  3111. return fs_is_plugin_page( $this->_menu->get_raw_slug() ) || 
  3112. fs_is_plugin_page( $this->_slug ); 
  3113.  
  3114. /** Events 
  3115. ------------------------------------------------------------------------------------------------------------------*/ 
  3116. /** 
  3117. * Delete site install from Database. 
  3118. * @author Vova Feldman (@svovaf) 
  3119. * @since 1.0.1 
  3120. * @param bool $store 
  3121. */ 
  3122. function _delete_site( $store = true ) { 
  3123. $sites = self::get_all_sites(); 
  3124.  
  3125. if ( isset( $sites[ $this->_slug ] ) ) { 
  3126. unset( $sites[ $this->_slug ] ); 
  3127.  
  3128. self::$_accounts->set_option( 'sites', $sites, $store ); 
  3129.  
  3130. /** 
  3131. * Delete plugin's plans information. 
  3132. * @param bool $store Flush to Database if true. 
  3133. * @author Vova Feldman (@svovaf) 
  3134. * @since 1.0.9 
  3135. */ 
  3136. private function _delete_plans( $store = true ) { 
  3137. $this->_logger->entrance(); 
  3138.  
  3139. $plans = self::get_all_plans(); 
  3140.  
  3141. unset( $plans[ $this->_slug ] ); 
  3142.  
  3143. self::$_accounts->set_option( 'plans', $plans, $store ); 
  3144.  
  3145. /** 
  3146. * Delete all plugin licenses. 
  3147. * @author Vova Feldman (@svovaf) 
  3148. * @since 1.0.9 
  3149. * @param bool $store 
  3150. * @param string|bool $plugin_slug 
  3151. */ 
  3152. private function _delete_licenses( $store = true, $plugin_slug = false ) { 
  3153. $this->_logger->entrance(); 
  3154.  
  3155. $all_licenses = self::get_all_licenses(); 
  3156.  
  3157. if ( ! is_string( $plugin_slug ) ) { 
  3158. $plugin_slug = $this->_slug; 
  3159.  
  3160. unset( $all_licenses[ $plugin_slug ] ); 
  3161.  
  3162. self::$_accounts->set_option( 'licenses', $all_licenses, $store ); 
  3163.  
  3164. /** 
  3165. * Check if Freemius was added on new plugin installation. 
  3166. * @author Vova Feldman (@svovaf) 
  3167. * @since 1.1.5 
  3168. * @return bool 
  3169. */ 
  3170. function is_plugin_new_install() { 
  3171. return isset( $this->_storage->is_plugin_new_install ) && 
  3172. $this->_storage->is_plugin_new_install; 
  3173.  
  3174. /** 
  3175. * Check if it's the first plugin release that is running Freemius. 
  3176. * @author Vova Feldman (@svovaf) 
  3177. * @since 1.2.1.5 
  3178. * @return bool 
  3179. */ 
  3180. function is_first_freemius_powered_version() { 
  3181. return empty( $this->_storage->plugin_last_version ); 
  3182.  
  3183. /** 
  3184. * Plugin activated hook. 
  3185. * @author Vova Feldman (@svovaf) 
  3186. * @since 1.0.1 
  3187. * @uses FS_Api 
  3188. */ 
  3189. function _activate_plugin_event_hook() { 
  3190. $this->_logger->entrance( 'slug = ' . $this->_slug ); 
  3191.  
  3192. if ( ! current_user_can( 'activate_plugins' ) ) { 
  3193. return; 
  3194.  
  3195. $this->unregister_uninstall_hook(); 
  3196.  
  3197. // Clear API cache on activation. 
  3198. FS_Api::clear_cache(); 
  3199.  
  3200. if ( $this->is_registered() ) { 
  3201. $this->reconnect_locally(); 
  3202.  
  3203. // Schedule re-activation event and sync. 
  3204. // $this->sync_install( array(), true ); 
  3205. $this->schedule_install_sync(); 
  3206.  
  3207. $is_premium_version_activation = ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) ); 
  3208.  
  3209. // 1. If running in the activation of the FREE module, get the basename of the PREMIUM. 
  3210. // 2. If running in the activation of the PREMIUM module, get the basename of the FREE. 
  3211. $other_version_basename = $is_premium_version_activation ? 
  3212. $this->_free_plugin_basename : 
  3213. $this->premium_plugin_basename(); 
  3214.  
  3215. /** 
  3216. * If the other module version is activate, deactivate it. 
  3217. * @author Leo Fajardo (@leorw) 
  3218. * @since 1.2.2 
  3219. */ 
  3220. if ( is_plugin_active( $other_version_basename ) ) { 
  3221. deactivate_plugins( $other_version_basename ); 
  3222.  
  3223. // If activating the premium module version, add an admin noitce to congratulate for an upgrade completion. 
  3224. if ( $is_premium_version_activation ) { 
  3225. $this->_admin_notices->add( 
  3226. sprintf( __fs( 'successful-version-upgrade-message', $this->_slug ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),  
  3227. __fs( 'woot', $this->_slug ) . '!' 
  3228. ); 
  3229. } else if ( $this->is_anonymous() ) { 
  3230. /** 
  3231. * Reset "skipped" click cache on the following: 
  3232. * 1. Development mode. 
  3233. * 2. If the user skipped the exact same version before. 
  3234. * @todo 3. If explicitly asked to retry after every activation. 
  3235. */ 
  3236. if ( WP_FS__DEV_MODE || 
  3237. $this->get_plugin_version() == $this->_storage->is_anonymous['version'] 
  3238. ) { 
  3239. $this->reset_anonymous_mode(); 
  3240.  
  3241. if ( ! isset( $this->_storage->is_plugin_new_install ) ) { 
  3242. /** 
  3243. * If no previous version of plugin's version exist, it means that it's either 
  3244. * the first time that the plugin installed on the site, or the plugin was installed 
  3245. * before but didn't have Freemius integrated. 
  3246. * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires 
  3247. * on manual activation via the dashboard, is_plugin_activation() is TRUE 
  3248. * only after immediate activation. 
  3249. * @since 1.1.4 
  3250. * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/ 
  3251. */ 
  3252. $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version ); 
  3253.  
  3254. if ( ! $this->_anonymous_mode && $this->has_api_connectivity( WP_FS__DEV_MODE ) ) { 
  3255. // Store hint that the plugin was just activated to enable auto-redirection to settings. 
  3256. add_option( "fs_{$this->_slug}_activated", true ); 
  3257.  
  3258. /** 
  3259. * Activation hook is executed after the plugin's main file is loaded, therefore,  
  3260. * after the plugin was loaded. The logic is located at activate_plugin() 
  3261. * ./wp-admin/includes/plugin.php. 
  3262. * @author Vova Feldman (@svovaf) 
  3263. * @since 1.1.9 
  3264. */ 
  3265. $this->_storage->was_plugin_loaded = true; 
  3266.  
  3267. /** 
  3268. * Delete account. 
  3269. * @author Vova Feldman (@svovaf) 
  3270. * @since 1.0.3 
  3271. * @param bool $check_user Enforce checking if user have plugins activation privileges. 
  3272. */ 
  3273. function delete_account_event( $check_user = true ) { 
  3274. $this->_logger->entrance( 'slug = ' . $this->_slug ); 
  3275.  
  3276. if ( $check_user && ! current_user_can( 'activate_plugins' ) ) { 
  3277. return; 
  3278.  
  3279. $this->do_action( 'before_account_delete' ); 
  3280.  
  3281. // Clear all admin notices. 
  3282. $this->_admin_notices->clear_all_sticky(); 
  3283.  
  3284. $this->_delete_site( false ); 
  3285.  
  3286. $this->_delete_plans( false ); 
  3287.  
  3288. $this->_delete_licenses( false ); 
  3289.  
  3290. // Delete add-ons related to plugin's account. 
  3291. $this->_delete_account_addons( false ); 
  3292.  
  3293. // @todo Delete plans and licenses of add-ons. 
  3294.  
  3295. self::$_accounts->store(); 
  3296.  
  3297. /** 
  3298. * IMPORTANT: 
  3299. * Clear crons must be executed before clearing all storage. 
  3300. * Otherwise, the cron will not be cleared. 
  3301. */ 
  3302. $this->clear_sync_cron(); 
  3303. $this->clear_install_sync_cron(); 
  3304.  
  3305. // Clear all storage data. 
  3306. $this->_storage->clear_all( true, array( 
  3307. 'connectivity_test',  
  3308. 'is_on',  
  3309. ) ); 
  3310.  
  3311. // Send delete event. 
  3312. $this->get_api_site_scope()->call( '/', 'delete' ); 
  3313.  
  3314. $this->do_action( 'after_account_delete' ); 
  3315.  
  3316. /** 
  3317. * Plugin deactivation hook. 
  3318. * @author Vova Feldman (@svovaf) 
  3319. * @since 1.0.1 
  3320. */ 
  3321. function _deactivate_plugin_hook() { 
  3322. $this->_logger->entrance( 'slug = ' . $this->_slug ); 
  3323.  
  3324. if ( ! current_user_can( 'activate_plugins' ) ) { 
  3325. return; 
  3326.  
  3327. $this->_admin_notices->clear_all_sticky(); 
  3328. if ( isset( $this->_storage->sticky_optin_added ) ) { 
  3329. unset( $this->_storage->sticky_optin_added ); 
  3330.  
  3331. if ( ! isset( $this->_storage->is_plugin_new_install ) ) { 
  3332. // Remember that plugin was already installed. 
  3333. $this->_storage->is_plugin_new_install = false; 
  3334.  
  3335. // Hook to plugin uninstall. 
  3336. register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) ); 
  3337.  
  3338. $this->clear_module_main_file_cache(); 
  3339. $this->clear_sync_cron(); 
  3340. $this->clear_install_sync_cron(); 
  3341.  
  3342. if ( $this->is_registered() ) { 
  3343. // Send deactivation event. 
  3344. $this->sync_install( array( 
  3345. 'is_active' => false,  
  3346. ) ); 
  3347. } else { 
  3348. if ( ! $this->has_api_connectivity() ) { 
  3349. // Reset connectivity test cache. 
  3350. unset( $this->_storage->connectivity_test ); 
  3351.  
  3352. // Clear API cache on deactivation. 
  3353. FS_Api::clear_cache(); 
  3354.  
  3355. $this->remove_sdk_reference(); 
  3356.  
  3357. /** 
  3358. * @author Vova Feldman (@svovaf) 
  3359. * @since 1.1.6 
  3360. */ 
  3361. private function remove_sdk_reference() { 
  3362. global $fs_active_plugins; 
  3363.  
  3364. foreach ( $fs_active_plugins->plugins as $sdk_path => &$data ) { 
  3365. if ( $this->_plugin_basename == $data->plugin_path ) { 
  3366. unset( $fs_active_plugins->plugins[ $sdk_path ] ); 
  3367. break; 
  3368.  
  3369. fs_fallback_to_newest_active_sdk(); 
  3370.  
  3371. /** 
  3372. * @author Vova Feldman (@svovaf) 
  3373. * @since 1.1.3 
  3374. * @param bool $is_anonymous 
  3375. */ 
  3376. private function set_anonymous_mode( $is_anonymous = true ) { 
  3377. // Store information regarding skip to try and opt-in the user 
  3378. // again in the future. 
  3379. $this->_storage->is_anonymous = array( 
  3380. 'is' => $is_anonymous,  
  3381. 'timestamp' => WP_FS__SCRIPT_START_TIME,  
  3382. 'version' => $this->get_plugin_version(),  
  3383. ); 
  3384.  
  3385. // Update anonymous mode cache. 
  3386. $this->_is_anonymous = $is_anonymous; 
  3387.  
  3388. /** 
  3389. * @author Vova Feldman (@svovaf) 
  3390. * @since 1.1.3 
  3391. */ 
  3392. private function reset_anonymous_mode() { 
  3393. unset( $this->_storage->is_anonymous ); 
  3394.  
  3395. /** 
  3396. * Clears the anonymous mode and redirects to the opt-in screen. 
  3397. * @author Vova Feldman (@svovaf) 
  3398. * @since 1.1.7 
  3399. */ 
  3400. function connect_again() { 
  3401. if ( ! $this->is_anonymous() ) { 
  3402. return; 
  3403.  
  3404. $this->reset_anonymous_mode(); 
  3405.  
  3406. fs_redirect( $this->get_activation_url() ); 
  3407.  
  3408. /** 
  3409. * Skip account connect, and set anonymous mode. 
  3410. * @author Vova Feldman (@svovaf) 
  3411. * @since 1.1.1 
  3412. */ 
  3413. private function skip_connection() { 
  3414. $this->_logger->entrance(); 
  3415.  
  3416. $this->_admin_notices->remove_sticky( 'connect_account' ); 
  3417.  
  3418. $this->set_anonymous_mode(); 
  3419.  
  3420. // Send anonymous skip event. 
  3421. // No user identified info nor any tracking will be sent after the user skips the opt-in. 
  3422. $this->get_api_plugin_scope()->call( 'skip.json', 'put', array( 
  3423. 'uid' => $this->get_anonymous_id(),  
  3424. ) ); 
  3425.  
  3426. /** 
  3427. * Plugin version update hook. 
  3428. * @author Vova Feldman (@svovaf) 
  3429. * @since 1.0.4 
  3430. */ 
  3431. private function update_plugin_version_event() { 
  3432. $this->_logger->entrance(); 
  3433.  
  3434. if ( ! $this->is_registered() ) { 
  3435. return; 
  3436.  
  3437. $this->schedule_install_sync(); 
  3438. // $this->sync_install( array(), true ); 
  3439.  
  3440. /** 
  3441. * Return a list of modified plugins since the last sync. 
  3442. * Note: 
  3443. * There's no point to store a plugins counter since even if the number of 
  3444. * plugins didn't change, we still need to check if the versions are all the 
  3445. * same and the activity state is similar. 
  3446. * @author Vova Feldman (@svovaf) 
  3447. * @since 1.1.8 
  3448. * @return array|false 
  3449. */ 
  3450. private function get_plugins_data_for_api() { 
  3451. // Alias. 
  3452. $option_name = 'all_plugins'; 
  3453.  
  3454. $all_cached_plugins = self::$_accounts->get_option( $option_name ); 
  3455.  
  3456. if ( ! is_object( $all_cached_plugins ) ) { 
  3457. $all_cached_plugins = (object) array( 
  3458. 'timestamp' => '',  
  3459. 'md5' => '',  
  3460. 'plugins' => array(),  
  3461. ); 
  3462.  
  3463. $time = time(); 
  3464.  
  3465. if ( ! empty( $all_cached_plugins->timestamp ) && 
  3466. ( $time - $all_cached_plugins->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC 
  3467. ) { 
  3468. // Don't send plugin updates if last update was in the past 5 min. 
  3469. return false; 
  3470.  
  3471. // Write timestamp to lock the logic. 
  3472. $all_cached_plugins->timestamp = $time; 
  3473. self::$_accounts->set_option( $option_name, $all_cached_plugins, true ); 
  3474.  
  3475. // Reload options from DB. 
  3476. self::$_accounts->load( true ); 
  3477. $all_cached_plugins = self::$_accounts->get_option( $option_name ); 
  3478.  
  3479. if ( $time != $all_cached_plugins->timestamp ) { 
  3480. // If timestamp is different, then another thread captured the lock. 
  3481. return false; 
  3482.  
  3483. // Check if there's a change in plugins. 
  3484. $all_plugins = self::get_all_plugins(); 
  3485.  
  3486. // Check if plugins changed. 
  3487. ksort( $all_plugins ); 
  3488.  
  3489. $plugins_signature = ''; 
  3490. foreach ( $all_plugins as $basename => $data ) { 
  3491. $plugins_signature .= $data['slug'] . ', ' . 
  3492. $data['Version'] . ', ' . 
  3493. ( $data['is_active'] ? '1' : '0' ) . ';'; 
  3494.  
  3495. // Check if plugins status changed (version or active/inactive). 
  3496. $plugins_changed = ( $all_cached_plugins->md5 !== md5( $plugins_signature ) ); 
  3497.  
  3498. $plugins_update_data = array(); 
  3499.  
  3500. if ( $plugins_changed ) { 
  3501. // Change in plugins, report changes. 
  3502.  
  3503. // Update existing plugins info. 
  3504. foreach ( $all_cached_plugins->plugins as $basename => $data ) { 
  3505. if ( ! isset( $all_plugins[ $basename ] ) ) { 
  3506. // Plugin uninstalled. 
  3507. $uninstalled_plugin_data = $data; 
  3508. $uninstalled_plugin_data['is_active'] = false; 
  3509. $uninstalled_plugin_data['is_uninstalled'] = true; 
  3510. $plugins_update_data[] = $uninstalled_plugin_data; 
  3511.  
  3512. unset( $all_plugins[ $basename ] ); 
  3513. unset( $all_cached_plugins->plugins[ $basename ] ); 
  3514. } else if ( $data['is_active'] !== $all_plugins[ $basename ]['is_active'] || 
  3515. $data['version'] !== $all_plugins[ $basename ]['Version'] 
  3516. ) { 
  3517. // Plugin activated or deactivated, or version changed. 
  3518. $all_cached_plugins->plugins[ $basename ]['is_active'] = $all_plugins[ $basename ]['is_active']; 
  3519. $all_cached_plugins->plugins[ $basename ]['version'] = $all_plugins[ $basename ]['Version']; 
  3520.  
  3521. $plugins_update_data[] = $all_cached_plugins->plugins[ $basename ]; 
  3522.  
  3523. // Find new plugins that weren't yet seen before. 
  3524. foreach ( $all_plugins as $basename => $data ) { 
  3525. if ( ! isset( $all_cached_plugins->plugins[ $basename ] ) ) { 
  3526. // New plugin. 
  3527. $new_plugin = array( 
  3528. 'slug' => $data['slug'],  
  3529. 'version' => $data['Version'],  
  3530. 'title' => $data['Name'],  
  3531. 'is_active' => $data['is_active'],  
  3532. 'is_uninstalled' => false,  
  3533. ); 
  3534.  
  3535. $plugins_update_data[] = $new_plugin; 
  3536. $all_cached_plugins->plugins[ $basename ] = $new_plugin; 
  3537.  
  3538. $all_cached_plugins->md5 = md5( $plugins_signature ); 
  3539. $all_cached_plugins->timestamp = $time; 
  3540. self::$_accounts->set_option( $option_name, $all_cached_plugins, true ); 
  3541.  
  3542. return $plugins_update_data; 
  3543.  
  3544. /** 
  3545. * Return a list of modified themes since the last sync. 
  3546. * Note: 
  3547. * There's no point to store a themes counter since even if the number of 
  3548. * themes didn't change, we still need to check if the versions are all the 
  3549. * same and the activity state is similar. 
  3550. * @author Vova Feldman (@svovaf) 
  3551. * @since 1.1.8 
  3552. * @return array|false 
  3553. */ 
  3554. private function get_themes_data_for_api() { 
  3555. // Alias. 
  3556. $option_name = 'all_themes'; 
  3557.  
  3558. $all_cached_themes = self::$_accounts->get_option( $option_name ); 
  3559.  
  3560. if ( ! is_object( $all_cached_themes ) ) { 
  3561. $all_cached_themes = (object) array( 
  3562. 'timestamp' => '',  
  3563. 'md5' => '',  
  3564. 'themes' => array(),  
  3565. ); 
  3566.  
  3567. $time = time(); 
  3568.  
  3569. if ( ! empty( $all_cached_themes->timestamp ) && 
  3570. ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC 
  3571. ) { 
  3572. // Don't send theme updates if last update was in the past 5 min. 
  3573. return false; 
  3574.  
  3575. // Write timestamp to lock the logic. 
  3576. $all_cached_themes->timestamp = $time; 
  3577. self::$_accounts->set_option( $option_name, $all_cached_themes, true ); 
  3578.  
  3579. // Reload options from DB. 
  3580. self::$_accounts->load( true ); 
  3581. $all_cached_themes = self::$_accounts->get_option( $option_name ); 
  3582.  
  3583. if ( $time != $all_cached_themes->timestamp ) { 
  3584. // If timestamp is different, then another thread captured the lock. 
  3585. return false; 
  3586.  
  3587. // Get active theme. 
  3588. $active_theme = wp_get_theme(); 
  3589. $active_theme_stylesheet = $active_theme->get_stylesheet(); 
  3590.  
  3591. // Check if there's a change in themes. 
  3592. $all_themes = wp_get_themes(); 
  3593.  
  3594. // Check if themes changed. 
  3595. ksort( $all_themes ); 
  3596.  
  3597. $themes_signature = ''; 
  3598. foreach ( $all_themes as $slug => $data ) { 
  3599. $is_active = ( $slug === $active_theme_stylesheet ); 
  3600. $themes_signature .= $slug . ', ' . 
  3601. $data->version . ', ' . 
  3602. ( $is_active ? '1' : '0' ) . ';'; 
  3603.  
  3604. // Check if themes status changed (version or active/inactive). 
  3605. $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) ); 
  3606.  
  3607. $themes_update_data = array(); 
  3608.  
  3609. if ( $themes_changed ) { 
  3610. // Change in themes, report changes. 
  3611.  
  3612. // Update existing themes info. 
  3613. foreach ( $all_cached_themes->themes as $slug => $data ) { 
  3614. $is_active = ( $slug === $active_theme_stylesheet ); 
  3615.  
  3616. if ( ! isset( $all_themes[ $slug ] ) ) { 
  3617. // Plugin uninstalled. 
  3618. $uninstalled_theme_data = $data; 
  3619. $uninstalled_theme_data['is_active'] = false; 
  3620. $uninstalled_theme_data['is_uninstalled'] = true; 
  3621. $themes_update_data[] = $uninstalled_theme_data; 
  3622.  
  3623. unset( $all_themes[ $slug ] ); 
  3624. unset( $all_cached_themes->themes[ $slug ] ); 
  3625. } else if ( $data['is_active'] !== $is_active || 
  3626. $data['version'] !== $all_themes[ $slug ]->version 
  3627. ) { 
  3628. // Plugin activated or deactivated, or version changed. 
  3629.  
  3630. $all_cached_themes->themes[ $slug ]['is_active'] = $is_active; 
  3631. $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version; 
  3632.  
  3633. $themes_update_data[] = $all_cached_themes->themes[ $slug ]; 
  3634.  
  3635. // Find new themes that weren't yet seen before. 
  3636. foreach ( $all_themes as $slug => $data ) { 
  3637. if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) { 
  3638. $is_active = ( $slug === $active_theme_stylesheet ); 
  3639.  
  3640. // New plugin. 
  3641. $new_plugin = array( 
  3642. 'slug' => $slug,  
  3643. 'version' => $data->version,  
  3644. 'title' => $data->name,  
  3645. 'is_active' => $is_active,  
  3646. 'is_uninstalled' => false,  
  3647. ); 
  3648.  
  3649. $themes_update_data[] = $new_plugin; 
  3650. $all_cached_themes->themes[ $slug ] = $new_plugin; 
  3651.  
  3652. $all_cached_themes->md5 = md5( $themes_signature ); 
  3653. $all_cached_themes->timestamp = time(); 
  3654. self::$_accounts->set_option( $option_name, $all_cached_themes, true ); 
  3655.  
  3656. return $themes_update_data; 
  3657.  
  3658. /** 
  3659. * Update install details. 
  3660. * @author Vova Feldman (@svovaf) 
  3661. * @since 1.1.2 
  3662. * @param string[] string $override 
  3663. * @param bool $include_plugins Since 1.1.8 by default include plugin changes. 
  3664. * @param bool $include_themes Since 1.1.8 by default include plugin changes. 
  3665. * @return array 
  3666. */ 
  3667. private function get_install_data_for_api( 
  3668. array $override,  
  3669. $include_plugins = true,  
  3670. $include_themes = true 
  3671. ) { 
  3672. /** 
  3673. * @since 1.1.8 Also send plugin updates. 
  3674. */ 
  3675. if ( $include_plugins && ! isset( $override['plugins'] ) ) { 
  3676. $plugins = $this->get_plugins_data_for_api(); 
  3677. if ( ! empty( $plugins ) ) { 
  3678. $override['plugins'] = $plugins; 
  3679. /** 
  3680. * @since 1.1.8 Also send themes updates. 
  3681. */ 
  3682. if ( $include_themes && ! isset( $override['themes'] ) ) { 
  3683. $themes = $this->get_themes_data_for_api(); 
  3684. if ( ! empty( $themes ) ) { 
  3685. $override['themes'] = $themes; 
  3686.  
  3687. return array_merge( array( 
  3688. 'version' => $this->get_plugin_version(),  
  3689. 'is_premium' => $this->is_premium(),  
  3690. 'language' => get_bloginfo( 'language' ),  
  3691. 'charset' => get_bloginfo( 'charset' ),  
  3692. 'platform_version' => get_bloginfo( 'version' ),  
  3693. 'sdk_version' => $this->version,  
  3694. 'programming_language_version' => phpversion(),  
  3695. 'title' => get_bloginfo( 'name' ),  
  3696. 'url' => get_site_url(),  
  3697. // Special params. 
  3698. 'is_active' => true,  
  3699. 'is_disconnected' => $this->is_tracking_prohibited(),  
  3700. 'is_uninstalled' => false,  
  3701. ), $override ); 
  3702.  
  3703. /** 
  3704. * Update install only if changed. 
  3705. * @author Vova Feldman (@svovaf) 
  3706. * @since 1.0.9 
  3707. * @param string[] string $override 
  3708. * @param bool $flush 
  3709. * @return false|object|string 
  3710. */ 
  3711. private function send_install_update( $override = array(), $flush = false ) { 
  3712. $this->_logger->entrance(); 
  3713.  
  3714. $check_properties = $this->get_install_data_for_api( $override ); 
  3715.  
  3716. if ( $flush ) { 
  3717. $params = $check_properties; 
  3718. } else { 
  3719. $params = array(); 
  3720. $special = array(); 
  3721. $special_override = false; 
  3722.  
  3723. foreach ( $check_properties as $p => $v ) { 
  3724. if ( property_exists( $this->_site, $p ) ) { 
  3725. if ( ( is_bool( $this->_site->{$p} ) || ! empty( $this->_site->{$p} ) ) && 
  3726. $this->_site->{$p} != $v 
  3727. ) { 
  3728. $this->_site->{$p} = $v; 
  3729. $params[ $p ] = $v; 
  3730. } else { 
  3731. $special[ $p ] = $v; 
  3732.  
  3733. if ( isset( $override[ $p ] ) || 
  3734. 'plugins' === $p || 
  3735. 'themes' === $p 
  3736. ) { 
  3737. $special_override = true; 
  3738.  
  3739. if ( $special_override || 0 < count( $params ) ) { 
  3740. // Add special params only if has at least one 
  3741. // standard param, or if explicitly requested to 
  3742. // override a special param or a param which is not exist 
  3743. // in the install object. 
  3744. $params = array_merge( $params, $special ); 
  3745.  
  3746. if ( 0 < count( $params ) ) { 
  3747. // Update last install sync timestamp. 
  3748. $this->_storage->install_sync_timestamp = time(); 
  3749.  
  3750. $params['uid'] = $this->get_anonymous_id(); 
  3751.  
  3752. // Send updated values to FS. 
  3753. $site = $this->get_api_site_scope()->call( '/', 'put', $params ); 
  3754.  
  3755. if ( $this->is_api_result_entity( $site ) ) { 
  3756. // I successfully sent install update, clear scheduled sync if exist. 
  3757. $this->clear_install_sync_cron(); 
  3758.  
  3759. return $site; 
  3760.  
  3761. return false; 
  3762.  
  3763. /** 
  3764. * Update install only if changed. 
  3765. * @author Vova Feldman (@svovaf) 
  3766. * @since 1.0.9 
  3767. * @param string[] string $override 
  3768. * @param bool $flush 
  3769. */ 
  3770. private function sync_install( $override = array(), $flush = false ) { 
  3771. $this->_logger->entrance(); 
  3772.  
  3773. $site = $this->send_install_update( $override, $flush ); 
  3774.  
  3775. if ( false === $site ) { 
  3776. // No sync required. 
  3777. return; 
  3778.  
  3779. if ( ! $this->is_api_result_entity( $site ) ) { 
  3780. // Failed to sync, don't update locally. 
  3781. return; 
  3782.  
  3783. $plan = $this->get_plan(); 
  3784. $this->_site = new FS_Site( $site ); 
  3785. $this->_site->plan = $plan; 
  3786.  
  3787. $this->_store_site( true ); 
  3788.  
  3789. /** 
  3790. * Track install's custom event. 
  3791. * IMPORTANT: 
  3792. * Custom event tracking is currently only supported for specific clients. 
  3793. * If you are not one of them, please don't use this method. If you will,  
  3794. * the API will simply ignore your request based on the plugin ID. 
  3795. * Need custom tracking for your plugin or theme? 
  3796. * If you are interested in custom event tracking please contact yo@freemius.com 
  3797. * for further details. 
  3798. * @author Vova Feldman (@svovaf) 
  3799. * @since 1.2.1 
  3800. * @param string $name Event name. 
  3801. * @param array $properties Associative key/value array with primitive values only 
  3802. * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s. 
  3803. * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call. 
  3804. * @return object|false Event data or FALSE on failure. 
  3805. * @throws \Freemius_InvalidArgumentException 
  3806. */ 
  3807. public function track_event( $name, $properties = array(), $process_at = false, $once = false ) { 
  3808. $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) ); 
  3809.  
  3810. if ( ! $this->is_registered() ) { 
  3811. return false; 
  3812.  
  3813. $event = array( 'type' => $name ); 
  3814.  
  3815. if ( is_numeric( $process_at ) && $process_at > time() ) { 
  3816. $event['process_at'] = $process_at; 
  3817.  
  3818. if ( $once ) { 
  3819. $event['once'] = true; 
  3820.  
  3821. if ( ! empty( $properties ) ) { 
  3822. // Verify associative array values are primitive. 
  3823. foreach ( $properties as $k => $v ) { 
  3824. if ( ! is_scalar( $v ) ) { 
  3825. throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' ); 
  3826.  
  3827. $event['properties'] = $properties; 
  3828.  
  3829. $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event ); 
  3830.  
  3831. return $this->is_api_error( $result ) ? 
  3832. false : 
  3833. $result; 
  3834.  
  3835. /** 
  3836. * Track install's custom event only once, but it still triggers the API call. 
  3837. * IMPORTANT: 
  3838. * Custom event tracking is currently only supported for specific clients. 
  3839. * If you are not one of them, please don't use this method. If you will,  
  3840. * the API will simply ignore your request based on the plugin ID. 
  3841. * Need custom tracking for your plugin or theme? 
  3842. * If you are interested in custom event tracking please contact yo@freemius.com 
  3843. * for further details. 
  3844. * @author Vova Feldman (@svovaf) 
  3845. * @since 1.2.1 
  3846. * @param string $name Event name. 
  3847. * @param array $properties Associative key/value array with primitive values only 
  3848. * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s. 
  3849. * @return object|false Event data or FALSE on failure. 
  3850. * @throws \Freemius_InvalidArgumentException 
  3851. * @user Freemius::track_event() 
  3852. */ 
  3853. public function track_event_once( $name, $properties = array(), $process_at = false ) { 
  3854. return $this->track_event( $name, $properties, $process_at, true ); 
  3855.  
  3856. /** 
  3857. * Plugin uninstall hook. 
  3858. * @author Vova Feldman (@svovaf) 
  3859. * @since 1.0.1 
  3860. * @param bool $check_user Enforce checking if user have plugins activation privileges. 
  3861. */ 
  3862. function _uninstall_plugin_event( $check_user = true ) { 
  3863. $this->_logger->entrance( 'slug = ' . $this->_slug ); 
  3864.  
  3865. if ( $check_user && ! current_user_can( 'activate_plugins' ) ) { 
  3866. return; 
  3867.  
  3868. $params = array(); 
  3869. $uninstall_reason = null; 
  3870. if ( isset( $this->_storage->uninstall_reason ) ) { 
  3871. $uninstall_reason = $this->_storage->uninstall_reason; 
  3872. $params['reason_id'] = $uninstall_reason->id; 
  3873. $params['reason_info'] = $uninstall_reason->info; 
  3874.  
  3875. if ( ! $this->is_registered() && isset( $uninstall_reason ) ) { 
  3876. // Send anonymous uninstall event only if user submitted a feedback. 
  3877. if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) { 
  3878. $this->opt_in( false, false, false, false, true ); 
  3879. } else { 
  3880. $params['uid'] = $this->get_anonymous_id(); 
  3881. $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params ); 
  3882. } else { 
  3883. // Send uninstall event. 
  3884. $this->send_install_update( array_merge( $params, array( 
  3885. 'is_active' => false,  
  3886. 'is_uninstalled' => true,  
  3887. ) ) ); 
  3888.  
  3889. // @todo Decide if we want to delete plugin information from db. 
  3890.  
  3891. /** 
  3892. * @author Vova Feldman (@svovaf) 
  3893. * @since 1.1.1 
  3894. * @return string 
  3895. */ 
  3896. private function premium_plugin_basename() { 
  3897. return preg_replace( '/\//', '-premium/', $this->_free_plugin_basename, 1 ); 
  3898.  
  3899. /** 
  3900. * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking. 
  3901. * @author Vova Feldman (@svovaf) 
  3902. * @since 1.0.2 
  3903. */ 
  3904. public static function _uninstall_plugin_hook() { 
  3905. self::_load_required_static(); 
  3906.  
  3907. self::$_static_logger->entrance(); 
  3908.  
  3909. if ( ! current_user_can( 'activate_plugins' ) ) { 
  3910. return; 
  3911.  
  3912. $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) ); 
  3913.  
  3914. self::$_static_logger->info( 'plugin = ' . $plugin_file ); 
  3915.  
  3916. define( 'WP_FS__UNINSTALL_MODE', true ); 
  3917.  
  3918. $fs = self::get_instance_by_file( $plugin_file ); 
  3919.  
  3920. if ( is_object( $fs ) ) { 
  3921. self::require_plugin_essentials(); 
  3922.  
  3923. if ( is_plugin_active( $fs->_free_plugin_basename ) || 
  3924. is_plugin_active( $fs->premium_plugin_basename() ) 
  3925. ) { 
  3926. // Deleting Free or Premium plugin version while the other version still installed. 
  3927. return; 
  3928.  
  3929. $fs->_uninstall_plugin_event(); 
  3930.  
  3931. $fs->do_action( 'after_uninstall' ); 
  3932.  
  3933. #---------------------------------------------------------------------------------- 
  3934. #region Plugin Information 
  3935. #---------------------------------------------------------------------------------- 
  3936.  
  3937. /** 
  3938. * Load WordPress core plugin.php essential module. 
  3939. * @author Vova Feldman (@svovaf) 
  3940. * @since 1.1.1 
  3941. */ 
  3942. private static function require_plugin_essentials() { 
  3943. if ( ! function_exists( 'get_plugins' ) ) { 
  3944. self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' ); 
  3945.  
  3946. require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  3947.  
  3948. /** 
  3949. * Load WordPress core pluggable.php module. 
  3950. * @author Vova Feldman (@svovaf) 
  3951. * @since 1.1.2 
  3952. */ 
  3953. private static function require_pluggable_essentials() { 
  3954. if ( ! function_exists( 'wp_get_current_user' ) ) { 
  3955. require_once( ABSPATH . 'wp-includes/pluggable.php' ); 
  3956.  
  3957. /** 
  3958. * Return plugin data. 
  3959. * @author Vova Feldman (@svovaf) 
  3960. * @since 1.0.1 
  3961. * @return array 
  3962. */ 
  3963. function get_plugin_data() { 
  3964. if ( ! isset( $this->_plugin_data ) ) { 
  3965. self::require_plugin_essentials(); 
  3966.  
  3967. /** 
  3968. * @author Vova Feldman (@svovaf) 
  3969. * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data. 
  3970. * @link https://github.com/Freemius/wordpress-sdk/issues/77 
  3971. */ 
  3972. $this->_plugin_data = get_plugin_data( 
  3973. $this->_plugin_main_file_path,  
  3974. false,  
  3975. false 
  3976. ); 
  3977.  
  3978. return $this->_plugin_data; 
  3979.  
  3980. /** 
  3981. * @author Vova Feldman (@svovaf) 
  3982. * @since 1.0.1 
  3983. * @return string Plugin slug. 
  3984. */ 
  3985. function get_slug() { 
  3986. return $this->_slug; 
  3987.  
  3988. /** 
  3989. * @author Vova Feldman (@svovaf) 
  3990. * @since 1.0.1 
  3991. * @return number Plugin ID. 
  3992. */ 
  3993. function get_id() { 
  3994. return $this->_plugin->id; 
  3995.  
  3996. /** 
  3997. * @author Vova Feldman (@svovaf) 
  3998. * @since 1.2.1.5 
  3999. * @return string Freemius SDK version 
  4000. */ 
  4001. function get_sdk_version() { 
  4002. return $this->version; 
  4003.  
  4004. /** 
  4005. * @author Vova Feldman (@svovaf) 
  4006. * @since 1.2.1.5 
  4007. * @return number Parent plugin ID (if parent exist). 
  4008. */ 
  4009. function get_parent_id() { 
  4010. return $this->is_addon() ? 
  4011. $this->get_parent_instance()->get_id() : 
  4012. $this->_plugin->id; 
  4013.  
  4014. /** 
  4015. * @author Vova Feldman (@svovaf) 
  4016. * @since 1.0.1 
  4017. * @return string Plugin public key. 
  4018. */ 
  4019. function get_public_key() { 
  4020. return $this->_plugin->public_key; 
  4021.  
  4022. /** 
  4023. * Will be available only on sandbox mode. 
  4024. * @author Vova Feldman (@svovaf) 
  4025. * @since 1.0.4 
  4026. * @return mixed Plugin secret key. 
  4027. */ 
  4028. function get_secret_key() { 
  4029. return $this->_plugin->secret_key; 
  4030.  
  4031. /** 
  4032. * @author Vova Feldman (@svovaf) 
  4033. * @since 1.1.1 
  4034. * @return bool 
  4035. */ 
  4036. function has_secret_key() { 
  4037. return ! empty( $this->_plugin->secret_key ); 
  4038.  
  4039. /** 
  4040. * @author Vova Feldman (@svovaf) 
  4041. * @since 1.0.9 
  4042. * @return string 
  4043. */ 
  4044. function get_plugin_name() { 
  4045. $this->_logger->entrance(); 
  4046.  
  4047. if ( ! isset( $this->_plugin_name ) ) { 
  4048. $plugin_data = $this->get_plugin_data(); 
  4049.  
  4050. // Get name. 
  4051. $this->_plugin_name = $plugin_data['Name']; 
  4052.  
  4053. // Check if plugin name contains [Premium] suffix and remove it. 
  4054. $suffix = '[premium]'; 
  4055. $suffix_len = strlen( $suffix ); 
  4056.  
  4057. if ( strlen( $plugin_data['Name'] ) > $suffix_len && 
  4058. $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len ) 
  4059. ) { 
  4060. $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len ); 
  4061.  
  4062. $this->_logger->departure( 'Name = ' . $this->_plugin_name ); 
  4063.  
  4064. return $this->_plugin_name; 
  4065.  
  4066. /** 
  4067. * @author Vova Feldman (@svovaf) 
  4068. * @since 1.0.0 
  4069. * @return string 
  4070. */ 
  4071. function get_plugin_version() { 
  4072. $this->_logger->entrance(); 
  4073.  
  4074. $plugin_data = $this->get_plugin_data(); 
  4075.  
  4076. $this->_logger->departure( 'Version = ' . $plugin_data['Version'] ); 
  4077.  
  4078. return $this->apply_filters( 'plugin_version', $plugin_data['Version'] ); 
  4079.  
  4080. /** 
  4081. * @author Vova Feldman (@svovaf) 
  4082. * @since 1.0.4 
  4083. * @return string 
  4084. */ 
  4085. function get_plugin_basename() { 
  4086. return $this->_plugin_basename; 
  4087.  
  4088. function get_plugin_folder_name() { 
  4089. $this->_logger->entrance(); 
  4090.  
  4091. $plugin_folder = $this->_plugin_basename; 
  4092.  
  4093. while ( '.' !== dirname( $plugin_folder ) ) { 
  4094. $plugin_folder = dirname( $plugin_folder ); 
  4095.  
  4096. $this->_logger->departure( 'Folder Name = ' . $plugin_folder ); 
  4097.  
  4098. return $plugin_folder; 
  4099.  
  4100. #endregion ------------------------------------------------------------------ 
  4101.  
  4102. /** Account 
  4103. ------------------------------------------------------------------------------------------------------------------*/ 
  4104.  
  4105. /** 
  4106. * Find plugin's slug by plugin's basename. 
  4107. * @author Vova Feldman (@svovaf) 
  4108. * @since 1.0.9 
  4109. * @param string $plugin_base_name 
  4110. * @return false|string 
  4111. */ 
  4112. private static function find_slug_by_basename( $plugin_base_name ) { 
  4113. $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() ); 
  4114.  
  4115. if ( ! array( $file_slug_map ) || ! isset( $file_slug_map[ $plugin_base_name ] ) ) { 
  4116. return false; 
  4117.  
  4118. return $file_slug_map[ $plugin_base_name ]; 
  4119.  
  4120. /** 
  4121. * Store the map between the plugin's basename to the slug. 
  4122. * @author Vova Feldman (@svovaf) 
  4123. * @since 1.0.9 
  4124. */ 
  4125. private function store_file_slug_map() { 
  4126. $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() ); 
  4127.  
  4128. if ( ! array( $file_slug_map ) ) { 
  4129. $file_slug_map = array(); 
  4130.  
  4131. if ( ! isset( $file_slug_map[ $this->_plugin_basename ] ) || 
  4132. $file_slug_map[ $this->_plugin_basename ] !== $this->_slug 
  4133. ) { 
  4134. $file_slug_map[ $this->_plugin_basename ] = $this->_slug; 
  4135. self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true ); 
  4136.  
  4137. /** 
  4138. * @return FS_User[] 
  4139. */ 
  4140. static function get_all_users() { 
  4141. $users = self::$_accounts->get_option( 'users', array() ); 
  4142.  
  4143. if ( ! is_array( $users ) ) { 
  4144. $users = array(); 
  4145.  
  4146. return $users; 
  4147.  
  4148. /** 
  4149. * @return FS_Site[] 
  4150. */ 
  4151. private static function get_all_sites() { 
  4152. $sites = self::$_accounts->get_option( 'sites', array() ); 
  4153.  
  4154. if ( ! is_array( $sites ) ) { 
  4155. $sites = array(); 
  4156.  
  4157. return $sites; 
  4158.  
  4159. /** 
  4160. * @author Vova Feldman (@svovaf) 
  4161. * @since 1.0.6 
  4162. * @return FS_Plugin_License[] 
  4163. */ 
  4164. private static function get_all_licenses() { 
  4165. $licenses = self::$_accounts->get_option( 'licenses', array() ); 
  4166.  
  4167. if ( ! is_array( $licenses ) ) { 
  4168. $licenses = array(); 
  4169.  
  4170. return $licenses; 
  4171.  
  4172. /** 
  4173. * @return FS_Plugin_Plan[] 
  4174. */ 
  4175. private static function get_all_plans() { 
  4176. $plans = self::$_accounts->get_option( 'plans', array() ); 
  4177.  
  4178. if ( ! is_array( $plans ) ) { 
  4179. $plans = array(); 
  4180.  
  4181. return $plans; 
  4182.  
  4183. /** 
  4184. * @author Vova Feldman (@svovaf) 
  4185. * @since 1.0.4 
  4186. * @return FS_Plugin_Tag[] 
  4187. */ 
  4188. private static function get_all_updates() { 
  4189. $updates = self::$_accounts->get_option( 'updates', array() ); 
  4190.  
  4191. if ( ! is_array( $updates ) ) { 
  4192. $updates = array(); 
  4193.  
  4194. return $updates; 
  4195.  
  4196. /** 
  4197. * @author Vova Feldman (@svovaf) 
  4198. * @since 1.0.6 
  4199. * @return array<number, FS_Plugin[]>|false 
  4200. */ 
  4201. private static function get_all_addons() { 
  4202. $addons = self::$_accounts->get_option( 'addons', array() ); 
  4203.  
  4204. if ( ! is_array( $addons ) ) { 
  4205. $addons = array(); 
  4206.  
  4207. return $addons; 
  4208.  
  4209. /** 
  4210. * @author Vova Feldman (@svovaf) 
  4211. * @since 1.0.6 
  4212. * @return FS_Plugin[]|false 
  4213. */ 
  4214. private static function get_all_account_addons() { 
  4215. $addons = self::$_accounts->get_option( 'account_addons', array() ); 
  4216.  
  4217. if ( ! is_array( $addons ) ) { 
  4218. $addons = array(); 
  4219.  
  4220. return $addons; 
  4221.  
  4222. /** 
  4223. * Check if user is registered. 
  4224. * @author Vova Feldman (@svovaf) 
  4225. * @since 1.0.1 
  4226. * @return bool 
  4227. */ 
  4228. function is_registered() { 
  4229. return is_object( $this->_user ); 
  4230.  
  4231. /** 
  4232. * Returns TRUE if the user opted-in and didn't disconnect (opt-out). 
  4233. * @author Leo Fajardo (@leorw) 
  4234. * @since 1.2.1.5 
  4235. * @return bool 
  4236. */ 
  4237. function is_tracking_allowed() { 
  4238. return ( is_object( $this->_site ) && true !== $this->_site->is_disconnected ); 
  4239.  
  4240. /** 
  4241. * @author Vova Feldman (@svovaf) 
  4242. * @since 1.0.4 
  4243. * @return FS_Plugin 
  4244. */ 
  4245. function get_plugin() { 
  4246. return $this->_plugin; 
  4247.  
  4248. /** 
  4249. * @author Vova Feldman (@svovaf) 
  4250. * @since 1.0.3 
  4251. * @return FS_User 
  4252. */ 
  4253. function get_user() { 
  4254. return $this->_user; 
  4255.  
  4256. /** 
  4257. * @author Vova Feldman (@svovaf) 
  4258. * @since 1.0.3 
  4259. * @return FS_Site 
  4260. */ 
  4261. function get_site() { 
  4262. return $this->_site; 
  4263.  
  4264. /** 
  4265. * Get plugin add-ons. 
  4266. * @author Vova Feldman (@svovaf) 
  4267. * @since 1.0.6 
  4268. * @since 1.1.7.3 If not yet loaded, fetch data from the API. 
  4269. * @param bool $flush 
  4270. * @return FS_Plugin[]|false 
  4271. */ 
  4272. function get_addons( $flush = false ) { 
  4273. $this->_logger->entrance(); 
  4274.  
  4275. if ( ! $this->_has_addons ) { 
  4276. return false; 
  4277.  
  4278. $addons = $this->_sync_addons( $flush ); 
  4279.  
  4280. return ( ! is_array( $addons ) || empty( $addons ) ) ? 
  4281. false : 
  4282. $addons; 
  4283.  
  4284. /** 
  4285. * @author Vova Feldman (@svovaf) 
  4286. * @since 1.0.6 
  4287. * @return FS_Plugin[]|false 
  4288. */ 
  4289. function get_account_addons() { 
  4290. $this->_logger->entrance(); 
  4291.  
  4292. $addons = self::get_all_account_addons(); 
  4293.  
  4294. if ( ! is_array( $addons ) || 
  4295. ! isset( $addons[ $this->_plugin->id ] ) || 
  4296. ! is_array( $addons[ $this->_plugin->id ] ) || 
  4297. 0 === count( $addons[ $this->_plugin->id ] ) 
  4298. ) { 
  4299. return false; 
  4300.  
  4301. return $addons[ $this->_plugin->id ]; 
  4302.  
  4303. /** 
  4304. * Check if user has any 
  4305. * @author Vova Feldman (@svovaf) 
  4306. * @since 1.1.6 
  4307. * @return bool 
  4308. */ 
  4309. function has_account_addons() { 
  4310. $addons = $this->get_account_addons(); 
  4311.  
  4312. return is_array( $addons ) && ( 0 < count( $addons ) ); 
  4313.  
  4314.  
  4315. /** 
  4316. * Get add-on by ID (from local data). 
  4317. * @author Vova Feldman (@svovaf) 
  4318. * @since 1.0.6 
  4319. * @param number $id 
  4320. * @return FS_Plugin|false 
  4321. */ 
  4322. function get_addon( $id ) { 
  4323. $this->_logger->entrance(); 
  4324.  
  4325. $addons = $this->get_addons(); 
  4326.  
  4327. if ( is_array( $addons ) ) { 
  4328. foreach ( $addons as $addon ) { 
  4329. if ( $id == $addon->id ) { 
  4330. return $addon; 
  4331.  
  4332. return false; 
  4333.  
  4334. /** 
  4335. * Get add-on by slug (from local data). 
  4336. * @author Vova Feldman (@svovaf) 
  4337. * @since 1.0.6 
  4338. * @param string $slug 
  4339. * @param bool $flush 
  4340. * @return FS_Plugin|false 
  4341. */ 
  4342. function get_addon_by_slug( $slug, $flush = false ) { 
  4343. $this->_logger->entrance(); 
  4344.  
  4345. $addons = $this->get_addons( $flush ); 
  4346.  
  4347. if ( is_array( $addons ) ) { 
  4348. foreach ( $addons as $addon ) { 
  4349. if ( $slug === $addon->slug ) { 
  4350. return $addon; 
  4351.  
  4352. return false; 
  4353.  
  4354. #---------------------------------------------------------------------------------- 
  4355. #region Plans & Licensing 
  4356. #---------------------------------------------------------------------------------- 
  4357.  
  4358. /** 
  4359. * Check if running premium plugin code. 
  4360. * @author Vova Feldman (@svovaf) 
  4361. * @since 1.0.5 
  4362. * @return bool 
  4363. */ 
  4364. function is_premium() { 
  4365. return $this->_plugin->is_premium; 
  4366.  
  4367. /** 
  4368. * Get site's plan ID. 
  4369. * @author Vova Feldman (@svovaf) 
  4370. * @since 1.0.2 
  4371. * @return number 
  4372. */ 
  4373. function get_plan_id() { 
  4374. return $this->_site->plan->id; 
  4375.  
  4376. /** 
  4377. * Get site's plan title. 
  4378. * @author Vova Feldman (@svovaf) 
  4379. * @since 1.0.2 
  4380. * @return string 
  4381. */ 
  4382. function get_plan_title() { 
  4383. return $this->_site->plan->title; 
  4384.  
  4385. /** 
  4386. * @author Vova Feldman (@svovaf) 
  4387. * @since 1.0.9 
  4388. * @return FS_Plugin_Plan|false 
  4389. */ 
  4390. function get_plan() { 
  4391. return is_object( $this->_site->plan ) ? 
  4392. $this->_site->plan : 
  4393. false; 
  4394.  
  4395. /** 
  4396. * @author Vova Feldman (@svovaf) 
  4397. * @since 1.0.3 
  4398. * @return bool 
  4399. */ 
  4400. function is_trial() { 
  4401. $this->_logger->entrance(); 
  4402.  
  4403. if ( ! $this->is_registered() ) { 
  4404. return false; 
  4405.  
  4406. return $this->_site->is_trial(); 
  4407.  
  4408. /** 
  4409. * Check if currently in a trial with payment method (credit card or paypal). 
  4410. * @author Vova Feldman (@svovaf) 
  4411. * @since 1.1.7 
  4412. * @return bool 
  4413. */ 
  4414. function is_paid_trial() { 
  4415. $this->_logger->entrance(); 
  4416.  
  4417. if ( ! $this->is_trial() ) { 
  4418. return false; 
  4419.  
  4420. return $this->has_active_valid_license() && ( $this->_site->trial_plan_id == $this->_license->plan_id ); 
  4421.  
  4422. /** 
  4423. * Check if trial already utilized. 
  4424. * @since 1.0.9 
  4425. * @return bool 
  4426. */ 
  4427. function is_trial_utilized() { 
  4428. $this->_logger->entrance(); 
  4429.  
  4430. if ( ! $this->is_registered() ) { 
  4431. return false; 
  4432.  
  4433. return $this->_site->is_trial_utilized(); 
  4434.  
  4435. /** 
  4436. * Get trial plan information (if in trial). 
  4437. * @author Vova Feldman (@svovaf) 
  4438. * @since 1.0.9 
  4439. * @return bool|FS_Plugin_Plan 
  4440. */ 
  4441. function get_trial_plan() { 
  4442. $this->_logger->entrance(); 
  4443.  
  4444. if ( ! $this->is_trial() ) { 
  4445. return false; 
  4446.  
  4447. return $this->_storage->trial_plan; 
  4448.  
  4449. /** 
  4450. * Check if the user has an activate, non-expired license on current plugin's install. 
  4451. * @since 1.0.9 
  4452. * @return bool 
  4453. */ 
  4454. function is_paying() { 
  4455. $this->_logger->entrance(); 
  4456.  
  4457. if ( ! $this->is_registered() ) { 
  4458. return false; 
  4459.  
  4460. if ( ! $this->has_paid_plan() ) { 
  4461. return false; 
  4462.  
  4463. return ( 
  4464. ! $this->is_trial() && 
  4465. 'free' !== $this->_site->plan->name && 
  4466. $this->has_active_valid_license() 
  4467. ); 
  4468.  
  4469. /** 
  4470. * @author Vova Feldman (@svovaf) 
  4471. * @since 1.0.4 
  4472. * @return bool 
  4473. */ 
  4474. function is_free_plan() { 
  4475. if ( ! $this->is_registered() ) { 
  4476. return true; 
  4477.  
  4478. if ( ! $this->has_paid_plan() ) { 
  4479. return true; 
  4480.  
  4481. return ( 
  4482. 'free' === $this->_site->plan->name || 
  4483. ! $this->has_features_enabled_license() 
  4484. ); 
  4485.  
  4486. /** 
  4487. * @author Vova Feldman (@svovaf) 
  4488. * @since 1.0.5 
  4489. * @return bool 
  4490. */ 
  4491. function _has_premium_license() { 
  4492. $this->_logger->entrance(); 
  4493.  
  4494. $premium_license = $this->_get_available_premium_license(); 
  4495.  
  4496. return ( false !== $premium_license ); 
  4497.  
  4498. /** 
  4499. * Check if user has any licenses associated with the plugin (including expired or blocking). 
  4500. * @author Vova Feldman (@svovaf) 
  4501. * @since 1.1.7.3 
  4502. * @return bool 
  4503. */ 
  4504. private function has_any_license() { 
  4505. return is_array( $this->_licenses ) && ( 0 < count( $this->_licenses ) ); 
  4506.  
  4507. /** 
  4508. * @author Vova Feldman (@svovaf) 
  4509. * @since 1.0.5 
  4510. * @return FS_Plugin_License|false 
  4511. */ 
  4512. function _get_available_premium_license() { 
  4513. $this->_logger->entrance(); 
  4514.  
  4515. if ( ! $this->has_paid_plan() ) { 
  4516. return false; 
  4517.  
  4518. if ( is_array( $this->_licenses ) ) { 
  4519. foreach ( $this->_licenses as $license ) { 
  4520. if ( ! $license->is_utilized() && $license->is_features_enabled() ) { 
  4521. return $license; 
  4522.  
  4523. return false; 
  4524.  
  4525. /** 
  4526. * Sync local plugin plans with remote server. 
  4527. * @author Vova Feldman (@svovaf) 
  4528. * @since 1.0.5 
  4529. * @return FS_Plugin_Plan[]|object 
  4530. */ 
  4531. function _sync_plans() { 
  4532. $plans = $this->_fetch_plugin_plans(); 
  4533.  
  4534. if ( $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) { 
  4535. $this->_plans = $plans; 
  4536. $this->_store_plans(); 
  4537.  
  4538. $this->do_action( 'after_plans_sync', $plans ); 
  4539.  
  4540. return $this->_plans; 
  4541.  
  4542. /** 
  4543. * @author Vova Feldman (@svovaf) 
  4544. * @since 1.0.5 
  4545. * @param number $id 
  4546. * @return FS_Plugin_Plan|false 
  4547. */ 
  4548. function _get_plan_by_id( $id ) { 
  4549. $this->_logger->entrance(); 
  4550.  
  4551. if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) { 
  4552. $this->_sync_plans(); 
  4553.  
  4554. foreach ( $this->_plans as $plan ) { 
  4555. if ( $id == $plan->id ) { 
  4556. return $plan; 
  4557.  
  4558. return false; 
  4559.  
  4560. /** 
  4561. * @author Vova Feldman (@svovaf) 
  4562. * @since 1.1.8.1 
  4563. * @param string $name 
  4564. * @return FS_Plugin_Plan|false 
  4565. */ 
  4566. private function get_plan_by_name( $name ) { 
  4567. $this->_logger->entrance(); 
  4568.  
  4569. if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) { 
  4570. $this->_sync_plans(); 
  4571.  
  4572. foreach ( $this->_plans as $plan ) { 
  4573. if ( $name == $plan->name ) { 
  4574. return $plan; 
  4575.  
  4576. return false; 
  4577.  
  4578. /** 
  4579. * Sync local plugin plans with remote server. 
  4580. * @author Vova Feldman (@svovaf) 
  4581. * @since 1.0.6 
  4582. * @param number|bool $site_license_id 
  4583. * @return FS_Plugin_License[]|object 
  4584. */ 
  4585. function _sync_licenses( $site_license_id = false ) { 
  4586. $licenses = $this->_fetch_licenses( false, $site_license_id ); 
  4587.  
  4588. if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) { 
  4589. $this->_licenses = $licenses; 
  4590. $this->_store_licenses(); 
  4591.  
  4592. // Update current license. 
  4593. if ( is_object( $this->_license ) ) { 
  4594. $this->_license = $this->_get_license_by_id( $this->_license->id ); 
  4595.  
  4596. return $this->_licenses; 
  4597.  
  4598. /** 
  4599. * @author Vova Feldman (@svovaf) 
  4600. * @since 1.0.5 
  4601. * @param number $id 
  4602. * @return FS_Plugin_License|false 
  4603. */ 
  4604. function _get_license_by_id( $id ) { 
  4605. $this->_logger->entrance(); 
  4606.  
  4607. if ( ! is_numeric( $id ) ) { 
  4608. return false; 
  4609.  
  4610. if ( ! $this->has_any_license() ) { 
  4611. $this->_sync_licenses(); 
  4612.  
  4613. foreach ( $this->_licenses as $license ) { 
  4614. if ( $id == $license->id ) { 
  4615. return $license; 
  4616.  
  4617. return false; 
  4618.  
  4619. /** 
  4620. * Sync site's license with user licenses. 
  4621. * @author Vova Feldman (@svovaf) 
  4622. * @since 1.0.6 
  4623. * @param FS_Plugin_License|null $new_license 
  4624. */ 
  4625. function _update_site_license( $new_license ) { 
  4626. $this->_logger->entrance(); 
  4627.  
  4628. $this->_license = $new_license; 
  4629.  
  4630. if ( ! is_object( $new_license ) ) { 
  4631. $this->_site->license_id = null; 
  4632. $this->_sync_site_subscription( null ); 
  4633.  
  4634. return; 
  4635.  
  4636. $this->_site->license_id = $this->_license->id; 
  4637.  
  4638. if ( ! is_array( $this->_licenses ) ) { 
  4639. $this->_licenses = array(); 
  4640.  
  4641. $is_license_found = false; 
  4642. for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) { 
  4643. if ( $new_license->id == $this->_licenses[ $i ]->id ) { 
  4644. $this->_licenses[ $i ] = $new_license; 
  4645.  
  4646. $is_license_found = true; 
  4647. break; 
  4648.  
  4649. // If new license just append. 
  4650. if ( ! $is_license_found ) { 
  4651. $this->_licenses[] = $new_license; 
  4652.  
  4653. $this->_sync_site_subscription( $new_license ); 
  4654.  
  4655. /** 
  4656. * Sync site's subscription. 
  4657. * @author Vova Feldman (@svovaf) 
  4658. * @since 1.0.9 
  4659. * @param FS_Plugin_License|null $license 
  4660. * @return bool|\FS_Subscription 
  4661. */ 
  4662. private function _sync_site_subscription( $license ) { 
  4663. if ( ! is_object( $license ) ) { 
  4664. unset( $this->_storage->subscription ); 
  4665.  
  4666. return false; 
  4667.  
  4668. // Load subscription details if not lifetime. 
  4669. $subscription = $license->is_lifetime() ? 
  4670. false : 
  4671. $this->_fetch_site_license_subscription(); 
  4672.  
  4673. if ( is_object( $subscription ) && ! isset( $subscription->error ) ) { 
  4674. $this->_storage->subscription = $subscription; 
  4675. } else { 
  4676. unset( $this->_storage->subscription ); 
  4677.  
  4678. return $subscription; 
  4679.  
  4680. /** 
  4681. * @author Vova Feldman (@svovaf) 
  4682. * @since 1.0.6 
  4683. * @return bool|\FS_Plugin_License 
  4684. */ 
  4685. function _get_license() { 
  4686. return $this->_license; 
  4687.  
  4688. /** 
  4689. * @return bool|\FS_Subscription 
  4690. */ 
  4691. function _get_subscription() { 
  4692. return isset( $this->_storage->subscription ) ? 
  4693. $this->_storage->subscription : 
  4694. false; 
  4695.  
  4696. /** 
  4697. * @author Vova Feldman (@svovaf) 
  4698. * @since 1.0.2 
  4699. * @param string $plan Plan name 
  4700. * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans. 
  4701. * @return bool 
  4702. */ 
  4703. function is_plan( $plan, $exact = false ) { 
  4704. $this->_logger->entrance(); 
  4705.  
  4706. if ( ! $this->is_registered() ) { 
  4707. return false; 
  4708.  
  4709. $plan = strtolower( $plan ); 
  4710.  
  4711. if ( $this->_site->plan->name === $plan ) // Exact plan. 
  4712. return true; 
  4713. } else if ( $exact ) // Required exact, but plans are different. 
  4714. return false; 
  4715.  
  4716. $current_plan_order = - 1; 
  4717. $required_plan_order = - 1; 
  4718. for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) { 
  4719. if ( $plan === $this->_plans[ $i ]->name ) { 
  4720. $required_plan_order = $i; 
  4721. } else if ( $this->_site->plan->name === $this->_plans[ $i ]->name ) { 
  4722. $current_plan_order = $i; 
  4723.  
  4724. return ( $current_plan_order > $required_plan_order ); 
  4725.  
  4726. /** 
  4727. * Check if plan based on trial. If not in trial mode, should return false. 
  4728. * @since 1.0.9 
  4729. * @param string $plan Plan name 
  4730. * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans. 
  4731. * @return bool 
  4732. */ 
  4733. function is_trial_plan( $plan, $exact = false ) { 
  4734. $this->_logger->entrance(); 
  4735.  
  4736. if ( ! $this->is_registered() ) { 
  4737. return false; 
  4738.  
  4739. if ( ! $this->is_trial() ) { 
  4740. return false; 
  4741.  
  4742. if ( ! isset( $this->_storage->trial_plan ) ) { 
  4743. // Store trial plan information. 
  4744. $this->_enrich_site_trial_plan( true ); 
  4745.  
  4746. if ( $this->_storage->trial_plan->name === $plan ) // Exact plan. 
  4747. return true; 
  4748. } else if ( $exact ) // Required exact, but plans are different. 
  4749. return false; 
  4750.  
  4751. $current_plan_order = - 1; 
  4752. $required_plan_order = - 1; 
  4753. for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) { 
  4754. if ( $plan === $this->_plans[ $i ]->name ) { 
  4755. $required_plan_order = $i; 
  4756. } else if ( $this->_storage->trial_plan->name === $this->_plans[ $i ]->name ) { 
  4757. $current_plan_order = $i; 
  4758.  
  4759. return ( $current_plan_order > $required_plan_order ); 
  4760.  
  4761. /** 
  4762. * Check if plugin has any paid plans. 
  4763. * @author Vova Feldman (@svovaf) 
  4764. * @since 1.0.7 
  4765. * @return bool 
  4766. */ 
  4767. function has_paid_plan() { 
  4768. return $this->_has_paid_plans || 
  4769. FS_Plan_Manager::instance()->has_paid_plan( $this->_plans ); 
  4770.  
  4771. /** 
  4772. * Check if plugin has any plan with a trail. 
  4773. * @author Vova Feldman (@svovaf) 
  4774. * @since 1.0.9 
  4775. * @return bool 
  4776. */ 
  4777. function has_trial_plan() { 
  4778. if ( ! $this->is_registered() ) { 
  4779. /** 
  4780. * @author Vova Feldman(@svovaf) 
  4781. * @since 1.2.1.5 
  4782. * Allow setting a trial from the SDK without calling the API. 
  4783. * But, if the user did opt-in, continue using the real data from the API. 
  4784. */ 
  4785. if ( $this->_trial_days >= 0 ) { 
  4786. return true; 
  4787.  
  4788. return false; 
  4789.  
  4790. return $this->_storage->get( 'has_trial_plan', false ); 
  4791.  
  4792. /** 
  4793. * Check if plugin has any free plan, or is it premium only. 
  4794. * Note: If no plans configured, assume plugin is free. 
  4795. * @author Vova Feldman (@svovaf) 
  4796. * @since 1.0.7 
  4797. * @return bool 
  4798. */ 
  4799. function has_free_plan() { 
  4800. return ! $this->is_only_premium(); 
  4801.  
  4802. /** 
  4803. * Displays a license activation dialog box when the user clicks on the "Activate License" 
  4804. * or "Change License" link on the plugins 
  4805. * page. 
  4806. * @author Leo Fajardo (@leorw) 
  4807. * @since 1.1.9 
  4808. */ 
  4809. function _add_license_activation_dialog_box() { 
  4810. $vars = array( 
  4811. 'slug' => $this->_slug,  
  4812. ); 
  4813.  
  4814. fs_require_template( 'forms/license-activation.php', $vars ); 
  4815. fs_require_template( 'forms/resend-key.php', $vars ); 
  4816.  
  4817. /** 
  4818. * Displays the opt-out dialog box when the user clicks on the "Opt Out" link on the "Plugins" 
  4819. * page. 
  4820. * @author Leo Fajardo (@leorw) 
  4821. * @since 1.2.1.5 
  4822. */ 
  4823. function _add_optout_dialog() { 
  4824. $vars = array( 
  4825. 'slug' => $this->_slug,  
  4826. ); 
  4827.  
  4828. fs_require_template( 'forms/optout.php', $vars ); 
  4829.  
  4830. /** 
  4831. * Prepare page to include all required UI and logic for the license activation dialog. 
  4832. * @author Vova Feldman (@svovaf) 
  4833. * @since 1.2.0 
  4834. */ 
  4835. function _add_license_activation() { 
  4836. if ( ! current_user_can( 'activate_plugins' ) ) { 
  4837. // Only admins can activate a license. 
  4838. return; 
  4839.  
  4840. if ( ! $this->has_paid_plan() ) { 
  4841. // Module doesn't have any paid plans. 
  4842. return; 
  4843.  
  4844. if ( ! $this->is_premium() ) { 
  4845. // Only add license activation logic to the premium version. 
  4846. return; 
  4847.  
  4848. // Add license activation link and AJAX request handler. 
  4849. if ( $this->is_plugins_page() ) { 
  4850. /** 
  4851. * @since 1.2.0 Add license action link only on plugins page. 
  4852. */ 
  4853. $this->_add_license_action_link(); 
  4854.  
  4855. // Add license activation AJAX callback. 
  4856. $this->add_ajax_action( 'activate_license', array( &$this, '_activate_license_ajax_action' ) ); 
  4857.  
  4858. // Add resend license AJAX callback. 
  4859. $this->add_ajax_action( 'resend_license_key', array( &$this, '_resend_license_key_ajax_action' ) ); 
  4860.  
  4861. /** 
  4862. * @author Leo Fajardo (@leorw) 
  4863. * @since 1.1.9 
  4864. */ 
  4865. function _activate_license_ajax_action() { 
  4866. $license_key = trim( fs_request_get( 'license_key' ) ); 
  4867.  
  4868. if ( empty( $license_key ) ) { 
  4869. exit; 
  4870.  
  4871. $slug = $_POST['slug']; 
  4872. $fs = ( ( $slug === $this->_slug ) ? $this : self::instance( $slug ) ); 
  4873. $error = false; 
  4874. $next_page = false; 
  4875.  
  4876. if ( $this->is_registered() ) { 
  4877. $api = $fs->get_api_site_scope(); 
  4878. $install = $api->call( '/', 'put', array( 
  4879. 'license_key' => $this->apply_filters( 'license_key', $license_key ) 
  4880. ) ); 
  4881.  
  4882. if ( isset( $install->error ) ) { 
  4883. $error = $install->error->message; 
  4884. } else { 
  4885. $fs = $this->is_addon() ? 
  4886. $this->get_parent_instance() : 
  4887. $this; 
  4888.  
  4889. $next_page = $fs->_get_sync_license_url( $this->get_id(), true ); 
  4890.  
  4891. $this->reconnect_locally(); 
  4892. } else { 
  4893. $next_page = $this->opt_in( false, false, false, $license_key ); 
  4894.  
  4895. if ( isset( $next_page->error ) ) { 
  4896. $error = $next_page->error; 
  4897.  
  4898. $result = array( 
  4899. 'success' => ( false === $error ) 
  4900. ); 
  4901.  
  4902. if ( false !== $error ) { 
  4903. $result['error'] = $error; 
  4904. } else { 
  4905. $result['next_page'] = $next_page; 
  4906.  
  4907. echo json_encode( $result ); 
  4908.  
  4909. exit; 
  4910.  
  4911. /** 
  4912. * Billing update AJAX callback. 
  4913. * @author Vova Feldman (@svovaf) 
  4914. * @since 1.2.1.5 
  4915. */ 
  4916. function _update_billing_ajax_action() { 
  4917. check_ajax_referer( $this->get_action_tag( 'update_billing' ), 'security' ); 
  4918.  
  4919. if ( ! current_user_can( 'activate_plugins' ) ) { 
  4920. // Only for admins. 
  4921. $this->shoot_ajax_failure(); 
  4922.  
  4923. $billing = fs_request_get( 'billing' ); 
  4924.  
  4925. $api = $this->get_api_user_scope(); 
  4926. $result = $api->call( '/billing.json', 'put', array_merge( $billing, array( 
  4927. 'plugin_id' => $this->get_parent_id(),  
  4928. ) ) ); 
  4929.  
  4930. if ( ! $this->is_api_result_entity( $result ) ) { 
  4931. $this->shoot_ajax_failure(); 
  4932.  
  4933. // Purge cached billing. 
  4934. $this->get_api_user_scope()->purge_cache( 'billing.json' ); 
  4935.  
  4936. $this->shoot_ajax_success(); 
  4937.  
  4938. /** 
  4939. * Trial start for anonymous users (AJAX callback). 
  4940. * @author Vova Feldman (@svovaf) 
  4941. * @since 1.2.1.5 
  4942. */ 
  4943. function _start_trial_ajax_action() { 
  4944. check_ajax_referer( $this->get_action_tag( 'start_trial' ), 'security' ); 
  4945.  
  4946. if ( ! current_user_can( 'activate_plugins' ) ) { 
  4947. // Only for admins. 
  4948. $this->shoot_ajax_failure(); 
  4949.  
  4950. $trial_data = fs_request_get( 'trial' ); 
  4951.  
  4952. $next_page = $this->opt_in( 
  4953. false,  
  4954. false,  
  4955. false,  
  4956. false,  
  4957. false,  
  4958. $trial_data['plan_id'] 
  4959. ); 
  4960.  
  4961. if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) { 
  4962. $this->shoot_ajax_failure( 
  4963. isset( $next_page->error ) ? 
  4964. $next_page->error->message : 
  4965. var_export( $next_page, true ) 
  4966. ); 
  4967.  
  4968. $this->shoot_ajax_success( array( 
  4969. 'next_page' => $next_page,  
  4970. ) ); 
  4971.  
  4972. /** 
  4973. * @author Leo Fajardo (@leorw) 
  4974. * @since 1.2.0 
  4975. */ 
  4976. function _resend_license_key_ajax_action() { 
  4977. if ( ! isset( $_POST['email'] ) ) { 
  4978. exit; 
  4979.  
  4980. $email_address = trim( $_POST['email'] ); 
  4981. if ( empty( $email_address ) ) { 
  4982. exit; 
  4983.  
  4984. $error = false; 
  4985.  
  4986. $api = $this->get_api_plugin_scope(); 
  4987. $result = $api->call( '/licenses/resend.json', 'post',  
  4988. array( 
  4989. 'email' => $email_address,  
  4990. 'is_localhost' => WP_FS__IS_LOCALHOST 
  4991. ); 
  4992.  
  4993. if ( is_object( $result ) && isset( $result->error ) ) { 
  4994. $error = $result->error; 
  4995.  
  4996. if ( in_array( $error->code, array( 'invalid_email', 'no_user' ) ) ) { 
  4997. $error = __fs( 'email-not-found' ); 
  4998. } else if ( 'no_license' === $error->code ) { 
  4999. $error = __fs( 'no-active-licenses' ); 
  5000. } else { 
  5001. $error = $error->message; 
  5002.  
  5003. $licenses = array( 
  5004. 'success' => ( false === $error ) 
  5005. ); 
  5006.  
  5007. if ( false !== $error ) { 
  5008. $licenses['error'] = sprintf( '%s... %s', __fs( 'oops', $this->_slug ), strtolower( $error ) ); 
  5009.  
  5010. echo json_encode( $licenses ); 
  5011.  
  5012. exit; 
  5013.  
  5014. /** 
  5015. * Helper method to check if user in the plugins page. 
  5016. * @author Vova Feldman (@svovaf) 
  5017. * @since 1.2.1.5 
  5018. * @return bool 
  5019. */ 
  5020. private function is_plugins_page() { 
  5021. global $pagenow; 
  5022.  
  5023. return ( 'plugins.php' === $pagenow ); 
  5024.  
  5025. #---------------------------------------------------------------------------------- 
  5026. #region URL Generators 
  5027. #---------------------------------------------------------------------------------- 
  5028.  
  5029. /** 
  5030. * Alias to pricing_url(). 
  5031. * @author Vova Feldman (@svovaf) 
  5032. * @since 1.0.2 
  5033. * @uses pricing_url() 
  5034. * @param string $period Billing cycle 
  5035. * @param bool $is_trial 
  5036. * @return string 
  5037. */ 
  5038. function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) { 
  5039. return $this->pricing_url( $period, $is_trial ); 
  5040.  
  5041. /** 
  5042. * @author Vova Feldman (@svovaf) 
  5043. * @since 1.0.9 
  5044. * @uses get_upgrade_url() 
  5045. * @return string 
  5046. */ 
  5047. function get_trial_url() { 
  5048. return $this->get_upgrade_url( WP_FS__PERIOD_ANNUALLY, true ); 
  5049.  
  5050. /** 
  5051. * Plugin's pricing URL. 
  5052. * @author Vova Feldman (@svovaf) 
  5053. * @since 1.0.4 
  5054. * @param string $billing_cycle Billing cycle 
  5055. * @param bool $is_trial 
  5056. * @return string 
  5057. */ 
  5058. function pricing_url( $billing_cycle = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) { 
  5059. $this->_logger->entrance(); 
  5060.  
  5061. $params = array( 
  5062. 'billing_cycle' => $billing_cycle 
  5063. ); 
  5064.  
  5065. if ( $is_trial ) { 
  5066. $params['trial'] = 'true'; 
  5067.  
  5068. return $this->_get_admin_page_url( 'pricing', $params ); 
  5069.  
  5070. /** 
  5071. * Checkout page URL. 
  5072. * @author Vova Feldman (@svovaf) 
  5073. * @since 1.0.6 
  5074. * @param string $billing_cycle Billing cycle 
  5075. * @param bool $is_trial 
  5076. * @param array $extra (optional) Extra parameters, override other query params. 
  5077. * @return string 
  5078. */ 
  5079. function checkout_url( 
  5080. $billing_cycle = WP_FS__PERIOD_ANNUALLY,  
  5081. $is_trial = false,  
  5082. $extra = array() 
  5083. ) { 
  5084. $this->_logger->entrance(); 
  5085.  
  5086. $params = array( 
  5087. 'checkout' => 'true',  
  5088. 'billing_cycle' => $billing_cycle,  
  5089. ); 
  5090.  
  5091. if ( $is_trial ) { 
  5092. $params['trial'] = 'true'; 
  5093.  
  5094. /** 
  5095. * Params in extra override other params. 
  5096. */ 
  5097. $params = array_merge( $params, $extra ); 
  5098.  
  5099. return $this->_get_admin_page_url( 'pricing', $params ); 
  5100.  
  5101. /** 
  5102. * Add-on checkout URL. 
  5103. * @author Vova Feldman (@svovaf) 
  5104. * @since 1.1.7 
  5105. * @param number $addon_id 
  5106. * @param number $pricing_id 
  5107. * @param string $billing_cycle 
  5108. * @param bool $is_trial 
  5109. * @return string 
  5110. */ 
  5111. function addon_checkout_url( 
  5112. $addon_id,  
  5113. $pricing_id,  
  5114. $billing_cycle = WP_FS__PERIOD_ANNUALLY,  
  5115. $is_trial = false 
  5116. ) { 
  5117. return $this->checkout_url( $billing_cycle, $is_trial, array( 
  5118. 'plugin_id' => $addon_id,  
  5119. 'pricing_id' => $pricing_id,  
  5120. ) ); 
  5121.  
  5122. #endregion 
  5123.  
  5124. #endregion ------------------------------------------------------------------ 
  5125.  
  5126. /** 
  5127. * Check if plugin has any add-ons. 
  5128. * @author Vova Feldman (@svovaf) 
  5129. * @since 1.0.5 
  5130. * @since 1.1.7.3 Base logic only on the parameter provided by the developer in the init function. 
  5131. * @return bool 
  5132. */ 
  5133. function has_addons() { 
  5134. $this->_logger->entrance(); 
  5135.  
  5136. return $this->_has_addons; 
  5137.  
  5138. /** 
  5139. * Check if plugin can work in anonymous mode. 
  5140. * @author Vova Feldman (@svovaf) 
  5141. * @since 1.0.9 
  5142. * @return bool 
  5143. * @deprecated Please use is_enable_anonymous() instead 
  5144. */ 
  5145. function enable_anonymous() { 
  5146. return $this->_enable_anonymous; 
  5147.  
  5148. /** 
  5149. * Check if plugin can work in anonymous mode. 
  5150. * @author Vova Feldman (@svovaf) 
  5151. * @since 1.1.9 
  5152. * @return bool 
  5153. */ 
  5154. function is_enable_anonymous() { 
  5155. return $this->_enable_anonymous; 
  5156.  
  5157. /** 
  5158. * Check if plugin is premium only (no free plans). 
  5159. * @author Vova Feldman (@svovaf) 
  5160. * @since 1.1.9 
  5161. * @return bool 
  5162. */ 
  5163. function is_only_premium() { 
  5164. return $this->_is_premium_only; 
  5165.  
  5166. /** 
  5167. * Check if feature supported with current site's plan. 
  5168. * @author Vova Feldman (@svovaf) 
  5169. * @since 1.0.1 
  5170. * @todo IMPLEMENT 
  5171. * @param number $feature_id 
  5172. * @throws Exception 
  5173. */ 
  5174. function is_feature_supported( $feature_id ) { 
  5175. throw new Exception( 'not implemented' ); 
  5176.  
  5177. /** 
  5178. * @author Vova Feldman (@svovaf) 
  5179. * @since 1.0.1 
  5180. * @return bool Is running in SSL/HTTPS 
  5181. */ 
  5182. function is_ssl() { 
  5183. return WP_FS__IS_HTTPS; 
  5184.  
  5185. /** 
  5186. * @author Vova Feldman (@svovaf) 
  5187. * @since 1.0.9 
  5188. * @return bool Is running in AJAX call. 
  5189. * @link http://wordpress.stackexchange.com/questions/70676/how-to-check-if-i-am-in-admin-ajax 
  5190. */ 
  5191. function is_ajax() { 
  5192. return ( defined( 'DOING_AJAX' ) && DOING_AJAX ); 
  5193.  
  5194. /** 
  5195. * Check if it's an AJAX call targeted for the current module. 
  5196. * @author Vova Feldman (@svovaf) 
  5197. * @since 1.2.0 
  5198. * @param array|string $actions Collection of AJAX actions. 
  5199. * @return bool 
  5200. */ 
  5201. function is_ajax_action( $actions ) { 
  5202. // Verify it's an ajax call. 
  5203. if ( ! $this->is_ajax() ) { 
  5204. return false; 
  5205.  
  5206. // Verify the call is relevant for the plugin. 
  5207. if ( $this->_slug !== fs_request_get( 'slug' ) ) { 
  5208. return false; 
  5209.  
  5210. // Verify it's one of the specified actions. 
  5211. if ( is_string( $actions ) ) { 
  5212. $actions = explode( ', ', $actions ); 
  5213.  
  5214. if ( is_array( $actions ) && 0 < count( $actions ) ) { 
  5215. $ajax_action = fs_request_get( 'action' ); 
  5216.  
  5217. foreach ( $actions as $action ) { 
  5218. if ( $ajax_action === $this->get_action_tag( $action ) ) { 
  5219. return true; 
  5220.  
  5221. return false; 
  5222.  
  5223. /** 
  5224. * @author Vova Feldman (@svovaf) 
  5225. * @since 1.1.7 
  5226. * @return bool 
  5227. */ 
  5228. function is_cron() { 
  5229. return ( defined( 'DOING_CRON' ) && DOING_CRON ); 
  5230.  
  5231. /** 
  5232. * Check if a real user is visiting the admin dashboard. 
  5233. * @author Vova Feldman (@svovaf) 
  5234. * @since 1.1.7 
  5235. * @return bool 
  5236. */ 
  5237. function is_user_in_admin() { 
  5238. return is_admin() && ! $this->is_ajax() && ! $this->is_cron(); 
  5239.  
  5240. /** 
  5241. * Check if running in HTTPS and if site's plan matching the specified plan. 
  5242. * @param string $plan 
  5243. * @param bool $exact 
  5244. * @return bool 
  5245. */ 
  5246. function is_ssl_and_plan( $plan, $exact = false ) { 
  5247. return ( $this->is_ssl() && $this->is_plan( $plan, $exact ) ); 
  5248.  
  5249. /** 
  5250. * Construct plugin's settings page URL. 
  5251. * @author Vova Feldman (@svovaf) 
  5252. * @since 1.0.4 
  5253. * @param string $page 
  5254. * @param array $params 
  5255. * @return string 
  5256. */ 
  5257. function _get_admin_page_url( $page = '', $params = array() ) { 
  5258. if ( 0 < count( $params ) ) { 
  5259. foreach ( $params as $k => $v ) { 
  5260. $params[ $k ] = urlencode( $v ); 
  5261.  
  5262. if ( ! $this->_menu->is_top_level() ) { 
  5263. $parent_slug = $this->_menu->get_parent_slug(); 
  5264. $menu_file = ( false !== strpos( $parent_slug, '.php' ) ) ? 
  5265. $parent_slug : 
  5266. 'admin.php'; 
  5267.  
  5268. return add_query_arg( array_merge( $params, array( 
  5269. 'page' => $this->_menu->get_slug( $page ),  
  5270. ) ), admin_url( $menu_file, 'admin' ) ); 
  5271.  
  5272. if ( $this->_menu->is_cpt() ) { 
  5273. if ( empty( $page ) && $this->is_activation_mode() ) { 
  5274. return add_query_arg( array_merge( $params, array( 
  5275. 'page' => $this->_menu->get_slug() 
  5276. ) ), admin_url( 'admin.php', 'admin' ) ); 
  5277. } else { 
  5278. if ( ! empty( $page ) ) { 
  5279. $params['page'] = $this->_menu->get_slug( $page ); 
  5280.  
  5281. return add_query_arg( $params, admin_url( $this->_menu->get_raw_slug(), 'admin' ) ); 
  5282. } else { 
  5283. return add_query_arg( array_merge( $params, array( 
  5284. 'page' => $this->_menu->get_slug( $page ),  
  5285. ) ), admin_url( 'admin.php', 'admin' ) ); 
  5286.  
  5287. /** 
  5288. * Plugin's account page + sync license URL. 
  5289. * @author Vova Feldman (@svovaf) 
  5290. * @since 1.1.9.1 
  5291. * @param bool|number $plugin_id 
  5292. * @param bool $add_action_nonce 
  5293. * @return string 
  5294. */ 
  5295. function _get_sync_license_url( $plugin_id = false, $add_action_nonce = true ) { 
  5296. $params = array(); 
  5297.  
  5298. if ( is_numeric( $plugin_id ) ) { 
  5299. $params['plugin_id'] = $plugin_id; 
  5300.  
  5301. return $this->get_account_url( 
  5302. $this->_slug . '_sync_license',  
  5303. $params,  
  5304. $add_action_nonce 
  5305. ); 
  5306.  
  5307. /** 
  5308. * Plugin's account URL. 
  5309. * @author Vova Feldman (@svovaf) 
  5310. * @since 1.0.4 
  5311. * @param bool|string $action 
  5312. * @param array $params 
  5313. * @param bool $add_action_nonce 
  5314. * @return string 
  5315. */ 
  5316. function get_account_url( $action = false, $params = array(), $add_action_nonce = true ) { 
  5317. if ( is_string( $action ) ) { 
  5318. $params['fs_action'] = $action; 
  5319.  
  5320. self::require_pluggable_essentials(); 
  5321.  
  5322. return ( $add_action_nonce && is_string( $action ) ) ? 
  5323. fs_nonce_url( $this->_get_admin_page_url( 'account', $params ), $action ) : 
  5324. $this->_get_admin_page_url( 'account', $params ); 
  5325.  
  5326. /** 
  5327. * @author Vova Feldman (@svovaf) 
  5328. * @since 1.2.0 
  5329. * @param string $tab 
  5330. * @param bool $action 
  5331. * @param array $params 
  5332. * @param bool $add_action_nonce 
  5333. * @return string 
  5334. * @uses get_account_url() 
  5335. */ 
  5336. function get_account_tab_url( $tab, $action = false, $params = array(), $add_action_nonce = true ) { 
  5337. $params['tab'] = $tab; 
  5338.  
  5339. return $this->get_account_url( $action, $params, $add_action_nonce ); 
  5340.  
  5341. /** 
  5342. * Plugin's account URL. 
  5343. * @author Vova Feldman (@svovaf) 
  5344. * @since 1.0.4 
  5345. * @param bool|string $topic 
  5346. * @param bool|string $message 
  5347. * @return string 
  5348. */ 
  5349. function contact_url( $topic = false, $message = false ) { 
  5350. $params = array(); 
  5351. if ( is_string( $topic ) ) { 
  5352. $params['topic'] = $topic; 
  5353. if ( is_string( $message ) ) { 
  5354. $params['message'] = $message; 
  5355.  
  5356. if ( $this->is_addon() ) { 
  5357. $params['addon_id'] = $this->get_id(); 
  5358.  
  5359. return $this->get_parent_instance()->_get_admin_page_url( 'contact', $params ); 
  5360. } else { 
  5361. return $this->_get_admin_page_url( 'contact', $params ); 
  5362.  
  5363. /** 
  5364. * Add-on direct info URL. 
  5365. * @author Vova Feldman (@svovaf) 
  5366. * @since 1.1.0 
  5367. * @param string $slug 
  5368. * @return string 
  5369. */ 
  5370. function addon_url( $slug ) { 
  5371. return $this->_get_admin_page_url( 'addons', array( 
  5372. 'slug' => $slug 
  5373. ) ); 
  5374.  
  5375. /** Logger 
  5376. ------------------------------------------------------------------------------------------------------------------*/ 
  5377. /** 
  5378. * @param string $id 
  5379. * @param bool $prefix_slug 
  5380. * @return FS_Logger 
  5381. */ 
  5382. function get_logger( $id = '', $prefix_slug = true ) { 
  5383. return FS_Logger::get_logger( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id ); 
  5384.  
  5385. /** 
  5386. * @param $id 
  5387. * @param bool $load_options 
  5388. * @param bool $prefix_slug 
  5389. * @return FS_Option_Manager 
  5390. */ 
  5391. function get_options_manager( $id, $load_options = false, $prefix_slug = true ) { 
  5392. return FS_Option_Manager::get_manager( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id, $load_options ); 
  5393.  
  5394. /** Security 
  5395. ------------------------------------------------------------------------------------------------------------------*/ 
  5396. private function _encrypt( $str ) { 
  5397. if ( is_null( $str ) ) { 
  5398. return null; 
  5399.  
  5400. return base64_encode( $str ); 
  5401.  
  5402. private function _decrypt( $str ) { 
  5403. if ( is_null( $str ) ) { 
  5404. return null; 
  5405.  
  5406. return base64_decode( $str ); 
  5407.  
  5408. /** 
  5409. * @author Vova Feldman (@svovaf) 
  5410. * @since 1.0.5 
  5411. * @param FS_Entity $entity 
  5412. * @return FS_Entity Return an encrypted clone entity. 
  5413. */ 
  5414. private function _encrypt_entity( FS_Entity $entity ) { 
  5415. $clone = clone $entity; 
  5416. $props = get_object_vars( $entity ); 
  5417.  
  5418. foreach ( $props as $key => $val ) { 
  5419. $clone->{$key} = $this->_encrypt( $val ); 
  5420.  
  5421. return $clone; 
  5422.  
  5423. /** 
  5424. * @author Vova Feldman (@svovaf) 
  5425. * @since 1.0.5 
  5426. * @param FS_Entity $entity 
  5427. * @return FS_Entity Return an decrypted clone entity. 
  5428. */ 
  5429. private function _decrypt_entity( FS_Entity $entity ) { 
  5430. $clone = clone $entity; 
  5431. $props = get_object_vars( $entity ); 
  5432.  
  5433. foreach ( $props as $key => $val ) { 
  5434. $clone->{$key} = $this->_decrypt( $val ); 
  5435.  
  5436. return $clone; 
  5437.  
  5438. /** 
  5439. * Tries to activate account based on POST params. 
  5440. * @author Vova Feldman (@svovaf) 
  5441. * @since 1.0.2 
  5442. */ 
  5443. function _activate_account() { 
  5444. if ( $this->is_registered() ) { 
  5445. // Already activated. 
  5446. return; 
  5447.  
  5448. self::_clean_admin_content_section(); 
  5449.  
  5450. if ( fs_request_is_action( 'activate' ) && fs_request_is_post() ) { 
  5451. // check_admin_referer( 'activate_' . $this->_plugin->public_key ); 
  5452.  
  5453. // Verify matching plugin details. 
  5454. if ( $this->_plugin->id != fs_request_get( 'plugin_id' ) || $this->_slug != fs_request_get( 'plugin_slug' ) ) { 
  5455. return; 
  5456.  
  5457. $user = new FS_User(); 
  5458. $user->id = fs_request_get( 'user_id' ); 
  5459. $user->public_key = fs_request_get( 'user_public_key' ); 
  5460. $user->secret_key = fs_request_get( 'user_secret_key' ); 
  5461. $user->email = fs_request_get( 'user_email' ); 
  5462. $user->first = fs_request_get( 'user_first' ); 
  5463. $user->last = fs_request_get( 'user_last' ); 
  5464. $user->is_verified = fs_request_get_bool( 'user_is_verified' ); 
  5465.  
  5466. $site = new FS_Site(); 
  5467. $site->id = fs_request_get( 'install_id' ); 
  5468. $site->public_key = fs_request_get( 'install_public_key' ); 
  5469. $site->secret_key = fs_request_get( 'install_secret_key' ); 
  5470. $site->plan->id = fs_request_get( 'plan_id' ); 
  5471. $site->plan->title = fs_request_get( 'plan_title' ); 
  5472. $site->plan->name = fs_request_get( 'plan_name' ); 
  5473.  
  5474. $plans = array(); 
  5475. $plans_data = json_decode( urldecode( fs_request_get( 'plans' ) ) ); 
  5476. foreach ( $plans_data as $p ) { 
  5477. $plans[] = new FS_Plugin_Plan( $p ); 
  5478.  
  5479. $this->_set_account( $user, $site, $plans ); 
  5480.  
  5481. // Reload the page with the keys. 
  5482. fs_redirect( $this->_get_admin_page_url() ); 
  5483.  
  5484. /** 
  5485. * @author Vova Feldman (@svovaf) 
  5486. * @since 1.0.7 
  5487. * @param string $email 
  5488. * @return FS_User|bool 
  5489. */ 
  5490. static function _get_user_by_email( $email ) { 
  5491. self::$_static_logger->entrance(); 
  5492.  
  5493. $email = trim( strtolower( $email ) ); 
  5494. $users = self::get_all_users(); 
  5495. if ( is_array( $users ) ) { 
  5496. foreach ( $users as $u ) { 
  5497. if ( $email === trim( strtolower( $u->email ) ) ) { 
  5498. return $u; 
  5499.  
  5500. return false; 
  5501.  
  5502. #---------------------------------------------------------------------------------- 
  5503. #region Account (Loading, Updates & Activation) 
  5504. #---------------------------------------------------------------------------------- 
  5505.  
  5506. /*** 
  5507. * Load account information (user + site). 
  5508. * @author Vova Feldman (@svovaf) 
  5509. * @since 1.0.1 
  5510. */ 
  5511. private function _load_account() { 
  5512. $this->_logger->entrance(); 
  5513.  
  5514. $this->do_action( 'before_account_load' ); 
  5515.  
  5516. $sites = self::get_all_sites(); 
  5517. $users = self::get_all_users(); 
  5518. $plans = self::get_all_plans(); 
  5519. $licenses = self::get_all_licenses(); 
  5520.  
  5521. if ( $this->_logger->is_on() && is_admin() ) { 
  5522. $this->_logger->log( 'sites = ' . var_export( $sites, true ) ); 
  5523. $this->_logger->log( 'users = ' . var_export( $users, true ) ); 
  5524. $this->_logger->log( 'plans = ' . var_export( $plans, true ) ); 
  5525. $this->_logger->log( 'licenses = ' . var_export( $licenses, true ) ); 
  5526.  
  5527. $site = isset( $sites[ $this->_slug ] ) ? $sites[ $this->_slug ] : false; 
  5528.  
  5529. if ( is_object( $site ) && 
  5530. is_numeric( $site->id ) && 
  5531. is_numeric( $site->user_id ) && 
  5532. is_object( $site->plan ) 
  5533. ) { 
  5534. // Load site. 
  5535. $this->_site = clone $site; 
  5536. $this->_site->plan = $this->_decrypt_entity( $this->_site->plan ); 
  5537.  
  5538. // Load relevant user. 
  5539. $this->_user = clone $users[ $this->_site->user_id ]; 
  5540.  
  5541. // Load plans. 
  5542. $this->_plans = $plans[ $this->_slug ]; 
  5543. if ( ! is_array( $this->_plans ) || empty( $this->_plans ) ) { 
  5544. $this->_sync_plans(); 
  5545. } else { 
  5546. for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) { 
  5547. if ( $this->_plans[ $i ] instanceof FS_Plugin_Plan ) { 
  5548. $this->_plans[ $i ] = $this->_decrypt_entity( $this->_plans[ $i ] ); 
  5549. } else { 
  5550. unset( $this->_plans[ $i ] ); 
  5551.  
  5552. // Load licenses. 
  5553. $this->_licenses = array(); 
  5554. if ( is_array( $licenses ) && 
  5555. isset( $licenses[ $this->_slug ] ) && 
  5556. isset( $licenses[ $this->_slug ][ $this->_user->id ] ) 
  5557. ) { 
  5558. $this->_licenses = $licenses[ $this->_slug ][ $this->_user->id ]; 
  5559.  
  5560. $this->_license = $this->_get_license_by_id( $this->_site->license_id ); 
  5561.  
  5562. if ( $this->_site->version != $this->get_plugin_version() ) { 
  5563. // If stored install version is different than current installed plugin version,  
  5564. // then update plugin version event. 
  5565. $this->update_plugin_version_event(); 
  5566.  
  5567. $this->_register_account_hooks(); 
  5568.  
  5569. /** 
  5570. * @author Vova Feldman (@svovaf) 
  5571. * @since 1.0.1 
  5572. * @param FS_User $user 
  5573. * @param FS_Site $site 
  5574. * @param bool|array $plans 
  5575. */ 
  5576. private function _set_account( FS_User $user, FS_Site $site, $plans = false ) { 
  5577. $site->slug = $this->_slug; 
  5578. $site->user_id = $user->id; 
  5579.  
  5580. $this->_site = $site; 
  5581. $this->_user = $user; 
  5582. if ( false !== $plans ) { 
  5583. $this->_plans = $plans; 
  5584.  
  5585. $this->send_install_update(); 
  5586.  
  5587. $this->_store_account(); 
  5588.  
  5589.  
  5590. /** 
  5591. * @author Vova Feldman (@svovaf) 
  5592. * @since 1.1.7.4 
  5593. * @param array $override_with 
  5594. * @return array 
  5595. */ 
  5596. function get_opt_in_params( $override_with = array() ) { 
  5597. $this->_logger->entrance(); 
  5598.  
  5599. $current_user = self::_get_current_wp_user(); 
  5600.  
  5601. $activation_action = $this->_slug . '_activate_new'; 
  5602. $return_url = $this->is_anonymous() ? 
  5603. // If skipped already, then return to the account page. 
  5604. $this->get_account_url( $activation_action, array(), false ) : 
  5605. // Return to the module's main page. 
  5606. $this->_get_admin_page_url( 
  5607. '',  
  5608. array( 'fs_action' => $activation_action ) 
  5609. ); 
  5610.  
  5611. $params = array( 
  5612. 'user_firstname' => $current_user->user_firstname,  
  5613. 'user_lastname' => $current_user->user_lastname,  
  5614. 'user_nickname' => $current_user->user_nicename,  
  5615. 'user_email' => $current_user->user_email,  
  5616. 'user_ip' => WP_FS__REMOTE_ADDR,  
  5617. 'plugin_slug' => $this->_slug,  
  5618. 'plugin_id' => $this->get_id(),  
  5619. 'plugin_public_key' => $this->get_public_key(),  
  5620. 'plugin_version' => $this->get_plugin_version(),  
  5621. 'return_url' => fs_nonce_url( $return_url, $activation_action ),  
  5622. 'account_url' => fs_nonce_url( $this->_get_admin_page_url( 
  5623. 'account',  
  5624. array( 'fs_action' => 'sync_user' ) 
  5625. ), 'sync_user' ),  
  5626. 'site_uid' => $this->get_anonymous_id(),  
  5627. 'site_url' => get_site_url(),  
  5628. 'site_name' => get_bloginfo( 'name' ),  
  5629. 'platform_version' => get_bloginfo( 'version' ),  
  5630. 'sdk_version' => $this->version,  
  5631. 'programming_language_version' => phpversion(),  
  5632. 'language' => get_bloginfo( 'language' ),  
  5633. 'charset' => get_bloginfo( 'charset' ),  
  5634. 'is_premium' => $this->is_premium(),  
  5635. 'is_active' => true,  
  5636. 'is_uninstalled' => false,  
  5637. ); 
  5638.  
  5639. if ( $this->is_pending_activation() && 
  5640. ! empty( $this->_storage->pending_license_key ) 
  5641. ) { 
  5642. $params['license_key'] = $this->_storage->pending_license_key; 
  5643.  
  5644. if ( WP_FS__SKIP_EMAIL_ACTIVATION && $this->has_secret_key() ) { 
  5645. // Even though rand() is known for its security issues,  
  5646. // the timestamp adds another layer of protection. 
  5647. // It would be very hard for an attacker to get the secret key form here. 
  5648. // Plus, this should never run in production since the secret should never 
  5649. // be included in the production version. 
  5650. $params['ts'] = WP_FS__SCRIPT_START_TIME; 
  5651. $params['salt'] = md5( uniqid( rand() ) ); 
  5652. $params['secure'] = md5( 
  5653. $params['ts'] . 
  5654. $params['salt'] . 
  5655. $this->get_secret_key() 
  5656. ); 
  5657.  
  5658. return array_merge( $params, $override_with ); 
  5659.  
  5660. /** 
  5661. * 1. If successful opt-in or pending activation returns the next page that the user should be redirected to. 
  5662. * 2. If there was an API error, return the API result. 
  5663. * @author Vova Feldman (@svovaf) 
  5664. * @since 1.1.7.4 
  5665. * @param string|bool $email 
  5666. * @param string|bool $first 
  5667. * @param string|bool $last 
  5668. * @param string|bool $license_key 
  5669. * @param bool $is_uninstall If "true", this means that the module is currently being uninstalled. 
  5670. * In this case, the user and site info will be sent to the server but no 
  5671. * data will be saved to the WP installation's database. 
  5672. * @param number|bool $trial_plan_id 
  5673. * @return string|object 
  5674. * @use WP_Error 
  5675. */ 
  5676. function opt_in( 
  5677. $email = false,  
  5678. $first = false,  
  5679. $last = false,  
  5680. $license_key = false,  
  5681. $is_uninstall = false,  
  5682. $trial_plan_id = false 
  5683. ) { 
  5684. $this->_logger->entrance(); 
  5685.  
  5686. if ( false === $email ) { 
  5687. $current_user = self::_get_current_wp_user(); 
  5688. $email = $current_user->user_email; 
  5689.  
  5690. /** 
  5691. * @since 1.2.1 If activating with license key, ignore the context-user 
  5692. * since the user will be automatically loaded from the license. 
  5693. */ 
  5694. if ( empty( $license_key ) ) { 
  5695. // Clean up pending license if opt-ing in again. 
  5696. $this->_storage->remove( 'pending_license_key' ); 
  5697.  
  5698. if ( ! $is_uninstall ) { 
  5699. $fs_user = Freemius::_get_user_by_email( $email ); 
  5700. if ( is_object( $fs_user ) && ! $this->is_pending_activation() ) { 
  5701. return $this->install_with_current_user( false, $trial_plan_id ); 
  5702.  
  5703. $user_info = array(); 
  5704. if ( ! empty( $email ) ) { 
  5705. $user_info['user_email'] = $email; 
  5706. if ( ! empty( $first ) ) { 
  5707. $user_info['user_firstname'] = $first; 
  5708. if ( ! empty( $last ) ) { 
  5709. $user_info['user_lastname'] = $last; 
  5710.  
  5711. $params = $this->get_opt_in_params( $user_info ); 
  5712.  
  5713. $filtered_license_key = false; 
  5714. if ( is_string( $license_key ) ) { 
  5715. $filtered_license_key = $this->apply_filters( 'license_key', $license_key ); 
  5716. $params['license_key'] = $filtered_license_key; 
  5717. } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) { 
  5718. $params['trial_plan_id'] = $trial_plan_id; 
  5719.  
  5720. if ( $is_uninstall ) { 
  5721. $params['uninstall_params'] = array( 
  5722. 'reason_id' => $this->_storage->uninstall_reason->id,  
  5723. 'reason_info' => $this->_storage->uninstall_reason->info 
  5724. ); 
  5725.  
  5726. $params['format'] = 'json'; 
  5727.  
  5728. $url = WP_FS__ADDRESS . '/action/service/user/install/'; 
  5729. if ( isset( $_COOKIE['XDEBUG_SESSION'] ) ) { 
  5730. $url = add_query_arg( 'XDEBUG_SESSION', 'PHPSTORM', $url ); 
  5731.  
  5732. $response = wp_remote_post( $url, array( 
  5733. 'method' => 'POST',  
  5734. 'body' => $params,  
  5735. 'timeout' => 15,  
  5736. ) ); 
  5737.  
  5738. if ( $response instanceof WP_Error ) { 
  5739. if ( 'https://' === substr( $url, 0, 8 ) && 
  5740. isset( $response->errors ) && 
  5741. isset( $response->errors['http_request_failed'] ) 
  5742. ) { 
  5743. $http_error = strtolower( $response->errors['http_request_failed'][0] ); 
  5744.  
  5745. if ( false !== strpos( $http_error, 'ssl' ) ) { 
  5746. // Failed due to old version of cURL or Open SSL (SSLv3 is not supported by CloudFlare). 
  5747. $url = 'http://' . substr( $url, 8 ); 
  5748.  
  5749. $response = wp_remote_post( $url, array( 
  5750. 'method' => 'POST',  
  5751. 'body' => $params,  
  5752. 'timeout' => 15,  
  5753. ) ); 
  5754.  
  5755. if ( is_wp_error( $response ) ) { 
  5756. /** 
  5757. * @var WP_Error $response 
  5758. */ 
  5759. $result = new stdClass(); 
  5760.  
  5761. $error_code = $response->get_error_code(); 
  5762. $error_type = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $error_code ) ) ); 
  5763.  
  5764. $result->error = (object) array( 
  5765. 'type' => $error_type,  
  5766. 'message' => $response->get_error_message(),  
  5767. 'code' => $error_code,  
  5768. 'http' => 402 
  5769. ); 
  5770.  
  5771. return $result; 
  5772.  
  5773. // Module is being uninstalled, don't handle the returned data. 
  5774. if ( $is_uninstall ) { 
  5775. return true; 
  5776.  
  5777. $decoded = @json_decode( $response['body'] ); 
  5778.  
  5779. if ( empty( $decoded ) ) { 
  5780. return false; 
  5781.  
  5782. if ( ! $this->is_api_result_object( $decoded ) ) { 
  5783. if ( ! empty( $params['license_key'] ) ) { 
  5784. // Pass the fully entered license key to the failure handler. 
  5785. $params['license_key'] = $license_key; 
  5786.  
  5787. return $is_uninstall ? 
  5788. $decoded : 
  5789. $this->apply_filters( 'after_install_failure', $decoded, $params ); 
  5790. } else if ( isset( $decoded->pending_activation ) && $decoded->pending_activation ) { 
  5791. // Pending activation, add message. 
  5792. return $this->set_pending_confirmation( 
  5793. true,  
  5794. false,  
  5795. $filtered_license_key,  
  5796. ! empty( $params['trial_plan_id'] ) 
  5797. ); 
  5798. } else if ( isset( $decoded->install_secret_key ) ) { 
  5799. return $this->install_with_new_user( 
  5800. $decoded->user_id,  
  5801. $decoded->user_public_key,  
  5802. $decoded->user_secret_key,  
  5803. $decoded->install_id,  
  5804. $decoded->install_public_key,  
  5805. $decoded->install_secret_key,  
  5806. false 
  5807. ); 
  5808.  
  5809. return $decoded; 
  5810.  
  5811. /** 
  5812. * Set user and site identities. 
  5813. * @author Vova Feldman (@svovaf) 
  5814. * @since 1.0.9 
  5815. * @param FS_User $user 
  5816. * @param FS_Site $site 
  5817. * @param bool $redirect 
  5818. * @return string If redirect is `false`, returns the next page the user should be redirected to. 
  5819. */ 
  5820. function setup_account( FS_User $user, FS_Site $site, $redirect = true ) { 
  5821. $this->_user = $user; 
  5822. $this->_site = $site; 
  5823.  
  5824. $this->_sync_plans(); 
  5825.  
  5826. $this->_enrich_site_plan( false ); 
  5827.  
  5828. $this->_set_account( $user, $site ); 
  5829.  
  5830. if ( $this->is_trial() ) { 
  5831. // Store trial plan information. 
  5832. $this->_enrich_site_trial_plan( true ); 
  5833.  
  5834. // If Freemius was OFF before, turn it on. 
  5835. $this->turn_on(); 
  5836.  
  5837. $this->do_action( 'after_account_connection', $user, $site ); 
  5838.  
  5839. if ( is_numeric( $site->license_id ) ) { 
  5840. $this->_license = $this->_get_license_by_id( $site->license_id ); 
  5841.  
  5842. $this->_admin_notices->remove_sticky( 'connect_account' ); 
  5843.  
  5844. if ( $this->is_pending_activation() ) { 
  5845. // Remove pending activation sticky notice (if still exist). 
  5846. $this->_admin_notices->remove_sticky( 'activation_pending' ); 
  5847.  
  5848. // Remove plugin from pending activation mode. 
  5849. unset( $this->_storage->is_pending_activation ); 
  5850.  
  5851. if ( ! $this->is_paying_or_trial() ) { 
  5852. $this->_admin_notices->add_sticky( 
  5853. sprintf( __fs( 'plugin-x-activation-message', $this->_slug ), '<b>' . $this->get_plugin_name() . '</b>' ),  
  5854. 'activation_complete' 
  5855. ); 
  5856.  
  5857. if ( $this->is_paying_or_trial() && ! $this->is_premium() ) { 
  5858. if ( $this->is_paying() ) { 
  5859. $this->_admin_notices->add_sticky( 
  5860. sprintf( 
  5861. __fs( 'activation-with-plan-x-message', $this->_slug ),  
  5862. $this->_site->plan->title 
  5863. ) . $this->get_complete_upgrade_instructions(),  
  5864. 'plan_upgraded',  
  5865. __fs( 'yee-haw', $this->_slug ) . '!' 
  5866. ); 
  5867. } else { 
  5868. $this->_admin_notices->add_sticky( 
  5869. sprintf( 
  5870. __fs( 'trial-started-message', $this->_slug ),  
  5871. '<i>' . $this->get_plugin_name() . '</i>' 
  5872. ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),  
  5873. 'trial_started',  
  5874. __fs( 'yee-haw', $this->_slug ) . '!' 
  5875. ); 
  5876.  
  5877. $this->_admin_notices->remove_sticky( array( 
  5878. 'trial_promotion',  
  5879. ) ); 
  5880.  
  5881. $plugin_id = fs_request_get( 'plugin_id', false ); 
  5882.  
  5883. // Store activation time ONLY for plugins (not add-ons). 
  5884. if ( ! is_numeric( $plugin_id ) || ( $plugin_id == $this->_plugin->id ) ) { 
  5885. $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME; 
  5886.  
  5887. $next_page = ''; 
  5888.  
  5889. if ( is_numeric( $plugin_id ) ) { 
  5890. if ( $plugin_id != $this->_plugin->id ) { 
  5891. // Add-on was installed - sync license right after install. 
  5892. $next_page = $this->_get_sync_license_url( $plugin_id ); 
  5893. } else { 
  5894. /** 
  5895. * @author Vova Feldman (@svovaf) 
  5896. * @since 1.1.9 If site installed with a valid license, sync license. 
  5897. */ 
  5898. if ( $this->is_paying() ) { 
  5899. $this->_sync_plugin_license( true ); 
  5900.  
  5901. // Reload the page with the keys. 
  5902. $next_page = $this->is_anonymous() ? 
  5903. // If user previously skipped, redirect to account page. 
  5904. $this->get_account_url() : 
  5905. $this->get_after_activation_url( 'after_connect_url' ); 
  5906.  
  5907. if ( ! empty( $next_page ) && $redirect ) { 
  5908. fs_redirect( $next_page ); 
  5909.  
  5910. return $next_page; 
  5911.  
  5912. /** 
  5913. * Install plugin with new user information after approval. 
  5914. * @author Vova Feldman (@svovaf) 
  5915. * @since 1.0.7 
  5916. */ 
  5917. function _install_with_new_user() { 
  5918. $this->_logger->entrance(); 
  5919.  
  5920. if ( $this->is_registered() ) { 
  5921. return; 
  5922.  
  5923. if ( fs_request_is_action( $this->_slug . '_activate_new' ) ) { 
  5924. // check_admin_referer( $this->_slug . '_activate_new' ); 
  5925.  
  5926. if ( fs_request_has( 'user_secret_key' ) ) { 
  5927. $this->install_with_new_user( 
  5928. fs_request_get( 'user_id' ),  
  5929. fs_request_get( 'user_public_key' ),  
  5930. fs_request_get( 'user_secret_key' ),  
  5931. fs_request_get( 'install_id' ),  
  5932. fs_request_get( 'install_public_key' ),  
  5933. fs_request_get( 'install_secret_key' ) 
  5934. ); 
  5935. } else if ( fs_request_has( 'pending_activation' ) ) { 
  5936. $this->set_pending_confirmation( fs_request_get( 'user_email' ), true ); 
  5937.  
  5938. /** 
  5939. * Install plugin with new user. 
  5940. * @author Vova Feldman (@svovaf) 
  5941. * @since 1.1.7.4 
  5942. * @param number $user_id 
  5943. * @param string $user_public_key 
  5944. * @param string $user_secret_key 
  5945. * @param number $install_id 
  5946. * @param string $install_public_key 
  5947. * @param string $install_secret_key 
  5948. * @param bool $redirect 
  5949. * @return string If redirect is `false`, returns the next page the user should be redirected to. 
  5950. */ 
  5951. private function install_with_new_user( 
  5952. $user_id,  
  5953. $user_public_key,  
  5954. $user_secret_key,  
  5955. $install_id,  
  5956. $install_public_key,  
  5957. $install_secret_key,  
  5958. $redirect = true 
  5959. ) { 
  5960. $user = new FS_User(); 
  5961. $user->id = $user_id; 
  5962. $user->public_key = $user_public_key; 
  5963. $user->secret_key = $user_secret_key; 
  5964.  
  5965. $this->_user = $user; 
  5966. $user_result = $this->get_api_user_scope()->get(); 
  5967. $user = new FS_User( $user_result ); 
  5968. $this->_user = $user; 
  5969.  
  5970. $site = new FS_Site(); 
  5971. $site->id = $install_id; 
  5972. $site->public_key = $install_public_key; 
  5973. $site->secret_key = $install_secret_key; 
  5974.  
  5975. $this->_site = $site; 
  5976. $site_result = $this->get_api_site_scope()->get(); 
  5977. $site = new FS_Site( $site_result ); 
  5978. $this->_site = $site; 
  5979.  
  5980. return $this->setup_account( $this->_user, $this->_site, $redirect ); 
  5981.  
  5982. /** 
  5983. * @author Vova Feldman (@svovaf) 
  5984. * @since 1.1.7.4 
  5985. * @param string|bool $email 
  5986. * @param bool $redirect 
  5987. * @param string|bool $license_key Since 1.2.1.5 
  5988. * @param bool $is_pending_trial Since 1.2.1.5 
  5989. * @return string Since 1.2.1.5 if $redirect is `false`, return the pending activation page. 
  5990. */ 
  5991. private function set_pending_confirmation( 
  5992. $email = false,  
  5993. $redirect = true,  
  5994. $license_key = false,  
  5995. $is_pending_trial = false 
  5996. ) { 
  5997. // Install must be activated via email since 
  5998. // user with the same email already exist. 
  5999. $this->_storage->is_pending_activation = true; 
  6000. $this->_add_pending_activation_notice( $email, $is_pending_trial ); 
  6001.  
  6002. if ( ! empty( $license_key ) ) { 
  6003. $this->_storage->pending_license_key = $license_key; 
  6004.  
  6005. // Remove the opt-in sticky notice. 
  6006. $this->_admin_notices->remove_sticky( array( 
  6007. 'connect_account',  
  6008. 'trial_promotion',  
  6009. ) ); 
  6010.  
  6011. $next_page = $this->get_after_activation_url( 'after_pending_connect_url' ); 
  6012.  
  6013. // Reload the page with with pending activation message. 
  6014. if ( $redirect ) { 
  6015. fs_redirect( $next_page ); 
  6016.  
  6017. return $next_page; 
  6018.  
  6019. /** 
  6020. * Install plugin with current logged WP user info. 
  6021. * @author Vova Feldman (@svovaf) 
  6022. * @since 1.0.7 
  6023. */ 
  6024. function _install_with_current_user() { 
  6025. $this->_logger->entrance(); 
  6026.  
  6027. if ( $this->is_registered() ) { 
  6028. return; 
  6029.  
  6030. if ( fs_request_is_action( $this->_slug . '_activate_existing' ) && fs_request_is_post() ) { 
  6031. // check_admin_referer( 'activate_existing_' . $this->_plugin->public_key ); 
  6032.  
  6033. /** 
  6034. * @author Vova Feldman (@svovaf) 
  6035. * @since 1.1.9 Add license key if given. 
  6036. */ 
  6037. $license_key = fs_request_get( 'license_secret_key' ); 
  6038.  
  6039. $this->install_with_current_user( $license_key ); 
  6040.  
  6041.  
  6042. /** 
  6043. * @author Vova Feldman (@svovaf) 
  6044. * @since 1.1.7.4 
  6045. * @param string|bool $license_key 
  6046. * @param number|bool $trial_plan_id 
  6047. * @param bool $redirect 
  6048. * @return string|object If redirect is `false`, returns the next page the user should be redirected to, or the 
  6049. * API error object if failed to install. 
  6050. */ 
  6051. private function install_with_current_user( 
  6052. $license_key = false,  
  6053. $trial_plan_id = false,  
  6054. $redirect = true 
  6055. ) { 
  6056. // Get current logged WP user. 
  6057. $current_user = self::_get_current_wp_user(); 
  6058.  
  6059. // Find the relevant FS user by the email. 
  6060. $user = self::_get_user_by_email( $current_user->user_email ); 
  6061.  
  6062. // We have to set the user before getting user scope API handler. 
  6063. $this->_user = $user; 
  6064.  
  6065. $extra_install_params = array( 
  6066. 'uid' => $this->get_anonymous_id(),  
  6067. ); 
  6068.  
  6069. if ( ! empty( $license_key ) ) { 
  6070. $filtered_license_key = $this->apply_filters( 'license_key', $license_key ); 
  6071. $extra_install_params['license_key'] = $filtered_license_key; 
  6072. } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) { 
  6073. $extra_install_params['trial_plan_id'] = $trial_plan_id; 
  6074.  
  6075. $args = $this->get_install_data_for_api( $extra_install_params, false, false ); 
  6076.  
  6077. // Install the plugin. 
  6078. $install = $this->get_api_user_scope()->call( 
  6079. "/plugins/{$this->get_id()}/installs.json",  
  6080. 'post',  
  6081. $args 
  6082. ); 
  6083.  
  6084. if ( ! $this->is_api_result_entity( $install ) ) { 
  6085. if ( ! empty( $args['license_key'] ) ) { 
  6086. // Pass full the fully entered license key to the failure handler. 
  6087. $args['license_key'] = $license_key; 
  6088.  
  6089. $install = $this->apply_filters( 'after_install_failure', $install, $args ); 
  6090.  
  6091. $this->_admin_notices->add( 
  6092. sprintf( __fs( 'could-not-activate-x', $this->_slug ), $this->get_plugin_name() ) . ' ' . 
  6093. __fs( 'contact-us-with-error-message', $this->_slug ) . ' ' . '<b>' . $install->error->message . '</b>',  
  6094. __fs( 'oops', $this->_slug ) . '...',  
  6095. 'error' 
  6096. ); 
  6097.  
  6098. if ( $redirect ) { 
  6099. fs_redirect( $this->get_activation_url( array( 'error' => $install->error->message ) ) ); 
  6100.  
  6101. return $install; 
  6102.  
  6103. $site = new FS_Site( $install ); 
  6104. $this->_site = $site; 
  6105.  
  6106. return $this->setup_account( $this->_user, $this->_site, $redirect ); 
  6107.  
  6108. /** 
  6109. * Tries to activate add-on account based on parent plugin info. 
  6110. * @author Vova Feldman (@svovaf) 
  6111. * @since 1.0.6 
  6112. * @param Freemius $parent_fs 
  6113. */ 
  6114. private function _activate_addon_account( Freemius $parent_fs ) { 
  6115. if ( $this->is_registered() ) { 
  6116. // Already activated. 
  6117. return; 
  6118.  
  6119. // Activate add-on with parent plugin credentials. 
  6120. $addon_install = $parent_fs->get_api_site_scope()->call( 
  6121. "/addons/{$this->_plugin->id}/installs.json",  
  6122. 'post',  
  6123. $this->get_install_data_for_api( array( 
  6124. 'uid' => $this->get_anonymous_id(),  
  6125. ), false, false ) 
  6126. ); 
  6127.  
  6128. if ( isset( $addon_install->error ) ) { 
  6129. $this->_admin_notices->add( 
  6130. sprintf( __fs( 'could-not-activate-x', $this->_slug ), $this->get_plugin_name() ) . ' ' . 
  6131. __fs( 'contact-us-with-error-message', $this->_slug ) . ' ' . '<b>' . $addon_install->error->message . '</b>',  
  6132. __fs( 'oops', $this->_slug ) . '...',  
  6133. 'error' 
  6134. ); 
  6135.  
  6136. return; 
  6137.  
  6138. // First of all, set site info - otherwise we won't 
  6139. // be able to invoke API calls. 
  6140. $this->_site = new FS_Site( $addon_install ); 
  6141.  
  6142. // Sync add-on plans. 
  6143. $this->_sync_plans(); 
  6144.  
  6145. // Get site's current plan. 
  6146. $this->_site->plan = $this->_get_plan_by_id( $this->_site->plan->id ); 
  6147.  
  6148. // Get user information based on parent's plugin. 
  6149. $user = $parent_fs->get_user(); 
  6150.  
  6151. $this->_set_account( $user, $this->_site ); 
  6152.  
  6153. // Sync licenses. 
  6154. $this->_sync_licenses(); 
  6155.  
  6156. // Try to activate premium license. 
  6157. $this->_activate_license( true ); 
  6158.  
  6159. #endregion 
  6160.  
  6161. #---------------------------------------------------------------------------------- 
  6162. #region Admin Menu Items 
  6163. #---------------------------------------------------------------------------------- 
  6164.  
  6165. private $_menu_items = array(); 
  6166.  
  6167. /** 
  6168. * @author Vova Feldman (@svovaf) 
  6169. * @since 1.0.7 
  6170. * @return string 
  6171. */ 
  6172. function get_menu_slug() { 
  6173. return $this->_menu->get_slug(); 
  6174.  
  6175. /** 
  6176. * @author Vova Feldman (@svovaf) 
  6177. * @since 1.0.9 
  6178. */ 
  6179. function _prepare_admin_menu() { 
  6180. // if ( ! $this->is_on() ) { 
  6181. // return; 
  6182. // } 
  6183.  
  6184. if ( ! $this->has_api_connectivity() && ! $this->is_enable_anonymous() ) { 
  6185. $this->_menu->remove_menu_item(); 
  6186. } else { 
  6187. $this->do_action( 'before_admin_menu_init' ); 
  6188.  
  6189. $this->add_menu_action(); 
  6190. $this->add_submenu_items(); 
  6191.  
  6192. /** 
  6193. * Admin dashboard menu items modifications. 
  6194. * NOTE: admin_menu action executed before admin_init. 
  6195. * @author Vova Feldman (@svovaf) 
  6196. * @since 1.0.7 
  6197. */ 
  6198. private function add_menu_action() { 
  6199. if ( $this->is_activation_mode() ) { 
  6200. $this->override_plugin_menu_with_activation(); 
  6201. } else { 
  6202. // If not registered try to install user. 
  6203. if ( ! $this->is_registered() && 
  6204. fs_request_is_action( $this->_slug . '_activate_new' ) 
  6205. ) { 
  6206. $this->_install_with_new_user(); 
  6207.  
  6208. /** 
  6209. * @author Vova Feldman (@svovaf) 
  6210. * @since 1.0.1 
  6211. */ 
  6212. function _redirect_on_clicked_menu_link() { 
  6213. $this->_logger->entrance(); 
  6214.  
  6215. $page = strtolower( isset( $_REQUEST['page'] ) ? $_REQUEST['page'] : '' ); 
  6216.  
  6217. $this->_logger->log( 'page = ' . $page ); 
  6218.  
  6219. foreach ( $this->_menu_items as $priority => $items ) { 
  6220. foreach ( $items as $item ) { 
  6221. if ( isset( $item['url'] ) ) { 
  6222. if ( $page === $this->_menu->get_slug( strtolower( $item['menu_slug'] ) ) ) { 
  6223. $this->_logger->log( 'Redirecting to ' . $item['url'] ); 
  6224.  
  6225. fs_redirect( $item['url'] ); 
  6226.  
  6227. /** 
  6228. * Remove plugin's all admin menu items & pages, and replace with activation page. 
  6229. * @author Vova Feldman (@svovaf) 
  6230. * @since 1.0.1 
  6231. */ 
  6232. private function override_plugin_menu_with_activation() { 
  6233. $this->_logger->entrance(); 
  6234.  
  6235. $hook = false; 
  6236.  
  6237. if ( $this->_menu->is_top_level() ) { 
  6238. $hook = $this->_menu->override_menu_item( array( &$this, '_connect_page_render' ) ); 
  6239.  
  6240. if ( false === $hook ) { 
  6241. // Create new menu item just for the opt-in. 
  6242. $hook = add_menu_page( 
  6243. $this->get_plugin_name(),  
  6244. $this->get_plugin_name(),  
  6245. 'manage_options',  
  6246. $this->_menu->get_slug(),  
  6247. array( &$this, '_connect_page_render' ) 
  6248. ); 
  6249. } else { 
  6250. $menus = array( $this->_menu->get_parent_slug() ); 
  6251.  
  6252. if ( $this->_menu->is_override_exact() ) { 
  6253. // Make sure the current page is matching the activation page. 
  6254. $activation_url = strtolower( $this->get_activation_url() ); 
  6255. $request_url = strtolower( $_SERVER['REQUEST_URI'] ); 
  6256.  
  6257. if ( parse_url( $activation_url, PHP_URL_PATH ) !== parse_url( $request_url, PHP_URL_PATH ) ) { 
  6258. // Different path - DO NOT OVERRIDE PAGE. 
  6259. return; 
  6260.  
  6261. $activation_url_params = array(); 
  6262. parse_str( parse_url( $activation_url, PHP_URL_QUERY ), $activation_url_params ); 
  6263.  
  6264. $request_url_params = array(); 
  6265. parse_str( parse_url( $request_url, PHP_URL_QUERY ), $request_url_params ); 
  6266.  
  6267.  
  6268. foreach ( $activation_url_params as $key => $val ) { 
  6269. if ( ! isset( $request_url_params[ $key ] ) || $val != $request_url_params[ $key ] ) { 
  6270. // Not matching query string - DO NOT OVERRIDE PAGE. 
  6271. return; 
  6272.  
  6273. foreach ( $menus as $parent_slug ) { 
  6274. $hook = $this->_menu->override_submenu_action( 
  6275. $parent_slug,  
  6276. $this->_menu->get_raw_slug(),  
  6277. array( &$this, '_connect_page_render' ) 
  6278. ); 
  6279.  
  6280. if ( false !== $hook ) { 
  6281. // Found plugin's submenu item. 
  6282. break; 
  6283.  
  6284. if ( $this->_menu->is_activation_page() ) { 
  6285. // Clean admin page from distracting content. 
  6286. self::_clean_admin_content_section(); 
  6287.  
  6288. if ( false !== $hook ) { 
  6289. if ( fs_request_is_action( $this->_slug . '_activate_existing' ) ) { 
  6290. add_action( "load-$hook", array( &$this, '_install_with_current_user' ) ); 
  6291. } else if ( fs_request_is_action( $this->_slug . '_activate_new' ) ) { 
  6292. add_action( "load-$hook", array( &$this, '_install_with_new_user' ) ); 
  6293.  
  6294. /** 
  6295. * @author Leo Fajardo (leorw) 
  6296. * @since 1.2.1 
  6297. * return string 
  6298. */ 
  6299. function get_top_level_menu_capability() { 
  6300. global $menu; 
  6301.  
  6302. $top_level_menu_slug = $this->get_top_level_menu_slug(); 
  6303.  
  6304. foreach ( $menu as $menu_info ) { 
  6305. /** 
  6306. * The second element in the menu info array is the capability/role that has access to the menu and the 
  6307. * third element is the menu slug. 
  6308. */ 
  6309. if ( $menu_info[2] === $top_level_menu_slug ) { 
  6310. return $menu_info[1]; 
  6311.  
  6312. return 'read'; 
  6313.  
  6314. /** 
  6315. * @author Vova Feldman (@svovaf) 
  6316. * @since 1.0.0 
  6317. * @return string 
  6318. */ 
  6319. private function get_top_level_menu_slug() { 
  6320. return ( $this->is_addon() ? 
  6321. $this->get_parent_instance()->_menu->get_top_level_menu_slug() : 
  6322. $this->_menu->get_top_level_menu_slug() ); 
  6323.  
  6324. /** 
  6325. * Add default Freemius menu items. 
  6326. * @author Vova Feldman (@svovaf) 
  6327. * @since 1.0.0 
  6328. */ 
  6329. private function add_submenu_items() { 
  6330. $this->_logger->entrance(); 
  6331.  
  6332. if ( ! $this->is_addon() ) { 
  6333. if ( ! $this->is_activation_mode() ) { 
  6334. if ( $this->is_registered() ) { 
  6335. // Add user account page. 
  6336. $this->add_submenu_item( 
  6337. __fs( 'account', $this->_slug ),  
  6338. array( &$this, '_account_page_render' ),  
  6339. $this->get_plugin_name() . ' – ' . __fs( 'account', $this->_slug ),  
  6340. 'manage_options',  
  6341. 'account',  
  6342. array( &$this, '_account_page_load' ),  
  6343. WP_FS__DEFAULT_PRIORITY,  
  6344. $this->_menu->is_submenu_item_visible( 'account' ) 
  6345. ); 
  6346.  
  6347. // Add contact page. 
  6348. $this->add_submenu_item( 
  6349. __fs( 'contact-us', $this->_slug ),  
  6350. array( &$this, '_contact_page_render' ),  
  6351. $this->get_plugin_name() . ' – ' . __fs( 'contact-us', $this->_slug ),  
  6352. 'manage_options',  
  6353. 'contact',  
  6354. 'Freemius::_clean_admin_content_section',  
  6355. WP_FS__DEFAULT_PRIORITY,  
  6356. $this->_menu->is_submenu_item_visible( 'contact' ) 
  6357. ); 
  6358.  
  6359. if ( $this->has_addons() ) { 
  6360. $this->add_submenu_item( 
  6361. __fs( 'add-ons', $this->_slug ),  
  6362. array( &$this, '_addons_page_render' ),  
  6363. $this->get_plugin_name() . ' – ' . __fs( 'add-ons', $this->_slug ),  
  6364. 'manage_options',  
  6365. 'addons',  
  6366. array( &$this, '_addons_page_load' ),  
  6367. WP_FS__LOWEST_PRIORITY - 1,  
  6368. $this->_menu->is_submenu_item_visible( 'addons' ) 
  6369. ); 
  6370.  
  6371. $show_pricing = ( $this->has_paid_plan() && $this->_menu->is_submenu_item_visible( 'pricing' ) ); 
  6372. // If user don't have paid plans, add pricing page 
  6373. // to support add-ons checkout but don't add the submenu item. 
  6374. // || (isset( $_GET['page'] ) && $this->_menu->get_slug( 'pricing' ) == $_GET['page']); 
  6375.  
  6376. $pricing_cta_slug = 'upgrade'; 
  6377. $pricing_class = 'upgrade-mode'; 
  6378. if ( $show_pricing ) { 
  6379. if ( $this->_admin_notices->has_sticky( 'trial_promotion' ) && 
  6380. ! $this->is_paying_or_trial() 
  6381. ) { 
  6382. // If running a trial promotion, modify the pricing to load the trial. 
  6383. $pricing_cta_slug = 'start-trial'; 
  6384. $pricing_class = 'trial-mode'; 
  6385. } else if ( $this->is_paying() ) { 
  6386. $pricing_cta_slug = 'pricing'; 
  6387. $pricing_class = ''; 
  6388.  
  6389. // Add upgrade/pricing page. 
  6390. $this->add_submenu_item( 
  6391. __fs( $pricing_cta_slug, $this->_slug ) . '  ➤',  
  6392. array( &$this, '_pricing_page_render' ),  
  6393. $this->get_plugin_name() . ' – ' . __fs( 'pricing', $this->_slug ),  
  6394. 'manage_options',  
  6395. 'pricing',  
  6396. 'Freemius::_clean_admin_content_section',  
  6397. WP_FS__LOWEST_PRIORITY,  
  6398. $show_pricing,  
  6399. $pricing_class 
  6400. ); 
  6401.  
  6402.  
  6403. if ( 0 < count( $this->_menu_items ) ) { 
  6404. if ( ! $this->_menu->is_top_level() ) { 
  6405. fs_enqueue_local_style( 'fs_common', '/admin/common.css' ); 
  6406.  
  6407. // Append submenu items right after the plugin's submenu item. 
  6408. $this->order_sub_submenu_items(); 
  6409. } else { 
  6410. // Append submenu items. 
  6411. $this->embed_submenu_items(); 
  6412.  
  6413. /** 
  6414. * Moved the actual submenu item additions to a separated function,  
  6415. * in order to support sub-submenu items when the plugin's settings 
  6416. * only have a submenu and not top-level menu item. 
  6417. * @author Vova Feldman (@svovaf) 
  6418. * @since 1.1.4 
  6419. */ 
  6420. private function embed_submenu_items() { 
  6421. $item_template = $this->_menu->is_top_level() ? 
  6422. '<span class="fs-submenu-item %s %s %s">%s</span>' : 
  6423. '<span class="fs-submenu-item fs-sub %s %s %s">%s</span>'; 
  6424.  
  6425. $top_level_menu_capability = $this->get_top_level_menu_capability(); 
  6426.  
  6427. ksort( $this->_menu_items ); 
  6428.  
  6429. foreach ( $this->_menu_items as $priority => $items ) { 
  6430. foreach ( $items as $item ) { 
  6431. $capability = ( ! empty( $item['capability'] ) ? $item['capability'] : $top_level_menu_capability ); 
  6432.  
  6433. $menu_item = sprintf( 
  6434. $item_template,  
  6435. $this->_slug,  
  6436. $item['menu_slug'],  
  6437. ! empty( $item['class'] ) ? $item['class'] : '',  
  6438. $item['menu_title'] 
  6439. ); 
  6440.  
  6441. $menu_slug = $this->_menu->get_slug( $item['menu_slug'] ); 
  6442.  
  6443. if ( ! isset( $item['url'] ) ) { 
  6444. $hook = add_submenu_page( 
  6445. $item['show_submenu'] ? 
  6446. $this->get_top_level_menu_slug() : 
  6447. null,  
  6448. $item['page_title'],  
  6449. $menu_item,  
  6450. $capability,  
  6451. $menu_slug,  
  6452. $item['render_function'] 
  6453. ); 
  6454.  
  6455. if ( false !== $item['before_render_function'] ) { 
  6456. add_action( "load-$hook", $item['before_render_function'] ); 
  6457. } else { 
  6458. add_submenu_page( 
  6459. $this->get_top_level_menu_slug(),  
  6460. $item['page_title'],  
  6461. $menu_item,  
  6462. $capability,  
  6463. $menu_slug,  
  6464. array( $this, '' ) 
  6465. ); 
  6466.  
  6467. /** 
  6468. * Re-order the submenu items so all Freemius added new submenu items 
  6469. * are added right after the plugin's settings submenu item. 
  6470. * @author Vova Feldman (@svovaf) 
  6471. * @since 1.1.4 
  6472. */ 
  6473. private function order_sub_submenu_items() { 
  6474. global $submenu; 
  6475.  
  6476. $menu_slug = $this->_menu->get_top_level_menu_slug(); 
  6477.  
  6478. /** 
  6479. * Before "admin_menu" fires, WordPress will loop over the default submenus and remove pages for which the user 
  6480. * does not have permissions. So in case a plugin does not have top-level menu but does have submenus under any 
  6481. * of the default menus, only users that have the right role can access its sub-submenus (Account, Contact Us,  
  6482. * Support Forum, etc.) since $submenu[ $menu_slug ] will be empty if the user doesn't have permission. 
  6483. * In case a plugin does not have submenus under any of the default menus but does have submenus under the menu 
  6484. * of another plugin, only users that have the right role can access its sub-submenus since we will use the 
  6485. * capability needed to access the parent menu as the capability for the submenus that we will add. 
  6486. */ 
  6487. if ( empty( $submenu[ $menu_slug ] ) ) { 
  6488. return; 
  6489.  
  6490. $top_level_menu = &$submenu[ $menu_slug ]; 
  6491.  
  6492. $all_submenu_items_after = array(); 
  6493.  
  6494. $found_submenu_item = false; 
  6495.  
  6496. foreach ( $top_level_menu as $submenu_id => $meta ) { 
  6497. if ( $found_submenu_item ) { 
  6498. // Remove all submenu items after the plugin's submenu item. 
  6499. $all_submenu_items_after[] = $meta; 
  6500. unset( $top_level_menu[ $submenu_id ] ); 
  6501.  
  6502. if ( $this->_menu->get_raw_slug() === $meta[2] ) { 
  6503. // Found the submenu item, put all below. 
  6504. $found_submenu_item = true; 
  6505. continue; 
  6506.  
  6507. // Embed all plugin's new submenu items. 
  6508. $this->embed_submenu_items(); 
  6509.  
  6510. // Start with specially high number to make sure it's appended. 
  6511. $i = max( 10000, max( array_keys( $top_level_menu ) ) + 1 ); 
  6512. foreach ( $all_submenu_items_after as $meta ) { 
  6513. $top_level_menu[ $i ] = $meta; 
  6514. $i ++; 
  6515.  
  6516. // Sort submenu items. 
  6517. ksort( $top_level_menu ); 
  6518.  
  6519. /** 
  6520. * Displays the Support Forum link when enabled. 
  6521. * Can be filtered like so: 
  6522. * function _fs_show_support_menu( $is_visible, $menu_id ) { 
  6523. * if ( 'support' === $menu_id ) { 
  6524. * return _fs->is_registered(); 
  6525. * } 
  6526. * return $is_visible; 
  6527. * } 
  6528. * _fs()->add_filter('is_submenu_visible', '_fs_show_support_menu', 10, 2); 
  6529. */ 
  6530. function _add_default_submenu_items() { 
  6531. if ( ! $this->is_on() ) { 
  6532. return; 
  6533.  
  6534. if ( ! $this->is_activation_mode() ) { 
  6535. if ( $this->_menu->is_submenu_item_visible( 'support' ) ) { 
  6536. $this->add_submenu_link_item( 
  6537. $this->apply_filters( 'support_forum_submenu', __fs( 'support-forum', $this->_slug ) ),  
  6538. $this->apply_filters( 'support_forum_url', 'https://wordpress.org/support/plugin/' . $this->_slug ),  
  6539. 'wp-support-forum',  
  6540. null,  
  6541. 50 
  6542. ); 
  6543.  
  6544. /** 
  6545. * @author Vova Feldman (@svovaf) 
  6546. * @since 1.0.1 
  6547. * @param string $menu_title 
  6548. * @param callable $render_function 
  6549. * @param bool|string $page_title 
  6550. * @param string $capability 
  6551. * @param bool|string $menu_slug 
  6552. * @param bool|callable $before_render_function 
  6553. * @param int $priority 
  6554. * @param bool $show_submenu 
  6555. * @param string $class Since 1.2.1.5 can add custom classes to menu items. 
  6556. */ 
  6557. function add_submenu_item( 
  6558. $menu_title,  
  6559. $render_function,  
  6560. $page_title = false,  
  6561. $capability = 'manage_options',  
  6562. $menu_slug = false,  
  6563. $before_render_function = false,  
  6564. $priority = WP_FS__DEFAULT_PRIORITY,  
  6565. $show_submenu = true,  
  6566. $class = '' 
  6567. ) { 
  6568. $this->_logger->entrance( 'Title = ' . $menu_title ); 
  6569.  
  6570. if ( $this->is_addon() ) { 
  6571. $parent_fs = $this->get_parent_instance(); 
  6572.  
  6573. if ( is_object( $parent_fs ) ) { 
  6574. $parent_fs->add_submenu_item( 
  6575. $menu_title,  
  6576. $render_function,  
  6577. $page_title,  
  6578. $capability,  
  6579. $menu_slug,  
  6580. $before_render_function,  
  6581. $priority,  
  6582. $show_submenu,  
  6583. $class 
  6584. ); 
  6585.  
  6586. return; 
  6587.  
  6588. if ( ! isset( $this->_menu_items[ $priority ] ) ) { 
  6589. $this->_menu_items[ $priority ] = array(); 
  6590.  
  6591. $this->_menu_items[ $priority ][] = array( 
  6592. 'page_title' => is_string( $page_title ) ? $page_title : $menu_title,  
  6593. 'menu_title' => $menu_title,  
  6594. 'capability' => $capability,  
  6595. 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),  
  6596. 'render_function' => $render_function,  
  6597. 'before_render_function' => $before_render_function,  
  6598. 'show_submenu' => $show_submenu,  
  6599. 'class' => $class,  
  6600. ); 
  6601.  
  6602. /** 
  6603. * @author Vova Feldman (@svovaf) 
  6604. * @since 1.0.1 
  6605. * @param string $menu_title 
  6606. * @param string $url 
  6607. * @param bool $menu_slug 
  6608. * @param string $capability 
  6609. * @param int $priority 
  6610. */ 
  6611. function add_submenu_link_item( 
  6612. $menu_title,  
  6613. $url,  
  6614. $menu_slug = false,  
  6615. $capability = 'read',  
  6616. $priority = WP_FS__DEFAULT_PRIORITY 
  6617. ) { 
  6618. $this->_logger->entrance( 'Title = ' . $menu_title . '; Url = ' . $url ); 
  6619.  
  6620. if ( $this->is_addon() ) { 
  6621. $parent_fs = $this->get_parent_instance(); 
  6622.  
  6623. if ( is_object( $parent_fs ) ) { 
  6624. $parent_fs->add_submenu_link_item( 
  6625. $menu_title,  
  6626. $url,  
  6627. $menu_slug,  
  6628. $capability,  
  6629. $priority 
  6630. ); 
  6631.  
  6632. return; 
  6633.  
  6634. if ( ! isset( $this->_menu_items[ $priority ] ) ) { 
  6635. $this->_menu_items[ $priority ] = array(); 
  6636.  
  6637. $this->_menu_items[ $priority ][] = array( 
  6638. 'menu_title' => $menu_title,  
  6639. 'capability' => $capability,  
  6640. 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),  
  6641. 'url' => $url,  
  6642. 'page_title' => $menu_title,  
  6643. 'render_function' => 'fs_dummy',  
  6644. 'before_render_function' => '',  
  6645. ); 
  6646.  
  6647. #endregion ------------------------------------------------------------------ 
  6648.  
  6649. /** Actions / Hooks / Filters 
  6650. ------------------------------------------------------------------------------------------------------------------*/ 
  6651. /** 
  6652. * @author Vova Feldman (@svovaf) 
  6653. * @since 1.1.7 
  6654. * @param string $tag 
  6655. * @return string 
  6656. */ 
  6657. public function get_action_tag( $tag ) { 
  6658. return "fs_{$tag}_{$this->_slug}"; 
  6659.  
  6660. /** 
  6661. * @author Vova Feldman (@svovaf) 
  6662. * @since 1.2.1 
  6663. * @param string $tag 
  6664. * @return string 
  6665. */ 
  6666. private function get_ajax_action_tag( $tag ) { 
  6667. return 'wp_ajax_' . $this->get_action_tag( $tag ); 
  6668.  
  6669. /** 
  6670. * Do action, specific for the current context plugin. 
  6671. * @author Vova Feldman (@svovaf) 
  6672. * @since 1.0.1 
  6673. * @param string $tag The name of the action to be executed. 
  6674. * @param mixed $arg, ... Optional. Additional arguments which are passed on to the 
  6675. * functions hooked to the action. Default empty. 
  6676. * @uses do_action() 
  6677. */ 
  6678. function do_action( $tag, $arg = '' ) { 
  6679. $this->_logger->entrance( $tag ); 
  6680.  
  6681. $args = func_get_args(); 
  6682.  
  6683. call_user_func_array( 'do_action', array_merge( 
  6684. array( $this->get_action_tag( $tag ) ),  
  6685. array_slice( $args, 1 ) ) 
  6686. ); 
  6687.  
  6688. /** 
  6689. * Add action, specific for the current context plugin. 
  6690. * @author Vova Feldman (@svovaf) 
  6691. * @since 1.0.1 
  6692. * @param string $tag 
  6693. * @param callable $function_to_add 
  6694. * @param int $priority 
  6695. * @param int $accepted_args 
  6696. * @uses add_action() 
  6697. */ 
  6698. function add_action( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) { 
  6699. $this->_logger->entrance( $tag ); 
  6700.  
  6701. add_action( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args ); 
  6702.  
  6703. /** 
  6704. * Add AJAX action, specific for the current context plugin. 
  6705. * @author Vova Feldman (@svovaf) 
  6706. * @since 1.2.1 
  6707. * @param string $tag 
  6708. * @param callable $function_to_add 
  6709. * @param int $priority 
  6710. * @param int $accepted_args 
  6711. * @uses add_action() 
  6712. * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching. 
  6713. */ 
  6714. function add_ajax_action( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) { 
  6715. $this->_logger->entrance( $tag ); 
  6716.  
  6717. if ( ! $this->is_ajax_action( $tag ) ) { 
  6718. return false; 
  6719.  
  6720. add_action( $this->get_ajax_action_tag( $tag ), $function_to_add, $priority, $accepted_args ); 
  6721.  
  6722. $this->_logger->info( "$tag AJAX callback action added." ); 
  6723.  
  6724. return true; 
  6725.  
  6726. /** 
  6727. * Send a JSON response back to an Ajax request. 
  6728. * @author Vova Feldman (@svovaf) 
  6729. * @since 1.2.1.5 
  6730. * @param mixed $response 
  6731. */ 
  6732. function shoot_ajax_response( $response ) { 
  6733. wp_send_json( $response ); 
  6734.  
  6735. /** 
  6736. * Send a JSON response back to an Ajax request, indicating success. 
  6737. * @author Vova Feldman (@svovaf) 
  6738. * @since 1.2.1.5 
  6739. * @param mixed $data Data to encode as JSON, then print and exit. 
  6740. */ 
  6741. function shoot_ajax_success( $data = null ) { 
  6742. wp_send_json_success( $data ); 
  6743.  
  6744. /** 
  6745. * Send a JSON response back to an Ajax request, indicating failure. 
  6746. * @author Vova Feldman (@svovaf) 
  6747. * @since 1.2.1.5 
  6748. * @param mixed $error Optional error message. 
  6749. */ 
  6750. function shoot_ajax_failure( $error = '' ) { 
  6751. $result = array( 'success' => false ); 
  6752. if ( ! empty( $error ) ) { 
  6753. $result['error'] = $error; 
  6754.  
  6755. wp_send_json( $result ); 
  6756.  
  6757. /** 
  6758. * Apply filter, specific for the current context plugin. 
  6759. * @author Vova Feldman (@svovaf) 
  6760. * @since 1.0.9 
  6761. * @param string $tag The name of the filter hook. 
  6762. * @param mixed $value The value on which the filters hooked to `$tag` are applied on. 
  6763. * @return mixed The filtered value after all hooked functions are applied to it. 
  6764. * @uses apply_filters() 
  6765. */ 
  6766. function apply_filters( $tag, $value ) { 
  6767. $this->_logger->entrance( $tag ); 
  6768.  
  6769. $args = func_get_args(); 
  6770. array_unshift( $args, $this->_slug ); 
  6771.  
  6772. return call_user_func_array( 'fs_apply_filter', $args ); 
  6773.  
  6774. /** 
  6775. * Add filter, specific for the current context plugin. 
  6776. * @author Vova Feldman (@svovaf) 
  6777. * @since 1.0.9 
  6778. * @param string $tag 
  6779. * @param callable $function_to_add 
  6780. * @param int $priority 
  6781. * @param int $accepted_args 
  6782. * @uses add_filter() 
  6783. */ 
  6784. function add_filter( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) { 
  6785. $this->_logger->entrance( $tag ); 
  6786.  
  6787. add_filter( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args ); 
  6788.  
  6789. /** 
  6790. * Check if has filter. 
  6791. * @author Vova Feldman (@svovaf) 
  6792. * @since 1.1.4 
  6793. * @param string $tag 
  6794. * @param callable|bool $function_to_check Optional. The callback to check for. Default false. 
  6795. * @return false|int 
  6796. * @uses has_filter() 
  6797. */ 
  6798. function has_filter( $tag, $function_to_check = false ) { 
  6799. $this->_logger->entrance( $tag ); 
  6800.  
  6801. return has_filter( $this->get_action_tag( $tag ), $function_to_check ); 
  6802.  
  6803. /** 
  6804. * Override default i18n text phrases. 
  6805. * @author Vova Feldman (@svovaf) 
  6806. * @since 1.1.6 
  6807. * @param string[] string $key_value 
  6808. * @uses fs_override_i18n() 
  6809. */ 
  6810. function override_i18n( $key_value ) { 
  6811. fs_override_i18n( $key_value, $this->_slug ); 
  6812.  
  6813. /** Account Page 
  6814. ------------------------------------------------------------------------------------------------------------------*/ 
  6815. /** 
  6816. * Update site information. 
  6817. * @author Vova Feldman (@svovaf) 
  6818. * @since 1.0.1 
  6819. * @param bool $store Flush to Database if true. 
  6820. */ 
  6821. private function _store_site( $store = true ) { 
  6822. $this->_logger->entrance(); 
  6823.  
  6824. if ( empty( $this->_site->id ) ) { 
  6825. $this->_logger->error( "Empty install ID, can't store site." ); 
  6826.  
  6827. return; 
  6828.  
  6829. $encrypted_site = clone $this->_site; 
  6830. $encrypted_site->plan = $this->_encrypt_entity( $this->_site->plan ); 
  6831.  
  6832. $sites = self::get_all_sites(); 
  6833. $sites[ $this->_slug ] = $encrypted_site; 
  6834. self::$_accounts->set_option( 'sites', $sites, $store ); 
  6835.  
  6836. /** 
  6837. * Update plugin's plans information. 
  6838. * @author Vova Feldman (@svovaf) 
  6839. * @since 1.0.2 
  6840. * @param bool $store Flush to Database if true. 
  6841. */ 
  6842. private function _store_plans( $store = true ) { 
  6843. $this->_logger->entrance(); 
  6844.  
  6845. $plans = self::get_all_plans(); 
  6846.  
  6847. // Copy plans. 
  6848. $encrypted_plans = array(); 
  6849. for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) { 
  6850. $encrypted_plans[] = $this->_encrypt_entity( $this->_plans[ $i ] ); 
  6851.  
  6852. $plans[ $this->_slug ] = $encrypted_plans; 
  6853. self::$_accounts->set_option( 'plans', $plans, $store ); 
  6854.  
  6855. /** 
  6856. * Update user's plugin licenses. 
  6857. * @author Vova Feldman (@svovaf) 
  6858. * @since 1.0.5 
  6859. * @param bool $store 
  6860. * @param string|bool $plugin_slug 
  6861. * @param FS_Plugin_License[] $licenses 
  6862. */ 
  6863. private function _store_licenses( $store = true, $plugin_slug = false, $licenses = array() ) { 
  6864. $this->_logger->entrance(); 
  6865.  
  6866. $all_licenses = self::get_all_licenses(); 
  6867.  
  6868. if ( ! is_string( $plugin_slug ) ) { 
  6869. $plugin_slug = $this->_slug; 
  6870. $licenses = $this->_licenses; 
  6871.  
  6872. if ( ! isset( $all_licenses[ $plugin_slug ] ) ) { 
  6873. $all_licenses[ $plugin_slug ] = array(); 
  6874.  
  6875. $all_licenses[ $plugin_slug ][ $this->_user->id ] = $licenses; 
  6876.  
  6877. self::$_accounts->set_option( 'licenses', $all_licenses, $store ); 
  6878.  
  6879. /** 
  6880. * Update user information. 
  6881. * @author Vova Feldman (@svovaf) 
  6882. * @since 1.0.1 
  6883. * @param bool $store Flush to Database if true. 
  6884. */ 
  6885. private function _store_user( $store = true ) { 
  6886. $this->_logger->entrance(); 
  6887.  
  6888. if ( empty( $this->_user->id ) ) { 
  6889. $this->_logger->error( "Empty user ID, can't store user." ); 
  6890.  
  6891. return; 
  6892.  
  6893. $users = self::get_all_users(); 
  6894. $users[ $this->_user->id ] = $this->_user; 
  6895. self::$_accounts->set_option( 'users', $users, $store ); 
  6896.  
  6897. /** 
  6898. * Update new updates information. 
  6899. * @author Vova Feldman (@svovaf) 
  6900. * @since 1.0.4 
  6901. * @param FS_Plugin_Tag|null $update 
  6902. * @param bool $store Flush to Database if true. 
  6903. * @param bool|number $plugin_id 
  6904. */ 
  6905. private function _store_update( $update, $store = true, $plugin_id = false ) { 
  6906. $this->_logger->entrance(); 
  6907.  
  6908. if ( $update instanceof FS_Plugin_Tag ) { 
  6909. $update->updated = time(); 
  6910.  
  6911. if ( ! is_numeric( $plugin_id ) ) { 
  6912. $plugin_id = $this->_plugin->id; 
  6913.  
  6914. $updates = self::get_all_updates(); 
  6915. $updates[ $plugin_id ] = $update; 
  6916. self::$_accounts->set_option( 'updates', $updates, $store ); 
  6917.  
  6918. /** 
  6919. * Update new updates information. 
  6920. * @author Vova Feldman (@svovaf) 
  6921. * @since 1.0.6 
  6922. * @param FS_Plugin[] $plugin_addons 
  6923. * @param bool $store Flush to Database if true. 
  6924. */ 
  6925. private function _store_addons( $plugin_addons, $store = true ) { 
  6926. $this->_logger->entrance(); 
  6927.  
  6928. $addons = self::get_all_addons(); 
  6929. $addons[ $this->_plugin->id ] = $plugin_addons; 
  6930. self::$_accounts->set_option( 'addons', $addons, $store ); 
  6931.  
  6932. /** 
  6933. * Delete plugin's associated add-ons. 
  6934. * @author Vova Feldman (@svovaf) 
  6935. * @since 1.0.8 
  6936. * @param bool $store 
  6937. * @return bool 
  6938. */ 
  6939. private function _delete_account_addons( $store = true ) { 
  6940. $all_addons = self::get_all_account_addons(); 
  6941.  
  6942. if ( ! isset( $all_addons[ $this->_plugin->id ] ) ) { 
  6943. return false; 
  6944.  
  6945. unset( $all_addons[ $this->_plugin->id ] ); 
  6946.  
  6947. self::$_accounts->set_option( 'account_addons', $all_addons, $store ); 
  6948.  
  6949. return true; 
  6950.  
  6951. /** 
  6952. * Update account add-ons list. 
  6953. * @author Vova Feldman (@svovaf) 
  6954. * @since 1.0.6 
  6955. * @param FS_Plugin[] $addons 
  6956. * @param bool $store Flush to Database if true. 
  6957. */ 
  6958. private function _store_account_addons( $addons, $store = true ) { 
  6959. $this->_logger->entrance(); 
  6960.  
  6961. $all_addons = self::get_all_account_addons(); 
  6962. $all_addons[ $this->_plugin->id ] = $addons; 
  6963. self::$_accounts->set_option( 'account_addons', $all_addons, $store ); 
  6964.  
  6965. /** 
  6966. * Store account params in the Database. 
  6967. * @author Vova Feldman (@svovaf) 
  6968. * @since 1.0.1 
  6969. */ 
  6970. private function _store_account() { 
  6971. $this->_logger->entrance(); 
  6972.  
  6973. $this->_store_site( false ); 
  6974. $this->_store_user( false ); 
  6975. $this->_store_plans( false ); 
  6976. $this->_store_licenses( false ); 
  6977.  
  6978. self::$_accounts->store(); 
  6979.  
  6980. /** 
  6981. * Sync user's information. 
  6982. * @author Vova Feldman (@svovaf) 
  6983. * @since 1.0.3 
  6984. * @uses FS_Api 
  6985. */ 
  6986. private function _handle_account_user_sync() { 
  6987. $this->_logger->entrance(); 
  6988.  
  6989. $api = $this->get_api_user_scope(); 
  6990.  
  6991. // Get user's information. 
  6992. $user = $api->get( '/', true ); 
  6993.  
  6994. if ( isset( $user->id ) ) { 
  6995. $this->_user->first = $user->first; 
  6996. $this->_user->last = $user->last; 
  6997. $this->_user->email = $user->email; 
  6998.  
  6999. $is_menu_item_account_visible = $this->_menu->is_submenu_item_visible( 'account' ); 
  7000.  
  7001. if ( $user->is_verified && 
  7002. ( ! isset( $this->_user->is_verified ) || false === $this->_user->is_verified ) 
  7003. ) { 
  7004. $this->_user->is_verified = true; 
  7005.  
  7006. $this->do_action( 'account_email_verified', $user->email ); 
  7007.  
  7008. $this->_admin_notices->add( 
  7009. __fs( 'email-verified-message', $this->_slug ),  
  7010. __fs( 'right-on', $this->_slug ) . '!',  
  7011. 'success',  
  7012. // Make admin sticky if account menu item is invisible,  
  7013. // since the page will be auto redirected to the plugin's 
  7014. // main settings page, and the non-sticky message 
  7015. // will disappear. 
  7016. ! $is_menu_item_account_visible,  
  7017. false,  
  7018. 'email_verified' 
  7019. ); 
  7020.  
  7021. // Flush user details to DB. 
  7022. $this->_store_user(); 
  7023.  
  7024. $this->do_action( 'after_account_user_sync', $user ); 
  7025.  
  7026. /** 
  7027. * If account menu item is hidden, redirect to plugin's main settings page. 
  7028. * @author Vova Feldman (@svovaf) 
  7029. * @since 1.1.6 
  7030. * @link https://github.com/Freemius/wordpress-sdk/issues/6 
  7031. */ 
  7032. if ( ! $is_menu_item_account_visible ) { 
  7033. fs_redirect( $this->_get_admin_page_url() ); 
  7034.  
  7035. /** 
  7036. * @param bool $store 
  7037. * @return FS_Plugin_Plan|object|false 
  7038. */ 
  7039. private function _enrich_site_plan( $store = true ) { 
  7040. // Try to load plan from local cache. 
  7041. $plan = $this->_get_plan_by_id( $this->_site->plan->id ); 
  7042.  
  7043. if ( false === $plan ) { 
  7044. $plan = $this->_fetch_site_plan(); 
  7045.  
  7046. if ( $plan instanceof FS_Plugin_Plan ) { 
  7047. $this->_update_plan( $plan, $store ); 
  7048.  
  7049. return $plan; 
  7050.  
  7051. /** 
  7052. * @author Vova Feldman (@svovaf) 
  7053. * @since 1.0.9 
  7054. * @uses FS_Api 
  7055. * @param bool $store 
  7056. * @return FS_Plugin_Plan|object|false 
  7057. */ 
  7058. private function _enrich_site_trial_plan( $store = true ) { 
  7059. // Try to load plan from local cache. 
  7060. $trial_plan = $this->_get_plan_by_id( $this->_site->trial_plan_id ); 
  7061.  
  7062. if ( false === $trial_plan ) { 
  7063. $trial_plan = $this->_fetch_site_plan( $this->_site->trial_plan_id ); 
  7064.  
  7065. if ( $trial_plan instanceof FS_Plugin_Plan ) { 
  7066. $this->_storage->store( 'trial_plan', $trial_plan, $store ); 
  7067.  
  7068. return $trial_plan; 
  7069.  
  7070. /** 
  7071. * @author Vova Feldman (@svovaf) 
  7072. * @since 1.0.9 
  7073. * @uses FS_Api 
  7074. * @param number|bool $license_id 
  7075. * @return FS_Subscription|object|bool 
  7076. */ 
  7077. private function _fetch_site_license_subscription( $license_id = false ) { 
  7078. $this->_logger->entrance(); 
  7079. $api = $this->get_api_site_scope(); 
  7080.  
  7081. if ( ! is_numeric( $license_id ) ) { 
  7082. $license_id = $this->_license->id; 
  7083.  
  7084. $result = $api->get( "/licenses/{$license_id}/subscriptions.json", true ); 
  7085.  
  7086. return ! isset( $result->error ) ? 
  7087. ( ( is_array( $result->subscriptions ) && 0 < count( $result->subscriptions ) ) ? 
  7088. new FS_Subscription( $result->subscriptions[0] ) : 
  7089. false 
  7090. ) : 
  7091. $result; 
  7092.  
  7093. /** 
  7094. * @author Vova Feldman (@svovaf) 
  7095. * @since 1.0.4 
  7096. * @uses FS_Api 
  7097. * @param number|bool $plan_id 
  7098. * @return FS_Plugin_Plan|object 
  7099. */ 
  7100. private function _fetch_site_plan( $plan_id = false ) { 
  7101. $this->_logger->entrance(); 
  7102. $api = $this->get_api_site_scope(); 
  7103.  
  7104. if ( ! is_numeric( $plan_id ) ) { 
  7105. $plan_id = $this->_site->plan->id; 
  7106.  
  7107. $plan = $api->get( "/plans/{$plan_id}.json", true ); 
  7108.  
  7109. return ! isset( $plan->error ) ? new FS_Plugin_Plan( $plan ) : $plan; 
  7110.  
  7111. /** 
  7112. * @author Vova Feldman (@svovaf) 
  7113. * @since 1.0.5 
  7114. * @uses FS_Api 
  7115. * @return FS_Plugin_Plan[]|object 
  7116. */ 
  7117. private function _fetch_plugin_plans() { 
  7118. $this->_logger->entrance(); 
  7119. $api = $this->get_api_site_scope(); 
  7120.  
  7121. $result = $api->get( '/plans.json', true ); 
  7122.  
  7123. if ( $this->is_api_result_object( $result, 'plans' ) && is_array( $result->plans ) ) { 
  7124. for ( $i = 0, $len = count( $result->plans ); $i < $len; $i ++ ) { 
  7125. $result->plans[ $i ] = new FS_Plugin_Plan( $result->plans[ $i ] ); 
  7126.  
  7127. $result = $result->plans; 
  7128.  
  7129. return $result; 
  7130.  
  7131. /** 
  7132. * @author Vova Feldman (@svovaf) 
  7133. * @since 1.0.5 
  7134. * @uses FS_Api 
  7135. * @param number|bool $plugin_id 
  7136. * @param number|bool $site_license_id 
  7137. * @return FS_Plugin_License[]|object 
  7138. */ 
  7139. private function _fetch_licenses( $plugin_id = false, $site_license_id = false ) { 
  7140. $this->_logger->entrance(); 
  7141.  
  7142. $api = $this->get_api_user_scope(); 
  7143.  
  7144. if ( ! is_numeric( $plugin_id ) ) { 
  7145. $plugin_id = $this->_plugin->id; 
  7146.  
  7147. $result = $api->get( "/plugins/{$plugin_id}/licenses.json", true ); 
  7148.  
  7149. $is_site_license_synced = false; 
  7150.  
  7151. $api_errors = array(); 
  7152.  
  7153. if ( $this->is_api_result_object( $result, 'licenses' ) && 
  7154. is_array( $result->licenses ) 
  7155. ) { 
  7156. for ( $i = 0, $len = count( $result->licenses ); $i < $len; $i ++ ) { 
  7157. $result->licenses[ $i ] = new FS_Plugin_License( $result->licenses[ $i ] ); 
  7158.  
  7159. if ( ( ! $is_site_license_synced ) && is_numeric( $site_license_id ) ) { 
  7160. $is_site_license_synced = ( $site_license_id == $result->licenses[ $i ]->id ); 
  7161.  
  7162. $result = $result->licenses; 
  7163. } else { 
  7164. $api_errors[] = $result; 
  7165. $result = array(); 
  7166.  
  7167. if ( ! $is_site_license_synced ) { 
  7168. $api = $this->get_api_site_scope(); 
  7169.  
  7170. if ( is_numeric( $site_license_id ) ) { 
  7171. // Try to retrieve a foreign license that is linked to the install. 
  7172. $api_result = $api->call( '/licenses.json' ); 
  7173.  
  7174. if ( $this->is_api_result_object( $api_result, 'licenses' ) && 
  7175. is_array( $api_result->licenses ) 
  7176. ) { 
  7177. $licenses = $api_result->licenses; 
  7178.  
  7179. if ( ! empty( $licenses ) ) { 
  7180. $result[] = new FS_Plugin_License( $licenses[0] ); 
  7181. } else { 
  7182. $api_errors[] = $api_result; 
  7183. } else if ( is_object( $this->_license ) ) { 
  7184. // Fetch foreign license by ID and license key. 
  7185. $license = $api->get( "/licenses/{$this->_license->id}.json?license_key=" . 
  7186. urlencode( $this->_license->secret_key ) ); 
  7187.  
  7188. if ( $this->is_api_result_entity( $license ) ) { 
  7189. $result[] = new FS_Plugin_License( $license ); 
  7190. } else { 
  7191. $api_errors[] = $license; 
  7192.  
  7193. if ( is_array( $result ) && 0 < count( $result ) ) { 
  7194. // If found at least one license, return license collection even if there are errors. 
  7195. return $result; 
  7196.  
  7197. if ( ! empty( $api_errors ) ) { 
  7198. // If found any errors and no licenses, return first error. 
  7199. return $api_errors[0]; 
  7200.  
  7201. // Fallback to empty licenses list. 
  7202. return $result; 
  7203.  
  7204. /** 
  7205. * @author Vova Feldman (@svovaf) 
  7206. * @since 1.2.0 
  7207. * @uses FS_Api 
  7208. * @param number|bool $plugin_id 
  7209. * @return FS_Payment[]|object 
  7210. */ 
  7211. function _fetch_payments( $plugin_id = false ) { 
  7212. $this->_logger->entrance(); 
  7213.  
  7214. $api = $this->get_api_user_scope(); 
  7215.  
  7216. if ( ! is_numeric( $plugin_id ) ) { 
  7217. $plugin_id = $this->_plugin->id; 
  7218.  
  7219. $result = $api->get( "/plugins/{$plugin_id}/payments.json", true ); 
  7220.  
  7221. if ( ! isset( $result->error ) ) { 
  7222. for ( $i = 0, $len = count( $result->payments ); $i < $len; $i ++ ) { 
  7223. $result->payments[ $i ] = new FS_Payment( $result->payments[ $i ] ); 
  7224. $result = $result->payments; 
  7225.  
  7226. return $result; 
  7227.  
  7228. /** 
  7229. * @author Vova Feldman (@svovaf) 
  7230. * @since 1.2.1.5 
  7231. * @uses FS_Api 
  7232. * @return \FS_Billing|mixed 
  7233. */ 
  7234. function _fetch_billing() { 
  7235. require_once WP_FS__DIR_INCLUDES . '/entities/class-fs-billing.php'; 
  7236.  
  7237. $billing = $this->get_api_user_scope()->call( 'billing.json' ); 
  7238.  
  7239. if ( $this->is_api_result_entity( $billing ) ) { 
  7240. $billing = new FS_Billing( $billing ); 
  7241.  
  7242. return $billing; 
  7243.  
  7244. /** 
  7245. * @author Vova Feldman (@svovaf) 
  7246. * @since 1.0.4 
  7247. * @param FS_Plugin_Plan $plan 
  7248. * @param bool $store 
  7249. */ 
  7250. private function _update_plan( $plan, $store = false ) { 
  7251. $this->_logger->entrance(); 
  7252.  
  7253. $this->_site->plan = $plan; 
  7254. $this->_store_site( $store ); 
  7255.  
  7256. /** 
  7257. * @author Vova Feldman (@svovaf) 
  7258. * @since 1.0.5 
  7259. * @param FS_Plugin_License[] $licenses 
  7260. * @param string|bool $plugin_slug 
  7261. */ 
  7262. private function _update_licenses( $licenses, $plugin_slug = false ) { 
  7263. $this->_logger->entrance(); 
  7264.  
  7265. if ( is_array( $licenses ) ) { 
  7266. for ( $i = 0, $len = count( $licenses ); $i < $len; $i ++ ) { 
  7267. $licenses[ $i ]->updated = time(); 
  7268.  
  7269. if ( ! is_string( $plugin_slug ) ) { 
  7270. $this->_licenses = $licenses; 
  7271.  
  7272. $this->_store_licenses( true, $plugin_slug, $licenses ); 
  7273.  
  7274. /** 
  7275. * @author Vova Feldman (@svovaf) 
  7276. * @since 1.0.4 
  7277. * @param bool|number $plugin_id 
  7278. * @param bool $flush Since 1.1.7.3 
  7279. * @return object|false New plugin tag info if exist. 
  7280. */ 
  7281. private function _fetch_newer_version( $plugin_id = false, $flush = true ) { 
  7282. $latest_tag = $this->_fetch_latest_version( $plugin_id, $flush ); 
  7283.  
  7284. if ( ! is_object( $latest_tag ) ) { 
  7285. return false; 
  7286.  
  7287. // Check if version is actually newer. 
  7288. $has_new_version = 
  7289. // If it's an non-installed add-on then always return latest. 
  7290. ( $this->_is_addon_id( $plugin_id ) && ! $this->is_addon_activated( $plugin_id ) ) || 
  7291. // Compare versions. 
  7292. version_compare( $this->get_plugin_version(), $latest_tag->version, '<' ); 
  7293.  
  7294. $this->_logger->departure( $has_new_version ? 'Found newer plugin version ' . $latest_tag->version : 'No new version' ); 
  7295.  
  7296. return $has_new_version ? $latest_tag : false; 
  7297.  
  7298. /** 
  7299. * @author Vova Feldman (@svovaf) 
  7300. * @since 1.0.5 
  7301. * @param bool|number $plugin_id 
  7302. * @param bool $flush Since 1.1.7.3 
  7303. * @return bool|FS_Plugin_Tag 
  7304. */ 
  7305. function get_update( $plugin_id = false, $flush = true ) { 
  7306. $this->_logger->entrance(); 
  7307.  
  7308. if ( ! is_numeric( $plugin_id ) ) { 
  7309. $plugin_id = $this->_plugin->id; 
  7310.  
  7311. $this->_check_updates( true, $plugin_id, $flush ); 
  7312. $updates = $this->get_all_updates(); 
  7313.  
  7314. return isset( $updates[ $plugin_id ] ) && is_object( $updates[ $plugin_id ] ) ? $updates[ $plugin_id ] : false; 
  7315.  
  7316. /** 
  7317. * Check if site assigned with active license. 
  7318. * @author Vova Feldman (@svovaf) 
  7319. * @since 1.0.6 
  7320. * @deprecated Please use has_active_valid_license() instead because license can be cancelled. 
  7321. */ 
  7322. function has_active_license() { 
  7323. return ( 
  7324. is_object( $this->_license ) && 
  7325. is_numeric( $this->_license->id ) && 
  7326. ! $this->_license->is_expired() 
  7327. ); 
  7328.  
  7329. /** 
  7330. * Check if site assigned with active & valid (not expired) license. 
  7331. * @author Vova Feldman (@svovaf) 
  7332. * @since 1.2.1 
  7333. */ 
  7334. function has_active_valid_license() { 
  7335. return ( 
  7336. is_object( $this->_license ) && 
  7337. is_numeric( $this->_license->id ) && 
  7338. $this->_license->is_active() && 
  7339. $this->_license->is_valid() 
  7340. ); 
  7341.  
  7342. /** 
  7343. * Check if site assigned with license with enabled features. 
  7344. * @author Vova Feldman (@svovaf) 
  7345. * @since 1.0.6 
  7346. * @return bool 
  7347. */ 
  7348. function has_features_enabled_license() { 
  7349. return ( 
  7350. is_object( $this->_license ) && 
  7351. is_numeric( $this->_license->id ) && 
  7352. $this->_license->is_features_enabled() 
  7353. ); 
  7354.  
  7355. /** 
  7356. * Check if user is a trial or have feature enabled license. 
  7357. * @author Vova Feldman (@svovaf) 
  7358. * @since 1.1.7 
  7359. * @return bool 
  7360. */ 
  7361. function can_use_premium_code() { 
  7362. return $this->is_trial() || $this->has_features_enabled_license(); 
  7363.  
  7364. /** 
  7365. * Sync site's plan. 
  7366. * @author Vova Feldman (@svovaf) 
  7367. * @since 1.0.3 
  7368. * @uses FS_Api 
  7369. * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by 
  7370. * the admin. 
  7371. */ 
  7372. private function _sync_license( $background = false ) { 
  7373. $this->_logger->entrance(); 
  7374.  
  7375. $plugin_id = fs_request_get( 'plugin_id', $this->get_id() ); 
  7376.  
  7377. $is_addon_sync = ( ! $this->_plugin->is_addon() && $plugin_id != $this->get_id() ); 
  7378.  
  7379. if ( $is_addon_sync ) { 
  7380. $this->_sync_addon_license( $plugin_id, $background ); 
  7381. } else { 
  7382. $this->_sync_plugin_license( $background ); 
  7383.  
  7384. $this->do_action( 'after_account_plan_sync', $this->_site->plan->name ); 
  7385.  
  7386. /** 
  7387. * Sync plugin's add-on license. 
  7388. * @author Vova Feldman (@svovaf) 
  7389. * @since 1.0.6 
  7390. * @uses FS_Api 
  7391. * @param number $addon_id 
  7392. * @param bool $background 
  7393. */ 
  7394. private function _sync_addon_license( $addon_id, $background ) { 
  7395. $this->_logger->entrance(); 
  7396.  
  7397. if ( $this->is_addon_activated( $addon_id ) ) { 
  7398. // If already installed, use add-on sync. 
  7399. $fs_addon = self::get_instance_by_id( $addon_id ); 
  7400. $fs_addon->_sync_license( $background ); 
  7401.  
  7402. return; 
  7403.  
  7404. // Validate add-on exists. 
  7405. $addon = $this->get_addon( $addon_id ); 
  7406.  
  7407. if ( ! is_object( $addon ) ) { 
  7408. return; 
  7409.  
  7410. // Add add-on into account add-ons. 
  7411. $account_addons = $this->get_account_addons(); 
  7412. if ( ! is_array( $account_addons ) ) { 
  7413. $account_addons = array(); 
  7414. $account_addons[] = $addon->id; 
  7415. $account_addons = array_unique( $account_addons ); 
  7416. $this->_store_account_addons( $account_addons ); 
  7417.  
  7418. // Load add-on licenses. 
  7419. $licenses = $this->_fetch_licenses( $addon->id ); 
  7420.  
  7421. // Sync add-on licenses. 
  7422. if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) { 
  7423. $this->_update_licenses( $licenses, $addon->slug ); 
  7424.  
  7425. if ( ! $this->is_addon_installed( $addon->slug ) && FS_License_Manager::has_premium_license( $licenses ) ) { 
  7426. $plans_result = $this->get_api_site_or_plugin_scope()->get( "/addons/{$addon_id}/plans.json" ); 
  7427.  
  7428. if ( ! isset( $plans_result->error ) ) { 
  7429. $plans = array(); 
  7430. foreach ( $plans_result->plans as $plan ) { 
  7431. $plans[] = new FS_Plugin_Plan( $plan ); 
  7432.  
  7433. $this->_admin_notices->add_sticky( 
  7434. FS_Plan_Manager::instance()->has_free_plan( $plans ) ? 
  7435. sprintf( 
  7436. __fs( 'addon-successfully-upgraded-message', $this->_slug ),  
  7437. $addon->title 
  7438. ) . ' ' . $this->_get_latest_download_link( 
  7439. __fs( 'download-latest-version', $this->_slug ),  
  7440. $addon_id 
  7441. sprintf( 
  7442. __fs( 'addon-successfully-purchased-message', $this->_slug ),  
  7443. $addon->title 
  7444. ) . ' ' . $this->_get_latest_download_link( 
  7445. __fs( 'download-latest-version', $this->_slug ),  
  7446. $addon_id 
  7447. ),  
  7448. 'addon_plan_upgraded_' . $addon->slug,  
  7449. __fs( 'yee-haw', $this->_slug ) . '!' 
  7450. ); 
  7451.  
  7452. /** 
  7453. * Sync site's plugin plan. 
  7454. * @author Vova Feldman (@svovaf) 
  7455. * @since 1.0.6 
  7456. * @uses FS_Api 
  7457. * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by 
  7458. * the admin. 
  7459. */ 
  7460. private function _sync_plugin_license( $background = false ) { 
  7461. $this->_logger->entrance(); 
  7462.  
  7463. /** 
  7464. * Sync site info. 
  7465. * @todo This line will execute install sync on a daily basis, even if running the free version (for opted-in users). The reason we want to keep it that way is for cases when the user was a paying customer, then there was a failure in subscription payment, and then after some time the payment was successful. This could be heavily optimized. For example, we can skip the $flush if the current install was never associated with a paid version. 
  7466. */ 
  7467. $site = $this->send_install_update( array(), true ); 
  7468.  
  7469. $plan_change = 'none'; 
  7470.  
  7471. if ( ! $this->is_api_result_entity( $site ) ) { 
  7472. // Show API messages only if not background sync or if paying customer. 
  7473. if ( ! $background || $this->is_paying() ) { 
  7474. // Try to ping API to see if not blocked. 
  7475. if ( ! FS_Api::test() ) { 
  7476. /** 
  7477. * Failed to ping API - blocked! 
  7478. * @author Vova Feldman (@svovaf) 
  7479. * @since 1.1.6 Only show message related to one of the Freemius powered plugins. Once it will be resolved it will fix the issue for all plugins anyways. There's no point to scare users with multiple error messages. 
  7480. */ 
  7481. $api = $this->get_api_site_scope(); 
  7482.  
  7483. if ( ! self::$_global_admin_notices->has_sticky( 'api_blocked' ) ) { 
  7484. self::$_global_admin_notices->add( 
  7485. sprintf( 
  7486. __fs( 'server-blocking-access', $this->_slug ),  
  7487. $this->get_plugin_name(),  
  7488. '<a href="' . $api->get_url() . '" target="_blank">' . $api->get_url() . '</a>' 
  7489. ) . '<br> ' . __fs( 'server-error-message', $this->_slug ) . var_export( $site->error, true ),  
  7490. __fs( 'oops', $this->_slug ) . '...',  
  7491. 'error',  
  7492. $background,  
  7493. false,  
  7494. 'api_blocked' 
  7495. ); 
  7496. } else { 
  7497. // Authentication params are broken. 
  7498. $this->_admin_notices->add( 
  7499. __fs( 'wrong-authentication-param-message', $this->_slug ),  
  7500. __fs( 'oops', $this->_slug ) . '...',  
  7501. 'error' 
  7502. ); 
  7503.  
  7504. // No reason to continue with license sync while there are API issues. 
  7505. return; 
  7506.  
  7507. // Remove sticky API connectivity message. 
  7508. self::$_global_admin_notices->remove_sticky( 'api_blocked' ); 
  7509.  
  7510. $site = new FS_Site( $site ); 
  7511.  
  7512. // Sync plans. 
  7513. $this->_sync_plans(); 
  7514.  
  7515. if ( ! $this->has_paid_plan() ) { 
  7516. $this->_site = $site; 
  7517. $this->_enrich_site_plan( true ); 
  7518. $this->_store_site(); 
  7519. } else { 
  7520. /** 
  7521. * Sync licenses. Pass the site's license ID so that the foreign licenses will be fetched if the license 
  7522. * associated with that ID is not included in the user's licenses collection. 
  7523. */ 
  7524. $this->_sync_licenses( $site->license_id ); 
  7525.  
  7526. // Check if plan / license changed. 
  7527. if ( ! FS_Entity::equals( $site->plan, $this->_site->plan ) || 
  7528. // Check if trial started. 
  7529. $site->trial_plan_id != $this->_site->trial_plan_id || 
  7530. $site->trial_ends != $this->_site->trial_ends || 
  7531. // Check if license changed. 
  7532. $site->license_id != $this->_site->license_id 
  7533. ) { 
  7534. if ( $site->is_trial() && ( ! $this->_site->is_trial() || $site->trial_ends != $this->_site->trial_ends ) ) { 
  7535. // New trial started. 
  7536. $this->_site = $site; 
  7537. $plan_change = 'trial_started'; 
  7538.  
  7539. // Store trial plan information. 
  7540. $this->_enrich_site_trial_plan( true ); 
  7541.  
  7542. // For trial with subscription use-case. 
  7543. $new_license = is_null( $site->license_id ) ? null : $this->_get_license_by_id( $site->license_id ); 
  7544.  
  7545. if ( is_object( $new_license ) && $new_license->is_valid() ) { 
  7546. $this->_site = $site; 
  7547. $this->_update_site_license( $new_license ); 
  7548. $this->_store_licenses(); 
  7549. $this->_enrich_site_plan( true ); 
  7550.  
  7551. $this->_sync_site_subscription( $this->_license ); 
  7552. } else if ( $this->_site->is_trial() && ! $site->is_trial() && ! is_numeric( $site->license_id ) ) { 
  7553. // Was in trial, but now trial expired and no license ID. 
  7554. // New trial started. 
  7555. $this->_site = $site; 
  7556. $plan_change = 'trial_expired'; 
  7557.  
  7558. // Clear trial plan information. 
  7559. $this->_storage->trial_plan = null; 
  7560.  
  7561. } else { 
  7562. $is_free = $this->is_free_plan(); 
  7563.  
  7564. // Make sure license exist and not expired. 
  7565. $new_license = is_null( $site->license_id ) ? 
  7566. null : 
  7567. $this->_get_license_by_id( $site->license_id ); 
  7568.  
  7569. if ( $is_free && is_null( $new_license ) && $this->has_any_license() && $this->_license->is_cancelled ) { 
  7570. // License cancelled. 
  7571. $this->_site = $site; 
  7572. $this->_update_site_license( $new_license ); 
  7573. $this->_store_licenses(); 
  7574. $this->_enrich_site_plan( true ); 
  7575.  
  7576. $plan_change = 'cancelled'; 
  7577. } else if ( $is_free && ( ( ! is_object( $new_license ) || $new_license->is_expired() ) ) ) { 
  7578. // The license is expired, so ignore upgrade method. 
  7579. } else { 
  7580. // License changed. 
  7581. $this->_site = $site; 
  7582. $this->_update_site_license( $new_license ); 
  7583. $this->_store_licenses(); 
  7584. $this->_enrich_site_plan( true ); 
  7585.  
  7586. $plan_change = $is_free ? 
  7587. 'upgraded' : 
  7588. ( is_object( $new_license ) ? 
  7589. 'changed' : 
  7590. 'downgraded' ); 
  7591.  
  7592. // Store updated site info. 
  7593. $this->_store_site(); 
  7594. } else { 
  7595. if ( is_object( $this->_license ) && $this->_license->is_expired() ) { 
  7596. if ( ! $this->has_features_enabled_license() ) { 
  7597. $this->_deactivate_license(); 
  7598. $plan_change = 'downgraded'; 
  7599. } else { 
  7600. $plan_change = 'expired'; 
  7601.  
  7602. if ( is_numeric( $site->license_id ) && is_object( $this->_license ) ) { 
  7603. $this->_sync_site_subscription( $this->_license ); 
  7604.  
  7605. if ( $this->has_paid_plan() ) { 
  7606. switch ( $plan_change ) { 
  7607. case 'none': 
  7608. if ( ! $background && is_admin() ) { 
  7609. $plan = $this->is_trial() ? 
  7610. $this->_storage->trial_plan : 
  7611. $this->_site->plan; 
  7612.  
  7613. $this->_admin_notices->add( 
  7614. sprintf( 
  7615. __fs( 'plan-did-not-change-message', $this->_slug ),  
  7616. '<i><b>' . $plan->title . ( $this->is_trial() ? ' ' . __fs( 'trial', $this->_slug ) : '' ) . '</b></i>' 
  7617. ) . ' ' . sprintf( 
  7618. '<a href="%s">%s</a>',  
  7619. $this->contact_url( 
  7620. 'bug',  
  7621. sprintf( __fs( 'plan-did-not-change-email-message', $this->_slug ),  
  7622. strtoupper( $plan->name ) 
  7623. ),  
  7624. __fs( 'contact-us-here', $this->_slug ) 
  7625. ),  
  7626. __fs( 'hmm', $this->_slug ) . '...' 
  7627. ); 
  7628. break; 
  7629. case 'upgraded': 
  7630. $this->_admin_notices->add_sticky( 
  7631. sprintf( 
  7632. __fs( 'plan-upgraded-message', $this->_slug ),  
  7633. '<i>' . $this->get_plugin_name() . '</i>' 
  7634. ) . $this->get_complete_upgrade_instructions(),  
  7635. 'plan_upgraded',  
  7636. __fs( 'yee-haw', $this->_slug ) . '!' 
  7637. ); 
  7638.  
  7639. $this->_admin_notices->remove_sticky( array( 
  7640. 'trial_started',  
  7641. 'trial_promotion',  
  7642. 'trial_expired',  
  7643. 'activation_complete',  
  7644. ) ); 
  7645. break; 
  7646. case 'changed': 
  7647. $this->_admin_notices->add_sticky( 
  7648. sprintf( 
  7649. __fs( 'plan-changed-to-x-message', $this->_slug ),  
  7650. $this->_site->plan->title 
  7651. ),  
  7652. 'plan_changed' 
  7653. ); 
  7654.  
  7655. $this->_admin_notices->remove_sticky( array( 
  7656. 'trial_started',  
  7657. 'trial_promotion',  
  7658. 'trial_expired',  
  7659. 'activation_complete',  
  7660. ) ); 
  7661. break; 
  7662. case 'downgraded': 
  7663. $this->_admin_notices->add_sticky( 
  7664. sprintf( __fs( 'license-expired-blocking-message', $this->_slug ) ),  
  7665. 'license_expired',  
  7666. __fs( 'hmm', $this->_slug ) . '...' 
  7667. ); 
  7668. $this->_admin_notices->remove_sticky( 'plan_upgraded' ); 
  7669. break; 
  7670. case 'cancelled': 
  7671. $this->_admin_notices->add( 
  7672. __fs( 'license-cancelled', $this->_slug ) . ' ' . 
  7673. sprintf( 
  7674. '<a href="%s">%s</a>',  
  7675. $this->contact_url( 'bug' ),  
  7676. __fs( 'contact-us-here', $this->_slug ) 
  7677. ),  
  7678. __fs( 'hmm', $this->_slug ) . '...',  
  7679. 'error' 
  7680. ); 
  7681. $this->_admin_notices->remove_sticky( 'plan_upgraded' ); 
  7682. break; 
  7683. case 'expired': 
  7684. $this->_admin_notices->add_sticky( 
  7685. sprintf( __fs( 'license-expired-non-blocking-message', $this->_slug ), $this->_site->plan->title ),  
  7686. 'license_expired',  
  7687. __fs( 'hmm', $this->_slug ) . '...' 
  7688. ); 
  7689. $this->_admin_notices->remove_sticky( 'plan_upgraded' ); 
  7690. break; 
  7691. case 'trial_started': 
  7692. $this->_admin_notices->add_sticky( 
  7693. sprintf( 
  7694. __fs( 'trial-started-message', $this->_slug ),  
  7695. '<i>' . $this->get_plugin_name() . '</i>' 
  7696. ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),  
  7697. 'trial_started',  
  7698. __fs( 'yee-haw', $this->_slug ) . '!' 
  7699. ); 
  7700.  
  7701. $this->_admin_notices->remove_sticky( array( 
  7702. 'trial_promotion',  
  7703. ) ); 
  7704. break; 
  7705. case 'trial_expired': 
  7706. $this->_admin_notices->add_sticky( 
  7707. __fs( 'trial-expired-message', $this->_slug ),  
  7708. 'trial_expired',  
  7709. __fs( 'hmm', $this->_slug ) . '...' 
  7710. ); 
  7711. $this->_admin_notices->remove_sticky( array( 
  7712. 'trial_started',  
  7713. 'trial_promotion',  
  7714. 'plan_upgraded',  
  7715. ) ); 
  7716. break; 
  7717.  
  7718. if ( 'none' !== $plan_change ) { 
  7719. $this->do_action( 'after_license_change', $plan_change, $this->_site->plan ); 
  7720.  
  7721. /** 
  7722. * @author Vova Feldman (@svovaf) 
  7723. * @since 1.0.5 
  7724. * @param bool $background 
  7725. */ 
  7726. protected function _activate_license( $background = false ) { 
  7727. $this->_logger->entrance(); 
  7728.  
  7729. $license_id = fs_request_get( 'license_id' ); 
  7730.  
  7731. if ( FS_Plugin_License::is_valid_id( $license_id ) && $license_id == $this->_site->license_id ) { 
  7732. // License is already activated. 
  7733. return; 
  7734.  
  7735. $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ? 
  7736. $this->_get_license_by_id( $license_id ) : 
  7737. $this->_get_available_premium_license(); 
  7738.  
  7739. if ( ! is_object( $premium_license ) ) { 
  7740. return; 
  7741.  
  7742. /** 
  7743. * If the premium license is already associated with the install, just 
  7744. * update the license reference (activation is not required). 
  7745. * @since 1.1.9 
  7746. */ 
  7747. if ( $premium_license->id == $this->_site->license_id ) { 
  7748. // License is already activated. 
  7749. $this->_update_site_license( $premium_license ); 
  7750. $this->_enrich_site_plan( false ); 
  7751. $this->_store_account(); 
  7752.  
  7753. return; 
  7754.  
  7755. if ( $this->_site->user_id != $premium_license->user_id ) { 
  7756. $api_request_params = array( 'license_key' => $premium_license->secret_key ); 
  7757. } else { 
  7758. $api_request_params = array(); 
  7759.  
  7760. $api = $this->get_api_site_scope(); 
  7761. $license = $api->call( "/licenses/{$premium_license->id}.json", 'put', $api_request_params ); 
  7762.  
  7763. if ( ! $this->is_api_result_entity( $license ) ) { 
  7764. if ( ! $background ) { 
  7765. $this->_admin_notices->add( sprintf( 
  7766. '%s %s',  
  7767. __fs( 'license-activation-failed-message', $this->_slug ),  
  7768. ( is_object( $license ) && isset( $license->error ) ? 
  7769. $license->error->message : 
  7770. sprintf( '%s<br><code>%s</code>',  
  7771. __fs( 'server-error-message', $this->_slug ),  
  7772. var_export( $license, true ) 
  7773. ),  
  7774. __fs( 'hmm', $this->_slug ) . '...',  
  7775. 'error' 
  7776. ); 
  7777.  
  7778. return; 
  7779.  
  7780. $premium_license = new FS_Plugin_License( $license ); 
  7781.  
  7782. // Updated site plan. 
  7783. $site = $this->get_api_site_scope()->get( '/', true ); 
  7784. if ( $this->is_api_result_entity( $site ) ) { 
  7785. $this->_site = new FS_Site( $site ); 
  7786. $this->_update_site_license( $premium_license ); 
  7787. $this->_enrich_site_plan( false ); 
  7788.  
  7789. $this->_store_account(); 
  7790.  
  7791. if ( ! $background ) { 
  7792. $this->_admin_notices->add_sticky( 
  7793. __fs( 'license-activated-message', $this->_slug ) . 
  7794. $this->get_complete_upgrade_instructions(),  
  7795. 'license_activated',  
  7796. __fs( 'yee-haw', $this->_slug ) . '!' 
  7797. ); 
  7798.  
  7799. $this->_admin_notices->remove_sticky( array( 
  7800. 'trial_promotion',  
  7801. 'license_expired',  
  7802. ) ); 
  7803.  
  7804. /** 
  7805. * @author Vova Feldman (@svovaf) 
  7806. * @since 1.0.5 
  7807. * @param bool $show_notice 
  7808. */ 
  7809. protected function _deactivate_license( $show_notice = true ) { 
  7810. $this->_logger->entrance(); 
  7811.  
  7812. if ( ! is_object( $this->_license ) ) { 
  7813. $this->_admin_notices->add( 
  7814. sprintf( __fs( 'no-active-license-message', $this->_slug ), $this->_site->plan->title ),  
  7815. __fs( 'hmm', $this->_slug ) . '...' 
  7816. ); 
  7817.  
  7818. return; 
  7819.  
  7820. $api = $this->get_api_site_scope(); 
  7821. $license = $api->call( "/licenses/{$this->_site->license_id}.json", 'delete' ); 
  7822.  
  7823. if ( isset( $license->error ) ) { 
  7824. $this->_admin_notices->add( 
  7825. __fs( 'license-deactivation-failed-message', $this->_slug ) . '<br> ' . 
  7826. __fs( 'server-error-message', $this->_slug ) . ' ' . var_export( $license->error, true ),  
  7827. __fs( 'hmm', $this->_slug ) . '...',  
  7828. 'error' 
  7829. ); 
  7830.  
  7831. return; 
  7832.  
  7833. // Update license cache. 
  7834. for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) { 
  7835. if ( $license->id == $this->_licenses[ $i ]->id ) { 
  7836. $this->_licenses[ $i ] = new FS_Plugin_License( $license ); 
  7837.  
  7838. // Updated site plan to default. 
  7839. $this->_sync_plans(); 
  7840. $this->_site->plan->id = $this->_plans[0]->id; 
  7841. // Unlink license from site. 
  7842. $this->_update_site_license( null ); 
  7843. $this->_enrich_site_plan( false ); 
  7844.  
  7845. $this->_store_account(); 
  7846.  
  7847. if ( $show_notice ) { 
  7848. $this->_admin_notices->add( 
  7849. sprintf( __fs( 'license-deactivation-message', $this->_slug ), $this->_site->plan->title ),  
  7850. __fs( 'ok', $this->_slug ) 
  7851. ); 
  7852.  
  7853. $this->_admin_notices->remove_sticky( array( 
  7854. 'plan_upgraded',  
  7855. 'license_activated',  
  7856. ) ); 
  7857.  
  7858. /** 
  7859. * Site plan downgrade. 
  7860. * @author Vova Feldman (@svovaf) 
  7861. * @since 1.0.4 
  7862. * @uses FS_Api 
  7863. */ 
  7864. private function _downgrade_site() { 
  7865. $this->_logger->entrance(); 
  7866.  
  7867. $api = $this->get_api_site_scope(); 
  7868. $site = $api->call( 'downgrade.json', 'put' ); 
  7869.  
  7870. $plan_downgraded = false; 
  7871. $plan = false; 
  7872. if ( $this->is_api_result_entity( $site ) ) { 
  7873. $prev_plan_id = $this->_site->plan->id; 
  7874.  
  7875. // Update new site plan id. 
  7876. $this->_site->plan->id = $site->plan_id; 
  7877.  
  7878. $plan = $this->_enrich_site_plan(); 
  7879. $subscription = $this->_sync_site_subscription( $this->_license ); 
  7880.  
  7881. // Plan downgraded if plan was changed or subscription was cancelled. 
  7882. $plan_downgraded = ( $plan instanceof FS_Plugin_Plan && $prev_plan_id != $plan->id ) || 
  7883. ( is_object( $subscription ) && ! isset( $subscription->error ) && ! $subscription->is_active() ); 
  7884. } else { 
  7885. // handle different error cases. 
  7886.  
  7887.  
  7888. if ( $plan_downgraded ) { 
  7889. // Remove previous sticky message about upgrade (if exist). 
  7890. $this->_admin_notices->remove_sticky( 'plan_upgraded' ); 
  7891.  
  7892. $this->_admin_notices->add( 
  7893. sprintf( __fs( 'plan-x-downgraded-message', $this->_slug ),  
  7894. $plan->title,  
  7895. human_time_diff( time(), strtotime( $this->_license->expiration ) ) 
  7896. ); 
  7897.  
  7898. // Store site updates. 
  7899. $this->_store_site(); 
  7900. } else { 
  7901. $this->_admin_notices->add( 
  7902. __fs( 'plan-downgraded-failure-message', $this->_slug ),  
  7903. __fs( 'oops', $this->_slug ) . '...',  
  7904. 'error' 
  7905. ); 
  7906.  
  7907. /** 
  7908. * @author Vova Feldman (@svovaf) 
  7909. * @since 1.1.8.1 
  7910. * @param bool|string $plan_name 
  7911. * @return bool If trial was successfully started. 
  7912. */ 
  7913. function start_trial( $plan_name = false ) { 
  7914. $this->_logger->entrance(); 
  7915.  
  7916. if ( $this->is_trial() ) { 
  7917. // Already in trial mode. 
  7918. $this->_admin_notices->add( 
  7919. __fs( 'in-trial-mode', $this->_slug ),  
  7920. __fs( 'oops', $this->_slug ) . '...',  
  7921. 'error' 
  7922. ); 
  7923.  
  7924. return false; 
  7925.  
  7926. if ( $this->_site->is_trial_utilized() ) { 
  7927. // Trial was already utilized. 
  7928. $this->_admin_notices->add( 
  7929. __fs( 'trial-utilized', $this->_slug ),  
  7930. __fs( 'oops', $this->_slug ) . '...',  
  7931. 'error' 
  7932. ); 
  7933.  
  7934. return false; 
  7935.  
  7936. if ( false !== $plan_name ) { 
  7937. $plan = $this->get_plan_by_name( $plan_name ); 
  7938.  
  7939. if ( false === $plan ) { 
  7940. // Plan doesn't exist. 
  7941. $this->_admin_notices->add( 
  7942. sprintf( __fs( 'trial-plan-x-not-exist', $this->_slug ), $plan_name ),  
  7943. __fs( 'oops', $this->_slug ) . '...',  
  7944. 'error' 
  7945. ); 
  7946.  
  7947. return false; 
  7948.  
  7949. if ( ! $plan->has_trial() ) { 
  7950. // Plan doesn't exist. 
  7951. $this->_admin_notices->add( 
  7952. sprintf( __fs( 'plan-x-no-trial', $this->_slug ), $plan_name ),  
  7953. __fs( 'oops', $this->_slug ) . '...',  
  7954. 'error' 
  7955. ); 
  7956.  
  7957. return false; 
  7958. } else { 
  7959. if ( ! $this->has_trial_plan() ) { 
  7960. // None of the plans have a trial. 
  7961. $this->_admin_notices->add( 
  7962. __fs( 'no-trials', $this->_slug ),  
  7963. __fs( 'oops', $this->_slug ) . '...',  
  7964. 'error' 
  7965. ); 
  7966.  
  7967. return false; 
  7968.  
  7969. $plans_with_trial = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans ); 
  7970.  
  7971. $plan = $plans_with_trial[0]; 
  7972.  
  7973. $api = $this->get_api_site_scope(); 
  7974. $plan = $api->call( "plans/{$plan->id}/trials.json", 'post' ); 
  7975.  
  7976. if ( ! $this->is_api_result_entity( $plan ) ) { 
  7977. // Some API error while trying to start the trial. 
  7978. $this->_admin_notices->add( 
  7979. __fs( 'unexpected-api-error', $this->_slug ) . ' ' . var_export( $plan, true ),  
  7980. __fs( 'oops', $this->_slug ) . '...',  
  7981. 'error' 
  7982. ); 
  7983.  
  7984. return false; 
  7985.  
  7986. // Sync license. 
  7987. $this->_sync_license(); 
  7988.  
  7989. return $this->is_trial(); 
  7990.  
  7991. /** 
  7992. * Cancel site trial. 
  7993. * @author Vova Feldman (@svovaf) 
  7994. * @since 1.0.9 
  7995. * @uses FS_Api 
  7996. */ 
  7997. private function _cancel_trial() { 
  7998. $this->_logger->entrance(); 
  7999.  
  8000. if ( ! $this->is_trial() ) { 
  8001. $this->_admin_notices->add( 
  8002. __fs( 'trial-cancel-no-trial-message', $this->_slug ),  
  8003. __fs( 'oops', $this->_slug ) . '...',  
  8004. 'error' 
  8005. ); 
  8006.  
  8007. return; 
  8008.  
  8009. $api = $this->get_api_site_scope(); 
  8010. $site = $api->call( 'trials.json', 'delete' ); 
  8011.  
  8012. $trial_cancelled = false; 
  8013.  
  8014. if ( $this->is_api_result_entity( $site ) ) { 
  8015. $prev_trial_ends = $this->_site->trial_ends; 
  8016.  
  8017. if ( $this->is_paid_trial() ) { 
  8018. $this->_license->expiration = $site->trial_ends; 
  8019. $this->_license->is_cancelled = true; 
  8020. $this->_update_site_license( $this->_license ); 
  8021. $this->_store_licenses(); 
  8022.  
  8023. // Clear subscription reference. 
  8024. $this->_sync_site_subscription( null ); 
  8025.  
  8026. // Update site info. 
  8027. $this->_site = new FS_Site( $site ); 
  8028. $this->_enrich_site_plan(); 
  8029.  
  8030. $trial_cancelled = ( $prev_trial_ends != $site->trial_ends ); 
  8031. } else { 
  8032. // handle different error cases. 
  8033.  
  8034.  
  8035. if ( $trial_cancelled ) { 
  8036. // Remove previous sticky messages about upgrade or trial (if exist). 
  8037. $this->_admin_notices->remove_sticky( array( 
  8038. 'trial_started',  
  8039. 'trial_promotion',  
  8040. 'plan_upgraded',  
  8041. ) ); 
  8042.  
  8043. // Store site updates. 
  8044. $this->_store_site(); 
  8045.  
  8046. if ( ! $this->is_addon() || 
  8047. ! $this->deactivate_premium_only_addon_without_license( true ) 
  8048. ) { 
  8049. $this->_admin_notices->add( 
  8050. sprintf( __fs( 'trial-cancel-message', $this->_slug ), $this->_storage->trial_plan->title ) 
  8051. ); 
  8052.  
  8053. // Clear trial plan information. 
  8054. unset( $this->_storage->trial_plan ); 
  8055. } else { 
  8056. $this->_admin_notices->add( 
  8057. __fs( 'trial-cancel-failure-message', $this->_slug ),  
  8058. __fs( 'oops', $this->_slug ) . '...',  
  8059. 'error' 
  8060. ); 
  8061.  
  8062. /** 
  8063. * @author Vova Feldman (@svovaf) 
  8064. * @since 1.0.6 
  8065. * @param bool|number $plugin_id 
  8066. * @return bool 
  8067. */ 
  8068. private function _is_addon_id( $plugin_id ) { 
  8069. return is_numeric( $plugin_id ) && ( $this->get_id() != $plugin_id ); 
  8070.  
  8071. /** 
  8072. * Check if user eligible to download premium version updates. 
  8073. * @author Vova Feldman (@svovaf) 
  8074. * @since 1.0.6 
  8075. * @return bool 
  8076. */ 
  8077. private function _can_download_premium() { 
  8078. return $this->has_active_valid_license() || 
  8079. ( $this->is_trial() && ! $this->get_trial_plan()->is_free() ); 
  8080.  
  8081. /** 
  8082. * @author Vova Feldman (@svovaf) 
  8083. * @since 1.0.6 
  8084. * @param bool|number $addon_id 
  8085. * @param string $type "json" or "zip" 
  8086. * @return string 
  8087. */ 
  8088. private function _get_latest_version_endpoint( $addon_id = false, $type = 'json' ) { 
  8089.  
  8090. $is_addon = $this->_is_addon_id( $addon_id ); 
  8091.  
  8092. $is_premium = null; 
  8093. if ( ! $is_addon ) { 
  8094. $is_premium = $this->_can_download_premium(); 
  8095. } else if ( $this->is_addon_activated( $addon_id ) ) { 
  8096. $is_premium = self::get_instance_by_id( $addon_id )->_can_download_premium(); 
  8097.  
  8098. return // If add-on, then append add-on ID. 
  8099. ( $is_addon ? "/addons/$addon_id" : '' ) . 
  8100. '/updates/latest.' . $type . 
  8101. // If add-on and not yet activated, try to fetch based on server licensing. 
  8102. ( is_bool( $is_premium ) ? '?is_premium=' . json_encode( $is_premium ) : '' ); 
  8103.  
  8104. /** 
  8105. * @author Vova Feldman (@svovaf) 
  8106. * @since 1.0.4 
  8107. * @param bool|number $addon_id 
  8108. * @param bool $flush Since 1.1.7.3 
  8109. * @return object|false Plugin latest tag info. 
  8110. */ 
  8111. function _fetch_latest_version( $addon_id = false, $flush = true ) { 
  8112. $this->_logger->entrance(); 
  8113.  
  8114. /** 
  8115. * @since 1.1.7.3 Check for plugin updates from Freemius only if opted-in. 
  8116. * @since 1.1.7.4 Also check updates for add-ons. 
  8117. */ 
  8118. if ( ! $this->is_registered() && 
  8119. ! $this->_is_addon_id( $addon_id ) 
  8120. ) { 
  8121. return false; 
  8122.  
  8123. $tag = $this->get_api_site_or_plugin_scope()->get( 
  8124. $this->_get_latest_version_endpoint( $addon_id, 'json' ),  
  8125. $flush 
  8126. ); 
  8127.  
  8128. $latest_version = ( is_object( $tag ) && isset( $tag->version ) ) ? $tag->version : 'couldn\'t get'; 
  8129.  
  8130. $this->_logger->departure( 'Latest version ' . $latest_version ); 
  8131.  
  8132. return ( is_object( $tag ) && isset( $tag->version ) ) ? $tag : false; 
  8133.  
  8134. #---------------------------------------------------------------------------------- 
  8135. #region Download Plugin 
  8136. #---------------------------------------------------------------------------------- 
  8137.  
  8138. /** 
  8139. * Download latest plugin version, based on plan. 
  8140. * Not like _download_latest(), this will redirect the page 
  8141. * to secure download url to prevent dual download (from FS to WP server,  
  8142. * and then from WP server to the client / browser). 
  8143. * @author Vova Feldman (@svovaf) 
  8144. * @since 1.0.9 
  8145. * @param bool|number $plugin_id 
  8146. * @uses FS_Api 
  8147. * @uses wp_redirect() 
  8148. */ 
  8149. private function _download_latest_directly( $plugin_id = false ) { 
  8150. $this->_logger->entrance(); 
  8151.  
  8152. wp_redirect( $this->_get_latest_download_api_url( $plugin_id ) ); 
  8153.  
  8154. /** 
  8155. * Get latest plugin FS API download URL. 
  8156. * @author Vova Feldman (@svovaf) 
  8157. * @since 1.0.9 
  8158. * @param bool|number $plugin_id 
  8159. * @return string 
  8160. */ 
  8161. private function _get_latest_download_api_url( $plugin_id = false ) { 
  8162. $this->_logger->entrance(); 
  8163.  
  8164. return $this->get_api_site_scope()->get_signed_url( 
  8165. $this->_get_latest_version_endpoint( $plugin_id, 'zip' ) 
  8166. ); 
  8167.  
  8168. /** 
  8169. * Get payment invoice URL. 
  8170. * @author Vova Feldman (@svovaf) 
  8171. * @since 1.2.0 
  8172. * @param bool|number $payment_id 
  8173. * @return string 
  8174. */ 
  8175. function _get_invoice_api_url( $payment_id = false ) { 
  8176. $this->_logger->entrance(); 
  8177.  
  8178. return $this->get_api_user_scope()->get_signed_url( 
  8179. "/payments/{$payment_id}/invoice.pdf" 
  8180. ); 
  8181.  
  8182. /** 
  8183. * Get latest plugin download link. 
  8184. * @author Vova Feldman (@svovaf) 
  8185. * @since 1.0.9 
  8186. * @param string $label 
  8187. * @param bool|number $plugin_id 
  8188. * @return string 
  8189. */ 
  8190. private function _get_latest_download_link( $label, $plugin_id = false ) { 
  8191. return sprintf( 
  8192. '<a target="_blank" href="%s">%s</a>',  
  8193. $this->_get_latest_download_local_url( $plugin_id ),  
  8194. $label 
  8195. ); 
  8196.  
  8197. /** 
  8198. * Get latest plugin download local URL. 
  8199. * @author Vova Feldman (@svovaf) 
  8200. * @since 1.0.9 
  8201. * @param bool|number $plugin_id 
  8202. * @return string 
  8203. */ 
  8204. function _get_latest_download_local_url( $plugin_id = false ) { 
  8205. // Add timestamp to protect from caching. 
  8206. $params = array( 'ts' => WP_FS__SCRIPT_START_TIME ); 
  8207.  
  8208. if ( ! empty( $plugin_id ) ) { 
  8209. $params['plugin_id'] = $plugin_id; 
  8210.  
  8211. return $this->get_account_url( 'download_latest', $params ); 
  8212.  
  8213. #endregion Download Plugin ------------------------------------------------------------------ 
  8214.  
  8215. /** 
  8216. * @author Vova Feldman (@svovaf) 
  8217. * @since 1.0.4 
  8218. * @uses FS_Api 
  8219. * @param bool $background Hints the method if it's a background updates check. If false, it means that 
  8220. * was initiated by the admin. 
  8221. * @param bool|number $plugin_id 
  8222. * @param bool $flush Since 1.1.7.3 
  8223. */ 
  8224. private function _check_updates( $background = false, $plugin_id = false, $flush = true ) { 
  8225. $this->_logger->entrance(); 
  8226.  
  8227. // Check if there's a newer version for download. 
  8228. $new_version = $this->_fetch_newer_version( $plugin_id, $flush ); 
  8229.  
  8230. $update = null; 
  8231. if ( is_object( $new_version ) ) { 
  8232. $update = new FS_Plugin_Tag( $new_version ); 
  8233.  
  8234. if ( ! $background ) { 
  8235. $this->_admin_notices->add( 
  8236. sprintf( 
  8237. __fs( 'version-x-released', $this->_slug ) . ' ' . __fs( 'please-download-x', $this->_slug ),  
  8238. $update->version,  
  8239. sprintf( 
  8240. '<a href="%s" target="_blank">%s</a>',  
  8241. $this->get_account_url( 'download_latest' ),  
  8242. sprintf( __fs( 'latest-x-version', $this->_slug ), $this->_site->plan->title ) 
  8243. ),  
  8244. __fs( 'new', $this->_slug ) . '!' 
  8245. ); 
  8246. } else if ( false === $new_version && ! $background ) { 
  8247. $this->_admin_notices->add( 
  8248. __fs( 'you-have-latest', $this->_slug ),  
  8249. __fs( 'you-are-good', $this->_slug ) 
  8250. ); 
  8251.  
  8252. $this->_store_update( $update, true, $plugin_id ); 
  8253.  
  8254. /** 
  8255. * @author Vova Feldman (@svovaf) 
  8256. * @since 1.0.4 
  8257. * @param bool $flush Since 1.1.7.3 add 24 hour cache by default. 
  8258. * @return FS_Plugin[] 
  8259. * @uses FS_Api 
  8260. */ 
  8261. private function _sync_addons( $flush = false ) { 
  8262. $this->_logger->entrance(); 
  8263.  
  8264. $api = $this->get_api_site_or_plugin_scope(); 
  8265.  
  8266. /** 
  8267. * @since 1.2.1 
  8268. * If there's a cached version of the add-ons and not asking 
  8269. * for a flush, just use the currently stored add-ons. 
  8270. */ 
  8271. if ( ! $flush && $api->is_cached( '/addons.json?enriched=true' ) ) { 
  8272. $addons = self::get_all_addons(); 
  8273.  
  8274. return $addons[ $this->_plugin->id ]; 
  8275.  
  8276. $result = $api->get( '/addons.json?enriched=true', $flush ); 
  8277.  
  8278. $addons = array(); 
  8279. if ( $this->is_api_result_object( $result, 'plugins' ) && 
  8280. is_array( $result->plugins ) 
  8281. ) { 
  8282. for ( $i = 0, $len = count( $result->plugins ); $i < $len; $i ++ ) { 
  8283. $addons[ $i ] = new FS_Plugin( $result->plugins[ $i ] ); 
  8284.  
  8285. $this->_store_addons( $addons, true ); 
  8286.  
  8287. return $addons; 
  8288.  
  8289. /** 
  8290. * Handle user email update. 
  8291. * @author Vova Feldman (@svovaf) 
  8292. * @since 1.0.3 
  8293. * @uses FS_Api 
  8294. * @param string $new_email 
  8295. * @return object 
  8296. */ 
  8297. private function _update_email( $new_email ) { 
  8298. $this->_logger->entrance(); 
  8299.  
  8300.  
  8301. $api = $this->get_api_user_scope(); 
  8302. $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id, email, is_verified", 'put', array( 
  8303. 'email' => $new_email,  
  8304. 'after_email_confirm_url' => $this->_get_admin_page_url( 
  8305. 'account',  
  8306. array( 'fs_action' => 'sync_user' ) 
  8307. ),  
  8308. ) ); 
  8309.  
  8310. if ( ! isset( $user->error ) ) { 
  8311. $this->_user->email = $user->email; 
  8312. $this->_user->is_verified = $user->is_verified; 
  8313. $this->_store_user(); 
  8314. } else { 
  8315. // handle different error cases. 
  8316.  
  8317.  
  8318. return $user; 
  8319.  
  8320. #---------------------------------------------------------------------------------- 
  8321. #region API Error Handling 
  8322. #---------------------------------------------------------------------------------- 
  8323.  
  8324. /** 
  8325. * @author Vova Feldman (@svovaf) 
  8326. * @since 1.1.1 
  8327. * @param mixed $result 
  8328. * @return bool Is API result contains an error. 
  8329. */ 
  8330. private function is_api_error( $result ) { 
  8331. return FS_Api::is_api_error( $result ); 
  8332.  
  8333. /** 
  8334. * Checks if given API result is a non-empty and not an error object. 
  8335. * @author Vova Feldman (@svovaf) 
  8336. * @since 1.2.1.5 
  8337. * @param mixed $result 
  8338. * @param string|null $required_property Optional property we want to verify that is set. 
  8339. * @return bool 
  8340. */ 
  8341. function is_api_result_object( $result, $required_property = null ) { 
  8342. return FS_Api::is_api_result_object( $result, $required_property ); 
  8343.  
  8344. /** 
  8345. * Checks if given API result is a non-empty entity object with non-empty ID. 
  8346. * @author Vova Feldman (@svovaf) 
  8347. * @since 1.2.1.5 
  8348. * @param mixed $result 
  8349. * @return bool 
  8350. */ 
  8351. private function is_api_result_entity( $result ) { 
  8352. return FS_Api::is_api_result_entity( $result ); 
  8353.  
  8354. #endregion 
  8355.  
  8356. /** 
  8357. * Make sure a given argument is an array of a specific type. 
  8358. * @author Vova Feldman (@svovaf) 
  8359. * @since 1.2.1.5 
  8360. * @param mixed $array 
  8361. * @param string $class 
  8362. * @return bool 
  8363. */ 
  8364. private function is_array_instanceof( $array, $class ) { 
  8365. return ( is_array( $array ) && ( empty( $array ) || $array[0] instanceof $class ) ); 
  8366.  
  8367. /** 
  8368. * Start install ownership change. 
  8369. * @author Vova Feldman (@svovaf) 
  8370. * @since 1.1.1 
  8371. * @uses FS_Api 
  8372. * @param string $candidate_email 
  8373. * @return bool Is ownership change successfully initiated. 
  8374. */ 
  8375. private function init_change_owner( $candidate_email ) { 
  8376. $this->_logger->entrance(); 
  8377.  
  8378. $api = $this->get_api_site_scope(); 
  8379. $result = $api->call( "/users/{$this->_user->id}.json", 'put', array( 
  8380. 'email' => $candidate_email,  
  8381. 'after_confirm_url' => $this->_get_admin_page_url( 
  8382. 'account',  
  8383. array( 'fs_action' => 'change_owner' ) 
  8384. ),  
  8385. ) ); 
  8386.  
  8387. return ! $this->is_api_error( $result ); 
  8388.  
  8389. /** 
  8390. * Handle install ownership change. 
  8391. * @author Vova Feldman (@svovaf) 
  8392. * @since 1.1.1 
  8393. * @uses FS_Api 
  8394. * @return bool Was ownership change successfully complete. 
  8395. */ 
  8396. private function complete_change_owner() { 
  8397. $this->_logger->entrance(); 
  8398.  
  8399. $site_result = $this->get_api_site_scope( true )->get(); 
  8400. $site = new FS_Site( $site_result ); 
  8401. $this->_site = $site; 
  8402.  
  8403. $user = new FS_User(); 
  8404. $user->id = fs_request_get( 'user_id' ); 
  8405.  
  8406. // Validate install's user and given user. 
  8407. if ( $user->id != $this->_site->user_id ) { 
  8408. return false; 
  8409.  
  8410. $user->public_key = fs_request_get( 'user_public_key' ); 
  8411. $user->secret_key = fs_request_get( 'user_secret_key' ); 
  8412.  
  8413. // Fetch new user information. 
  8414. $this->_user = $user; 
  8415. $user_result = $this->get_api_user_scope( true )->get(); 
  8416. $user = new FS_User( $user_result ); 
  8417. $this->_user = $user; 
  8418.  
  8419. $this->_set_account( $user, $site ); 
  8420.  
  8421. return true; 
  8422.  
  8423. /** 
  8424. * Handle user name update. 
  8425. * @author Vova Feldman (@svovaf) 
  8426. * @since 1.0.9 
  8427. * @uses FS_Api 
  8428. * @return object 
  8429. */ 
  8430. private function update_user_name() { 
  8431. $this->_logger->entrance(); 
  8432. $name = fs_request_get( 'fs_user_name_' . $this->_slug, '' ); 
  8433.  
  8434. $api = $this->get_api_user_scope(); 
  8435. $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id, first, last", 'put', array( 
  8436. 'name' => $name,  
  8437. ) ); 
  8438.  
  8439. if ( ! isset( $user->error ) ) { 
  8440. $this->_user->first = $user->first; 
  8441. $this->_user->last = $user->last; 
  8442. $this->_store_user(); 
  8443. } else { 
  8444. // handle different error cases. 
  8445.  
  8446.  
  8447. return $user; 
  8448.  
  8449. /** 
  8450. * Verify user email. 
  8451. * @author Vova Feldman (@svovaf) 
  8452. * @since 1.0.3 
  8453. * @uses FS_Api 
  8454. */ 
  8455. private function verify_email() { 
  8456. $this->_handle_account_user_sync(); 
  8457.  
  8458. if ( $this->_user->is_verified() ) { 
  8459. return; 
  8460.  
  8461. $api = $this->get_api_site_scope(); 
  8462. $result = $api->call( "/users/{$this->_user->id}/verify.json", 'put', array( 
  8463. 'after_email_confirm_url' => $this->_get_admin_page_url( 
  8464. 'account',  
  8465. array( 'fs_action' => 'sync_user' ) 
  8466. ) ); 
  8467.  
  8468. if ( ! isset( $result->error ) ) { 
  8469. $this->_admin_notices->add( sprintf( 
  8470. __fs( 'verification-email-sent-message', $this->_slug ),  
  8471. sprintf( '<a href="mailto:%1s">%2s</a>', esc_url( $this->_user->email ), $this->_user->email ) 
  8472. ) ); 
  8473. } else { 
  8474. // handle different error cases. 
  8475.  
  8476.  
  8477. /** 
  8478. * @author Vova Feldman (@svovaf) 
  8479. * @since 1.1.2 
  8480. * @param array $params 
  8481. * @return string 
  8482. */ 
  8483. private function get_activation_url( $params = array() ) { 
  8484. return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params ) ); 
  8485.  
  8486. /** 
  8487. * @author Vova Feldman (@svovaf) 
  8488. * @since 1.2.1.5 
  8489. * @param array $params 
  8490. * @return string 
  8491. */ 
  8492. function get_reconnect_url( $params = array() ) { 
  8493. $params['fs_action'] = 'reset_anonymous_mode'; 
  8494. $params['fs_slug'] = $this->_slug; 
  8495.  
  8496. return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params ) ); 
  8497.  
  8498. /** 
  8499. * Get the URL of the page that should be loaded after the user connect or skip in the opt-in screen. 
  8500. * @author Vova Feldman (@svovaf) 
  8501. * @since 1.1.3 
  8502. * @param string $filter Filter name. 
  8503. * @return string 
  8504. */ 
  8505. function get_after_activation_url( $filter ) { 
  8506. $first_time_path = $this->_menu->get_first_time_path(); 
  8507.  
  8508. return $this->apply_filters( 
  8509. $filter,  
  8510. empty( $first_time_path ) ? 
  8511. $this->_get_admin_page_url() : 
  8512. $first_time_path 
  8513. ); 
  8514.  
  8515. /** 
  8516. * Handle account page updates / edits / actions. 
  8517. * @author Vova Feldman (@svovaf) 
  8518. * @since 1.0.2 
  8519. */ 
  8520. private function _handle_account_edits() { 
  8521. if ( ! current_user_can( 'activate_plugins' ) ) { 
  8522. return; 
  8523.  
  8524. $plugin_id = fs_request_get( 'plugin_id', $this->get_id() ); 
  8525. $action = fs_get_action(); 
  8526.  
  8527. switch ( $action ) { 
  8528. case 'delete_account': 
  8529. check_admin_referer( $action ); 
  8530.  
  8531. if ( $plugin_id == $this->get_id() ) { 
  8532. $this->delete_account_event(); 
  8533.  
  8534. // Clear user and site. 
  8535. $this->_site = null; 
  8536. $this->_user = null; 
  8537.  
  8538. fs_redirect( $this->get_activation_url() ); 
  8539. } else { 
  8540. if ( $this->is_addon_activated( $plugin_id ) ) { 
  8541. $fs_addon = self::get_instance_by_id( $plugin_id ); 
  8542. $fs_addon->delete_account_event(); 
  8543.  
  8544. fs_redirect( $this->_get_admin_page_url( 'account' ) ); 
  8545.  
  8546. return; 
  8547.  
  8548. case 'downgrade_account': 
  8549. check_admin_referer( $action ); 
  8550.  
  8551. if ( $plugin_id == $this->get_id() ) { 
  8552. $this->_downgrade_site(); 
  8553. } else if ( $this->is_addon_activated( $plugin_id ) ) { 
  8554. $fs_addon = self::get_instance_by_id( $plugin_id ); 
  8555. $fs_addon->_downgrade_site(); 
  8556.  
  8557. return; 
  8558.  
  8559. case 'activate_license': 
  8560. check_admin_referer( $action ); 
  8561.  
  8562. if ( $plugin_id == $this->get_id() ) { 
  8563. $this->_activate_license(); 
  8564. } else { 
  8565. if ( $this->is_addon_activated( $plugin_id ) ) { 
  8566. $fs_addon = self::get_instance_by_id( $plugin_id ); 
  8567. $fs_addon->_activate_license(); 
  8568.  
  8569. return; 
  8570.  
  8571. case 'deactivate_license': 
  8572. check_admin_referer( $action ); 
  8573.  
  8574. if ( $plugin_id == $this->get_id() ) { 
  8575. $this->_deactivate_license(); 
  8576. } else { 
  8577. if ( $this->is_addon_activated( $plugin_id ) ) { 
  8578. $fs_addon = self::get_instance_by_id( $plugin_id ); 
  8579. $fs_addon->_deactivate_license(); 
  8580.  
  8581. return; 
  8582.  
  8583. case 'check_updates': 
  8584. check_admin_referer( $action ); 
  8585. $this->_check_updates(); 
  8586.  
  8587. return; 
  8588.  
  8589. case 'change_owner': 
  8590. $state = fs_request_get( 'state', 'init' ); 
  8591. switch ( $state ) { 
  8592. case 'init': 
  8593. $candidate_email = fs_request_get( 'candidate_email', '' ); 
  8594.  
  8595. if ( $this->init_change_owner( $candidate_email ) ) { 
  8596. $this->_admin_notices->add( sprintf( __fs( 'change-owner-request-sent-x', $this->_slug ), '<b>' . $this->_user->email . '</b>' ) ); 
  8597. break; 
  8598. case 'owner_confirmed': 
  8599. $candidate_email = fs_request_get( 'candidate_email', '' ); 
  8600.  
  8601. $this->_admin_notices->add( sprintf( __fs( 'change-owner-request_owner-confirmed', $this->_slug ), '<b>' . $candidate_email . '</b>' ) ); 
  8602. break; 
  8603. case 'candidate_confirmed': 
  8604. if ( $this->complete_change_owner() ) { 
  8605. $this->_admin_notices->add_sticky( 
  8606. sprintf( __fs( 'change-owner-request_candidate-confirmed', $this->_slug ), '<b>' . $this->_user->email . '</b>' ),  
  8607. 'ownership_changed',  
  8608. __fs( 'congrats', $this->_slug ) . '!' 
  8609. ); 
  8610. } else { 
  8611. // @todo Handle failed ownership change message. 
  8612. break; 
  8613.  
  8614. return; 
  8615.  
  8616. case 'update_email': 
  8617. check_admin_referer( 'update_email' ); 
  8618.  
  8619. $new_email = fs_request_get( 'fs_email_' . $this->_slug, '' ); 
  8620. $result = $this->_update_email( $new_email ); 
  8621.  
  8622. if ( isset( $result->error ) ) { 
  8623. switch ( $result->error->code ) { 
  8624. case 'user_exist': 
  8625. $this->_admin_notices->add( 
  8626. __fs( 'user-exist-message', $this->_slug ) . ' ' . 
  8627. sprintf( __fs( 'user-exist-message_ownership', $this->_slug ), '<b>' . $new_email . '</b>' ) . 
  8628. sprintf( 
  8629. '<a style="margin-left: 10px;" href="%s"><button class="button button-primary">%s  ➜</button></a>',  
  8630. $this->get_account_url( 'change_owner', array( 
  8631. 'state' => 'init',  
  8632. 'candidate_email' => $new_email 
  8633. ) ),  
  8634. __fs( 'change-ownership', $this->_slug ) 
  8635. ),  
  8636. __fs( 'oops', $this->_slug ) . '...',  
  8637. 'error' 
  8638. ); 
  8639. break; 
  8640. } else { 
  8641. $this->_admin_notices->add( __fs( 'email-updated-message', $this->_slug ) ); 
  8642.  
  8643. return; 
  8644.  
  8645. case 'update_user_name': 
  8646. check_admin_referer( 'update_user_name' ); 
  8647.  
  8648. $result = $this->update_user_name(); 
  8649.  
  8650. if ( isset( $result->error ) ) { 
  8651. $this->_admin_notices->add( 
  8652. __fs( 'name-update-failed-message', $this->_slug ),  
  8653. __fs( 'oops', $this->_slug ) . '...',  
  8654. 'error' 
  8655. ); 
  8656. } else { 
  8657. $this->_admin_notices->add( __fs( 'name-updated-message', $this->_slug ) ); 
  8658.  
  8659. return; 
  8660.  
  8661. #region Actions that might be called from external links (e.g. email) 
  8662.  
  8663. case 'cancel_trial': 
  8664. if ( $plugin_id == $this->get_id() ) { 
  8665. $this->_cancel_trial(); 
  8666. } else { 
  8667. if ( $this->is_addon_activated( $plugin_id ) ) { 
  8668. $fs_addon = self::get_instance_by_id( $plugin_id ); 
  8669. $fs_addon->_cancel_trial(); 
  8670.  
  8671. return; 
  8672.  
  8673. case 'verify_email': 
  8674. $this->verify_email(); 
  8675.  
  8676. return; 
  8677.  
  8678. case 'sync_user': 
  8679. $this->_handle_account_user_sync(); 
  8680.  
  8681. return; 
  8682.  
  8683. case $this->_slug . '_sync_license': 
  8684. $this->_sync_license(); 
  8685.  
  8686. return; 
  8687.  
  8688. case 'download_latest': 
  8689. $this->_download_latest_directly( $plugin_id ); 
  8690.  
  8691. return; 
  8692.  
  8693. #endregion 
  8694.  
  8695. if ( WP_FS__IS_POST_REQUEST ) { 
  8696. $properties = array( 'site_secret_key', 'site_id', 'site_public_key' ); 
  8697. foreach ( $properties as $p ) { 
  8698. if ( 'update_' . $p === $action ) { 
  8699. check_admin_referer( $action ); 
  8700.  
  8701. $this->_logger->log( $action ); 
  8702.  
  8703. $site_property = substr( $p, strlen( 'site_' ) ); 
  8704. $site_property_value = fs_request_get( 'fs_' . $p . '_' . $this->_slug, '' ); 
  8705. $this->get_site()->{$site_property} = $site_property_value; 
  8706.  
  8707. // Store account after modification. 
  8708. $this->_store_site(); 
  8709.  
  8710. $this->do_action( 'account_property_edit', 'site', $site_property, $site_property_value ); 
  8711.  
  8712. $this->_admin_notices->add( sprintf( 
  8713. __fs( 'x-updated', $this->_slug ),  
  8714. '<b>' . str_replace( '_', ' ', $p ) . '</b>' ) ); 
  8715.  
  8716. return; 
  8717.  
  8718. /** 
  8719. * Account page resources load. 
  8720. * @author Vova Feldman (@svovaf) 
  8721. * @since 1.0.6 
  8722. */ 
  8723. function _account_page_load() { 
  8724. $this->_logger->entrance(); 
  8725.  
  8726. $this->_logger->info( var_export( $_REQUEST, true ) ); 
  8727.  
  8728. fs_enqueue_local_style( 'fs_account', '/admin/account.css' ); 
  8729.  
  8730. if ( $this->has_addons() ) { 
  8731. wp_enqueue_script( 'plugin-install' ); 
  8732. add_thickbox(); 
  8733.  
  8734. function fs_addons_body_class( $classes ) { 
  8735. $classes .= ' plugins-php'; 
  8736.  
  8737. return $classes; 
  8738.  
  8739. add_filter( 'admin_body_class', 'fs_addons_body_class' ); 
  8740.  
  8741. if ( $this->has_paid_plan() && 
  8742. ! $this->has_any_license() && 
  8743. ! $this->is_sync_executed() && 
  8744. $this->is_tracking_allowed() 
  8745. ) { 
  8746. /** 
  8747. * If no licenses found and no sync job was executed during the last 24 hours,  
  8748. * just execute the sync job right away (blocking execution). 
  8749. * @since 1.1.7.3 
  8750. */ 
  8751. $this->run_manual_sync(); 
  8752.  
  8753. $this->_handle_account_edits(); 
  8754.  
  8755. $this->do_action( 'account_page_load_before_departure' ); 
  8756.  
  8757. /** 
  8758. * Render account page. 
  8759. * @author Vova Feldman (@svovaf) 
  8760. * @since 1.0.0 
  8761. */ 
  8762. function _account_page_render() { 
  8763. $this->_logger->entrance(); 
  8764.  
  8765. $vars = array( 'slug' => $this->_slug ); 
  8766. if ( 'billing' === fs_request_get( 'tab' ) ) { 
  8767. fs_require_once_template( 'billing.php', $vars ); 
  8768. } else { 
  8769. fs_require_once_template( 'account.php', $vars ); 
  8770.  
  8771. /** 
  8772. * Render account connect page. 
  8773. * @author Vova Feldman (@svovaf) 
  8774. * @since 1.0.7 
  8775. */ 
  8776. function _connect_page_render() { 
  8777. $this->_logger->entrance(); 
  8778.  
  8779. $vars = array( 'slug' => $this->_slug ); 
  8780. fs_require_once_template( 'connect.php', $vars ); 
  8781.  
  8782. /** 
  8783. * Load required resources before add-ons page render. 
  8784. * @author Vova Feldman (@svovaf) 
  8785. * @since 1.0.6 
  8786. */ 
  8787. function _addons_page_load() { 
  8788. $this->_logger->entrance(); 
  8789.  
  8790. fs_enqueue_local_style( 'fs_addons', '/admin/add-ons.css' ); 
  8791.  
  8792. wp_enqueue_script( 'plugin-install' ); 
  8793. add_thickbox(); 
  8794.  
  8795. function fs_addons_body_class( $classes ) { 
  8796. $classes .= ' plugins-php'; 
  8797.  
  8798. return $classes; 
  8799.  
  8800. add_filter( 'admin_body_class', 'fs_addons_body_class' ); 
  8801.  
  8802. if ( ! $this->is_registered() && $this->is_org_repo_compliant() ) { 
  8803. $this->_admin_notices->add( 
  8804. sprintf( __fs( 'addons-info-external-message', $this->_slug ), '<b>' . $this->get_plugin_name() . '</b>' ),  
  8805. __fs( 'heads-up', $this->_slug ),  
  8806. 'update-nag' 
  8807. ); 
  8808.  
  8809. /** 
  8810. * Render add-ons page. 
  8811. * @author Vova Feldman (@svovaf) 
  8812. * @since 1.0.6 
  8813. */ 
  8814. function _addons_page_render() { 
  8815. $this->_logger->entrance(); 
  8816.  
  8817. $vars = array( 'slug' => $this->_slug ); 
  8818. fs_require_once_template( 'add-ons.php', $vars ); 
  8819.  
  8820. /** Pricing & Upgrade 
  8821. ------------------------------------------------------------------------------------------------------------------*/ 
  8822. /** 
  8823. * Render pricing page. 
  8824. * @author Vova Feldman (@svovaf) 
  8825. * @since 1.0.0 
  8826. */ 
  8827. function _pricing_page_render() { 
  8828. $this->_logger->entrance(); 
  8829.  
  8830. $vars = array( 'slug' => $this->_slug ); 
  8831.  
  8832. if ( 'true' === fs_request_get( 'checkout', false ) ) { 
  8833. fs_require_once_template( 'checkout.php', $vars ); 
  8834. } else { 
  8835. fs_require_once_template( 'pricing.php', $vars ); 
  8836.  
  8837. #---------------------------------------------------------------------------------- 
  8838. #region Contact Us 
  8839. #---------------------------------------------------------------------------------- 
  8840.  
  8841. /** 
  8842. * Render contact-us page. 
  8843. * @author Vova Feldman (@svovaf) 
  8844. * @since 1.0.3 
  8845. */ 
  8846. function _contact_page_render() { 
  8847. $this->_logger->entrance(); 
  8848.  
  8849. $vars = array( 'slug' => $this->_slug ); 
  8850. fs_require_once_template( 'contact.php', $vars ); 
  8851.  
  8852. #endregion ------------------------------------------------------------------------ 
  8853.  
  8854. /** 
  8855. * Hide all admin notices to prevent distractions. 
  8856. * @author Vova Feldman (@svovaf) 
  8857. * @since 1.0.3 
  8858. * @uses remove_all_actions() 
  8859. */ 
  8860. private static function _hide_admin_notices() { 
  8861. remove_all_actions( 'admin_notices' ); 
  8862. remove_all_actions( 'network_admin_notices' ); 
  8863. remove_all_actions( 'all_admin_notices' ); 
  8864. remove_all_actions( 'user_admin_notices' ); 
  8865.  
  8866. static function _clean_admin_content_section_hook() { 
  8867. self::_hide_admin_notices(); 
  8868.  
  8869. // Hide footer. 
  8870. echo '<style>#wpfooter { display: none !important; }</style>'; 
  8871.  
  8872. /** 
  8873. * Attach to admin_head hook to hide all admin notices. 
  8874. * @author Vova Feldman (@svovaf) 
  8875. * @since 1.0.3 
  8876. */ 
  8877. static function _clean_admin_content_section() { 
  8878. add_action( 'admin_head', 'Freemius::_clean_admin_content_section_hook' ); 
  8879.  
  8880. /** CSS & JavaScript 
  8881. ------------------------------------------------------------------------------------------------------------------*/ 
  8882. /** function _enqueue_script($handle, $src) { 
  8883. $url = plugins_url( substr( WP_FS__DIR_JS, strlen( $this->_plugin_dir_path ) ) . '/assets/js/' . $src ); 
  8884.   
  8885. $this->_logger->entrance( 'script = ' . $url ); 
  8886.   
  8887. wp_enqueue_script( $handle, $url ); 
  8888. }*/ 
  8889.  
  8890. /** SDK 
  8891. ------------------------------------------------------------------------------------------------------------------*/ 
  8892. private $_user_api; 
  8893.  
  8894. /** 
  8895. * @author Vova Feldman (@svovaf) 
  8896. * @since 1.0.2 
  8897. * @param bool $flush 
  8898. * @return FS_Api 
  8899. */ 
  8900. function get_api_user_scope( $flush = false ) { 
  8901. if ( ! isset( $this->_user_api ) || $flush ) { 
  8902. $this->_user_api = FS_Api::instance( 
  8903. $this->_slug,  
  8904. 'user',  
  8905. $this->_user->id,  
  8906. $this->_user->public_key,  
  8907. ! $this->is_live(),  
  8908. $this->_user->secret_key 
  8909. ); 
  8910.  
  8911. return $this->_user_api; 
  8912.  
  8913. private $_site_api; 
  8914.  
  8915. /** 
  8916. * @author Vova Feldman (@svovaf) 
  8917. * @since 1.0.2 
  8918. * @param bool $flush 
  8919. * @return FS_Api 
  8920. */ 
  8921. function get_api_site_scope( $flush = false ) { 
  8922. if ( ! isset( $this->_site_api ) || $flush ) { 
  8923. $this->_site_api = FS_Api::instance( 
  8924. $this->_slug,  
  8925. 'install',  
  8926. $this->_site->id,  
  8927. $this->_site->public_key,  
  8928. ! $this->is_live(),  
  8929. $this->_site->secret_key 
  8930. ); 
  8931.  
  8932. return $this->_site_api; 
  8933.  
  8934. private $_plugin_api; 
  8935.  
  8936. /** 
  8937. * Get plugin public API scope. 
  8938. * @author Vova Feldman (@svovaf) 
  8939. * @since 1.0.7 
  8940. * @return FS_Api 
  8941. */ 
  8942. function get_api_plugin_scope() { 
  8943. if ( ! isset( $this->_plugin_api ) ) { 
  8944. $this->_plugin_api = FS_Api::instance( 
  8945. $this->_slug,  
  8946. 'plugin',  
  8947. $this->_plugin->id,  
  8948. $this->_plugin->public_key,  
  8949. ! $this->is_live() 
  8950. ); 
  8951.  
  8952. return $this->_plugin_api; 
  8953.  
  8954. /** 
  8955. * Get site API scope object (fallback to public plugin scope when not registered). 
  8956. * @author Vova Feldman (@svovaf) 
  8957. * @since 1.0.7 
  8958. * @return FS_Api 
  8959. */ 
  8960. function get_api_site_or_plugin_scope() { 
  8961. return $this->is_registered() ? 
  8962. $this->get_api_site_scope() : 
  8963. $this->get_api_plugin_scope(); 
  8964.  
  8965. /** 
  8966. * Show trial promotional notice (if any trial exist). 
  8967. * @author Vova Feldman (@svovaf) 
  8968. * @since 1.0.9 
  8969. * @param $plans 
  8970. */ 
  8971. function _check_for_trial_plans( $plans ) { 
  8972. $this->_storage->has_trial_plan = FS_Plan_Manager::instance()->has_trial_plan( $plans ); 
  8973.  
  8974. /** 
  8975. * During trial promotion the "upgrade" submenu item turns to 
  8976. * "start trial" to encourage the trial. Since we want to keep 
  8977. * the same menu item handler and there's no robust way to 
  8978. * add new arguments to the menu item link's querystring,  
  8979. * use JavaScript to find the menu item and update the href of 
  8980. * the link. 
  8981. * @author Vova Feldman (@svovaf) 
  8982. * @since 1.2.1.5 
  8983. */ 
  8984. function _fix_start_trial_menu_item_url() { 
  8985. $template_args = array( 'slug' => $this->_slug ); 
  8986. fs_require_template( 'add-trial-to-pricing.php', $template_args ); 
  8987.  
  8988. /** 
  8989. * Show trial promotional notice (if any trial exist). 
  8990. * @author Vova Feldman (@svovaf) 
  8991. * @since 1.0.9 
  8992. * @return bool If trial notice added. 
  8993. */ 
  8994. function _add_trial_notice() { 
  8995. if ( ! current_user_can( 'activate_plugins' ) ) { 
  8996. return false; 
  8997.  
  8998. if ( ! $this->is_user_in_admin() ) { 
  8999. return false; 
  9000.  
  9001. // Check if trial message is already shown. 
  9002. if ( $this->_admin_notices->has_sticky( 'trial_promotion' ) ) { 
  9003. add_action( 'admin_footer', array( &$this, '_fix_start_trial_menu_item_url' ) ); 
  9004.  
  9005. $this->_menu->add_counter_to_menu_item( 1, 'fs-trial' ); 
  9006.  
  9007. return false; 
  9008.  
  9009. if ( $this->is_premium() && ! WP_FS__DEV_MODE ) { 
  9010. // Don't show trial if running the premium code, unless running in DEV mode. 
  9011. return false; 
  9012.  
  9013. if ( ! $this->has_trial_plan() ) { 
  9014. // No plans with trial. 
  9015. return false; 
  9016.  
  9017. if ( ! $this->apply_filters( 'show_trial', true ) ) { 
  9018. // Developer explicitly asked not to show the trial promo. 
  9019. return false; 
  9020.  
  9021. if ( $this->is_registered() ) { 
  9022. // Check if trial already utilized. 
  9023. if ( $this->_site->is_trial_utilized() ) { 
  9024. return false; 
  9025.  
  9026. if ( $this->is_paying_or_trial() ) { 
  9027. // Don't show trial if paying or already in trial. 
  9028. return false; 
  9029.  
  9030. if ( $this->is_activation_mode() || $this->is_pending_activation() ) { 
  9031. // If not yet opted-in/skipped, or pending activation, don't show trial. 
  9032. return false; 
  9033.  
  9034. $last_time_trial_promotion_shown = $this->_storage->get( 'trial_promotion_shown', false ); 
  9035. $was_promotion_shown_before = ( false !== $last_time_trial_promotion_shown ); 
  9036.  
  9037. // Show promotion if never shown before and 24 hours after initial activation with FS. 
  9038. if ( ! $was_promotion_shown_before && 
  9039. $this->_storage->install_timestamp > ( time() - WP_FS__TIME_24_HOURS_IN_SEC ) 
  9040. ) { 
  9041. return false; 
  9042.  
  9043. // OR if promotion was shown before, try showing it every 30 days. 
  9044. if ( $was_promotion_shown_before && 
  9045. 30 * WP_FS__TIME_24_HOURS_IN_SEC > time() - $last_time_trial_promotion_shown 
  9046. ) { 
  9047. return false; 
  9048.  
  9049. $trial_period = $this->_trial_days; 
  9050. $require_payment = $this->_is_trial_require_payment; 
  9051. $trial_url = $this->get_trial_url(); 
  9052. $plans_string = strtolower( __fs( 'awesome', $this->_slug ) ); 
  9053.  
  9054. if ( $this->is_registered() ) { 
  9055. // If opted-in, override trial with up to date data from API. 
  9056. $trial_plans = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans ); 
  9057. $trial_plans_count = count( $trial_plans ); 
  9058.  
  9059. if ( 0 === $trial_plans_count ) { 
  9060. // If there's no plans with a trial just exit. 
  9061. return false; 
  9062.  
  9063. /** 
  9064. * @var FS_Plugin_Plan $paid_plan 
  9065. */ 
  9066. $paid_plan = $trial_plans[0]; 
  9067. $require_payment = $paid_plan->is_require_subscription; 
  9068. $trial_period = $paid_plan->trial_period; 
  9069.  
  9070. $total_paid_plans = count( $this->_plans ) - ( FS_Plan_Manager::instance()->has_free_plan( $this->_plans ) ? 1 : 0 ); 
  9071.  
  9072. if ( $total_paid_plans !== $trial_plans_count ) { 
  9073. // Not all paid plans have a trial - generate a string of those that have it. 
  9074. for ( $i = 0; $i < $trial_plans_count; $i ++ ) { 
  9075. $plans_string .= sprintf( 
  9076. '<a href="%s">%s</a>',  
  9077. $trial_url,  
  9078. $trial_plans[ $i ]->title 
  9079. ); 
  9080.  
  9081. if ( $i < $trial_plans_count - 2 ) { 
  9082. $plans_string .= ', '; 
  9083. } else if ( $i == $trial_plans_count - 2 ) { 
  9084. $plans_string .= ' and '; 
  9085.  
  9086. $message = sprintf( 
  9087. __fs( 'hey', $this->_slug ) . '! ' . __fs( 'trial-x-promotion-message', $this->_slug ),  
  9088. sprintf( '<b>%s</b>', $this->get_plugin_name() ),  
  9089. $plans_string,  
  9090. $trial_period 
  9091. ); 
  9092.  
  9093. // "No Credit-Card Required" or "No Commitment for N Days". 
  9094. $cc_string = $require_payment ? 
  9095. sprintf( __fs( 'no-commitment-for-x-days', $this->_slug ), $trial_period ) : 
  9096. __fs( 'no-cc-required', $this->_slug ) . '!'; 
  9097.  
  9098.  
  9099. // Start trial button. 
  9100. $button = ' ' . sprintf( 
  9101. '<a style="margin-left: 10px; vertical-align: super;" href="%s"><button class="button button-primary">%s  ➜</button></a>',  
  9102. $trial_url,  
  9103. __fs( 'start-free-trial', $this->_slug ) 
  9104. ); 
  9105.  
  9106. $this->_admin_notices->add_sticky( 
  9107. $this->apply_filters( 'trial_promotion_message', "{$message} {$cc_string} {$button}" ),  
  9108. 'trial_promotion',  
  9109. '',  
  9110. 'promotion' 
  9111. ); 
  9112.  
  9113. $this->_storage->trial_promotion_shown = WP_FS__SCRIPT_START_TIME; 
  9114.  
  9115. return true; 
  9116.  
  9117. /** 
  9118. * @author Vova Feldman (@svovaf) 
  9119. * @since 1.2.1.5 
  9120. */ 
  9121. function _enqueue_common_css() { 
  9122. if ( $this->has_paid_plan() && ! $this->is_paying() ) { 
  9123. // Add basic CSS for admin-notices and menu-item colors. 
  9124. fs_enqueue_local_style( 'fs_common', '/admin/common.css' ); 
  9125.  
  9126. /** Action Links 
  9127. ------------------------------------------------------------------------------------------------------------------*/ 
  9128. private $_action_links_hooked = false; 
  9129. private $_action_links = array(); 
  9130.  
  9131. /** 
  9132. * Hook to plugin action links filter. 
  9133. * @author Vova Feldman (@svovaf) 
  9134. * @since 1.0.0 
  9135. */ 
  9136. private function hook_plugin_action_links() { 
  9137. $this->_logger->entrance(); 
  9138.  
  9139. $this->_action_links_hooked = true; 
  9140.  
  9141. $this->_logger->log( 'Adding action links hooks.' ); 
  9142.  
  9143. // Add action link to settings page. 
  9144. add_filter( 'plugin_action_links_' . $this->_plugin_basename, array( 
  9145. &$this,  
  9146. '_modify_plugin_action_links_hook' 
  9147. ), WP_FS__DEFAULT_PRIORITY, 2 ); 
  9148. add_filter( 'network_admin_plugin_action_links_' . $this->_plugin_basename, array( 
  9149. &$this,  
  9150. '_modify_plugin_action_links_hook' 
  9151. ), WP_FS__DEFAULT_PRIORITY, 2 ); 
  9152.  
  9153. /** 
  9154. * Add plugin action link. 
  9155. * @author Vova Feldman (@svovaf) 
  9156. * @since 1.0.0 
  9157. * @param $label 
  9158. * @param $url 
  9159. * @param bool $external 
  9160. * @param int $priority 
  9161. * @param bool $key 
  9162. */ 
  9163. function add_plugin_action_link( $label, $url, $external = false, $priority = WP_FS__DEFAULT_PRIORITY, $key = false ) { 
  9164. $this->_logger->entrance(); 
  9165.  
  9166. if ( ! isset( $this->_action_links[ $priority ] ) ) { 
  9167. $this->_action_links[ $priority ] = array(); 
  9168.  
  9169. if ( false === $key ) { 
  9170. $key = preg_replace( "/[^A-Za-z0-9 ]/", '', strtolower( $label ) ); 
  9171.  
  9172. $this->_action_links[ $priority ][] = array( 
  9173. 'label' => $label,  
  9174. 'href' => $url,  
  9175. 'key' => $key,  
  9176. 'external' => $external 
  9177. ); 
  9178.  
  9179. /** 
  9180. * Adds Upgrade and Add-Ons links to the main Plugins page link actions collection. 
  9181. * @author Vova Feldman (@svovaf) 
  9182. * @since 1.0.0 
  9183. */ 
  9184. function _add_upgrade_action_link() { 
  9185. $this->_logger->entrance(); 
  9186.  
  9187. if ( $this->is_registered() ) { 
  9188. if ( ! $this->is_paying() && $this->has_paid_plan() ) { 
  9189. $this->add_plugin_action_link( 
  9190. __fs( 'upgrade', $this->_slug ),  
  9191. $this->get_upgrade_url(),  
  9192. false,  
  9193. 7,  
  9194. 'upgrade' 
  9195. ); 
  9196.  
  9197. if ( $this->has_addons() ) { 
  9198. $this->add_plugin_action_link( 
  9199. __fs( 'add-ons', $this->_slug ),  
  9200. $this->_get_admin_page_url( 'addons' ),  
  9201. false,  
  9202. 9,  
  9203. 'addons' 
  9204. ); 
  9205.  
  9206. /** 
  9207. * Adds "Activate License" or "Change License" link to the main Plugins page link actions collection. 
  9208. * @author Leo Fajardo (@leorw) 
  9209. * @since 1.1.9 
  9210. */ 
  9211. function _add_license_action_link() { 
  9212. $this->_logger->entrance(); 
  9213.  
  9214. if ( $this->is_free_plan() && $this->is_addon() ) { 
  9215. return; 
  9216.  
  9217. if ( ! $this->is_ajax() ) { 
  9218. // Inject license activation dialog UI and client side code. 
  9219. add_action( 'admin_footer', array( &$this, '_add_license_activation_dialog_box' ) ); 
  9220.  
  9221. $link_text = __fs( 
  9222. $this->is_free_plan() ? 'activate-license' : 'change-license',  
  9223. $this->_slug 
  9224. ); 
  9225.  
  9226. $this->add_plugin_action_link( 
  9227. $link_text,  
  9228. '#',  
  9229. false,  
  9230. 11,  
  9231. ( 'activate-license ' . $this->_slug ) 
  9232. ); 
  9233.  
  9234. /** 
  9235. * Adds "Opt in" or "Opt out" link to the main "Plugins" page link actions collection. 
  9236. * @author Leo Fajardo (@leorw) 
  9237. * @since 1.2.1.5 
  9238. */ 
  9239. function _add_tracking_links() { 
  9240. if ( ! current_user_can( 'activate_plugins' ) ) { 
  9241. return; 
  9242.  
  9243. $this->_logger->entrance(); 
  9244.  
  9245. if ( ! $this->is_enable_anonymous() ) { 
  9246. // Don't allow to opt-out if anonymous mode is disabled. 
  9247. return; 
  9248.  
  9249. if ( ! $this->is_free_plan() ) { 
  9250. // Don't allow to opt-out if running in paid plan. 
  9251. return; 
  9252.  
  9253. if ( $this->add_ajax_action( 'stop_tracking', array( &$this, '_stop_tracking_callback' ) ) ) { 
  9254. return; 
  9255.  
  9256. if ( $this->add_ajax_action( 'allow_tracking', array( &$this, '_allow_tracking_callback' ) ) ) { 
  9257. return; 
  9258.  
  9259. if ( fs_request_is_action_secure( $this->_slug . '_reconnect' ) ) { 
  9260. if ( ! $this->is_registered() && $this->is_anonymous() ) { 
  9261. $this->connect_again(); 
  9262.  
  9263. return; 
  9264.  
  9265. $url = '#'; 
  9266.  
  9267. if ( $this->is_registered() ) { 
  9268. if ( $this->is_tracking_allowed() ) { 
  9269. $link_text_id = 'opt-out'; 
  9270. } else { 
  9271. $link_text_id = 'opt-in'; 
  9272.  
  9273. add_action( 'admin_footer', array( &$this, '_add_optout_dialog' ) ); 
  9274. } else { 
  9275. $link_text_id = 'opt-in'; 
  9276.  
  9277. $params = ! $this->is_anonymous() ? 
  9278. array() : 
  9279. array( 
  9280. 'nonce' => wp_create_nonce( $this->_slug . '_reconnect' ),  
  9281. 'fs_action' => ( $this->_slug . '_reconnect' ),  
  9282. ); 
  9283.  
  9284. $url = $this->get_activation_url( $params ); 
  9285.  
  9286. $this->add_plugin_action_link( 
  9287. __fs( $link_text_id, $this->_slug ),  
  9288. $url,  
  9289. false,  
  9290. 13,  
  9291. "opt-in-or-opt-out {$this->_slug}" 
  9292. ); 
  9293.  
  9294. /** 
  9295. * Get the URL of the page that should be loaded right after the plugin activation. 
  9296. * @author Vova Feldman (@svovaf) 
  9297. * @since 1.1.7.4 
  9298. * @return string 
  9299. */ 
  9300. function get_after_plugin_activation_redirect_url() { 
  9301. $url = false; 
  9302. $plugin_fs = false; 
  9303.  
  9304. if ( ! $this->is_addon() ) { 
  9305. $first_time_path = $this->_menu->get_first_time_path(); 
  9306. $plugin_fs = $this; 
  9307. $url = $plugin_fs->is_activation_mode() ? 
  9308. $plugin_fs->get_activation_url() : 
  9309. ( empty( $first_time_path ) ? 
  9310. $this->_get_admin_page_url() : 
  9311. $first_time_path ); 
  9312. } else { 
  9313. if ( $this->is_parent_plugin_installed() ) { 
  9314. $plugin_fs = self::get_parent_instance(); 
  9315.  
  9316. if ( is_object( $plugin_fs ) ) { 
  9317. if ( ! $plugin_fs->is_registered() ) { 
  9318. // Forward to parent plugin connect when parent not registered. 
  9319. $url = $plugin_fs->get_activation_url(); 
  9320. } else { 
  9321. // Forward to account page. 
  9322. $url = $plugin_fs->_get_admin_page_url( 'account' ); 
  9323.  
  9324. return $url; 
  9325.  
  9326. /** 
  9327. * Forward page to activation page. 
  9328. * @author Vova Feldman (@svovaf) 
  9329. * @since 1.0.3 
  9330. */ 
  9331. function _redirect_on_activation_hook() { 
  9332. $url = $this->get_after_plugin_activation_redirect_url(); 
  9333.  
  9334. if ( is_string( $url ) ) { 
  9335. fs_redirect( $url ); 
  9336.  
  9337. /** 
  9338. * Modify plugin's page action links collection. 
  9339. * @author Vova Feldman (@svovaf) 
  9340. * @since 1.0.0 
  9341. * @param array $links 
  9342. * @param $file 
  9343. * @return array 
  9344. */ 
  9345. function _modify_plugin_action_links_hook( $links, $file ) { 
  9346. $this->_logger->entrance(); 
  9347.  
  9348. $passed_deactivate = false; 
  9349. $deactivate_link = ''; 
  9350. $before_deactivate = array(); 
  9351. $after_deactivate = array(); 
  9352. foreach ( $links as $key => $link ) { 
  9353. if ( 'deactivate' === $key ) { 
  9354. $deactivate_link = $link; 
  9355. $passed_deactivate = true; 
  9356. continue; 
  9357.  
  9358. if ( ! $passed_deactivate ) { 
  9359. $before_deactivate[ $key ] = $link; 
  9360. } else { 
  9361. $after_deactivate[ $key ] = $link; 
  9362.  
  9363. ksort( $this->_action_links ); 
  9364.  
  9365. foreach ( $this->_action_links as $new_links ) { 
  9366. foreach ( $new_links as $link ) { 
  9367. $before_deactivate[ $link['key'] ] = '<a href="' . $link['href'] . '"' . ( $link['external'] ? ' target="_blank"' : '' ) . '>' . $link['label'] . '</a>'; 
  9368.  
  9369. if ( ! empty( $deactivate_link ) ) { 
  9370. if ( ! $this->is_paying_or_trial() || $this->is_premium() ) { 
  9371. /** 
  9372. * This HTML element is used to identify the correct plugin when attaching an event to its Deactivate link. 
  9373. * If user is paying or in trial and have the free version installed,  
  9374. * assume that the deactivation is for the upgrade process, so this is not needed. 
  9375. */ 
  9376. $deactivate_link .= '<i class="fs-slug" data-slug="' . $this->_slug . '"></i>'; 
  9377.  
  9378. // Append deactivation link. 
  9379. $before_deactivate['deactivate'] = $deactivate_link; 
  9380.  
  9381. return array_merge( $before_deactivate, $after_deactivate ); 
  9382.  
  9383. /** 
  9384. * Adds admin message. 
  9385. * @author Vova Feldman (@svovaf) 
  9386. * @since 1.0.4 
  9387. * @param string $message 
  9388. * @param string $title 
  9389. * @param string $type 
  9390. */ 
  9391. function add_admin_message( $message, $title = '', $type = 'success' ) { 
  9392. $this->_admin_notices->add( $message, $title, $type ); 
  9393.  
  9394. /** 
  9395. * Adds sticky admin message. 
  9396. * @author Vova Feldman (@svovaf) 
  9397. * @since 1.1.0 
  9398. * @param string $message 
  9399. * @param string $id 
  9400. * @param string $title 
  9401. * @param string $type 
  9402. */ 
  9403. function add_sticky_admin_message( $message, $id, $title = '', $type = 'success' ) { 
  9404. $this->_admin_notices->add_sticky( $message, $id, $title, $type ); 
  9405.  
  9406. /** 
  9407. * Helper function that returns the final steps for the upgrade completion. 
  9408. * If the module is already running the premium code, returns an empty string. 
  9409. * @author Vova Feldman (@svovaf) 
  9410. * @since 1.2.1 
  9411. * @param string $plan_title 
  9412. * @return string 
  9413. */ 
  9414. private function get_complete_upgrade_instructions( $plan_title = '' ) { 
  9415. if ( $this->is_premium() ) { 
  9416. return ''; 
  9417.  
  9418. if ( empty( $plan_title ) ) { 
  9419. $plan_title = $this->_site->plan->title; 
  9420.  
  9421. // @since 1.2.1.5 The free version is auto deactivated. 
  9422. $deactivation_step = version_compare( $this->version, '1.2.1.5', '<' ) ? 
  9423. ( '<li>' . __fs( 'deactivate-free-version', $this->_slug ) . '.</li>' ) : 
  9424. ''; 
  9425.  
  9426. return sprintf( 
  9427. ' %s: <ol><li>%s.</li>%s<li>%s (<a href="%s" target="_blank">%s</a>).</li></ol>',  
  9428. __fs( 'follow-steps-to-complete-upgrade', $this->_slug ),  
  9429. $this->_get_latest_download_link( sprintf( 
  9430. __fs( 'download-latest-x-version', $this->_slug ),  
  9431. $plan_title 
  9432. ) ),  
  9433. $deactivation_step,  
  9434. __fs( 'upload-and-activate', $this->_slug ),  
  9435. '//bit.ly/upload-wp-plugin',  
  9436. __fs( 'howto-upload-activate', $this->_slug ) 
  9437. ); 
  9438.  
  9439. /** Plugin Auto-Updates (@since 1.0.4) 
  9440. ------------------------------------------------------------------------------------------------------------------*/ 
  9441. /** 
  9442. * @var string[] 
  9443. */ 
  9444. private static $_auto_updated_plugins; 
  9445.  
  9446. /** 
  9447. * @todo TEST IF IT WORKS!!! 
  9448. * Include plugins for automatic updates based on stored settings. 
  9449. * @see http://wordpress.stackexchange.com/questions/131394/how-do-i-exclude-plugins-from-getting-automatically-updated/131404#131404 
  9450. * @author Vova Feldman (@svovaf) 
  9451. * @since 1.0.4 
  9452. * @param bool $update Whether to update (not used for plugins) 
  9453. * @param object $item The plugin's info 
  9454. * @return bool 
  9455. */ 
  9456. static function _include_plugins_in_auto_update( $update, $item ) { 
  9457. // Before version 3.8.2 the $item was the file name of the plugin,  
  9458. // while in 3.8.2 statistics were added (https://core.trac.wordpress.org/changeset/27905). 
  9459. $by_slug = ( (int) str_replace( '.', '', get_bloginfo( 'version' ) ) >= 382 ); 
  9460.  
  9461. if ( ! isset( self::$_auto_updated_plugins ) ) { 
  9462. $plugins = self::$_accounts->get_option( 'plugins', array() ); 
  9463.  
  9464. $identifiers = array(); 
  9465. foreach ( $plugins as $p ) { 
  9466. /** 
  9467. * @var FS_Plugin $p 
  9468. */ 
  9469. if ( isset( $p->auto_update ) && $p->auto_update ) { 
  9470. $identifiers[] = ( $by_slug ? $p->slug : plugin_basename( $p->file ) ); 
  9471.  
  9472. self::$_auto_updated_plugins = $identifiers; 
  9473.  
  9474. if ( in_array( $by_slug ? $item->slug : $item, self::$_auto_updated_plugins ) ) { 
  9475. return true; 
  9476.  
  9477. // Pass update decision to next filters 
  9478. return $update; 
  9479.  
  9480. #---------------------------------------------------------------------------------- 
  9481. #region Versioning 
  9482. #---------------------------------------------------------------------------------- 
  9483.  
  9484. /** 
  9485. * Check if Freemius in SDK upgrade mode. 
  9486. * @author Vova Feldman (@svovaf) 
  9487. * @since 1.0.9 
  9488. * @return bool 
  9489. */ 
  9490. function is_sdk_upgrade_mode() { 
  9491. return isset( $this->_storage->sdk_upgrade_mode ) ? 
  9492. $this->_storage->sdk_upgrade_mode : 
  9493. false; 
  9494.  
  9495. /** 
  9496. * Turn SDK upgrade mode off. 
  9497. * @author Vova Feldman (@svovaf) 
  9498. * @since 1.0.9 
  9499. */ 
  9500. function set_sdk_upgrade_complete() { 
  9501. $this->_storage->sdk_upgrade_mode = false; 
  9502.  
  9503. /** 
  9504. * Check if plugin upgrade mode. 
  9505. * @author Vova Feldman (@svovaf) 
  9506. * @since 1.0.9 
  9507. * @return bool 
  9508. */ 
  9509. function is_plugin_upgrade_mode() { 
  9510. return isset( $this->_storage->plugin_upgrade_mode ) ? 
  9511. $this->_storage->plugin_upgrade_mode : 
  9512. false; 
  9513.  
  9514. /** 
  9515. * Turn plugin upgrade mode off. 
  9516. * @author Vova Feldman (@svovaf) 
  9517. * @since 1.0.9 
  9518. * @return bool 
  9519. */ 
  9520. function set_plugin_upgrade_complete() { 
  9521. $this->_storage->plugin_upgrade_mode = false; 
  9522.  
  9523. #endregion 
  9524.  
  9525. #---------------------------------------------------------------------------------- 
  9526. #region Permissions 
  9527. #---------------------------------------------------------------------------------- 
  9528.  
  9529. /** 
  9530. * Check if specific permission requested. 
  9531. * @author Vova Feldman (@svovaf) 
  9532. * @since 1.1.6 
  9533. * @param string $permission 
  9534. * @return bool 
  9535. */ 
  9536. function is_permission_requested( $permission ) { 
  9537. return isset( $this->_permissions[ $permission ] ) && ( true === $this->_permissions[ $permission ] ); 
  9538.  
  9539. #endregion 
  9540.  
  9541. #---------------------------------------------------------------------------------- 
  9542. #region Marketing 
  9543. #---------------------------------------------------------------------------------- 
  9544.  
  9545. /** 
  9546. * Check if current user purchased any other plugins before. 
  9547. * @author Vova Feldman (@svovaf) 
  9548. * @since 1.0.9 
  9549. * @return bool 
  9550. */ 
  9551. function has_purchased_before() { 
  9552. // TODO: Implement has_purchased_before() method. 
  9553. throw new Exception( 'not implemented' ); 
  9554.  
  9555. /** 
  9556. * Check if current user classified as an agency. 
  9557. * @author Vova Feldman (@svovaf) 
  9558. * @since 1.0.9 
  9559. * @return bool 
  9560. */ 
  9561. function is_agency() { 
  9562. // TODO: Implement is_agency() method. 
  9563. throw new Exception( 'not implemented' ); 
  9564.  
  9565. /** 
  9566. * Check if current user classified as a developer. 
  9567. * @author Vova Feldman (@svovaf) 
  9568. * @since 1.0.9 
  9569. * @return bool 
  9570. */ 
  9571. function is_developer() { 
  9572. // TODO: Implement is_developer() method. 
  9573. throw new Exception( 'not implemented' ); 
  9574.  
  9575. /** 
  9576. * Check if current user classified as a business. 
  9577. * @author Vova Feldman (@svovaf) 
  9578. * @since 1.0.9 
  9579. * @return bool 
  9580. */ 
  9581. function is_business() { 
  9582. // TODO: Implement is_business() method. 
  9583. throw new Exception( 'not implemented' ); 
  9584.  
  9585. #endregion