PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.1
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 | pages/page-mainwp-extensions-handler.php +962 -1061 4.6.0.1 → 6.2.1 View file →
@@ -6,1212 +6,1113 @@
6 6 */
7 7
8 8 namespace MainWP\Dashboard;
9 9
10 +// Exit if accessed directly.
11 +if ( ! defined( 'ABSPATH' ) ) {
12 + exit;
13 +}
14 +
10 15 /**
11 16 * Class MainWP_Extensions_Handler
12 17 */
13 -class MainWP_Extensions_Handler {
18 +class MainWP_Extensions_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
14 19
15 20 // phpcs:disable Generic.Metrics.CyclomaticComplexity -- complexity.
16 21
17 - /**
18 - * Method get_class_name()
19 - *
20 - * Get Class Name.
21 - *
22 - * @return object
23 - */
24 - public static function get_class_name() {
25 - return __CLASS__;
26 - }
22 + /**
23 + * Method get_class_name()
24 + *
25 + * Get Class Name.
26 + *
27 + * @return object
28 + */
29 + public static function get_class_name() {
30 + return __CLASS__;
31 + }
27 32
28 - /**
29 - * All extensions.
30 - *
31 - * @var array $extensions
32 - */
33 - public static $extensions;
33 + /**
34 + * All extensions.
35 + *
36 + * @var array $extensions
37 + */
38 + public static $extensions;
34 39
35 - /**
36 - * All disabled extensions.
37 - *
38 - * @var array $extensions
39 - */
40 - public static $extensions_disabled;
40 + /**
41 + * All disabled extensions.
42 + *
43 + * @var array $extensions
44 + */
45 + public static $extensions_disabled;
41 46
47 + /**
48 + * Get Extension Slug.
49 + *
50 + * @param mixed $slug Extension Slug.
51 + *
52 + * @return string Extensions Slug.
53 + */
54 + public static function get_extension_slug( $slug ) {
55 + $currentExtensions = static::get_extensions();
56 + if ( ! is_array( $currentExtensions ) || empty( $currentExtensions ) ) {
57 + return $slug;
58 + }
42 59
43 - /**
44 - * Possible options.
45 - *
46 - * @var array $possible_options
47 - */
48 - private static $possible_options = array(
49 - 'plugin_upgrades',
50 - 'theme_upgrades',
51 - 'premium_upgrades',
52 - 'plugins',
53 - 'themes',
54 - 'dtsSync',
55 - 'version',
56 - 'sync_errors',
57 - 'ignored_plugins',
58 - 'wp_upgrades',
59 - 'site_info',
60 - 'client',
61 - 'signature_algo',
62 - 'verify_method',
63 - 'pubkey',
64 - );
60 + foreach ( $currentExtensions as $extension ) {
61 + if ( isset( $extension['api'] ) && ( $extension['api'] === $slug ) ) {
62 + return $extension['slug'];
63 + }
64 + }
65 65
66 + return $slug;
67 + }
66 68
67 - /**
68 - * Get Extension Slug.
69 - *
70 - * @param mixed $slug Extension Slug.
71 - *
72 - * @return string Extensions Slug.
73 - */
74 - public static function get_extension_slug( $slug ) {
75 - $currentExtensions = self::get_extensions();
76 - if ( ! is_array( $currentExtensions ) || empty( $currentExtensions ) ) {
77 - return $slug;
78 - }
69 + /**
70 + * Get all extension slugs.
71 + *
72 + * @return array am_slugs|slugs.
73 + */
74 + public static function get_slugs() {
75 + $currentExtensions = static::get_extensions();
79 76
80 - foreach ( $currentExtensions as $extension ) {
81 - if ( isset( $extension['api'] ) && ( $extension['api'] === $slug ) ) {
82 - return $extension['slug'];
83 - }
84 - }
77 + if ( empty( $currentExtensions ) ) {
78 + return array(
79 + 'slugs' => '',
80 + 'am_slugs' => '',
81 + );
82 + }
85 83
86 - return $slug;
87 - }
84 + $out = '';
85 + $am_out = '';
86 + foreach ( $currentExtensions as $extension ) {
87 + if ( ! isset( $extension['api'] ) || '' === $extension['api'] ) {
88 + continue;
89 + }
88 90
89 - /**
90 - * Get all extension slugs.
91 - *
92 - * @return array am_slugs|slugs.
93 - */
94 - public static function get_slugs() {
95 - $currentExtensions = self::get_extensions();
91 + if ( isset( $extension['apiManager'] ) && ! empty( $extension['apiManager'] ) && 'Activated' === $extension['activated_key'] ) {
92 + if ( '' !== $am_out ) {
93 + $am_out .= ',';
94 + }
95 + $am_out .= $extension['api'];
96 + } else {
97 + if ( '' !== $out ) {
98 + $out .= ',';
99 + }
100 + $out .= $extension['api'];
101 + }
102 + }
96 103
97 - if ( empty( $currentExtensions ) ) {
98 - return array(
99 - 'slugs' => '',
100 - 'am_slugs' => '',
101 - );
102 - }
104 + return array(
105 + 'slugs' => $out,
106 + 'am_slugs' => $am_out,
107 + );
108 + }
103 109
104 - $out = '';
105 - $am_out = '';
106 - foreach ( $currentExtensions as $extension ) {
107 - if ( ! isset( $extension['api'] ) || '' === $extension['api'] ) {
108 - continue;
109 - }
110 + /**
111 + * Clean up MainWP Extention names.
112 + *
113 + * @param string $extension Array of MainWP Extentsions.
114 + * @param bool $forced forced polish.
115 + *
116 + * @return string $menu_name Final Menu Name.
117 + */
118 + public static function polish_ext_name( $extension, $forced = false ) {
119 + if ( $forced || ( isset( $extension['mainwp'] ) && $extension['mainwp'] ) ) {
120 + $menu_name = static::polish_string_name( $extension['name'] );
121 + } else {
122 + $menu_name = $extension['name'];
123 + }
124 + return $menu_name;
125 + }
110 126
111 - if ( isset( $extension['apiManager'] ) && ! empty( $extension['apiManager'] ) && 'Activated' === $extension['activated_key'] ) {
112 - if ( '' !== $am_out ) {
113 - $am_out .= ',';
114 - }
115 - $am_out .= $extension['api'];
116 - } else {
117 - if ( '' !== $out ) {
118 - $out .= ',';
119 - }
120 - $out .= $extension['api'];
121 - }
122 - }
127 + /**
128 + * Clean up MainWP Extention names.
129 + *
130 + * @param string $name Extention name string.
131 + *
132 + * @return string $menu_name Final Name.
133 + */
134 + public static function polish_string_name( $name ) {
135 + if ( false !== stripos( $name, 'for Mainwp' ) ) {
136 + return $name; // skip.
137 + }
138 + $new_name = str_replace(
139 + array(
140 + 'Extensions',
141 + 'Mainwp',
142 + 'Extension',
143 + 'MainWP',
144 + 'Add-on',
145 + ),
146 + '',
147 + $name
148 + );
149 + $new_name = trim( $new_name );
150 + return $new_name;
151 + }
123 152
124 - return array(
125 - 'slugs' => $out,
126 - 'am_slugs' => $am_out,
127 - );
128 - }
129 153
130 - /**
131 - * Clean up MainWP Extention names.
132 - *
133 - * @param array $extension Array of MainWP Extentsions.
134 - * @param array $forced forced polish.
135 - *
136 - * @return string $menu_name Final Menu Name.
137 - */
138 - public static function polish_ext_name( $extension, $forced = false ) {
139 - if ( $forced || ( isset( $extension['mainwp'] ) && $extension['mainwp'] ) ) {
140 - $menu_name = self::polish_string_name( $extension['name'] );
141 - } else {
142 - $menu_name = $extension['name'];
143 - }
144 - return $menu_name;
145 - }
154 + /**
155 + * Load MainWP Extensions.
156 + *
157 + * @param bool $forced Forced reload value.
158 + *
159 + * @return array Array of loaded Extensions.
160 + */
161 + public static function get_extensions( $forced = false ) {
162 + if ( ! isset( static::$extensions ) || $forced ) {
163 + static::$extensions = array();
164 + $extensions = get_option( 'mainwp_extensions', array() );
165 + foreach ( $extensions as $extension ) {
166 + $slug = $extension['slug'];
167 + if ( function_exists( '\mainwp_current_user_can' ) ) { // to fix later init, it's ok to check user have right.
168 + if ( \mainwp_current_user_can( 'extension', dirname( $slug ) ) ) {
169 + static::$extensions[] = $extension;
170 + }
171 + } else {
172 + static::$extensions[] = $extension;
173 + }
174 + }
175 + }
176 + return static::$extensions;
177 + }
146 178
147 - /**
148 - * Clean up MainWP Extention names.
149 - *
150 - * @param string $name Extention name string.
151 - *
152 - * @return string $menu_name Final Name.
153 - */
154 - public static function polish_string_name( $name ) {
155 - if ( false !== stripos( $name, 'for Mainwp' ) ) {
156 - return $name; // skip.
157 - }
158 - $new_name = str_replace(
159 - array(
160 - 'Extensions',
161 - 'Mainwp',
162 - 'Extension',
163 - 'MainWP',
164 - ),
165 - '',
166 - $name
167 - );
168 - $new_name = trim( $new_name );
169 - return $new_name;
170 - }
171 179
180 + /**
181 + * Get disabled MainWP Extensions.
182 + *
183 + * @param bool $compatible_api_response To get compatible api response values.
184 + *
185 + * @return array Array of disabled Extensions.
186 + */
187 + public static function get_extensions_disabled( $compatible_api_response = false ) { // phpcs:ignore -- NOSONAR - complex.
188 + if ( ! isset( static::$extensions_disabled ) || $compatible_api_response ) {
189 + static::$extensions_disabled = array();
190 + $all_available_extensions = MainWP_Extensions_View::get_available_extensions( 'all' );
191 + $all_plugins = get_plugins();
172 192
173 - /**
174 - * Load MainWP Extensions.
175 - *
176 - * @param bool $forced Forced reload value.
177 - *
178 - * @return array Array of loaded Extensions.
179 - */
180 - public static function get_extensions( $forced = false ) {
181 - if ( ! isset( self::$extensions ) || $forced ) {
182 - self::$extensions = array();
183 - $extensions = get_option( 'mainwp_extensions', array() );
184 - foreach ( $extensions as $extension ) {
185 - $slug = $extension['slug'];
186 - if ( function_exists( 'mainwp_current_user_have_right' ) ) { // to fix later init, it's ok to check user have right.
187 - if ( mainwp_current_user_have_right( 'extension', dirname( $slug ) ) ) {
188 - self::$extensions[] = $extension;
189 - }
190 - } else {
191 - self::$extensions[] = $extension;
192 - }
193 - }
194 - }
195 - return self::$extensions;
196 - }
193 + $exts_disabled = array();
197 194
195 + if ( $all_plugins ) {
196 + foreach ( $all_plugins as $plugin => $plugin_data ) {
197 + if ( is_plugin_active( $plugin ) ) {
198 + continue;
199 + }
200 + $slug = dirname( $plugin );
201 + if ( isset( $all_available_extensions[ $slug ] ) ) {
198 202
199 - /**
200 - * Get disabled MainWP Extensions.
201 - *
202 - * @param bool $compatible_api_response To get compatible api response values.
203 - *
204 - * @return array Array of disabled Extensions.
205 - */
206 - public static function get_extensions_disabled( $compatible_api_response = false ) {
207 - if ( ! isset( self::$extensions_disabled ) || $compatible_api_response ) {
208 - self::$extensions_disabled = array();
209 - $all_available_extensions = MainWP_Extensions_View::get_available_extensions( 'all' );
210 - $all_plugins = get_plugins();
203 + $ext = $all_available_extensions[ $slug ];
211 204
212 - $exts_disabled = array();
205 + $extension = array();
213 206
214 - if ( $all_plugins ) {
215 - foreach ( $all_plugins as $plugin => $plugin_data ) {
216 - if ( is_plugin_active( $plugin ) ) {
217 - continue;
218 - }
219 - $slug = dirname( $plugin );
220 - if ( isset( $all_available_extensions[ $slug ] ) ) {
207 + $extension['name'] = $plugin_data['Name'];
208 + $extension['slug'] = $plugin;
209 + $extension['version'] = $plugin_data['Version'];
210 + $extension['description'] = $plugin_data['Description'];
211 + $extension['author'] = $plugin_data['Author'];
212 + $extension['img'] = isset( $ext['img'] ) ? $ext['img'] : '';
213 + $extension['iconURI'] = isset( $plugin_data['IconURI'] ) ? $plugin_data['IconURI'] : '';
214 + $extension['SupportForumURI'] = '';
215 + $extension['DocumentationURI'] = '';
216 + $extension['page'] = 'Extensions-' . str_replace( ' ', '-', ucwords( str_replace( '-', ' ', dirname( $slug ) ) ) );
221 217
222 - $ext = $all_available_extensions[ $slug ];
218 + $extension['api_key'] = '';
219 + $extension['activated_key'] = 'Deactivated';
220 + $extension['deactivate_checkbox'] = 'off';
221 + $extension['product_id'] = $ext['product_id'];
222 + $extension['instance_id'] = '';
223 + $extension['software_version'] = '';
224 + $extension['product_item_id'] = '';
225 + $extension['type'] = $ext['type'];
223 226
224 - $extension = array();
227 + if ( $compatible_api_response ) {
228 + $exts_disabled[ $ext['product_id'] ] = $extension;
229 + } else {
230 + $exts_disabled[] = $extension;
231 + }
232 + }
233 + }
234 + }
225 235
226 - $extension['name'] = $plugin_data['Name'];
227 - $extension['slug'] = $plugin;
228 - $extension['version'] = $plugin_data['Version'];
229 - $extension['description'] = $plugin_data['Description'];
230 - $extension['author'] = $plugin_data['Author'];
231 - $extension['img'] = isset( $ext['img'] ) ? $ext['img'] : '';
232 - $extension['iconURI'] = isset( $plugin_data['IconURI'] ) ? $plugin_data['IconURI'] : '';
233 - $extension['SupportForumURI'] = '';
234 - $extension['DocumentationURI'] = '';
235 - $extension['page'] = 'Extensions-' . str_replace( ' ', '-', ucwords( str_replace( '-', ' ', dirname( $slug ) ) ) );
236 + if ( $compatible_api_response ) {
237 + return $exts_disabled;
238 + } else {
239 + static::$extensions_disabled = $exts_disabled;
240 + }
241 + }
242 + return static::$extensions_disabled;
243 + }
236 244
237 - $extension['api_key'] = '';
238 - $extension['activated_key'] = 'Deactivated';
239 - $extension['deactivate_checkbox'] = 'off';
240 - $extension['product_id'] = $ext['product_id'];
241 - $extension['instance_id'] = '';
242 - $extension['software_version'] = '';
243 - $extension['product_item_id'] = '';
244 - $extension['type'] = $ext['type'];
245 + /**
246 + * Get not installed MainWP Extensions.
247 + *
248 + * @return array Array of not installed Extensions.
249 + */
250 + public static function get_extensions_not_installed() {
251 + $all_available_extensions = MainWP_Extensions_View::get_available_extensions( 'all' );
245 252
246 - if ( $compatible_api_response ) {
247 - $exts_disabled[ $ext['product_id'] ] = $extension;
248 - } else {
249 - $exts_disabled[] = $extension;
250 - }
251 - }
252 - }
253 - }
253 + $all_plugins = get_plugins();
254 254
255 - if ( $compatible_api_response ) {
256 - return $exts_disabled;
257 - } else {
258 - self::$extensions_disabled = $exts_disabled;
259 - }
260 - }
261 - return self::$extensions_disabled;
262 - }
255 + $installed_slugs = array();
256 + foreach ( $all_plugins as $plugin => $plugin_data ) {
257 + $slug = dirname( $plugin );
258 + if ( ! isset( $installed_slugs[ $slug ] ) ) {
259 + $installed_slugs[] = $slug;
260 + }
261 + }
263 262
264 - /**
265 - * Get MainWP Extensions infor array.
266 - *
267 - * @param array $args Empty Array.
268 - *
269 - * @return array Array of Extensions.
270 - */
271 - public static function get_indexed_extensions_infor( $args = array() ) {
272 - if ( ! is_array( $args ) ) {
273 - $args = array();
274 - }
275 - $extensions = self::get_extensions();
276 - $return = array();
277 - foreach ( $extensions as $extension ) {
278 - if ( isset( $args['activated'] ) && ! empty( $args['activated'] ) ) {
279 - if ( isset( $extension['apiManager'] ) && $extension['apiManager'] ) {
280 - if ( ! isset( $extension['activated_key'] ) || 'Activated' !== $extension['activated_key'] ) {
281 - continue;
282 - }
283 - }
284 - }
285 - $ext = array();
286 - $ext['version'] = $extension['version'];
287 - $ext['name'] = $extension['name'];
288 - $ext['page'] = $extension['page'];
289 - if ( isset( $extension['activated_key'] ) && 'Activated' === $extension['activated_key'] ) {
290 - $ext['activated_key'] = 'Activated';
291 - }
292 - $return[ $extension['slug'] ] = $ext;
293 - }
294 - return $return;
295 - }
263 + $exts_not_installed = array();
264 + foreach ( $all_available_extensions as $slug => $info ) {
265 + if ( ! in_array( $slug, $installed_slugs ) ) {
266 + $exts_not_installed[ $slug ] = array(
267 + 'name' => $info['title'],
268 + 'slug' => $info['slug'],
269 + 'link' => $info['link'],
270 + 'img' => isset( $info['img'] ) ? $info['img'] : '',
271 + 'description' => isset( $info['desc'] ) ? $info['desc'] : '',
272 + );
273 + }
274 + }
275 + return $exts_not_installed;
276 + }
296 277
297 - /**
298 - * Generate API Password.
299 - *
300 - * @param integer $length Lenght of password.
301 - * @param bool $special_chars true|false, allow special characters.
302 - * @param bool $extra_special_chars true|false, allow extra special characters.
303 - *
304 - * @return MainWP_Api_Manager_Password_Management::generate_password()
305 - */
306 - public static function gen_api_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
307 - MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
308 - return MainWP_Api_Manager_Password_Management::generate_password( $length, $special_chars, $extra_special_chars );
309 - }
278 + /**
279 + * Get MainWP Extensions infor array.
280 + *
281 + * @param array $args Empty Array.
282 + * @param bool $deactivated_license Get extensions that deactivated license or not.
283 + *
284 + * @return array Array of Extensions.
285 + */
286 + public static function get_indexed_extensions_infor( $args = array(), $deactivated_license = null ) { // phpcs:ignore -- NOSONAR - complex.
287 + if ( ! is_array( $args ) ) {
288 + $args = array();
289 + }
290 + $extensions = static::get_extensions();
291 + $return = array();
292 + foreach ( $extensions as $extension ) {
293 + if ( ( isset( $args['activated'] ) && ! empty( $args['activated'] ) && isset( $extension['apiManager'] ) && $extension['apiManager'] ) && ( ! isset( $extension['activated_key'] ) || 'Activated' !== $extension['activated_key'] ) ) {
294 + continue;
295 + }
296 + $apiManager = isset( $extension['apiManager'] ) && $extension['apiManager'] ? true : false;
297 + $ext = array();
298 + $ext['version'] = $extension['version'];
299 + $ext['apiManager'] = $apiManager;
300 + $ext['name'] = $extension['name'];
301 + $ext['page'] = $extension['page'];
302 + $ext['mainwp_version'] = isset( $extension['mainwp_version'] ) ? $extension['mainwp_version'] : '';
310 303
304 + if ( isset( $extension['activated_key'] ) && 'Activated' === $extension['activated_key'] ) {
305 + $ext['activated_key'] = 'Activated';
306 + }
311 307
312 - /**
313 - * Add Extension Menu.
314 - *
315 - * @param mixed $slug Extension slug.
316 - *
317 - * @return boolean true|false.
318 - *
319 - * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
320 - */
321 - public static function add_extension_menu( $slug ) {
322 - $snMenuExtensions = get_option( 'mainwp_extmenu' );
323 - if ( ! is_array( $snMenuExtensions ) ) {
324 - $snMenuExtensions = array();
325 - }
308 + if ( null !== $deactivated_license && ( ( $deactivated_license && $apiManager && isset( $ext['activated_key'] ) && 'Activated' === $ext['activated_key'] ) || ( ! $deactivated_license && ( ! isset( $ext['activated_key'] ) || 'Activated' !== $ext['activated_key'] ) ) ) ) {
309 + continue; // get deactivated license, skip activated license.
310 + }
326 311
327 - $snMenuExtensions[] = $slug;
312 + $return[ $extension['slug'] ] = $ext;
313 + }
314 + return $return;
315 + }
328 316
329 - MainWP_Utility::update_option( 'mainwp_extmenu', $snMenuExtensions );
317 + /**
318 + * Generate API Password.
319 + *
320 + * @param integer $length Lenght of password.
321 + * @param bool $special_chars true|false, allow special characters.
322 + * @param bool $extra_special_chars true|false, allow extra special characters.
323 + *
324 + * @return MainWP_Api_Manager_Password_Management::generate_password()
325 + */
326 + public static function gen_api_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
327 + MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
328 + return MainWP_Api_Manager_Password_Management::generate_password( $length, $special_chars, $extra_special_chars );
329 + }
330 330
331 - /**
332 - * Adds Extension to the navigation menu
333 - *
334 - * Adds Extension instance to the Extensions located in the main MainWP navigation menu.
335 - *
336 - * @param string $slug Extension slug.
337 - *
338 - * @since 4.0
339 - */
340 - do_action( 'mainwp_added_extension_menu', $slug );
341 331
342 - return true;
343 - }
332 + /**
333 + * Add Extension Menu.
334 + *
335 + * @param mixed $slug Extension slug.
336 + *
337 + * @return boolean true|false.
338 + *
339 + * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
340 + */
341 + public static function add_extension_menu( $slug ) {
342 + $snMenuExtensions = get_option( 'mainwp_extmenu' );
343 + if ( ! is_array( $snMenuExtensions ) ) {
344 + $snMenuExtensions = array();
345 + }
344 346
345 - /**
346 - * HTTP Request Reject Unsafe Urls.
347 - *
348 - * @param bool $args args.
349 - *
350 - * @return mixed args.
351 - */
352 - public static function http_request_reject_unsafe_urls( $args ) {
353 - $args['reject_unsafe_urls'] = false;
347 + $snMenuExtensions[] = $slug;
354 348
355 - return $args;
356 - }
349 + MainWP_Utility::update_option( 'mainwp_extmenu', $snMenuExtensions );
357 350
358 - /**
359 - * No SSL Filter Function.
360 - *
361 - * @param bool $args args.
362 - *
363 - * @return mixed args.
364 - */
365 - public static function no_ssl_filter_function( $args ) {
366 - $args['sslverify'] = false;
367 - return $args;
368 - }
351 + /**
352 + * Adds Extension to the navigation menu
353 + *
354 + * Adds Extension instance to the Extensions located in the main MainWP navigation menu.
355 + *
356 + * @param string $slug Extension slug.
357 + *
358 + * @since 4.0
359 + */
360 + do_action( 'mainwp_added_extension_menu', $slug );
369 361
370 - /**
371 - * No SSL Filter Extention Upgrade.
372 - *
373 - * @param bool $r Results.
374 - * @param mixed $url Upgrade Extension URL.
375 - *
376 - * @return mixed false|$r.
377 - */
378 - public static function no_ssl_filter_extension_upgrade( $r, $url ) {
379 - if ( ( false !== strpos( $url, 'am_download_file=' ) ) && ( false !== strpos( $url, 'am_email=' ) ) ) {
380 - $r['sslverify'] = false;
381 - }
362 + return true;
363 + }
382 364
383 - return $r;
384 - }
365 + /**
366 + * HTTP Request Reject Unsafe Urls.
367 + *
368 + * @param bool $args args.
369 + *
370 + * @return mixed args.
371 + */
372 + public static function http_request_reject_unsafe_urls( $args ) {
373 + $args['reject_unsafe_urls'] = false;
385 374
386 - /**
387 - * Install MainWP Extension.
388 - *
389 - * @param mixed $url MainWP Extension update URL.
390 - * @param bool $activatePlugin true|false Whether or not to activate extension.
391 - *
392 - * @return mixed $return
393 - *
394 - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
395 - */
396 - public static function install_plugin( $url, $activatePlugin = false ) {
375 + return $args;
376 + }
397 377
398 - $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
378 + /**
379 + * No SSL Filter Function.
380 + *
381 + * @param bool $args args.
382 + *
383 + * @return mixed args.
384 + */
385 + public static function no_ssl_filter_function( $args ) {
386 + $args['sslverify'] = false;
387 + return $args;
388 + }
399 389
400 - /**
401 - * WordPress files system object.
402 - *
403 - * @global object
404 - */
405 - global $wp_filesystem;
390 + /**
391 + * No SSL Filter Extention Upgrade.
392 + *
393 + * @param bool $r Results.
394 + * @param mixed $url Upgrade Extension URL.
395 + *
396 + * @return mixed false|$r.
397 + */
398 + public static function no_ssl_filter_extension_upgrade( $r, $url ) {
399 + if ( ( false !== strpos( $url, 'am_download_file=' ) ) && ( false !== strpos( $url, 'am_email=' ) ) ) {
400 + $r['sslverify'] = false;
401 + }
406 402
407 - if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) {
408 - include_once ABSPATH . '/wp-admin/includes/screen.php';
409 - }
403 + return $r;
404 + }
410 405
411 - include_once ABSPATH . '/wp-admin/includes/template.php';
412 - include_once ABSPATH . '/wp-admin/includes/misc.php';
413 - include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
414 - include_once ABSPATH . '/wp-admin/includes/plugin.php';
406 + /**
407 + * Install MainWP Extension.
408 + *
409 + * @param mixed $url MainWP Extension update URL.
410 + * @param bool $activatePlugin true|false Whether or not to activate extension.
411 + *
412 + * @return mixed $return
413 + *
414 + * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
415 + */
416 + public static function install_plugin( $url, $activatePlugin = false ) { //phpcs:ignore -- NOSONAR - complex.
415 417
416 - $installer = new \WP_Upgrader();
417 - $ssl_verifyhost = get_option( 'mainwp_sslVerifyCertificate' );
418 - $ssl_api_verifyhost = ( ( false === get_option( 'mainwp_api_sslVerifyCertificate' ) ) || ( 1 === (int) get_option( 'mainwp_api_sslVerifyCertificate' ) ) ) ? 1 : 0;
418 + MainWP_System_Utility::get_wp_file_system();
419 419
420 - if ( empty( $ssl_api_verifyhost ) ) {
421 - add_filter( 'http_request_args', array( self::get_class_name(), 'no_ssl_filter_function' ), 99, 2 );
422 - }
420 + /**
421 + * WordPress files system object.
422 + *
423 + * @global object
424 + */
425 + global $wp_filesystem;
423 426
424 - add_filter( 'http_request_args', array( self::get_class_name(), 'http_request_reject_unsafe_urls' ), 99, 2 );
427 + if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) {
428 + include_once ABSPATH . '/wp-admin/includes/screen.php'; // NOSONAR - WP compatible.
429 + }
425 430
426 - $result = $installer->run(
427 - array(
428 - 'package' => $url,
429 - 'destination' => WP_PLUGIN_DIR,
430 - 'clear_destination' => false,
431 - 'clear_working' => true,
432 - 'hook_extra' => array(),
433 - )
434 - );
431 + include_once ABSPATH . '/wp-admin/includes/template.php'; // NOSONAR - WP compatible.
432 + include_once ABSPATH . '/wp-admin/includes/misc.php'; // NOSONAR - WP compatible.
433 + include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; // NOSONAR - WP compatible.
434 + include_once ABSPATH . '/wp-admin/includes/plugin.php'; // NOSONAR - WP compatible.
435 435
436 - remove_filter( 'http_request_args', array( self::get_class_name(), 'http_request_reject_unsafe_urls' ), 99, 2 );
436 + // To fix conflict.
437 + if ( is_plugin_active( 'git-updater/git-updater.php' ) ) {
438 + remove_all_filters( 'upgrader_source_selection' );
439 + }
437 440
438 - if ( '0' === $ssl_verifyhost ) {
439 - remove_filter( 'http_request_args', array( self::get_class_name(), 'no_ssl_filter_function' ), 99 );
440 - }
441 + $installer = new \WP_Upgrader();
442 + $ssl_verifyhost = get_option( 'mainwp_sslVerifyCertificate' );
443 + $ssl_api_verifyhost = ( ( false === get_option( 'mainwp_api_sslVerifyCertificate' ) ) || ( 1 === (int) get_option( 'mainwp_api_sslVerifyCertificate' ) ) ) ? 1 : 0;
441 444
442 - $error = null;
443 - $output = null;
444 - $plugin_slug = null;
445 + if ( empty( $ssl_api_verifyhost ) ) {
446 + add_filter( 'http_request_args', array( static::get_class_name(), 'no_ssl_filter_function' ), 99, 2 );
447 + }
445 448
446 - if ( is_wp_error( $result ) ) {
447 - $error_code = $result->get_error_code();
448 - if ( $result->get_error_data() && is_string( $result->get_error_data() ) ) {
449 - $error = $error_code . ' - ' . $result->get_error_data();
450 - } else {
451 - $error = $error_code;
452 - }
453 - } else {
454 - $path = $result['destination'];
449 + add_filter( 'http_request_args', array( static::get_class_name(), 'http_request_reject_unsafe_urls' ), 99, 2 );
455 450
456 - foreach ( $result['source_files'] as $srcFile ) {
451 + $result = $installer->run(
452 + array(
453 + 'package' => $url,
454 + 'destination' => WP_PLUGIN_DIR,
455 + 'clear_destination' => false,
456 + 'clear_working' => true,
457 + 'hook_extra' => array(),
458 + )
459 + );
457 460
458 - if ( 'readme.txt' === $srcFile ) {
459 - continue;
460 - }
461 + remove_filter( 'http_request_args', array( static::get_class_name(), 'http_request_reject_unsafe_urls' ), 99, 2 );
461 462
462 - $thePlugin = get_plugin_data( $path . $srcFile );
463 + if ( '0' === $ssl_verifyhost ) {
464 + remove_filter( 'http_request_args', array( static::get_class_name(), 'no_ssl_filter_function' ), 99 );
465 + }
463 466
464 - if ( null !== $thePlugin && '' !== $thePlugin && '' !== $thePlugin['Name'] ) {
465 - $the_name = self::polish_string_name( $thePlugin['Name'] );
466 - $output .= esc_html( $the_name ) . ' ' . esc_html__( 'installed successfully. Do not forget to activate the extension API license.', 'mainwp' );
467 - $plugin_slug = $result['destination_name'] . '/' . $srcFile;
467 + $error = null;
468 + $output = null;
469 + $plugin_slug = null;
468 470
469 - if ( $activatePlugin ) {
470 - activate_plugin( $path . $srcFile, '', false, true );
471 + if ( is_wp_error( $result ) ) {
472 + $error_code = $result->get_error_code();
473 + if ( $result->get_error_data() && is_string( $result->get_error_data() ) ) {
474 + $error = $error_code . ' - ' . $result->get_error_data();
475 + } else {
476 + $error = $error_code;
477 + }
478 + } else {
479 + $path = $result['destination'];
471 480
472 - /**
473 - * Extension API activation
474 - *
475 - * Activates the extension API license upon the extension installation.
476 - *
477 - * @since Unknown
478 - * @ignore
479 - */
480 - do_action( 'mainwp_api_extension_activated', $path . $srcFile );
481 - }
481 + foreach ( $result['source_files'] as $srcFile ) {
482 482
483 - break;
484 - }
485 - }
486 - }
483 + if ( 'readme.txt' === $srcFile ) {
484 + continue;
485 + }
487 486
488 - if ( ! empty( $error ) ) {
489 - $return['error'] = $error;
490 - } else {
491 - $return['result'] = 'SUCCESS';
492 - $return['output'] = $output;
493 - $return['slug'] = esc_html( $plugin_slug );
494 - }
487 + $thePlugin = get_plugin_data( $path . $srcFile );
495 488
496 - return $return;
497 - }
489 + if ( null !== $thePlugin && '' !== $thePlugin && '' !== $thePlugin['Name'] ) {
490 + $the_name = static::polish_string_name( $thePlugin['Name'] );
491 + $output .= esc_html( $the_name ) . ' ' . esc_html__( 'installed successfully. Do not forget to activate the extension API license.', 'mainwp' );
492 + $plugin_slug = $result['destination_name'] . '/' . $srcFile;
498 493
499 - /**
500 - * Check if MainWP Extension is available.
501 - *
502 - * @param mixed $pAPI MainWP Extension API Key.
503 - *
504 - * @return boolean true|false.
505 - */
506 - public static function is_extension_available( $pAPI ) {
494 + if ( $activatePlugin ) {
495 + activate_plugin( $path . $srcFile, '', false, true );
507 496
508 - MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
497 + /**
498 + * Extension API activation
499 + *
500 + * Activates the extension API license upon the extension installation.
501 + *
502 + * @since Unknown
503 + * @ignore
504 + */
505 + do_action( 'mainwp_api_extension_activated', $path . $srcFile );
506 + }
509 507
510 - $extensions = self::get_extensions();
511 - if ( isset( $extensions ) && is_array( $extensions ) ) {
512 - foreach ( $extensions as $extension ) {
513 - $slug = dirname( $extension['slug'] );
514 - if ( $slug === $pAPI ) {
515 - return true;
516 - }
517 - }
518 - }
519 - return false;
520 - }
508 + break;
509 + }
510 + }
511 + }
521 512
522 - /**
523 - * Check if MainWP Extension is enabled.
524 - *
525 - * @param mixed $pluginFile MainWP Extension to bo verified.
526 - *
527 - * @return array 'key' => md5( $pluginFile . '-SNNonceAdder').
528 - */
529 - public static function is_extension_enabled( $pluginFile ) {
530 - MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
531 - return array( 'key' => md5( $pluginFile . '-SNNonceAdder' ) );
532 - }
513 + if ( ! empty( $error ) ) {
514 + $return['error'] = $error;
515 + } else {
516 + $return['result'] = 'SUCCESS';
517 + $return['output'] = $output;
518 + $return['slug'] = esc_html( $plugin_slug );
519 + }
533 520
534 - /**
535 - * Create Menu Extension Array.
536 - *
537 - * @param mixed $slug menu slug.
538 - *
539 - * @return array Menu Array.
540 - */
541 - public static function added_on_menu( $slug ) {
542 - $snMenuExtensions = get_option( 'mainwp_extmenu' );
543 - if ( ! is_array( $snMenuExtensions ) ) {
544 - $snMenuExtensions = array();
545 - }
546 - return in_array( $slug, $snMenuExtensions );
547 - }
521 + return $return;
522 + }
548 523
549 - /**
550 - * Check if MainWP Extension is activated or not.
551 - *
552 - * @param mixed $plugin_slug MainWP Extension slug.
553 - *
554 - * @return boolean true|false.
555 - */
556 - public static function is_extension_activated( $plugin_slug ) {
557 - $extensions = self::get_indexed_extensions_infor( array( 'activated' => true ) );
558 - return isset( $extensions[ $plugin_slug ] ) ? true : false;
559 - }
524 + /**
525 + * Check if MainWP Extension is available.
526 + *
527 + * @param mixed $pAPI MainWP Extension API Key.
528 + *
529 + * @return boolean true|false.
530 + */
531 + public static function is_extension_available( $pAPI ) {
560 532
561 - /**
562 - * Verify MainWP Extension.
563 - *
564 - * @param mixed $pluginFile MainWP Extensoin to verify.
565 - * @param mixed $key Child Site Key.
566 - *
567 - * @return mixed md5( $pluginFile . '-SNNonceAdder' ) === $key
568 - */
569 - public static function hook_verify( $pluginFile, $key ) {
570 - return ( md5( $pluginFile . '-SNNonceAdder' ) === $key );
571 - }
533 + MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
572 534
573 - /**
574 - * Get sql websites for current user.
575 - *
576 - * @param mixed $pluginFile Extension plugin file to verify.
577 - * @param mixed $key PThe child-key.
578 - *
579 - * @return mixed null|sql query.
580 - *
581 - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_current_wpid()
582 - */
583 - public static function hook_get_dashboard_sites( $pluginFile, $key ) {
584 - if ( ! self::hook_verify( $pluginFile, $key ) ) {
585 - return null;
586 - }
535 + $extensions = static::get_extensions();
536 + if ( isset( $extensions ) && is_array( $extensions ) ) {
537 + foreach ( $extensions as $extension ) {
538 + $slug = dirname( $extension['slug'] );
539 + if ( $slug === $pAPI ) {
540 + return true;
541 + }
542 + }
543 + }
544 + return false;
545 + }
587 546
588 - $current_wpid = MainWP_System_Utility::get_current_wpid();
547 + /**
548 + * Check if MainWP Extension is enabled.
549 + *
550 + * @param mixed $pluginFile MainWP Extension to bo verified.
551 + *
552 + * @return array 'key' => md5( $pluginFile . '-SNNonceAdder').
553 + */
554 + public static function is_extension_enabled( $pluginFile ) {
555 + MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
556 + return array( 'key' => md5( $pluginFile . '-SNNonceAdder' ) ); // NOSONAR - safe for sig file name.
557 + }
589 558
590 - if ( $current_wpid ) {
591 - $sql = MainWP_DB::instance()->get_sql_website_by_id( $current_wpid );
592 - } else {
593 - $sql = MainWP_DB::instance()->get_sql_websites_for_current_user();
594 - }
559 + /**
560 + * Create Menu Extension Array.
561 + *
562 + * @param mixed $slug menu slug.
563 + *
564 + * @return array Menu Array.
565 + */
566 + public static function added_on_menu( $slug ) {
567 + $snMenuExtensions = get_option( 'mainwp_extmenu' );
568 + if ( ! is_array( $snMenuExtensions ) ) {
569 + $snMenuExtensions = array();
570 + }
571 + return in_array( $slug, $snMenuExtensions );
572 + }
595 573
596 - return MainWP_DB::instance()->query( $sql );
597 - }
574 + /**
575 + * Check if MainWP Extension is activated or not.
576 + *
577 + * @param mixed $plugin_slug MainWP Extension slug.
578 + *
579 + * @return boolean true|false.
580 + */
581 + public static function is_extension_activated( $plugin_slug ) {
582 + $extensions = static::get_indexed_extensions_infor( array( 'activated' => true ) );
583 + return isset( $extensions[ $plugin_slug ] ) ? true : false;
584 + }
598 585
599 - /**
600 - * Fetch Authorized URLS.
601 - *
602 - * @param mixed $pluginFile Extension plugin file to verify.
603 - * @param string $key The child key.
604 - * @param object $dbwebsites The websites.
605 - * @param string $what Action to perorm.
606 - * @param mixed $params Request parameters.
607 - * @param mixed $handle Request handle.
608 - * @param mixed $output Request output.
609 - *
610 - * @uses MainWP_Connect::fetch_urls_authed()
611 - *
612 - * @return mixed false|MainWP_Connect::fetch_urls_authed()
613 - */
614 - public static function hook_fetch_urls_authed( $pluginFile, $key, $dbwebsites, $what, $params, $handle, $output ) {
615 - if ( ! self::hook_verify( $pluginFile, $key ) ) {
616 - return false;
617 - }
586 + /**
587 + * Verify MainWP Extension.
588 + *
589 + * @param mixed $pluginFile MainWP Extensoin to verify.
590 + * @param mixed $key Child Site Key.
591 + *
592 + * @return mixed md5( $pluginFile . '-SNNonceAdder' ) === $key
593 + */
594 + public static function hook_verify( $pluginFile, $key ) {
595 + return md5( $pluginFile . '-SNNonceAdder' ) === $key; // NOSONAR - safe for sig file name.
596 + }
618 597
619 - return MainWP_Connect::fetch_urls_authed( $dbwebsites, $what, $params, $handle, $output );
620 - }
598 + /**
599 + * Get sql websites for current user.
600 + *
601 + * @param mixed $pluginFile Extension plugin file to verify.
602 + * @param mixed $key PThe child-key.
603 + *
604 + * @return mixed null|sql query.
605 + *
606 + * @uses \MainWP\Dashboard\MainWP_System_Utility::get_current_wpid()
607 + */
608 + public static function hook_get_dashboard_sites( $pluginFile, $key ) {
609 + if ( ! static::hook_verify( $pluginFile, $key ) ) {
610 + return null;
611 + }
621 612
622 - /**
623 - * Fetch Authorized URL.
624 - *
625 - * @throws MainWP_Exception On incorrect website.
626 - * @param mixed $pluginFile Extension plugin file to verify.
627 - * @param mixed $key The child-key.
628 - * @param mixed $websiteId Child Site ID.
629 - * @param mixed $what What.
630 - * @param mixed $params Parameters.
631 - * @param null $rawResponse Raw responce.
632 - *
633 - * @return mixed false|throw|error
634 - *
635 - * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
636 - */
637 - public static function hook_fetch_url_authed( $pluginFile, $key, $websiteId, $what, $params, $rawResponse = null ) {
638 - if ( ! self::hook_verify( $pluginFile, $key ) ) {
639 - return false;
640 - }
613 + $current_wpid = MainWP_System_Utility::get_current_wpid();
641 614
642 - try {
643 - $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
644 - if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
645 - throw new MainWP_Exception( 'You can not edit this website.' );
646 - }
615 + if ( $current_wpid ) {
616 + $sql = MainWP_DB::instance()->get_sql_website_by_id( $current_wpid );
617 + } else {
618 + $sql = MainWP_DB::instance()->get_sql_websites_for_current_user();
619 + }
647 620
648 - return MainWP_Connect::fetch_url_authed( $website, $what, $params, $checkConstraints = false, $pForceFetch = false, $pRetryFailed = true, $rawResponse );
649 - } catch ( MainWP_Exception $e ) {
650 - return array(
651 - 'error' => MainWP_Error_Helper::get_error_message( $e ),
652 - 'errorCode' => 'excep_fetch_url_authed',
653 - );
654 - }
655 - }
621 + return MainWP_DB::instance()->query( $sql );
622 + }
656 623
657 - /**
658 - * Get DB Sites.
659 - *
660 - * @param mixed $pluginFile Extension plugin file to verify.
661 - * @param mixed $key The child-key.
662 - * @param mixed $sites Child Sites.
663 - * @param string $groups Groups.
664 - * @param bool $options Options.
665 - *
666 - * @return array $dbwebsites.
667 - *
668 - * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
669 - * @uses \MainWP\Dashboard\MainWP_Utility::map_site()
670 - */
671 - public static function hook_get_db_sites( $pluginFile, $key, $sites, $groups = '', $options = false ) {
672 - if ( ! self::hook_verify( $pluginFile, $key ) ) {
673 - return false;
674 - }
624 + /**
625 + * Fetch Authorized URLS.
626 + *
627 + * @param mixed $pluginFile Extension plugin file to verify.
628 + * @param string $key The child key.
629 + * @param object $dbwebsites The websites.
630 + * @param string $what Action to perorm.
631 + * @param mixed $params Request parameters.
632 + * @param mixed $handle Request handle.
633 + * @param mixed $output Request output.
634 + *
635 + * @uses MainWP_Connect::fetch_urls_authed()
636 + *
637 + * @return mixed false|MainWP_Connect::fetch_urls_authed()
638 + */
639 + public static function hook_fetch_urls_authed( $pluginFile, $key, $dbwebsites, $what, $params, $handle, $output ) {
640 + if ( ! static::hook_verify( $pluginFile, $key ) ) {
641 + return false;
642 + }
675 643
676 - MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
644 + return MainWP_Connect::fetch_urls_authed( $dbwebsites, $what, $params, $handle, $output );
645 + }
677 646
678 - $dbwebsites = array();
647 + /**
648 + * Fetch Authorized URL.
649 + *
650 + * @throws MainWP_Exception On incorrect website.
651 + * @param mixed $pluginFile Extension plugin file to verify.
652 + * @param mixed $key The child-key.
653 + * @param mixed $websiteId Child Site ID.
654 + * @param mixed $what What.
655 + * @param mixed $params Parameters.
656 + * @param null $rawResponse Raw responce.
657 + *
658 + * @return mixed false|throw|error
659 + *
660 + * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
661 + */
662 + public static function hook_fetch_url_authed( $pluginFile, $key, $websiteId, $what, $params, $rawResponse = null ) {
663 + if ( ! static::hook_verify( $pluginFile, $key ) ) {
664 + return false;
665 + }
679 666
680 - $data_fields = MainWP_System_Utility::get_default_map_site_fields();
681 - $data_fields[] = 'verify_certificate';
667 + $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
682 668
683 - if ( is_array( $options ) ) {
684 - foreach ( $options as $option_name => $value ) {
685 - if ( ! empty( $value ) && in_array( $option_name, self::$possible_options ) ) {
686 - if ( ! in_array( $option_name, $data_fields ) ) {
687 - $data_fields[] = $option_name;
688 - }
689 - }
690 - }
691 - }
669 + if ( $website && $website->suspended && ( ! defined( 'DOING_CRON' ) || true !== DOING_CRON ) && ( ! defined( 'WP_CLI' ) || true !== WP_CLI ) ) {
670 + return array(
671 + 'error' => esc_html__( 'Suspended site.', 'mainwp' ),
672 + 'errorCode' => 'SUSPENDED_SITE',
673 + );
674 + }
692 675
693 - if ( '' !== $sites ) {
694 - foreach ( $sites as $k => $v ) {
695 - if ( MainWP_Utility::ctype_digit( $v ) ) {
696 - $website = MainWP_DB::instance()->get_website_by_id( $v );
697 - if ( empty( $website ) ) {
698 - continue;
699 - }
700 - $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
701 - }
702 - }
703 - }
676 + return static::fetch_url_authed( $websiteId, $what, $params, $rawResponse );
677 + }
704 678
705 - if ( is_array( $groups ) ) {
706 - foreach ( $groups as $k => $v ) {
707 - if ( MainWP_Utility::ctype_digit( $v ) ) {
708 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_id( $v ) );
709 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
710 - $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
711 - }
712 - MainWP_DB::free_result( $websites );
713 - }
714 - }
715 - }
679 + /**
680 + * Fetch Authorized URL.
681 + *
682 + * @throws MainWP_Exception On incorrect website.
683 + * @param mixed $websiteId Child Site ID.
684 + * @param mixed $what What.
685 + * @param mixed $params Parameters.
686 + * @param null $rawResponse Raw responce.
687 + *
688 + * @return mixed false|throw|error
689 + *
690 + * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
691 + */
692 + public static function fetch_url_authed( $websiteId, $what, $params, $rawResponse = null ) {
693 + try {
694 + $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
695 + if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
696 + throw new MainWP_Exception( 'You can not edit this website.' );
697 + }
716 698
717 - return $dbwebsites;
718 - }
699 + return MainWP_Connect::fetch_url_authed( $website, $what, $params, false, false, true, $rawResponse );
700 + } catch ( MainWP_Exception $e ) {
701 + return array(
702 + 'error' => MainWP_Error_Helper::get_error_message( $e ),
703 + 'errorCode' => 'excep_fetch_url_authed',
704 + );
705 + }
706 + }
719 707
708 + /**
709 + * Retrieves database sites based on provided parameters.
710 + *
711 + * This method verifies the hook using the plugin file and key, then retrieves
712 + * sites from the database according to the specified criteria.
713 + *
714 + * @param string $pluginFile The path to the plugin file for verification.
715 + * @param string $key The key used for hook verification.
716 + * @param array|string $sites Site IDs to retrieve. Can be an array of IDs or a comma-separated string.
717 + * @param array|string $groups Optional. Group IDs to filter sites by. Can be an array or comma-separated string.
718 + * Default is empty string.
719 + * @param array|bool $options Optional. Fields to return or additional query options.
720 + * Set to false to return all fields. Default is false.
721 + * @param array|string $clients Optional. Client IDs to filter sites by. Can be an array or comma-separated string.
722 + * Default is empty string.
723 + *
724 + * @return array|bool Array of site objects on success, or false if hook verification fails.
725 + *
726 + * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
727 + * @uses \MainWP\Dashboard\MainWP_Utility::map_site()
728 + */
729 + public static function hook_get_db_sites( $pluginFile, $key, $sites, $groups = '', $options = false, $clients = '' ) {
730 + if ( ! static::hook_verify( $pluginFile, $key ) ) {
731 + return false;
732 + }
720 733
721 - /**
722 - * Get DB Sites.
723 - *
724 - * @since 4.4.2
725 - *
726 - * @param mixed $pluginFile Extension plugin file to verify.
727 - * @param mixed $key The child-key.
728 - * @param mixed $params params.
729 - *
730 - * @return array $dbwebsites.
731 - */
732 - public static function hook_get_db_websites( $pluginFile, $key, $params = array() ) {
733 - if ( ! self::hook_verify( $pluginFile, $key ) ) {
734 - return false;
735 - }
734 + MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
735 + $params = array(
736 + 'fields' => $options,
737 + 'sites' => $sites,
738 + 'groups' => $groups,
739 + 'clients' => $clients,
740 + );
741 + return MainWP_DB::instance()->get_db_sites( $params );
742 + }
736 743
737 - if ( empty( $params ) || ! is_array( $params ) ) {
738 - return false;
739 - }
744 + /**
745 + * Get DB Sites.
746 + *
747 + * @since 4.4.2
748 + *
749 + * @param mixed $pluginFile Extension plugin file to verify.
750 + * @param mixed $key The child-key.
751 + * @param mixed $params params.
752 + *
753 + * @return array $dbwebsites.
754 + */
755 + public static function hook_get_db_websites( $pluginFile, $key, $params = array() ) {
756 + if ( ! static::hook_verify( $pluginFile, $key ) ) {
757 + return false;
758 + }
740 759
741 - $dbwebsites = array();
760 + if ( empty( $params ) || ! is_array( $params ) ) {
761 + return false;
762 + }
763 + return MainWP_DB::instance()->get_db_sites( $params );
764 + }
742 765
743 - $data_fields = MainWP_System_Utility::get_default_map_site_fields();
744 - $data_fields[] = 'verify_certificate';
745 766
746 - $fields = isset( $params['fields'] ) && is_array( $params['fields'] ) ? $params['fields'] : array();
747 - $sites = isset( $params['sites'] ) && is_array( $params['sites'] ) ? $params['sites'] : array();
748 - $groups = isset( $params['groups'] ) && is_array( $params['groups'] ) ? $params['groups'] : array();
749 - $clients = isset( $params['clients'] ) && is_array( $params['clients'] ) ? $params['clients'] : array();
767 + /**
768 + * Get Sites.
769 + *
770 + * @param string $pluginFile Extension plugin file to verify.
771 + * @param string $key The child-key.
772 + * @param int $websiteid The id of the child site you wish to retrieve.
773 + * @param bool $for_manager Check Team Control.
774 + * @param array $others Array of others.
775 + *
776 + * @return array $output Array of content to output.
777 + *
778 + * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
779 + * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
780 + */
781 + public static function hook_get_sites( $pluginFile, $key, $websiteid = null, $for_manager = false, $others = array() ) { // phpcs:ignore -- NOSONAR - not quite complex function.
782 + if ( ! static::hook_verify( $pluginFile, $key ) ) {
783 + return false;
784 + }
750 785
751 - if ( is_array( $fields ) ) {
752 - foreach ( $fields as $field ) {
753 - if ( in_array( $field, self::$possible_options ) ) {
754 - if ( ! in_array( $field, $data_fields ) ) {
755 - $data_fields[] = $field;
756 - }
757 - }
758 - }
759 - }
786 + if ( $for_manager && ( ! defined( 'MWP_TEAMCONTROL_PLUGIN_SLUG' ) || ! \mainwp_current_user_can( 'extension', dirname( MWP_TEAMCONTROL_PLUGIN_SLUG ) ) ) ) {
787 + return false;
788 + }
789 + MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
760 790
761 - if ( ! empty( $sites ) ) {
762 - foreach ( $sites as $k => $v ) {
763 - if ( MainWP_Utility::ctype_digit( $v ) ) {
764 - $website = MainWP_DB::instance()->get_website_by_id( $v );
765 - if ( empty( $website ) ) {
766 - continue;
767 - }
768 - $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
769 - }
770 - }
771 - }
791 + return MainWP_DB::instance()->get_sites( $websiteid, $for_manager, $others );
792 + }
772 793
773 - if ( ! empty( $groups ) ) {
774 - foreach ( $groups as $k => $v ) {
775 - if ( MainWP_Utility::ctype_digit( $v ) ) {
776 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_id( $v ) );
777 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
778 - $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
779 - }
780 - MainWP_DB::free_result( $websites );
781 - }
782 - }
783 - }
794 + /**
795 + * Method hook_get_groups()
796 + *
797 + * Get Child Sites within groups & store them in an array.
798 + *
799 + * @param string $pluginFile Extension plugin file to verify.
800 + * @param string $key The child-key.
801 + * @param int $groupid The id of the group you wish to retrieve.
802 + * @param bool $for_manager Check Team Control.
803 + *
804 + * @return array|bool $output|false An array of arrays, the inner-array contains the id/name/array of site ids for the supplied groupid/all groups. False when something goes wrong.
805 + */
806 + public static function hook_get_groups( $pluginFile, $key, $groupid, $for_manager = false ) {
807 + if ( ! static::hook_verify( $pluginFile, $key ) ) {
808 + return false;
809 + }
784 810
785 - $client_sites = MainWP_DB_Client::instance()->get_websites_by_client_ids( $clients );
786 - if ( $client_sites ) {
787 - foreach ( $client_sites as $website ) {
788 - $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
789 - }
790 - }
811 + if ( $for_manager && ( ! defined( 'MWP_TEAMCONTROL_PLUGIN_SLUG' ) || ! \mainwp_current_user_can( 'extension', dirname( MWP_TEAMCONTROL_PLUGIN_SLUG ) ) ) ) {
812 + return false;
813 + }
791 814
792 - return $dbwebsites;
793 - }
815 + MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
794 816
817 + if ( isset( $groupid ) ) {
818 + $group = MainWP_DB_Common::instance()->get_group_by_id( $groupid );
819 + if ( empty( $group ) ) {
820 + return false;
821 + }
795 822
796 - /**
797 - * Get Sites.
798 - *
799 - * @param string $pluginFile Extension plugin file to verify.
800 - * @param string $key The child-key.
801 - * @param int $websiteid The id of the child site you wish to retrieve.
802 - * @param bool $for_manager Check Team Control.
803 - * @param array $others Array of others.
804 - *
805 - * @return array $output Array of content to output.
806 - *
807 - * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
808 - * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
809 - */
810 - public static function hook_get_sites( $pluginFile, $key, $websiteid = null, $for_manager = false, $others = array() ) { // phpcs:ignore -- not quite complex function.
811 - if ( ! self::hook_verify( $pluginFile, $key ) ) {
812 - return false;
813 - }
823 + $websites = MainWP_DB::instance()->get_websites_by_group_id( $group->id );
824 + $websitesOut = array();
825 + foreach ( $websites as $website ) {
826 + $websitesOut[] = $website->id;
827 + }
814 828
815 - if ( $for_manager && ( ! defined( 'MWP_TEAMCONTROL_PLUGIN_SLUG' ) || ! mainwp_current_user_have_right( 'extension', dirname( MWP_TEAMCONTROL_PLUGIN_SLUG ) ) ) ) {
816 - return false;
817 - }
829 + return array(
830 + array(
831 + 'id' => $groupid,
832 + 'name' => $group->name,
833 + 'websites' => $websitesOut,
834 + ),
835 + );
836 + }
818 837
819 - MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
838 + $groups = MainWP_DB_Common::instance()->get_groups_and_count( null, $for_manager );
839 + $output = array();
840 + foreach ( $groups as $group ) {
841 + $websites = MainWP_DB::instance()->get_websites_by_group_id( $group->id );
842 + $websitesOut = array();
843 + foreach ( $websites as $website ) {
844 + if ( in_array( $website->id, $websitesOut ) ) {
845 + continue;
846 + }
847 + $websitesOut[] = $website->id;
848 + }
849 + $output[] = array(
850 + 'id' => $group->id,
851 + 'name' => $group->name,
852 + 'websites' => $websitesOut,
853 + );
854 + }
820 855
821 - if ( ! is_array( $others ) ) {
822 - $others = array();
823 - }
856 + return $output;
857 + }
824 858
825 - $search_site = null;
826 - $orderBy = 'wp.url';
827 - $offset = false;
828 - $rowcount = false;
829 - $extraWhere = null;
830 859
831 - if ( isset( $websiteid ) && ( null !== $websiteid ) ) {
832 - $website = MainWP_DB::instance()->get_website_by_id( $websiteid );
860 + /**
861 + * Get all loaded extensions.
862 + *
863 + * @return mainwp_extensions value.
864 + */
865 + public static function hook_get_all_extensions() {
866 + MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
867 + return get_option( 'mainwp_extensions' );
868 + }
833 869
834 - if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
835 - return false;
836 - }
870 + /**
871 + * Clone Site.
872 + *
873 + * @param mixed $pluginFile Extension plugin file to verify.
874 + * @param mixed $key The child-key.
875 + * @param mixed $websiteid Child Site ID.
876 + * @param mixed $cloneID Clone ID.
877 + * @param mixed $clone_url URL to CLone to.
878 + * @param bool $force_update true|false, force an update.
879 + *
880 + * @return mixed false|$ret
881 + *
882 + * @uses \MainWP\Dashboard\MainWP_Sync::sync_site()
883 + * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
884 + * @uses \MainWP\Dashboard\MainWP_Utility::remove_http_www_prefix()
885 + */
886 + public static function hook_clone_site( $pluginFile, $key, $websiteid, $cloneID, $clone_url, $force_update = false ) { //phpcs:ignore -- NOSONAR - complex.
887 + if ( ! static::hook_verify( $pluginFile, $key ) ) {
888 + return false;
889 + }
837 890
838 - if ( ! mainwp_current_user_have_right( 'site', $websiteid ) ) {
839 - return false;
840 - }
891 + if ( ! empty( $websiteid ) && ! empty( $cloneID ) ) {
841 892
842 - return array(
843 - array(
844 - 'id' => $websiteid,
845 - 'url' => MainWP_Utility::get_nice_url( $website->url, true ),
846 - 'name' => $website->name,
847 - 'totalsize' => $website->totalsize,
848 - 'sync_errors' => $website->sync_errors,
849 - ),
850 - );
851 - } else {
852 - if ( isset( $others['orderby'] ) ) {
853 - if ( ( 'site' === $others['orderby'] ) ) {
854 - $orderBy = 'wp.name ' . ( 'asc' === $others['order'] ? 'asc' : 'desc' );
855 - } elseif ( ( 'url' === $others['orderby'] ) ) {
856 - $orderBy = 'wp.url ' . ( 'asc' === $others['order'] ? 'asc' : 'desc' );
857 - }
858 - }
859 - if ( isset( $others['search'] ) ) {
860 - $search_site = trim( $others['search'] );
861 - }
893 + $sql = MainWP_DB::instance()->get_sql_website_by_id( $websiteid );
894 + $websites = MainWP_DB::instance()->query( $sql );
895 + $website = MainWP_DB::fetch_object( $websites );
862 896
863 - if ( is_array( $others ) ) {
864 - if ( isset( $others['plugins_slug'] ) ) {
897 + if ( empty( $website ) ) {
898 + return array( 'error' => esc_html__( 'Website not found.', 'mainwp' ) );
899 + }
865 900
866 - $slugs = explode( ',', $others['plugins_slug'] );
867 - $extraWhere = '';
868 - foreach ( $slugs as $slug ) {
869 - $slug = wp_json_encode( $slug );
870 - $slug = trim( $slug, '"' );
871 - $slug = str_replace( '\\', '.', $slug );
872 - $extraWhere .= ' wp.plugins REGEXP "' . $slug . '" OR';
873 - }
874 - $extraWhere = trim( rtrim( $extraWhere, 'OR' ) );
901 + $ret = array();
875 902
876 - if ( '' === $extraWhere ) {
877 - $extraWhere = null;
878 - } else {
879 - $extraWhere = '(' . $extraWhere . ')';
880 - }
881 - }
882 - }
883 - }
903 + if ( '/' !== substr( $clone_url, - 1 ) ) {
904 + $clone_url .= '/';
905 + }
884 906
885 - $totalRecords = '';
907 + $tmp1 = MainWP_Utility::remove_http_www_prefix( $website->url );
908 + $tmp2 = MainWP_Utility::remove_http_www_prefix( $clone_url );
886 909
887 - if ( isset( $others['per_page'] ) && ! empty( $others['per_page'] ) ) {
888 - $sql = MainWP_DB::instance()->get_sql_websites_for_current_user( false, $search_site, $orderBy, false, false, $extraWhere, $for_manager );
889 - $websites_total = MainWP_DB::instance()->query( $sql );
890 - $totalRecords = ( $websites_total ? MainWP_DB::num_rows( $websites_total ) : 0 );
910 + if ( false === strpos( $tmp2, $tmp1 ) ) {
911 + return false;
912 + }
891 913
892 - if ( $websites_total ) {
893 - MainWP_DB::free_result( $websites_total );
894 - }
914 + $clone_sites = MainWP_DB::instance()->get_websites_by_url( $clone_url );
915 + if ( $clone_sites ) {
916 + $clone_site = current( $clone_sites );
917 + if ( $clone_site && $clone_site->is_staging ) {
895 918
896 - $rowcount = absint( $others['per_page'] );
897 - $pagenum = isset( $others['paged'] ) ? absint( $others['paged'] ) : 0;
898 - if ( $pagenum > $totalRecords ) {
899 - $pagenum = $totalRecords;
900 - }
901 - $pagenum = max( 1, $pagenum );
902 - $offset = ( $pagenum - 1 ) * $rowcount;
919 + // try to decrypt priv key.
920 + $de_privkey = MainWP_Encrypt_Data_Lib::instance()->decrypt_privkey( base64_decode( $website->privkey ), $website->id ); // phpcs:ignore -- NOSONAR - base64_encode trust.
921 + if ( ! empty( $de_privkey ) ) {
922 + $en_privkey = MainWP_Encrypt_Data_Lib::instance()->encrypt_privkey( $de_privkey, $clone_site->id, true ); // create encrypt priv key for clone site.
923 + } else {
924 + $en_privkey = base64_decode( $website->privkey ); // phpcs:ignore -- NOSONAR -trust - compatible.
925 + }
903 926
904 - }
927 + if ( $force_update ) {
928 + MainWP_DB::instance()->update_website_values(
929 + $clone_site->id,
930 + array(
931 + 'adminname' => $website->adminname,
932 + 'pubkey' => $website->pubkey,
933 + 'privkey' => is_array( $en_privkey ) ? base64_encode( json_encode( $en_privkey ) ) : base64_encode( $en_privkey ), //phpcs:ignore -- NOSONAR -ok.
934 + 'verify_certificate' => $website->verify_certificate,
935 + 'uniqueId' => ( null !== $website->uniqueId ? $website->uniqueId : '' ),
936 + // MWP-1548: decrypt the source's credentials and let
937 + // update_website_values re-encrypt under a fresh keyfile,
938 + // so the cloned site doesn't share the source row's file_key.
939 + 'http_user' => MainWP_Credential_Storage::decrypt_credential( $website->http_user ),
940 + 'http_pass' => MainWP_Credential_Storage::decrypt_credential( $website->http_pass ),
941 + 'ssl_version' => $website->ssl_version,
942 + 'force_use_ipv4' => $website->force_use_ipv4,
943 + )
944 + );
945 + }
946 + $ret['siteid'] = $clone_site->id;
947 + $ret['response'] = esc_html__( 'Site updated.', 'mainwp' );
948 + }
949 + return $ret;
950 + }
951 + $clone_name = $website->name . ' - ' . $cloneID;
905 952
906 - $sql = MainWP_DB::instance()->get_sql_websites_for_current_user( false, $search_site, $orderBy, $offset, $rowcount, $extraWhere, $for_manager );
907 - $websites = MainWP_DB::instance()->query( $sql );
953 + /**
954 + * Current user global.
955 + *
956 + * @global string
957 + */
958 + global $current_user;
908 959
909 - $output = array();
910 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
911 - $re = array(
912 - 'id' => $website->id,
913 - 'url' => MainWP_Utility::get_nice_url( $website->url, true ),
914 - 'name' => $website->name,
915 - 'totalsize' => $website->totalsize,
916 - 'sync_errors' => $website->sync_errors,
917 - );
960 + $others = array(
961 + 'groupids' => array(),
962 + 'groupnames' => array(),
963 + 'verifyCertificate' => $website->verify_certificate,
964 + 'addUniqueId' => ( null !== $website->uniqueId ? $website->uniqueId : '' ),
965 + // MWP-1548: same pattern as the force_update branch above --
966 + // decrypt the source's credentials, let add_website re-encrypt
967 + // for the new clone with a fresh keyfile.
968 + 'http_user' => MainWP_Credential_Storage::decrypt_credential( $website->http_user ),
969 + 'http_pass' => MainWP_Credential_Storage::decrypt_credential( $website->http_pass ),
970 + 'sslVersion' => $website->ssl_version,
971 + 'force_use_ipv4' => $website->force_use_ipv4,
972 + 'wpe' => $website->wpe,
973 + 'isStaging' => 1,
974 + );
918 975
919 - if ( 0 < $totalRecords ) {
920 - $re['totalRecords'] = $totalRecords;
921 - $totalRecords = 0;
922 - }
976 + $de_privkey = base64_decode( $website->privkey ); //phpcs:ignore -- NOSONAR -trust - compatible.
977 + $de_privkey = MainWP_Encrypt_Data_Lib::instance()->decrypt_privkey( $de_privkey, $website->id );
923 978
924 - $output[] = $re;
925 - }
926 - MainWP_DB::free_result( $websites );
979 + if ( ! empty( $de_privkey ) ) {
980 + $de_privkey = base64_encode( $de_privkey ); //phpcs:ignore -- NOSONAR -ok.
981 + } else {
982 + $de_privkey = $website->privkey; // compatible - encoded.
983 + }
927 984
928 - return $output;
929 - }
985 + $id = MainWP_DB::instance()->add_website( $current_user->ID, $clone_name, $clone_url, $website->adminname, $website->pubkey, $de_privkey, $others );
930 986
931 - /**
932 - * Method hook_get_groups()
933 - *
934 - * Get Child Sites within groups & store them in an array.
935 - *
936 - * @param string $pluginFile Extension plugin file to verify.
937 - * @param string $key The child-key.
938 - * @param int $groupid The id of the group you wish to retrieve.
939 - * @param bool $for_manager Check Team Control.
940 - *
941 - * @return array|bool $output|false An array of arrays, the inner-array contains the id/name/array of site ids for the supplied groupid/all groups. False when something goes wrong.
942 - */
943 - public static function hook_get_groups( $pluginFile, $key, $groupid, $for_manager = false ) {
944 - if ( ! self::hook_verify( $pluginFile, $key ) ) {
945 - return false;
946 - }
987 + /** This action is documented in class\class-mainwp-manage-sites-view.php */
988 + do_action( 'mainwp_added_new_site', $id, $website );
947 989
948 - if ( $for_manager && ( ! defined( 'MWP_TEAMCONTROL_PLUGIN_SLUG' ) || ! mainwp_current_user_have_right( 'extension', dirname( MWP_TEAMCONTROL_PLUGIN_SLUG ) ) ) ) {
949 - return false;
950 - }
990 + if ( $id ) {
991 + $group_id = get_option( 'mainwp_stagingsites_group_id' );
992 + if ( $group_id ) {
993 + $website = MainWP_DB::instance()->get_website_by_id( $id );
994 + if ( MainWP_System_Utility::can_edit_website( $website ) ) {
995 + MainWP_Sync::sync_site( $website, false, false );
996 + $group = MainWP_DB_Common::instance()->get_group_by_id( $group_id );
997 + if ( ! empty( $group ) ) {
998 + MainWP_DB_Common::instance()->update_group_site( $group->id, $id );
999 + }
1000 + }
1001 + }
1002 + $ret['response'] = esc_html__( 'Site successfully added.', 'mainwp' );
1003 + $ret['siteid'] = $id;
1004 + }
1005 + return $ret;
1006 + }
951 1007
952 - MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
1008 + return false;
1009 + }
953 1010
954 - if ( isset( $groupid ) ) {
955 - $group = MainWP_DB_Common::instance()->get_group_by_id( $groupid );
956 - if ( empty( $group ) ) {
957 - return false;
958 - }
1011 + /**
1012 + * Delete Clones Site.
1013 + *
1014 + * @param mixed $pluginFile Extension plugin file to verify.
1015 + * @param mixed $key The child-key.
1016 + * @param mixed $clone_url URL to Clone to.
1017 + * @param bool $clone_site_id Cloned Site ID.
1018 + *
1019 + * @return mixed false|array Array => "Success".
1020 + *
1021 + * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
1022 + * @uses \MainWP\Dashboard\MainWP_System_Utility::get_icons_dir()
1023 + */
1024 + public static function hook_delete_clone_site( $pluginFile, $key, $clone_url = '', $clone_site_id = false ) { //phpcs:ignore -- NOSONAR - complex.
1025 + if ( ! static::hook_verify( $pluginFile, $key ) ) {
1026 + return false;
1027 + }
959 1028
960 - $websites = MainWP_DB::instance()->get_websites_by_group_id( $group->id );
961 - $websitesOut = array();
962 - foreach ( $websites as $website ) {
963 - $websitesOut[] = $website->id;
964 - }
1029 + if ( empty( $clone_url ) && empty( $clone_site_id ) ) {
1030 + return false;
1031 + }
965 1032
966 - return array(
967 - array(
968 - 'id' => $groupid,
969 - 'name' => $group->name,
970 - 'websites' => $websitesOut,
971 - ),
972 - );
973 - }
1033 + $clone_site = null;
1034 + if ( ! empty( $clone_url ) ) {
1035 + if ( '/' !== substr( $clone_url, - 1 ) ) {
1036 + $clone_url .= '/';
1037 + }
1038 + $clone_sites = MainWP_DB::instance()->get_websites_by_url( $clone_url );
1039 + if ( ! empty( $clone_sites ) ) {
1040 + $clone_site = current( $clone_sites );
974 1041
975 - $groups = MainWP_DB_Common::instance()->get_groups_and_count( null, $for_manager );
976 - $output = array();
977 - foreach ( $groups as $group ) {
978 - $websites = MainWP_DB::instance()->get_websites_by_group_id( $group->id );
979 - $websitesOut = array();
980 - foreach ( $websites as $website ) {
981 - if ( in_array( $website->id, $websitesOut ) ) {
982 - continue;
983 - }
984 - $websitesOut[] = $website->id;
985 - }
986 - $output[] = array(
987 - 'id' => $group->id,
988 - 'name' => $group->name,
989 - 'websites' => $websitesOut,
990 - );
991 - }
1042 + }
1043 + } elseif ( ! empty( $clone_site_id ) ) {
1044 + $sql = MainWP_DB::instance()->get_sql_website_by_id( $clone_site_id );
1045 + $websites = MainWP_DB::instance()->query( $sql );
1046 + $clone_site = MainWP_DB::fetch_object( $websites );
1047 + }
992 1048
993 - return $output;
994 - }
1049 + if ( empty( $clone_site ) ) {
1050 + return array( 'error' => esc_html__( 'Not found the clone website', 'mainwp' ) );
1051 + }
995 1052
1053 + if ( $clone_site ) {
1054 + if ( empty( $clone_site->is_staging ) ) {
1055 + return false;
1056 + }
996 1057
997 - /**
998 - * Get all loaded extensions.
999 - *
1000 - * @return mainwp_extensions value.
1001 - */
1002 - public static function hook_get_all_extensions() {
1003 - MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
1004 - return get_option( 'mainwp_extensions' );
1005 - }
1058 + MainWP_System_Utility::get_wp_file_system();
1006 1059
1007 - /**
1008 - * Clone Site.
1009 - *
1010 - * @param mixed $pluginFile Extension plugin file to verify.
1011 - * @param mixed $key The child-key.
1012 - * @param mixed $websiteid Child Site ID.
1013 - * @param mixed $cloneID Clone ID.
1014 - * @param mixed $clone_url URL to CLone to.
1015 - * @param bool $force_update true|false, force an update.
1016 - *
1017 - * @return mixed false|$ret
1018 - *
1019 - * @uses \MainWP\Dashboard\MainWP_Sync::sync_site()
1020 - * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
1021 - * @uses \MainWP\Dashboard\MainWP_Utility::remove_http_www_prefix()
1022 - */
1023 - public static function hook_clone_site( $pluginFile, $key, $websiteid, $cloneID, $clone_url, $force_update = false ) {
1024 - if ( ! self::hook_verify( $pluginFile, $key ) ) {
1025 - return false;
1026 - }
1060 + /**
1061 + * WordPress files system object.
1062 + *
1063 + * @global object
1064 + */
1065 + global $wp_filesystem;
1027 1066
1028 - if ( ! empty( $websiteid ) && ! empty( $cloneID ) ) {
1067 + $favi = MainWP_DB::instance()->get_website_option( $clone_site, 'favi_icon', '' );
1068 + if ( ! empty( $favi ) && ( false !== strpos( $favi, 'favi-' . $clone_site->id . '-' ) ) ) {
1069 + $dirs = MainWP_System_Utility::get_icons_dir();
1070 + if ( $wp_filesystem->exists( $dirs[0] . $favi ) ) {
1071 + $wp_filesystem->delete( $dirs[0] . $favi );
1072 + }
1073 + }
1029 1074
1030 - $sql = MainWP_DB::instance()->get_sql_website_by_id( $websiteid );
1031 - $websites = MainWP_DB::instance()->query( $sql );
1032 - $website = MainWP_DB::fetch_object( $websites );
1075 + MainWP_DB::instance()->remove_website( $clone_site->id );
1033 1076
1034 - if ( empty( $website ) ) {
1035 - return array( 'error' => esc_html__( 'Website not found.', 'mainwp' ) );
1036 - }
1077 + /** This action is documented in pages\page-mainwp-manage-sites-handler.php */
1078 + do_action( 'mainwp_delete_site', $clone_site );
1079 + return array( 'result' => 'SUCCESS' );
1080 + }
1037 1081
1038 - $ret = array();
1082 + return false;
1083 + }
1039 1084
1040 - if ( '/' !== substr( $clone_url, - 1 ) ) {
1041 - $clone_url .= '/';
1042 - }
1085 + /**
1086 + * Add Groups.
1087 + *
1088 + * @param mixed $pluginFile Extension plugin file to verify.
1089 + * @param mixed $key The child-key.
1090 + * @param mixed $newName Name that you want to give the group.
1091 + *
1092 + * @return mixed false|$groupId
1093 + *
1094 + * @uses \MainWP\Dashboard\MainWP_Manage_Groups::check_group_name()
1095 + */
1096 + public static function hook_add_group( $pluginFile, $key, $newName ) {
1043 1097
1044 - $tmp1 = MainWP_Utility::remove_http_www_prefix( $website->url );
1045 - $tmp2 = MainWP_Utility::remove_http_www_prefix( $clone_url );
1098 + if ( ! static::hook_verify( $pluginFile, $key ) ) {
1099 + return false;
1100 + }
1046 1101
1047 - if ( false === strpos( $tmp2, $tmp1 ) ) {
1048 - return false;
1049 - }
1102 + /**
1103 + * Current user global.
1104 + *
1105 + * @global string
1106 + */
1107 + global $current_user;
1050 1108
1051 - $clone_sites = MainWP_DB::instance()->get_websites_by_url( $clone_url );
1052 - if ( $clone_sites ) {
1053 - $clone_site = current( $clone_sites );
1054 - if ( $clone_site && $clone_site->is_staging ) {
1055 - if ( $force_update ) {
1056 - MainWP_DB::instance()->update_website_values(
1057 - $clone_site->id,
1058 - array(
1059 - 'adminname' => $website->adminname,
1060 - 'pubkey' => $website->pubkey,
1061 - 'privkey' => $website->privkey,
1062 - 'verify_certificate' => $website->verify_certificate,
1063 - 'uniqueId' => ( null !== $website->uniqueId ? $website->uniqueId : '' ),
1064 - 'http_user' => $website->http_user,
1065 - 'http_pass' => $website->http_pass,
1066 - 'ssl_version' => $website->ssl_version,
1067 - )
1068 - );
1069 - }
1070 - $ret['siteid'] = $clone_site->id;
1071 - $ret['response'] = esc_html__( 'Site updated.', 'mainwp' );
1072 - }
1073 - return $ret;
1074 - }
1075 - $clone_name = $website->name . ' - ' . $cloneID;
1109 + if ( ! empty( $newName ) ) {
1110 + $groupId = MainWP_DB_Common::instance()->add_group( $current_user->ID, MainWP_Manage_Groups::check_group_name( $newName ) );
1076 1111
1077 - /**
1078 - * Current user global.
1079 - *
1080 - * @global string
1081 - */
1082 - global $current_user;
1083 -
1084 - $id = MainWP_DB::instance()->add_website( $current_user->ID, $clone_name, $clone_url, $website->adminname, $website->pubkey, $website->privkey, array(), array(), $website->verify_certificate, ( null !== $website->uniqueId ? $website->uniqueId : '' ), $website->http_user, $website->http_pass, $website->ssl_version, $website->wpe, $isStaging = 1 );
1085 -
1086 - /** This action is documented in class\class-mainwp-manage-sites-view.php */
1087 - do_action( 'mainwp_added_new_site', $id, $website );
1088 -
1089 - if ( $id ) {
1090 - $group_id = get_option( 'mainwp_stagingsites_group_id' );
1091 - if ( $group_id ) {
1092 - $website = MainWP_DB::instance()->get_website_by_id( $id );
1093 - if ( MainWP_System_Utility::can_edit_website( $website ) ) {
1094 - MainWP_Sync::sync_site( $website, false, false );
1095 - $group = MainWP_DB_Common::instance()->get_group_by_id( $group_id );
1096 - if ( ! empty( $group ) ) {
1097 - MainWP_DB_Common::instance()->update_group_site( $group->id, $id );
1098 - }
1099 - }
1100 - }
1101 - $ret['response'] = esc_html__( 'Site successfully added.', 'mainwp' );
1102 - $ret['siteid'] = $id;
1103 - }
1104 - return $ret;
1105 - }
1106 -
1107 - return false;
1108 - }
1109 -
1110 - /**
1111 - * Delete Clones Site.
1112 - *
1113 - * @param mixed $pluginFile Extension plugin file to verify.
1114 - * @param mixed $key The child-key.
1115 - * @param mixed $clone_url URL to Clone to.
1116 - * @param bool $clone_site_id Cloned Site ID.
1117 - *
1118 - * @return mixed false|array Array => "Success".
1119 - *
1120 - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
1121 - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_icons_dir()
1122 - */
1123 - public static function hook_delete_clone_site( $pluginFile, $key, $clone_url = '', $clone_site_id = false ) {
1124 - if ( ! self::hook_verify( $pluginFile, $key ) ) {
1125 - return false;
1126 - }
1127 -
1128 - if ( ( empty( $clone_url ) && empty( $clone_site_id ) ) ) {
1129 - return false;
1130 - }
1131 -
1132 - $clone_site = null;
1133 - if ( ! empty( $clone_url ) ) {
1134 - if ( '/' !== substr( $clone_url, - 1 ) ) {
1135 - $clone_url .= '/';
1136 - }
1137 - $clone_sites = MainWP_DB::instance()->get_websites_by_url( $clone_url );
1138 - if ( ! empty( $clone_sites ) ) {
1139 - $clone_site = current( $clone_sites );
1140 -
1141 - }
1142 - } elseif ( ! empty( $clone_site_id ) ) {
1143 - $sql = MainWP_DB::instance()->get_sql_website_by_id( $clone_site_id );
1144 - $websites = MainWP_DB::instance()->query( $sql );
1145 - $clone_site = MainWP_DB::fetch_object( $websites );
1146 - }
1147 -
1148 - if ( empty( $clone_site ) ) {
1149 - return array( 'error' => esc_html__( 'Not found the clone website', 'mainwp' ) );
1150 - }
1151 -
1152 - if ( $clone_site ) {
1153 - if ( empty( $clone_site->is_staging ) ) {
1154 - return false;
1155 - }
1156 -
1157 - $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
1158 -
1159 - /**
1160 - * WordPress files system object.
1161 - *
1162 - * @global object
1163 - */
1164 - global $wp_filesystem;
1165 -
1166 - $favi = MainWP_DB::instance()->get_website_option( $clone_site, 'favi_icon', '' );
1167 - if ( ! empty( $favi ) && ( false !== strpos( $favi, 'favi-' . $clone_site->id . '-' ) ) ) {
1168 - $dirs = MainWP_System_Utility::get_icons_dir();
1169 - if ( $wp_filesystem->exists( $dirs[0] . $favi ) ) {
1170 - $wp_filesystem->delete( $dirs[0] . $favi );
1171 - }
1172 - }
1173 -
1174 - MainWP_DB::instance()->remove_website( $clone_site->id );
1175 -
1176 - /** This action is documented in pages\page-mainwp-manage-sites-handler.php */
1177 - do_action( 'mainwp_delete_site', $clone_site );
1178 - return array( 'result' => 'SUCCESS' );
1179 - }
1180 -
1181 - return false;
1182 - }
1183 -
1184 - /**
1185 - * Add Groups.
1186 - *
1187 - * @param mixed $pluginFile Extension plugin file to verify.
1188 - * @param mixed $key The child-key.
1189 - * @param mixed $newName Name that you want to give the group.
1190 - *
1191 - * @return mixed false|$groupId
1192 - *
1193 - * @uses \MainWP\Dashboard\MainWP_Manage_Groups::check_group_name()
1194 - */
1195 - public static function hook_add_group( $pluginFile, $key, $newName ) {
1196 -
1197 - if ( ! self::hook_verify( $pluginFile, $key ) ) {
1198 - return false;
1199 - }
1200 -
1201 - /**
1202 - * Current user global.
1203 - *
1204 - * @global string
1205 - */
1206 - global $current_user;
1207 -
1208 - if ( ! empty( $newName ) ) {
1209 - $groupId = MainWP_DB_Common::instance()->add_group( $current_user->ID, MainWP_Manage_Groups::check_group_name( $newName ) );
1210 -
1211 - /** This action is documented in pages\page-mainwp-manage-groups.php */
1212 - do_action( 'mainwp_added_new_group', $groupId );
1213 - return $groupId;
1214 - }
1215 - return false;
1216 - }
1112 + /** This action is documented in pages\page-mainwp-manage-groups.php */
1113 + do_action( 'mainwp_added_new_group', $groupId );
1114 + return $groupId;
1115 + }
1116 + return false;
1117 + }
1217 1118 }