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