PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / trunk
MainWP Dashboard: Self-hosted WordPress Management for Agencies vtrunk
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-common-functions.php

class-mainwp-common-functions.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies trunk, at class/class-mainwp-common-functions.php

288 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP System Utility Helper
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Class MainWP_Common_Functions
17 *
18 * @package MainWP\Dashboard
19 */
20 class MainWP_Common_Functions { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
21
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;
28
29 /**
30 * Protected variable to hold User extension.
31 *
32 * @var mixed Default null.
33 */
34 protected $userExtension = null;
35
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 }
46
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 }
61
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 }
73
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();
86
87 $params = array(
88 'view' => 'updates_view',
89 'others_fields' => array( 'premium_upgrades' ),
90 );
91
92 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user_by_params( $params ) );
93
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 }
98
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;
106
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();
109
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 }
113
114 if ( is_array( $wp_upgrades ) && ! empty( $wp_upgrades ) ) {
115 ++$total_wp_upgrades;
116 }
117
118 $plugin_upgrades = ! empty( $website->plugin_upgrades ) ? json_decode( $website->plugin_upgrades, true ) : array();
119 if ( $website->is_ignorePluginUpdates ) {
120 $plugin_upgrades = array();
121 }
122
123 $theme_upgrades = ! empty( $website->theme_upgrades ) ? json_decode( $website->theme_upgrades, true ) : array();
124 if ( $website->is_ignoreThemeUpdates ) {
125 $theme_upgrades = array();
126 }
127
128 $decodedPremiumUpgrades = ! empty( $website->premium_upgrades ) ? json_decode( $website->premium_upgrades, true ) : array();
129
130 if ( is_array( $decodedPremiumUpgrades ) ) {
131 foreach ( $decodedPremiumUpgrades as $crrSlug => $premiumUpgrade ) {
132 $premiumUpgrade['premium'] = true;
133
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 }
151
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 }
157
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 }
162
163 $total_plugin_upgrades += count( $plugin_upgrades );
164 }
165
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 }
171
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 }
176
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 }
287 }
288