プラグインがユーザーにデータを送信させる場合、— それが管理者側であれ、公開側であれ —「ユーザー権限」をチェックする必要があります。
ユーザーの権限グループと権限
効率的なセキュリティ・レイヤを構築するうえで、最も重要なステップは、ユーザー・アクセス許可システムを導入することです。WordPress は、これをユーザーの権限グループと権限という形で提供しています。
WordPress にログインしているすべてのユーザーには、ユーザーの権限グループに応じて、特定のユーザー権限が自動的に割り当てられます。
ユーザー権限グループ は、ユーザーがどのグループに属しているかを示す表現です。各グループは、事前に定義された権限の特定のセットを持っています。
たとえば、Web サイトのメインユーザーは管理者というユーザー権限グループを持ち、他のユーザーは編集者や投稿者といった権限グループを持つかもしれません。1つの権限グループに複数のユーザーを割り当てることもできます。たとえば、1つの Web サイトに2人の管理者がいるような場合です。
ユーザー権限 は、各ユーザーまたはユーザー権限グループに割り当てる、特定のアクセス許可です。
たとえば、管理者は manage_options
権限を持っており、Web サイトのオプションを表示、編集、保存できます。一方、編集者にはこの権限がないため、オプションを操作できません。
これらの権限は、管理画面のさまざまな場所でチェックされます。ユーザー権限グループに割り当てられた権限によって、メニュー、機能、その他の WordPress 体験の側面が追加または削除される可能性があります。
プラグインを作成する際には、現在のユーザーが必要な権限を持っている場合にのみ、コードを実行するようにしてください。
ヒエラルキー (階層構造)
ユーザー権限グループが高いほど、ユーザーはより多くの権限を持ちます。各ユーザー権限グループは、ヒエラルキーの直前の権限グループを継承します。
たとえば、「管理者」は、1つのサイト・インストールにおける最高のユーザー権限グループであり、「購読者」、「寄稿者」、「投稿者」、「編集者」の権限グループとその権限を継承します。
例
制限なし
以下の例では、投稿をゴミ箱に捨てるためのリンクを、フロントエンドに作成しています。このコードはユーザーの権限をチェックしないため、サイトへの訪問者なら誰でも、投稿をゴミ箱に捨てることができます !
/**
* Generate a Delete link based on the homepage url.
*
* @param string $content Existing content.
*
* @return string|null
*/
function wporg_generate_delete_link( $content ) {
// Run only for single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
// Add query arguments: action, post.
$url = add_query_arg(
[
'action' => 'wporg_frontend_delete',
'post' => get_the_ID(),
], home_url()
);
return $content . ' <a href="' . esc_url( $url ) . '">' . esc_html__( 'Delete Post', 'wporg' ) . '</a>';
}
return null;
}
/**
* Request handler
*/
function wporg_delete_post() {
if ( isset( $_GET['action'] ) && 'wporg_frontend_delete' === $_GET['action'] ) {
// Verify we have a post id.
$post_id = ( isset( $_GET['post'] ) ) ? ( $_GET['post'] ) : ( null );
// Verify there is a post with such a number.
$post = get_post( (int) $post_id );
if ( empty( $post ) ) {
return;
}
// Delete the post.
wp_trash_post( $post_id );
// Redirect to admin page.
$redirect = admin_url( 'edit.php' );
wp_safe_redirect( $redirect );
// We are done.
die;
}
}
/**
* Add the delete link to the end of the post content.
*/
add_filter( 'the_content', 'wporg_generate_delete_link' );
/**
* Register our request handler with the init hook.
*/
add_action( 'init', 'wporg_delete_post' );
特定の権限に限定
上記の例では、サイトの訪問者であれば誰でも「削除」リンクをクリックし、投稿をゴミ箱に捨てることができます。しかし、「削除」リンクをクリックできるのは、「編集者」以上に限定したいと思っています。
これを実現するため、「編集者」以上しか持っていない、edit_others_posts
権限を現在のユーザーが持っているかどうかをチェックします:
/**
* Generate a Delete link based on the homepage url.
*
* @param string $content Existing content.
*
* @return string|null
*/
function wporg_generate_delete_link( $content ) {
// Run only for single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
// Add query arguments: action, post.
$url = add_query_arg(
[
'action' => 'wporg_frontend_delete',
'post' => get_the_ID(),
], home_url()
);
return $content . ' <a href="' . esc_url( $url ) . '">' . esc_html__( 'Delete Post', 'wporg' ) . '</a>';
}
return null;
}
/**
* Request handler
*/
function wporg_delete_post() {
if ( isset( $_GET['action'] ) && 'wporg_frontend_delete' === $_GET['action'] ) {
// Verify we have a post id.
$post_id = ( isset( $_GET['post'] ) ) ? ( $_GET['post'] ) : ( null );
// Verify there is a post with such a number.
$post = get_post( (int) $post_id );
if ( empty( $post ) ) {
return;
}
// Delete the post.
wp_trash_post( $post_id );
// Redirect to admin page.
$redirect = admin_url( 'edit.php' );
wp_safe_redirect( $redirect );
// We are done.
die;
}
}
/**
* Add delete post ability
*/
add_action('plugins_loaded', 'wporg_add_delete_post_ability');
function wporg_add_delete_post_ability() {
if ( current_user_can( 'edit_others_posts' ) ) {
/**
* Add the delete link to the end of the post content.
*/
add_filter( 'the_content', 'wporg_generate_delete_link' );
/**
* Register our request handler with the init hook.
*/
add_action( 'init', 'wporg_delete_post' );
}
}