如何按字母顺序对收藏插件屏幕进行排序?

时间:2012-12-19 作者:brasofilo

查看收藏夹插件选项卡时,/wp-admin/plugin-install.php?tab=favorites, 列表无序,是否可以从A到Z排序?

favorite plugins screen

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

该页面由WP_Plugin_Install_List_Table 类,该类调用函数plugins_api().
在这个函数中,我们找到了两个允许操作表输出的过滤器挂钩:plugins_api_argsplugins_api_result.

问题是API不提供有序查询,所以我们只能对每个页面的结果进行排序。解决方案是增加每页的项目数,直到不需要分页为止。默认限制为30个插件。

Related Q&A:

Relevant links:

  • [wp-hackers] Help with the API on WordPress.org?WordPress.org Plugin Information API Docs
  • [博客]undocumented WordPress.org Plugin API for plugin authorsname, requires 以及其他插件API响应)。

    <?php
    /*
    Plugin Name: All my Favorites
    Plugin URI:  https://wordpress.stackexchange.com/q/76643/12615
    Description: Order and increment the number of plugins per page in the Favorites tab.
    Version:     1.0
    Author:      Rodolfo Buaiz
    Author URI:  https://wordpress.stackexchange.com/users/12615/brasofilo
    License:     GPL v2
    */
    
    !defined( \'ABSPATH\' ) AND exit();
    
    if ( !class_exists( \'All_Favorites_Ordered\' ) ) :
    
    class All_Favorites_Ordered 
    {
        // store the options
        protected $params;
    
        /**
         * Set up plugin
         * 
         * @param  array $options  [per_page, order_by, order]
         * @return void
         */
        public function __construct( array $options ) 
        {
            $this->params = $options;    
            add_filter( \'plugins_api_args\', array( $this, \'max_num_of_plugs\' ), 10, 2 );
            add_filter( \'plugins_api_result\', array( $this, \'order_plugins\' ), 10, 3 );
        }   
    
        /**
         * Modifies the per_page argument when querying Favorites
         * 
         * @param  object $args [page, per_page, browse/user]
         * @param  object $action [query_plugins]
         * @return object $args
         */
        public function max_num_of_plugs( $args, $action )
        {
            // Not our query, exit earlier. 
            // Other tabs have browse instead of user.
            if( !$args->user ) 
                return $args;
    
            $args->per_page = $this->params[\'per_page\'];
            return $args;
        }
    
        /**
         * Sort result from Plugin API call
         * Add admin head action to print CSS
         * 
         * @param  object $res API response
         * @param  string $action [query_plugins]
         * @param  object $args [per_page, order_by, order]
         * @return object $res Original or modified response
         */
        public function order_plugins( $res, $action, $args )
        {
            // Not our query, exit earlier
            if( !$args->user ) 
                return $res;
    
            // Amazingly, this works here
            add_action( \'admin_head-plugin-install.php\', array( $this, \'hide_pagination\' ) );
    
            usort( $res->plugins, array($this, \'sort_obj\') );
    
            return $res;
        }
    
        /**
         * Hide Paging and other elements from Favorites screen
         * 
         * @return string Echoed in admin_head
         */
        public function hide_pagination()
        {
            echo \'<style>.install-help, .tablenav.top, .tablenav.bottom {display:none !important}</style>\';
        }    
    
        /**
         * Sort array of objects
         * 
         * @param  int callback ( mixed $a, mixed $b )
         * @return object Sorted array
         */
        private function sort_obj( $a, $b ) 
        { 
            $val = $this->params[\'order_by\'];
    
            if( \'ASC\' == $this->params[\'order\'] )
                return strnatcmp( strtolower($a->$val), strtolower($b->$val) );
            else
                return strnatcmp( strtolower($b->$val), strtolower($a->$val) );
        }
    
    }
    endif;
    
    /**
     * Initialize the plugin.
     * Possible order_by: name, slug, requires, tested (up to), rating, num_ratings, version
     */
    new All_Favorites_Ordered( array(
            \'per_page\'  => 200
        ,   \'order_by\'  => \'name\'
        ,   \'order\'     => \'ASC\'
    ) );
    

结束