4. 2 メディア一覧(簡易版)

 メディアライブラリは、多機能で、一般ユーザーが使いやすいものではありません。
 機能は次のものに絞っています。
  ・削除
  ・表示(ファイルそのものの表示)
 これらメニューは常に表示されます。
 投稿者・寄稿者の場合、本人のメディアのみ表示します。
 作成者は、当然、氏名表示。
 削除でリダイレクトはしません。このプログラムでクローズしています。

 アドミニメニュー登録の順番は、11番以降のあいたところにしています。
 下記関数は、“共通関数”に記載します。
   ・hk_func_orderby_select_media
   ・hk_func_order_select
   ・hk_func_author_select
 フック'admin_action_'は、admin.php の最後にあります。admin.php の処理が終了したところで、“分岐”しています。

コード

// ================================
// メディア一覧(簡易版)
// ================================
function hk_media_list() {
    require_once( WP_PLUGIN_DIR.'/hk-easy/hk-media-list.php' );
    exit;
}
add_action( 'admin_action_hk_media_list', 'hk_media_list' );

// リンク メディア一覧(簡易版)
function hk_easy_get_link_media_list() {
    return 'upload.php?action=hk_media_list&post_type=attachment&mode=list&hk_light_type=media_list';
}

// --------------------------------
// メディア一覧(簡易版)メニュー登録
// --------------------------------
function hk_easy_admin_menu_media_list() {
    global $menu;
    $order = 11;
    while ( isset( $menu[ $order ] ) )
        $order++;
    $page_title = 'メディア(簡易版)';
    $menu_title = 'メディア(簡易版)';
    $capability = 'upload_files';
    $menu_slug  = hk_easy_get_link_media_list();
    $function   = '';
    $icon_url   = 'dashicons-edit';
    add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $order );
    return;
}
add_action( 'admin_menu', 'hk_easy_admin_menu_media_list' );

hk-media-list.php
<?php
// ================================
// メディア一覧(簡易版)
// ================================

// --------------------------------
// スタイル記述
// --------------------------------
function hk_media_list_admin_head() {
?>
<style type="text/css">
    .hk_list_table {
        border-collapse: collapse;
        border-spacing: 0;
        border-style: none;
        color: #000000;
        width: 100%;
        margin: 0;
    }
    .hk_list_table thead,
    .hk_list_table tbody {
        border:1px #a9a9a9 solid;
    }
    .hk_list_table th,
    .hk_list_table td {
        padding: 6px 12px;
        padding: 0.428571428rem 0.857142857rem;
        font-weight: normal;
        text-align: left;
        vertical-align: middle;
    }
    .hk_list_table_cb {
        width: 24px;
        width: 1.714285714rem;
    }
</style>
<?php
    return;
}
add_action( 'admin_head', 'hk_media_list_admin_head' );

// ================================
// メイン
// ================================
global $title, $parent_file;
global $wp_query;
global $post;
// hk_light_type
if ( ! isset( $_REQUEST['hk_light_type'] ) )    wp_die('ERROR:hk_light_type');
$light_type = $_REQUEST['hk_light_type'];
if ( $light_type != 'media_list' )              wp_die('ERROR:hk_light_type');
// post_type
if ( ! isset( $_REQUEST['post_type'] ) )        wp_die('ERROR:post_type');
$post_type = $_REQUEST['post_type'];
if ( $post_type != 'attachment' )               wp_die('ERROR:post_type');
// mode
if ( ! isset( $_REQUEST['mode'] ) )             wp_die('ERROR:mode');
$mode = $_REQUEST['mode'];
if ( $mode != 'list' )                          wp_die('ERROR:mode');
// orderby
if ( isset( $_REQUEST['orderby'] ) )
        $orderby = $_REQUEST['orderby'];
else    $orderby = 'date';
// order
if ( isset( $_REQUEST['order'] ) )
        $order = $_REQUEST['order'];
else    $order = 'DESC';
// author
if ( isset( $_REQUEST['author'] ) )
        $author = $_REQUEST['author'];
else    $author = '';
// paged
if ( isset( $_REQUEST['paged'] ) )
        $paged = absint( $_REQUEST['paged'] );
else    $paged = 1;
// 削除
if ( isset( $_REQUEST['doaction'] ) )
        $do_action = $_REQUEST['doaction'];
else    $do_action = '';
$message = '';
if ( $do_action == 'delete' ) {
    // POST_ID
    if ( isset( $_REQUEST['post'] ) )
            $post_id = (int) $_REQUEST['post'];
    else    $post_id = 0;
    if ( $post_id )
        $post = get_post( $post_id );
    else    wp_die('ERROR:post');
    // 削除
    check_admin_referer('delete-post_'.$post_id);
    if ( ! $post )
        wp_die('この項目はすでに削除されています。');
    if ( ! current_user_can( 'delete_post', $post_id ) )
        wp_die('この項目を削除する権限がありません。');
    if ( ! wp_delete_attachment( $post_id, true ) )
        wp_die('削除中にエラーが発生しました。');
    $message = 'メディアファイルを完全に削除しました。';
}
// ヘッダー
$page_title = 'メディア一覧(簡易版)';
$title = $page_title;
$parent_file = hk_easy_get_link_media_list();
// admin-header
require_once( ABSPATH . 'wp-admin/admin-header.php' );
// ページタイトル
echo "\n".'<div class="wrap">'."\n";
echo '  <h1>'.esc_html( $page_title ).'</h1>'."\n";
// メッセージ
if ( $message )
    echo '  <div id="message" class="updated notice is-dismissible"><p>'.$message."</p></div>\n";
// 現在ページへのリンク
$media_url  = hk_easy_get_link_media_list();
$media_url .= '&orderby='.$orderby.'&order='.$order.'&author='.$author.'&paged='.$paged;
// 投稿者・寄稿者 本人のメディアのみ
if (( current_user_can('author') ) || ( current_user_can('contributor') ))
    $author = get_current_user_id();
// メディア取得
$args = array(
    'post_type'         => 'attachment',
    'post_status'       => 'any',
    'orderby'           => $orderby,
    'order'             => $order,
    'author'            => $author,
    'paged'             => $paged,
    'posts_per_page'    => 10,
);
query_posts( $args );
// FORM
echo '<form method="get">'."\n";
echo '  <input type="hidden" name="action"        value="hk_media_list" />'."\n";
echo '  <input type="hidden" name="hk_light_type" value="media_list" />'."\n";
echo '  <input type="hidden" name="post_type"     value="attachment" />'."\n";
echo '  <input type="hidden" name="mode"          value="list" />'."\n";
// 一覧表上部メニュー 表示順序変更 & ページネーション
echo '  <div class="tablenav top hk_clear">'."\n";
// 表示順序変更
echo '    <div class="alignleft">'."\n";
// 順序項目選択
hk_func_orderby_select_media( $orderby );
// 順序選択
hk_func_order_select( $order );
// 作成者選択(管理者・編集者)
if (( current_user_can('administrator') ) || ( current_user_can('editor') ))
    hk_func_author_select( $author );
// 送信ボタン
echo '      <input type="submit" value="表示変更" />'."\n";
echo "    </div>\n";
// ページネーション
$total_items = $wp_query->found_posts;
$total_pages = $wp_query->max_num_pages;
if ( $total_pages ) {
    if ( $total_pages < 2 ) $page_class = ' one-page';
    else                    $page_class = '';
}
else                        $page_class = ' no-pages';
echo '    <div class="tablenav-pages'.$page_class.'">'."\n";
echo '      <span class="displaying-num">'.$total_items.'項目</span>'."\n";
// 現在ページ
$current = $paged;
$current_url = admin_url( $media_url );
echo '      <span class="pagination-links">'."\n";
// 最初のページへ
$url   = $current_url;
$url   = remove_query_arg( 'paged', $url );
$url_1 = esc_url( $url );
echo '        <a class="first-page" title="最初のページへ" href="'.$url_1.'">&laquo;</a>'."\n";
// 前のページへ
$page_no = max( 1, $current-1 );
$url   = $current_url;
$url   = add_query_arg( 'paged', $page_no, $url );
$url_2 = esc_url( $url );
echo '        <a class="prev-page" title="前のページへ" href="'.$url_2.'">&lsaquo;</a>'."\n";
echo '        <span class="paging-input">'."\n";
// 現在のページ
echo '          <label for="current-page-selector" class="screen-reader-text">ページを選択</label>'."\n";
$size = strlen( $total_pages );
echo '          <input class="current-page" id="current-page-selector" title="現在のページ" type="text" name="paged" value="'.$current.'" size="'.$size.'" /> / <span class="total-pages">'.$total_pages.'</span>'."\n";
echo '        </span>'."\n";
// 次のページへ
$page_no = min( $total_pages, $current+1 );
$url   = $current_url;
$url   = add_query_arg( 'paged', $page_no, $url );
$url_3 = esc_url( $url );
echo '        <a class="next-page" title="次のページへ" href="'.$url_3.'">&rsaquo;</a>'."\n";
// 最後のページへ
$url   = $current_url;
$url   = add_query_arg( 'paged', $total_pages, $url );
$url_4 = esc_url( $url );
echo '        <a class="last-page" title="最後のページへ" href="'.$url_4.'">&raquo;</a>'."\n";
echo '      </span>'."\n";
echo '    </div>'."\n";
// ページネーション終わり
echo '  </div>'."\n";
// form 終わり
echo "</form>\n";
// 一覧表
echo '  <table class="hk_list_table">'."\n";
echo "    <thead>\n";
// 項目名
echo "      <tr>\n";
echo '        <th scope="col" class="hk_list_table_cb"></th>'."\n";
echo '        <th scope="col"></th>'."\n";
echo '        <th scope="col">ファイル</th>'."\n";
echo '        <th scope="col">作成者</th>'."\n";
echo '        <th scope="col">アップロード先</th>'."\n";
echo '        <th scope="col">作成日</th>'."\n";
echo "      </tr>\n";
echo "    </thead>\n";
// データ
if ( have_posts() ) {
    echo "    <tbody>\n";
    while ( have_posts() ) {
        the_post();
        $post_id = $post->ID;
        $can_edit_post = current_user_can( 'edit_post', $post_id );
        // 行表示開始
        echo '      <tr>'."\n";
        // チェックボックス
        echo '        <td>';
        if ( $can_edit_post )
            echo '<span class="dashicons dashicons-edit"></span>';
        echo '</td>'."\n";
        // アイコン
        $thumb = wp_get_attachment_image( $post_id, array( 80, 60 ), true );
        echo '        <td>'.$thumb.'</td>'."\n";
        // ファイル
        echo '        <td>'."\n";
        $att_title = _draft_or_post_title();
        echo '<strong>'.$att_title.'</strong>'."\n";
        $guid = $post->guid;
        $file_name = wp_basename( $guid );
        echo '<br />'.$file_name."\n";
        $actions = array();
        if ( current_user_can( 'delete_post', $post->ID ) ) {
            $delete_url = $media_url.'&post='.$post_id.'&doaction=delete';
            $delete_url = admin_url( $delete_url );
            $delete_url = wp_nonce_url( $delete_url, 'delete-post_'.$post_id );
            $actions['delete'] = '<a title="この項目を完全に削除する" href="'.$delete_url.'">完全に削除する</a>'."\n";
        }
        $actions['view'] = '<a title="この項目を表示する" href="'.$guid.'" target="_blank">表示</a>';
        if ( ! empty( $actions ) )
            echo '<br /> '.implode( " | \n", $actions )."\n";
        echo '        </td>'."\n";
        // 作成者
        $author_id  = $post->post_author;
        $section = get_user_meta( $author_id, 'hk_section', true );
        $section = htmlspecialchars( $section );
        $author_obj = get_userdata( $author_id );
        if ( $author_obj )
                $author_name = $author_obj->last_name.' '.$author_obj->first_name;
        else    $author_name = '------';
        if ( $section != '' )
            $author_name = $section.'<br />'.$author_name;
        echo '        <td>'.$author_name."</td>\n";
        // アップロード先
        if ( $post->post_parent > 0 ) {
            $post_title = _draft_or_post_title( $post->post_parent );
            echo '        <td><strong>'.$post_title.'</strong></td>'."\n";
        }
        else    echo '        <td></td>'."\n";
        // 作成日
        $date  = get_the_date('Y/m/d');
        $dates = explode( '/', $date );
        $date  = $dates[0].'年'.$dates[1].'月'.$dates[2].'日';
        echo '        <td>'.$date."</td>\n";
        echo "      </tr>\n";
    }
    echo "    </tbody>\n";
    echo "  </table>\n";
    // 一覧表下部
    echo '  <div class="tablenav bottom hk_clear">'."\n";
    // ページネーション
    echo '    <div class="tablenav-pages'.$page_class.'">'."\n";
    echo '      <span class="displaying-num">'.$total_items.'項目</span>'."\n";
    echo '      <span class="pagination-links">'."\n";
    // 最初のページへ
    echo '        <a class="first-page" title="最初のページへ" href="'.$url_1.'">&laquo;</a>'."\n";
    // 前のページへ
    echo '        <a class="prev-page" title="前のページへ" href="'.$url_2.'">&lsaquo;</a>'."\n";
    echo '        <span class="paging-input">'."\n";
    // 現在のページ
    echo '        <span class="screen-reader-text">現在のページ</span>'."\n";
    echo '        <span class="paging-input">'.$current.' / <span class="total-pages">'.$total_pages.'</span></span>'."\n";
    // 次のページへ
    echo '        <a class="next-page" title="次のページへ" href="'.$url_3.'">&rsaquo;</a>'."\n";
    // 最後のページへ
    echo '        <a class="last-page" title="最後のページへ" href="'.$url_4.'">&raquo;</a>'."\n";
    echo '      </span>'."\n";
    echo '    </div>'."\n";
    // ページネーション終わり
    echo '  </div>'."\n";
}
else {
    echo "    <tbody>\n";
    echo '      <tr><td colspan="6">メディアはありません。</td></tr>'."\n";
    echo "    </tbody>\n";
    echo "  </table>\n";
}
echo "</div>\n";
// フッター
include( ABSPATH . 'wp-admin/admin-footer.php' );
exit;
?>

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

関連

更新日:2016/03/24
掲載日:2016/01/15