PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.1
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 / includes / rest-api / controller / version1 / class-mainwp-rest-api-v1-helper.php

class-mainwp-rest-api-v1-helper.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.1, at includes/rest-api/controller/version1/class-mainwp-rest-api-v1-helper.php

163 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP REST API Helper
4 *
5 * This class handles the REST API
6 *
7 * @package MainWP\Dashboard
8 * @author Martin Gibson
9 */
10
11 namespace MainWP\Dashboard;
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Class Rest_Api
20 */
21 class Rest_Api_V1_Helper { //phpcs:ignore -- NOSONAR - multi methods.
22
23 /**
24 * Protected static variable to hold the single instance of the class.
25 *
26 * @var mixed Default null
27 */
28 private static $instance = null;
29
30
31 /**
32 * Method instance()
33 *
34 * Create public static instance.
35 *
36 * @static
37 * @return static::$instance
38 */
39 public static function instance() {
40 if ( null === static::$instance ) {
41 static::$instance = new self();
42 }
43 return static::$instance;
44 }
45
46 /**
47 * Method handle_site_update_item()
48 *
49 * @param int $website_id website id.
50 * @param string $update_type result update.
51 * @param array $result result update.
52 *
53 * @return array result
54 */
55 public function handle_site_update_item( $website_id, $update_type, $result ) { //phpcs:ignore -- NOSONAR - complex.
56
57 $website = false;
58
59 if ( $website_id ) {
60 $website = MainWP_DB::instance()->get_website_by_id( $website_id, false, array( 'rollback_updates_data' ) ); // to fix loading premium_upgrades.
61 }
62
63 if ( $website && is_array( $result ) ) {
64
65 $return_results = array( 'updates_info' => array() );
66
67 // logging feature.
68 $_type = '';
69 if ( 'plugin' === $update_type || 'theme' === $update_type ) {
70 $_type = $update_type;
71 } elseif ( 'translation' === $update_type ) {
72 $_type = 'trans';
73 }
74
75 if ( isset( $result['upgrades_error'] ) ) {
76 foreach ( $result['upgrades_error'] as $k => $v ) {
77 $return_results['result_error'][ rawurlencode( $k ) ] = esc_html( $v );
78 $return_results['updates_info'][ rawurlencode( $k ) ] = array(
79 'success' => 0,
80 'error' => esc_html( $v ),
81 );
82 }
83 }
84
85 if ( ! empty( $_type ) && isset( $result['other_data'] ) ) { // ok.
86 $output_array = $result['other_data']; // updated_data: plugins,themes,trans.
87 mainwp_get_actions_handler_instance()->do_action_mainwp_install_actions( $website, 'updated', $output_array, $_type );
88 if ( is_array( $output_array ) ) {
89 $updated_data = isset( $output_array['updated_data'] ) ? $output_array['updated_data'] : array();
90 if ( is_array( $updated_data ) ) {
91
92 $saved_roll_items = ! empty( $website->rollback_updates_data ) ? json_decode( $website->rollback_updates_data, true ) : array();
93 if ( ! is_array( $saved_roll_items ) ) {
94 $saved_roll_items = array();
95 }
96
97 $update = false;
98 foreach ( $updated_data as $item ) {
99
100 if ( ! is_array( $item ) ) {
101 continue;
102 }
103
104 $success = ! empty( $item['success'] ) ? 1 : 0;
105 $slug = isset( $item['slug'] ) ? $item['slug'] : '';
106 $name = isset( $item['name'] ) ? $item['name'] : '';
107 $version = isset( $item['version'] ) ? $item['version'] : '';
108 $old_version = isset( $item['old_version'] ) ? $item['old_version'] : '';
109
110 if ( ! empty( $slug ) ) {
111 $return_results['updates_info'][ esc_html( $slug ) ] = array(
112 'success' => $success,
113 'name' => $name,
114 'old_version' => $old_version,
115 'version' => $version,
116 );
117 }
118
119 if ( ! empty( $item ) && ! empty( $slug ) && ! empty( $item['rollback'] ) ) {
120 $msg = MainWP_Updates_Helper::get_roll_msg( $item );
121 $return_results['result_error'][ rawurlencode( $slug ) ] = esc_html( $msg );
122
123 if ( ! empty( $version ) ) {
124 if ( ! isset( $saved_roll_items[ $_type ] ) ) {
125 $saved_roll_items[ $_type ] = array();
126 }
127
128 $saved_item = array(
129 'name' => $name,
130 'old_version' => $old_version,
131 'version' => $version,
132 'created' => time(),
133 );
134
135 if ( ! empty( $item['error'] ) ) {
136 $saved_item['error'] = $item['error'];
137 }
138
139 $saved_roll_items[ $_type ][ $slug ][ $version ] = $saved_item;
140 $update = true;
141 }
142 } elseif ( ! empty( $item ) && ! empty( $slug ) && ! empty( $success ) ) {
143 if ( ! empty( $version ) && isset( $saved_roll_items[ $_type ][ $slug ][ $version ] ) ) {
144 unset( $saved_roll_items[ $_type ][ $slug ][ $version ] );
145 $update = true;
146 }
147 }
148 }
149 if ( $update ) {
150 MainWP_DB::instance()->update_website_option( $website, 'rollback_updates_data', wp_json_encode( $saved_roll_items ) );
151 }
152 }
153 }
154 }
155
156 return $return_results;
157 }
158
159 return array();
160 }
161 }
162 // End of class.
163