| 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 |
$actived_themes = MainWP_Utility::get_sub_array_having( $allThemes, 'active', 1 ); |
| 88 |
$actived_themes = MainWP_Utility::sortmulti( $actived_themes, 'name', 'asc' ); |
| 89 |
|
| 90 |
$inactive_themes = MainWP_Utility::get_sub_array_having( $allThemes, 'active', 0 ); |
| 91 |
$inactive_themes = MainWP_Utility::sortmulti( $inactive_themes, 'name', 'asc' ); |
| 92 |
|
| 93 |
?> |
| 94 |
<div class="ui grid"> |
| 95 |
<div class="twelve wide column"> |
| 96 |
<h3 class="ui header handle-drag"> |
| 97 |
<?php |
| 98 |
/** |
| 99 |
* Filter: mainwp_themes_widget_title |
| 100 |
* |
| 101 |
* Filters the Themes widget title text. |
| 102 |
* |
| 103 |
* @since 4.1 |
| 104 |
*/ |
| 105 |
echo esc_html( apply_filters( 'mainwp_themes_widget_title', __( 'Themes', 'mainwp' ), $website ) ); |
| 106 |
?> |
| 107 |
<div class="sub header"><?php esc_html_e( 'Installed themes on the child site', 'mainwp' ); ?></div> |
| 108 |
</h3> |
| 109 |
</div> |
| 110 |
<div class="four wide column right aligned"> |
| 111 |
<div class="ui dropdown right mainwp-dropdown-tab"> |
| 112 |
<div class="text"><?php esc_html_e( 'Active', 'mainwp' ); ?></div> |
| 113 |
<i class="dropdown icon"></i> |
| 114 |
<div class="menu"> |
| 115 |
<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> |
| 116 |
<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> |
| 117 |
</div> |
| 118 |
</div> |
| 119 |
</div> |
| 120 |
</div> |
| 121 |
<div class="ui section hidden divider"></div> |
| 122 |
<?php |
| 123 |
/** |
| 124 |
* Action: mainwp_themes_widget_top |
| 125 |
* |
| 126 |
* Fires at the top of the Themes widget on the Individual site overview page. |
| 127 |
* |
| 128 |
* @param object $website Object containing the child site info. |
| 129 |
* @param array $allThemes Array containing all detected themes data. |
| 130 |
* |
| 131 |
* @since 4.1 |
| 132 |
*/ |
| 133 |
do_action( 'mainwp_themes_widget_top', $website, $allThemes ); |
| 134 |
?> |
| 135 |
<div id="mainwp-widget-active-themes" class="ui tab active" data-tab="active_themes"> |
| 136 |
<?php |
| 137 |
/** |
| 138 |
* Action: mainwp_before_active_themes_list |
| 139 |
* |
| 140 |
* Fires before the active theme list in the Themes widget on the Individual site overview page. |
| 141 |
* |
| 142 |
* @param object $website Object containing the child site info. |
| 143 |
* @param array $actived_themes Array containing all active themes data. |
| 144 |
* |
| 145 |
* @since 4.1 |
| 146 |
*/ |
| 147 |
do_action( 'mainwp_before_active_themes_list', $website, $actived_themes ); |
| 148 |
?> |
| 149 |
<div class="ui divided selection list"> |
| 150 |
<?php |
| 151 |
$_count = count( $actived_themes ); |
| 152 |
for ( $i = 0; $i < $_count; $i ++ ) { |
| 153 |
$slug = $actived_themes[ $i ]['slug']; |
| 154 |
?> |
| 155 |
<div class="item"> |
| 156 |
<input class="themeSlug" type="hidden" name="slug" value="<?php echo esc_attr( wp_strip_all_tags( $actived_themes[ $i ]['slug'] ) ); ?>"/> |
| 157 |
<input class="websiteId" type="hidden" name="id" value="<?php echo esc_attr( $website->id ); ?>"/> |
| 158 |
<div class="right floated content themesAction"> |
| 159 |
<a href="#" disabled class="button ui mini grey basic" data-position="top right" data-tooltip="<?php esc_attr_e( 'Active theme cannot be deactivated. If you need to activate another theme, go to the list of inactive themes and activate the wanted theme.', 'mainwp' ); ?>" data-inverted=""><?php esc_html_e( 'Deactivate', 'mainwp' ); ?></a> |
| 160 |
</div> |
| 161 |
<div class="middle aligned content"> |
| 162 |
<?php echo esc_html( $actived_themes[ $i ]['name'] . ' ' . $actived_themes[ $i ]['version'] ); ?> |
| 163 |
</div> |
| 164 |
<div class="mainwp-row-actions-working"> |
| 165 |
<i class="ui active inline loader tiny"></i> <?php esc_html_e( 'Please wait...', 'mainwp' ); ?> |
| 166 |
</div> |
| 167 |
</div> |
| 168 |
<?php } ?> |
| 169 |
</div> |
| 170 |
<?php |
| 171 |
/** |
| 172 |
* Action: mainwp_after_active_themes_list |
| 173 |
* |
| 174 |
* Fires after the active themes list in the Themes widget on the Individual site overview page. |
| 175 |
* |
| 176 |
* @param object $website Object containing the child site info. |
| 177 |
* @param array $actived_themes Array containing all active themes data. |
| 178 |
* |
| 179 |
* @since 4.1 |
| 180 |
*/ |
| 181 |
do_action( 'mainwp_after_active_themes_list', $website, $actived_themes ); |
| 182 |
?> |
| 183 |
</div> |
| 184 |
<div id="mainwp-widget-inactive-themes" class="ui tab" data-tab="inactive_themes"> |
| 185 |
<?php |
| 186 |
/** |
| 187 |
* Action: mainwp_before_inactive_themes_list |
| 188 |
* |
| 189 |
* Fires before the inactive themes list in the Themes widget on the Individual site overview page. |
| 190 |
* |
| 191 |
* @param object $website Object containing the child site info. |
| 192 |
* @param array $inactive_themes Array containing all inactive themes data. |
| 193 |
* |
| 194 |
* @since 4.1 |
| 195 |
*/ |
| 196 |
do_action( 'mainwp_before_inactive_themes_list', $website, $inactive_themes ); |
| 197 |
?> |
| 198 |
<div class="ui divided selection list"> |
| 199 |
<?php |
| 200 |
$_count = count( $inactive_themes ); |
| 201 |
for ( $i = 0; $i < $_count; $i ++ ) { |
| 202 |
$slug = $inactive_themes[ $i ]['slug']; |
| 203 |
$is_parent = ( isset( $inactive_themes[ $i ]['parent_active'] ) && 1 == $inactive_themes[ $i ]['parent_active'] ) ? true : false; |
| 204 |
?> |
| 205 |
<div class="item"> |
| 206 |
<input class="themeName" type="hidden" name="slug" value="<?php echo esc_attr( wp_strip_all_tags( $inactive_themes[ $i ]['name'] ) ); ?>"/> |
| 207 |
<input class="themeSlug" type="hidden" name="slug" value="<?php echo esc_attr( wp_strip_all_tags( $inactive_themes[ $i ]['slug'] ) ); ?>"/> |
| 208 |
<input class="websiteId" type="hidden" name="id" value="<?php echo esc_attr( $website->id ); ?>"/> |
| 209 |
<div class="right floated content themesAction"> |
| 210 |
<?php if ( mainwp_current_user_have_right( 'dashboard', 'activate_deactivate_themes' ) ) { ?> |
| 211 |
<a href="#" class="mainwp-theme-activate ui mini green button" data-position="top right" 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> |
| 212 |
<?php } ?> |
| 213 |
<?php |
| 214 |
if ( mainwp_current_user_have_right( 'dashboard', 'delete_themes' ) ) { |
| 215 |
$parent_str = sprintf( __( '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'] : '' ); |
| 216 |
?> |
| 217 |
<a href="#" class="<?php echo $is_parent ? '' : 'mainwp-theme-delete'; ?> ui mini basic button" data-position="top right" 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; ?>" <?php echo $is_parent ? 'disabled onclick="javascript:void(0)"' : ''; ?> data-inverted=""><?php esc_html_e( 'Delete', 'mainwp' ); ?></a> |
| 218 |
<?php } ?> |
| 219 |
</div> |
| 220 |
<div class="middle aligned content"> |
| 221 |
<?php echo esc_html( $inactive_themes[ $i ]['name'] . ' ' . $inactive_themes[ $i ]['version'] ); ?> |
| 222 |
</div> |
| 223 |
<div class="mainwp-row-actions-working"> |
| 224 |
<i class="ui active inline loader tiny"></i> <?php esc_html_e( 'Please wait...', 'mainwp' ); ?> |
| 225 |
</div> |
| 226 |
</div> |
| 227 |
<?php } ?> |
| 228 |
</div> |
| 229 |
<?php |
| 230 |
/** |
| 231 |
* Action: mainwp_after_inactive_themes_list |
| 232 |
* |
| 233 |
* Fires after the inactive themes list in the Themes widget on the Individual site overview page. |
| 234 |
* |
| 235 |
* @param object $website Object containing the child site info. |
| 236 |
* @param array $inactive_themes Array containing all inactive themes data. |
| 237 |
* |
| 238 |
* @since 4.1 |
| 239 |
*/ |
| 240 |
do_action( 'mainwp_after_inactive_themes_list', $website, $inactive_themes ); |
| 241 |
?> |
| 242 |
</div> |
| 243 |
<?php |
| 244 |
/** |
| 245 |
* Action: mainwp_themes_widget_bottom |
| 246 |
* |
| 247 |
* Fires at the bottom of the Themes widget on the Individual site overview page. |
| 248 |
* |
| 249 |
* @param object $website Object containing the child site info. |
| 250 |
* @param array $allThemes Array containing all detected themes data. |
| 251 |
* |
| 252 |
* @since 4.1 |
| 253 |
*/ |
| 254 |
do_action( 'mainwp_themes_widget_bottom', $website, $allThemes ); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Method activate_theme() |
| 259 |
* |
| 260 |
* Fire off Action activate & display result |
| 261 |
*/ |
| 262 |
public static function activate_theme() { |
| 263 |
self::action( 'activate' ); |
| 264 |
die( wp_json_encode( array( 'result' => __( 'Theme has been activated!', 'mainwp' ) ) ) ); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Method delete_theme() |
| 269 |
* |
| 270 |
* Fire off action deactivate & display result |
| 271 |
*/ |
| 272 |
public static function delete_theme() { |
| 273 |
self::action( 'delete' ); |
| 274 |
die( wp_json_encode( array( 'result' => __( 'Theme has been permanently deleted!', 'mainwp' ) ) ) ); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Method action() |
| 279 |
* |
| 280 |
* Initiate try catch for chosen Action. |
| 281 |
* |
| 282 |
* @param mixed $action Theme Action. |
| 283 |
* |
| 284 |
* @throws \Exception Error message. |
| 285 |
* |
| 286 |
* @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id() |
| 287 |
* @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message() |
| 288 |
* @uses \MainWP\Dashboard\MainWP_Exception |
| 289 |
* @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website() |
| 290 |
* @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed() |
| 291 |
* @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website() |
| 292 |
*/ |
| 293 |
public static function action( $action ) { |
| 294 |
$theme = isset( $_POST['theme'] ) ? sanitize_text_field( wp_unslash( $_POST['theme'] ) ) : ''; |
| 295 |
$websiteId = isset( $_POST['websiteId'] ) ? intval( $_POST['websiteId'] ) : false; |
| 296 |
|
| 297 |
if ( empty( $theme ) || empty( $websiteId ) ) { |
| 298 |
die( wp_json_encode( array( 'error' => __( 'Theme or site ID not found. Please, reload the page and try again.', 'mainwp' ) ) ) ); |
| 299 |
} |
| 300 |
|
| 301 |
$website = MainWP_DB::instance()->get_website_by_id( $websiteId ); |
| 302 |
if ( ! MainWP_System_Utility::can_edit_website( $website ) ) { |
| 303 |
die( wp_json_encode( array( 'error' => __( 'You cannot edit this website.', 'mainwp' ) ) ) ); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Action: mainwp_before_theme_action |
| 308 |
* |
| 309 |
* Fires before theme activate/delete actions. |
| 310 |
* |
| 311 |
* @since 4.1 |
| 312 |
*/ |
| 313 |
do_action( 'mainwp_before_theme_action', $action, $theme, $website ); |
| 314 |
try { |
| 315 |
$information = MainWP_Connect::fetch_url_authed( |
| 316 |
$website, |
| 317 |
'theme_action', |
| 318 |
array( |
| 319 |
'action' => $action, |
| 320 |
'theme' => $theme, |
| 321 |
) |
| 322 |
); |
| 323 |
} catch ( MainWP_Exception $e ) { |
| 324 |
die( wp_json_encode( array( 'error' => MainWP_Error_Helper::get_error_message( $e ) ) ) ); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Action: mainwp_after_theme_action |
| 329 |
* |
| 330 |
* Fires after theme activate/delete actions. |
| 331 |
* |
| 332 |
* @since 4.1 |
| 333 |
*/ |
| 334 |
do_action( 'mainwp_after_theme_action', $information, $action, $theme, $website ); |
| 335 |
|
| 336 |
if ( ! isset( $information['status'] ) || ( 'SUCCESS' !== $information['status'] ) ) { |
| 337 |
die( wp_json_encode( array( 'error' => __( 'Unexpected error occurred. Please try again.', 'mainwp' ) ) ) ); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
} |
| 342 |
|