PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.4.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.4.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.4.1, at class/class-mainwp-sync.php

655 lines 24.5 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 if ( null == $pWebsite ) {
59 return false;
60 }
61 $userExtension = MainWP_DB_Common::instance()->get_user_extension_by_user_id( $pWebsite->userid );
62 if ( null == $userExtension ) {
63 return false;
64 }
65
66 if ( $clear_session ) {
67 MainWP_Utility::end_session();
68 }
69
70 try {
71
72 /**
73 * Filter: mainwp_clone_enabled
74 *
75 * Filters whether the Clone feature is enabled or disabled.
76 *
77 * @since Unknown
78 */
79 $cloneEnabled = apply_filters( 'mainwp_clone_enabled', false );
80 $cloneSites = array();
81 if ( $cloneEnabled ) {
82 $disallowedCloneSites = get_option( 'mainwp_clone_disallowedsites' );
83 if ( false === $disallowedCloneSites ) {
84 $disallowedCloneSites = array();
85 }
86 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
87 if ( $websites ) {
88 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
89 if ( in_array( $website->id, $disallowedCloneSites ) ) {
90 continue;
91 }
92 if ( $website->id == $pWebsite->id ) {
93 continue;
94 }
95
96 $cloneSites[ $website->id ] = array(
97 'name' => $website->name,
98 'url' => $website->url,
99 'extauth' => $website->extauth,
100 'size' => $website->totalsize,
101 'connect_admin' => $website->adminname,
102 );
103 }
104 MainWP_DB::free_result( $websites );
105 }
106 }
107
108 $primaryBackup = MainWP_System_Utility::get_primary_backup();
109
110 $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.
111
112 /**
113 * Filter: mainwp_sync_others_data
114 *
115 * Filters additional data in the sync request. Allows extensions or 3rd party plugins to hook data to the sync request.
116 *
117 * @param object $pWebsite Object contaning child site data.
118 *
119 * @since Unknown
120 *
121 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
122 */
123 $othersData = apply_filters( 'mainwp_sync_others_data', $othersData, $pWebsite );
124
125 $saved_days_number = apply_filters( 'mainwp_site_actions_saved_days_number', 30 );
126
127 $information = MainWP_Connect::fetch_url_authed(
128 $pWebsite,
129 'stats',
130 array(
131 'optimize' => ( ( get_option( 'mainwp_optimize', 0 ) == 1 ) ? 1 : 0 ),
132 'cloneSites' => ( ! $cloneEnabled ? 0 : rawurlencode( wp_json_encode( $cloneSites ) ) ),
133 'othersData' => wp_json_encode( $othersData ),
134 'server' => get_admin_url(),
135 'numberdaysOutdatePluginTheme' => get_option( 'mainwp_numberdays_Outdate_Plugin_Theme', 365 ),
136 'primaryBackup' => $primaryBackup,
137 'siteId' => $pWebsite->id,
138 'child_actions_saved_days_number' => intval( $saved_days_number ),
139 ),
140 true,
141 $pForceFetch
142 );
143 $return = self::sync_information_array( $pWebsite, $information, '', 1, false, $pAllowDisconnect );
144
145 return $return;
146 } catch ( MainWP_Exception $e ) {
147 $sync_errors = '';
148 $check_result = 1;
149
150 if ( $e->getMessage() == 'HTTPERROR' ) {
151 $sync_errors = esc_html__( 'HTTP error', 'mainwp' ) . ( $e->get_message_extra() != null ? ' - ' . $e->get_message_extra() : '' );
152 $check_result = - 1;
153 } elseif ( $e->getMessage() == 'NOMAINWP' ) {
154 $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>' );
155 $check_result = 1;
156 }
157
158 return self::sync_information_array( $pWebsite, $information, $sync_errors, $check_result, true, $pAllowDisconnect );
159 }
160 }
161
162 /**
163 * Method sync_information_array()
164 *
165 * Grab all Child Site Information.
166 *
167 * @param object $pWebsite The website object.
168 * @param array $information Array contaning information returned from child site.
169 * @param string $sync_errors Check for Sync Errors.
170 * @param int $check_result Check if offline.
171 * @param bool $error True|False.
172 * @param bool $pAllowDisconnect True|False.
173 *
174 * @return bool true|false True on success, false on failure.
175 *
176 * @uses \MainWP\Dashboard\MainWP_DB::update_website_option()
177 * @uses \MainWP\Dashboard\MainWP_DB::update_website_sync_values()
178 * @uses \MainWP\Dashboard\MainWP_DB::update_website_values()
179 * @uses \MainWP\Dashboard\MainWP_Logger::warning_for_website()
180 * @uses \MainWP\Dashboard\MainWP_Monitoring_Handler::get_health_noticed_status_value()
181 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
182 * @uses \MainWP\Dashboard\MainWP_Utility::get_site_health()
183 */
184 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.
185 $emptyArray = wp_json_encode( array() );
186 $websiteValues = array(
187 'directories' => $emptyArray,
188 'plugin_upgrades' => $emptyArray,
189 'theme_upgrades' => $emptyArray,
190 'translation_upgrades' => $emptyArray,
191 'securityIssues' => $emptyArray,
192 'themes' => $emptyArray,
193 'plugins' => $emptyArray,
194 'users' => $emptyArray,
195 'categories' => $emptyArray,
196 'offline_check_result' => $check_result,
197 );
198 $websiteSyncValues = array(
199 'sync_errors' => $sync_errors,
200 'version' => 0,
201 );
202
203 $done = false;
204
205 $current_siteid = 0;
206 if ( is_string( $pWebsite ) || is_int( $pWebsite ) ) {
207 $current_siteid = intval( $pWebsite );
208 } elseif ( is_object( $pWebsite ) && ( ! property_exists( $pWebsite, 'plugin_updates' ) || ! property_exists( $pWebsite, 'theme_updates' ) ) ) {
209 $current_siteid = $pWebsite->id;
210 }
211
212 // to get full data.
213 if ( $current_siteid ) {
214 $pWebsite = MainWP_DB::instance()->get_website_by_id( $current_siteid );
215 }
216
217 /**
218 * Filter: mainwp_before_save_sync_result
219 *
220 * Filters data returned from child site before saving to the database.
221 *
222 * @param object $pWebsite Object containing child site data.
223 *
224 * @since 3.4
225 */
226 $information = apply_filters( 'mainwp_before_save_sync_result', $information, $pWebsite );
227
228 if ( isset( $information['siteurl'] ) ) {
229 $websiteValues['siteurl'] = $information['siteurl'];
230 $done = true;
231 }
232
233 if ( isset( $information['version'] ) ) {
234 $websiteSyncValues['version'] = $information['version'];
235 $done = true;
236 }
237
238 $phpversion = '';
239 if ( isset( $information['site_info'] ) && null != $information['site_info'] ) {
240 if ( is_array( $information['site_info'] ) && isset( $information['site_info']['phpversion'] ) ) {
241 $phpversion = $information['site_info']['phpversion'];
242 }
243 if ( is_array( $information['site_info'] ) && isset( $information['site_info']['ip'] ) ) {
244 $websiteValues['ip'] = sanitize_text_field( wp_unslash( $information['site_info']['ip'] ) );
245 }
246 MainWP_DB::instance()->update_website_option( $pWebsite, 'site_info', wp_json_encode( $information['site_info'] ) );
247 $done = true;
248 } else {
249 MainWP_DB::instance()->update_website_option( $pWebsite, 'site_info', $emptyArray );
250 }
251 MainWP_DB::instance()->update_website_option( $pWebsite, 'phpversion', $phpversion );
252
253 if ( isset( $information['directories'] ) && is_array( $information['directories'] ) ) {
254 $websiteValues['directories'] = wp_json_encode( $information['directories'] );
255 $done = true;
256 } elseif ( isset( $information['directories'] ) ) {
257 $websiteValues['directories'] = $information['directories'];
258 $done = true;
259 }
260
261 if ( isset( $information['wp_updates'] ) && null != $information['wp_updates'] ) {
262 $current_upgrades = MainWP_DB::instance()->get_website_option( $pWebsite, 'wp_upgrades' );
263 $current_upgrades = ( '' != $current_upgrades ) ? json_decode( $current_upgrades, true ) : array();
264
265 MainWP_DB::instance()->update_website_option(
266 $pWebsite,
267 'wp_upgrades',
268 wp_json_encode(
269 array(
270 'current' => $information['wpversion'],
271 'new' => $information['wp_updates'],
272 'check_timestamp' => isset( $current_upgrades['check_timestamp'] ) ? $current_upgrades['check_timestamp'] : time(),
273 )
274 )
275 );
276 $done = true;
277 } else {
278 MainWP_DB::instance()->update_website_option( $pWebsite, 'wp_upgrades', $emptyArray );
279 }
280
281 if ( isset( $information['plugin_updates'] ) ) {
282 $current_upgrades = ( '' != $pWebsite->plugin_upgrades ) ? json_decode( $pWebsite->plugin_upgrades, true ) : array();
283 $update_values = array();
284 if ( is_array( $information['plugin_updates'] ) ) {
285 foreach ( $information['plugin_updates'] as $file => $update ) {
286 if ( isset( $current_upgrades[ $file ] ) && isset( $current_upgrades[ $file ]['check_timestamp'] ) ) {
287 $update['check_timestamp'] = $current_upgrades[ $file ]['check_timestamp'];
288 } else {
289 $update['check_timestamp'] = time();
290 }
291 $update_values[ $file ] = $update;
292 }
293 }
294 $websiteValues['plugin_upgrades'] = wp_json_encode( $update_values );
295 $done = true;
296 }
297
298 if ( isset( $information['theme_updates'] ) ) {
299 $current_upgrades = ( '' != $pWebsite->theme_upgrades ) ? json_decode( $pWebsite->theme_upgrades, true ) : array();
300 $update_values = array();
301 if ( is_array( $information['theme_updates'] ) ) {
302 foreach ( $information['theme_updates'] as $file => $update ) {
303 if ( isset( $current_upgrades[ $file ] ) && isset( $current_upgrades[ $file ]['check_timestamp'] ) ) {
304 $update['check_timestamp'] = $current_upgrades[ $file ]['check_timestamp'];
305 } else {
306 $update['check_timestamp'] = time();
307 }
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, $k ) {
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['nossl'] ) ) {
424 $websiteValues['nossl'] = $information['nossl'];
425 $done = true;
426 }
427
428 if ( isset( $information['wpe'] ) ) {
429 $websiteValues['wpe'] = $information['wpe'];
430 $done = true;
431 }
432
433 if ( isset( $information['wphost'] ) ) {
434 MainWP_DB::instance()->update_website_option( $pWebsite, 'wphost', $information['wphost'] );
435 }
436
437 if ( isset( $information['last_post_gmt'] ) ) {
438 $websiteSyncValues['last_post_gmt'] = $information['last_post_gmt'];
439 $done = true;
440 }
441
442 if ( isset( $information['health_site_status'] ) ) {
443 $health_status = $information['health_site_status'];
444 $hstatus = MainWP_Utility::get_site_health( $health_status );
445 $custom_health_value = $hstatus['val'] - $hstatus['critical'] * 100; // computes custom health value to support sorting by sites health and sites health threshold.
446 $websiteSyncValues['health_value'] = $custom_health_value;
447 $done = true;
448 MainWP_DB::instance()->update_website_option( $pWebsite, 'health_site_status', wp_json_encode( $health_status ) );
449 $new_noticed = MainWP_Monitoring_Handler::get_health_noticed_status_value( $pWebsite, $custom_health_value );
450 if ( null !== $new_noticed ) {
451 MainWP_DB::instance()->update_website_sync_values(
452 $pWebsite->id,
453 array(
454 'health_site_noticed' => $new_noticed,
455 )
456 );
457 }
458
459 $hval = $hstatus['val'];
460 $critical = $hstatus['critical'];
461 $health_status = 0;
462 if ( 80 <= $hval && 0 == $critical ) {
463 $health_status = 0; // Good.
464 } else {
465 $health_status = 1; // Should be improved'.
466 }
467 $websiteSyncValues['health_status'] = $health_status;
468 } else {
469 MainWP_DB::instance()->update_website_option( $pWebsite, 'health_site_status', $emptyArray );
470 }
471
472 if ( isset( $information['mainwpdir'] ) ) {
473 $websiteValues['mainwpdir'] = $information['mainwpdir'];
474 $done = true;
475 }
476
477 if ( isset( $information['uniqueId'] ) ) {
478 $websiteValues['uniqueId'] = $information['uniqueId'];
479 $done = true;
480 }
481
482 if ( isset( $information['clone_adminname'] ) ) {
483 $websiteValues['adminname'] = $information['clone_adminname'];
484 $done = true;
485 }
486
487 if ( isset( $information['admin_nicename'] ) ) {
488 MainWP_DB::instance()->update_website_option( $pWebsite, 'admin_nicename', trim( $information['admin_nicename'] ) );
489 $done = true;
490 }
491
492 if ( isset( $information['admin_useremail'] ) ) {
493 MainWP_DB::instance()->update_website_option( $pWebsite, 'admin_useremail', trim( $information['admin_useremail'] ) );
494 $done = true;
495 }
496
497 if ( isset( $information['plugins_outdate_info'] ) ) {
498 MainWP_DB::instance()->update_website_option( $pWebsite, 'plugins_outdate_info', wp_json_encode( $information['plugins_outdate_info'] ) );
499 $done = true;
500 } else {
501 MainWP_DB::instance()->update_website_option( $pWebsite, 'plugins_outdate_info', $emptyArray );
502 }
503
504 if ( isset( $information['themes_outdate_info'] ) ) {
505 MainWP_DB::instance()->update_website_option( $pWebsite, 'themes_outdate_info', wp_json_encode( $information['themes_outdate_info'] ) );
506 $done = true;
507 } else {
508 MainWP_DB::instance()->update_website_option( $pWebsite, 'themes_outdate_info', $emptyArray );
509 }
510
511 if ( isset( $information['primaryLasttimeBackup'] ) ) {
512 MainWP_DB::instance()->update_website_option( $pWebsite, 'primary_lasttime_backup', $information['primaryLasttimeBackup'] );
513 $done = true;
514 }
515
516 if ( isset( $information['child_site_actions_data'] ) ) {
517 if ( is_array( $information['child_site_actions_data'] ) && isset( $information['child_site_actions_data']['connected_admin'] ) ) {
518 unset( $information['child_site_actions_data']['connected_admin'] );
519 }
520 MainWP_DB_Site_Actions::instance()->sync_site_actions( $pWebsite->id, $information['child_site_actions_data'] );
521 $done = true;
522 }
523
524 if ( ! $done ) {
525 if ( isset( $information['wpversion'] ) ) {
526 $done = true;
527 } elseif ( isset( $information['error'] ) ) {
528 MainWP_Logger::instance()->warning_for_website( $pWebsite, 'SYNC ERROR', '[' . esc_html( $information['error'] ) . ']' );
529 $error = true;
530 $done = true;
531 $websiteSyncValues['sync_errors'] = esc_html__( 'ERROR: ', 'mainwp' ) . esc_html( $information['error'] );
532 } elseif ( ! empty( $sync_errors ) ) {
533 MainWP_Logger::instance()->warning_for_website( $pWebsite, 'SYNC ERROR', '[' . $sync_errors . ']' );
534
535 $error = true;
536 if ( ! $pAllowDisconnect ) {
537 $sync_errors = '';
538 }
539
540 $websiteSyncValues['sync_errors'] = $sync_errors;
541 } else {
542 MainWP_Logger::instance()->warning_for_website( $pWebsite, 'SYNC ERROR', '[Undefined error]' );
543 $error = true;
544 if ( $pAllowDisconnect ) {
545 $websiteSyncValues['sync_errors'] = esc_html__( 'Undefined error! Please, reinstall the MainWP Child plugin on the child site.', 'mainwp' );
546 }
547 }
548 }
549
550 if ( $done ) {
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 return ( ! $error );
574 }
575
576 /**
577 * Method sync_site_icon()
578 *
579 * Get site's icon.
580 *
581 * @param mixed $siteId site's id.
582 *
583 * @return array result error or success
584 * @throws \Exception Error message.
585 *
586 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
587 * @uses \MainWP\Dashboard\MainWP_Connect::get_file_content()
588 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
589 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_website_option()
590 * @uses \MainWP\Dashboard\MainWP_Exception
591 * @uses \MainWP\Dashboard\MainWP_Logger::debug()
592 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
593 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
594 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_dir()
595 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
596 */
597 public static function sync_site_icon( $siteId = null ) {
598 if ( MainWP_Utility::ctype_digit( $siteId ) ) {
599 $website = MainWP_DB::instance()->get_website_by_id( $siteId );
600 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
601 $error = '';
602 try {
603 $information = MainWP_Connect::fetch_url_authed( $website, 'get_site_icon' );
604 } catch ( MainWP_Exception $e ) {
605 $error = $e->getMessage();
606 }
607
608 if ( '' != $error ) {
609 return array( 'error' => $error );
610 } elseif ( isset( $information['faviIconUrl'] ) && ! empty( $information['faviIconUrl'] ) ) {
611 MainWP_Logger::instance()->debug( 'Downloading icon :: ' . esc_html( $information['faviIconUrl'] ) );
612 $content = MainWP_Connect::get_file_content( $information['faviIconUrl'] );
613 if ( ! empty( $content ) ) {
614
615 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
616
617 /**
618 * WordPress files system object.
619 *
620 * @global object
621 */
622 global $wp_filesystem;
623
624 $dirs = MainWP_System_Utility::get_mainwp_dir( 'icons', true );
625 $iconsDir = $dirs[0];
626 $filename = basename( $information['faviIconUrl'] );
627 $filename = strtok( $filename, '?' );
628 if ( $filename ) {
629 $filename = 'favi-' . $siteId . '-' . $filename;
630 $size = false;
631 if ( MainWP_Utility::check_image_file_name( $filename ) ) {
632 $size = $wp_filesystem->put_contents( $iconsDir . $filename, $content ); // phpcs:ignore --
633 }
634 if ( $size ) {
635 MainWP_Logger::instance()->debug( 'Icon size :: ' . $size );
636 MainWP_DB::instance()->update_website_option( $website, 'favi_icon', $filename );
637 return array( 'result' => 'success' );
638 } else {
639 return array( 'error' => 'Save icon file failed.' );
640 }
641 }
642 return array( 'undefined_error' => true );
643 } else {
644 return array( 'error' => esc_html__( 'Download icon file failed', 'mainwp' ) );
645 }
646 } else {
647 return array( 'undefined_error' => true );
648 }
649 }
650 }
651 return array( 'result' => 'NOSITE' );
652 }
653
654 }
655