PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.2 6.2.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 All 155 releases
mainwp / class / class-mainwp-updates-report-manager.php

class-mainwp-updates-report-manager.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.2, at class/class-mainwp-updates-report-manager.php

326 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Updates report manager.
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 /**
17 * Updates report manager.
18 *
19 * For storing update report data and sending notifications for auto updates.
20 */
21 class MainWP_Updates_Report_Manager {
22
23 /**
24 * Option name.
25 */
26 const OPTION_NAME = 'mainwp_updates_report_storage';
27
28 /**
29 * Update types.
30 */
31 const TYPE_PLUGIN = 'plugins';
32 const TYPE_THEME = 'themes';
33 const TYPE_CORE = 'core';
34 const TYPE_TRANSLATION = 'translations';
35
36 const MAPPED_TYPES = array(
37 'plugin' => self::TYPE_PLUGIN,
38 'theme' => self::TYPE_THEME,
39 'trans' => self::TYPE_TRANSLATION,
40 'core' => self::TYPE_CORE,
41 );
42
43 /**
44 * Valid types.
45 *
46 * @return array
47 */
48 protected static function get_types() {
49 return array(
50 self::TYPE_PLUGIN,
51 self::TYPE_THEME,
52 self::TYPE_CORE,
53 self::TYPE_TRANSLATION,
54 );
55 }
56
57
58
59 /**
60 * Initialize storage.
61 *
62 * Optional: call once before updates begin.
63 *
64 * @return void
65 */
66 public static function init() {
67 MainWP_Utility::update_option( 'mainwp_automatic_updates_ready_to_send_notification', 0 );
68 MainWP_Utility::update_option(
69 self::OPTION_NAME,
70 array(
71 self::TYPE_PLUGIN => array(),
72 self::TYPE_THEME => array(),
73 self::TYPE_CORE => array(),
74 self::TYPE_TRANSLATION => array(),
75 ),
76 );
77 }
78
79
80 /**
81 * Save update data for a specific website.
82 *
83 * @param object $website Child site object.
84 * @param array $data Update data.
85 * @param string $type Update type.
86 *
87 * @return bool
88 */
89 public static function save_update_info( $website, $data, $type ) {
90
91 if ( empty( $website ) || empty( $website->id ) || ! is_array( $data ) ) {
92 return false;
93 }
94
95 $type = isset( self::MAPPED_TYPES[ $type ] )
96 ? self::MAPPED_TYPES[ $type ]
97 : null;
98
99 if ( empty( $type ) ) {
100 return false;
101 }
102
103 $save_items = array();
104
105 if ( in_array( $type, array( self::TYPE_PLUGIN, self::TYPE_THEME, self::TYPE_TRANSLATION ), true ) ) {
106
107 $updated_data = isset( $data['updated_data'] ) ? $data['updated_data'] : array();
108
109 if ( ! is_array( $updated_data ) ) {
110 $updated_data = array();
111 }
112
113 foreach ( $updated_data as $item ) {
114 $item = MainWP_Utility::instance()->sanitize_data( $item );
115 if ( ! empty( $item ) && isset( $item['name'] ) ) {
116 $save_items[] = $item;
117 }
118 }
119
120 if ( empty( $save_items ) ) {
121 return false;
122 }
123 } elseif ( self::TYPE_CORE === $type ) {
124 $item = MainWP_Utility::instance()->sanitize_data( $data );
125 $item['success'] = isset( $item['upgrade'] ) && 'SUCCESS' === $item['upgrade'] ? 1 : 0;
126 $save_items[] = $item;
127
128 } else {
129 return false;
130 }
131
132 $count_bulk = count( $save_items );
133
134 $dura = MainWP_Execution_Helper::get_run_time();
135
136 if ( $dura && $count_bulk > 0 ) {
137 $dura /= $count_bulk;
138 }
139
140 foreach ( $save_items as $item ) {
141 $item['site_id'] = $website->id;
142 if ( ! empty( $dura ) ) {
143 $item['duration'] = $dura;
144 }
145 if ( ! static::save( $type, $item ) ) {
146 return false;
147 }
148 }
149 return true;
150 }
151
152
153 /**
154 * Save an update items.
155 *
156 * @param string $type Update type.
157 * @param array $item Update data.
158 *
159 * @return bool
160 */
161 public static function save( $type, array $item ) {
162
163 if ( ! in_array( $type, self::get_types(), true ) ) {
164 return false;
165 }
166
167 $data = get_option( self::OPTION_NAME, array() );
168
169 if ( ! is_array( $data ) ) {
170 $data = array();
171 }
172
173 if ( ! isset( $data[ $type ] ) || ! is_array( $data[ $type ] ) ) {
174 $data[ $type ] = array();
175 }
176
177 // Append a single item.
178 $data[ $type ][] = $item;
179
180 return update_option( self::OPTION_NAME, $data, false );
181 }
182
183
184 /**
185 * Get update data.
186 *
187 * @param string|null $type Optional update type.
188 *
189 * @return array
190 */
191 public static function get( $type = null ) {
192
193 $data = get_option( self::OPTION_NAME, array() );
194
195 if ( ! is_array( $data ) ) {
196 return array();
197 }
198
199 // If no data for any type, return empty array.
200 if ( empty( $data[ self::TYPE_PLUGIN ] ) && empty( $data[ self::TYPE_THEME ] ) && empty( $data[ self::TYPE_CORE ] ) && empty( $data[ self::TYPE_TRANSLATION ] ) ) {
201 return array();
202 }
203
204 if ( null === $type ) {
205 return $data;
206 }
207
208 if ( ! in_array( $type, self::get_types(), true ) ) {
209 return array();
210 }
211
212 return isset( $data[ $type ] ) ? $data[ $type ] : array();
213 }
214
215
216 /**
217 * Send update info.
218 *
219 * @return void
220 */
221 public static function send_auto_update_info() {
222
223 $auto_updated_items = static::get_auto_update_notice_data_grouped_by_site();
224
225 if ( empty( $auto_updated_items ) ) {
226 return;
227 }
228
229 // dashboard admin email and default general email settings.
230 $admin_email_settings = MainWP_Notification_Settings::get_default_emails_fields( 'auto_updates', '', true ); // get default subject and heading only.
231 $admin_email_settings['disable'] = 0;
232 // general email and default admin email.
233 $admin_email_settings['recipients'] = MainWP_Notification_Settings::get_general_email(); // sent to general notification email only.
234
235 $heading = isset( $admin_email_settings['heading'] ) ? $admin_email_settings['heading'] : '';
236
237 $mail_content = MainWP_Notification_Template::instance()->get_template_html(
238 'emails/mainwp-auto-updates-notification-email.php',
239 array(
240 'auto_updated_items' => $auto_updated_items,
241 'heading' => $heading,
242 )
243 );
244
245 $plain_text = get_option( 'mainwp_daily_digest_plain_text', false );
246
247 $params = array(
248 'plain_text' => apply_filters( 'mainwp_send_email_text_format', $plain_text, 'auto_updates' ),
249 'mail_content' => $mail_content,
250 );
251
252 // send all individual daily digest to admin in one email.
253 MainWP_Notification::start_notification_auto_updates_info( $admin_email_settings, $params ); // will send email to general notification email.
254 }
255
256 /**
257 * Get websites to notice auto update.
258 *
259 * @return array
260 */
261 public static function get_auto_update_notice_data_grouped_by_site() { // phpcs:ignore -- NOSONAR - complexity.
262
263 $data = static::get();
264
265 if ( empty( $data ) ) {
266 return array();
267 }
268
269 $updated_data = array();
270 $sites_ids = array();
271 foreach ( $data as $type => $items ) {
272 if ( ! in_array( $type, self::get_types(), true ) ) {
273 continue;
274 }
275 if ( empty( $items ) || ! is_array( $items ) ) {
276 continue;
277 }
278 foreach ( $items as $item ) {
279 if ( ! isset( $item['site_id'] ) || empty( $item['site_id'] ) ) {
280 continue;
281 }
282 if ( empty( $item['success'] ) ) {
283 continue;
284 }
285 if ( ! isset( $updated_data[ $item['site_id'] ] ) ) {
286 $updated_data[ $item['site_id'] ] = array();
287 }
288 if ( ! isset( $updated_data[ $item['site_id'] ][ $type ] ) ) {
289 $updated_data[ $item['site_id'] ][ $type ] = array();
290 }
291 // add item to the site and type.
292 $updated_data[ $item['site_id'] ][ $type ][] = $item;
293 $sites_ids[ $item['site_id'] ] = $item['site_id'];
294 }
295 }
296
297 if ( ! empty( $sites_ids ) ) {
298 $websites = MainWP_DB::instance()->query(
299 MainWP_DB::instance()->get_sql_websites_for_current_user_by_params(
300 array(
301 'include' => $sites_ids,
302 'view' => 'base_view',
303 )
304 )
305 );
306 if ( ! empty( $websites ) && is_array( $websites ) ) {
307 foreach ( $websites as $website ) {
308 if ( isset( $updated_data[ $website->id ] ) ) {
309 $updated_data[ $website->id ]['site_name'] = $website->name;
310 $updated_data[ $website->id ]['site_url'] = $website->url;
311 $updated_data[ $website->id ]['site_id'] = $website->id;
312 }
313 }
314 }
315 }
316
317 // skip sites that don't have site_name (deleted or not accessible).
318 foreach ( $updated_data as $site_id => $site_data ) {
319 if ( ! isset( $site_data['site_name'] ) ) {
320 unset( $updated_data[ $site_id ] );
321 }
322 }
323 return $updated_data;
324 }
325 }
326