PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.3
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.3
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-updates-helper.php

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

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