PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.6.0.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.6.0.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 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 4.6.0.1, at class/class-mainwp-sync.php

671 lines 24.8 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 {
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 self::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 -- 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.
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', 0 ) ? 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 $information = MainWP_Connect::fetch_url_authed(
150 $pWebsite,
151 'stats',
152 $postdata,
153 true,
154 $pForceFetch
155 );
156 $return = self::sync_information_array( $pWebsite, $information, '', 1, false, $pAllowDisconnect );
157 MainWP_Logger::instance()->log_execution_time( 'sync :: [siteid=' . $pWebsite->id . ']' );
158 return $return;
159 } catch ( MainWP_Exception $e ) {
160 $sync_errors = '';
161 $check_result = 1;
162
163 if ( $e->getMessage() === 'HTTPERROR' ) {
164 $sync_errors = esc_html__( 'HTTP error', 'mainwp' ) . ( ! empty( $e->get_message_extra() ) ? ' - ' . $e->get_message_extra() : '' );
165 $check_result = - 1;
166 } elseif ( $e->getMessage() === 'NOMAINWP' ) {
167 $sync_errors = sprintf( esc_html__( 'MainWP Child plugin not detected or could not be reached! Ensure the MainWP Child plugin is installed and activated on the child site, and there are no security rules blocking requests. If you continue experiencing this issue, check the %1$sMainWP Community%2$s for help.', 'mainwp' ), '<a href="https://managers.mainwp.com/c/community-support/5" target="_blank">', '</a>' );
168 $check_result = 1;
169 }
170
171 MainWP_Logger::instance()->log_execution_time( 'sync :: [siteid=' . $pWebsite->id . ']' );
172 return self::sync_information_array( $pWebsite, $information, $sync_errors, $check_result, true, $pAllowDisconnect );
173 }
174 }
175
176 /**
177 * Method sync_information_array()
178 *
179 * Grab all Child Site Information.
180 *
181 * @param object $pWebsite The website object.
182 * @param array $information Array contaning information returned from child site.
183 * @param string $sync_errors Check for Sync Errors.
184 * @param int $check_result Check if offline.
185 * @param bool $error True|False.
186 * @param bool $pAllowDisconnect True|False.
187 *
188 * @return bool true|false True on success, false on failure.
189 *
190 * @uses \MainWP\Dashboard\MainWP_DB::update_website_option()
191 * @uses \MainWP\Dashboard\MainWP_DB::update_website_sync_values()
192 * @uses \MainWP\Dashboard\MainWP_DB::update_website_values()
193 * @uses \MainWP\Dashboard\MainWP_Logger::warning_for_website()
194 * @uses \MainWP\Dashboard\MainWP_Monitoring_Handler::get_health_noticed_status_value()
195 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
196 * @uses \MainWP\Dashboard\MainWP_Utility::get_site_health()
197 */
198 public static function sync_information_array( &$pWebsite, &$information, $sync_errors = '', $check_result = 1, $error = false, $pAllowDisconnect = true ) { // phpcs:ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
199 $emptyArray = wp_json_encode( array() );
200 $websiteValues = array(
201 'directories' => $emptyArray,
202 'plugin_upgrades' => $emptyArray,
203 'theme_upgrades' => $emptyArray,
204 'translation_upgrades' => $emptyArray,
205 'securityIssues' => $emptyArray,
206 'themes' => $emptyArray,
207 'plugins' => $emptyArray,
208 'users' => $emptyArray,
209 'categories' => $emptyArray,
210 'offline_check_result' => $check_result,
211 );
212 $websiteSyncValues = array(
213 'sync_errors' => $sync_errors,
214 'version' => 0,
215 );
216
217 $_error = $sync_errors;
218
219 $done = false;
220
221 $current_siteid = 0;
222 if ( is_string( $pWebsite ) || is_int( $pWebsite ) ) {
223 $current_siteid = intval( $pWebsite );
224 } elseif ( is_object( $pWebsite ) && ( ! property_exists( $pWebsite, 'plugin_updates' ) || ! property_exists( $pWebsite, 'theme_updates' ) ) ) {
225 $current_siteid = $pWebsite->id;
226 }
227
228 // to get full data.
229 if ( $current_siteid ) {
230 $pWebsite = MainWP_DB::instance()->get_website_by_id( $current_siteid );
231 }
232
233 /**
234 * Filter: mainwp_before_save_sync_result
235 *
236 * Filters data returned from child site before saving to the database.
237 *
238 * @param object $pWebsite Object containing child site data.
239 *
240 * @since 3.4
241 */
242 $information = apply_filters( 'mainwp_before_save_sync_result', $information, $pWebsite );
243
244 if ( isset( $information['siteurl'] ) ) {
245 $websiteValues['siteurl'] = $information['siteurl'];
246 $done = true;
247 }
248
249 if ( isset( $information['version'] ) ) {
250 $websiteSyncValues['version'] = $information['version'];
251 $done = true;
252 }
253
254 $phpversion = '';
255 if ( isset( $information['site_info'] ) && ! empty( $information['site_info'] ) ) {
256 if ( is_array( $information['site_info'] ) && isset( $information['site_info']['phpversion'] ) ) {
257 $phpversion = $information['site_info']['phpversion'];
258 }
259 if ( is_array( $information['site_info'] ) && isset( $information['site_info']['ip'] ) ) {
260 $websiteValues['ip'] = sanitize_text_field( wp_unslash( $information['site_info']['ip'] ) );
261 }
262 MainWP_DB::instance()->update_website_option( $pWebsite, 'site_info', wp_json_encode( $information['site_info'] ) );
263 $done = true;
264 } else {
265 MainWP_DB::instance()->update_website_option( $pWebsite, 'site_info', $emptyArray );
266 }
267 MainWP_DB::instance()->update_website_option( $pWebsite, 'phpversion', $phpversion );
268
269 if ( isset( $information['directories'] ) && is_array( $information['directories'] ) ) {
270 $websiteValues['directories'] = wp_json_encode( $information['directories'] );
271 $done = true;
272 } elseif ( isset( $information['directories'] ) ) {
273 $websiteValues['directories'] = $information['directories'];
274 $done = true;
275 }
276
277 if ( isset( $information['wp_updates'] ) && ! empty( $information['wp_updates'] ) ) {
278 MainWP_DB::instance()->update_website_option(
279 $pWebsite,
280 'wp_upgrades',
281 wp_json_encode(
282 array(
283 'current' => $information['wpversion'],
284 'new' => $information['wp_updates'],
285 )
286 )
287 );
288 $done = true;
289 } else {
290 MainWP_DB::instance()->update_website_option( $pWebsite, 'wp_upgrades', $emptyArray );
291 }
292
293 if ( isset( $information['plugin_updates'] ) ) {
294 $update_values = array();
295 if ( is_array( $information['plugin_updates'] ) ) {
296 foreach ( $information['plugin_updates'] as $file => $update ) {
297 $update_values[ $file ] = $update;
298 }
299 }
300 $websiteValues['plugin_upgrades'] = wp_json_encode( $update_values );
301 $done = true;
302 }
303
304 if ( isset( $information['theme_updates'] ) ) {
305 $update_values = array();
306 if ( is_array( $information['theme_updates'] ) ) {
307 foreach ( $information['theme_updates'] as $file => $update ) {
308 $update_values[ $file ] = $update;
309 }
310 }
311 $websiteValues['theme_upgrades'] = wp_json_encode( $update_values );
312 $done = true;
313 }
314
315 if ( isset( $information['translation_updates'] ) ) {
316 $websiteValues['translation_upgrades'] = wp_json_encode( $information['translation_updates'] );
317 $done = true;
318 }
319
320 if ( isset( $information['premium_updates'] ) ) {
321 MainWP_DB::instance()->update_website_option( $pWebsite, 'premium_upgrades', wp_json_encode( $information['premium_updates'] ) );
322 $done = true;
323 } else {
324 MainWP_DB::instance()->update_website_option( $pWebsite, 'premium_upgrades', $emptyArray );
325 }
326
327 if ( isset( $information['securityStats'] ) ) {
328 $total_securityIssues = 0;
329 $securityStats = $information['securityStats'];
330 if ( is_array( $securityStats ) ) {
331 /** This filter is documented in ../pages/page-mainwp-security-issues.php */
332 $filterStats = apply_filters( 'mainwp_security_issues_stats', false, $securityStats, $pWebsite );
333 if ( false !== $filterStats && is_array( $filterStats ) ) {
334 $securityStats = array_merge( $securityStats, $filterStats );
335 }
336
337 $unset_scripts = apply_filters( 'mainwp_unset_security_scripts_stylesheets', true );
338 if ( $unset_scripts ) {
339 if ( isset( $securityStats['versions'] ) ) {
340 unset( $securityStats['versions'] );
341 }
342 if ( isset( $securityStats['registered_versions'] ) ) {
343 unset( $securityStats['registered_versions'] );
344 }
345 }
346
347 $tmp_issues = array_filter(
348 $securityStats,
349 function ( $v ) {
350 return 'N' === $v;
351 },
352 ARRAY_FILTER_USE_BOTH
353 );
354 $total_securityIssues = count( $tmp_issues );
355 $securityStats = wp_json_encode( $securityStats );
356 } else {
357 $securityStats = $emptyArray;
358 }
359 $websiteValues['securityIssues'] = $total_securityIssues;
360 MainWP_DB::instance()->update_website_option( $pWebsite, 'security_stats', $securityStats );
361 $done = true;
362 } elseif ( isset( $information['securityIssues'] ) && MainWP_Utility::ctype_digit( $information['securityIssues'] ) && $information['securityIssues'] >= 0 ) {
363 $websiteValues['securityIssues'] = $information['securityIssues'];
364 $done = true;
365 }
366
367 if ( isset( $information['recent_comments'] ) ) {
368 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_comments', wp_json_encode( $information['recent_comments'] ) );
369 $done = true;
370 } else {
371 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_comments', $emptyArray );
372 }
373
374 if ( isset( $information['recent_posts'] ) ) {
375 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_posts', wp_json_encode( $information['recent_posts'] ) );
376 $done = true;
377 } else {
378 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_posts', $emptyArray );
379 }
380
381 if ( isset( $information['recent_pages'] ) ) {
382 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_pages', wp_json_encode( $information['recent_pages'] ) );
383 $done = true;
384 } else {
385 MainWP_DB::instance()->update_website_option( $pWebsite, 'recent_pages', $emptyArray );
386 }
387
388 if ( isset( $information['themes'] ) ) {
389 $websiteValues['themes'] = wp_json_encode( $information['themes'] );
390 $done = true;
391 }
392
393 if ( isset( $information['plugins'] ) ) {
394 $websiteValues['plugins'] = wp_json_encode( $information['plugins'] );
395 $done = true;
396 }
397
398 if ( isset( $information['users'] ) ) {
399 $websiteValues['users'] = wp_json_encode( $information['users'] );
400 $done = true;
401 }
402
403 if ( isset( $information['categories'] ) ) {
404 $websiteValues['categories'] = wp_json_encode( $information['categories'] );
405 $done = true;
406 }
407
408 if ( isset( $information['totalsize'] ) ) {
409 $websiteSyncValues['totalsize'] = $information['totalsize'];
410 $done = true;
411 }
412
413 if ( isset( $information['dbsize'] ) ) {
414 $websiteSyncValues['dbsize'] = $information['dbsize'];
415 $done = true;
416 }
417
418 if ( isset( $information['extauth'] ) ) {
419 $websiteSyncValues['extauth'] = $information['extauth'];
420 $done = true;
421 }
422
423 if ( isset( $information['wpe'] ) ) {
424 $websiteValues['wpe'] = $information['wpe'];
425 $done = true;
426 }
427
428 if ( isset( $information['wphost'] ) ) {
429 MainWP_DB::instance()->update_website_option( $pWebsite, 'wphost', $information['wphost'] );
430 }
431
432 if ( isset( $information['last_post_gmt'] ) ) {
433 $websiteSyncValues['last_post_gmt'] = $information['last_post_gmt'];
434 $done = true;
435 }
436
437 if ( isset( $information['health_site_status'] ) ) {
438 $health_status = $information['health_site_status'];
439 $hstatus = MainWP_Utility::get_site_health( $health_status );
440 $custom_health_value = $hstatus['val'] - $hstatus['critical'] * 100; // computes custom health value to support sorting by sites health and sites health threshold.
441 $websiteSyncValues['health_value'] = $custom_health_value;
442 $done = true;
443 MainWP_DB::instance()->update_website_option( $pWebsite, 'health_site_status', wp_json_encode( $health_status ) );
444 $new_noticed = MainWP_Monitoring_Handler::get_health_noticed_status_value( $pWebsite, $custom_health_value );
445 if ( null !== $new_noticed ) {
446 MainWP_DB::instance()->update_website_sync_values(
447 $pWebsite->id,
448 array(
449 'health_site_noticed' => $new_noticed,
450 )
451 );
452 }
453
454 $hval = $hstatus['val'];
455 $critical = $hstatus['critical'];
456 $health_status = 0;
457 if ( 80 <= $hval && empty( $critical ) ) {
458 $health_status = 0; // Good.
459 } else {
460 $health_status = 1; // Should be improved'.
461 }
462 $websiteSyncValues['health_status'] = $health_status;
463 } else {
464 MainWP_DB::instance()->update_website_option( $pWebsite, 'health_site_status', $emptyArray );
465 }
466
467 if ( isset( $information['mainwpdir'] ) ) {
468 $websiteValues['mainwpdir'] = $information['mainwpdir'];
469 $done = true;
470 }
471
472 if ( isset( $information['uniqueId'] ) ) {
473 $websiteValues['uniqueId'] = $information['uniqueId'];
474 $done = true;
475 }
476
477 if ( isset( $information['clone_adminname'] ) ) {
478 $websiteValues['adminname'] = $information['clone_adminname'];
479 $done = true;
480 }
481
482 if ( isset( $information['admin_nicename'] ) ) {
483 MainWP_DB::instance()->update_website_option( $pWebsite, 'admin_nicename', trim( $information['admin_nicename'] ) );
484 $done = true;
485 }
486
487 if ( isset( $information['admin_useremail'] ) ) {
488 MainWP_DB::instance()->update_website_option( $pWebsite, 'admin_useremail', trim( $information['admin_useremail'] ) );
489 $done = true;
490 }
491
492 if ( isset( $information['plugins_outdate_info'] ) ) {
493 MainWP_DB::instance()->update_website_option( $pWebsite, 'plugins_outdate_info', wp_json_encode( $information['plugins_outdate_info'] ) );
494 $done = true;
495 } else {
496 MainWP_DB::instance()->update_website_option( $pWebsite, 'plugins_outdate_info', $emptyArray );
497 }
498
499 if ( isset( $information['themes_outdate_info'] ) ) {
500 MainWP_DB::instance()->update_website_option( $pWebsite, 'themes_outdate_info', wp_json_encode( $information['themes_outdate_info'] ) );
501 $done = true;
502 } else {
503 MainWP_DB::instance()->update_website_option( $pWebsite, 'themes_outdate_info', $emptyArray );
504 }
505
506 if ( isset( $information['primaryLasttimeBackup'] ) ) {
507 MainWP_DB::instance()->update_website_option( $pWebsite, 'primary_lasttime_backup', $information['primaryLasttimeBackup'] );
508 $done = true;
509 }
510
511 if ( isset( $information['child_site_actions_data'] ) ) {
512 if ( is_array( $information['child_site_actions_data'] ) && isset( $information['child_site_actions_data']['connected_admin'] ) ) {
513 unset( $information['child_site_actions_data']['connected_admin'] );
514 }
515 MainWP_DB_Site_Actions::instance()->sync_site_actions( $pWebsite->id, $information['child_site_actions_data'] );
516 $done = true;
517 }
518
519 if ( ! $done ) {
520 if ( isset( $information['wpversion'] ) ) {
521 $done = true;
522 } elseif ( isset( $information['error'] ) ) {
523 MainWP_Logger::instance()->warning_for_website( $pWebsite, 'SYNC ERROR', '[' . esc_html( $information['error'] ) . ']' );
524 $error = true;
525 $done = true;
526 $_error = esc_html__( 'ERROR: ', 'mainwp' ) . esc_html( $information['error'] );
527 $websiteSyncValues['sync_errors'] = $_error;
528 } elseif ( ! empty( $sync_errors ) ) {
529 MainWP_Logger::instance()->warning_for_website( $pWebsite, 'SYNC ERROR', '[' . $sync_errors . ']' );
530 $_error = $sync_errors;
531 $error = true;
532 if ( ! $pAllowDisconnect ) {
533 $sync_errors = '';
534 }
535
536 $websiteSyncValues['sync_errors'] = $sync_errors;
537 } else {
538 MainWP_Logger::instance()->warning_for_website( $pWebsite, 'SYNC ERROR', '[Undefined error]' );
539 $error = true;
540 if ( $pAllowDisconnect ) {
541 $sync_errors = esc_html__( 'Undefined error! Please, reinstall the MainWP Child plugin on the child site.', 'mainwp' );
542 $websiteSyncValues['sync_errors'] = $sync_errors;
543 $_error = $sync_errors;
544 }
545 }
546 }
547
548 $act_success = false;
549 if ( $done ) {
550 $act_success = true;
551 $websiteSyncValues['dtsSync'] = time();
552 }
553 MainWP_DB::instance()->update_website_sync_values( $pWebsite->id, $websiteSyncValues );
554 MainWP_DB::instance()->update_website_values( $pWebsite->id, $websiteValues );
555
556 // Sync action.
557 if ( ! $error ) {
558 do_action_deprecated( 'mainwp-site-synced', array( $pWebsite, $information ), '4.0.7.2', 'mainwp_site_synced' ); // @deprecated Use 'mainwp_site_synced' instead.
559
560 /**
561 * Action: mainwp_site_synced
562 *
563 * Fires upon successful site synchronization.
564 *
565 * @param object $pWebsite Object containing child site info.
566 * @param array $information Array containing information returned from child site.
567 *
568 * @since 3.4
569 */
570 do_action( 'mainwp_site_synced', $pWebsite, $information );
571 }
572
573 $post_data = array();
574
575 /**
576 * Action: mainwp_site_sync
577 *
578 * Fires upon successful site synchronization.
579 *
580 * @param object $pWebsite Object containing child site info.
581 * @param array $information Array containing information returned from child site.
582 * @param bool $act_success action success or failed.
583 * @param string $_error Sync error message if existed.
584 * @param array $post_data Addition post data.
585 *
586 * @since 3.4
587 */
588 do_action( 'mainwp_site_sync', $pWebsite, $information, $act_success, $_error, $post_data );
589
590 return ( ! $error );
591 }
592
593 /**
594 * Method get_wp_icon()
595 *
596 * Get site's icon.
597 *
598 * @param mixed $siteId site's id.
599 *
600 * @return array result error or success
601 * @throws \Exception Error message.
602 *
603 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
604 * @uses \MainWP\Dashboard\MainWP_Connect::get_file_content()
605 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
606 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_website_option()
607 * @uses \MainWP\Dashboard\MainWP_Exception
608 * @uses \MainWP\Dashboard\MainWP_Logger::debug()
609 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
610 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
611 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_dir()
612 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
613 */
614 public static function get_wp_icon( $siteId = null ) {
615 if ( MainWP_Utility::ctype_digit( $siteId ) ) {
616 $website = MainWP_DB::instance()->get_website_by_id( $siteId );
617 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
618 $error = '';
619 try {
620 $information = MainWP_Connect::fetch_url_authed( $website, 'get_site_icon' );
621 } catch ( MainWP_Exception $e ) {
622 $error = $e->getMessage();
623 }
624
625 if ( ! empty( $error ) ) {
626 return array( 'error' => $error );
627 } elseif ( isset( $information['faviIconUrl'] ) && ! empty( $information['faviIconUrl'] ) ) {
628 MainWP_Logger::instance()->debug( 'Downloading icon :: ' . esc_html( $information['faviIconUrl'] ) );
629 $content = MainWP_Connect::get_file_content( $information['faviIconUrl'] );
630 if ( ! empty( $content ) ) {
631
632 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
633
634 /**
635 * WordPress files system object.
636 *
637 * @global object
638 */
639 global $wp_filesystem;
640
641 $dirs = MainWP_System_Utility::get_mainwp_dir( 'icons', true );
642 $iconsDir = $dirs[0];
643 $filename = basename( $information['faviIconUrl'] );
644 $filename = strtok( $filename, '?' );
645 if ( $filename ) {
646 $filename = 'favi-' . $siteId . '-' . $filename;
647 $size = false;
648 if ( MainWP_Utility::check_image_file_name( $filename ) ) {
649 $size = $wp_filesystem->put_contents( $iconsDir . $filename, $content ); // phpcs:ignore --
650 }
651 if ( $size ) {
652 MainWP_Logger::instance()->debug( 'Icon size :: ' . $size );
653 MainWP_DB::instance()->update_website_option( $website, 'favi_icon', $filename );
654 return array( 'result' => 'success' );
655 } else {
656 return array( 'error' => 'Save icon file failed.' );
657 }
658 }
659 return array( 'undefined_error' => true );
660 } else {
661 return array( 'error' => esc_html__( 'Download icon file failed', 'mainwp' ) );
662 }
663 } else {
664 return array( 'undefined_error' => true );
665 }
666 }
667 }
668 return array( 'result' => 'NOSITE' );
669 }
670 }
671