From 6132da5b3b3bdb4c33e3f4fa6dc1213ab374dc6a Mon Sep 17 00:00:00 2001 From: ISHIKAWA Koutarou Date: Thu, 11 Jul 2024 22:07:09 +0900 Subject: [PATCH] =?UTF-8?q?fixes=20#30=20users/roles-and-capabilities=20/i?= =?UTF-8?q?ndex.md=20=E5=92=8C=E8=A8=B3=E3=81=97=E3=81=BE=E3=81=97?= =?UTF-8?q?=E3=81=9F=E3=80=82=20(#31)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fixes #17 * Delete javascript/enqueuing/index.md * Add files via upload * ssue #30 users/roles-and-capabilities /index.md * issue #30 users/roles-and-capabilities /index.md 未確定だった用語の和訳を確定: - Capabilities の和訳を「権限」に - Auper Admin の和訳を「特権管理者」に * Update users/roles-and-capabilities/index.md Co-authored-by: Akira Tachibana * Update users/roles-and-capabilities/index.md Co-authored-by: Akira Tachibana * Update users/roles-and-capabilities/index.md Co-authored-by: Akira Tachibana * Update users/roles-and-capabilities/index.md Co-authored-by: Akira Tachibana --------- Co-authored-by: Akira Tachibana --- users/roles-and-capabilities/index.md | 153 +++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 3 deletions(-) diff --git a/users/roles-and-capabilities/index.md b/users/roles-and-capabilities/index.md index 47d0e87..9d851e6 100644 --- a/users/roles-and-capabilities/index.md +++ b/users/roles-and-capabilities/index.md @@ -1,30 +1,64 @@ + +# 権限グループと権限 - + +権限グループと権限は、WordPress の2つの重要な側面で、ユーザー権限をコントロールできます。 + +WordPress は、権限グループとその権限を、テーブル `options` のキー `user_roles` の下に格納します。 + +## 権限グループ + +権限グループは、ユーザーの一連の権限を定義します。たとえば、ユーザーが自分のダッシュボードで何を見たり、何をしたりできるかです。 + +**デフォルトでは、WordPress には6つの権限グループがあります:** + +- 特権管理者 (Super Admin) +- 管理者 (Administrator) +- 編集者 (Editor) +- 投稿者 (Author) +- 寄稿者 (Contributor) +- 購読者 (Subscriber) + + +権限グループを追加したり、デフォルトの権限グループを削除できます。 ![Users roles](https://i3.wp.com/developer.wordpress.org/files/2014/09/wp-roles.png) + +### 権限グループの追加 + +[`add_role()`](https://developer.wordpress.org/reference/functions/add_role/) を使用して、新しい権限グループを追加し、権限を割り当てられます。 ``` function wporg_simple_role() { @@ -43,17 +77,35 @@ function wporg_simple_role() { add_action( 'init', 'wporg_simple_role' ); ``` + +[alert]最初に [`add_role()`](https://developer.wordpress.org/reference/functions/add_role/) をコールすると、権限グループと権限がデータベースに格納されます ! + +連続してコールしても、権限リストの変更も含めて、何もしません。これはあなたが期待している動作ではないかもしれません。[/alert] + +[info]権限リストを一括で変更するには: [`remove_role()`](https://developer.wordpress.org/reference/functions/remove_role/) を使って権限グループを削除し、[`add_role()`](https://developer.wordpress.org/reference/functions/add_role/) を使って新しい権限を追加します。 + +必ず、権限が期待しているものと異なる場合 (つまり、このような状態) にのみ実行してください。さもないと、パフォーマンスが著しく低下します ! [/info] + +### 権限グループの削除 + +[`remove_role()`](https://developer.wordpress.org/reference/functions/remove_role/) で権限グループを削除します。 ``` function wporg_simple_role_remove() { @@ -64,27 +116,59 @@ function wporg_simple_role_remove() { add_action( 'init', 'wporg_simple_role_remove' ); ``` + +[alert]最初に [`remove_role()`](https://developer.wordpress.org/reference/functions/remove_role/) をコールすると、権限グループと権限がデータベースから削除されます ! + +連続してコールしても、何もしません。[/alert] + +[info]デフォルトの権限グループを削除する場合: + +- 管理者と特権管理者の権限グループを削除することに**反対**することを推奨します ! +- WordPress の将来のアップデートで、削除した権限グループが再び追加される可能性があるため、プラグイン / テーマのコードは必ず保管しておいてください。 +- WordPress のデフォルト権限グループである `subscriber` を削除する場合には、`update_option('default_role', YOUR_NEW_DEFAULT_ROLE)` を実行します。[/info] + +## 権限 + +権限とは、**権限グループ**ができること、できないことを定義するものです: 投稿の編集、投稿の公開、等。 + +[info]カスタム投稿タイプは、特定の権限のセットを必要とすることがあります。[/info] + +### 権限の追加 + +権限グループに新しい権限を定義できます。 + +[`get_role()`](https://developer.wordpress.org/reference/functions/get_role/) を使って権限グループオブジェクトを取得し、そのオブジェクトの `add_cap()` メソッドを使って新しい権限を追加します。 ``` function wporg_simple_role_caps() { @@ -99,47 +183,98 @@ function wporg_simple_role_caps() { add_action( 'init', 'wporg_simple_role_caps', 11 ); ``` + +[info]どの権限グループにもカスタム権限を追加できます。 + +WordPress のデフォルトの管理画面では、これらは何の効果もありませんが、カスタムの管理画面やフロントエンドの領域には使用できます。[/info] + +### 権限の削除 + +権限グループから権限を削除できます。 + +実装は「権限の追加」と似ていますが、違いは権限グループオブジェクトに `remove_cap()` メソッドを使うことです。 + +## 権限グループと権限の使用 + +### 権限グループの取得 + +[`get_role()`](https://developer.wordpress.org/reference/functions/get_role/) で、その権限グループのすべての権限を含む、権限グループオブジェクトを取得します。 + +### ユーザーのできること + +ユーザーが指定された**権限グループ**または**権限**を持っているかどうかを [`user_can()`](https://developer.wordpress.org/reference/functions/user_can/) でチェックします。 ``` user_can( $user, $capability ); ``` + +[warning]ドキュメント化されていない第3引数 $args があり、テスト対象のオブジェクトを含めることができます。 + +例: 投稿 ID を渡して、特定の投稿の権限をテストする。[/warning] + +### 現在のユーザーのできること + +[`current_user_can()`](https://developer.wordpress.org/reference/functions/current_user_can/) は [`user_can()`](https://developer.wordpress.org/reference/functions/user_can/) のラッパー関数で、現在のユーザーオブジェクトをパラメータ `$user` として使用します。 + +これは、バックエンドとフロントエンドの領域が、アクセスや変更に一定レベルの権限を必要とするシナリオで使用します。 ``` current_user_can( $capability ); ``` + +### 例 + +ここでは、ユーザーが適切な権限を持っている場合に、テンプレート・ファイルに編集リンクを追加する実用的な例を示します: ``` if ( current_user_can( 'edit_posts' ) ) { @@ -147,14 +282,26 @@ if ( current_user_can( 'edit_posts' ) ) { } ``` + +## マルチサイト + +[`current_user_can_for_blog()`](https://developer.wordpress.org/reference/functions/current_user_can_for_blog/) 関数は、現在のユーザーが特定のブログで特定の**権限グループ**または**権限**を持っているかどうかをテストするために使用されます。 ``` current_user_can_for_blog( $blog_id, $capability ); ``` + +## 参考資料 -Codex Reference for [User Roles and Capabilities](https://wordpress.org/documentation/article/roles-and-capabilities/). \ No newline at end of file + +[ユーザーの権限グループと権限](https://ja.wordpress.org/support/article/roles-and-capabilities/)のドキュメント。 \ No newline at end of file