[博客]undocumented WordPress.org Plugin API for plugin authors下面的插件按照给定的参数对列表进行排序(name
, 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\'
) );