Yoast\WP\SEO\Abilities\Infrastructure

Post_Access_Checker{}Yoast 1.0

Checks whether the current user may edit the posts behind indexables.

The abilities are gated site-wide by a capability check, but that capability must not grant access to posts the user could not otherwise edit. This checker applies the exact per-post WordPress check, current_user_can( edit_post, $id ), mirroring the bulk editor's access rules.

Хуков нет.

Использование

$Post_Access_Checker = new Post_Access_Checker();
// use class methods

Методы

  1. public ensure_can_edit( int $post_id )
  2. public array_values( $editable )
  3. public filter_editable( array $indexables )
  4. public forbidden_error()
  5. ERROR: no method name found on line `* Mirrors WP core's rest_cannot_edit wording.`
  6. ERROR: no method name found on line ``

Код Post_Access_Checker{} Yoast 28.3

class Post_Access_Checker {

	/**
	 * Ensures the current user may edit the given post.
	 *
	 * @param int $post_id The post ID.
	 *
	 * @return true|WP_Error True when the post is editable, or a 403 error.
	 */
	public function ensure_can_edit( int $post_id ) {
		if ( \current_user_can( 'edit_post', $post_id ) ) {
			return true;
		}

		return $this->forbidden_error();
	}

	/**
	 * Builds the 403 error for posts the current user may not edit.
	 *
	 * Mirrors WP core's rest_cannot_edit wording.
	 *
	 * @return WP_Error The forbidden error.
	 */
	public function forbidden_error(): WP_Error {
		return new WP_Error(
			'yoast_seo_cannot_edit_post',
			\__( 'Sorry, you are not allowed to edit this post.', 'wordpress-seo' ),
			[ 'status' => 403 ],
		);
	}

	/**
	 * Filters a list of post indexables down to the ones the current user may edit.
	 *
	 * @param Indexable[] $indexables The post indexables.
	 *
	 * @return Indexable[] The editable indexables, reindexed.
	 */
	public function filter_editable( array $indexables ): array {
		$editable = \array_filter(
			$indexables,
			static function ( $indexable ) {
				return \current_user_can( 'edit_post', (int) $indexable->object_id );
			},
		);

		return \array_values( $editable );
	}
}