PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.3.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.3.2
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 5.0.3.2, at widgets/widget-mainwp-widget-themes.php

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