| 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-post', 'wporg_themes_add_bulk_suspend_option' ); |
| 1311 | add_filter( 'handle_bulk_actions-edit-post', '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 | function wporg_themes_suspend_bulk_action_handler( $redirect_to, $doaction, $post_ids ) { |
| 1330 | if ( $doaction !== 'suspend_themes' ) { |
| 1331 | return $redirect_to; |
| 1332 | } |
| 1333 | |
| 1334 | foreach ( $post_ids as $post_id ) { |
| 1335 | wp_update_post( |
| 1336 | array( |
| 1337 | 'ID' => $post_id, |
| 1338 | 'post_status' => 'suspended', |
| 1339 | ) |
| 1340 | ); |
| 1341 | } |
| 1342 | |
| 1343 | $redirect_to = add_query_arg( 'bulk_suspend_themes', count( $post_ids ), $redirect_to ); |
| 1344 | |
| 1345 | return $redirect_to; |
| 1346 | } |
| 1347 | |
| 1348 | function wporg_themes_suspend_bulk_action_admin_notice() { |
| 1349 | if ( ! empty( $_REQUEST['bulk_suspend_themes'] ) ) { |
| 1350 | $suspended_themes_count = intval( $_REQUEST['bulk_suspend_themes'] ); |
| 1351 | |
| 1352 | printf( '<div id="message" class="updated fade">' . |
| 1353 | _n( '%s theme is suspended.', |
| 1354 | '%s theme are suspended.', |
| 1355 | $suspended_themes_count, |
| 1356 | 'wporg-themes' |
| 1357 | ) . '</div>', $suspended_themes_count ); |
| 1358 | } |
| 1359 | } |