PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / trunk
MainWP Dashboard: Self-hosted WordPress Management for Agencies vtrunk
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 / widgets / widget-mainwp-widget-themes.php

widget-mainwp-widget-themes.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies trunk, at widgets/widget-mainwp-widget-themes.php

325 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Themes Widget
4 *
5 * Grab current Child Site theme data & build Widget
6 *
7 * @package MainWP/Plugins
8 */
9
10 namespace MainWP\Dashboard;
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Class MainWP_Widget_Themes
19 */
20 class MainWP_Widget_Themes { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
21
22 /**
23 * Method get_class_name()
24 *
25 * Get Class Name
26 *
27 * @return string __CLASS__ Class Name.
28 */
29 public static function get_class_name() {
30 return __CLASS__;
31 }
32
33 /**
34 * Method render()
35 *
36 * Fire off render_widget().
37 */
38 public static function render() {
39 static::render_widget();
40 }
41
42
43 /**
44 * Method render_widget()
45 *
46 * Build themes widget.
47 *
48 * @uses \MainWP\Dashboard\MainWP_DB::query()
49 * @uses \MainWP\Dashboard\MainWP_DB::get_sql_website_by_id()
50 * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
51 * @uses \MainWP\Dashboard\MainWP_DB::free_result()
52 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_current_wpid()
53 */
54 public static function render_widget() {
55 $current_wpid = MainWP_System_Utility::get_current_wpid();
56 if ( empty( $current_wpid ) ) {
57 return;
58 }
59
60 $sql = MainWP_DB::instance()->get_sql_website_by_id( $current_wpid );
61 $websites = MainWP_DB::instance()->query( $sql );
62 $allThemes = array();
63 if ( $websites ) {
64 $website = MainWP_DB::fetch_object( $websites );
65 if ( $website && '' !== $website->themes ) {
66 $themes = json_decode( $website->themes, 1 );
67 if ( is_array( $themes ) && ! empty( $themes ) ) {
68 foreach ( $themes as $theme ) {
69 $allThemes[] = $theme;
70 }
71 }
72 }
73 MainWP_DB::free_result( $websites );
74 }
75
76 static::render_html_widget( $website, $allThemes );
77 }
78
79 /**
80 *
81 * Method render_html_widget().
82 *
83 * Render html themes widget for current site.
84 *
85 * @param object $website Object containing the child site info.
86 * @param array $allThemes Array containing all detected themes data.
87 *
88 * @uses \MainWP\Dashboard\MainWP_Utility::get_sub_array_having()
89 * @uses \MainWP\Dashboard\MainWP_Utility::sortmulti()
90 */
91 public static function render_html_widget( $website, $allThemes ) { // phpcs:ignore -- NOSONAR - complex.
92
93 $is_demo = MainWP_Demo_Handle::is_demo_mode();
94
95 $actived_themes = MainWP_Utility::get_sub_array_having( $allThemes, 'active', 1 );
96 $actived_themes = MainWP_Utility::sortmulti( $actived_themes, 'name', 'asc' );
97
98 $inactive_themes = MainWP_Utility::get_sub_array_having( $allThemes, 'active', 0 );
99 $inactive_themes = MainWP_Utility::sortmulti( $inactive_themes, 'name', 'asc' );
100
101 $wp_info = MainWP_DB::instance()->get_website_option( $website, 'site_info' );
102 $wp_info = ! empty( $wp_info ) ? json_decode( $wp_info, true ) : array();
103
104 if ( ! is_array( $wp_info ) ) {
105 $wp_info = array();
106 }
107
108 $use_tzformat = ! empty( $wp_info['format_datetime'] ) && is_array( $wp_info['format_datetime'] ) ? $wp_info['format_datetime'] : array();
109 $offs = ! empty( $use_tzformat['gmt_offset'] ) ? intval( $use_tzformat['gmt_offset'] ) : 0;
110 $offs_info = esc_html__( ' - Timezone:', 'mainwp' ) . ' UTC ' . ( $offs >= 0 ? '+' . $offs : $offs );
111 $site_info = $website->name . ' (' . $website->url . ') ' . ( ! empty( $offs_info ) ? $offs_info : '' );
112
113 ?>
114 <div class="mainwp-widget-header">
115 <h2 class="ui header handle-drag">
116 <?php
117 /**
118 * Filter: mainwp_themes_widget_title
119 *
120 * Filters the Themes widget title text.
121 *
122 * @since 4.1
123 */
124 echo esc_html( apply_filters( 'mainwp_themes_widget_title', esc_html__( 'Themes', 'mainwp' ), $website ) );
125 ?>
126 <div class="sub header"><?php esc_html_e( 'Inactive themes on the child site', 'mainwp' ); ?></div>
127 </h2>
128 </div>
129 <div class="mainwp-scrolly-overflow">
130 <?php
131 /**
132 * Action: mainwp_themes_widget_top
133 *
134 * Fires at the top of the Themes widget on the Individual site overview page.
135 *
136 * @param object $website Object containing the child site info.
137 * @param array $allThemes Array containing all detected themes data.
138 *
139 * @since 4.1
140 */
141 do_action( 'mainwp_themes_widget_top', $website, $allThemes );
142 ?>
143 <div id="mainwp-widget-inactive-themes" site-info="<?php echo esc_attr( $site_info ); ?>" site-id="<?php echo intval( $website->id ); ?>">
144 <?php
145 /**
146 * Action: mainwp_before_inactive_themes_list
147 *
148 * Fires before the inactive themes list in the Themes widget on the Individual site overview page.
149 *
150 * @param object $website Object containing the child site info.
151 * @param array $inactive_themes Array containing all inactive themes data.
152 *
153 * @since 4.1
154 */
155 do_action( 'mainwp_before_inactive_themes_list', $website, $inactive_themes );
156 ?>
157 <div class="ui divided list">
158 <?php
159 $_count = count( $inactive_themes );
160 for ( $i = 0; $i < $_count; $i++ ) {
161 $slug = $inactive_themes[ $i ]['slug'];
162 $is_parent = ( isset( $inactive_themes[ $i ]['parent_active'] ) && 1 === (int) $inactive_themes[ $i ]['parent_active'] ) ? true : false;
163 ?>
164 <div class="item row-manage-item">
165 <input class="themeName" type="hidden" name="name" value="<?php echo esc_attr( wp_strip_all_tags( $inactive_themes[ $i ]['name'] ) ); ?>"/>
166 <input class="themeSlug" type="hidden" name="slug" value="<?php echo esc_attr( wp_strip_all_tags( $inactive_themes[ $i ]['slug'] ) ); ?>"/>
167 <input class="websiteId" type="hidden" name="id" value="<?php echo esc_attr( $website->id ); ?>"/>
168 <div class="right floated content themesAction">
169 <div class="ui right pointing dropdown">
170 <i class="ellipsis vertical icon"></i>
171 <div class="menu">
172 <?php if ( \mainwp_current_user_can( 'dashboard', 'activate_deactivate_themes' ) ) : ?>
173 <a href="#" class="mainwp-theme-activate item <?php echo $is_demo ? 'disabled' : ''; ?>"><?php esc_html_e( 'Activate', 'mainwp' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></a>
174 <?php endif; ?>
175 <?php if ( \mainwp_current_user_can( 'dashboard', 'delete_themes' ) ) : ?>
176 <a href="#" class="<?php echo $is_parent ? '' : 'mainwp-theme-delete'; ?> item <?php echo $is_demo ? 'disabled' : ''; ?>" <?php echo $is_parent ? 'disabled onclick="javascript:void(0)"' : ''; ?>><?php esc_html_e( 'Delete', 'mainwp' ); ?></a>
177 <?php endif; ?>
178 <a href="#" history-view="widget-themes" class="mainwp-show-history item <?php echo $is_demo ? 'disabled' : ''; ?>"><?php esc_html_e( 'History', 'mainwp' ); ?></a>
179 </div>
180 </div>
181 </div>
182 <div class="middle aligned content">
183 <?php echo MainWP_System_Utility::get_theme_icon( $slug ); // phpcs:ignore WordPress.Security.EscapeOutput ?>&nbsp;&nbsp;&nbsp;&nbsp;
184 <?php echo esc_html( $inactive_themes[ $i ]['name'] . ' ' . $inactive_themes[ $i ]['version'] ); ?>
185 </div>
186 <div class="mainwp-row-actions-working">
187 <i class="ui active inline loader tiny"></i> <?php esc_html_e( 'Please wait...', 'mainwp' ); ?>
188 </div>
189 </div>
190 <?php } ?>
191 </div>
192 <?php
193 /**
194 * Action: mainwp_after_inactive_themes_list
195 *
196 * Fires after the inactive themes list in the Themes widget on the Individual site overview page.
197 *
198 * @param object $website Object containing the child site info.
199 * @param array $inactive_themes Array containing all inactive themes data.
200 *
201 * @since 4.1
202 */
203 do_action( 'mainwp_after_inactive_themes_list', $website, $inactive_themes );
204 ?>
205 </div>
206 </div>
207
208 <?php
209 /**
210 * Action: mainwp_themes_widget_bottom
211 *
212 * Fires at the bottom of the Themes widget on the Individual site overview page.
213 *
214 * @param object $website Object containing the child site info.
215 * @param array $allThemes Array containing all detected themes data.
216 *
217 * @since 4.1
218 */
219 do_action( 'mainwp_themes_widget_bottom', $website, $allThemes );
220 ?>
221 <div class="ui two column grid mainwp-widget-footer">
222 <div class="left aligned middle aligned column"></div>
223 <div class="right aligned middle aligned column">
224 <a href="admin.php?page=ThemesManage" class="ui mini basic button"><?php esc_html_e( 'Manage Themes', 'mainwp' ); ?></a>
225 </div>
226 </div>
227 <?php
228 }
229
230 /**
231 * Method activate_theme()
232 *
233 * Fire off Action activate & display result
234 */
235 public static function activate_theme() {
236 static::action( 'activate' );
237 die( wp_json_encode( array( 'result' => esc_html__( 'Theme has been activated!', 'mainwp' ) ) ) );
238 }
239
240 /**
241 * Method delete_theme()
242 *
243 * Fire off action deactivate & display result
244 */
245 public static function delete_theme() {
246 static::action( 'delete' );
247 die( wp_json_encode( array( 'result' => esc_html__( 'Theme has been permanently deleted!', 'mainwp' ) ) ) );
248 }
249
250 /**
251 * Method action()
252 *
253 * Initiate try catch for chosen Action.
254 *
255 * @param mixed $action Theme Action.
256 *
257 * @throws \MainWP_Exception Error message.
258 *
259 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
260 * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message()
261 * @uses \MainWP\Dashboard\MainWP_Exception
262 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
263 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
264 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
265 */
266 public static function action( $action ) {
267 $theme = isset( $_POST['theme'] ) ? sanitize_text_field( wp_unslash( $_POST['theme'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
268 $websiteId = isset( $_POST['websiteId'] ) ? intval( $_POST['websiteId'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
269
270 if ( empty( $theme ) || empty( $websiteId ) ) {
271 die( wp_json_encode( array( 'error' => esc_html__( 'Theme or site ID not found. Please, reload the page and try again.', 'mainwp' ) ) ) );
272 }
273
274 $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
275 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
276 die( wp_json_encode( array( 'error' => esc_html__( 'You cannot edit this website.', 'mainwp' ) ) ) );
277 }
278
279 if ( MainWP_System_Utility::is_suspended_site( $website ) ) {
280 die(
281 wp_json_encode(
282 array(
283 'error' => esc_html__( 'Suspended site.', 'mainwp' ),
284 'errorCode' => 'SUSPENDED_SITE',
285 )
286 )
287 );
288 }
289
290 /**
291 * Action: mainwp_before_theme_action
292 *
293 * Fires before theme activate/delete actions.
294 *
295 * @since 4.1
296 */
297 do_action( 'mainwp_before_theme_action', $action, $theme, $website );
298 try {
299 $information = MainWP_Connect::fetch_url_authed(
300 $website,
301 'theme_action',
302 array(
303 'action' => $action,
304 'theme' => $theme,
305 )
306 );
307 } catch ( MainWP_Exception $e ) {
308 die( wp_json_encode( array( 'error' => MainWP_Error_Helper::get_error_message( $e ) ) ) );
309 }
310
311 /**
312 * Action: mainwp_after_theme_action
313 *
314 * Fires after theme activate/delete actions.
315 *
316 * @since 4.1
317 */
318 do_action( 'mainwp_after_theme_action', $information, $action, $theme, $website );
319
320 if ( ! isset( $information['status'] ) || ( 'SUCCESS' !== $information['status'] ) ) {
321 die( wp_json_encode( array( 'error' => esc_html__( 'Unexpected error occurred. Please try again.', 'mainwp' ) ) ) );
322 }
323 }
324 }
325