Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Draft Bulk Action #23

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions php/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,29 +417,19 @@ public function add_admin_menu() {
__( 'Patterns', 'dlx-pattern-wrangler' ),
__( 'Patterns', 'dlx-pattern-wrangler' ),
'manage_options',
'dlx-pattern-wrangler',
array( $this, 'admin_page' ),
'edit.php?post_type=wp_block',
'',
'dashicons-layout',
4
6
);

add_submenu_page(
'dlx-pattern-wrangler',
'edit.php?post_type=wp_block',
__( 'Settings', 'dlx-pattern-wrangler' ),
__( 'Settings', 'dlx-pattern-wrangler' ),
'manage_options',
'edit_posts',
'dlx-pattern-wrangler',
array( $this, 'admin_page' ),
1
);

add_submenu_page(
'dlx-pattern-wrangler',
__( 'Block Patterns', 'dlx-pattern-wrangler' ),
__( 'Block Patterns', 'dlx-pattern-wrangler' ),
'edit_posts',
'edit.php?post_type=wp_block',
'',
10
);
}
Expand All @@ -450,7 +440,7 @@ public function add_admin_menu() {
* @param string $hook The current admin page.
*/
public function enqueue_scripts( $hook ) {
if ( 'toplevel_page_dlx-pattern-wrangler' !== $hook && 'appearance_page_dlx-pattern-wrangler' !== $hook ) {
if ( 'patterns_page_dlx-pattern-wrangler' !== $hook && 'appearance_page_dlx-pattern-wrangler' !== $hook ) {
return;
}

Expand Down
54 changes: 54 additions & 0 deletions php/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public function run() {
// Add a featured image to the wp_block post type column.
add_action( 'manage_wp_block_posts_custom_column', array( $this, 'add_featured_image_column_content' ), 10, 2 );

// Add bulk action for making patterns a draft.
add_filter( 'bulk_actions-edit-wp_block', array( $this, 'add_draft_bulk_actions' ) );

// Handle bulk actions for making patterns a draft.
add_action( 'handle_bulk_actions-edit-wp_block', array( $this, 'handle_bulk_actions' ), 10, 3 );

// Show the customizer UI if enabled.
$show_customizer_ui = (bool) $options['showCustomizerUI'];
if ( $show_customizer_ui ) {
Expand Down Expand Up @@ -83,6 +89,54 @@ public function run() {
}
}

/**
* Handle bulk actions.
*
* @param string $redirect_to Redirect URL.
* @param string $doaction Action to perform.
* @param array $post_ids Array of post IDs.
*
* @return string
*/
public function handle_bulk_actions( $redirect_to, $doaction, $post_ids ) {
// Check if action is draft pattern.
if ( 'draft_pattern' !== $doaction ) {
return $redirect_to;
}

// Loop through post IDs and update status.
foreach ( $post_ids as $post_id ) {
wp_update_post(
array(
'ID' => $post_id,
'post_status' => 'draft',
)
);
}

// Build redirect URL.
$redirect_url = add_query_arg(
array(
'post_type' => 'wp_block',
'notice_action' => 'draft_pattern',
),
admin_url( 'edit.php' )
);
return esc_url_raw( $redirect_url );
}

/**
* Add draft bulk actions.
*
* @param array $actions Array of actions.
*
* @return array Updated actions.
*/
public function add_draft_bulk_actions( $actions ) {
$actions['draft_pattern'] = __( 'Set as Draft', 'dlx-pattern-wrangler' );
return $actions;
}

/**
* Enable menus UI.
*/
Expand Down
Loading