class-fs-admin-menu-manager.php
2 years ago
class-fs-admin-notice-manager.php
2 years ago
class-fs-cache-manager.php
2 years ago
class-fs-clone-manager.php
2 years ago
class-fs-gdpr-manager.php
2 years ago
class-fs-key-value-storage.php
2 years ago
class-fs-license-manager.php
2 years ago
class-fs-option-manager.php
2 years ago
class-fs-permission-manager.php
2 years ago
class-fs-plan-manager.php
2 years ago
class-fs-plugin-manager.php
2 years ago
index.php
2 years ago
class-fs-plugin-manager.php
233 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package Freemius |
| 4 | * @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 | * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 |
| 6 | * @since 1.0.6 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | class FS_Plugin_Manager { |
| 14 | /** |
| 15 | * @since 1.2.2 |
| 16 | * |
| 17 | * @var string|number |
| 18 | */ |
| 19 | protected $_module_id; |
| 20 | /** |
| 21 | * @since 1.2.2 |
| 22 | * |
| 23 | * @var FS_Plugin |
| 24 | */ |
| 25 | protected $_module; |
| 26 | |
| 27 | /** |
| 28 | * @var FS_Plugin_Manager[] |
| 29 | */ |
| 30 | private static $_instances = array(); |
| 31 | /** |
| 32 | * @var FS_Logger |
| 33 | */ |
| 34 | protected $_logger; |
| 35 | |
| 36 | /** |
| 37 | * Option names |
| 38 | * |
| 39 | * @author Leo Fajardo (@leorw) |
| 40 | * @since 1.2.2 |
| 41 | */ |
| 42 | const OPTION_NAME_PLUGINS = 'plugins'; |
| 43 | const OPTION_NAME_THEMES = 'themes'; |
| 44 | |
| 45 | /** |
| 46 | * @param string|number $module_id |
| 47 | * |
| 48 | * @return FS_Plugin_Manager |
| 49 | */ |
| 50 | static function instance( $module_id ) { |
| 51 | $key = 'm_' . $module_id; |
| 52 | |
| 53 | if ( ! isset( self::$_instances[ $key ] ) ) { |
| 54 | self::$_instances[ $key ] = new FS_Plugin_Manager( $module_id ); |
| 55 | } |
| 56 | |
| 57 | return self::$_instances[ $key ]; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param string|number $module_id |
| 62 | */ |
| 63 | protected function __construct( $module_id ) { |
| 64 | $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $module_id . '_' . 'plugins', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); |
| 65 | $this->_module_id = $module_id; |
| 66 | |
| 67 | $this->load(); |
| 68 | } |
| 69 | |
| 70 | protected function get_option_manager() { |
| 71 | return FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true, true ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @author Leo Fajardo (@leorw) |
| 76 | * @since 1.2.2 |
| 77 | * |
| 78 | * @param string|bool $module_type "plugin", "theme", or "false" for all modules. |
| 79 | * |
| 80 | * @return array |
| 81 | */ |
| 82 | protected function get_all_modules( $module_type = false ) { |
| 83 | $option_manager = $this->get_option_manager(); |
| 84 | |
| 85 | if ( false !== $module_type ) { |
| 86 | return fs_get_entities( $option_manager->get_option( $module_type . 's', array() ), FS_Plugin::get_class_name() ); |
| 87 | } |
| 88 | |
| 89 | return array( |
| 90 | self::OPTION_NAME_PLUGINS => fs_get_entities( $option_manager->get_option( self::OPTION_NAME_PLUGINS, array() ), FS_Plugin::get_class_name() ), |
| 91 | self::OPTION_NAME_THEMES => fs_get_entities( $option_manager->get_option( self::OPTION_NAME_THEMES, array() ), FS_Plugin::get_class_name() ), |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Load plugin data from local DB. |
| 97 | * |
| 98 | * @author Vova Feldman (@svovaf) |
| 99 | * @since 1.0.6 |
| 100 | */ |
| 101 | function load() { |
| 102 | $all_modules = $this->get_all_modules(); |
| 103 | |
| 104 | if ( ! is_numeric( $this->_module_id ) ) { |
| 105 | unset( $all_modules[ self::OPTION_NAME_THEMES ] ); |
| 106 | } |
| 107 | |
| 108 | foreach ( $all_modules as $modules ) { |
| 109 | /** |
| 110 | * @since 1.2.2 |
| 111 | * |
| 112 | * @var $modules FS_Plugin[] |
| 113 | */ |
| 114 | foreach ( $modules as $module ) { |
| 115 | $found_module = false; |
| 116 | |
| 117 | /** |
| 118 | * If module ID is not numeric, it must be a plugin's slug. |
| 119 | * |
| 120 | * @author Leo Fajardo (@leorw) |
| 121 | * @since 1.2.2 |
| 122 | */ |
| 123 | if ( ! is_numeric( $this->_module_id ) ) { |
| 124 | if ( $this->_module_id === $module->slug ) { |
| 125 | $this->_module_id = $module->id; |
| 126 | $found_module = true; |
| 127 | } |
| 128 | } else if ( $this->_module_id == $module->id ) { |
| 129 | $found_module = true; |
| 130 | } |
| 131 | |
| 132 | if ( $found_module ) { |
| 133 | $this->_module = $module; |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Store plugin on local DB. |
| 142 | * |
| 143 | * @author Vova Feldman (@svovaf) |
| 144 | * @since 1.0.6 |
| 145 | * |
| 146 | * @param bool|FS_Plugin $module |
| 147 | * @param bool $flush |
| 148 | * |
| 149 | * @return bool|\FS_Plugin |
| 150 | */ |
| 151 | function store( $module = false, $flush = true ) { |
| 152 | if ( false !== $module ) { |
| 153 | $this->_module = $module; |
| 154 | } |
| 155 | |
| 156 | $all_modules = $this->get_all_modules( $this->_module->type ); |
| 157 | $all_modules[ $this->_module->slug ] = $this->_module; |
| 158 | |
| 159 | $options_manager = $this->get_option_manager(); |
| 160 | $options_manager->set_option( $this->_module->type . 's', $all_modules, $flush ); |
| 161 | |
| 162 | return $this->_module; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Update local plugin data if different. |
| 167 | * |
| 168 | * @author Vova Feldman (@svovaf) |
| 169 | * @since 1.0.6 |
| 170 | * |
| 171 | * @param \FS_Plugin $plugin |
| 172 | * @param bool $store |
| 173 | * |
| 174 | * @return bool True if plugin was updated. |
| 175 | */ |
| 176 | function update( FS_Plugin $plugin, $store = true ) { |
| 177 | if ( ! ($this->_module instanceof FS_Plugin ) || |
| 178 | $this->_module->slug != $plugin->slug || |
| 179 | $this->_module->public_key != $plugin->public_key || |
| 180 | $this->_module->secret_key != $plugin->secret_key || |
| 181 | $this->_module->parent_plugin_id != $plugin->parent_plugin_id || |
| 182 | $this->_module->title != $plugin->title |
| 183 | ) { |
| 184 | $this->store( $plugin, $store ); |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @author Vova Feldman (@svovaf) |
| 194 | * @since 1.0.6 |
| 195 | * |
| 196 | * @param FS_Plugin $plugin |
| 197 | * @param bool $store |
| 198 | */ |
| 199 | function set( FS_Plugin $plugin, $store = false ) { |
| 200 | $this->_module = $plugin; |
| 201 | |
| 202 | if ( $store ) { |
| 203 | $this->store(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @author Vova Feldman (@svovaf) |
| 209 | * @since 1.0.6 |
| 210 | * |
| 211 | * @return bool|\FS_Plugin |
| 212 | */ |
| 213 | function get() { |
| 214 | if ( isset( $this->_module ) ) { |
| 215 | return $this->_module; |
| 216 | } |
| 217 | |
| 218 | if ( empty( $this->_module_id ) ) { |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Return an FS_Plugin entity that has its `id` and `is_live` properties set (`is_live` is initialized in the FS_Plugin constructor) to avoid triggering an error that is relevant to these properties when the FS_Plugin entity is used before the `parse_settings()` method is called. This can happen when creating a regular WordPress site by cloning a subsite of a multisite network and the data that is stored in the network-level storage is not cloned. |
| 224 | * |
| 225 | * @author Leo Fajardo (@leorw) |
| 226 | * @since 2.5.0 |
| 227 | */ |
| 228 | $plugin = new FS_Plugin(); |
| 229 | $plugin->id = $this->_module_id; |
| 230 | |
| 231 | return $plugin; |
| 232 | } |
| 233 | } |