网站中所有上传文件的列表

时间:2013-04-21 作者:Ghinnersmee

我可以列出用户在网络站点上载的所有文件吗?在多站点中,每个用户都可以使用自己的博客管理自己的文件,但超级管理员无法监视所有上载的文件。。。我该怎么做?

2 个回复
最合适的回答,由SO网友:fuxia 整理而成

您必须浏览每个博客并获取最近的附件,包括:

$args    = array (
    \'post_type\'   => \'attachment\',
    \'numberposts\' => 30
);
$attachments = get_posts( $args );
仪表板小部件在上注册wp_network_dashboard_setup 在多站点和wp_dashboard_setup 在单个站点中。

确保在插件标题中添加以下行以启用插件网络:

 * Network:     true 
然后你可以得到这样一张桌子:

enter image description here

(是的,从我当地的开发网站上看,它看起来有点奇怪)

示例插件,非常原始

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: List recent uploads in a network
 * Version:     2013.04.21
 * Network:     true
 */

if ( is_multisite() )
    add_action(
        \'wp_network_dashboard_setup\',
        array ( \'T5_Recent_Uploads_Dashboard_Widget\', \'register\' )
    );
else
    add_action(
        \'wp_dashboard_setup\',
        array ( \'T5_Recent_Uploads_Dashboard_Widget\', \'register\' )
    );

class T5_Recent_Uploads_Dashboard_Widget
{
    protected static $instance = NULL;

    protected $max_items = 30;

    public static function register()
    {
        self::$instance = new self;

        wp_add_dashboard_widget(
            __CLASS__,
            \'Recent Uploads\',
            array ( self::$instance, \'render\' )
        );
    }

    public function render()
    {
        $uploads = $this->get_recent_uploads();

        if ( empty ( $uploads ) )
            return print \'<p>No new uploads</p>\';

        printf( \'<p>%d new uploads.</p>\', count( $uploads ) );

        print $this->get_attachment_table( $uploads );

        // inspect content here
        //print \'<pre>$uploads = \' . htmlspecialchars( var_export( $uploads, TRUE ), ENT_QUOTES, \'utf-8\', FALSE ) . \'</pre>\';
    }

    protected function get_recent_uploads()
    {
        if ( ! is_multisite() )
            return $this->get_attachments();

        $uploads      = array();
        $current_blog = get_current_blog_id();
        $blogs        = $this->get_blog_ids();

        foreach ( $blogs as $blog )
        {
            switch_to_blog( $blog->blog_id );
            $attachments = $this->get_attachments();

            foreach ( $attachments as $attachment )
                $uploads[] = $this->prepare_attachment( $attachment, $blog );
        }

        // restore current blog
        switch_to_blog( $current_blog );

        return array_slice( $uploads, 0, $this->max_items );
    }

    protected function prepare_attachment( $attachment, $blog )
    {
        $attachment->blog_id   = $blog->blog_id;
        $attachment->blog_name = get_bloginfo( \'name\' );
        $attachment->edit      = get_edit_post_link( $attachment->ID );

        return $attachment;
    }

    protected function get_blog_ids()
    {
        global $wpdb;

        $query = $wpdb->prepare(
            "SELECT blog_id
                FROM $wpdb->blogs
                WHERE site_id    = %d
                AND public   = \'1\'
                AND archived = \'0\'
                AND mature   = \'0\'
                AND spam     = \'0\'
                AND deleted  = \'0\'
                ORDER BY blog_id",
            $wpdb->siteid
        );

        return $wpdb->get_results( $query );
    }

    protected function get_attachment_table( $attachments )
    {
        $table = \'<table class="widefat">\' . $this->get_table_header() . \'<tbody>\';

        foreach ( $attachments as $attachment )
            $table .= $this->get_row( $attachment );

        return "$table</tbody></table>";
    }

    protected function get_attachments()
    {
        $args    = array (
            \'post_type\'   => \'attachment\',
            \'numberposts\' => $this->max_items
        );

        return get_posts( $args );
    }

    protected function get_table_header()
    {
        $columns = array ( \'Attachment\', \'User\', \'Date\' );

        if ( is_multisite() )
            $columns[] = \'Site\';

        $header  = \'<tr><th>\' . join( \'</th><th>\', $columns ) . \'</th></tr>\';
        return "<thead>$header</thead><tfoot>$header</tfoot>";

    }

    protected function get_row( $attachment )
    {
        $cells   = array ();
        $cells[] = $this->get_attachment_title( $attachment );
        $cells[] = $this->get_user_link( $attachment );
        $cells[] = mysql2date( \'Y.m.d\', $attachment->post_date );

        if ( is_multisite() )
            $cells[] = $attachment->blog_name;

        return \'<tr><td>\' . join( \'</td><td>\', $cells ) . \'</td></tr>\';
    }

    protected function get_attachment_title( $attachment )
    {
        $title = $attachment->post_title;

        if ( empty ( $title ) )
            $title = basename( $attachment->guid );

        return "<a href=\'$attachment->edit\'>$title</a>";
    }

    protected function get_user_link( $attachment )
    {
        $name = get_the_author_meta( \'nickname\', $attachment->post_author );
        $url = network_admin_url( \'user-edit.php?user_id=\' . $attachment->post_author );

        return "<a href=\'$url\'>$name</a>";
    }
}

SO网友:user43819

WordPress 3.7引入了wp\\u get\\u sites(),它将返回WordPress多站点网络中的站点数组。这可以替换get\\u blog\\u ids()。

结束

相关推荐

Using Multisite with a CDN?

我运行的Wordpress多站点由Bluehost托管,只有5个低容量站点。我的站点加载时间最近很糟糕,所以我使用了延迟加载、DB优化和Smush。它和快速缓存来尝试解决速度问题。花了很多时间寻找一个好的CDN,显然,没有一个CDN可以轻松地用WP Multisite实现。每一个都需要W3 Total缓存,无法通过网络激活。关于如何创建镜像静态页面等,有一些看起来很冒险的教程,但没有任何内容可以让安装变得清晰简单。我选择的CDN是MaxCDN,但即使它们也需要W3TC。有人对如何在云中或CDN服务中安装多