PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.1
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-handler.php

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

229 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
13 * Class MainWP_Common_Handler
14 *
15 * @package MainWP\Dashboard
16 */
17 class MainWP_Common_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
18
19 /**
20 * Protected static variable to hold the single instance of the class.
21 *
22 * @var mixed Default null
23 */
24 private static $instance = null;
25
26 /**
27 * Method instance()
28 *
29 * Create public static instance.
30 *
31 * @static
32 * @return static::$instance
33 */
34 public static function instance() {
35 if ( null === static::$instance ) {
36 static::$instance = new self();
37 }
38 return static::$instance;
39 }
40
41 /**
42 * Method sites_available_updates_count()
43 *
44 * Returns the number of available udpates for sites.
45 *
46 * @return object $response An object that contains the return data and status of the API request.
47 */
48 public function sites_available_updates_count() { // phpcs:ignore -- NOSONAR - complex function.
49 $is_staging = 'no';
50
51 $db_updater_count = false;
52 $total_plugin_db_upgrades = 0;
53 $supported_db_updater_plugins = array();
54
55 $get_fields = array( 'premium_upgrades', 'favi_icon' );
56 $custom_fields = apply_filters( 'mainwp_available_updates_count_custom_fields_data', array(), 'updates_count' );
57
58 if ( is_array( $custom_fields ) && 1 === count( $custom_fields ) && in_array( 'plugin_db_upgrades', $custom_fields ) ) {
59 $get_fields = array_merge( $get_fields, $custom_fields );
60 $db_updater_count = true;
61 $supported_db_updater_plugins = apply_filters( 'mainwp_database_updater_supported_plugins', array() );
62 }
63
64 $sql = MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, false, $get_fields, $is_staging );
65
66 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
67 $websites = MainWP_DB::instance()->query( $sql );
68
69 $mainwp_show_language_updates = get_option( 'mainwp_show_language_updates', 1 );
70
71 $total_wp_upgrades = 0;
72 $total_plugin_upgrades = 0;
73 $total_translation_upgrades = 0;
74 $total_theme_upgrades = 0;
75
76 MainWP_DB::data_seek( $websites, 0 );
77
78 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
79 $wp_upgrades = MainWP_DB::instance()->get_website_option( $website, 'wp_upgrades' );
80 $wp_upgrades = ! empty( $wp_upgrades ) ? json_decode( $wp_upgrades, true ) : array();
81
82 if ( $website->is_ignoreCoreUpdates ) {
83 $wp_upgrades = array();
84 }
85
86 if ( is_array( $wp_upgrades ) && ! empty( $wp_upgrades ) ) {
87 ++$total_wp_upgrades;
88 }
89
90 $translation_upgrades = json_decode( $website->translation_upgrades, true );
91
92 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
93 if ( $website->is_ignorePluginUpdates ) {
94 $plugin_upgrades = array();
95 }
96
97 $theme_upgrades = json_decode( $website->theme_upgrades, true );
98 if ( $website->is_ignoreThemeUpdates ) {
99 $theme_upgrades = array();
100 }
101
102 $decodedPremiumUpgrades = MainWP_DB::instance()->get_website_option( $website, 'premium_upgrades' );
103 $decodedPremiumUpgrades = ! empty( $decodedPremiumUpgrades ) ? json_decode( $decodedPremiumUpgrades, true ) : array();
104
105 if ( is_array( $decodedPremiumUpgrades ) ) {
106 foreach ( $decodedPremiumUpgrades as $crrSlug => $premiumUpgrade ) {
107 $premiumUpgrade['premium'] = true;
108
109 if ( 'plugin' === $premiumUpgrade['type'] ) {
110 if ( ! is_array( $plugin_upgrades ) ) {
111 $plugin_upgrades = array();
112 }
113 if ( ! $website->is_ignorePluginUpdates ) {
114
115 $premiumUpgrade = array_filter( $premiumUpgrade );
116 if ( ! isset( $plugin_upgrades[ $crrSlug ] ) ) {
117 $plugin_upgrades[ $crrSlug ] = array();
118 }
119
120 $plugin_upgrades[ $crrSlug ] = array_merge( $plugin_upgrades[ $crrSlug ], $premiumUpgrade );
121 }
122 } elseif ( 'theme' === $premiumUpgrade['type'] ) {
123 if ( ! is_array( $theme_upgrades ) ) {
124 $theme_upgrades = array();
125 }
126 if ( ! $website->is_ignoreThemeUpdates ) {
127 $theme_upgrades[ $crrSlug ] = $premiumUpgrade;
128 }
129 }
130 }
131 }
132
133 if ( is_array( $translation_upgrades ) ) {
134 $total_translation_upgrades += count( $translation_upgrades );
135 }
136
137 if ( is_array( $plugin_upgrades ) ) {
138 $ignored_plugins = json_decode( $website->ignored_plugins, true );
139 if ( is_array( $ignored_plugins ) ) {
140 $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
141 }
142
143 $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
144 if ( is_array( $ignored_plugins ) ) {
145 $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
146 }
147
148 $total_plugin_upgrades += count( $plugin_upgrades );
149 }
150
151 if ( is_array( $theme_upgrades ) ) {
152 $ignored_themes = json_decode( $website->ignored_themes, true );
153 if ( is_array( $ignored_themes ) ) {
154 $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
155 }
156
157 $ignored_themes = json_decode( $userExtension->ignored_themes, true );
158 if ( is_array( $ignored_themes ) ) {
159 $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
160 }
161
162 $total_theme_upgrades += count( $theme_upgrades );
163 }
164
165 if ( $db_updater_count ) {
166
167 $plugin_db_upgrades = ! empty( $website->plugin_db_upgrades ) ? json_decode( $website->plugin_db_upgrades, true ) : array();
168 if ( $website->is_ignorePluginUpdates ) {
169 $plugin_db_upgrades = array();
170 }
171
172 if ( is_array( $plugin_db_upgrades ) && ! $website->is_ignorePluginUpdates ) {
173 $_ignored_plugins = ! empty( $website->ignored_plugins ) ? json_decode( $website->ignored_plugins, true ) : array();
174 if ( is_array( $_ignored_plugins ) ) {
175 $plugin_db_upgrades = array_diff_key( $plugin_db_upgrades, $_ignored_plugins );
176 }
177
178 $_ignored_plugins = ! empty( $userExtension->ignored_plugins ) ? json_decode( $userExtension->ignored_plugins, true ) : array();
179 if ( is_array( $_ignored_plugins ) ) {
180 $plugin_db_upgrades = array_diff_key( $plugin_db_upgrades, $_ignored_plugins );
181 }
182
183 // supported the WC plugin.
184 if ( is_array( $plugin_db_upgrades ) ) {
185 foreach ( $supported_db_updater_plugins as $supp_slug ) {
186 if ( isset( $plugin_db_upgrades[ $supp_slug ] ) ) {
187 ++$total_plugin_db_upgrades;
188 }
189 }
190 }
191 }
192 }
193 }
194
195 // WP Upgrades part.
196 $total_upgrades = $total_wp_upgrades + $total_plugin_upgrades + $total_theme_upgrades;
197
198 // to fix incorrect total updates.
199 if ( $mainwp_show_language_updates ) {
200 $total_upgrades += $total_translation_upgrades;
201 $data = array(
202 'total' => $total_upgrades,
203 'wp' => $total_wp_upgrades,
204 'plugins' => $total_plugin_upgrades,
205 'themes' => $total_theme_upgrades,
206 'translations' => $total_translation_upgrades,
207 );
208 } else {
209 $data = array(
210 'total' => $total_upgrades,
211 'wp' => $total_wp_upgrades,
212 'plugins' => $total_plugin_upgrades,
213 'themes' => $total_theme_upgrades,
214 'translations' => 0,
215 );
216 }
217
218 if ( $db_updater_count ) {
219 $data['db_updater_count'] = $total_plugin_db_upgrades;
220 $data['total'] += $total_plugin_db_upgrades;
221 }
222 MainWP_DB::free_result( $websites );
223
224 return $data;
225 }
226 }
227
228 // End of class.
229