/freemius/includes/class-freemius.php

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