| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Common Handler |
| 4 |
* |
| 5 |
* This class handles the common functions. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MainWP\Dashboard; |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class MainWP_Common_Handler |
| 19 |
* |
| 20 |
* @package MainWP\Dashboard |
| 21 |
*/ |
| 22 |
class MainWP_Common_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 23 |
|
| 24 |
/** |
| 25 |
* Protected static variable to hold the single instance of the class. |
| 26 |
* |
| 27 |
* @var mixed Default null |
| 28 |
*/ |
| 29 |
private static $instance = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Method instance() |
| 33 |
* |
| 34 |
* Create public static instance. |
| 35 |
* |
| 36 |
* @static |
| 37 |
* @return static::$instance |
| 38 |
*/ |
| 39 |
public static function instance() { |
| 40 |
if ( null === static::$instance ) { |
| 41 |
static::$instance = new self(); |
| 42 |
} |
| 43 |
return static::$instance; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Method sites_available_updates_count() |
| 48 |
* |
| 49 |
* Returns the number of available udpates for sites. |
| 50 |
* |
| 51 |
* @return mixed $response An object that contains the return data and status of the API request. |
| 52 |
*/ |
| 53 |
public function sites_available_updates_count() { // phpcs:ignore -- NOSONAR - complex function. |
| 54 |
|
| 55 |
$cache_group = MainWP_Cache_Helper::CGR_UPDATES; |
| 56 |
|
| 57 |
$cache_key = MainWP_Cache_Helper::get_cache_key( 'updates_count', $cache_group ); |
| 58 |
|
| 59 |
$cache_values = MainWP_Cache_Helper::instance()->get_cache( |
| 60 |
$cache_key, |
| 61 |
$cache_group |
| 62 |
); |
| 63 |
|
| 64 |
if ( is_array( $cache_values ) ) { |
| 65 |
return $cache_values; |
| 66 |
} |
| 67 |
|
| 68 |
$is_staging = 'no'; |
| 69 |
|
| 70 |
$db_updater_count = false; |
| 71 |
$total_plugin_db_upgrades = 0; |
| 72 |
$supported_db_updater_plugins = array(); |
| 73 |
|
| 74 |
$get_fields = array( 'premium_upgrades', 'favi_icon', 'ignored_wp_upgrades', 'ignored_trans_updates' ); |
| 75 |
$custom_fields = apply_filters( 'mainwp_available_updates_count_custom_fields_data', array(), 'updates_count' ); |
| 76 |
|
| 77 |
if ( is_array( $custom_fields ) && 1 === count( $custom_fields ) && in_array( 'plugin_db_upgrades', $custom_fields ) ) { |
| 78 |
$get_fields = array_merge( $get_fields, $custom_fields ); |
| 79 |
$db_updater_count = true; |
| 80 |
$supported_db_updater_plugins = apply_filters( 'mainwp_database_updater_supported_plugins', array() ); |
| 81 |
} |
| 82 |
|
| 83 |
$sql = MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, false, $get_fields, $is_staging, array( 'connected' => 'yes' ) ); |
| 84 |
|
| 85 |
$userExtension = MainWP_DB_Common::instance()->get_user_extension(); |
| 86 |
|
| 87 |
$decodedIgnoredCores = ! empty( $userExtension->ignored_wp_upgrades ) ? json_decode( $userExtension->ignored_wp_upgrades, true ) : array(); |
| 88 |
if ( ! is_array( $decodedIgnoredCores ) ) { |
| 89 |
$decodedIgnoredCores = array(); |
| 90 |
} |
| 91 |
|
| 92 |
$websites = MainWP_DB::instance()->query( $sql ); |
| 93 |
|
| 94 |
$mainwp_show_language_updates = get_option( 'mainwp_show_language_updates', 1 ); |
| 95 |
|
| 96 |
$total_wp_upgrades = 0; |
| 97 |
$total_plugin_upgrades = 0; |
| 98 |
$total_translation_upgrades = 0; |
| 99 |
$total_theme_upgrades = 0; |
| 100 |
|
| 101 |
MainWP_DB::data_seek( $websites, 0 ); |
| 102 |
|
| 103 |
while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) { |
| 104 |
$wp_upgrades = ! empty( $website->wp_upgrades ) ? json_decode( $website->wp_upgrades, true ) : array(); |
| 105 |
$ignored_core_upgrades = ! empty( $website->ignored_wp_upgrades ) ? json_decode( $website->ignored_wp_upgrades, true ) : array(); |
| 106 |
|
| 107 |
if ( $website->is_ignoreCoreUpdates || MainWP_Common_Functions::instance()->is_ignored_updates( $wp_upgrades, $ignored_core_upgrades, 'core' ) || MainWP_Common_Functions::instance()->is_ignored_updates( $wp_upgrades, $decodedIgnoredCores, 'core' ) ) { |
| 108 |
$wp_upgrades = array(); |
| 109 |
} |
| 110 |
|
| 111 |
if ( is_array( $wp_upgrades ) && ! empty( $wp_upgrades ) ) { |
| 112 |
++$total_wp_upgrades; |
| 113 |
} |
| 114 |
|
| 115 |
$translation_upgrades = json_decode( $website->translation_upgrades, true ); |
| 116 |
|
| 117 |
if ( ! empty( $website->ignored_trans_updates ) ) { |
| 118 |
$translation_upgrades = array(); |
| 119 |
} |
| 120 |
|
| 121 |
$plugin_upgrades = json_decode( $website->plugin_upgrades, true ); |
| 122 |
if ( $website->is_ignorePluginUpdates ) { |
| 123 |
$plugin_upgrades = array(); |
| 124 |
} |
| 125 |
|
| 126 |
$theme_upgrades = json_decode( $website->theme_upgrades, true ); |
| 127 |
if ( $website->is_ignoreThemeUpdates ) { |
| 128 |
$theme_upgrades = array(); |
| 129 |
} |
| 130 |
|
| 131 |
$decodedPremiumUpgrades = MainWP_DB::instance()->get_website_option( $website, 'premium_upgrades' ); |
| 132 |
$decodedPremiumUpgrades = ! empty( $decodedPremiumUpgrades ) ? json_decode( $decodedPremiumUpgrades, true ) : array(); |
| 133 |
|
| 134 |
if ( is_array( $decodedPremiumUpgrades ) ) { |
| 135 |
foreach ( $decodedPremiumUpgrades as $crrSlug => $premiumUpgrade ) { |
| 136 |
$premiumUpgrade['premium'] = true; |
| 137 |
|
| 138 |
if ( 'plugin' === $premiumUpgrade['type'] ) { |
| 139 |
if ( ! is_array( $plugin_upgrades ) ) { |
| 140 |
$plugin_upgrades = array(); |
| 141 |
} |
| 142 |
if ( ! $website->is_ignorePluginUpdates ) { |
| 143 |
|
| 144 |
$premiumUpgrade = array_filter( $premiumUpgrade ); |
| 145 |
if ( ! isset( $plugin_upgrades[ $crrSlug ] ) ) { |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
$plugin_upgrades[ $crrSlug ] = array_merge( $plugin_upgrades[ $crrSlug ], $premiumUpgrade ); |
| 150 |
} |
| 151 |
} elseif ( 'theme' === $premiumUpgrade['type'] ) { |
| 152 |
if ( ! is_array( $theme_upgrades ) ) { |
| 153 |
$theme_upgrades = array(); |
| 154 |
} |
| 155 |
if ( ! $website->is_ignoreThemeUpdates ) { |
| 156 |
$theme_upgrades[ $crrSlug ] = $premiumUpgrade; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if ( is_array( $translation_upgrades ) ) { |
| 163 |
$total_translation_upgrades += count( $translation_upgrades ); |
| 164 |
} |
| 165 |
|
| 166 |
if ( is_array( $plugin_upgrades ) ) { |
| 167 |
$ignored_plugins = json_decode( $website->ignored_plugins, true ); |
| 168 |
if ( is_array( $ignored_plugins ) ) { |
| 169 |
$plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins ); |
| 170 |
} |
| 171 |
|
| 172 |
$ignored_plugins = json_decode( $userExtension->ignored_plugins, true ); |
| 173 |
if ( is_array( $ignored_plugins ) ) { |
| 174 |
$plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins ); |
| 175 |
} |
| 176 |
|
| 177 |
$total_plugin_upgrades += count( $plugin_upgrades ); |
| 178 |
} |
| 179 |
|
| 180 |
if ( is_array( $theme_upgrades ) ) { |
| 181 |
$ignored_themes = json_decode( $website->ignored_themes, true ); |
| 182 |
if ( is_array( $ignored_themes ) ) { |
| 183 |
$theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes ); |
| 184 |
} |
| 185 |
|
| 186 |
$ignored_themes = json_decode( $userExtension->ignored_themes, true ); |
| 187 |
if ( is_array( $ignored_themes ) ) { |
| 188 |
$theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes ); |
| 189 |
} |
| 190 |
|
| 191 |
$total_theme_upgrades += count( $theme_upgrades ); |
| 192 |
} |
| 193 |
|
| 194 |
if ( $db_updater_count ) { |
| 195 |
|
| 196 |
$plugin_db_upgrades = ! empty( $website->plugin_db_upgrades ) ? json_decode( $website->plugin_db_upgrades, true ) : array(); |
| 197 |
if ( $website->is_ignorePluginUpdates ) { |
| 198 |
$plugin_db_upgrades = array(); |
| 199 |
} |
| 200 |
|
| 201 |
if ( is_array( $plugin_db_upgrades ) && ! $website->is_ignorePluginUpdates ) { |
| 202 |
$_ignored_plugins = ! empty( $website->ignored_plugins ) ? json_decode( $website->ignored_plugins, true ) : array(); |
| 203 |
if ( is_array( $_ignored_plugins ) ) { |
| 204 |
$plugin_db_upgrades = $this->get_not_ignored_updates_plugins_db( $plugin_db_upgrades, $_ignored_plugins ); |
| 205 |
} |
| 206 |
|
| 207 |
$_ignored_plugins = ! empty( $userExtension->ignored_plugins ) ? json_decode( $userExtension->ignored_plugins, true ) : array(); |
| 208 |
if ( is_array( $_ignored_plugins ) ) { |
| 209 |
$plugin_db_upgrades = $this->get_not_ignored_updates_plugins_db( $plugin_db_upgrades, $_ignored_plugins ); |
| 210 |
} |
| 211 |
|
| 212 |
// supported the WC plugin. |
| 213 |
if ( is_array( $plugin_db_upgrades ) ) { |
| 214 |
foreach ( $supported_db_updater_plugins as $supp_slug ) { |
| 215 |
if ( isset( $plugin_db_upgrades[ $supp_slug ] ) ) { |
| 216 |
++$total_plugin_db_upgrades; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// WP Upgrades part. |
| 225 |
$total_upgrades = $total_wp_upgrades + $total_plugin_upgrades + $total_theme_upgrades; |
| 226 |
|
| 227 |
// to fix incorrect total updates. |
| 228 |
if ( $mainwp_show_language_updates ) { |
| 229 |
$total_upgrades += $total_translation_upgrades; |
| 230 |
$data = array( |
| 231 |
'total' => $total_upgrades, |
| 232 |
'wp' => $total_wp_upgrades, |
| 233 |
'plugins' => $total_plugin_upgrades, |
| 234 |
'themes' => $total_theme_upgrades, |
| 235 |
'translations' => $total_translation_upgrades, |
| 236 |
); |
| 237 |
} else { |
| 238 |
$data = array( |
| 239 |
'total' => $total_upgrades, |
| 240 |
'wp' => $total_wp_upgrades, |
| 241 |
'plugins' => $total_plugin_upgrades, |
| 242 |
'themes' => $total_theme_upgrades, |
| 243 |
'translations' => 0, |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
if ( $db_updater_count ) { |
| 248 |
$data['db_updater_count'] = $total_plugin_db_upgrades; |
| 249 |
$data['total'] += $total_plugin_db_upgrades; |
| 250 |
} |
| 251 |
MainWP_DB::free_result( $websites ); |
| 252 |
|
| 253 |
// set cache. |
| 254 |
MainWP_Cache_Helper::add_cache( $cache_key, $cache_group, $data ); |
| 255 |
|
| 256 |
return $data; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
/** |
| 261 |
* Method get_not_ignored_updates_plugins_db(). |
| 262 |
* |
| 263 |
* To compatible with new ignored update info. |
| 264 |
* |
| 265 |
* @param array $updates Update info. |
| 266 |
* @param array $ignored Ignored update info. |
| 267 |
* @param int $count_ignored Count ignored updates. |
| 268 |
* |
| 269 |
* @return array $updates Not ignored updates info. |
| 270 |
*/ |
| 271 |
public function get_not_ignored_updates_plugins_db( $updates, $ignored, &$count_ignored = 0 ) { //phpcs:ignore -- NOSONAR - complexity. |
| 272 |
if ( ! is_array( $updates ) || ! is_array( $ignored ) ) { |
| 273 |
return $updates; |
| 274 |
} |
| 275 |
$new_updates = array(); |
| 276 |
foreach ( $updates as $slug => $dbup_info ) { |
| 277 |
if ( isset( $ignored[ $slug ] ) ) { |
| 278 |
if ( is_string( $ignored[ $slug ] ) ) { |
| 279 |
++$count_ignored; |
| 280 |
// old ignored info. |
| 281 |
continue; // ignored update. |
| 282 |
} elseif ( is_array( $ignored[ $slug ] ) && ! empty( $ignored[ $slug ]['ignored_versions'] ) ) { |
| 283 |
$ignored_vers = is_array( $ignored[ $slug ]['ignored_versions'] ) ? $ignored[ $slug ]['ignored_versions'] : array(); |
| 284 |
$cur_version = is_array( $dbup_info ) && isset( $dbup_info['version'] ) ? $dbup_info['version'] : ''; |
| 285 |
if ( in_array( 'all_versions', $ignored_vers ) || ( ! empty( $cur_version ) && in_array( $cur_version, $ignored_vers ) ) ) { |
| 286 |
++$count_ignored; |
| 287 |
continue; // ignored update. |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
$new_updates[ $slug ] = $dbup_info; |
| 292 |
} |
| 293 |
return $new_updates; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
// End of class. |
| 298 |
|