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