| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Bulk_Editor\Infrastructure\Posts; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Bulk_Editor\Application\Updates\Post_Access_Checker_Interface; |
| 7 |
|
| 8 |
/** |
| 9 |
* Resolves, for a page of posts, whether the current user may edit each one. |
| 10 |
* |
| 11 |
* The database query narrows the candidates by post type, status and author (a sound approximation that |
| 12 |
* keeps pagination cheap). This resolver then applies the exact per-post check, current_user_can( |
| 13 |
* 'edit_post', $id ), so the collector can lock and blank the posts the update endpoint would refuse (for |
| 14 |
* example a post that a plugin blocks for the current user through map_meta_cap). It runs on the page only, |
| 15 |
* never the whole result set, so the cost stays bound to the page size. |
| 16 |
*/ |
| 17 |
class Post_Editability_Resolver { |
| 18 |
|
| 19 |
/** |
| 20 |
* The post access checker. |
| 21 |
* |
| 22 |
* @var Post_Access_Checker_Interface |
| 23 |
*/ |
| 24 |
private $post_access_checker; |
| 25 |
|
| 26 |
/** |
| 27 |
* The constructor. |
| 28 |
* |
| 29 |
* @param Post_Access_Checker_Interface $post_access_checker The post access checker. |
| 30 |
*/ |
| 31 |
public function __construct( Post_Access_Checker_Interface $post_access_checker ) { |
| 32 |
$this->post_access_checker = $post_access_checker; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns, for each given post ID, whether the current user may edit it. |
| 37 |
* |
| 38 |
* @param array<int> $post_ids The post IDs on the current page. |
| 39 |
* |
| 40 |
* @return array<int, bool> A map of post ID to whether the current user may edit it. |
| 41 |
*/ |
| 42 |
public function resolve( array $post_ids ): array { |
| 43 |
if ( $post_ids === [] ) { |
| 44 |
return []; |
| 45 |
} |
| 46 |
|
| 47 |
// Prime the post cache once so the per-post edit check does not run a query per post. |
| 48 |
\_prime_post_caches( $post_ids, false, false ); |
| 49 |
|
| 50 |
$editability = []; |
| 51 |
foreach ( $post_ids as $post_id ) { |
| 52 |
$editability[ $post_id ] = $this->post_access_checker->can_edit( $post_id ); |
| 53 |
} |
| 54 |
|
| 55 |
return $editability; |
| 56 |
} |
| 57 |
} |
| 58 |
|