PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.3
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.3
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 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-sync.php

class-mainwp-sync.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.3, at class/class-mainwp-sync.php

682 lines 30.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Sync Handler
4 *
5 * Handle all syncing between MainWP & Child Site Network.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard;
11
12 /**
13 * Class MainWP_Sync
14 *
15 * @package MainWP\Dashboard
16 */
17 class MainWP_Sync { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
18
19
20 /**
21 * Method sync_website()
22 *
23 * Sync Child Site.
24 *
25 * @param object $website object.
26 * @param bool $clear_session to run the ending session or not.
27 *
28 * @return bool sync result.
29 */
30 public static function sync_website( $website, $clear_session = true ) {
31 if ( ! is_object( $website ) ) {
32 return false;
33 }
34 MainWP_DB::instance()->update_website_sync_values( $website->id, array( 'dtsSyncStart' => time() ) );
35 return static::sync_site( $website, false, true, $clear_session );
36 }
37
38 /**
39 * Method sync_site()
40 *
41 * @param mixed $pWebsite Null|userid.
42 * @param bool $pForceFetch Check if a fourced Sync.
43 * @param bool $pAllowDisconnect Check if allowed to disconect.
44 * @param bool $clear_session to run the ending session or not.
45 *
46 * @return bool sync_information_array
47 *
48 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension_by_user_id()
49 * @uses \MainWP\Dashboard\MainWP_DB::query()
50 * @uses \MainWP\Dashboard\MainWP_DB::update_website_option()
51 * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
52 * @uses \MainWP\Dashboard\MainWP_DB::free_result()
53 * @uses \MainWP\Dashboard\MainWP_Exception
54 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_primary_backup()
55 * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
56 */
57 public static function sync_site( &$pWebsite = null, $pForceFetch = false, $pAllowDisconnect = true, $clear_session = true ) { // phpcs:ignore -- NOSONAR - complexity method.
58
59 // to support demo data.
60 if ( MainWP_Demo_Handle::get_instance()->is_demo_website( $pWebsite ) ) {
61 return MainWP_Demo_Handle::get_instance()->handle_action_demo( $pWebsite, 'sync_site' );
62 }
63
64 if ( null === $pWebsite ) {
65 return false;
66 }
67 $userExtension = MainWP_DB_Common::instance()->get_user_extension_by_user_id( $pWebsite->userid );
68 if ( null === $userExtension ) {
69 return false;
70 }
71
72 if ( $clear_session ) {
73 MainWP_Utility::end_session();
74 }
75
76 try {
77
78 /**
79 * Filter: mainwp_clone_enabled
80 *
81 * Filters whether the Clone feature is enabled or disabled.
82 *
83 * @since Unknown
84 */
85 $cloneEnabled = apply_filters( 'mainwp_clone_enabled', false );
86 $cloneSites = array();
87
88 $disallowed_current_site = false;
89
90 if ( $cloneEnabled ) {
91 $disallowedCloneSites = get_option( 'mainwp_clone_disallowedsites' );
92 if ( false === $disallowedCloneSites ) {
93 $disallowedCloneSites = array();
94 }
95 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
96 if ( $websites ) {
97 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
98 if ( in_array( $website->id, $disallowedCloneSites ) ) {
99 continue;
100 }
101 if ( (int) $website->id === (int) $pWebsite->id ) {
102 continue;
103 }
104
105 $cloneSites[ $website->id ] = array(
106 'name' => $website->name,
107 'url' => $website->url,
108 'extauth' => $website->extauth,
109 'size' => $website->totalsize,
110 'connect_admin' => $website->adminname,
111 );
112 }
113 MainWP_DB::free_result( $websites );
114 }
115 $disallowed_current_site = in_array( $pWebsite->id, $disallowedCloneSites ) ? true : false;
116 }
117
118 $primaryBackup = MainWP_System_Utility::get_primary_backup();
119
120 $othersData = apply_filters_deprecated( 'mainwp-sync-others-data', array( array(), $pWebsite ), '4.0.7.2', 'mainwp_sync_others_data' ); // @deprecated Use 'mainwp_sync_others_data' instead. NOSONAR - not IP.
121
122 /**
123 * Filter: mainwp_sync_others_data
124 *
125 * Filters additional data in the sync request. Allows extensions or 3rd party plugins to hook data to the sync request.
126 *
127 * @param object $pWebsite Object contaning child site data.
128 *
129 * @since Unknown
130 *
131 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
132 */
133 $othersData = apply_filters( 'mainwp_sync_others_data', $othersData, $pWebsite );
134
135 $saved_days_number = apply_filters( 'mainwp_site_actions_saved_days_number', 30 );
136
137 $postdata = array(
138 'optimize' => 1 === (int) get_option( 'mainwp_optimize', 1 ) ? 1 : 0,
139 'cloneSites' => ( ! $cloneEnabled || $disallowed_current_site ) ? 0 : rawurlencode( wp_json_encode( $cloneSites ) ),
140 'othersData' => wp_json_encode( $othersData ),
141 'server' => get_admin_url(),
142 'numberdaysOutdatePluginTheme' => get_option( 'mainwp_numberdays_Outdate_Plugin_Theme', 365 ),
143 'primaryBackup' => $primaryBackup,
144 'siteId' => $pWebsite->id,
145 'child_actions_saved_days_number' => intval( $saved_days_number ),
146 'pingnonce' => MainWP_Utility::instance()->create_site_nonce( 'pingnonce', $pWebsite->id ),
147 );
148
149 $reg_verify = MainWP_DB::instance()->get_website_option( $pWebsite, 'register_verify_key', '' );
150 if ( empty( $reg_verify ) ) {
151 $postdata['sync_regverify'] = 1;
152 }
153
154 $information = MainWP_Connect::fetch_url_authed(
155 $pWebsite,
156 'stats',
157 $postdata,
158 true,
159 $pForceFetch
160 );
161
162 $return = static::sync_information_array( $pWebsite, $information, '', false, false, $pAllowDisconnect );
163 MainWP_Logger::instance()->log_execution_time( 'sync :: [siteid=' . $pWebsite->id . ']' );
164 return $return;
165 } catch ( MainWP_Exception $e ) {
166 $sync_errors = '';
167
168 if ( $e->getMessage() === 'HTTPERROR' ) {
169 $sync_errors = esc_html__( 'HTTP error', 'mainwp' ) . ( ! empty( $e->get_message_extra() ) ? ' - ' . $e->get_message_extra() : '' );
170 } elseif ( $e->getMessage() === 'NOMAINWP' ) {
171 $sync_errors = MainWP_Error_Helper::get_error_not_detected_connect();
172 }
173
174 MainWP_Logger::instance()->log_execution_time( 'sync :: [siteid=' . $pWebsite->id . ']' );
175 return static::sync_information_array( $pWebsite, $information, $sync_errors, false, true, $pAllowDisconnect );
176 }
177 }
178
179 /**
180 * Method sync_information_array()
181 *
182 * Grab all Child Site Information.
183 *
184 * @param object $pWebsite The website object.
185 * @param array $information Array contaning information returned from child site.
186 * @param string $sync_errors Check for Sync Errors.
187 * @param int $check_result Check if offline.
188 * @param bool $error True|False.
189 * @param bool $pAllowDisconnect True|False.
190 *
191 * @return bool true|false True on success, false on failure.
192 *
193 * @uses \MainWP\Dashboard\MainWP_DB::update_website_option()
194 * @uses \MainWP\Dashboard\MainWP_DB::update_website_sync_values()
195 * @uses \MainWP\Dashboard\MainWP_DB::update_website_values()
196 * @uses \MainWP\Dashboard\MainWP_Logger::warning_for_website()
197 * @uses \MainWP\Dashboard\MainWP_Monitoring_Handler::get_health_noticed_status_value()
198 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
199 * @uses \MainWP\Dashboard\MainWP_Utility::get_site_health()
200 */
201 public static function sync_information_array( &$pWebsite, &$information, $sync_errors = '', $check_result = false, $error = false, $pAllowDisconnect = true ) { // phpcs:ignore -- NOSONAR -Current complexity is the only way to achieve desired results, pull request solutions appreciated.
202 $emptyArray = wp_json_encode( array() );
203 $websiteValues = array(
204 'directories' => $emptyArray,
205 'plugin_upgrades' => $emptyArray,
206 'theme_upgrades' => $emptyArray,
207 'translation_upgrades' => $emptyArray,
208 'securityIssues' => $emptyArray,
209 'themes' => $emptyArray,
210 'plugins' => $emptyArray,
211 'users' => $emptyArray,
212 'categories' => $emptyArray,
213 );
214 $websiteSyncValues = array(
215 'sync_errors' => $sync_errors,
216 'version' => 0,
217 );
218
219 $_error = $sync_errors;
220
221 $done = false;
222
223 $current_siteid = 0;
224 if ( is_string( $pWebsite ) || is_int( $pWebsite ) ) {
225 $current_siteid = intval( $pWebsite );
226 } elseif ( is_object( $pWebsite ) && ( ! property_exists( $pWebsite, 'plugin_updates' ) || ! property_exists( $pWebsite, 'theme_updates' ) ) ) {
227 $current_siteid = $pWebsite->id;
228 }
229
230 // to get full data.
231 if ( $current_siteid ) {
232 $pWebsite = MainWP_DB::instance()->get_website_by_id( $current_siteid );
233 }
234
235 /**
236 * Filter: mainwp_before_save_sync_result
237 *
238 * Filters data returned from child site before saving to the database.
239 *
240 * @param object $pWebsite Object containing child site data.
241 *
242 * @since 3.4
243 */
244 $information = apply_filters( 'mainwp_before_save_sync_result', $information, $pWebsite );
245
246 if ( ! empty( $information['regverify_info'] ) ) {
247 MainWP_DB::instance()->update_website_option( $pWebsite, 'register_verify_key', $information['regverify_info'] );
248 $done = true;
249 }
250
251 if ( isset( $information['siteurl'] ) ) {
252 $websiteValues['siteurl'] = $information['siteurl'];
253 $done = true;
254 }
255
256 if ( isset( $information['version'] ) ) {
257 $websiteSyncValues['version'] = $information['version'];
258 $done = true;
259 }
260
261 $phpversion = '';
262 if ( isset( $information['site_info'] ) && ! empty( $information['site_info'] ) ) {
263 if ( is_array( $information['site_info'] ) && isset( $information['site_info']['phpversion'] ) ) {
264 $phpversion = $information['site_info']['phpversion'];
265 }
266 if ( is_array( $information['site_info'] ) && isset( $information['site_info']['ip'] ) ) {
267 $websiteValues['ip'] = sanitize_text_field( wp_unslash( $information['site_info']['ip'] ) );
268 }
269 MainWP_DB::instance()->update_website_option( $pWebsite, 'site_info', wp_json_encode( $information['site_info'] ) );
270 $done = true;
271 } else {
272 MainWP_DB::instance()->update_website_option( $pWebsite, 'site_info', $emptyArray );
273 }
274 if ( ! empty( $phpversion ) ) {
275 MainWP_DB::instance()->update_website_option( $pWebsite, 'phpversion', $phpversion );
276 }
277 if ( isset( $information['directories'] ) && is_array( $information['directories'] ) ) {
278 $websiteValues['directories'] = wp_json_encode( $information['directories'] );
279 $done = true;
280 } elseif ( isset( $information['directories'] ) ) {
281 $websiteValues['directories'] = $information['directories'];
282 $done = true;
283 }
284
285 if ( isset( $information['wp_updates'] ) && ! empty( $information['wp_updates'] ) ) {
286 MainWP_DB::instance()->update_website_option(
287 $pWebsite,
288 'wp_upgrades',
289 wp_json_encode(
290 array(
291 'current' => $information['wpversion'],
292 'new' => $information['wp_updates'],
293 )
294 )
295 );
296 $done = true;
297 } else {
298 MainWP_DB::instance()->update_website_option( $pWebsite, 'wp_upgrades', $emptyArray );
299 }
300
301 if ( isset( $information['plugin_updates'] ) ) {
302 $update_values = array();
303 if ( is_array( $information['plugin_updates'] ) ) {
304 foreach ( $information['plugin_updates'] as $file => $update ) {
305 $update_values[ $file ] = $update;
306 }
307 }
308 $websiteValues['plugin_upgrades'] = wp_json_encode( $update_values );
309 $done = true;
310 }
311
312 if ( isset( $information['theme_updates'] ) ) {
313 $update_values = array();
314 if ( is_array( $information['theme_updates'] ) ) {
315 foreach ( $information['theme_updates'] as $file => $update ) {
316 $update_values[ $file ] = $update;
317 }
318 }
319 $websiteValues['theme_upgrades'] = wp_json_encode( $update_values );
320 $done = true;
321 }
322
323 if ( isset( $information['translation_updates'] ) ) {
324 $websiteValues['translation_upgrades'] = wp_json_encode( $information['translation_updates'] );
325 $done = true;
326 }
327
328 if ( isset( $information['premium_updates'] ) ) {
329 MainWP_DB::instance()->update_website_option( $pWebsite, 'premium_upgrades', wp_json_encode( $information['premium_updates'] ) );
330 $done = true;
331 } else {
332 MainWP_DB::instance()->update_website_option( $pWebsite, 'premium_upgrades', $emptyArray );
333 }
334
335 if ( isset( $information['securityStats'] ) ) {
336 $total_securityIssues = 0;
337 $securityStats = $information['securityStats'];
338 if ( is_array( $securityStats ) ) {
339 /** This filter is documented in ../pages/page-mainwp-security-issues.php */
340 $filterStats = apply_filters( 'mainwp_security_issues_stats', false, $securityStats, $pWebsite );
341 if ( false !== $filterStats && is_array( $filterStats ) ) {
342 $securityStats = array_merge( $securityStats, $filterStats );
343 }
344
345 $unset_scripts = apply_filters( 'mainwp_unset_security_scripts_stylesheets', true );
346 if ( $unset_scripts ) {
347 if ( isset( $securityStats['versions'] ) ) {
348 unset( $securityStats['versions'] );
349 }
350 if ( isset( $securityStats['registered_versions'] ) ) {
351 unset( $securityStats['registered_versions'] );
352 }
353 }
354
355 $tmp_issues = array_filter(
356 $securityStats,
357 function ( $v ) {
358 return 'N' === $v;
359 },
360 ARRAY_FILTER_USE_BOTH
361 );
362 $total_securityIssues = count( $tmp_issues );
363 $securityStats = wp_json_encode( $securityStats );
364 } else {
365 $securityStats = $emptyArray;
366 }
367 $websiteValues['securityIssues'] = $total_securityIssues;
368 MainWP_DB::instance()->update_website_option( $pWebsite, 'security_stats', $securityStats );
369 $done = true;
370 } elseif ( isset( $information['securityIssues'] ) && MainWP_Utility::ctype_digit( $information['securityIssues'] ) && $information['securityIssues'] >= 0 ) {
371 $websiteValues['securityIssues'] = $information['securityIssues'];
372 $done = true;
373 }
374
375 if ( isset( $information['recent_comments'] ) ) {
376 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_comments', wp_json_encode( $information['recent_comments'] ) );
377 $done = true;
378 } else {
379 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_comments', $emptyArray );
380 }
381
382 if ( isset( $information['recent_posts'] ) ) {
383 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_posts', wp_json_encode( $information['recent_posts'] ) );
384 $done = true;
385 } else {
386 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_posts', $emptyArray );
387 }
388
389 if ( isset( $information['recent_pages'] ) ) {
390 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_pages', wp_json_encode( $information['recent_pages'] ) );
391 $done = true;
392 } else {
393 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_pages', $emptyArray );
394 }
395
396 if ( isset( $information['themes'] ) ) {
397 $websiteValues['themes'] = wp_json_encode( $information['themes'] );
398 $done = true;
399 }
400
401 if ( isset( $information['plugins'] ) ) {
402 $websiteValues['plugins'] = wp_json_encode( $information['plugins'] );
403 $done = true;
404 }
405
406 if ( isset( $information['users'] ) ) {
407 $websiteValues['users'] = wp_json_encode( $information['users'] );
408 $done = true;
409 }
410
411 if ( isset( $information['categories_list'] ) ) {
412 $websiteValues['categories'] = wp_json_encode( $information['categories_list'] );
413 $done = true;
414 } elseif ( isset( $information['categories'] ) ) { // support old child version.
415 $websiteValues['categories'] = wp_json_encode( $information['categories'] );
416 $done = true;
417 }
418
419 if ( isset( $information['totalsize'] ) ) {
420 $websiteSyncValues['totalsize'] = $information['totalsize'];
421 $done = true;
422 }
423
424 if ( isset( $information['dbsize'] ) ) {
425 $websiteSyncValues['dbsize'] = $information['dbsize'];
426 $done = true;
427 }
428
429 if ( isset( $information['extauth'] ) ) {
430 $websiteSyncValues['extauth'] = $information['extauth'];
431 $done = true;
432 }
433
434 if ( isset( $information['wpe'] ) ) {
435 $websiteValues['wpe'] = $information['wpe'];
436 $done = true;
437 }
438
439 if ( isset( $information['wphost'] ) ) {
440 MainWP_DB::instance()->update_website_option( $pWebsite, 'wphost', $information['wphost'] );
441 }
442
443 if ( isset( $information['last_post_gmt'] ) ) {
444 $websiteSyncValues['last_post_gmt'] = $information['last_post_gmt'];
445 $done = true;
446 }
447
448 if ( isset( $information['health_site_status'] ) ) {
449 $health_status = $information['health_site_status'];
450 $hstatus = MainWP_Utility::get_site_health( $health_status );
451 $custom_health_value = $hstatus['val'] - $hstatus['critical'] * 100; // computes custom health value to support sorting by sites health and sites health threshold.
452 $websiteSyncValues['health_value'] = $custom_health_value;
453 $done = true;
454 MainWP_DB::instance()->update_website_option( $pWebsite, 'health_site_status', wp_json_encode( $health_status ) );
455 $new_noticed = MainWP_Monitoring_Handler::get_health_noticed_status_value( $pWebsite, $custom_health_value );
456 if ( null !== $new_noticed ) {
457 MainWP_DB::instance()->update_website_sync_values(
458 $pWebsite->id,
459 array(
460 'health_site_noticed' => $new_noticed,
461 )
462 );
463 }
464
465 $hval = $hstatus['val'];
466 $critical = $hstatus['critical'];
467 $health_status = 0;
468 if ( 80 <= $hval && empty( $critical ) ) {
469 $health_status = 0; // Good.
470 } else {
471 $health_status = 1; // Should be improved'.
472 }
473 $websiteSyncValues['health_status'] = $health_status;
474 } else {
475 MainWP_DB::instance()->update_website_option( $pWebsite, 'health_site_status', $emptyArray );
476 }
477
478 if ( isset( $information['mainwpdir'] ) ) {
479 $websiteValues['mainwpdir'] = $information['mainwpdir'];
480 $done = true;
481 }
482
483 if ( isset( $information['uniqueId'] ) ) {
484 $websiteValues['uniqueId'] = $information['uniqueId'];
485 $done = true;
486 }
487
488 if ( isset( $information['clone_adminname'] ) ) {
489 $websiteValues['adminname'] = $information['clone_adminname'];
490 $done = true;
491 }
492
493 if ( isset( $information['admin_nicename'] ) ) {
494 MainWP_DB::instance()->update_website_option( $pWebsite, 'admin_nicename', trim( $information['admin_nicename'] ) );
495 $done = true;
496 }
497
498 if ( isset( $information['admin_useremail'] ) ) {
499 MainWP_DB::instance()->update_website_option( $pWebsite, 'admin_useremail', trim( $information['admin_useremail'] ) );
500 $done = true;
501 }
502
503 if ( isset( $information['plugins_outdate_info'] ) ) {
504 MainWP_DB::instance()->update_website_option( $pWebsite, 'plugins_outdate_info', wp_json_encode( $information['plugins_outdate_info'] ) );
505 $done = true;
506 } else {
507 MainWP_DB::instance()->update_website_option( $pWebsite, 'plugins_outdate_info', $emptyArray );
508 }
509
510 if ( isset( $information['themes_outdate_info'] ) ) {
511 MainWP_DB::instance()->update_website_option( $pWebsite, 'themes_outdate_info', wp_json_encode( $information['themes_outdate_info'] ) );
512 $done = true;
513 } else {
514 MainWP_DB::instance()->update_website_option( $pWebsite, 'themes_outdate_info', $emptyArray );
515 }
516
517 if ( isset( $information['primaryLasttimeBackup'] ) ) {
518 MainWP_DB::instance()->update_website_option( $pWebsite, 'primary_lasttime_backup', $information['primaryLasttimeBackup'] );
519 $done = true;
520 }
521
522 if ( isset( $information['child_site_actions_data'] ) ) {
523 if ( is_array( $information['child_site_actions_data'] ) && isset( $information['child_site_actions_data']['connected_admin'] ) ) {
524 unset( $information['child_site_actions_data']['connected_admin'] );
525 }
526 MainWP_DB_Site_Actions::instance()->sync_site_actions( $pWebsite->id, $information['child_site_actions_data'] );
527 $done = true;
528 }
529
530 if ( ! $done ) {
531 if ( isset( $information['wpversion'] ) ) {
532 $done = true;
533 } elseif ( isset( $information['error'] ) ) {
534 MainWP_Logger::instance()->warning_for_website( $pWebsite, 'SYNC ERROR', '[' . esc_html( $information['error'] ) . ']' );
535 $error = true;
536 $done = true;
537 $_error = esc_html__( 'ERROR: ', 'mainwp' ) . esc_html( $information['error'] );
538 $websiteSyncValues['sync_errors'] = $_error;
539 } elseif ( ! empty( $sync_errors ) ) {
540 MainWP_Logger::instance()->warning_for_website( $pWebsite, 'SYNC ERROR', '[' . $sync_errors . ']' );
541 $_error = $sync_errors;
542 $error = true;
543 if ( ! $pAllowDisconnect ) {
544 $sync_errors = '';
545 }
546
547 $websiteSyncValues['sync_errors'] = $sync_errors;
548 } else {
549 MainWP_Logger::instance()->warning_for_website( $pWebsite, 'SYNC ERROR', '[Undefined error]' );
550 $error = true;
551 if ( $pAllowDisconnect ) {
552 $sync_errors = esc_html__( 'Undefined error! Please, reinstall the MainWP Child plugin on the child site.', 'mainwp' );
553 $websiteSyncValues['sync_errors'] = $sync_errors;
554 $_error = $sync_errors;
555 }
556 }
557 }
558
559 $act_success = false;
560 if ( $done ) {
561 $act_success = true;
562 $websiteSyncValues['dtsSync'] = time();
563 }
564 MainWP_DB::instance()->update_website_sync_values( $pWebsite->id, $websiteSyncValues );
565 MainWP_DB::instance()->update_website_values( $pWebsite->id, $websiteValues );
566
567 // Sync action.
568 if ( ! $error ) {
569 do_action_deprecated( 'mainwp-site-synced', array( $pWebsite, $information ), '4.0.7.2', 'mainwp_site_synced' ); // @deprecated Use 'mainwp_site_synced' instead. NOSONAR - not IP.
570
571 /**
572 * Action: mainwp_site_synced
573 *
574 * Fires upon successful site synchronization.
575 *
576 * @param object $pWebsite Object containing child site info.
577 * @param array $information Array containing information returned from child site.
578 *
579 * @since 3.4
580 */
581 do_action( 'mainwp_site_synced', $pWebsite, $information );
582 }
583
584 $post_data = array();
585
586 /**
587 * Action: mainwp_site_sync
588 *
589 * Fires upon successful site synchronization.
590 *
591 * @param object $pWebsite Object containing child site info.
592 * @param array $information Array containing information returned from child site.
593 * @param bool $act_success action success or failed.
594 * @param string $_error Sync error message if existed.
595 * @param array $post_data Addition post data.
596 *
597 * @since 3.4
598 */
599 do_action( 'mainwp_site_sync', $pWebsite, $information, $act_success, $_error, $post_data );
600
601 return ! $error;
602 }
603
604 /**
605 * Method get_wp_icon()
606 *
607 * Get site's icon.
608 *
609 * @param mixed $siteId site's id.
610 *
611 * @return array result error or success
612 * @throws \MainWP_Exception Error message.
613 *
614 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
615 * @uses \MainWP\Dashboard\MainWP_Connect::get_file_content()
616 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
617 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_website_option()
618 * @uses \MainWP\Dashboard\MainWP_Exception
619 * @uses \MainWP\Dashboard\MainWP_Logger::debug()
620 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
621 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
622 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_dir()
623 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
624 */
625 public static function get_wp_icon( $siteId = null ) { // phpcs:ignore -- NOSONAR - complex.
626 if ( MainWP_Utility::ctype_digit( $siteId ) ) {
627 $website = MainWP_DB::instance()->get_website_by_id( $siteId );
628 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
629 $error = '';
630 try {
631 $information = MainWP_Connect::fetch_url_authed( $website, 'get_site_icon' );
632 } catch ( MainWP_Exception $e ) {
633 $error = $e->getMessage();
634 }
635
636 if ( ! empty( $error ) ) {
637 return array( 'error' => $error );
638 } elseif ( isset( $information['faviIconUrl'] ) && ! empty( $information['faviIconUrl'] ) ) {
639 MainWP_Logger::instance()->debug( 'Downloading icon :: ' . esc_html( $information['faviIconUrl'] ) );
640 $content = MainWP_Connect::get_file_content( $information['faviIconUrl'] );
641 if ( ! empty( $content ) ) {
642
643 MainWP_System_Utility::get_wp_file_system();
644
645 /**
646 * WordPress files system object.
647 *
648 * @global object
649 */
650 global $wp_filesystem;
651
652 $dirs = MainWP_System_Utility::get_mainwp_dir( 'icons', true );
653 $iconsDir = $dirs[0];
654 $filename = basename( $information['faviIconUrl'] );
655 $filename = strtok( $filename, '?' );
656 if ( $filename ) {
657 $filename = 'favi-' . $siteId . '-' . $filename;
658 $size = false;
659 if ( MainWP_Utility::check_image_file_name( $filename ) ) {
660 $size = $wp_filesystem->put_contents( $iconsDir . $filename, $content ); // phpcs:ignore --
661 }
662 if ( $size ) {
663 MainWP_Logger::instance()->debug( 'Icon size :: ' . $size );
664 MainWP_DB::instance()->update_website_option( $website, 'favi_icon', $filename );
665 return array( 'result' => 'success' );
666 } else {
667 return array( 'error' => 'Save icon file failed.' );
668 }
669 }
670 return array( 'undefined_error' => true );
671 } else {
672 return array( 'error' => esc_html__( 'Download icon file failed', 'mainwp' ) );
673 }
674 } else {
675 return array( 'undefined_error' => true );
676 }
677 }
678 }
679 return array( 'result' => 'NOSITE' );
680 }
681 }
682