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 / class / class-mainwp-updates-helper.php

class-mainwp-updates-helper.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.1, at class/class-mainwp-updates-helper.php

197 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Updates 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_Updates_Helper
17 *
18 * @package MainWP\Dashboard
19 */
20 class MainWP_Updates_Helper { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
21
22 /**
23 * Private static variable to hold the single instance of the class.
24 *
25 * @static
26 *
27 * @var mixed Default null
28 */
29 private static $instance = null;
30
31 /**
32 * Gets Class Name.
33 *
34 * @return object
35 */
36 public static function get_class_name() {
37 return __CLASS__;
38 }
39
40
41 /**
42 * Method instance()
43 *
44 * Create public static instance.
45 *
46 * @static
47 * @return MainWP_Updates_Helper
48 */
49 public static function instance() {
50 if ( null === static::$instance ) {
51 static::$instance = new self();
52 }
53
54 return static::$instance;
55 }
56
57 /**
58 * Method get_roll_items_updates_of_site()
59 *
60 * @param object $website data.
61 *
62 * @return array roll items data.
63 */
64 public function get_roll_items_updates_of_site( $website ) { //phpcs:ignore -- NOSONAR - complex.
65
66 if ( ! is_object( $website ) ) {
67 return array();
68 }
69
70 if ( property_exists( $website, 'rollback_updates_data' ) ) {
71 $saved_roll_items = ! empty( $website->rollback_updates_data ) ? json_decode( $website->rollback_updates_data, true ) : array();
72 } else {
73 $site_opts = MainWP_DB::instance()->get_website_options_array( $website, array( 'rollback_updates_data' ) );
74 $saved_roll_items = is_array( $site_opts ) && ! empty( $site_opts['rollback_updates_data'] ) ? json_decode( $site_opts['rollback_updates_data'], true ) : array();
75 }
76
77 if ( ! is_array( $saved_roll_items ) || empty( $saved_roll_items ) ) {
78 return array();
79 }
80
81 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
82 $theme_upgrades = json_decode( $website->theme_upgrades, true );
83
84 $roll_plugins = array();
85 $roll_themes = array();
86
87 if ( ! empty( $plugin_upgrades ) || ! empty( $theme_upgrades ) ) {
88 $saved_roll_items = ! empty( $website->rollback_updates_data ) ? json_decode( $website->rollback_updates_data, true ) : array();
89 if ( is_array( $saved_roll_items ) ) {
90
91 if ( ! empty( $plugin_upgrades ) && isset( $saved_roll_items['plugin'] ) && is_array( $saved_roll_items['plugin'] ) ) {
92 foreach ( $plugin_upgrades as $slug => $item ) {
93 $last_version = $item['update']['new_version'];
94
95 if ( isset( $saved_roll_items['plugin'][ $slug ][ $last_version ] ) ) {
96 $roll_plugins[ $slug ] = $saved_roll_items['plugin'][ $slug ][ $last_version ];
97 }
98 }
99 }
100
101 if ( ! empty( $theme_upgrades ) && isset( $saved_roll_items['theme'] ) && is_array( $saved_roll_items['theme'] ) ) {
102 foreach ( $saved_roll_items['theme'] as $item ) {
103 foreach ( $theme_upgrades as $slug => $item ) {
104 $last_version = $item['update']['new_version'];
105
106 if ( isset( $saved_roll_items['theme'][ $slug ][ $last_version ] ) ) {
107 $roll_themes[ $slug ] = $saved_roll_items['theme'][ $slug ][ $last_version ];
108 }
109 }
110 }
111 }
112 }
113 }
114
115 $data = array();
116
117 if ( ! empty( $roll_plugins ) ) {
118 $data['plugins'] = $roll_plugins;
119 }
120 if ( ! empty( $roll_themes ) ) {
121 $data['themes'] = $roll_themes;
122 }
123 return $data;
124 }
125
126 /**
127 * Method get_roll_msg().
128 *
129 * @param array $item update item.
130 * @param bool $with_icon get msg with icon.
131 * @param string $msg_type default|notice.
132 *
133 * @return string icons.
134 */
135 public static function get_roll_msg( $item = array(), $with_icon = false, $msg_type = 'default' ) {
136
137 if ( 'notice' === $msg_type ) {
138 $msg = __( 'This version failed a previous update and was rolled back. Proceed with caution.', 'mainwp' );
139 } else {
140 if ( ! is_array( $item ) || empty( $item['name'] ) ) {
141 return '';
142 }
143 $name = isset( $item['name'] ) ? $item['name'] : '';
144 $old_version = isset( $item['old_version'] ) ? $item['old_version'] : '';
145 $version = isset( $item['version'] ) ? $item['version'] : '';
146 // translators: 1: Plugin/theme name, 2: Version rolled back from, 3: Version rolled back to.
147 $msg = sprintf( __( 'WordPress detected an error with %1$s and rolled it back from version %2$s to version %3$s to ensure site stability.', 'mainwp' ), $name, $version, $old_version );
148 }
149
150 if ( ! $with_icon ) {
151 return $msg;
152 }
153
154 return static::get_roll_icon( $msg );
155 }
156
157 /**
158 * Method get_roll_icon().
159 *
160 * @param string $ttip tooltip text.
161 * @param bool $icon_only get icon only.
162 *
163 * @return string icons.
164 */
165 public static function get_roll_icon( $ttip = '', $icon_only = false ) {
166 $icon = '<i class="orange undo icon"></i>';
167 if ( $icon_only ) {
168 return $icon;
169 }
170 return ' <span data-inverted="" data-position="right center" data-tooltip="' . esc_html( $ttip ) . '">' . $icon . '</span>';
171 }
172
173
174 /**
175 * Method get_roll_update_plugintheme_items().
176 *
177 * @param string $item_type plugin|theme.
178 * @param array $roll_data roll update data.
179 *
180 * @return array roll items.
181 */
182 public static function get_roll_update_plugintheme_items( $item_type, $roll_data ) {
183
184 $rollItems = ! empty( $roll_data ) ? json_decode( $roll_data, true ) : array();
185
186 if ( is_array( $rollItems ) ) {
187 if ( 'plugin' === $item_type ) {
188 return ! empty( $rollItems['plugin'] ) && is_array( $rollItems['plugin'] ) ? $rollItems['plugin'] : array();
189 } elseif ( 'theme' === $item_type ) {
190 return ! empty( $rollItems['theme'] ) && is_array( $rollItems['theme'] ) ? $rollItems['theme'] : array();
191 }
192 }
193
194 return array();
195 }
196 }
197