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 ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @author Leo Fajardo (@leorw) |
76
|
|
|
* @since 1.2.2 |
77
|
|
|
* |
78
|
|
|
* @param string|false $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 $option_manager->get_option( $module_type . 's', array() ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return array( |
90
|
|
|
self::OPTION_NAME_PLUGINS => $option_manager->get_option( self::OPTION_NAME_PLUGINS, array() ), |
91
|
|
|
self::OPTION_NAME_THEMES => $option_manager->get_option( self::OPTION_NAME_THEMES, array() ), |
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
|
|
|
return isset( $this->_module ) ? |
215
|
|
|
$this->_module : |
216
|
|
|
false; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.