| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Bulk_Editor\Infrastructure\Updates; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Bulk_Editor\Application\Updates\Post_Access_Checker_Interface; |
| 7 |
use Yoast\WP\SEO\Helpers\Post_Type_Helper; |
| 8 |
|
| 9 |
/** |
| 10 |
* Checks post access through the WordPress APIs. |
| 11 |
*/ |
| 12 |
class Post_Access_Checker implements Post_Access_Checker_Interface { |
| 13 |
|
| 14 |
/** |
| 15 |
* The post type helper. |
| 16 |
* |
| 17 |
* @var Post_Type_Helper |
| 18 |
*/ |
| 19 |
private $post_type_helper; |
| 20 |
|
| 21 |
/** |
| 22 |
* The constructor. |
| 23 |
* |
| 24 |
* @param Post_Type_Helper $post_type_helper The post type helper. |
| 25 |
*/ |
| 26 |
public function __construct( Post_Type_Helper $post_type_helper ) { |
| 27 |
$this->post_type_helper = $post_type_helper; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Whether the post exists. |
| 32 |
* |
| 33 |
* @param int $post_id The ID of the post. |
| 34 |
* |
| 35 |
* @return bool Whether the post exists. |
| 36 |
*/ |
| 37 |
public function exists( int $post_id ): bool { |
| 38 |
return \get_post( $post_id ) !== null; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Whether the post is of an indexable post type. |
| 43 |
* |
| 44 |
* @param int $post_id The ID of the post. |
| 45 |
* |
| 46 |
* @return bool Whether the post is of an indexable post type. |
| 47 |
*/ |
| 48 |
public function is_supported_type( int $post_id ): bool { |
| 49 |
$post = \get_post( $post_id ); |
| 50 |
if ( $post === null ) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
return $this->post_type_helper->is_of_indexable_post_type( $post->post_type ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Whether the current user is allowed to edit the post. |
| 59 |
* |
| 60 |
* @param int $post_id The ID of the post. |
| 61 |
* |
| 62 |
* @return bool Whether the current user is allowed to edit the post. |
| 63 |
*/ |
| 64 |
public function can_edit( int $post_id ): bool { |
| 65 |
return \current_user_can( 'edit_post', $post_id ); |
| 66 |
} |
| 67 |
} |
| 68 |
|