PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
← All changes | class/class-mainwp-api-manager-key.php +248 -259 5.0.3 → 6.2.2 View file →
@@ -10,9 +10,9 @@
10 10 namespace MainWP\Dashboard;
11 11
12 12 // Exit if accessed directly.
13 13 if ( ! defined( 'ABSPATH' ) ) {
14 - exit;
14 + exit;
15 15 }
16 16
17 17 /**
18 18 * Class MainWP_Api_Manager_Key
@@ -25,312 +25,301 @@
25 25 * @author Todd Lahman LLC
26 26 * @copyright Copyright (c) Todd Lahman LLC
27 27 * @since 1.3.0
28 28 */
29 -class MainWP_Api_Manager_Key {
29 +class MainWP_Api_Manager_Key { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
30 30
31 - /**
32 - * Set initial $instance value.
33 - *
34 - * @var null
35 - */
36 - protected static $instance = null;
31 + /**
32 + * Set initial $instance value.
33 + *
34 + * @var null
35 + */
36 + protected static $instance = null;
37 37
38 - /**
39 - * Set initial $apisslverify value.
40 - *
41 - * @var int
42 - */
43 - protected static $apisslverify = 1;
38 + /**
39 + * Set initial $apisslverify value.
40 + *
41 + * @var int
42 + */
43 + protected static $apisslverify = 1;
44 44
45 - /**
46 - * Create a new Self Instance.
47 - *
48 - * @return mixed self::$instance
49 - */
50 - public static function instance() {
45 + /**
46 + * Create a new Self Instance.
47 + *
48 + * @return mixed static::$instance
49 + */
50 + public static function instance() {
51 51
52 - if ( is_null( self::$instance ) ) {
53 - self::$instance = new self();
54 - }
52 + if ( is_null( static::$instance ) ) {
53 + static::$instance = new self();
54 + }
55 55
56 - return self::$instance;
57 - }
56 + return static::$instance;
57 + }
58 58
59 - /**
60 - * MainWP_Api_Manager_Key constructor.
61 - *
62 - * Run any time class is called.
63 - * Validate SSL certificate.
64 - */
65 - public function __construct() {
66 - self::$apisslverify = ( ( get_option( 'mainwp_api_sslVerifyCertificate' ) === false ) || ( 1 === (int) get_option( 'mainwp_api_sslVerifyCertificate' ) ) ) ? 1 : 0;
67 - }
59 + /**
60 + * MainWP_Api_Manager_Key constructor.
61 + *
62 + * Run any time class is called.
63 + * Validate SSL certificate.
64 + */
65 + public function __construct() {
66 + static::$apisslverify = ( ( get_option( 'mainwp_api_sslVerifyCertificate' ) === false ) || ( 1 === (int) get_option( 'mainwp_api_sslVerifyCertificate' ) ) ) ? 1 : 0;
67 + }
68 68
69 - /**
70 - * Activate extension.
71 - *
72 - * This function checks the users login information & grabs the update URL for the specific extension & activates it.
73 - *
74 - * @param array $args Extension arguments.
75 - *
76 - * @return array Request response.
77 - *
78 - * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
79 - */
80 - public function activate( $args ) {
69 + /**
70 + * Activate extension.
71 + *
72 + * This function checks the users login information & grabs the update URL for the specific extension & activates it.
73 + *
74 + * @param array $args Extension arguments.
75 + *
76 + * @return array Request response.
77 + *
78 + * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
79 + */
80 + public function activate( $args ) {
81 81
82 - $defaults = array(
83 - 'request' => 'softwareactivation',
84 - 'dashboard_version' => MainWP_System::instance()->get_dashboard_version(),
85 - );
82 + $defaults = array(
83 + 'request' => 'softwareactivation',
84 + 'dashboard_version' => MainWP_System::instance()->get_dashboard_version(),
85 + );
86 86
87 - $args = wp_parse_args( $defaults, $args );
87 + $args = wp_parse_args( $defaults, $args );
88 88
89 - $request = wp_remote_post(
90 - MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
91 - array(
92 - 'body' => $args,
93 - 'timeout' => 50,
94 - 'sslverify' => self::$apisslverify,
95 - )
96 - );
89 + $request = wp_remote_post(
90 + MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
91 + array(
92 + 'body' => $args,
93 + 'timeout' => 50,
94 + 'sslverify' => static::$apisslverify,
95 + )
96 + );
97 97
98 - if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
98 + if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
99 99
100 - return false;
101 - }
100 + return false;
101 + }
102 102
103 - $response = wp_remote_retrieve_body( $request );
103 + return wp_remote_retrieve_body( $request );
104 + }
104 105
105 - return $response;
106 - }
106 + /**
107 + * Deactivate extension .
108 + *
109 + * This function checks the users login information & grabs the update URL for the specific extension & deactivates it.
110 + *
111 + * @param array $args Extension arguments.
112 + *
113 + * @return array Request response.
114 + *
115 + * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
116 + */
117 + public function deactivate( $args ) {
107 118
108 - /**
109 - * Deactivate extension .
110 - *
111 - * This function checks the users login information & grabs the update URL for the specific extension & deactivates it.
112 - *
113 - * @param array $args Extension arguments.
114 - *
115 - * @return array Request response.
116 - *
117 - * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
118 - */
119 - public function deactivate( $args ) {
119 + $defaults = array(
120 + 'request' => 'deactivate', // old: wc-api/deactivation.
121 + 'dashboard_version' => MainWP_System::instance()->get_dashboard_version(),
122 + );
120 123
121 - $defaults = array(
122 - 'request' => 'deactivate', // old: wc-api/deactivation.
123 - 'dashboard_version' => MainWP_System::instance()->get_dashboard_version(),
124 - );
124 + $args = wp_parse_args( $defaults, $args );
125 125
126 - $args = wp_parse_args( $defaults, $args );
126 + $request = wp_remote_post(
127 + MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api', // old: wc-api/deactivate.
128 + array(
129 + 'body' => $args,
130 + 'timeout' => 50,
131 + 'sslverify' => static::$apisslverify,
132 + )
133 + );
127 134
128 - $request = wp_remote_post(
129 - MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api', // old: wc-api/deactivate.
130 - array(
131 - 'body' => $args,
132 - 'timeout' => 50,
133 - 'sslverify' => self::$apisslverify,
134 - )
135 - );
135 + if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
136 + // Request failed.
137 + return false;
138 + }
136 139
137 - if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
138 - // Request failed.
139 - return false;
140 - }
140 + return wp_remote_retrieve_body( $request );
141 + }
141 142
142 - $response = wp_remote_retrieve_body( $request );
143 + /**
144 + * Grab extension API Key.
145 + *
146 + * This function checks the users login information & grabs the update URL for the specific extension & returns the API Key.
147 + *
148 + * @param array $args Extension arguments.
149 + *
150 + * @return array Request response.
151 + *
152 + * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
153 + */
154 + public function grab_api_key( $args ) {
143 155
144 - return $response;
145 - }
156 + $defaults = array(
157 + 'request' => 'grabapikey',
158 + 'dashboard_version' => MainWP_System::instance()->get_dashboard_version(),
159 + );
146 160
147 - /**
148 - * Grab extension API Key.
149 - *
150 - * This function checks the users login information & grabs the update URL for the specific extension & returns the API Key.
151 - *
152 - * @param array $args Extension arguments.
153 - *
154 - * @return array Request response.
155 - *
156 - * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
157 - */
158 - public function grab_api_key( $args ) {
161 + $args = wp_parse_args( $defaults, $args );
159 162
160 - $defaults = array(
161 - 'request' => 'grabapikey',
162 - 'dashboard_version' => MainWP_System::instance()->get_dashboard_version(),
163 - );
163 + $request = wp_remote_post(
164 + MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
165 + array(
166 + 'body' => $args,
167 + 'timeout' => 50,
168 + 'sslverify' => static::$apisslverify,
169 + )
170 + );
164 171
165 - $args = wp_parse_args( $defaults, $args );
172 + if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
173 + // Request failed.
174 + return false;
175 + }
166 176
167 - $request = wp_remote_post(
168 - MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
169 - array(
170 - 'body' => $args,
171 - 'timeout' => 50,
172 - 'sslverify' => self::$apisslverify,
173 - )
174 - );
177 + return wp_remote_retrieve_body( $request );
178 + }
175 179
176 - if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
177 - // Request failed.
178 - return false;
179 - }
180 + /**
181 + * Test login API.
182 + *
183 + * This function checks the users login information & Tests it against the MainWP.com Login Credentials stored on the license server.
184 + *
185 + * @param aray $args Login arguments.
186 + *
187 + * @return array Request response.
188 + *
189 + * @throws \MainWP_Exception Request error codes.
190 + *
191 + * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
192 + * @uses \MainWP\Dashboard\MainWP_Logger::debug()
193 + * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
194 + * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
195 + */
196 + public function verify_api_key( $args ) {
180 197
181 - $response = wp_remote_retrieve_body( $request );
198 + $defaults = array(
199 + 'request' => 'testloginapi',
200 + );
182 201
183 - return $response;
184 - }
202 + $args = wp_parse_args( $defaults, $args );
185 203
186 - /**
187 - * Test login API.
188 - *
189 - * This function checks the users login information & Tests it against the MainWP.com Login Credentials stored on the license server.
190 - *
191 - * @param aray $args Login arguments.
192 - *
193 - * @return array Request response.
194 - *
195 - * @throws \Exception Request error codes.
196 - *
197 - * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
198 - * @uses \MainWP\Dashboard\MainWP_Logger::debug()
199 - * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
200 - * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
201 - */
202 - public function verify_api_key( $args ) {
204 + $request = wp_remote_post(
205 + MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
206 + array(
207 + 'body' => $args,
208 + 'timeout' => 50,
209 + 'sslverify' => static::$apisslverify,
210 + )
211 + );
203 212
204 - $defaults = array(
205 - 'request' => 'testloginapi',
206 - );
213 + if ( is_wp_error( $request ) ) {
214 + if ( 1 === (int) static::$apisslverify ) {
215 + MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', 0 );
207 216
208 - $args = wp_parse_args( $defaults, $args );
217 + return array( 'retry_action' => 1 );
218 + }
209 219
210 - $request = wp_remote_post(
211 - MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
212 - array(
213 - 'body' => $args,
214 - 'timeout' => 50,
215 - 'sslverify' => self::$apisslverify,
216 - )
217 - );
220 + throw new MainWP_Exception( $request->get_error_message() ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
221 + }
218 222
219 - if ( is_wp_error( $request ) ) {
220 - if ( 1 === (int) self::$apisslverify ) {
221 - MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', 0 );
223 + $code = wp_remote_retrieve_response_code( $request );
224 + if ( 200 !== $code ) {
225 + /* translators: 1: Opening anchor tag, 2: Closing anchor tag */
226 + $error = sprintf( esc_html__( 'Login verification could not be completed. Please contact %1$sMainWP Support%2$s so we can assist.', 'mainwp' ), '<a href="https://community.mainwp.com/" target="_blank">', '</a>' );
227 + throw new MainWP_Exception( $error ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Escaped.
228 + }
222 229
223 - return array( 'retry_action' => 1 );
224 - }
230 + return wp_remote_retrieve_body( $request );
231 + }
225 232
226 - throw new \Exception( $request->get_error_message() ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
227 - }
233 + /**
234 + * Get purchased software.
235 + *
236 + * This function grabs a list of purchased MainWP Extensions that are available for download.
237 + *
238 + * @param array $args Software Arguments.
239 + *
240 + * @return array Request response.
241 + *
242 + * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
243 + */
244 + public function get_purchased_software( $args ) {
228 245
229 - $code = wp_remote_retrieve_response_code( $request );
230 - if ( 200 !== $code ) {
231 - $error = sprintf( esc_html__( 'Login verification could not be completed. Please contact %1$sMainWP Support%2$s so we can assist.', 'mainwp' ), '<a href="https://managers.mainwp.com/" target="_blank">', '</a>' );
232 - throw new \Exception( $error ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Escaped.
233 - }
246 + $defaults = array(
247 + 'request' => 'getpurchasedsoftware',
248 + );
234 249
235 - $response = wp_remote_retrieve_body( $request );
250 + $args = wp_parse_args( $defaults, $args );
236 251
237 - return $response;
238 - }
252 + $request = wp_remote_post(
253 + MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
254 + array(
255 + 'body' => $args,
256 + 'timeout' => 200,
257 + 'sslverify' => static::$apisslverify,
258 + )
259 + );
239 260
240 - /**
241 - * Get purchased software.
242 - *
243 - * This function grabs a list of purchased MainWP Extensions that are available for download.
244 - *
245 - * @param array $args Software Arguments.
246 - *
247 - * @return array Request response.
248 - *
249 - * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
250 - */
251 - public function get_purchased_software( $args ) {
261 + MainWP_Logger::instance()->debug( 'Get purchased softwares: ' . MainWP_Utility::value_to_string( $request ) );
252 262
253 - $defaults = array(
254 - 'request' => 'getpurchasedsoftware',
255 - );
263 + if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
264 + // Request failed.
265 + return false;
266 + }
256 267
257 - $args = wp_parse_args( $defaults, $args );
268 + return wp_remote_retrieve_body( $request );
269 + }
258 270
259 - $request = wp_remote_post(
260 - MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
261 - array(
262 - 'body' => $args,
263 - 'timeout' => 200,
264 - 'sslverify' => self::$apisslverify,
265 - )
266 - );
271 + /**
272 + * Purchase software.
273 + *
274 + * @param array $args Software arguments.
275 + *
276 + * @return array Request response.
277 + *
278 + * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
279 + */
280 + public function purchase_software( $args ) {
281 + $defaults = array(
282 + 'request' => 'purchasesoftware',
283 + );
284 + $args = wp_parse_args( $defaults, $args );
267 285
268 - MainWP_Logger::instance()->debug( 'Get purchased softwares: ' . MainWP_Utility::value_to_string( $request ) );
286 + $request = wp_remote_post(
287 + MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
288 + array(
289 + 'body' => $args,
290 + 'timeout' => 50,
291 + 'sslverify' => static::$apisslverify,
292 + )
293 + );
269 294
270 - if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
271 - // Request failed.
272 - return false;
273 - }
295 + if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
296 + // Request failed.
297 + return false;
298 + }
274 299
275 - $response = wp_remote_retrieve_body( $request );
300 + return wp_remote_retrieve_body( $request );
301 + }
276 302
277 - return $response;
278 - }
303 + /**
304 + * Get decrypt master api key.
305 + *
306 + * @return array Request response.
307 + */
308 + public function get_decrypt_master_api_key() {
309 + $decryp_api_key = '';
279 310
280 - /**
281 - * Purchase software.
282 - *
283 - * @param array $args Software arguments.
284 - *
285 - * @return array Request response.
286 - *
287 - * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
288 - */
289 - public function purchase_software( $args ) {
290 - $defaults = array(
291 - 'request' => 'purchasesoftware',
292 - );
293 - $args = wp_parse_args( $defaults, $args );
294 -
295 - $request = wp_remote_post(
296 - MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
297 - array(
298 - 'body' => $args,
299 - 'timeout' => 50,
300 - 'sslverify' => self::$apisslverify,
301 - )
302 - );
303 -
304 - if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
305 - // Request failed.
306 - return false;
307 - }
308 -
309 - $response = wp_remote_retrieve_body( $request );
310 - return $response;
311 - }
312 -
313 - /**
314 - * Get decrypt master api key.
315 - *
316 - * @return array Request response.
317 - */
318 - public function get_decrypt_master_api_key() {
319 - $decryp_api_key = '';
320 -
321 - // to check old value.
322 - $old_enscrypt = get_option( 'mainwp_extensions_master_api_key', false );
323 - if ( false !== $old_enscrypt && is_string( $old_enscrypt ) && ! empty( $old_enscrypt ) ) { // to compatible.
324 - // old encrypt.
325 - $decryp_api_key = ! empty( $old_enscrypt ) ? MainWP_Api_Manager_Password_Management::decrypt_string( $old_enscrypt ) : '';
326 - // convert to new encrypt: to compatible.
327 - if ( ! empty( $decryp_api_key ) && is_string( $decryp_api_key ) ) {
328 - MainWP_Keys_Manager::instance()->update_key_value( 'mainwp_extensions_master_api_key', $decryp_api_key );
329 - }
330 - }
331 - $decryp_api_key = MainWP_Keys_Manager::instance()->get_keys_value( 'mainwp_extensions_master_api_key' );
332 - return $decryp_api_key;
333 - }
311 + // to check old value.
312 + $old_enscrypt = get_option( 'mainwp_extensions_master_api_key', false );
313 + if ( false !== $old_enscrypt && is_string( $old_enscrypt ) && ! empty( $old_enscrypt ) ) { // to compatible.
314 + // old encrypt.
315 + $decryp_api_key = ! empty( $old_enscrypt ) ? MainWP_Api_Manager_Password_Management::decrypt_string( $old_enscrypt ) : '';
316 + // convert to new encrypt: to compatible.
317 + if ( ! empty( $decryp_api_key ) && is_string( $decryp_api_key ) ) {
318 + MainWP_Keys_Manager::instance()->update_key_value( 'mainwp_extensions_master_api_key', $decryp_api_key );
319 + }
320 + }
321 + return MainWP_Keys_Manager::instance()->get_keys_value( 'mainwp_extensions_master_api_key' );
322 + }
334 323 }
335 324
336 325 // Class is instantiated as an object by other classes on-demand.