PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.4
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.4
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 / modules / logs / connectors / class-connector-installer.php

class-connector-installer.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.4, at modules/logs/connectors/class-connector-installer.php

365 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Module Logs Installer connector class.
4 *
5 * @package MainWP\Dashboard
6 * @version 4.5.1
7 */
8
9 namespace MainWP\Dashboard\Module\Log;
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Class Connector_Installer
15 *
16 * @package MainWP\Dashboard
17 */
18 class Connector_Installer extends Log_Connector {
19
20 /**
21 * Connector name.
22 *
23 * @var string Connector slug.
24 * */
25 public $name = 'installer';
26
27 /**
28 * Actions names.
29 *
30 * @var array Actions registered for this connector.
31 * */
32 public $actions = array(
33 'mainwp_install_update_actions',
34 'mainwp_install_plugin_action', // call child function: plugin_action.
35 'mainwp_install_theme_action', // call child function: theme_action.
36 );
37
38
39 /**
40 * Return translated connector label.
41 *
42 * @return string Translated connector label.
43 */
44 public function get_label() {
45 return esc_html__( 'Installer', 'mainwp' );
46 }
47
48 /**
49 * Return translated action labels.
50 *
51 * @return array Action label translations.
52 */
53 public function get_action_labels() {
54 return array(
55 'install' => esc_html__( 'Installed', 'mainwp' ),
56 'activate' => esc_html__( 'Activated', 'mainwp' ),
57 'deactivate' => esc_html__( 'Deactivated', 'mainwp' ),
58 'delete' => esc_html__( 'Deleted', 'mainwp' ),
59 'updated' => esc_html__( 'Updated', 'mainwp' ),
60 );
61 }
62
63 /**
64 * Return translated context labels.
65 *
66 * @return array Context label translations.
67 */
68 public function get_context_labels() {
69 return array(
70 'plugins' => esc_html__( 'Plugins', 'mainwp' ),
71 'themes' => esc_html__( 'Themes', 'mainwp' ),
72 'core' => esc_html__( 'Core', 'mainwp' ),
73 'translation' => esc_html__( 'Translation', 'mainwp' ),
74 );
75 }
76
77 /**
78 * Log plugin|theme installations.
79 *
80 * @action mainwp_install_update_actions.
81 *
82 * @param array $website website.
83 * @param string $pAction install action.
84 * @param array $data data result.
85 * @param string $type action type.
86 * @param mixed $post_data post data (option).
87 * @param bool $upload true|false install by upload.
88 */
89 public function callback_mainwp_install_update_actions( $website, $pAction, $data, $type, $post_data = array(), $upload = false ) { //phpcs:ignore -- NOSONAR - complex method.
90
91 if ( empty( $website ) || empty( $pAction ) || ! is_string( $pAction ) || ! is_array( $data ) ) {
92 return;
93 }
94
95 if ( ! in_array( $pAction, array( 'install', 'updated' ), true ) ) {
96 return;
97 }
98
99 $context = '';
100
101 if ( 'plugin' === $type ) {
102 $context = 'plugin';
103 } elseif ( 'theme' === $type ) {
104 $context = 'theme';
105 } elseif ( 'trans' === $type ) {
106 $context = 'translation';
107 } elseif ( 'core' === $type ) {
108 $context = 'core';
109 } else {
110 return;
111 }
112
113 $logs_args = array();
114
115 $action = $pAction;
116
117 $args = array();
118
119 if ( 'install' === $action ) {
120
121 $installed_items = isset( $data['install_items'] ) ? $data['install_items'] : array();
122
123 if ( ! is_array( $installed_items ) ) {
124 $installed_items = array();
125 }
126
127 if ( empty( $installed_items ) ) {
128 return false;
129 }
130
131 foreach ( $installed_items as $item ) {
132 $item = $this->sanitize_data( $item );
133 if ( ! empty( $item ) && isset( $item['name'] ) ) {
134 $args = array(
135 'name' => $item['name'],
136 'version' => $item['version'],
137 'upload' => $upload ? 1 : 0,
138 'extra_info' => $item,
139 );
140 $logs_args[] = $args;
141 }
142 }
143
144 if ( empty( $logs_args ) ) {
145 return false;
146 }
147
148 $message = esc_html_x(
149 '%1$s',
150 'Plugin/theme installation. 1: Plugins/themes namess, 2: vesion',
151 'mainwp'
152 );
153
154 } elseif ( 'updated' === $action && ( in_array( $type, array( 'plugin', 'theme', 'trans' ) ) ) ) {
155
156 $updated_data = isset( $data['updated_data'] ) ? $data['updated_data'] : array();
157
158 if ( ! is_array( $updated_data ) ) {
159 $updated_data = array();
160 }
161
162 $logs_args = array();
163
164 foreach ( $updated_data as $item ) {
165 $item = $this->sanitize_data( $item );
166 if ( ! empty( $item ) && isset( $item['name'] ) ) {
167 $args = array(
168 'name' => $item['name'],
169 );
170 if ( isset( $item['version'] ) ) {
171 $args['version'] = $item['version'];
172 }
173 $save_info = $item;
174 if ( ! empty( $item['rollback'] ) ) {
175 $save_info['rollback_info'] = $item;
176 }
177 $args['extra_info'] = $save_info;
178 $logs_args[] = $args;
179 }
180 }
181
182 if ( empty( $logs_args ) ) {
183 return false;
184 }
185
186 if ( 'plugin' === $type || 'theme' === $type || 'trans' === $type ) {
187 $message = esc_html_x(
188 '%1$s',
189 'Update. 1: name',
190 'mainwp'
191 );
192 } else {
193 return;
194 }
195 } elseif ( 'updated' === $action && 'core' === $type ) {
196 $message = esc_html__( '%1$s', 'mainwp' );
197 $args = array(
198 'name' => 'WordPress', // label.
199 'version' => isset( $data['version'] ) ? $data['version'] : '',
200 'old_version' => isset( $data['old_version'] ) ? $data['old_version'] : '',
201 'error' => isset( $data['error'] ) ? $data['error'] : '',
202 );
203 $logs_args[] = $args;
204
205 } else {
206 return false;
207 }
208
209 $count_bulk = count( $logs_args );
210 foreach ( $logs_args as $args ) {
211 $args['duration_bulk'] = $count_bulk;
212
213 $state = 1;
214 if ( isset( $args['extra_info'] ) && is_array( $args['extra_info'] ) ) {
215 if ( isset( $args['extra_info']['success'] ) ) {
216 $state = ! empty( $args['extra_info']['success'] ) ? 1 : 0;
217 }
218 $args['extra_info'] = wp_json_encode( $args['extra_info'] );
219 }
220 $this->log(
221 $message,
222 $args,
223 $website->id,
224 $context,
225 $action,
226 $state
227 );
228 }
229
230 return true;
231 }
232
233 /**
234 * Log plugin actions.
235 *
236 * @action mainwp_install_plugin_action.
237 *
238 * @param array $website website.
239 * @param string $plugin_act plugin action: activate, deactivate, delete.
240 * @param array $params params array.
241 * @param array $action_data response data.
242 */
243 public function callback_mainwp_install_plugin_action( $website, $plugin_act, $params, $action_data ) {
244
245 if ( empty( $website ) || ! is_object( $website ) || empty( $website->id ) || empty( $plugin_act ) || ! is_string( $plugin_act ) || ! is_array( $params ) ) {
246 return;
247 }
248
249 if ( ! in_array( $plugin_act, array( 'activate', 'delete', 'deactivate' ), true ) ) {
250 return;
251 }
252
253 if ( ! is_array( $action_data ) ) {
254 return;
255 }
256
257 $logs_args = array();
258
259 foreach ( $action_data as $item ) {
260 $item = $this->sanitize_data( $item );
261 if ( ! empty( $item ) && isset( $item['name'] ) ) {
262 $logs_args[] = array(
263 'name' => $item['name'],
264 'version' => $item['version'],
265 'slug' => $item['slug'],
266 );
267 }
268 }
269
270 if ( empty( $logs_args ) ) {
271 return;
272 }
273
274 $action = $plugin_act;
275 $context = 'plugin';
276
277 $message = esc_html_x(
278 '%1$s',
279 '1: Plugin name, 2: plugin version',
280 'mainwp'
281 );
282
283 $state = 1;
284
285 $count_bulk = count( $logs_args );
286 foreach ( $logs_args as $args ) {
287 $args['duration_bulk'] = $count_bulk;
288 $this->log(
289 $message,
290 $args,
291 $website->id,
292 $context,
293 $action,
294 $state
295 );
296 }
297 }
298
299 /**
300 * Log themes actions.
301 *
302 * @action mainwp_install_theme_action.
303 *
304 * @param array $website website.
305 * @param string $theme_act theme action: activate, delete.
306 * @param array $params params array.
307 * @param array $action_data response data.
308 */
309 public function callback_mainwp_install_theme_action( $website, $theme_act, $params, $action_data ) {
310
311 if ( empty( $website ) || ! is_object( $website ) || empty( $website->id ) || empty( $theme_act ) || ! is_string( $theme_act ) || ! is_array( $params ) ) {
312 return;
313 }
314
315 if ( ! in_array( $theme_act, array( 'activate', 'delete', 'deactivate' ), true ) ) {
316 return;
317 }
318
319 if ( ! is_array( $action_data ) ) {
320 return;
321 }
322
323 $logs_args = array();
324
325 foreach ( $action_data as $item ) {
326 $item = $this->sanitize_data( $item );
327 if ( ! empty( $item ) && isset( $item['name'] ) ) {
328 $logs_args[] = array(
329 'name' => $item['name'],
330 'version' => $item['version'],
331 'slug' => $item['slug'],
332 );
333 }
334 }
335
336 if ( empty( $logs_args ) ) {
337 return;
338 }
339
340 $action = $theme_act;
341 $context = 'theme';
342
343 $message = esc_html_x(
344 '%1$s',
345 '1: Theme name, 2: Theme version',
346 'mainwp'
347 );
348
349 $state = 1;
350
351 $count_bulk = count( $logs_args );
352 foreach ( $logs_args as $args ) {
353 $args['duration_bulk'] = $count_bulk;
354 $this->log(
355 $message,
356 $args,
357 $website->id,
358 $context,
359 $action,
360 $state
361 );
362 }
363 }
364 }
365