| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Plugins Widget |
| 4 |
* |
| 5 |
* Grab current Child Site plugin data & build Widget |
| 6 |
* |
| 7 |
* @package MainWP/Plugins |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MainWP\Dashboard; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class MainWP_Widget_Plugins |
| 14 |
*/ |
| 15 |
class MainWP_Widget_Plugins { |
| 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 |
* Method prepair_icons() |
| 38 |
* |
| 39 |
* Utilizes WP API to grab plugin icons, last_updated, active_installs |
| 40 |
*/ |
| 41 |
public function prepare_icons() { |
| 42 |
include ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 43 |
|
| 44 |
$args = array( |
| 45 |
'fields' => array( |
| 46 |
'last_updated' => true, |
| 47 |
'icons' => true, |
| 48 |
'active_installs' => true, |
| 49 |
), |
| 50 |
); |
| 51 |
|
| 52 |
$api = plugins_api( 'query_plugins', $args ); |
| 53 |
|
| 54 |
if ( is_wp_error( $api ) ) { |
| 55 |
$this->error = $api; |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$this->items = $api->plugins; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Method render_widget() |
| 65 |
* |
| 66 |
* Build Plugins Widget |
| 67 |
* |
| 68 |
* @uses \MainWP\Dashboard\MainWP_DB::query() |
| 69 |
* @uses \MainWP\Dashboard\MainWP_DB::get_sql_website_by_id() |
| 70 |
* @uses \MainWP\Dashboard\MainWP_DB::fetch_object() |
| 71 |
* @uses \MainWP\Dashboard\MainWP_DB::free_result() |
| 72 |
* @uses \MainWP\Dashboard\MainWP_System_Utility::get_current_wpid() |
| 73 |
*/ |
| 74 |
public static function render_widget() { |
| 75 |
$current_wpid = MainWP_System_Utility::get_current_wpid(); |
| 76 |
if ( empty( $current_wpid ) ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
$sql = MainWP_DB::instance()->get_sql_website_by_id( $current_wpid ); |
| 81 |
$websites = MainWP_DB::instance()->query( $sql ); |
| 82 |
$allPlugins = array(); |
| 83 |
if ( $websites ) { |
| 84 |
$website = MainWP_DB::fetch_object( $websites ); |
| 85 |
if ( $website && '' !== $website->plugins ) { |
| 86 |
$plugins = json_decode( $website->plugins, 1 ); |
| 87 |
if ( is_array( $plugins ) && 0 != count( $plugins ) ) { |
| 88 |
foreach ( $plugins as $plugin ) { |
| 89 |
if ( isset( $plugin['mainwp'] ) && ( 'T' === $plugin['mainwp'] ) ) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
$allPlugins[] = $plugin; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
MainWP_DB::free_result( $websites ); |
| 97 |
} |
| 98 |
|
| 99 |
self::render_html_widget( $website, $allPlugins ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Method render_html_widget(). |
| 104 |
* |
| 105 |
* Render HTML plugins widget for current site |
| 106 |
* |
| 107 |
* @param object $website Object containing the child site info. |
| 108 |
* @param array $allPlugins Array containing all detected plugins data. |
| 109 |
* |
| 110 |
* @uses \MainWP\Dashboard\MainWP_Utility::get_sub_array_having() |
| 111 |
* @uses \MainWP\Dashboard\MainWP_Utility::sortmulti() |
| 112 |
*/ |
| 113 |
public static function render_html_widget( $website, $allPlugins ) { |
| 114 |
|
| 115 |
$actived_plugins = MainWP_Utility::get_sub_array_having( $allPlugins, 'active', 1 ); |
| 116 |
$actived_plugins = MainWP_Utility::sortmulti( $actived_plugins, 'name', 'asc' ); |
| 117 |
|
| 118 |
$inactive_plugins = MainWP_Utility::get_sub_array_having( $allPlugins, 'active', 0 ); |
| 119 |
$inactive_plugins = MainWP_Utility::sortmulti( $inactive_plugins, 'name', 'asc' ); |
| 120 |
|
| 121 |
?> |
| 122 |
<div class="ui grid"> |
| 123 |
<div class="twelve wide column"> |
| 124 |
<h3 class="ui header handle-drag"> |
| 125 |
<?php |
| 126 |
/** |
| 127 |
* Filter: mainwp_plugins_widget_title |
| 128 |
* |
| 129 |
* Filters the Plugins widget title text. |
| 130 |
* |
| 131 |
* @param object $website Object containing the child site info. |
| 132 |
* |
| 133 |
* @since 4.1 |
| 134 |
*/ |
| 135 |
echo esc_html( apply_filters( 'mainwp_plugins_widget_title', esc_html__( 'Plugins', 'mainwp' ), $website ) ); |
| 136 |
?> |
| 137 |
<div class="sub header"><?php esc_html_e( 'Installed plugins on the child site', 'mainwp' ); ?></div> |
| 138 |
</h3> |
| 139 |
</div> |
| 140 |
<div class="four wide column right aligned"> |
| 141 |
<div class="ui dropdown right mainwp-dropdown-tab"> |
| 142 |
<div class="text"><?php esc_html_e( 'Active', 'mainwp' ); ?></div> |
| 143 |
<i class="dropdown icon"></i> |
| 144 |
<div class="menu"> |
| 145 |
<a class="item" data-tab="active_plugins" data-value="active_plugins" title="<?php esc_attr_e( 'Active', 'mainwp' ); ?>" href="#"><?php esc_html_e( 'Active', 'mainwp' ); ?></a> |
| 146 |
<a class="item" data-tab="inactive_plugins" data-value="inactive_plugins" title="<?php esc_attr_e( 'Inactive', 'mainwp' ); ?>" href="#"><?php esc_html_e( 'Inactive', 'mainwp' ); ?></a> |
| 147 |
</div> |
| 148 |
</div> |
| 149 |
</div> |
| 150 |
</div> |
| 151 |
<div class="ui section hidden divider"></div> |
| 152 |
<?php |
| 153 |
/** |
| 154 |
* Action: mainwp_plugins_widget_top |
| 155 |
* |
| 156 |
* Fires at the top of the Plugins widget on the Individual site overview page. |
| 157 |
* |
| 158 |
* @param object $website Object containing the child site info. |
| 159 |
* @param array $allPlugins Array containing all detected plugins data. |
| 160 |
* |
| 161 |
* @since 4.1 |
| 162 |
*/ |
| 163 |
do_action( 'mainwp_plugins_widget_top', $website, $allPlugins ); |
| 164 |
?> |
| 165 |
<div id="mainwp-widget-active-plugins" class="ui tab active" data-tab="active_plugins"> |
| 166 |
<?php |
| 167 |
/** |
| 168 |
* Action: mainwp_before_active_plugins_list |
| 169 |
* |
| 170 |
* Fires before the active plugins list in the Plugins widget on the Individual site overview page. |
| 171 |
* |
| 172 |
* @param object $website Object containing the child site info. |
| 173 |
* @param array $actived_plugins Array containing all active plugins data. |
| 174 |
* |
| 175 |
* @since 4.1 |
| 176 |
*/ |
| 177 |
do_action( 'mainwp_before_active_plugins_list', $website, $actived_plugins ); |
| 178 |
?> |
| 179 |
<div class="ui divided selection list"> |
| 180 |
<?php |
| 181 |
$_count = count( $actived_plugins ); |
| 182 |
for ( $i = 0; $i < $_count; $i ++ ) { |
| 183 |
$slug = wp_strip_all_tags( $actived_plugins[ $i ]['slug'] ); |
| 184 |
$plugin_directory = dirname( $slug ); |
| 185 |
?> |
| 186 |
<div class="item <?php echo esc_html( dirname( $slug ) ); ?>"> |
| 187 |
<input class="pluginSlug" type="hidden" name="slug" value="<?php echo esc_attr( wp_strip_all_tags( $actived_plugins[ $i ]['slug'] ) ); ?>"/> |
| 188 |
<input class="websiteId" type="hidden" name="id" value="<?php echo esc_attr( $website->id ); ?>"/> |
| 189 |
<div class="right floated pluginsAction"> |
| 190 |
<?php if ( mainwp_current_user_have_right( 'dashboard', 'activate_deactivate_plugins' ) ) { ?> |
| 191 |
<a href="#" class="mainwp-plugin-deactivate ui mini button green" data-position="top right" data-tooltip="<?php esc_attr_e( 'Deactivate the ', 'mainwp' ) . esc_html( $actived_plugins[ $i ]['name'] ) . esc_attr_e( ' plugin on the child site.', 'mainwp' ); ?>" data-inverted=""><?php esc_html_e( 'Deactivate', 'mainwp' ); ?></a> |
| 192 |
<?php } ?> |
| 193 |
</div> |
| 194 |
<div class="middle aligned content"> |
| 195 |
<?php echo MainWP_System_Utility::get_plugin_icon( $plugin_directory ); ?> |
| 196 |
<a href="<?php echo admin_url() . 'plugin-install.php?tab=plugin-information&wpplugin=' . intval( $website->id ) . '&plugin=' . dirname( wp_strip_all_tags( $actived_plugins[ $i ]['slug'] ) ); ?>" target="_blank" class="open-plugin-details-modal" title="More information about <?php echo wp_strip_all_tags( $actived_plugins[ $i ]['name'] ); ?>"> |
| 197 |
<?php echo esc_html( $actived_plugins[ $i ]['name'] . ' ' . $actived_plugins[ $i ]['version'] ); ?> |
| 198 |
</a> |
| 199 |
</div> |
| 200 |
<div class="mainwp-row-actions-working"> |
| 201 |
<i class="notched circle loading icon"></i> <?php esc_html_e( 'Please wait...', 'mainwp' ); ?> |
| 202 |
</div> |
| 203 |
</div> |
| 204 |
<?php } ?> |
| 205 |
</div> |
| 206 |
<?php |
| 207 |
/** |
| 208 |
* Action: mainwp_after_active_plugins_list |
| 209 |
* |
| 210 |
* Fires after the active plugins list in the Plugins widget on the Individual site overview page. |
| 211 |
* |
| 212 |
* @param object $website Object containing the child site info. |
| 213 |
* @param array $actived_plugins Array containing all active plugins data. |
| 214 |
* |
| 215 |
* @since 4.1 |
| 216 |
*/ |
| 217 |
do_action( 'mainwp_after_active_plugins_list', $website, $actived_plugins ); |
| 218 |
?> |
| 219 |
</div> |
| 220 |
<div id="mainwp-widget-inactive-plugins" class="ui tab" data-tab="inactive_plugins"> |
| 221 |
<?php |
| 222 |
/** |
| 223 |
* Action: mainwp_before_inactive_plugins_list |
| 224 |
* |
| 225 |
* Fires before the inactive plugins list in the Plugins widget on the Individual site overview page. |
| 226 |
* |
| 227 |
* @param object $website Object containing the child site info. |
| 228 |
* @param array $inactive_plugins Array containing all active plugins data. |
| 229 |
* |
| 230 |
* @since 4.1 |
| 231 |
*/ |
| 232 |
do_action( 'mainwp_before_inactive_plugins_list', $website, $inactive_plugins ); |
| 233 |
?> |
| 234 |
<div class="ui middle aligned divided selection list"> |
| 235 |
<?php |
| 236 |
$_count = count( $inactive_plugins ); |
| 237 |
for ( $i = 0; $i < $_count; $i ++ ) { |
| 238 |
$slug = $inactive_plugins[ $i ]['slug']; |
| 239 |
$plugin_directory = dirname( $slug ); |
| 240 |
?> |
| 241 |
<div class="item <?php echo esc_html( sanitize_text_field( dirname( $slug ) ) ); ?>"> |
| 242 |
<input class="pluginSlug" type="hidden" name="slug" value="<?php echo esc_attr( wp_strip_all_tags( $inactive_plugins[ $i ]['slug'] ) ); ?>"/> |
| 243 |
<input class="websiteId" type="hidden" name="id" value="<?php echo esc_attr( $website->id ); ?>"/> |
| 244 |
<div class="right floated content pluginsAction"> |
| 245 |
<?php if ( mainwp_current_user_have_right( 'dashboard', 'activate_deactivate_plugins' ) ) { ?> |
| 246 |
<a href="#" class="mainwp-plugin-activate ui mini green button" data-position="top right" data-tooltip="<?php esc_attr_e( 'Activate the ', 'mainwp' ) . wp_strip_all_tags( $inactive_plugins[ $i ]['name'] ) . esc_attr_e( ' plugin on the child site.', 'mainwp' ); ?>" data-inverted=""><?php esc_html_e( 'Activate', 'mainwp' ); ?></a> |
| 247 |
<?php } ?> |
| 248 |
<?php if ( mainwp_current_user_have_right( 'dashboard', 'delete_plugins' ) ) { ?> |
| 249 |
<a href="#" class="mainwp-plugin-delete ui mini basic button" data-position="top right" data-tooltip="<?php esc_attr_e( 'Delete the ', 'mainwp' ) . wp_strip_all_tags( $inactive_plugins[ $i ]['name'] ) . esc_attr_e( ' plugin from the child site.', 'mainwp' ); ?>" data-inverted=""><?php esc_html_e( 'Delete', 'mainwp' ); ?></a> |
| 250 |
<?php } ?> |
| 251 |
</div> |
| 252 |
<div class="middle aligned content"> |
| 253 |
<?php echo MainWP_System_Utility::get_plugin_icon( $plugin_directory ); ?> |
| 254 |
<a href="<?php echo admin_url() . 'plugin-install.php?tab=plugin-information&wpplugin=' . intval( $website->id ) . '&plugin=' . dirname( $inactive_plugins[ $i ]['slug'] ); ?>" target="_blank" class="open-plugin-details-modal" title="More information about <?php echo wp_strip_all_tags( $inactive_plugins[ $i ]['name'] ); ?>"> |
| 255 |
<?php echo esc_html( $inactive_plugins[ $i ]['name'] . ' ' . $inactive_plugins[ $i ]['version'] ); ?> |
| 256 |
</a> |
| 257 |
</div> |
| 258 |
<div class="mainwp-row-actions-working"> |
| 259 |
<i class="ui active inline loader tiny"></i> <?php esc_html_e( 'Please wait...', 'mainwp' ); ?> |
| 260 |
</div> |
| 261 |
</div> |
| 262 |
<?php } ?> |
| 263 |
</div> |
| 264 |
<?php |
| 265 |
/** |
| 266 |
* Action: mainwp_after_inactive_plugins_list |
| 267 |
* |
| 268 |
* Fires after the inactive plugins list in the Plugins widget on the Individual site overview page. |
| 269 |
* |
| 270 |
* @param object $website Object containing the child site info. |
| 271 |
* @param array $inactive_plugins Array containing all active plugins data. |
| 272 |
* |
| 273 |
* @since 4.1 |
| 274 |
*/ |
| 275 |
do_action( 'mainwp_after_inactive_plugins_list', $website, $inactive_plugins ); |
| 276 |
?> |
| 277 |
</div> |
| 278 |
<?php |
| 279 |
/** |
| 280 |
* Action: mainwp_plugins_widget_bottom |
| 281 |
* |
| 282 |
* Fires at the bottom of the Plugins widget on the Individual site overview page. |
| 283 |
* |
| 284 |
* @param object $website Object containing the child site info. |
| 285 |
* @param array $allPlugins Array containing all detected plugins data. |
| 286 |
* |
| 287 |
* @since 4.1 |
| 288 |
*/ |
| 289 |
do_action( 'mainwp_plugins_widget_bottom', $website, $allPlugins ); |
| 290 |
MainWP_Updates::render_plugin_details_modal(); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Method activate_plugin() |
| 295 |
* |
| 296 |
* Fire off Action activate & display result |
| 297 |
*/ |
| 298 |
public static function activate_plugin() { |
| 299 |
self::action( 'activate' ); |
| 300 |
die( wp_json_encode( array( 'result' => esc_html__( 'Plugin has been activated!', 'mainwp' ) ) ) ); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Method deactivate_plugin() |
| 305 |
* |
| 306 |
* Fire off action deactivate & display result |
| 307 |
*/ |
| 308 |
public static function deactivate_plugin() { |
| 309 |
self::action( 'deactivate' ); |
| 310 |
die( wp_json_encode( array( 'result' => esc_html__( 'Plugin has been deactivated!', 'mainwp' ) ) ) ); |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Method delete_plugin() |
| 315 |
* |
| 316 |
* Fire off action delete & display result |
| 317 |
*/ |
| 318 |
public static function delete_plugin() { |
| 319 |
self::action( 'delete' ); |
| 320 |
die( wp_json_encode( array( 'result' => esc_html__( 'Plugin has been permanently deleted!', 'mainwp' ) ) ) ); |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Method action() |
| 325 |
* |
| 326 |
* Initiate try catch for chosen Action |
| 327 |
* |
| 328 |
* @param mixed $action Plugin Action. |
| 329 |
* |
| 330 |
* @throws \Exception Error message. |
| 331 |
* |
| 332 |
* @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id() |
| 333 |
* @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message() |
| 334 |
* @uses \MainWP\Dashboard\MainWP_Exception |
| 335 |
* @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed() |
| 336 |
* @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website() |
| 337 |
*/ |
| 338 |
public static function action( $action ) { |
| 339 |
$plugin = isset( $_POST['plugin'] ) ? wp_unslash( $_POST['plugin'] ) : ''; |
| 340 |
$plugin = urldecode( $plugin ); |
| 341 |
$websiteId = isset( $_POST['websiteId'] ) ? intval( $_POST['websiteId'] ) : false; |
| 342 |
|
| 343 |
if ( empty( $plugin ) || empty( $websiteId ) ) { |
| 344 |
die( wp_json_encode( array( 'error' => esc_html__( 'Plugin or site ID not found. Please, reload the page and try again.', 'mainwp' ) ) ) ); |
| 345 |
} |
| 346 |
|
| 347 |
$website = MainWP_DB::instance()->get_website_by_id( $websiteId ); |
| 348 |
if ( ! MainWP_System_Utility::can_edit_website( $website ) ) { |
| 349 |
die( wp_json_encode( array( 'error' => esc_html__( 'You cannot edit this website.', 'mainwp' ) ) ) ); |
| 350 |
} |
| 351 |
|
| 352 |
if ( MainWP_System_Utility::is_suspended_site( $website ) ) { |
| 353 |
die( |
| 354 |
wp_json_encode( |
| 355 |
array( |
| 356 |
'error' => esc_html__( 'Suspended site.', 'mainwp' ), |
| 357 |
'errorCode' => 'SUSPENDED_SITE', |
| 358 |
) |
| 359 |
) |
| 360 |
); |
| 361 |
} |
| 362 |
|
| 363 |
try { |
| 364 |
|
| 365 |
/** |
| 366 |
* Action: mainwp_before_plugin_action |
| 367 |
* |
| 368 |
* Fires before plugin activate/deactivate/delete actions. |
| 369 |
* |
| 370 |
* @since 4.1 |
| 371 |
*/ |
| 372 |
do_action( 'mainwp_before_plugin_action', $action, $plugin, $website ); |
| 373 |
|
| 374 |
$information = MainWP_Connect::fetch_url_authed( |
| 375 |
$website, |
| 376 |
'plugin_action', |
| 377 |
array( |
| 378 |
'action' => $action, |
| 379 |
'plugin' => $plugin, |
| 380 |
) |
| 381 |
); |
| 382 |
|
| 383 |
/** |
| 384 |
* Action: mainwp_after_plugin_action |
| 385 |
* |
| 386 |
* Fires after plugin activate/deactivate/delete actions. |
| 387 |
* |
| 388 |
* @since 4.1 |
| 389 |
*/ |
| 390 |
do_action( 'mainwp_after_plugin_action', $information, $action, $plugin, $website ); |
| 391 |
|
| 392 |
} catch ( MainWP_Exception $e ) { |
| 393 |
die( wp_json_encode( array( 'error' => MainWP_Error_Helper::get_error_message( $e ) ) ) ); |
| 394 |
} |
| 395 |
|
| 396 |
if ( ! isset( $information['status'] ) || ( 'SUCCESS' !== $information['status'] ) ) { |
| 397 |
die( wp_json_encode( array( 'error' => esc_html__( 'Unexpected error occurred. Please try again.', 'mainwp' ) ) ) ); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
} |
| 402 |
|