| 1300 | |
| 1301 | /** |
| 1302 | * Add suspend bulk action |
| 1303 | */ |
| 1304 | add_action( 'current_screen', 'wporg_themes_bulk_hooks' ); |
| 1305 | |
| 1306 | /** |
| 1307 | * Hooks to current_screen hook, so that we have the WP_Screen defined |
| 1308 | */ |
| 1309 | function wporg_themes_bulk_hooks() { |
| 1310 | add_filter( 'bulk_actions-edit-repopackage', 'wporg_themes_add_bulk_suspend_option' ); |
| 1311 | add_filter( 'handle_bulk_actions-edit-repopackage', 'wporg_themes_suspend_bulk_action_handler', 10, 3 ); |
| 1312 | add_action( 'admin_notices', 'wporg_themes_suspend_bulk_action_admin_notice' ); |
| 1313 | } |
| 1314 | |
| 1315 | /** |
| 1316 | * Add a new bulk action |
| 1317 | * |
| 1318 | * Add the suspend themes action |
| 1319 | * |
| 1320 | * @param array $bulk_actions Array of bulk actions. |
| 1321 | * @return array Updated array of bulk actions. |
| 1322 | */ |
| 1323 | function wporg_themes_add_bulk_suspend_option( $bulk_actions ) { |
| 1324 | $bulk_actions['suspend_themes'] = __( 'Suspend themes', 'wporg-themes' ); |
| 1325 | |
| 1326 | return $bulk_actions; |
| 1327 | } |
| 1328 | |
| 1329 | /** |
| 1330 | * Add the action handler for the suspend themes action |
| 1331 | * |
| 1332 | * @param string $redirect_to The redirect URL. |
| 1333 | * @param string $doaction The action being taken. |
| 1334 | * @param array $post_ids The post IDs to take the action on. |
| 1335 | * @return string Redirection URL after the action. |
| 1336 | */ |
| 1337 | function wporg_themes_suspend_bulk_action_handler( $redirect_to, $doaction, $post_ids ) { |
| 1338 | if ( $doaction !== 'suspend_themes' ) { |
| 1339 | return $redirect_to; |
| 1340 | } |
| 1341 | |
| 1342 | foreach ( $post_ids as $post_id ) { |
| 1343 | wp_update_post( |
| 1344 | array( |
| 1345 | 'ID' => $post_id, |
| 1346 | 'post_status' => 'suspended', |
| 1347 | ) |
| 1348 | ); |
| 1349 | } |
| 1350 | |
| 1351 | $redirect_to = add_query_arg( 'bulk_suspend_themes', count( $post_ids ), $redirect_to ); |
| 1352 | |
| 1353 | return $redirect_to; |
| 1354 | } |
| 1355 | |
| 1356 | /** |
| 1357 | * The admin notice after the suspend themes action happened. |
| 1358 | */ |
| 1359 | function wporg_themes_suspend_bulk_action_admin_notice() { |
| 1360 | if ( ! empty( $_REQUEST['bulk_suspend_themes'] ) ) { |
| 1361 | $suspended_themes_count = intval( $_REQUEST['bulk_suspend_themes'] ); |
| 1362 | |
| 1363 | printf( '<div id="message" class="updated notice is-dismissible"><p>' . |
| 1364 | _n( '%s theme is suspended.', |
| 1365 | '%s theme are suspended.', |
| 1366 | $suspended_themes_count, |
| 1367 | 'wporg-themes' |
| 1368 | ) . '</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __(' Dismiss this notice.', 'wporg-themes' ) . '</span></button></div>', |
| 1369 | $suspended_themes_count ); |
| 1370 | } |
| 1371 | } |