Skip to content

Commit 321f2e3

Browse files
Enhance bulk action handlers to validate user permissions and post conditions, and improve unit test coverage.
1 parent 343f002 commit 321f2e3

2 files changed

Lines changed: 77 additions & 18 deletions

File tree

src/handlers/bulk-handler.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,17 @@ public function rewrite_bulk_action_handler( $redirect_to, $doaction, $post_ids
9292
$skipped = 0;
9393
if ( \is_array( $post_ids ) ) {
9494
foreach ( $post_ids as $post_id ) {
95+
$post = \get_post( $post_id );
96+
if ( empty( $post ) || ! $this->permissions_helper->should_rewrite_and_republish_be_allowed( $post ) ) {
97+
continue;
98+
}
9599
if ( ! \current_user_can( 'edit_post', $post_id ) ) {
96100
++$skipped;
97101
continue;
98102
}
99-
$post = \get_post( $post_id );
100-
if ( ! empty( $post ) && $this->permissions_helper->should_rewrite_and_republish_be_allowed( $post ) ) {
101-
$new_post_id = $this->post_duplicator->create_duplicate_for_rewrite_and_republish( $post );
102-
if ( ! \is_wp_error( $new_post_id ) ) {
103-
++$counter;
104-
}
103+
$new_post_id = $this->post_duplicator->create_duplicate_for_rewrite_and_republish( $post );
104+
if ( ! \is_wp_error( $new_post_id ) ) {
105+
++$counter;
105106
}
106107
}
107108
}
@@ -130,20 +131,22 @@ public function clone_bulk_action_handler( $redirect_to, $doaction, $post_ids )
130131
$skipped = 0;
131132
if ( \is_array( $post_ids ) ) {
132133
foreach ( $post_ids as $post_id ) {
134+
$post = \get_post( $post_id );
135+
if ( empty( $post ) || $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
136+
continue;
137+
}
138+
if ( \intval( \get_option( 'duplicate_post_copychildren' ) ) === 1
139+
&& \is_post_type_hierarchical( $post->post_type )
140+
&& Utils::has_ancestors_marked( $post, $post_ids )
141+
) {
142+
continue;
143+
}
133144
if ( ! \current_user_can( 'edit_post', $post_id ) ) {
134145
++$skipped;
135146
continue;
136147
}
137-
$post = \get_post( $post_id );
138-
if ( ! empty( $post ) && ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
139-
if ( \intval( \get_option( 'duplicate_post_copychildren' ) !== 1 )
140-
|| ! \is_post_type_hierarchical( $post->post_type )
141-
|| ( \is_post_type_hierarchical( $post->post_type ) && ! Utils::has_ancestors_marked( $post, $post_ids ) )
142-
) {
143-
if ( ! \is_wp_error( \duplicate_post_create_duplicate( $post ) ) ) {
144-
++$counter;
145-
}
146-
}
148+
if ( ! \is_wp_error( \duplicate_post_create_duplicate( $post ) ) ) {
149+
++$counter;
147150
}
148151
}
149152
}

tests/Unit/Handlers/Bulk_Handler_Test.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,39 @@ public function test_clone_bulk_action_handler_returns_early_for_wrong_action()
9292
* @return void
9393
*/
9494
public function test_clone_bulk_action_handler_skips_posts_user_cannot_edit() {
95-
$redirect_to = 'http://example.com/wp-admin/edit.php';
95+
$redirect_to = 'http://example.com/wp-admin/edit.php';
96+
$post1 = Mockery::mock( WP_Post::class );
97+
$post1->ID = 1;
98+
$post1->post_type = 'post';
99+
$post2 = Mockery::mock( WP_Post::class );
100+
$post2->ID = 2;
101+
$post2->post_type = 'post';
102+
103+
Monkey\Functions\expect( 'get_post' )
104+
->with( 1 )
105+
->andReturn( $post1 );
106+
107+
Monkey\Functions\expect( 'get_post' )
108+
->with( 2 )
109+
->andReturn( $post2 );
110+
111+
$this->permissions_helper
112+
->allows( 'is_rewrite_and_republish_copy' )
113+
->with( $post1 )
114+
->andReturn( false );
115+
116+
$this->permissions_helper
117+
->allows( 'is_rewrite_and_republish_copy' )
118+
->with( $post2 )
119+
->andReturn( false );
120+
121+
Monkey\Functions\expect( 'get_option' )
122+
->with( 'duplicate_post_copychildren' )
123+
->andReturn( 0 );
124+
125+
Monkey\Functions\expect( 'is_post_type_hierarchical' )
126+
->with( 'post' )
127+
->andReturn( false );
96128

97129
Monkey\Functions\expect( 'current_user_can' )
98130
->with( 'edit_post', 1 )
@@ -189,7 +221,31 @@ public function test_rewrite_bulk_action_handler_returns_early_for_wrong_action(
189221
* @return void
190222
*/
191223
public function test_rewrite_bulk_action_handler_skips_posts_user_cannot_edit() {
192-
$redirect_to = 'http://example.com/wp-admin/edit.php';
224+
$redirect_to = 'http://example.com/wp-admin/edit.php';
225+
$post1 = Mockery::mock( WP_Post::class );
226+
$post1->ID = 1;
227+
$post1->post_status = 'publish';
228+
$post2 = Mockery::mock( WP_Post::class );
229+
$post2->ID = 2;
230+
$post2->post_status = 'publish';
231+
232+
Monkey\Functions\expect( 'get_post' )
233+
->with( 1 )
234+
->andReturn( $post1 );
235+
236+
Monkey\Functions\expect( 'get_post' )
237+
->with( 2 )
238+
->andReturn( $post2 );
239+
240+
$this->permissions_helper
241+
->allows( 'should_rewrite_and_republish_be_allowed' )
242+
->with( $post1 )
243+
->andReturn( true );
244+
245+
$this->permissions_helper
246+
->allows( 'should_rewrite_and_republish_be_allowed' )
247+
->with( $post2 )
248+
->andReturn( true );
193249

194250
Monkey\Functions\expect( 'current_user_can' )
195251
->with( 'edit_post', 1 )

0 commit comments

Comments
 (0)