| 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.6 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class FS_Plugin_Manager { |
| 14 |
/** |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
protected $_slug; |
| 18 |
/** |
| 19 |
* @var FS_Plugin |
| 20 |
*/ |
| 21 |
protected $_plugin; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var FS_Plugin_Manager[] |
| 25 |
*/ |
| 26 |
private static $_instances = array(); |
| 27 |
/** |
| 28 |
* @var FS_Logger |
| 29 |
*/ |
| 30 |
protected $_logger; |
| 31 |
|
| 32 |
/** |
| 33 |
* @param string $slug |
| 34 |
* |
| 35 |
* @return FS_Plugin_Manager |
| 36 |
*/ |
| 37 |
static function instance( $slug ) { |
| 38 |
if ( ! isset( self::$_instances[ $slug ] ) ) { |
| 39 |
self::$_instances[ $slug ] = new FS_Plugin_Manager( $slug ); |
| 40 |
} |
| 41 |
|
| 42 |
return self::$_instances[ $slug ]; |
| 43 |
} |
| 44 |
|
| 45 |
protected function __construct( $slug ) { |
| 46 |
$this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $slug . '_' . 'plugins', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); |
| 47 |
|
| 48 |
$this->_slug = $slug; |
| 49 |
$this->load(); |
| 50 |
} |
| 51 |
|
| 52 |
protected function get_option_manager() { |
| 53 |
return FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true ); |
| 54 |
} |
| 55 |
|
| 56 |
protected function get_all_plugins() { |
| 57 |
return $this->get_option_manager()->get_option( 'plugins', array() ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Load plugin data from local DB. |
| 62 |
* |
| 63 |
* @author Vova Feldman (@svovaf) |
| 64 |
* @since 1.0.6 |
| 65 |
*/ |
| 66 |
function load() { |
| 67 |
$all_plugins = $this->get_all_plugins(); |
| 68 |
$this->_plugin = isset( $all_plugins[ $this->_slug ] ) ? |
| 69 |
$all_plugins[ $this->_slug ] : |
| 70 |
null; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Store plugin on local DB. |
| 75 |
* |
| 76 |
* @author Vova Feldman (@svovaf) |
| 77 |
* @since 1.0.6 |
| 78 |
* |
| 79 |
* @param bool|FS_Plugin $plugin |
| 80 |
* @param bool $flush |
| 81 |
* |
| 82 |
* @return bool|\FS_Plugin |
| 83 |
*/ |
| 84 |
function store( $plugin = false, $flush = true ) { |
| 85 |
$all_plugins = $this->get_all_plugins(); |
| 86 |
|
| 87 |
if ( false !== $plugin ) { |
| 88 |
$this->_plugin = $plugin; |
| 89 |
} |
| 90 |
|
| 91 |
$all_plugins[ $this->_slug ] = $this->_plugin; |
| 92 |
|
| 93 |
$options_manager = $this->get_option_manager(); |
| 94 |
$options_manager->set_option( 'plugins', $all_plugins, $flush ); |
| 95 |
|
| 96 |
return $this->_plugin; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Update local plugin data if different. |
| 101 |
* |
| 102 |
* @author Vova Feldman (@svovaf) |
| 103 |
* @since 1.0.6 |
| 104 |
* |
| 105 |
* @param \FS_Plugin $plugin |
| 106 |
* @param bool $store |
| 107 |
* |
| 108 |
* @return bool True if plugin was updated. |
| 109 |
*/ |
| 110 |
function update( FS_Plugin $plugin, $store = true ) { |
| 111 |
if ( ! ( $this->_plugin instanceof FS_Plugin ) || |
| 112 |
$this->_plugin->slug != $plugin->slug || |
| 113 |
$this->_plugin->public_key != $plugin->public_key || |
| 114 |
$this->_plugin->secret_key != $plugin->secret_key || |
| 115 |
$this->_plugin->parent_plugin_id != $plugin->parent_plugin_id || |
| 116 |
$this->_plugin->title != $plugin->title |
| 117 |
) { |
| 118 |
$this->store( $plugin, $store ); |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @author Vova Feldman (@svovaf) |
| 128 |
* @since 1.0.6 |
| 129 |
* |
| 130 |
* @param FS_Plugin $plugin |
| 131 |
* @param bool $store |
| 132 |
*/ |
| 133 |
function set( FS_Plugin $plugin, $store = false ) { |
| 134 |
$this->_plugin = $plugin; |
| 135 |
|
| 136 |
if ( $store ) { |
| 137 |
$this->store(); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* @author Vova Feldman (@svovaf) |
| 143 |
* @since 1.0.6 |
| 144 |
* |
| 145 |
* @return bool|\FS_Plugin |
| 146 |
*/ |
| 147 |
function get() { |
| 148 |
return isset( $this->_plugin ) ? |
| 149 |
$this->_plugin : |
| 150 |
false; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
} |