johnbillion / extended-cpts

A library which provides extended functionality to WordPress custom post types and taxonomies.
GNU General Public License v2.0
979 stars 96 forks source link

Admin columns, 'function' argument #63

Closed Rayken closed 7 years ago

Rayken commented 7 years ago

So I've found the need to pass arguments to this function, therefore I'd suggest using call_user_func_array instead. Perhaps using is_callable to decide which to use? Anyhow the point is that I'm using namespaces and I'm adding custom admin columns after registering the post types and being able to provide arguments gives higher flexibility!

https://github.com/johnbillion/extended-cpts/blob/master/extended-cpts.php#L1610

Edit: oh I guess the same goes for "filters" and "sortables" but not sure, haven't looked

Edit2: on second though, maybe simply something like

if ( isset( $c[ $col ]['function'] ) ) {
    if ( count( $c[ $col ]['function'] ) > 1 ) {
        call_user_func_array( $c[ $col ]['function'][0], $c[ $col ]['function'][1] );
    }
    else {
        call_user_func( $c[ $col ]['function'] );
    }
} else if ( isset( $c[ $col ]['meta_key'] ) ) {
...

which in turn would mean I can do:

add_filter( 'ext-cpts/my-cpt/args', function( $args ) {
    $args['admin_cols']['my_custom_column'] = array(
        'title'         => __( 'Custom column name', 'textdomain' ),
        'function'      => array( array( __NAMESPACE__ . '\Foo', 'test' ), array( 'something_useful' ) )
    );

    return $args;
});

I think.

johnbillion commented 7 years ago

Thanks for the report. I've given this some thought, and I can certainly see a use case for this, but this is easy enough to solve with an anonymous closure and the use statement.

I don't want to overcomplicate a parameter which is essentially a callable and fits a widely used pattern.

Example of how you could achieve this:


add_filter( 'ext-cpts/my-cpt/args', function( $args ) {
    $something_useful = 123;
    $args['admin_cols']['my_custom_column'] = array(
        'title'         => __( 'Custom column name', 'textdomain' ),
        'function'      => function() use ( $something_useful ) {
            Foo( $something_useful );
        }
    );

    return $args;
});