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

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