5. 2 アドミニメニュー ユーザー別投稿権限設定

 ユーザー毎に次のような投稿権限を設定します。
  (1)コアメニューの操作権限
  (2)一般メニューの操作権限
  (3)カテゴリ別投稿権限
  (4)個別ページ編集権限
 ユーザー名は氏名表示です。
 入力欄にID を入力して設定します。全部の場合は、all と入力します。
 ダッシュボードは非表示にできません。
 管理者の場合、コアメニューと一般メニューは非表示にできません。

 【独自用語の説明】
  ・コアメニュー:WordPress のメニュー
  ・一般メニュー:コアメニュー、カテゴリメニュー、ページメニュー以外のメニュー

 データベースには下記4個のデータを登録しています。
  'hk_manage_author_core':コアメニューの権限設定
  'hk_manage_author_other':一般メニューの権限設定
  'hk_manage_author_cat':カテゴリの権限設定
  'hk_manage_author_page':ページの権限設定
 下記関数に関しては“共通関数”をご覧ください。
  hk_func_hex2spchar
  hk_func_spchar2hex
  hk_func_post_csv_read

コード

// ================================
// 管理者・編集者・投稿者・寄稿者の投稿権限設定
// ================================
class Hk_Admin_Menu_Author {
    // ------ private プロパティ ------
    private $administrator_ids = array();    // 管理者 ID
    private $editor_ids        = array();    // 編集者 ID
    private $author_ids        = array();    // 投稿者 ID
    private $contributor_ids   = array();    // 寄稿者 ID
    private $member_data       = array();    // ユーザーデータ    user_id => ,'section','name','core','other','cat','page'
    private $all_titles        = array();    // 全メニュータイトル    GROUP => ID => タイトル
    private $other_title_id    = array();    // 一般メニュー    タイトル => id

    // --- メイン ---------------------
    // 管理画面メイン
    //   投稿権限設定
    // --------------------------------
    function admin_main() {
        global $hk_core_menu_check;
        // コアメニューチェック
        if ( ! $hk_core_menu_check ) {
            // コアメニューの構造異常
            echo "\n\n".'<div class="hk_plugin_admin">'."\n";
            echo '  <h1>会員の権限設定</h1>'."\n";
            echo '  <div class="hk_message">'."\n";
            echo 'WordPress の管理メニューのデータ構造に異常があり実行できません。'."\n";
            echo '  </div>'."\n";
            echo "</div>\n";
            return;
        }
        // メッセージ初期化
        $admin_message = '';
        // 全メニュータイトル取得
        $this->get_menu_titles();
        // 会員情報初期化
        $this->user_init();
        // 変更を保存
        if ( isset( $_POST["admin_next"] ) ) {
            $admin_next = hk_func_post_read( 'admin_next', 1 );
            if ( $admin_next == '変更を保存' ) {
                // フォームデータ取り込み
                $admin_message = $this->admin_post_data( $admin_message );
                // DB書き込み
                if ( $admin_message == '' ) {
                    $this->db_write();
                    $admin_message = '変更を保存しました。';
                }
            }
        }
        // 管理画面に表示
        echo "\n\n".'<div class="hk_plugin_admin hk_admin_menu_author">'."\n";
        echo '  <h1>会員の権限設定</h1>'."\n";
        if ( $admin_message != '' ) {
            $admin_message = trim( $admin_message );
            $admin_message = nl2br( $admin_message );
            echo '  <div class="hk_message">'."\n";
            echo $admin_message."\n";
            echo '  </div>'."\n";
        }
        // フォーム表示開始
        echo '<form method="post">'."\n";
        echo '  <div class="hk_chap hk_user_table">'."\n";
        // 会員情報
        echo '    <p> 入力欄にID を入力してください。複数の場合は、,(半角カンマ)または半角スペースで区切ってください。<br />'."\n";
        echo ' 全部の場合は、all と入力してください。<br />'."\n";
        echo ' コアメニューは、WordPressのメニューです。一般メニューは、コアメニュー、カテゴリメニュー、ページメニュー以外のメニューです。</p>'."\n";
        // 管理者
        $this->user_setting_table( '管理者', $this->administrator_ids );
        // 編集者
        $this->user_setting_table( '編集者', $this->editor_ids );
        // 投稿者
        $this->user_setting_table( '投稿者', $this->author_ids );
        // 寄稿者
        $this->user_setting_table( '寄稿者', $this->contributor_ids );
        echo '  </div><!-- hk_chap -->'."\n";
        // SUBMIT ボタン
        echo '  <p><input type="submit" name="admin_next" value="変更を保存" /></p>'."\n";
        echo "</form>\n";
        // メニュー一覧
        echo '<div class="hk_chap hk_menu_table hk_clear">'."\n";
        // 左側
        echo '  <div class="alignleft hk_width33">'."\n";
        // コアメニュー
        $this->menu_table( 'コアメニュー', 'core' );
        // 一般メニュー
        $this->menu_table( '一般メニュー', 'other' );
        echo '  </div>'."\n";
        // 中側
        echo '  <div class="alignleft hk_width33">'."\n";
        // カテゴリメニュー
        $this->menu_table( 'カテゴリメニュー', 'cat' );
        echo '  </div>'."\n";
        // 右側
        echo '  <div class="alignleft hk_width33">'."\n";
        // ページメニュー
        $this->menu_table( 'ページメニュー', 'page' );
        echo '  </div>'."\n";
        echo '</div><!-- hk_chap -->'."\n";
        // END
        echo '</div><!-- hk_plugin_admin -->'."\n";
        return;
    }

    // --- サブルーチン ---------------
    // 全メニュータイトル取得
    // --------------------------------
    private function get_menu_titles() {
        global $menu;
        // メニューデータ初期化
        $this->all_titles = array();        // メニュー一覧     GROUP => ID => タイトル
        $core_titles  = array();            // コアタイトル     id => title
        $other_titles = array();            // 一般タイトル     id => title
        $cat_titles   = array();            // カテゴリタイトル id => title
        $page_titles  = array();            // ページタイトル   id => title
        $this->other_title_id = array();    // 一般ID           title => id
        // メニューデータ取得
        foreach( $menu as $order => $each_menu ) {
             // ダッシュボード
            if ( $order == 2 )                          continue;
            // タイトル
            $title = $each_menu[ 0 ];
            // セパレータ
            if ( $title == '' )                         continue;
            // グループ別処理
            if ( ! isset( $each_menu['hk_group'] ) )    continue;
            $group = $each_menu['hk_group'];
            // コア
            if ( $group == 'core' ) {
                $id    = $each_menu['hk_order_org'];
                $title = $each_menu['hk_title'];
                $core_titles[ $id ] = $title;
            }
            // 一般
            else if ( $group == 'other' ) {
                $id    = $each_menu['hk_order_org'];
                $title = $each_menu[ 0 ];
                $other_titles[ $id ] = $title;
                $this->other_title_id[ $title ] = $id;
            }
            // カテゴリ
            else if ( $group == 'cat' ) {
                $id = $each_menu['hk_id'];
                $cat_titles[ $id ] = $each_menu[ 0 ];
            }
            // 個別ページ
            else if ( $group == 'page' ) {
                $id = $each_menu['hk_id'];
                $page_titles[ $id ] = $each_menu[ 0 ];
            }
        }
        unset( $order );
        unset( $each_menu );
        // ソート
        ksort( $core_titles );
        $this->all_titles['core']  = $core_titles;
        ksort( $other_titles );
        $this->all_titles['other'] = $other_titles;
        ksort( $cat_titles );
        $this->all_titles['cat']   = $cat_titles;
        ksort( $page_titles );
        $this->all_titles['page']  = $page_titles;
        return;
    }

    // --- サブルーチン ---------------
    // ユーザ設定表示
    // --------------------------------
    private function user_setting_table( $title, $user_ids ) {
        echo '    <h3>'.$title.'</h3>'."\n";
        $user_num = count( $user_ids );
        if ( $user_num == 0 ) {
            echo '    <p>'.$title.'はいません</p>'."\n";
            return;
        }
        echo '<table><tbody>'."\n";
        echo '  <tr>'."\n";
        echo '    <th>部署・氏名</th>'."\n";
        echo '    <th>コアメニュー</th>'."\n";
        echo '    <th>一般メニュー</th>'."\n";
        echo '    <th>カテゴリ</th>'."\n";
        echo '    <th>ページ</th>'."\n";
        echo '  </tr>'."\n";
        foreach ( $user_ids as $user_id ) {
            echo '  <tr>'."\n";
            $section = $this->member_data[ $user_id ]['section'];
            $name    = $this->member_data[ $user_id ]['name'];
            if ( $section != '' )
                $name = $section.'<br />'.$name;
            echo '    <td>'.$name.'</td>'."\n";
            if ( in_array( $user_id, $this->administrator_ids ) ) {
                echo '    <td class="hk_centerRL">all<input type="hidden" name="core_' .$user_id.'" value="all" /></td>'."\n";
                echo '    <td class="hk_centerRL">all<input type="hidden" name="other_'.$user_id.'" value="all" /></td>'."\n";
                echo '    <td class="hk_centerRL">all<input type="hidden" name="cat_'  .$user_id.'" value="all" /></td>'."\n";
                echo '    <td class="hk_centerRL">all<input type="hidden" name="page_' .$user_id.'" value="all" /></td>'."\n";
            }
            else {
                echo '    <td class="hk_centerRL"><input type="text" name="core_' .$user_id.'" size="12" value="'.$this->member_data[ $user_id ]['core']. '" /></td>'."\n";
                echo '    <td class="hk_centerRL"><input type="text" name="other_'.$user_id.'" size="12" value="'.$this->member_data[ $user_id ]['other'].'" /></td>'."\n";
                echo '    <td class="hk_centerRL"><input type="text" name="cat_'.  $user_id.'" size="12" value="'.$this->member_data[ $user_id ]['cat'].  '" /></td>'."\n";
                echo '    <td class="hk_centerRL"><input type="text" name="page_'. $user_id.'" size="12" value="'.$this->member_data[ $user_id ]['page']. '" /></td>'."\n";
            }
            echo '  </tr>'."\n";
        }
        unset( $user_id );
        echo '</tbody></table>'."\n";
        return;
    }

    // --- サブルーチン ---------------
    // メニュー一覧表示
    // --------------------------------
    private function menu_table( $group_title, $group ) {
        echo '    <h3>'.$group_title.'</h3>'."\n";
        $id_titles = $this->all_titles[ $group ];
        if ( empty( $id_titles ) ) {
            echo "    <p> ありません</p>\n";
            return;
        }
        echo '<table><tbody>'."\n";
        echo '  <tr>'."\n";
        echo '    <th>ID</th>'."\n";
        echo '    <th>メニュー名</th>'."\n";
        echo '  </tr>'."\n";
        foreach( $id_titles as $id => $title ) {
            $title_1 = htmlspecialchars( $title );
            echo '  <tr>'."\n";
            echo '    <td class="hk_right">'.$id.'</td>'."\n";
            echo '    <td>'.$title_1.'</td>'."\n";
            echo '  </tr>'."\n";
        }
        unset( $id );
        unset( $title );
        echo '</tbody></table>'."\n";
        return;
    }

    // --- サブルーチン ---------------
    // 会員情報初期化
    // --------------------------------
    private function user_init() {
        $this->member_data = array();
        $this->administrator_ids = array();
        $this->editor_ids        = array();
        $this->author_ids        = array();
        $this->contributor_ids   = array();
        // 共通パラメータ
        $args = array(
            'orderby' => 'ID',
            'order'   => 'ASC',
            'fields'  => 'all',
        );
        // 管理者
        $args['role'] = 'administrator';
        $administrators = get_users( $args );
        foreach ( $administrators as $member ) {
            $user_id = $this->user_init_sub( $member );
            $this->member_data[ $user_id ]['core']  = 'all';
            $this->member_data[ $user_id ]['other'] = 'all';
            $this->member_data[ $user_id ]['cat']   = 'all';
            $this->member_data[ $user_id ]['page']  = 'all';
            $this->administrator_ids[] = $user_id;
        }
        unset( $member );
        // 編集者
        $args['role'] = 'editor';
        $editors = get_users( $args );
        foreach ( $editors as $member ) {
            $user_id = $this->user_init_sub( $member );
            $this->editor_ids[] = $user_id;
        }
        unset( $member );
        // 投稿者
        $args['role'] = 'author';
        $authors = get_users( $args );
        foreach ( $authors as $member ) {
            $user_id = $this->user_init_sub( $member );
            $this->author_ids[] = $user_id;
        }
        unset( $member );
        // 寄稿者
        $args['role'] = 'contributor';
        $contributors = get_users( $args );
        foreach ( $contributors as $member ) {
            $user_id = $this->user_init_sub( $member );
            $this->contributor_ids[] = $user_id;
        }
        unset( $member );
        return;
    }

    // --- サブルーチン ---------------
    // 会員情報初期化 サブ
    // --------------------------------
    private function user_init_sub( $member ) {
        // ユーザ基本情報
        $user_id = $member->ID;
        $section = get_user_meta( $user_id, 'hk_section', true );
        $section = htmlspecialchars( $section );
        $name    = $member->last_name.' '.$member->first_name;
        $this->member_data[ $user_id ]['section']  = $section;
        $this->member_data[ $user_id ]['name']  = $name;
        // DB読み込み
        $db_data = get_user_meta( $user_id, 'hk_admin_menu', true );
        if ( $db_data == '' ) {
            $this->member_data[ $user_id ]['core']  = '';
            $this->member_data[ $user_id ]['other'] = '';
            $this->member_data[ $user_id ]['cat']   = '';
            $this->member_data[ $user_id ]['page']  = '';
        }
        else {
            $lines = explode( '&', $db_data );
            foreach( $lines as $line ) {
                list( $group, $value ) = explode( '=', $line );
                if (( $value == '' ) || ( $value == 'all' )) {
                    $this->member_data[ $user_id ][ $group ] = $value;
                    continue;
                }
                // コア・カテゴリ・ページ
                if ( $group != 'other' ) {
                    $array_1 = explode( ',', $value );
                    $array_2 = array();
                    // 存在チェック
                    foreach( $array_1 as $id ) {
                        if ( isset( $this->all_titles[ $group ][ $id ] ) )
                            $array_2[] = $id;
                    }
                    unset( $id );
                }
                // 一般
                else {
                    $array_1 = explode( ',', $value );
                    // タイトル 逆文字変換
                    $array_1 =  array_map( 'hk_func_hex2spchar', $array_1 );
                    $array_2 = array();
                    // 存在チェック
                    foreach( $array_1 as $title ) {
                        if ( isset( $this->other_title_id[ $title ] ) ) {
                            // title -> id
                            $id = $this->other_title_id[ $title ];
                            $array_2[] = $id;
                        }
                    }
                    unset( $title );
                }
                // ソート
                sort( $array_2 );
                $this->member_data[ $user_id ][ $group ] = implode( ',', $array_2 );
            }
            unset( $line );
        }
        return $user_id;
    }

    // --- サブルーチン ---------------
    // DB書き込み
    // --------------------------------
    private function db_write() {
        foreach ( $this->member_data as $user_id => $value ) {
            // コア
            $db_data = 'core='.$value['core'];
            // 一般
            if (( $value['other'] == '' ) || ( $value['other'] == 'all' ))
                $db_data .= '&other='.$value['other'];
            else {
                $array_1 = explode( ',', $value['other'] );
                $array_2 = array();
                foreach ( $array_1 as $id ) {
                    $title = $this->all_titles['other'][ $id ];
                    // 文字変換
                    $title = hk_func_spchar2hex( $title );
                    $array_2[] = $title;
                }
                unset( $id );
                $db_data .= '&other='.implode( ',', $array_2 );
            }
            // カテゴリ
            $db_data .= '&cat='.$value['cat'];
            // ページ
            $db_data .= '&page='.$value['page'];
            // DB書き込み
            update_user_meta( $user_id, 'hk_admin_menu', $db_data );
        }
        unset( $user_id );
        unset( $value );
        return;
    }

    // --- サブルーチン ---------------
    // POSTされたデータの取り込み
    // --------------------------------
    private function admin_post_data( $admin_message ) {
        foreach ( $this->member_data as $user_id => $value ) {
            $user_name = $value['name'];
            // コア
            $admin_message = $this->admin_post_data_sub( 'core',  'コア',     $user_id, $user_name, $admin_message );
            // 一般
            $admin_message = $this->admin_post_data_sub( 'other', '一般',     $user_id, $user_name, $admin_message );
            // カテゴリ
            $admin_message = $this->admin_post_data_sub( 'cat',   'カテゴリ', $user_id, $user_name, $admin_message );
            // ページ
            $admin_message = $this->admin_post_data_sub( 'page',  'ページ',   $user_id, $user_name, $admin_message );
        }
        unset( $user_id );
        unset( $value );
        return $admin_message;
    }

    // --- サブルーチン ---------------
    // POSTされたデータの取り込み サブ
    // --------------------------------
    private function admin_post_data_sub( $group, $group_name, $user_id, $user_name, $admin_message ) {
        $message = '';
        $key = $group.'_'.$user_id;
        list( $csv_data, $rtc ) = hk_func_post_csv_read( $key, 'all' );
        if ( $rtc == -1 )
            $message = $user_name.':'.$group_name.'の書式が違います。'."\n";
        else if (( $csv_data !== '' ) && ( $csv_data != 'all' )) {
            $ids = explode( ',', $csv_data );
            // 存在チェック
            foreach( $ids as $id ) {
                if ( ! isset( $this->all_titles[ $group ][ $id ] ) )
                    $message .= $user_name.':'.$group_name.'に no '.$id.' が存在しません。'."\n";
            }
            unset( $id );
            if ( $message == '' ) {
                sort( $ids );
                $csv_data = implode( ',', $ids );
            }
        }
        $this->member_data[ $user_id ][ $group ] = $csv_data;
        return $admin_message.$message;
    }
} // END OF class Hk_Admin_Menu_Author

$hk_admin_menu_author = new Hk_Admin_Menu_Author();

// --------------------------------
// 管理画面に設定メニューの追加
// --------------------------------
function hk_admin_menu_author_setting() {
    global $hk_admin_menu_author;
    add_options_page( '投稿権限設定', '投稿権限設定', 'manage_options', 'hk_admin_menu_author', array( $hk_admin_menu_author, 'admin_main' ) );
    return;
}
add_action( 'admin_menu', 'hk_admin_menu_author_setting' );

// --------------------------------
// スタイル記述
// --------------------------------
function hk_admin_menu_author_admin_head() {
    global $pagenow;
    if ( $pagenow != 'options-general.php' )            return;
    if (( ! isset( $_GET['page'] ) )
     || ( $_GET['page'] != 'hk_admin_menu_author' ))    return;
?>
<style type="text/css">
    .hk_width33 {
        width: 33%;
    }
    .hk_admin_menu_author .hk_user_table table {
        border-collapse: collapse;
        border-spacing: 0;
        border-style: none;
        color: #000000;
        width: auto;
        margin: 0 0 36px 12px;
        margin: 0 0 2.571428571rem 0.857142857rem;
    }
    .hk_admin_menu_author .hk_user_table th,
    .hk_admin_menu_author .hk_user_table td {
        border: 1px #a9a9a9 solid;
        padding: 3px 3px;
        padding: 0.214285714rem 0.214285714rem;
        width: 100px;
        width: 7.1428571428rem;
    }
    .hk_admin_menu_author .hk_menu_table table {
        border-collapse: collapse;
        border-spacing: 0;
        border-style: none;
        color: #000000;
        width: auto;
        margin: 0 0 24px 12px;
        margin: 0 0 1.714285714rem 0.857142857rem;
    }
    .hk_admin_menu_author .hk_menu_table th,
    .hk_admin_menu_author .hk_menu_table td {
        border: 1px #a9a9a9 solid;
        padding: 3px 3px;
        padding: 0.214285714rem 0.214285714rem;
    }
</style>
<?php
    return;
}
add_action( 'admin_head', 'hk_admin_menu_author_admin_head' );

 このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。

関連

更新日:2016/03/25
掲載日:2016/01/20