Don't you hate it when a function or method in WordPress accepts its arguments as an array and you never remember the names of the arguments and the possible values for each?
$query = new WP_Query( [
'post_type' => 'post',
'category_something' => 'does this accept an integer or a string?',
'number_of_...errr'
] );
This library provides well-documented classes which represent some of the array-type parameters that are used in WordPress. Using these classes at the point where you're constructing the arguments to pass into a WordPress function means you get familiar autocompletion and intellisense in your code editor, and strict typing thanks to the behaviour of the new typed properties feature in PHP 7.4.
$args = new \Args\WP_Query;
$args->tag = 'amazing';
$args->posts_per_page = 100;
$query = new \WP_Query( $args->toArray() );
$args = new \Args\get_posts;
$args->numberposts = 25;
$args->suppress_filters = false;
$posts = get_posts( $args->toArray() );
\Args\WP_Query
for theWP_Query
class constructor\Args\get_posts
for theget_posts()
function
These classes are generated directly from the parameter hash notation in WordPress core. I'll be working on a partly automated process for creating these at some point. I need to give some more thought on how best to name these classes and how best to handle functions that accept multiple paramters where one or more is an args array.
PHP 7.4 introduced typed class properties, and these are implemented in this library where possible. If you pass a value of the wrong type to an argument that is typed, you'll get a fatal error. No more mystery bugs.
- PHP 7.4+
composer require johnbillion/args
I have a name for these array-type parameters for passing arguments. I call them Stockholm Parameters. We've gotten so used to using them that we forget what a terrible design pattern it is. This library exists to work around the immediate issue without rearchitecting the whole of WordPress.