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