| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP System Utility Helper |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class MainWP_Common_Functions |
| 12 |
* |
| 13 |
* @package MainWP\Dashboard |
| 14 |
*/ |
| 15 |
class MainWP_Common_Functions { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 16 |
|
| 17 |
/** |
| 18 |
* Private static variable to hold the single instance of the class. |
| 19 |
* |
| 20 |
* @var mixed Default null |
| 21 |
*/ |
| 22 |
private static $instance = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Protected variable to hold User extension. |
| 26 |
* |
| 27 |
* @var mixed Default null. |
| 28 |
*/ |
| 29 |
protected $userExtension = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Method get_class_name() |
| 33 |
* |
| 34 |
* Get Class Name. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public static function get_class_name() { |
| 39 |
return __CLASS__; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Method instance() |
| 44 |
* |
| 45 |
* Create public static instance. |
| 46 |
* |
| 47 |
* @static |
| 48 |
* @return MainWP_Common_Functions |
| 49 |
*/ |
| 50 |
public static function instance() { |
| 51 |
if ( null === static::$instance ) { |
| 52 |
static::$instance = new self(); |
| 53 |
} |
| 54 |
return static::$instance; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* MainWP_Common_Functions constructor. |
| 59 |
* |
| 60 |
* Runs any time class is called. |
| 61 |
*/ |
| 62 |
public function __construct() { |
| 63 |
static::$instance = $this; |
| 64 |
if ( null === $this->userExtension ) { |
| 65 |
$this->userExtension = MainWP_DB_Common::instance()->get_user_extension(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get child site ids that have available updates. |
| 71 |
* |
| 72 |
* @return array $site_ids Array of Child Site ID's that have updates. |
| 73 |
* |
| 74 |
* @uses \MainWP\Dashboard\MainWP_DB::instance()::query() |
| 75 |
* @uses \MainWP\Dashboard\MainWP_DB::instance()::get_sql_websites_for_current_user() |
| 76 |
* @uses \MainWP\Dashboard\MainWP_DB::fetch_object() |
| 77 |
* @uses \MainWP\Dashboard\MainWP_DB::instance()::get_website_options_array() |
| 78 |
*/ |
| 79 |
public function get_available_update_siteids() { // phpcs:ignore -- NOSONAR - complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated. |
| 80 |
$site_ids = array(); |
| 81 |
|
| 82 |
$params = array( |
| 83 |
'view' => 'updates_view', |
| 84 |
'others_fields' => array( 'premium_upgrades' ), |
| 85 |
); |
| 86 |
|
| 87 |
$websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user_by_params( $params ) ); |
| 88 |
|
| 89 |
$decodedIgnoredCores = ! empty( $this->userExtension->ignored_wp_upgrades ) ? json_decode( $this->userExtension->ignored_wp_upgrades, true ) : array(); |
| 90 |
if ( ! is_array( $decodedIgnoredCores ) ) { |
| 91 |
$decodedIgnoredCores = array(); |
| 92 |
} |
| 93 |
|
| 94 |
while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) { |
| 95 |
$hasSyncErrors = ( '' !== $website->sync_errors ); |
| 96 |
$cnt = 0; |
| 97 |
if ( 1 === (int) $website->offline_check_result && ! $hasSyncErrors ) { |
| 98 |
$total_wp_upgrades = 0; |
| 99 |
$total_plugin_upgrades = 0; |
| 100 |
$total_theme_upgrades = 0; |
| 101 |
|
| 102 |
$wp_upgrades = isset( $website->wp_upgrades ) && ! empty( $website->wp_upgrades ) ? json_decode( $website->wp_upgrades, true ) : array(); |
| 103 |
$ignored_core_upgrades = ! empty( $website->ignored_wp_upgrades ) ? json_decode( $website->ignored_wp_upgrades, true ) : array(); |
| 104 |
|
| 105 |
if ( $website->is_ignoreCoreUpdates || $this->is_ignored_updates( $wp_upgrades, $ignored_core_upgrades, 'core' ) || $this->is_ignored_updates( $wp_upgrades, $decodedIgnoredCores, 'core' ) ) { |
| 106 |
$wp_upgrades = array(); |
| 107 |
} |
| 108 |
|
| 109 |
if ( is_array( $wp_upgrades ) && ! empty( $wp_upgrades ) ) { |
| 110 |
++$total_wp_upgrades; |
| 111 |
} |
| 112 |
|
| 113 |
$plugin_upgrades = ! empty( $website->plugin_upgrades ) ? json_decode( $website->plugin_upgrades, true ) : array(); |
| 114 |
if ( $website->is_ignorePluginUpdates ) { |
| 115 |
$plugin_upgrades = array(); |
| 116 |
} |
| 117 |
|
| 118 |
$theme_upgrades = ! empty( $website->theme_upgrades ) ? json_decode( $website->theme_upgrades, true ) : array(); |
| 119 |
if ( $website->is_ignoreThemeUpdates ) { |
| 120 |
$theme_upgrades = array(); |
| 121 |
} |
| 122 |
|
| 123 |
$decodedPremiumUpgrades = ! empty( $website->premium_upgrades ) ? json_decode( $website->premium_upgrades, true ) : array(); |
| 124 |
|
| 125 |
if ( is_array( $decodedPremiumUpgrades ) ) { |
| 126 |
foreach ( $decodedPremiumUpgrades as $crrSlug => $premiumUpgrade ) { |
| 127 |
$premiumUpgrade['premium'] = true; |
| 128 |
|
| 129 |
if ( 'plugin' === $premiumUpgrade['type'] ) { |
| 130 |
if ( ! is_array( $plugin_upgrades ) ) { |
| 131 |
$plugin_upgrades = array(); |
| 132 |
} |
| 133 |
if ( ! $website->is_ignorePluginUpdates ) { |
| 134 |
$plugin_upgrades[ $crrSlug ] = $premiumUpgrade; |
| 135 |
} |
| 136 |
} elseif ( 'theme' === $premiumUpgrade['type'] ) { |
| 137 |
if ( ! is_array( $theme_upgrades ) ) { |
| 138 |
$theme_upgrades = array(); |
| 139 |
} |
| 140 |
if ( ! $website->is_ignoreThemeUpdates ) { |
| 141 |
$theme_upgrades[ $crrSlug ] = $premiumUpgrade; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if ( is_array( $plugin_upgrades ) ) { |
| 148 |
$ignored_plugins = json_decode( $website->ignored_plugins, true ); |
| 149 |
if ( is_array( $ignored_plugins ) ) { |
| 150 |
$plugin_upgrades = $this->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins ); |
| 151 |
} |
| 152 |
|
| 153 |
$ignored_plugins = json_decode( $this->userExtension->ignored_plugins, true ); |
| 154 |
if ( is_array( $ignored_plugins ) ) { |
| 155 |
$plugin_upgrades = $this->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins ); |
| 156 |
} |
| 157 |
|
| 158 |
$total_plugin_upgrades += count( $plugin_upgrades ); |
| 159 |
} |
| 160 |
|
| 161 |
if ( is_array( $theme_upgrades ) ) { |
| 162 |
$ignored_themes = json_decode( $website->ignored_themes, true ); |
| 163 |
if ( is_array( $ignored_themes ) ) { |
| 164 |
$theme_upgrades = $this->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes ); |
| 165 |
} |
| 166 |
|
| 167 |
$ignored_themes = json_decode( $this->userExtension->ignored_themes, true ); |
| 168 |
if ( is_array( $ignored_themes ) ) { |
| 169 |
$theme_upgrades = $this->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes ); |
| 170 |
} |
| 171 |
|
| 172 |
$total_theme_upgrades += count( $theme_upgrades ); |
| 173 |
} |
| 174 |
|
| 175 |
$cnt = $total_wp_upgrades + $total_plugin_upgrades + $total_theme_upgrades; |
| 176 |
|
| 177 |
if ( 0 < $cnt ) { |
| 178 |
$site_ids[] = $website->id; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return $site_ids; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Method get_not_ignored_updates_themesplugins(). |
| 188 |
* |
| 189 |
* To compatible with new ignored update info. |
| 190 |
* |
| 191 |
* @since 5.2. |
| 192 |
* |
| 193 |
* @param array $updates Update info. |
| 194 |
* @param array $ignored Ignored update info. |
| 195 |
* |
| 196 |
* @return array $updates Not ignored updates info. |
| 197 |
*/ |
| 198 |
public function get_not_ignored_updates_themesplugins( $updates, $ignored ) { //phpcs:ignore -- NOSONAR - complexity. |
| 199 |
if ( ! is_array( $updates ) || ! is_array( $ignored ) ) { |
| 200 |
return $updates; |
| 201 |
} |
| 202 |
$new_updates = array(); |
| 203 |
foreach ( $updates as $slug => $info ) { |
| 204 |
if ( isset( $ignored[ $slug ] ) ) { |
| 205 |
if ( is_string( $ignored[ $slug ] ) ) { |
| 206 |
// old ignored info. |
| 207 |
continue; // ignored update. |
| 208 |
} elseif ( is_array( $ignored[ $slug ] ) && ! empty( $ignored[ $slug ]['ignored_versions'] ) ) { |
| 209 |
$ignored_vers = is_array( $ignored[ $slug ]['ignored_versions'] ) ? $ignored[ $slug ]['ignored_versions'] : array(); |
| 210 |
$new_version = is_array( $info ) && isset( $info['update']['new_version'] ) ? $info['update']['new_version'] : ''; |
| 211 |
if ( in_array( 'all_versions', $ignored_vers ) || in_array( $new_version, $ignored_vers ) ) { |
| 212 |
continue; // ignored update. |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
$new_updates[ $slug ] = $info; |
| 217 |
} |
| 218 |
return $new_updates; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Method is_ignored_updates(). |
| 224 |
* |
| 225 |
* To compatible with new ignored update info. |
| 226 |
* |
| 227 |
* @since 5.2. |
| 228 |
* |
| 229 |
* @param array $item Update info of theme or plugin. |
| 230 |
* @param array $ignored Ignored update info. |
| 231 |
* @param string $type theme/plugin/core. |
| 232 |
|
| 233 |
* @return bool Ignored updates. |
| 234 |
*/ |
| 235 |
public function is_ignored_updates( $item, $ignored, $type = 'plugin' ) { //phpcs:ignore -- NOSONAR complex function. |
| 236 |
|
| 237 |
if ( ! is_array( $item ) || ! is_array( $ignored ) ) { |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
if ( in_array( $type, array( 'plugin', 'theme' ) ) ) { |
| 242 |
$item_slug = isset( $item['update'] ) && is_array( $item['update'] ) && isset( $item['update'][ $type ] ) ? $item['update'][ $type ] : ''; |
| 243 |
$new_version = isset( $item['update']['new_version'] ) ? $item['update']['new_version'] : ''; |
| 244 |
|
| 245 |
if ( empty( $item_slug ) && ! empty( $item['slug'] ) ) { |
| 246 |
$item_slug = $item['slug']; // for data of plugin/theme of site. |
| 247 |
} |
| 248 |
if ( empty( $new_version ) && ! empty( $item['new_version'] ) ) { |
| 249 |
$new_version = $item['new_version']; // for data of plugin/theme of site. |
| 250 |
} |
| 251 |
|
| 252 |
if ( isset( $ignored[ $item_slug ] ) ) { |
| 253 |
if ( is_string( $ignored[ $item_slug ] ) ) { // old ignore info. |
| 254 |
return true; // ignored update. |
| 255 |
} elseif ( is_array( $ignored[ $item_slug ] ) && ! empty( $ignored[ $item_slug ]['ignored_versions'] ) ) { |
| 256 |
$ignored_vers = is_array( $ignored[ $item_slug ]['ignored_versions'] ) ? $ignored[ $item_slug ]['ignored_versions'] : array(); |
| 257 |
if ( in_array( 'all_versions', $ignored_vers ) || in_array( $new_version, $ignored_vers ) ) { |
| 258 |
return true; // ignored update. |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
return false; |
| 263 |
} elseif ( 'core' === $type ) { |
| 264 |
$ignored_vers = isset( $ignored['ignored_versions'] ) && is_array( $ignored['ignored_versions'] ) ? $ignored['ignored_versions'] : array(); |
| 265 |
$new_version = isset( $item['new'] ) ? $item['new'] : ''; |
| 266 |
if ( empty( $new_version ) ) { |
| 267 |
$new_version = isset( $item['new_version'] ) ? $item['new_version'] : ''; // to support some info. |
| 268 |
} |
| 269 |
if ( ! empty( $new_version ) && ( in_array( 'all_versions', $ignored_vers ) || in_array( $new_version, $ignored_vers ) ) ) { |
| 270 |
return true; // ignored update. |
| 271 |
} |
| 272 |
} |
| 273 |
return false; |
| 274 |
} |
| 275 |
} |
| 276 |
|