没有这样的文档,我也不希望很快有这样的文档。WordPress可以为这些情况实现专门的属性对象,例如get_taxonomies()
可能是这样的:
class WP_Taxonomy_Properties {
private $data;
public function __construct( Array $data ) {
$this->data = $data;
}
public function get_labels() {
return $this->data[\'labels\'];
}
public function get_description() {}
public function get_public() {}
public function get_hierarchical() {}
public function get_show_ui() {}
public function get_show_in_menu() {}
public function get_show_in_nav_menus(){}
public function get_show_tagcloud() {}
public function get_meta_box_cb() {}
public function get_capabilities() {}
public function get_rewrite() {}
public function get_query_var() {}
public function get_update_count_callback() {}
public function is_builtin() {}
}
您的IDE将提供每个自动完成的方法。不再需要手动查找。
但这在不久的将来不会发生,至少有两个原因。
在核心代码中实现OOP几乎没有什么兴趣。如果你找到一个对象,它会做得太多(看WP_Screen
例如),或者它是第三方库(SimplePie)。
这需要进行大规模清理,因为许多物体实际上是隐藏的。课堂上的一个例子Walker_Nav_Menu
; 仔细观察$args
:
/**
* Start the element output.
*
* @see Walker::start_el()
*
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu()
* @param int $id Current item ID.
*/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
$class_names = $value = \'\';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = \'menu-item-\' . $item->ID;
/**
* Filter the CSS class(es) applied to a menu item\'s <li>.
*
* @since 3.0.0
*
* @param array $classes The CSS classes that are applied to the menu item\'s <li>.
* @param object $item The current menu item.
* @param array $args An array of arguments. @see wp_nav_menu()
*/
$class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args ) );
$class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';
/**
* Filter the ID applied to a menu item\'s <li>.
*
* @since 3.0.1
*
* @param string The ID that is applied to the menu item\'s <li>.
* @param object $item The current menu item.
* @param array $args An array of arguments. @see wp_nav_menu()
*/
$id = apply_filters( \'nav_menu_item_id\', \'menu-item-\'. $item->ID, $item, $args );
$id = $id ? \' id="\' . esc_attr( $id ) . \'"\' : \'\';
$output .= $indent . \'<li\' . $id . $value . $class_names .\'>\';
$atts = array();
$atts[\'title\'] = ! empty( $item->attr_title ) ? $item->attr_title : \'\';
$atts[\'target\'] = ! empty( $item->target ) ? $item->target : \'\';
$atts[\'rel\'] = ! empty( $item->xfn ) ? $item->xfn : \'\';
$atts[\'href\'] = ! empty( $item->url ) ? $item->url : \'\';
/**
* Filter the HTML attributes applied to a menu item\'s <a>.
*
* @since 3.6.0
*
* @param array $atts {
* The HTML attributes applied to the menu item\'s <a>, empty strings are ignored.
*
* @type string $title The title attribute.
* @type string $target The target attribute.
* @type string $rel The rel attribute.
* @type string $href The href attribute.
* }
* @param object $item The current menu item.
* @param array $args An array of arguments. @see wp_nav_menu()
*/
$atts = apply_filters( \'nav_menu_link_attributes\', $atts, $item, $args );
$attributes = \'\';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( \'href\' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= \' \' . $attr . \'="\' . $value . \'"\';
}
}
$item_output = $args->before;
$item_output .= \'<a\'. $attributes .\'>\';
/** This filter is documented in wp-includes/post-template.php */
$item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID ) . $args->link_after;
$item_output .= \'</a>\';
$item_output .= $args->after;
/**
* Filter a menu item\'s starting output.
*
* The menu item\'s starting output only includes $args->before, the opening <a>,
* the menu item\'s title, the closing </a>, and $args->after. Currently, there is
* no filter for modifying the opening and closing <li> for a menu item.
*
* @since 3.0.0
*
* @param string $item_output The menu item\'s starting HTML output.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu()
*/
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
$args
不是数组,而是在函数中使用类型转换创建的松散对象之一
wp_nav_menu()
:
$args = (object) $args;
.
现在怎么办?无法更改中的签名Walker_Nav_Menu
, 因为那是一个Walker
(继承是一个糟糕概念的众多原因之一)。更改子类签名将引发E_STRICT
通知。您不能只传递一个数组,因为这会破坏许多第三方Walker。引入新对象时,首先必须修复使用它们的类。可能有很多这样的案例。